13deb3ec6SMatthias Ringwald /* 23deb3ec6SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 33deb3ec6SMatthias Ringwald * 43deb3ec6SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 53deb3ec6SMatthias Ringwald * modification, are permitted provided that the following conditions 63deb3ec6SMatthias Ringwald * are met: 73deb3ec6SMatthias Ringwald * 83deb3ec6SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 93deb3ec6SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 103deb3ec6SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 113deb3ec6SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 123deb3ec6SMatthias Ringwald * documentation and/or other materials provided with the distribution. 133deb3ec6SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 143deb3ec6SMatthias Ringwald * contributors may be used to endorse or promote products derived 153deb3ec6SMatthias Ringwald * from this software without specific prior written permission. 163deb3ec6SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 173deb3ec6SMatthias Ringwald * personal benefit and not for any commercial purpose or for 183deb3ec6SMatthias Ringwald * monetary gain. 193deb3ec6SMatthias Ringwald * 203deb3ec6SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 213deb3ec6SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 223deb3ec6SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 233deb3ec6SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 243deb3ec6SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 253deb3ec6SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 263deb3ec6SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 273deb3ec6SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 283deb3ec6SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 293deb3ec6SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 303deb3ec6SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 313deb3ec6SMatthias Ringwald * SUCH DAMAGE. 323deb3ec6SMatthias Ringwald * 333deb3ec6SMatthias Ringwald * Please inquire about commercial licensing options at 343deb3ec6SMatthias Ringwald * [email protected] 353deb3ec6SMatthias Ringwald * 363deb3ec6SMatthias Ringwald */ 37ab2c6ae4SMatthias Ringwald 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 11576cc1527SMatthias Ringwald 1169c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_hf_connection_context_for_acl_handle(uint16_t handle){ 1179c9c64c1SMatthias Ringwald btstack_linked_list_iterator_t it; 1189c9c64c1SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1199c9c64c1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1209c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1219c9c64c1SMatthias Ringwald if (hfp_connection->acl_handle != handle) continue; 1229c9c64c1SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_HF) continue; 1239c9c64c1SMatthias Ringwald return hfp_connection; 1249c9c64c1SMatthias Ringwald } 1259c9c64c1SMatthias Ringwald return NULL; 1269c9c64c1SMatthias Ringwald } 1279c9c64c1SMatthias Ringwald 12876cc1527SMatthias Ringwald /* emit functinos */ 1293deb3ec6SMatthias Ringwald 130a473a009SMatthias Ringwald static void hfp_hf_emit_subscriber_information(const hfp_connection_t * hfp_connection, uint8_t status){ 131a473a009SMatthias Ringwald if (hfp_hf_callback == NULL) return; 132d703d377SMatthias Ringwald uint8_t event[33]; 133a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 134a0ffb263SMatthias Ringwald event[1] = sizeof(event) - 2; 135a473a009SMatthias Ringwald event[2] = HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION; 136d703d377SMatthias Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 137d703d377SMatthias Ringwald event[5] = status; 138d703d377SMatthias Ringwald event[6] = hfp_connection->bnip_type; 139d703d377SMatthias Ringwald uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - 8); 140d703d377SMatthias Ringwald strncpy((char*)&event[7], hfp_connection->bnip_number, size); 141d703d377SMatthias Ringwald event[7 + size] = 0; 142a473a009SMatthias Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 143a0ffb263SMatthias Ringwald } 144a0ffb263SMatthias Ringwald 145a473a009SMatthias Ringwald static void hfp_hf_emit_type_and_number(const hfp_connection_t * hfp_connection, uint8_t event_subtype){ 146a473a009SMatthias Ringwald if (hfp_hf_callback == NULL) return; 147d703d377SMatthias Ringwald uint8_t event[32]; 148a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 149a0ffb263SMatthias Ringwald event[1] = sizeof(event) - 2; 150a0ffb263SMatthias Ringwald event[2] = event_subtype; 151d703d377SMatthias Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 152d703d377SMatthias Ringwald event[5] = hfp_connection->bnip_type; 153d703d377SMatthias Ringwald uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - 7); 154d703d377SMatthias Ringwald strncpy((char*)&event[6], hfp_connection->bnip_number, size); 155d703d377SMatthias Ringwald event[6 + size] = 0; 156a473a009SMatthias Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 157a0ffb263SMatthias Ringwald } 158a0ffb263SMatthias Ringwald 159a473a009SMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(const hfp_connection_t * hfp_connection){ 160a473a009SMatthias Ringwald if (hfp_hf_callback == NULL) return; 161d703d377SMatthias Ringwald uint8_t event[38]; 1620aee97efSMilanka Ringwald int pos = 0; 1630aee97efSMilanka Ringwald event[pos++] = HCI_EVENT_HFP_META; 1640aee97efSMilanka Ringwald event[pos++] = sizeof(event) - 2; 1650aee97efSMilanka Ringwald event[pos++] = HFP_SUBEVENT_ENHANCED_CALL_STATUS; 166d703d377SMatthias Ringwald little_endian_store_16(event, pos, hfp_connection->acl_handle); 167d703d377SMatthias Ringwald pos += 2; 168a473a009SMatthias Ringwald event[pos++] = hfp_connection->clcc_idx; 169a473a009SMatthias Ringwald event[pos++] = hfp_connection->clcc_dir; 170a473a009SMatthias Ringwald event[pos++] = hfp_connection->clcc_status; 171a473a009SMatthias Ringwald event[pos++] = hfp_connection->clcc_mode; 172a473a009SMatthias Ringwald event[pos++] = hfp_connection->clcc_mpty; 173a473a009SMatthias Ringwald event[pos++] = hfp_connection->bnip_type; 174d703d377SMatthias Ringwald uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - pos - 1); 175a473a009SMatthias Ringwald strncpy((char*)&event[pos], hfp_connection->bnip_number, size); 1760aee97efSMilanka Ringwald pos += size; 1770aee97efSMilanka Ringwald event[pos++] = 0; 178a473a009SMatthias Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, pos); 179a0ffb263SMatthias Ringwald } 180a0ffb263SMatthias Ringwald 18176cc1527SMatthias Ringwald 182a473a009SMatthias Ringwald static void hfp_emit_ag_indicator_event(const hfp_connection_t * hfp_connection, const hfp_ag_indicator_t * indicator){ 183a473a009SMatthias Ringwald if (hfp_hf_callback == NULL) return; 184d703d377SMatthias Ringwald uint8_t event[12+HFP_MAX_INDICATOR_DESC_SIZE+1]; 18576cc1527SMatthias Ringwald int pos = 0; 18676cc1527SMatthias Ringwald event[pos++] = HCI_EVENT_HFP_META; 18776cc1527SMatthias Ringwald event[pos++] = sizeof(event) - 2; 18876cc1527SMatthias Ringwald event[pos++] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED; 189d703d377SMatthias Ringwald little_endian_store_16(event, pos, hfp_connection->acl_handle); 190d703d377SMatthias Ringwald pos += 2; 191a473a009SMatthias Ringwald event[pos++] = indicator->index; 192a473a009SMatthias Ringwald event[pos++] = indicator->status; 193a473a009SMatthias Ringwald event[pos++] = indicator->min_range; 194a473a009SMatthias Ringwald event[pos++] = indicator->max_range; 195a473a009SMatthias Ringwald event[pos++] = indicator->mandatory; 196a473a009SMatthias Ringwald event[pos++] = indicator->enabled; 197a473a009SMatthias Ringwald event[pos++] = indicator->status_changed; 198a473a009SMatthias Ringwald strncpy((char*)&event[pos], indicator->name, HFP_MAX_INDICATOR_DESC_SIZE); 19976cc1527SMatthias Ringwald pos += HFP_MAX_INDICATOR_DESC_SIZE; 20076cc1527SMatthias Ringwald event[pos] = 0; 201a473a009SMatthias Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 2023deb3ec6SMatthias Ringwald } 2033deb3ec6SMatthias Ringwald 204a473a009SMatthias Ringwald static void hfp_emit_network_operator_event(const hfp_connection_t * hfp_connection){ 205a473a009SMatthias Ringwald if (hfp_hf_callback == NULL) return; 206d703d377SMatthias Ringwald uint8_t event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE+1]; 20776cc1527SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 20876cc1527SMatthias Ringwald event[1] = sizeof(event) - 2; 20976cc1527SMatthias Ringwald event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED; 210d703d377SMatthias Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 211d703d377SMatthias Ringwald event[5] = hfp_connection->network_operator.mode; 212d703d377SMatthias Ringwald event[6] = hfp_connection->network_operator.format; 213d703d377SMatthias Ringwald strncpy((char*)&event[7], hfp_connection->network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE); 214d703d377SMatthias Ringwald event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE] = 0; 215a473a009SMatthias Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 2163deb3ec6SMatthias Ringwald } 2173deb3ec6SMatthias Ringwald 218b95cac54SMilanka Ringwald 219b95cac54SMilanka Ringwald static void hfp_hf_emit_enhanced_voice_recognition_text(hfp_connection_t * hfp_connection){ 220b95cac54SMilanka Ringwald btstack_assert(hfp_connection != NULL); 221b95cac54SMilanka Ringwald uint8_t event[HFP_MAX_VR_TEXT_SIZE]; 222b95cac54SMilanka Ringwald int pos = 0; 223b95cac54SMilanka Ringwald event[pos++] = HCI_EVENT_HFP_META; 224b95cac54SMilanka Ringwald event[pos++] = sizeof(event) - 2; 225b95cac54SMilanka Ringwald event[pos++] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_MESSAGE; 226b95cac54SMilanka Ringwald little_endian_store_16(event, pos, hfp_connection->acl_handle); 227b95cac54SMilanka Ringwald pos += 2; 228b95cac54SMilanka Ringwald little_endian_store_16(event, pos, hfp_connection->ag_msg.text_id); 229b95cac54SMilanka Ringwald pos += 2; 230b95cac54SMilanka Ringwald event[pos++] = hfp_connection->ag_msg.text_type; 231*e83f1be7SMilanka Ringwald event[pos++] = hfp_connection->ag_msg.text_operation; 232b95cac54SMilanka Ringwald 233b95cac54SMilanka Ringwald // length, zero ending 234b95cac54SMilanka Ringwald uint16_t value_length = hfp_connection->ag_vra_msg_length; 235b95cac54SMilanka Ringwald uint8_t * value = &hfp_connection->line_buffer[0]; 236b95cac54SMilanka Ringwald uint16_t size = btstack_min(value_length, sizeof(event) - pos - 2 - 1); 237b95cac54SMilanka Ringwald little_endian_store_16(event, pos, size+1); 238b95cac54SMilanka Ringwald pos += 2; 239b95cac54SMilanka Ringwald memcpy(&event[pos], value, size); 240b95cac54SMilanka Ringwald event[pos + size] = 0; 241b95cac54SMilanka Ringwald pos += size + 1; 242b95cac54SMilanka Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, pos); 243b95cac54SMilanka Ringwald } 244b95cac54SMilanka Ringwald 24576cc1527SMatthias Ringwald /* send commands */ 24689425bfcSMilanka Ringwald 24789425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){ 2483deb3ec6SMatthias Ringwald char buffer[20]; 2491599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s\r", cmd); 25089425bfcSMilanka Ringwald return send_str_over_rfcomm(cid, buffer); 25189425bfcSMilanka Ringwald } 25289425bfcSMilanka Ringwald 25389425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){ 25489425bfcSMilanka Ringwald char buffer[20]; 2551599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s%s\r", cmd, mark); 25689425bfcSMilanka Ringwald return send_str_over_rfcomm(cid, buffer); 25789425bfcSMilanka Ringwald } 25889425bfcSMilanka Ringwald 25986da9d74SMatthias Ringwald static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint16_t value){ 26089425bfcSMilanka Ringwald char buffer[40]; 2611599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s=%d\r", cmd, value); 2623deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2633deb3ec6SMatthias Ringwald } 2643deb3ec6SMatthias Ringwald 2653deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){ 2663deb3ec6SMatthias Ringwald char buffer[30]; 26789425bfcSMilanka Ringwald const int size = sizeof(buffer); 26889425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS); 269aeb0f0feSMatthias Ringwald offset += join(buffer+offset, size-offset, hfp_hf_codecs, hfp_hf_codecs_nr); 2701599fe57SMatthias Ringwald offset += snprintf(buffer+offset, size-offset, "\r"); 2713deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2723deb3ec6SMatthias Ringwald } 2733deb3ec6SMatthias Ringwald 2743deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){ 2753deb3ec6SMatthias Ringwald char buffer[50]; 27689425bfcSMilanka Ringwald const int size = sizeof(buffer); 27789425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS); 27889425bfcSMilanka Ringwald offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr); 2791599fe57SMatthias Ringwald offset += snprintf(buffer+offset, size-offset, "\r"); 2803deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2813deb3ec6SMatthias Ringwald } 2823deb3ec6SMatthias Ringwald 2833deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){ 2843deb3ec6SMatthias Ringwald char buffer[30]; 28589425bfcSMilanka Ringwald const int size = sizeof(buffer); 28689425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR); 287aeb0f0feSMatthias Ringwald offset += join(buffer+offset, size-offset, hfp_hf_indicators, hfp_hf_indicators_nr); 2881599fe57SMatthias Ringwald offset += snprintf(buffer+offset, size-offset, "\r"); 2893deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2903deb3ec6SMatthias Ringwald } 2913deb3ec6SMatthias Ringwald 29289425bfcSMilanka Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){ 2933deb3ec6SMatthias Ringwald char buffer[20]; 2941599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate); 295ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 296ce263fc8SMatthias Ringwald } 297ce263fc8SMatthias Ringwald 298ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){ 299ce263fc8SMatthias Ringwald char buffer[40]; 300aeb0f0feSMatthias Ringwald snprintf(buffer, sizeof(buffer), "%s%s;\r", HFP_CALL_PHONE_NUMBER, hfp_hf_phone_number); 301ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 302ce263fc8SMatthias Ringwald } 303ce263fc8SMatthias Ringwald 304a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){ 305ce263fc8SMatthias Ringwald char buffer[40]; 3061599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "%s>%d;\r", HFP_CALL_PHONE_NUMBER, memory_id); 307ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 308ce263fc8SMatthias Ringwald } 309ce263fc8SMatthias Ringwald 310f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){ 31189425bfcSMilanka Ringwald char buffer[40]; 3121599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s=%u\r", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number); 313ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 314ce263fc8SMatthias Ringwald } 315ce263fc8SMatthias Ringwald 316ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){ 317ce263fc8SMatthias Ringwald char buffer[20]; 3181599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s=%c\r", HFP_TRANSMIT_DTMF_CODES, code); 319ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 320ce263fc8SMatthias Ringwald } 321ce263fc8SMatthias Ringwald 32297d2cadbSMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){ 3231599fe57SMatthias Ringwald return send_str_over_rfcomm(cid, (char *) "ATA\r"); 32497d2cadbSMatthias Ringwald } 32597d2cadbSMatthias Ringwald 32689425bfcSMilanka Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){ 327aeb0f0feSMatthias Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_hf_supported_features); 32889425bfcSMilanka Ringwald } 32989425bfcSMilanka Ringwald 33089425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){ 33189425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?"); 33289425bfcSMilanka Ringwald } 33389425bfcSMilanka Ringwald 33489425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){ 33589425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?"); 33689425bfcSMilanka Ringwald } 33789425bfcSMilanka Ringwald 33889425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){ 33989425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?"); 34089425bfcSMilanka Ringwald } 34189425bfcSMilanka Ringwald 34289425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){ 34389425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?"); 34489425bfcSMilanka Ringwald } 34589425bfcSMilanka Ringwald 34689425bfcSMilanka Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){ 34789425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?"); 34889425bfcSMilanka Ringwald } 34989425bfcSMilanka Ringwald 35089425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){ 35189425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0"); 35289425bfcSMilanka Ringwald } 35389425bfcSMilanka Ringwald 35489425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){ 35589425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?"); 35689425bfcSMilanka Ringwald } 35789425bfcSMilanka Ringwald 35889425bfcSMilanka Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){ 35989425bfcSMilanka Ringwald return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP); 36089425bfcSMilanka Ringwald } 36189425bfcSMilanka Ringwald 36289425bfcSMilanka Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){ 36389425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain); 36489425bfcSMilanka Ringwald } 36589425bfcSMilanka Ringwald 36689425bfcSMilanka Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){ 36789425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain); 36889425bfcSMilanka Ringwald } 36989425bfcSMilanka Ringwald 37089425bfcSMilanka Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){ 37189425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate); 37289425bfcSMilanka Ringwald } 37389425bfcSMilanka Ringwald 37489425bfcSMilanka Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){ 37589425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate); 37689425bfcSMilanka Ringwald } 37789425bfcSMilanka Ringwald 37889425bfcSMilanka Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){ 37989425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate); 38089425bfcSMilanka Ringwald } 38189425bfcSMilanka Ringwald 38289425bfcSMilanka Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){ 38389425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec); 38489425bfcSMilanka Ringwald } 38589425bfcSMilanka Ringwald 38689425bfcSMilanka Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){ 38789425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable); 38889425bfcSMilanka Ringwald } 38989425bfcSMilanka Ringwald 39089425bfcSMilanka Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){ 39189425bfcSMilanka Ringwald return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER); 39289425bfcSMilanka Ringwald } 39389425bfcSMilanka Ringwald 39489425bfcSMilanka Ringwald static int hfp_hf_send_chup(uint16_t cid){ 39589425bfcSMilanka Ringwald return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL); 39689425bfcSMilanka Ringwald } 39789425bfcSMilanka Ringwald 398ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){ 39989425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1"); 400ce263fc8SMatthias Ringwald } 401ce263fc8SMatthias Ringwald 402667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){ 40389425bfcSMilanka Ringwald return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS); 404667ec068SMatthias Ringwald } 405667ec068SMatthias Ringwald 40676cc1527SMatthias Ringwald /* state machines */ 4073deb3ec6SMatthias Ringwald 408a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){ 409a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 410a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 411aa4dd815SMatthias Ringwald int done = 1; 412498a8121SMilanka Ringwald log_info("hfp_hf_run_for_context_service_level_connection state %d\n", hfp_connection->state); 413a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 4143deb3ec6SMatthias Ringwald case HFP_EXCHANGE_SUPPORTED_FEATURES: 415aeb0f0feSMatthias Ringwald hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_hf_codecs, &hfp_hf_codecs_nr); 416a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES; 417a0ffb263SMatthias Ringwald hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid); 4183deb3ec6SMatthias Ringwald break; 4193deb3ec6SMatthias Ringwald case HFP_NOTIFY_ON_CODECS: 420a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS; 421a0ffb263SMatthias Ringwald hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 4223deb3ec6SMatthias Ringwald break; 4233deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INDICATORS: 424a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 425a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid); 4263deb3ec6SMatthias Ringwald break; 4273deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INDICATORS_STATUS: 428a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS; 429a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid); 4303deb3ec6SMatthias Ringwald break; 4313deb3ec6SMatthias Ringwald case HFP_ENABLE_INDICATORS_STATUS_UPDATE: 432a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE; 433a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1); 4343deb3ec6SMatthias Ringwald break; 4353deb3ec6SMatthias Ringwald case HFP_RETRIEVE_CAN_HOLD_CALL: 436a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL; 437a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid); 4383deb3ec6SMatthias Ringwald break; 4393deb3ec6SMatthias Ringwald case HFP_LIST_GENERIC_STATUS_INDICATORS: 440a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 441a0ffb263SMatthias Ringwald hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4423deb3ec6SMatthias Ringwald break; 4433deb3ec6SMatthias Ringwald case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS: 444a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS; 445a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4463deb3ec6SMatthias Ringwald break; 4473deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 448a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 449a0ffb263SMatthias Ringwald hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4503deb3ec6SMatthias Ringwald break; 4513deb3ec6SMatthias Ringwald default: 452aa4dd815SMatthias Ringwald done = 0; 4533deb3ec6SMatthias Ringwald break; 4543deb3ec6SMatthias Ringwald } 4553deb3ec6SMatthias Ringwald return done; 4563deb3ec6SMatthias Ringwald } 4573deb3ec6SMatthias Ringwald 458ce263fc8SMatthias Ringwald 459a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){ 460a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 461498a8121SMilanka Ringwald if (hfp_connection->ok_pending){ 462498a8121SMilanka Ringwald return 0; 463498a8121SMilanka Ringwald } 464ce263fc8SMatthias Ringwald int done = 0; 465a0ffb263SMatthias Ringwald if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 466a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 467ce263fc8SMatthias Ringwald done = 1; 468a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators); 469ce263fc8SMatthias Ringwald return done; 470ce263fc8SMatthias Ringwald }; 471a0ffb263SMatthias Ringwald if (hfp_connection->change_status_update_for_individual_ag_indicators){ 472a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 473ce263fc8SMatthias Ringwald done = 1; 474a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid, 475a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap, 476a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr); 477ce263fc8SMatthias Ringwald return done; 478ce263fc8SMatthias Ringwald } 479ce263fc8SMatthias Ringwald 480a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 481ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_SET_FORMAT: 482a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK; 483a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 484a0ffb263SMatthias Ringwald hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid); 485ce263fc8SMatthias Ringwald return 1; 486ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_SEND_QUERY: 487a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT; 488a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 489a0ffb263SMatthias Ringwald hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid); 490ce263fc8SMatthias Ringwald return 1; 491ce263fc8SMatthias Ringwald default: 492ce263fc8SMatthias Ringwald break; 493ce263fc8SMatthias Ringwald } 494ce263fc8SMatthias Ringwald 495a0ffb263SMatthias Ringwald if (hfp_connection->enable_extended_audio_gateway_error_report){ 496a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 497ce263fc8SMatthias Ringwald done = 1; 498a0ffb263SMatthias Ringwald hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report); 499ce263fc8SMatthias Ringwald return done; 500ce263fc8SMatthias Ringwald } 501ce263fc8SMatthias Ringwald 502ce263fc8SMatthias Ringwald return done; 503ce263fc8SMatthias Ringwald } 504ce263fc8SMatthias Ringwald 505af97579eSMilanka Ringwald static int hfp_hf_voice_recognition_state_machine(hfp_connection_t * hfp_connection){ 506be55a11dSMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) { 507be55a11dSMilanka Ringwald return 0; 508be55a11dSMilanka Ringwald } 509be55a11dSMilanka Ringwald int done = 0; 510fd4151d1SMilanka Ringwald 5110b4debbfSMilanka Ringwald if (hfp_connection->ok_pending == 1){ 5120b4debbfSMilanka Ringwald return 0; 5130b4debbfSMilanka Ringwald } 5140b4debbfSMilanka Ringwald // voice recognition activated from AG 5150b4debbfSMilanka Ringwald if (hfp_connection->command == HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION){ 5160b4debbfSMilanka Ringwald switch(hfp_connection->vra_state_requested){ 5170b4debbfSMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED: 5180b4debbfSMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_OFF: 519de9e0ea7SMilanka Ringwald case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 5200b4debbfSMilanka Ringwald // ignore AG command, continue to wait for OK 5210b4debbfSMilanka Ringwald return 0; 522cf75be85SMilanka Ringwald 5230b4debbfSMilanka Ringwald default: 524b95cac54SMilanka Ringwald if (hfp_connection->ag_vra_msg_length > 0){ 525b95cac54SMilanka Ringwald hfp_hf_emit_enhanced_voice_recognition_text(hfp_connection); 526b95cac54SMilanka Ringwald hfp_connection->ag_vra_msg_length = 0; 527b95cac54SMilanka Ringwald break; 528b95cac54SMilanka Ringwald } 529cf75be85SMilanka Ringwald switch(hfp_connection->ag_vra_state){ 530cf75be85SMilanka Ringwald case HFP_VOICE_RECOGNITION_STATE_AG_READY: 531013cc750SMilanka Ringwald switch (hfp_connection->ag_vra_status){ 532013cc750SMilanka Ringwald case 0: 5330b4debbfSMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF; 534013cc750SMilanka Ringwald break; 535013cc750SMilanka Ringwald case 1: 536013cc750SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED; 537013cc750SMilanka Ringwald break; 538013cc750SMilanka Ringwald case 2: 539013cc750SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 540013cc750SMilanka Ringwald break; 541013cc750SMilanka Ringwald default: 542013cc750SMilanka Ringwald break; 5430b4debbfSMilanka Ringwald } 5440b4debbfSMilanka Ringwald break; 545cf75be85SMilanka Ringwald default: 546cf75be85SMilanka Ringwald // state messages from AG 547cf75be85SMilanka Ringwald hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS); 548cf75be85SMilanka Ringwald break; 549cf75be85SMilanka Ringwald } 550cf75be85SMilanka Ringwald break; 5510b4debbfSMilanka Ringwald } 5520b4debbfSMilanka Ringwald hfp_connection->command = HFP_CMD_NONE; 5530b4debbfSMilanka Ringwald } 5540b4debbfSMilanka Ringwald 5550b4debbfSMilanka Ringwald 556498a8121SMilanka Ringwald switch (hfp_connection->vra_state_requested){ 557fdda66c0SMilanka Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 558fdda66c0SMilanka Ringwald done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0); 559fdda66c0SMilanka Ringwald if (done != 0){ 560fdda66c0SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF; 561498a8121SMilanka Ringwald hfp_connection->ok_pending = 1; 562498a8121SMilanka Ringwald } 563fd4151d1SMilanka Ringwald return 1; 564fd4151d1SMilanka Ringwald 565fd4151d1SMilanka Ringwald 566fdda66c0SMilanka Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED: 567fdda66c0SMilanka Ringwald done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1); 568fdda66c0SMilanka Ringwald if (done != 0){ 569fdda66c0SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED; 570fd4151d1SMilanka Ringwald hfp_connection->ok_pending = 1; 571fd4151d1SMilanka Ringwald return 1; 5720b4debbfSMilanka Ringwald } 5730b4debbfSMilanka Ringwald break; 574013cc750SMilanka Ringwald 575de9e0ea7SMilanka Ringwald case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 576de9e0ea7SMilanka Ringwald done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 2); 577de9e0ea7SMilanka Ringwald if (done != 0){ 578de9e0ea7SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 579de9e0ea7SMilanka Ringwald hfp_connection->ok_pending = 1; 580de9e0ea7SMilanka Ringwald return 1; 581de9e0ea7SMilanka Ringwald } 582de9e0ea7SMilanka Ringwald break; 583de9e0ea7SMilanka Ringwald 584de9e0ea7SMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_OFF: 585de9e0ea7SMilanka Ringwald hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF; 586de9e0ea7SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 587de9e0ea7SMilanka Ringwald hfp_connection->activate_voice_recognition = false; 588de9e0ea7SMilanka Ringwald if (hfp_connection->activate_voice_recognition){ 589de9e0ea7SMilanka Ringwald hfp_hf_activate_voice_recognition(hfp_connection->acl_handle); 590de9e0ea7SMilanka Ringwald } else { 591553a4a56SMilanka Ringwald hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS); 592de9e0ea7SMilanka Ringwald } 593de9e0ea7SMilanka Ringwald break; 594de9e0ea7SMilanka Ringwald 595be55a11dSMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED: 596498a8121SMilanka Ringwald hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_ACTIVATED; 597498a8121SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 598de9e0ea7SMilanka Ringwald hfp_connection->activate_voice_recognition = false; 599de9e0ea7SMilanka Ringwald if (hfp_connection->deactivate_voice_recognition){ 600de9e0ea7SMilanka Ringwald hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle); 601de9e0ea7SMilanka Ringwald } else { 602553a4a56SMilanka Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS); 603de9e0ea7SMilanka Ringwald } 604be55a11dSMilanka Ringwald break; 605be55a11dSMilanka Ringwald 606de9e0ea7SMilanka Ringwald 607013cc750SMilanka Ringwald case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 608013cc750SMilanka Ringwald hfp_connection->vra_state = HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 609498a8121SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 610de9e0ea7SMilanka Ringwald hfp_connection->activate_voice_recognition = false; 611de9e0ea7SMilanka Ringwald if (hfp_connection->deactivate_voice_recognition){ 612de9e0ea7SMilanka Ringwald hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle); 613de9e0ea7SMilanka Ringwald } else { 614de9e0ea7SMilanka Ringwald hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_SUCCESS); 615de9e0ea7SMilanka Ringwald } 616be55a11dSMilanka Ringwald break; 617fd4151d1SMilanka Ringwald 618be55a11dSMilanka Ringwald default: 619be55a11dSMilanka Ringwald break; 620be55a11dSMilanka Ringwald } 621be55a11dSMilanka Ringwald return done; 622be55a11dSMilanka Ringwald } 623be55a11dSMilanka Ringwald 624be55a11dSMilanka Ringwald 625be55a11dSMilanka Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){ 626a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 627ce263fc8SMatthias Ringwald 628332ca98fSMatthias Ringwald if (hfp_connection->trigger_codec_exchange){ 629332ca98fSMatthias Ringwald hfp_connection->trigger_codec_exchange = 0; 630ce263fc8SMatthias Ringwald 631a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 632a0ffb263SMatthias Ringwald hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid); 633332ca98fSMatthias Ringwald return 1; 634332ca98fSMatthias Ringwald } 635332ca98fSMatthias Ringwald 6361cc65c4fSMatthias Ringwald if (hfp_connection->hf_send_codec_confirm){ 6371cc65c4fSMatthias Ringwald hfp_connection->hf_send_codec_confirm = false; 638ce263fc8SMatthias Ringwald 639a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 640fcb08cdbSMilanka Ringwald hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed); 6411cc65c4fSMatthias Ringwald return 1; 6421cc65c4fSMatthias Ringwald } 6431cc65c4fSMatthias Ringwald 6441cc65c4fSMatthias Ringwald if (hfp_connection->hf_send_supported_codecs){ 6451cc65c4fSMatthias Ringwald hfp_connection->hf_send_supported_codecs = false; 6461cc65c4fSMatthias Ringwald 647a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 648a0ffb263SMatthias Ringwald hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 6491cc65c4fSMatthias Ringwald return 1; 6501cc65c4fSMatthias Ringwald } 651ce263fc8SMatthias Ringwald 652ce263fc8SMatthias Ringwald return 0; 653ce263fc8SMatthias Ringwald } 654ce263fc8SMatthias Ringwald 655a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){ 656505f1c30SMatthias Ringwald if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) || 657505f1c30SMatthias Ringwald (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0; 658ce263fc8SMatthias Ringwald 65964f19dedSMilanka Ringwald if (hfp_connection->release_audio_connection){ 660a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 661a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 0; 662a0ffb263SMatthias Ringwald gap_disconnect(hfp_connection->sco_handle); 663ce263fc8SMatthias Ringwald return 1; 664ce263fc8SMatthias Ringwald } 665ce263fc8SMatthias Ringwald 666a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 667ce263fc8SMatthias Ringwald 668ce263fc8SMatthias Ringwald // run codecs exchange 669a0ffb263SMatthias Ringwald int done = codecs_exchange_state_machine(hfp_connection); 670ce263fc8SMatthias Ringwald if (done) return 1; 671ce263fc8SMatthias Ringwald 67238200c1dSMilanka Ringwald if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0; 67338200c1dSMilanka Ringwald if (hfp_connection->establish_audio_connection){ 67438200c1dSMilanka Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 67538200c1dSMilanka Ringwald hfp_connection->establish_audio_connection = 0; 67638200c1dSMilanka Ringwald hfp_setup_synchronous_connection(hfp_connection); 67738200c1dSMilanka Ringwald return 1; 67838200c1dSMilanka Ringwald } 679ce263fc8SMatthias Ringwald return 0; 680ce263fc8SMatthias Ringwald } 681ce263fc8SMatthias Ringwald 68238200c1dSMilanka Ringwald 683a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 684eaf2b0a1SMatthias Ringwald 685eaf2b0a1SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 686eaf2b0a1SMatthias Ringwald 687a0ffb263SMatthias Ringwald if (hfp_connection->hf_answer_incoming_call){ 688a0ffb263SMatthias Ringwald hfp_hf_cmd_ata(hfp_connection->rfcomm_cid); 689a0ffb263SMatthias Ringwald hfp_connection->hf_answer_incoming_call = 0; 690ce263fc8SMatthias Ringwald return 1; 691ce263fc8SMatthias Ringwald } 692ce263fc8SMatthias Ringwald return 0; 693ce263fc8SMatthias Ringwald } 694ce263fc8SMatthias Ringwald 6951c6a0fc0SMatthias Ringwald static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){ 6967522e673SMatthias Ringwald 69776cc1527SMatthias Ringwald btstack_assert(hfp_connection != NULL); 69876cc1527SMatthias Ringwald btstack_assert(hfp_connection->local_role == HFP_ROLE_HF); 69976cc1527SMatthias Ringwald 70076cc1527SMatthias Ringwald // during SDP query, RFCOMM CID is not set 70176cc1527SMatthias Ringwald if (hfp_connection->rfcomm_cid == 0) return; 70222387625SMatthias Ringwald 7033721a235SMatthias Ringwald // assert command could be sent 7043721a235SMatthias Ringwald if (hci_can_send_command_packet_now() == 0) return; 7053721a235SMatthias Ringwald 7063721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP 7073721a235SMatthias Ringwald // WBS Disassociate 7083721a235SMatthias Ringwald if (hfp_connection->cc256x_send_wbs_disassociate){ 7093721a235SMatthias Ringwald hfp_connection->cc256x_send_wbs_disassociate = false; 7103721a235SMatthias Ringwald hci_send_cmd(&hci_ti_wbs_disassociate); 7113721a235SMatthias Ringwald return; 7123721a235SMatthias Ringwald } 7133721a235SMatthias Ringwald // Write Codec Config 7143721a235SMatthias Ringwald if (hfp_connection->cc256x_send_write_codec_config){ 7153721a235SMatthias Ringwald hfp_connection->cc256x_send_write_codec_config = false; 7163721a235SMatthias Ringwald hfp_cc256x_write_codec_config(hfp_connection); 7173721a235SMatthias Ringwald return; 7183721a235SMatthias Ringwald } 7193721a235SMatthias Ringwald // WBS Associate 7203721a235SMatthias Ringwald if (hfp_connection->cc256x_send_wbs_associate){ 7213721a235SMatthias Ringwald hfp_connection->cc256x_send_wbs_associate = false; 7223721a235SMatthias Ringwald hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle); 7233721a235SMatthias Ringwald return; 7243721a235SMatthias Ringwald } 7253721a235SMatthias Ringwald #endif 726689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 727689d4323SMatthias Ringwald // Enable WBS 728689d4323SMatthias Ringwald if (hfp_connection->bcm_send_enable_wbs){ 729689d4323SMatthias Ringwald hfp_connection->bcm_send_enable_wbs = false; 730689d4323SMatthias Ringwald hci_send_cmd(&hci_bcm_enable_wbs, 1, 2); 731689d4323SMatthias Ringwald return; 732689d4323SMatthias Ringwald } 733689d4323SMatthias Ringwald // Write I2S/PCM params 734689d4323SMatthias Ringwald if (hfp_connection->bcm_send_write_i2spcm_interface_param){ 735689d4323SMatthias Ringwald hfp_connection->bcm_send_write_i2spcm_interface_param = false; 736689d4323SMatthias Ringwald hfp_bcm_write_i2spcm_interface_param(hfp_connection); 737689d4323SMatthias Ringwald return; 738689d4323SMatthias Ringwald } 739689d4323SMatthias Ringwald // Disable WBS 740689d4323SMatthias Ringwald if (hfp_connection->bcm_send_disable_wbs){ 741689d4323SMatthias Ringwald hfp_connection->bcm_send_disable_wbs = false; 742689d4323SMatthias Ringwald hci_send_cmd(&hci_bcm_enable_wbs, 0, 2); 743689d4323SMatthias Ringwald return; 744689d4323SMatthias Ringwald } 745689d4323SMatthias Ringwald #endif 74648e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS) 74748e6eeeeSMatthias Ringwald if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){ 74848e6eeeeSMatthias Ringwald hfp_finalize_connection_context(hfp_connection); 74948e6eeeeSMatthias Ringwald return; 75048e6eeeeSMatthias Ringwald } 75148e6eeeeSMatthias Ringwald #endif 7523721a235SMatthias Ringwald 753cb81d35dSMatthias Ringwald if (hfp_connection->accept_sco){ 754cb81d35dSMatthias Ringwald bool incoming_eSCO = hfp_connection->accept_sco == 2; 755cb81d35dSMatthias Ringwald hfp_connection->accept_sco = 0; 7567522e673SMatthias Ringwald // notify about codec selection if not done already 7577522e673SMatthias Ringwald if (hfp_connection->negotiated_codec == 0){ 7587522e673SMatthias Ringwald hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 7597522e673SMatthias Ringwald } 760cb81d35dSMatthias Ringwald hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO); 7617522e673SMatthias Ringwald return; 7627522e673SMatthias Ringwald } 7637522e673SMatthias Ringwald 764d4dd47ffSMatthias Ringwald if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) { 765d4dd47ffSMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 766d4dd47ffSMatthias Ringwald return; 767d4dd47ffSMatthias Ringwald } 768a0ffb263SMatthias Ringwald int done = hfp_hf_run_for_context_service_level_connection(hfp_connection); 769ce263fc8SMatthias Ringwald if (!done){ 770a0ffb263SMatthias Ringwald done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection); 771ce263fc8SMatthias Ringwald } 772ce263fc8SMatthias Ringwald if (!done){ 773c95b5b3cSMilanka Ringwald done = hfp_hf_run_for_audio_connection(hfp_connection); 774be55a11dSMilanka Ringwald } 775be55a11dSMilanka Ringwald if (!done){ 776c95b5b3cSMilanka Ringwald done = hfp_hf_voice_recognition_state_machine(hfp_connection); 777ce263fc8SMatthias Ringwald } 778ce263fc8SMatthias Ringwald if (!done){ 779a0ffb263SMatthias Ringwald done = call_setup_state_machine(hfp_connection); 780ce263fc8SMatthias Ringwald } 781ce263fc8SMatthias Ringwald 782e2c3b58dSMilanka Ringwald // don't send a new command while ok still pending or SLC is not established 783e2c3b58dSMilanka Ringwald if (hfp_connection->ok_pending || (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED)){ 784e2c3b58dSMilanka Ringwald return; 785e2c3b58dSMilanka Ringwald } 7861016a228SMatthias Ringwald 787a0ffb263SMatthias Ringwald if (hfp_connection->send_microphone_gain){ 788a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 0; 789a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 790a0ffb263SMatthias Ringwald hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 791ce263fc8SMatthias Ringwald return; 792ce263fc8SMatthias Ringwald } 793ce263fc8SMatthias Ringwald 794a0ffb263SMatthias Ringwald if (hfp_connection->send_speaker_gain){ 795a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 0; 796a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 797a0ffb263SMatthias Ringwald hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 798ce263fc8SMatthias Ringwald return; 799ce263fc8SMatthias Ringwald } 800ce263fc8SMatthias Ringwald 801a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_calling_line_notification){ 802a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_calling_line_notification = 0; 803a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 804a0ffb263SMatthias Ringwald hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0); 805ce263fc8SMatthias Ringwald return; 806ce263fc8SMatthias Ringwald } 807ce263fc8SMatthias Ringwald 808a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_calling_line_notification){ 809a0ffb263SMatthias Ringwald hfp_connection->hf_activate_calling_line_notification = 0; 810a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 811a0ffb263SMatthias Ringwald hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1); 812ce263fc8SMatthias Ringwald return; 813ce263fc8SMatthias Ringwald } 814ce263fc8SMatthias Ringwald 815a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){ 816a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0; 81799af1e28SMilanka Ringwald hfp_connection->response_pending_for_command = HFP_CMD_TURN_OFF_EC_AND_NR; 818a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 819e2c3b58dSMilanka Ringwald hfp_hf_send_cmd_with_int(hfp_connection->rfcomm_cid, HFP_TURN_OFF_EC_AND_NR, 0); 820ce263fc8SMatthias Ringwald return; 821ce263fc8SMatthias Ringwald } 822ce263fc8SMatthias Ringwald 823a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_call_waiting_notification){ 824a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_call_waiting_notification = 0; 825a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 826a0ffb263SMatthias Ringwald hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0); 827ce263fc8SMatthias Ringwald return; 828ce263fc8SMatthias Ringwald } 829ce263fc8SMatthias Ringwald 830a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_call_waiting_notification){ 831a0ffb263SMatthias Ringwald hfp_connection->hf_activate_call_waiting_notification = 0; 832a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 833a0ffb263SMatthias Ringwald hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1); 834ce263fc8SMatthias Ringwald return; 835ce263fc8SMatthias Ringwald } 836ce263fc8SMatthias Ringwald 837a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_outgoing_call){ 838a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_outgoing_call = 0; 839a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 840a0ffb263SMatthias Ringwald hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid); 841ce263fc8SMatthias Ringwald return; 842ce263fc8SMatthias Ringwald } 843ce263fc8SMatthias Ringwald 844a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_memory_dialing){ 845a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_memory_dialing = 0; 846a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 847a0ffb263SMatthias Ringwald hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id); 848ce263fc8SMatthias Ringwald return; 849ce263fc8SMatthias Ringwald } 850ce263fc8SMatthias Ringwald 851a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_redial_last_number){ 852a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_redial_last_number = 0; 853a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 854a0ffb263SMatthias Ringwald hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid); 855ce263fc8SMatthias Ringwald return; 856ce263fc8SMatthias Ringwald } 857ce263fc8SMatthias Ringwald 858a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chup){ 859a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 0; 860a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 861a0ffb263SMatthias Ringwald hfp_hf_send_chup(hfp_connection->rfcomm_cid); 862ce263fc8SMatthias Ringwald return; 863ce263fc8SMatthias Ringwald } 864ce263fc8SMatthias Ringwald 865a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_0){ 866a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_0 = 0; 867a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 868a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0); 869ce263fc8SMatthias Ringwald return; 870ce263fc8SMatthias Ringwald } 871ce263fc8SMatthias Ringwald 872a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_1){ 873a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_1 = 0; 874a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 875a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1); 876ce263fc8SMatthias Ringwald return; 877ce263fc8SMatthias Ringwald } 878ce263fc8SMatthias Ringwald 879a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_2){ 880a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_2 = 0; 881a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 882a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2); 883ce263fc8SMatthias Ringwald return; 884ce263fc8SMatthias Ringwald } 885ce263fc8SMatthias Ringwald 886a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_3){ 887a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_3 = 0; 888a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 889a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3); 890ce263fc8SMatthias Ringwald return; 891ce263fc8SMatthias Ringwald } 892ce263fc8SMatthias Ringwald 893a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_4){ 894a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_4 = 0; 895a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 896a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4); 897ce263fc8SMatthias Ringwald return; 898ce263fc8SMatthias Ringwald } 899ce263fc8SMatthias Ringwald 900a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_x){ 901a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 0; 902a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 903a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index); 904667ec068SMatthias Ringwald return; 905667ec068SMatthias Ringwald } 906667ec068SMatthias Ringwald 907a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_dtmf_code){ 908a0ffb263SMatthias Ringwald char code = hfp_connection->hf_send_dtmf_code; 909a0ffb263SMatthias Ringwald hfp_connection->hf_send_dtmf_code = 0; 910a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 911a0ffb263SMatthias Ringwald hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code); 912ce263fc8SMatthias Ringwald return; 913ce263fc8SMatthias Ringwald } 914ce263fc8SMatthias Ringwald 915a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_binp){ 916a0ffb263SMatthias Ringwald hfp_connection->hf_send_binp = 0; 917a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 918a0ffb263SMatthias Ringwald hfp_hf_send_binp(hfp_connection->rfcomm_cid); 919ce263fc8SMatthias Ringwald return; 920ce263fc8SMatthias Ringwald } 921ce263fc8SMatthias Ringwald 922a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_clcc){ 923a0ffb263SMatthias Ringwald hfp_connection->hf_send_clcc = 0; 924a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 925a0ffb263SMatthias Ringwald hfp_hf_send_clcc(hfp_connection->rfcomm_cid); 926667ec068SMatthias Ringwald return; 927667ec068SMatthias Ringwald } 928667ec068SMatthias Ringwald 929a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_rrh){ 930a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 0; 931667ec068SMatthias Ringwald char buffer[20]; 932a0ffb263SMatthias Ringwald switch (hfp_connection->hf_send_rrh_command){ 933667ec068SMatthias Ringwald case '?': 9341599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s?\r", 935ff7d6aeaSMatthias Ringwald HFP_RESPONSE_AND_HOLD); 936ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 937a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 938667ec068SMatthias Ringwald return; 939667ec068SMatthias Ringwald case '0': 940667ec068SMatthias Ringwald case '1': 941667ec068SMatthias Ringwald case '2': 9421599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s=%c\r", 943ff7d6aeaSMatthias Ringwald HFP_RESPONSE_AND_HOLD, 944ff7d6aeaSMatthias Ringwald hfp_connection->hf_send_rrh_command); 945ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 946a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 947667ec068SMatthias Ringwald return; 948667ec068SMatthias Ringwald default: 949667ec068SMatthias Ringwald break; 950667ec068SMatthias Ringwald } 951667ec068SMatthias Ringwald return; 952667ec068SMatthias Ringwald } 953667ec068SMatthias Ringwald 954a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_cnum){ 955a0ffb263SMatthias Ringwald hfp_connection->hf_send_cnum = 0; 956667ec068SMatthias Ringwald char buffer[20]; 9571599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s\r", 958ff7d6aeaSMatthias Ringwald HFP_SUBSCRIBER_NUMBER_INFORMATION); 959ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 960a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 961667ec068SMatthias Ringwald return; 962667ec068SMatthias Ringwald } 963667ec068SMatthias Ringwald 964667ec068SMatthias Ringwald // update HF indicators 965a0ffb263SMatthias Ringwald if (hfp_connection->generic_status_update_bitmap){ 966667ec068SMatthias Ringwald int i; 967aeb0f0feSMatthias Ringwald for (i=0; i < hfp_hf_indicators_nr; i++){ 968a0ffb263SMatthias Ringwald if (get_bit(hfp_connection->generic_status_update_bitmap, i)){ 969a0ffb263SMatthias Ringwald if (hfp_connection->generic_status_indicators[i].state){ 970a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 971a0ffb263SMatthias Ringwald hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0); 972667ec068SMatthias Ringwald char buffer[30]; 9731599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s=%u,%u\r", 974ff7d6aeaSMatthias Ringwald HFP_TRANSFER_HF_INDICATOR_STATUS, 975aeb0f0feSMatthias Ringwald hfp_hf_indicators[i], 976aeb0f0feSMatthias Ringwald (unsigned int)hfp_hf_indicators_value[i]); 977ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 978a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 979667ec068SMatthias Ringwald } else { 980aeb0f0feSMatthias Ringwald log_info("Not sending HF indicator %u as it is disabled", hfp_hf_indicators[i]); 981667ec068SMatthias Ringwald } 982667ec068SMatthias Ringwald return; 983667ec068SMatthias Ringwald } 984667ec068SMatthias Ringwald } 985667ec068SMatthias Ringwald } 986667ec068SMatthias Ringwald 987ce263fc8SMatthias Ringwald if (done) return; 988ce263fc8SMatthias Ringwald // deal with disconnect 989a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 990ce263fc8SMatthias Ringwald case HFP_W2_DISCONNECT_RFCOMM: 991a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 992a0ffb263SMatthias Ringwald rfcomm_disconnect(hfp_connection->rfcomm_cid); 993ce263fc8SMatthias Ringwald break; 994ce263fc8SMatthias Ringwald 995ce263fc8SMatthias Ringwald default: 996ce263fc8SMatthias Ringwald break; 997ce263fc8SMatthias Ringwald } 998ce263fc8SMatthias Ringwald } 999ce263fc8SMatthias Ringwald 1000a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){ 1001a0ffb263SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 10026a7f44bdSMilanka Ringwald 1003ca59be51SMatthias Ringwald hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr); 10047522e673SMatthias Ringwald 1005184a03edSMilanka Ringwald uint8_t i; 1006184a03edSMilanka Ringwald for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 1007184a03edSMilanka Ringwald hfp_connection->ag_indicators[i].status_changed = 0; 1008184a03edSMilanka Ringwald hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]); 1009184a03edSMilanka Ringwald } 1010667ec068SMatthias Ringwald // restore volume settings 1011a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = hfp_hf_speaker_gain; 1012a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 1013ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain); 1014a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = hfp_hf_microphone_gain; 1015a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 1016ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain); 1017667ec068SMatthias Ringwald // enable all indicators 1018aeb0f0feSMatthias Ringwald for (i=0; i < hfp_hf_indicators_nr; i++){ 1019aeb0f0feSMatthias Ringwald hfp_connection->generic_status_indicators[i].uuid = hfp_hf_indicators[i]; 1020a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[i].state = 1; 1021667ec068SMatthias Ringwald } 1022ce263fc8SMatthias Ringwald } 1023ce263fc8SMatthias Ringwald 10241cc65c4fSMatthias Ringwald static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){ 1025aeb0f0feSMatthias Ringwald if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_hf_codecs_nr, hfp_hf_codecs)){ 10261cc65c4fSMatthias Ringwald // Codec supported, confirm 10271cc65c4fSMatthias Ringwald hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 10281cc65c4fSMatthias Ringwald hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 10291cc65c4fSMatthias Ringwald log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD"); 10301cc65c4fSMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC; 10311cc65c4fSMatthias Ringwald 10321cc65c4fSMatthias Ringwald hfp_connection->hf_send_codec_confirm = true; 10331cc65c4fSMatthias Ringwald } else { 10341cc65c4fSMatthias Ringwald // Codec not supported, send supported codecs 10351cc65c4fSMatthias Ringwald hfp_connection->codec_confirmed = 0; 10361cc65c4fSMatthias Ringwald hfp_connection->suggested_codec = 0; 10371cc65c4fSMatthias Ringwald hfp_connection->negotiated_codec = 0; 10381cc65c4fSMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 10391cc65c4fSMatthias Ringwald 10401cc65c4fSMatthias Ringwald hfp_connection->hf_send_supported_codecs = true; 10411cc65c4fSMatthias Ringwald } 10421cc65c4fSMatthias Ringwald } 10431cc65c4fSMatthias Ringwald 1044e2c3b58dSMilanka Ringwald static bool hfp_hf_switch_on_ok_pending(hfp_connection_t *hfp_connection, uint8_t status){ 1045e2c3b58dSMilanka Ringwald bool event_emited = true; 1046e2c3b58dSMilanka Ringwald 104799af1e28SMilanka Ringwald switch (hfp_connection->response_pending_for_command){ 1048e2c3b58dSMilanka Ringwald case HFP_CMD_TURN_OFF_EC_AND_NR: 1049e2c3b58dSMilanka Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE, status); 1050e2c3b58dSMilanka Ringwald break; 1051e2c3b58dSMilanka Ringwald default: 1052e2c3b58dSMilanka Ringwald event_emited = false; 1053e2c3b58dSMilanka Ringwald 1054a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 10553deb3ec6SMatthias Ringwald case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 1056a0ffb263SMatthias Ringwald if (has_codec_negotiation_feature(hfp_connection)){ 1057a0ffb263SMatthias Ringwald hfp_connection->state = HFP_NOTIFY_ON_CODECS; 10583deb3ec6SMatthias Ringwald break; 10593deb3ec6SMatthias Ringwald } 1060a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 10613deb3ec6SMatthias Ringwald break; 10623deb3ec6SMatthias Ringwald 10633deb3ec6SMatthias Ringwald case HFP_W4_NOTIFY_ON_CODECS: 1064a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 10653deb3ec6SMatthias Ringwald break; 10663deb3ec6SMatthias Ringwald 10673deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS: 1068a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS; 10693deb3ec6SMatthias Ringwald break; 10703deb3ec6SMatthias Ringwald 10713deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS_STATUS: 1072a0ffb263SMatthias Ringwald hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE; 10733deb3ec6SMatthias Ringwald break; 10743deb3ec6SMatthias Ringwald 10753deb3ec6SMatthias Ringwald case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE: 1076a0ffb263SMatthias Ringwald if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 1077a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL; 10783deb3ec6SMatthias Ringwald break; 10793deb3ec6SMatthias Ringwald } 1080a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 1081a0ffb263SMatthias Ringwald hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 10823deb3ec6SMatthias Ringwald break; 10833deb3ec6SMatthias Ringwald } 1084a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 10853deb3ec6SMatthias Ringwald break; 10863deb3ec6SMatthias Ringwald 10873deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_CAN_HOLD_CALL: 1088a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 1089a0ffb263SMatthias Ringwald hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 10903deb3ec6SMatthias Ringwald break; 10913deb3ec6SMatthias Ringwald } 1092a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 10933deb3ec6SMatthias Ringwald break; 10943deb3ec6SMatthias Ringwald 10953deb3ec6SMatthias Ringwald case HFP_W4_LIST_GENERIC_STATUS_INDICATORS: 1096a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS; 10973deb3ec6SMatthias Ringwald break; 10983deb3ec6SMatthias Ringwald 10993deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS: 1100a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 11013deb3ec6SMatthias Ringwald break; 11023deb3ec6SMatthias Ringwald 11033deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 1104a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 11053deb3ec6SMatthias Ringwald break; 1106ce263fc8SMatthias Ringwald case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 1107a0ffb263SMatthias Ringwald if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 1108a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 1109ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 1110ce263fc8SMatthias Ringwald break; 1111ce263fc8SMatthias Ringwald } 11123deb3ec6SMatthias Ringwald 1113a0ffb263SMatthias Ringwald if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){ 1114a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 0; 1115ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 1116ce263fc8SMatthias Ringwald break; 11173deb3ec6SMatthias Ringwald } 11183deb3ec6SMatthias Ringwald 1119a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 1120ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK: 1121a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1122ce263fc8SMatthias Ringwald break; 1123ce263fc8SMatthias Ringwald case HPF_HF_QUERY_OPERATOR_W4_RESULT: 1124a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET; 1125a473a009SMatthias Ringwald hfp_emit_network_operator_event(hfp_connection); 1126ce263fc8SMatthias Ringwald break; 1127ce263fc8SMatthias Ringwald default: 1128ce263fc8SMatthias Ringwald break; 11293deb3ec6SMatthias Ringwald } 1130ce263fc8SMatthias Ringwald 1131a0ffb263SMatthias Ringwald if (hfp_connection->enable_extended_audio_gateway_error_report){ 1132a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = 0; 1133ce263fc8SMatthias Ringwald break; 11343deb3ec6SMatthias Ringwald } 11353deb3ec6SMatthias Ringwald 1136a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 1137aa4dd815SMatthias Ringwald case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 1138a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 11393deb3ec6SMatthias Ringwald break; 1140ce263fc8SMatthias Ringwald case HFP_CODECS_HF_CONFIRMED_CODEC: 1141a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 1142ce263fc8SMatthias Ringwald break; 11433deb3ec6SMatthias Ringwald default: 11443deb3ec6SMatthias Ringwald break; 11453deb3ec6SMatthias Ringwald } 1146af97579eSMilanka Ringwald hfp_hf_voice_recognition_state_machine(hfp_connection); 1147be55a11dSMilanka Ringwald break; 1148be55a11dSMilanka Ringwald case HFP_AUDIO_CONNECTION_ESTABLISHED: 1149af97579eSMilanka Ringwald hfp_hf_voice_recognition_state_machine(hfp_connection); 11503deb3ec6SMatthias Ringwald break; 11513deb3ec6SMatthias Ringwald default: 11523deb3ec6SMatthias Ringwald break; 11533deb3ec6SMatthias Ringwald } 1154e2c3b58dSMilanka Ringwald break; 1155e2c3b58dSMilanka Ringwald } 11563deb3ec6SMatthias Ringwald 11573deb3ec6SMatthias Ringwald // done 115899af1e28SMilanka Ringwald hfp_connection->response_pending_for_command = HFP_CMD_NONE; 1159be55a11dSMilanka Ringwald hfp_connection->ok_pending = 0; 1160a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1161e2c3b58dSMilanka Ringwald return event_emited; 11623deb3ec6SMatthias Ringwald } 11633deb3ec6SMatthias Ringwald 1164a03dbc20SMilanka Ringwald static bool hfp_is_ringing(hfp_callsetup_status_t callsetup_status){ 1165a03dbc20SMilanka Ringwald switch (callsetup_status){ 1166a03dbc20SMilanka Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1167a03dbc20SMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1168a03dbc20SMilanka Ringwald return true; 1169a03dbc20SMilanka Ringwald default: 1170a03dbc20SMilanka Ringwald return false; 1171a03dbc20SMilanka Ringwald } 1172a03dbc20SMilanka Ringwald } 1173be55a11dSMilanka Ringwald 1174b08371a9SMilanka Ringwald static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) { 11754562e2a2SMatthias Ringwald uint16_t i; 1176a03dbc20SMilanka Ringwald 11774562e2a2SMatthias Ringwald for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 11784562e2a2SMatthias Ringwald if (hfp_connection->ag_indicators[i].status_changed) { 11794562e2a2SMatthias Ringwald if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){ 1180a03dbc20SMilanka Ringwald hfp_callsetup_status_t new_hf_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status; 1181a03dbc20SMilanka Ringwald bool ringing_old = hfp_is_ringing(hfp_hf_callsetup_status); 1182a03dbc20SMilanka Ringwald bool ringing_new = hfp_is_ringing(new_hf_callsetup_status); 1183a03dbc20SMilanka Ringwald if (ringing_old != ringing_new){ 1184a03dbc20SMilanka Ringwald if (ringing_new){ 1185a03dbc20SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGING); 1186a03dbc20SMilanka Ringwald } else { 1187a03dbc20SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGING); 1188a03dbc20SMilanka Ringwald } 1189a03dbc20SMilanka Ringwald } 1190a03dbc20SMilanka Ringwald hfp_hf_callsetup_status = new_hf_callsetup_status; 11914562e2a2SMatthias Ringwald } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){ 1192aeb0f0feSMatthias Ringwald hfp_hf_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status; 11934562e2a2SMatthias Ringwald // avoid set but not used warning 1194aeb0f0feSMatthias Ringwald (void) hfp_hf_callheld_status; 11954562e2a2SMatthias Ringwald } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){ 1196674ebed5SMilanka Ringwald hfp_call_status_t new_hf_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status; 1197674ebed5SMilanka Ringwald if (hfp_hf_call_status != new_hf_call_status){ 1198674ebed5SMilanka Ringwald if (new_hf_call_status == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS){ 1199674ebed5SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED); 1200674ebed5SMilanka Ringwald } else { 1201674ebed5SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED); 1202674ebed5SMilanka Ringwald } 1203674ebed5SMilanka Ringwald } 1204674ebed5SMilanka Ringwald hfp_hf_call_status = new_hf_call_status; 12054562e2a2SMatthias Ringwald } 12064562e2a2SMatthias Ringwald hfp_connection->ag_indicators[i].status_changed = 0; 1207a473a009SMatthias Ringwald hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]); 12084562e2a2SMatthias Ringwald break; 12094562e2a2SMatthias Ringwald } 12104562e2a2SMatthias Ringwald } 12114562e2a2SMatthias Ringwald } 12124562e2a2SMatthias Ringwald 1213426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){ 1214186dd3d2SMatthias Ringwald int value; 1215186dd3d2SMatthias Ringwald int i; 1216e2c3b58dSMilanka Ringwald bool event_emited; 1217e2c3b58dSMilanka Ringwald 1218a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1219667ec068SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1220a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1221a473a009SMatthias Ringwald hfp_hf_emit_subscriber_information(hfp_connection, 0); 1222667ec068SMatthias Ringwald break; 1223667ec068SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_STATUS: 1224a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1225ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0])); 1226667ec068SMatthias Ringwald break; 1227667ec068SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 1228a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1229a473a009SMatthias Ringwald hfp_hf_emit_enhanced_call_status(hfp_connection); 1230667ec068SMatthias Ringwald break; 1231ce263fc8SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 1232a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 12332308e108SMilanka Ringwald value = btstack_atoi((char*)hfp_connection->line_buffer); 1234667ec068SMatthias Ringwald hfp_hf_speaker_gain = value; 1235ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value); 1236ce263fc8SMatthias Ringwald break; 1237ce263fc8SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 1238a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 12392308e108SMilanka Ringwald value = btstack_atoi((char*)hfp_connection->line_buffer); 1240667ec068SMatthias Ringwald hfp_hf_microphone_gain = value; 1241ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value); 1242ce263fc8SMatthias Ringwald break; 1243ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 1244a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1245ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number); 1246a0ffb263SMatthias Ringwald break; 1247a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1248a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1249a473a009SMatthias Ringwald hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION); 1250a0ffb263SMatthias Ringwald break; 1251a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1252a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1253a473a009SMatthias Ringwald hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION); 1254ce263fc8SMatthias Ringwald break; 1255ce263fc8SMatthias Ringwald case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1256a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 12575a4785c8SMatthias Ringwald hfp_connection->ok_pending = 0; 1258a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 1259ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value); 1260ce263fc8SMatthias Ringwald break; 12610b4debbfSMilanka Ringwald case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION: 12620b4debbfSMilanka Ringwald break; 1263fdda66c0SMilanka Ringwald case HFP_CMD_ERROR: 126490244c92SMilanka Ringwald switch (hfp_connection->state){ 126590244c92SMilanka Ringwald case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 126690244c92SMilanka Ringwald switch (hfp_connection->codecs_state){ 126790244c92SMilanka Ringwald case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 1268fdda66c0SMilanka Ringwald hfp_reset_context_flags(hfp_connection); 126990244c92SMilanka Ringwald hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec); 127090244c92SMilanka Ringwald return; 127190244c92SMilanka Ringwald default: 127290244c92SMilanka Ringwald break; 127390244c92SMilanka Ringwald } 127456f1adacSMilanka Ringwald break; 127556f1adacSMilanka Ringwald default: 127656f1adacSMilanka Ringwald break; 127756f1adacSMilanka Ringwald } 1278e2c3b58dSMilanka Ringwald 1279fdda66c0SMilanka Ringwald // handle error response for voice activation (HF initiated) 12800b4debbfSMilanka Ringwald switch(hfp_connection->vra_state_requested){ 1281de9e0ea7SMilanka Ringwald case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1282de9e0ea7SMilanka Ringwald hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 1283be55a11dSMilanka Ringwald break; 1284be55a11dSMilanka Ringwald default: 1285e2c3b58dSMilanka Ringwald if (hfp_connection->vra_state_requested == hfp_connection->vra_state){ 1286e2c3b58dSMilanka Ringwald break; 1287e2c3b58dSMilanka Ringwald } 12880b4debbfSMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 1289553a4a56SMilanka Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 12900b4debbfSMilanka Ringwald hfp_reset_context_flags(hfp_connection); 12910b4debbfSMilanka Ringwald return; 1292be55a11dSMilanka Ringwald } 1293e2c3b58dSMilanka Ringwald event_emited = hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 1294e2c3b58dSMilanka Ringwald if (!event_emited){ 12950b4debbfSMilanka Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1); 1296e2c3b58dSMilanka Ringwald } 1297fdda66c0SMilanka Ringwald hfp_reset_context_flags(hfp_connection); 1298ce263fc8SMatthias Ringwald break; 1299fdda66c0SMilanka Ringwald 1300ce263fc8SMatthias Ringwald case HFP_CMD_OK: 1301e2c3b58dSMilanka Ringwald hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_SUCCESS); 1302ce263fc8SMatthias Ringwald break; 1303ce263fc8SMatthias Ringwald case HFP_CMD_RING: 13045a4785c8SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1305ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING); 1306ce263fc8SMatthias Ringwald break; 1307ce263fc8SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 13085a4785c8SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 13094562e2a2SMatthias Ringwald hfp_hf_handle_transfer_ag_indicator_status(hfp_connection); 1310ce263fc8SMatthias Ringwald break; 1311c741b032SMilanka Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 13125a4785c8SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1313184a03edSMilanka Ringwald // report status after SLC established 1314184a03edSMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){ 1315184a03edSMilanka Ringwald break; 1316184a03edSMilanka Ringwald } 1317c741b032SMilanka Ringwald for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 1318a473a009SMatthias Ringwald hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]); 1319c741b032SMilanka Ringwald } 1320c741b032SMilanka Ringwald break; 13211cc65c4fSMatthias Ringwald case HFP_CMD_AG_SUGGESTED_CODEC: 13221cc65c4fSMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 13235a4785c8SMatthias Ringwald hfp_hf_handle_suggested_codec(hfp_connection); 13241cc65c4fSMatthias Ringwald break; 1325eac56539SMilanka Ringwald case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING: 1326eac56539SMilanka 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)); 1327ce263fc8SMatthias Ringwald default: 1328ce263fc8SMatthias Ringwald break; 13293deb3ec6SMatthias Ringwald } 13300cef86faSMatthias Ringwald } 1331426f9988SMatthias Ringwald 133276cc1527SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){ 133376cc1527SMatthias Ringwald return (byte == '\n') || (byte == '\r'); 133476cc1527SMatthias Ringwald } 133576cc1527SMatthias Ringwald 13360b4debbfSMilanka Ringwald static void hfp_hf_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){ 1337426f9988SMatthias Ringwald // assertion: size >= 1 as rfcomm.c does not deliver empty packets 1338426f9988SMatthias Ringwald if (size < 1) return; 1339426f9988SMatthias Ringwald 1340426f9988SMatthias Ringwald hfp_log_rfcomm_message("HFP_HF_RX", packet, size); 1341e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES 1342e43d1938SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet); 1343e43d1938SMatthias Ringwald #endif 1344426f9988SMatthias Ringwald 1345426f9988SMatthias Ringwald // process messages byte-wise 1346a7ba78b0SMilanka Ringwald uint8_t pos; 1347426f9988SMatthias Ringwald for (pos = 0; pos < size; pos++){ 1348426f9988SMatthias Ringwald hfp_parse(hfp_connection, packet[pos], 1); 13491599fe57SMatthias Ringwald // parse until end of line "\r" or "\n" 1350426f9988SMatthias Ringwald if (!hfp_parser_is_end_of_line(packet[pos])) continue; 13510b4debbfSMilanka Ringwald hfp_hf_handle_rfcomm_command(hfp_connection); 13523deb3ec6SMatthias Ringwald } 1353a7ba78b0SMilanka Ringwald } 13543deb3ec6SMatthias Ringwald 13551c6a0fc0SMatthias Ringwald static void hfp_hf_run(void){ 1356665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1357665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1358665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1359a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 136022387625SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_HF) continue; 13611c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 13623deb3ec6SMatthias Ringwald } 13633deb3ec6SMatthias Ringwald } 13643deb3ec6SMatthias Ringwald 13651c6a0fc0SMatthias Ringwald static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 13660b4debbfSMilanka Ringwald hfp_connection_t * hfp_connection; 13673deb3ec6SMatthias Ringwald switch (packet_type){ 13683deb3ec6SMatthias Ringwald case RFCOMM_DATA_PACKET: 13690b4debbfSMilanka Ringwald hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 13700b4debbfSMilanka Ringwald if (!hfp_connection) return; 13710b4debbfSMilanka Ringwald hfp_hf_handle_rfcomm_data(hfp_connection, packet, size); 13720b4debbfSMilanka Ringwald hfp_hf_run_for_context(hfp_connection); 13730b4debbfSMilanka Ringwald return; 13743deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 1375d4dd47ffSMatthias Ringwald if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 1376d4dd47ffSMatthias Ringwald uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 13771c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid)); 1378d4dd47ffSMatthias Ringwald return; 1379d4dd47ffSMatthias Ringwald } 138027950165SMatthias Ringwald hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF); 1381202c8a4cSMatthias Ringwald break; 13823deb3ec6SMatthias Ringwald default: 13833deb3ec6SMatthias Ringwald break; 13843deb3ec6SMatthias Ringwald } 13851c6a0fc0SMatthias Ringwald hfp_hf_run(); 13863deb3ec6SMatthias Ringwald } 13873deb3ec6SMatthias Ringwald 13881c6a0fc0SMatthias Ringwald static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1389405014fbSMatthias Ringwald hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF); 13901c6a0fc0SMatthias Ringwald hfp_hf_run(); 1391405014fbSMatthias Ringwald } 1392405014fbSMatthias Ringwald 1393aeb0f0feSMatthias Ringwald static void hfp_hf_set_defaults(void){ 1394aeb0f0feSMatthias Ringwald hfp_hf_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 1395aeb0f0feSMatthias Ringwald hfp_hf_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS; 1396aeb0f0feSMatthias Ringwald hfp_hf_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1397aeb0f0feSMatthias Ringwald hfp_hf_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD; 1398aeb0f0feSMatthias Ringwald hfp_hf_codecs_nr = 0; 1399aeb0f0feSMatthias Ringwald hfp_hf_speaker_gain = 9; 1400aeb0f0feSMatthias Ringwald hfp_hf_microphone_gain = 9; 1401aeb0f0feSMatthias Ringwald hfp_hf_indicators_nr = 0; 1402aeb0f0feSMatthias Ringwald } 1403aeb0f0feSMatthias Ringwald 1404b4df8028SMilanka Ringwald uint8_t hfp_hf_init(uint16_t rfcomm_channel_nr){ 1405b4df8028SMilanka Ringwald uint8_t status = rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 1406b4df8028SMilanka Ringwald if (status != ERROR_CODE_SUCCESS){ 1407b4df8028SMilanka Ringwald return status; 1408b4df8028SMilanka Ringwald } 1409b4df8028SMilanka Ringwald 1410520c92d5SMatthias Ringwald hfp_init(); 1411aeb0f0feSMatthias Ringwald hfp_hf_set_defaults(); 1412d63c37a1SMatthias Ringwald 14131c6a0fc0SMatthias Ringwald hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler; 14141c6a0fc0SMatthias Ringwald hci_add_event_handler(&hfp_hf_hci_event_callback_registration); 141527950165SMatthias Ringwald 141627950165SMatthias Ringwald // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us 14171c6a0fc0SMatthias Ringwald hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler); 1418b4df8028SMilanka Ringwald return ERROR_CODE_SUCCESS; 141920b2edb6SMatthias Ringwald } 142027950165SMatthias Ringwald 142120b2edb6SMatthias Ringwald void hfp_hf_deinit(void){ 142220b2edb6SMatthias Ringwald hfp_deinit(); 1423aeb0f0feSMatthias Ringwald hfp_hf_set_defaults(); 1424aeb0f0feSMatthias Ringwald 1425aeb0f0feSMatthias Ringwald hfp_hf_callback = NULL; 142620b2edb6SMatthias Ringwald (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t)); 1427aeb0f0feSMatthias Ringwald (void) memset(hfp_hf_phone_number, 0, sizeof(hfp_hf_phone_number)); 1428a0ffb263SMatthias Ringwald } 1429a0ffb263SMatthias Ringwald 14307ca89cabSMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, const uint8_t * codecs){ 143168466199SMilanka Ringwald btstack_assert(codecs_nr < HFP_MAX_NUM_CODECS); 14323deb3ec6SMatthias Ringwald 1433aeb0f0feSMatthias Ringwald hfp_hf_codecs_nr = codecs_nr; 14343deb3ec6SMatthias Ringwald int i; 14353deb3ec6SMatthias Ringwald for (i=0; i<codecs_nr; i++){ 1436aeb0f0feSMatthias Ringwald hfp_hf_codecs[i] = codecs[i]; 14373deb3ec6SMatthias Ringwald } 14383deb3ec6SMatthias Ringwald } 14393deb3ec6SMatthias Ringwald 1440a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){ 1441aeb0f0feSMatthias Ringwald hfp_hf_supported_features = supported_features; 1442a0ffb263SMatthias Ringwald } 14433deb3ec6SMatthias Ringwald 14447ca89cabSMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){ 1445aeb0f0feSMatthias Ringwald btstack_assert(hfp_hf_indicators_nr < HFP_MAX_NUM_INDICATORS); 144668466199SMilanka Ringwald 1447aeb0f0feSMatthias Ringwald hfp_hf_indicators_nr = indicators_nr; 14483deb3ec6SMatthias Ringwald int i; 1449aeb0f0feSMatthias Ringwald for (i = 0; i < hfp_hf_indicators_nr ; i++){ 1450aeb0f0feSMatthias Ringwald hfp_hf_indicators[i] = indicators[i]; 14513deb3ec6SMatthias Ringwald } 14523deb3ec6SMatthias Ringwald } 14533deb3ec6SMatthias Ringwald 14544eb3f1d8SMilanka Ringwald uint8_t hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){ 14554eb3f1d8SMilanka Ringwald return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF); 14563deb3ec6SMatthias Ringwald } 14573deb3ec6SMatthias Ringwald 1458657bc59fSMilanka Ringwald uint8_t hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){ 14599c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1460a33eb0c4SMilanka Ringwald if (!hfp_connection){ 1461657bc59fSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1462a33eb0c4SMilanka Ringwald } 14631ffa0dd9SMilanka Ringwald hfp_trigger_release_service_level_connection(hfp_connection); 14641c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1465657bc59fSMilanka Ringwald return ERROR_CODE_SUCCESS; 14663deb3ec6SMatthias Ringwald } 14673deb3ec6SMatthias Ringwald 14683c65e705SMilanka Ringwald static uint8_t hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){ 14699c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1470a0ffb263SMatthias Ringwald if (!hfp_connection) { 14713c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 14723deb3ec6SMatthias Ringwald } 1473a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = enable; 14741c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 14753c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 14763deb3ec6SMatthias Ringwald } 14773deb3ec6SMatthias Ringwald 14783c65e705SMilanka Ringwald uint8_t hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 14793c65e705SMilanka Ringwald return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1); 1480ce263fc8SMatthias Ringwald } 1481ce263fc8SMatthias Ringwald 14823c65e705SMilanka Ringwald uint8_t hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 14833c65e705SMilanka Ringwald return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0); 1484ce263fc8SMatthias Ringwald } 1485ce263fc8SMatthias Ringwald 14863deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format 14873c65e705SMilanka Ringwald uint8_t hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){ 14889c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1489a0ffb263SMatthias Ringwald if (!hfp_connection) { 14903c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 14913deb3ec6SMatthias Ringwald } 1492a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 1; 1493a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap; 14941c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 14953c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 14963deb3ec6SMatthias Ringwald } 14973deb3ec6SMatthias Ringwald 14983c65e705SMilanka Ringwald uint8_t hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){ 14999c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1500a0ffb263SMatthias Ringwald if (!hfp_connection) { 15013c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 15023deb3ec6SMatthias Ringwald } 15033c65e705SMilanka Ringwald 1504a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 1505ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET: 1506a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT; 1507ce263fc8SMatthias Ringwald break; 1508ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_FORMAT_SET: 1509a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1510ce263fc8SMatthias Ringwald break; 1511ce263fc8SMatthias Ringwald default: 15123c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1513ce263fc8SMatthias Ringwald } 15141c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 15153c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 15163deb3ec6SMatthias Ringwald } 15173deb3ec6SMatthias Ringwald 15183c65e705SMilanka Ringwald static uint8_t hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){ 15199c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1520a0ffb263SMatthias Ringwald if (!hfp_connection) { 15213c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 15223deb3ec6SMatthias Ringwald } 1523a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = enable; 15241c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 15253c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 15263deb3ec6SMatthias Ringwald } 15273deb3ec6SMatthias Ringwald 1528ce263fc8SMatthias Ringwald 15293c65e705SMilanka Ringwald uint8_t hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 15303c65e705SMilanka Ringwald return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1); 1531ce263fc8SMatthias Ringwald } 1532ce263fc8SMatthias Ringwald 15333c65e705SMilanka Ringwald uint8_t hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 15343c65e705SMilanka Ringwald return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0); 1535ce263fc8SMatthias Ringwald } 1536ce263fc8SMatthias Ringwald 153738200c1dSMilanka Ringwald static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){ 1538aeb0f0feSMatthias Ringwald return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) && (hfp_hf_supported_features & (1 << HFP_HFSF_ESCO_S4)); 153938200c1dSMilanka Ringwald } 1540ce263fc8SMatthias Ringwald 15413c65e705SMilanka Ringwald uint8_t hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){ 15429c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1543a33eb0c4SMilanka Ringwald if (!hfp_connection) { 15443c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1545a33eb0c4SMilanka Ringwald } 1546ce263fc8SMatthias Ringwald 15473c65e705SMilanka Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 15483c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 15493c65e705SMilanka Ringwald } 15503c65e705SMilanka Ringwald 15513c65e705SMilanka Ringwald if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){ 15523c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 15533c65e705SMilanka Ringwald } 1554f4412093SMatthias Ringwald if (has_codec_negotiation_feature(hfp_connection)) { 1555a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state) { 1556aa4dd815SMatthias Ringwald case HFP_CODECS_W4_AG_COMMON_CODEC: 1557aa4dd815SMatthias Ringwald break; 1558ec3bfc1aSMatthias Ringwald case HFP_CODECS_EXCHANGED: 1559ec3bfc1aSMatthias Ringwald hfp_connection->trigger_codec_exchange = 1; 1560ec3bfc1aSMatthias Ringwald break; 1561aa4dd815SMatthias Ringwald default: 15621cc65c4fSMatthias Ringwald hfp_connection->codec_confirmed = 0; 15631cc65c4fSMatthias Ringwald hfp_connection->suggested_codec = 0; 15641cc65c4fSMatthias Ringwald hfp_connection->negotiated_codec = 0; 15651cc65c4fSMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 156638200c1dSMilanka Ringwald hfp_connection->trigger_codec_exchange = 1; 1567aa4dd815SMatthias Ringwald break; 15683deb3ec6SMatthias Ringwald } 1569f4412093SMatthias Ringwald } else { 1570f4412093SMatthias Ringwald log_info("no codec negotiation feature, use CVSD"); 1571f4412093SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 1572f4412093SMatthias Ringwald hfp_connection->suggested_codec = HFP_CODEC_CVSD; 1573f4412093SMatthias Ringwald hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 1574f4412093SMatthias Ringwald hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 1575f4412093SMatthias Ringwald hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection)); 1576f4412093SMatthias Ringwald hfp_connection->establish_audio_connection = 1; 1577f4412093SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 1578ce263fc8SMatthias Ringwald } 1579ce263fc8SMatthias Ringwald 15801c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 15813c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 15823deb3ec6SMatthias Ringwald } 15833deb3ec6SMatthias Ringwald 15843c65e705SMilanka Ringwald uint8_t hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){ 15859c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1586a33eb0c4SMilanka Ringwald if (!hfp_connection) { 15873c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1588a33eb0c4SMilanka Ringwald } 15890b4debbfSMilanka Ringwald if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){ 15900b4debbfSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 15910b4debbfSMilanka Ringwald } 15920b4debbfSMilanka Ringwald uint8_t status = hfp_trigger_release_audio_connection(hfp_connection); 15930b4debbfSMilanka Ringwald if (status == ERROR_CODE_SUCCESS){ 15941c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 15950b4debbfSMilanka Ringwald } 15963c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 15973deb3ec6SMatthias Ringwald } 15983deb3ec6SMatthias Ringwald 15993c65e705SMilanka Ringwald uint8_t hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){ 16009c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1601a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16023c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1603a33eb0c4SMilanka Ringwald } 1604ce263fc8SMatthias Ringwald 1605aeb0f0feSMatthias Ringwald if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1606a0ffb263SMatthias Ringwald hfp_connection->hf_answer_incoming_call = 1; 16071c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1608ce263fc8SMatthias Ringwald } else { 1609aeb0f0feSMatthias Ringwald log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_hf_callsetup_status); 16103c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1611ce263fc8SMatthias Ringwald } 16123c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1613ce263fc8SMatthias Ringwald } 1614ce263fc8SMatthias Ringwald 16153c65e705SMilanka Ringwald uint8_t hfp_hf_terminate_call(hci_con_handle_t acl_handle){ 16169c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1617a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16183c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1619a33eb0c4SMilanka Ringwald } 1620a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 1; 16211c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 16223c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1623ce263fc8SMatthias Ringwald } 1624ce263fc8SMatthias Ringwald 16253c65e705SMilanka Ringwald uint8_t hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){ 16269c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1627a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16283c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1629a33eb0c4SMilanka Ringwald } 1630ce263fc8SMatthias Ringwald 1631aeb0f0feSMatthias Ringwald if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1632a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 1; 16331c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1634ce263fc8SMatthias Ringwald } 16353c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1636ce263fc8SMatthias Ringwald } 1637ce263fc8SMatthias Ringwald 16383c65e705SMilanka Ringwald uint8_t hfp_hf_user_busy(hci_con_handle_t acl_handle){ 16399c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1640a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16413c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1642a33eb0c4SMilanka Ringwald } 1643ce263fc8SMatthias Ringwald 1644aeb0f0feSMatthias Ringwald if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1645a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_0 = 1; 16461c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1647ce263fc8SMatthias Ringwald } 16483c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1649ce263fc8SMatthias Ringwald } 1650ce263fc8SMatthias Ringwald 16513c65e705SMilanka Ringwald uint8_t hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){ 16529c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1653a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16543c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1655a33eb0c4SMilanka Ringwald } 1656ce263fc8SMatthias Ringwald 1657aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1658aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1659a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_1 = 1; 16601c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1661ce263fc8SMatthias Ringwald } 16623c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1663ce263fc8SMatthias Ringwald } 1664ce263fc8SMatthias Ringwald 16653c65e705SMilanka Ringwald uint8_t hfp_hf_swap_calls(hci_con_handle_t acl_handle){ 16669c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1667a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16683c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1669a33eb0c4SMilanka Ringwald } 1670ce263fc8SMatthias Ringwald 1671aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1672aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1673a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_2 = 1; 16741c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1675ce263fc8SMatthias Ringwald } 16763c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1677ce263fc8SMatthias Ringwald } 1678ce263fc8SMatthias Ringwald 16793c65e705SMilanka Ringwald uint8_t hfp_hf_join_held_call(hci_con_handle_t acl_handle){ 16809c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1681a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16823c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1683a33eb0c4SMilanka Ringwald } 1684ce263fc8SMatthias Ringwald 1685aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1686aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1687a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_3 = 1; 16881c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1689ce263fc8SMatthias Ringwald } 16903c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1691ce263fc8SMatthias Ringwald } 1692ce263fc8SMatthias Ringwald 16933c65e705SMilanka Ringwald uint8_t hfp_hf_connect_calls(hci_con_handle_t acl_handle){ 16949c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1695a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16963c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1697a33eb0c4SMilanka Ringwald } 1698ce263fc8SMatthias Ringwald 1699aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1700aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1701a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_4 = 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_release_call_with_index(hci_con_handle_t acl_handle, int index){ 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 } 1712667ec068SMatthias 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_x = 1; 1716a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x_index = 10 + index; 17171c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1718667ec068SMatthias Ringwald } 17193c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1720667ec068SMatthias Ringwald } 1721667ec068SMatthias Ringwald 17223c65e705SMilanka Ringwald uint8_t hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){ 17239c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1724a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17253c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1726a33eb0c4SMilanka Ringwald } 1727667ec068SMatthias Ringwald 1728aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1729aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1730a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 1; 1731a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x_index = 20 + index; 17321c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1733667ec068SMatthias Ringwald } 17343c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1735667ec068SMatthias Ringwald } 1736ce263fc8SMatthias Ringwald 17373c65e705SMilanka Ringwald uint8_t hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){ 17389c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1739a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17403c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1741a33eb0c4SMilanka Ringwald } 1742ce263fc8SMatthias Ringwald 1743a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_outgoing_call = 1; 1744aeb0f0feSMatthias Ringwald snprintf(hfp_hf_phone_number, sizeof(hfp_hf_phone_number), "%s", number); 17451c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 17463c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1747ce263fc8SMatthias Ringwald } 1748ce263fc8SMatthias Ringwald 17493c65e705SMilanka Ringwald uint8_t hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){ 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 1755a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_memory_dialing = 1; 1756a0ffb263SMatthias Ringwald hfp_connection->memory_id = memory_id; 1757a0ffb263SMatthias Ringwald 17581c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 17593c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1760ce263fc8SMatthias Ringwald } 1761ce263fc8SMatthias Ringwald 17623c65e705SMilanka Ringwald uint8_t hfp_hf_redial_last_number(hci_con_handle_t acl_handle){ 17639c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1764a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17653c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1766a33eb0c4SMilanka Ringwald } 1767ce263fc8SMatthias Ringwald 1768a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_redial_last_number = 1; 17691c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 17703c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1771ce263fc8SMatthias Ringwald } 1772ce263fc8SMatthias Ringwald 17733c65e705SMilanka Ringwald uint8_t hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){ 17749c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1775a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17763c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1777a33eb0c4SMilanka Ringwald } 1778ce263fc8SMatthias Ringwald 1779a0ffb263SMatthias Ringwald hfp_connection->hf_activate_call_waiting_notification = 1; 17801c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 17813c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1782ce263fc8SMatthias Ringwald } 1783ce263fc8SMatthias Ringwald 1784ce263fc8SMatthias Ringwald 17853c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){ 17869c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1787a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17883c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1789a33eb0c4SMilanka Ringwald } 1790ce263fc8SMatthias Ringwald 1791a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_call_waiting_notification = 1; 17921c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 17933c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1794ce263fc8SMatthias Ringwald } 1795ce263fc8SMatthias Ringwald 1796ce263fc8SMatthias Ringwald 17973c65e705SMilanka Ringwald uint8_t hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){ 17989c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1799a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18003c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1801a33eb0c4SMilanka Ringwald } 1802ce263fc8SMatthias Ringwald 1803a0ffb263SMatthias Ringwald hfp_connection->hf_activate_calling_line_notification = 1; 18041c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18053c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1806ce263fc8SMatthias Ringwald } 1807ce263fc8SMatthias Ringwald 18083c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){ 18099c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1810a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18113c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1812a33eb0c4SMilanka Ringwald } 1813ce263fc8SMatthias Ringwald 1814a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_calling_line_notification = 1; 18151c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18163c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1817ce263fc8SMatthias Ringwald } 1818ce263fc8SMatthias Ringwald 18196ba83b5eSMilanka Ringwald static bool hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection_t * hfp_connection){ 18206ba83b5eSMilanka Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_EC_NR_FUNCTION); 1821aeb0f0feSMatthias Ringwald int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_EC_NR_FUNCTION); 18226ba83b5eSMilanka Ringwald return hf && ag; 1823ce263fc8SMatthias Ringwald } 1824ce263fc8SMatthias Ringwald 18253c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){ 18269c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1827a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18283c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1829a33eb0c4SMilanka Ringwald } 18306ba83b5eSMilanka Ringwald if (!hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection)){ 18316ba83b5eSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 18326ba83b5eSMilanka Ringwald } 1833ce263fc8SMatthias Ringwald 1834a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1; 18351c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18363c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1837ce263fc8SMatthias Ringwald } 1838ce263fc8SMatthias Ringwald 1839acd11d4aSMilanka Ringwald static bool hfp_hf_vra_flag_supported(hfp_connection_t * hfp_connection){ 1840acd11d4aSMilanka Ringwald int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION); 1841acd11d4aSMilanka Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION); 1842be55a11dSMilanka Ringwald return hf && ag; 1843be55a11dSMilanka Ringwald } 1844be55a11dSMilanka Ringwald 1845acd11d4aSMilanka Ringwald static bool hfp_hf_enhanced_vra_flag_supported(hfp_connection_t * hfp_connection){ 1846acd11d4aSMilanka Ringwald int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS); 1847acd11d4aSMilanka Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS); 1848acd11d4aSMilanka Ringwald return hf && ag; 1849acd11d4aSMilanka Ringwald } 1850acd11d4aSMilanka Ringwald 1851acd11d4aSMilanka Ringwald uint8_t hfp_hf_activate_voice_recognition(hci_con_handle_t acl_handle){ 1852fdda66c0SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1853fdda66c0SMilanka Ringwald if (!hfp_connection) { 1854fdda66c0SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1855be55a11dSMilanka Ringwald } 1856013cc750SMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){ 1857013cc750SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1858013cc750SMilanka Ringwald } 1859acd11d4aSMilanka Ringwald 1860acd11d4aSMilanka Ringwald bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection); 1861acd11d4aSMilanka Ringwald bool legacy_vra_supported = hfp_hf_vra_flag_supported(hfp_connection); 1862acd11d4aSMilanka Ringwald if (!enhanced_vra_supported && !legacy_vra_supported){ 1863acd11d4aSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1864af97579eSMilanka Ringwald } 1865af97579eSMilanka Ringwald 1866498a8121SMilanka Ringwald switch (hfp_connection->vra_state){ 1867be55a11dSMilanka Ringwald case HFP_VRA_VOICE_RECOGNITION_OFF: 1868de9e0ea7SMilanka Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 1869498a8121SMilanka Ringwald hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION; 1870fd4151d1SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED; 1871acd11d4aSMilanka Ringwald hfp_connection->enhanced_voice_recognition_enabled = enhanced_vra_supported; 1872be55a11dSMilanka Ringwald break; 1873de9e0ea7SMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_OFF: 1874de9e0ea7SMilanka Ringwald hfp_connection->activate_voice_recognition = true; 1875de9e0ea7SMilanka Ringwald break; 1876be55a11dSMilanka Ringwald default: 1877be55a11dSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1878be55a11dSMilanka Ringwald } 1879ce263fc8SMatthias Ringwald 1880af97579eSMilanka Ringwald hfp_hf_run_for_context(hfp_connection); 1881fdda66c0SMilanka Ringwald return ERROR_CODE_SUCCESS; 1882af97579eSMilanka Ringwald } 1883af97579eSMilanka Ringwald 1884acd11d4aSMilanka Ringwald uint8_t hfp_hf_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle){ 1885af97579eSMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1886af97579eSMilanka Ringwald if (!hfp_connection) { 1887af97579eSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1888af97579eSMilanka Ringwald } 1889acd11d4aSMilanka Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){ 189008a0b01cSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 189108a0b01cSMilanka Ringwald } 1892acd11d4aSMilanka Ringwald 1893acd11d4aSMilanka Ringwald bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection); 1894acd11d4aSMilanka Ringwald if (!enhanced_vra_supported){ 1895acd11d4aSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1896acd11d4aSMilanka Ringwald } 1897acd11d4aSMilanka Ringwald 1898acd11d4aSMilanka Ringwald switch (hfp_connection->vra_state){ 1899acd11d4aSMilanka Ringwald case HFP_VRA_VOICE_RECOGNITION_ACTIVATED: 1900acd11d4aSMilanka Ringwald case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1901acd11d4aSMilanka Ringwald hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION; 1902acd11d4aSMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 1903acd11d4aSMilanka Ringwald break; 1904acd11d4aSMilanka Ringwald default: 1905fdda66c0SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1906af97579eSMilanka Ringwald } 1907013cc750SMilanka Ringwald 1908acd11d4aSMilanka Ringwald hfp_hf_run_for_context(hfp_connection); 1909acd11d4aSMilanka Ringwald return ERROR_CODE_SUCCESS; 1910acd11d4aSMilanka Ringwald } 1911acd11d4aSMilanka Ringwald 1912acd11d4aSMilanka Ringwald 1913acd11d4aSMilanka Ringwald uint8_t hfp_hf_deactivate_voice_recognition(hci_con_handle_t acl_handle){ 1914acd11d4aSMilanka Ringwald // return deactivate_voice_recognition(acl_handle, false); 1915acd11d4aSMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1916acd11d4aSMilanka Ringwald if (!hfp_connection) { 1917acd11d4aSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1918acd11d4aSMilanka Ringwald } 1919acd11d4aSMilanka Ringwald 1920acd11d4aSMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || 1921acd11d4aSMilanka Ringwald hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){ 1922acd11d4aSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1923acd11d4aSMilanka Ringwald } 1924acd11d4aSMilanka Ringwald 1925acd11d4aSMilanka Ringwald bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection); 1926acd11d4aSMilanka Ringwald bool legacy_vra_supported = hfp_hf_vra_flag_supported(hfp_connection); 1927acd11d4aSMilanka Ringwald if (!enhanced_vra_supported && !legacy_vra_supported){ 1928acd11d4aSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1929acd11d4aSMilanka Ringwald } 1930acd11d4aSMilanka Ringwald 1931fdda66c0SMilanka Ringwald switch (hfp_connection->vra_state){ 1932de9e0ea7SMilanka Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED: 1933fdda66c0SMilanka Ringwald case HFP_VRA_VOICE_RECOGNITION_ACTIVATED: 1934de9e0ea7SMilanka Ringwald case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1935de9e0ea7SMilanka Ringwald case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1936fdda66c0SMilanka Ringwald hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION; 1937fdda66c0SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF; 1938fdda66c0SMilanka Ringwald break; 1939de9e0ea7SMilanka Ringwald 1940de9e0ea7SMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED: 1941de9e0ea7SMilanka Ringwald case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1942de9e0ea7SMilanka Ringwald hfp_connection->deactivate_voice_recognition = true; 1943de9e0ea7SMilanka Ringwald break; 1944de9e0ea7SMilanka Ringwald 1945de9e0ea7SMilanka Ringwald case HFP_VRA_VOICE_RECOGNITION_OFF: 1946de9e0ea7SMilanka Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 1947de9e0ea7SMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_OFF: 1948fdda66c0SMilanka Ringwald default: 1949fdda66c0SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1950fdda66c0SMilanka Ringwald } 1951fdda66c0SMilanka Ringwald 1952fdda66c0SMilanka Ringwald hfp_hf_run_for_context(hfp_connection); 1953fdda66c0SMilanka Ringwald return ERROR_CODE_SUCCESS; 1954af97579eSMilanka Ringwald } 1955af97579eSMilanka Ringwald 19563c65e705SMilanka Ringwald uint8_t hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 19579c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1958a33eb0c4SMilanka Ringwald if (!hfp_connection) { 19593c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1960a33eb0c4SMilanka Ringwald } 1961c8626498SMilanka Ringwald 19623c65e705SMilanka Ringwald if (hfp_connection->microphone_gain == gain) { 19633c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 19643c65e705SMilanka Ringwald } 19653c65e705SMilanka Ringwald 1966c1ab6cc1SMatthias Ringwald if ((gain < 0) || (gain > 15)){ 1967a0ffb263SMatthias Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 19683c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1969a0ffb263SMatthias Ringwald } 19703c65e705SMilanka Ringwald 1971a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = gain; 1972a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 19731c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 19743c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1975ce263fc8SMatthias Ringwald } 1976ce263fc8SMatthias Ringwald 19773c65e705SMilanka Ringwald uint8_t hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 19789c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1979a33eb0c4SMilanka Ringwald if (!hfp_connection) { 19803c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1981a33eb0c4SMilanka Ringwald } 1982c8626498SMilanka Ringwald 19833c65e705SMilanka Ringwald if (hfp_connection->speaker_gain == gain){ 19843c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 19853c65e705SMilanka Ringwald } 19863c65e705SMilanka Ringwald 1987c1ab6cc1SMatthias Ringwald if ((gain < 0) || (gain > 15)){ 1988a0ffb263SMatthias Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 19893c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1990a0ffb263SMatthias Ringwald } 19913c65e705SMilanka Ringwald 1992a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = gain; 1993a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 19941c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 19953c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1996ce263fc8SMatthias Ringwald } 1997ce263fc8SMatthias Ringwald 19983c65e705SMilanka Ringwald uint8_t hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){ 19999c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2000a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20013c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2002a33eb0c4SMilanka Ringwald } 2003a0ffb263SMatthias Ringwald hfp_connection->hf_send_dtmf_code = code; 20041c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20053c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2006ce263fc8SMatthias Ringwald } 2007ce263fc8SMatthias Ringwald 20083c65e705SMilanka Ringwald uint8_t hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 20099c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2010a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20113c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2012a33eb0c4SMilanka Ringwald } 2013a0ffb263SMatthias Ringwald hfp_connection->hf_send_binp = 1; 20141c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20153c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2016ce263fc8SMatthias Ringwald } 20173deb3ec6SMatthias Ringwald 20183c65e705SMilanka Ringwald uint8_t hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){ 20199c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2020a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20213c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2022a33eb0c4SMilanka Ringwald } 2023a0ffb263SMatthias Ringwald hfp_connection->hf_send_clcc = 1; 20241c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20253c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2026667ec068SMatthias Ringwald } 2027667ec068SMatthias Ringwald 2028667ec068SMatthias Ringwald 20293c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){ 20309c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2031a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20323c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2033a33eb0c4SMilanka Ringwald } 2034a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 2035a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '?'; 20361c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20373c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2038667ec068SMatthias Ringwald } 2039667ec068SMatthias Ringwald 20403c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){ 20419c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2042a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20433c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2044a33eb0c4SMilanka Ringwald } 2045a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 2046a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '0'; 20471c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20483c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2049667ec068SMatthias Ringwald } 2050667ec068SMatthias Ringwald 20513c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){ 20529c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2053a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20543c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2055a33eb0c4SMilanka Ringwald } 2056a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 2057a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '1'; 20581c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20593c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2060667ec068SMatthias Ringwald } 2061667ec068SMatthias Ringwald 20623c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){ 20639c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2064a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20653c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2066a33eb0c4SMilanka Ringwald } 2067a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 2068a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '2'; 20691c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20703c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2071667ec068SMatthias Ringwald } 2072667ec068SMatthias Ringwald 20733c65e705SMilanka Ringwald uint8_t hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){ 20749c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2075a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20763c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2077a33eb0c4SMilanka Ringwald } 2078a0ffb263SMatthias Ringwald hfp_connection->hf_send_cnum = 1; 20791c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20803c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2081667ec068SMatthias Ringwald } 2082667ec068SMatthias Ringwald 20833c65e705SMilanka Ringwald uint8_t hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){ 20849c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2085a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20863c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2087a33eb0c4SMilanka Ringwald } 2088667ec068SMatthias Ringwald // find index for assigned number 2089667ec068SMatthias Ringwald int i; 2090aeb0f0feSMatthias Ringwald for (i = 0; i < hfp_hf_indicators_nr ; i++){ 2091aeb0f0feSMatthias Ringwald if (hfp_hf_indicators[i] == assigned_number){ 2092667ec068SMatthias Ringwald // set value 2093aeb0f0feSMatthias Ringwald hfp_hf_indicators_value[i] = value; 2094667ec068SMatthias Ringwald // mark for update 2095a0ffb263SMatthias Ringwald if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){ 2096a0ffb263SMatthias Ringwald hfp_connection->generic_status_update_bitmap |= (1<<i); 2097667ec068SMatthias Ringwald // send update 20981c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 2099a0ffb263SMatthias Ringwald } 21003c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2101667ec068SMatthias Ringwald } 2102667ec068SMatthias Ringwald } 21033c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2104667ec068SMatthias Ringwald } 2105667ec068SMatthias Ringwald 2106d7f6b5cbSMatthias Ringwald int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){ 2107d7f6b5cbSMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2108d7f6b5cbSMatthias Ringwald if (!hfp_connection) { 2109d7f6b5cbSMatthias Ringwald return 0; 2110d7f6b5cbSMatthias Ringwald } 2111d7f6b5cbSMatthias Ringwald return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE); 2112d7f6b5cbSMatthias Ringwald } 211376cc1527SMatthias Ringwald 211476cc1527SMatthias 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){ 211576cc1527SMatthias Ringwald if (!name){ 2116aeb0f0feSMatthias Ringwald name = hfp_hf_default_service_name; 211776cc1527SMatthias Ringwald } 211876cc1527SMatthias Ringwald hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name); 211976cc1527SMatthias Ringwald 212076cc1527SMatthias Ringwald // Construct SupportedFeatures for SDP bitmap: 212176cc1527SMatthias Ringwald // 212276cc1527SMatthias Ringwald // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 212376cc1527SMatthias Ringwald // of the Bits 0 to 4 of the unsolicited result code +BRSF" 212476cc1527SMatthias Ringwald // 212576cc1527SMatthias Ringwald // Wide band speech (bit 5) requires Codec negotiation 212676cc1527SMatthias Ringwald // 212776cc1527SMatthias Ringwald uint16_t sdp_features = supported_features & 0x1f; 2128ef3ae4ebSMilanka Ringwald if ( (wide_band_speech != 0) && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){ 212976cc1527SMatthias Ringwald sdp_features |= 1 << 5; 213076cc1527SMatthias Ringwald } 2131ef3ae4ebSMilanka Ringwald 2132ef3ae4ebSMilanka Ringwald if (supported_features & (1 << HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS)){ 213356f1adacSMilanka Ringwald sdp_features |= 1 << 6; 2134ef3ae4ebSMilanka Ringwald } 2135ef3ae4ebSMilanka Ringwald 2136ef3ae4ebSMilanka Ringwald if (supported_features & (1 << HFP_HFSF_VOICE_RECOGNITION_TEXT)){ 213756f1adacSMilanka Ringwald sdp_features |= 1 << 7; 2138ef3ae4ebSMilanka Ringwald } 2139ef3ae4ebSMilanka Ringwald 214076cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 214176cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 214276cc1527SMatthias Ringwald } 214376cc1527SMatthias Ringwald 214476cc1527SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){ 214568466199SMilanka Ringwald btstack_assert(callback != NULL); 214668466199SMilanka Ringwald 214776cc1527SMatthias Ringwald hfp_hf_callback = callback; 214876cc1527SMatthias Ringwald hfp_set_hf_callback(callback); 214976cc1527SMatthias Ringwald } 2150