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 23*2fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 24*2fca4dadSMilanka Ringwald * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 253deb3ec6SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 263deb3ec6SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 273deb3ec6SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 283deb3ec6SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 293deb3ec6SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 303deb3ec6SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 313deb3ec6SMatthias Ringwald * SUCH DAMAGE. 323deb3ec6SMatthias Ringwald * 333deb3ec6SMatthias Ringwald * Please inquire about commercial licensing options at 343deb3ec6SMatthias Ringwald * [email protected] 353deb3ec6SMatthias Ringwald * 363deb3ec6SMatthias Ringwald */ 37ab2c6ae4SMatthias Ringwald 38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "hfp_hf.c" 393deb3ec6SMatthias Ringwald 403deb3ec6SMatthias Ringwald // ***************************************************************************** 413deb3ec6SMatthias Ringwald // 42fffdd288SMatthias Ringwald // HFP Hands-Free (HF) unit 433deb3ec6SMatthias Ringwald // 443deb3ec6SMatthias Ringwald // ***************************************************************************** 453deb3ec6SMatthias Ringwald 467907f069SMatthias Ringwald #include "btstack_config.h" 473deb3ec6SMatthias Ringwald 483deb3ec6SMatthias Ringwald #include <stdint.h> 493cfa4086SMatthias Ringwald #include <stdio.h> 503deb3ec6SMatthias Ringwald #include <string.h> 513deb3ec6SMatthias Ringwald 52235946f1SMatthias Ringwald #include "bluetooth_sdp.h" 5359c6af15SMatthias Ringwald #include "btstack_debug.h" 54d4dd47ffSMatthias Ringwald #include "btstack_event.h" 553deb3ec6SMatthias Ringwald #include "btstack_memory.h" 5659c6af15SMatthias Ringwald #include "btstack_run_loop.h" 5759c6af15SMatthias Ringwald #include "classic/core.h" 5859c6af15SMatthias Ringwald #include "classic/hfp.h" 5959c6af15SMatthias Ringwald #include "classic/hfp_hf.h" 60efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h" 61746ccb7eSMatthias Ringwald #include "classic/sdp_server.h" 62023f2764SMatthias Ringwald #include "classic/sdp_util.h" 6359c6af15SMatthias Ringwald #include "hci.h" 6459c6af15SMatthias Ringwald #include "hci_cmd.h" 6559c6af15SMatthias Ringwald #include "hci_dump.h" 6659c6af15SMatthias Ringwald #include "l2cap.h" 673deb3ec6SMatthias Ringwald 6820b2edb6SMatthias Ringwald // const 69aeb0f0feSMatthias Ringwald static const char hfp_hf_default_service_name[] = "Hands-Free unit"; 7020b2edb6SMatthias Ringwald 7120b2edb6SMatthias Ringwald // globals 72aeb0f0feSMatthias Ringwald 73aeb0f0feSMatthias Ringwald // higher layer callbacks 74aeb0f0feSMatthias Ringwald static btstack_packet_handler_t hfp_hf_callback; 75aeb0f0feSMatthias Ringwald 761c6a0fc0SMatthias Ringwald static btstack_packet_callback_registration_t hfp_hf_hci_event_callback_registration; 7727950165SMatthias Ringwald 78aeb0f0feSMatthias Ringwald static uint16_t hfp_hf_supported_features; 79aeb0f0feSMatthias Ringwald static uint8_t hfp_hf_codecs_nr; 80aeb0f0feSMatthias Ringwald static uint8_t hfp_hf_codecs[HFP_MAX_NUM_CODECS]; 813deb3ec6SMatthias Ringwald 82aeb0f0feSMatthias Ringwald static uint8_t hfp_hf_indicators_nr; 83aeb0f0feSMatthias Ringwald static uint8_t hfp_hf_indicators[HFP_MAX_NUM_INDICATORS]; 84aeb0f0feSMatthias Ringwald static uint32_t hfp_hf_indicators_value[HFP_MAX_NUM_INDICATORS]; 85667ec068SMatthias Ringwald 8620b2edb6SMatthias Ringwald static uint8_t hfp_hf_speaker_gain; 8720b2edb6SMatthias Ringwald static uint8_t hfp_hf_microphone_gain; 883deb3ec6SMatthias Ringwald 89aeb0f0feSMatthias Ringwald static hfp_call_status_t hfp_hf_call_status; 90aeb0f0feSMatthias Ringwald static hfp_callsetup_status_t hfp_hf_callsetup_status; 91aeb0f0feSMatthias Ringwald static hfp_callheld_status_t hfp_hf_callheld_status; 923deb3ec6SMatthias Ringwald 93aeb0f0feSMatthias Ringwald static char hfp_hf_phone_number[25]; 94ce263fc8SMatthias Ringwald 95ce263fc8SMatthias Ringwald 9676cc1527SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){ 97aeb0f0feSMatthias Ringwald int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_CODEC_NEGOTIATION); 9876cc1527SMatthias Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION); 9976cc1527SMatthias Ringwald return hf && ag; 10076cc1527SMatthias Ringwald } 10176cc1527SMatthias Ringwald 10276cc1527SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){ 103aeb0f0feSMatthias Ringwald int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_THREE_WAY_CALLING); 10476cc1527SMatthias Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING); 10576cc1527SMatthias Ringwald return hf && ag; 10676cc1527SMatthias Ringwald } 10776cc1527SMatthias Ringwald 10876cc1527SMatthias Ringwald 10976cc1527SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){ 110aeb0f0feSMatthias Ringwald int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_HF_INDICATORS); 11176cc1527SMatthias Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS); 11276cc1527SMatthias Ringwald return hf && ag; 11376cc1527SMatthias Ringwald } 11476cc1527SMatthias Ringwald 115fcf4ede6SMilanka Ringwald static bool hfp_hf_vra_flag_supported(hfp_connection_t * hfp_connection){ 116fcf4ede6SMilanka Ringwald int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION); 117fcf4ede6SMilanka Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION); 118fcf4ede6SMilanka Ringwald return hf && ag; 119fcf4ede6SMilanka Ringwald } 120fcf4ede6SMilanka Ringwald 121fcf4ede6SMilanka Ringwald static bool hfp_hf_enhanced_vra_flag_supported(hfp_connection_t * hfp_connection){ 122fcf4ede6SMilanka Ringwald int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS); 123fcf4ede6SMilanka Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS); 124fcf4ede6SMilanka Ringwald return hf && ag; 125fcf4ede6SMilanka Ringwald } 12676cc1527SMatthias Ringwald 1279c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_hf_connection_context_for_acl_handle(uint16_t handle){ 1289c9c64c1SMatthias Ringwald btstack_linked_list_iterator_t it; 1299c9c64c1SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1309c9c64c1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1319c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1329c9c64c1SMatthias Ringwald if (hfp_connection->acl_handle != handle) continue; 1339c9c64c1SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_HF) continue; 1349c9c64c1SMatthias Ringwald return hfp_connection; 1359c9c64c1SMatthias Ringwald } 1369c9c64c1SMatthias Ringwald return NULL; 1379c9c64c1SMatthias Ringwald } 1389c9c64c1SMatthias Ringwald 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); 63384fb9ac1SMilanka Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 634553a4a56SMilanka Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS); 63584fb9ac1SMilanka Ringwald } else { 63684fb9ac1SMilanka Ringwald // postpone VRA event to simplify application logic 63784fb9ac1SMilanka Ringwald hfp_connection->emit_vra_enabled_after_audio_established = true; 63884fb9ac1SMilanka 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 73984fb9ac1SMilanka Ringwald // emit postponed VRA event 74084fb9ac1SMilanka Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->emit_vra_enabled_after_audio_established){ 74184fb9ac1SMilanka Ringwald hfp_connection->emit_vra_enabled_after_audio_established = false; 74284fb9ac1SMilanka Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS); 74384fb9ac1SMilanka Ringwald } 74484fb9ac1SMilanka 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 } 1051722a85f3SMilanka Ringwald 1052722a85f3SMilanka Ringwald for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 1053722a85f3SMilanka Ringwald hfp_connection->ag_indicators[i].status_changed = 0; 1054722a85f3SMilanka Ringwald hfp_emit_ag_indicator_status_event(hfp_connection, &hfp_connection->ag_indicators[i]); 1055722a85f3SMilanka Ringwald } 1056722a85f3SMilanka Ringwald 1057667ec068SMatthias Ringwald // restore volume settings 1058a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = hfp_hf_speaker_gain; 1059a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 1060ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain); 1061a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = hfp_hf_microphone_gain; 1062a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 1063ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain); 1064667ec068SMatthias Ringwald // enable all indicators 1065aeb0f0feSMatthias Ringwald for (i=0; i < hfp_hf_indicators_nr; i++){ 1066aeb0f0feSMatthias Ringwald hfp_connection->generic_status_indicators[i].uuid = hfp_hf_indicators[i]; 1067a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[i].state = 1; 1068667ec068SMatthias Ringwald } 1069ce263fc8SMatthias Ringwald } 1070ce263fc8SMatthias Ringwald 10711cc65c4fSMatthias Ringwald static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){ 1072aeb0f0feSMatthias Ringwald if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_hf_codecs_nr, hfp_hf_codecs)){ 10731cc65c4fSMatthias Ringwald // Codec supported, confirm 10741cc65c4fSMatthias Ringwald hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 10751cc65c4fSMatthias Ringwald hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 10761cc65c4fSMatthias Ringwald log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD"); 10771cc65c4fSMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC; 10781cc65c4fSMatthias Ringwald 10791cc65c4fSMatthias Ringwald hfp_connection->hf_send_codec_confirm = true; 10801cc65c4fSMatthias Ringwald } else { 10811cc65c4fSMatthias Ringwald // Codec not supported, send supported codecs 10821cc65c4fSMatthias Ringwald hfp_connection->codec_confirmed = 0; 10831cc65c4fSMatthias Ringwald hfp_connection->suggested_codec = 0; 10841cc65c4fSMatthias Ringwald hfp_connection->negotiated_codec = 0; 10851cc65c4fSMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 10861cc65c4fSMatthias Ringwald 10871cc65c4fSMatthias Ringwald hfp_connection->hf_send_supported_codecs = true; 10881cc65c4fSMatthias Ringwald } 10891cc65c4fSMatthias Ringwald } 10901cc65c4fSMatthias Ringwald 1091e2c3b58dSMilanka Ringwald static bool hfp_hf_switch_on_ok_pending(hfp_connection_t *hfp_connection, uint8_t status){ 1092e2c3b58dSMilanka Ringwald bool event_emited = true; 1093e2c3b58dSMilanka Ringwald 109499af1e28SMilanka Ringwald switch (hfp_connection->response_pending_for_command){ 1095e2c3b58dSMilanka Ringwald case HFP_CMD_TURN_OFF_EC_AND_NR: 1096e2c3b58dSMilanka Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE, status); 1097e2c3b58dSMilanka Ringwald break; 1098e2c3b58dSMilanka Ringwald default: 1099e2c3b58dSMilanka Ringwald event_emited = false; 1100e2c3b58dSMilanka Ringwald 1101a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 11023deb3ec6SMatthias Ringwald case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 1103a0ffb263SMatthias Ringwald if (has_codec_negotiation_feature(hfp_connection)){ 1104a0ffb263SMatthias Ringwald hfp_connection->state = HFP_NOTIFY_ON_CODECS; 11053deb3ec6SMatthias Ringwald break; 11063deb3ec6SMatthias Ringwald } 1107a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 11083deb3ec6SMatthias Ringwald break; 11093deb3ec6SMatthias Ringwald 11103deb3ec6SMatthias Ringwald case HFP_W4_NOTIFY_ON_CODECS: 1111a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 11123deb3ec6SMatthias Ringwald break; 11133deb3ec6SMatthias Ringwald 11143deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS: 1115a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS; 11163deb3ec6SMatthias Ringwald break; 11173deb3ec6SMatthias Ringwald 11183deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS_STATUS: 1119a0ffb263SMatthias Ringwald hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE; 11203deb3ec6SMatthias Ringwald break; 11213deb3ec6SMatthias Ringwald 11223deb3ec6SMatthias Ringwald case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE: 1123a0ffb263SMatthias Ringwald if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 1124a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL; 11253deb3ec6SMatthias Ringwald break; 11263deb3ec6SMatthias Ringwald } 1127a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 1128a0ffb263SMatthias Ringwald hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 11293deb3ec6SMatthias Ringwald break; 11303deb3ec6SMatthias Ringwald } 1131a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 11323deb3ec6SMatthias Ringwald break; 11333deb3ec6SMatthias Ringwald 11343deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_CAN_HOLD_CALL: 1135a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 1136a0ffb263SMatthias Ringwald hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 11373deb3ec6SMatthias Ringwald break; 11383deb3ec6SMatthias Ringwald } 1139a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 11403deb3ec6SMatthias Ringwald break; 11413deb3ec6SMatthias Ringwald 11423deb3ec6SMatthias Ringwald case HFP_W4_LIST_GENERIC_STATUS_INDICATORS: 1143a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS; 11443deb3ec6SMatthias Ringwald break; 11453deb3ec6SMatthias Ringwald 11463deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS: 1147a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 11483deb3ec6SMatthias Ringwald break; 11493deb3ec6SMatthias Ringwald 11503deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 1151a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 11523deb3ec6SMatthias Ringwald break; 1153ce263fc8SMatthias Ringwald case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 1154a0ffb263SMatthias Ringwald if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 1155a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 1156ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 1157ce263fc8SMatthias Ringwald break; 1158ce263fc8SMatthias Ringwald } 11593deb3ec6SMatthias Ringwald 1160a0ffb263SMatthias Ringwald if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){ 1161a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 0; 1162ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 1163ce263fc8SMatthias Ringwald break; 11643deb3ec6SMatthias Ringwald } 11653deb3ec6SMatthias Ringwald 1166a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 1167ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK: 1168a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1169ce263fc8SMatthias Ringwald break; 1170ce263fc8SMatthias Ringwald case HPF_HF_QUERY_OPERATOR_W4_RESULT: 1171a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET; 1172a473a009SMatthias Ringwald hfp_emit_network_operator_event(hfp_connection); 1173ce263fc8SMatthias Ringwald break; 1174ce263fc8SMatthias Ringwald default: 1175ce263fc8SMatthias Ringwald break; 11763deb3ec6SMatthias Ringwald } 1177ce263fc8SMatthias Ringwald 1178a0ffb263SMatthias Ringwald if (hfp_connection->enable_extended_audio_gateway_error_report){ 1179a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = 0; 1180ce263fc8SMatthias Ringwald break; 11813deb3ec6SMatthias Ringwald } 11823deb3ec6SMatthias Ringwald 1183a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 1184aa4dd815SMatthias Ringwald case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 1185a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 11863deb3ec6SMatthias Ringwald break; 1187ce263fc8SMatthias Ringwald case HFP_CODECS_HF_CONFIRMED_CODEC: 1188a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 1189ce263fc8SMatthias Ringwald break; 11903deb3ec6SMatthias Ringwald default: 11913deb3ec6SMatthias Ringwald break; 11923deb3ec6SMatthias Ringwald } 1193af97579eSMilanka Ringwald hfp_hf_voice_recognition_state_machine(hfp_connection); 1194be55a11dSMilanka Ringwald break; 1195be55a11dSMilanka Ringwald case HFP_AUDIO_CONNECTION_ESTABLISHED: 1196af97579eSMilanka Ringwald hfp_hf_voice_recognition_state_machine(hfp_connection); 11973deb3ec6SMatthias Ringwald break; 11983deb3ec6SMatthias Ringwald default: 11993deb3ec6SMatthias Ringwald break; 12003deb3ec6SMatthias Ringwald } 1201e2c3b58dSMilanka Ringwald break; 1202e2c3b58dSMilanka Ringwald } 12033deb3ec6SMatthias Ringwald 12043deb3ec6SMatthias Ringwald // done 120599af1e28SMilanka Ringwald hfp_connection->response_pending_for_command = HFP_CMD_NONE; 1206be55a11dSMilanka Ringwald hfp_connection->ok_pending = 0; 1207a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1208e2c3b58dSMilanka Ringwald return event_emited; 12093deb3ec6SMatthias Ringwald } 12103deb3ec6SMatthias Ringwald 1211a03dbc20SMilanka Ringwald static bool hfp_is_ringing(hfp_callsetup_status_t callsetup_status){ 1212a03dbc20SMilanka Ringwald switch (callsetup_status){ 1213a03dbc20SMilanka Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1214a03dbc20SMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1215a03dbc20SMilanka Ringwald return true; 1216a03dbc20SMilanka Ringwald default: 1217a03dbc20SMilanka Ringwald return false; 1218a03dbc20SMilanka Ringwald } 1219a03dbc20SMilanka Ringwald } 1220be55a11dSMilanka Ringwald 1221b08371a9SMilanka Ringwald static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) { 12224562e2a2SMatthias Ringwald uint16_t i; 1223a03dbc20SMilanka Ringwald 12244562e2a2SMatthias Ringwald for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 12254562e2a2SMatthias Ringwald if (hfp_connection->ag_indicators[i].status_changed) { 12264562e2a2SMatthias Ringwald if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){ 1227a03dbc20SMilanka Ringwald hfp_callsetup_status_t new_hf_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status; 1228a03dbc20SMilanka Ringwald bool ringing_old = hfp_is_ringing(hfp_hf_callsetup_status); 1229a03dbc20SMilanka Ringwald bool ringing_new = hfp_is_ringing(new_hf_callsetup_status); 1230a03dbc20SMilanka Ringwald if (ringing_old != ringing_new){ 1231a03dbc20SMilanka Ringwald if (ringing_new){ 1232a03dbc20SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGING); 1233a03dbc20SMilanka Ringwald } else { 1234a03dbc20SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGING); 1235a03dbc20SMilanka Ringwald } 1236a03dbc20SMilanka Ringwald } 1237a03dbc20SMilanka Ringwald hfp_hf_callsetup_status = new_hf_callsetup_status; 12384562e2a2SMatthias Ringwald } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){ 1239aeb0f0feSMatthias Ringwald hfp_hf_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status; 12404562e2a2SMatthias Ringwald // avoid set but not used warning 1241aeb0f0feSMatthias Ringwald (void) hfp_hf_callheld_status; 12424562e2a2SMatthias Ringwald } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){ 1243674ebed5SMilanka Ringwald hfp_call_status_t new_hf_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status; 1244674ebed5SMilanka Ringwald if (hfp_hf_call_status != new_hf_call_status){ 1245674ebed5SMilanka Ringwald if (new_hf_call_status == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS){ 1246674ebed5SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED); 1247674ebed5SMilanka Ringwald } else { 1248674ebed5SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED); 1249674ebed5SMilanka Ringwald } 1250674ebed5SMilanka Ringwald } 1251674ebed5SMilanka Ringwald hfp_hf_call_status = new_hf_call_status; 12524562e2a2SMatthias Ringwald } 12534562e2a2SMatthias Ringwald hfp_connection->ag_indicators[i].status_changed = 0; 1254ce3797e1SMilanka Ringwald hfp_emit_ag_indicator_status_event(hfp_connection, &hfp_connection->ag_indicators[i]); 12554562e2a2SMatthias Ringwald break; 12564562e2a2SMatthias Ringwald } 12574562e2a2SMatthias Ringwald } 12584562e2a2SMatthias Ringwald } 12594562e2a2SMatthias Ringwald 1260426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){ 1261186dd3d2SMatthias Ringwald int value; 1262186dd3d2SMatthias Ringwald int i; 1263e2c3b58dSMilanka Ringwald bool event_emited; 1264e2c3b58dSMilanka Ringwald 1265a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1266667ec068SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1267a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1268a473a009SMatthias Ringwald hfp_hf_emit_subscriber_information(hfp_connection, 0); 1269667ec068SMatthias Ringwald break; 1270667ec068SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_STATUS: 1271a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1272ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0])); 1273667ec068SMatthias Ringwald break; 1274667ec068SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 1275a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1276a473a009SMatthias Ringwald hfp_hf_emit_enhanced_call_status(hfp_connection); 1277667ec068SMatthias Ringwald break; 1278ce263fc8SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 1279a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 12802308e108SMilanka Ringwald value = btstack_atoi((char*)hfp_connection->line_buffer); 1281667ec068SMatthias Ringwald hfp_hf_speaker_gain = value; 1282ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value); 1283ce263fc8SMatthias Ringwald break; 1284ce263fc8SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 1285a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 12862308e108SMilanka Ringwald value = btstack_atoi((char*)hfp_connection->line_buffer); 1287667ec068SMatthias Ringwald hfp_hf_microphone_gain = value; 1288ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value); 1289ce263fc8SMatthias Ringwald break; 1290ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 1291a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1292ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number); 1293a0ffb263SMatthias Ringwald break; 1294a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1295a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1296a473a009SMatthias Ringwald hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION); 1297a0ffb263SMatthias Ringwald break; 1298a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1299a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1300a473a009SMatthias Ringwald hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION); 1301ce263fc8SMatthias Ringwald break; 1302ce263fc8SMatthias Ringwald case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1303a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 13045a4785c8SMatthias Ringwald hfp_connection->ok_pending = 0; 1305a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 1306ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value); 1307ce263fc8SMatthias Ringwald break; 13080b4debbfSMilanka Ringwald case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION: 13090b4debbfSMilanka Ringwald break; 1310fdda66c0SMilanka Ringwald case HFP_CMD_ERROR: 131190244c92SMilanka Ringwald switch (hfp_connection->state){ 131290244c92SMilanka Ringwald case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 131390244c92SMilanka Ringwald switch (hfp_connection->codecs_state){ 131490244c92SMilanka Ringwald case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 1315fdda66c0SMilanka Ringwald hfp_reset_context_flags(hfp_connection); 131690244c92SMilanka Ringwald hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec); 131790244c92SMilanka Ringwald return; 131890244c92SMilanka Ringwald default: 131990244c92SMilanka Ringwald break; 132090244c92SMilanka Ringwald } 132156f1adacSMilanka Ringwald break; 132256f1adacSMilanka Ringwald default: 132356f1adacSMilanka Ringwald break; 132456f1adacSMilanka Ringwald } 1325e2c3b58dSMilanka Ringwald 1326fdda66c0SMilanka Ringwald // handle error response for voice activation (HF initiated) 13270b4debbfSMilanka Ringwald switch(hfp_connection->vra_state_requested){ 1328de9e0ea7SMilanka Ringwald case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1329de9e0ea7SMilanka Ringwald hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 1330be55a11dSMilanka Ringwald break; 1331be55a11dSMilanka Ringwald default: 1332e2c3b58dSMilanka Ringwald if (hfp_connection->vra_state_requested == hfp_connection->vra_state){ 1333e2c3b58dSMilanka Ringwald break; 1334e2c3b58dSMilanka Ringwald } 13350b4debbfSMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 1336553a4a56SMilanka Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 13370b4debbfSMilanka Ringwald hfp_reset_context_flags(hfp_connection); 13380b4debbfSMilanka Ringwald return; 1339be55a11dSMilanka Ringwald } 1340e2c3b58dSMilanka Ringwald event_emited = hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 1341e2c3b58dSMilanka Ringwald if (!event_emited){ 13420b4debbfSMilanka Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1); 1343e2c3b58dSMilanka Ringwald } 1344fdda66c0SMilanka Ringwald hfp_reset_context_flags(hfp_connection); 1345ce263fc8SMatthias Ringwald break; 1346fdda66c0SMilanka Ringwald 1347ce263fc8SMatthias Ringwald case HFP_CMD_OK: 1348e2c3b58dSMilanka Ringwald hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_SUCCESS); 1349ce263fc8SMatthias Ringwald break; 1350ce263fc8SMatthias Ringwald case HFP_CMD_RING: 13515a4785c8SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1352ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING); 1353ce263fc8SMatthias Ringwald break; 1354ce263fc8SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 13555a4785c8SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 13564562e2a2SMatthias Ringwald hfp_hf_handle_transfer_ag_indicator_status(hfp_connection); 1357ce263fc8SMatthias Ringwald break; 1358c741b032SMilanka Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 13595a4785c8SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1360184a03edSMilanka Ringwald // report status after SLC established 1361184a03edSMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){ 1362184a03edSMilanka Ringwald break; 1363184a03edSMilanka Ringwald } 1364c741b032SMilanka Ringwald for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 13651ac1f60fSMilanka Ringwald hfp_emit_ag_indicator_mapping_event(hfp_connection, &hfp_connection->ag_indicators[i]); 1366c741b032SMilanka Ringwald } 1367c741b032SMilanka Ringwald break; 13681cc65c4fSMatthias Ringwald case HFP_CMD_AG_SUGGESTED_CODEC: 13691cc65c4fSMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 13705a4785c8SMatthias Ringwald hfp_hf_handle_suggested_codec(hfp_connection); 13711cc65c4fSMatthias Ringwald break; 1372eac56539SMilanka Ringwald case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING: 1373eac56539SMilanka 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)); 1374ce263fc8SMatthias Ringwald default: 1375ce263fc8SMatthias Ringwald break; 13763deb3ec6SMatthias Ringwald } 13770cef86faSMatthias Ringwald } 1378426f9988SMatthias Ringwald 137976cc1527SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){ 138076cc1527SMatthias Ringwald return (byte == '\n') || (byte == '\r'); 138176cc1527SMatthias Ringwald } 138276cc1527SMatthias Ringwald 13830b4debbfSMilanka Ringwald static void hfp_hf_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){ 1384426f9988SMatthias Ringwald // assertion: size >= 1 as rfcomm.c does not deliver empty packets 1385426f9988SMatthias Ringwald if (size < 1) return; 1386426f9988SMatthias Ringwald 1387426f9988SMatthias Ringwald hfp_log_rfcomm_message("HFP_HF_RX", packet, size); 1388e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES 1389e43d1938SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet); 1390e43d1938SMatthias Ringwald #endif 1391426f9988SMatthias Ringwald 1392426f9988SMatthias Ringwald // process messages byte-wise 1393a7ba78b0SMilanka Ringwald uint8_t pos; 1394426f9988SMatthias Ringwald for (pos = 0; pos < size; pos++){ 1395426f9988SMatthias Ringwald hfp_parse(hfp_connection, packet[pos], 1); 13961599fe57SMatthias Ringwald // parse until end of line "\r" or "\n" 1397426f9988SMatthias Ringwald if (!hfp_parser_is_end_of_line(packet[pos])) continue; 13980b4debbfSMilanka Ringwald hfp_hf_handle_rfcomm_command(hfp_connection); 13993deb3ec6SMatthias Ringwald } 1400a7ba78b0SMilanka Ringwald } 14013deb3ec6SMatthias Ringwald 14021c6a0fc0SMatthias Ringwald static void hfp_hf_run(void){ 1403665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1404665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1405665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1406a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 140722387625SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_HF) continue; 14081c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 14093deb3ec6SMatthias Ringwald } 14103deb3ec6SMatthias Ringwald } 14113deb3ec6SMatthias Ringwald 14121c6a0fc0SMatthias Ringwald static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 14130b4debbfSMilanka Ringwald hfp_connection_t * hfp_connection; 14143deb3ec6SMatthias Ringwald switch (packet_type){ 14153deb3ec6SMatthias Ringwald case RFCOMM_DATA_PACKET: 14160b4debbfSMilanka Ringwald hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 14170b4debbfSMilanka Ringwald if (!hfp_connection) return; 14180b4debbfSMilanka Ringwald hfp_hf_handle_rfcomm_data(hfp_connection, packet, size); 14190b4debbfSMilanka Ringwald hfp_hf_run_for_context(hfp_connection); 14200b4debbfSMilanka Ringwald return; 14213deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 1422d4dd47ffSMatthias Ringwald if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 1423d4dd47ffSMatthias Ringwald uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 14241c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid)); 1425d4dd47ffSMatthias Ringwald return; 1426d4dd47ffSMatthias Ringwald } 142727950165SMatthias Ringwald hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF); 1428202c8a4cSMatthias Ringwald break; 14293deb3ec6SMatthias Ringwald default: 14303deb3ec6SMatthias Ringwald break; 14313deb3ec6SMatthias Ringwald } 14321c6a0fc0SMatthias Ringwald hfp_hf_run(); 14333deb3ec6SMatthias Ringwald } 14343deb3ec6SMatthias Ringwald 14351c6a0fc0SMatthias Ringwald static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1436405014fbSMatthias Ringwald hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF); 14371c6a0fc0SMatthias Ringwald hfp_hf_run(); 1438405014fbSMatthias Ringwald } 1439405014fbSMatthias Ringwald 1440aeb0f0feSMatthias Ringwald static void hfp_hf_set_defaults(void){ 1441aeb0f0feSMatthias Ringwald hfp_hf_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 1442aeb0f0feSMatthias Ringwald hfp_hf_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS; 1443aeb0f0feSMatthias Ringwald hfp_hf_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1444aeb0f0feSMatthias Ringwald hfp_hf_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD; 1445aeb0f0feSMatthias Ringwald hfp_hf_codecs_nr = 0; 1446aeb0f0feSMatthias Ringwald hfp_hf_speaker_gain = 9; 1447aeb0f0feSMatthias Ringwald hfp_hf_microphone_gain = 9; 1448aeb0f0feSMatthias Ringwald hfp_hf_indicators_nr = 0; 1449aeb0f0feSMatthias Ringwald } 1450aeb0f0feSMatthias Ringwald 1451b4df8028SMilanka Ringwald uint8_t hfp_hf_init(uint16_t rfcomm_channel_nr){ 1452b4df8028SMilanka Ringwald uint8_t status = rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 1453b4df8028SMilanka Ringwald if (status != ERROR_CODE_SUCCESS){ 1454b4df8028SMilanka Ringwald return status; 1455b4df8028SMilanka Ringwald } 1456b4df8028SMilanka Ringwald 1457520c92d5SMatthias Ringwald hfp_init(); 1458aeb0f0feSMatthias Ringwald hfp_hf_set_defaults(); 1459d63c37a1SMatthias Ringwald 14601c6a0fc0SMatthias Ringwald hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler; 14611c6a0fc0SMatthias Ringwald hci_add_event_handler(&hfp_hf_hci_event_callback_registration); 146227950165SMatthias Ringwald 146327950165SMatthias Ringwald // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us 14641c6a0fc0SMatthias Ringwald hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler); 1465b4df8028SMilanka Ringwald return ERROR_CODE_SUCCESS; 146620b2edb6SMatthias Ringwald } 146727950165SMatthias Ringwald 146820b2edb6SMatthias Ringwald void hfp_hf_deinit(void){ 146920b2edb6SMatthias Ringwald hfp_deinit(); 1470aeb0f0feSMatthias Ringwald hfp_hf_set_defaults(); 1471aeb0f0feSMatthias Ringwald 1472aeb0f0feSMatthias Ringwald hfp_hf_callback = NULL; 147320b2edb6SMatthias Ringwald (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t)); 1474aeb0f0feSMatthias Ringwald (void) memset(hfp_hf_phone_number, 0, sizeof(hfp_hf_phone_number)); 1475a0ffb263SMatthias Ringwald } 1476a0ffb263SMatthias Ringwald 14777ca89cabSMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, const uint8_t * codecs){ 147868466199SMilanka Ringwald btstack_assert(codecs_nr < HFP_MAX_NUM_CODECS); 14793deb3ec6SMatthias Ringwald 1480aeb0f0feSMatthias Ringwald hfp_hf_codecs_nr = codecs_nr; 14813deb3ec6SMatthias Ringwald int i; 14823deb3ec6SMatthias Ringwald for (i=0; i<codecs_nr; i++){ 1483aeb0f0feSMatthias Ringwald hfp_hf_codecs[i] = codecs[i]; 14843deb3ec6SMatthias Ringwald } 14853deb3ec6SMatthias Ringwald } 14863deb3ec6SMatthias Ringwald 1487a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){ 1488aeb0f0feSMatthias Ringwald hfp_hf_supported_features = supported_features; 1489a0ffb263SMatthias Ringwald } 14903deb3ec6SMatthias Ringwald 14917ca89cabSMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){ 1492aeb0f0feSMatthias Ringwald btstack_assert(hfp_hf_indicators_nr < HFP_MAX_NUM_INDICATORS); 149368466199SMilanka Ringwald 1494aeb0f0feSMatthias Ringwald hfp_hf_indicators_nr = indicators_nr; 14953deb3ec6SMatthias Ringwald int i; 1496aeb0f0feSMatthias Ringwald for (i = 0; i < hfp_hf_indicators_nr ; i++){ 1497aeb0f0feSMatthias Ringwald hfp_hf_indicators[i] = indicators[i]; 14983deb3ec6SMatthias Ringwald } 14993deb3ec6SMatthias Ringwald } 15003deb3ec6SMatthias Ringwald 15014eb3f1d8SMilanka Ringwald uint8_t hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){ 15024eb3f1d8SMilanka Ringwald return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF); 15033deb3ec6SMatthias Ringwald } 15043deb3ec6SMatthias Ringwald 1505657bc59fSMilanka Ringwald uint8_t hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){ 15069c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1507a33eb0c4SMilanka Ringwald if (!hfp_connection){ 1508657bc59fSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1509a33eb0c4SMilanka Ringwald } 15101ffa0dd9SMilanka Ringwald hfp_trigger_release_service_level_connection(hfp_connection); 15111c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1512657bc59fSMilanka Ringwald return ERROR_CODE_SUCCESS; 15133deb3ec6SMatthias Ringwald } 15143deb3ec6SMatthias Ringwald 15153c65e705SMilanka Ringwald static uint8_t hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){ 15169c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1517a0ffb263SMatthias Ringwald if (!hfp_connection) { 15183c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 15193deb3ec6SMatthias Ringwald } 1520a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = enable; 15211c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 15223c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 15233deb3ec6SMatthias Ringwald } 15243deb3ec6SMatthias Ringwald 15253c65e705SMilanka Ringwald uint8_t hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 15263c65e705SMilanka Ringwald return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1); 1527ce263fc8SMatthias Ringwald } 1528ce263fc8SMatthias Ringwald 15293c65e705SMilanka Ringwald uint8_t hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 15303c65e705SMilanka Ringwald return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0); 1531ce263fc8SMatthias Ringwald } 1532ce263fc8SMatthias Ringwald 15333deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format 15343c65e705SMilanka Ringwald uint8_t hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){ 15359c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1536a0ffb263SMatthias Ringwald if (!hfp_connection) { 15373c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 15383deb3ec6SMatthias Ringwald } 1539a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 1; 1540a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap; 15411c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 15423c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 15433deb3ec6SMatthias Ringwald } 15443deb3ec6SMatthias Ringwald 15453c65e705SMilanka Ringwald uint8_t hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){ 15469c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1547a0ffb263SMatthias Ringwald if (!hfp_connection) { 15483c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 15493deb3ec6SMatthias Ringwald } 15503c65e705SMilanka Ringwald 1551a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 1552ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET: 1553a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT; 1554ce263fc8SMatthias Ringwald break; 1555ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_FORMAT_SET: 1556a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1557ce263fc8SMatthias Ringwald break; 1558ce263fc8SMatthias Ringwald default: 15593c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1560ce263fc8SMatthias Ringwald } 15611c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 15623c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 15633deb3ec6SMatthias Ringwald } 15643deb3ec6SMatthias Ringwald 15653c65e705SMilanka Ringwald static uint8_t hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){ 15669c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1567a0ffb263SMatthias Ringwald if (!hfp_connection) { 15683c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 15693deb3ec6SMatthias Ringwald } 1570a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = enable; 15711c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 15723c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 15733deb3ec6SMatthias Ringwald } 15743deb3ec6SMatthias Ringwald 1575ce263fc8SMatthias Ringwald 15763c65e705SMilanka Ringwald uint8_t hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 15773c65e705SMilanka Ringwald return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1); 1578ce263fc8SMatthias Ringwald } 1579ce263fc8SMatthias Ringwald 15803c65e705SMilanka Ringwald uint8_t hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 15813c65e705SMilanka Ringwald return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0); 1582ce263fc8SMatthias Ringwald } 1583ce263fc8SMatthias Ringwald 158438200c1dSMilanka Ringwald static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){ 1585aeb0f0feSMatthias Ringwald return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) && (hfp_hf_supported_features & (1 << HFP_HFSF_ESCO_S4)); 158638200c1dSMilanka Ringwald } 1587ce263fc8SMatthias Ringwald 15883c65e705SMilanka Ringwald uint8_t hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){ 15899c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1590a33eb0c4SMilanka Ringwald if (!hfp_connection) { 15913c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1592a33eb0c4SMilanka Ringwald } 1593ce263fc8SMatthias Ringwald 15943c65e705SMilanka Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 15953c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 15963c65e705SMilanka Ringwald } 15973c65e705SMilanka Ringwald 15983c65e705SMilanka Ringwald if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){ 15993c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 16003c65e705SMilanka Ringwald } 1601f4412093SMatthias Ringwald if (has_codec_negotiation_feature(hfp_connection)) { 1602a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state) { 1603aa4dd815SMatthias Ringwald case HFP_CODECS_W4_AG_COMMON_CODEC: 1604aa4dd815SMatthias Ringwald break; 1605ec3bfc1aSMatthias Ringwald case HFP_CODECS_EXCHANGED: 1606ec3bfc1aSMatthias Ringwald hfp_connection->trigger_codec_exchange = 1; 1607ec3bfc1aSMatthias Ringwald break; 1608aa4dd815SMatthias Ringwald default: 16091cc65c4fSMatthias Ringwald hfp_connection->codec_confirmed = 0; 16101cc65c4fSMatthias Ringwald hfp_connection->suggested_codec = 0; 16111cc65c4fSMatthias Ringwald hfp_connection->negotiated_codec = 0; 16121cc65c4fSMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 161338200c1dSMilanka Ringwald hfp_connection->trigger_codec_exchange = 1; 1614aa4dd815SMatthias Ringwald break; 16153deb3ec6SMatthias Ringwald } 1616f4412093SMatthias Ringwald } else { 1617f4412093SMatthias Ringwald log_info("no codec negotiation feature, use CVSD"); 1618f4412093SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 1619f4412093SMatthias Ringwald hfp_connection->suggested_codec = HFP_CODEC_CVSD; 1620f4412093SMatthias Ringwald hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 1621f4412093SMatthias Ringwald hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 1622f4412093SMatthias Ringwald hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection)); 1623f4412093SMatthias Ringwald hfp_connection->establish_audio_connection = 1; 1624f4412093SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 1625ce263fc8SMatthias Ringwald } 1626ce263fc8SMatthias Ringwald 16271c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 16283c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 16293deb3ec6SMatthias Ringwald } 16303deb3ec6SMatthias Ringwald 16313c65e705SMilanka Ringwald uint8_t hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){ 16329c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1633a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16343c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1635a33eb0c4SMilanka Ringwald } 16360b4debbfSMilanka Ringwald if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){ 16370b4debbfSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 16380b4debbfSMilanka Ringwald } 16390b4debbfSMilanka Ringwald uint8_t status = hfp_trigger_release_audio_connection(hfp_connection); 16400b4debbfSMilanka Ringwald if (status == ERROR_CODE_SUCCESS){ 16411c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 16420b4debbfSMilanka Ringwald } 16433c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 16443deb3ec6SMatthias Ringwald } 16453deb3ec6SMatthias Ringwald 16463c65e705SMilanka Ringwald uint8_t hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){ 16479c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1648a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16493c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1650a33eb0c4SMilanka Ringwald } 1651ce263fc8SMatthias Ringwald 1652aeb0f0feSMatthias Ringwald if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1653a0ffb263SMatthias Ringwald hfp_connection->hf_answer_incoming_call = 1; 16541c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1655ce263fc8SMatthias Ringwald } else { 1656aeb0f0feSMatthias Ringwald log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_hf_callsetup_status); 16573c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1658ce263fc8SMatthias Ringwald } 16593c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1660ce263fc8SMatthias Ringwald } 1661ce263fc8SMatthias Ringwald 16623c65e705SMilanka Ringwald uint8_t hfp_hf_terminate_call(hci_con_handle_t acl_handle){ 16639c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1664a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16653c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1666a33eb0c4SMilanka Ringwald } 1667a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 1; 16681c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 16693c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1670ce263fc8SMatthias Ringwald } 1671ce263fc8SMatthias Ringwald 16723c65e705SMilanka Ringwald uint8_t hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){ 16739c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1674a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16753c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1676a33eb0c4SMilanka Ringwald } 1677ce263fc8SMatthias Ringwald 1678aeb0f0feSMatthias Ringwald if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1679a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 1; 16801c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1681ce263fc8SMatthias Ringwald } 16823c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1683ce263fc8SMatthias Ringwald } 1684ce263fc8SMatthias Ringwald 16853c65e705SMilanka Ringwald uint8_t hfp_hf_user_busy(hci_con_handle_t acl_handle){ 16869c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1687a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16883c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1689a33eb0c4SMilanka Ringwald } 1690ce263fc8SMatthias Ringwald 1691aeb0f0feSMatthias Ringwald if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1692a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_0 = 1; 16931c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1694ce263fc8SMatthias Ringwald } 16953c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1696ce263fc8SMatthias Ringwald } 1697ce263fc8SMatthias Ringwald 16983c65e705SMilanka Ringwald uint8_t hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){ 16999c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1700a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17013c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1702a33eb0c4SMilanka Ringwald } 1703ce263fc8SMatthias Ringwald 1704aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1705aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1706a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_1 = 1; 17071c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1708ce263fc8SMatthias Ringwald } 17093c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1710ce263fc8SMatthias Ringwald } 1711ce263fc8SMatthias Ringwald 17123c65e705SMilanka Ringwald uint8_t hfp_hf_swap_calls(hci_con_handle_t acl_handle){ 17139c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1714a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17153c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1716a33eb0c4SMilanka Ringwald } 1717ce263fc8SMatthias Ringwald 1718aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1719aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1720a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_2 = 1; 17211c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1722ce263fc8SMatthias Ringwald } 17233c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1724ce263fc8SMatthias Ringwald } 1725ce263fc8SMatthias Ringwald 17263c65e705SMilanka Ringwald uint8_t hfp_hf_join_held_call(hci_con_handle_t acl_handle){ 17279c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1728a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17293c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1730a33eb0c4SMilanka Ringwald } 1731ce263fc8SMatthias Ringwald 1732aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1733aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1734a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_3 = 1; 17351c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1736ce263fc8SMatthias Ringwald } 17373c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1738ce263fc8SMatthias Ringwald } 1739ce263fc8SMatthias Ringwald 17403c65e705SMilanka Ringwald uint8_t hfp_hf_connect_calls(hci_con_handle_t acl_handle){ 17419c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1742a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17433c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1744a33eb0c4SMilanka Ringwald } 1745ce263fc8SMatthias Ringwald 1746aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1747aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1748a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_4 = 1; 17491c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1750ce263fc8SMatthias Ringwald } 17513c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1752ce263fc8SMatthias Ringwald } 1753ce263fc8SMatthias Ringwald 17543c65e705SMilanka Ringwald uint8_t hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){ 17559c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1756a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17573c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1758a33eb0c4SMilanka Ringwald } 1759667ec068SMatthias Ringwald 1760aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1761aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1762a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 1; 1763a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x_index = 10 + index; 17641c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1765667ec068SMatthias Ringwald } 17663c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1767667ec068SMatthias Ringwald } 1768667ec068SMatthias Ringwald 17693c65e705SMilanka Ringwald uint8_t hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){ 17709c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1771a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17723c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1773a33eb0c4SMilanka Ringwald } 1774667ec068SMatthias Ringwald 1775aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1776aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1777a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 1; 1778a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x_index = 20 + index; 17791c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1780667ec068SMatthias Ringwald } 17813c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1782667ec068SMatthias Ringwald } 1783ce263fc8SMatthias Ringwald 17843c65e705SMilanka Ringwald uint8_t hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){ 17859c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1786a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17873c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1788a33eb0c4SMilanka Ringwald } 1789ce263fc8SMatthias Ringwald 1790a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_outgoing_call = 1; 1791aeb0f0feSMatthias Ringwald snprintf(hfp_hf_phone_number, sizeof(hfp_hf_phone_number), "%s", number); 17921c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 17933c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1794ce263fc8SMatthias Ringwald } 1795ce263fc8SMatthias Ringwald 17963c65e705SMilanka Ringwald uint8_t hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){ 17979c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1798a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17993c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1800a33eb0c4SMilanka Ringwald } 1801ce263fc8SMatthias Ringwald 1802a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_memory_dialing = 1; 1803a0ffb263SMatthias Ringwald hfp_connection->memory_id = memory_id; 1804a0ffb263SMatthias Ringwald 18051c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18063c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1807ce263fc8SMatthias Ringwald } 1808ce263fc8SMatthias Ringwald 18093c65e705SMilanka Ringwald uint8_t hfp_hf_redial_last_number(hci_con_handle_t acl_handle){ 18109c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1811a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18123c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1813a33eb0c4SMilanka Ringwald } 1814ce263fc8SMatthias Ringwald 1815a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_redial_last_number = 1; 18161c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18173c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1818ce263fc8SMatthias Ringwald } 1819ce263fc8SMatthias Ringwald 18203c65e705SMilanka Ringwald uint8_t hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){ 18219c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1822a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18233c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1824a33eb0c4SMilanka Ringwald } 1825ce263fc8SMatthias Ringwald 1826a0ffb263SMatthias Ringwald hfp_connection->hf_activate_call_waiting_notification = 1; 18271c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18283c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1829ce263fc8SMatthias Ringwald } 1830ce263fc8SMatthias Ringwald 1831ce263fc8SMatthias Ringwald 18323c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){ 18339c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1834a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18353c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1836a33eb0c4SMilanka Ringwald } 1837ce263fc8SMatthias Ringwald 1838a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_call_waiting_notification = 1; 18391c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18403c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1841ce263fc8SMatthias Ringwald } 1842ce263fc8SMatthias Ringwald 1843ce263fc8SMatthias Ringwald 18443c65e705SMilanka Ringwald uint8_t hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){ 18459c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1846a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18473c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1848a33eb0c4SMilanka Ringwald } 1849ce263fc8SMatthias Ringwald 1850a0ffb263SMatthias Ringwald hfp_connection->hf_activate_calling_line_notification = 1; 18511c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18523c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1853ce263fc8SMatthias Ringwald } 1854ce263fc8SMatthias Ringwald 18553c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){ 18569c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1857a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18583c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1859a33eb0c4SMilanka Ringwald } 1860ce263fc8SMatthias Ringwald 1861a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_calling_line_notification = 1; 18621c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18633c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1864ce263fc8SMatthias Ringwald } 1865ce263fc8SMatthias Ringwald 18666ba83b5eSMilanka Ringwald static bool hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection_t * hfp_connection){ 18676ba83b5eSMilanka Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_EC_NR_FUNCTION); 1868aeb0f0feSMatthias Ringwald int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_EC_NR_FUNCTION); 18696ba83b5eSMilanka Ringwald return hf && ag; 1870ce263fc8SMatthias Ringwald } 1871ce263fc8SMatthias Ringwald 18723c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){ 18739c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1874a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18753c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1876a33eb0c4SMilanka Ringwald } 18776ba83b5eSMilanka Ringwald if (!hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection)){ 18786ba83b5eSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 18796ba83b5eSMilanka Ringwald } 1880ce263fc8SMatthias Ringwald 1881a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1; 18821c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18833c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1884ce263fc8SMatthias Ringwald } 1885ce263fc8SMatthias Ringwald 1886acd11d4aSMilanka Ringwald uint8_t hfp_hf_activate_voice_recognition(hci_con_handle_t acl_handle){ 1887fdda66c0SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1888fdda66c0SMilanka Ringwald if (!hfp_connection) { 1889fdda66c0SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1890be55a11dSMilanka Ringwald } 1891013cc750SMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){ 1892013cc750SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1893013cc750SMilanka Ringwald } 1894acd11d4aSMilanka Ringwald 1895acd11d4aSMilanka Ringwald bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection); 1896acd11d4aSMilanka Ringwald bool legacy_vra_supported = hfp_hf_vra_flag_supported(hfp_connection); 1897acd11d4aSMilanka Ringwald if (!enhanced_vra_supported && !legacy_vra_supported){ 1898acd11d4aSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1899af97579eSMilanka Ringwald } 1900af97579eSMilanka Ringwald 1901498a8121SMilanka Ringwald switch (hfp_connection->vra_state){ 1902be55a11dSMilanka Ringwald case HFP_VRA_VOICE_RECOGNITION_OFF: 1903de9e0ea7SMilanka Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 1904fd4151d1SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED; 1905acd11d4aSMilanka Ringwald hfp_connection->enhanced_voice_recognition_enabled = enhanced_vra_supported; 1906be55a11dSMilanka Ringwald break; 1907de9e0ea7SMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_OFF: 1908de9e0ea7SMilanka Ringwald hfp_connection->activate_voice_recognition = true; 1909de9e0ea7SMilanka Ringwald break; 1910be55a11dSMilanka Ringwald default: 1911be55a11dSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1912be55a11dSMilanka Ringwald } 1913ce263fc8SMatthias Ringwald 1914af97579eSMilanka Ringwald hfp_hf_run_for_context(hfp_connection); 1915fdda66c0SMilanka Ringwald return ERROR_CODE_SUCCESS; 1916af97579eSMilanka Ringwald } 1917af97579eSMilanka Ringwald 1918acd11d4aSMilanka Ringwald uint8_t hfp_hf_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle){ 1919af97579eSMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1920af97579eSMilanka Ringwald if (!hfp_connection) { 1921af97579eSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1922af97579eSMilanka Ringwald } 192384fb9ac1SMilanka Ringwald 192484fb9ac1SMilanka Ringwald if (hfp_connection->emit_vra_enabled_after_audio_established){ 192584fb9ac1SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 192684fb9ac1SMilanka Ringwald } 192784fb9ac1SMilanka Ringwald 1928acd11d4aSMilanka Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){ 192908a0b01cSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 193008a0b01cSMilanka Ringwald } 1931acd11d4aSMilanka Ringwald 1932acd11d4aSMilanka Ringwald bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection); 1933acd11d4aSMilanka Ringwald if (!enhanced_vra_supported){ 1934acd11d4aSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1935acd11d4aSMilanka Ringwald } 1936acd11d4aSMilanka Ringwald 1937acd11d4aSMilanka Ringwald switch (hfp_connection->vra_state){ 1938acd11d4aSMilanka Ringwald case HFP_VRA_VOICE_RECOGNITION_ACTIVATED: 1939acd11d4aSMilanka Ringwald case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1940acd11d4aSMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 1941acd11d4aSMilanka Ringwald break; 1942acd11d4aSMilanka Ringwald default: 1943fdda66c0SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1944af97579eSMilanka Ringwald } 1945013cc750SMilanka Ringwald 1946acd11d4aSMilanka Ringwald hfp_hf_run_for_context(hfp_connection); 1947acd11d4aSMilanka Ringwald return ERROR_CODE_SUCCESS; 1948acd11d4aSMilanka Ringwald } 1949acd11d4aSMilanka Ringwald 1950acd11d4aSMilanka Ringwald 1951acd11d4aSMilanka Ringwald uint8_t hfp_hf_deactivate_voice_recognition(hci_con_handle_t acl_handle){ 1952acd11d4aSMilanka Ringwald // return deactivate_voice_recognition(acl_handle, false); 1953acd11d4aSMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1954acd11d4aSMilanka Ringwald if (!hfp_connection) { 1955acd11d4aSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1956acd11d4aSMilanka Ringwald } 1957acd11d4aSMilanka Ringwald 195884fb9ac1SMilanka Ringwald if (hfp_connection->emit_vra_enabled_after_audio_established){ 195984fb9ac1SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 196084fb9ac1SMilanka Ringwald } 196184fb9ac1SMilanka Ringwald 1962acd11d4aSMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || 1963acd11d4aSMilanka Ringwald hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){ 1964acd11d4aSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1965acd11d4aSMilanka Ringwald } 1966acd11d4aSMilanka Ringwald 1967acd11d4aSMilanka Ringwald bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection); 1968acd11d4aSMilanka Ringwald bool legacy_vra_supported = hfp_hf_vra_flag_supported(hfp_connection); 1969acd11d4aSMilanka Ringwald if (!enhanced_vra_supported && !legacy_vra_supported){ 1970acd11d4aSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1971acd11d4aSMilanka Ringwald } 1972acd11d4aSMilanka Ringwald 1973fdda66c0SMilanka Ringwald switch (hfp_connection->vra_state){ 1974de9e0ea7SMilanka Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED: 1975fdda66c0SMilanka Ringwald case HFP_VRA_VOICE_RECOGNITION_ACTIVATED: 1976de9e0ea7SMilanka Ringwald case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1977de9e0ea7SMilanka Ringwald case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1978fdda66c0SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF; 1979fdda66c0SMilanka Ringwald break; 1980de9e0ea7SMilanka Ringwald 1981de9e0ea7SMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED: 1982de9e0ea7SMilanka Ringwald case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1983de9e0ea7SMilanka Ringwald hfp_connection->deactivate_voice_recognition = true; 1984de9e0ea7SMilanka Ringwald break; 1985de9e0ea7SMilanka Ringwald 1986de9e0ea7SMilanka Ringwald case HFP_VRA_VOICE_RECOGNITION_OFF: 1987de9e0ea7SMilanka Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 1988de9e0ea7SMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_OFF: 1989fdda66c0SMilanka Ringwald default: 1990fdda66c0SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1991fdda66c0SMilanka Ringwald } 1992fdda66c0SMilanka Ringwald 1993fdda66c0SMilanka Ringwald hfp_hf_run_for_context(hfp_connection); 1994fdda66c0SMilanka Ringwald return ERROR_CODE_SUCCESS; 1995af97579eSMilanka Ringwald } 1996af97579eSMilanka Ringwald 19973c65e705SMilanka Ringwald uint8_t hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 19989c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1999a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20003c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2001a33eb0c4SMilanka Ringwald } 2002c8626498SMilanka Ringwald 20033c65e705SMilanka Ringwald if (hfp_connection->microphone_gain == gain) { 20043c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 20053c65e705SMilanka Ringwald } 20063c65e705SMilanka Ringwald 2007c1ab6cc1SMatthias Ringwald if ((gain < 0) || (gain > 15)){ 2008a0ffb263SMatthias Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 20093c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2010a0ffb263SMatthias Ringwald } 20113c65e705SMilanka Ringwald 2012a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = gain; 2013a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 20141c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20153c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2016ce263fc8SMatthias Ringwald } 2017ce263fc8SMatthias Ringwald 20183c65e705SMilanka Ringwald uint8_t hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 20199c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2020a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20213c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2022a33eb0c4SMilanka Ringwald } 2023c8626498SMilanka Ringwald 20243c65e705SMilanka Ringwald if (hfp_connection->speaker_gain == gain){ 20253c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 20263c65e705SMilanka Ringwald } 20273c65e705SMilanka Ringwald 2028c1ab6cc1SMatthias Ringwald if ((gain < 0) || (gain > 15)){ 2029a0ffb263SMatthias Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 20303c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2031a0ffb263SMatthias Ringwald } 20323c65e705SMilanka Ringwald 2033a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = gain; 2034a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 20351c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20363c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2037ce263fc8SMatthias Ringwald } 2038ce263fc8SMatthias Ringwald 20393c65e705SMilanka Ringwald uint8_t hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){ 20409c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2041a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20423c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2043a33eb0c4SMilanka Ringwald } 2044a0ffb263SMatthias Ringwald hfp_connection->hf_send_dtmf_code = code; 20451c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20463c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2047ce263fc8SMatthias Ringwald } 2048ce263fc8SMatthias Ringwald 20493c65e705SMilanka Ringwald uint8_t hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 20509c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2051a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20523c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2053a33eb0c4SMilanka Ringwald } 2054a0ffb263SMatthias Ringwald hfp_connection->hf_send_binp = 1; 20551c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20563c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2057ce263fc8SMatthias Ringwald } 20583deb3ec6SMatthias Ringwald 20593c65e705SMilanka Ringwald uint8_t hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){ 20609c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2061a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20623c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2063a33eb0c4SMilanka Ringwald } 2064a0ffb263SMatthias Ringwald hfp_connection->hf_send_clcc = 1; 20651c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20663c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2067667ec068SMatthias Ringwald } 2068667ec068SMatthias Ringwald 2069667ec068SMatthias Ringwald 20703c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){ 20719c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2072a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20733c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2074a33eb0c4SMilanka Ringwald } 2075a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 2076a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '?'; 20771c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20783c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2079667ec068SMatthias Ringwald } 2080667ec068SMatthias Ringwald 20813c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){ 20829c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2083a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20843c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2085a33eb0c4SMilanka Ringwald } 2086a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 2087a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '0'; 20881c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20893c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2090667ec068SMatthias Ringwald } 2091667ec068SMatthias Ringwald 20923c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){ 20939c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2094a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20953c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2096a33eb0c4SMilanka Ringwald } 2097a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 2098a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '1'; 20991c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 21003c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2101667ec068SMatthias Ringwald } 2102667ec068SMatthias Ringwald 21033c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){ 21049c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2105a33eb0c4SMilanka Ringwald if (!hfp_connection) { 21063c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2107a33eb0c4SMilanka Ringwald } 2108a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 2109a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '2'; 21101c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 21113c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2112667ec068SMatthias Ringwald } 2113667ec068SMatthias Ringwald 21143c65e705SMilanka Ringwald uint8_t hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){ 21159c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2116a33eb0c4SMilanka Ringwald if (!hfp_connection) { 21173c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2118a33eb0c4SMilanka Ringwald } 2119a0ffb263SMatthias Ringwald hfp_connection->hf_send_cnum = 1; 21201c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 21213c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2122667ec068SMatthias Ringwald } 2123667ec068SMatthias Ringwald 21243c65e705SMilanka Ringwald uint8_t hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){ 21259c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2126a33eb0c4SMilanka Ringwald if (!hfp_connection) { 21273c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2128a33eb0c4SMilanka Ringwald } 2129667ec068SMatthias Ringwald // find index for assigned number 2130667ec068SMatthias Ringwald int i; 2131aeb0f0feSMatthias Ringwald for (i = 0; i < hfp_hf_indicators_nr ; i++){ 2132aeb0f0feSMatthias Ringwald if (hfp_hf_indicators[i] == assigned_number){ 2133667ec068SMatthias Ringwald // set value 2134aeb0f0feSMatthias Ringwald hfp_hf_indicators_value[i] = value; 2135667ec068SMatthias Ringwald // mark for update 2136a0ffb263SMatthias Ringwald if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){ 2137a0ffb263SMatthias Ringwald hfp_connection->generic_status_update_bitmap |= (1<<i); 2138667ec068SMatthias Ringwald // send update 21391c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 2140a0ffb263SMatthias Ringwald } 21413c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2142667ec068SMatthias Ringwald } 2143667ec068SMatthias Ringwald } 21443c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2145667ec068SMatthias Ringwald } 2146667ec068SMatthias Ringwald 2147d7f6b5cbSMatthias Ringwald int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){ 2148d7f6b5cbSMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2149d7f6b5cbSMatthias Ringwald if (!hfp_connection) { 2150d7f6b5cbSMatthias Ringwald return 0; 2151d7f6b5cbSMatthias Ringwald } 2152d7f6b5cbSMatthias Ringwald return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE); 2153d7f6b5cbSMatthias Ringwald } 215476cc1527SMatthias Ringwald 215576cc1527SMatthias 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){ 215676cc1527SMatthias Ringwald if (!name){ 2157aeb0f0feSMatthias Ringwald name = hfp_hf_default_service_name; 215876cc1527SMatthias Ringwald } 215976cc1527SMatthias Ringwald hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name); 216076cc1527SMatthias Ringwald 216176cc1527SMatthias Ringwald // Construct SupportedFeatures for SDP bitmap: 216276cc1527SMatthias Ringwald // 216376cc1527SMatthias Ringwald // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 216476cc1527SMatthias Ringwald // of the Bits 0 to 4 of the unsolicited result code +BRSF" 216576cc1527SMatthias Ringwald // 216676cc1527SMatthias Ringwald // Wide band speech (bit 5) requires Codec negotiation 216776cc1527SMatthias Ringwald // 216876cc1527SMatthias Ringwald uint16_t sdp_features = supported_features & 0x1f; 2169ef3ae4ebSMilanka Ringwald if ( (wide_band_speech != 0) && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){ 217076cc1527SMatthias Ringwald sdp_features |= 1 << 5; 217176cc1527SMatthias Ringwald } 2172ef3ae4ebSMilanka Ringwald 2173ef3ae4ebSMilanka Ringwald if (supported_features & (1 << HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS)){ 217456f1adacSMilanka Ringwald sdp_features |= 1 << 6; 2175ef3ae4ebSMilanka Ringwald } 2176ef3ae4ebSMilanka Ringwald 2177ef3ae4ebSMilanka Ringwald if (supported_features & (1 << HFP_HFSF_VOICE_RECOGNITION_TEXT)){ 217856f1adacSMilanka Ringwald sdp_features |= 1 << 7; 2179ef3ae4ebSMilanka Ringwald } 2180ef3ae4ebSMilanka Ringwald 218176cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 218276cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 218376cc1527SMatthias Ringwald } 218476cc1527SMatthias Ringwald 218576cc1527SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){ 218668466199SMilanka Ringwald btstack_assert(callback != NULL); 218768466199SMilanka Ringwald 218876cc1527SMatthias Ringwald hfp_hf_callback = callback; 218976cc1527SMatthias Ringwald hfp_set_hf_callback(callback); 219076cc1527SMatthias Ringwald } 2191