xref: /btstack/src/classic/hfp_hf.c (revision 97d2cadb384a5febac2b6eb58bb8a7fe3da28e4d)
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 
693deb3ec6SMatthias Ringwald static const char default_hfp_hf_service_name[] = "Hands-Free unit";
703deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
713deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0;
723deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
733deb3ec6SMatthias Ringwald 
743deb3ec6SMatthias Ringwald static uint8_t hfp_indicators_nr = 0;
753deb3ec6SMatthias Ringwald static uint8_t hfp_indicators[HFP_MAX_NUM_HF_INDICATORS];
76667ec068SMatthias Ringwald static uint32_t hfp_indicators_value[HFP_MAX_NUM_HF_INDICATORS];
77667ec068SMatthias Ringwald 
78667ec068SMatthias Ringwald static uint8_t hfp_hf_speaker_gain = 9;
79667ec068SMatthias Ringwald static uint8_t hfp_hf_microphone_gain = 9;
803deb3ec6SMatthias Ringwald 
81ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_hf_callback;
823deb3ec6SMatthias Ringwald 
83ce263fc8SMatthias Ringwald static hfp_call_status_t hfp_call_status;
84ce263fc8SMatthias Ringwald static hfp_callsetup_status_t hfp_callsetup_status;
85ce263fc8SMatthias Ringwald static hfp_callheld_status_t hfp_callheld_status;
86ce263fc8SMatthias Ringwald 
87ce263fc8SMatthias Ringwald static char phone_number[25];
88ce263fc8SMatthias Ringwald 
899c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_hf_connection_context_for_acl_handle(uint16_t handle){
909c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_t it;
919c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
929c9c64c1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
939c9c64c1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
949c9c64c1SMatthias Ringwald         if (hfp_connection->acl_handle != handle)      continue;
959c9c64c1SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
969c9c64c1SMatthias Ringwald         return hfp_connection;
979c9c64c1SMatthias Ringwald     }
989c9c64c1SMatthias Ringwald     return NULL;
999c9c64c1SMatthias Ringwald }
1009c9c64c1SMatthias Ringwald 
10113839019SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
1023deb3ec6SMatthias Ringwald     if (callback == NULL){
1033deb3ec6SMatthias Ringwald         log_error("hfp_hf_register_packet_handler called with NULL callback");
1043deb3ec6SMatthias Ringwald         return;
1053deb3ec6SMatthias Ringwald     }
106ca59be51SMatthias Ringwald     hfp_hf_callback = callback;
107ca59be51SMatthias Ringwald     hfp_set_hf_callback(callback);
1083deb3ec6SMatthias Ringwald }
1093deb3ec6SMatthias Ringwald 
11013839019SMatthias 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){
111a0ffb263SMatthias Ringwald     if (!callback) return;
112a0ffb263SMatthias Ringwald     uint8_t event[31];
113a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
114a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
115a0ffb263SMatthias Ringwald     event[2] = event_subtype;
116a0ffb263SMatthias Ringwald     event[3] = status;
117a0ffb263SMatthias Ringwald     event[4] = bnip_type;
118adaba9f3SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 6) ? (int) strlen(bnip_number) : (int) sizeof(event) - 6;
119a0ffb263SMatthias Ringwald     strncpy((char*)&event[5], bnip_number, size);
120a0ffb263SMatthias Ringwald     event[5 + size] = 0;
12113839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
122a0ffb263SMatthias Ringwald }
123a0ffb263SMatthias Ringwald 
12413839019SMatthias 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){
125a0ffb263SMatthias Ringwald     if (!callback) return;
126a0ffb263SMatthias Ringwald     uint8_t event[30];
127a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
128a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
129a0ffb263SMatthias Ringwald     event[2] = event_subtype;
130a0ffb263SMatthias Ringwald     event[3] = bnip_type;
131adaba9f3SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 5) ? (int) strlen(bnip_number) : (int) sizeof(event) - 5;
132a0ffb263SMatthias Ringwald     strncpy((char*)&event[4], bnip_number, size);
133a0ffb263SMatthias Ringwald     event[4 + size] = 0;
13413839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
135a0ffb263SMatthias Ringwald }
136a0ffb263SMatthias Ringwald 
13713839019SMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(btstack_packet_handler_t callback, uint8_t clcc_idx, uint8_t clcc_dir,
138a0ffb263SMatthias Ringwald                 uint8_t clcc_status, uint8_t clcc_mpty, uint8_t bnip_type, const char * bnip_number){
139a0ffb263SMatthias Ringwald     if (!callback) return;
140a0ffb263SMatthias Ringwald     uint8_t event[35];
141a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
142a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
143a0ffb263SMatthias Ringwald     event[2] = HFP_SUBEVENT_ENHANCED_CALL_STATUS;
144a0ffb263SMatthias Ringwald     event[3] = clcc_idx;
145a0ffb263SMatthias Ringwald     event[4] = clcc_dir;
146a0ffb263SMatthias Ringwald     event[6] = clcc_status;
147a0ffb263SMatthias Ringwald     event[7] = clcc_mpty;
148a0ffb263SMatthias Ringwald     event[8] = bnip_type;
149adaba9f3SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 10) ? (int) strlen(bnip_number) : (int) sizeof(event) - 10;
150a0ffb263SMatthias Ringwald     strncpy((char*)&event[9], bnip_number, size);
151a0ffb263SMatthias Ringwald     event[9 + size] = 0;
15213839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
153a0ffb263SMatthias Ringwald }
154a0ffb263SMatthias Ringwald 
155a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
1563deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
157a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
1583deb3ec6SMatthias Ringwald     return hf && ag;
1593deb3ec6SMatthias Ringwald }
1603deb3ec6SMatthias Ringwald 
161a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
1623deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_THREE_WAY_CALLING);
163a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING);
1643deb3ec6SMatthias Ringwald     return hf && ag;
1653deb3ec6SMatthias Ringwald }
1663deb3ec6SMatthias Ringwald 
1673deb3ec6SMatthias Ringwald 
168a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
1693deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_HF_INDICATORS);
170a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS);
1713deb3ec6SMatthias Ringwald     return hf && ag;
1723deb3ec6SMatthias Ringwald }
1733deb3ec6SMatthias Ringwald 
1744f84bf36SMatthias 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){
1753deb3ec6SMatthias Ringwald     if (!name){
1763deb3ec6SMatthias Ringwald         name = default_hfp_hf_service_name;
1773deb3ec6SMatthias Ringwald     }
178235946f1SMatthias Ringwald     hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name);
1793deb3ec6SMatthias Ringwald 
1804f84bf36SMatthias Ringwald     // Construct SupportedFeatures for SDP bitmap:
1814f84bf36SMatthias Ringwald     //
1824f84bf36SMatthias Ringwald     // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
1834f84bf36SMatthias Ringwald     //  of the Bits 0 to 4 of the unsolicited result code +BRSF"
1844f84bf36SMatthias Ringwald     //
1854f84bf36SMatthias Ringwald     uint16_t sdp_features = supported_features & 0x1f;
1864f84bf36SMatthias Ringwald     if (supported_features & wide_band_speech){
1874f84bf36SMatthias Ringwald         sdp_features |= 1 << 5; // Wide band speech bit
1884f84bf36SMatthias Ringwald     }
189aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
1904f84bf36SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
191aa4dd815SMatthias Ringwald }
1923deb3ec6SMatthias Ringwald 
19389425bfcSMilanka Ringwald 
19489425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){
1953deb3ec6SMatthias Ringwald     char buffer[20];
19689425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s\r\n", cmd);
19789425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
19889425bfcSMilanka Ringwald }
19989425bfcSMilanka Ringwald 
20089425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){
20189425bfcSMilanka Ringwald     char buffer[20];
20289425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s%s\r\n", cmd, mark);
20389425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
20489425bfcSMilanka Ringwald }
20589425bfcSMilanka Ringwald 
20689425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint8_t value){
20789425bfcSMilanka Ringwald     char buffer[40];
20889425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%d\r\n", cmd, value);
2093deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2103deb3ec6SMatthias Ringwald }
2113deb3ec6SMatthias Ringwald 
2123deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){
2133deb3ec6SMatthias Ringwald     char buffer[30];
21489425bfcSMilanka Ringwald     const int size = sizeof(buffer);
21589425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS);
21689425bfcSMilanka Ringwald     offset += join(buffer+offset, size-offset, hfp_codecs, hfp_codecs_nr);
21789425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n");
2183deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2193deb3ec6SMatthias Ringwald }
2203deb3ec6SMatthias Ringwald 
2213deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){
2223deb3ec6SMatthias Ringwald     char buffer[50];
22389425bfcSMilanka Ringwald     const int size = sizeof(buffer);
22489425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS);
22589425bfcSMilanka Ringwald     offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr);
22689425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n");
2273deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2283deb3ec6SMatthias Ringwald }
2293deb3ec6SMatthias Ringwald 
2303deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){
2313deb3ec6SMatthias Ringwald     char buffer[30];
23289425bfcSMilanka Ringwald     const int size = sizeof(buffer);
23389425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR);
23489425bfcSMilanka Ringwald     offset += join(buffer+offset, size-offset, hfp_indicators, hfp_indicators_nr);
23589425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n");
2363deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2373deb3ec6SMatthias Ringwald }
2383deb3ec6SMatthias Ringwald 
23989425bfcSMilanka Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){
2403deb3ec6SMatthias Ringwald     char buffer[20];
24189425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r\n", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate);
242ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
243ce263fc8SMatthias Ringwald }
244ce263fc8SMatthias Ringwald 
245ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){
246ce263fc8SMatthias Ringwald     char buffer[40];
24789425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "%s%s;\r\n", HFP_CALL_PHONE_NUMBER, phone_number);
248ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
249ce263fc8SMatthias Ringwald }
250ce263fc8SMatthias Ringwald 
251a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){
252ce263fc8SMatthias Ringwald     char buffer[40];
25389425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "%s>%d;\r\n", HFP_CALL_PHONE_NUMBER, memory_id);
254ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
255ce263fc8SMatthias Ringwald }
256ce263fc8SMatthias Ringwald 
257f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){
25889425bfcSMilanka Ringwald     char buffer[40];
25989425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%u\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number);
260ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
261ce263fc8SMatthias Ringwald }
262ce263fc8SMatthias Ringwald 
263ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){
264ce263fc8SMatthias Ringwald     char buffer[20];
26589425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%c\r\n", HFP_TRANSMIT_DTMF_CODES, code);
266ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
267ce263fc8SMatthias Ringwald }
268ce263fc8SMatthias Ringwald 
269*97d2cadbSMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){
270*97d2cadbSMatthias Ringwald     return send_str_over_rfcomm(cid, "ATA\r\n");
271*97d2cadbSMatthias Ringwald }
272*97d2cadbSMatthias Ringwald 
27389425bfcSMilanka Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){
27489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_supported_features);
27589425bfcSMilanka Ringwald }
27689425bfcSMilanka Ringwald 
27789425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){
27889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?");
27989425bfcSMilanka Ringwald }
28089425bfcSMilanka Ringwald 
28189425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){
28289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?");
28389425bfcSMilanka Ringwald }
28489425bfcSMilanka Ringwald 
28589425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){
28689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?");
28789425bfcSMilanka Ringwald }
28889425bfcSMilanka Ringwald 
28989425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){
29089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?");
29189425bfcSMilanka Ringwald }
29289425bfcSMilanka Ringwald 
29389425bfcSMilanka Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){
29489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?");
29589425bfcSMilanka Ringwald }
29689425bfcSMilanka Ringwald 
29789425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){
29889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0");
29989425bfcSMilanka Ringwald }
30089425bfcSMilanka Ringwald 
30189425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){
30289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?");
30389425bfcSMilanka Ringwald }
30489425bfcSMilanka Ringwald 
30589425bfcSMilanka Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){
30689425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP);
30789425bfcSMilanka Ringwald }
30889425bfcSMilanka Ringwald 
30989425bfcSMilanka Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){
31089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
31189425bfcSMilanka Ringwald }
31289425bfcSMilanka Ringwald 
31389425bfcSMilanka Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){
31489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
31589425bfcSMilanka Ringwald }
31689425bfcSMilanka Ringwald 
31789425bfcSMilanka Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){
31889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate);
31989425bfcSMilanka Ringwald }
32089425bfcSMilanka Ringwald 
32189425bfcSMilanka Ringwald static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){
32289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_TURN_OFF_EC_AND_NR, activate);
32389425bfcSMilanka Ringwald }
32489425bfcSMilanka Ringwald 
32589425bfcSMilanka Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
32689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate);
32789425bfcSMilanka Ringwald }
32889425bfcSMilanka Ringwald 
32989425bfcSMilanka Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
33089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
33189425bfcSMilanka Ringwald }
33289425bfcSMilanka Ringwald 
33389425bfcSMilanka Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
33489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
33589425bfcSMilanka Ringwald }
33689425bfcSMilanka Ringwald 
33789425bfcSMilanka Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
33889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
33989425bfcSMilanka Ringwald }
34089425bfcSMilanka Ringwald 
34189425bfcSMilanka Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
34289425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER);
34389425bfcSMilanka Ringwald }
34489425bfcSMilanka Ringwald 
34589425bfcSMilanka Ringwald static int hfp_hf_send_chup(uint16_t cid){
34689425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL);
34789425bfcSMilanka Ringwald }
34889425bfcSMilanka Ringwald 
349ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){
35089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1");
351ce263fc8SMatthias Ringwald }
352ce263fc8SMatthias Ringwald 
353667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){
35489425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS);
355667ec068SMatthias Ringwald }
356667ec068SMatthias Ringwald 
35713839019SMatthias Ringwald static void hfp_emit_ag_indicator_event(btstack_packet_handler_t callback, hfp_ag_indicator_t indicator){
3583deb3ec6SMatthias Ringwald     if (!callback) return;
359a0ffb263SMatthias Ringwald     uint8_t event[5+HFP_MAX_INDICATOR_DESC_SIZE+1];
3603deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3613deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3623deb3ec6SMatthias Ringwald     event[2] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
363a0ffb263SMatthias Ringwald     event[3] = indicator.index;
364a0ffb263SMatthias Ringwald     event[4] = indicator.status;
365a0ffb263SMatthias Ringwald     strncpy((char*)&event[5], indicator.name, HFP_MAX_INDICATOR_DESC_SIZE);
366a0ffb263SMatthias Ringwald     event[5+HFP_MAX_INDICATOR_DESC_SIZE] = 0;
36713839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
3683deb3ec6SMatthias Ringwald }
3693deb3ec6SMatthias Ringwald 
37013839019SMatthias Ringwald static void hfp_emit_network_operator_event(btstack_packet_handler_t callback, hfp_network_opearator_t network_operator){
3713deb3ec6SMatthias Ringwald     if (!callback) return;
37289425bfcSMilanka Ringwald     uint8_t event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE+1];
3733deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3743deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3753deb3ec6SMatthias Ringwald     event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED;
376a0ffb263SMatthias Ringwald     event[3] = network_operator.mode;
377a0ffb263SMatthias Ringwald     event[4] = network_operator.format;
37889425bfcSMilanka Ringwald     strncpy((char*)&event[5], network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE);
37989425bfcSMilanka Ringwald     event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE] = 0;
38013839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
3813deb3ec6SMatthias Ringwald }
3823deb3ec6SMatthias Ringwald 
383a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
384a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
385a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
386aa4dd815SMatthias Ringwald     int done = 1;
3873deb3ec6SMatthias Ringwald 
388a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
3893deb3ec6SMatthias Ringwald         case HFP_EXCHANGE_SUPPORTED_FEATURES:
390d715cf51SMatthias Ringwald             hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr);
391a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
392a0ffb263SMatthias Ringwald             hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid);
3933deb3ec6SMatthias Ringwald             break;
3943deb3ec6SMatthias Ringwald         case HFP_NOTIFY_ON_CODECS:
395a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
396a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
3973deb3ec6SMatthias Ringwald             break;
3983deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS:
399a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
400a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid);
4013deb3ec6SMatthias Ringwald             break;
4023deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS_STATUS:
403a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
404a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid);
4053deb3ec6SMatthias Ringwald             break;
4063deb3ec6SMatthias Ringwald         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
407a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
408a0ffb263SMatthias Ringwald             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1);
4093deb3ec6SMatthias Ringwald             break;
4103deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_CAN_HOLD_CALL:
411a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
412a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid);
4133deb3ec6SMatthias Ringwald             break;
4143deb3ec6SMatthias Ringwald         case HFP_LIST_GENERIC_STATUS_INDICATORS:
415a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
416a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4173deb3ec6SMatthias Ringwald             break;
4183deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
419a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
420a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4213deb3ec6SMatthias Ringwald             break;
4223deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
423a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
424a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4253deb3ec6SMatthias Ringwald             break;
4263deb3ec6SMatthias Ringwald         default:
427aa4dd815SMatthias Ringwald             done = 0;
4283deb3ec6SMatthias Ringwald             break;
4293deb3ec6SMatthias Ringwald     }
4303deb3ec6SMatthias Ringwald     return done;
4313deb3ec6SMatthias Ringwald }
4323deb3ec6SMatthias Ringwald 
433ce263fc8SMatthias Ringwald 
434a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
435a0ffb263SMatthias Ringwald     if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
436a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
437ce263fc8SMatthias Ringwald 
438ce263fc8SMatthias Ringwald     int done = 0;
439a0ffb263SMatthias Ringwald     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
440a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
441ce263fc8SMatthias Ringwald         done = 1;
442a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
443ce263fc8SMatthias Ringwald         return done;
444ce263fc8SMatthias Ringwald     };
445a0ffb263SMatthias Ringwald     if (hfp_connection->change_status_update_for_individual_ag_indicators){
446a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
447ce263fc8SMatthias Ringwald         done = 1;
448a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
449a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap,
450a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_nr);
451ce263fc8SMatthias Ringwald         return done;
452ce263fc8SMatthias Ringwald     }
453ce263fc8SMatthias Ringwald 
454a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
455ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
456a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
457a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
458a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
459ce263fc8SMatthias Ringwald             return 1;
460ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
461a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
462a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
463a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
464ce263fc8SMatthias Ringwald             return 1;
465ce263fc8SMatthias Ringwald         default:
466ce263fc8SMatthias Ringwald             break;
467ce263fc8SMatthias Ringwald     }
468ce263fc8SMatthias Ringwald 
469a0ffb263SMatthias Ringwald     if (hfp_connection->enable_extended_audio_gateway_error_report){
470a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
471ce263fc8SMatthias Ringwald         done = 1;
472a0ffb263SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
473ce263fc8SMatthias Ringwald         return done;
474ce263fc8SMatthias Ringwald     }
475ce263fc8SMatthias Ringwald 
476ce263fc8SMatthias Ringwald     return done;
477ce263fc8SMatthias Ringwald }
478ce263fc8SMatthias Ringwald 
479a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
480ce263fc8SMatthias Ringwald     /* events ( == commands):
481ce263fc8SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
482ce263fc8SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
483ce263fc8SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
484ce263fc8SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
485ce263fc8SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
486ce263fc8SMatthias Ringwald     */
487ce263fc8SMatthias Ringwald 
488a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
489ce263fc8SMatthias Ringwald 
490a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
491ce263fc8SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
492a0ffb263SMatthias Ringwald             if (hfp_connection->codecs_state == HFP_CODECS_W4_AG_COMMON_CODEC) return 0;
493ce263fc8SMatthias Ringwald 
494a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
495a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
496a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
497ce263fc8SMatthias Ringwald             return 1;
498ce263fc8SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
499a0ffb263SMatthias Ringwald             hfp_connection->codec_confirmed = 0;
500a0ffb263SMatthias Ringwald             hfp_connection->suggested_codec = 0;
501a0ffb263SMatthias Ringwald             hfp_connection->negotiated_codec = 0;
502ce263fc8SMatthias Ringwald 
503a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
504a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
505a0ffb263SMatthias Ringwald             hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
506ce263fc8SMatthias Ringwald             break;
507ce263fc8SMatthias Ringwald 
5086a7f44bdSMilanka Ringwald          case HFP_CMD_AG_SUGGESTED_CODEC:{
5096a7f44bdSMilanka Ringwald             if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){
510a0ffb263SMatthias Ringwald                 hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
511a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
512a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
5130e0be203SMatthias Ringwald                 hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
5140e0be203SMatthias Ringwald                 log_info("hfp: codec confirmed: %s", hfp_connection->negotiated_codec == HFP_CODEC_MSBC ? "mSBC" : "CVSD");
515fcb08cdbSMilanka Ringwald                 hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
516ce263fc8SMatthias Ringwald             } else {
517a0ffb263SMatthias Ringwald                 hfp_connection->codec_confirmed = 0;
518a0ffb263SMatthias Ringwald                 hfp_connection->suggested_codec = 0;
519a0ffb263SMatthias Ringwald                 hfp_connection->negotiated_codec = 0;
520a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
521a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
522a0ffb263SMatthias Ringwald                 hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
523ce263fc8SMatthias Ringwald 
524ce263fc8SMatthias Ringwald             }
525ce263fc8SMatthias Ringwald             break;
5266a7f44bdSMilanka Ringwald         }
527ce263fc8SMatthias Ringwald         default:
528ce263fc8SMatthias Ringwald             break;
529ce263fc8SMatthias Ringwald     }
530ce263fc8SMatthias Ringwald     return 0;
531ce263fc8SMatthias Ringwald }
532ce263fc8SMatthias Ringwald 
533a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
534a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
535a0ffb263SMatthias Ringwald         hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0;
536ce263fc8SMatthias Ringwald 
537ce263fc8SMatthias Ringwald 
538a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){
539a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
540a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
541a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
542ce263fc8SMatthias Ringwald         return 1;
543ce263fc8SMatthias Ringwald     }
544ce263fc8SMatthias Ringwald 
545a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
546ce263fc8SMatthias Ringwald 
547ce263fc8SMatthias Ringwald     // run codecs exchange
548a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
549ce263fc8SMatthias Ringwald     if (done) return 1;
550ce263fc8SMatthias Ringwald 
551ce263fc8SMatthias Ringwald     return 0;
552ce263fc8SMatthias Ringwald }
553ce263fc8SMatthias Ringwald 
554a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
555a0ffb263SMatthias Ringwald     if (hfp_connection->hf_answer_incoming_call){
556a0ffb263SMatthias Ringwald         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
557a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 0;
558ce263fc8SMatthias Ringwald         return 1;
559ce263fc8SMatthias Ringwald     }
560ce263fc8SMatthias Ringwald     return 0;
561ce263fc8SMatthias Ringwald }
562ce263fc8SMatthias Ringwald 
563a0ffb263SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t * hfp_connection){
564a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
565724f400dSMatthias Ringwald     if (!hfp_connection->rfcomm_cid) return;
5667522e673SMatthias Ringwald 
56722387625SMatthias Ringwald     if (hfp_connection->local_role != HFP_ROLE_HF) {
56822387625SMatthias Ringwald         log_info("HFP HF%p, wrong role %u", hfp_connection, hfp_connection->local_role);
56922387625SMatthias Ringwald         return;
57022387625SMatthias Ringwald     }
57122387625SMatthias Ringwald 
572b72c4a9eSMatthias Ringwald     if (hfp_connection->hf_accept_sco && hci_can_send_command_packet_now()){
573b72c4a9eSMatthias Ringwald 
574b72c4a9eSMatthias Ringwald         hfp_connection->hf_accept_sco = 0;
5757522e673SMatthias Ringwald 
5767522e673SMatthias Ringwald         // notify about codec selection if not done already
5777522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == 0){
5787522e673SMatthias Ringwald             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
5797522e673SMatthias Ringwald         }
5807522e673SMatthias Ringwald 
5817522e673SMatthias Ringwald         // remote supported feature eSCO is set if link type is eSCO
5827522e673SMatthias Ringwald         // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms,
5837522e673SMatthias Ringwald         uint16_t max_latency;
5847522e673SMatthias Ringwald         uint8_t  retransmission_effort;
5857522e673SMatthias Ringwald         uint16_t packet_types;
5867522e673SMatthias Ringwald 
587d9f77559SMatthias Ringwald         if (hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){
5887522e673SMatthias Ringwald             max_latency = 0x000c;
5897522e673SMatthias Ringwald             retransmission_effort = 0x02;
5907522e673SMatthias Ringwald             packet_types = 0x388;
5917522e673SMatthias Ringwald         } else {
5927522e673SMatthias Ringwald             max_latency = 0xffff;
5937522e673SMatthias Ringwald             retransmission_effort = 0xff;
5947522e673SMatthias Ringwald             packet_types = 0x003f;
5957522e673SMatthias Ringwald         }
5967522e673SMatthias Ringwald 
5977522e673SMatthias Ringwald         uint16_t sco_voice_setting = hci_get_sco_voice_setting();
5987522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
5997522e673SMatthias Ringwald             sco_voice_setting = 0x0043; // Transparent data
6007522e673SMatthias Ringwald         }
6017522e673SMatthias Ringwald 
602b72c4a9eSMatthias Ringwald         log_info("HFP: sending hci_accept_connection_request, sco_voice_setting 0x%02x", sco_voice_setting);
6037522e673SMatthias Ringwald         hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency,
6047522e673SMatthias Ringwald                         sco_voice_setting, retransmission_effort, packet_types);
6057522e673SMatthias Ringwald         return;
6067522e673SMatthias Ringwald     }
6077522e673SMatthias Ringwald 
608d4dd47ffSMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
609d4dd47ffSMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
610d4dd47ffSMatthias Ringwald         return;
611d4dd47ffSMatthias Ringwald     }
612a0ffb263SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
613ce263fc8SMatthias Ringwald     if (!done){
614a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
615ce263fc8SMatthias Ringwald     }
616ce263fc8SMatthias Ringwald     if (!done){
617a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_audio_connection(hfp_connection);
618ce263fc8SMatthias Ringwald     }
619ce263fc8SMatthias Ringwald     if (!done){
620a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
621ce263fc8SMatthias Ringwald     }
622ce263fc8SMatthias Ringwald 
6231016a228SMatthias Ringwald     // don't send a new command while ok still pending
6241016a228SMatthias Ringwald     if (hfp_connection->ok_pending) return;
6251016a228SMatthias Ringwald 
626a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
627a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
628a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
629a0ffb263SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
630ce263fc8SMatthias Ringwald         return;
631ce263fc8SMatthias Ringwald     }
632ce263fc8SMatthias Ringwald 
633a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
634a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
635a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
636a0ffb263SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
637ce263fc8SMatthias Ringwald         return;
638ce263fc8SMatthias Ringwald     }
639ce263fc8SMatthias Ringwald 
640a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_calling_line_notification){
641a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_calling_line_notification = 0;
642a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
643a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
644ce263fc8SMatthias Ringwald         return;
645ce263fc8SMatthias Ringwald     }
646ce263fc8SMatthias Ringwald 
647a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_calling_line_notification){
648a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_calling_line_notification = 0;
649a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
650a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
651ce263fc8SMatthias Ringwald         return;
652ce263fc8SMatthias Ringwald     }
653ce263fc8SMatthias Ringwald 
654a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
655a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
656a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
657a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0);
658ce263fc8SMatthias Ringwald         return;
659ce263fc8SMatthias Ringwald     }
660ce263fc8SMatthias Ringwald 
661a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){
662a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0;
663a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
664a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1);
665ce263fc8SMatthias Ringwald         return;
666ce263fc8SMatthias Ringwald     }
667ce263fc8SMatthias Ringwald 
668a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_voice_recognition_notification){
669a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_voice_recognition_notification = 0;
670a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
671a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
672ce263fc8SMatthias Ringwald         return;
673ce263fc8SMatthias Ringwald     }
674ce263fc8SMatthias Ringwald 
675a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_voice_recognition_notification){
676a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_voice_recognition_notification = 0;
677a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
678a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
679ce263fc8SMatthias Ringwald         return;
680ce263fc8SMatthias Ringwald     }
681ce263fc8SMatthias Ringwald 
682ce263fc8SMatthias Ringwald 
683a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_call_waiting_notification){
684a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_call_waiting_notification = 0;
685a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
686a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
687ce263fc8SMatthias Ringwald         return;
688ce263fc8SMatthias Ringwald     }
689ce263fc8SMatthias Ringwald 
690a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_call_waiting_notification){
691a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_call_waiting_notification = 0;
692a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
693a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
694ce263fc8SMatthias Ringwald         return;
695ce263fc8SMatthias Ringwald     }
696ce263fc8SMatthias Ringwald 
697a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_outgoing_call){
698a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_outgoing_call = 0;
699a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
700a0ffb263SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
701ce263fc8SMatthias Ringwald         return;
702ce263fc8SMatthias Ringwald     }
703ce263fc8SMatthias Ringwald 
704a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_memory_dialing){
705a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_memory_dialing = 0;
706a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
707a0ffb263SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
708ce263fc8SMatthias Ringwald         return;
709ce263fc8SMatthias Ringwald     }
710ce263fc8SMatthias Ringwald 
711a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_redial_last_number){
712a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_redial_last_number = 0;
713a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
714a0ffb263SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
715ce263fc8SMatthias Ringwald         return;
716ce263fc8SMatthias Ringwald     }
717ce263fc8SMatthias Ringwald 
718a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chup){
719a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 0;
720a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
721a0ffb263SMatthias Ringwald         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
722ce263fc8SMatthias Ringwald         return;
723ce263fc8SMatthias Ringwald     }
724ce263fc8SMatthias Ringwald 
725a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_0){
726a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 0;
727a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
728a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
729ce263fc8SMatthias Ringwald         return;
730ce263fc8SMatthias Ringwald     }
731ce263fc8SMatthias Ringwald 
732a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_1){
733a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 0;
734a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
735a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
736ce263fc8SMatthias Ringwald         return;
737ce263fc8SMatthias Ringwald     }
738ce263fc8SMatthias Ringwald 
739a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_2){
740a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 0;
741a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
742a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
743ce263fc8SMatthias Ringwald         return;
744ce263fc8SMatthias Ringwald     }
745ce263fc8SMatthias Ringwald 
746a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_3){
747a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 0;
748a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
749a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
750ce263fc8SMatthias Ringwald         return;
751ce263fc8SMatthias Ringwald     }
752ce263fc8SMatthias Ringwald 
753a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_4){
754a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 0;
755a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
756a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
757ce263fc8SMatthias Ringwald         return;
758ce263fc8SMatthias Ringwald     }
759ce263fc8SMatthias Ringwald 
760a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_x){
761a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 0;
762a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
763a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
764667ec068SMatthias Ringwald         return;
765667ec068SMatthias Ringwald     }
766667ec068SMatthias Ringwald 
767a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_dtmf_code){
768a0ffb263SMatthias Ringwald         char code = hfp_connection->hf_send_dtmf_code;
769a0ffb263SMatthias Ringwald         hfp_connection->hf_send_dtmf_code = 0;
770a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
771a0ffb263SMatthias Ringwald         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
772ce263fc8SMatthias Ringwald         return;
773ce263fc8SMatthias Ringwald     }
774ce263fc8SMatthias Ringwald 
775a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_binp){
776a0ffb263SMatthias Ringwald         hfp_connection->hf_send_binp = 0;
777a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
778a0ffb263SMatthias Ringwald         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
779ce263fc8SMatthias Ringwald         return;
780ce263fc8SMatthias Ringwald     }
781ce263fc8SMatthias Ringwald 
782a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_clcc){
783a0ffb263SMatthias Ringwald         hfp_connection->hf_send_clcc = 0;
784a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
785a0ffb263SMatthias Ringwald         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
786667ec068SMatthias Ringwald         return;
787667ec068SMatthias Ringwald     }
788667ec068SMatthias Ringwald 
789a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_rrh){
790a0ffb263SMatthias Ringwald         hfp_connection->hf_send_rrh = 0;
791667ec068SMatthias Ringwald         char buffer[20];
792a0ffb263SMatthias Ringwald         switch (hfp_connection->hf_send_rrh_command){
793667ec068SMatthias Ringwald             case '?':
794667ec068SMatthias Ringwald                 sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD);
795a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
796667ec068SMatthias Ringwald                 return;
797667ec068SMatthias Ringwald             case '0':
798667ec068SMatthias Ringwald             case '1':
799667ec068SMatthias Ringwald             case '2':
800a0ffb263SMatthias Ringwald                 sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, hfp_connection->hf_send_rrh_command);
801a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
802667ec068SMatthias Ringwald                 return;
803667ec068SMatthias Ringwald             default:
804667ec068SMatthias Ringwald                 break;
805667ec068SMatthias Ringwald         }
806667ec068SMatthias Ringwald         return;
807667ec068SMatthias Ringwald     }
808667ec068SMatthias Ringwald 
809a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_cnum){
810a0ffb263SMatthias Ringwald         hfp_connection->hf_send_cnum = 0;
811667ec068SMatthias Ringwald         char buffer[20];
812667ec068SMatthias Ringwald         sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION);
813a0ffb263SMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
814667ec068SMatthias Ringwald         return;
815667ec068SMatthias Ringwald     }
816667ec068SMatthias Ringwald 
817667ec068SMatthias Ringwald     // update HF indicators
818a0ffb263SMatthias Ringwald     if (hfp_connection->generic_status_update_bitmap){
819667ec068SMatthias Ringwald         int i;
820667ec068SMatthias Ringwald         for (i=0;i<hfp_indicators_nr;i++){
821a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
822a0ffb263SMatthias Ringwald                 if (hfp_connection->generic_status_indicators[i].state){
823a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
824a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
825667ec068SMatthias Ringwald                     char buffer[30];
826ecb7d461SMatthias Ringwald                     sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], (unsigned int) hfp_indicators_value[i]);
827a0ffb263SMatthias Ringwald                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
828667ec068SMatthias Ringwald                 } else {
82960ebb071SMilanka Ringwald                     log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]);
830667ec068SMatthias Ringwald                 }
831667ec068SMatthias Ringwald                 return;
832667ec068SMatthias Ringwald             }
833667ec068SMatthias Ringwald         }
834667ec068SMatthias Ringwald     }
835667ec068SMatthias Ringwald 
836ce263fc8SMatthias Ringwald     if (done) return;
837ce263fc8SMatthias Ringwald     // deal with disconnect
838a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
839ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
840a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
841a0ffb263SMatthias Ringwald             rfcomm_disconnect(hfp_connection->rfcomm_cid);
842ce263fc8SMatthias Ringwald             break;
843ce263fc8SMatthias Ringwald 
844ce263fc8SMatthias Ringwald         default:
845ce263fc8SMatthias Ringwald             break;
846ce263fc8SMatthias Ringwald     }
847ce263fc8SMatthias Ringwald }
848ce263fc8SMatthias Ringwald 
849a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
850a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
8516a7f44bdSMilanka Ringwald 
852ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
8537522e673SMatthias Ringwald 
854667ec068SMatthias Ringwald     // restore volume settings
855a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
856a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
857ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
858a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
859a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
860ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
861667ec068SMatthias Ringwald     // enable all indicators
862667ec068SMatthias Ringwald     int i;
863667ec068SMatthias Ringwald     for (i=0;i<hfp_indicators_nr;i++){
864a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i];
865a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].state = 1;
866667ec068SMatthias Ringwald     }
867ce263fc8SMatthias Ringwald }
868ce263fc8SMatthias Ringwald 
869a0ffb263SMatthias Ringwald static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){
870a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 0;
871a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
8723deb3ec6SMatthias Ringwald         case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
873a0ffb263SMatthias Ringwald             if (has_codec_negotiation_feature(hfp_connection)){
874a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_NOTIFY_ON_CODECS;
8753deb3ec6SMatthias Ringwald                 break;
8763deb3ec6SMatthias Ringwald             }
877a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
8783deb3ec6SMatthias Ringwald             break;
8793deb3ec6SMatthias Ringwald 
8803deb3ec6SMatthias Ringwald         case HFP_W4_NOTIFY_ON_CODECS:
881a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
8823deb3ec6SMatthias Ringwald             break;
8833deb3ec6SMatthias Ringwald 
8843deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS:
885a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
8863deb3ec6SMatthias Ringwald             break;
8873deb3ec6SMatthias Ringwald 
8883deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
889a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
8903deb3ec6SMatthias Ringwald             break;
8913deb3ec6SMatthias Ringwald 
8923deb3ec6SMatthias Ringwald         case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
893a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
894a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
8953deb3ec6SMatthias Ringwald                 break;
8963deb3ec6SMatthias Ringwald             }
897a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
898a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
8993deb3ec6SMatthias Ringwald                 break;
9003deb3ec6SMatthias Ringwald             }
901a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9023deb3ec6SMatthias Ringwald             break;
9033deb3ec6SMatthias Ringwald 
9043deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
905a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
906a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
9073deb3ec6SMatthias Ringwald                 break;
9083deb3ec6SMatthias Ringwald             }
909a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9103deb3ec6SMatthias Ringwald             break;
9113deb3ec6SMatthias Ringwald 
9123deb3ec6SMatthias Ringwald         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
913a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
9143deb3ec6SMatthias Ringwald             break;
9153deb3ec6SMatthias Ringwald 
9163deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
917a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
9183deb3ec6SMatthias Ringwald             break;
9193deb3ec6SMatthias Ringwald 
9203deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
921a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9223deb3ec6SMatthias Ringwald             break;
923ce263fc8SMatthias Ringwald         case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
924a0ffb263SMatthias Ringwald             if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
925a0ffb263SMatthias Ringwald                 hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
926ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
927ce263fc8SMatthias Ringwald                 break;
928ce263fc8SMatthias Ringwald             }
9293deb3ec6SMatthias Ringwald 
930a0ffb263SMatthias Ringwald             if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
931a0ffb263SMatthias Ringwald                 hfp_connection->change_status_update_for_individual_ag_indicators = 0;
932ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
933ce263fc8SMatthias Ringwald                 break;
9343deb3ec6SMatthias Ringwald             }
9353deb3ec6SMatthias Ringwald 
936a0ffb263SMatthias Ringwald             switch (hfp_connection->hf_query_operator_state){
937ce263fc8SMatthias Ringwald                 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
93860ebb071SMilanka Ringwald                     // printf("Format set, querying name\n");
939a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
940ce263fc8SMatthias Ringwald                     break;
941ce263fc8SMatthias Ringwald                 case HPF_HF_QUERY_OPERATOR_W4_RESULT:
942a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
943ca59be51SMatthias Ringwald                     hfp_emit_network_operator_event(hfp_hf_callback, hfp_connection->network_operator);
944ce263fc8SMatthias Ringwald                     break;
945ce263fc8SMatthias Ringwald                 default:
946ce263fc8SMatthias Ringwald                     break;
9473deb3ec6SMatthias Ringwald             }
948ce263fc8SMatthias Ringwald 
949a0ffb263SMatthias Ringwald             if (hfp_connection->enable_extended_audio_gateway_error_report){
950a0ffb263SMatthias Ringwald                 hfp_connection->enable_extended_audio_gateway_error_report = 0;
951ce263fc8SMatthias Ringwald                 break;
9523deb3ec6SMatthias Ringwald             }
9533deb3ec6SMatthias Ringwald 
954a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
955aa4dd815SMatthias Ringwald                 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
956a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
9573deb3ec6SMatthias Ringwald                     break;
958ce263fc8SMatthias Ringwald                 case HFP_CODECS_HF_CONFIRMED_CODEC:
959a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
960ce263fc8SMatthias Ringwald                     break;
9613deb3ec6SMatthias Ringwald                 default:
9623deb3ec6SMatthias Ringwald                     break;
9633deb3ec6SMatthias Ringwald             }
9643deb3ec6SMatthias Ringwald             break;
9653deb3ec6SMatthias Ringwald         default:
9663deb3ec6SMatthias Ringwald             break;
9673deb3ec6SMatthias Ringwald     }
9683deb3ec6SMatthias Ringwald 
9693deb3ec6SMatthias Ringwald     // done
970a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
9713deb3ec6SMatthias Ringwald }
9723deb3ec6SMatthias Ringwald 
9733deb3ec6SMatthias Ringwald 
974e9c22d4eSMatthias Ringwald static void hfp_hf_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
9758a46ec40SMatthias Ringwald     UNUSED(packet_type);    // ok: only called with RFCOMM_DATA_PACKET
9768a46ec40SMatthias Ringwald 
9778a46ec40SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
9788a46ec40SMatthias Ringwald     if (size < 1) return;
9799ec2630cSMatthias Ringwald 
980a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
981a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
9823deb3ec6SMatthias Ringwald 
9838a46ec40SMatthias Ringwald     // temp overwrite last byte (most likely \n for log_info)
9841e35c04dSMatthias Ringwald     char last_char = packet[size-1];
9851e35c04dSMatthias Ringwald     packet[size-1] = 0;
9861e35c04dSMatthias Ringwald     log_info("HFP_RX %s", packet);
9871e35c04dSMatthias Ringwald     packet[size-1] = last_char;
9881e35c04dSMatthias Ringwald 
9898a46ec40SMatthias Ringwald     // process messages byte-wise
990667ec068SMatthias Ringwald     int pos, i, value;
9913deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
992a0ffb263SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 1);
993ce263fc8SMatthias Ringwald     }
9943deb3ec6SMatthias Ringwald 
995a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
996667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
997a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
998a0ffb263SMatthias Ringwald             // printf("Subscriber Number: number %s, type %u\n", hfp_connection->bnip_number, hfp_connection->bnip_type);
999ca59be51SMatthias Ringwald             hfp_hf_emit_subscriber_information(hfp_hf_callback, HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION, 0, hfp_connection->bnip_type, hfp_connection->bnip_number);
1000667ec068SMatthias Ringwald             break;
1001667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
1002a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1003a0ffb263SMatthias Ringwald             // printf("Response and Hold status: %s\n", hfp_connection->line_buffer);
1004ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
1005667ec068SMatthias Ringwald             break;
1006667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1007a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1008a0ffb263SMatthias Ringwald             // printf("Enhanced Call Status: idx %u, dir %u, status %u, mpty %u, number %s, type %u\n",
1009a0ffb263SMatthias Ringwald             //      hfp_connection->clcc_idx, hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty,
1010a0ffb263SMatthias Ringwald             //      hfp_connection->bnip_number, hfp_connection->bnip_type);
1011ca59be51SMatthias Ringwald             hfp_hf_emit_enhanced_call_status(hfp_hf_callback, hfp_connection->clcc_idx,
1012a0ffb263SMatthias Ringwald                 hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty,
1013a0ffb263SMatthias Ringwald                 hfp_connection->bnip_type, hfp_connection->bnip_number);
1014667ec068SMatthias Ringwald             break;
1015ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1016a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
10172308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1018667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
1019ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1020ce263fc8SMatthias Ringwald             break;
1021ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1022a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
10232308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1024667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
1025ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1026ce263fc8SMatthias Ringwald             break;
1027ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1028a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1029ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1030a0ffb263SMatthias Ringwald             break;
1031a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1032a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1033ca59be51SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1034a0ffb263SMatthias Ringwald             break;
1035a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1036a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1037ca59be51SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1038ce263fc8SMatthias Ringwald             break;
1039ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1040a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 0;
1041a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1042a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
1043ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1044ce263fc8SMatthias Ringwald             break;
1045ce263fc8SMatthias Ringwald         case HFP_CMD_ERROR:
1046a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 0;
1047a0ffb263SMatthias Ringwald             hfp_reset_context_flags(hfp_connection);
1048a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1049ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1);
1050ce263fc8SMatthias Ringwald             break;
1051ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
1052a0ffb263SMatthias Ringwald             hfp_hf_switch_on_ok(hfp_connection);
1053ce263fc8SMatthias Ringwald             break;
1054ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
1055ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
1056ce263fc8SMatthias Ringwald             break;
1057ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1058a0ffb263SMatthias Ringwald             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1059a0ffb263SMatthias Ringwald                 if (hfp_connection->ag_indicators[i].status_changed) {
1060a0ffb263SMatthias Ringwald                     if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
1061a0ffb263SMatthias Ringwald                         hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
1062a0ffb263SMatthias Ringwald                     } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
1063a0ffb263SMatthias Ringwald                         hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
1064d5ff3b26SMatthias Ringwald                         // avoid set but not used warning
1065d5ff3b26SMatthias Ringwald                         (void) hfp_callheld_status;
1066a0ffb263SMatthias Ringwald                     } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
1067a0ffb263SMatthias Ringwald                         hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
1068ce263fc8SMatthias Ringwald                     }
1069a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[i].status_changed = 0;
1070ca59be51SMatthias Ringwald                     hfp_emit_ag_indicator_event(hfp_hf_callback, hfp_connection->ag_indicators[i]);
10713deb3ec6SMatthias Ringwald                     break;
10723deb3ec6SMatthias Ringwald                 }
10733deb3ec6SMatthias Ringwald             }
1074ce263fc8SMatthias Ringwald             break;
1075ce263fc8SMatthias Ringwald         default:
1076ce263fc8SMatthias Ringwald             break;
10773deb3ec6SMatthias Ringwald     }
1078a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
10793deb3ec6SMatthias Ringwald }
10803deb3ec6SMatthias Ringwald 
10810cb5b971SMatthias Ringwald static void hfp_run(void){
1082665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1083665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1084665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1085a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
108622387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
1087a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
10883deb3ec6SMatthias Ringwald     }
10893deb3ec6SMatthias Ringwald }
10903deb3ec6SMatthias Ringwald 
1091e9c22d4eSMatthias Ringwald static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
10923deb3ec6SMatthias Ringwald     switch (packet_type){
10933deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
1094e9c22d4eSMatthias Ringwald             hfp_hf_handle_rfcomm_event(packet_type, channel, packet, size);
10953deb3ec6SMatthias Ringwald             break;
10963deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1097d4dd47ffSMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
1098d4dd47ffSMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
1099d4dd47ffSMatthias Ringwald                 hfp_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
1100d4dd47ffSMatthias Ringwald                 return;
1101d4dd47ffSMatthias Ringwald             }
1102323d3000SMatthias Ringwald             hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1103202c8a4cSMatthias Ringwald             break;
11043deb3ec6SMatthias Ringwald         default:
11053deb3ec6SMatthias Ringwald             break;
11063deb3ec6SMatthias Ringwald     }
11073deb3ec6SMatthias Ringwald     hfp_run();
11083deb3ec6SMatthias Ringwald }
11093deb3ec6SMatthias Ringwald 
1110a0ffb263SMatthias Ringwald void hfp_hf_init(uint16_t rfcomm_channel_nr){
1111520c92d5SMatthias Ringwald     hfp_init();
1112d63c37a1SMatthias Ringwald 
1113e9c22d4eSMatthias Ringwald     rfcomm_register_service(rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
1114e9c22d4eSMatthias Ringwald     hfp_set_hf_rfcomm_packet_handler(&rfcomm_packet_handler);
11151b005648SMatthias Ringwald     hfp_set_hf_run_for_context(hfp_run_for_context);
1116d68dcce1SMatthias Ringwald 
1117a0ffb263SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1118a0ffb263SMatthias Ringwald     hfp_codecs_nr = 0;
1119a0ffb263SMatthias Ringwald     hfp_indicators_nr = 0;
1120a0ffb263SMatthias Ringwald     hfp_hf_speaker_gain = 9;
1121a0ffb263SMatthias Ringwald     hfp_hf_microphone_gain = 9;
1122a0ffb263SMatthias Ringwald }
1123a0ffb263SMatthias Ringwald 
1124a0ffb263SMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, uint8_t * codecs){
11253deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
1126a0ffb263SMatthias Ringwald         log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
11273deb3ec6SMatthias Ringwald         return;
11283deb3ec6SMatthias Ringwald     }
11293deb3ec6SMatthias Ringwald 
11303deb3ec6SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
11313deb3ec6SMatthias Ringwald     int i;
11323deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
11333deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
11343deb3ec6SMatthias Ringwald     }
11353deb3ec6SMatthias Ringwald }
11363deb3ec6SMatthias Ringwald 
1137a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){
11383deb3ec6SMatthias Ringwald     hfp_supported_features = supported_features;
1139a0ffb263SMatthias Ringwald }
11403deb3ec6SMatthias Ringwald 
1141a0ffb263SMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, uint16_t * indicators){
11423deb3ec6SMatthias Ringwald     hfp_indicators_nr = indicators_nr;
11433deb3ec6SMatthias Ringwald     int i;
1144a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
11453deb3ec6SMatthias Ringwald         hfp_indicators[i] = indicators[i];
11463deb3ec6SMatthias Ringwald     }
11473deb3ec6SMatthias Ringwald }
11483deb3ec6SMatthias Ringwald 
11493deb3ec6SMatthias Ringwald void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
1150323d3000SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
11513deb3ec6SMatthias Ringwald }
11523deb3ec6SMatthias Ringwald 
1153c8626498SMilanka Ringwald void hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
11549c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1155a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1156a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1157a33eb0c4SMilanka Ringwald         return;
1158a33eb0c4SMilanka Ringwald     }
1159a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
1160a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11613deb3ec6SMatthias Ringwald }
11623deb3ec6SMatthias Ringwald 
1163c8626498SMilanka Ringwald static void hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
11649c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1165a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1166a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
11673deb3ec6SMatthias Ringwald         return;
11683deb3ec6SMatthias Ringwald     }
1169a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = enable;
1170a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11713deb3ec6SMatthias Ringwald }
11723deb3ec6SMatthias Ringwald 
1173c8626498SMilanka Ringwald void hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1174c8626498SMilanka Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1175ce263fc8SMatthias Ringwald }
1176ce263fc8SMatthias Ringwald 
1177c8626498SMilanka Ringwald void hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1178c8626498SMilanka Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1179ce263fc8SMatthias Ringwald }
1180ce263fc8SMatthias Ringwald 
11813deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
1182c8626498SMilanka Ringwald void hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
11839c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1184a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1185a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
11863deb3ec6SMatthias Ringwald         return;
11873deb3ec6SMatthias Ringwald     }
1188a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1189a0ffb263SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
1190a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11913deb3ec6SMatthias Ringwald }
11923deb3ec6SMatthias Ringwald 
1193c8626498SMilanka Ringwald void hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
11949c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1195a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1196a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
11973deb3ec6SMatthias Ringwald         return;
11983deb3ec6SMatthias Ringwald     }
1199a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
1200ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1201a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1202ce263fc8SMatthias Ringwald             break;
1203ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1204a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1205ce263fc8SMatthias Ringwald             break;
1206ce263fc8SMatthias Ringwald         default:
1207ce263fc8SMatthias Ringwald             break;
1208ce263fc8SMatthias Ringwald     }
1209a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12103deb3ec6SMatthias Ringwald }
12113deb3ec6SMatthias Ringwald 
1212c8626498SMilanka Ringwald static void hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
12139c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1214a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1215a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
12163deb3ec6SMatthias Ringwald         return;
12173deb3ec6SMatthias Ringwald     }
1218a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = enable;
1219a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12203deb3ec6SMatthias Ringwald }
12213deb3ec6SMatthias Ringwald 
1222ce263fc8SMatthias Ringwald 
1223c8626498SMilanka Ringwald void hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1224c8626498SMilanka Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1225ce263fc8SMatthias Ringwald }
1226ce263fc8SMatthias Ringwald 
1227c8626498SMilanka Ringwald void hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1228c8626498SMilanka Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1229ce263fc8SMatthias Ringwald }
1230ce263fc8SMatthias Ringwald 
1231ce263fc8SMatthias Ringwald 
1232c8626498SMilanka Ringwald void hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
12339c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1234a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1235a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1236a33eb0c4SMilanka Ringwald         return;
1237a33eb0c4SMilanka Ringwald     }
1238a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
1239ce263fc8SMatthias Ringwald 
1240a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
1241a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
12423deb3ec6SMatthias Ringwald 
1243a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
1244ce263fc8SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults");
1245a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1246a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 1;
1247ce263fc8SMatthias Ringwald     } else {
1248a0ffb263SMatthias Ringwald         switch (hfp_connection->codecs_state){
1249aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1250aa4dd815SMatthias Ringwald                 break;
1251aa4dd815SMatthias Ringwald             default:
1252a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP;
1253aa4dd815SMatthias Ringwald                 break;
12543deb3ec6SMatthias Ringwald         }
1255ce263fc8SMatthias Ringwald     }
1256ce263fc8SMatthias Ringwald 
1257a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12583deb3ec6SMatthias Ringwald }
12593deb3ec6SMatthias Ringwald 
1260c8626498SMilanka Ringwald void hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
12619c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1262a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1263a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1264a33eb0c4SMilanka Ringwald         return;
1265a33eb0c4SMilanka Ringwald     }
1266a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
1267a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12683deb3ec6SMatthias Ringwald }
12693deb3ec6SMatthias Ringwald 
1270c8626498SMilanka Ringwald void hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
12719c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1272a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1273a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1274a33eb0c4SMilanka Ringwald         return;
1275a33eb0c4SMilanka Ringwald     }
1276ce263fc8SMatthias Ringwald 
1277ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1278a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 1;
1279a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1280ce263fc8SMatthias Ringwald     } else {
1281ce263fc8SMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
1282ce263fc8SMatthias Ringwald     }
1283ce263fc8SMatthias Ringwald }
1284ce263fc8SMatthias Ringwald 
1285c8626498SMilanka Ringwald void hfp_hf_terminate_call(hci_con_handle_t acl_handle){
12869c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1287a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1288a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1289a33eb0c4SMilanka Ringwald         return;
1290a33eb0c4SMilanka Ringwald     }
1291a0ffb263SMatthias Ringwald     hfp_connection->hf_send_chup = 1;
1292a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1293ce263fc8SMatthias Ringwald }
1294ce263fc8SMatthias Ringwald 
1295c8626498SMilanka Ringwald void hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){
12969c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1297a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1298a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1299a33eb0c4SMilanka Ringwald         return;
1300a33eb0c4SMilanka Ringwald     }
1301ce263fc8SMatthias Ringwald 
1302ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1303a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 1;
1304a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1305ce263fc8SMatthias Ringwald     }
1306ce263fc8SMatthias Ringwald }
1307ce263fc8SMatthias Ringwald 
1308c8626498SMilanka Ringwald void hfp_hf_user_busy(hci_con_handle_t acl_handle){
13099c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1310a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1311a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1312a33eb0c4SMilanka Ringwald         return;
1313a33eb0c4SMilanka Ringwald     }
1314ce263fc8SMatthias Ringwald 
1315ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1316a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 1;
1317a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1318ce263fc8SMatthias Ringwald     }
1319ce263fc8SMatthias Ringwald }
1320ce263fc8SMatthias Ringwald 
1321c8626498SMilanka Ringwald void hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){
13229c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1323a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1324a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1325a33eb0c4SMilanka Ringwald         return;
1326a33eb0c4SMilanka Ringwald     }
1327ce263fc8SMatthias Ringwald 
1328ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1329ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1330a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 1;
1331a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1332ce263fc8SMatthias Ringwald     }
1333ce263fc8SMatthias Ringwald }
1334ce263fc8SMatthias Ringwald 
1335c8626498SMilanka Ringwald void hfp_hf_swap_calls(hci_con_handle_t acl_handle){
13369c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1337a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1338a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1339a33eb0c4SMilanka Ringwald         return;
1340a33eb0c4SMilanka Ringwald     }
1341ce263fc8SMatthias Ringwald 
1342ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1343ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1344a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 1;
1345a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1346ce263fc8SMatthias Ringwald     }
1347ce263fc8SMatthias Ringwald }
1348ce263fc8SMatthias Ringwald 
1349c8626498SMilanka Ringwald void hfp_hf_join_held_call(hci_con_handle_t acl_handle){
13509c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1351a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1352a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1353a33eb0c4SMilanka Ringwald         return;
1354a33eb0c4SMilanka Ringwald     }
1355ce263fc8SMatthias Ringwald 
1356ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1357ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1358a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 1;
1359a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1360ce263fc8SMatthias Ringwald     }
1361ce263fc8SMatthias Ringwald }
1362ce263fc8SMatthias Ringwald 
1363c8626498SMilanka Ringwald void hfp_hf_connect_calls(hci_con_handle_t acl_handle){
13649c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1365a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1366a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1367a33eb0c4SMilanka Ringwald         return;
1368a33eb0c4SMilanka Ringwald     }
1369ce263fc8SMatthias Ringwald 
1370ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1371ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1372a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 1;
1373a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1374ce263fc8SMatthias Ringwald     }
1375ce263fc8SMatthias Ringwald }
1376ce263fc8SMatthias Ringwald 
1377c8626498SMilanka Ringwald void hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){
13789c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1379a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1380a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1381a33eb0c4SMilanka Ringwald         return;
1382a33eb0c4SMilanka Ringwald     }
1383667ec068SMatthias Ringwald 
1384667ec068SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1385667ec068SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1386a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1387a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 10 + index;
1388a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1389667ec068SMatthias Ringwald     }
1390667ec068SMatthias Ringwald }
1391667ec068SMatthias Ringwald 
1392c8626498SMilanka Ringwald void hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
13939c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1394a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1395a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1396a33eb0c4SMilanka Ringwald         return;
1397a33eb0c4SMilanka Ringwald     }
1398667ec068SMatthias Ringwald 
1399667ec068SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1400667ec068SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1401a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1402a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 20 + index;
1403a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1404667ec068SMatthias Ringwald     }
1405667ec068SMatthias Ringwald }
1406ce263fc8SMatthias Ringwald 
1407c8626498SMilanka Ringwald void hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
14089c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1409a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1410a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1411a33eb0c4SMilanka Ringwald         return;
1412a33eb0c4SMilanka Ringwald     }
1413ce263fc8SMatthias Ringwald 
1414a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_outgoing_call = 1;
1415ce263fc8SMatthias Ringwald     snprintf(phone_number, sizeof(phone_number), "%s", number);
1416a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1417ce263fc8SMatthias Ringwald }
1418ce263fc8SMatthias Ringwald 
1419c8626498SMilanka Ringwald void hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
14209c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1421a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1422a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1423a33eb0c4SMilanka Ringwald         return;
1424a33eb0c4SMilanka Ringwald     }
1425ce263fc8SMatthias Ringwald 
1426a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_memory_dialing = 1;
1427a0ffb263SMatthias Ringwald     hfp_connection->memory_id = memory_id;
1428a0ffb263SMatthias Ringwald 
1429a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1430ce263fc8SMatthias Ringwald }
1431ce263fc8SMatthias Ringwald 
1432c8626498SMilanka Ringwald void hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
14339c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1434a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1435a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1436a33eb0c4SMilanka Ringwald         return;
1437a33eb0c4SMilanka Ringwald     }
1438ce263fc8SMatthias Ringwald 
1439a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_redial_last_number = 1;
1440a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1441ce263fc8SMatthias Ringwald }
1442ce263fc8SMatthias Ringwald 
1443c8626498SMilanka Ringwald void hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
14449c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1445a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1446a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1447a33eb0c4SMilanka Ringwald         return;
1448a33eb0c4SMilanka Ringwald     }
1449ce263fc8SMatthias Ringwald 
1450a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_call_waiting_notification = 1;
1451a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1452ce263fc8SMatthias Ringwald }
1453ce263fc8SMatthias Ringwald 
1454ce263fc8SMatthias Ringwald 
1455c8626498SMilanka Ringwald void hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
14569c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1457a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1458a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1459a33eb0c4SMilanka Ringwald         return;
1460a33eb0c4SMilanka Ringwald     }
1461ce263fc8SMatthias Ringwald 
1462a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_call_waiting_notification = 1;
1463a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1464ce263fc8SMatthias Ringwald }
1465ce263fc8SMatthias Ringwald 
1466ce263fc8SMatthias Ringwald 
1467c8626498SMilanka Ringwald void hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){
14689c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1469a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1470a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1471a33eb0c4SMilanka Ringwald         return;
1472a33eb0c4SMilanka Ringwald     }
1473ce263fc8SMatthias Ringwald 
1474a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_calling_line_notification = 1;
1475a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1476ce263fc8SMatthias Ringwald }
1477ce263fc8SMatthias Ringwald 
1478c8626498SMilanka Ringwald void hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
14799c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1480a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1481a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1482a33eb0c4SMilanka Ringwald         return;
1483a33eb0c4SMilanka Ringwald     }
1484ce263fc8SMatthias Ringwald 
1485a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_calling_line_notification = 1;
1486a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1487ce263fc8SMatthias Ringwald }
1488ce263fc8SMatthias Ringwald 
1489ce263fc8SMatthias Ringwald 
1490c8626498SMilanka Ringwald void hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
14919c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1492a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1493a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1494a33eb0c4SMilanka Ringwald         return;
1495a33eb0c4SMilanka Ringwald     }
1496ce263fc8SMatthias Ringwald 
1497a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1;
1498a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1499ce263fc8SMatthias Ringwald }
1500ce263fc8SMatthias Ringwald 
1501c8626498SMilanka Ringwald void hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
15029c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1503a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1504a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1505a33eb0c4SMilanka Ringwald         return;
1506a33eb0c4SMilanka Ringwald     }
1507ce263fc8SMatthias Ringwald 
1508a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
1509a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1510ce263fc8SMatthias Ringwald }
1511ce263fc8SMatthias Ringwald 
1512c8626498SMilanka Ringwald void hfp_hf_activate_voice_recognition_notification(hci_con_handle_t acl_handle){
15139c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1514a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1515a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1516a33eb0c4SMilanka Ringwald         return;
1517a33eb0c4SMilanka Ringwald     }
1518ce263fc8SMatthias Ringwald 
1519a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_voice_recognition_notification = 1;
1520a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1521ce263fc8SMatthias Ringwald }
1522ce263fc8SMatthias Ringwald 
1523c8626498SMilanka Ringwald void hfp_hf_deactivate_voice_recognition_notification(hci_con_handle_t acl_handle){
15249c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1525a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1526a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1527a33eb0c4SMilanka Ringwald         return;
1528a33eb0c4SMilanka Ringwald     }
1529ce263fc8SMatthias Ringwald 
1530a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_voice_recognition_notification = 1;
1531a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1532ce263fc8SMatthias Ringwald }
1533ce263fc8SMatthias Ringwald 
1534c8626498SMilanka Ringwald void hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
15359c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1536a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1537a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1538a33eb0c4SMilanka Ringwald         return;
1539a33eb0c4SMilanka Ringwald     }
1540c8626498SMilanka Ringwald 
1541a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain == gain) return;
1542a0ffb263SMatthias Ringwald     if (gain < 0 || gain > 15){
1543a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1544a0ffb263SMatthias Ringwald         return;
1545a0ffb263SMatthias Ringwald     }
1546a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = gain;
1547a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
1548a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1549ce263fc8SMatthias Ringwald }
1550ce263fc8SMatthias Ringwald 
1551c8626498SMilanka Ringwald void hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
15529c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1553a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1554a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1555a33eb0c4SMilanka Ringwald         return;
1556a33eb0c4SMilanka Ringwald     }
1557c8626498SMilanka Ringwald 
1558a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain == gain) return;
1559a0ffb263SMatthias Ringwald     if (gain < 0 || gain > 15){
1560a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1561a0ffb263SMatthias Ringwald         return;
1562a0ffb263SMatthias Ringwald     }
1563a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = gain;
1564a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
1565a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1566ce263fc8SMatthias Ringwald }
1567ce263fc8SMatthias Ringwald 
1568c8626498SMilanka Ringwald void hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
15699c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1570a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1571a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1572a33eb0c4SMilanka Ringwald         return;
1573a33eb0c4SMilanka Ringwald     }
1574a33eb0c4SMilanka Ringwald 
1575a0ffb263SMatthias Ringwald     hfp_connection->hf_send_dtmf_code = code;
1576a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1577ce263fc8SMatthias Ringwald }
1578ce263fc8SMatthias Ringwald 
1579c8626498SMilanka Ringwald void hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
15809c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1581a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1582a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1583a33eb0c4SMilanka Ringwald         return;
1584a33eb0c4SMilanka Ringwald     }
1585a0ffb263SMatthias Ringwald     hfp_connection->hf_send_binp = 1;
1586a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1587ce263fc8SMatthias Ringwald }
15883deb3ec6SMatthias Ringwald 
1589c8626498SMilanka Ringwald void hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
15909c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1591a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1592a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1593a33eb0c4SMilanka Ringwald         return;
1594a33eb0c4SMilanka Ringwald     }
1595a0ffb263SMatthias Ringwald     hfp_connection->hf_send_clcc = 1;
1596a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1597667ec068SMatthias Ringwald }
1598667ec068SMatthias Ringwald 
1599667ec068SMatthias Ringwald 
1600c8626498SMilanka Ringwald void hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
16019c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1602a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1603a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1604a33eb0c4SMilanka Ringwald         return;
1605a33eb0c4SMilanka Ringwald     }
1606a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1607a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '?';
1608a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1609667ec068SMatthias Ringwald }
1610667ec068SMatthias Ringwald 
1611c8626498SMilanka Ringwald void hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
16129c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1613a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1614a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1615a33eb0c4SMilanka Ringwald         return;
1616a33eb0c4SMilanka Ringwald     }
1617a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1618a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '0';
1619a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1620667ec068SMatthias Ringwald }
1621667ec068SMatthias Ringwald 
1622c8626498SMilanka Ringwald void hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
16239c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1624a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1625a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1626a33eb0c4SMilanka Ringwald         return;
1627a33eb0c4SMilanka Ringwald     }
1628a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1629a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '1';
1630a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1631667ec068SMatthias Ringwald }
1632667ec068SMatthias Ringwald 
1633c8626498SMilanka Ringwald void hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
16349c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1635a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1636a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1637a33eb0c4SMilanka Ringwald         return;
1638a33eb0c4SMilanka Ringwald     }
1639a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1640a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '2';
1641a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1642667ec068SMatthias Ringwald }
1643667ec068SMatthias Ringwald 
1644c8626498SMilanka Ringwald void hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
16459c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1646a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1647a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1648a33eb0c4SMilanka Ringwald         return;
1649a33eb0c4SMilanka Ringwald     }
1650a0ffb263SMatthias Ringwald     hfp_connection->hf_send_cnum = 1;
1651a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1652667ec068SMatthias Ringwald }
1653667ec068SMatthias Ringwald 
1654c8626498SMilanka Ringwald void hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
16559c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1656a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1657a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1658a33eb0c4SMilanka Ringwald         return;
1659a33eb0c4SMilanka Ringwald     }
1660667ec068SMatthias Ringwald     // find index for assigned number
1661667ec068SMatthias Ringwald     int i;
1662667ec068SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
1663667ec068SMatthias Ringwald         if (hfp_indicators[i] == assigned_number){
1664667ec068SMatthias Ringwald             // set value
1665667ec068SMatthias Ringwald             hfp_indicators_value[i] = value;
1666667ec068SMatthias Ringwald             // mark for update
1667a0ffb263SMatthias Ringwald             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
1668a0ffb263SMatthias Ringwald                 hfp_connection->generic_status_update_bitmap |= (1<<i);
1669667ec068SMatthias Ringwald                 // send update
1670a0ffb263SMatthias Ringwald                 hfp_run_for_context(hfp_connection);
1671a0ffb263SMatthias Ringwald             }
1672667ec068SMatthias Ringwald             return;
1673667ec068SMatthias Ringwald         }
1674667ec068SMatthias Ringwald     }
1675667ec068SMatthias Ringwald }
1676667ec068SMatthias Ringwald 
1677