xref: /btstack/src/classic/hfp_hf.c (revision 0aee97eff3e77a1a29b856d1782b8523c8efe833)
13deb3ec6SMatthias Ringwald /*
23deb3ec6SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
33deb3ec6SMatthias Ringwald  *
43deb3ec6SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
53deb3ec6SMatthias Ringwald  * modification, are permitted provided that the following conditions
63deb3ec6SMatthias Ringwald  * are met:
73deb3ec6SMatthias Ringwald  *
83deb3ec6SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
93deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
103deb3ec6SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
113deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
123deb3ec6SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
133deb3ec6SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
143deb3ec6SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
153deb3ec6SMatthias Ringwald  *    from this software without specific prior written permission.
163deb3ec6SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
173deb3ec6SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
183deb3ec6SMatthias Ringwald  *    monetary gain.
193deb3ec6SMatthias Ringwald  *
203deb3ec6SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
213deb3ec6SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
223deb3ec6SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
233deb3ec6SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
243deb3ec6SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
253deb3ec6SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
263deb3ec6SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
273deb3ec6SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
283deb3ec6SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
293deb3ec6SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
303deb3ec6SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313deb3ec6SMatthias Ringwald  * SUCH DAMAGE.
323deb3ec6SMatthias Ringwald  *
333deb3ec6SMatthias Ringwald  * Please inquire about commercial licensing options at
343deb3ec6SMatthias Ringwald  * [email protected]
353deb3ec6SMatthias Ringwald  *
363deb3ec6SMatthias Ringwald  */
37ab2c6ae4SMatthias Ringwald 
38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "hfp_hf.c"
393deb3ec6SMatthias Ringwald 
403deb3ec6SMatthias Ringwald // *****************************************************************************
413deb3ec6SMatthias Ringwald //
42fffdd288SMatthias Ringwald // HFP Hands-Free (HF) unit
433deb3ec6SMatthias Ringwald //
443deb3ec6SMatthias Ringwald // *****************************************************************************
453deb3ec6SMatthias Ringwald 
467907f069SMatthias Ringwald #include "btstack_config.h"
473deb3ec6SMatthias Ringwald 
483deb3ec6SMatthias Ringwald #include <stdint.h>
493deb3ec6SMatthias Ringwald #include <stdio.h>
503deb3ec6SMatthias Ringwald #include <stdlib.h>
513deb3ec6SMatthias Ringwald #include <string.h>
523deb3ec6SMatthias Ringwald 
53235946f1SMatthias Ringwald #include "bluetooth_sdp.h"
5459c6af15SMatthias Ringwald #include "btstack_debug.h"
55d4dd47ffSMatthias Ringwald #include "btstack_event.h"
563deb3ec6SMatthias Ringwald #include "btstack_memory.h"
5759c6af15SMatthias Ringwald #include "btstack_run_loop.h"
5859c6af15SMatthias Ringwald #include "classic/core.h"
5959c6af15SMatthias Ringwald #include "classic/hfp.h"
6059c6af15SMatthias Ringwald #include "classic/hfp_hf.h"
61efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
62746ccb7eSMatthias Ringwald #include "classic/sdp_server.h"
63023f2764SMatthias Ringwald #include "classic/sdp_util.h"
6459c6af15SMatthias Ringwald #include "hci.h"
6559c6af15SMatthias Ringwald #include "hci_cmd.h"
6659c6af15SMatthias Ringwald #include "hci_dump.h"
6759c6af15SMatthias Ringwald #include "l2cap.h"
683deb3ec6SMatthias Ringwald 
6927950165SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
7027950165SMatthias Ringwald 
713deb3ec6SMatthias Ringwald static const char default_hfp_hf_service_name[] = "Hands-Free unit";
723deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
733deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0;
743deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
753deb3ec6SMatthias Ringwald 
763deb3ec6SMatthias Ringwald static uint8_t hfp_indicators_nr = 0;
773deb3ec6SMatthias Ringwald static uint8_t hfp_indicators[HFP_MAX_NUM_HF_INDICATORS];
78667ec068SMatthias Ringwald static uint32_t hfp_indicators_value[HFP_MAX_NUM_HF_INDICATORS];
79667ec068SMatthias Ringwald 
80667ec068SMatthias Ringwald static uint8_t hfp_hf_speaker_gain = 9;
81667ec068SMatthias Ringwald static uint8_t hfp_hf_microphone_gain = 9;
823deb3ec6SMatthias Ringwald 
83ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_hf_callback;
843deb3ec6SMatthias Ringwald 
85ce263fc8SMatthias Ringwald static hfp_call_status_t hfp_call_status;
86ce263fc8SMatthias Ringwald static hfp_callsetup_status_t hfp_callsetup_status;
87ce263fc8SMatthias Ringwald static hfp_callheld_status_t hfp_callheld_status;
88ce263fc8SMatthias Ringwald 
89ce263fc8SMatthias Ringwald static char phone_number[25];
90ce263fc8SMatthias Ringwald 
919c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_hf_connection_context_for_acl_handle(uint16_t handle){
929c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_t it;
939c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
949c9c64c1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
959c9c64c1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
969c9c64c1SMatthias Ringwald         if (hfp_connection->acl_handle != handle)      continue;
979c9c64c1SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
989c9c64c1SMatthias Ringwald         return hfp_connection;
999c9c64c1SMatthias Ringwald     }
1009c9c64c1SMatthias Ringwald     return NULL;
1019c9c64c1SMatthias Ringwald }
1029c9c64c1SMatthias Ringwald 
10313839019SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
1043deb3ec6SMatthias Ringwald     if (callback == NULL){
1053deb3ec6SMatthias Ringwald         log_error("hfp_hf_register_packet_handler called with NULL callback");
1063deb3ec6SMatthias Ringwald         return;
1073deb3ec6SMatthias Ringwald     }
108ca59be51SMatthias Ringwald     hfp_hf_callback = callback;
109ca59be51SMatthias Ringwald     hfp_set_hf_callback(callback);
1103deb3ec6SMatthias Ringwald }
1113deb3ec6SMatthias Ringwald 
11213839019SMatthias Ringwald static void hfp_hf_emit_subscriber_information(btstack_packet_handler_t callback, uint8_t event_subtype, uint8_t status, uint8_t bnip_type, const char * bnip_number){
113a0ffb263SMatthias Ringwald     if (!callback) return;
114a0ffb263SMatthias Ringwald     uint8_t event[31];
115a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
116a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
117a0ffb263SMatthias Ringwald     event[2] = event_subtype;
118a0ffb263SMatthias Ringwald     event[3] = status;
119a0ffb263SMatthias Ringwald     event[4] = bnip_type;
120adaba9f3SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 6) ? (int) strlen(bnip_number) : (int) sizeof(event) - 6;
121a0ffb263SMatthias Ringwald     strncpy((char*)&event[5], bnip_number, size);
122a0ffb263SMatthias Ringwald     event[5 + size] = 0;
12313839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
124a0ffb263SMatthias Ringwald }
125a0ffb263SMatthias Ringwald 
12613839019SMatthias Ringwald static void hfp_hf_emit_type_and_number(btstack_packet_handler_t callback, uint8_t event_subtype, uint8_t bnip_type, const char * bnip_number){
127a0ffb263SMatthias Ringwald     if (!callback) return;
128a0ffb263SMatthias Ringwald     uint8_t event[30];
129a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
130a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
131a0ffb263SMatthias Ringwald     event[2] = event_subtype;
132a0ffb263SMatthias Ringwald     event[3] = bnip_type;
133adaba9f3SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 5) ? (int) strlen(bnip_number) : (int) sizeof(event) - 5;
134a0ffb263SMatthias Ringwald     strncpy((char*)&event[4], bnip_number, size);
135a0ffb263SMatthias Ringwald     event[4 + size] = 0;
13613839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
137a0ffb263SMatthias Ringwald }
138a0ffb263SMatthias Ringwald 
13913839019SMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(btstack_packet_handler_t callback, uint8_t clcc_idx, uint8_t clcc_dir,
140*0aee97efSMilanka Ringwald                 uint8_t clcc_status, uint8_t clcc_mode, uint8_t clcc_mpty, uint8_t bnip_type, const char * bnip_number){
141a0ffb263SMatthias Ringwald     if (!callback) return;
142*0aee97efSMilanka Ringwald     printf("hfp_hf_emit_enhanced_call_status: %s \n", bnip_number);
143*0aee97efSMilanka Ringwald     uint8_t event[36];
144*0aee97efSMilanka Ringwald     int pos = 0;
145*0aee97efSMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
146*0aee97efSMilanka Ringwald     event[pos++] = sizeof(event) - 2;
147*0aee97efSMilanka Ringwald     event[pos++] = HFP_SUBEVENT_ENHANCED_CALL_STATUS;
148*0aee97efSMilanka Ringwald     event[pos++] = clcc_idx;
149*0aee97efSMilanka Ringwald     event[pos++] = clcc_dir;
150*0aee97efSMilanka Ringwald     event[pos++] = clcc_status;
151*0aee97efSMilanka Ringwald     event[pos++] = clcc_mode;
152*0aee97efSMilanka Ringwald     event[pos++] = clcc_mpty;
153*0aee97efSMilanka Ringwald     event[pos++] = bnip_type;
154*0aee97efSMilanka Ringwald     int size = (strlen(bnip_number) < sizeof(event) - pos) ? (int) strlen(bnip_number) : (int) sizeof(event) - pos;
155*0aee97efSMilanka Ringwald     strncpy((char*)&event[pos], bnip_number, size);
156*0aee97efSMilanka Ringwald     pos += size;
157*0aee97efSMilanka Ringwald     event[pos++] = 0;
158*0aee97efSMilanka Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, pos);
159a0ffb263SMatthias Ringwald }
160a0ffb263SMatthias Ringwald 
161a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
1623deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
163a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
1643deb3ec6SMatthias Ringwald     return hf && ag;
1653deb3ec6SMatthias Ringwald }
1663deb3ec6SMatthias Ringwald 
167a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
1683deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_THREE_WAY_CALLING);
169a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING);
1703deb3ec6SMatthias Ringwald     return hf && ag;
1713deb3ec6SMatthias Ringwald }
1723deb3ec6SMatthias Ringwald 
1733deb3ec6SMatthias Ringwald 
174a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
1753deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_HF_INDICATORS);
176a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS);
1773deb3ec6SMatthias Ringwald     return hf && ag;
1783deb3ec6SMatthias Ringwald }
1793deb3ec6SMatthias Ringwald 
1804f84bf36SMatthias 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){
1813deb3ec6SMatthias Ringwald     if (!name){
1823deb3ec6SMatthias Ringwald         name = default_hfp_hf_service_name;
1833deb3ec6SMatthias Ringwald     }
184235946f1SMatthias Ringwald     hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name);
1853deb3ec6SMatthias Ringwald 
1864f84bf36SMatthias Ringwald     // Construct SupportedFeatures for SDP bitmap:
1874f84bf36SMatthias Ringwald     //
1884f84bf36SMatthias Ringwald     // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
1894f84bf36SMatthias Ringwald     //  of the Bits 0 to 4 of the unsolicited result code +BRSF"
1904f84bf36SMatthias Ringwald     //
19142bafa11SMatthias Ringwald     // Wide band speech (bit 5) requires Codec negotiation
19242bafa11SMatthias Ringwald     //
1934f84bf36SMatthias Ringwald     uint16_t sdp_features = supported_features & 0x1f;
19442bafa11SMatthias Ringwald     if (wide_band_speech && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){
19542bafa11SMatthias Ringwald         sdp_features |= 1 << 5;
1964f84bf36SMatthias Ringwald     }
197aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
1984f84bf36SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
199aa4dd815SMatthias Ringwald }
2003deb3ec6SMatthias Ringwald 
20189425bfcSMilanka Ringwald 
20289425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){
2033deb3ec6SMatthias Ringwald     char buffer[20];
20489425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s\r\n", cmd);
20589425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
20689425bfcSMilanka Ringwald }
20789425bfcSMilanka Ringwald 
20889425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){
20989425bfcSMilanka Ringwald     char buffer[20];
21089425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s%s\r\n", cmd, mark);
21189425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
21289425bfcSMilanka Ringwald }
21389425bfcSMilanka Ringwald 
21486da9d74SMatthias Ringwald static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint16_t value){
21589425bfcSMilanka Ringwald     char buffer[40];
21689425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%d\r\n", cmd, value);
2173deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2183deb3ec6SMatthias Ringwald }
2193deb3ec6SMatthias Ringwald 
2203deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){
2213deb3ec6SMatthias Ringwald     char buffer[30];
22289425bfcSMilanka Ringwald     const int size = sizeof(buffer);
22389425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS);
22489425bfcSMilanka Ringwald     offset += join(buffer+offset, size-offset, hfp_codecs, hfp_codecs_nr);
22589425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n");
2263deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2273deb3ec6SMatthias Ringwald }
2283deb3ec6SMatthias Ringwald 
2293deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){
2303deb3ec6SMatthias Ringwald     char buffer[50];
23189425bfcSMilanka Ringwald     const int size = sizeof(buffer);
23289425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS);
23389425bfcSMilanka Ringwald     offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr);
23489425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n");
2353deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2363deb3ec6SMatthias Ringwald }
2373deb3ec6SMatthias Ringwald 
2383deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){
2393deb3ec6SMatthias Ringwald     char buffer[30];
24089425bfcSMilanka Ringwald     const int size = sizeof(buffer);
24189425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR);
24289425bfcSMilanka Ringwald     offset += join(buffer+offset, size-offset, hfp_indicators, hfp_indicators_nr);
24389425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n");
2443deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2453deb3ec6SMatthias Ringwald }
2463deb3ec6SMatthias Ringwald 
24789425bfcSMilanka Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){
2483deb3ec6SMatthias Ringwald     char buffer[20];
24989425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r\n", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate);
250ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
251ce263fc8SMatthias Ringwald }
252ce263fc8SMatthias Ringwald 
253ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){
254ce263fc8SMatthias Ringwald     char buffer[40];
25589425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "%s%s;\r\n", HFP_CALL_PHONE_NUMBER, phone_number);
256ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
257ce263fc8SMatthias Ringwald }
258ce263fc8SMatthias Ringwald 
259a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){
260ce263fc8SMatthias Ringwald     char buffer[40];
26189425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "%s>%d;\r\n", HFP_CALL_PHONE_NUMBER, memory_id);
262ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
263ce263fc8SMatthias Ringwald }
264ce263fc8SMatthias Ringwald 
265f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){
26689425bfcSMilanka Ringwald     char buffer[40];
26789425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%u\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number);
268ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
269ce263fc8SMatthias Ringwald }
270ce263fc8SMatthias Ringwald 
271ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){
272ce263fc8SMatthias Ringwald     char buffer[20];
27389425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%c\r\n", HFP_TRANSMIT_DTMF_CODES, code);
274ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
275ce263fc8SMatthias Ringwald }
276ce263fc8SMatthias Ringwald 
27797d2cadbSMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){
27897d2cadbSMatthias Ringwald     return send_str_over_rfcomm(cid, "ATA\r\n");
27997d2cadbSMatthias Ringwald }
28097d2cadbSMatthias Ringwald 
28189425bfcSMilanka Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){
28289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_supported_features);
28389425bfcSMilanka Ringwald }
28489425bfcSMilanka Ringwald 
28589425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){
28689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?");
28789425bfcSMilanka Ringwald }
28889425bfcSMilanka Ringwald 
28989425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){
29089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?");
29189425bfcSMilanka Ringwald }
29289425bfcSMilanka Ringwald 
29389425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){
29489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?");
29589425bfcSMilanka Ringwald }
29689425bfcSMilanka Ringwald 
29789425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){
29889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?");
29989425bfcSMilanka Ringwald }
30089425bfcSMilanka Ringwald 
30189425bfcSMilanka Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){
30289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?");
30389425bfcSMilanka Ringwald }
30489425bfcSMilanka Ringwald 
30589425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){
30689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0");
30789425bfcSMilanka Ringwald }
30889425bfcSMilanka Ringwald 
30989425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){
31089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?");
31189425bfcSMilanka Ringwald }
31289425bfcSMilanka Ringwald 
31389425bfcSMilanka Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){
31489425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP);
31589425bfcSMilanka Ringwald }
31689425bfcSMilanka Ringwald 
31789425bfcSMilanka Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){
31889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
31989425bfcSMilanka Ringwald }
32089425bfcSMilanka Ringwald 
32189425bfcSMilanka Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){
32289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
32389425bfcSMilanka Ringwald }
32489425bfcSMilanka Ringwald 
32589425bfcSMilanka Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){
32689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate);
32789425bfcSMilanka Ringwald }
32889425bfcSMilanka Ringwald 
32989425bfcSMilanka Ringwald static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){
33089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_TURN_OFF_EC_AND_NR, activate);
33189425bfcSMilanka Ringwald }
33289425bfcSMilanka Ringwald 
33389425bfcSMilanka Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
33489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate);
33589425bfcSMilanka Ringwald }
33689425bfcSMilanka Ringwald 
33789425bfcSMilanka Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
33889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
33989425bfcSMilanka Ringwald }
34089425bfcSMilanka Ringwald 
34189425bfcSMilanka Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
34289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
34389425bfcSMilanka Ringwald }
34489425bfcSMilanka Ringwald 
34589425bfcSMilanka Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
34689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
34789425bfcSMilanka Ringwald }
34889425bfcSMilanka Ringwald 
34989425bfcSMilanka Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
35089425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER);
35189425bfcSMilanka Ringwald }
35289425bfcSMilanka Ringwald 
35389425bfcSMilanka Ringwald static int hfp_hf_send_chup(uint16_t cid){
35489425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL);
35589425bfcSMilanka Ringwald }
35689425bfcSMilanka Ringwald 
357ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){
35889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1");
359ce263fc8SMatthias Ringwald }
360ce263fc8SMatthias Ringwald 
361667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){
36289425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS);
363667ec068SMatthias Ringwald }
364667ec068SMatthias Ringwald 
36513839019SMatthias Ringwald static void hfp_emit_ag_indicator_event(btstack_packet_handler_t callback, hfp_ag_indicator_t indicator){
3663deb3ec6SMatthias Ringwald     if (!callback) return;
367c741b032SMilanka Ringwald     uint8_t event[10+HFP_MAX_INDICATOR_DESC_SIZE+1];
368c741b032SMilanka Ringwald     int pos = 0;
369c741b032SMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
370c741b032SMilanka Ringwald     event[pos++] = sizeof(event) - 2;
371c741b032SMilanka Ringwald     event[pos++] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
372c741b032SMilanka Ringwald     event[pos++] = indicator.index;
373c741b032SMilanka Ringwald     event[pos++] = indicator.status;
374c741b032SMilanka Ringwald     event[pos++] = indicator.min_range;
375c741b032SMilanka Ringwald     event[pos++] = indicator.max_range;
376c741b032SMilanka Ringwald     event[pos++] = indicator.mandatory;
377c741b032SMilanka Ringwald     event[pos++] = indicator.enabled;
378c741b032SMilanka Ringwald     event[pos++] = indicator.status_changed;
379c741b032SMilanka Ringwald     strncpy((char*)&event[pos], indicator.name, HFP_MAX_INDICATOR_DESC_SIZE);
380c741b032SMilanka Ringwald     pos += HFP_MAX_INDICATOR_DESC_SIZE;
381c741b032SMilanka Ringwald     event[pos] = 0;
38213839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
3833deb3ec6SMatthias Ringwald }
3843deb3ec6SMatthias Ringwald 
38513839019SMatthias Ringwald static void hfp_emit_network_operator_event(btstack_packet_handler_t callback, hfp_network_opearator_t network_operator){
3863deb3ec6SMatthias Ringwald     if (!callback) return;
38789425bfcSMilanka Ringwald     uint8_t event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE+1];
3883deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3893deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3903deb3ec6SMatthias Ringwald     event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED;
391a0ffb263SMatthias Ringwald     event[3] = network_operator.mode;
392a0ffb263SMatthias Ringwald     event[4] = network_operator.format;
39389425bfcSMilanka Ringwald     strncpy((char*)&event[5], network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE);
39489425bfcSMilanka Ringwald     event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE] = 0;
39513839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
3963deb3ec6SMatthias Ringwald }
3973deb3ec6SMatthias Ringwald 
398a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
399a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
400a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
401aa4dd815SMatthias Ringwald     int done = 1;
4023deb3ec6SMatthias Ringwald 
403a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
4043deb3ec6SMatthias Ringwald         case HFP_EXCHANGE_SUPPORTED_FEATURES:
405d715cf51SMatthias Ringwald             hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr);
406a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
407a0ffb263SMatthias Ringwald             hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid);
4083deb3ec6SMatthias Ringwald             break;
4093deb3ec6SMatthias Ringwald         case HFP_NOTIFY_ON_CODECS:
410a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
411a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
4123deb3ec6SMatthias Ringwald             break;
4133deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS:
414a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
415a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid);
4163deb3ec6SMatthias Ringwald             break;
4173deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS_STATUS:
418a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
419a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid);
4203deb3ec6SMatthias Ringwald             break;
4213deb3ec6SMatthias Ringwald         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
422a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
423a0ffb263SMatthias Ringwald             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1);
4243deb3ec6SMatthias Ringwald             break;
4253deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_CAN_HOLD_CALL:
426a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
427a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid);
4283deb3ec6SMatthias Ringwald             break;
4293deb3ec6SMatthias Ringwald         case HFP_LIST_GENERIC_STATUS_INDICATORS:
430a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
431a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4323deb3ec6SMatthias Ringwald             break;
4333deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
434a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
435a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4363deb3ec6SMatthias Ringwald             break;
4373deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
438a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
439a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4403deb3ec6SMatthias Ringwald             break;
4413deb3ec6SMatthias Ringwald         default:
442aa4dd815SMatthias Ringwald             done = 0;
4433deb3ec6SMatthias Ringwald             break;
4443deb3ec6SMatthias Ringwald     }
4453deb3ec6SMatthias Ringwald     return done;
4463deb3ec6SMatthias Ringwald }
4473deb3ec6SMatthias Ringwald 
448ce263fc8SMatthias Ringwald 
449a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
450a0ffb263SMatthias Ringwald     if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
451a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
452ce263fc8SMatthias Ringwald 
453ce263fc8SMatthias Ringwald     int done = 0;
454a0ffb263SMatthias Ringwald     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
455a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
456ce263fc8SMatthias Ringwald         done = 1;
457a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
458ce263fc8SMatthias Ringwald         return done;
459ce263fc8SMatthias Ringwald     };
460a0ffb263SMatthias Ringwald     if (hfp_connection->change_status_update_for_individual_ag_indicators){
461a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
462ce263fc8SMatthias Ringwald         done = 1;
463a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
464a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap,
465a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_nr);
466ce263fc8SMatthias Ringwald         return done;
467ce263fc8SMatthias Ringwald     }
468ce263fc8SMatthias Ringwald 
469a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
470ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
471a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
472a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
473a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
474ce263fc8SMatthias Ringwald             return 1;
475ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
476a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
477a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
478a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
479ce263fc8SMatthias Ringwald             return 1;
480ce263fc8SMatthias Ringwald         default:
481ce263fc8SMatthias Ringwald             break;
482ce263fc8SMatthias Ringwald     }
483ce263fc8SMatthias Ringwald 
484a0ffb263SMatthias Ringwald     if (hfp_connection->enable_extended_audio_gateway_error_report){
485a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
486ce263fc8SMatthias Ringwald         done = 1;
487a0ffb263SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
488ce263fc8SMatthias Ringwald         return done;
489ce263fc8SMatthias Ringwald     }
490ce263fc8SMatthias Ringwald 
491ce263fc8SMatthias Ringwald     return done;
492ce263fc8SMatthias Ringwald }
493ce263fc8SMatthias Ringwald 
494a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
495ce263fc8SMatthias Ringwald     /* events ( == commands):
496ce263fc8SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
497ce263fc8SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
498ce263fc8SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
499ce263fc8SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
500ce263fc8SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
501ce263fc8SMatthias Ringwald     */
502ce263fc8SMatthias Ringwald 
503a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
504ce263fc8SMatthias Ringwald 
505a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
506ce263fc8SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
507a0ffb263SMatthias Ringwald             if (hfp_connection->codecs_state == HFP_CODECS_W4_AG_COMMON_CODEC) return 0;
508ce263fc8SMatthias Ringwald 
509a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
510a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
511a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
512ce263fc8SMatthias Ringwald             return 1;
513ce263fc8SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
514a0ffb263SMatthias Ringwald             hfp_connection->codec_confirmed = 0;
515a0ffb263SMatthias Ringwald             hfp_connection->suggested_codec = 0;
516a0ffb263SMatthias Ringwald             hfp_connection->negotiated_codec = 0;
517ce263fc8SMatthias Ringwald 
518a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
519a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
520a0ffb263SMatthias Ringwald             hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
521ce263fc8SMatthias Ringwald             break;
522ce263fc8SMatthias Ringwald 
5236a7f44bdSMilanka Ringwald          case HFP_CMD_AG_SUGGESTED_CODEC:{
5246a7f44bdSMilanka Ringwald             if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){
525a0ffb263SMatthias Ringwald                 hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
526a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
527a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
5280e0be203SMatthias Ringwald                 hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
5290e0be203SMatthias Ringwald                 log_info("hfp: codec confirmed: %s", hfp_connection->negotiated_codec == HFP_CODEC_MSBC ? "mSBC" : "CVSD");
530fcb08cdbSMilanka Ringwald                 hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
531ce263fc8SMatthias Ringwald             } else {
532a0ffb263SMatthias Ringwald                 hfp_connection->codec_confirmed = 0;
533a0ffb263SMatthias Ringwald                 hfp_connection->suggested_codec = 0;
534a0ffb263SMatthias Ringwald                 hfp_connection->negotiated_codec = 0;
535a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
536a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
537a0ffb263SMatthias Ringwald                 hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
538ce263fc8SMatthias Ringwald 
539ce263fc8SMatthias Ringwald             }
540ce263fc8SMatthias Ringwald             break;
5416a7f44bdSMilanka Ringwald         }
542ce263fc8SMatthias Ringwald         default:
543ce263fc8SMatthias Ringwald             break;
544ce263fc8SMatthias Ringwald     }
545ce263fc8SMatthias Ringwald     return 0;
546ce263fc8SMatthias Ringwald }
547ce263fc8SMatthias Ringwald 
548a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
549a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
550a0ffb263SMatthias Ringwald         hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0;
551ce263fc8SMatthias Ringwald 
552ce263fc8SMatthias Ringwald 
553a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){
554a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
555a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
556a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
557ce263fc8SMatthias Ringwald         return 1;
558ce263fc8SMatthias Ringwald     }
559ce263fc8SMatthias Ringwald 
560a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
561ce263fc8SMatthias Ringwald 
562ce263fc8SMatthias Ringwald     // run codecs exchange
563a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
564ce263fc8SMatthias Ringwald     if (done) return 1;
565ce263fc8SMatthias Ringwald 
56638200c1dSMilanka Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
56738200c1dSMilanka Ringwald     if (hfp_connection->establish_audio_connection){
56838200c1dSMilanka Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
56938200c1dSMilanka Ringwald         hfp_connection->establish_audio_connection = 0;
57038200c1dSMilanka Ringwald         hfp_setup_synchronous_connection(hfp_connection);
57138200c1dSMilanka Ringwald         return 1;
57238200c1dSMilanka Ringwald     }
57338200c1dSMilanka Ringwald 
574ce263fc8SMatthias Ringwald     return 0;
575ce263fc8SMatthias Ringwald }
576ce263fc8SMatthias Ringwald 
57738200c1dSMilanka Ringwald 
578a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
579a0ffb263SMatthias Ringwald     if (hfp_connection->hf_answer_incoming_call){
580a0ffb263SMatthias Ringwald         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
581a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 0;
582ce263fc8SMatthias Ringwald         return 1;
583ce263fc8SMatthias Ringwald     }
584ce263fc8SMatthias Ringwald     return 0;
585ce263fc8SMatthias Ringwald }
586ce263fc8SMatthias Ringwald 
587a0ffb263SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t * hfp_connection){
588a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
589724f400dSMatthias Ringwald     if (!hfp_connection->rfcomm_cid) return;
5907522e673SMatthias Ringwald 
59122387625SMatthias Ringwald     if (hfp_connection->local_role != HFP_ROLE_HF) {
59222387625SMatthias Ringwald         log_info("HFP HF%p, wrong role %u", hfp_connection, hfp_connection->local_role);
59322387625SMatthias Ringwald         return;
59422387625SMatthias Ringwald     }
59522387625SMatthias Ringwald 
596b72c4a9eSMatthias Ringwald     if (hfp_connection->hf_accept_sco && hci_can_send_command_packet_now()){
597b72c4a9eSMatthias Ringwald 
598b72c4a9eSMatthias Ringwald         hfp_connection->hf_accept_sco = 0;
5997522e673SMatthias Ringwald 
6007522e673SMatthias Ringwald         // notify about codec selection if not done already
6017522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == 0){
6027522e673SMatthias Ringwald             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
6037522e673SMatthias Ringwald         }
6047522e673SMatthias Ringwald 
6057522e673SMatthias Ringwald         // remote supported feature eSCO is set if link type is eSCO
6067522e673SMatthias Ringwald         // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms,
6077522e673SMatthias Ringwald         uint16_t max_latency;
6087522e673SMatthias Ringwald         uint8_t  retransmission_effort;
6097522e673SMatthias Ringwald         uint16_t packet_types;
6107522e673SMatthias Ringwald 
611d9f77559SMatthias Ringwald         if (hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){
6127522e673SMatthias Ringwald             max_latency = 0x000c;
6137522e673SMatthias Ringwald             retransmission_effort = 0x02;
6147522e673SMatthias Ringwald             packet_types = 0x388;
6157522e673SMatthias Ringwald         } else {
6167522e673SMatthias Ringwald             max_latency = 0xffff;
6177522e673SMatthias Ringwald             retransmission_effort = 0xff;
6187522e673SMatthias Ringwald             packet_types = 0x003f;
6197522e673SMatthias Ringwald         }
6207522e673SMatthias Ringwald 
6217522e673SMatthias Ringwald         uint16_t sco_voice_setting = hci_get_sco_voice_setting();
6227522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
6237522e673SMatthias Ringwald             sco_voice_setting = 0x0043; // Transparent data
6247522e673SMatthias Ringwald         }
6257522e673SMatthias Ringwald 
626b72c4a9eSMatthias Ringwald         log_info("HFP: sending hci_accept_connection_request, sco_voice_setting 0x%02x", sco_voice_setting);
6277522e673SMatthias Ringwald         hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency,
6287522e673SMatthias Ringwald                         sco_voice_setting, retransmission_effort, packet_types);
6297522e673SMatthias Ringwald         return;
6307522e673SMatthias Ringwald     }
6317522e673SMatthias Ringwald 
632d4dd47ffSMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
633d4dd47ffSMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
634d4dd47ffSMatthias Ringwald         return;
635d4dd47ffSMatthias Ringwald     }
636a0ffb263SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
637ce263fc8SMatthias Ringwald     if (!done){
638a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
639ce263fc8SMatthias Ringwald     }
640ce263fc8SMatthias Ringwald     if (!done){
641a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_audio_connection(hfp_connection);
642ce263fc8SMatthias Ringwald     }
643ce263fc8SMatthias Ringwald     if (!done){
644a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
645ce263fc8SMatthias Ringwald     }
646ce263fc8SMatthias Ringwald 
6471016a228SMatthias Ringwald     // don't send a new command while ok still pending
6481016a228SMatthias Ringwald     if (hfp_connection->ok_pending) return;
6491016a228SMatthias Ringwald 
650a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
651a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
652a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
653a0ffb263SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
654ce263fc8SMatthias Ringwald         return;
655ce263fc8SMatthias Ringwald     }
656ce263fc8SMatthias Ringwald 
657a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
658a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
659a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
660a0ffb263SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
661ce263fc8SMatthias Ringwald         return;
662ce263fc8SMatthias Ringwald     }
663ce263fc8SMatthias Ringwald 
664a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_calling_line_notification){
665a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_calling_line_notification = 0;
666a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
667a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
668ce263fc8SMatthias Ringwald         return;
669ce263fc8SMatthias Ringwald     }
670ce263fc8SMatthias Ringwald 
671a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_calling_line_notification){
672a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_calling_line_notification = 0;
673a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
674a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
675ce263fc8SMatthias Ringwald         return;
676ce263fc8SMatthias Ringwald     }
677ce263fc8SMatthias Ringwald 
678a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
679a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
680a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
681a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0);
682ce263fc8SMatthias Ringwald         return;
683ce263fc8SMatthias Ringwald     }
684ce263fc8SMatthias Ringwald 
685a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){
686a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0;
687a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
688a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1);
689ce263fc8SMatthias Ringwald         return;
690ce263fc8SMatthias Ringwald     }
691ce263fc8SMatthias Ringwald 
692a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_voice_recognition_notification){
693a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_voice_recognition_notification = 0;
694a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
695a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
696ce263fc8SMatthias Ringwald         return;
697ce263fc8SMatthias Ringwald     }
698ce263fc8SMatthias Ringwald 
699a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_voice_recognition_notification){
700a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_voice_recognition_notification = 0;
701a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
702a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
703ce263fc8SMatthias Ringwald         return;
704ce263fc8SMatthias Ringwald     }
705ce263fc8SMatthias Ringwald 
706ce263fc8SMatthias Ringwald 
707a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_call_waiting_notification){
708a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_call_waiting_notification = 0;
709a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
710a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
711ce263fc8SMatthias Ringwald         return;
712ce263fc8SMatthias Ringwald     }
713ce263fc8SMatthias Ringwald 
714a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_call_waiting_notification){
715a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_call_waiting_notification = 0;
716a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
717a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
718ce263fc8SMatthias Ringwald         return;
719ce263fc8SMatthias Ringwald     }
720ce263fc8SMatthias Ringwald 
721a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_outgoing_call){
722a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_outgoing_call = 0;
723a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
724a0ffb263SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
725ce263fc8SMatthias Ringwald         return;
726ce263fc8SMatthias Ringwald     }
727ce263fc8SMatthias Ringwald 
728a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_memory_dialing){
729a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_memory_dialing = 0;
730a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
731a0ffb263SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
732ce263fc8SMatthias Ringwald         return;
733ce263fc8SMatthias Ringwald     }
734ce263fc8SMatthias Ringwald 
735a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_redial_last_number){
736a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_redial_last_number = 0;
737a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
738a0ffb263SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
739ce263fc8SMatthias Ringwald         return;
740ce263fc8SMatthias Ringwald     }
741ce263fc8SMatthias Ringwald 
742a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chup){
743a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 0;
744a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
745a0ffb263SMatthias Ringwald         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
746ce263fc8SMatthias Ringwald         return;
747ce263fc8SMatthias Ringwald     }
748ce263fc8SMatthias Ringwald 
749a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_0){
750a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 0;
751a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
752a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
753ce263fc8SMatthias Ringwald         return;
754ce263fc8SMatthias Ringwald     }
755ce263fc8SMatthias Ringwald 
756a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_1){
757a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 0;
758a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
759a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
760ce263fc8SMatthias Ringwald         return;
761ce263fc8SMatthias Ringwald     }
762ce263fc8SMatthias Ringwald 
763a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_2){
764a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 0;
765a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
766a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
767ce263fc8SMatthias Ringwald         return;
768ce263fc8SMatthias Ringwald     }
769ce263fc8SMatthias Ringwald 
770a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_3){
771a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 0;
772a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
773a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
774ce263fc8SMatthias Ringwald         return;
775ce263fc8SMatthias Ringwald     }
776ce263fc8SMatthias Ringwald 
777a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_4){
778a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 0;
779a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
780a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
781ce263fc8SMatthias Ringwald         return;
782ce263fc8SMatthias Ringwald     }
783ce263fc8SMatthias Ringwald 
784a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_x){
785a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 0;
786a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
787a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
788667ec068SMatthias Ringwald         return;
789667ec068SMatthias Ringwald     }
790667ec068SMatthias Ringwald 
791a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_dtmf_code){
792a0ffb263SMatthias Ringwald         char code = hfp_connection->hf_send_dtmf_code;
793a0ffb263SMatthias Ringwald         hfp_connection->hf_send_dtmf_code = 0;
794a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
795a0ffb263SMatthias Ringwald         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
796ce263fc8SMatthias Ringwald         return;
797ce263fc8SMatthias Ringwald     }
798ce263fc8SMatthias Ringwald 
799a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_binp){
800a0ffb263SMatthias Ringwald         hfp_connection->hf_send_binp = 0;
801a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
802a0ffb263SMatthias Ringwald         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
803ce263fc8SMatthias Ringwald         return;
804ce263fc8SMatthias Ringwald     }
805ce263fc8SMatthias Ringwald 
806a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_clcc){
807a0ffb263SMatthias Ringwald         hfp_connection->hf_send_clcc = 0;
808a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
809a0ffb263SMatthias Ringwald         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
810667ec068SMatthias Ringwald         return;
811667ec068SMatthias Ringwald     }
812667ec068SMatthias Ringwald 
813a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_rrh){
814a0ffb263SMatthias Ringwald         hfp_connection->hf_send_rrh = 0;
815667ec068SMatthias Ringwald         char buffer[20];
816a0ffb263SMatthias Ringwald         switch (hfp_connection->hf_send_rrh_command){
817667ec068SMatthias Ringwald             case '?':
818667ec068SMatthias Ringwald                 sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD);
819a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
820667ec068SMatthias Ringwald                 return;
821667ec068SMatthias Ringwald             case '0':
822667ec068SMatthias Ringwald             case '1':
823667ec068SMatthias Ringwald             case '2':
824a0ffb263SMatthias Ringwald                 sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, hfp_connection->hf_send_rrh_command);
825a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
826667ec068SMatthias Ringwald                 return;
827667ec068SMatthias Ringwald             default:
828667ec068SMatthias Ringwald                 break;
829667ec068SMatthias Ringwald         }
830667ec068SMatthias Ringwald         return;
831667ec068SMatthias Ringwald     }
832667ec068SMatthias Ringwald 
833a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_cnum){
834a0ffb263SMatthias Ringwald         hfp_connection->hf_send_cnum = 0;
835667ec068SMatthias Ringwald         char buffer[20];
836667ec068SMatthias Ringwald         sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION);
837a0ffb263SMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
838667ec068SMatthias Ringwald         return;
839667ec068SMatthias Ringwald     }
840667ec068SMatthias Ringwald 
841667ec068SMatthias Ringwald     // update HF indicators
842a0ffb263SMatthias Ringwald     if (hfp_connection->generic_status_update_bitmap){
843667ec068SMatthias Ringwald         int i;
844667ec068SMatthias Ringwald         for (i=0;i<hfp_indicators_nr;i++){
845a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
846a0ffb263SMatthias Ringwald                 if (hfp_connection->generic_status_indicators[i].state){
847a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
848a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
849667ec068SMatthias Ringwald                     char buffer[30];
850ecb7d461SMatthias Ringwald                     sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], (unsigned int) hfp_indicators_value[i]);
851a0ffb263SMatthias Ringwald                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
852667ec068SMatthias Ringwald                 } else {
85360ebb071SMilanka Ringwald                     log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]);
854667ec068SMatthias Ringwald                 }
855667ec068SMatthias Ringwald                 return;
856667ec068SMatthias Ringwald             }
857667ec068SMatthias Ringwald         }
858667ec068SMatthias Ringwald     }
859667ec068SMatthias Ringwald 
860ce263fc8SMatthias Ringwald     if (done) return;
861ce263fc8SMatthias Ringwald     // deal with disconnect
862a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
863ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
864a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
865a0ffb263SMatthias Ringwald             rfcomm_disconnect(hfp_connection->rfcomm_cid);
866ce263fc8SMatthias Ringwald             break;
867ce263fc8SMatthias Ringwald 
868ce263fc8SMatthias Ringwald         default:
869ce263fc8SMatthias Ringwald             break;
870ce263fc8SMatthias Ringwald     }
871ce263fc8SMatthias Ringwald }
872ce263fc8SMatthias Ringwald 
873a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
874a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
8756a7f44bdSMilanka Ringwald 
876ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
8777522e673SMatthias Ringwald 
878667ec068SMatthias Ringwald     // restore volume settings
879a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
880a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
881ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
882a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
883a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
884ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
885667ec068SMatthias Ringwald     // enable all indicators
886667ec068SMatthias Ringwald     int i;
887667ec068SMatthias Ringwald     for (i=0;i<hfp_indicators_nr;i++){
888a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i];
889a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].state = 1;
890667ec068SMatthias Ringwald     }
891ce263fc8SMatthias Ringwald }
892ce263fc8SMatthias Ringwald 
893a0ffb263SMatthias Ringwald static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){
894a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 0;
895a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
8963deb3ec6SMatthias Ringwald         case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
897a0ffb263SMatthias Ringwald             if (has_codec_negotiation_feature(hfp_connection)){
898a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_NOTIFY_ON_CODECS;
8993deb3ec6SMatthias Ringwald                 break;
9003deb3ec6SMatthias Ringwald             }
901a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
9023deb3ec6SMatthias Ringwald             break;
9033deb3ec6SMatthias Ringwald 
9043deb3ec6SMatthias Ringwald         case HFP_W4_NOTIFY_ON_CODECS:
905a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
9063deb3ec6SMatthias Ringwald             break;
9073deb3ec6SMatthias Ringwald 
9083deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS:
909a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
9103deb3ec6SMatthias Ringwald             break;
9113deb3ec6SMatthias Ringwald 
9123deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
913a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
9143deb3ec6SMatthias Ringwald             break;
9153deb3ec6SMatthias Ringwald 
9163deb3ec6SMatthias Ringwald         case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
917a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
918a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
9193deb3ec6SMatthias Ringwald                 break;
9203deb3ec6SMatthias Ringwald             }
921a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
922a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
9233deb3ec6SMatthias Ringwald                 break;
9243deb3ec6SMatthias Ringwald             }
925a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9263deb3ec6SMatthias Ringwald             break;
9273deb3ec6SMatthias Ringwald 
9283deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
929a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
930a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
9313deb3ec6SMatthias Ringwald                 break;
9323deb3ec6SMatthias Ringwald             }
933a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9343deb3ec6SMatthias Ringwald             break;
9353deb3ec6SMatthias Ringwald 
9363deb3ec6SMatthias Ringwald         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
937a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
9383deb3ec6SMatthias Ringwald             break;
9393deb3ec6SMatthias Ringwald 
9403deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
941a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
9423deb3ec6SMatthias Ringwald             break;
9433deb3ec6SMatthias Ringwald 
9443deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
945a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9463deb3ec6SMatthias Ringwald             break;
947ce263fc8SMatthias Ringwald         case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
948a0ffb263SMatthias Ringwald             if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
949a0ffb263SMatthias Ringwald                 hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
950ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
951ce263fc8SMatthias Ringwald                 break;
952ce263fc8SMatthias Ringwald             }
9533deb3ec6SMatthias Ringwald 
954a0ffb263SMatthias Ringwald             if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
955a0ffb263SMatthias Ringwald                 hfp_connection->change_status_update_for_individual_ag_indicators = 0;
956ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
957ce263fc8SMatthias Ringwald                 break;
9583deb3ec6SMatthias Ringwald             }
9593deb3ec6SMatthias Ringwald 
960a0ffb263SMatthias Ringwald             switch (hfp_connection->hf_query_operator_state){
961ce263fc8SMatthias Ringwald                 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
96260ebb071SMilanka Ringwald                     // printf("Format set, querying name\n");
963a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
964ce263fc8SMatthias Ringwald                     break;
965ce263fc8SMatthias Ringwald                 case HPF_HF_QUERY_OPERATOR_W4_RESULT:
966a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
967ca59be51SMatthias Ringwald                     hfp_emit_network_operator_event(hfp_hf_callback, hfp_connection->network_operator);
968ce263fc8SMatthias Ringwald                     break;
969ce263fc8SMatthias Ringwald                 default:
970ce263fc8SMatthias Ringwald                     break;
9713deb3ec6SMatthias Ringwald             }
972ce263fc8SMatthias Ringwald 
973a0ffb263SMatthias Ringwald             if (hfp_connection->enable_extended_audio_gateway_error_report){
974a0ffb263SMatthias Ringwald                 hfp_connection->enable_extended_audio_gateway_error_report = 0;
975ce263fc8SMatthias Ringwald                 break;
9763deb3ec6SMatthias Ringwald             }
9773deb3ec6SMatthias Ringwald 
978a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
979aa4dd815SMatthias Ringwald                 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
980a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
9813deb3ec6SMatthias Ringwald                     break;
982ce263fc8SMatthias Ringwald                 case HFP_CODECS_HF_CONFIRMED_CODEC:
983a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
984ce263fc8SMatthias Ringwald                     break;
9853deb3ec6SMatthias Ringwald                 default:
9863deb3ec6SMatthias Ringwald                     break;
9873deb3ec6SMatthias Ringwald             }
9883deb3ec6SMatthias Ringwald             break;
9893deb3ec6SMatthias Ringwald         default:
9903deb3ec6SMatthias Ringwald             break;
9913deb3ec6SMatthias Ringwald     }
9923deb3ec6SMatthias Ringwald 
9933deb3ec6SMatthias Ringwald     // done
994a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
9953deb3ec6SMatthias Ringwald }
9963deb3ec6SMatthias Ringwald 
997e0d09929SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
998e0d09929SMatthias Ringwald     return byte == '\n' || byte == '\r';
999e0d09929SMatthias Ringwald }
1000e0d09929SMatthias Ringwald 
1001e9c22d4eSMatthias Ringwald static void hfp_hf_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
10028a46ec40SMatthias Ringwald     UNUSED(packet_type);    // ok: only called with RFCOMM_DATA_PACKET
10038a46ec40SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
10048a46ec40SMatthias Ringwald     if (size < 1) return;
10059ec2630cSMatthias Ringwald 
1006a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
1007a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
10083deb3ec6SMatthias Ringwald 
1009186dd3d2SMatthias Ringwald     hfp_log_rfcomm_message("HFP_HF_RX", packet, size);
10101e35c04dSMatthias Ringwald 
10118a46ec40SMatthias Ringwald     // process messages byte-wise
1012186dd3d2SMatthias Ringwald     int pos;
10133deb3ec6SMatthias Ringwald     for (pos = 0; pos < size; pos++){
1014a0ffb263SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 1);
10153deb3ec6SMatthias Ringwald 
1016c741b032SMilanka Ringwald         // parse until end of line "\r\n"
1017e0d09929SMatthias Ringwald         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
1018e0d09929SMatthias Ringwald 
1019186dd3d2SMatthias Ringwald         int value;
1020186dd3d2SMatthias Ringwald         int i;
1021a0ffb263SMatthias Ringwald         switch (hfp_connection->command){
1022667ec068SMatthias Ringwald             case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1023a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1024ca59be51SMatthias Ringwald                 hfp_hf_emit_subscriber_information(hfp_hf_callback, HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION, 0, hfp_connection->bnip_type, hfp_connection->bnip_number);
1025667ec068SMatthias Ringwald                 break;
1026667ec068SMatthias Ringwald             case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
1027a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1028ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
1029667ec068SMatthias Ringwald                 break;
1030667ec068SMatthias Ringwald             case HFP_CMD_LIST_CURRENT_CALLS:
1031a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1032ca59be51SMatthias Ringwald                 hfp_hf_emit_enhanced_call_status(hfp_hf_callback, hfp_connection->clcc_idx,
1033*0aee97efSMilanka Ringwald                     hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mode,
1034*0aee97efSMilanka Ringwald                     hfp_connection->clcc_mpty, hfp_connection->bnip_type, hfp_connection->bnip_number);
1035667ec068SMatthias Ringwald                 break;
1036ce263fc8SMatthias Ringwald             case HFP_CMD_SET_SPEAKER_GAIN:
1037a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
10382308e108SMilanka Ringwald                 value = btstack_atoi((char*)hfp_connection->line_buffer);
1039667ec068SMatthias Ringwald                 hfp_hf_speaker_gain = value;
1040ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1041ce263fc8SMatthias Ringwald                 break;
1042ce263fc8SMatthias Ringwald             case HFP_CMD_SET_MICROPHONE_GAIN:
1043a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
10442308e108SMilanka Ringwald                 value = btstack_atoi((char*)hfp_connection->line_buffer);
1045667ec068SMatthias Ringwald                 hfp_hf_microphone_gain = value;
1046ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1047ce263fc8SMatthias Ringwald                 break;
1048ce263fc8SMatthias Ringwald             case HFP_CMD_AG_SENT_PHONE_NUMBER:
1049a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1050ca59be51SMatthias Ringwald                 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1051a0ffb263SMatthias Ringwald                 break;
1052a0ffb263SMatthias Ringwald             case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1053a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1054ca59be51SMatthias Ringwald                 hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1055a0ffb263SMatthias Ringwald                 break;
1056a0ffb263SMatthias Ringwald             case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1057a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1058ca59be51SMatthias Ringwald                 hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1059ce263fc8SMatthias Ringwald                 break;
1060ce263fc8SMatthias Ringwald             case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1061a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 0;
1062a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1063a0ffb263SMatthias Ringwald                 hfp_connection->extended_audio_gateway_error = 0;
1064ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1065ce263fc8SMatthias Ringwald                 break;
1066ce263fc8SMatthias Ringwald             case HFP_CMD_ERROR:
1067a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 0;
1068a0ffb263SMatthias Ringwald                 hfp_reset_context_flags(hfp_connection);
1069a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1070ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1);
1071ce263fc8SMatthias Ringwald                 break;
1072ce263fc8SMatthias Ringwald             case HFP_CMD_OK:
1073a0ffb263SMatthias Ringwald                 hfp_hf_switch_on_ok(hfp_connection);
1074ce263fc8SMatthias Ringwald                 break;
1075ce263fc8SMatthias Ringwald             case HFP_CMD_RING:
1076ca59be51SMatthias Ringwald                 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
1077ce263fc8SMatthias Ringwald                 break;
1078ce263fc8SMatthias Ringwald             case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1079a0ffb263SMatthias Ringwald                 for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1080a0ffb263SMatthias Ringwald                     if (hfp_connection->ag_indicators[i].status_changed) {
1081a0ffb263SMatthias Ringwald                         if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
1082a0ffb263SMatthias Ringwald                             hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
1083a0ffb263SMatthias Ringwald                         } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
1084a0ffb263SMatthias Ringwald                             hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
1085d5ff3b26SMatthias Ringwald                             // avoid set but not used warning
1086d5ff3b26SMatthias Ringwald                             (void) hfp_callheld_status;
1087a0ffb263SMatthias Ringwald                         } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
1088a0ffb263SMatthias Ringwald                             hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
1089ce263fc8SMatthias Ringwald                         }
1090a0ffb263SMatthias Ringwald                         hfp_connection->ag_indicators[i].status_changed = 0;
1091ca59be51SMatthias Ringwald                         hfp_emit_ag_indicator_event(hfp_hf_callback, hfp_connection->ag_indicators[i]);
10923deb3ec6SMatthias Ringwald                         break;
10933deb3ec6SMatthias Ringwald                     }
10943deb3ec6SMatthias Ringwald                 }
1095ce263fc8SMatthias Ringwald                 break;
1096c741b032SMilanka Ringwald             case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1097c741b032SMilanka Ringwald                 for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1098c741b032SMilanka Ringwald                     hfp_emit_ag_indicator_event(hfp_hf_callback, hfp_connection->ag_indicators[i]);
1099c741b032SMilanka Ringwald                 }
1100c741b032SMilanka Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1101c741b032SMilanka Ringwald                 break;
1102ce263fc8SMatthias Ringwald             default:
1103ce263fc8SMatthias Ringwald                 break;
11043deb3ec6SMatthias Ringwald         }
11050cef86faSMatthias Ringwald     }
1106a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11073deb3ec6SMatthias Ringwald }
11083deb3ec6SMatthias Ringwald 
11090cb5b971SMatthias Ringwald static void hfp_run(void){
1110665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1111665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1112665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1113a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
111422387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
1115a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
11163deb3ec6SMatthias Ringwald     }
11173deb3ec6SMatthias Ringwald }
11183deb3ec6SMatthias Ringwald 
1119e9c22d4eSMatthias Ringwald static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
11203deb3ec6SMatthias Ringwald     switch (packet_type){
11213deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
1122e9c22d4eSMatthias Ringwald             hfp_hf_handle_rfcomm_event(packet_type, channel, packet, size);
11233deb3ec6SMatthias Ringwald             break;
11243deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1125d4dd47ffSMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
1126d4dd47ffSMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
1127d4dd47ffSMatthias Ringwald                 hfp_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
1128d4dd47ffSMatthias Ringwald                 return;
1129d4dd47ffSMatthias Ringwald             }
113027950165SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1131202c8a4cSMatthias Ringwald             break;
11323deb3ec6SMatthias Ringwald         default:
11333deb3ec6SMatthias Ringwald             break;
11343deb3ec6SMatthias Ringwald     }
11353deb3ec6SMatthias Ringwald     hfp_run();
11363deb3ec6SMatthias Ringwald }
11373deb3ec6SMatthias Ringwald 
1138405014fbSMatthias Ringwald static void hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1139405014fbSMatthias Ringwald     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
114038200c1dSMilanka Ringwald 
114138200c1dSMilanka Ringwald     // allow for sco established -> ring transition and sco retry
114238200c1dSMilanka Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
114338200c1dSMilanka Ringwald     if (hci_event_packet_get_type(packet) != HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE) return;
114438200c1dSMilanka Ringwald     hfp_run();
1145405014fbSMatthias Ringwald }
1146405014fbSMatthias Ringwald 
1147a0ffb263SMatthias Ringwald void hfp_hf_init(uint16_t rfcomm_channel_nr){
1148520c92d5SMatthias Ringwald     hfp_init();
1149d63c37a1SMatthias Ringwald 
1150405014fbSMatthias Ringwald     hci_event_callback_registration.callback = &hci_event_packet_handler;
115127950165SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
115227950165SMatthias Ringwald 
1153e9c22d4eSMatthias Ringwald     rfcomm_register_service(rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
115427950165SMatthias Ringwald 
115527950165SMatthias Ringwald     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
1156e9c22d4eSMatthias Ringwald     hfp_set_hf_rfcomm_packet_handler(&rfcomm_packet_handler);
115727950165SMatthias Ringwald 
11581b005648SMatthias Ringwald     hfp_set_hf_run_for_context(hfp_run_for_context);
1159d68dcce1SMatthias Ringwald 
1160a0ffb263SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1161a0ffb263SMatthias Ringwald     hfp_codecs_nr = 0;
1162a0ffb263SMatthias Ringwald     hfp_indicators_nr = 0;
1163a0ffb263SMatthias Ringwald     hfp_hf_speaker_gain = 9;
1164a0ffb263SMatthias Ringwald     hfp_hf_microphone_gain = 9;
1165a0ffb263SMatthias Ringwald }
1166a0ffb263SMatthias Ringwald 
1167a0ffb263SMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, uint8_t * codecs){
11683deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
1169a0ffb263SMatthias Ringwald         log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
11703deb3ec6SMatthias Ringwald         return;
11713deb3ec6SMatthias Ringwald     }
11723deb3ec6SMatthias Ringwald 
11733deb3ec6SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
11743deb3ec6SMatthias Ringwald     int i;
11753deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
11763deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
11773deb3ec6SMatthias Ringwald     }
11783deb3ec6SMatthias Ringwald }
11793deb3ec6SMatthias Ringwald 
1180a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){
11813deb3ec6SMatthias Ringwald     hfp_supported_features = supported_features;
1182a0ffb263SMatthias Ringwald }
11833deb3ec6SMatthias Ringwald 
1184a0ffb263SMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, uint16_t * indicators){
11853deb3ec6SMatthias Ringwald     hfp_indicators_nr = indicators_nr;
11863deb3ec6SMatthias Ringwald     int i;
1187a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
11883deb3ec6SMatthias Ringwald         hfp_indicators[i] = indicators[i];
11893deb3ec6SMatthias Ringwald     }
11903deb3ec6SMatthias Ringwald }
11913deb3ec6SMatthias Ringwald 
11923deb3ec6SMatthias Ringwald void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
1193323d3000SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
11943deb3ec6SMatthias Ringwald }
11953deb3ec6SMatthias Ringwald 
1196c8626498SMilanka Ringwald void hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
11979c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1198a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1199a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1200a33eb0c4SMilanka Ringwald         return;
1201a33eb0c4SMilanka Ringwald     }
1202a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
1203a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12043deb3ec6SMatthias Ringwald }
12053deb3ec6SMatthias Ringwald 
1206c8626498SMilanka Ringwald static void hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
12079c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1208a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1209a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
12103deb3ec6SMatthias Ringwald         return;
12113deb3ec6SMatthias Ringwald     }
1212a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = enable;
1213a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12143deb3ec6SMatthias Ringwald }
12153deb3ec6SMatthias Ringwald 
1216c8626498SMilanka Ringwald void hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1217c8626498SMilanka Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1218ce263fc8SMatthias Ringwald }
1219ce263fc8SMatthias Ringwald 
1220c8626498SMilanka Ringwald void hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1221c8626498SMilanka Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1222ce263fc8SMatthias Ringwald }
1223ce263fc8SMatthias Ringwald 
12243deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
1225c8626498SMilanka Ringwald void hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
12269c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1227a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1228a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
12293deb3ec6SMatthias Ringwald         return;
12303deb3ec6SMatthias Ringwald     }
1231a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1232a0ffb263SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
1233a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12343deb3ec6SMatthias Ringwald }
12353deb3ec6SMatthias Ringwald 
1236c8626498SMilanka Ringwald void hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
12379c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1238a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1239a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
12403deb3ec6SMatthias Ringwald         return;
12413deb3ec6SMatthias Ringwald     }
1242a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
1243ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1244a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1245ce263fc8SMatthias Ringwald             break;
1246ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1247a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1248ce263fc8SMatthias Ringwald             break;
1249ce263fc8SMatthias Ringwald         default:
1250ce263fc8SMatthias Ringwald             break;
1251ce263fc8SMatthias Ringwald     }
1252a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12533deb3ec6SMatthias Ringwald }
12543deb3ec6SMatthias Ringwald 
1255c8626498SMilanka Ringwald static void hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
12569c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1257a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1258a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
12593deb3ec6SMatthias Ringwald         return;
12603deb3ec6SMatthias Ringwald     }
1261a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = enable;
1262a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12633deb3ec6SMatthias Ringwald }
12643deb3ec6SMatthias Ringwald 
1265ce263fc8SMatthias Ringwald 
1266c8626498SMilanka Ringwald void hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1267c8626498SMilanka Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1268ce263fc8SMatthias Ringwald }
1269ce263fc8SMatthias Ringwald 
1270c8626498SMilanka Ringwald void hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1271c8626498SMilanka Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1272ce263fc8SMatthias Ringwald }
1273ce263fc8SMatthias Ringwald 
127438200c1dSMilanka Ringwald static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){
127538200c1dSMilanka Ringwald     return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) &&  (hfp_supported_features  & (1<<HFP_HFSF_ESCO_S4));
127638200c1dSMilanka Ringwald }
1277ce263fc8SMatthias Ringwald 
1278c8626498SMilanka Ringwald void hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
12799c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1280a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1281a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1282a33eb0c4SMilanka Ringwald         return;
1283a33eb0c4SMilanka Ringwald     }
1284a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
1285ce263fc8SMatthias Ringwald 
1286a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
1287a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
12883deb3ec6SMatthias Ringwald 
128938200c1dSMilanka Ringwald     hfp_connection->trigger_codec_exchange = 0;
129038200c1dSMilanka Ringwald     hfp_connection->establish_audio_connection = 1;
1291a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
1292ce263fc8SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults");
1293a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
129438200c1dSMilanka Ringwald         hfp_connection->suggested_codec = HFP_CODEC_CVSD;
129538200c1dSMilanka Ringwald         hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
129638200c1dSMilanka Ringwald         hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
129738200c1dSMilanka Ringwald         hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection));
129838200c1dSMilanka Ringwald         hfp_connection->trigger_codec_exchange = 0;
129938200c1dSMilanka Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
1300ce263fc8SMatthias Ringwald     } else {
1301a0ffb263SMatthias Ringwald         switch (hfp_connection->codecs_state){
1302aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1303aa4dd815SMatthias Ringwald                 break;
1304aa4dd815SMatthias Ringwald             default:
130538200c1dSMilanka Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1306a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP;
1307aa4dd815SMatthias Ringwald                 break;
13083deb3ec6SMatthias Ringwald         }
1309ce263fc8SMatthias Ringwald     }
1310ce263fc8SMatthias Ringwald 
1311a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
13123deb3ec6SMatthias Ringwald }
13133deb3ec6SMatthias Ringwald 
1314c8626498SMilanka Ringwald void hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
13159c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1316a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1317a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1318a33eb0c4SMilanka Ringwald         return;
1319a33eb0c4SMilanka Ringwald     }
1320a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
1321a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
13223deb3ec6SMatthias Ringwald }
13233deb3ec6SMatthias Ringwald 
1324c8626498SMilanka Ringwald void hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
13259c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1326a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1327a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1328a33eb0c4SMilanka Ringwald         return;
1329a33eb0c4SMilanka Ringwald     }
1330ce263fc8SMatthias Ringwald 
1331ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1332a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 1;
1333a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1334ce263fc8SMatthias Ringwald     } else {
1335ce263fc8SMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
1336ce263fc8SMatthias Ringwald     }
1337ce263fc8SMatthias Ringwald }
1338ce263fc8SMatthias Ringwald 
1339c8626498SMilanka Ringwald void hfp_hf_terminate_call(hci_con_handle_t acl_handle){
13409c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1341a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1342a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1343a33eb0c4SMilanka Ringwald         return;
1344a33eb0c4SMilanka Ringwald     }
1345a0ffb263SMatthias Ringwald     hfp_connection->hf_send_chup = 1;
1346a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1347ce263fc8SMatthias Ringwald }
1348ce263fc8SMatthias Ringwald 
1349c8626498SMilanka Ringwald void hfp_hf_reject_incoming_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){
1357a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 1;
1358a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1359ce263fc8SMatthias Ringwald     }
1360ce263fc8SMatthias Ringwald }
1361ce263fc8SMatthias Ringwald 
1362c8626498SMilanka Ringwald void hfp_hf_user_busy(hci_con_handle_t acl_handle){
13639c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1364a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1365a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1366a33eb0c4SMilanka Ringwald         return;
1367a33eb0c4SMilanka Ringwald     }
1368ce263fc8SMatthias Ringwald 
1369ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1370a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 1;
1371a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1372ce263fc8SMatthias Ringwald     }
1373ce263fc8SMatthias Ringwald }
1374ce263fc8SMatthias Ringwald 
1375c8626498SMilanka Ringwald void hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){
13769c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1377a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1378a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1379a33eb0c4SMilanka Ringwald         return;
1380a33eb0c4SMilanka Ringwald     }
1381ce263fc8SMatthias Ringwald 
1382ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1383ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1384a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 1;
1385a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1386ce263fc8SMatthias Ringwald     }
1387ce263fc8SMatthias Ringwald }
1388ce263fc8SMatthias Ringwald 
1389c8626498SMilanka Ringwald void hfp_hf_swap_calls(hci_con_handle_t acl_handle){
13909c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1391a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1392a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1393a33eb0c4SMilanka Ringwald         return;
1394a33eb0c4SMilanka Ringwald     }
1395ce263fc8SMatthias Ringwald 
1396ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1397ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1398a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 1;
1399a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1400ce263fc8SMatthias Ringwald     }
1401ce263fc8SMatthias Ringwald }
1402ce263fc8SMatthias Ringwald 
1403c8626498SMilanka Ringwald void hfp_hf_join_held_call(hci_con_handle_t acl_handle){
14049c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1405a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1406a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1407a33eb0c4SMilanka Ringwald         return;
1408a33eb0c4SMilanka Ringwald     }
1409ce263fc8SMatthias Ringwald 
1410ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1411ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1412a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 1;
1413a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1414ce263fc8SMatthias Ringwald     }
1415ce263fc8SMatthias Ringwald }
1416ce263fc8SMatthias Ringwald 
1417c8626498SMilanka Ringwald void hfp_hf_connect_calls(hci_con_handle_t acl_handle){
14189c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1419a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1420a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1421a33eb0c4SMilanka Ringwald         return;
1422a33eb0c4SMilanka Ringwald     }
1423ce263fc8SMatthias Ringwald 
1424ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1425ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1426a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 1;
1427a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1428ce263fc8SMatthias Ringwald     }
1429ce263fc8SMatthias Ringwald }
1430ce263fc8SMatthias Ringwald 
1431c8626498SMilanka Ringwald void hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){
14329c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1433a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1434a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1435a33eb0c4SMilanka Ringwald         return;
1436a33eb0c4SMilanka Ringwald     }
1437667ec068SMatthias Ringwald 
1438667ec068SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1439667ec068SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1440a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1441a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 10 + index;
1442a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1443667ec068SMatthias Ringwald     }
1444667ec068SMatthias Ringwald }
1445667ec068SMatthias Ringwald 
1446c8626498SMilanka Ringwald void hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
14479c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1448a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1449a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1450a33eb0c4SMilanka Ringwald         return;
1451a33eb0c4SMilanka Ringwald     }
1452667ec068SMatthias Ringwald 
1453667ec068SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1454667ec068SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1455a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1456a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 20 + index;
1457a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1458667ec068SMatthias Ringwald     }
1459667ec068SMatthias Ringwald }
1460ce263fc8SMatthias Ringwald 
1461c8626498SMilanka Ringwald void hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
14629c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1463a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1464a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1465a33eb0c4SMilanka Ringwald         return;
1466a33eb0c4SMilanka Ringwald     }
1467ce263fc8SMatthias Ringwald 
1468a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_outgoing_call = 1;
1469ce263fc8SMatthias Ringwald     snprintf(phone_number, sizeof(phone_number), "%s", number);
1470a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1471ce263fc8SMatthias Ringwald }
1472ce263fc8SMatthias Ringwald 
1473c8626498SMilanka Ringwald void hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
14749c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1475a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1476a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1477a33eb0c4SMilanka Ringwald         return;
1478a33eb0c4SMilanka Ringwald     }
1479ce263fc8SMatthias Ringwald 
1480a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_memory_dialing = 1;
1481a0ffb263SMatthias Ringwald     hfp_connection->memory_id = memory_id;
1482a0ffb263SMatthias Ringwald 
1483a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1484ce263fc8SMatthias Ringwald }
1485ce263fc8SMatthias Ringwald 
1486c8626498SMilanka Ringwald void hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
14879c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1488a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1489a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1490a33eb0c4SMilanka Ringwald         return;
1491a33eb0c4SMilanka Ringwald     }
1492ce263fc8SMatthias Ringwald 
1493a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_redial_last_number = 1;
1494a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1495ce263fc8SMatthias Ringwald }
1496ce263fc8SMatthias Ringwald 
1497c8626498SMilanka Ringwald void hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
14989c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1499a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1500a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1501a33eb0c4SMilanka Ringwald         return;
1502a33eb0c4SMilanka Ringwald     }
1503ce263fc8SMatthias Ringwald 
1504a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_call_waiting_notification = 1;
1505a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1506ce263fc8SMatthias Ringwald }
1507ce263fc8SMatthias Ringwald 
1508ce263fc8SMatthias Ringwald 
1509c8626498SMilanka Ringwald void hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
15109c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1511a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1512a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1513a33eb0c4SMilanka Ringwald         return;
1514a33eb0c4SMilanka Ringwald     }
1515ce263fc8SMatthias Ringwald 
1516a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_call_waiting_notification = 1;
1517a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1518ce263fc8SMatthias Ringwald }
1519ce263fc8SMatthias Ringwald 
1520ce263fc8SMatthias Ringwald 
1521c8626498SMilanka Ringwald void hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){
15229c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1523a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1524a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1525a33eb0c4SMilanka Ringwald         return;
1526a33eb0c4SMilanka Ringwald     }
1527ce263fc8SMatthias Ringwald 
1528a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_calling_line_notification = 1;
1529a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1530ce263fc8SMatthias Ringwald }
1531ce263fc8SMatthias Ringwald 
1532c8626498SMilanka Ringwald void hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
15339c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1534a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1535a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1536a33eb0c4SMilanka Ringwald         return;
1537a33eb0c4SMilanka Ringwald     }
1538ce263fc8SMatthias Ringwald 
1539a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_calling_line_notification = 1;
1540a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1541ce263fc8SMatthias Ringwald }
1542ce263fc8SMatthias Ringwald 
1543ce263fc8SMatthias Ringwald 
1544c8626498SMilanka Ringwald void hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
15459c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1546a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1547a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1548a33eb0c4SMilanka Ringwald         return;
1549a33eb0c4SMilanka Ringwald     }
1550ce263fc8SMatthias Ringwald 
1551a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1;
1552a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1553ce263fc8SMatthias Ringwald }
1554ce263fc8SMatthias Ringwald 
1555c8626498SMilanka Ringwald void hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
15569c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1557a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1558a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1559a33eb0c4SMilanka Ringwald         return;
1560a33eb0c4SMilanka Ringwald     }
1561ce263fc8SMatthias Ringwald 
1562a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
1563a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1564ce263fc8SMatthias Ringwald }
1565ce263fc8SMatthias Ringwald 
1566c8626498SMilanka Ringwald void hfp_hf_activate_voice_recognition_notification(hci_con_handle_t acl_handle){
15679c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1568a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1569a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1570a33eb0c4SMilanka Ringwald         return;
1571a33eb0c4SMilanka Ringwald     }
1572ce263fc8SMatthias Ringwald 
1573a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_voice_recognition_notification = 1;
1574a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1575ce263fc8SMatthias Ringwald }
1576ce263fc8SMatthias Ringwald 
1577c8626498SMilanka Ringwald void hfp_hf_deactivate_voice_recognition_notification(hci_con_handle_t acl_handle){
15789c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1579a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1580a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1581a33eb0c4SMilanka Ringwald         return;
1582a33eb0c4SMilanka Ringwald     }
1583ce263fc8SMatthias Ringwald 
1584a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_voice_recognition_notification = 1;
1585a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1586ce263fc8SMatthias Ringwald }
1587ce263fc8SMatthias Ringwald 
1588c8626498SMilanka Ringwald void hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
15899c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1590a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1591a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1592a33eb0c4SMilanka Ringwald         return;
1593a33eb0c4SMilanka Ringwald     }
1594c8626498SMilanka Ringwald 
1595a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain == gain) return;
1596a0ffb263SMatthias Ringwald     if (gain < 0 || gain > 15){
1597a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1598a0ffb263SMatthias Ringwald         return;
1599a0ffb263SMatthias Ringwald     }
1600a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = gain;
1601a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
1602a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1603ce263fc8SMatthias Ringwald }
1604ce263fc8SMatthias Ringwald 
1605c8626498SMilanka Ringwald void hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
16069c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1607a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1608a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1609a33eb0c4SMilanka Ringwald         return;
1610a33eb0c4SMilanka Ringwald     }
1611c8626498SMilanka Ringwald 
1612a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain == gain) return;
1613a0ffb263SMatthias Ringwald     if (gain < 0 || gain > 15){
1614a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1615a0ffb263SMatthias Ringwald         return;
1616a0ffb263SMatthias Ringwald     }
1617a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = gain;
1618a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
1619a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1620ce263fc8SMatthias Ringwald }
1621ce263fc8SMatthias Ringwald 
1622c8626498SMilanka Ringwald void hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
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     }
1628a33eb0c4SMilanka Ringwald 
1629a0ffb263SMatthias Ringwald     hfp_connection->hf_send_dtmf_code = code;
1630a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1631ce263fc8SMatthias Ringwald }
1632ce263fc8SMatthias Ringwald 
1633c8626498SMilanka Ringwald void hfp_hf_request_phone_number_for_voice_tag(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_binp = 1;
1640a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1641ce263fc8SMatthias Ringwald }
16423deb3ec6SMatthias Ringwald 
1643c8626498SMilanka Ringwald void hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
16449c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1645a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1646a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1647a33eb0c4SMilanka Ringwald         return;
1648a33eb0c4SMilanka Ringwald     }
1649a0ffb263SMatthias Ringwald     hfp_connection->hf_send_clcc = 1;
1650a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1651667ec068SMatthias Ringwald }
1652667ec068SMatthias Ringwald 
1653667ec068SMatthias Ringwald 
1654c8626498SMilanka Ringwald void hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
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     }
1660a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1661a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '?';
1662a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1663667ec068SMatthias Ringwald }
1664667ec068SMatthias Ringwald 
1665c8626498SMilanka Ringwald void hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
16669c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1667a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1668a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1669a33eb0c4SMilanka Ringwald         return;
1670a33eb0c4SMilanka Ringwald     }
1671a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1672a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '0';
1673a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1674667ec068SMatthias Ringwald }
1675667ec068SMatthias Ringwald 
1676c8626498SMilanka Ringwald void hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
16779c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1678a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1679a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1680a33eb0c4SMilanka Ringwald         return;
1681a33eb0c4SMilanka Ringwald     }
1682a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1683a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '1';
1684a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1685667ec068SMatthias Ringwald }
1686667ec068SMatthias Ringwald 
1687c8626498SMilanka Ringwald void hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
16889c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1689a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1690a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1691a33eb0c4SMilanka Ringwald         return;
1692a33eb0c4SMilanka Ringwald     }
1693a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1694a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '2';
1695a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1696667ec068SMatthias Ringwald }
1697667ec068SMatthias Ringwald 
1698c8626498SMilanka Ringwald void hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
16999c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1700a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1701a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1702a33eb0c4SMilanka Ringwald         return;
1703a33eb0c4SMilanka Ringwald     }
1704a0ffb263SMatthias Ringwald     hfp_connection->hf_send_cnum = 1;
1705a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1706667ec068SMatthias Ringwald }
1707667ec068SMatthias Ringwald 
1708c8626498SMilanka Ringwald void hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
17099c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1710a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1711a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1712a33eb0c4SMilanka Ringwald         return;
1713a33eb0c4SMilanka Ringwald     }
1714667ec068SMatthias Ringwald     // find index for assigned number
1715667ec068SMatthias Ringwald     int i;
1716667ec068SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
1717667ec068SMatthias Ringwald         if (hfp_indicators[i] == assigned_number){
1718667ec068SMatthias Ringwald             // set value
1719667ec068SMatthias Ringwald             hfp_indicators_value[i] = value;
1720667ec068SMatthias Ringwald             // mark for update
1721a0ffb263SMatthias Ringwald             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
1722a0ffb263SMatthias Ringwald                 hfp_connection->generic_status_update_bitmap |= (1<<i);
1723667ec068SMatthias Ringwald                 // send update
1724a0ffb263SMatthias Ringwald                 hfp_run_for_context(hfp_connection);
1725a0ffb263SMatthias Ringwald             }
1726667ec068SMatthias Ringwald             return;
1727667ec068SMatthias Ringwald         }
1728667ec068SMatthias Ringwald     }
1729667ec068SMatthias Ringwald }
1730667ec068SMatthias Ringwald 
1731d7f6b5cbSMatthias Ringwald int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){
1732d7f6b5cbSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1733d7f6b5cbSMatthias Ringwald     if (!hfp_connection) {
1734d7f6b5cbSMatthias Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1735d7f6b5cbSMatthias Ringwald         return 0;
1736d7f6b5cbSMatthias Ringwald     }
1737d7f6b5cbSMatthias Ringwald     return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
1738d7f6b5cbSMatthias Ringwald }
1739