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 115fcf4ede6SMilanka Ringwald static bool hfp_hf_vra_flag_supported(hfp_connection_t * hfp_connection){ 116fcf4ede6SMilanka Ringwald int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION); 117fcf4ede6SMilanka Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION); 118fcf4ede6SMilanka Ringwald return hf && ag; 119fcf4ede6SMilanka Ringwald } 120fcf4ede6SMilanka Ringwald 121fcf4ede6SMilanka Ringwald static bool hfp_hf_enhanced_vra_flag_supported(hfp_connection_t * hfp_connection){ 122fcf4ede6SMilanka Ringwald int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS); 123fcf4ede6SMilanka Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS); 124fcf4ede6SMilanka Ringwald return hf && ag; 125fcf4ede6SMilanka Ringwald } 12676cc1527SMatthias Ringwald 1279c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_hf_connection_context_for_acl_handle(uint16_t handle){ 1289c9c64c1SMatthias Ringwald btstack_linked_list_iterator_t it; 1299c9c64c1SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1309c9c64c1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1319c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1329c9c64c1SMatthias Ringwald if (hfp_connection->acl_handle != handle) continue; 1339c9c64c1SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_HF) continue; 1349c9c64c1SMatthias Ringwald return hfp_connection; 1359c9c64c1SMatthias Ringwald } 1369c9c64c1SMatthias Ringwald return NULL; 1379c9c64c1SMatthias Ringwald } 1389c9c64c1SMatthias Ringwald 13976cc1527SMatthias Ringwald /* emit functinos */ 1403deb3ec6SMatthias Ringwald 141a473a009SMatthias Ringwald static void hfp_hf_emit_subscriber_information(const hfp_connection_t * hfp_connection, uint8_t status){ 142a473a009SMatthias Ringwald if (hfp_hf_callback == NULL) return; 143d703d377SMatthias Ringwald uint8_t event[33]; 144a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 145a0ffb263SMatthias Ringwald event[1] = sizeof(event) - 2; 146a473a009SMatthias Ringwald event[2] = HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION; 147d703d377SMatthias Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 148d703d377SMatthias Ringwald event[5] = status; 149d703d377SMatthias Ringwald event[6] = hfp_connection->bnip_type; 150d703d377SMatthias Ringwald uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - 8); 151d703d377SMatthias Ringwald strncpy((char*)&event[7], hfp_connection->bnip_number, size); 152d703d377SMatthias Ringwald event[7 + size] = 0; 153a473a009SMatthias Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 154a0ffb263SMatthias Ringwald } 155a0ffb263SMatthias Ringwald 156a473a009SMatthias Ringwald static void hfp_hf_emit_type_and_number(const hfp_connection_t * hfp_connection, uint8_t event_subtype){ 157a473a009SMatthias Ringwald if (hfp_hf_callback == NULL) return; 158d703d377SMatthias Ringwald uint8_t event[32]; 159a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 160a0ffb263SMatthias Ringwald event[1] = sizeof(event) - 2; 161a0ffb263SMatthias Ringwald event[2] = event_subtype; 162d703d377SMatthias Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 163d703d377SMatthias Ringwald event[5] = hfp_connection->bnip_type; 164d703d377SMatthias Ringwald uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - 7); 165d703d377SMatthias Ringwald strncpy((char*)&event[6], hfp_connection->bnip_number, size); 166d703d377SMatthias Ringwald event[6 + size] = 0; 167a473a009SMatthias Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 168a0ffb263SMatthias Ringwald } 169a0ffb263SMatthias Ringwald 170a473a009SMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(const hfp_connection_t * hfp_connection){ 171a473a009SMatthias Ringwald if (hfp_hf_callback == NULL) return; 172d703d377SMatthias Ringwald uint8_t event[38]; 1730aee97efSMilanka Ringwald int pos = 0; 1740aee97efSMilanka Ringwald event[pos++] = HCI_EVENT_HFP_META; 1750aee97efSMilanka Ringwald event[pos++] = sizeof(event) - 2; 1760aee97efSMilanka Ringwald event[pos++] = HFP_SUBEVENT_ENHANCED_CALL_STATUS; 177d703d377SMatthias Ringwald little_endian_store_16(event, pos, hfp_connection->acl_handle); 178d703d377SMatthias Ringwald pos += 2; 179a473a009SMatthias Ringwald event[pos++] = hfp_connection->clcc_idx; 180a473a009SMatthias Ringwald event[pos++] = hfp_connection->clcc_dir; 181a473a009SMatthias Ringwald event[pos++] = hfp_connection->clcc_status; 182a473a009SMatthias Ringwald event[pos++] = hfp_connection->clcc_mode; 183a473a009SMatthias Ringwald event[pos++] = hfp_connection->clcc_mpty; 184a473a009SMatthias Ringwald event[pos++] = hfp_connection->bnip_type; 185d703d377SMatthias Ringwald uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - pos - 1); 186a473a009SMatthias Ringwald strncpy((char*)&event[pos], hfp_connection->bnip_number, size); 1870aee97efSMilanka Ringwald pos += size; 1880aee97efSMilanka Ringwald event[pos++] = 0; 189a473a009SMatthias Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, pos); 190a0ffb263SMatthias Ringwald } 191a0ffb263SMatthias Ringwald 1921ac1f60fSMilanka Ringwald static void hfp_emit_ag_indicator_mapping_event(const hfp_connection_t * hfp_connection, const hfp_ag_indicator_t * indicator){ 1931ac1f60fSMilanka Ringwald if (hfp_hf_callback == NULL) return; 1941ac1f60fSMilanka Ringwald uint8_t event[8+HFP_MAX_INDICATOR_DESC_SIZE+1]; 1951ac1f60fSMilanka Ringwald int pos = 0; 1961ac1f60fSMilanka Ringwald event[pos++] = HCI_EVENT_HFP_META; 1971ac1f60fSMilanka Ringwald event[pos++] = sizeof(event) - 2; 1981ac1f60fSMilanka Ringwald event[pos++] = HFP_SUBEVENT_AG_INDICATOR_MAPPING; 1991ac1f60fSMilanka Ringwald little_endian_store_16(event, pos, hfp_connection->acl_handle); 2001ac1f60fSMilanka Ringwald pos += 2; 2011ac1f60fSMilanka Ringwald event[pos++] = indicator->index; 2021ac1f60fSMilanka Ringwald event[pos++] = indicator->min_range; 2031ac1f60fSMilanka Ringwald event[pos++] = indicator->max_range; 2041ac1f60fSMilanka Ringwald strncpy((char*)&event[pos], indicator->name, HFP_MAX_INDICATOR_DESC_SIZE); 2051ac1f60fSMilanka Ringwald pos += HFP_MAX_INDICATOR_DESC_SIZE; 2061ac1f60fSMilanka Ringwald event[pos] = 0; 2071ac1f60fSMilanka Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 2081ac1f60fSMilanka Ringwald } 20976cc1527SMatthias Ringwald 210ce3797e1SMilanka Ringwald static void hfp_emit_ag_indicator_status_event(const hfp_connection_t * hfp_connection, const hfp_ag_indicator_t * indicator){ 211a473a009SMatthias Ringwald if (hfp_hf_callback == NULL) return; 212d703d377SMatthias Ringwald uint8_t event[12+HFP_MAX_INDICATOR_DESC_SIZE+1]; 21376cc1527SMatthias Ringwald int pos = 0; 21476cc1527SMatthias Ringwald event[pos++] = HCI_EVENT_HFP_META; 21576cc1527SMatthias Ringwald event[pos++] = sizeof(event) - 2; 21676cc1527SMatthias Ringwald event[pos++] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED; 217d703d377SMatthias Ringwald little_endian_store_16(event, pos, hfp_connection->acl_handle); 218d703d377SMatthias Ringwald pos += 2; 219a473a009SMatthias Ringwald event[pos++] = indicator->index; 220a473a009SMatthias Ringwald event[pos++] = indicator->status; 221a473a009SMatthias Ringwald event[pos++] = indicator->min_range; 222a473a009SMatthias Ringwald event[pos++] = indicator->max_range; 223a473a009SMatthias Ringwald event[pos++] = indicator->mandatory; 224a473a009SMatthias Ringwald event[pos++] = indicator->enabled; 225a473a009SMatthias Ringwald event[pos++] = indicator->status_changed; 226a473a009SMatthias Ringwald strncpy((char*)&event[pos], indicator->name, HFP_MAX_INDICATOR_DESC_SIZE); 22776cc1527SMatthias Ringwald pos += HFP_MAX_INDICATOR_DESC_SIZE; 22876cc1527SMatthias Ringwald event[pos] = 0; 229a473a009SMatthias Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 2303deb3ec6SMatthias Ringwald } 2313deb3ec6SMatthias Ringwald 232a473a009SMatthias Ringwald static void hfp_emit_network_operator_event(const hfp_connection_t * hfp_connection){ 233a473a009SMatthias Ringwald if (hfp_hf_callback == NULL) return; 234d703d377SMatthias Ringwald uint8_t event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE+1]; 23576cc1527SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 23676cc1527SMatthias Ringwald event[1] = sizeof(event) - 2; 23776cc1527SMatthias Ringwald event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED; 238d703d377SMatthias Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 239d703d377SMatthias Ringwald event[5] = hfp_connection->network_operator.mode; 240d703d377SMatthias Ringwald event[6] = hfp_connection->network_operator.format; 241d703d377SMatthias Ringwald strncpy((char*)&event[7], hfp_connection->network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE); 242d703d377SMatthias Ringwald event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE] = 0; 243a473a009SMatthias Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 2443deb3ec6SMatthias Ringwald } 2453deb3ec6SMatthias Ringwald 246b95cac54SMilanka Ringwald 247b95cac54SMilanka Ringwald static void hfp_hf_emit_enhanced_voice_recognition_text(hfp_connection_t * hfp_connection){ 248b95cac54SMilanka Ringwald btstack_assert(hfp_connection != NULL); 24951aa5d5aSMilanka Ringwald uint8_t event[HFP_MAX_VR_TEXT_SIZE + 11]; 250b95cac54SMilanka Ringwald int pos = 0; 251b95cac54SMilanka Ringwald event[pos++] = HCI_EVENT_HFP_META; 252b95cac54SMilanka Ringwald event[pos++] = sizeof(event) - 2; 253b95cac54SMilanka Ringwald event[pos++] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_MESSAGE; 254b95cac54SMilanka Ringwald little_endian_store_16(event, pos, hfp_connection->acl_handle); 255b95cac54SMilanka Ringwald pos += 2; 256b95cac54SMilanka Ringwald little_endian_store_16(event, pos, hfp_connection->ag_msg.text_id); 257b95cac54SMilanka Ringwald pos += 2; 258b95cac54SMilanka Ringwald event[pos++] = hfp_connection->ag_msg.text_type; 259e83f1be7SMilanka Ringwald event[pos++] = hfp_connection->ag_msg.text_operation; 260b95cac54SMilanka Ringwald 26151aa5d5aSMilanka Ringwald // length, zero ending is already in message 262b95cac54SMilanka Ringwald uint8_t * value = &hfp_connection->line_buffer[0]; 26351aa5d5aSMilanka Ringwald uint16_t value_length = hfp_connection->ag_vra_msg_length; 26451aa5d5aSMilanka Ringwald 26551aa5d5aSMilanka Ringwald little_endian_store_16(event, pos, value_length); 266b95cac54SMilanka Ringwald pos += 2; 26751aa5d5aSMilanka Ringwald memcpy(&event[pos], value, value_length); 26851aa5d5aSMilanka Ringwald pos += value_length; 26951aa5d5aSMilanka Ringwald 270b95cac54SMilanka Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, pos); 271b95cac54SMilanka Ringwald } 272b95cac54SMilanka Ringwald 27376cc1527SMatthias Ringwald /* send commands */ 27489425bfcSMilanka Ringwald 27589425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){ 2763deb3ec6SMatthias Ringwald char buffer[20]; 2771599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s\r", cmd); 27889425bfcSMilanka Ringwald return send_str_over_rfcomm(cid, buffer); 27989425bfcSMilanka Ringwald } 28089425bfcSMilanka Ringwald 28189425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){ 28289425bfcSMilanka Ringwald char buffer[20]; 2831599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s%s\r", cmd, mark); 28489425bfcSMilanka Ringwald return send_str_over_rfcomm(cid, buffer); 28589425bfcSMilanka Ringwald } 28689425bfcSMilanka Ringwald 28786da9d74SMatthias Ringwald static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint16_t value){ 28889425bfcSMilanka Ringwald char buffer[40]; 2891599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s=%d\r", cmd, value); 2903deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2913deb3ec6SMatthias Ringwald } 2923deb3ec6SMatthias Ringwald 2933deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){ 2943deb3ec6SMatthias Ringwald char buffer[30]; 29589425bfcSMilanka Ringwald const int size = sizeof(buffer); 29689425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS); 297aeb0f0feSMatthias Ringwald offset += join(buffer+offset, size-offset, hfp_hf_codecs, hfp_hf_codecs_nr); 2981599fe57SMatthias Ringwald offset += snprintf(buffer+offset, size-offset, "\r"); 2993deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 3003deb3ec6SMatthias Ringwald } 3013deb3ec6SMatthias Ringwald 3023deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){ 3033deb3ec6SMatthias Ringwald char buffer[50]; 30489425bfcSMilanka Ringwald const int size = sizeof(buffer); 30589425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS); 30689425bfcSMilanka Ringwald offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr); 3071599fe57SMatthias Ringwald offset += snprintf(buffer+offset, size-offset, "\r"); 3083deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 3093deb3ec6SMatthias Ringwald } 3103deb3ec6SMatthias Ringwald 3113deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){ 3123deb3ec6SMatthias Ringwald char buffer[30]; 31389425bfcSMilanka Ringwald const int size = sizeof(buffer); 31489425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR); 315aeb0f0feSMatthias Ringwald offset += join(buffer+offset, size-offset, hfp_hf_indicators, hfp_hf_indicators_nr); 3161599fe57SMatthias Ringwald offset += snprintf(buffer+offset, size-offset, "\r"); 3173deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 3183deb3ec6SMatthias Ringwald } 3193deb3ec6SMatthias Ringwald 32089425bfcSMilanka Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){ 3213deb3ec6SMatthias Ringwald char buffer[20]; 3221599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate); 323ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 324ce263fc8SMatthias Ringwald } 325ce263fc8SMatthias Ringwald 326ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){ 327ce263fc8SMatthias Ringwald char buffer[40]; 328aeb0f0feSMatthias Ringwald snprintf(buffer, sizeof(buffer), "%s%s;\r", HFP_CALL_PHONE_NUMBER, hfp_hf_phone_number); 329ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 330ce263fc8SMatthias Ringwald } 331ce263fc8SMatthias Ringwald 332a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){ 333ce263fc8SMatthias Ringwald char buffer[40]; 3341599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "%s>%d;\r", HFP_CALL_PHONE_NUMBER, memory_id); 335ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 336ce263fc8SMatthias Ringwald } 337ce263fc8SMatthias Ringwald 338f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){ 33989425bfcSMilanka Ringwald char buffer[40]; 3401599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s=%u\r", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number); 341ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 342ce263fc8SMatthias Ringwald } 343ce263fc8SMatthias Ringwald 344ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){ 345ce263fc8SMatthias Ringwald char buffer[20]; 3461599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s=%c\r", HFP_TRANSMIT_DTMF_CODES, code); 347ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 348ce263fc8SMatthias Ringwald } 349ce263fc8SMatthias Ringwald 35097d2cadbSMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){ 3511599fe57SMatthias Ringwald return send_str_over_rfcomm(cid, (char *) "ATA\r"); 35297d2cadbSMatthias Ringwald } 35397d2cadbSMatthias Ringwald 35489425bfcSMilanka Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){ 355aeb0f0feSMatthias Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_hf_supported_features); 35689425bfcSMilanka Ringwald } 35789425bfcSMilanka Ringwald 35889425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){ 35989425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?"); 36089425bfcSMilanka Ringwald } 36189425bfcSMilanka Ringwald 36289425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){ 36389425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?"); 36489425bfcSMilanka Ringwald } 36589425bfcSMilanka Ringwald 36689425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){ 36789425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?"); 36889425bfcSMilanka Ringwald } 36989425bfcSMilanka Ringwald 37089425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){ 37189425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?"); 37289425bfcSMilanka Ringwald } 37389425bfcSMilanka Ringwald 37489425bfcSMilanka Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){ 37589425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?"); 37689425bfcSMilanka Ringwald } 37789425bfcSMilanka Ringwald 37889425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){ 37989425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0"); 38089425bfcSMilanka Ringwald } 38189425bfcSMilanka Ringwald 38289425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){ 38389425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?"); 38489425bfcSMilanka Ringwald } 38589425bfcSMilanka Ringwald 38689425bfcSMilanka Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){ 38789425bfcSMilanka Ringwald return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP); 38889425bfcSMilanka Ringwald } 38989425bfcSMilanka Ringwald 39089425bfcSMilanka Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){ 39189425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain); 39289425bfcSMilanka Ringwald } 39389425bfcSMilanka Ringwald 39489425bfcSMilanka Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){ 39589425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain); 39689425bfcSMilanka Ringwald } 39789425bfcSMilanka Ringwald 39889425bfcSMilanka Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){ 39989425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate); 40089425bfcSMilanka Ringwald } 40189425bfcSMilanka Ringwald 40289425bfcSMilanka Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){ 40389425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate); 40489425bfcSMilanka Ringwald } 40589425bfcSMilanka Ringwald 40689425bfcSMilanka Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){ 40789425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate); 40889425bfcSMilanka Ringwald } 40989425bfcSMilanka Ringwald 41089425bfcSMilanka Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){ 41189425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec); 41289425bfcSMilanka Ringwald } 41389425bfcSMilanka Ringwald 41489425bfcSMilanka Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){ 41589425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable); 41689425bfcSMilanka Ringwald } 41789425bfcSMilanka Ringwald 41889425bfcSMilanka Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){ 41989425bfcSMilanka Ringwald return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER); 42089425bfcSMilanka Ringwald } 42189425bfcSMilanka Ringwald 42289425bfcSMilanka Ringwald static int hfp_hf_send_chup(uint16_t cid){ 42389425bfcSMilanka Ringwald return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL); 42489425bfcSMilanka Ringwald } 42589425bfcSMilanka Ringwald 426ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){ 42789425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1"); 428ce263fc8SMatthias Ringwald } 429ce263fc8SMatthias Ringwald 430667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){ 43189425bfcSMilanka Ringwald return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS); 432667ec068SMatthias Ringwald } 433667ec068SMatthias Ringwald 43476cc1527SMatthias Ringwald /* state machines */ 4353deb3ec6SMatthias Ringwald 436a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){ 437a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 438a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 439aa4dd815SMatthias Ringwald int done = 1; 440498a8121SMilanka Ringwald log_info("hfp_hf_run_for_context_service_level_connection state %d\n", hfp_connection->state); 441a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 4423deb3ec6SMatthias Ringwald case HFP_EXCHANGE_SUPPORTED_FEATURES: 443aeb0f0feSMatthias Ringwald hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_hf_codecs, &hfp_hf_codecs_nr); 444a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES; 445a0ffb263SMatthias Ringwald hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid); 4463deb3ec6SMatthias Ringwald break; 4473deb3ec6SMatthias Ringwald case HFP_NOTIFY_ON_CODECS: 448a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS; 449a0ffb263SMatthias Ringwald hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 4503deb3ec6SMatthias Ringwald break; 4513deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INDICATORS: 452a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 453a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid); 4543deb3ec6SMatthias Ringwald break; 4553deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INDICATORS_STATUS: 456a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS; 457a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid); 4583deb3ec6SMatthias Ringwald break; 4593deb3ec6SMatthias Ringwald case HFP_ENABLE_INDICATORS_STATUS_UPDATE: 460a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE; 461a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1); 4623deb3ec6SMatthias Ringwald break; 4633deb3ec6SMatthias Ringwald case HFP_RETRIEVE_CAN_HOLD_CALL: 464a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL; 465a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid); 4663deb3ec6SMatthias Ringwald break; 4673deb3ec6SMatthias Ringwald case HFP_LIST_GENERIC_STATUS_INDICATORS: 468a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 469a0ffb263SMatthias Ringwald hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4703deb3ec6SMatthias Ringwald break; 4713deb3ec6SMatthias Ringwald case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS: 472a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS; 473a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4743deb3ec6SMatthias Ringwald break; 4753deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 476a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 477a0ffb263SMatthias Ringwald hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4783deb3ec6SMatthias Ringwald break; 4793deb3ec6SMatthias Ringwald default: 480aa4dd815SMatthias Ringwald done = 0; 4813deb3ec6SMatthias Ringwald break; 4823deb3ec6SMatthias Ringwald } 4833deb3ec6SMatthias Ringwald return done; 4843deb3ec6SMatthias Ringwald } 4853deb3ec6SMatthias Ringwald 486ce263fc8SMatthias Ringwald 487a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){ 488a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 489498a8121SMilanka Ringwald if (hfp_connection->ok_pending){ 490498a8121SMilanka Ringwald return 0; 491498a8121SMilanka Ringwald } 492ce263fc8SMatthias Ringwald int done = 0; 493a0ffb263SMatthias Ringwald if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 494a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 495ce263fc8SMatthias Ringwald done = 1; 496a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators); 497ce263fc8SMatthias Ringwald return done; 498ce263fc8SMatthias Ringwald }; 499a0ffb263SMatthias Ringwald if (hfp_connection->change_status_update_for_individual_ag_indicators){ 500a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 501ce263fc8SMatthias Ringwald done = 1; 502a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid, 503a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap, 504a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr); 505ce263fc8SMatthias Ringwald return done; 506ce263fc8SMatthias Ringwald } 507ce263fc8SMatthias Ringwald 508a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 509ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_SET_FORMAT: 510a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK; 511a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 512a0ffb263SMatthias Ringwald hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid); 513ce263fc8SMatthias Ringwald return 1; 514ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_SEND_QUERY: 515a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT; 516a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 517a0ffb263SMatthias Ringwald hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid); 518ce263fc8SMatthias Ringwald return 1; 519ce263fc8SMatthias Ringwald default: 520ce263fc8SMatthias Ringwald break; 521ce263fc8SMatthias Ringwald } 522ce263fc8SMatthias Ringwald 523a0ffb263SMatthias Ringwald if (hfp_connection->enable_extended_audio_gateway_error_report){ 524a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 525ce263fc8SMatthias Ringwald done = 1; 526a0ffb263SMatthias Ringwald hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report); 527ce263fc8SMatthias Ringwald return done; 528ce263fc8SMatthias Ringwald } 529ce263fc8SMatthias Ringwald 530ce263fc8SMatthias Ringwald return done; 531ce263fc8SMatthias Ringwald } 532ce263fc8SMatthias Ringwald 533af97579eSMilanka Ringwald static int hfp_hf_voice_recognition_state_machine(hfp_connection_t * hfp_connection){ 534be55a11dSMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) { 535be55a11dSMilanka Ringwald return 0; 536be55a11dSMilanka Ringwald } 537be55a11dSMilanka Ringwald int done = 0; 538fd4151d1SMilanka Ringwald 5390b4debbfSMilanka Ringwald if (hfp_connection->ok_pending == 1){ 5400b4debbfSMilanka Ringwald return 0; 5410b4debbfSMilanka Ringwald } 5420b4debbfSMilanka Ringwald // voice recognition activated from AG 5430b4debbfSMilanka Ringwald if (hfp_connection->command == HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION){ 5440b4debbfSMilanka Ringwald switch(hfp_connection->vra_state_requested){ 5450b4debbfSMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED: 5460b4debbfSMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_OFF: 547de9e0ea7SMilanka Ringwald case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 5480b4debbfSMilanka Ringwald // ignore AG command, continue to wait for OK 5490b4debbfSMilanka Ringwald return 0; 550cf75be85SMilanka Ringwald 5510b4debbfSMilanka Ringwald default: 552b95cac54SMilanka Ringwald if (hfp_connection->ag_vra_msg_length > 0){ 553b95cac54SMilanka Ringwald hfp_hf_emit_enhanced_voice_recognition_text(hfp_connection); 554b95cac54SMilanka Ringwald hfp_connection->ag_vra_msg_length = 0; 555b95cac54SMilanka Ringwald break; 556b95cac54SMilanka Ringwald } 557cf75be85SMilanka Ringwald switch(hfp_connection->ag_vra_state){ 558cf75be85SMilanka Ringwald case HFP_VOICE_RECOGNITION_STATE_AG_READY: 559013cc750SMilanka Ringwald switch (hfp_connection->ag_vra_status){ 560013cc750SMilanka Ringwald case 0: 5610b4debbfSMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF; 562013cc750SMilanka Ringwald break; 563013cc750SMilanka Ringwald case 1: 564013cc750SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED; 565013cc750SMilanka Ringwald break; 566013cc750SMilanka Ringwald case 2: 567013cc750SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 568013cc750SMilanka Ringwald break; 569013cc750SMilanka Ringwald default: 570013cc750SMilanka Ringwald break; 5710b4debbfSMilanka Ringwald } 5720b4debbfSMilanka Ringwald break; 573cf75be85SMilanka Ringwald default: 574cf75be85SMilanka Ringwald // state messages from AG 575cf75be85SMilanka Ringwald hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS); 5768d5005b5SMilanka Ringwald hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY; 577cf75be85SMilanka Ringwald break; 578cf75be85SMilanka Ringwald } 579cf75be85SMilanka Ringwald break; 5800b4debbfSMilanka Ringwald } 5810b4debbfSMilanka Ringwald hfp_connection->command = HFP_CMD_NONE; 5820b4debbfSMilanka Ringwald } 5830b4debbfSMilanka Ringwald 5840b4debbfSMilanka Ringwald 585498a8121SMilanka Ringwald switch (hfp_connection->vra_state_requested){ 586fdda66c0SMilanka Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 587fdda66c0SMilanka Ringwald done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0); 588fdda66c0SMilanka Ringwald if (done != 0){ 589fdda66c0SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF; 590498a8121SMilanka Ringwald hfp_connection->ok_pending = 1; 591498a8121SMilanka Ringwald } 592fd4151d1SMilanka Ringwald return 1; 593fd4151d1SMilanka Ringwald 594fd4151d1SMilanka Ringwald 595fdda66c0SMilanka Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED: 596fdda66c0SMilanka Ringwald done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1); 597fdda66c0SMilanka Ringwald if (done != 0){ 598fdda66c0SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED; 599fd4151d1SMilanka Ringwald hfp_connection->ok_pending = 1; 600fd4151d1SMilanka Ringwald return 1; 6010b4debbfSMilanka Ringwald } 6020b4debbfSMilanka Ringwald break; 603013cc750SMilanka Ringwald 604de9e0ea7SMilanka Ringwald case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 605de9e0ea7SMilanka Ringwald done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 2); 606de9e0ea7SMilanka Ringwald if (done != 0){ 607de9e0ea7SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 608de9e0ea7SMilanka Ringwald hfp_connection->ok_pending = 1; 609de9e0ea7SMilanka Ringwald return 1; 610de9e0ea7SMilanka Ringwald } 611de9e0ea7SMilanka Ringwald break; 612de9e0ea7SMilanka Ringwald 613de9e0ea7SMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_OFF: 614de9e0ea7SMilanka Ringwald hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF; 615de9e0ea7SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 616de9e0ea7SMilanka Ringwald hfp_connection->activate_voice_recognition = false; 617de9e0ea7SMilanka Ringwald if (hfp_connection->activate_voice_recognition){ 618fcf4ede6SMilanka Ringwald hfp_connection->enhanced_voice_recognition_enabled = hfp_hf_enhanced_vra_flag_supported(hfp_connection); 619de9e0ea7SMilanka Ringwald hfp_hf_activate_voice_recognition(hfp_connection->acl_handle); 620de9e0ea7SMilanka Ringwald } else { 621553a4a56SMilanka Ringwald hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS); 622de9e0ea7SMilanka Ringwald } 623de9e0ea7SMilanka Ringwald break; 624de9e0ea7SMilanka Ringwald 625be55a11dSMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED: 626498a8121SMilanka Ringwald hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_ACTIVATED; 627498a8121SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 628de9e0ea7SMilanka Ringwald hfp_connection->activate_voice_recognition = false; 629de9e0ea7SMilanka Ringwald if (hfp_connection->deactivate_voice_recognition){ 630de9e0ea7SMilanka Ringwald hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle); 631de9e0ea7SMilanka Ringwald } else { 632fcf4ede6SMilanka Ringwald hfp_connection->enhanced_voice_recognition_enabled = hfp_hf_enhanced_vra_flag_supported(hfp_connection); 633*84fb9ac1SMilanka Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 634553a4a56SMilanka Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS); 635*84fb9ac1SMilanka Ringwald } else { 636*84fb9ac1SMilanka Ringwald // postpone VRA event to simplify application logic 637*84fb9ac1SMilanka Ringwald hfp_connection->emit_vra_enabled_after_audio_established = true; 638*84fb9ac1SMilanka Ringwald } 639de9e0ea7SMilanka Ringwald } 640be55a11dSMilanka Ringwald break; 641be55a11dSMilanka Ringwald 642de9e0ea7SMilanka Ringwald 643013cc750SMilanka Ringwald case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 644013cc750SMilanka Ringwald hfp_connection->vra_state = HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 645498a8121SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 646de9e0ea7SMilanka Ringwald hfp_connection->activate_voice_recognition = false; 647de9e0ea7SMilanka Ringwald if (hfp_connection->deactivate_voice_recognition){ 648de9e0ea7SMilanka Ringwald hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle); 649de9e0ea7SMilanka Ringwald } else { 650de9e0ea7SMilanka Ringwald hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_SUCCESS); 651de9e0ea7SMilanka Ringwald } 652be55a11dSMilanka Ringwald break; 653fd4151d1SMilanka Ringwald 654be55a11dSMilanka Ringwald default: 655be55a11dSMilanka Ringwald break; 656be55a11dSMilanka Ringwald } 657be55a11dSMilanka Ringwald return done; 658be55a11dSMilanka Ringwald } 659be55a11dSMilanka Ringwald 660be55a11dSMilanka Ringwald 661be55a11dSMilanka Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){ 662a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 663ce263fc8SMatthias Ringwald 664332ca98fSMatthias Ringwald if (hfp_connection->trigger_codec_exchange){ 665332ca98fSMatthias Ringwald hfp_connection->trigger_codec_exchange = 0; 666ce263fc8SMatthias Ringwald 667a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 668a0ffb263SMatthias Ringwald hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid); 669332ca98fSMatthias Ringwald return 1; 670332ca98fSMatthias Ringwald } 671332ca98fSMatthias Ringwald 6721cc65c4fSMatthias Ringwald if (hfp_connection->hf_send_codec_confirm){ 6731cc65c4fSMatthias Ringwald hfp_connection->hf_send_codec_confirm = false; 674ce263fc8SMatthias Ringwald 675a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 676fcb08cdbSMilanka Ringwald hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed); 6771cc65c4fSMatthias Ringwald return 1; 6781cc65c4fSMatthias Ringwald } 6791cc65c4fSMatthias Ringwald 6801cc65c4fSMatthias Ringwald if (hfp_connection->hf_send_supported_codecs){ 6811cc65c4fSMatthias Ringwald hfp_connection->hf_send_supported_codecs = false; 6821cc65c4fSMatthias Ringwald 683a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 684a0ffb263SMatthias Ringwald hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 6851cc65c4fSMatthias Ringwald return 1; 6861cc65c4fSMatthias Ringwald } 687ce263fc8SMatthias Ringwald 688ce263fc8SMatthias Ringwald return 0; 689ce263fc8SMatthias Ringwald } 690ce263fc8SMatthias Ringwald 691a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){ 692505f1c30SMatthias Ringwald if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) || 693505f1c30SMatthias Ringwald (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0; 694ce263fc8SMatthias Ringwald 69564f19dedSMilanka Ringwald if (hfp_connection->release_audio_connection){ 696a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 697a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 0; 698a0ffb263SMatthias Ringwald gap_disconnect(hfp_connection->sco_handle); 699ce263fc8SMatthias Ringwald return 1; 700ce263fc8SMatthias Ringwald } 701ce263fc8SMatthias Ringwald 702a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 703ce263fc8SMatthias Ringwald 704ce263fc8SMatthias Ringwald // run codecs exchange 705a0ffb263SMatthias Ringwald int done = codecs_exchange_state_machine(hfp_connection); 706ce263fc8SMatthias Ringwald if (done) return 1; 707ce263fc8SMatthias Ringwald 70838200c1dSMilanka Ringwald if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0; 70938200c1dSMilanka Ringwald if (hfp_connection->establish_audio_connection){ 71038200c1dSMilanka Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 71138200c1dSMilanka Ringwald hfp_connection->establish_audio_connection = 0; 71238200c1dSMilanka Ringwald hfp_setup_synchronous_connection(hfp_connection); 71338200c1dSMilanka Ringwald return 1; 71438200c1dSMilanka Ringwald } 715ce263fc8SMatthias Ringwald return 0; 716ce263fc8SMatthias Ringwald } 717ce263fc8SMatthias Ringwald 71838200c1dSMilanka Ringwald 719a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 720eaf2b0a1SMatthias Ringwald 721eaf2b0a1SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 722eaf2b0a1SMatthias Ringwald 723a0ffb263SMatthias Ringwald if (hfp_connection->hf_answer_incoming_call){ 724a0ffb263SMatthias Ringwald hfp_hf_cmd_ata(hfp_connection->rfcomm_cid); 725a0ffb263SMatthias Ringwald hfp_connection->hf_answer_incoming_call = 0; 726ce263fc8SMatthias Ringwald return 1; 727ce263fc8SMatthias Ringwald } 728ce263fc8SMatthias Ringwald return 0; 729ce263fc8SMatthias Ringwald } 730ce263fc8SMatthias Ringwald 7311c6a0fc0SMatthias Ringwald static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){ 7327522e673SMatthias Ringwald 73376cc1527SMatthias Ringwald btstack_assert(hfp_connection != NULL); 73476cc1527SMatthias Ringwald btstack_assert(hfp_connection->local_role == HFP_ROLE_HF); 73576cc1527SMatthias Ringwald 73676cc1527SMatthias Ringwald // during SDP query, RFCOMM CID is not set 73776cc1527SMatthias Ringwald if (hfp_connection->rfcomm_cid == 0) return; 73822387625SMatthias Ringwald 739*84fb9ac1SMilanka Ringwald // emit postponed VRA event 740*84fb9ac1SMilanka Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->emit_vra_enabled_after_audio_established){ 741*84fb9ac1SMilanka Ringwald hfp_connection->emit_vra_enabled_after_audio_established = false; 742*84fb9ac1SMilanka Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS); 743*84fb9ac1SMilanka Ringwald } 744*84fb9ac1SMilanka Ringwald 7453721a235SMatthias Ringwald // assert command could be sent 7463721a235SMatthias Ringwald if (hci_can_send_command_packet_now() == 0) return; 7473721a235SMatthias Ringwald 7483721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP 7493721a235SMatthias Ringwald // WBS Disassociate 7503721a235SMatthias Ringwald if (hfp_connection->cc256x_send_wbs_disassociate){ 7513721a235SMatthias Ringwald hfp_connection->cc256x_send_wbs_disassociate = false; 7523721a235SMatthias Ringwald hci_send_cmd(&hci_ti_wbs_disassociate); 7533721a235SMatthias Ringwald return; 7543721a235SMatthias Ringwald } 7553721a235SMatthias Ringwald // Write Codec Config 7563721a235SMatthias Ringwald if (hfp_connection->cc256x_send_write_codec_config){ 7573721a235SMatthias Ringwald hfp_connection->cc256x_send_write_codec_config = false; 7583721a235SMatthias Ringwald hfp_cc256x_write_codec_config(hfp_connection); 7593721a235SMatthias Ringwald return; 7603721a235SMatthias Ringwald } 7613721a235SMatthias Ringwald // WBS Associate 7623721a235SMatthias Ringwald if (hfp_connection->cc256x_send_wbs_associate){ 7633721a235SMatthias Ringwald hfp_connection->cc256x_send_wbs_associate = false; 7643721a235SMatthias Ringwald hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle); 7653721a235SMatthias Ringwald return; 7663721a235SMatthias Ringwald } 7673721a235SMatthias Ringwald #endif 768689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 769689d4323SMatthias Ringwald // Enable WBS 770689d4323SMatthias Ringwald if (hfp_connection->bcm_send_enable_wbs){ 771689d4323SMatthias Ringwald hfp_connection->bcm_send_enable_wbs = false; 772689d4323SMatthias Ringwald hci_send_cmd(&hci_bcm_enable_wbs, 1, 2); 773689d4323SMatthias Ringwald return; 774689d4323SMatthias Ringwald } 775689d4323SMatthias Ringwald // Write I2S/PCM params 776689d4323SMatthias Ringwald if (hfp_connection->bcm_send_write_i2spcm_interface_param){ 777689d4323SMatthias Ringwald hfp_connection->bcm_send_write_i2spcm_interface_param = false; 778689d4323SMatthias Ringwald hfp_bcm_write_i2spcm_interface_param(hfp_connection); 779689d4323SMatthias Ringwald return; 780689d4323SMatthias Ringwald } 781689d4323SMatthias Ringwald // Disable WBS 782689d4323SMatthias Ringwald if (hfp_connection->bcm_send_disable_wbs){ 783689d4323SMatthias Ringwald hfp_connection->bcm_send_disable_wbs = false; 784689d4323SMatthias Ringwald hci_send_cmd(&hci_bcm_enable_wbs, 0, 2); 785689d4323SMatthias Ringwald return; 786689d4323SMatthias Ringwald } 787689d4323SMatthias Ringwald #endif 78848e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS) 78948e6eeeeSMatthias Ringwald if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){ 79048e6eeeeSMatthias Ringwald hfp_finalize_connection_context(hfp_connection); 79148e6eeeeSMatthias Ringwald return; 79248e6eeeeSMatthias Ringwald } 79348e6eeeeSMatthias Ringwald #endif 7943721a235SMatthias Ringwald 795cb81d35dSMatthias Ringwald if (hfp_connection->accept_sco){ 796cb81d35dSMatthias Ringwald bool incoming_eSCO = hfp_connection->accept_sco == 2; 797cb81d35dSMatthias Ringwald hfp_connection->accept_sco = 0; 7987522e673SMatthias Ringwald // notify about codec selection if not done already 7997522e673SMatthias Ringwald if (hfp_connection->negotiated_codec == 0){ 8007522e673SMatthias Ringwald hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 8017522e673SMatthias Ringwald } 802cb81d35dSMatthias Ringwald hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO); 8037522e673SMatthias Ringwald return; 8047522e673SMatthias Ringwald } 8057522e673SMatthias Ringwald 806d4dd47ffSMatthias Ringwald if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) { 807d4dd47ffSMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 808d4dd47ffSMatthias Ringwald return; 809d4dd47ffSMatthias Ringwald } 810a0ffb263SMatthias Ringwald int done = hfp_hf_run_for_context_service_level_connection(hfp_connection); 811ce263fc8SMatthias Ringwald if (!done){ 812a0ffb263SMatthias Ringwald done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection); 813ce263fc8SMatthias Ringwald } 814ce263fc8SMatthias Ringwald if (!done){ 815c95b5b3cSMilanka Ringwald done = hfp_hf_run_for_audio_connection(hfp_connection); 816be55a11dSMilanka Ringwald } 817be55a11dSMilanka Ringwald if (!done){ 818c95b5b3cSMilanka Ringwald done = hfp_hf_voice_recognition_state_machine(hfp_connection); 819ce263fc8SMatthias Ringwald } 820ce263fc8SMatthias Ringwald if (!done){ 821a0ffb263SMatthias Ringwald done = call_setup_state_machine(hfp_connection); 822ce263fc8SMatthias Ringwald } 823ce263fc8SMatthias Ringwald 824e2c3b58dSMilanka Ringwald // don't send a new command while ok still pending or SLC is not established 825e2c3b58dSMilanka Ringwald if (hfp_connection->ok_pending || (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED)){ 826e2c3b58dSMilanka Ringwald return; 827e2c3b58dSMilanka Ringwald } 8281016a228SMatthias Ringwald 829a0ffb263SMatthias Ringwald if (hfp_connection->send_microphone_gain){ 830a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 0; 831a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 832a0ffb263SMatthias Ringwald hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 833ce263fc8SMatthias Ringwald return; 834ce263fc8SMatthias Ringwald } 835ce263fc8SMatthias Ringwald 836a0ffb263SMatthias Ringwald if (hfp_connection->send_speaker_gain){ 837a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 0; 838a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 839a0ffb263SMatthias Ringwald hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 840ce263fc8SMatthias Ringwald return; 841ce263fc8SMatthias Ringwald } 842ce263fc8SMatthias Ringwald 843a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_calling_line_notification){ 844a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_calling_line_notification = 0; 845a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 846a0ffb263SMatthias Ringwald hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0); 847ce263fc8SMatthias Ringwald return; 848ce263fc8SMatthias Ringwald } 849ce263fc8SMatthias Ringwald 850a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_calling_line_notification){ 851a0ffb263SMatthias Ringwald hfp_connection->hf_activate_calling_line_notification = 0; 852a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 853a0ffb263SMatthias Ringwald hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1); 854ce263fc8SMatthias Ringwald return; 855ce263fc8SMatthias Ringwald } 856ce263fc8SMatthias Ringwald 857a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){ 858a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0; 85999af1e28SMilanka Ringwald hfp_connection->response_pending_for_command = HFP_CMD_TURN_OFF_EC_AND_NR; 860a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 861e2c3b58dSMilanka Ringwald hfp_hf_send_cmd_with_int(hfp_connection->rfcomm_cid, HFP_TURN_OFF_EC_AND_NR, 0); 862ce263fc8SMatthias Ringwald return; 863ce263fc8SMatthias Ringwald } 864ce263fc8SMatthias Ringwald 865a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_call_waiting_notification){ 866a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_call_waiting_notification = 0; 867a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 868a0ffb263SMatthias Ringwald hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0); 869ce263fc8SMatthias Ringwald return; 870ce263fc8SMatthias Ringwald } 871ce263fc8SMatthias Ringwald 872a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_call_waiting_notification){ 873a0ffb263SMatthias Ringwald hfp_connection->hf_activate_call_waiting_notification = 0; 874a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 875a0ffb263SMatthias Ringwald hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1); 876ce263fc8SMatthias Ringwald return; 877ce263fc8SMatthias Ringwald } 878ce263fc8SMatthias Ringwald 879a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_outgoing_call){ 880a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_outgoing_call = 0; 881a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 882a0ffb263SMatthias Ringwald hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid); 883ce263fc8SMatthias Ringwald return; 884ce263fc8SMatthias Ringwald } 885ce263fc8SMatthias Ringwald 886a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_memory_dialing){ 887a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_memory_dialing = 0; 888a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 889a0ffb263SMatthias Ringwald hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id); 890ce263fc8SMatthias Ringwald return; 891ce263fc8SMatthias Ringwald } 892ce263fc8SMatthias Ringwald 893a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_redial_last_number){ 894a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_redial_last_number = 0; 895a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 896a0ffb263SMatthias Ringwald hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid); 897ce263fc8SMatthias Ringwald return; 898ce263fc8SMatthias Ringwald } 899ce263fc8SMatthias Ringwald 900a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chup){ 901a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 0; 902a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 903a0ffb263SMatthias Ringwald hfp_hf_send_chup(hfp_connection->rfcomm_cid); 904ce263fc8SMatthias Ringwald return; 905ce263fc8SMatthias Ringwald } 906ce263fc8SMatthias Ringwald 907a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_0){ 908a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_0 = 0; 909a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 910a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0); 911ce263fc8SMatthias Ringwald return; 912ce263fc8SMatthias Ringwald } 913ce263fc8SMatthias Ringwald 914a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_1){ 915a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_1 = 0; 916a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 917a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1); 918ce263fc8SMatthias Ringwald return; 919ce263fc8SMatthias Ringwald } 920ce263fc8SMatthias Ringwald 921a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_2){ 922a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_2 = 0; 923a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 924a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2); 925ce263fc8SMatthias Ringwald return; 926ce263fc8SMatthias Ringwald } 927ce263fc8SMatthias Ringwald 928a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_3){ 929a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_3 = 0; 930a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 931a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3); 932ce263fc8SMatthias Ringwald return; 933ce263fc8SMatthias Ringwald } 934ce263fc8SMatthias Ringwald 935a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_4){ 936a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_4 = 0; 937a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 938a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4); 939ce263fc8SMatthias Ringwald return; 940ce263fc8SMatthias Ringwald } 941ce263fc8SMatthias Ringwald 942a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_x){ 943a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 0; 944a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 945a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index); 946667ec068SMatthias Ringwald return; 947667ec068SMatthias Ringwald } 948667ec068SMatthias Ringwald 949a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_dtmf_code){ 950a0ffb263SMatthias Ringwald char code = hfp_connection->hf_send_dtmf_code; 951a0ffb263SMatthias Ringwald hfp_connection->hf_send_dtmf_code = 0; 952a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 953a0ffb263SMatthias Ringwald hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code); 954ce263fc8SMatthias Ringwald return; 955ce263fc8SMatthias Ringwald } 956ce263fc8SMatthias Ringwald 957a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_binp){ 958a0ffb263SMatthias Ringwald hfp_connection->hf_send_binp = 0; 959a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 960a0ffb263SMatthias Ringwald hfp_hf_send_binp(hfp_connection->rfcomm_cid); 961ce263fc8SMatthias Ringwald return; 962ce263fc8SMatthias Ringwald } 963ce263fc8SMatthias Ringwald 964a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_clcc){ 965a0ffb263SMatthias Ringwald hfp_connection->hf_send_clcc = 0; 966a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 967a0ffb263SMatthias Ringwald hfp_hf_send_clcc(hfp_connection->rfcomm_cid); 968667ec068SMatthias Ringwald return; 969667ec068SMatthias Ringwald } 970667ec068SMatthias Ringwald 971a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_rrh){ 972a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 0; 973667ec068SMatthias Ringwald char buffer[20]; 974a0ffb263SMatthias Ringwald switch (hfp_connection->hf_send_rrh_command){ 975667ec068SMatthias Ringwald case '?': 9761599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s?\r", 977ff7d6aeaSMatthias Ringwald HFP_RESPONSE_AND_HOLD); 978ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 979a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 980667ec068SMatthias Ringwald return; 981667ec068SMatthias Ringwald case '0': 982667ec068SMatthias Ringwald case '1': 983667ec068SMatthias Ringwald case '2': 9841599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s=%c\r", 985ff7d6aeaSMatthias Ringwald HFP_RESPONSE_AND_HOLD, 986ff7d6aeaSMatthias Ringwald hfp_connection->hf_send_rrh_command); 987ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 988a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 989667ec068SMatthias Ringwald return; 990667ec068SMatthias Ringwald default: 991667ec068SMatthias Ringwald break; 992667ec068SMatthias Ringwald } 993667ec068SMatthias Ringwald return; 994667ec068SMatthias Ringwald } 995667ec068SMatthias Ringwald 996a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_cnum){ 997a0ffb263SMatthias Ringwald hfp_connection->hf_send_cnum = 0; 998667ec068SMatthias Ringwald char buffer[20]; 9991599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s\r", 1000ff7d6aeaSMatthias Ringwald HFP_SUBSCRIBER_NUMBER_INFORMATION); 1001ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 1002a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 1003667ec068SMatthias Ringwald return; 1004667ec068SMatthias Ringwald } 1005667ec068SMatthias Ringwald 1006667ec068SMatthias Ringwald // update HF indicators 1007a0ffb263SMatthias Ringwald if (hfp_connection->generic_status_update_bitmap){ 1008667ec068SMatthias Ringwald int i; 1009aeb0f0feSMatthias Ringwald for (i=0; i < hfp_hf_indicators_nr; i++){ 1010a0ffb263SMatthias Ringwald if (get_bit(hfp_connection->generic_status_update_bitmap, i)){ 1011a0ffb263SMatthias Ringwald if (hfp_connection->generic_status_indicators[i].state){ 1012a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1013a0ffb263SMatthias Ringwald hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0); 1014667ec068SMatthias Ringwald char buffer[30]; 10151599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s=%u,%u\r", 1016ff7d6aeaSMatthias Ringwald HFP_TRANSFER_HF_INDICATOR_STATUS, 1017aeb0f0feSMatthias Ringwald hfp_hf_indicators[i], 1018aeb0f0feSMatthias Ringwald (unsigned int)hfp_hf_indicators_value[i]); 1019ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 1020a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 1021667ec068SMatthias Ringwald } else { 1022aeb0f0feSMatthias Ringwald log_info("Not sending HF indicator %u as it is disabled", hfp_hf_indicators[i]); 1023667ec068SMatthias Ringwald } 1024667ec068SMatthias Ringwald return; 1025667ec068SMatthias Ringwald } 1026667ec068SMatthias Ringwald } 1027667ec068SMatthias Ringwald } 1028667ec068SMatthias Ringwald 1029ce263fc8SMatthias Ringwald if (done) return; 1030ce263fc8SMatthias Ringwald // deal with disconnect 1031a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 1032ce263fc8SMatthias Ringwald case HFP_W2_DISCONNECT_RFCOMM: 1033a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 1034a0ffb263SMatthias Ringwald rfcomm_disconnect(hfp_connection->rfcomm_cid); 1035ce263fc8SMatthias Ringwald break; 1036ce263fc8SMatthias Ringwald 1037ce263fc8SMatthias Ringwald default: 1038ce263fc8SMatthias Ringwald break; 1039ce263fc8SMatthias Ringwald } 1040ce263fc8SMatthias Ringwald } 1041ce263fc8SMatthias Ringwald 1042a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){ 1043a0ffb263SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 10446a7f44bdSMilanka Ringwald 1045ca59be51SMatthias Ringwald hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr); 10467522e673SMatthias Ringwald 1047184a03edSMilanka Ringwald uint8_t i; 1048184a03edSMilanka Ringwald for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 10491ac1f60fSMilanka Ringwald hfp_emit_ag_indicator_mapping_event(hfp_connection, &hfp_connection->ag_indicators[i]); 1050184a03edSMilanka Ringwald } 1051667ec068SMatthias Ringwald // restore volume settings 1052a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = hfp_hf_speaker_gain; 1053a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 1054ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain); 1055a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = hfp_hf_microphone_gain; 1056a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 1057ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain); 1058667ec068SMatthias Ringwald // enable all indicators 1059aeb0f0feSMatthias Ringwald for (i=0; i < hfp_hf_indicators_nr; i++){ 1060aeb0f0feSMatthias Ringwald hfp_connection->generic_status_indicators[i].uuid = hfp_hf_indicators[i]; 1061a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[i].state = 1; 1062667ec068SMatthias Ringwald } 1063ce263fc8SMatthias Ringwald } 1064ce263fc8SMatthias Ringwald 10651cc65c4fSMatthias Ringwald static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){ 1066aeb0f0feSMatthias Ringwald if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_hf_codecs_nr, hfp_hf_codecs)){ 10671cc65c4fSMatthias Ringwald // Codec supported, confirm 10681cc65c4fSMatthias Ringwald hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 10691cc65c4fSMatthias Ringwald hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 10701cc65c4fSMatthias Ringwald log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD"); 10711cc65c4fSMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC; 10721cc65c4fSMatthias Ringwald 10731cc65c4fSMatthias Ringwald hfp_connection->hf_send_codec_confirm = true; 10741cc65c4fSMatthias Ringwald } else { 10751cc65c4fSMatthias Ringwald // Codec not supported, send supported codecs 10761cc65c4fSMatthias Ringwald hfp_connection->codec_confirmed = 0; 10771cc65c4fSMatthias Ringwald hfp_connection->suggested_codec = 0; 10781cc65c4fSMatthias Ringwald hfp_connection->negotiated_codec = 0; 10791cc65c4fSMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 10801cc65c4fSMatthias Ringwald 10811cc65c4fSMatthias Ringwald hfp_connection->hf_send_supported_codecs = true; 10821cc65c4fSMatthias Ringwald } 10831cc65c4fSMatthias Ringwald } 10841cc65c4fSMatthias Ringwald 1085e2c3b58dSMilanka Ringwald static bool hfp_hf_switch_on_ok_pending(hfp_connection_t *hfp_connection, uint8_t status){ 1086e2c3b58dSMilanka Ringwald bool event_emited = true; 1087e2c3b58dSMilanka Ringwald 108899af1e28SMilanka Ringwald switch (hfp_connection->response_pending_for_command){ 1089e2c3b58dSMilanka Ringwald case HFP_CMD_TURN_OFF_EC_AND_NR: 1090e2c3b58dSMilanka Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE, status); 1091e2c3b58dSMilanka Ringwald break; 1092e2c3b58dSMilanka Ringwald default: 1093e2c3b58dSMilanka Ringwald event_emited = false; 1094e2c3b58dSMilanka Ringwald 1095a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 10963deb3ec6SMatthias Ringwald case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 1097a0ffb263SMatthias Ringwald if (has_codec_negotiation_feature(hfp_connection)){ 1098a0ffb263SMatthias Ringwald hfp_connection->state = HFP_NOTIFY_ON_CODECS; 10993deb3ec6SMatthias Ringwald break; 11003deb3ec6SMatthias Ringwald } 1101a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 11023deb3ec6SMatthias Ringwald break; 11033deb3ec6SMatthias Ringwald 11043deb3ec6SMatthias Ringwald case HFP_W4_NOTIFY_ON_CODECS: 1105a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 11063deb3ec6SMatthias Ringwald break; 11073deb3ec6SMatthias Ringwald 11083deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS: 1109a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS; 11103deb3ec6SMatthias Ringwald break; 11113deb3ec6SMatthias Ringwald 11123deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS_STATUS: 1113a0ffb263SMatthias Ringwald hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE; 11143deb3ec6SMatthias Ringwald break; 11153deb3ec6SMatthias Ringwald 11163deb3ec6SMatthias Ringwald case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE: 1117a0ffb263SMatthias Ringwald if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 1118a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL; 11193deb3ec6SMatthias Ringwald break; 11203deb3ec6SMatthias Ringwald } 1121a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 1122a0ffb263SMatthias Ringwald hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 11233deb3ec6SMatthias Ringwald break; 11243deb3ec6SMatthias Ringwald } 1125a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 11263deb3ec6SMatthias Ringwald break; 11273deb3ec6SMatthias Ringwald 11283deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_CAN_HOLD_CALL: 1129a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 1130a0ffb263SMatthias Ringwald hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 11313deb3ec6SMatthias Ringwald break; 11323deb3ec6SMatthias Ringwald } 1133a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 11343deb3ec6SMatthias Ringwald break; 11353deb3ec6SMatthias Ringwald 11363deb3ec6SMatthias Ringwald case HFP_W4_LIST_GENERIC_STATUS_INDICATORS: 1137a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS; 11383deb3ec6SMatthias Ringwald break; 11393deb3ec6SMatthias Ringwald 11403deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS: 1141a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 11423deb3ec6SMatthias Ringwald break; 11433deb3ec6SMatthias Ringwald 11443deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 1145a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 11463deb3ec6SMatthias Ringwald break; 1147ce263fc8SMatthias Ringwald case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 1148a0ffb263SMatthias Ringwald if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 1149a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 1150ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 1151ce263fc8SMatthias Ringwald break; 1152ce263fc8SMatthias Ringwald } 11533deb3ec6SMatthias Ringwald 1154a0ffb263SMatthias Ringwald if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){ 1155a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 0; 1156ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 1157ce263fc8SMatthias Ringwald break; 11583deb3ec6SMatthias Ringwald } 11593deb3ec6SMatthias Ringwald 1160a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 1161ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK: 1162a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1163ce263fc8SMatthias Ringwald break; 1164ce263fc8SMatthias Ringwald case HPF_HF_QUERY_OPERATOR_W4_RESULT: 1165a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET; 1166a473a009SMatthias Ringwald hfp_emit_network_operator_event(hfp_connection); 1167ce263fc8SMatthias Ringwald break; 1168ce263fc8SMatthias Ringwald default: 1169ce263fc8SMatthias Ringwald break; 11703deb3ec6SMatthias Ringwald } 1171ce263fc8SMatthias Ringwald 1172a0ffb263SMatthias Ringwald if (hfp_connection->enable_extended_audio_gateway_error_report){ 1173a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = 0; 1174ce263fc8SMatthias Ringwald break; 11753deb3ec6SMatthias Ringwald } 11763deb3ec6SMatthias Ringwald 1177a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 1178aa4dd815SMatthias Ringwald case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 1179a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 11803deb3ec6SMatthias Ringwald break; 1181ce263fc8SMatthias Ringwald case HFP_CODECS_HF_CONFIRMED_CODEC: 1182a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 1183ce263fc8SMatthias Ringwald break; 11843deb3ec6SMatthias Ringwald default: 11853deb3ec6SMatthias Ringwald break; 11863deb3ec6SMatthias Ringwald } 1187af97579eSMilanka Ringwald hfp_hf_voice_recognition_state_machine(hfp_connection); 1188be55a11dSMilanka Ringwald break; 1189be55a11dSMilanka Ringwald case HFP_AUDIO_CONNECTION_ESTABLISHED: 1190af97579eSMilanka Ringwald hfp_hf_voice_recognition_state_machine(hfp_connection); 11913deb3ec6SMatthias Ringwald break; 11923deb3ec6SMatthias Ringwald default: 11933deb3ec6SMatthias Ringwald break; 11943deb3ec6SMatthias Ringwald } 1195e2c3b58dSMilanka Ringwald break; 1196e2c3b58dSMilanka Ringwald } 11973deb3ec6SMatthias Ringwald 11983deb3ec6SMatthias Ringwald // done 119999af1e28SMilanka Ringwald hfp_connection->response_pending_for_command = HFP_CMD_NONE; 1200be55a11dSMilanka Ringwald hfp_connection->ok_pending = 0; 1201a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1202e2c3b58dSMilanka Ringwald return event_emited; 12033deb3ec6SMatthias Ringwald } 12043deb3ec6SMatthias Ringwald 1205a03dbc20SMilanka Ringwald static bool hfp_is_ringing(hfp_callsetup_status_t callsetup_status){ 1206a03dbc20SMilanka Ringwald switch (callsetup_status){ 1207a03dbc20SMilanka Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1208a03dbc20SMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1209a03dbc20SMilanka Ringwald return true; 1210a03dbc20SMilanka Ringwald default: 1211a03dbc20SMilanka Ringwald return false; 1212a03dbc20SMilanka Ringwald } 1213a03dbc20SMilanka Ringwald } 1214be55a11dSMilanka Ringwald 1215b08371a9SMilanka Ringwald static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) { 12164562e2a2SMatthias Ringwald uint16_t i; 1217a03dbc20SMilanka Ringwald 12184562e2a2SMatthias Ringwald for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 12194562e2a2SMatthias Ringwald if (hfp_connection->ag_indicators[i].status_changed) { 12204562e2a2SMatthias Ringwald if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){ 1221a03dbc20SMilanka Ringwald hfp_callsetup_status_t new_hf_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status; 1222a03dbc20SMilanka Ringwald bool ringing_old = hfp_is_ringing(hfp_hf_callsetup_status); 1223a03dbc20SMilanka Ringwald bool ringing_new = hfp_is_ringing(new_hf_callsetup_status); 1224a03dbc20SMilanka Ringwald if (ringing_old != ringing_new){ 1225a03dbc20SMilanka Ringwald if (ringing_new){ 1226a03dbc20SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGING); 1227a03dbc20SMilanka Ringwald } else { 1228a03dbc20SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGING); 1229a03dbc20SMilanka Ringwald } 1230a03dbc20SMilanka Ringwald } 1231a03dbc20SMilanka Ringwald hfp_hf_callsetup_status = new_hf_callsetup_status; 12324562e2a2SMatthias Ringwald } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){ 1233aeb0f0feSMatthias Ringwald hfp_hf_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status; 12344562e2a2SMatthias Ringwald // avoid set but not used warning 1235aeb0f0feSMatthias Ringwald (void) hfp_hf_callheld_status; 12364562e2a2SMatthias Ringwald } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){ 1237674ebed5SMilanka Ringwald hfp_call_status_t new_hf_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status; 1238674ebed5SMilanka Ringwald if (hfp_hf_call_status != new_hf_call_status){ 1239674ebed5SMilanka Ringwald if (new_hf_call_status == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS){ 1240674ebed5SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED); 1241674ebed5SMilanka Ringwald } else { 1242674ebed5SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED); 1243674ebed5SMilanka Ringwald } 1244674ebed5SMilanka Ringwald } 1245674ebed5SMilanka Ringwald hfp_hf_call_status = new_hf_call_status; 12464562e2a2SMatthias Ringwald } 12474562e2a2SMatthias Ringwald hfp_connection->ag_indicators[i].status_changed = 0; 1248ce3797e1SMilanka Ringwald hfp_emit_ag_indicator_status_event(hfp_connection, &hfp_connection->ag_indicators[i]); 12494562e2a2SMatthias Ringwald break; 12504562e2a2SMatthias Ringwald } 12514562e2a2SMatthias Ringwald } 12524562e2a2SMatthias Ringwald } 12534562e2a2SMatthias Ringwald 1254426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){ 1255186dd3d2SMatthias Ringwald int value; 1256186dd3d2SMatthias Ringwald int i; 1257e2c3b58dSMilanka Ringwald bool event_emited; 1258e2c3b58dSMilanka Ringwald 1259a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1260667ec068SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1261a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1262a473a009SMatthias Ringwald hfp_hf_emit_subscriber_information(hfp_connection, 0); 1263667ec068SMatthias Ringwald break; 1264667ec068SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_STATUS: 1265a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1266ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0])); 1267667ec068SMatthias Ringwald break; 1268667ec068SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 1269a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1270a473a009SMatthias Ringwald hfp_hf_emit_enhanced_call_status(hfp_connection); 1271667ec068SMatthias Ringwald break; 1272ce263fc8SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 1273a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 12742308e108SMilanka Ringwald value = btstack_atoi((char*)hfp_connection->line_buffer); 1275667ec068SMatthias Ringwald hfp_hf_speaker_gain = value; 1276ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value); 1277ce263fc8SMatthias Ringwald break; 1278ce263fc8SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 1279a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 12802308e108SMilanka Ringwald value = btstack_atoi((char*)hfp_connection->line_buffer); 1281667ec068SMatthias Ringwald hfp_hf_microphone_gain = value; 1282ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value); 1283ce263fc8SMatthias Ringwald break; 1284ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 1285a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1286ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number); 1287a0ffb263SMatthias Ringwald break; 1288a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1289a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1290a473a009SMatthias Ringwald hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION); 1291a0ffb263SMatthias Ringwald break; 1292a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1293a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1294a473a009SMatthias Ringwald hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION); 1295ce263fc8SMatthias Ringwald break; 1296ce263fc8SMatthias Ringwald case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1297a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 12985a4785c8SMatthias Ringwald hfp_connection->ok_pending = 0; 1299a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 1300ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value); 1301ce263fc8SMatthias Ringwald break; 13020b4debbfSMilanka Ringwald case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION: 13030b4debbfSMilanka Ringwald break; 1304fdda66c0SMilanka Ringwald case HFP_CMD_ERROR: 130590244c92SMilanka Ringwald switch (hfp_connection->state){ 130690244c92SMilanka Ringwald case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 130790244c92SMilanka Ringwald switch (hfp_connection->codecs_state){ 130890244c92SMilanka Ringwald case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 1309fdda66c0SMilanka Ringwald hfp_reset_context_flags(hfp_connection); 131090244c92SMilanka Ringwald hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec); 131190244c92SMilanka Ringwald return; 131290244c92SMilanka Ringwald default: 131390244c92SMilanka Ringwald break; 131490244c92SMilanka Ringwald } 131556f1adacSMilanka Ringwald break; 131656f1adacSMilanka Ringwald default: 131756f1adacSMilanka Ringwald break; 131856f1adacSMilanka Ringwald } 1319e2c3b58dSMilanka Ringwald 1320fdda66c0SMilanka Ringwald // handle error response for voice activation (HF initiated) 13210b4debbfSMilanka Ringwald switch(hfp_connection->vra_state_requested){ 1322de9e0ea7SMilanka Ringwald case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1323de9e0ea7SMilanka Ringwald hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 1324be55a11dSMilanka Ringwald break; 1325be55a11dSMilanka Ringwald default: 1326e2c3b58dSMilanka Ringwald if (hfp_connection->vra_state_requested == hfp_connection->vra_state){ 1327e2c3b58dSMilanka Ringwald break; 1328e2c3b58dSMilanka Ringwald } 13290b4debbfSMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 1330553a4a56SMilanka Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 13310b4debbfSMilanka Ringwald hfp_reset_context_flags(hfp_connection); 13320b4debbfSMilanka Ringwald return; 1333be55a11dSMilanka Ringwald } 1334e2c3b58dSMilanka Ringwald event_emited = hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 1335e2c3b58dSMilanka Ringwald if (!event_emited){ 13360b4debbfSMilanka Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1); 1337e2c3b58dSMilanka Ringwald } 1338fdda66c0SMilanka Ringwald hfp_reset_context_flags(hfp_connection); 1339ce263fc8SMatthias Ringwald break; 1340fdda66c0SMilanka Ringwald 1341ce263fc8SMatthias Ringwald case HFP_CMD_OK: 1342e2c3b58dSMilanka Ringwald hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_SUCCESS); 1343ce263fc8SMatthias Ringwald break; 1344ce263fc8SMatthias Ringwald case HFP_CMD_RING: 13455a4785c8SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1346ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING); 1347ce263fc8SMatthias Ringwald break; 1348ce263fc8SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 13495a4785c8SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 13504562e2a2SMatthias Ringwald hfp_hf_handle_transfer_ag_indicator_status(hfp_connection); 1351ce263fc8SMatthias Ringwald break; 1352c741b032SMilanka Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 13535a4785c8SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1354184a03edSMilanka Ringwald // report status after SLC established 1355184a03edSMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){ 1356184a03edSMilanka Ringwald break; 1357184a03edSMilanka Ringwald } 1358c741b032SMilanka Ringwald for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 13591ac1f60fSMilanka Ringwald hfp_emit_ag_indicator_mapping_event(hfp_connection, &hfp_connection->ag_indicators[i]); 1360c741b032SMilanka Ringwald } 1361c741b032SMilanka Ringwald break; 13621cc65c4fSMatthias Ringwald case HFP_CMD_AG_SUGGESTED_CODEC: 13631cc65c4fSMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 13645a4785c8SMatthias Ringwald hfp_hf_handle_suggested_codec(hfp_connection); 13651cc65c4fSMatthias Ringwald break; 1366eac56539SMilanka Ringwald case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING: 1367eac56539SMilanka 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)); 1368ce263fc8SMatthias Ringwald default: 1369ce263fc8SMatthias Ringwald break; 13703deb3ec6SMatthias Ringwald } 13710cef86faSMatthias Ringwald } 1372426f9988SMatthias Ringwald 137376cc1527SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){ 137476cc1527SMatthias Ringwald return (byte == '\n') || (byte == '\r'); 137576cc1527SMatthias Ringwald } 137676cc1527SMatthias Ringwald 13770b4debbfSMilanka Ringwald static void hfp_hf_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){ 1378426f9988SMatthias Ringwald // assertion: size >= 1 as rfcomm.c does not deliver empty packets 1379426f9988SMatthias Ringwald if (size < 1) return; 1380426f9988SMatthias Ringwald 1381426f9988SMatthias Ringwald hfp_log_rfcomm_message("HFP_HF_RX", packet, size); 1382e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES 1383e43d1938SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet); 1384e43d1938SMatthias Ringwald #endif 1385426f9988SMatthias Ringwald 1386426f9988SMatthias Ringwald // process messages byte-wise 1387a7ba78b0SMilanka Ringwald uint8_t pos; 1388426f9988SMatthias Ringwald for (pos = 0; pos < size; pos++){ 1389426f9988SMatthias Ringwald hfp_parse(hfp_connection, packet[pos], 1); 13901599fe57SMatthias Ringwald // parse until end of line "\r" or "\n" 1391426f9988SMatthias Ringwald if (!hfp_parser_is_end_of_line(packet[pos])) continue; 13920b4debbfSMilanka Ringwald hfp_hf_handle_rfcomm_command(hfp_connection); 13933deb3ec6SMatthias Ringwald } 1394a7ba78b0SMilanka Ringwald } 13953deb3ec6SMatthias Ringwald 13961c6a0fc0SMatthias Ringwald static void hfp_hf_run(void){ 1397665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1398665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1399665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1400a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 140122387625SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_HF) continue; 14021c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 14033deb3ec6SMatthias Ringwald } 14043deb3ec6SMatthias Ringwald } 14053deb3ec6SMatthias Ringwald 14061c6a0fc0SMatthias Ringwald static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 14070b4debbfSMilanka Ringwald hfp_connection_t * hfp_connection; 14083deb3ec6SMatthias Ringwald switch (packet_type){ 14093deb3ec6SMatthias Ringwald case RFCOMM_DATA_PACKET: 14100b4debbfSMilanka Ringwald hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 14110b4debbfSMilanka Ringwald if (!hfp_connection) return; 14120b4debbfSMilanka Ringwald hfp_hf_handle_rfcomm_data(hfp_connection, packet, size); 14130b4debbfSMilanka Ringwald hfp_hf_run_for_context(hfp_connection); 14140b4debbfSMilanka Ringwald return; 14153deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 1416d4dd47ffSMatthias Ringwald if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 1417d4dd47ffSMatthias Ringwald uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 14181c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid)); 1419d4dd47ffSMatthias Ringwald return; 1420d4dd47ffSMatthias Ringwald } 142127950165SMatthias Ringwald hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF); 1422202c8a4cSMatthias Ringwald break; 14233deb3ec6SMatthias Ringwald default: 14243deb3ec6SMatthias Ringwald break; 14253deb3ec6SMatthias Ringwald } 14261c6a0fc0SMatthias Ringwald hfp_hf_run(); 14273deb3ec6SMatthias Ringwald } 14283deb3ec6SMatthias Ringwald 14291c6a0fc0SMatthias Ringwald static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1430405014fbSMatthias Ringwald hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF); 14311c6a0fc0SMatthias Ringwald hfp_hf_run(); 1432405014fbSMatthias Ringwald } 1433405014fbSMatthias Ringwald 1434aeb0f0feSMatthias Ringwald static void hfp_hf_set_defaults(void){ 1435aeb0f0feSMatthias Ringwald hfp_hf_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 1436aeb0f0feSMatthias Ringwald hfp_hf_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS; 1437aeb0f0feSMatthias Ringwald hfp_hf_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1438aeb0f0feSMatthias Ringwald hfp_hf_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD; 1439aeb0f0feSMatthias Ringwald hfp_hf_codecs_nr = 0; 1440aeb0f0feSMatthias Ringwald hfp_hf_speaker_gain = 9; 1441aeb0f0feSMatthias Ringwald hfp_hf_microphone_gain = 9; 1442aeb0f0feSMatthias Ringwald hfp_hf_indicators_nr = 0; 1443aeb0f0feSMatthias Ringwald } 1444aeb0f0feSMatthias Ringwald 1445b4df8028SMilanka Ringwald uint8_t hfp_hf_init(uint16_t rfcomm_channel_nr){ 1446b4df8028SMilanka Ringwald uint8_t status = rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 1447b4df8028SMilanka Ringwald if (status != ERROR_CODE_SUCCESS){ 1448b4df8028SMilanka Ringwald return status; 1449b4df8028SMilanka Ringwald } 1450b4df8028SMilanka Ringwald 1451520c92d5SMatthias Ringwald hfp_init(); 1452aeb0f0feSMatthias Ringwald hfp_hf_set_defaults(); 1453d63c37a1SMatthias Ringwald 14541c6a0fc0SMatthias Ringwald hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler; 14551c6a0fc0SMatthias Ringwald hci_add_event_handler(&hfp_hf_hci_event_callback_registration); 145627950165SMatthias Ringwald 145727950165SMatthias Ringwald // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us 14581c6a0fc0SMatthias Ringwald hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler); 1459b4df8028SMilanka Ringwald return ERROR_CODE_SUCCESS; 146020b2edb6SMatthias Ringwald } 146127950165SMatthias Ringwald 146220b2edb6SMatthias Ringwald void hfp_hf_deinit(void){ 146320b2edb6SMatthias Ringwald hfp_deinit(); 1464aeb0f0feSMatthias Ringwald hfp_hf_set_defaults(); 1465aeb0f0feSMatthias Ringwald 1466aeb0f0feSMatthias Ringwald hfp_hf_callback = NULL; 146720b2edb6SMatthias Ringwald (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t)); 1468aeb0f0feSMatthias Ringwald (void) memset(hfp_hf_phone_number, 0, sizeof(hfp_hf_phone_number)); 1469a0ffb263SMatthias Ringwald } 1470a0ffb263SMatthias Ringwald 14717ca89cabSMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, const uint8_t * codecs){ 147268466199SMilanka Ringwald btstack_assert(codecs_nr < HFP_MAX_NUM_CODECS); 14733deb3ec6SMatthias Ringwald 1474aeb0f0feSMatthias Ringwald hfp_hf_codecs_nr = codecs_nr; 14753deb3ec6SMatthias Ringwald int i; 14763deb3ec6SMatthias Ringwald for (i=0; i<codecs_nr; i++){ 1477aeb0f0feSMatthias Ringwald hfp_hf_codecs[i] = codecs[i]; 14783deb3ec6SMatthias Ringwald } 14793deb3ec6SMatthias Ringwald } 14803deb3ec6SMatthias Ringwald 1481a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){ 1482aeb0f0feSMatthias Ringwald hfp_hf_supported_features = supported_features; 1483a0ffb263SMatthias Ringwald } 14843deb3ec6SMatthias Ringwald 14857ca89cabSMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){ 1486aeb0f0feSMatthias Ringwald btstack_assert(hfp_hf_indicators_nr < HFP_MAX_NUM_INDICATORS); 148768466199SMilanka Ringwald 1488aeb0f0feSMatthias Ringwald hfp_hf_indicators_nr = indicators_nr; 14893deb3ec6SMatthias Ringwald int i; 1490aeb0f0feSMatthias Ringwald for (i = 0; i < hfp_hf_indicators_nr ; i++){ 1491aeb0f0feSMatthias Ringwald hfp_hf_indicators[i] = indicators[i]; 14923deb3ec6SMatthias Ringwald } 14933deb3ec6SMatthias Ringwald } 14943deb3ec6SMatthias Ringwald 14954eb3f1d8SMilanka Ringwald uint8_t hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){ 14964eb3f1d8SMilanka Ringwald return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF); 14973deb3ec6SMatthias Ringwald } 14983deb3ec6SMatthias Ringwald 1499657bc59fSMilanka Ringwald uint8_t hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){ 15009c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1501a33eb0c4SMilanka Ringwald if (!hfp_connection){ 1502657bc59fSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1503a33eb0c4SMilanka Ringwald } 15041ffa0dd9SMilanka Ringwald hfp_trigger_release_service_level_connection(hfp_connection); 15051c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1506657bc59fSMilanka Ringwald return ERROR_CODE_SUCCESS; 15073deb3ec6SMatthias Ringwald } 15083deb3ec6SMatthias Ringwald 15093c65e705SMilanka Ringwald static uint8_t hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){ 15109c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1511a0ffb263SMatthias Ringwald if (!hfp_connection) { 15123c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 15133deb3ec6SMatthias Ringwald } 1514a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = enable; 15151c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 15163c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 15173deb3ec6SMatthias Ringwald } 15183deb3ec6SMatthias Ringwald 15193c65e705SMilanka Ringwald uint8_t hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 15203c65e705SMilanka Ringwald return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1); 1521ce263fc8SMatthias Ringwald } 1522ce263fc8SMatthias Ringwald 15233c65e705SMilanka Ringwald uint8_t hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 15243c65e705SMilanka Ringwald return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0); 1525ce263fc8SMatthias Ringwald } 1526ce263fc8SMatthias Ringwald 15273deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format 15283c65e705SMilanka Ringwald uint8_t hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){ 15299c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1530a0ffb263SMatthias Ringwald if (!hfp_connection) { 15313c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 15323deb3ec6SMatthias Ringwald } 1533a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 1; 1534a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap; 15351c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 15363c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 15373deb3ec6SMatthias Ringwald } 15383deb3ec6SMatthias Ringwald 15393c65e705SMilanka Ringwald uint8_t hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){ 15409c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1541a0ffb263SMatthias Ringwald if (!hfp_connection) { 15423c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 15433deb3ec6SMatthias Ringwald } 15443c65e705SMilanka Ringwald 1545a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 1546ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET: 1547a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT; 1548ce263fc8SMatthias Ringwald break; 1549ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_FORMAT_SET: 1550a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1551ce263fc8SMatthias Ringwald break; 1552ce263fc8SMatthias Ringwald default: 15533c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1554ce263fc8SMatthias Ringwald } 15551c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 15563c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 15573deb3ec6SMatthias Ringwald } 15583deb3ec6SMatthias Ringwald 15593c65e705SMilanka Ringwald static uint8_t hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){ 15609c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1561a0ffb263SMatthias Ringwald if (!hfp_connection) { 15623c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 15633deb3ec6SMatthias Ringwald } 1564a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = enable; 15651c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 15663c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 15673deb3ec6SMatthias Ringwald } 15683deb3ec6SMatthias Ringwald 1569ce263fc8SMatthias Ringwald 15703c65e705SMilanka Ringwald uint8_t hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 15713c65e705SMilanka Ringwald return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1); 1572ce263fc8SMatthias Ringwald } 1573ce263fc8SMatthias Ringwald 15743c65e705SMilanka Ringwald uint8_t hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 15753c65e705SMilanka Ringwald return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0); 1576ce263fc8SMatthias Ringwald } 1577ce263fc8SMatthias Ringwald 157838200c1dSMilanka Ringwald static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){ 1579aeb0f0feSMatthias Ringwald return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) && (hfp_hf_supported_features & (1 << HFP_HFSF_ESCO_S4)); 158038200c1dSMilanka Ringwald } 1581ce263fc8SMatthias Ringwald 15823c65e705SMilanka Ringwald uint8_t hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){ 15839c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1584a33eb0c4SMilanka Ringwald if (!hfp_connection) { 15853c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1586a33eb0c4SMilanka Ringwald } 1587ce263fc8SMatthias Ringwald 15883c65e705SMilanka Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 15893c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 15903c65e705SMilanka Ringwald } 15913c65e705SMilanka Ringwald 15923c65e705SMilanka Ringwald if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){ 15933c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 15943c65e705SMilanka Ringwald } 1595f4412093SMatthias Ringwald if (has_codec_negotiation_feature(hfp_connection)) { 1596a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state) { 1597aa4dd815SMatthias Ringwald case HFP_CODECS_W4_AG_COMMON_CODEC: 1598aa4dd815SMatthias Ringwald break; 1599ec3bfc1aSMatthias Ringwald case HFP_CODECS_EXCHANGED: 1600ec3bfc1aSMatthias Ringwald hfp_connection->trigger_codec_exchange = 1; 1601ec3bfc1aSMatthias Ringwald break; 1602aa4dd815SMatthias Ringwald default: 16031cc65c4fSMatthias Ringwald hfp_connection->codec_confirmed = 0; 16041cc65c4fSMatthias Ringwald hfp_connection->suggested_codec = 0; 16051cc65c4fSMatthias Ringwald hfp_connection->negotiated_codec = 0; 16061cc65c4fSMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 160738200c1dSMilanka Ringwald hfp_connection->trigger_codec_exchange = 1; 1608aa4dd815SMatthias Ringwald break; 16093deb3ec6SMatthias Ringwald } 1610f4412093SMatthias Ringwald } else { 1611f4412093SMatthias Ringwald log_info("no codec negotiation feature, use CVSD"); 1612f4412093SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 1613f4412093SMatthias Ringwald hfp_connection->suggested_codec = HFP_CODEC_CVSD; 1614f4412093SMatthias Ringwald hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 1615f4412093SMatthias Ringwald hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 1616f4412093SMatthias Ringwald hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection)); 1617f4412093SMatthias Ringwald hfp_connection->establish_audio_connection = 1; 1618f4412093SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 1619ce263fc8SMatthias Ringwald } 1620ce263fc8SMatthias Ringwald 16211c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 16223c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 16233deb3ec6SMatthias Ringwald } 16243deb3ec6SMatthias Ringwald 16253c65e705SMilanka Ringwald uint8_t hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){ 16269c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1627a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16283c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1629a33eb0c4SMilanka Ringwald } 16300b4debbfSMilanka Ringwald if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){ 16310b4debbfSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 16320b4debbfSMilanka Ringwald } 16330b4debbfSMilanka Ringwald uint8_t status = hfp_trigger_release_audio_connection(hfp_connection); 16340b4debbfSMilanka Ringwald if (status == ERROR_CODE_SUCCESS){ 16351c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 16360b4debbfSMilanka Ringwald } 16373c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 16383deb3ec6SMatthias Ringwald } 16393deb3ec6SMatthias Ringwald 16403c65e705SMilanka Ringwald uint8_t hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){ 16419c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1642a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16433c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1644a33eb0c4SMilanka Ringwald } 1645ce263fc8SMatthias Ringwald 1646aeb0f0feSMatthias Ringwald if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1647a0ffb263SMatthias Ringwald hfp_connection->hf_answer_incoming_call = 1; 16481c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1649ce263fc8SMatthias Ringwald } else { 1650aeb0f0feSMatthias Ringwald log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_hf_callsetup_status); 16513c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1652ce263fc8SMatthias Ringwald } 16533c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1654ce263fc8SMatthias Ringwald } 1655ce263fc8SMatthias Ringwald 16563c65e705SMilanka Ringwald uint8_t hfp_hf_terminate_call(hci_con_handle_t acl_handle){ 16579c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1658a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16593c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1660a33eb0c4SMilanka Ringwald } 1661a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 1; 16621c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 16633c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1664ce263fc8SMatthias Ringwald } 1665ce263fc8SMatthias Ringwald 16663c65e705SMilanka Ringwald uint8_t hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){ 16679c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1668a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16693c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1670a33eb0c4SMilanka Ringwald } 1671ce263fc8SMatthias Ringwald 1672aeb0f0feSMatthias Ringwald if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1673a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 1; 16741c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1675ce263fc8SMatthias Ringwald } 16763c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1677ce263fc8SMatthias Ringwald } 1678ce263fc8SMatthias Ringwald 16793c65e705SMilanka Ringwald uint8_t hfp_hf_user_busy(hci_con_handle_t acl_handle){ 16809c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1681a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16823c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1683a33eb0c4SMilanka Ringwald } 1684ce263fc8SMatthias Ringwald 1685aeb0f0feSMatthias Ringwald if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1686a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_0 = 1; 16871c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1688ce263fc8SMatthias Ringwald } 16893c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1690ce263fc8SMatthias Ringwald } 1691ce263fc8SMatthias Ringwald 16923c65e705SMilanka Ringwald uint8_t hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){ 16939c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1694a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16953c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1696a33eb0c4SMilanka Ringwald } 1697ce263fc8SMatthias Ringwald 1698aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1699aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1700a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_1 = 1; 17011c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1702ce263fc8SMatthias Ringwald } 17033c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1704ce263fc8SMatthias Ringwald } 1705ce263fc8SMatthias Ringwald 17063c65e705SMilanka Ringwald uint8_t hfp_hf_swap_calls(hci_con_handle_t acl_handle){ 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 } 1711ce263fc8SMatthias 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_2 = 1; 17151c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1716ce263fc8SMatthias Ringwald } 17173c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1718ce263fc8SMatthias Ringwald } 1719ce263fc8SMatthias Ringwald 17203c65e705SMilanka Ringwald uint8_t hfp_hf_join_held_call(hci_con_handle_t acl_handle){ 17219c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1722a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17233c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1724a33eb0c4SMilanka Ringwald } 1725ce263fc8SMatthias Ringwald 1726aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1727aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1728a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_3 = 1; 17291c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1730ce263fc8SMatthias Ringwald } 17313c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1732ce263fc8SMatthias Ringwald } 1733ce263fc8SMatthias Ringwald 17343c65e705SMilanka Ringwald uint8_t hfp_hf_connect_calls(hci_con_handle_t acl_handle){ 17359c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1736a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17373c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1738a33eb0c4SMilanka Ringwald } 1739ce263fc8SMatthias Ringwald 1740aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1741aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1742a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_4 = 1; 17431c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1744ce263fc8SMatthias Ringwald } 17453c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1746ce263fc8SMatthias Ringwald } 1747ce263fc8SMatthias Ringwald 17483c65e705SMilanka Ringwald uint8_t hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){ 17499c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1750a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17513c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1752a33eb0c4SMilanka Ringwald } 1753667ec068SMatthias Ringwald 1754aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1755aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1756a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 1; 1757a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x_index = 10 + index; 17581c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1759667ec068SMatthias Ringwald } 17603c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1761667ec068SMatthias Ringwald } 1762667ec068SMatthias Ringwald 17633c65e705SMilanka Ringwald uint8_t hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){ 17649c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1765a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17663c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1767a33eb0c4SMilanka Ringwald } 1768667ec068SMatthias Ringwald 1769aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1770aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1771a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 1; 1772a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x_index = 20 + index; 17731c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1774667ec068SMatthias Ringwald } 17753c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1776667ec068SMatthias Ringwald } 1777ce263fc8SMatthias Ringwald 17783c65e705SMilanka Ringwald uint8_t hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){ 17799c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1780a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17813c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1782a33eb0c4SMilanka Ringwald } 1783ce263fc8SMatthias Ringwald 1784a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_outgoing_call = 1; 1785aeb0f0feSMatthias Ringwald snprintf(hfp_hf_phone_number, sizeof(hfp_hf_phone_number), "%s", number); 17861c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 17873c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1788ce263fc8SMatthias Ringwald } 1789ce263fc8SMatthias Ringwald 17903c65e705SMilanka Ringwald uint8_t hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){ 17919c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1792a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17933c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1794a33eb0c4SMilanka Ringwald } 1795ce263fc8SMatthias Ringwald 1796a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_memory_dialing = 1; 1797a0ffb263SMatthias Ringwald hfp_connection->memory_id = memory_id; 1798a0ffb263SMatthias Ringwald 17991c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18003c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1801ce263fc8SMatthias Ringwald } 1802ce263fc8SMatthias Ringwald 18033c65e705SMilanka Ringwald uint8_t hfp_hf_redial_last_number(hci_con_handle_t acl_handle){ 18049c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1805a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18063c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1807a33eb0c4SMilanka Ringwald } 1808ce263fc8SMatthias Ringwald 1809a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_redial_last_number = 1; 18101c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18113c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1812ce263fc8SMatthias Ringwald } 1813ce263fc8SMatthias Ringwald 18143c65e705SMilanka Ringwald uint8_t hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){ 18159c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1816a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18173c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1818a33eb0c4SMilanka Ringwald } 1819ce263fc8SMatthias Ringwald 1820a0ffb263SMatthias Ringwald hfp_connection->hf_activate_call_waiting_notification = 1; 18211c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18223c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1823ce263fc8SMatthias Ringwald } 1824ce263fc8SMatthias Ringwald 1825ce263fc8SMatthias Ringwald 18263c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){ 18279c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1828a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18293c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1830a33eb0c4SMilanka Ringwald } 1831ce263fc8SMatthias Ringwald 1832a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_call_waiting_notification = 1; 18331c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18343c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1835ce263fc8SMatthias Ringwald } 1836ce263fc8SMatthias Ringwald 1837ce263fc8SMatthias Ringwald 18383c65e705SMilanka Ringwald uint8_t hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){ 18399c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1840a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18413c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1842a33eb0c4SMilanka Ringwald } 1843ce263fc8SMatthias Ringwald 1844a0ffb263SMatthias Ringwald hfp_connection->hf_activate_calling_line_notification = 1; 18451c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18463c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1847ce263fc8SMatthias Ringwald } 1848ce263fc8SMatthias Ringwald 18493c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){ 18509c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1851a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18523c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1853a33eb0c4SMilanka Ringwald } 1854ce263fc8SMatthias Ringwald 1855a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_calling_line_notification = 1; 18561c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18573c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1858ce263fc8SMatthias Ringwald } 1859ce263fc8SMatthias Ringwald 18606ba83b5eSMilanka Ringwald static bool hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection_t * hfp_connection){ 18616ba83b5eSMilanka Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_EC_NR_FUNCTION); 1862aeb0f0feSMatthias Ringwald int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_EC_NR_FUNCTION); 18636ba83b5eSMilanka Ringwald return hf && ag; 1864ce263fc8SMatthias Ringwald } 1865ce263fc8SMatthias Ringwald 18663c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){ 18679c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1868a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18693c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1870a33eb0c4SMilanka Ringwald } 18716ba83b5eSMilanka Ringwald if (!hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection)){ 18726ba83b5eSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 18736ba83b5eSMilanka Ringwald } 1874ce263fc8SMatthias Ringwald 1875a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1; 18761c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18773c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1878ce263fc8SMatthias Ringwald } 1879ce263fc8SMatthias Ringwald 1880acd11d4aSMilanka Ringwald uint8_t hfp_hf_activate_voice_recognition(hci_con_handle_t acl_handle){ 1881fdda66c0SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1882fdda66c0SMilanka Ringwald if (!hfp_connection) { 1883fdda66c0SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1884be55a11dSMilanka Ringwald } 1885013cc750SMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){ 1886013cc750SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1887013cc750SMilanka Ringwald } 1888acd11d4aSMilanka Ringwald 1889acd11d4aSMilanka Ringwald bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection); 1890acd11d4aSMilanka Ringwald bool legacy_vra_supported = hfp_hf_vra_flag_supported(hfp_connection); 1891acd11d4aSMilanka Ringwald if (!enhanced_vra_supported && !legacy_vra_supported){ 1892acd11d4aSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1893af97579eSMilanka Ringwald } 1894af97579eSMilanka Ringwald 1895498a8121SMilanka Ringwald switch (hfp_connection->vra_state){ 1896be55a11dSMilanka Ringwald case HFP_VRA_VOICE_RECOGNITION_OFF: 1897de9e0ea7SMilanka Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 1898fd4151d1SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED; 1899acd11d4aSMilanka Ringwald hfp_connection->enhanced_voice_recognition_enabled = enhanced_vra_supported; 1900be55a11dSMilanka Ringwald break; 1901de9e0ea7SMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_OFF: 1902de9e0ea7SMilanka Ringwald hfp_connection->activate_voice_recognition = true; 1903de9e0ea7SMilanka Ringwald break; 1904be55a11dSMilanka Ringwald default: 1905be55a11dSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1906be55a11dSMilanka Ringwald } 1907ce263fc8SMatthias Ringwald 1908af97579eSMilanka Ringwald hfp_hf_run_for_context(hfp_connection); 1909fdda66c0SMilanka Ringwald return ERROR_CODE_SUCCESS; 1910af97579eSMilanka Ringwald } 1911af97579eSMilanka Ringwald 1912acd11d4aSMilanka Ringwald uint8_t hfp_hf_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle){ 1913af97579eSMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1914af97579eSMilanka Ringwald if (!hfp_connection) { 1915af97579eSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1916af97579eSMilanka Ringwald } 1917*84fb9ac1SMilanka Ringwald 1918*84fb9ac1SMilanka Ringwald if (hfp_connection->emit_vra_enabled_after_audio_established){ 1919*84fb9ac1SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1920*84fb9ac1SMilanka Ringwald } 1921*84fb9ac1SMilanka Ringwald 1922acd11d4aSMilanka Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){ 192308a0b01cSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 192408a0b01cSMilanka Ringwald } 1925acd11d4aSMilanka Ringwald 1926acd11d4aSMilanka Ringwald bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection); 1927acd11d4aSMilanka Ringwald if (!enhanced_vra_supported){ 1928acd11d4aSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1929acd11d4aSMilanka Ringwald } 1930acd11d4aSMilanka Ringwald 1931acd11d4aSMilanka Ringwald switch (hfp_connection->vra_state){ 1932acd11d4aSMilanka Ringwald case HFP_VRA_VOICE_RECOGNITION_ACTIVATED: 1933acd11d4aSMilanka Ringwald case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1934acd11d4aSMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 1935acd11d4aSMilanka Ringwald break; 1936acd11d4aSMilanka Ringwald default: 1937fdda66c0SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1938af97579eSMilanka Ringwald } 1939013cc750SMilanka Ringwald 1940acd11d4aSMilanka Ringwald hfp_hf_run_for_context(hfp_connection); 1941acd11d4aSMilanka Ringwald return ERROR_CODE_SUCCESS; 1942acd11d4aSMilanka Ringwald } 1943acd11d4aSMilanka Ringwald 1944acd11d4aSMilanka Ringwald 1945acd11d4aSMilanka Ringwald uint8_t hfp_hf_deactivate_voice_recognition(hci_con_handle_t acl_handle){ 1946acd11d4aSMilanka Ringwald // return deactivate_voice_recognition(acl_handle, false); 1947acd11d4aSMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1948acd11d4aSMilanka Ringwald if (!hfp_connection) { 1949acd11d4aSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1950acd11d4aSMilanka Ringwald } 1951acd11d4aSMilanka Ringwald 1952*84fb9ac1SMilanka Ringwald if (hfp_connection->emit_vra_enabled_after_audio_established){ 1953*84fb9ac1SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1954*84fb9ac1SMilanka Ringwald } 1955*84fb9ac1SMilanka Ringwald 1956acd11d4aSMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || 1957acd11d4aSMilanka Ringwald hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){ 1958acd11d4aSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1959acd11d4aSMilanka Ringwald } 1960acd11d4aSMilanka Ringwald 1961acd11d4aSMilanka Ringwald bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection); 1962acd11d4aSMilanka Ringwald bool legacy_vra_supported = hfp_hf_vra_flag_supported(hfp_connection); 1963acd11d4aSMilanka Ringwald if (!enhanced_vra_supported && !legacy_vra_supported){ 1964acd11d4aSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1965acd11d4aSMilanka Ringwald } 1966acd11d4aSMilanka Ringwald 1967fdda66c0SMilanka Ringwald switch (hfp_connection->vra_state){ 1968de9e0ea7SMilanka Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED: 1969fdda66c0SMilanka Ringwald case HFP_VRA_VOICE_RECOGNITION_ACTIVATED: 1970de9e0ea7SMilanka Ringwald case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1971de9e0ea7SMilanka Ringwald case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1972fdda66c0SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF; 1973fdda66c0SMilanka Ringwald break; 1974de9e0ea7SMilanka Ringwald 1975de9e0ea7SMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED: 1976de9e0ea7SMilanka Ringwald case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1977de9e0ea7SMilanka Ringwald hfp_connection->deactivate_voice_recognition = true; 1978de9e0ea7SMilanka Ringwald break; 1979de9e0ea7SMilanka Ringwald 1980de9e0ea7SMilanka Ringwald case HFP_VRA_VOICE_RECOGNITION_OFF: 1981de9e0ea7SMilanka Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 1982de9e0ea7SMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_OFF: 1983fdda66c0SMilanka Ringwald default: 1984fdda66c0SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1985fdda66c0SMilanka Ringwald } 1986fdda66c0SMilanka Ringwald 1987fdda66c0SMilanka Ringwald hfp_hf_run_for_context(hfp_connection); 1988fdda66c0SMilanka Ringwald return ERROR_CODE_SUCCESS; 1989af97579eSMilanka Ringwald } 1990af97579eSMilanka Ringwald 19913c65e705SMilanka Ringwald uint8_t hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 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 } 1996c8626498SMilanka Ringwald 19973c65e705SMilanka Ringwald if (hfp_connection->microphone_gain == gain) { 19983c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 19993c65e705SMilanka Ringwald } 20003c65e705SMilanka Ringwald 2001c1ab6cc1SMatthias Ringwald if ((gain < 0) || (gain > 15)){ 2002a0ffb263SMatthias Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 20033c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2004a0ffb263SMatthias Ringwald } 20053c65e705SMilanka Ringwald 2006a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = gain; 2007a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 20081c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20093c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2010ce263fc8SMatthias Ringwald } 2011ce263fc8SMatthias Ringwald 20123c65e705SMilanka Ringwald uint8_t hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 20139c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2014a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20153c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2016a33eb0c4SMilanka Ringwald } 2017c8626498SMilanka Ringwald 20183c65e705SMilanka Ringwald if (hfp_connection->speaker_gain == gain){ 20193c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 20203c65e705SMilanka Ringwald } 20213c65e705SMilanka Ringwald 2022c1ab6cc1SMatthias Ringwald if ((gain < 0) || (gain > 15)){ 2023a0ffb263SMatthias Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 20243c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2025a0ffb263SMatthias Ringwald } 20263c65e705SMilanka Ringwald 2027a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = gain; 2028a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 20291c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20303c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2031ce263fc8SMatthias Ringwald } 2032ce263fc8SMatthias Ringwald 20333c65e705SMilanka Ringwald uint8_t hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){ 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_dtmf_code = code; 20391c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20403c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2041ce263fc8SMatthias Ringwald } 2042ce263fc8SMatthias Ringwald 20433c65e705SMilanka Ringwald uint8_t hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 20449c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2045a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20463c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2047a33eb0c4SMilanka Ringwald } 2048a0ffb263SMatthias Ringwald hfp_connection->hf_send_binp = 1; 20491c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20503c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2051ce263fc8SMatthias Ringwald } 20523deb3ec6SMatthias Ringwald 20533c65e705SMilanka Ringwald uint8_t hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){ 20549c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2055a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20563c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2057a33eb0c4SMilanka Ringwald } 2058a0ffb263SMatthias Ringwald hfp_connection->hf_send_clcc = 1; 20591c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20603c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2061667ec068SMatthias Ringwald } 2062667ec068SMatthias Ringwald 2063667ec068SMatthias Ringwald 20643c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){ 20659c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2066a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20673c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2068a33eb0c4SMilanka Ringwald } 2069a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 2070a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '?'; 20711c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20723c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2073667ec068SMatthias Ringwald } 2074667ec068SMatthias Ringwald 20753c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){ 20769c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2077a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20783c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2079a33eb0c4SMilanka Ringwald } 2080a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 2081a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '0'; 20821c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20833c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2084667ec068SMatthias Ringwald } 2085667ec068SMatthias Ringwald 20863c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){ 20879c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2088a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20893c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2090a33eb0c4SMilanka Ringwald } 2091a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 2092a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '1'; 20931c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20943c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2095667ec068SMatthias Ringwald } 2096667ec068SMatthias Ringwald 20973c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){ 20989c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2099a33eb0c4SMilanka Ringwald if (!hfp_connection) { 21003c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2101a33eb0c4SMilanka Ringwald } 2102a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 2103a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '2'; 21041c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 21053c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2106667ec068SMatthias Ringwald } 2107667ec068SMatthias Ringwald 21083c65e705SMilanka Ringwald uint8_t hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){ 21099c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2110a33eb0c4SMilanka Ringwald if (!hfp_connection) { 21113c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2112a33eb0c4SMilanka Ringwald } 2113a0ffb263SMatthias Ringwald hfp_connection->hf_send_cnum = 1; 21141c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 21153c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2116667ec068SMatthias Ringwald } 2117667ec068SMatthias Ringwald 21183c65e705SMilanka Ringwald uint8_t hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){ 21199c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2120a33eb0c4SMilanka Ringwald if (!hfp_connection) { 21213c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2122a33eb0c4SMilanka Ringwald } 2123667ec068SMatthias Ringwald // find index for assigned number 2124667ec068SMatthias Ringwald int i; 2125aeb0f0feSMatthias Ringwald for (i = 0; i < hfp_hf_indicators_nr ; i++){ 2126aeb0f0feSMatthias Ringwald if (hfp_hf_indicators[i] == assigned_number){ 2127667ec068SMatthias Ringwald // set value 2128aeb0f0feSMatthias Ringwald hfp_hf_indicators_value[i] = value; 2129667ec068SMatthias Ringwald // mark for update 2130a0ffb263SMatthias Ringwald if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){ 2131a0ffb263SMatthias Ringwald hfp_connection->generic_status_update_bitmap |= (1<<i); 2132667ec068SMatthias Ringwald // send update 21331c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 2134a0ffb263SMatthias Ringwald } 21353c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2136667ec068SMatthias Ringwald } 2137667ec068SMatthias Ringwald } 21383c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2139667ec068SMatthias Ringwald } 2140667ec068SMatthias Ringwald 2141d7f6b5cbSMatthias Ringwald int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){ 2142d7f6b5cbSMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2143d7f6b5cbSMatthias Ringwald if (!hfp_connection) { 2144d7f6b5cbSMatthias Ringwald return 0; 2145d7f6b5cbSMatthias Ringwald } 2146d7f6b5cbSMatthias Ringwald return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE); 2147d7f6b5cbSMatthias Ringwald } 214876cc1527SMatthias Ringwald 214976cc1527SMatthias 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){ 215076cc1527SMatthias Ringwald if (!name){ 2151aeb0f0feSMatthias Ringwald name = hfp_hf_default_service_name; 215276cc1527SMatthias Ringwald } 215376cc1527SMatthias Ringwald hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name); 215476cc1527SMatthias Ringwald 215576cc1527SMatthias Ringwald // Construct SupportedFeatures for SDP bitmap: 215676cc1527SMatthias Ringwald // 215776cc1527SMatthias Ringwald // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 215876cc1527SMatthias Ringwald // of the Bits 0 to 4 of the unsolicited result code +BRSF" 215976cc1527SMatthias Ringwald // 216076cc1527SMatthias Ringwald // Wide band speech (bit 5) requires Codec negotiation 216176cc1527SMatthias Ringwald // 216276cc1527SMatthias Ringwald uint16_t sdp_features = supported_features & 0x1f; 2163ef3ae4ebSMilanka Ringwald if ( (wide_band_speech != 0) && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){ 216476cc1527SMatthias Ringwald sdp_features |= 1 << 5; 216576cc1527SMatthias Ringwald } 2166ef3ae4ebSMilanka Ringwald 2167ef3ae4ebSMilanka Ringwald if (supported_features & (1 << HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS)){ 216856f1adacSMilanka Ringwald sdp_features |= 1 << 6; 2169ef3ae4ebSMilanka Ringwald } 2170ef3ae4ebSMilanka Ringwald 2171ef3ae4ebSMilanka Ringwald if (supported_features & (1 << HFP_HFSF_VOICE_RECOGNITION_TEXT)){ 217256f1adacSMilanka Ringwald sdp_features |= 1 << 7; 2173ef3ae4ebSMilanka Ringwald } 2174ef3ae4ebSMilanka Ringwald 217576cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 217676cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 217776cc1527SMatthias Ringwald } 217876cc1527SMatthias Ringwald 217976cc1527SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){ 218068466199SMilanka Ringwald btstack_assert(callback != NULL); 218168466199SMilanka Ringwald 218276cc1527SMatthias Ringwald hfp_hf_callback = callback; 218376cc1527SMatthias Ringwald hfp_set_hf_callback(callback); 218476cc1527SMatthias Ringwald } 2185