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 232fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 242fca4dadSMilanka Ringwald * GMBH 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 38e501bae0SMatthias 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> 493cfa4086SMatthias Ringwald #include <stdio.h> 503deb3ec6SMatthias Ringwald #include <string.h> 513deb3ec6SMatthias Ringwald 52235946f1SMatthias Ringwald #include "bluetooth_sdp.h" 5359c6af15SMatthias Ringwald #include "btstack_debug.h" 54d4dd47ffSMatthias Ringwald #include "btstack_event.h" 553deb3ec6SMatthias Ringwald #include "btstack_memory.h" 5659c6af15SMatthias Ringwald #include "btstack_run_loop.h" 5759c6af15SMatthias Ringwald #include "classic/core.h" 5859c6af15SMatthias Ringwald #include "classic/hfp.h" 5959c6af15SMatthias Ringwald #include "classic/hfp_hf.h" 60efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h" 61746ccb7eSMatthias Ringwald #include "classic/sdp_server.h" 62023f2764SMatthias Ringwald #include "classic/sdp_util.h" 6359c6af15SMatthias Ringwald #include "hci.h" 6459c6af15SMatthias Ringwald #include "hci_cmd.h" 6559c6af15SMatthias Ringwald #include "hci_dump.h" 6659c6af15SMatthias Ringwald #include "l2cap.h" 673deb3ec6SMatthias Ringwald 6820b2edb6SMatthias Ringwald // const 69aeb0f0feSMatthias Ringwald static const char hfp_hf_default_service_name[] = "Hands-Free unit"; 7020b2edb6SMatthias Ringwald 7120b2edb6SMatthias Ringwald // globals 72aeb0f0feSMatthias Ringwald 73aeb0f0feSMatthias Ringwald // higher layer callbacks 74aeb0f0feSMatthias Ringwald static btstack_packet_handler_t hfp_hf_callback; 75aeb0f0feSMatthias Ringwald 761c6a0fc0SMatthias Ringwald static btstack_packet_callback_registration_t hfp_hf_hci_event_callback_registration; 7727950165SMatthias Ringwald 78aeb0f0feSMatthias Ringwald static uint16_t hfp_hf_supported_features; 79aeb0f0feSMatthias Ringwald static uint8_t hfp_hf_codecs_nr; 80aeb0f0feSMatthias Ringwald static uint8_t hfp_hf_codecs[HFP_MAX_NUM_CODECS]; 813deb3ec6SMatthias Ringwald 82aeb0f0feSMatthias Ringwald static uint8_t hfp_hf_indicators_nr; 83aeb0f0feSMatthias Ringwald static uint8_t hfp_hf_indicators[HFP_MAX_NUM_INDICATORS]; 84aeb0f0feSMatthias Ringwald static uint32_t hfp_hf_indicators_value[HFP_MAX_NUM_INDICATORS]; 85667ec068SMatthias Ringwald 8620b2edb6SMatthias Ringwald static uint8_t hfp_hf_speaker_gain; 8720b2edb6SMatthias Ringwald static uint8_t hfp_hf_microphone_gain; 883deb3ec6SMatthias Ringwald 89aeb0f0feSMatthias Ringwald static hfp_call_status_t hfp_hf_call_status; 90aeb0f0feSMatthias Ringwald static hfp_callsetup_status_t hfp_hf_callsetup_status; 91aeb0f0feSMatthias Ringwald static hfp_callheld_status_t hfp_hf_callheld_status; 923deb3ec6SMatthias Ringwald 93aeb0f0feSMatthias Ringwald static char hfp_hf_phone_number[25]; 94ce263fc8SMatthias Ringwald 95ce263fc8SMatthias Ringwald 9676cc1527SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){ 97aeb0f0feSMatthias Ringwald int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_CODEC_NEGOTIATION); 9876cc1527SMatthias Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION); 9976cc1527SMatthias Ringwald return hf && ag; 10076cc1527SMatthias Ringwald } 10176cc1527SMatthias Ringwald 10276cc1527SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){ 103aeb0f0feSMatthias Ringwald int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_THREE_WAY_CALLING); 10476cc1527SMatthias Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING); 10576cc1527SMatthias Ringwald return hf && ag; 10676cc1527SMatthias Ringwald } 10776cc1527SMatthias Ringwald 10876cc1527SMatthias Ringwald 10976cc1527SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){ 110aeb0f0feSMatthias Ringwald int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_HF_INDICATORS); 11176cc1527SMatthias Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS); 11276cc1527SMatthias Ringwald return hf && ag; 11376cc1527SMatthias Ringwald } 11476cc1527SMatthias Ringwald 115fcf4ede6SMilanka Ringwald static bool hfp_hf_vra_flag_supported(hfp_connection_t * hfp_connection){ 116fcf4ede6SMilanka Ringwald int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION); 117fcf4ede6SMilanka Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION); 118fcf4ede6SMilanka Ringwald return hf && ag; 119fcf4ede6SMilanka Ringwald } 120fcf4ede6SMilanka Ringwald 121fcf4ede6SMilanka Ringwald static bool hfp_hf_enhanced_vra_flag_supported(hfp_connection_t * hfp_connection){ 122fcf4ede6SMilanka Ringwald int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS); 123fcf4ede6SMilanka Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS); 124fcf4ede6SMilanka Ringwald return hf && ag; 125fcf4ede6SMilanka Ringwald } 12676cc1527SMatthias Ringwald 1279c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_hf_connection_context_for_acl_handle(uint16_t handle){ 1289c9c64c1SMatthias Ringwald btstack_linked_list_iterator_t it; 1299c9c64c1SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1309c9c64c1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1319c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1329c9c64c1SMatthias Ringwald if (hfp_connection->acl_handle != handle) continue; 1339c9c64c1SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_HF) continue; 1349c9c64c1SMatthias Ringwald return hfp_connection; 1359c9c64c1SMatthias Ringwald } 1369c9c64c1SMatthias Ringwald return NULL; 1379c9c64c1SMatthias Ringwald } 1389c9c64c1SMatthias Ringwald 139c10fde09SMatthias Ringwald /* emit functions */ 1403deb3ec6SMatthias Ringwald 141a473a009SMatthias Ringwald static void hfp_hf_emit_subscriber_information(const hfp_connection_t * hfp_connection, uint8_t status){ 142a473a009SMatthias Ringwald if (hfp_hf_callback == NULL) return; 143c10fde09SMatthias Ringwald uint16_t bnip_number_len = btstack_min(strlen(hfp_connection->bnip_number), sizeof(hfp_connection->bnip_number)-1); 144c10fde09SMatthias Ringwald uint8_t event[7 + sizeof(hfp_connection->bnip_number)]; 145a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 146c10fde09SMatthias Ringwald event[1] = 6 + bnip_number_len; 147a473a009SMatthias Ringwald event[2] = HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION; 148d703d377SMatthias Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 149d703d377SMatthias Ringwald event[5] = status; 150d703d377SMatthias Ringwald event[6] = hfp_connection->bnip_type; 151c10fde09SMatthias Ringwald memcpy(&event[7], hfp_connection->bnip_number, bnip_number_len); 152c10fde09SMatthias Ringwald event[7 + bnip_number_len] = 0; 153c10fde09SMatthias Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, 8 + bnip_number_len); 154a0ffb263SMatthias Ringwald } 155a0ffb263SMatthias Ringwald 156a473a009SMatthias Ringwald static void hfp_hf_emit_type_and_number(const hfp_connection_t * hfp_connection, uint8_t event_subtype){ 157a473a009SMatthias Ringwald if (hfp_hf_callback == NULL) return; 158c10fde09SMatthias Ringwald uint16_t bnip_number_len = btstack_min(strlen(hfp_connection->bnip_number), sizeof(hfp_connection->bnip_number)-1); 159c10fde09SMatthias Ringwald uint8_t event[6 + sizeof(hfp_connection->bnip_number)]; 160a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 161c10fde09SMatthias Ringwald event[1] = 5 + bnip_number_len; 162a0ffb263SMatthias Ringwald event[2] = event_subtype; 163d703d377SMatthias Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 164d703d377SMatthias Ringwald event[5] = hfp_connection->bnip_type; 165c10fde09SMatthias Ringwald memcpy(&event[6], hfp_connection->bnip_number, bnip_number_len); 166c10fde09SMatthias Ringwald event[6 + bnip_number_len] = 0; 167c10fde09SMatthias Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, 7 + bnip_number_len); 168a0ffb263SMatthias Ringwald } 169a0ffb263SMatthias Ringwald 170a473a009SMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(const hfp_connection_t * hfp_connection){ 171a473a009SMatthias Ringwald if (hfp_hf_callback == NULL) return; 172c10fde09SMatthias Ringwald uint16_t bnip_number_len = btstack_min(strlen(hfp_connection->bnip_number), sizeof(hfp_connection->bnip_number)-1); 173c10fde09SMatthias Ringwald uint8_t event[11 + sizeof(hfp_connection->bnip_number)]; 174c10fde09SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 175c10fde09SMatthias Ringwald event[1] = 10 + bnip_number_len; 176c10fde09SMatthias Ringwald event[2] = HFP_SUBEVENT_ENHANCED_CALL_STATUS; 177c10fde09SMatthias Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 178c10fde09SMatthias Ringwald event[4] = hfp_connection->clcc_idx; 179c10fde09SMatthias Ringwald event[5] = hfp_connection->clcc_dir; 180c10fde09SMatthias Ringwald event[6] = hfp_connection->clcc_status; 181c10fde09SMatthias Ringwald event[7] = hfp_connection->clcc_mode; 182c10fde09SMatthias Ringwald event[8] = hfp_connection->clcc_mpty; 183c10fde09SMatthias Ringwald event[9] = hfp_connection->bnip_type; 184c10fde09SMatthias Ringwald memcpy(&event[10], hfp_connection->bnip_number, bnip_number_len); 185c10fde09SMatthias Ringwald event[11+bnip_number_len] = 0; 186c10fde09SMatthias Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, 12+bnip_number_len); 187a0ffb263SMatthias Ringwald } 188a0ffb263SMatthias Ringwald 1891ac1f60fSMilanka Ringwald static void hfp_emit_ag_indicator_mapping_event(const hfp_connection_t * hfp_connection, const hfp_ag_indicator_t * indicator){ 1901ac1f60fSMilanka Ringwald if (hfp_hf_callback == NULL) return; 191c10fde09SMatthias Ringwald uint8_t event[8 + HFP_MAX_INDICATOR_DESC_SIZE]; 192c10fde09SMatthias Ringwald uint16_t indicator_len = btstack_min(strlen(indicator->name), HFP_MAX_INDICATOR_DESC_SIZE-1); 193c10fde09SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 194c10fde09SMatthias Ringwald event[1] = 7 + indicator_len; 195c10fde09SMatthias Ringwald event[2] = HFP_SUBEVENT_AG_INDICATOR_MAPPING; 196c10fde09SMatthias Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 197c10fde09SMatthias Ringwald event[5] = indicator->index; 198c10fde09SMatthias Ringwald event[6] = indicator->min_range; 199c10fde09SMatthias Ringwald event[7] = indicator->max_range; 200c10fde09SMatthias Ringwald memcpy(&event[8], indicator->name, indicator_len); 201c10fde09SMatthias Ringwald event[8+indicator_len] = 0; 202c10fde09SMatthias Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, 9 + indicator_len); 2031ac1f60fSMilanka Ringwald } 20476cc1527SMatthias Ringwald 205ce3797e1SMilanka Ringwald static void hfp_emit_ag_indicator_status_event(const hfp_connection_t * hfp_connection, const hfp_ag_indicator_t * indicator){ 206a473a009SMatthias Ringwald if (hfp_hf_callback == NULL) return; 207c10fde09SMatthias Ringwald uint8_t event[12+HFP_MAX_INDICATOR_DESC_SIZE]; 208c10fde09SMatthias Ringwald uint16_t indicator_len = btstack_min(strlen(indicator->name), HFP_MAX_INDICATOR_DESC_SIZE-1); 209c10fde09SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 210c10fde09SMatthias Ringwald event[1] = 11 + indicator_len; 211c10fde09SMatthias Ringwald event[2] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED; 212c10fde09SMatthias Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 213c10fde09SMatthias Ringwald event[5] = indicator->index; 214c10fde09SMatthias Ringwald event[6] = indicator->status; 215c10fde09SMatthias Ringwald event[7] = indicator->min_range; 216c10fde09SMatthias Ringwald event[8] = indicator->max_range; 217c10fde09SMatthias Ringwald event[9] = indicator->mandatory; 218c10fde09SMatthias Ringwald event[10] = indicator->enabled; 219c10fde09SMatthias Ringwald event[11] = indicator->status_changed; 220c10fde09SMatthias Ringwald memcpy(&event[12], indicator->name, indicator_len); 221c10fde09SMatthias Ringwald event[12+indicator_len] = 0; 222c10fde09SMatthias Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, 13 + indicator_len); 2233deb3ec6SMatthias Ringwald } 2243deb3ec6SMatthias Ringwald 225a473a009SMatthias Ringwald static void hfp_emit_network_operator_event(const hfp_connection_t * hfp_connection){ 226a473a009SMatthias Ringwald if (hfp_hf_callback == NULL) return; 227c10fde09SMatthias Ringwald uint16_t operator_len = btstack_min(strlen(hfp_connection->network_operator.name), HFP_MAX_NETWORK_OPERATOR_NAME_SIZE-1); 228c10fde09SMatthias Ringwald uint8_t event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE]; 22976cc1527SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 23076cc1527SMatthias Ringwald event[1] = sizeof(event) - 2; 231c10fde09SMatthias Ringwald event[2] = 6 + operator_len; 232d703d377SMatthias Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 233d703d377SMatthias Ringwald event[5] = hfp_connection->network_operator.mode; 234d703d377SMatthias Ringwald event[6] = hfp_connection->network_operator.format; 235c10fde09SMatthias Ringwald memcpy(&event[7], hfp_connection->network_operator.name, operator_len); 236c10fde09SMatthias Ringwald event[7+operator_len] = 0; 237c10fde09SMatthias Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, 8 + operator_len); 2383deb3ec6SMatthias Ringwald } 2393deb3ec6SMatthias Ringwald 240b95cac54SMilanka Ringwald 241b95cac54SMilanka Ringwald static void hfp_hf_emit_enhanced_voice_recognition_text(hfp_connection_t * hfp_connection){ 242b95cac54SMilanka Ringwald btstack_assert(hfp_connection != NULL); 24351aa5d5aSMilanka Ringwald uint8_t event[HFP_MAX_VR_TEXT_SIZE + 11]; 244b95cac54SMilanka Ringwald int pos = 0; 245b95cac54SMilanka Ringwald event[pos++] = HCI_EVENT_HFP_META; 246b95cac54SMilanka Ringwald event[pos++] = sizeof(event) - 2; 247b95cac54SMilanka Ringwald event[pos++] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_MESSAGE; 248b95cac54SMilanka Ringwald little_endian_store_16(event, pos, hfp_connection->acl_handle); 249b95cac54SMilanka Ringwald pos += 2; 250b95cac54SMilanka Ringwald little_endian_store_16(event, pos, hfp_connection->ag_msg.text_id); 251b95cac54SMilanka Ringwald pos += 2; 252b95cac54SMilanka Ringwald event[pos++] = hfp_connection->ag_msg.text_type; 253e83f1be7SMilanka Ringwald event[pos++] = hfp_connection->ag_msg.text_operation; 254b95cac54SMilanka Ringwald 25551aa5d5aSMilanka Ringwald // length, zero ending is already in message 256b95cac54SMilanka Ringwald uint8_t * value = &hfp_connection->line_buffer[0]; 25751aa5d5aSMilanka Ringwald uint16_t value_length = hfp_connection->ag_vra_msg_length; 25851aa5d5aSMilanka Ringwald 25951aa5d5aSMilanka Ringwald little_endian_store_16(event, pos, value_length); 260b95cac54SMilanka Ringwald pos += 2; 26151aa5d5aSMilanka Ringwald memcpy(&event[pos], value, value_length); 26251aa5d5aSMilanka Ringwald pos += value_length; 26351aa5d5aSMilanka Ringwald 264b95cac54SMilanka Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, pos); 265b95cac54SMilanka Ringwald } 266b95cac54SMilanka Ringwald 26776cc1527SMatthias Ringwald /* send commands */ 26889425bfcSMilanka Ringwald 26989425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){ 2703deb3ec6SMatthias Ringwald char buffer[20]; 2711599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s\r", cmd); 27289425bfcSMilanka Ringwald return send_str_over_rfcomm(cid, buffer); 27389425bfcSMilanka Ringwald } 27489425bfcSMilanka Ringwald 27589425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){ 27689425bfcSMilanka Ringwald char buffer[20]; 2771599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s%s\r", cmd, mark); 27889425bfcSMilanka Ringwald return send_str_over_rfcomm(cid, buffer); 27989425bfcSMilanka Ringwald } 28089425bfcSMilanka Ringwald 28186da9d74SMatthias Ringwald static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint16_t value){ 28289425bfcSMilanka Ringwald char buffer[40]; 2831599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s=%d\r", cmd, value); 2843deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2853deb3ec6SMatthias Ringwald } 2863deb3ec6SMatthias Ringwald 2873deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){ 2883deb3ec6SMatthias Ringwald char buffer[30]; 28989425bfcSMilanka Ringwald const int size = sizeof(buffer); 29089425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS); 291aeb0f0feSMatthias Ringwald offset += join(buffer+offset, size-offset, hfp_hf_codecs, hfp_hf_codecs_nr); 2921599fe57SMatthias Ringwald offset += snprintf(buffer+offset, size-offset, "\r"); 2933deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2943deb3ec6SMatthias Ringwald } 2953deb3ec6SMatthias Ringwald 2963deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){ 2973deb3ec6SMatthias Ringwald char buffer[50]; 29889425bfcSMilanka Ringwald const int size = sizeof(buffer); 29989425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS); 30089425bfcSMilanka Ringwald offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr); 3011599fe57SMatthias Ringwald offset += snprintf(buffer+offset, size-offset, "\r"); 3023deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 3033deb3ec6SMatthias Ringwald } 3043deb3ec6SMatthias Ringwald 3053deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){ 3063deb3ec6SMatthias Ringwald char buffer[30]; 30789425bfcSMilanka Ringwald const int size = sizeof(buffer); 30889425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR); 309aeb0f0feSMatthias Ringwald offset += join(buffer+offset, size-offset, hfp_hf_indicators, hfp_hf_indicators_nr); 3101599fe57SMatthias Ringwald offset += snprintf(buffer+offset, size-offset, "\r"); 3113deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 3123deb3ec6SMatthias Ringwald } 3133deb3ec6SMatthias Ringwald 31489425bfcSMilanka Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){ 3153deb3ec6SMatthias Ringwald char buffer[20]; 3161599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate); 317ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 318ce263fc8SMatthias Ringwald } 319ce263fc8SMatthias Ringwald 320ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){ 321ce263fc8SMatthias Ringwald char buffer[40]; 322aeb0f0feSMatthias Ringwald snprintf(buffer, sizeof(buffer), "%s%s;\r", HFP_CALL_PHONE_NUMBER, hfp_hf_phone_number); 323ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 324ce263fc8SMatthias Ringwald } 325ce263fc8SMatthias Ringwald 326a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){ 327ce263fc8SMatthias Ringwald char buffer[40]; 3281599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "%s>%d;\r", HFP_CALL_PHONE_NUMBER, memory_id); 329ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 330ce263fc8SMatthias Ringwald } 331ce263fc8SMatthias Ringwald 332f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){ 33389425bfcSMilanka Ringwald char buffer[40]; 3341599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s=%u\r", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number); 335ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 336ce263fc8SMatthias Ringwald } 337ce263fc8SMatthias Ringwald 338ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){ 339ce263fc8SMatthias Ringwald char buffer[20]; 3401599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s=%c\r", HFP_TRANSMIT_DTMF_CODES, code); 341ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 342ce263fc8SMatthias Ringwald } 343ce263fc8SMatthias Ringwald 34497d2cadbSMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){ 3451599fe57SMatthias Ringwald return send_str_over_rfcomm(cid, (char *) "ATA\r"); 34697d2cadbSMatthias Ringwald } 34797d2cadbSMatthias Ringwald 34889425bfcSMilanka Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){ 349aeb0f0feSMatthias Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_hf_supported_features); 35089425bfcSMilanka Ringwald } 35189425bfcSMilanka Ringwald 35289425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){ 35389425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?"); 35489425bfcSMilanka Ringwald } 35589425bfcSMilanka Ringwald 35689425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){ 35789425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?"); 35889425bfcSMilanka Ringwald } 35989425bfcSMilanka Ringwald 36089425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){ 36189425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?"); 36289425bfcSMilanka Ringwald } 36389425bfcSMilanka Ringwald 36489425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){ 36589425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?"); 36689425bfcSMilanka Ringwald } 36789425bfcSMilanka Ringwald 36889425bfcSMilanka Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){ 36989425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?"); 37089425bfcSMilanka Ringwald } 37189425bfcSMilanka Ringwald 37289425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){ 37389425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0"); 37489425bfcSMilanka Ringwald } 37589425bfcSMilanka Ringwald 37689425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){ 37789425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?"); 37889425bfcSMilanka Ringwald } 37989425bfcSMilanka Ringwald 38089425bfcSMilanka Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){ 38189425bfcSMilanka Ringwald return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP); 38289425bfcSMilanka Ringwald } 38389425bfcSMilanka Ringwald 38489425bfcSMilanka Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){ 38589425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain); 38689425bfcSMilanka Ringwald } 38789425bfcSMilanka Ringwald 38889425bfcSMilanka Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){ 38989425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain); 39089425bfcSMilanka Ringwald } 39189425bfcSMilanka Ringwald 39289425bfcSMilanka Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){ 39389425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate); 39489425bfcSMilanka Ringwald } 39589425bfcSMilanka Ringwald 39689425bfcSMilanka Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){ 39789425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate); 39889425bfcSMilanka Ringwald } 39989425bfcSMilanka Ringwald 40089425bfcSMilanka Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){ 40189425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate); 40289425bfcSMilanka Ringwald } 40389425bfcSMilanka Ringwald 40489425bfcSMilanka Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){ 40589425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec); 40689425bfcSMilanka Ringwald } 40789425bfcSMilanka Ringwald 40889425bfcSMilanka Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){ 40989425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable); 41089425bfcSMilanka Ringwald } 41189425bfcSMilanka Ringwald 41289425bfcSMilanka Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){ 41389425bfcSMilanka Ringwald return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER); 41489425bfcSMilanka Ringwald } 41589425bfcSMilanka Ringwald 41689425bfcSMilanka Ringwald static int hfp_hf_send_chup(uint16_t cid){ 41789425bfcSMilanka Ringwald return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL); 41889425bfcSMilanka Ringwald } 41989425bfcSMilanka Ringwald 420ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){ 42189425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1"); 422ce263fc8SMatthias Ringwald } 423ce263fc8SMatthias Ringwald 424667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){ 42589425bfcSMilanka Ringwald return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS); 426667ec068SMatthias Ringwald } 427667ec068SMatthias Ringwald 42876cc1527SMatthias Ringwald /* state machines */ 4293deb3ec6SMatthias Ringwald 430a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){ 431a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 432a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 433aa4dd815SMatthias Ringwald int done = 1; 434498a8121SMilanka Ringwald log_info("hfp_hf_run_for_context_service_level_connection state %d\n", hfp_connection->state); 435a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 4363deb3ec6SMatthias Ringwald case HFP_EXCHANGE_SUPPORTED_FEATURES: 437aeb0f0feSMatthias Ringwald hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_hf_codecs, &hfp_hf_codecs_nr); 438a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES; 439a0ffb263SMatthias Ringwald hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid); 4403deb3ec6SMatthias Ringwald break; 4413deb3ec6SMatthias Ringwald case HFP_NOTIFY_ON_CODECS: 442a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS; 443a0ffb263SMatthias Ringwald hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 4443deb3ec6SMatthias Ringwald break; 4453deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INDICATORS: 446a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 447a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid); 4483deb3ec6SMatthias Ringwald break; 4493deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INDICATORS_STATUS: 450a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS; 451a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid); 4523deb3ec6SMatthias Ringwald break; 4533deb3ec6SMatthias Ringwald case HFP_ENABLE_INDICATORS_STATUS_UPDATE: 454a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE; 455a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1); 4563deb3ec6SMatthias Ringwald break; 4573deb3ec6SMatthias Ringwald case HFP_RETRIEVE_CAN_HOLD_CALL: 458a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL; 459a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid); 4603deb3ec6SMatthias Ringwald break; 4613deb3ec6SMatthias Ringwald case HFP_LIST_GENERIC_STATUS_INDICATORS: 462a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 463a0ffb263SMatthias Ringwald hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4643deb3ec6SMatthias Ringwald break; 4653deb3ec6SMatthias Ringwald case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS: 466a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS; 467a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4683deb3ec6SMatthias Ringwald break; 4693deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 470a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 471a0ffb263SMatthias Ringwald hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4723deb3ec6SMatthias Ringwald break; 4733deb3ec6SMatthias Ringwald default: 474aa4dd815SMatthias Ringwald done = 0; 4753deb3ec6SMatthias Ringwald break; 4763deb3ec6SMatthias Ringwald } 4773deb3ec6SMatthias Ringwald return done; 4783deb3ec6SMatthias Ringwald } 4793deb3ec6SMatthias Ringwald 480ce263fc8SMatthias Ringwald 481a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){ 482a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 483498a8121SMilanka Ringwald if (hfp_connection->ok_pending){ 484498a8121SMilanka Ringwald return 0; 485498a8121SMilanka Ringwald } 486ce263fc8SMatthias Ringwald int done = 0; 487a0ffb263SMatthias Ringwald if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 488a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 489ce263fc8SMatthias Ringwald done = 1; 490a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators); 491ce263fc8SMatthias Ringwald return done; 492ce263fc8SMatthias Ringwald }; 493a0ffb263SMatthias Ringwald if (hfp_connection->change_status_update_for_individual_ag_indicators){ 494a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 495ce263fc8SMatthias Ringwald done = 1; 496a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid, 497a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap, 498a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr); 499ce263fc8SMatthias Ringwald return done; 500ce263fc8SMatthias Ringwald } 501ce263fc8SMatthias Ringwald 502a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 503ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_SET_FORMAT: 504a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK; 505a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 506a0ffb263SMatthias Ringwald hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid); 507ce263fc8SMatthias Ringwald return 1; 508ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_SEND_QUERY: 509a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT; 510a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 511a0ffb263SMatthias Ringwald hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid); 512ce263fc8SMatthias Ringwald return 1; 513ce263fc8SMatthias Ringwald default: 514ce263fc8SMatthias Ringwald break; 515ce263fc8SMatthias Ringwald } 516ce263fc8SMatthias Ringwald 517a0ffb263SMatthias Ringwald if (hfp_connection->enable_extended_audio_gateway_error_report){ 518a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 519ce263fc8SMatthias Ringwald done = 1; 520a0ffb263SMatthias Ringwald hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report); 521ce263fc8SMatthias Ringwald return done; 522ce263fc8SMatthias Ringwald } 523ce263fc8SMatthias Ringwald 524ce263fc8SMatthias Ringwald return done; 525ce263fc8SMatthias Ringwald } 526ce263fc8SMatthias Ringwald 527af97579eSMilanka Ringwald static int hfp_hf_voice_recognition_state_machine(hfp_connection_t * hfp_connection){ 528be55a11dSMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) { 529be55a11dSMilanka Ringwald return 0; 530be55a11dSMilanka Ringwald } 531be55a11dSMilanka Ringwald int done = 0; 532fd4151d1SMilanka Ringwald 5330b4debbfSMilanka Ringwald if (hfp_connection->ok_pending == 1){ 5340b4debbfSMilanka Ringwald return 0; 5350b4debbfSMilanka Ringwald } 5360b4debbfSMilanka Ringwald // voice recognition activated from AG 5370b4debbfSMilanka Ringwald if (hfp_connection->command == HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION){ 5380b4debbfSMilanka Ringwald switch(hfp_connection->vra_state_requested){ 5390b4debbfSMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED: 5400b4debbfSMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_OFF: 541de9e0ea7SMilanka Ringwald case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 5420b4debbfSMilanka Ringwald // ignore AG command, continue to wait for OK 5430b4debbfSMilanka Ringwald return 0; 544cf75be85SMilanka Ringwald 5450b4debbfSMilanka Ringwald default: 546b95cac54SMilanka Ringwald if (hfp_connection->ag_vra_msg_length > 0){ 547b95cac54SMilanka Ringwald hfp_hf_emit_enhanced_voice_recognition_text(hfp_connection); 548b95cac54SMilanka Ringwald hfp_connection->ag_vra_msg_length = 0; 549b95cac54SMilanka Ringwald break; 550b95cac54SMilanka Ringwald } 551cf75be85SMilanka Ringwald switch(hfp_connection->ag_vra_state){ 552cf75be85SMilanka Ringwald case HFP_VOICE_RECOGNITION_STATE_AG_READY: 553013cc750SMilanka Ringwald switch (hfp_connection->ag_vra_status){ 554013cc750SMilanka Ringwald case 0: 5550b4debbfSMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF; 556013cc750SMilanka Ringwald break; 557013cc750SMilanka Ringwald case 1: 558013cc750SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED; 559013cc750SMilanka Ringwald break; 560013cc750SMilanka Ringwald case 2: 561013cc750SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 562013cc750SMilanka Ringwald break; 563013cc750SMilanka Ringwald default: 564013cc750SMilanka Ringwald break; 5650b4debbfSMilanka Ringwald } 5660b4debbfSMilanka Ringwald break; 567cf75be85SMilanka Ringwald default: 568cf75be85SMilanka Ringwald // state messages from AG 569cf75be85SMilanka Ringwald hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS); 5708d5005b5SMilanka Ringwald hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY; 571cf75be85SMilanka Ringwald break; 572cf75be85SMilanka Ringwald } 573cf75be85SMilanka Ringwald break; 5740b4debbfSMilanka Ringwald } 5750b4debbfSMilanka Ringwald hfp_connection->command = HFP_CMD_NONE; 5760b4debbfSMilanka Ringwald } 5770b4debbfSMilanka Ringwald 5780b4debbfSMilanka Ringwald 579498a8121SMilanka Ringwald switch (hfp_connection->vra_state_requested){ 580fdda66c0SMilanka Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 581fdda66c0SMilanka Ringwald done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0); 582fdda66c0SMilanka Ringwald if (done != 0){ 583fdda66c0SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF; 584498a8121SMilanka Ringwald hfp_connection->ok_pending = 1; 585498a8121SMilanka Ringwald } 586fd4151d1SMilanka Ringwald return 1; 587fd4151d1SMilanka Ringwald 588fd4151d1SMilanka Ringwald 589fdda66c0SMilanka Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED: 590fdda66c0SMilanka Ringwald done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1); 591fdda66c0SMilanka Ringwald if (done != 0){ 592fdda66c0SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED; 593fd4151d1SMilanka Ringwald hfp_connection->ok_pending = 1; 594fd4151d1SMilanka Ringwald return 1; 5950b4debbfSMilanka Ringwald } 5960b4debbfSMilanka Ringwald break; 597013cc750SMilanka Ringwald 598de9e0ea7SMilanka Ringwald case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 599de9e0ea7SMilanka Ringwald done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 2); 600de9e0ea7SMilanka Ringwald if (done != 0){ 601de9e0ea7SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 602de9e0ea7SMilanka Ringwald hfp_connection->ok_pending = 1; 603de9e0ea7SMilanka Ringwald return 1; 604de9e0ea7SMilanka Ringwald } 605de9e0ea7SMilanka Ringwald break; 606de9e0ea7SMilanka Ringwald 607de9e0ea7SMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_OFF: 608de9e0ea7SMilanka Ringwald hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF; 609de9e0ea7SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 610de9e0ea7SMilanka Ringwald hfp_connection->activate_voice_recognition = false; 611de9e0ea7SMilanka Ringwald if (hfp_connection->activate_voice_recognition){ 612fcf4ede6SMilanka Ringwald hfp_connection->enhanced_voice_recognition_enabled = hfp_hf_enhanced_vra_flag_supported(hfp_connection); 613de9e0ea7SMilanka Ringwald hfp_hf_activate_voice_recognition(hfp_connection->acl_handle); 614de9e0ea7SMilanka Ringwald } else { 615553a4a56SMilanka Ringwald hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS); 616de9e0ea7SMilanka Ringwald } 617de9e0ea7SMilanka Ringwald break; 618de9e0ea7SMilanka Ringwald 619be55a11dSMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED: 620498a8121SMilanka Ringwald hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_ACTIVATED; 621498a8121SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 622de9e0ea7SMilanka Ringwald hfp_connection->activate_voice_recognition = false; 623de9e0ea7SMilanka Ringwald if (hfp_connection->deactivate_voice_recognition){ 624de9e0ea7SMilanka Ringwald hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle); 625de9e0ea7SMilanka Ringwald } else { 626fcf4ede6SMilanka Ringwald hfp_connection->enhanced_voice_recognition_enabled = hfp_hf_enhanced_vra_flag_supported(hfp_connection); 62784fb9ac1SMilanka Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 628553a4a56SMilanka Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS); 62984fb9ac1SMilanka Ringwald } else { 63084fb9ac1SMilanka Ringwald // postpone VRA event to simplify application logic 63184fb9ac1SMilanka Ringwald hfp_connection->emit_vra_enabled_after_audio_established = true; 63284fb9ac1SMilanka Ringwald } 633de9e0ea7SMilanka Ringwald } 634be55a11dSMilanka Ringwald break; 635be55a11dSMilanka Ringwald 636de9e0ea7SMilanka Ringwald 637013cc750SMilanka Ringwald case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 638013cc750SMilanka Ringwald hfp_connection->vra_state = HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 639498a8121SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 640de9e0ea7SMilanka Ringwald hfp_connection->activate_voice_recognition = false; 641de9e0ea7SMilanka Ringwald if (hfp_connection->deactivate_voice_recognition){ 642de9e0ea7SMilanka Ringwald hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle); 643de9e0ea7SMilanka Ringwald } else { 644de9e0ea7SMilanka Ringwald hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_SUCCESS); 645de9e0ea7SMilanka Ringwald } 646be55a11dSMilanka Ringwald break; 647fd4151d1SMilanka Ringwald 648be55a11dSMilanka Ringwald default: 649be55a11dSMilanka Ringwald break; 650be55a11dSMilanka Ringwald } 651be55a11dSMilanka Ringwald return done; 652be55a11dSMilanka Ringwald } 653be55a11dSMilanka Ringwald 654be55a11dSMilanka Ringwald 655be55a11dSMilanka Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){ 656a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 657ce263fc8SMatthias Ringwald 658332ca98fSMatthias Ringwald if (hfp_connection->trigger_codec_exchange){ 659332ca98fSMatthias Ringwald hfp_connection->trigger_codec_exchange = 0; 660ce263fc8SMatthias Ringwald 661a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 662a0ffb263SMatthias Ringwald hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid); 663332ca98fSMatthias Ringwald return 1; 664332ca98fSMatthias Ringwald } 665332ca98fSMatthias Ringwald 6661cc65c4fSMatthias Ringwald if (hfp_connection->hf_send_codec_confirm){ 6671cc65c4fSMatthias Ringwald hfp_connection->hf_send_codec_confirm = false; 668ce263fc8SMatthias Ringwald 669a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 670fcb08cdbSMilanka Ringwald hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed); 6711cc65c4fSMatthias Ringwald return 1; 6721cc65c4fSMatthias Ringwald } 6731cc65c4fSMatthias Ringwald 6741cc65c4fSMatthias Ringwald if (hfp_connection->hf_send_supported_codecs){ 6751cc65c4fSMatthias Ringwald hfp_connection->hf_send_supported_codecs = false; 6761cc65c4fSMatthias Ringwald 677a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 678a0ffb263SMatthias Ringwald hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 6791cc65c4fSMatthias Ringwald return 1; 6801cc65c4fSMatthias Ringwald } 681ce263fc8SMatthias Ringwald 682ce263fc8SMatthias Ringwald return 0; 683ce263fc8SMatthias Ringwald } 684ce263fc8SMatthias Ringwald 685a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){ 686505f1c30SMatthias Ringwald if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) || 687505f1c30SMatthias Ringwald (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0; 688ce263fc8SMatthias Ringwald 68964f19dedSMilanka Ringwald if (hfp_connection->release_audio_connection){ 690a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 691a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 0; 692a0ffb263SMatthias Ringwald gap_disconnect(hfp_connection->sco_handle); 693ce263fc8SMatthias Ringwald return 1; 694ce263fc8SMatthias Ringwald } 695ce263fc8SMatthias Ringwald 696a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 697ce263fc8SMatthias Ringwald 698ce263fc8SMatthias Ringwald // run codecs exchange 699a0ffb263SMatthias Ringwald int done = codecs_exchange_state_machine(hfp_connection); 700ce263fc8SMatthias Ringwald if (done) return 1; 701ce263fc8SMatthias Ringwald 70238200c1dSMilanka Ringwald if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0; 70338200c1dSMilanka Ringwald if (hfp_connection->establish_audio_connection){ 70438200c1dSMilanka Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 70538200c1dSMilanka Ringwald hfp_connection->establish_audio_connection = 0; 70638200c1dSMilanka Ringwald hfp_setup_synchronous_connection(hfp_connection); 70738200c1dSMilanka Ringwald return 1; 70838200c1dSMilanka Ringwald } 709ce263fc8SMatthias Ringwald return 0; 710ce263fc8SMatthias Ringwald } 711ce263fc8SMatthias Ringwald 71238200c1dSMilanka Ringwald 713a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 714eaf2b0a1SMatthias Ringwald 715eaf2b0a1SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 716eaf2b0a1SMatthias Ringwald 717a0ffb263SMatthias Ringwald if (hfp_connection->hf_answer_incoming_call){ 718a0ffb263SMatthias Ringwald hfp_hf_cmd_ata(hfp_connection->rfcomm_cid); 719a0ffb263SMatthias Ringwald hfp_connection->hf_answer_incoming_call = 0; 720ce263fc8SMatthias Ringwald return 1; 721ce263fc8SMatthias Ringwald } 722ce263fc8SMatthias Ringwald return 0; 723ce263fc8SMatthias Ringwald } 724ce263fc8SMatthias Ringwald 7251c6a0fc0SMatthias Ringwald static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){ 7267522e673SMatthias Ringwald 72776cc1527SMatthias Ringwald btstack_assert(hfp_connection != NULL); 72876cc1527SMatthias Ringwald btstack_assert(hfp_connection->local_role == HFP_ROLE_HF); 72976cc1527SMatthias Ringwald 73076cc1527SMatthias Ringwald // during SDP query, RFCOMM CID is not set 73176cc1527SMatthias Ringwald if (hfp_connection->rfcomm_cid == 0) return; 73222387625SMatthias Ringwald 73384fb9ac1SMilanka Ringwald // emit postponed VRA event 73484fb9ac1SMilanka Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->emit_vra_enabled_after_audio_established){ 73584fb9ac1SMilanka Ringwald hfp_connection->emit_vra_enabled_after_audio_established = false; 73684fb9ac1SMilanka Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS); 73784fb9ac1SMilanka Ringwald } 73884fb9ac1SMilanka Ringwald 7393721a235SMatthias Ringwald // assert command could be sent 7403721a235SMatthias Ringwald if (hci_can_send_command_packet_now() == 0) return; 7413721a235SMatthias Ringwald 7423721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP 7433721a235SMatthias Ringwald // WBS Disassociate 7443721a235SMatthias Ringwald if (hfp_connection->cc256x_send_wbs_disassociate){ 7453721a235SMatthias Ringwald hfp_connection->cc256x_send_wbs_disassociate = false; 7463721a235SMatthias Ringwald hci_send_cmd(&hci_ti_wbs_disassociate); 7473721a235SMatthias Ringwald return; 7483721a235SMatthias Ringwald } 7493721a235SMatthias Ringwald // Write Codec Config 7503721a235SMatthias Ringwald if (hfp_connection->cc256x_send_write_codec_config){ 7513721a235SMatthias Ringwald hfp_connection->cc256x_send_write_codec_config = false; 7523721a235SMatthias Ringwald hfp_cc256x_write_codec_config(hfp_connection); 7533721a235SMatthias Ringwald return; 7543721a235SMatthias Ringwald } 7553721a235SMatthias Ringwald // WBS Associate 7563721a235SMatthias Ringwald if (hfp_connection->cc256x_send_wbs_associate){ 7573721a235SMatthias Ringwald hfp_connection->cc256x_send_wbs_associate = false; 7583721a235SMatthias Ringwald hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle); 7593721a235SMatthias Ringwald return; 7603721a235SMatthias Ringwald } 7613721a235SMatthias Ringwald #endif 762689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 763689d4323SMatthias Ringwald // Enable WBS 764689d4323SMatthias Ringwald if (hfp_connection->bcm_send_enable_wbs){ 765689d4323SMatthias Ringwald hfp_connection->bcm_send_enable_wbs = false; 766689d4323SMatthias Ringwald hci_send_cmd(&hci_bcm_enable_wbs, 1, 2); 767689d4323SMatthias Ringwald return; 768689d4323SMatthias Ringwald } 769689d4323SMatthias Ringwald // Write I2S/PCM params 770689d4323SMatthias Ringwald if (hfp_connection->bcm_send_write_i2spcm_interface_param){ 771689d4323SMatthias Ringwald hfp_connection->bcm_send_write_i2spcm_interface_param = false; 772689d4323SMatthias Ringwald hfp_bcm_write_i2spcm_interface_param(hfp_connection); 773689d4323SMatthias Ringwald return; 774689d4323SMatthias Ringwald } 775689d4323SMatthias Ringwald // Disable WBS 776689d4323SMatthias Ringwald if (hfp_connection->bcm_send_disable_wbs){ 777689d4323SMatthias Ringwald hfp_connection->bcm_send_disable_wbs = false; 778689d4323SMatthias Ringwald hci_send_cmd(&hci_bcm_enable_wbs, 0, 2); 779689d4323SMatthias Ringwald return; 780689d4323SMatthias Ringwald } 781689d4323SMatthias Ringwald #endif 7822b5f92fdSMatthias Ringwald #ifdef ENABLE_RTK_PCM_WBS 7832b5f92fdSMatthias Ringwald if (hfp_connection->rtk_send_sco_config){ 7842b5f92fdSMatthias Ringwald hfp_connection->rtk_send_sco_config = false; 7852b5f92fdSMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 7862b5f92fdSMatthias Ringwald log_info("RTK SCO: 16k + mSBC"); 7872b5f92fdSMatthias Ringwald hci_send_cmd(&hci_rtk_configure_sco_routing, 0x81, 0x90, 0x00, 0x00, 0x1a, 0x0c, 0x00, 0x00, 0x41); 7882b5f92fdSMatthias Ringwald } else { 7892b5f92fdSMatthias Ringwald log_info("RTK SCO: 16k + CVSD"); 7902b5f92fdSMatthias Ringwald hci_send_cmd(&hci_rtk_configure_sco_routing, 0x81, 0x90, 0x00, 0x00, 0x1a, 0x0c, 0x0c, 0x00, 0x01); 7912b5f92fdSMatthias Ringwald } 7922b5f92fdSMatthias Ringwald return; 7932b5f92fdSMatthias Ringwald } 7942b5f92fdSMatthias Ringwald #endif 79548e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS) 79648e6eeeeSMatthias Ringwald if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){ 79748e6eeeeSMatthias Ringwald hfp_finalize_connection_context(hfp_connection); 79848e6eeeeSMatthias Ringwald return; 79948e6eeeeSMatthias Ringwald } 80048e6eeeeSMatthias Ringwald #endif 8013721a235SMatthias Ringwald 802cb81d35dSMatthias Ringwald if (hfp_connection->accept_sco){ 803cb81d35dSMatthias Ringwald bool incoming_eSCO = hfp_connection->accept_sco == 2; 804cb81d35dSMatthias Ringwald hfp_connection->accept_sco = 0; 8057522e673SMatthias Ringwald // notify about codec selection if not done already 8067522e673SMatthias Ringwald if (hfp_connection->negotiated_codec == 0){ 8077522e673SMatthias Ringwald hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 8087522e673SMatthias Ringwald } 809cb81d35dSMatthias Ringwald hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO); 8107522e673SMatthias Ringwald return; 8117522e673SMatthias Ringwald } 8127522e673SMatthias Ringwald 813d4dd47ffSMatthias Ringwald if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) { 814d4dd47ffSMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 815d4dd47ffSMatthias Ringwald return; 816d4dd47ffSMatthias Ringwald } 817a0ffb263SMatthias Ringwald int done = hfp_hf_run_for_context_service_level_connection(hfp_connection); 818ce263fc8SMatthias Ringwald if (!done){ 819a0ffb263SMatthias Ringwald done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection); 820ce263fc8SMatthias Ringwald } 821ce263fc8SMatthias Ringwald if (!done){ 822c95b5b3cSMilanka Ringwald done = hfp_hf_run_for_audio_connection(hfp_connection); 823be55a11dSMilanka Ringwald } 824be55a11dSMilanka Ringwald if (!done){ 825c95b5b3cSMilanka Ringwald done = hfp_hf_voice_recognition_state_machine(hfp_connection); 826ce263fc8SMatthias Ringwald } 827ce263fc8SMatthias Ringwald if (!done){ 828a0ffb263SMatthias Ringwald done = call_setup_state_machine(hfp_connection); 829ce263fc8SMatthias Ringwald } 830ce263fc8SMatthias Ringwald 831e2c3b58dSMilanka Ringwald // don't send a new command while ok still pending or SLC is not established 832e2c3b58dSMilanka Ringwald if (hfp_connection->ok_pending || (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED)){ 833e2c3b58dSMilanka Ringwald return; 834e2c3b58dSMilanka Ringwald } 8351016a228SMatthias Ringwald 836a0ffb263SMatthias Ringwald if (hfp_connection->send_microphone_gain){ 837a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 0; 838a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 839a0ffb263SMatthias Ringwald hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 840ce263fc8SMatthias Ringwald return; 841ce263fc8SMatthias Ringwald } 842ce263fc8SMatthias Ringwald 843a0ffb263SMatthias Ringwald if (hfp_connection->send_speaker_gain){ 844a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 0; 845a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 846a0ffb263SMatthias Ringwald hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 847ce263fc8SMatthias Ringwald return; 848ce263fc8SMatthias Ringwald } 849ce263fc8SMatthias Ringwald 850a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_calling_line_notification){ 851a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_calling_line_notification = 0; 852a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 853a0ffb263SMatthias Ringwald hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0); 854ce263fc8SMatthias Ringwald return; 855ce263fc8SMatthias Ringwald } 856ce263fc8SMatthias Ringwald 857a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_calling_line_notification){ 858a0ffb263SMatthias Ringwald hfp_connection->hf_activate_calling_line_notification = 0; 859a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 860a0ffb263SMatthias Ringwald hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1); 861ce263fc8SMatthias Ringwald return; 862ce263fc8SMatthias Ringwald } 863ce263fc8SMatthias Ringwald 864a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){ 865a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0; 86699af1e28SMilanka Ringwald hfp_connection->response_pending_for_command = HFP_CMD_TURN_OFF_EC_AND_NR; 867a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 868e2c3b58dSMilanka Ringwald hfp_hf_send_cmd_with_int(hfp_connection->rfcomm_cid, HFP_TURN_OFF_EC_AND_NR, 0); 869ce263fc8SMatthias Ringwald return; 870ce263fc8SMatthias Ringwald } 871ce263fc8SMatthias Ringwald 872a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_call_waiting_notification){ 873a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_call_waiting_notification = 0; 874a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 875a0ffb263SMatthias Ringwald hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0); 876ce263fc8SMatthias Ringwald return; 877ce263fc8SMatthias Ringwald } 878ce263fc8SMatthias Ringwald 879a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_call_waiting_notification){ 880a0ffb263SMatthias Ringwald hfp_connection->hf_activate_call_waiting_notification = 0; 881a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 882a0ffb263SMatthias Ringwald hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1); 883ce263fc8SMatthias Ringwald return; 884ce263fc8SMatthias Ringwald } 885ce263fc8SMatthias Ringwald 886a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_outgoing_call){ 887a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_outgoing_call = 0; 888a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 889a0ffb263SMatthias Ringwald hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid); 890ce263fc8SMatthias Ringwald return; 891ce263fc8SMatthias Ringwald } 892ce263fc8SMatthias Ringwald 893a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_memory_dialing){ 894a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_memory_dialing = 0; 895a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 896a0ffb263SMatthias Ringwald hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id); 897ce263fc8SMatthias Ringwald return; 898ce263fc8SMatthias Ringwald } 899ce263fc8SMatthias Ringwald 900a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_redial_last_number){ 901a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_redial_last_number = 0; 902a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 903a0ffb263SMatthias Ringwald hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid); 904ce263fc8SMatthias Ringwald return; 905ce263fc8SMatthias Ringwald } 906ce263fc8SMatthias Ringwald 907a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chup){ 908a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 0; 909a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 910a0ffb263SMatthias Ringwald hfp_hf_send_chup(hfp_connection->rfcomm_cid); 911ce263fc8SMatthias Ringwald return; 912ce263fc8SMatthias Ringwald } 913ce263fc8SMatthias Ringwald 914a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_0){ 915a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_0 = 0; 916a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 917a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0); 918ce263fc8SMatthias Ringwald return; 919ce263fc8SMatthias Ringwald } 920ce263fc8SMatthias Ringwald 921a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_1){ 922a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_1 = 0; 923a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 924a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1); 925ce263fc8SMatthias Ringwald return; 926ce263fc8SMatthias Ringwald } 927ce263fc8SMatthias Ringwald 928a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_2){ 929a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_2 = 0; 930a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 931a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2); 932ce263fc8SMatthias Ringwald return; 933ce263fc8SMatthias Ringwald } 934ce263fc8SMatthias Ringwald 935a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_3){ 936a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_3 = 0; 937a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 938a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3); 939ce263fc8SMatthias Ringwald return; 940ce263fc8SMatthias Ringwald } 941ce263fc8SMatthias Ringwald 942a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_4){ 943a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_4 = 0; 944a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 945a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4); 946ce263fc8SMatthias Ringwald return; 947ce263fc8SMatthias Ringwald } 948ce263fc8SMatthias Ringwald 949a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_x){ 950a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 0; 951a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 952a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index); 953667ec068SMatthias Ringwald return; 954667ec068SMatthias Ringwald } 955667ec068SMatthias Ringwald 956a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_dtmf_code){ 957a0ffb263SMatthias Ringwald char code = hfp_connection->hf_send_dtmf_code; 958a0ffb263SMatthias Ringwald hfp_connection->hf_send_dtmf_code = 0; 959a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 960a0ffb263SMatthias Ringwald hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code); 961ce263fc8SMatthias Ringwald return; 962ce263fc8SMatthias Ringwald } 963ce263fc8SMatthias Ringwald 964a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_binp){ 965a0ffb263SMatthias Ringwald hfp_connection->hf_send_binp = 0; 966a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 967a0ffb263SMatthias Ringwald hfp_hf_send_binp(hfp_connection->rfcomm_cid); 968ce263fc8SMatthias Ringwald return; 969ce263fc8SMatthias Ringwald } 970ce263fc8SMatthias Ringwald 971a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_clcc){ 972a0ffb263SMatthias Ringwald hfp_connection->hf_send_clcc = 0; 973a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 974a0ffb263SMatthias Ringwald hfp_hf_send_clcc(hfp_connection->rfcomm_cid); 975667ec068SMatthias Ringwald return; 976667ec068SMatthias Ringwald } 977667ec068SMatthias Ringwald 978a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_rrh){ 979a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 0; 980667ec068SMatthias Ringwald char buffer[20]; 981a0ffb263SMatthias Ringwald switch (hfp_connection->hf_send_rrh_command){ 982667ec068SMatthias Ringwald case '?': 9831599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s?\r", 984ff7d6aeaSMatthias Ringwald HFP_RESPONSE_AND_HOLD); 985ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 986a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 987667ec068SMatthias Ringwald return; 988667ec068SMatthias Ringwald case '0': 989667ec068SMatthias Ringwald case '1': 990667ec068SMatthias Ringwald case '2': 9911599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s=%c\r", 992ff7d6aeaSMatthias Ringwald HFP_RESPONSE_AND_HOLD, 993ff7d6aeaSMatthias Ringwald hfp_connection->hf_send_rrh_command); 994ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 995a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 996667ec068SMatthias Ringwald return; 997667ec068SMatthias Ringwald default: 998667ec068SMatthias Ringwald break; 999667ec068SMatthias Ringwald } 1000667ec068SMatthias Ringwald return; 1001667ec068SMatthias Ringwald } 1002667ec068SMatthias Ringwald 1003a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_cnum){ 1004a0ffb263SMatthias Ringwald hfp_connection->hf_send_cnum = 0; 1005667ec068SMatthias Ringwald char buffer[20]; 10061599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s\r", 1007ff7d6aeaSMatthias Ringwald HFP_SUBSCRIBER_NUMBER_INFORMATION); 1008ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 1009a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 1010667ec068SMatthias Ringwald return; 1011667ec068SMatthias Ringwald } 1012667ec068SMatthias Ringwald 1013667ec068SMatthias Ringwald // update HF indicators 1014a0ffb263SMatthias Ringwald if (hfp_connection->generic_status_update_bitmap){ 1015667ec068SMatthias Ringwald int i; 1016aeb0f0feSMatthias Ringwald for (i=0; i < hfp_hf_indicators_nr; i++){ 1017a0ffb263SMatthias Ringwald if (get_bit(hfp_connection->generic_status_update_bitmap, i)){ 1018a0ffb263SMatthias Ringwald if (hfp_connection->generic_status_indicators[i].state){ 1019a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1020a0ffb263SMatthias Ringwald hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0); 1021667ec068SMatthias Ringwald char buffer[30]; 10221599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s=%u,%u\r", 1023ff7d6aeaSMatthias Ringwald HFP_TRANSFER_HF_INDICATOR_STATUS, 1024aeb0f0feSMatthias Ringwald hfp_hf_indicators[i], 1025aeb0f0feSMatthias Ringwald (unsigned int)hfp_hf_indicators_value[i]); 1026ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 1027a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 1028667ec068SMatthias Ringwald } else { 1029aeb0f0feSMatthias Ringwald log_info("Not sending HF indicator %u as it is disabled", hfp_hf_indicators[i]); 1030667ec068SMatthias Ringwald } 1031667ec068SMatthias Ringwald return; 1032667ec068SMatthias Ringwald } 1033667ec068SMatthias Ringwald } 1034667ec068SMatthias Ringwald } 1035667ec068SMatthias Ringwald 1036ce263fc8SMatthias Ringwald if (done) return; 1037ce263fc8SMatthias Ringwald // deal with disconnect 1038a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 1039ce263fc8SMatthias Ringwald case HFP_W2_DISCONNECT_RFCOMM: 1040a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 1041a0ffb263SMatthias Ringwald rfcomm_disconnect(hfp_connection->rfcomm_cid); 1042ce263fc8SMatthias Ringwald break; 1043ce263fc8SMatthias Ringwald 1044ce263fc8SMatthias Ringwald default: 1045ce263fc8SMatthias Ringwald break; 1046ce263fc8SMatthias Ringwald } 1047ce263fc8SMatthias Ringwald } 1048ce263fc8SMatthias Ringwald 1049a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){ 1050a0ffb263SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 10516a7f44bdSMilanka Ringwald 10527095467fSMatthias Ringwald hfp_emit_slc_connection_event(hfp_connection->local_role, 0, hfp_connection->acl_handle, hfp_connection->remote_addr); 10537522e673SMatthias Ringwald 1054184a03edSMilanka Ringwald uint8_t i; 1055184a03edSMilanka Ringwald for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 10561ac1f60fSMilanka Ringwald hfp_emit_ag_indicator_mapping_event(hfp_connection, &hfp_connection->ag_indicators[i]); 1057184a03edSMilanka Ringwald } 1058722a85f3SMilanka Ringwald 1059722a85f3SMilanka Ringwald for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 1060722a85f3SMilanka Ringwald hfp_connection->ag_indicators[i].status_changed = 0; 1061722a85f3SMilanka Ringwald hfp_emit_ag_indicator_status_event(hfp_connection, &hfp_connection->ag_indicators[i]); 1062722a85f3SMilanka Ringwald } 1063722a85f3SMilanka Ringwald 1064667ec068SMatthias Ringwald // restore volume settings 1065a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = hfp_hf_speaker_gain; 1066a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 1067ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain); 1068a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = hfp_hf_microphone_gain; 1069a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 1070ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain); 1071667ec068SMatthias Ringwald // enable all indicators 1072aeb0f0feSMatthias Ringwald for (i=0; i < hfp_hf_indicators_nr; i++){ 1073aeb0f0feSMatthias Ringwald hfp_connection->generic_status_indicators[i].uuid = hfp_hf_indicators[i]; 1074a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[i].state = 1; 1075667ec068SMatthias Ringwald } 1076ce263fc8SMatthias Ringwald } 1077ce263fc8SMatthias Ringwald 10781cc65c4fSMatthias Ringwald static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){ 1079aeb0f0feSMatthias Ringwald if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_hf_codecs_nr, hfp_hf_codecs)){ 10801cc65c4fSMatthias Ringwald // Codec supported, confirm 10811cc65c4fSMatthias Ringwald hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 10821cc65c4fSMatthias Ringwald hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 10831cc65c4fSMatthias Ringwald log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD"); 10841cc65c4fSMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC; 10851cc65c4fSMatthias Ringwald 10861cc65c4fSMatthias Ringwald hfp_connection->hf_send_codec_confirm = true; 10871cc65c4fSMatthias Ringwald } else { 10881cc65c4fSMatthias Ringwald // Codec not supported, send supported codecs 10891cc65c4fSMatthias Ringwald hfp_connection->codec_confirmed = 0; 10901cc65c4fSMatthias Ringwald hfp_connection->suggested_codec = 0; 10911cc65c4fSMatthias Ringwald hfp_connection->negotiated_codec = 0; 10921cc65c4fSMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 10931cc65c4fSMatthias Ringwald 10941cc65c4fSMatthias Ringwald hfp_connection->hf_send_supported_codecs = true; 10951cc65c4fSMatthias Ringwald } 10961cc65c4fSMatthias Ringwald } 10971cc65c4fSMatthias Ringwald 1098e2c3b58dSMilanka Ringwald static bool hfp_hf_switch_on_ok_pending(hfp_connection_t *hfp_connection, uint8_t status){ 1099e2c3b58dSMilanka Ringwald bool event_emited = true; 1100e2c3b58dSMilanka Ringwald 110199af1e28SMilanka Ringwald switch (hfp_connection->response_pending_for_command){ 1102e2c3b58dSMilanka Ringwald case HFP_CMD_TURN_OFF_EC_AND_NR: 1103e2c3b58dSMilanka Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE, status); 1104e2c3b58dSMilanka Ringwald break; 1105e2c3b58dSMilanka Ringwald default: 1106e2c3b58dSMilanka Ringwald event_emited = false; 1107e2c3b58dSMilanka Ringwald 1108a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 11093deb3ec6SMatthias Ringwald case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 1110a0ffb263SMatthias Ringwald if (has_codec_negotiation_feature(hfp_connection)){ 1111a0ffb263SMatthias Ringwald hfp_connection->state = HFP_NOTIFY_ON_CODECS; 11123deb3ec6SMatthias Ringwald break; 11133deb3ec6SMatthias Ringwald } 1114a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 11153deb3ec6SMatthias Ringwald break; 11163deb3ec6SMatthias Ringwald 11173deb3ec6SMatthias Ringwald case HFP_W4_NOTIFY_ON_CODECS: 1118a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 11193deb3ec6SMatthias Ringwald break; 11203deb3ec6SMatthias Ringwald 11213deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS: 1122a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS; 11233deb3ec6SMatthias Ringwald break; 11243deb3ec6SMatthias Ringwald 11253deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS_STATUS: 1126a0ffb263SMatthias Ringwald hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE; 11273deb3ec6SMatthias Ringwald break; 11283deb3ec6SMatthias Ringwald 11293deb3ec6SMatthias Ringwald case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE: 1130a0ffb263SMatthias Ringwald if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 1131a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL; 11323deb3ec6SMatthias Ringwald break; 11333deb3ec6SMatthias Ringwald } 1134a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 1135a0ffb263SMatthias Ringwald hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 11363deb3ec6SMatthias Ringwald break; 11373deb3ec6SMatthias Ringwald } 1138a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 11393deb3ec6SMatthias Ringwald break; 11403deb3ec6SMatthias Ringwald 11413deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_CAN_HOLD_CALL: 1142a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 1143a0ffb263SMatthias Ringwald hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 11443deb3ec6SMatthias Ringwald break; 11453deb3ec6SMatthias Ringwald } 1146a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 11473deb3ec6SMatthias Ringwald break; 11483deb3ec6SMatthias Ringwald 11493deb3ec6SMatthias Ringwald case HFP_W4_LIST_GENERIC_STATUS_INDICATORS: 1150a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS; 11513deb3ec6SMatthias Ringwald break; 11523deb3ec6SMatthias Ringwald 11533deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS: 1154a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 11553deb3ec6SMatthias Ringwald break; 11563deb3ec6SMatthias Ringwald 11573deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 1158a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 11593deb3ec6SMatthias Ringwald break; 1160ce263fc8SMatthias Ringwald case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 1161a0ffb263SMatthias Ringwald if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 1162a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 1163ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 1164ce263fc8SMatthias Ringwald break; 1165ce263fc8SMatthias Ringwald } 11663deb3ec6SMatthias Ringwald 1167a0ffb263SMatthias Ringwald if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){ 1168a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 0; 1169ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 1170ce263fc8SMatthias Ringwald break; 11713deb3ec6SMatthias Ringwald } 11723deb3ec6SMatthias Ringwald 1173a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 1174ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK: 1175a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1176ce263fc8SMatthias Ringwald break; 1177ce263fc8SMatthias Ringwald case HPF_HF_QUERY_OPERATOR_W4_RESULT: 1178a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET; 1179a473a009SMatthias Ringwald hfp_emit_network_operator_event(hfp_connection); 1180ce263fc8SMatthias Ringwald break; 1181ce263fc8SMatthias Ringwald default: 1182ce263fc8SMatthias Ringwald break; 11833deb3ec6SMatthias Ringwald } 1184ce263fc8SMatthias Ringwald 1185a0ffb263SMatthias Ringwald if (hfp_connection->enable_extended_audio_gateway_error_report){ 1186a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = 0; 1187ce263fc8SMatthias Ringwald break; 11883deb3ec6SMatthias Ringwald } 11893deb3ec6SMatthias Ringwald 1190a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 1191aa4dd815SMatthias Ringwald case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 1192a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 11933deb3ec6SMatthias Ringwald break; 1194ce263fc8SMatthias Ringwald case HFP_CODECS_HF_CONFIRMED_CODEC: 1195a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 1196ce263fc8SMatthias Ringwald break; 11973deb3ec6SMatthias Ringwald default: 11983deb3ec6SMatthias Ringwald break; 11993deb3ec6SMatthias Ringwald } 1200af97579eSMilanka Ringwald hfp_hf_voice_recognition_state_machine(hfp_connection); 1201be55a11dSMilanka Ringwald break; 1202be55a11dSMilanka Ringwald case HFP_AUDIO_CONNECTION_ESTABLISHED: 1203af97579eSMilanka Ringwald hfp_hf_voice_recognition_state_machine(hfp_connection); 12043deb3ec6SMatthias Ringwald break; 12053deb3ec6SMatthias Ringwald default: 12063deb3ec6SMatthias Ringwald break; 12073deb3ec6SMatthias Ringwald } 1208e2c3b58dSMilanka Ringwald break; 1209e2c3b58dSMilanka Ringwald } 12103deb3ec6SMatthias Ringwald 12113deb3ec6SMatthias Ringwald // done 121299af1e28SMilanka Ringwald hfp_connection->response_pending_for_command = HFP_CMD_NONE; 1213be55a11dSMilanka Ringwald hfp_connection->ok_pending = 0; 1214a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1215e2c3b58dSMilanka Ringwald return event_emited; 12163deb3ec6SMatthias Ringwald } 12173deb3ec6SMatthias Ringwald 1218a03dbc20SMilanka Ringwald static bool hfp_is_ringing(hfp_callsetup_status_t callsetup_status){ 1219a03dbc20SMilanka Ringwald switch (callsetup_status){ 1220a03dbc20SMilanka Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1221a03dbc20SMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1222a03dbc20SMilanka Ringwald return true; 1223a03dbc20SMilanka Ringwald default: 1224a03dbc20SMilanka Ringwald return false; 1225a03dbc20SMilanka Ringwald } 1226a03dbc20SMilanka Ringwald } 1227be55a11dSMilanka Ringwald 1228b08371a9SMilanka Ringwald static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) { 12294562e2a2SMatthias Ringwald uint16_t i; 1230a03dbc20SMilanka Ringwald 12314562e2a2SMatthias Ringwald for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 12324562e2a2SMatthias Ringwald if (hfp_connection->ag_indicators[i].status_changed) { 12334562e2a2SMatthias Ringwald if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){ 1234a03dbc20SMilanka Ringwald hfp_callsetup_status_t new_hf_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status; 1235a03dbc20SMilanka Ringwald bool ringing_old = hfp_is_ringing(hfp_hf_callsetup_status); 1236a03dbc20SMilanka Ringwald bool ringing_new = hfp_is_ringing(new_hf_callsetup_status); 1237a03dbc20SMilanka Ringwald if (ringing_old != ringing_new){ 1238a03dbc20SMilanka Ringwald if (ringing_new){ 1239a03dbc20SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGING); 1240a03dbc20SMilanka Ringwald } else { 1241a03dbc20SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGING); 1242a03dbc20SMilanka Ringwald } 1243a03dbc20SMilanka Ringwald } 1244a03dbc20SMilanka Ringwald hfp_hf_callsetup_status = new_hf_callsetup_status; 12454562e2a2SMatthias Ringwald } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){ 1246aeb0f0feSMatthias Ringwald hfp_hf_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status; 12474562e2a2SMatthias Ringwald // avoid set but not used warning 1248aeb0f0feSMatthias Ringwald (void) hfp_hf_callheld_status; 12494562e2a2SMatthias Ringwald } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){ 1250674ebed5SMilanka Ringwald hfp_call_status_t new_hf_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status; 1251674ebed5SMilanka Ringwald if (hfp_hf_call_status != new_hf_call_status){ 1252674ebed5SMilanka Ringwald if (new_hf_call_status == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS){ 1253674ebed5SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED); 1254674ebed5SMilanka Ringwald } else { 1255674ebed5SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED); 1256674ebed5SMilanka Ringwald } 1257674ebed5SMilanka Ringwald } 1258674ebed5SMilanka Ringwald hfp_hf_call_status = new_hf_call_status; 12594562e2a2SMatthias Ringwald } 12604562e2a2SMatthias Ringwald hfp_connection->ag_indicators[i].status_changed = 0; 1261ce3797e1SMilanka Ringwald hfp_emit_ag_indicator_status_event(hfp_connection, &hfp_connection->ag_indicators[i]); 12624562e2a2SMatthias Ringwald break; 12634562e2a2SMatthias Ringwald } 12644562e2a2SMatthias Ringwald } 12654562e2a2SMatthias Ringwald } 12664562e2a2SMatthias Ringwald 1267426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){ 1268186dd3d2SMatthias Ringwald int value; 1269186dd3d2SMatthias Ringwald int i; 1270e2c3b58dSMilanka Ringwald bool event_emited; 1271e2c3b58dSMilanka Ringwald 1272*125560b8SMatthias Ringwald // last argument is still in line_buffer 1273*125560b8SMatthias Ringwald 1274a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1275667ec068SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1276a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1277a473a009SMatthias Ringwald hfp_hf_emit_subscriber_information(hfp_connection, 0); 1278667ec068SMatthias Ringwald break; 1279667ec068SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_STATUS: 1280a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1281ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0])); 1282667ec068SMatthias Ringwald break; 1283667ec068SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 1284a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1285a473a009SMatthias Ringwald hfp_hf_emit_enhanced_call_status(hfp_connection); 1286667ec068SMatthias Ringwald break; 1287ce263fc8SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 1288a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 12892308e108SMilanka Ringwald value = btstack_atoi((char*)hfp_connection->line_buffer); 1290667ec068SMatthias Ringwald hfp_hf_speaker_gain = value; 1291ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value); 1292ce263fc8SMatthias Ringwald break; 1293ce263fc8SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 1294a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 12952308e108SMilanka Ringwald value = btstack_atoi((char*)hfp_connection->line_buffer); 1296667ec068SMatthias Ringwald hfp_hf_microphone_gain = value; 1297ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value); 1298ce263fc8SMatthias Ringwald break; 1299ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 1300a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1301ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number); 1302a0ffb263SMatthias Ringwald break; 1303a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1304a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1305a473a009SMatthias Ringwald hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION); 1306a0ffb263SMatthias Ringwald break; 1307a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1308a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1309a473a009SMatthias Ringwald hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION); 1310ce263fc8SMatthias Ringwald break; 1311ce263fc8SMatthias Ringwald case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1312a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 13135a4785c8SMatthias Ringwald hfp_connection->ok_pending = 0; 1314a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 1315ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value); 1316ce263fc8SMatthias Ringwald break; 13170b4debbfSMilanka Ringwald case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION: 13180b4debbfSMilanka Ringwald break; 1319fdda66c0SMilanka Ringwald case HFP_CMD_ERROR: 132090244c92SMilanka Ringwald switch (hfp_connection->state){ 132190244c92SMilanka Ringwald case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 132290244c92SMilanka Ringwald switch (hfp_connection->codecs_state){ 132390244c92SMilanka Ringwald case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 1324fdda66c0SMilanka Ringwald hfp_reset_context_flags(hfp_connection); 132590244c92SMilanka Ringwald hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec); 132690244c92SMilanka Ringwald return; 132790244c92SMilanka Ringwald default: 132890244c92SMilanka Ringwald break; 132990244c92SMilanka Ringwald } 133056f1adacSMilanka Ringwald break; 133156f1adacSMilanka Ringwald default: 133256f1adacSMilanka Ringwald break; 133356f1adacSMilanka Ringwald } 1334e2c3b58dSMilanka Ringwald 1335fdda66c0SMilanka Ringwald // handle error response for voice activation (HF initiated) 13360b4debbfSMilanka Ringwald switch(hfp_connection->vra_state_requested){ 1337de9e0ea7SMilanka Ringwald case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1338de9e0ea7SMilanka Ringwald hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 1339be55a11dSMilanka Ringwald break; 1340be55a11dSMilanka Ringwald default: 1341e2c3b58dSMilanka Ringwald if (hfp_connection->vra_state_requested == hfp_connection->vra_state){ 1342e2c3b58dSMilanka Ringwald break; 1343e2c3b58dSMilanka Ringwald } 13440b4debbfSMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 1345553a4a56SMilanka Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 13460b4debbfSMilanka Ringwald hfp_reset_context_flags(hfp_connection); 13470b4debbfSMilanka Ringwald return; 1348be55a11dSMilanka Ringwald } 1349e2c3b58dSMilanka Ringwald event_emited = hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 1350e2c3b58dSMilanka Ringwald if (!event_emited){ 13510b4debbfSMilanka Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1); 1352e2c3b58dSMilanka Ringwald } 1353fdda66c0SMilanka Ringwald hfp_reset_context_flags(hfp_connection); 1354ce263fc8SMatthias Ringwald break; 1355fdda66c0SMilanka Ringwald 1356ce263fc8SMatthias Ringwald case HFP_CMD_OK: 1357e2c3b58dSMilanka Ringwald hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_SUCCESS); 1358ce263fc8SMatthias Ringwald break; 1359ce263fc8SMatthias Ringwald case HFP_CMD_RING: 13605a4785c8SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1361ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING); 1362ce263fc8SMatthias Ringwald break; 1363ce263fc8SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 13645a4785c8SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 13654562e2a2SMatthias Ringwald hfp_hf_handle_transfer_ag_indicator_status(hfp_connection); 1366ce263fc8SMatthias Ringwald break; 1367c741b032SMilanka Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 13685a4785c8SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1369184a03edSMilanka Ringwald // report status after SLC established 1370184a03edSMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){ 1371184a03edSMilanka Ringwald break; 1372184a03edSMilanka Ringwald } 1373c741b032SMilanka Ringwald for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 13741ac1f60fSMilanka Ringwald hfp_emit_ag_indicator_mapping_event(hfp_connection, &hfp_connection->ag_indicators[i]); 1375c741b032SMilanka Ringwald } 1376c741b032SMilanka Ringwald break; 13771cc65c4fSMatthias Ringwald case HFP_CMD_AG_SUGGESTED_CODEC: 13781cc65c4fSMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 13795a4785c8SMatthias Ringwald hfp_hf_handle_suggested_codec(hfp_connection); 13801cc65c4fSMatthias Ringwald break; 1381eac56539SMilanka Ringwald case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING: 1382eac56539SMilanka Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_IN_BAND_RING_TONE, get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE)); 1383ce263fc8SMatthias Ringwald default: 1384ce263fc8SMatthias Ringwald break; 13853deb3ec6SMatthias Ringwald } 13860cef86faSMatthias Ringwald } 1387426f9988SMatthias Ringwald 138876cc1527SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){ 138976cc1527SMatthias Ringwald return (byte == '\n') || (byte == '\r'); 139076cc1527SMatthias Ringwald } 139176cc1527SMatthias Ringwald 13920b4debbfSMilanka Ringwald static void hfp_hf_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){ 1393426f9988SMatthias Ringwald // assertion: size >= 1 as rfcomm.c does not deliver empty packets 1394426f9988SMatthias Ringwald if (size < 1) return; 1395426f9988SMatthias Ringwald 1396426f9988SMatthias Ringwald hfp_log_rfcomm_message("HFP_HF_RX", packet, size); 1397e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES 1398e43d1938SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet); 1399e43d1938SMatthias Ringwald #endif 1400426f9988SMatthias Ringwald 1401426f9988SMatthias Ringwald // process messages byte-wise 1402a7ba78b0SMilanka Ringwald uint8_t pos; 1403426f9988SMatthias Ringwald for (pos = 0; pos < size; pos++){ 1404426f9988SMatthias Ringwald hfp_parse(hfp_connection, packet[pos], 1); 14051599fe57SMatthias Ringwald // parse until end of line "\r" or "\n" 1406426f9988SMatthias Ringwald if (!hfp_parser_is_end_of_line(packet[pos])) continue; 14070b4debbfSMilanka Ringwald hfp_hf_handle_rfcomm_command(hfp_connection); 14083deb3ec6SMatthias Ringwald } 1409a7ba78b0SMilanka Ringwald } 14103deb3ec6SMatthias Ringwald 14111c6a0fc0SMatthias Ringwald static void hfp_hf_run(void){ 1412665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1413665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1414665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1415a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 141622387625SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_HF) continue; 14171c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 14183deb3ec6SMatthias Ringwald } 14193deb3ec6SMatthias Ringwald } 14203deb3ec6SMatthias Ringwald 14211c6a0fc0SMatthias Ringwald static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 14220b4debbfSMilanka Ringwald hfp_connection_t * hfp_connection; 14233deb3ec6SMatthias Ringwald switch (packet_type){ 14243deb3ec6SMatthias Ringwald case RFCOMM_DATA_PACKET: 14250b4debbfSMilanka Ringwald hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 14260b4debbfSMilanka Ringwald if (!hfp_connection) return; 14270b4debbfSMilanka Ringwald hfp_hf_handle_rfcomm_data(hfp_connection, packet, size); 14280b4debbfSMilanka Ringwald hfp_hf_run_for_context(hfp_connection); 14290b4debbfSMilanka Ringwald return; 14303deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 1431d4dd47ffSMatthias Ringwald if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 1432d4dd47ffSMatthias Ringwald uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 14331c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid)); 1434d4dd47ffSMatthias Ringwald return; 1435d4dd47ffSMatthias Ringwald } 143627950165SMatthias Ringwald hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF); 1437202c8a4cSMatthias Ringwald break; 14383deb3ec6SMatthias Ringwald default: 14393deb3ec6SMatthias Ringwald break; 14403deb3ec6SMatthias Ringwald } 14411c6a0fc0SMatthias Ringwald hfp_hf_run(); 14423deb3ec6SMatthias Ringwald } 14433deb3ec6SMatthias Ringwald 14441c6a0fc0SMatthias Ringwald static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1445405014fbSMatthias Ringwald hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF); 14461c6a0fc0SMatthias Ringwald hfp_hf_run(); 1447405014fbSMatthias Ringwald } 1448405014fbSMatthias Ringwald 1449aeb0f0feSMatthias Ringwald static void hfp_hf_set_defaults(void){ 1450aeb0f0feSMatthias Ringwald hfp_hf_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 1451aeb0f0feSMatthias Ringwald hfp_hf_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS; 1452aeb0f0feSMatthias Ringwald hfp_hf_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1453aeb0f0feSMatthias Ringwald hfp_hf_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD; 1454aeb0f0feSMatthias Ringwald hfp_hf_codecs_nr = 0; 1455aeb0f0feSMatthias Ringwald hfp_hf_speaker_gain = 9; 1456aeb0f0feSMatthias Ringwald hfp_hf_microphone_gain = 9; 1457aeb0f0feSMatthias Ringwald hfp_hf_indicators_nr = 0; 1458aeb0f0feSMatthias Ringwald } 1459aeb0f0feSMatthias Ringwald 1460b4df8028SMilanka Ringwald uint8_t hfp_hf_init(uint16_t rfcomm_channel_nr){ 1461b4df8028SMilanka Ringwald uint8_t status = rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 1462b4df8028SMilanka Ringwald if (status != ERROR_CODE_SUCCESS){ 1463b4df8028SMilanka Ringwald return status; 1464b4df8028SMilanka Ringwald } 1465b4df8028SMilanka Ringwald 1466520c92d5SMatthias Ringwald hfp_init(); 1467aeb0f0feSMatthias Ringwald hfp_hf_set_defaults(); 1468d63c37a1SMatthias Ringwald 14691c6a0fc0SMatthias Ringwald hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler; 14701c6a0fc0SMatthias Ringwald hci_add_event_handler(&hfp_hf_hci_event_callback_registration); 147127950165SMatthias Ringwald 147227950165SMatthias Ringwald // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us 14731c6a0fc0SMatthias Ringwald hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler); 1474b4df8028SMilanka Ringwald return ERROR_CODE_SUCCESS; 147520b2edb6SMatthias Ringwald } 147627950165SMatthias Ringwald 147720b2edb6SMatthias Ringwald void hfp_hf_deinit(void){ 147820b2edb6SMatthias Ringwald hfp_deinit(); 1479aeb0f0feSMatthias Ringwald hfp_hf_set_defaults(); 1480aeb0f0feSMatthias Ringwald 1481aeb0f0feSMatthias Ringwald hfp_hf_callback = NULL; 148220b2edb6SMatthias Ringwald (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t)); 1483aeb0f0feSMatthias Ringwald (void) memset(hfp_hf_phone_number, 0, sizeof(hfp_hf_phone_number)); 1484a0ffb263SMatthias Ringwald } 1485a0ffb263SMatthias Ringwald 14867ca89cabSMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, const uint8_t * codecs){ 148768466199SMilanka Ringwald btstack_assert(codecs_nr < HFP_MAX_NUM_CODECS); 14883deb3ec6SMatthias Ringwald 1489aeb0f0feSMatthias Ringwald hfp_hf_codecs_nr = codecs_nr; 14903deb3ec6SMatthias Ringwald int i; 14913deb3ec6SMatthias Ringwald for (i=0; i<codecs_nr; i++){ 1492aeb0f0feSMatthias Ringwald hfp_hf_codecs[i] = codecs[i]; 14933deb3ec6SMatthias Ringwald } 14943deb3ec6SMatthias Ringwald } 14953deb3ec6SMatthias Ringwald 1496a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){ 1497aeb0f0feSMatthias Ringwald hfp_hf_supported_features = supported_features; 1498a0ffb263SMatthias Ringwald } 14993deb3ec6SMatthias Ringwald 15007ca89cabSMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){ 1501aeb0f0feSMatthias Ringwald btstack_assert(hfp_hf_indicators_nr < HFP_MAX_NUM_INDICATORS); 150268466199SMilanka Ringwald 1503aeb0f0feSMatthias Ringwald hfp_hf_indicators_nr = indicators_nr; 15043deb3ec6SMatthias Ringwald int i; 1505aeb0f0feSMatthias Ringwald for (i = 0; i < hfp_hf_indicators_nr ; i++){ 1506aeb0f0feSMatthias Ringwald hfp_hf_indicators[i] = indicators[i]; 15073deb3ec6SMatthias Ringwald } 15083deb3ec6SMatthias Ringwald } 15093deb3ec6SMatthias Ringwald 15104eb3f1d8SMilanka Ringwald uint8_t hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){ 15114eb3f1d8SMilanka Ringwald return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF); 15123deb3ec6SMatthias Ringwald } 15133deb3ec6SMatthias Ringwald 1514657bc59fSMilanka Ringwald uint8_t hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){ 15159c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1516a33eb0c4SMilanka Ringwald if (!hfp_connection){ 1517657bc59fSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1518a33eb0c4SMilanka Ringwald } 15191ffa0dd9SMilanka Ringwald hfp_trigger_release_service_level_connection(hfp_connection); 15201c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1521657bc59fSMilanka Ringwald return ERROR_CODE_SUCCESS; 15223deb3ec6SMatthias Ringwald } 15233deb3ec6SMatthias Ringwald 15243c65e705SMilanka Ringwald static uint8_t hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){ 15259c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1526a0ffb263SMatthias Ringwald if (!hfp_connection) { 15273c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 15283deb3ec6SMatthias Ringwald } 1529a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = enable; 15301c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 15313c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 15323deb3ec6SMatthias Ringwald } 15333deb3ec6SMatthias Ringwald 15343c65e705SMilanka Ringwald uint8_t hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 15353c65e705SMilanka Ringwald return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1); 1536ce263fc8SMatthias Ringwald } 1537ce263fc8SMatthias Ringwald 15383c65e705SMilanka Ringwald uint8_t hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 15393c65e705SMilanka Ringwald return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0); 1540ce263fc8SMatthias Ringwald } 1541ce263fc8SMatthias Ringwald 15423deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format 15433c65e705SMilanka Ringwald uint8_t hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){ 15449c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1545a0ffb263SMatthias Ringwald if (!hfp_connection) { 15463c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 15473deb3ec6SMatthias Ringwald } 1548a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 1; 1549a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap; 15501c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 15513c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 15523deb3ec6SMatthias Ringwald } 15533deb3ec6SMatthias Ringwald 15543c65e705SMilanka Ringwald uint8_t hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){ 15559c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1556a0ffb263SMatthias Ringwald if (!hfp_connection) { 15573c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 15583deb3ec6SMatthias Ringwald } 15593c65e705SMilanka Ringwald 1560a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 1561ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET: 1562a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT; 1563ce263fc8SMatthias Ringwald break; 1564ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_FORMAT_SET: 1565a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1566ce263fc8SMatthias Ringwald break; 1567ce263fc8SMatthias Ringwald default: 15683c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1569ce263fc8SMatthias Ringwald } 15701c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 15713c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 15723deb3ec6SMatthias Ringwald } 15733deb3ec6SMatthias Ringwald 15743c65e705SMilanka Ringwald static uint8_t hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){ 15759c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1576a0ffb263SMatthias Ringwald if (!hfp_connection) { 15773c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 15783deb3ec6SMatthias Ringwald } 1579a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = enable; 15801c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 15813c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 15823deb3ec6SMatthias Ringwald } 15833deb3ec6SMatthias Ringwald 1584ce263fc8SMatthias Ringwald 15853c65e705SMilanka Ringwald uint8_t hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 15863c65e705SMilanka Ringwald return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1); 1587ce263fc8SMatthias Ringwald } 1588ce263fc8SMatthias Ringwald 15893c65e705SMilanka Ringwald uint8_t hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 15903c65e705SMilanka Ringwald return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0); 1591ce263fc8SMatthias Ringwald } 1592ce263fc8SMatthias Ringwald 159338200c1dSMilanka Ringwald static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){ 1594aeb0f0feSMatthias Ringwald return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) && (hfp_hf_supported_features & (1 << HFP_HFSF_ESCO_S4)); 159538200c1dSMilanka Ringwald } 1596ce263fc8SMatthias Ringwald 15973c65e705SMilanka Ringwald uint8_t hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){ 15989c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1599a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16003c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1601a33eb0c4SMilanka Ringwald } 1602ce263fc8SMatthias Ringwald 16033c65e705SMilanka Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 16043c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 16053c65e705SMilanka Ringwald } 16063c65e705SMilanka Ringwald 16073c65e705SMilanka Ringwald if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){ 16083c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 16093c65e705SMilanka Ringwald } 1610f4412093SMatthias Ringwald if (has_codec_negotiation_feature(hfp_connection)) { 1611a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state) { 1612aa4dd815SMatthias Ringwald case HFP_CODECS_W4_AG_COMMON_CODEC: 1613aa4dd815SMatthias Ringwald break; 1614ec3bfc1aSMatthias Ringwald case HFP_CODECS_EXCHANGED: 1615ec3bfc1aSMatthias Ringwald hfp_connection->trigger_codec_exchange = 1; 1616ec3bfc1aSMatthias Ringwald break; 1617aa4dd815SMatthias Ringwald default: 16181cc65c4fSMatthias Ringwald hfp_connection->codec_confirmed = 0; 16191cc65c4fSMatthias Ringwald hfp_connection->suggested_codec = 0; 16201cc65c4fSMatthias Ringwald hfp_connection->negotiated_codec = 0; 16211cc65c4fSMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 162238200c1dSMilanka Ringwald hfp_connection->trigger_codec_exchange = 1; 1623aa4dd815SMatthias Ringwald break; 16243deb3ec6SMatthias Ringwald } 1625f4412093SMatthias Ringwald } else { 1626f4412093SMatthias Ringwald log_info("no codec negotiation feature, use CVSD"); 1627f4412093SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 1628f4412093SMatthias Ringwald hfp_connection->suggested_codec = HFP_CODEC_CVSD; 1629f4412093SMatthias Ringwald hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 1630f4412093SMatthias Ringwald hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 1631f4412093SMatthias Ringwald hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection)); 1632f4412093SMatthias Ringwald hfp_connection->establish_audio_connection = 1; 1633f4412093SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 1634ce263fc8SMatthias Ringwald } 1635ce263fc8SMatthias Ringwald 16361c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 16373c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 16383deb3ec6SMatthias Ringwald } 16393deb3ec6SMatthias Ringwald 16403c65e705SMilanka Ringwald uint8_t hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){ 16419c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1642a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16433c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1644a33eb0c4SMilanka Ringwald } 16450b4debbfSMilanka Ringwald if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){ 16460b4debbfSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 16470b4debbfSMilanka Ringwald } 16480b4debbfSMilanka Ringwald uint8_t status = hfp_trigger_release_audio_connection(hfp_connection); 16490b4debbfSMilanka Ringwald if (status == ERROR_CODE_SUCCESS){ 16501c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 16510b4debbfSMilanka Ringwald } 16523c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 16533deb3ec6SMatthias Ringwald } 16543deb3ec6SMatthias Ringwald 16553c65e705SMilanka Ringwald uint8_t hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){ 16569c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1657a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16583c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1659a33eb0c4SMilanka Ringwald } 1660ce263fc8SMatthias Ringwald 1661aeb0f0feSMatthias Ringwald if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1662a0ffb263SMatthias Ringwald hfp_connection->hf_answer_incoming_call = 1; 16631c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1664ce263fc8SMatthias Ringwald } else { 1665aeb0f0feSMatthias Ringwald log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_hf_callsetup_status); 16663c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1667ce263fc8SMatthias Ringwald } 16683c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1669ce263fc8SMatthias Ringwald } 1670ce263fc8SMatthias Ringwald 16713c65e705SMilanka Ringwald uint8_t hfp_hf_terminate_call(hci_con_handle_t acl_handle){ 16729c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1673a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16743c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1675a33eb0c4SMilanka Ringwald } 1676a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 1; 16771c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 16783c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1679ce263fc8SMatthias Ringwald } 1680ce263fc8SMatthias Ringwald 16813c65e705SMilanka Ringwald uint8_t hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){ 16829c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1683a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16843c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1685a33eb0c4SMilanka Ringwald } 1686ce263fc8SMatthias Ringwald 1687aeb0f0feSMatthias Ringwald if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1688a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 1; 16891c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1690ce263fc8SMatthias Ringwald } 16913c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1692ce263fc8SMatthias Ringwald } 1693ce263fc8SMatthias Ringwald 16943c65e705SMilanka Ringwald uint8_t hfp_hf_user_busy(hci_con_handle_t acl_handle){ 16959c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1696a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16973c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1698a33eb0c4SMilanka Ringwald } 1699ce263fc8SMatthias Ringwald 1700aeb0f0feSMatthias Ringwald if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1701a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_0 = 1; 17021c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1703ce263fc8SMatthias Ringwald } 17043c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1705ce263fc8SMatthias Ringwald } 1706ce263fc8SMatthias Ringwald 17073c65e705SMilanka Ringwald uint8_t hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){ 17089c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1709a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17103c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1711a33eb0c4SMilanka Ringwald } 1712ce263fc8SMatthias Ringwald 1713aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1714aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1715a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_1 = 1; 17161c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1717ce263fc8SMatthias Ringwald } 17183c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1719ce263fc8SMatthias Ringwald } 1720ce263fc8SMatthias Ringwald 17213c65e705SMilanka Ringwald uint8_t hfp_hf_swap_calls(hci_con_handle_t acl_handle){ 17229c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1723a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17243c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1725a33eb0c4SMilanka Ringwald } 1726ce263fc8SMatthias Ringwald 1727aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1728aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1729a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_2 = 1; 17301c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1731ce263fc8SMatthias Ringwald } 17323c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1733ce263fc8SMatthias Ringwald } 1734ce263fc8SMatthias Ringwald 17353c65e705SMilanka Ringwald uint8_t hfp_hf_join_held_call(hci_con_handle_t acl_handle){ 17369c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1737a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17383c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1739a33eb0c4SMilanka Ringwald } 1740ce263fc8SMatthias Ringwald 1741aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1742aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1743a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_3 = 1; 17441c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1745ce263fc8SMatthias Ringwald } 17463c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1747ce263fc8SMatthias Ringwald } 1748ce263fc8SMatthias Ringwald 17493c65e705SMilanka Ringwald uint8_t hfp_hf_connect_calls(hci_con_handle_t acl_handle){ 17509c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1751a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17523c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1753a33eb0c4SMilanka Ringwald } 1754ce263fc8SMatthias Ringwald 1755aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1756aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1757a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_4 = 1; 17581c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1759ce263fc8SMatthias Ringwald } 17603c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1761ce263fc8SMatthias Ringwald } 1762ce263fc8SMatthias Ringwald 17633c65e705SMilanka Ringwald uint8_t hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){ 17649c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1765a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17663c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1767a33eb0c4SMilanka Ringwald } 1768667ec068SMatthias Ringwald 1769aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1770aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1771a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 1; 1772a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x_index = 10 + index; 17731c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1774667ec068SMatthias Ringwald } 17753c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1776667ec068SMatthias Ringwald } 1777667ec068SMatthias Ringwald 17783c65e705SMilanka Ringwald uint8_t hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){ 17799c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1780a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17813c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1782a33eb0c4SMilanka Ringwald } 1783667ec068SMatthias Ringwald 1784aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1785aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1786a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 1; 1787a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x_index = 20 + index; 17881c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1789667ec068SMatthias Ringwald } 17903c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1791667ec068SMatthias Ringwald } 1792ce263fc8SMatthias Ringwald 17933c65e705SMilanka Ringwald uint8_t hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){ 17949c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1795a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17963c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1797a33eb0c4SMilanka Ringwald } 1798ce263fc8SMatthias Ringwald 1799a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_outgoing_call = 1; 1800aeb0f0feSMatthias Ringwald snprintf(hfp_hf_phone_number, sizeof(hfp_hf_phone_number), "%s", number); 18011c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18023c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1803ce263fc8SMatthias Ringwald } 1804ce263fc8SMatthias Ringwald 18053c65e705SMilanka Ringwald uint8_t hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){ 18069c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1807a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18083c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1809a33eb0c4SMilanka Ringwald } 1810ce263fc8SMatthias Ringwald 1811a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_memory_dialing = 1; 1812a0ffb263SMatthias Ringwald hfp_connection->memory_id = memory_id; 1813a0ffb263SMatthias Ringwald 18141c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18153c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1816ce263fc8SMatthias Ringwald } 1817ce263fc8SMatthias Ringwald 18183c65e705SMilanka Ringwald uint8_t hfp_hf_redial_last_number(hci_con_handle_t acl_handle){ 18199c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1820a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18213c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1822a33eb0c4SMilanka Ringwald } 1823ce263fc8SMatthias Ringwald 1824a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_redial_last_number = 1; 18251c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18263c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1827ce263fc8SMatthias Ringwald } 1828ce263fc8SMatthias Ringwald 18293c65e705SMilanka Ringwald uint8_t hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){ 18309c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1831a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18323c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1833a33eb0c4SMilanka Ringwald } 1834ce263fc8SMatthias Ringwald 1835a0ffb263SMatthias Ringwald hfp_connection->hf_activate_call_waiting_notification = 1; 18361c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18373c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1838ce263fc8SMatthias Ringwald } 1839ce263fc8SMatthias Ringwald 1840ce263fc8SMatthias Ringwald 18413c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){ 18429c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1843a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18443c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1845a33eb0c4SMilanka Ringwald } 1846ce263fc8SMatthias Ringwald 1847a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_call_waiting_notification = 1; 18481c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18493c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1850ce263fc8SMatthias Ringwald } 1851ce263fc8SMatthias Ringwald 1852ce263fc8SMatthias Ringwald 18533c65e705SMilanka Ringwald uint8_t hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){ 18549c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1855a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18563c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1857a33eb0c4SMilanka Ringwald } 1858ce263fc8SMatthias Ringwald 1859a0ffb263SMatthias Ringwald hfp_connection->hf_activate_calling_line_notification = 1; 18601c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18613c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1862ce263fc8SMatthias Ringwald } 1863ce263fc8SMatthias Ringwald 18643c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){ 18659c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1866a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18673c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1868a33eb0c4SMilanka Ringwald } 1869ce263fc8SMatthias Ringwald 1870a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_calling_line_notification = 1; 18711c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18723c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1873ce263fc8SMatthias Ringwald } 1874ce263fc8SMatthias Ringwald 18756ba83b5eSMilanka Ringwald static bool hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection_t * hfp_connection){ 18766ba83b5eSMilanka Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_EC_NR_FUNCTION); 1877aeb0f0feSMatthias Ringwald int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_EC_NR_FUNCTION); 18786ba83b5eSMilanka Ringwald return hf && ag; 1879ce263fc8SMatthias Ringwald } 1880ce263fc8SMatthias Ringwald 18813c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){ 18829c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1883a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18843c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1885a33eb0c4SMilanka Ringwald } 18866ba83b5eSMilanka Ringwald if (!hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection)){ 18876ba83b5eSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 18886ba83b5eSMilanka Ringwald } 1889ce263fc8SMatthias Ringwald 1890a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1; 18911c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18923c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1893ce263fc8SMatthias Ringwald } 1894ce263fc8SMatthias Ringwald 1895acd11d4aSMilanka Ringwald uint8_t hfp_hf_activate_voice_recognition(hci_con_handle_t acl_handle){ 1896fdda66c0SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1897fdda66c0SMilanka Ringwald if (!hfp_connection) { 1898fdda66c0SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1899be55a11dSMilanka Ringwald } 1900013cc750SMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){ 1901013cc750SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1902013cc750SMilanka Ringwald } 1903acd11d4aSMilanka Ringwald 1904acd11d4aSMilanka Ringwald bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection); 1905acd11d4aSMilanka Ringwald bool legacy_vra_supported = hfp_hf_vra_flag_supported(hfp_connection); 1906acd11d4aSMilanka Ringwald if (!enhanced_vra_supported && !legacy_vra_supported){ 1907acd11d4aSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1908af97579eSMilanka Ringwald } 1909af97579eSMilanka Ringwald 1910498a8121SMilanka Ringwald switch (hfp_connection->vra_state){ 1911be55a11dSMilanka Ringwald case HFP_VRA_VOICE_RECOGNITION_OFF: 1912de9e0ea7SMilanka Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 1913fd4151d1SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED; 1914acd11d4aSMilanka Ringwald hfp_connection->enhanced_voice_recognition_enabled = enhanced_vra_supported; 1915be55a11dSMilanka Ringwald break; 1916de9e0ea7SMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_OFF: 1917de9e0ea7SMilanka Ringwald hfp_connection->activate_voice_recognition = true; 1918de9e0ea7SMilanka Ringwald break; 1919be55a11dSMilanka Ringwald default: 1920be55a11dSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1921be55a11dSMilanka Ringwald } 1922ce263fc8SMatthias Ringwald 1923af97579eSMilanka Ringwald hfp_hf_run_for_context(hfp_connection); 1924fdda66c0SMilanka Ringwald return ERROR_CODE_SUCCESS; 1925af97579eSMilanka Ringwald } 1926af97579eSMilanka Ringwald 1927acd11d4aSMilanka Ringwald uint8_t hfp_hf_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle){ 1928af97579eSMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1929af97579eSMilanka Ringwald if (!hfp_connection) { 1930af97579eSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1931af97579eSMilanka Ringwald } 193284fb9ac1SMilanka Ringwald 193384fb9ac1SMilanka Ringwald if (hfp_connection->emit_vra_enabled_after_audio_established){ 193484fb9ac1SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 193584fb9ac1SMilanka Ringwald } 193684fb9ac1SMilanka Ringwald 1937acd11d4aSMilanka Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){ 193808a0b01cSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 193908a0b01cSMilanka Ringwald } 1940acd11d4aSMilanka Ringwald 1941acd11d4aSMilanka Ringwald bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection); 1942acd11d4aSMilanka Ringwald if (!enhanced_vra_supported){ 1943acd11d4aSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1944acd11d4aSMilanka Ringwald } 1945acd11d4aSMilanka Ringwald 1946acd11d4aSMilanka Ringwald switch (hfp_connection->vra_state){ 1947acd11d4aSMilanka Ringwald case HFP_VRA_VOICE_RECOGNITION_ACTIVATED: 1948acd11d4aSMilanka Ringwald case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1949acd11d4aSMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 1950acd11d4aSMilanka Ringwald break; 1951acd11d4aSMilanka Ringwald default: 1952fdda66c0SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1953af97579eSMilanka Ringwald } 1954013cc750SMilanka Ringwald 1955acd11d4aSMilanka Ringwald hfp_hf_run_for_context(hfp_connection); 1956acd11d4aSMilanka Ringwald return ERROR_CODE_SUCCESS; 1957acd11d4aSMilanka Ringwald } 1958acd11d4aSMilanka Ringwald 1959acd11d4aSMilanka Ringwald 1960acd11d4aSMilanka Ringwald uint8_t hfp_hf_deactivate_voice_recognition(hci_con_handle_t acl_handle){ 1961acd11d4aSMilanka Ringwald // return deactivate_voice_recognition(acl_handle, false); 1962acd11d4aSMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1963acd11d4aSMilanka Ringwald if (!hfp_connection) { 1964acd11d4aSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1965acd11d4aSMilanka Ringwald } 1966acd11d4aSMilanka Ringwald 196784fb9ac1SMilanka Ringwald if (hfp_connection->emit_vra_enabled_after_audio_established){ 196884fb9ac1SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 196984fb9ac1SMilanka Ringwald } 197084fb9ac1SMilanka Ringwald 1971acd11d4aSMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || 1972acd11d4aSMilanka Ringwald hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){ 1973acd11d4aSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1974acd11d4aSMilanka Ringwald } 1975acd11d4aSMilanka Ringwald 1976acd11d4aSMilanka Ringwald bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection); 1977acd11d4aSMilanka Ringwald bool legacy_vra_supported = hfp_hf_vra_flag_supported(hfp_connection); 1978acd11d4aSMilanka Ringwald if (!enhanced_vra_supported && !legacy_vra_supported){ 1979acd11d4aSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1980acd11d4aSMilanka Ringwald } 1981acd11d4aSMilanka Ringwald 1982fdda66c0SMilanka Ringwald switch (hfp_connection->vra_state){ 1983de9e0ea7SMilanka Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED: 1984fdda66c0SMilanka Ringwald case HFP_VRA_VOICE_RECOGNITION_ACTIVATED: 1985de9e0ea7SMilanka Ringwald case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1986de9e0ea7SMilanka Ringwald case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1987fdda66c0SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF; 1988fdda66c0SMilanka Ringwald break; 1989de9e0ea7SMilanka Ringwald 1990de9e0ea7SMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED: 1991de9e0ea7SMilanka Ringwald case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1992de9e0ea7SMilanka Ringwald hfp_connection->deactivate_voice_recognition = true; 1993de9e0ea7SMilanka Ringwald break; 1994de9e0ea7SMilanka Ringwald 1995de9e0ea7SMilanka Ringwald case HFP_VRA_VOICE_RECOGNITION_OFF: 1996de9e0ea7SMilanka Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 1997de9e0ea7SMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_OFF: 1998fdda66c0SMilanka Ringwald default: 1999fdda66c0SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2000fdda66c0SMilanka Ringwald } 2001fdda66c0SMilanka Ringwald 2002fdda66c0SMilanka Ringwald hfp_hf_run_for_context(hfp_connection); 2003fdda66c0SMilanka Ringwald return ERROR_CODE_SUCCESS; 2004af97579eSMilanka Ringwald } 2005af97579eSMilanka Ringwald 20063c65e705SMilanka Ringwald uint8_t hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 20079c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2008a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20093c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2010a33eb0c4SMilanka Ringwald } 2011c8626498SMilanka Ringwald 20123c65e705SMilanka Ringwald if (hfp_connection->microphone_gain == gain) { 20133c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 20143c65e705SMilanka Ringwald } 20153c65e705SMilanka Ringwald 2016c1ab6cc1SMatthias Ringwald if ((gain < 0) || (gain > 15)){ 2017a0ffb263SMatthias Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 20183c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2019a0ffb263SMatthias Ringwald } 20203c65e705SMilanka Ringwald 2021a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = gain; 2022a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 20231c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20243c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2025ce263fc8SMatthias Ringwald } 2026ce263fc8SMatthias Ringwald 20273c65e705SMilanka Ringwald uint8_t hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 20289c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2029a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20303c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2031a33eb0c4SMilanka Ringwald } 2032c8626498SMilanka Ringwald 20333c65e705SMilanka Ringwald if (hfp_connection->speaker_gain == gain){ 20343c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 20353c65e705SMilanka Ringwald } 20363c65e705SMilanka Ringwald 2037c1ab6cc1SMatthias Ringwald if ((gain < 0) || (gain > 15)){ 2038a0ffb263SMatthias Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 20393c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2040a0ffb263SMatthias Ringwald } 20413c65e705SMilanka Ringwald 2042a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = gain; 2043a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 20441c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20453c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2046ce263fc8SMatthias Ringwald } 2047ce263fc8SMatthias Ringwald 20483c65e705SMilanka Ringwald uint8_t hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){ 20499c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2050a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20513c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2052a33eb0c4SMilanka Ringwald } 2053a0ffb263SMatthias Ringwald hfp_connection->hf_send_dtmf_code = code; 20541c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20553c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2056ce263fc8SMatthias Ringwald } 2057ce263fc8SMatthias Ringwald 20583c65e705SMilanka Ringwald uint8_t hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 20599c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2060a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20613c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2062a33eb0c4SMilanka Ringwald } 2063a0ffb263SMatthias Ringwald hfp_connection->hf_send_binp = 1; 20641c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20653c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2066ce263fc8SMatthias Ringwald } 20673deb3ec6SMatthias Ringwald 20683c65e705SMilanka Ringwald uint8_t hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){ 20699c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2070a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20713c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2072a33eb0c4SMilanka Ringwald } 2073a0ffb263SMatthias Ringwald hfp_connection->hf_send_clcc = 1; 20741c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20753c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2076667ec068SMatthias Ringwald } 2077667ec068SMatthias Ringwald 2078667ec068SMatthias Ringwald 20793c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){ 20809c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2081a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20823c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2083a33eb0c4SMilanka Ringwald } 2084a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 2085a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '?'; 20861c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20873c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2088667ec068SMatthias Ringwald } 2089667ec068SMatthias Ringwald 20903c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){ 20919c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2092a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20933c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2094a33eb0c4SMilanka Ringwald } 2095a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 2096a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '0'; 20971c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20983c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2099667ec068SMatthias Ringwald } 2100667ec068SMatthias Ringwald 21013c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){ 21029c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2103a33eb0c4SMilanka Ringwald if (!hfp_connection) { 21043c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2105a33eb0c4SMilanka Ringwald } 2106a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 2107a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '1'; 21081c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 21093c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2110667ec068SMatthias Ringwald } 2111667ec068SMatthias Ringwald 21123c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){ 21139c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2114a33eb0c4SMilanka Ringwald if (!hfp_connection) { 21153c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2116a33eb0c4SMilanka Ringwald } 2117a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 2118a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '2'; 21191c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 21203c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2121667ec068SMatthias Ringwald } 2122667ec068SMatthias Ringwald 21233c65e705SMilanka Ringwald uint8_t hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){ 21249c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2125a33eb0c4SMilanka Ringwald if (!hfp_connection) { 21263c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2127a33eb0c4SMilanka Ringwald } 2128a0ffb263SMatthias Ringwald hfp_connection->hf_send_cnum = 1; 21291c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 21303c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2131667ec068SMatthias Ringwald } 2132667ec068SMatthias Ringwald 21333c65e705SMilanka Ringwald uint8_t hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){ 21349c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2135a33eb0c4SMilanka Ringwald if (!hfp_connection) { 21363c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2137a33eb0c4SMilanka Ringwald } 2138667ec068SMatthias Ringwald // find index for assigned number 2139667ec068SMatthias Ringwald int i; 2140aeb0f0feSMatthias Ringwald for (i = 0; i < hfp_hf_indicators_nr ; i++){ 2141aeb0f0feSMatthias Ringwald if (hfp_hf_indicators[i] == assigned_number){ 2142667ec068SMatthias Ringwald // set value 2143aeb0f0feSMatthias Ringwald hfp_hf_indicators_value[i] = value; 2144667ec068SMatthias Ringwald // mark for update 2145a0ffb263SMatthias Ringwald if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){ 2146a0ffb263SMatthias Ringwald hfp_connection->generic_status_update_bitmap |= (1<<i); 2147667ec068SMatthias Ringwald // send update 21481c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 2149a0ffb263SMatthias Ringwald } 21503c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2151667ec068SMatthias Ringwald } 2152667ec068SMatthias Ringwald } 21533c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2154667ec068SMatthias Ringwald } 2155667ec068SMatthias Ringwald 2156d7f6b5cbSMatthias Ringwald int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){ 2157d7f6b5cbSMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2158d7f6b5cbSMatthias Ringwald if (!hfp_connection) { 2159d7f6b5cbSMatthias Ringwald return 0; 2160d7f6b5cbSMatthias Ringwald } 2161d7f6b5cbSMatthias Ringwald return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE); 2162d7f6b5cbSMatthias Ringwald } 216376cc1527SMatthias Ringwald 216476cc1527SMatthias 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){ 216576cc1527SMatthias Ringwald if (!name){ 2166aeb0f0feSMatthias Ringwald name = hfp_hf_default_service_name; 216776cc1527SMatthias Ringwald } 216876cc1527SMatthias Ringwald hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name); 216976cc1527SMatthias Ringwald 217076cc1527SMatthias Ringwald // Construct SupportedFeatures for SDP bitmap: 217176cc1527SMatthias Ringwald // 217276cc1527SMatthias Ringwald // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 217376cc1527SMatthias Ringwald // of the Bits 0 to 4 of the unsolicited result code +BRSF" 217476cc1527SMatthias Ringwald // 217576cc1527SMatthias Ringwald // Wide band speech (bit 5) requires Codec negotiation 217676cc1527SMatthias Ringwald // 217776cc1527SMatthias Ringwald uint16_t sdp_features = supported_features & 0x1f; 2178ef3ae4ebSMilanka Ringwald if ( (wide_band_speech != 0) && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){ 217976cc1527SMatthias Ringwald sdp_features |= 1 << 5; 218076cc1527SMatthias Ringwald } 2181ef3ae4ebSMilanka Ringwald 2182ef3ae4ebSMilanka Ringwald if (supported_features & (1 << HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS)){ 218356f1adacSMilanka Ringwald sdp_features |= 1 << 6; 2184ef3ae4ebSMilanka Ringwald } 2185ef3ae4ebSMilanka Ringwald 2186ef3ae4ebSMilanka Ringwald if (supported_features & (1 << HFP_HFSF_VOICE_RECOGNITION_TEXT)){ 218756f1adacSMilanka Ringwald sdp_features |= 1 << 7; 2188ef3ae4ebSMilanka Ringwald } 2189ef3ae4ebSMilanka Ringwald 219076cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 219176cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 219276cc1527SMatthias Ringwald } 219376cc1527SMatthias Ringwald 219476cc1527SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){ 219568466199SMilanka Ringwald btstack_assert(callback != NULL); 219668466199SMilanka Ringwald 219776cc1527SMatthias Ringwald hfp_hf_callback = callback; 219876cc1527SMatthias Ringwald hfp_set_hf_callback(callback); 219976cc1527SMatthias Ringwald } 2200