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_operation; 231b95cac54SMilanka Ringwald event[pos++] = hfp_connection->ag_msg.text_type; 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 { 591de9e0ea7SMilanka Ringwald hfp_emit_voice_recognition_state_event(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 { 6021a26de69SMilanka Ringwald hfp_emit_voice_recognition_state_event(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 1005667ec068SMatthias Ringwald // restore volume settings 1006a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = hfp_hf_speaker_gain; 1007a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 1008ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain); 1009a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = hfp_hf_microphone_gain; 1010a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 1011ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain); 1012667ec068SMatthias Ringwald // enable all indicators 1013667ec068SMatthias Ringwald int i; 1014aeb0f0feSMatthias Ringwald for (i=0; i < hfp_hf_indicators_nr; i++){ 1015aeb0f0feSMatthias Ringwald hfp_connection->generic_status_indicators[i].uuid = hfp_hf_indicators[i]; 1016a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[i].state = 1; 1017667ec068SMatthias Ringwald } 1018ce263fc8SMatthias Ringwald } 1019ce263fc8SMatthias Ringwald 10201cc65c4fSMatthias Ringwald static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){ 1021aeb0f0feSMatthias Ringwald if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_hf_codecs_nr, hfp_hf_codecs)){ 10221cc65c4fSMatthias Ringwald // Codec supported, confirm 10231cc65c4fSMatthias Ringwald hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 10241cc65c4fSMatthias Ringwald hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 10251cc65c4fSMatthias Ringwald log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD"); 10261cc65c4fSMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC; 10271cc65c4fSMatthias Ringwald 10281cc65c4fSMatthias Ringwald hfp_connection->hf_send_codec_confirm = true; 10291cc65c4fSMatthias Ringwald } else { 10301cc65c4fSMatthias Ringwald // Codec not supported, send supported codecs 10311cc65c4fSMatthias Ringwald hfp_connection->codec_confirmed = 0; 10321cc65c4fSMatthias Ringwald hfp_connection->suggested_codec = 0; 10331cc65c4fSMatthias Ringwald hfp_connection->negotiated_codec = 0; 10341cc65c4fSMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 10351cc65c4fSMatthias Ringwald 10361cc65c4fSMatthias Ringwald hfp_connection->hf_send_supported_codecs = true; 10371cc65c4fSMatthias Ringwald } 10381cc65c4fSMatthias Ringwald } 10391cc65c4fSMatthias Ringwald 1040e2c3b58dSMilanka Ringwald static bool hfp_hf_switch_on_ok_pending(hfp_connection_t *hfp_connection, uint8_t status){ 1041e2c3b58dSMilanka Ringwald bool event_emited = true; 1042e2c3b58dSMilanka Ringwald 104399af1e28SMilanka Ringwald switch (hfp_connection->response_pending_for_command){ 1044e2c3b58dSMilanka Ringwald case HFP_CMD_TURN_OFF_EC_AND_NR: 1045e2c3b58dSMilanka Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE, status); 1046e2c3b58dSMilanka Ringwald break; 1047e2c3b58dSMilanka Ringwald default: 1048e2c3b58dSMilanka Ringwald event_emited = false; 1049e2c3b58dSMilanka Ringwald 1050a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 10513deb3ec6SMatthias Ringwald case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 1052a0ffb263SMatthias Ringwald if (has_codec_negotiation_feature(hfp_connection)){ 1053a0ffb263SMatthias Ringwald hfp_connection->state = HFP_NOTIFY_ON_CODECS; 10543deb3ec6SMatthias Ringwald break; 10553deb3ec6SMatthias Ringwald } 1056a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 10573deb3ec6SMatthias Ringwald break; 10583deb3ec6SMatthias Ringwald 10593deb3ec6SMatthias Ringwald case HFP_W4_NOTIFY_ON_CODECS: 1060a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 10613deb3ec6SMatthias Ringwald break; 10623deb3ec6SMatthias Ringwald 10633deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS: 1064a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS; 10653deb3ec6SMatthias Ringwald break; 10663deb3ec6SMatthias Ringwald 10673deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS_STATUS: 1068a0ffb263SMatthias Ringwald hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE; 10693deb3ec6SMatthias Ringwald break; 10703deb3ec6SMatthias Ringwald 10713deb3ec6SMatthias Ringwald case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE: 1072a0ffb263SMatthias Ringwald if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 1073a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL; 10743deb3ec6SMatthias Ringwald break; 10753deb3ec6SMatthias Ringwald } 1076a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 1077a0ffb263SMatthias Ringwald hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 10783deb3ec6SMatthias Ringwald break; 10793deb3ec6SMatthias Ringwald } 1080a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 10813deb3ec6SMatthias Ringwald break; 10823deb3ec6SMatthias Ringwald 10833deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_CAN_HOLD_CALL: 1084a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 1085a0ffb263SMatthias Ringwald hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 10863deb3ec6SMatthias Ringwald break; 10873deb3ec6SMatthias Ringwald } 1088a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 10893deb3ec6SMatthias Ringwald break; 10903deb3ec6SMatthias Ringwald 10913deb3ec6SMatthias Ringwald case HFP_W4_LIST_GENERIC_STATUS_INDICATORS: 1092a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS; 10933deb3ec6SMatthias Ringwald break; 10943deb3ec6SMatthias Ringwald 10953deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS: 1096a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 10973deb3ec6SMatthias Ringwald break; 10983deb3ec6SMatthias Ringwald 10993deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 1100a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 11013deb3ec6SMatthias Ringwald break; 1102ce263fc8SMatthias Ringwald case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 1103a0ffb263SMatthias Ringwald if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 1104a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 1105ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 1106ce263fc8SMatthias Ringwald break; 1107ce263fc8SMatthias Ringwald } 11083deb3ec6SMatthias Ringwald 1109a0ffb263SMatthias Ringwald if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){ 1110a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 0; 1111ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 1112ce263fc8SMatthias Ringwald break; 11133deb3ec6SMatthias Ringwald } 11143deb3ec6SMatthias Ringwald 1115a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 1116ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK: 1117a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1118ce263fc8SMatthias Ringwald break; 1119ce263fc8SMatthias Ringwald case HPF_HF_QUERY_OPERATOR_W4_RESULT: 1120a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET; 1121a473a009SMatthias Ringwald hfp_emit_network_operator_event(hfp_connection); 1122ce263fc8SMatthias Ringwald break; 1123ce263fc8SMatthias Ringwald default: 1124ce263fc8SMatthias Ringwald break; 11253deb3ec6SMatthias Ringwald } 1126ce263fc8SMatthias Ringwald 1127a0ffb263SMatthias Ringwald if (hfp_connection->enable_extended_audio_gateway_error_report){ 1128a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = 0; 1129ce263fc8SMatthias Ringwald break; 11303deb3ec6SMatthias Ringwald } 11313deb3ec6SMatthias Ringwald 1132a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 1133aa4dd815SMatthias Ringwald case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 1134a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 11353deb3ec6SMatthias Ringwald break; 1136ce263fc8SMatthias Ringwald case HFP_CODECS_HF_CONFIRMED_CODEC: 1137a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 1138ce263fc8SMatthias Ringwald break; 11393deb3ec6SMatthias Ringwald default: 11403deb3ec6SMatthias Ringwald break; 11413deb3ec6SMatthias Ringwald } 1142af97579eSMilanka Ringwald hfp_hf_voice_recognition_state_machine(hfp_connection); 1143be55a11dSMilanka Ringwald break; 1144be55a11dSMilanka Ringwald case HFP_AUDIO_CONNECTION_ESTABLISHED: 1145af97579eSMilanka Ringwald hfp_hf_voice_recognition_state_machine(hfp_connection); 11463deb3ec6SMatthias Ringwald break; 11473deb3ec6SMatthias Ringwald default: 11483deb3ec6SMatthias Ringwald break; 11493deb3ec6SMatthias Ringwald } 1150e2c3b58dSMilanka Ringwald break; 1151e2c3b58dSMilanka Ringwald } 11523deb3ec6SMatthias Ringwald 11533deb3ec6SMatthias Ringwald // done 115499af1e28SMilanka Ringwald hfp_connection->response_pending_for_command = HFP_CMD_NONE; 1155be55a11dSMilanka Ringwald hfp_connection->ok_pending = 0; 1156a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1157e2c3b58dSMilanka Ringwald return event_emited; 11583deb3ec6SMatthias Ringwald } 11593deb3ec6SMatthias Ringwald 1160*a03dbc20SMilanka Ringwald static bool hfp_is_ringing(hfp_callsetup_status_t callsetup_status){ 1161*a03dbc20SMilanka Ringwald switch (callsetup_status){ 1162*a03dbc20SMilanka Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1163*a03dbc20SMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1164*a03dbc20SMilanka Ringwald return true; 1165*a03dbc20SMilanka Ringwald default: 1166*a03dbc20SMilanka Ringwald return false; 1167*a03dbc20SMilanka Ringwald } 1168*a03dbc20SMilanka Ringwald } 1169be55a11dSMilanka Ringwald 1170b08371a9SMilanka Ringwald static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) { 11714562e2a2SMatthias Ringwald uint16_t i; 1172*a03dbc20SMilanka Ringwald 11734562e2a2SMatthias Ringwald for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 11744562e2a2SMatthias Ringwald if (hfp_connection->ag_indicators[i].status_changed) { 11754562e2a2SMatthias Ringwald if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){ 1176*a03dbc20SMilanka Ringwald hfp_callsetup_status_t new_hf_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status; 1177*a03dbc20SMilanka Ringwald bool ringing_old = hfp_is_ringing(hfp_hf_callsetup_status); 1178*a03dbc20SMilanka Ringwald bool ringing_new = hfp_is_ringing(new_hf_callsetup_status); 1179*a03dbc20SMilanka Ringwald if (ringing_old != ringing_new){ 1180*a03dbc20SMilanka Ringwald if (ringing_new){ 1181*a03dbc20SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGING); 1182*a03dbc20SMilanka Ringwald } else { 1183*a03dbc20SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGING); 1184*a03dbc20SMilanka Ringwald } 1185*a03dbc20SMilanka Ringwald } 1186*a03dbc20SMilanka Ringwald hfp_hf_callsetup_status = new_hf_callsetup_status; 11874562e2a2SMatthias Ringwald } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){ 1188aeb0f0feSMatthias Ringwald hfp_hf_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status; 11894562e2a2SMatthias Ringwald // avoid set but not used warning 1190aeb0f0feSMatthias Ringwald (void) hfp_hf_callheld_status; 11914562e2a2SMatthias Ringwald } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){ 1192aeb0f0feSMatthias Ringwald hfp_hf_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status; 11934562e2a2SMatthias Ringwald } 11944562e2a2SMatthias Ringwald hfp_connection->ag_indicators[i].status_changed = 0; 1195a473a009SMatthias Ringwald hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]); 11964562e2a2SMatthias Ringwald break; 11974562e2a2SMatthias Ringwald } 11984562e2a2SMatthias Ringwald } 11994562e2a2SMatthias Ringwald } 12004562e2a2SMatthias Ringwald 1201426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){ 1202186dd3d2SMatthias Ringwald int value; 1203186dd3d2SMatthias Ringwald int i; 1204e2c3b58dSMilanka Ringwald bool event_emited; 1205e2c3b58dSMilanka Ringwald 1206a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1207667ec068SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1208a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1209a473a009SMatthias Ringwald hfp_hf_emit_subscriber_information(hfp_connection, 0); 1210667ec068SMatthias Ringwald break; 1211667ec068SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_STATUS: 1212a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1213ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0])); 1214667ec068SMatthias Ringwald break; 1215667ec068SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 1216a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1217a473a009SMatthias Ringwald hfp_hf_emit_enhanced_call_status(hfp_connection); 1218667ec068SMatthias Ringwald break; 1219ce263fc8SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 1220a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 12212308e108SMilanka Ringwald value = btstack_atoi((char*)hfp_connection->line_buffer); 1222667ec068SMatthias Ringwald hfp_hf_speaker_gain = value; 1223ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value); 1224ce263fc8SMatthias Ringwald break; 1225ce263fc8SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 1226a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 12272308e108SMilanka Ringwald value = btstack_atoi((char*)hfp_connection->line_buffer); 1228667ec068SMatthias Ringwald hfp_hf_microphone_gain = value; 1229ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value); 1230ce263fc8SMatthias Ringwald break; 1231ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 1232a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1233ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number); 1234a0ffb263SMatthias Ringwald break; 1235a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1236a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1237a473a009SMatthias Ringwald hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION); 1238a0ffb263SMatthias Ringwald break; 1239a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1240a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1241a473a009SMatthias Ringwald hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION); 1242ce263fc8SMatthias Ringwald break; 1243ce263fc8SMatthias Ringwald case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1244a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 12455a4785c8SMatthias Ringwald hfp_connection->ok_pending = 0; 1246a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 1247ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value); 1248ce263fc8SMatthias Ringwald break; 12490b4debbfSMilanka Ringwald case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION: 12500b4debbfSMilanka Ringwald break; 1251fdda66c0SMilanka Ringwald case HFP_CMD_ERROR: 125290244c92SMilanka Ringwald switch (hfp_connection->state){ 125390244c92SMilanka Ringwald case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 125490244c92SMilanka Ringwald switch (hfp_connection->codecs_state){ 125590244c92SMilanka Ringwald case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 1256fdda66c0SMilanka Ringwald hfp_reset_context_flags(hfp_connection); 125790244c92SMilanka Ringwald hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec); 125890244c92SMilanka Ringwald return; 125990244c92SMilanka Ringwald default: 126090244c92SMilanka Ringwald break; 126190244c92SMilanka Ringwald } 126256f1adacSMilanka Ringwald break; 126356f1adacSMilanka Ringwald default: 126456f1adacSMilanka Ringwald break; 126556f1adacSMilanka Ringwald } 1266e2c3b58dSMilanka Ringwald 1267fdda66c0SMilanka Ringwald // handle error response for voice activation (HF initiated) 12680b4debbfSMilanka Ringwald switch(hfp_connection->vra_state_requested){ 1269de9e0ea7SMilanka Ringwald case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1270de9e0ea7SMilanka Ringwald hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 1271be55a11dSMilanka Ringwald break; 1272be55a11dSMilanka Ringwald default: 1273e2c3b58dSMilanka Ringwald if (hfp_connection->vra_state_requested == hfp_connection->vra_state){ 1274e2c3b58dSMilanka Ringwald break; 1275e2c3b58dSMilanka Ringwald } 12760b4debbfSMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 12771a26de69SMilanka Ringwald hfp_emit_voice_recognition_state_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 12780b4debbfSMilanka Ringwald hfp_reset_context_flags(hfp_connection); 12790b4debbfSMilanka Ringwald return; 1280be55a11dSMilanka Ringwald } 1281e2c3b58dSMilanka Ringwald event_emited = hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 1282e2c3b58dSMilanka Ringwald if (!event_emited){ 12830b4debbfSMilanka Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1); 1284e2c3b58dSMilanka Ringwald } 1285fdda66c0SMilanka Ringwald hfp_reset_context_flags(hfp_connection); 1286ce263fc8SMatthias Ringwald break; 1287fdda66c0SMilanka Ringwald 1288ce263fc8SMatthias Ringwald case HFP_CMD_OK: 1289e2c3b58dSMilanka Ringwald hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_SUCCESS); 1290ce263fc8SMatthias Ringwald break; 1291ce263fc8SMatthias Ringwald case HFP_CMD_RING: 12925a4785c8SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1293ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING); 1294ce263fc8SMatthias Ringwald break; 1295ce263fc8SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 12965a4785c8SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 12974562e2a2SMatthias Ringwald hfp_hf_handle_transfer_ag_indicator_status(hfp_connection); 1298ce263fc8SMatthias Ringwald break; 1299c741b032SMilanka Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 13005a4785c8SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1301c741b032SMilanka Ringwald for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 1302a473a009SMatthias Ringwald hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]); 1303c741b032SMilanka Ringwald } 1304c741b032SMilanka Ringwald break; 13051cc65c4fSMatthias Ringwald case HFP_CMD_AG_SUGGESTED_CODEC: 13061cc65c4fSMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 13075a4785c8SMatthias Ringwald hfp_hf_handle_suggested_codec(hfp_connection); 13081cc65c4fSMatthias Ringwald break; 1309eac56539SMilanka Ringwald case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING: 1310eac56539SMilanka 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)); 1311ce263fc8SMatthias Ringwald default: 1312ce263fc8SMatthias Ringwald break; 13133deb3ec6SMatthias Ringwald } 13140cef86faSMatthias Ringwald } 1315426f9988SMatthias Ringwald 131676cc1527SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){ 131776cc1527SMatthias Ringwald return (byte == '\n') || (byte == '\r'); 131876cc1527SMatthias Ringwald } 131976cc1527SMatthias Ringwald 13200b4debbfSMilanka Ringwald static void hfp_hf_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){ 1321426f9988SMatthias Ringwald // assertion: size >= 1 as rfcomm.c does not deliver empty packets 1322426f9988SMatthias Ringwald if (size < 1) return; 1323426f9988SMatthias Ringwald 1324426f9988SMatthias Ringwald hfp_log_rfcomm_message("HFP_HF_RX", packet, size); 1325e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES 1326e43d1938SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet); 1327e43d1938SMatthias Ringwald #endif 1328426f9988SMatthias Ringwald 1329426f9988SMatthias Ringwald // process messages byte-wise 1330426f9988SMatthias Ringwald int pos; 1331426f9988SMatthias Ringwald for (pos = 0; pos < size; pos++){ 1332426f9988SMatthias Ringwald hfp_parse(hfp_connection, packet[pos], 1); 13331599fe57SMatthias Ringwald // parse until end of line "\r" or "\n" 1334426f9988SMatthias Ringwald if (!hfp_parser_is_end_of_line(packet[pos])) continue; 1335426f9988SMatthias Ringwald } 13360b4debbfSMilanka Ringwald hfp_hf_handle_rfcomm_command(hfp_connection); 13373deb3ec6SMatthias Ringwald } 13383deb3ec6SMatthias Ringwald 13391c6a0fc0SMatthias Ringwald static void hfp_hf_run(void){ 1340665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1341665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1342665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1343a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 134422387625SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_HF) continue; 13451c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 13463deb3ec6SMatthias Ringwald } 13473deb3ec6SMatthias Ringwald } 13483deb3ec6SMatthias Ringwald 13491c6a0fc0SMatthias Ringwald static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 13500b4debbfSMilanka Ringwald hfp_connection_t * hfp_connection; 13513deb3ec6SMatthias Ringwald switch (packet_type){ 13523deb3ec6SMatthias Ringwald case RFCOMM_DATA_PACKET: 13530b4debbfSMilanka Ringwald hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 13540b4debbfSMilanka Ringwald if (!hfp_connection) return; 13550b4debbfSMilanka Ringwald hfp_hf_handle_rfcomm_data(hfp_connection, packet, size); 13560b4debbfSMilanka Ringwald hfp_hf_run_for_context(hfp_connection); 13570b4debbfSMilanka Ringwald return; 13583deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 1359d4dd47ffSMatthias Ringwald if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 1360d4dd47ffSMatthias Ringwald uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 13611c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid)); 1362d4dd47ffSMatthias Ringwald return; 1363d4dd47ffSMatthias Ringwald } 136427950165SMatthias Ringwald hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF); 1365202c8a4cSMatthias Ringwald break; 13663deb3ec6SMatthias Ringwald default: 13673deb3ec6SMatthias Ringwald break; 13683deb3ec6SMatthias Ringwald } 13691c6a0fc0SMatthias Ringwald hfp_hf_run(); 13703deb3ec6SMatthias Ringwald } 13713deb3ec6SMatthias Ringwald 13721c6a0fc0SMatthias Ringwald static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1373405014fbSMatthias Ringwald hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF); 13741c6a0fc0SMatthias Ringwald hfp_hf_run(); 1375405014fbSMatthias Ringwald } 1376405014fbSMatthias Ringwald 1377aeb0f0feSMatthias Ringwald static void hfp_hf_set_defaults(void){ 1378aeb0f0feSMatthias Ringwald hfp_hf_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 1379aeb0f0feSMatthias Ringwald hfp_hf_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS; 1380aeb0f0feSMatthias Ringwald hfp_hf_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1381aeb0f0feSMatthias Ringwald hfp_hf_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD; 1382aeb0f0feSMatthias Ringwald hfp_hf_codecs_nr = 0; 1383aeb0f0feSMatthias Ringwald hfp_hf_speaker_gain = 9; 1384aeb0f0feSMatthias Ringwald hfp_hf_microphone_gain = 9; 1385aeb0f0feSMatthias Ringwald hfp_hf_indicators_nr = 0; 1386aeb0f0feSMatthias Ringwald } 1387aeb0f0feSMatthias Ringwald 1388b4df8028SMilanka Ringwald uint8_t hfp_hf_init(uint16_t rfcomm_channel_nr){ 1389b4df8028SMilanka Ringwald uint8_t status = rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 1390b4df8028SMilanka Ringwald if (status != ERROR_CODE_SUCCESS){ 1391b4df8028SMilanka Ringwald return status; 1392b4df8028SMilanka Ringwald } 1393b4df8028SMilanka Ringwald 1394520c92d5SMatthias Ringwald hfp_init(); 1395aeb0f0feSMatthias Ringwald hfp_hf_set_defaults(); 1396d63c37a1SMatthias Ringwald 13971c6a0fc0SMatthias Ringwald hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler; 13981c6a0fc0SMatthias Ringwald hci_add_event_handler(&hfp_hf_hci_event_callback_registration); 139927950165SMatthias Ringwald 140027950165SMatthias Ringwald // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us 14011c6a0fc0SMatthias Ringwald hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler); 1402b4df8028SMilanka Ringwald return ERROR_CODE_SUCCESS; 140320b2edb6SMatthias Ringwald } 140427950165SMatthias Ringwald 140520b2edb6SMatthias Ringwald void hfp_hf_deinit(void){ 140620b2edb6SMatthias Ringwald hfp_deinit(); 1407aeb0f0feSMatthias Ringwald hfp_hf_set_defaults(); 1408aeb0f0feSMatthias Ringwald 1409aeb0f0feSMatthias Ringwald hfp_hf_callback = NULL; 141020b2edb6SMatthias Ringwald (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t)); 1411aeb0f0feSMatthias Ringwald (void) memset(hfp_hf_phone_number, 0, sizeof(hfp_hf_phone_number)); 1412a0ffb263SMatthias Ringwald } 1413a0ffb263SMatthias Ringwald 14147ca89cabSMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, const uint8_t * codecs){ 141568466199SMilanka Ringwald btstack_assert(codecs_nr < HFP_MAX_NUM_CODECS); 14163deb3ec6SMatthias Ringwald 1417aeb0f0feSMatthias Ringwald hfp_hf_codecs_nr = codecs_nr; 14183deb3ec6SMatthias Ringwald int i; 14193deb3ec6SMatthias Ringwald for (i=0; i<codecs_nr; i++){ 1420aeb0f0feSMatthias Ringwald hfp_hf_codecs[i] = codecs[i]; 14213deb3ec6SMatthias Ringwald } 14223deb3ec6SMatthias Ringwald } 14233deb3ec6SMatthias Ringwald 1424a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){ 1425aeb0f0feSMatthias Ringwald hfp_hf_supported_features = supported_features; 1426a0ffb263SMatthias Ringwald } 14273deb3ec6SMatthias Ringwald 14287ca89cabSMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){ 1429aeb0f0feSMatthias Ringwald btstack_assert(hfp_hf_indicators_nr < HFP_MAX_NUM_INDICATORS); 143068466199SMilanka Ringwald 1431aeb0f0feSMatthias Ringwald hfp_hf_indicators_nr = indicators_nr; 14323deb3ec6SMatthias Ringwald int i; 1433aeb0f0feSMatthias Ringwald for (i = 0; i < hfp_hf_indicators_nr ; i++){ 1434aeb0f0feSMatthias Ringwald hfp_hf_indicators[i] = indicators[i]; 14353deb3ec6SMatthias Ringwald } 14363deb3ec6SMatthias Ringwald } 14373deb3ec6SMatthias Ringwald 14384eb3f1d8SMilanka Ringwald uint8_t hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){ 14394eb3f1d8SMilanka Ringwald return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF); 14403deb3ec6SMatthias Ringwald } 14413deb3ec6SMatthias Ringwald 1442657bc59fSMilanka Ringwald uint8_t hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){ 14439c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1444a33eb0c4SMilanka Ringwald if (!hfp_connection){ 1445657bc59fSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1446a33eb0c4SMilanka Ringwald } 14471ffa0dd9SMilanka Ringwald hfp_trigger_release_service_level_connection(hfp_connection); 14481c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1449657bc59fSMilanka Ringwald return ERROR_CODE_SUCCESS; 14503deb3ec6SMatthias Ringwald } 14513deb3ec6SMatthias Ringwald 14523c65e705SMilanka Ringwald static uint8_t hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){ 14539c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1454a0ffb263SMatthias Ringwald if (!hfp_connection) { 14553c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 14563deb3ec6SMatthias Ringwald } 1457a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = enable; 14581c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 14593c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 14603deb3ec6SMatthias Ringwald } 14613deb3ec6SMatthias Ringwald 14623c65e705SMilanka Ringwald uint8_t hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 14633c65e705SMilanka Ringwald return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1); 1464ce263fc8SMatthias Ringwald } 1465ce263fc8SMatthias Ringwald 14663c65e705SMilanka Ringwald uint8_t hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 14673c65e705SMilanka Ringwald return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0); 1468ce263fc8SMatthias Ringwald } 1469ce263fc8SMatthias Ringwald 14703deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format 14713c65e705SMilanka Ringwald uint8_t hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){ 14729c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1473a0ffb263SMatthias Ringwald if (!hfp_connection) { 14743c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 14753deb3ec6SMatthias Ringwald } 1476a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 1; 1477a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap; 14781c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 14793c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 14803deb3ec6SMatthias Ringwald } 14813deb3ec6SMatthias Ringwald 14823c65e705SMilanka Ringwald uint8_t hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){ 14839c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1484a0ffb263SMatthias Ringwald if (!hfp_connection) { 14853c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 14863deb3ec6SMatthias Ringwald } 14873c65e705SMilanka Ringwald 1488a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 1489ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET: 1490a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT; 1491ce263fc8SMatthias Ringwald break; 1492ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_FORMAT_SET: 1493a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1494ce263fc8SMatthias Ringwald break; 1495ce263fc8SMatthias Ringwald default: 14963c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1497ce263fc8SMatthias Ringwald } 14981c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 14993c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 15003deb3ec6SMatthias Ringwald } 15013deb3ec6SMatthias Ringwald 15023c65e705SMilanka Ringwald static uint8_t hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){ 15039c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1504a0ffb263SMatthias Ringwald if (!hfp_connection) { 15053c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 15063deb3ec6SMatthias Ringwald } 1507a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = enable; 15081c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 15093c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 15103deb3ec6SMatthias Ringwald } 15113deb3ec6SMatthias Ringwald 1512ce263fc8SMatthias Ringwald 15133c65e705SMilanka Ringwald uint8_t hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 15143c65e705SMilanka Ringwald return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1); 1515ce263fc8SMatthias Ringwald } 1516ce263fc8SMatthias Ringwald 15173c65e705SMilanka Ringwald uint8_t hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 15183c65e705SMilanka Ringwald return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0); 1519ce263fc8SMatthias Ringwald } 1520ce263fc8SMatthias Ringwald 152138200c1dSMilanka Ringwald static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){ 1522aeb0f0feSMatthias Ringwald return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) && (hfp_hf_supported_features & (1 << HFP_HFSF_ESCO_S4)); 152338200c1dSMilanka Ringwald } 1524ce263fc8SMatthias Ringwald 15253c65e705SMilanka Ringwald uint8_t hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){ 15269c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1527a33eb0c4SMilanka Ringwald if (!hfp_connection) { 15283c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1529a33eb0c4SMilanka Ringwald } 1530ce263fc8SMatthias Ringwald 15313c65e705SMilanka Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 15323c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 15333c65e705SMilanka Ringwald } 15343c65e705SMilanka Ringwald 15353c65e705SMilanka Ringwald if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){ 15363c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 15373c65e705SMilanka Ringwald } 1538f4412093SMatthias Ringwald if (has_codec_negotiation_feature(hfp_connection)) { 1539a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state) { 1540aa4dd815SMatthias Ringwald case HFP_CODECS_W4_AG_COMMON_CODEC: 1541aa4dd815SMatthias Ringwald break; 1542ec3bfc1aSMatthias Ringwald case HFP_CODECS_EXCHANGED: 1543ec3bfc1aSMatthias Ringwald hfp_connection->trigger_codec_exchange = 1; 1544ec3bfc1aSMatthias Ringwald break; 1545aa4dd815SMatthias Ringwald default: 15461cc65c4fSMatthias Ringwald hfp_connection->codec_confirmed = 0; 15471cc65c4fSMatthias Ringwald hfp_connection->suggested_codec = 0; 15481cc65c4fSMatthias Ringwald hfp_connection->negotiated_codec = 0; 15491cc65c4fSMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 155038200c1dSMilanka Ringwald hfp_connection->trigger_codec_exchange = 1; 1551aa4dd815SMatthias Ringwald break; 15523deb3ec6SMatthias Ringwald } 1553f4412093SMatthias Ringwald } else { 1554f4412093SMatthias Ringwald log_info("no codec negotiation feature, use CVSD"); 1555f4412093SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 1556f4412093SMatthias Ringwald hfp_connection->suggested_codec = HFP_CODEC_CVSD; 1557f4412093SMatthias Ringwald hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 1558f4412093SMatthias Ringwald hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 1559f4412093SMatthias Ringwald hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection)); 1560f4412093SMatthias Ringwald hfp_connection->establish_audio_connection = 1; 1561f4412093SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 1562ce263fc8SMatthias Ringwald } 1563ce263fc8SMatthias Ringwald 15641c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 15653c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 15663deb3ec6SMatthias Ringwald } 15673deb3ec6SMatthias Ringwald 15683c65e705SMilanka Ringwald uint8_t hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){ 15699c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1570a33eb0c4SMilanka Ringwald if (!hfp_connection) { 15713c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1572a33eb0c4SMilanka Ringwald } 15730b4debbfSMilanka Ringwald if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){ 15740b4debbfSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 15750b4debbfSMilanka Ringwald } 15760b4debbfSMilanka Ringwald uint8_t status = hfp_trigger_release_audio_connection(hfp_connection); 15770b4debbfSMilanka Ringwald if (status == ERROR_CODE_SUCCESS){ 15781c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 15790b4debbfSMilanka Ringwald } 15803c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 15813deb3ec6SMatthias Ringwald } 15823deb3ec6SMatthias Ringwald 15833c65e705SMilanka Ringwald uint8_t hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){ 15849c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1585a33eb0c4SMilanka Ringwald if (!hfp_connection) { 15863c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1587a33eb0c4SMilanka Ringwald } 1588ce263fc8SMatthias Ringwald 1589aeb0f0feSMatthias Ringwald if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1590a0ffb263SMatthias Ringwald hfp_connection->hf_answer_incoming_call = 1; 15911c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1592ce263fc8SMatthias Ringwald } else { 1593aeb0f0feSMatthias Ringwald log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_hf_callsetup_status); 15943c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1595ce263fc8SMatthias Ringwald } 15963c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1597ce263fc8SMatthias Ringwald } 1598ce263fc8SMatthias Ringwald 15993c65e705SMilanka Ringwald uint8_t hfp_hf_terminate_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 } 1604a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 1; 16051c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 16063c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1607ce263fc8SMatthias Ringwald } 1608ce263fc8SMatthias Ringwald 16093c65e705SMilanka Ringwald uint8_t hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){ 16109c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1611a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16123c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1613a33eb0c4SMilanka Ringwald } 1614ce263fc8SMatthias Ringwald 1615aeb0f0feSMatthias Ringwald if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1616a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 1; 16171c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1618ce263fc8SMatthias Ringwald } 16193c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1620ce263fc8SMatthias Ringwald } 1621ce263fc8SMatthias Ringwald 16223c65e705SMilanka Ringwald uint8_t hfp_hf_user_busy(hci_con_handle_t acl_handle){ 16239c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1624a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16253c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1626a33eb0c4SMilanka Ringwald } 1627ce263fc8SMatthias Ringwald 1628aeb0f0feSMatthias Ringwald if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1629a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_0 = 1; 16301c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1631ce263fc8SMatthias Ringwald } 16323c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1633ce263fc8SMatthias Ringwald } 1634ce263fc8SMatthias Ringwald 16353c65e705SMilanka Ringwald uint8_t hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){ 16369c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1637a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16383c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1639a33eb0c4SMilanka Ringwald } 1640ce263fc8SMatthias Ringwald 1641aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1642aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1643a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_1 = 1; 16441c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1645ce263fc8SMatthias Ringwald } 16463c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1647ce263fc8SMatthias Ringwald } 1648ce263fc8SMatthias Ringwald 16493c65e705SMilanka Ringwald uint8_t hfp_hf_swap_calls(hci_con_handle_t acl_handle){ 16509c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1651a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16523c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1653a33eb0c4SMilanka Ringwald } 1654ce263fc8SMatthias Ringwald 1655aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1656aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1657a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_2 = 1; 16581c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1659ce263fc8SMatthias Ringwald } 16603c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1661ce263fc8SMatthias Ringwald } 1662ce263fc8SMatthias Ringwald 16633c65e705SMilanka Ringwald uint8_t hfp_hf_join_held_call(hci_con_handle_t acl_handle){ 16649c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1665a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16663c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1667a33eb0c4SMilanka Ringwald } 1668ce263fc8SMatthias Ringwald 1669aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1670aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1671a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_3 = 1; 16721c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1673ce263fc8SMatthias Ringwald } 16743c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1675ce263fc8SMatthias Ringwald } 1676ce263fc8SMatthias Ringwald 16773c65e705SMilanka Ringwald uint8_t hfp_hf_connect_calls(hci_con_handle_t acl_handle){ 16789c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1679a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16803c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1681a33eb0c4SMilanka Ringwald } 1682ce263fc8SMatthias Ringwald 1683aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1684aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1685a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_4 = 1; 16861c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1687ce263fc8SMatthias Ringwald } 16883c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1689ce263fc8SMatthias Ringwald } 1690ce263fc8SMatthias Ringwald 16913c65e705SMilanka Ringwald uint8_t hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){ 16929c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1693a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16943c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1695a33eb0c4SMilanka Ringwald } 1696667ec068SMatthias Ringwald 1697aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1698aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1699a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 1; 1700a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x_index = 10 + index; 17011c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1702667ec068SMatthias Ringwald } 17033c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1704667ec068SMatthias Ringwald } 1705667ec068SMatthias Ringwald 17063c65e705SMilanka Ringwald uint8_t hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){ 17079c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1708a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17093c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1710a33eb0c4SMilanka Ringwald } 1711667ec068SMatthias Ringwald 1712aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1713aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1714a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 1; 1715a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x_index = 20 + index; 17161c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1717667ec068SMatthias Ringwald } 17183c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1719667ec068SMatthias Ringwald } 1720ce263fc8SMatthias Ringwald 17213c65e705SMilanka Ringwald uint8_t hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){ 17229c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1723a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17243c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1725a33eb0c4SMilanka Ringwald } 1726ce263fc8SMatthias Ringwald 1727a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_outgoing_call = 1; 1728aeb0f0feSMatthias Ringwald snprintf(hfp_hf_phone_number, sizeof(hfp_hf_phone_number), "%s", number); 17291c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 17303c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1731ce263fc8SMatthias Ringwald } 1732ce263fc8SMatthias Ringwald 17333c65e705SMilanka Ringwald uint8_t hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){ 17349c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1735a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17363c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1737a33eb0c4SMilanka Ringwald } 1738ce263fc8SMatthias Ringwald 1739a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_memory_dialing = 1; 1740a0ffb263SMatthias Ringwald hfp_connection->memory_id = memory_id; 1741a0ffb263SMatthias Ringwald 17421c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 17433c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1744ce263fc8SMatthias Ringwald } 1745ce263fc8SMatthias Ringwald 17463c65e705SMilanka Ringwald uint8_t hfp_hf_redial_last_number(hci_con_handle_t acl_handle){ 17479c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1748a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17493c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1750a33eb0c4SMilanka Ringwald } 1751ce263fc8SMatthias Ringwald 1752a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_redial_last_number = 1; 17531c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 17543c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1755ce263fc8SMatthias Ringwald } 1756ce263fc8SMatthias Ringwald 17573c65e705SMilanka Ringwald uint8_t hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){ 17589c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1759a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17603c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1761a33eb0c4SMilanka Ringwald } 1762ce263fc8SMatthias Ringwald 1763a0ffb263SMatthias Ringwald hfp_connection->hf_activate_call_waiting_notification = 1; 17641c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 17653c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1766ce263fc8SMatthias Ringwald } 1767ce263fc8SMatthias Ringwald 1768ce263fc8SMatthias Ringwald 17693c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){ 17709c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1771a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17723c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1773a33eb0c4SMilanka Ringwald } 1774ce263fc8SMatthias Ringwald 1775a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_call_waiting_notification = 1; 17761c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 17773c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1778ce263fc8SMatthias Ringwald } 1779ce263fc8SMatthias Ringwald 1780ce263fc8SMatthias Ringwald 17813c65e705SMilanka Ringwald uint8_t hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){ 17829c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1783a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17843c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1785a33eb0c4SMilanka Ringwald } 1786ce263fc8SMatthias Ringwald 1787a0ffb263SMatthias Ringwald hfp_connection->hf_activate_calling_line_notification = 1; 17881c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 17893c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1790ce263fc8SMatthias Ringwald } 1791ce263fc8SMatthias Ringwald 17923c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){ 17939c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1794a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17953c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1796a33eb0c4SMilanka Ringwald } 1797ce263fc8SMatthias Ringwald 1798a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_calling_line_notification = 1; 17991c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18003c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1801ce263fc8SMatthias Ringwald } 1802ce263fc8SMatthias Ringwald 18036ba83b5eSMilanka Ringwald static bool hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection_t * hfp_connection){ 18046ba83b5eSMilanka Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_EC_NR_FUNCTION); 1805aeb0f0feSMatthias Ringwald int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_EC_NR_FUNCTION); 18066ba83b5eSMilanka Ringwald return hf && ag; 1807ce263fc8SMatthias Ringwald } 1808ce263fc8SMatthias Ringwald 18093c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){ 18109c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1811a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18123c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1813a33eb0c4SMilanka Ringwald } 18146ba83b5eSMilanka Ringwald if (!hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection)){ 18156ba83b5eSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 18166ba83b5eSMilanka Ringwald } 1817ce263fc8SMatthias Ringwald 1818a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1; 18191c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18203c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1821ce263fc8SMatthias Ringwald } 1822ce263fc8SMatthias Ringwald 1823013cc750SMilanka Ringwald static bool hfp_hf_voice_recognition_supported(hfp_connection_t * hfp_connection, bool enhanced){ 1824013cc750SMilanka Ringwald uint8_t hf_vra_flag = HFP_HFSF_VOICE_RECOGNITION_FUNCTION; 1825013cc750SMilanka Ringwald uint8_t ag_vra_flag = HFP_AGSF_VOICE_RECOGNITION_FUNCTION; 1826013cc750SMilanka Ringwald 1827013cc750SMilanka Ringwald if (enhanced){ 1828013cc750SMilanka Ringwald hf_vra_flag = HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS; 1829013cc750SMilanka Ringwald ag_vra_flag = HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS; 1830013cc750SMilanka Ringwald } 1831013cc750SMilanka Ringwald 1832013cc750SMilanka Ringwald int ag = get_bit(hfp_connection->remote_supported_features, ag_vra_flag); 1833aeb0f0feSMatthias Ringwald int hf = get_bit(hfp_hf_supported_features, hf_vra_flag); 1834be55a11dSMilanka Ringwald return hf && ag; 1835be55a11dSMilanka Ringwald } 1836be55a11dSMilanka Ringwald 1837013cc750SMilanka Ringwald static uint8_t activate_voice_recognition(hci_con_handle_t acl_handle, bool enhanced){ 1838fdda66c0SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1839fdda66c0SMilanka Ringwald if (!hfp_connection) { 1840fdda66c0SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1841be55a11dSMilanka Ringwald } 1842013cc750SMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){ 1843013cc750SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1844013cc750SMilanka Ringwald } 1845013cc750SMilanka Ringwald if (!hfp_hf_voice_recognition_supported(hfp_connection, enhanced)){ 1846af97579eSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1847af97579eSMilanka Ringwald } 1848af97579eSMilanka Ringwald 1849498a8121SMilanka Ringwald switch (hfp_connection->vra_state){ 1850be55a11dSMilanka Ringwald case HFP_VRA_VOICE_RECOGNITION_OFF: 1851de9e0ea7SMilanka Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 1852498a8121SMilanka Ringwald hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION; 1853fd4151d1SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED; 18546b8275b0SMilanka Ringwald hfp_connection->enhanced_voice_recognition_enabled = enhanced; 1855be55a11dSMilanka Ringwald break; 1856de9e0ea7SMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_OFF: 1857de9e0ea7SMilanka Ringwald hfp_connection->activate_voice_recognition = true; 1858de9e0ea7SMilanka Ringwald break; 1859be55a11dSMilanka Ringwald default: 1860be55a11dSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1861be55a11dSMilanka Ringwald } 1862ce263fc8SMatthias Ringwald 1863af97579eSMilanka Ringwald hfp_hf_run_for_context(hfp_connection); 1864fdda66c0SMilanka Ringwald return ERROR_CODE_SUCCESS; 1865af97579eSMilanka Ringwald } 1866af97579eSMilanka Ringwald 1867013cc750SMilanka Ringwald 1868013cc750SMilanka Ringwald static uint8_t deactivate_voice_recognition(hci_con_handle_t acl_handle, bool enhanced){ 1869af97579eSMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1870af97579eSMilanka Ringwald if (!hfp_connection) { 1871af97579eSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1872af97579eSMilanka Ringwald } 1873c95b5b3cSMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){ 187408a0b01cSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 187508a0b01cSMilanka Ringwald } 1876013cc750SMilanka Ringwald if (!hfp_hf_voice_recognition_supported(hfp_connection, enhanced)){ 1877fdda66c0SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1878af97579eSMilanka Ringwald } 1879013cc750SMilanka Ringwald 1880fdda66c0SMilanka Ringwald switch (hfp_connection->vra_state){ 1881de9e0ea7SMilanka Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED: 1882fdda66c0SMilanka Ringwald case HFP_VRA_VOICE_RECOGNITION_ACTIVATED: 1883de9e0ea7SMilanka Ringwald case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1884de9e0ea7SMilanka Ringwald case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1885fdda66c0SMilanka Ringwald hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION; 1886fdda66c0SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF; 1887fdda66c0SMilanka Ringwald break; 1888de9e0ea7SMilanka Ringwald 1889de9e0ea7SMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED: 1890de9e0ea7SMilanka Ringwald case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1891de9e0ea7SMilanka Ringwald hfp_connection->deactivate_voice_recognition = true; 1892de9e0ea7SMilanka Ringwald break; 1893de9e0ea7SMilanka Ringwald 1894de9e0ea7SMilanka Ringwald case HFP_VRA_VOICE_RECOGNITION_OFF: 1895de9e0ea7SMilanka Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 1896de9e0ea7SMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_OFF: 1897fdda66c0SMilanka Ringwald default: 1898fdda66c0SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1899fdda66c0SMilanka Ringwald } 1900fdda66c0SMilanka Ringwald 1901fdda66c0SMilanka Ringwald hfp_hf_run_for_context(hfp_connection); 1902fdda66c0SMilanka Ringwald return ERROR_CODE_SUCCESS; 1903af97579eSMilanka Ringwald } 1904af97579eSMilanka Ringwald 1905013cc750SMilanka Ringwald uint8_t hfp_hf_activate_voice_recognition(hci_con_handle_t acl_handle){ 1906013cc750SMilanka Ringwald return activate_voice_recognition(acl_handle, false); 1907013cc750SMilanka Ringwald } 1908013cc750SMilanka Ringwald 1909e83c2025SMilanka Ringwald uint8_t hfp_hf_activate_enhanced_voice_recognition(hci_con_handle_t acl_handle){ 1910013cc750SMilanka Ringwald return activate_voice_recognition(acl_handle, true); 1911be55a11dSMilanka Ringwald } 1912fdda66c0SMilanka Ringwald 1913de9e0ea7SMilanka Ringwald uint8_t hfp_hf_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle){ 1914de9e0ea7SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1915de9e0ea7SMilanka Ringwald if (!hfp_connection) { 1916de9e0ea7SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1917de9e0ea7SMilanka Ringwald } 1918de9e0ea7SMilanka Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){ 1919de9e0ea7SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1920de9e0ea7SMilanka Ringwald } 1921de9e0ea7SMilanka Ringwald if (!hfp_hf_voice_recognition_supported(hfp_connection, true)){ 1922de9e0ea7SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1923de9e0ea7SMilanka Ringwald } 1924de9e0ea7SMilanka Ringwald 1925de9e0ea7SMilanka Ringwald switch (hfp_connection->vra_state){ 1926de9e0ea7SMilanka Ringwald case HFP_VRA_VOICE_RECOGNITION_ACTIVATED: 1927de9e0ea7SMilanka Ringwald case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1928de9e0ea7SMilanka Ringwald hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION; 1929de9e0ea7SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 1930de9e0ea7SMilanka Ringwald break; 1931de9e0ea7SMilanka Ringwald default: 1932de9e0ea7SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1933de9e0ea7SMilanka Ringwald } 1934de9e0ea7SMilanka Ringwald 1935de9e0ea7SMilanka Ringwald hfp_hf_run_for_context(hfp_connection); 1936de9e0ea7SMilanka Ringwald return ERROR_CODE_SUCCESS; 1937de9e0ea7SMilanka Ringwald } 1938de9e0ea7SMilanka Ringwald 1939fdda66c0SMilanka Ringwald 1940013cc750SMilanka Ringwald uint8_t hfp_hf_deactivate_voice_recognition(hci_con_handle_t acl_handle){ 1941013cc750SMilanka Ringwald return deactivate_voice_recognition(acl_handle, false); 1942be55a11dSMilanka Ringwald } 1943be55a11dSMilanka Ringwald 1944e83c2025SMilanka Ringwald uint8_t hfp_hf_deactivate_enhanced_voice_recognition(hci_con_handle_t acl_handle){ 1945013cc750SMilanka Ringwald return deactivate_voice_recognition(acl_handle, true); 1946ce263fc8SMatthias Ringwald } 1947ce263fc8SMatthias Ringwald 1948de9e0ea7SMilanka Ringwald 19493c65e705SMilanka Ringwald uint8_t hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 19509c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1951a33eb0c4SMilanka Ringwald if (!hfp_connection) { 19523c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1953a33eb0c4SMilanka Ringwald } 1954c8626498SMilanka Ringwald 19553c65e705SMilanka Ringwald if (hfp_connection->microphone_gain == gain) { 19563c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 19573c65e705SMilanka Ringwald } 19583c65e705SMilanka Ringwald 1959c1ab6cc1SMatthias Ringwald if ((gain < 0) || (gain > 15)){ 1960a0ffb263SMatthias Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 19613c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1962a0ffb263SMatthias Ringwald } 19633c65e705SMilanka Ringwald 1964a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = gain; 1965a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 19661c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 19673c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1968ce263fc8SMatthias Ringwald } 1969ce263fc8SMatthias Ringwald 19703c65e705SMilanka Ringwald uint8_t hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 19719c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1972a33eb0c4SMilanka Ringwald if (!hfp_connection) { 19733c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1974a33eb0c4SMilanka Ringwald } 1975c8626498SMilanka Ringwald 19763c65e705SMilanka Ringwald if (hfp_connection->speaker_gain == gain){ 19773c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 19783c65e705SMilanka Ringwald } 19793c65e705SMilanka Ringwald 1980c1ab6cc1SMatthias Ringwald if ((gain < 0) || (gain > 15)){ 1981a0ffb263SMatthias Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 19823c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1983a0ffb263SMatthias Ringwald } 19843c65e705SMilanka Ringwald 1985a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = gain; 1986a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 19871c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 19883c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1989ce263fc8SMatthias Ringwald } 1990ce263fc8SMatthias Ringwald 19913c65e705SMilanka Ringwald uint8_t hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){ 19929c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1993a33eb0c4SMilanka Ringwald if (!hfp_connection) { 19943c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1995a33eb0c4SMilanka Ringwald } 1996a0ffb263SMatthias Ringwald hfp_connection->hf_send_dtmf_code = code; 19971c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 19983c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1999ce263fc8SMatthias Ringwald } 2000ce263fc8SMatthias Ringwald 20013c65e705SMilanka Ringwald uint8_t hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 20029c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2003a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20043c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2005a33eb0c4SMilanka Ringwald } 2006a0ffb263SMatthias Ringwald hfp_connection->hf_send_binp = 1; 20071c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20083c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2009ce263fc8SMatthias Ringwald } 20103deb3ec6SMatthias Ringwald 20113c65e705SMilanka Ringwald uint8_t hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){ 20129c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2013a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20143c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2015a33eb0c4SMilanka Ringwald } 2016a0ffb263SMatthias Ringwald hfp_connection->hf_send_clcc = 1; 20171c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20183c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2019667ec068SMatthias Ringwald } 2020667ec068SMatthias Ringwald 2021667ec068SMatthias Ringwald 20223c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){ 20239c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2024a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20253c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2026a33eb0c4SMilanka Ringwald } 2027a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 2028a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '?'; 20291c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20303c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2031667ec068SMatthias Ringwald } 2032667ec068SMatthias Ringwald 20333c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){ 20349c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2035a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20363c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2037a33eb0c4SMilanka Ringwald } 2038a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 2039a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '0'; 20401c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20413c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2042667ec068SMatthias Ringwald } 2043667ec068SMatthias Ringwald 20443c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){ 20459c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2046a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20473c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2048a33eb0c4SMilanka Ringwald } 2049a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 2050a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '1'; 20511c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20523c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2053667ec068SMatthias Ringwald } 2054667ec068SMatthias Ringwald 20553c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){ 20569c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2057a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20583c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2059a33eb0c4SMilanka Ringwald } 2060a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 2061a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '2'; 20621c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20633c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2064667ec068SMatthias Ringwald } 2065667ec068SMatthias Ringwald 20663c65e705SMilanka Ringwald uint8_t hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){ 20679c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2068a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20693c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2070a33eb0c4SMilanka Ringwald } 2071a0ffb263SMatthias Ringwald hfp_connection->hf_send_cnum = 1; 20721c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20733c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2074667ec068SMatthias Ringwald } 2075667ec068SMatthias Ringwald 20763c65e705SMilanka Ringwald uint8_t hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){ 20779c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2078a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20793c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2080a33eb0c4SMilanka Ringwald } 2081667ec068SMatthias Ringwald // find index for assigned number 2082667ec068SMatthias Ringwald int i; 2083aeb0f0feSMatthias Ringwald for (i = 0; i < hfp_hf_indicators_nr ; i++){ 2084aeb0f0feSMatthias Ringwald if (hfp_hf_indicators[i] == assigned_number){ 2085667ec068SMatthias Ringwald // set value 2086aeb0f0feSMatthias Ringwald hfp_hf_indicators_value[i] = value; 2087667ec068SMatthias Ringwald // mark for update 2088a0ffb263SMatthias Ringwald if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){ 2089a0ffb263SMatthias Ringwald hfp_connection->generic_status_update_bitmap |= (1<<i); 2090667ec068SMatthias Ringwald // send update 20911c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 2092a0ffb263SMatthias Ringwald } 20933c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2094667ec068SMatthias Ringwald } 2095667ec068SMatthias Ringwald } 20963c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2097667ec068SMatthias Ringwald } 2098667ec068SMatthias Ringwald 2099d7f6b5cbSMatthias Ringwald int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){ 2100d7f6b5cbSMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2101d7f6b5cbSMatthias Ringwald if (!hfp_connection) { 2102d7f6b5cbSMatthias Ringwald return 0; 2103d7f6b5cbSMatthias Ringwald } 2104d7f6b5cbSMatthias Ringwald return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE); 2105d7f6b5cbSMatthias Ringwald } 210676cc1527SMatthias Ringwald 210776cc1527SMatthias 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){ 210876cc1527SMatthias Ringwald if (!name){ 2109aeb0f0feSMatthias Ringwald name = hfp_hf_default_service_name; 211076cc1527SMatthias Ringwald } 211176cc1527SMatthias Ringwald hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name); 211276cc1527SMatthias Ringwald 211376cc1527SMatthias Ringwald // Construct SupportedFeatures for SDP bitmap: 211476cc1527SMatthias Ringwald // 211576cc1527SMatthias Ringwald // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 211676cc1527SMatthias Ringwald // of the Bits 0 to 4 of the unsolicited result code +BRSF" 211776cc1527SMatthias Ringwald // 211876cc1527SMatthias Ringwald // Wide band speech (bit 5) requires Codec negotiation 211976cc1527SMatthias Ringwald // 212076cc1527SMatthias Ringwald uint16_t sdp_features = supported_features & 0x1f; 2121ef3ae4ebSMilanka Ringwald if ( (wide_band_speech != 0) && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){ 212276cc1527SMatthias Ringwald sdp_features |= 1 << 5; 212376cc1527SMatthias Ringwald } 2124ef3ae4ebSMilanka Ringwald 2125ef3ae4ebSMilanka Ringwald if (supported_features & (1 << HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS)){ 212656f1adacSMilanka Ringwald sdp_features |= 1 << 6; 2127ef3ae4ebSMilanka Ringwald } 2128ef3ae4ebSMilanka Ringwald 2129ef3ae4ebSMilanka Ringwald if (supported_features & (1 << HFP_HFSF_VOICE_RECOGNITION_TEXT)){ 213056f1adacSMilanka Ringwald sdp_features |= 1 << 7; 2131ef3ae4ebSMilanka Ringwald } 2132ef3ae4ebSMilanka Ringwald 213376cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 213476cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 213576cc1527SMatthias Ringwald } 213676cc1527SMatthias Ringwald 213776cc1527SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){ 213868466199SMilanka Ringwald btstack_assert(callback != NULL); 213968466199SMilanka Ringwald 214076cc1527SMatthias Ringwald hfp_hf_callback = callback; 214176cc1527SMatthias Ringwald hfp_set_hf_callback(callback); 214276cc1527SMatthias Ringwald } 2143