13deb3ec6SMatthias Ringwald /* 23deb3ec6SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 33deb3ec6SMatthias Ringwald * 43deb3ec6SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 53deb3ec6SMatthias Ringwald * modification, are permitted provided that the following conditions 63deb3ec6SMatthias Ringwald * are met: 73deb3ec6SMatthias Ringwald * 83deb3ec6SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 93deb3ec6SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 103deb3ec6SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 113deb3ec6SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 123deb3ec6SMatthias Ringwald * documentation and/or other materials provided with the distribution. 133deb3ec6SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 143deb3ec6SMatthias Ringwald * contributors may be used to endorse or promote products derived 153deb3ec6SMatthias Ringwald * from this software without specific prior written permission. 163deb3ec6SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 173deb3ec6SMatthias Ringwald * personal benefit and not for any commercial purpose or for 183deb3ec6SMatthias Ringwald * monetary gain. 193deb3ec6SMatthias Ringwald * 203deb3ec6SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 213deb3ec6SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 223deb3ec6SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 232fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 242fca4dadSMilanka Ringwald * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 253deb3ec6SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 263deb3ec6SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 273deb3ec6SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 283deb3ec6SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 293deb3ec6SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 303deb3ec6SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 313deb3ec6SMatthias Ringwald * SUCH DAMAGE. 323deb3ec6SMatthias Ringwald * 333deb3ec6SMatthias Ringwald * Please inquire about commercial licensing options at 343deb3ec6SMatthias Ringwald * [email protected] 353deb3ec6SMatthias Ringwald * 363deb3ec6SMatthias Ringwald */ 37ab2c6ae4SMatthias Ringwald 38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "hfp_hf.c" 393deb3ec6SMatthias Ringwald 403deb3ec6SMatthias Ringwald // ***************************************************************************** 413deb3ec6SMatthias Ringwald // 42fffdd288SMatthias Ringwald // HFP Hands-Free (HF) unit 433deb3ec6SMatthias Ringwald // 443deb3ec6SMatthias Ringwald // ***************************************************************************** 453deb3ec6SMatthias Ringwald 467907f069SMatthias Ringwald #include "btstack_config.h" 473deb3ec6SMatthias Ringwald 483deb3ec6SMatthias Ringwald #include <stdint.h> 493cfa4086SMatthias Ringwald #include <stdio.h> 503deb3ec6SMatthias Ringwald #include <string.h> 513deb3ec6SMatthias Ringwald 52235946f1SMatthias Ringwald #include "bluetooth_sdp.h" 5359c6af15SMatthias Ringwald #include "btstack_debug.h" 54d4dd47ffSMatthias Ringwald #include "btstack_event.h" 553deb3ec6SMatthias Ringwald #include "btstack_memory.h" 5659c6af15SMatthias Ringwald #include "btstack_run_loop.h" 5759c6af15SMatthias Ringwald #include "classic/core.h" 5859c6af15SMatthias Ringwald #include "classic/hfp.h" 5959c6af15SMatthias Ringwald #include "classic/hfp_hf.h" 60efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h" 61746ccb7eSMatthias Ringwald #include "classic/sdp_server.h" 62023f2764SMatthias Ringwald #include "classic/sdp_util.h" 6359c6af15SMatthias Ringwald #include "hci.h" 6459c6af15SMatthias Ringwald #include "hci_cmd.h" 6559c6af15SMatthias Ringwald #include "hci_dump.h" 6659c6af15SMatthias Ringwald #include "l2cap.h" 673deb3ec6SMatthias Ringwald 6820b2edb6SMatthias Ringwald // const 69aeb0f0feSMatthias Ringwald static const char hfp_hf_default_service_name[] = "Hands-Free unit"; 7020b2edb6SMatthias Ringwald 7120b2edb6SMatthias Ringwald // globals 72aeb0f0feSMatthias Ringwald 73aeb0f0feSMatthias Ringwald // higher layer callbacks 74aeb0f0feSMatthias Ringwald static btstack_packet_handler_t hfp_hf_callback; 75aeb0f0feSMatthias Ringwald 761c6a0fc0SMatthias Ringwald static btstack_packet_callback_registration_t hfp_hf_hci_event_callback_registration; 7727950165SMatthias Ringwald 78aeb0f0feSMatthias Ringwald static uint16_t hfp_hf_supported_features; 79aeb0f0feSMatthias Ringwald static uint8_t hfp_hf_codecs_nr; 80aeb0f0feSMatthias Ringwald static uint8_t hfp_hf_codecs[HFP_MAX_NUM_CODECS]; 813deb3ec6SMatthias Ringwald 82aeb0f0feSMatthias Ringwald static uint8_t hfp_hf_indicators_nr; 83aeb0f0feSMatthias Ringwald static uint8_t hfp_hf_indicators[HFP_MAX_NUM_INDICATORS]; 84aeb0f0feSMatthias Ringwald static uint32_t hfp_hf_indicators_value[HFP_MAX_NUM_INDICATORS]; 85667ec068SMatthias Ringwald 8620b2edb6SMatthias Ringwald static uint8_t hfp_hf_speaker_gain; 8720b2edb6SMatthias Ringwald static uint8_t hfp_hf_microphone_gain; 883deb3ec6SMatthias Ringwald 89aeb0f0feSMatthias Ringwald static hfp_call_status_t hfp_hf_call_status; 90aeb0f0feSMatthias Ringwald static hfp_callsetup_status_t hfp_hf_callsetup_status; 91aeb0f0feSMatthias Ringwald static hfp_callheld_status_t hfp_hf_callheld_status; 923deb3ec6SMatthias Ringwald 93aeb0f0feSMatthias Ringwald static char hfp_hf_phone_number[25]; 94ce263fc8SMatthias Ringwald 95ce263fc8SMatthias Ringwald 9676cc1527SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){ 97aeb0f0feSMatthias Ringwald int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_CODEC_NEGOTIATION); 9876cc1527SMatthias Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION); 9976cc1527SMatthias Ringwald return hf && ag; 10076cc1527SMatthias Ringwald } 10176cc1527SMatthias Ringwald 10276cc1527SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){ 103aeb0f0feSMatthias Ringwald int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_THREE_WAY_CALLING); 10476cc1527SMatthias Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING); 10576cc1527SMatthias Ringwald return hf && ag; 10676cc1527SMatthias Ringwald } 10776cc1527SMatthias Ringwald 10876cc1527SMatthias Ringwald 10976cc1527SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){ 110aeb0f0feSMatthias Ringwald int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_HF_INDICATORS); 11176cc1527SMatthias Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS); 11276cc1527SMatthias Ringwald return hf && ag; 11376cc1527SMatthias Ringwald } 11476cc1527SMatthias Ringwald 115fcf4ede6SMilanka Ringwald static bool hfp_hf_vra_flag_supported(hfp_connection_t * hfp_connection){ 116fcf4ede6SMilanka Ringwald int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION); 117fcf4ede6SMilanka Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION); 118fcf4ede6SMilanka Ringwald return hf && ag; 119fcf4ede6SMilanka Ringwald } 120fcf4ede6SMilanka Ringwald 121fcf4ede6SMilanka Ringwald static bool hfp_hf_enhanced_vra_flag_supported(hfp_connection_t * hfp_connection){ 122fcf4ede6SMilanka Ringwald int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS); 123fcf4ede6SMilanka Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS); 124fcf4ede6SMilanka Ringwald return hf && ag; 125fcf4ede6SMilanka Ringwald } 12676cc1527SMatthias Ringwald 1279c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_hf_connection_context_for_acl_handle(uint16_t handle){ 1289c9c64c1SMatthias Ringwald btstack_linked_list_iterator_t it; 1299c9c64c1SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1309c9c64c1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1319c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1329c9c64c1SMatthias Ringwald if (hfp_connection->acl_handle != handle) continue; 1339c9c64c1SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_HF) continue; 1349c9c64c1SMatthias Ringwald return hfp_connection; 1359c9c64c1SMatthias Ringwald } 1369c9c64c1SMatthias Ringwald return NULL; 1379c9c64c1SMatthias Ringwald } 1389c9c64c1SMatthias Ringwald 139c10fde09SMatthias Ringwald /* emit functions */ 1403deb3ec6SMatthias Ringwald 141a473a009SMatthias Ringwald static void hfp_hf_emit_subscriber_information(const hfp_connection_t * hfp_connection, uint8_t status){ 142a473a009SMatthias Ringwald if (hfp_hf_callback == NULL) return; 143c10fde09SMatthias Ringwald uint16_t bnip_number_len = btstack_min(strlen(hfp_connection->bnip_number), sizeof(hfp_connection->bnip_number)-1); 144c10fde09SMatthias Ringwald uint8_t event[7 + sizeof(hfp_connection->bnip_number)]; 145a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 146c10fde09SMatthias Ringwald event[1] = 6 + bnip_number_len; 147a473a009SMatthias Ringwald event[2] = HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION; 148d703d377SMatthias Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 149d703d377SMatthias Ringwald event[5] = status; 150d703d377SMatthias Ringwald event[6] = hfp_connection->bnip_type; 151c10fde09SMatthias Ringwald memcpy(&event[7], hfp_connection->bnip_number, bnip_number_len); 152c10fde09SMatthias Ringwald event[7 + bnip_number_len] = 0; 153c10fde09SMatthias Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, 8 + bnip_number_len); 154a0ffb263SMatthias Ringwald } 155a0ffb263SMatthias Ringwald 156*598d4936SMatthias Ringwald static void hfp_hf_emit_type_number_alpha(const hfp_connection_t * hfp_connection, uint8_t event_subtype){ 157a473a009SMatthias Ringwald if (hfp_hf_callback == NULL) return; 158c10fde09SMatthias Ringwald uint16_t bnip_number_len = btstack_min(strlen(hfp_connection->bnip_number), sizeof(hfp_connection->bnip_number)-1); 159*598d4936SMatthias Ringwald // 10 fixed - 1 (bnip_number_len <= sizeof(hfp_connection->bnip_number)-1) + 1 (trailing \0 for line buffer) 160*598d4936SMatthias Ringwald uint8_t event[10 + sizeof(hfp_connection->bnip_number) + sizeof(hfp_connection->line_buffer)]; 161*598d4936SMatthias Ringwald uint8_t alpha_len = hfp_connection->clip_have_alpha ? strlen((const char *) hfp_connection->line_buffer) : 0; 162*598d4936SMatthias Ringwald uint8_t pos = 0; 163*598d4936SMatthias Ringwald event[pos++] = HCI_EVENT_HFP_META; 164*598d4936SMatthias Ringwald event[pos++] = 8 + bnip_number_len + alpha_len; 165*598d4936SMatthias Ringwald event[pos++] = event_subtype; 166d703d377SMatthias Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 167*598d4936SMatthias Ringwald pos += 2; 168*598d4936SMatthias Ringwald event[pos++] = hfp_connection->bnip_type; 169*598d4936SMatthias Ringwald event[pos++] = bnip_number_len; 170*598d4936SMatthias Ringwald memcpy(&event[7], hfp_connection->bnip_number, bnip_number_len); 171*598d4936SMatthias Ringwald pos += bnip_number_len; 172*598d4936SMatthias Ringwald event[pos++] = 0; 173*598d4936SMatthias Ringwald event[pos++] = alpha_len; 174*598d4936SMatthias Ringwald memcpy(&event[pos], hfp_connection->line_buffer, alpha_len); 175*598d4936SMatthias Ringwald pos += alpha_len; 176*598d4936SMatthias Ringwald event[pos++] = 0; 177*598d4936SMatthias Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, pos); 178a0ffb263SMatthias Ringwald } 179a0ffb263SMatthias Ringwald 180a473a009SMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(const hfp_connection_t * hfp_connection){ 181a473a009SMatthias Ringwald if (hfp_hf_callback == NULL) return; 182c10fde09SMatthias Ringwald uint16_t bnip_number_len = btstack_min(strlen(hfp_connection->bnip_number), sizeof(hfp_connection->bnip_number)-1); 183c10fde09SMatthias Ringwald uint8_t event[11 + sizeof(hfp_connection->bnip_number)]; 184c10fde09SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 185c10fde09SMatthias Ringwald event[1] = 10 + bnip_number_len; 186c10fde09SMatthias Ringwald event[2] = HFP_SUBEVENT_ENHANCED_CALL_STATUS; 187c10fde09SMatthias Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 188c10fde09SMatthias Ringwald event[4] = hfp_connection->clcc_idx; 189c10fde09SMatthias Ringwald event[5] = hfp_connection->clcc_dir; 190c10fde09SMatthias Ringwald event[6] = hfp_connection->clcc_status; 191c10fde09SMatthias Ringwald event[7] = hfp_connection->clcc_mode; 192c10fde09SMatthias Ringwald event[8] = hfp_connection->clcc_mpty; 193c10fde09SMatthias Ringwald event[9] = hfp_connection->bnip_type; 194c10fde09SMatthias Ringwald memcpy(&event[10], hfp_connection->bnip_number, bnip_number_len); 195c10fde09SMatthias Ringwald event[11+bnip_number_len] = 0; 196c10fde09SMatthias Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, 12+bnip_number_len); 197a0ffb263SMatthias Ringwald } 198a0ffb263SMatthias Ringwald 1991ac1f60fSMilanka Ringwald static void hfp_emit_ag_indicator_mapping_event(const hfp_connection_t * hfp_connection, const hfp_ag_indicator_t * indicator){ 2001ac1f60fSMilanka Ringwald if (hfp_hf_callback == NULL) return; 201c10fde09SMatthias Ringwald uint8_t event[8 + HFP_MAX_INDICATOR_DESC_SIZE]; 202c10fde09SMatthias Ringwald uint16_t indicator_len = btstack_min(strlen(indicator->name), HFP_MAX_INDICATOR_DESC_SIZE-1); 203c10fde09SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 204c10fde09SMatthias Ringwald event[1] = 7 + indicator_len; 205c10fde09SMatthias Ringwald event[2] = HFP_SUBEVENT_AG_INDICATOR_MAPPING; 206c10fde09SMatthias Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 207c10fde09SMatthias Ringwald event[5] = indicator->index; 208c10fde09SMatthias Ringwald event[6] = indicator->min_range; 209c10fde09SMatthias Ringwald event[7] = indicator->max_range; 210c10fde09SMatthias Ringwald memcpy(&event[8], indicator->name, indicator_len); 211c10fde09SMatthias Ringwald event[8+indicator_len] = 0; 212c10fde09SMatthias Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, 9 + indicator_len); 2131ac1f60fSMilanka Ringwald } 21476cc1527SMatthias Ringwald 215ce3797e1SMilanka Ringwald static void hfp_emit_ag_indicator_status_event(const hfp_connection_t * hfp_connection, const hfp_ag_indicator_t * indicator){ 216a473a009SMatthias Ringwald if (hfp_hf_callback == NULL) return; 217c10fde09SMatthias Ringwald uint8_t event[12+HFP_MAX_INDICATOR_DESC_SIZE]; 218c10fde09SMatthias Ringwald uint16_t indicator_len = btstack_min(strlen(indicator->name), HFP_MAX_INDICATOR_DESC_SIZE-1); 219c10fde09SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 220c10fde09SMatthias Ringwald event[1] = 11 + indicator_len; 221c10fde09SMatthias Ringwald event[2] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED; 222c10fde09SMatthias Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 223c10fde09SMatthias Ringwald event[5] = indicator->index; 224c10fde09SMatthias Ringwald event[6] = indicator->status; 225c10fde09SMatthias Ringwald event[7] = indicator->min_range; 226c10fde09SMatthias Ringwald event[8] = indicator->max_range; 227c10fde09SMatthias Ringwald event[9] = indicator->mandatory; 228c10fde09SMatthias Ringwald event[10] = indicator->enabled; 229c10fde09SMatthias Ringwald event[11] = indicator->status_changed; 230c10fde09SMatthias Ringwald memcpy(&event[12], indicator->name, indicator_len); 231c10fde09SMatthias Ringwald event[12+indicator_len] = 0; 232c10fde09SMatthias Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, 13 + indicator_len); 2333deb3ec6SMatthias Ringwald } 2343deb3ec6SMatthias Ringwald 235a473a009SMatthias Ringwald static void hfp_emit_network_operator_event(const hfp_connection_t * hfp_connection){ 236a473a009SMatthias Ringwald if (hfp_hf_callback == NULL) return; 237c10fde09SMatthias Ringwald uint16_t operator_len = btstack_min(strlen(hfp_connection->network_operator.name), HFP_MAX_NETWORK_OPERATOR_NAME_SIZE-1); 238c10fde09SMatthias Ringwald uint8_t event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE]; 23976cc1527SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 24076cc1527SMatthias Ringwald event[1] = sizeof(event) - 2; 241c10fde09SMatthias Ringwald event[2] = 6 + operator_len; 242d703d377SMatthias Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 243d703d377SMatthias Ringwald event[5] = hfp_connection->network_operator.mode; 244d703d377SMatthias Ringwald event[6] = hfp_connection->network_operator.format; 245c10fde09SMatthias Ringwald memcpy(&event[7], hfp_connection->network_operator.name, operator_len); 246c10fde09SMatthias Ringwald event[7+operator_len] = 0; 247c10fde09SMatthias Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, 8 + operator_len); 2483deb3ec6SMatthias Ringwald } 2493deb3ec6SMatthias Ringwald 250b95cac54SMilanka Ringwald 251b95cac54SMilanka Ringwald static void hfp_hf_emit_enhanced_voice_recognition_text(hfp_connection_t * hfp_connection){ 252b95cac54SMilanka Ringwald btstack_assert(hfp_connection != NULL); 25351aa5d5aSMilanka Ringwald uint8_t event[HFP_MAX_VR_TEXT_SIZE + 11]; 254b95cac54SMilanka Ringwald int pos = 0; 255b95cac54SMilanka Ringwald event[pos++] = HCI_EVENT_HFP_META; 256b95cac54SMilanka Ringwald event[pos++] = sizeof(event) - 2; 257b95cac54SMilanka Ringwald event[pos++] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_MESSAGE; 258b95cac54SMilanka Ringwald little_endian_store_16(event, pos, hfp_connection->acl_handle); 259b95cac54SMilanka Ringwald pos += 2; 260b95cac54SMilanka Ringwald little_endian_store_16(event, pos, hfp_connection->ag_msg.text_id); 261b95cac54SMilanka Ringwald pos += 2; 262b95cac54SMilanka Ringwald event[pos++] = hfp_connection->ag_msg.text_type; 263e83f1be7SMilanka Ringwald event[pos++] = hfp_connection->ag_msg.text_operation; 264b95cac54SMilanka Ringwald 26551aa5d5aSMilanka Ringwald // length, zero ending is already in message 266b95cac54SMilanka Ringwald uint8_t * value = &hfp_connection->line_buffer[0]; 26751aa5d5aSMilanka Ringwald uint16_t value_length = hfp_connection->ag_vra_msg_length; 26851aa5d5aSMilanka Ringwald 26951aa5d5aSMilanka Ringwald little_endian_store_16(event, pos, value_length); 270b95cac54SMilanka Ringwald pos += 2; 27151aa5d5aSMilanka Ringwald memcpy(&event[pos], value, value_length); 27251aa5d5aSMilanka Ringwald pos += value_length; 27351aa5d5aSMilanka Ringwald 274b95cac54SMilanka Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, pos); 275b95cac54SMilanka Ringwald } 276b95cac54SMilanka Ringwald 27776cc1527SMatthias Ringwald /* send commands */ 27889425bfcSMilanka Ringwald 27989425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){ 2803deb3ec6SMatthias Ringwald char buffer[20]; 2811599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s\r", cmd); 28289425bfcSMilanka Ringwald return send_str_over_rfcomm(cid, buffer); 28389425bfcSMilanka Ringwald } 28489425bfcSMilanka Ringwald 28589425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){ 28689425bfcSMilanka Ringwald char buffer[20]; 2871599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s%s\r", cmd, mark); 28889425bfcSMilanka Ringwald return send_str_over_rfcomm(cid, buffer); 28989425bfcSMilanka Ringwald } 29089425bfcSMilanka Ringwald 29186da9d74SMatthias Ringwald static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint16_t value){ 29289425bfcSMilanka Ringwald char buffer[40]; 2931599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s=%d\r", cmd, value); 2943deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2953deb3ec6SMatthias Ringwald } 2963deb3ec6SMatthias Ringwald 2973deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){ 2983deb3ec6SMatthias Ringwald char buffer[30]; 29989425bfcSMilanka Ringwald const int size = sizeof(buffer); 30089425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS); 301aeb0f0feSMatthias Ringwald offset += join(buffer+offset, size-offset, hfp_hf_codecs, hfp_hf_codecs_nr); 3021599fe57SMatthias Ringwald offset += snprintf(buffer+offset, size-offset, "\r"); 3033deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 3043deb3ec6SMatthias Ringwald } 3053deb3ec6SMatthias Ringwald 3063deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){ 3073deb3ec6SMatthias Ringwald char buffer[50]; 30889425bfcSMilanka Ringwald const int size = sizeof(buffer); 30989425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS); 31089425bfcSMilanka Ringwald offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr); 3111599fe57SMatthias Ringwald offset += snprintf(buffer+offset, size-offset, "\r"); 3123deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 3133deb3ec6SMatthias Ringwald } 3143deb3ec6SMatthias Ringwald 3153deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){ 3163deb3ec6SMatthias Ringwald char buffer[30]; 31789425bfcSMilanka Ringwald const int size = sizeof(buffer); 31889425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR); 319aeb0f0feSMatthias Ringwald offset += join(buffer+offset, size-offset, hfp_hf_indicators, hfp_hf_indicators_nr); 3201599fe57SMatthias Ringwald offset += snprintf(buffer+offset, size-offset, "\r"); 3213deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 3223deb3ec6SMatthias Ringwald } 3233deb3ec6SMatthias Ringwald 32489425bfcSMilanka Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){ 3253deb3ec6SMatthias Ringwald char buffer[20]; 3261599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate); 327ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 328ce263fc8SMatthias Ringwald } 329ce263fc8SMatthias Ringwald 330ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){ 331ce263fc8SMatthias Ringwald char buffer[40]; 332aeb0f0feSMatthias Ringwald snprintf(buffer, sizeof(buffer), "%s%s;\r", HFP_CALL_PHONE_NUMBER, hfp_hf_phone_number); 333ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 334ce263fc8SMatthias Ringwald } 335ce263fc8SMatthias Ringwald 336a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){ 337ce263fc8SMatthias Ringwald char buffer[40]; 3381599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "%s>%d;\r", HFP_CALL_PHONE_NUMBER, memory_id); 339ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 340ce263fc8SMatthias Ringwald } 341ce263fc8SMatthias Ringwald 342f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){ 34389425bfcSMilanka Ringwald char buffer[40]; 3441599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s=%u\r", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number); 345ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 346ce263fc8SMatthias Ringwald } 347ce263fc8SMatthias Ringwald 348ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){ 349ce263fc8SMatthias Ringwald char buffer[20]; 3501599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s=%c\r", HFP_TRANSMIT_DTMF_CODES, code); 351ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 352ce263fc8SMatthias Ringwald } 353ce263fc8SMatthias Ringwald 35497d2cadbSMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){ 3551599fe57SMatthias Ringwald return send_str_over_rfcomm(cid, (char *) "ATA\r"); 35697d2cadbSMatthias Ringwald } 35797d2cadbSMatthias Ringwald 35889425bfcSMilanka Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){ 359aeb0f0feSMatthias Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_hf_supported_features); 36089425bfcSMilanka Ringwald } 36189425bfcSMilanka Ringwald 36289425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators(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_indicators_status(uint16_t cid){ 36789425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?"); 36889425bfcSMilanka Ringwald } 36989425bfcSMilanka Ringwald 37089425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){ 37189425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?"); 37289425bfcSMilanka Ringwald } 37389425bfcSMilanka Ringwald 37489425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_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_list_initital_supported_generic_status_indicators(uint16_t cid){ 37989425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?"); 38089425bfcSMilanka Ringwald } 38189425bfcSMilanka Ringwald 38289425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){ 38389425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0"); 38489425bfcSMilanka Ringwald } 38589425bfcSMilanka Ringwald 38689425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){ 38789425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?"); 38889425bfcSMilanka Ringwald } 38989425bfcSMilanka Ringwald 39089425bfcSMilanka Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){ 39189425bfcSMilanka Ringwald return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP); 39289425bfcSMilanka Ringwald } 39389425bfcSMilanka Ringwald 39489425bfcSMilanka Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){ 39589425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain); 39689425bfcSMilanka Ringwald } 39789425bfcSMilanka Ringwald 39889425bfcSMilanka Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){ 39989425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain); 40089425bfcSMilanka Ringwald } 40189425bfcSMilanka Ringwald 40289425bfcSMilanka Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){ 40389425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate); 40489425bfcSMilanka Ringwald } 40589425bfcSMilanka Ringwald 40689425bfcSMilanka Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){ 40789425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate); 40889425bfcSMilanka Ringwald } 40989425bfcSMilanka Ringwald 41089425bfcSMilanka Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){ 41189425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate); 41289425bfcSMilanka Ringwald } 41389425bfcSMilanka Ringwald 41489425bfcSMilanka Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){ 41589425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec); 41689425bfcSMilanka Ringwald } 41789425bfcSMilanka Ringwald 41889425bfcSMilanka Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){ 41989425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable); 42089425bfcSMilanka Ringwald } 42189425bfcSMilanka Ringwald 42289425bfcSMilanka Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){ 42389425bfcSMilanka Ringwald return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER); 42489425bfcSMilanka Ringwald } 42589425bfcSMilanka Ringwald 42689425bfcSMilanka Ringwald static int hfp_hf_send_chup(uint16_t cid){ 42789425bfcSMilanka Ringwald return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL); 42889425bfcSMilanka Ringwald } 42989425bfcSMilanka Ringwald 430ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){ 43189425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1"); 432ce263fc8SMatthias Ringwald } 433ce263fc8SMatthias Ringwald 434667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){ 43589425bfcSMilanka Ringwald return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS); 436667ec068SMatthias Ringwald } 437667ec068SMatthias Ringwald 43876cc1527SMatthias Ringwald /* state machines */ 4393deb3ec6SMatthias Ringwald 440a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){ 441a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 442a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 443aa4dd815SMatthias Ringwald int done = 1; 444498a8121SMilanka Ringwald log_info("hfp_hf_run_for_context_service_level_connection state %d\n", hfp_connection->state); 445a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 4463deb3ec6SMatthias Ringwald case HFP_EXCHANGE_SUPPORTED_FEATURES: 447aeb0f0feSMatthias Ringwald hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_hf_codecs, &hfp_hf_codecs_nr); 448a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES; 449a0ffb263SMatthias Ringwald hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid); 4503deb3ec6SMatthias Ringwald break; 4513deb3ec6SMatthias Ringwald case HFP_NOTIFY_ON_CODECS: 452a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS; 453a0ffb263SMatthias Ringwald hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 4543deb3ec6SMatthias Ringwald break; 4553deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INDICATORS: 456a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 457a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid); 4583deb3ec6SMatthias Ringwald break; 4593deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INDICATORS_STATUS: 460a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS; 461a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid); 4623deb3ec6SMatthias Ringwald break; 4633deb3ec6SMatthias Ringwald case HFP_ENABLE_INDICATORS_STATUS_UPDATE: 464a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE; 465a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1); 4663deb3ec6SMatthias Ringwald break; 4673deb3ec6SMatthias Ringwald case HFP_RETRIEVE_CAN_HOLD_CALL: 468a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL; 469a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid); 4703deb3ec6SMatthias Ringwald break; 4713deb3ec6SMatthias Ringwald case HFP_LIST_GENERIC_STATUS_INDICATORS: 472a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 473a0ffb263SMatthias Ringwald hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4743deb3ec6SMatthias Ringwald break; 4753deb3ec6SMatthias Ringwald case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS: 476a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS; 477a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4783deb3ec6SMatthias Ringwald break; 4793deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 480a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 481a0ffb263SMatthias Ringwald hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4823deb3ec6SMatthias Ringwald break; 4833deb3ec6SMatthias Ringwald default: 484aa4dd815SMatthias Ringwald done = 0; 4853deb3ec6SMatthias Ringwald break; 4863deb3ec6SMatthias Ringwald } 4873deb3ec6SMatthias Ringwald return done; 4883deb3ec6SMatthias Ringwald } 4893deb3ec6SMatthias Ringwald 490ce263fc8SMatthias Ringwald 491a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){ 492a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 493498a8121SMilanka Ringwald if (hfp_connection->ok_pending){ 494498a8121SMilanka Ringwald return 0; 495498a8121SMilanka Ringwald } 496ce263fc8SMatthias Ringwald int done = 0; 497a0ffb263SMatthias Ringwald if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 498a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 499ce263fc8SMatthias Ringwald done = 1; 500a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators); 501ce263fc8SMatthias Ringwald return done; 502ce263fc8SMatthias Ringwald }; 503a0ffb263SMatthias Ringwald if (hfp_connection->change_status_update_for_individual_ag_indicators){ 504a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 505ce263fc8SMatthias Ringwald done = 1; 506a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid, 507a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap, 508a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr); 509ce263fc8SMatthias Ringwald return done; 510ce263fc8SMatthias Ringwald } 511ce263fc8SMatthias Ringwald 512a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 513ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_SET_FORMAT: 514a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK; 515a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 516a0ffb263SMatthias Ringwald hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid); 517ce263fc8SMatthias Ringwald return 1; 518ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_SEND_QUERY: 519a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT; 520a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 521a0ffb263SMatthias Ringwald hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid); 522ce263fc8SMatthias Ringwald return 1; 523ce263fc8SMatthias Ringwald default: 524ce263fc8SMatthias Ringwald break; 525ce263fc8SMatthias Ringwald } 526ce263fc8SMatthias Ringwald 527a0ffb263SMatthias Ringwald if (hfp_connection->enable_extended_audio_gateway_error_report){ 528a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 529ce263fc8SMatthias Ringwald done = 1; 530a0ffb263SMatthias Ringwald hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report); 531ce263fc8SMatthias Ringwald return done; 532ce263fc8SMatthias Ringwald } 533ce263fc8SMatthias Ringwald 534ce263fc8SMatthias Ringwald return done; 535ce263fc8SMatthias Ringwald } 536ce263fc8SMatthias Ringwald 537af97579eSMilanka Ringwald static int hfp_hf_voice_recognition_state_machine(hfp_connection_t * hfp_connection){ 538be55a11dSMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) { 539be55a11dSMilanka Ringwald return 0; 540be55a11dSMilanka Ringwald } 541be55a11dSMilanka Ringwald int done = 0; 542fd4151d1SMilanka Ringwald 5430b4debbfSMilanka Ringwald if (hfp_connection->ok_pending == 1){ 5440b4debbfSMilanka Ringwald return 0; 5450b4debbfSMilanka Ringwald } 5460b4debbfSMilanka Ringwald // voice recognition activated from AG 5470b4debbfSMilanka Ringwald if (hfp_connection->command == HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION){ 5480b4debbfSMilanka Ringwald switch(hfp_connection->vra_state_requested){ 5490b4debbfSMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED: 5500b4debbfSMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_OFF: 551de9e0ea7SMilanka Ringwald case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 5520b4debbfSMilanka Ringwald // ignore AG command, continue to wait for OK 5530b4debbfSMilanka Ringwald return 0; 554cf75be85SMilanka Ringwald 5550b4debbfSMilanka Ringwald default: 556b95cac54SMilanka Ringwald if (hfp_connection->ag_vra_msg_length > 0){ 557b95cac54SMilanka Ringwald hfp_hf_emit_enhanced_voice_recognition_text(hfp_connection); 558b95cac54SMilanka Ringwald hfp_connection->ag_vra_msg_length = 0; 559b95cac54SMilanka Ringwald break; 560b95cac54SMilanka Ringwald } 561cf75be85SMilanka Ringwald switch(hfp_connection->ag_vra_state){ 562cf75be85SMilanka Ringwald case HFP_VOICE_RECOGNITION_STATE_AG_READY: 563013cc750SMilanka Ringwald switch (hfp_connection->ag_vra_status){ 564013cc750SMilanka Ringwald case 0: 5650b4debbfSMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF; 566013cc750SMilanka Ringwald break; 567013cc750SMilanka Ringwald case 1: 568013cc750SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED; 569013cc750SMilanka Ringwald break; 570013cc750SMilanka Ringwald case 2: 571013cc750SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 572013cc750SMilanka Ringwald break; 573013cc750SMilanka Ringwald default: 574013cc750SMilanka Ringwald break; 5750b4debbfSMilanka Ringwald } 5760b4debbfSMilanka Ringwald break; 577cf75be85SMilanka Ringwald default: 578cf75be85SMilanka Ringwald // state messages from AG 579cf75be85SMilanka Ringwald hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS); 5808d5005b5SMilanka Ringwald hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY; 581cf75be85SMilanka Ringwald break; 582cf75be85SMilanka Ringwald } 583cf75be85SMilanka Ringwald break; 5840b4debbfSMilanka Ringwald } 5850b4debbfSMilanka Ringwald hfp_connection->command = HFP_CMD_NONE; 5860b4debbfSMilanka Ringwald } 5870b4debbfSMilanka Ringwald 5880b4debbfSMilanka Ringwald 589498a8121SMilanka Ringwald switch (hfp_connection->vra_state_requested){ 590fdda66c0SMilanka Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 591fdda66c0SMilanka Ringwald done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0); 592fdda66c0SMilanka Ringwald if (done != 0){ 593fdda66c0SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF; 594498a8121SMilanka Ringwald hfp_connection->ok_pending = 1; 595498a8121SMilanka Ringwald } 596fd4151d1SMilanka Ringwald return 1; 597fd4151d1SMilanka Ringwald 598fd4151d1SMilanka Ringwald 599fdda66c0SMilanka Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED: 600fdda66c0SMilanka Ringwald done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1); 601fdda66c0SMilanka Ringwald if (done != 0){ 602fdda66c0SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED; 603fd4151d1SMilanka Ringwald hfp_connection->ok_pending = 1; 604fd4151d1SMilanka Ringwald return 1; 6050b4debbfSMilanka Ringwald } 6060b4debbfSMilanka Ringwald break; 607013cc750SMilanka Ringwald 608de9e0ea7SMilanka Ringwald case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 609de9e0ea7SMilanka Ringwald done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 2); 610de9e0ea7SMilanka Ringwald if (done != 0){ 611de9e0ea7SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 612de9e0ea7SMilanka Ringwald hfp_connection->ok_pending = 1; 613de9e0ea7SMilanka Ringwald return 1; 614de9e0ea7SMilanka Ringwald } 615de9e0ea7SMilanka Ringwald break; 616de9e0ea7SMilanka Ringwald 617de9e0ea7SMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_OFF: 618de9e0ea7SMilanka Ringwald hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF; 619de9e0ea7SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 620de9e0ea7SMilanka Ringwald hfp_connection->activate_voice_recognition = false; 621de9e0ea7SMilanka Ringwald if (hfp_connection->activate_voice_recognition){ 622fcf4ede6SMilanka Ringwald hfp_connection->enhanced_voice_recognition_enabled = hfp_hf_enhanced_vra_flag_supported(hfp_connection); 623de9e0ea7SMilanka Ringwald hfp_hf_activate_voice_recognition(hfp_connection->acl_handle); 624de9e0ea7SMilanka Ringwald } else { 625553a4a56SMilanka Ringwald hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS); 626de9e0ea7SMilanka Ringwald } 627de9e0ea7SMilanka Ringwald break; 628de9e0ea7SMilanka Ringwald 629be55a11dSMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED: 630498a8121SMilanka Ringwald hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_ACTIVATED; 631498a8121SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 632de9e0ea7SMilanka Ringwald hfp_connection->activate_voice_recognition = false; 633de9e0ea7SMilanka Ringwald if (hfp_connection->deactivate_voice_recognition){ 634de9e0ea7SMilanka Ringwald hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle); 635de9e0ea7SMilanka Ringwald } else { 636fcf4ede6SMilanka Ringwald hfp_connection->enhanced_voice_recognition_enabled = hfp_hf_enhanced_vra_flag_supported(hfp_connection); 63784fb9ac1SMilanka Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 638553a4a56SMilanka Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS); 63984fb9ac1SMilanka Ringwald } else { 64084fb9ac1SMilanka Ringwald // postpone VRA event to simplify application logic 64184fb9ac1SMilanka Ringwald hfp_connection->emit_vra_enabled_after_audio_established = true; 64284fb9ac1SMilanka Ringwald } 643de9e0ea7SMilanka Ringwald } 644be55a11dSMilanka Ringwald break; 645be55a11dSMilanka Ringwald 646de9e0ea7SMilanka Ringwald 647013cc750SMilanka Ringwald case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 648013cc750SMilanka Ringwald hfp_connection->vra_state = HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 649498a8121SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 650de9e0ea7SMilanka Ringwald hfp_connection->activate_voice_recognition = false; 651de9e0ea7SMilanka Ringwald if (hfp_connection->deactivate_voice_recognition){ 652de9e0ea7SMilanka Ringwald hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle); 653de9e0ea7SMilanka Ringwald } else { 654de9e0ea7SMilanka Ringwald hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_SUCCESS); 655de9e0ea7SMilanka Ringwald } 656be55a11dSMilanka Ringwald break; 657fd4151d1SMilanka Ringwald 658be55a11dSMilanka Ringwald default: 659be55a11dSMilanka Ringwald break; 660be55a11dSMilanka Ringwald } 661be55a11dSMilanka Ringwald return done; 662be55a11dSMilanka Ringwald } 663be55a11dSMilanka Ringwald 664be55a11dSMilanka Ringwald 665be55a11dSMilanka Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){ 666a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 667ce263fc8SMatthias Ringwald 668332ca98fSMatthias Ringwald if (hfp_connection->trigger_codec_exchange){ 669332ca98fSMatthias Ringwald hfp_connection->trigger_codec_exchange = 0; 670ce263fc8SMatthias Ringwald 671a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 672a0ffb263SMatthias Ringwald hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid); 673332ca98fSMatthias Ringwald return 1; 674332ca98fSMatthias Ringwald } 675332ca98fSMatthias Ringwald 6761cc65c4fSMatthias Ringwald if (hfp_connection->hf_send_codec_confirm){ 6771cc65c4fSMatthias Ringwald hfp_connection->hf_send_codec_confirm = false; 678ce263fc8SMatthias Ringwald 679a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 680fcb08cdbSMilanka Ringwald hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed); 6811cc65c4fSMatthias Ringwald return 1; 6821cc65c4fSMatthias Ringwald } 6831cc65c4fSMatthias Ringwald 6841cc65c4fSMatthias Ringwald if (hfp_connection->hf_send_supported_codecs){ 6851cc65c4fSMatthias Ringwald hfp_connection->hf_send_supported_codecs = false; 6861cc65c4fSMatthias Ringwald 687a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 688a0ffb263SMatthias Ringwald hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 6891cc65c4fSMatthias Ringwald return 1; 6901cc65c4fSMatthias Ringwald } 691ce263fc8SMatthias Ringwald 692ce263fc8SMatthias Ringwald return 0; 693ce263fc8SMatthias Ringwald } 694ce263fc8SMatthias Ringwald 695a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){ 696505f1c30SMatthias Ringwald if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) || 697505f1c30SMatthias Ringwald (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0; 698ce263fc8SMatthias Ringwald 69964f19dedSMilanka Ringwald if (hfp_connection->release_audio_connection){ 700a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 701a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 0; 702a0ffb263SMatthias Ringwald gap_disconnect(hfp_connection->sco_handle); 703ce263fc8SMatthias Ringwald return 1; 704ce263fc8SMatthias Ringwald } 705ce263fc8SMatthias Ringwald 706a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 707ce263fc8SMatthias Ringwald 708ce263fc8SMatthias Ringwald // run codecs exchange 709a0ffb263SMatthias Ringwald int done = codecs_exchange_state_machine(hfp_connection); 710ce263fc8SMatthias Ringwald if (done) return 1; 711ce263fc8SMatthias Ringwald 71238200c1dSMilanka Ringwald if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0; 71338200c1dSMilanka Ringwald if (hfp_connection->establish_audio_connection){ 71438200c1dSMilanka Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 71538200c1dSMilanka Ringwald hfp_connection->establish_audio_connection = 0; 71638200c1dSMilanka Ringwald hfp_setup_synchronous_connection(hfp_connection); 71738200c1dSMilanka Ringwald return 1; 71838200c1dSMilanka Ringwald } 719ce263fc8SMatthias Ringwald return 0; 720ce263fc8SMatthias Ringwald } 721ce263fc8SMatthias Ringwald 72238200c1dSMilanka Ringwald 723a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 724eaf2b0a1SMatthias Ringwald 725eaf2b0a1SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 726eaf2b0a1SMatthias Ringwald 727a0ffb263SMatthias Ringwald if (hfp_connection->hf_answer_incoming_call){ 728a0ffb263SMatthias Ringwald hfp_hf_cmd_ata(hfp_connection->rfcomm_cid); 729a0ffb263SMatthias Ringwald hfp_connection->hf_answer_incoming_call = 0; 730ce263fc8SMatthias Ringwald return 1; 731ce263fc8SMatthias Ringwald } 732ce263fc8SMatthias Ringwald return 0; 733ce263fc8SMatthias Ringwald } 734ce263fc8SMatthias Ringwald 7351c6a0fc0SMatthias Ringwald static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){ 7367522e673SMatthias Ringwald 73776cc1527SMatthias Ringwald btstack_assert(hfp_connection != NULL); 73876cc1527SMatthias Ringwald btstack_assert(hfp_connection->local_role == HFP_ROLE_HF); 73976cc1527SMatthias Ringwald 74076cc1527SMatthias Ringwald // during SDP query, RFCOMM CID is not set 74176cc1527SMatthias Ringwald if (hfp_connection->rfcomm_cid == 0) return; 74222387625SMatthias Ringwald 74384fb9ac1SMilanka Ringwald // emit postponed VRA event 74484fb9ac1SMilanka Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->emit_vra_enabled_after_audio_established){ 74584fb9ac1SMilanka Ringwald hfp_connection->emit_vra_enabled_after_audio_established = false; 74684fb9ac1SMilanka Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS); 74784fb9ac1SMilanka Ringwald } 74884fb9ac1SMilanka Ringwald 7493721a235SMatthias Ringwald // assert command could be sent 7503721a235SMatthias Ringwald if (hci_can_send_command_packet_now() == 0) return; 7513721a235SMatthias Ringwald 7523721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP 7533721a235SMatthias Ringwald // WBS Disassociate 7543721a235SMatthias Ringwald if (hfp_connection->cc256x_send_wbs_disassociate){ 7553721a235SMatthias Ringwald hfp_connection->cc256x_send_wbs_disassociate = false; 7563721a235SMatthias Ringwald hci_send_cmd(&hci_ti_wbs_disassociate); 7573721a235SMatthias Ringwald return; 7583721a235SMatthias Ringwald } 7593721a235SMatthias Ringwald // Write Codec Config 7603721a235SMatthias Ringwald if (hfp_connection->cc256x_send_write_codec_config){ 7613721a235SMatthias Ringwald hfp_connection->cc256x_send_write_codec_config = false; 7623721a235SMatthias Ringwald hfp_cc256x_write_codec_config(hfp_connection); 7633721a235SMatthias Ringwald return; 7643721a235SMatthias Ringwald } 7653721a235SMatthias Ringwald // WBS Associate 7663721a235SMatthias Ringwald if (hfp_connection->cc256x_send_wbs_associate){ 7673721a235SMatthias Ringwald hfp_connection->cc256x_send_wbs_associate = false; 7683721a235SMatthias Ringwald hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle); 7693721a235SMatthias Ringwald return; 7703721a235SMatthias Ringwald } 7713721a235SMatthias Ringwald #endif 772689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 773689d4323SMatthias Ringwald // Enable WBS 774689d4323SMatthias Ringwald if (hfp_connection->bcm_send_enable_wbs){ 775689d4323SMatthias Ringwald hfp_connection->bcm_send_enable_wbs = false; 776689d4323SMatthias Ringwald hci_send_cmd(&hci_bcm_enable_wbs, 1, 2); 777689d4323SMatthias Ringwald return; 778689d4323SMatthias Ringwald } 779689d4323SMatthias Ringwald // Write I2S/PCM params 780689d4323SMatthias Ringwald if (hfp_connection->bcm_send_write_i2spcm_interface_param){ 781689d4323SMatthias Ringwald hfp_connection->bcm_send_write_i2spcm_interface_param = false; 782689d4323SMatthias Ringwald hfp_bcm_write_i2spcm_interface_param(hfp_connection); 783689d4323SMatthias Ringwald return; 784689d4323SMatthias Ringwald } 785689d4323SMatthias Ringwald // Disable WBS 786689d4323SMatthias Ringwald if (hfp_connection->bcm_send_disable_wbs){ 787689d4323SMatthias Ringwald hfp_connection->bcm_send_disable_wbs = false; 788689d4323SMatthias Ringwald hci_send_cmd(&hci_bcm_enable_wbs, 0, 2); 789689d4323SMatthias Ringwald return; 790689d4323SMatthias Ringwald } 791689d4323SMatthias Ringwald #endif 7922b5f92fdSMatthias Ringwald #ifdef ENABLE_RTK_PCM_WBS 7932b5f92fdSMatthias Ringwald if (hfp_connection->rtk_send_sco_config){ 7942b5f92fdSMatthias Ringwald hfp_connection->rtk_send_sco_config = false; 7952b5f92fdSMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 7962b5f92fdSMatthias Ringwald log_info("RTK SCO: 16k + mSBC"); 7972b5f92fdSMatthias Ringwald hci_send_cmd(&hci_rtk_configure_sco_routing, 0x81, 0x90, 0x00, 0x00, 0x1a, 0x0c, 0x00, 0x00, 0x41); 7982b5f92fdSMatthias Ringwald } else { 7992b5f92fdSMatthias Ringwald log_info("RTK SCO: 16k + CVSD"); 8002b5f92fdSMatthias Ringwald hci_send_cmd(&hci_rtk_configure_sco_routing, 0x81, 0x90, 0x00, 0x00, 0x1a, 0x0c, 0x0c, 0x00, 0x01); 8012b5f92fdSMatthias Ringwald } 8022b5f92fdSMatthias Ringwald return; 8032b5f92fdSMatthias Ringwald } 8042b5f92fdSMatthias Ringwald #endif 80548e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS) 80648e6eeeeSMatthias Ringwald if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){ 80748e6eeeeSMatthias Ringwald hfp_finalize_connection_context(hfp_connection); 80848e6eeeeSMatthias Ringwald return; 80948e6eeeeSMatthias Ringwald } 81048e6eeeeSMatthias Ringwald #endif 8113721a235SMatthias Ringwald 812cb81d35dSMatthias Ringwald if (hfp_connection->accept_sco){ 813cb81d35dSMatthias Ringwald bool incoming_eSCO = hfp_connection->accept_sco == 2; 814cb81d35dSMatthias Ringwald hfp_connection->accept_sco = 0; 8157522e673SMatthias Ringwald // notify about codec selection if not done already 8167522e673SMatthias Ringwald if (hfp_connection->negotiated_codec == 0){ 8177522e673SMatthias Ringwald hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 8187522e673SMatthias Ringwald } 819cb81d35dSMatthias Ringwald hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO); 8207522e673SMatthias Ringwald return; 8217522e673SMatthias Ringwald } 8227522e673SMatthias Ringwald 823d4dd47ffSMatthias Ringwald if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) { 824d4dd47ffSMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 825d4dd47ffSMatthias Ringwald return; 826d4dd47ffSMatthias Ringwald } 827a0ffb263SMatthias Ringwald int done = hfp_hf_run_for_context_service_level_connection(hfp_connection); 828ce263fc8SMatthias Ringwald if (!done){ 829a0ffb263SMatthias Ringwald done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection); 830ce263fc8SMatthias Ringwald } 831ce263fc8SMatthias Ringwald if (!done){ 832c95b5b3cSMilanka Ringwald done = hfp_hf_run_for_audio_connection(hfp_connection); 833be55a11dSMilanka Ringwald } 834be55a11dSMilanka Ringwald if (!done){ 835c95b5b3cSMilanka Ringwald done = hfp_hf_voice_recognition_state_machine(hfp_connection); 836ce263fc8SMatthias Ringwald } 837ce263fc8SMatthias Ringwald if (!done){ 838a0ffb263SMatthias Ringwald done = call_setup_state_machine(hfp_connection); 839ce263fc8SMatthias Ringwald } 840ce263fc8SMatthias Ringwald 841e2c3b58dSMilanka Ringwald // don't send a new command while ok still pending or SLC is not established 842e2c3b58dSMilanka Ringwald if (hfp_connection->ok_pending || (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED)){ 843e2c3b58dSMilanka Ringwald return; 844e2c3b58dSMilanka Ringwald } 8451016a228SMatthias Ringwald 846a0ffb263SMatthias Ringwald if (hfp_connection->send_microphone_gain){ 847a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 0; 848a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 849a0ffb263SMatthias Ringwald hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 850ce263fc8SMatthias Ringwald return; 851ce263fc8SMatthias Ringwald } 852ce263fc8SMatthias Ringwald 853a0ffb263SMatthias Ringwald if (hfp_connection->send_speaker_gain){ 854a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 0; 855a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 856a0ffb263SMatthias Ringwald hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 857ce263fc8SMatthias Ringwald return; 858ce263fc8SMatthias Ringwald } 859ce263fc8SMatthias Ringwald 860a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_calling_line_notification){ 861a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_calling_line_notification = 0; 862a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 863a0ffb263SMatthias Ringwald hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0); 864ce263fc8SMatthias Ringwald return; 865ce263fc8SMatthias Ringwald } 866ce263fc8SMatthias Ringwald 867a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_calling_line_notification){ 868a0ffb263SMatthias Ringwald hfp_connection->hf_activate_calling_line_notification = 0; 869a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 870a0ffb263SMatthias Ringwald hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1); 871ce263fc8SMatthias Ringwald return; 872ce263fc8SMatthias Ringwald } 873ce263fc8SMatthias Ringwald 874a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){ 875a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0; 87699af1e28SMilanka Ringwald hfp_connection->response_pending_for_command = HFP_CMD_TURN_OFF_EC_AND_NR; 877a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 878e2c3b58dSMilanka Ringwald hfp_hf_send_cmd_with_int(hfp_connection->rfcomm_cid, HFP_TURN_OFF_EC_AND_NR, 0); 879ce263fc8SMatthias Ringwald return; 880ce263fc8SMatthias Ringwald } 881ce263fc8SMatthias Ringwald 882a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_call_waiting_notification){ 883a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_call_waiting_notification = 0; 884a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 885a0ffb263SMatthias Ringwald hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0); 886ce263fc8SMatthias Ringwald return; 887ce263fc8SMatthias Ringwald } 888ce263fc8SMatthias Ringwald 889a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_call_waiting_notification){ 890a0ffb263SMatthias Ringwald hfp_connection->hf_activate_call_waiting_notification = 0; 891a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 892a0ffb263SMatthias Ringwald hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1); 893ce263fc8SMatthias Ringwald return; 894ce263fc8SMatthias Ringwald } 895ce263fc8SMatthias Ringwald 896a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_outgoing_call){ 897a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_outgoing_call = 0; 898a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 899a0ffb263SMatthias Ringwald hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid); 900ce263fc8SMatthias Ringwald return; 901ce263fc8SMatthias Ringwald } 902ce263fc8SMatthias Ringwald 903a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_memory_dialing){ 904a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_memory_dialing = 0; 905a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 906a0ffb263SMatthias Ringwald hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id); 907ce263fc8SMatthias Ringwald return; 908ce263fc8SMatthias Ringwald } 909ce263fc8SMatthias Ringwald 910a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_redial_last_number){ 911a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_redial_last_number = 0; 912a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 913a0ffb263SMatthias Ringwald hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid); 914ce263fc8SMatthias Ringwald return; 915ce263fc8SMatthias Ringwald } 916ce263fc8SMatthias Ringwald 917a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chup){ 918a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 0; 919a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 920a0ffb263SMatthias Ringwald hfp_hf_send_chup(hfp_connection->rfcomm_cid); 921ce263fc8SMatthias Ringwald return; 922ce263fc8SMatthias Ringwald } 923ce263fc8SMatthias Ringwald 924a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_0){ 925a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_0 = 0; 926a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 927a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0); 928ce263fc8SMatthias Ringwald return; 929ce263fc8SMatthias Ringwald } 930ce263fc8SMatthias Ringwald 931a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_1){ 932a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_1 = 0; 933a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 934a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1); 935ce263fc8SMatthias Ringwald return; 936ce263fc8SMatthias Ringwald } 937ce263fc8SMatthias Ringwald 938a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_2){ 939a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_2 = 0; 940a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 941a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2); 942ce263fc8SMatthias Ringwald return; 943ce263fc8SMatthias Ringwald } 944ce263fc8SMatthias Ringwald 945a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_3){ 946a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_3 = 0; 947a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 948a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3); 949ce263fc8SMatthias Ringwald return; 950ce263fc8SMatthias Ringwald } 951ce263fc8SMatthias Ringwald 952a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_4){ 953a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_4 = 0; 954a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 955a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4); 956ce263fc8SMatthias Ringwald return; 957ce263fc8SMatthias Ringwald } 958ce263fc8SMatthias Ringwald 959a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_x){ 960a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 0; 961a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 962a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index); 963667ec068SMatthias Ringwald return; 964667ec068SMatthias Ringwald } 965667ec068SMatthias Ringwald 966a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_dtmf_code){ 967a0ffb263SMatthias Ringwald char code = hfp_connection->hf_send_dtmf_code; 968a0ffb263SMatthias Ringwald hfp_connection->hf_send_dtmf_code = 0; 969a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 970a0ffb263SMatthias Ringwald hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code); 971ce263fc8SMatthias Ringwald return; 972ce263fc8SMatthias Ringwald } 973ce263fc8SMatthias Ringwald 974a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_binp){ 975a0ffb263SMatthias Ringwald hfp_connection->hf_send_binp = 0; 976a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 977a0ffb263SMatthias Ringwald hfp_hf_send_binp(hfp_connection->rfcomm_cid); 978ce263fc8SMatthias Ringwald return; 979ce263fc8SMatthias Ringwald } 980ce263fc8SMatthias Ringwald 981a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_clcc){ 982a0ffb263SMatthias Ringwald hfp_connection->hf_send_clcc = 0; 983a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 984a0ffb263SMatthias Ringwald hfp_hf_send_clcc(hfp_connection->rfcomm_cid); 985667ec068SMatthias Ringwald return; 986667ec068SMatthias Ringwald } 987667ec068SMatthias Ringwald 988a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_rrh){ 989a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 0; 990667ec068SMatthias Ringwald char buffer[20]; 991a0ffb263SMatthias Ringwald switch (hfp_connection->hf_send_rrh_command){ 992667ec068SMatthias Ringwald case '?': 9931599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s?\r", 994ff7d6aeaSMatthias Ringwald HFP_RESPONSE_AND_HOLD); 995ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 996a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 997667ec068SMatthias Ringwald return; 998667ec068SMatthias Ringwald case '0': 999667ec068SMatthias Ringwald case '1': 1000667ec068SMatthias Ringwald case '2': 10011599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s=%c\r", 1002ff7d6aeaSMatthias Ringwald HFP_RESPONSE_AND_HOLD, 1003ff7d6aeaSMatthias Ringwald hfp_connection->hf_send_rrh_command); 1004ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 1005a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 1006667ec068SMatthias Ringwald return; 1007667ec068SMatthias Ringwald default: 1008667ec068SMatthias Ringwald break; 1009667ec068SMatthias Ringwald } 1010667ec068SMatthias Ringwald return; 1011667ec068SMatthias Ringwald } 1012667ec068SMatthias Ringwald 1013a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_cnum){ 1014a0ffb263SMatthias Ringwald hfp_connection->hf_send_cnum = 0; 1015667ec068SMatthias Ringwald char buffer[20]; 10161599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s\r", 1017ff7d6aeaSMatthias Ringwald HFP_SUBSCRIBER_NUMBER_INFORMATION); 1018ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 1019a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 1020667ec068SMatthias Ringwald return; 1021667ec068SMatthias Ringwald } 1022667ec068SMatthias Ringwald 1023667ec068SMatthias Ringwald // update HF indicators 1024a0ffb263SMatthias Ringwald if (hfp_connection->generic_status_update_bitmap){ 1025667ec068SMatthias Ringwald int i; 1026aeb0f0feSMatthias Ringwald for (i=0; i < hfp_hf_indicators_nr; i++){ 1027a0ffb263SMatthias Ringwald if (get_bit(hfp_connection->generic_status_update_bitmap, i)){ 1028a0ffb263SMatthias Ringwald if (hfp_connection->generic_status_indicators[i].state){ 1029a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1030a0ffb263SMatthias Ringwald hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0); 1031667ec068SMatthias Ringwald char buffer[30]; 10321599fe57SMatthias Ringwald snprintf(buffer, sizeof(buffer), "AT%s=%u,%u\r", 1033ff7d6aeaSMatthias Ringwald HFP_TRANSFER_HF_INDICATOR_STATUS, 1034aeb0f0feSMatthias Ringwald hfp_hf_indicators[i], 1035aeb0f0feSMatthias Ringwald (unsigned int)hfp_hf_indicators_value[i]); 1036ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 1037a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 1038667ec068SMatthias Ringwald } else { 1039aeb0f0feSMatthias Ringwald log_info("Not sending HF indicator %u as it is disabled", hfp_hf_indicators[i]); 1040667ec068SMatthias Ringwald } 1041667ec068SMatthias Ringwald return; 1042667ec068SMatthias Ringwald } 1043667ec068SMatthias Ringwald } 1044667ec068SMatthias Ringwald } 1045667ec068SMatthias Ringwald 1046ce263fc8SMatthias Ringwald if (done) return; 1047ce263fc8SMatthias Ringwald // deal with disconnect 1048a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 1049ce263fc8SMatthias Ringwald case HFP_W2_DISCONNECT_RFCOMM: 1050a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 1051a0ffb263SMatthias Ringwald rfcomm_disconnect(hfp_connection->rfcomm_cid); 1052ce263fc8SMatthias Ringwald break; 1053ce263fc8SMatthias Ringwald 1054ce263fc8SMatthias Ringwald default: 1055ce263fc8SMatthias Ringwald break; 1056ce263fc8SMatthias Ringwald } 1057ce263fc8SMatthias Ringwald } 1058ce263fc8SMatthias Ringwald 1059a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){ 1060a0ffb263SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 10616a7f44bdSMilanka Ringwald 10627095467fSMatthias Ringwald hfp_emit_slc_connection_event(hfp_connection->local_role, 0, hfp_connection->acl_handle, hfp_connection->remote_addr); 10637522e673SMatthias Ringwald 1064184a03edSMilanka Ringwald uint8_t i; 1065184a03edSMilanka Ringwald for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 10661ac1f60fSMilanka Ringwald hfp_emit_ag_indicator_mapping_event(hfp_connection, &hfp_connection->ag_indicators[i]); 1067184a03edSMilanka Ringwald } 1068722a85f3SMilanka Ringwald 1069722a85f3SMilanka Ringwald for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 1070722a85f3SMilanka Ringwald hfp_connection->ag_indicators[i].status_changed = 0; 1071722a85f3SMilanka Ringwald hfp_emit_ag_indicator_status_event(hfp_connection, &hfp_connection->ag_indicators[i]); 1072722a85f3SMilanka Ringwald } 1073722a85f3SMilanka Ringwald 1074667ec068SMatthias Ringwald // restore volume settings 1075a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = hfp_hf_speaker_gain; 1076a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 1077ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain); 1078a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = hfp_hf_microphone_gain; 1079a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 1080ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain); 1081667ec068SMatthias Ringwald // enable all indicators 1082aeb0f0feSMatthias Ringwald for (i=0; i < hfp_hf_indicators_nr; i++){ 1083aeb0f0feSMatthias Ringwald hfp_connection->generic_status_indicators[i].uuid = hfp_hf_indicators[i]; 1084a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[i].state = 1; 1085667ec068SMatthias Ringwald } 1086ce263fc8SMatthias Ringwald } 1087ce263fc8SMatthias Ringwald 10881cc65c4fSMatthias Ringwald static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){ 1089aeb0f0feSMatthias Ringwald if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_hf_codecs_nr, hfp_hf_codecs)){ 10901cc65c4fSMatthias Ringwald // Codec supported, confirm 10911cc65c4fSMatthias Ringwald hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 10921cc65c4fSMatthias Ringwald hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 10931cc65c4fSMatthias Ringwald log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD"); 10941cc65c4fSMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC; 10951cc65c4fSMatthias Ringwald 10961cc65c4fSMatthias Ringwald hfp_connection->hf_send_codec_confirm = true; 10971cc65c4fSMatthias Ringwald } else { 10981cc65c4fSMatthias Ringwald // Codec not supported, send supported codecs 10991cc65c4fSMatthias Ringwald hfp_connection->codec_confirmed = 0; 11001cc65c4fSMatthias Ringwald hfp_connection->suggested_codec = 0; 11011cc65c4fSMatthias Ringwald hfp_connection->negotiated_codec = 0; 11021cc65c4fSMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 11031cc65c4fSMatthias Ringwald 11041cc65c4fSMatthias Ringwald hfp_connection->hf_send_supported_codecs = true; 11051cc65c4fSMatthias Ringwald } 11061cc65c4fSMatthias Ringwald } 11071cc65c4fSMatthias Ringwald 1108e2c3b58dSMilanka Ringwald static bool hfp_hf_switch_on_ok_pending(hfp_connection_t *hfp_connection, uint8_t status){ 1109e2c3b58dSMilanka Ringwald bool event_emited = true; 1110e2c3b58dSMilanka Ringwald 111199af1e28SMilanka Ringwald switch (hfp_connection->response_pending_for_command){ 1112e2c3b58dSMilanka Ringwald case HFP_CMD_TURN_OFF_EC_AND_NR: 1113e2c3b58dSMilanka Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE, status); 1114e2c3b58dSMilanka Ringwald break; 1115e2c3b58dSMilanka Ringwald default: 1116e2c3b58dSMilanka Ringwald event_emited = false; 1117e2c3b58dSMilanka Ringwald 1118a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 11193deb3ec6SMatthias Ringwald case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 1120a0ffb263SMatthias Ringwald if (has_codec_negotiation_feature(hfp_connection)){ 1121a0ffb263SMatthias Ringwald hfp_connection->state = HFP_NOTIFY_ON_CODECS; 11223deb3ec6SMatthias Ringwald break; 11233deb3ec6SMatthias Ringwald } 1124a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 11253deb3ec6SMatthias Ringwald break; 11263deb3ec6SMatthias Ringwald 11273deb3ec6SMatthias Ringwald case HFP_W4_NOTIFY_ON_CODECS: 1128a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 11293deb3ec6SMatthias Ringwald break; 11303deb3ec6SMatthias Ringwald 11313deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS: 1132a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS; 11333deb3ec6SMatthias Ringwald break; 11343deb3ec6SMatthias Ringwald 11353deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS_STATUS: 1136a0ffb263SMatthias Ringwald hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE; 11373deb3ec6SMatthias Ringwald break; 11383deb3ec6SMatthias Ringwald 11393deb3ec6SMatthias Ringwald case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE: 1140a0ffb263SMatthias Ringwald if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 1141a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL; 11423deb3ec6SMatthias Ringwald break; 11433deb3ec6SMatthias Ringwald } 1144a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 1145a0ffb263SMatthias Ringwald hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 11463deb3ec6SMatthias Ringwald break; 11473deb3ec6SMatthias Ringwald } 1148a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 11493deb3ec6SMatthias Ringwald break; 11503deb3ec6SMatthias Ringwald 11513deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_CAN_HOLD_CALL: 1152a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 1153a0ffb263SMatthias Ringwald hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 11543deb3ec6SMatthias Ringwald break; 11553deb3ec6SMatthias Ringwald } 1156a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 11573deb3ec6SMatthias Ringwald break; 11583deb3ec6SMatthias Ringwald 11593deb3ec6SMatthias Ringwald case HFP_W4_LIST_GENERIC_STATUS_INDICATORS: 1160a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS; 11613deb3ec6SMatthias Ringwald break; 11623deb3ec6SMatthias Ringwald 11633deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS: 1164a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 11653deb3ec6SMatthias Ringwald break; 11663deb3ec6SMatthias Ringwald 11673deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 1168a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 11693deb3ec6SMatthias Ringwald break; 1170ce263fc8SMatthias Ringwald case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 1171a0ffb263SMatthias Ringwald if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 1172a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 1173ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 1174ce263fc8SMatthias Ringwald break; 1175ce263fc8SMatthias Ringwald } 11763deb3ec6SMatthias Ringwald 1177a0ffb263SMatthias Ringwald if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){ 1178a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 0; 1179ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 1180ce263fc8SMatthias Ringwald break; 11813deb3ec6SMatthias Ringwald } 11823deb3ec6SMatthias Ringwald 1183a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 1184ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK: 1185a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1186ce263fc8SMatthias Ringwald break; 1187ce263fc8SMatthias Ringwald case HPF_HF_QUERY_OPERATOR_W4_RESULT: 1188a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET; 1189a473a009SMatthias Ringwald hfp_emit_network_operator_event(hfp_connection); 1190ce263fc8SMatthias Ringwald break; 1191ce263fc8SMatthias Ringwald default: 1192ce263fc8SMatthias Ringwald break; 11933deb3ec6SMatthias Ringwald } 1194ce263fc8SMatthias Ringwald 1195a0ffb263SMatthias Ringwald if (hfp_connection->enable_extended_audio_gateway_error_report){ 1196a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = 0; 1197ce263fc8SMatthias Ringwald break; 11983deb3ec6SMatthias Ringwald } 11993deb3ec6SMatthias Ringwald 1200a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 1201aa4dd815SMatthias Ringwald case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 1202a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 12033deb3ec6SMatthias Ringwald break; 1204ce263fc8SMatthias Ringwald case HFP_CODECS_HF_CONFIRMED_CODEC: 1205a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 1206ce263fc8SMatthias Ringwald break; 12073deb3ec6SMatthias Ringwald default: 12083deb3ec6SMatthias Ringwald break; 12093deb3ec6SMatthias Ringwald } 1210af97579eSMilanka Ringwald hfp_hf_voice_recognition_state_machine(hfp_connection); 1211be55a11dSMilanka Ringwald break; 1212be55a11dSMilanka Ringwald case HFP_AUDIO_CONNECTION_ESTABLISHED: 1213af97579eSMilanka Ringwald hfp_hf_voice_recognition_state_machine(hfp_connection); 12143deb3ec6SMatthias Ringwald break; 12153deb3ec6SMatthias Ringwald default: 12163deb3ec6SMatthias Ringwald break; 12173deb3ec6SMatthias Ringwald } 1218e2c3b58dSMilanka Ringwald break; 1219e2c3b58dSMilanka Ringwald } 12203deb3ec6SMatthias Ringwald 12213deb3ec6SMatthias Ringwald // done 122299af1e28SMilanka Ringwald hfp_connection->response_pending_for_command = HFP_CMD_NONE; 1223be55a11dSMilanka Ringwald hfp_connection->ok_pending = 0; 1224a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1225e2c3b58dSMilanka Ringwald return event_emited; 12263deb3ec6SMatthias Ringwald } 12273deb3ec6SMatthias Ringwald 1228a03dbc20SMilanka Ringwald static bool hfp_is_ringing(hfp_callsetup_status_t callsetup_status){ 1229a03dbc20SMilanka Ringwald switch (callsetup_status){ 1230a03dbc20SMilanka Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1231a03dbc20SMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1232a03dbc20SMilanka Ringwald return true; 1233a03dbc20SMilanka Ringwald default: 1234a03dbc20SMilanka Ringwald return false; 1235a03dbc20SMilanka Ringwald } 1236a03dbc20SMilanka Ringwald } 1237be55a11dSMilanka Ringwald 1238b08371a9SMilanka Ringwald static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) { 12394562e2a2SMatthias Ringwald uint16_t i; 1240a03dbc20SMilanka Ringwald 12414562e2a2SMatthias Ringwald for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 12424562e2a2SMatthias Ringwald if (hfp_connection->ag_indicators[i].status_changed) { 12434562e2a2SMatthias Ringwald if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){ 1244a03dbc20SMilanka Ringwald hfp_callsetup_status_t new_hf_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status; 1245a03dbc20SMilanka Ringwald bool ringing_old = hfp_is_ringing(hfp_hf_callsetup_status); 1246a03dbc20SMilanka Ringwald bool ringing_new = hfp_is_ringing(new_hf_callsetup_status); 1247a03dbc20SMilanka Ringwald if (ringing_old != ringing_new){ 1248a03dbc20SMilanka Ringwald if (ringing_new){ 1249a03dbc20SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGING); 1250a03dbc20SMilanka Ringwald } else { 1251a03dbc20SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGING); 1252a03dbc20SMilanka Ringwald } 1253a03dbc20SMilanka Ringwald } 1254a03dbc20SMilanka Ringwald hfp_hf_callsetup_status = new_hf_callsetup_status; 12554562e2a2SMatthias Ringwald } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){ 1256aeb0f0feSMatthias Ringwald hfp_hf_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status; 12574562e2a2SMatthias Ringwald // avoid set but not used warning 1258aeb0f0feSMatthias Ringwald (void) hfp_hf_callheld_status; 12594562e2a2SMatthias Ringwald } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){ 1260674ebed5SMilanka Ringwald hfp_call_status_t new_hf_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status; 1261674ebed5SMilanka Ringwald if (hfp_hf_call_status != new_hf_call_status){ 1262674ebed5SMilanka Ringwald if (new_hf_call_status == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS){ 1263674ebed5SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED); 1264674ebed5SMilanka Ringwald } else { 1265674ebed5SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED); 1266674ebed5SMilanka Ringwald } 1267674ebed5SMilanka Ringwald } 1268674ebed5SMilanka Ringwald hfp_hf_call_status = new_hf_call_status; 12694562e2a2SMatthias Ringwald } 12704562e2a2SMatthias Ringwald hfp_connection->ag_indicators[i].status_changed = 0; 1271ce3797e1SMilanka Ringwald hfp_emit_ag_indicator_status_event(hfp_connection, &hfp_connection->ag_indicators[i]); 12724562e2a2SMatthias Ringwald break; 12734562e2a2SMatthias Ringwald } 12744562e2a2SMatthias Ringwald } 12754562e2a2SMatthias Ringwald } 12764562e2a2SMatthias Ringwald 1277426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){ 1278186dd3d2SMatthias Ringwald int value; 1279186dd3d2SMatthias Ringwald int i; 1280e2c3b58dSMilanka Ringwald bool event_emited; 1281e2c3b58dSMilanka Ringwald 1282125560b8SMatthias Ringwald // last argument is still in line_buffer 1283125560b8SMatthias Ringwald 1284a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1285667ec068SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1286a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1287a473a009SMatthias Ringwald hfp_hf_emit_subscriber_information(hfp_connection, 0); 1288667ec068SMatthias Ringwald break; 1289667ec068SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_STATUS: 1290a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1291ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0])); 1292667ec068SMatthias Ringwald break; 1293667ec068SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 1294a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1295a473a009SMatthias Ringwald hfp_hf_emit_enhanced_call_status(hfp_connection); 1296667ec068SMatthias Ringwald break; 1297ce263fc8SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 1298a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 12992308e108SMilanka Ringwald value = btstack_atoi((char*)hfp_connection->line_buffer); 1300667ec068SMatthias Ringwald hfp_hf_speaker_gain = value; 1301ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value); 1302ce263fc8SMatthias Ringwald break; 1303ce263fc8SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 1304a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 13052308e108SMilanka Ringwald value = btstack_atoi((char*)hfp_connection->line_buffer); 1306667ec068SMatthias Ringwald hfp_hf_microphone_gain = value; 1307ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value); 1308ce263fc8SMatthias Ringwald break; 1309ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 1310a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1311ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number); 1312a0ffb263SMatthias Ringwald break; 1313a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1314a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1315*598d4936SMatthias Ringwald hfp_hf_emit_type_number_alpha(hfp_connection, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION); 1316a0ffb263SMatthias Ringwald break; 1317a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1318a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1319*598d4936SMatthias Ringwald hfp_hf_emit_type_number_alpha(hfp_connection, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION); 1320ce263fc8SMatthias Ringwald break; 1321ce263fc8SMatthias Ringwald case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1322a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 13235a4785c8SMatthias Ringwald hfp_connection->ok_pending = 0; 1324a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 1325ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value); 1326ce263fc8SMatthias Ringwald break; 13270b4debbfSMilanka Ringwald case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION: 13280b4debbfSMilanka Ringwald break; 1329fdda66c0SMilanka Ringwald case HFP_CMD_ERROR: 133090244c92SMilanka Ringwald switch (hfp_connection->state){ 133190244c92SMilanka Ringwald case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 133290244c92SMilanka Ringwald switch (hfp_connection->codecs_state){ 133390244c92SMilanka Ringwald case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 1334fdda66c0SMilanka Ringwald hfp_reset_context_flags(hfp_connection); 133590244c92SMilanka Ringwald hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec); 133690244c92SMilanka Ringwald return; 133790244c92SMilanka Ringwald default: 133890244c92SMilanka Ringwald break; 133990244c92SMilanka Ringwald } 134056f1adacSMilanka Ringwald break; 134156f1adacSMilanka Ringwald default: 134256f1adacSMilanka Ringwald break; 134356f1adacSMilanka Ringwald } 1344e2c3b58dSMilanka Ringwald 1345fdda66c0SMilanka Ringwald // handle error response for voice activation (HF initiated) 13460b4debbfSMilanka Ringwald switch(hfp_connection->vra_state_requested){ 1347de9e0ea7SMilanka Ringwald case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1348de9e0ea7SMilanka Ringwald hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 1349be55a11dSMilanka Ringwald break; 1350be55a11dSMilanka Ringwald default: 1351e2c3b58dSMilanka Ringwald if (hfp_connection->vra_state_requested == hfp_connection->vra_state){ 1352e2c3b58dSMilanka Ringwald break; 1353e2c3b58dSMilanka Ringwald } 13540b4debbfSMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 1355553a4a56SMilanka Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 13560b4debbfSMilanka Ringwald hfp_reset_context_flags(hfp_connection); 13570b4debbfSMilanka Ringwald return; 1358be55a11dSMilanka Ringwald } 1359e2c3b58dSMilanka Ringwald event_emited = hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 1360e2c3b58dSMilanka Ringwald if (!event_emited){ 13610b4debbfSMilanka Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1); 1362e2c3b58dSMilanka Ringwald } 1363fdda66c0SMilanka Ringwald hfp_reset_context_flags(hfp_connection); 1364ce263fc8SMatthias Ringwald break; 1365fdda66c0SMilanka Ringwald 1366ce263fc8SMatthias Ringwald case HFP_CMD_OK: 1367e2c3b58dSMilanka Ringwald hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_SUCCESS); 1368ce263fc8SMatthias Ringwald break; 1369ce263fc8SMatthias Ringwald case HFP_CMD_RING: 13705a4785c8SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1371ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING); 1372ce263fc8SMatthias Ringwald break; 1373ce263fc8SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 13745a4785c8SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 13754562e2a2SMatthias Ringwald hfp_hf_handle_transfer_ag_indicator_status(hfp_connection); 1376ce263fc8SMatthias Ringwald break; 1377c741b032SMilanka Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 13785a4785c8SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1379184a03edSMilanka Ringwald // report status after SLC established 1380184a03edSMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){ 1381184a03edSMilanka Ringwald break; 1382184a03edSMilanka Ringwald } 1383c741b032SMilanka Ringwald for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 13841ac1f60fSMilanka Ringwald hfp_emit_ag_indicator_mapping_event(hfp_connection, &hfp_connection->ag_indicators[i]); 1385c741b032SMilanka Ringwald } 1386c741b032SMilanka Ringwald break; 13871cc65c4fSMatthias Ringwald case HFP_CMD_AG_SUGGESTED_CODEC: 13881cc65c4fSMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 13895a4785c8SMatthias Ringwald hfp_hf_handle_suggested_codec(hfp_connection); 13901cc65c4fSMatthias Ringwald break; 1391eac56539SMilanka Ringwald case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING: 1392eac56539SMilanka 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)); 1393ce263fc8SMatthias Ringwald default: 1394ce263fc8SMatthias Ringwald break; 13953deb3ec6SMatthias Ringwald } 13960cef86faSMatthias Ringwald } 1397426f9988SMatthias Ringwald 139876cc1527SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){ 139976cc1527SMatthias Ringwald return (byte == '\n') || (byte == '\r'); 140076cc1527SMatthias Ringwald } 140176cc1527SMatthias Ringwald 14020b4debbfSMilanka Ringwald static void hfp_hf_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){ 1403426f9988SMatthias Ringwald // assertion: size >= 1 as rfcomm.c does not deliver empty packets 1404426f9988SMatthias Ringwald if (size < 1) return; 1405426f9988SMatthias Ringwald 1406426f9988SMatthias Ringwald hfp_log_rfcomm_message("HFP_HF_RX", packet, size); 1407e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES 1408e43d1938SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet); 1409e43d1938SMatthias Ringwald #endif 1410426f9988SMatthias Ringwald 1411426f9988SMatthias Ringwald // process messages byte-wise 1412a7ba78b0SMilanka Ringwald uint8_t pos; 1413426f9988SMatthias Ringwald for (pos = 0; pos < size; pos++){ 1414426f9988SMatthias Ringwald hfp_parse(hfp_connection, packet[pos], 1); 14151599fe57SMatthias Ringwald // parse until end of line "\r" or "\n" 1416426f9988SMatthias Ringwald if (!hfp_parser_is_end_of_line(packet[pos])) continue; 14170b4debbfSMilanka Ringwald hfp_hf_handle_rfcomm_command(hfp_connection); 14183deb3ec6SMatthias Ringwald } 1419a7ba78b0SMilanka Ringwald } 14203deb3ec6SMatthias Ringwald 14211c6a0fc0SMatthias Ringwald static void hfp_hf_run(void){ 1422665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1423665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1424665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1425a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 142622387625SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_HF) continue; 14271c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 14283deb3ec6SMatthias Ringwald } 14293deb3ec6SMatthias Ringwald } 14303deb3ec6SMatthias Ringwald 14311c6a0fc0SMatthias Ringwald static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 14320b4debbfSMilanka Ringwald hfp_connection_t * hfp_connection; 14333deb3ec6SMatthias Ringwald switch (packet_type){ 14343deb3ec6SMatthias Ringwald case RFCOMM_DATA_PACKET: 14350b4debbfSMilanka Ringwald hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 14360b4debbfSMilanka Ringwald if (!hfp_connection) return; 14370b4debbfSMilanka Ringwald hfp_hf_handle_rfcomm_data(hfp_connection, packet, size); 14380b4debbfSMilanka Ringwald hfp_hf_run_for_context(hfp_connection); 14390b4debbfSMilanka Ringwald return; 14403deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 1441d4dd47ffSMatthias Ringwald if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 1442d4dd47ffSMatthias Ringwald uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 14431c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid)); 1444d4dd47ffSMatthias Ringwald return; 1445d4dd47ffSMatthias Ringwald } 144627950165SMatthias Ringwald hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF); 1447202c8a4cSMatthias Ringwald break; 14483deb3ec6SMatthias Ringwald default: 14493deb3ec6SMatthias Ringwald break; 14503deb3ec6SMatthias Ringwald } 14511c6a0fc0SMatthias Ringwald hfp_hf_run(); 14523deb3ec6SMatthias Ringwald } 14533deb3ec6SMatthias Ringwald 14541c6a0fc0SMatthias Ringwald static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1455405014fbSMatthias Ringwald hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF); 14561c6a0fc0SMatthias Ringwald hfp_hf_run(); 1457405014fbSMatthias Ringwald } 1458405014fbSMatthias Ringwald 1459aeb0f0feSMatthias Ringwald static void hfp_hf_set_defaults(void){ 1460aeb0f0feSMatthias Ringwald hfp_hf_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 1461aeb0f0feSMatthias Ringwald hfp_hf_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS; 1462aeb0f0feSMatthias Ringwald hfp_hf_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1463aeb0f0feSMatthias Ringwald hfp_hf_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD; 1464aeb0f0feSMatthias Ringwald hfp_hf_codecs_nr = 0; 1465aeb0f0feSMatthias Ringwald hfp_hf_speaker_gain = 9; 1466aeb0f0feSMatthias Ringwald hfp_hf_microphone_gain = 9; 1467aeb0f0feSMatthias Ringwald hfp_hf_indicators_nr = 0; 1468aeb0f0feSMatthias Ringwald } 1469aeb0f0feSMatthias Ringwald 1470b4df8028SMilanka Ringwald uint8_t hfp_hf_init(uint16_t rfcomm_channel_nr){ 1471b4df8028SMilanka Ringwald uint8_t status = rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 1472b4df8028SMilanka Ringwald if (status != ERROR_CODE_SUCCESS){ 1473b4df8028SMilanka Ringwald return status; 1474b4df8028SMilanka Ringwald } 1475b4df8028SMilanka Ringwald 1476520c92d5SMatthias Ringwald hfp_init(); 1477aeb0f0feSMatthias Ringwald hfp_hf_set_defaults(); 1478d63c37a1SMatthias Ringwald 14791c6a0fc0SMatthias Ringwald hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler; 14801c6a0fc0SMatthias Ringwald hci_add_event_handler(&hfp_hf_hci_event_callback_registration); 148127950165SMatthias Ringwald 148227950165SMatthias Ringwald // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us 14831c6a0fc0SMatthias Ringwald hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler); 1484b4df8028SMilanka Ringwald return ERROR_CODE_SUCCESS; 148520b2edb6SMatthias Ringwald } 148627950165SMatthias Ringwald 148720b2edb6SMatthias Ringwald void hfp_hf_deinit(void){ 148820b2edb6SMatthias Ringwald hfp_deinit(); 1489aeb0f0feSMatthias Ringwald hfp_hf_set_defaults(); 1490aeb0f0feSMatthias Ringwald 1491aeb0f0feSMatthias Ringwald hfp_hf_callback = NULL; 149220b2edb6SMatthias Ringwald (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t)); 1493aeb0f0feSMatthias Ringwald (void) memset(hfp_hf_phone_number, 0, sizeof(hfp_hf_phone_number)); 1494a0ffb263SMatthias Ringwald } 1495a0ffb263SMatthias Ringwald 14967ca89cabSMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, const uint8_t * codecs){ 149768466199SMilanka Ringwald btstack_assert(codecs_nr < HFP_MAX_NUM_CODECS); 14983deb3ec6SMatthias Ringwald 1499aeb0f0feSMatthias Ringwald hfp_hf_codecs_nr = codecs_nr; 15003deb3ec6SMatthias Ringwald int i; 15013deb3ec6SMatthias Ringwald for (i=0; i<codecs_nr; i++){ 1502aeb0f0feSMatthias Ringwald hfp_hf_codecs[i] = codecs[i]; 15033deb3ec6SMatthias Ringwald } 15043deb3ec6SMatthias Ringwald } 15053deb3ec6SMatthias Ringwald 1506a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){ 1507aeb0f0feSMatthias Ringwald hfp_hf_supported_features = supported_features; 1508a0ffb263SMatthias Ringwald } 15093deb3ec6SMatthias Ringwald 15107ca89cabSMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){ 1511aeb0f0feSMatthias Ringwald btstack_assert(hfp_hf_indicators_nr < HFP_MAX_NUM_INDICATORS); 151268466199SMilanka Ringwald 1513aeb0f0feSMatthias Ringwald hfp_hf_indicators_nr = indicators_nr; 15143deb3ec6SMatthias Ringwald int i; 1515aeb0f0feSMatthias Ringwald for (i = 0; i < hfp_hf_indicators_nr ; i++){ 1516aeb0f0feSMatthias Ringwald hfp_hf_indicators[i] = indicators[i]; 15173deb3ec6SMatthias Ringwald } 15183deb3ec6SMatthias Ringwald } 15193deb3ec6SMatthias Ringwald 15204eb3f1d8SMilanka Ringwald uint8_t hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){ 15214eb3f1d8SMilanka Ringwald return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF); 15223deb3ec6SMatthias Ringwald } 15233deb3ec6SMatthias Ringwald 1524657bc59fSMilanka Ringwald uint8_t hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){ 15259c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1526a33eb0c4SMilanka Ringwald if (!hfp_connection){ 1527657bc59fSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1528a33eb0c4SMilanka Ringwald } 15291ffa0dd9SMilanka Ringwald hfp_trigger_release_service_level_connection(hfp_connection); 15301c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1531657bc59fSMilanka Ringwald return ERROR_CODE_SUCCESS; 15323deb3ec6SMatthias Ringwald } 15333deb3ec6SMatthias Ringwald 15343c65e705SMilanka Ringwald static uint8_t hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){ 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->enable_status_update_for_ag_indicators = enable; 15401c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 15413c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 15423deb3ec6SMatthias Ringwald } 15433deb3ec6SMatthias Ringwald 15443c65e705SMilanka Ringwald uint8_t hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 15453c65e705SMilanka Ringwald return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1); 1546ce263fc8SMatthias Ringwald } 1547ce263fc8SMatthias Ringwald 15483c65e705SMilanka Ringwald uint8_t hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 15493c65e705SMilanka Ringwald return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0); 1550ce263fc8SMatthias Ringwald } 1551ce263fc8SMatthias Ringwald 15523deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format 15533c65e705SMilanka Ringwald uint8_t hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){ 15549c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1555a0ffb263SMatthias Ringwald if (!hfp_connection) { 15563c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 15573deb3ec6SMatthias Ringwald } 1558a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 1; 1559a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap; 15601c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 15613c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 15623deb3ec6SMatthias Ringwald } 15633deb3ec6SMatthias Ringwald 15643c65e705SMilanka Ringwald uint8_t hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){ 15659c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1566a0ffb263SMatthias Ringwald if (!hfp_connection) { 15673c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 15683deb3ec6SMatthias Ringwald } 15693c65e705SMilanka Ringwald 1570a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 1571ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET: 1572a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT; 1573ce263fc8SMatthias Ringwald break; 1574ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_FORMAT_SET: 1575a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1576ce263fc8SMatthias Ringwald break; 1577ce263fc8SMatthias Ringwald default: 15783c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1579ce263fc8SMatthias Ringwald } 15801c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 15813c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 15823deb3ec6SMatthias Ringwald } 15833deb3ec6SMatthias Ringwald 15843c65e705SMilanka Ringwald static uint8_t hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){ 15859c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1586a0ffb263SMatthias Ringwald if (!hfp_connection) { 15873c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 15883deb3ec6SMatthias Ringwald } 1589a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = enable; 15901c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 15913c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 15923deb3ec6SMatthias Ringwald } 15933deb3ec6SMatthias Ringwald 1594ce263fc8SMatthias Ringwald 15953c65e705SMilanka Ringwald uint8_t hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 15963c65e705SMilanka Ringwald return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1); 1597ce263fc8SMatthias Ringwald } 1598ce263fc8SMatthias Ringwald 15993c65e705SMilanka Ringwald uint8_t hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 16003c65e705SMilanka Ringwald return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0); 1601ce263fc8SMatthias Ringwald } 1602ce263fc8SMatthias Ringwald 160338200c1dSMilanka Ringwald static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){ 1604aeb0f0feSMatthias Ringwald return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) && (hfp_hf_supported_features & (1 << HFP_HFSF_ESCO_S4)); 160538200c1dSMilanka Ringwald } 1606ce263fc8SMatthias Ringwald 16073c65e705SMilanka Ringwald uint8_t hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){ 16089c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1609a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16103c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1611a33eb0c4SMilanka Ringwald } 1612ce263fc8SMatthias Ringwald 16133c65e705SMilanka Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 16143c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 16153c65e705SMilanka Ringwald } 16163c65e705SMilanka Ringwald 16173c65e705SMilanka Ringwald if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){ 16183c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 16193c65e705SMilanka Ringwald } 1620f4412093SMatthias Ringwald if (has_codec_negotiation_feature(hfp_connection)) { 1621a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state) { 1622aa4dd815SMatthias Ringwald case HFP_CODECS_W4_AG_COMMON_CODEC: 1623aa4dd815SMatthias Ringwald break; 1624ec3bfc1aSMatthias Ringwald case HFP_CODECS_EXCHANGED: 1625ec3bfc1aSMatthias Ringwald hfp_connection->trigger_codec_exchange = 1; 1626ec3bfc1aSMatthias Ringwald break; 1627aa4dd815SMatthias Ringwald default: 16281cc65c4fSMatthias Ringwald hfp_connection->codec_confirmed = 0; 16291cc65c4fSMatthias Ringwald hfp_connection->suggested_codec = 0; 16301cc65c4fSMatthias Ringwald hfp_connection->negotiated_codec = 0; 16311cc65c4fSMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 163238200c1dSMilanka Ringwald hfp_connection->trigger_codec_exchange = 1; 1633aa4dd815SMatthias Ringwald break; 16343deb3ec6SMatthias Ringwald } 1635f4412093SMatthias Ringwald } else { 1636f4412093SMatthias Ringwald log_info("no codec negotiation feature, use CVSD"); 1637f4412093SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 1638f4412093SMatthias Ringwald hfp_connection->suggested_codec = HFP_CODEC_CVSD; 1639f4412093SMatthias Ringwald hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 1640f4412093SMatthias Ringwald hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 1641f4412093SMatthias Ringwald hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection)); 1642f4412093SMatthias Ringwald hfp_connection->establish_audio_connection = 1; 1643f4412093SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 1644ce263fc8SMatthias Ringwald } 1645ce263fc8SMatthias Ringwald 16461c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 16473c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 16483deb3ec6SMatthias Ringwald } 16493deb3ec6SMatthias Ringwald 16503c65e705SMilanka Ringwald uint8_t hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){ 16519c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1652a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16533c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1654a33eb0c4SMilanka Ringwald } 16550b4debbfSMilanka Ringwald if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){ 16560b4debbfSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 16570b4debbfSMilanka Ringwald } 16580b4debbfSMilanka Ringwald uint8_t status = hfp_trigger_release_audio_connection(hfp_connection); 16590b4debbfSMilanka Ringwald if (status == ERROR_CODE_SUCCESS){ 16601c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 16610b4debbfSMilanka Ringwald } 16623c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 16633deb3ec6SMatthias Ringwald } 16643deb3ec6SMatthias Ringwald 16653c65e705SMilanka Ringwald uint8_t hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){ 16669c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1667a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16683c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1669a33eb0c4SMilanka Ringwald } 1670ce263fc8SMatthias Ringwald 1671aeb0f0feSMatthias Ringwald if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1672a0ffb263SMatthias Ringwald hfp_connection->hf_answer_incoming_call = 1; 16731c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1674ce263fc8SMatthias Ringwald } else { 1675aeb0f0feSMatthias Ringwald log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_hf_callsetup_status); 16763c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1677ce263fc8SMatthias Ringwald } 16783c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1679ce263fc8SMatthias Ringwald } 1680ce263fc8SMatthias Ringwald 16813c65e705SMilanka Ringwald uint8_t hfp_hf_terminate_call(hci_con_handle_t acl_handle){ 16829c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1683a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16843c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1685a33eb0c4SMilanka Ringwald } 1686a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 1; 16871c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 16883c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1689ce263fc8SMatthias Ringwald } 1690ce263fc8SMatthias Ringwald 16913c65e705SMilanka Ringwald uint8_t hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){ 16929c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1693a33eb0c4SMilanka Ringwald if (!hfp_connection) { 16943c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1695a33eb0c4SMilanka Ringwald } 1696ce263fc8SMatthias Ringwald 1697aeb0f0feSMatthias Ringwald if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1698a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 1; 16991c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1700ce263fc8SMatthias Ringwald } 17013c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1702ce263fc8SMatthias Ringwald } 1703ce263fc8SMatthias Ringwald 17043c65e705SMilanka Ringwald uint8_t hfp_hf_user_busy(hci_con_handle_t acl_handle){ 17059c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1706a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17073c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1708a33eb0c4SMilanka Ringwald } 1709ce263fc8SMatthias Ringwald 1710aeb0f0feSMatthias Ringwald if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1711a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_0 = 1; 17121c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1713ce263fc8SMatthias Ringwald } 17143c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1715ce263fc8SMatthias Ringwald } 1716ce263fc8SMatthias Ringwald 17173c65e705SMilanka Ringwald uint8_t hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){ 17189c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1719a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17203c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1721a33eb0c4SMilanka Ringwald } 1722ce263fc8SMatthias Ringwald 1723aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1724aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1725a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_1 = 1; 17261c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1727ce263fc8SMatthias Ringwald } 17283c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1729ce263fc8SMatthias Ringwald } 1730ce263fc8SMatthias Ringwald 17313c65e705SMilanka Ringwald uint8_t hfp_hf_swap_calls(hci_con_handle_t acl_handle){ 17329c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1733a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17343c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1735a33eb0c4SMilanka Ringwald } 1736ce263fc8SMatthias Ringwald 1737aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1738aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1739a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_2 = 1; 17401c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1741ce263fc8SMatthias Ringwald } 17423c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1743ce263fc8SMatthias Ringwald } 1744ce263fc8SMatthias Ringwald 17453c65e705SMilanka Ringwald uint8_t hfp_hf_join_held_call(hci_con_handle_t acl_handle){ 17469c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1747a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17483c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1749a33eb0c4SMilanka Ringwald } 1750ce263fc8SMatthias Ringwald 1751aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1752aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1753a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_3 = 1; 17541c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1755ce263fc8SMatthias Ringwald } 17563c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1757ce263fc8SMatthias Ringwald } 1758ce263fc8SMatthias Ringwald 17593c65e705SMilanka Ringwald uint8_t hfp_hf_connect_calls(hci_con_handle_t acl_handle){ 17609c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1761a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17623c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1763a33eb0c4SMilanka Ringwald } 1764ce263fc8SMatthias Ringwald 1765aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1766aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1767a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_4 = 1; 17681c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1769ce263fc8SMatthias Ringwald } 17703c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1771ce263fc8SMatthias Ringwald } 1772ce263fc8SMatthias Ringwald 17733c65e705SMilanka Ringwald uint8_t hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){ 17749c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1775a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17763c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1777a33eb0c4SMilanka Ringwald } 1778667ec068SMatthias Ringwald 1779aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1780aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1781a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 1; 1782a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x_index = 10 + index; 17831c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1784667ec068SMatthias Ringwald } 17853c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1786667ec068SMatthias Ringwald } 1787667ec068SMatthias Ringwald 17883c65e705SMilanka Ringwald uint8_t hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){ 17899c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1790a33eb0c4SMilanka Ringwald if (!hfp_connection) { 17913c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1792a33eb0c4SMilanka Ringwald } 1793667ec068SMatthias Ringwald 1794aeb0f0feSMatthias Ringwald if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1795aeb0f0feSMatthias Ringwald (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1796a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 1; 1797a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x_index = 20 + index; 17981c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 1799667ec068SMatthias Ringwald } 18003c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1801667ec068SMatthias Ringwald } 1802ce263fc8SMatthias Ringwald 18033c65e705SMilanka Ringwald uint8_t hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){ 18049c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1805a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18063c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1807a33eb0c4SMilanka Ringwald } 1808ce263fc8SMatthias Ringwald 1809a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_outgoing_call = 1; 1810aeb0f0feSMatthias Ringwald snprintf(hfp_hf_phone_number, sizeof(hfp_hf_phone_number), "%s", number); 18111c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18123c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1813ce263fc8SMatthias Ringwald } 1814ce263fc8SMatthias Ringwald 18153c65e705SMilanka Ringwald uint8_t hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){ 18169c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1817a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18183c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1819a33eb0c4SMilanka Ringwald } 1820ce263fc8SMatthias Ringwald 1821a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_memory_dialing = 1; 1822a0ffb263SMatthias Ringwald hfp_connection->memory_id = memory_id; 1823a0ffb263SMatthias Ringwald 18241c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18253c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1826ce263fc8SMatthias Ringwald } 1827ce263fc8SMatthias Ringwald 18283c65e705SMilanka Ringwald uint8_t hfp_hf_redial_last_number(hci_con_handle_t acl_handle){ 18299c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1830a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18313c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1832a33eb0c4SMilanka Ringwald } 1833ce263fc8SMatthias Ringwald 1834a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_redial_last_number = 1; 18351c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18363c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1837ce263fc8SMatthias Ringwald } 1838ce263fc8SMatthias Ringwald 18393c65e705SMilanka Ringwald uint8_t hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){ 18409c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1841a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18423c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1843a33eb0c4SMilanka Ringwald } 1844ce263fc8SMatthias Ringwald 1845a0ffb263SMatthias Ringwald hfp_connection->hf_activate_call_waiting_notification = 1; 18461c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18473c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1848ce263fc8SMatthias Ringwald } 1849ce263fc8SMatthias Ringwald 1850ce263fc8SMatthias Ringwald 18513c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){ 18529c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1853a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18543c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1855a33eb0c4SMilanka Ringwald } 1856ce263fc8SMatthias Ringwald 1857a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_call_waiting_notification = 1; 18581c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18593c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1860ce263fc8SMatthias Ringwald } 1861ce263fc8SMatthias Ringwald 1862ce263fc8SMatthias Ringwald 18633c65e705SMilanka Ringwald uint8_t hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){ 18649c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1865a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18663c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1867a33eb0c4SMilanka Ringwald } 1868ce263fc8SMatthias Ringwald 1869a0ffb263SMatthias Ringwald hfp_connection->hf_activate_calling_line_notification = 1; 18701c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18713c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1872ce263fc8SMatthias Ringwald } 1873ce263fc8SMatthias Ringwald 18743c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){ 18759c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1876a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18773c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1878a33eb0c4SMilanka Ringwald } 1879ce263fc8SMatthias Ringwald 1880a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_calling_line_notification = 1; 18811c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 18823c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1883ce263fc8SMatthias Ringwald } 1884ce263fc8SMatthias Ringwald 18856ba83b5eSMilanka Ringwald static bool hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection_t * hfp_connection){ 18866ba83b5eSMilanka Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_EC_NR_FUNCTION); 1887aeb0f0feSMatthias Ringwald int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_EC_NR_FUNCTION); 18886ba83b5eSMilanka Ringwald return hf && ag; 1889ce263fc8SMatthias Ringwald } 1890ce263fc8SMatthias Ringwald 18913c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){ 18929c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1893a33eb0c4SMilanka Ringwald if (!hfp_connection) { 18943c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1895a33eb0c4SMilanka Ringwald } 18966ba83b5eSMilanka Ringwald if (!hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection)){ 18976ba83b5eSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 18986ba83b5eSMilanka Ringwald } 1899ce263fc8SMatthias Ringwald 1900a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1; 19011c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 19023c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 1903ce263fc8SMatthias Ringwald } 1904ce263fc8SMatthias Ringwald 1905acd11d4aSMilanka Ringwald uint8_t hfp_hf_activate_voice_recognition(hci_con_handle_t acl_handle){ 1906fdda66c0SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1907fdda66c0SMilanka Ringwald if (!hfp_connection) { 1908fdda66c0SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1909be55a11dSMilanka Ringwald } 1910013cc750SMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){ 1911013cc750SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1912013cc750SMilanka Ringwald } 1913acd11d4aSMilanka Ringwald 1914acd11d4aSMilanka Ringwald bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection); 1915acd11d4aSMilanka Ringwald bool legacy_vra_supported = hfp_hf_vra_flag_supported(hfp_connection); 1916acd11d4aSMilanka Ringwald if (!enhanced_vra_supported && !legacy_vra_supported){ 1917acd11d4aSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1918af97579eSMilanka Ringwald } 1919af97579eSMilanka Ringwald 1920498a8121SMilanka Ringwald switch (hfp_connection->vra_state){ 1921be55a11dSMilanka Ringwald case HFP_VRA_VOICE_RECOGNITION_OFF: 1922de9e0ea7SMilanka Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 1923fd4151d1SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED; 1924acd11d4aSMilanka Ringwald hfp_connection->enhanced_voice_recognition_enabled = enhanced_vra_supported; 1925be55a11dSMilanka Ringwald break; 1926de9e0ea7SMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_OFF: 1927de9e0ea7SMilanka Ringwald hfp_connection->activate_voice_recognition = true; 1928de9e0ea7SMilanka Ringwald break; 1929be55a11dSMilanka Ringwald default: 1930be55a11dSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1931be55a11dSMilanka Ringwald } 1932ce263fc8SMatthias Ringwald 1933af97579eSMilanka Ringwald hfp_hf_run_for_context(hfp_connection); 1934fdda66c0SMilanka Ringwald return ERROR_CODE_SUCCESS; 1935af97579eSMilanka Ringwald } 1936af97579eSMilanka Ringwald 1937acd11d4aSMilanka Ringwald uint8_t hfp_hf_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle){ 1938af97579eSMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1939af97579eSMilanka Ringwald if (!hfp_connection) { 1940af97579eSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1941af97579eSMilanka Ringwald } 194284fb9ac1SMilanka Ringwald 194384fb9ac1SMilanka Ringwald if (hfp_connection->emit_vra_enabled_after_audio_established){ 194484fb9ac1SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 194584fb9ac1SMilanka Ringwald } 194684fb9ac1SMilanka Ringwald 1947acd11d4aSMilanka Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){ 194808a0b01cSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 194908a0b01cSMilanka Ringwald } 1950acd11d4aSMilanka Ringwald 1951acd11d4aSMilanka Ringwald bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection); 1952acd11d4aSMilanka Ringwald if (!enhanced_vra_supported){ 1953acd11d4aSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1954acd11d4aSMilanka Ringwald } 1955acd11d4aSMilanka Ringwald 1956acd11d4aSMilanka Ringwald switch (hfp_connection->vra_state){ 1957acd11d4aSMilanka Ringwald case HFP_VRA_VOICE_RECOGNITION_ACTIVATED: 1958acd11d4aSMilanka Ringwald case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1959acd11d4aSMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 1960acd11d4aSMilanka Ringwald break; 1961acd11d4aSMilanka Ringwald default: 1962fdda66c0SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1963af97579eSMilanka Ringwald } 1964013cc750SMilanka Ringwald 1965acd11d4aSMilanka Ringwald hfp_hf_run_for_context(hfp_connection); 1966acd11d4aSMilanka Ringwald return ERROR_CODE_SUCCESS; 1967acd11d4aSMilanka Ringwald } 1968acd11d4aSMilanka Ringwald 1969acd11d4aSMilanka Ringwald 1970acd11d4aSMilanka Ringwald uint8_t hfp_hf_deactivate_voice_recognition(hci_con_handle_t acl_handle){ 1971acd11d4aSMilanka Ringwald // return deactivate_voice_recognition(acl_handle, false); 1972acd11d4aSMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1973acd11d4aSMilanka Ringwald if (!hfp_connection) { 1974acd11d4aSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1975acd11d4aSMilanka Ringwald } 1976acd11d4aSMilanka Ringwald 197784fb9ac1SMilanka Ringwald if (hfp_connection->emit_vra_enabled_after_audio_established){ 197884fb9ac1SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 197984fb9ac1SMilanka Ringwald } 198084fb9ac1SMilanka Ringwald 1981acd11d4aSMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || 1982acd11d4aSMilanka Ringwald hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){ 1983acd11d4aSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1984acd11d4aSMilanka Ringwald } 1985acd11d4aSMilanka Ringwald 1986acd11d4aSMilanka Ringwald bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection); 1987acd11d4aSMilanka Ringwald bool legacy_vra_supported = hfp_hf_vra_flag_supported(hfp_connection); 1988acd11d4aSMilanka Ringwald if (!enhanced_vra_supported && !legacy_vra_supported){ 1989acd11d4aSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1990acd11d4aSMilanka Ringwald } 1991acd11d4aSMilanka Ringwald 1992fdda66c0SMilanka Ringwald switch (hfp_connection->vra_state){ 1993de9e0ea7SMilanka Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED: 1994fdda66c0SMilanka Ringwald case HFP_VRA_VOICE_RECOGNITION_ACTIVATED: 1995de9e0ea7SMilanka Ringwald case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1996de9e0ea7SMilanka Ringwald case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1997fdda66c0SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF; 1998fdda66c0SMilanka Ringwald break; 1999de9e0ea7SMilanka Ringwald 2000de9e0ea7SMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED: 2001de9e0ea7SMilanka Ringwald case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 2002de9e0ea7SMilanka Ringwald hfp_connection->deactivate_voice_recognition = true; 2003de9e0ea7SMilanka Ringwald break; 2004de9e0ea7SMilanka Ringwald 2005de9e0ea7SMilanka Ringwald case HFP_VRA_VOICE_RECOGNITION_OFF: 2006de9e0ea7SMilanka Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 2007de9e0ea7SMilanka Ringwald case HFP_VRA_W4_VOICE_RECOGNITION_OFF: 2008fdda66c0SMilanka Ringwald default: 2009fdda66c0SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2010fdda66c0SMilanka Ringwald } 2011fdda66c0SMilanka Ringwald 2012fdda66c0SMilanka Ringwald hfp_hf_run_for_context(hfp_connection); 2013fdda66c0SMilanka Ringwald return ERROR_CODE_SUCCESS; 2014af97579eSMilanka Ringwald } 2015af97579eSMilanka Ringwald 20163c65e705SMilanka Ringwald uint8_t hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 20179c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2018a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20193c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2020a33eb0c4SMilanka Ringwald } 2021c8626498SMilanka Ringwald 20223c65e705SMilanka Ringwald if (hfp_connection->microphone_gain == gain) { 20233c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 20243c65e705SMilanka Ringwald } 20253c65e705SMilanka Ringwald 2026c1ab6cc1SMatthias Ringwald if ((gain < 0) || (gain > 15)){ 2027a0ffb263SMatthias Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 20283c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2029a0ffb263SMatthias Ringwald } 20303c65e705SMilanka Ringwald 2031a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = gain; 2032a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 20331c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20343c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2035ce263fc8SMatthias Ringwald } 2036ce263fc8SMatthias Ringwald 20373c65e705SMilanka Ringwald uint8_t hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 20389c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2039a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20403c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2041a33eb0c4SMilanka Ringwald } 2042c8626498SMilanka Ringwald 20433c65e705SMilanka Ringwald if (hfp_connection->speaker_gain == gain){ 20443c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 20453c65e705SMilanka Ringwald } 20463c65e705SMilanka Ringwald 2047c1ab6cc1SMatthias Ringwald if ((gain < 0) || (gain > 15)){ 2048a0ffb263SMatthias Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 20493c65e705SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2050a0ffb263SMatthias Ringwald } 20513c65e705SMilanka Ringwald 2052a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = gain; 2053a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 20541c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20553c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2056ce263fc8SMatthias Ringwald } 2057ce263fc8SMatthias Ringwald 20583c65e705SMilanka Ringwald uint8_t hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){ 20599c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2060a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20613c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2062a33eb0c4SMilanka Ringwald } 2063a0ffb263SMatthias Ringwald hfp_connection->hf_send_dtmf_code = code; 20641c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20653c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2066ce263fc8SMatthias Ringwald } 2067ce263fc8SMatthias Ringwald 20683c65e705SMilanka Ringwald uint8_t hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 20699c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2070a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20713c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2072a33eb0c4SMilanka Ringwald } 2073a0ffb263SMatthias Ringwald hfp_connection->hf_send_binp = 1; 20741c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20753c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2076ce263fc8SMatthias Ringwald } 20773deb3ec6SMatthias Ringwald 20783c65e705SMilanka Ringwald uint8_t hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){ 20799c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2080a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20813c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2082a33eb0c4SMilanka Ringwald } 2083a0ffb263SMatthias Ringwald hfp_connection->hf_send_clcc = 1; 20841c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20853c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2086667ec068SMatthias Ringwald } 2087667ec068SMatthias Ringwald 2088667ec068SMatthias Ringwald 20893c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){ 20909c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2091a33eb0c4SMilanka Ringwald if (!hfp_connection) { 20923c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2093a33eb0c4SMilanka Ringwald } 2094a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 2095a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '?'; 20961c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 20973c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2098667ec068SMatthias Ringwald } 2099667ec068SMatthias Ringwald 21003c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){ 21019c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2102a33eb0c4SMilanka Ringwald if (!hfp_connection) { 21033c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2104a33eb0c4SMilanka Ringwald } 2105a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 2106a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '0'; 21071c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 21083c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2109667ec068SMatthias Ringwald } 2110667ec068SMatthias Ringwald 21113c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){ 21129c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2113a33eb0c4SMilanka Ringwald if (!hfp_connection) { 21143c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2115a33eb0c4SMilanka Ringwald } 2116a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 2117a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '1'; 21181c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 21193c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2120667ec068SMatthias Ringwald } 2121667ec068SMatthias Ringwald 21223c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){ 21239c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2124a33eb0c4SMilanka Ringwald if (!hfp_connection) { 21253c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2126a33eb0c4SMilanka Ringwald } 2127a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 2128a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '2'; 21291c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 21303c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2131667ec068SMatthias Ringwald } 2132667ec068SMatthias Ringwald 21333c65e705SMilanka Ringwald uint8_t hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){ 21349c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2135a33eb0c4SMilanka Ringwald if (!hfp_connection) { 21363c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2137a33eb0c4SMilanka Ringwald } 2138a0ffb263SMatthias Ringwald hfp_connection->hf_send_cnum = 1; 21391c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 21403c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2141667ec068SMatthias Ringwald } 2142667ec068SMatthias Ringwald 21433c65e705SMilanka Ringwald uint8_t hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){ 21449c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2145a33eb0c4SMilanka Ringwald if (!hfp_connection) { 21463c65e705SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2147a33eb0c4SMilanka Ringwald } 2148667ec068SMatthias Ringwald // find index for assigned number 2149667ec068SMatthias Ringwald int i; 2150aeb0f0feSMatthias Ringwald for (i = 0; i < hfp_hf_indicators_nr ; i++){ 2151aeb0f0feSMatthias Ringwald if (hfp_hf_indicators[i] == assigned_number){ 2152667ec068SMatthias Ringwald // set value 2153aeb0f0feSMatthias Ringwald hfp_hf_indicators_value[i] = value; 2154667ec068SMatthias Ringwald // mark for update 2155a0ffb263SMatthias Ringwald if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){ 2156a0ffb263SMatthias Ringwald hfp_connection->generic_status_update_bitmap |= (1<<i); 2157667ec068SMatthias Ringwald // send update 21581c6a0fc0SMatthias Ringwald hfp_hf_run_for_context(hfp_connection); 2159a0ffb263SMatthias Ringwald } 21603c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2161667ec068SMatthias Ringwald } 2162667ec068SMatthias Ringwald } 21633c65e705SMilanka Ringwald return ERROR_CODE_SUCCESS; 2164667ec068SMatthias Ringwald } 2165667ec068SMatthias Ringwald 2166d7f6b5cbSMatthias Ringwald int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){ 2167d7f6b5cbSMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2168d7f6b5cbSMatthias Ringwald if (!hfp_connection) { 2169d7f6b5cbSMatthias Ringwald return 0; 2170d7f6b5cbSMatthias Ringwald } 2171d7f6b5cbSMatthias Ringwald return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE); 2172d7f6b5cbSMatthias Ringwald } 217376cc1527SMatthias Ringwald 217476cc1527SMatthias 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){ 217576cc1527SMatthias Ringwald if (!name){ 2176aeb0f0feSMatthias Ringwald name = hfp_hf_default_service_name; 217776cc1527SMatthias Ringwald } 217876cc1527SMatthias Ringwald hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name); 217976cc1527SMatthias Ringwald 218076cc1527SMatthias Ringwald // Construct SupportedFeatures for SDP bitmap: 218176cc1527SMatthias Ringwald // 218276cc1527SMatthias Ringwald // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 218376cc1527SMatthias Ringwald // of the Bits 0 to 4 of the unsolicited result code +BRSF" 218476cc1527SMatthias Ringwald // 218576cc1527SMatthias Ringwald // Wide band speech (bit 5) requires Codec negotiation 218676cc1527SMatthias Ringwald // 218776cc1527SMatthias Ringwald uint16_t sdp_features = supported_features & 0x1f; 2188ef3ae4ebSMilanka Ringwald if ( (wide_band_speech != 0) && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){ 218976cc1527SMatthias Ringwald sdp_features |= 1 << 5; 219076cc1527SMatthias Ringwald } 2191ef3ae4ebSMilanka Ringwald 2192ef3ae4ebSMilanka Ringwald if (supported_features & (1 << HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS)){ 219356f1adacSMilanka Ringwald sdp_features |= 1 << 6; 2194ef3ae4ebSMilanka Ringwald } 2195ef3ae4ebSMilanka Ringwald 2196ef3ae4ebSMilanka Ringwald if (supported_features & (1 << HFP_HFSF_VOICE_RECOGNITION_TEXT)){ 219756f1adacSMilanka Ringwald sdp_features |= 1 << 7; 2198ef3ae4ebSMilanka Ringwald } 2199ef3ae4ebSMilanka Ringwald 220076cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 220176cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 220276cc1527SMatthias Ringwald } 220376cc1527SMatthias Ringwald 220476cc1527SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){ 220568466199SMilanka Ringwald btstack_assert(callback != NULL); 220668466199SMilanka Ringwald 220776cc1527SMatthias Ringwald hfp_hf_callback = callback; 220876cc1527SMatthias Ringwald hfp_set_hf_callback(callback); 220976cc1527SMatthias Ringwald } 2210