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_ag.c" 393deb3ec6SMatthias Ringwald 403deb3ec6SMatthias Ringwald // ***************************************************************************** 413deb3ec6SMatthias Ringwald // 4266a048abSMatthias Ringwald // HFP Audio Gateway (AG) 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 5256042629SMatthias Ringwald #include "hci_cmd.h" 5382636622SMatthias Ringwald #include "btstack_run_loop.h" 543deb3ec6SMatthias Ringwald 55235946f1SMatthias Ringwald #include "bluetooth_sdp.h" 563deb3ec6SMatthias Ringwald #include "hci.h" 573deb3ec6SMatthias Ringwald #include "btstack_memory.h" 583deb3ec6SMatthias Ringwald #include "hci_dump.h" 593deb3ec6SMatthias Ringwald #include "l2cap.h" 6016ece135SMatthias Ringwald #include "btstack_debug.h" 61e30a6a47SMatthias Ringwald #include "btstack_event.h" 6259c6af15SMatthias Ringwald #include "classic/core.h" 633edc84c5SMatthias Ringwald #include "classic/hfp.h" 643edc84c5SMatthias Ringwald #include "classic/hfp_ag.h" 65023f2764SMatthias Ringwald #include "classic/hfp_gsm_model.h" 66efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h" 67023f2764SMatthias Ringwald #include "classic/sdp_server.h" 68023f2764SMatthias Ringwald #include "classic/sdp_util.h" 693deb3ec6SMatthias Ringwald 70c5b64319SMatthias Ringwald // private prototypes 71f0aeb307SMatthias Ringwald static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection); 722ff1d7d7SMilanka Ringwald static void hfp_ag_hf_start_ringing_incoming(hfp_connection_t * hfp_connection); 73fcd59412SMilanka Ringwald static void hfp_ag_hf_start_ringing_outgoing(hfp_connection_t * hfp_connection); 74c5fa3c94SMilanka Ringwald static uint8_t hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection); 75c5b64319SMatthias Ringwald 76c5b64319SMatthias Ringwald // public prototypes 7735833313SMatthias Ringwald hfp_generic_status_indicator_t * get_hfp_generic_status_indicators(void); 7835833313SMatthias Ringwald int get_hfp_generic_status_indicators_nr(void); 79c5b64319SMatthias Ringwald void set_hfp_generic_status_indicators(hfp_generic_status_indicator_t * indicators, int indicator_nr); 80c5b64319SMatthias Ringwald void set_hfp_ag_indicators(hfp_ag_indicator_t * indicators, int indicator_nr); 81c5b64319SMatthias Ringwald int get_hfp_ag_indicators_nr(hfp_connection_t * context); 82c5b64319SMatthias Ringwald hfp_ag_indicator_t * get_hfp_ag_indicators(hfp_connection_t * context); 83c5b64319SMatthias Ringwald 8499af1e28SMilanka Ringwald #define HFP_SUBEVENT_INVALID 0xFFFF 8527950165SMatthias Ringwald 8620b2edb6SMatthias Ringwald // const 87aeb0f0feSMatthias Ringwald static const char hfp_ag_default_service_name[] = "Voice gateway"; 88aa4dd815SMatthias Ringwald 8920b2edb6SMatthias Ringwald // globals 90aeb0f0feSMatthias Ringwald 91aeb0f0feSMatthias Ringwald // higher layer callbacks 92aeb0f0feSMatthias Ringwald static btstack_packet_handler_t hfp_ag_callback; 93aeb0f0feSMatthias Ringwald 9481e25d0aSMatthias Ringwald static bool (*hfp_ag_custom_call_sm_handler)(hfp_ag_call_event_t event); 9581e25d0aSMatthias Ringwald 9620b2edb6SMatthias Ringwald static btstack_packet_callback_registration_t hfp_ag_hci_event_callback_registration; 97aa4dd815SMatthias Ringwald 98aeb0f0feSMatthias Ringwald static uint16_t hfp_ag_supported_features; 9920b2edb6SMatthias Ringwald 10040a8ee13SMatthias Ringwald // in-band ring tone is active on SLC if supported 10140a8ee13SMatthias Ringwald static bool hfp_ag_in_band_ring_tone_active; 10240a8ee13SMatthias Ringwald 103aeb0f0feSMatthias Ringwald // codecs 104aeb0f0feSMatthias Ringwald static uint8_t hfp_ag_codecs_nr; 105aeb0f0feSMatthias Ringwald static uint8_t hfp_ag_codecs[HFP_MAX_NUM_CODECS]; 1063deb3ec6SMatthias Ringwald 107aeb0f0feSMatthias Ringwald // AG indicators 108e58f738aSMatthias Ringwald static uint16_t hfp_ag_indicators_nr; 10925789943SMilanka Ringwald static hfp_ag_indicator_t hfp_ag_indicators[HFP_MAX_NUM_INDICATORS]; 1103deb3ec6SMatthias Ringwald 111aeb0f0feSMatthias Ringwald // generic status indicators 112e58f738aSMatthias Ringwald static uint16_t hfp_ag_generic_status_indicators_nr; 113aeb0f0feSMatthias Ringwald static hfp_generic_status_indicator_t hfp_ag_generic_status_indicators[HFP_MAX_NUM_INDICATORS]; 114a0ffb263SMatthias Ringwald 115e58f738aSMatthias Ringwald static uint16_t hfp_ag_call_hold_services_nr; 1163deb3ec6SMatthias Ringwald static char * hfp_ag_call_hold_services[6]; 1173deb3ec6SMatthias Ringwald 118ce263fc8SMatthias Ringwald static hfp_response_and_hold_state_t hfp_ag_response_and_hold_state; 11927bb1817SMatthias Ringwald static bool hfp_ag_response_and_hold_active = false; 120aa4dd815SMatthias Ringwald 12120b2edb6SMatthias Ringwald // Subscriber information entries 122aeb0f0feSMatthias Ringwald static hfp_phone_number_t * hfp_ag_subscriber_numbers; 123aeb0f0feSMatthias Ringwald static int hfp_ag_subscriber_numbers_count; 124ce263fc8SMatthias Ringwald 125a4f85bd2SMilanka Ringwald // call state 126a4f85bd2SMilanka Ringwald static btstack_timer_source_t hfp_ag_ring_timeout; 127a4f85bd2SMilanka Ringwald 12820b2edb6SMatthias Ringwald // code 129a0ffb263SMatthias Ringwald static int hfp_ag_get_ag_indicators_nr(hfp_connection_t * hfp_connection){ 130a0ffb263SMatthias Ringwald if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){ 131a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr; 1326535961aSMatthias Ringwald (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, 1336535961aSMatthias Ringwald hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 1343deb3ec6SMatthias Ringwald } 135a0ffb263SMatthias Ringwald return hfp_connection->ag_indicators_nr; 136a0ffb263SMatthias Ringwald } 137a0ffb263SMatthias Ringwald 138a0ffb263SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection){ 139a0ffb263SMatthias Ringwald // TODO: save only value, and value changed in the hfp_connection? 140a0ffb263SMatthias Ringwald if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){ 141a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr; 1426535961aSMatthias Ringwald (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, 1436535961aSMatthias Ringwald hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 144a0ffb263SMatthias Ringwald } 145a0ffb263SMatthias Ringwald return (hfp_ag_indicator_t *)&(hfp_connection->ag_indicators); 1463deb3ec6SMatthias Ringwald } 1473deb3ec6SMatthias Ringwald 148aa4dd815SMatthias Ringwald static hfp_ag_indicator_t * get_ag_indicator_for_name(const char * name){ 149aa4dd815SMatthias Ringwald int i; 150aa4dd815SMatthias Ringwald for (i = 0; i < hfp_ag_indicators_nr; i++){ 151aa4dd815SMatthias Ringwald if (strcmp(hfp_ag_indicators[i].name, name) == 0){ 152aa4dd815SMatthias Ringwald return &hfp_ag_indicators[i]; 153aa4dd815SMatthias Ringwald } 154aa4dd815SMatthias Ringwald } 155aa4dd815SMatthias Ringwald return NULL; 156aa4dd815SMatthias Ringwald } 157aa4dd815SMatthias Ringwald 158aa4dd815SMatthias Ringwald static int get_ag_indicator_index_for_name(const char * name){ 159aa4dd815SMatthias Ringwald int i; 160aa4dd815SMatthias Ringwald for (i = 0; i < hfp_ag_indicators_nr; i++){ 161aa4dd815SMatthias Ringwald if (strcmp(hfp_ag_indicators[i].name, name) == 0){ 162aa4dd815SMatthias Ringwald return i; 163aa4dd815SMatthias Ringwald } 164aa4dd815SMatthias Ringwald } 165aa4dd815SMatthias Ringwald return -1; 166aa4dd815SMatthias Ringwald } 167aa4dd815SMatthias Ringwald 1689c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_ag_connection_context_for_acl_handle(uint16_t handle){ 1699c9c64c1SMatthias Ringwald btstack_linked_list_iterator_t it; 1709c9c64c1SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1719c9c64c1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1729c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1739c9c64c1SMatthias Ringwald if (hfp_connection->acl_handle != handle) continue; 1749c9c64c1SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1759c9c64c1SMatthias Ringwald return hfp_connection; 1769c9c64c1SMatthias Ringwald } 1779c9c64c1SMatthias Ringwald return NULL; 1789c9c64c1SMatthias Ringwald } 1793deb3ec6SMatthias Ringwald 18040a8ee13SMatthias Ringwald static bool has_in_band_ring_tone(void){ 18140a8ee13SMatthias Ringwald return get_bit(hfp_ag_supported_features, HFP_AGSF_IN_BAND_RING_TONE) != 0; 18240a8ee13SMatthias Ringwald } 18340a8ee13SMatthias Ringwald 184f197e761SMatthias Ringwald static int use_in_band_tone(hfp_connection_t *hfp_connection) { 18540a8ee13SMatthias Ringwald return hfp_connection->ag_in_band_ring_tone_active ? 1 : 0; 1863deb3ec6SMatthias Ringwald } 1873deb3ec6SMatthias Ringwald 188a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){ 189a0ffb263SMatthias Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_CODEC_NEGOTIATION); 190aeb0f0feSMatthias Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_CODEC_NEGOTIATION); 1913deb3ec6SMatthias Ringwald return hf && ag; 1923deb3ec6SMatthias Ringwald } 1933deb3ec6SMatthias Ringwald 194a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){ 195a0ffb263SMatthias Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_THREE_WAY_CALLING); 196aeb0f0feSMatthias Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_THREE_WAY_CALLING); 1973deb3ec6SMatthias Ringwald return hf && ag; 1983deb3ec6SMatthias Ringwald } 1993deb3ec6SMatthias Ringwald 200a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){ 201a0ffb263SMatthias Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_HF_INDICATORS); 202aeb0f0feSMatthias Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_HF_INDICATORS); 2033deb3ec6SMatthias Ringwald return hf && ag; 2043deb3ec6SMatthias Ringwald } 2053deb3ec6SMatthias Ringwald 20676cc1527SMatthias Ringwald /* unsolicited responses */ 207aa4dd815SMatthias Ringwald 208f197e761SMatthias Ringwald static int hfp_ag_send_change_in_band_ring_tone_setting_cmd(hfp_connection_t * hfp_connection){ 2098d860c8fSMatthias Ringwald char buffer[40]; 210ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: %d\r\n", 211f197e761SMatthias Ringwald HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone(hfp_connection)); 212ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 213f197e761SMatthias Ringwald return send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 2143deb3ec6SMatthias Ringwald } 2153deb3ec6SMatthias Ringwald 2163deb3ec6SMatthias Ringwald static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){ 2173deb3ec6SMatthias Ringwald char buffer[40]; 218ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n\r\nOK\r\n", 219aeb0f0feSMatthias Ringwald HFP_SUPPORTED_FEATURES, hfp_ag_supported_features); 220ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 2213deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2223deb3ec6SMatthias Ringwald } 2233deb3ec6SMatthias Ringwald 224485ac19eSMilanka Ringwald static int hfp_ag_send_ok(uint16_t cid){ 2253deb3ec6SMatthias Ringwald char buffer[10]; 226ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\nOK\r\n"); 227ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 2283deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2293deb3ec6SMatthias Ringwald } 2303deb3ec6SMatthias Ringwald 231485ac19eSMilanka Ringwald static int hfp_ag_send_ring(uint16_t cid){ 232aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, (char *) "\r\nRING\r\n"); 233aa4dd815SMatthias Ringwald } 234aa4dd815SMatthias Ringwald 235245852b7SMilanka Ringwald static int hfp_ag_send_no_carrier(uint16_t cid){ 236245852b7SMilanka Ringwald char buffer[15]; 237245852b7SMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\nNO CARRIER\r\n"); 238245852b7SMilanka Ringwald buffer[sizeof(buffer) - 1] = 0; 239245852b7SMilanka Ringwald return send_str_over_rfcomm(cid, buffer); 240245852b7SMilanka Ringwald } 241245852b7SMilanka Ringwald 242aa4dd815SMatthias Ringwald static int hfp_ag_send_clip(uint16_t cid){ 243aa4dd815SMatthias Ringwald char buffer[50]; 244ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP, 245ff7d6aeaSMatthias Ringwald hfp_gsm_clip_number(), hfp_gsm_clip_type()); 246ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 247aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 248aa4dd815SMatthias Ringwald } 249aa4dd815SMatthias Ringwald 250ce263fc8SMatthias Ringwald static int hfp_send_subscriber_number_cmd(uint16_t cid, uint8_t type, const char * number){ 251ce263fc8SMatthias Ringwald char buffer[50]; 252ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: ,\"%s\",%u, , \r\n", 253ff7d6aeaSMatthias Ringwald HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type); 254ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 255ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 256ce263fc8SMatthias Ringwald } 257ce263fc8SMatthias Ringwald 258c1797c7dSMatthias Ringwald static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){ 259aa4dd815SMatthias Ringwald char buffer[50]; 260ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: %s\r\n", 261ff7d6aeaSMatthias Ringwald HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number()); 262ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 263aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 264aa4dd815SMatthias Ringwald } 265aa4dd815SMatthias Ringwald 266aa4dd815SMatthias Ringwald static int hfp_ag_send_call_waiting_notification(uint16_t cid){ 267aa4dd815SMatthias Ringwald char buffer[50]; 268ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: \"%s\",%u\r\n", 269ff7d6aeaSMatthias Ringwald HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(), 270ff7d6aeaSMatthias Ringwald hfp_gsm_clip_type()); 271ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 272aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 273aa4dd815SMatthias Ringwald } 274aa4dd815SMatthias Ringwald 275485ac19eSMilanka Ringwald static int hfp_ag_send_error(uint16_t cid){ 2763deb3ec6SMatthias Ringwald char buffer[10]; 277ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\nERROR\r\n"); 278ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 2793deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2803deb3ec6SMatthias Ringwald } 2813deb3ec6SMatthias Ringwald 282485ac19eSMilanka Ringwald static int hfp_ag_send_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){ 2833deb3ec6SMatthias Ringwald char buffer[20]; 284ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s=%d\r\n", 285ff7d6aeaSMatthias Ringwald HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error); 286ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 2873deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2883deb3ec6SMatthias Ringwald } 2893deb3ec6SMatthias Ringwald 290ad902e3dSMatthias Ringwald // get size for indicator string 291a0ffb263SMatthias Ringwald static int hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection, int i){ 292ad902e3dSMatthias Ringwald // template: ("$NAME",($MIN,$MAX)) 29345718b6fSMatthias Ringwald return 8 + (int) strlen(hfp_ag_get_ag_indicators(hfp_connection)[i].name) 294a0ffb263SMatthias Ringwald + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range) 295a0ffb263SMatthias Ringwald + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range); 296ad902e3dSMatthias Ringwald } 297ad902e3dSMatthias Ringwald 298ad902e3dSMatthias Ringwald // store indicator 2991167ff83SMatthias Ringwald static void hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection, int i, uint8_t * buffer, uint16_t buffer_size){ 3001167ff83SMatthias Ringwald snprintf((char *)buffer, buffer_size, "(\"%s\",(%d,%d)),", 301a0ffb263SMatthias Ringwald hfp_ag_get_ag_indicators(hfp_connection)[i].name, 302a0ffb263SMatthias Ringwald hfp_ag_get_ag_indicators(hfp_connection)[i].min_range, 303a0ffb263SMatthias Ringwald hfp_ag_get_ag_indicators(hfp_connection)[i].max_range); 3041167ff83SMatthias Ringwald ((char *)buffer)[buffer_size - 1] = 0; 3053deb3ec6SMatthias Ringwald } 306ad902e3dSMatthias Ringwald 307ad902e3dSMatthias Ringwald // structure: header [indicator [comma indicator]] footer 308a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_num_segments(hfp_connection_t * hfp_connection){ 309a0ffb263SMatthias Ringwald int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 310ad902e3dSMatthias Ringwald if (!num_indicators) return 2; 311c1ab6cc1SMatthias Ringwald return 3 + ((num_indicators-1) * 2); 3123deb3ec6SMatthias Ringwald } 313ad902e3dSMatthias Ringwald 314ad902e3dSMatthias Ringwald // get size of individual segment for hfp_ag_retrieve_indicators_cmd 315a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_get_segment_len(hfp_connection_t * hfp_connection, int index){ 316ad902e3dSMatthias Ringwald if (index == 0) { 317ab2445a0SMatthias Ringwald return (uint16_t) strlen(HFP_INDICATOR) + 3; // "\n\r%s:"" 318ad902e3dSMatthias Ringwald } 319ad902e3dSMatthias Ringwald index--; 320a0ffb263SMatthias Ringwald int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 321ad902e3dSMatthias Ringwald int indicator_index = index >> 1; 322ad902e3dSMatthias Ringwald if ((index & 1) == 0){ 323a0ffb263SMatthias Ringwald return hfp_ag_indicators_string_size(hfp_connection, indicator_index); 324ad902e3dSMatthias Ringwald } 325c1ab6cc1SMatthias Ringwald if (indicator_index == (num_indicators - 1)){ 326ad902e3dSMatthias Ringwald return 8; // "\r\n\r\nOK\r\n" 327ad902e3dSMatthias Ringwald } 328ad902e3dSMatthias Ringwald return 1; // comma 329ad902e3dSMatthias Ringwald } 330ad902e3dSMatthias Ringwald 3311167ff83SMatthias Ringwald static void hfp_ag_indicators_cmd_generator_store_segment(hfp_connection_t * hfp_connection, int index, uint8_t * buffer, uint16_t buffer_size){ 332ad902e3dSMatthias Ringwald if (index == 0){ 333ad902e3dSMatthias Ringwald *buffer++ = '\r'; 33474386ee0SMatthias Ringwald *buffer++ = '\n'; 335ab2445a0SMatthias Ringwald int len = (uint16_t) strlen(HFP_INDICATOR); 3366535961aSMatthias Ringwald (void)memcpy(buffer, HFP_INDICATOR, len); 337ad902e3dSMatthias Ringwald buffer += len; 338ad902e3dSMatthias Ringwald *buffer++ = ':'; 339ad902e3dSMatthias Ringwald return; 340ad902e3dSMatthias Ringwald } 341ad902e3dSMatthias Ringwald index--; 342a0ffb263SMatthias Ringwald int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 343ad902e3dSMatthias Ringwald int indicator_index = index >> 1; 344ad902e3dSMatthias Ringwald if ((index & 1) == 0){ 3451167ff83SMatthias Ringwald hfp_ag_indicators_string_store(hfp_connection, indicator_index, buffer, buffer_size); 346ad902e3dSMatthias Ringwald return; 347ad902e3dSMatthias Ringwald } 348c1ab6cc1SMatthias Ringwald if (indicator_index == (num_indicators-1)){ 3496535961aSMatthias Ringwald (void)memcpy(buffer, "\r\n\r\nOK\r\n", 8); 350ad902e3dSMatthias Ringwald return; 351ad902e3dSMatthias Ringwald } 352ad902e3dSMatthias Ringwald *buffer = ','; 3533deb3ec6SMatthias Ringwald } 3543deb3ec6SMatthias Ringwald 355aeb0f0feSMatthias Ringwald static int hfp_ag_generic_indicators_join(char * buffer, int buffer_size){ 356c1ab6cc1SMatthias Ringwald if (buffer_size < (hfp_ag_indicators_nr * 3)) return 0; 3573deb3ec6SMatthias Ringwald int i; 3583deb3ec6SMatthias Ringwald int offset = 0; 359aeb0f0feSMatthias Ringwald for (i = 0; i < (hfp_ag_generic_status_indicators_nr - 1); i++) { 360aeb0f0feSMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_generic_status_indicators[i].uuid); 3613deb3ec6SMatthias Ringwald } 362aeb0f0feSMatthias Ringwald if (i < hfp_ag_generic_status_indicators_nr){ 363aeb0f0feSMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_generic_status_indicators[i].uuid); 3643deb3ec6SMatthias Ringwald } 3653deb3ec6SMatthias Ringwald return offset; 3663deb3ec6SMatthias Ringwald } 3673deb3ec6SMatthias Ringwald 368aeb0f0feSMatthias Ringwald static int hfp_ag_generic_indicators_initial_status_join(char * buffer, int buffer_size){ 369aeb0f0feSMatthias Ringwald if (buffer_size < (hfp_ag_generic_status_indicators_nr * 3)) return 0; 3703deb3ec6SMatthias Ringwald int i; 3713deb3ec6SMatthias Ringwald int offset = 0; 372aeb0f0feSMatthias Ringwald for (i = 0; i < hfp_ag_generic_status_indicators_nr; i++) { 373aeb0f0feSMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "\r\n%s:%d,%d\r\n", HFP_GENERIC_STATUS_INDICATOR, hfp_ag_generic_status_indicators[i].uuid, hfp_ag_generic_status_indicators[i].state); 3743deb3ec6SMatthias Ringwald } 3753deb3ec6SMatthias Ringwald return offset; 3763deb3ec6SMatthias Ringwald } 3773deb3ec6SMatthias Ringwald 3783deb3ec6SMatthias Ringwald static int hfp_ag_indicators_status_join(char * buffer, int buffer_size){ 379c1ab6cc1SMatthias Ringwald if (buffer_size < (hfp_ag_indicators_nr * 3)) return 0; 3803deb3ec6SMatthias Ringwald int i; 3813deb3ec6SMatthias Ringwald int offset = 0; 382c1ab6cc1SMatthias Ringwald for (i = 0; i < (hfp_ag_indicators_nr-1); i++) { 3833deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_indicators[i].status); 3843deb3ec6SMatthias Ringwald } 3853deb3ec6SMatthias Ringwald if (i<hfp_ag_indicators_nr){ 3863deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_indicators[i].status); 3873deb3ec6SMatthias Ringwald } 3883deb3ec6SMatthias Ringwald return offset; 3893deb3ec6SMatthias Ringwald } 3903deb3ec6SMatthias Ringwald 3913deb3ec6SMatthias Ringwald static int hfp_ag_call_services_join(char * buffer, int buffer_size){ 392c1ab6cc1SMatthias Ringwald if (buffer_size < (hfp_ag_call_hold_services_nr * 3)) return 0; 3933deb3ec6SMatthias Ringwald int i; 3943deb3ec6SMatthias Ringwald int offset = snprintf(buffer, buffer_size, "("); 395c1ab6cc1SMatthias Ringwald for (i = 0; i < (hfp_ag_call_hold_services_nr-1); i++) { 3963deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%s,", hfp_ag_call_hold_services[i]); 3973deb3ec6SMatthias Ringwald } 3983deb3ec6SMatthias Ringwald if (i<hfp_ag_call_hold_services_nr){ 3993deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%s)", hfp_ag_call_hold_services[i]); 4003deb3ec6SMatthias Ringwald } 4013deb3ec6SMatthias Ringwald return offset; 4023deb3ec6SMatthias Ringwald } 4033deb3ec6SMatthias Ringwald 404485ac19eSMilanka Ringwald static int hfp_ag_send_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, 405ad902e3dSMatthias Ringwald int start_segment, int num_segments, 406a0ffb263SMatthias Ringwald int (*get_segment_len)(hfp_connection_t * hfp_connection, int segment), 4071167ff83SMatthias Ringwald void (*store_segment) (hfp_connection_t * hfp_connection, int segment, uint8_t * buffer, uint16_t buffer_size)){ 4083deb3ec6SMatthias Ringwald 409ad902e3dSMatthias Ringwald // assumes: can send now == true 410ad902e3dSMatthias Ringwald // assumes: num segments > 0 411aba39421SMatthias Ringwald // assumes: individual segments are smaller than MTU 412ad902e3dSMatthias Ringwald rfcomm_reserve_packet_buffer(); 413ad902e3dSMatthias Ringwald int mtu = rfcomm_get_max_frame_size(cid); 414ad902e3dSMatthias Ringwald uint8_t * data = rfcomm_get_outgoing_buffer(); 415ad902e3dSMatthias Ringwald int offset = 0; 416ad902e3dSMatthias Ringwald int segment = start_segment; 417ad902e3dSMatthias Ringwald while (segment < num_segments){ 418a0ffb263SMatthias Ringwald int segment_len = get_segment_len(hfp_connection, segment); 4191167ff83SMatthias Ringwald if ((offset + segment_len + 1) > mtu) break; 4201167ff83SMatthias Ringwald // append segment. As it appends a '\0', we provide a buffer one byte larger 4211167ff83SMatthias Ringwald store_segment(hfp_connection, segment, data+offset, segment_len + 1); 422ad902e3dSMatthias Ringwald offset += segment_len; 423ad902e3dSMatthias Ringwald segment++; 424ad902e3dSMatthias Ringwald } 425ad902e3dSMatthias Ringwald rfcomm_send_prepared(cid, offset); 426f20dd2aeSBjoern Hartmann #ifdef ENABLE_HFP_AT_MESSAGES 427f20dd2aeSBjoern Hartmann hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_SENT, (char *) data); 428f20dd2aeSBjoern Hartmann #endif 429ad902e3dSMatthias Ringwald return segment; 430ad902e3dSMatthias Ringwald } 4313deb3ec6SMatthias Ringwald 432ad902e3dSMatthias Ringwald // returns next segment to store 433485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, int start_segment){ 434a0ffb263SMatthias Ringwald int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection); 435485ac19eSMilanka Ringwald return hfp_ag_send_cmd_via_generator(cid, hfp_connection, start_segment, num_segments, 436894d499cSMatthias Ringwald hfp_ag_indicators_cmd_generator_get_segment_len, 437894d499cSMatthias Ringwald hfp_ag_indicators_cmd_generator_store_segment); 4383deb3ec6SMatthias Ringwald } 4393deb3ec6SMatthias Ringwald 440485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_status_cmd(uint16_t cid){ 4413deb3ec6SMatthias Ringwald char buffer[40]; 44289425bfcSMilanka Ringwald const int size = sizeof(buffer); 44389425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "\r\n%s:", HFP_INDICATOR); 44489425bfcSMilanka Ringwald offset += hfp_ag_indicators_status_join(buffer+offset, size-offset-9); 44589425bfcSMilanka Ringwald offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n"); 4463deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4473deb3ec6SMatthias Ringwald } 4483deb3ec6SMatthias Ringwald 449485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_can_hold_call_cmd(uint16_t cid){ 450ad902e3dSMatthias Ringwald char buffer[40]; 45189425bfcSMilanka Ringwald const int size = sizeof(buffer); 45289425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "\r\n%s:", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES); 45389425bfcSMilanka Ringwald offset += hfp_ag_call_services_join(buffer+offset, size-offset-9); 45489425bfcSMilanka Ringwald offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n"); 4553deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4563deb3ec6SMatthias Ringwald } 4573deb3ec6SMatthias Ringwald 4583deb3ec6SMatthias Ringwald 459485ac19eSMilanka Ringwald static int hfp_ag_send_list_supported_generic_status_indicators_cmd(uint16_t cid){ 460485ac19eSMilanka Ringwald return hfp_ag_send_ok(cid); 4613deb3ec6SMatthias Ringwald } 4623deb3ec6SMatthias Ringwald 463485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(uint16_t cid){ 4643deb3ec6SMatthias Ringwald char buffer[40]; 46589425bfcSMilanka Ringwald const int size = sizeof(buffer); 46689425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "\r\n%s:(", HFP_GENERIC_STATUS_INDICATOR); 467aeb0f0feSMatthias Ringwald offset += hfp_ag_generic_indicators_join(buffer + offset, size - offset - 10); 46889425bfcSMilanka Ringwald offset += snprintf(buffer+offset, size-offset, ")\r\n\r\nOK\r\n"); 4693deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4703deb3ec6SMatthias Ringwald } 4713deb3ec6SMatthias Ringwald 472485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid){ 4733deb3ec6SMatthias Ringwald char buffer[40]; 474aeb0f0feSMatthias Ringwald int offset = hfp_ag_generic_indicators_initial_status_join(buffer, sizeof(buffer) - 7); 4751cc1d9e9SMilanka Ringwald snprintf(buffer+offset, sizeof(buffer)-offset, "\r\nOK\r\n"); 4763deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4773deb3ec6SMatthias Ringwald } 4783deb3ec6SMatthias Ringwald 479485ac19eSMilanka Ringwald static int hfp_ag_send_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){ 4803deb3ec6SMatthias Ringwald char buffer[20]; 481ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s:%d,%d\r\n", 482ff7d6aeaSMatthias Ringwald HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index, 483ff7d6aeaSMatthias Ringwald indicator->status); 484ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 4853deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4863deb3ec6SMatthias Ringwald } 4873deb3ec6SMatthias Ringwald 488485ac19eSMilanka Ringwald static int hfp_ag_send_report_network_operator_name_cmd(uint16_t cid, hfp_network_opearator_t op){ 4895e71456cSMilanka Ringwald char buffer[41]; 4903deb3ec6SMatthias Ringwald if (strlen(op.name) == 0){ 49189425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s:%d,,\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode); 4923deb3ec6SMatthias Ringwald } else { 49389425bfcSMilanka Ringwald int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:%d,%d,", HFP_QUERY_OPERATOR_SELECTION, op.mode, op.format); 4945e71456cSMilanka Ringwald offset += snprintf(buffer+offset, 16, "%s", op.name); 49589425bfcSMilanka Ringwald snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n"); 4963deb3ec6SMatthias Ringwald } 4973deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4983deb3ec6SMatthias Ringwald } 4993deb3ec6SMatthias Ringwald 500a04aecb1SMilanka Ringwald static inline int hfp_ag_send_cmd_with_space_and_int(uint16_t cid, const char * cmd, uint8_t value){ 501a04aecb1SMilanka Ringwald char buffer[30]; 502a04aecb1SMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: %d\r\n", cmd, value); 503a04aecb1SMilanka Ringwald return send_str_over_rfcomm(cid, buffer); 504a04aecb1SMilanka Ringwald } 505a04aecb1SMilanka Ringwald 506a04aecb1SMilanka Ringwald 5071cc1d9e9SMilanka Ringwald static inline int hfp_ag_send_cmd_with_int(uint16_t cid, const char * cmd, uint8_t value){ 5081cc1d9e9SMilanka Ringwald char buffer[30]; 50989425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n", cmd, value); 5101cc1d9e9SMilanka Ringwald return send_str_over_rfcomm(cid, buffer); 5111cc1d9e9SMilanka Ringwald } 5123deb3ec6SMatthias Ringwald 513485ac19eSMilanka Ringwald static int hfp_ag_send_suggest_codec_cmd(uint16_t cid, uint8_t codec){ 5141cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec); 5153deb3ec6SMatthias Ringwald } 5163deb3ec6SMatthias Ringwald 517485ac19eSMilanka Ringwald static int hfp_ag_send_set_speaker_gain_cmd(uint16_t cid, uint8_t gain){ 5181cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain); 519aa4dd815SMatthias Ringwald } 520aa4dd815SMatthias Ringwald 521485ac19eSMilanka Ringwald static int hfp_ag_send_set_microphone_gain_cmd(uint16_t cid, uint8_t gain){ 5221cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain); 523aa4dd815SMatthias Ringwald } 524aa4dd815SMatthias Ringwald 525485ac19eSMilanka Ringwald static int hfp_ag_send_set_response_and_hold(uint16_t cid, int state){ 52666191aa0SMilanka Ringwald return hfp_ag_send_cmd_with_space_and_int(cid, HFP_RESPONSE_AND_HOLD, state); 527ce263fc8SMatthias Ringwald } 528ce263fc8SMatthias Ringwald 529cf75be85SMilanka Ringwald static int hfp_ag_send_enhanced_voice_recognition_state_cmd(hfp_connection_t * hfp_connection){ 530cf75be85SMilanka Ringwald char buffer[30]; 531fcf4ede6SMilanka Ringwald uint8_t evra_enabled = hfp_connection->enhanced_voice_recognition_enabled ? 1 : 0; 532fcf4ede6SMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, evra_enabled, hfp_connection->ag_vra_state); 533cf75be85SMilanka Ringwald return send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 534cf75be85SMilanka Ringwald } 535cf75be85SMilanka Ringwald 536d1b50446SMilanka Ringwald static int hfp_ag_send_voice_recognition_cmd(hfp_connection_t * hfp_connection, uint8_t activate_voice_recognition){ 53745796ff1SMilanka Ringwald char buffer[30]; 538d1b50446SMilanka Ringwald if (hfp_connection->enhanced_voice_recognition_enabled){ 539d1b50446SMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition, hfp_connection->ag_vra_state); 540d1b50446SMilanka Ringwald } else { 541d1b50446SMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: %d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition); 542d1b50446SMilanka Ringwald } 543d1b50446SMilanka Ringwald return send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 54445796ff1SMilanka Ringwald } 54545796ff1SMilanka Ringwald 54645796ff1SMilanka Ringwald static int hfp_ag_send_enhanced_voice_recognition_msg_cmd(hfp_connection_t * hfp_connection){ 547a2e701c0SMilanka Ringwald char buffer[HFP_VR_TEXT_HEADER_SIZE + HFP_MAX_VR_TEXT_SIZE]; 54845796ff1SMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: 1,%d,%X,%d,%d,\"%s\"\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, 54945796ff1SMilanka Ringwald hfp_connection->ag_vra_state, 55045796ff1SMilanka Ringwald hfp_connection->ag_msg.text_id, 55145796ff1SMilanka Ringwald hfp_connection->ag_msg.text_type, 55245796ff1SMilanka Ringwald hfp_connection->ag_msg.text_operation, 55345796ff1SMilanka Ringwald hfp_connection->ag_msg.text); 55445796ff1SMilanka Ringwald return send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 55545796ff1SMilanka Ringwald } 55645796ff1SMilanka Ringwald 557a0ffb263SMatthias Ringwald static uint8_t hfp_ag_suggest_codec(hfp_connection_t *hfp_connection){ 558bc1b1537SMilanka Ringwald if (hfp_connection->sco_for_msbc_failed) return HFP_CODEC_CVSD; 559bc1b1537SMilanka Ringwald 5602085619dSMatthias Ringwald #ifdef ENABLE_HFP_SUPER_WIDE_BAND_SPEECH 5612085619dSMatthias Ringwald if (hfp_supports_codec(HFP_CODEC_LC3_SWB, hfp_ag_codecs_nr, hfp_ag_codecs)){ 5622085619dSMatthias Ringwald if (hfp_supports_codec(HFP_CODEC_LC3_SWB, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){ 5632085619dSMatthias Ringwald return HFP_CODEC_LC3_SWB; 5642085619dSMatthias Ringwald } 5652085619dSMatthias Ringwald } 5662085619dSMatthias Ringwald #endif 5672085619dSMatthias Ringwald 5682085619dSMatthias Ringwald #ifdef ENABLE_HFP_WIDE_BAND_SPEECH 569aeb0f0feSMatthias Ringwald if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_ag_codecs_nr, hfp_ag_codecs)){ 5706a7f44bdSMilanka Ringwald if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){ 571df327ff3SMilanka Ringwald return HFP_CODEC_MSBC; 5723deb3ec6SMatthias Ringwald } 5733deb3ec6SMatthias Ringwald } 5742085619dSMatthias Ringwald #endif 5752085619dSMatthias Ringwald 576df327ff3SMilanka Ringwald return HFP_CODEC_CVSD; 5773deb3ec6SMatthias Ringwald } 5783deb3ec6SMatthias Ringwald 57976cc1527SMatthias Ringwald /* state machines */ 58076cc1527SMatthias Ringwald 581afac2a04SMilanka Ringwald static uint8_t hfp_ag_esco_s4_supported(hfp_connection_t * hfp_connection){ 582aeb0f0feSMatthias Ringwald return (hfp_connection->remote_supported_features & (1<<HFP_HFSF_ESCO_S4)) && (hfp_ag_supported_features & (1 << HFP_AGSF_ESCO_S4)); 5837522e673SMatthias Ringwald } 5847522e673SMatthias Ringwald 585a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){ 586aa4dd815SMatthias Ringwald /* events ( == commands): 587aa4dd815SMatthias Ringwald HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs 588aa4dd815SMatthias Ringwald HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 589aa4dd815SMatthias Ringwald hf_trigger_codec_connection_setup == received BCC 590aa4dd815SMatthias Ringwald ag_trigger_codec_connection_setup == received from AG to send BCS 591aa4dd815SMatthias Ringwald HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS 592aa4dd815SMatthias Ringwald */ 593a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 5943e3b9207SMilanka Ringwald case HFP_CODECS_EXCHANGED: 5953e3b9207SMilanka Ringwald if (hfp_connection->command == HFP_CMD_AVAILABLE_CODECS){ 5963e3b9207SMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 5973e3b9207SMilanka Ringwald return 1; 5983e3b9207SMilanka Ringwald } 5993e3b9207SMilanka Ringwald break; 6003e3b9207SMilanka Ringwald 601aa4dd815SMatthias Ringwald case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 602aa4dd815SMatthias Ringwald case HFP_CODECS_AG_RESEND_COMMON_CODEC: 603a814da6aSMatthias Ringwald hfp_connection->ag_send_common_codec = true; 604aa4dd815SMatthias Ringwald break; 605aa4dd815SMatthias Ringwald default: 606aa4dd815SMatthias Ringwald break; 607aa4dd815SMatthias Ringwald } 608aa4dd815SMatthias Ringwald 609a814da6aSMatthias Ringwald if (hfp_connection->ag_send_common_codec){ 610a814da6aSMatthias Ringwald hfp_connection->ag_send_common_codec = false; 611a814da6aSMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_AG_SENT_COMMON_CODEC; 612a814da6aSMatthias Ringwald hfp_connection->suggested_codec = hfp_ag_suggest_codec(hfp_connection); 613a814da6aSMatthias Ringwald hfp_ag_send_suggest_codec_cmd(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec); 614a814da6aSMatthias Ringwald return 1; 615a814da6aSMatthias Ringwald } 616a814da6aSMatthias Ringwald 617a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 618aa4dd815SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 619a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){ 620a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_LIST; 621485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 622aa4dd815SMatthias Ringwald return 1; 623aa4dd815SMatthias Ringwald } 624aa4dd815SMatthias Ringwald 625a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 626aa4dd815SMatthias Ringwald case HFP_CODECS_AG_SENT_COMMON_CODEC: 627a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_AG_RESEND_COMMON_CODEC; 628aa4dd815SMatthias Ringwald break; 629aa4dd815SMatthias Ringwald default: 630aa4dd815SMatthias Ringwald break; 631aa4dd815SMatthias Ringwald } 632485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 633aa4dd815SMatthias Ringwald return 1; 634aa4dd815SMatthias Ringwald 635aa4dd815SMatthias Ringwald case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 636a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 63740e8d6c5SMatthias Ringwald hfp_connection->establish_audio_connection = 1; 6382561704cSMatthias Ringwald hfp_connection->sco_for_msbc_failed = 0; 639485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 640aa4dd815SMatthias Ringwald return 1; 641aa4dd815SMatthias Ringwald 642aa4dd815SMatthias Ringwald case HFP_CMD_HF_CONFIRMED_CODEC: 643a0ffb263SMatthias Ringwald if (hfp_connection->codec_confirmed != hfp_connection->suggested_codec){ 644a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_ERROR; 645485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 646aa4dd815SMatthias Ringwald return 1; 647aa4dd815SMatthias Ringwald } 648a0ffb263SMatthias Ringwald hfp_connection->negotiated_codec = hfp_connection->codec_confirmed; 649a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 650c1ab6cc1SMatthias Ringwald log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD"); 651485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 6527522e673SMatthias Ringwald // now, pick link settings 653afac2a04SMilanka Ringwald hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection)); 6540327df7dSMatthias Ringwald // configure SBC coded if needed 6550327df7dSMatthias Ringwald hfp_prepare_for_sco(hfp_connection); 656aa4dd815SMatthias Ringwald return 1; 657aa4dd815SMatthias Ringwald default: 658aa4dd815SMatthias Ringwald break; 659aa4dd815SMatthias Ringwald } 660aa4dd815SMatthias Ringwald return 0; 661aa4dd815SMatthias Ringwald } 662aa4dd815SMatthias Ringwald 663a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){ 664a0ffb263SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 6657095467fSMatthias Ringwald hfp_emit_slc_connection_event(hfp_connection->local_role, 0, hfp_connection->acl_handle, hfp_connection->remote_addr); 666aa4dd815SMatthias Ringwald 66740a8ee13SMatthias Ringwald // in-band ring tone is active if supported 66840a8ee13SMatthias Ringwald hfp_connection->ag_in_band_ring_tone_active = has_in_band_ring_tone(); 66940a8ee13SMatthias Ringwald 67080702f97SMatthias Ringwald // HFP 4.35: "When [...] a new Service Level Connection is established all indicators are activated by default." 67180702f97SMatthias Ringwald uint16_t i; 67280702f97SMatthias Ringwald for (i=0;i<hfp_connection->ag_indicators_nr;i++){ 67380702f97SMatthias Ringwald hfp_connection->ag_indicators[i].enabled = 1; 67480702f97SMatthias Ringwald } 67580702f97SMatthias Ringwald 676a0ffb263SMatthias Ringwald // if active call exist, set per-hfp_connection state active, too (when audio is on) 677d0c20769SMatthias Ringwald if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 678a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE; 679aa4dd815SMatthias Ringwald } 680ce263fc8SMatthias Ringwald // if AG is ringing, also start ringing on the HF 681c4f8fc1cSMilanka Ringwald if (hfp_gsm_call_status() == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS){ 682c4f8fc1cSMilanka Ringwald switch (hfp_gsm_callsetup_status()){ 683c4f8fc1cSMilanka Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 6842ff1d7d7SMilanka Ringwald hfp_ag_hf_start_ringing_incoming(hfp_connection); 685c4f8fc1cSMilanka Ringwald break; 686fcd59412SMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 687fcd59412SMilanka Ringwald hfp_ag_hf_start_ringing_outgoing(hfp_connection); 688fcd59412SMilanka Ringwald break; 689c4f8fc1cSMilanka Ringwald default: 690c4f8fc1cSMilanka Ringwald break; 691c4f8fc1cSMilanka Ringwald } 692ce263fc8SMatthias Ringwald } 693aa4dd815SMatthias Ringwald } 6943deb3ec6SMatthias Ringwald 695a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){ 696c79b4cabSMatthias Ringwald // log_info("hfp_ag_run_for_context_service_level_connection state %u, command %u", hfp_connection->state, hfp_connection->command); 697a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 698c04cf7ffSMilanka Ringwald int sent = 0; 699a0ffb263SMatthias Ringwald switch(hfp_connection->command){ 7003deb3ec6SMatthias Ringwald case HFP_CMD_SUPPORTED_FEATURES: 701a0ffb263SMatthias Ringwald switch(hfp_connection->state){ 7023deb3ec6SMatthias Ringwald case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 703aa4dd815SMatthias Ringwald case HFP_EXCHANGE_SUPPORTED_FEATURES: 704aeb0f0feSMatthias Ringwald hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_ag_codecs, &hfp_ag_codecs_nr); 705a0ffb263SMatthias Ringwald if (has_codec_negotiation_feature(hfp_connection)){ 706a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS; 707aa4dd815SMatthias Ringwald } else { 708a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 709aa4dd815SMatthias Ringwald } 710a0ffb263SMatthias Ringwald hfp_ag_exchange_supported_features_cmd(hfp_connection->rfcomm_cid); 711aa4dd815SMatthias Ringwald return 1; 7123deb3ec6SMatthias Ringwald default: 7133deb3ec6SMatthias Ringwald break; 7143deb3ec6SMatthias Ringwald } 7153deb3ec6SMatthias Ringwald break; 7163deb3ec6SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 717c04cf7ffSMilanka Ringwald sent = codecs_exchange_state_machine(hfp_connection); 718a0ffb263SMatthias Ringwald if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){ 719a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 7203deb3ec6SMatthias Ringwald } 721c04cf7ffSMilanka Ringwald return sent; 722aa4dd815SMatthias Ringwald 723aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 724a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_W4_RETRIEVE_INDICATORS) { 725473ac565SMatthias Ringwald // HF requested AG Indicators and we did expect it 72684a0c24eSMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0; 727473ac565SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 728473ac565SMatthias Ringwald // continue below in state switch 729ad902e3dSMatthias Ringwald } 730ad902e3dSMatthias Ringwald break; 731aa4dd815SMatthias Ringwald 732aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 733a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break; 734a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE; 735485ac19eSMilanka Ringwald hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid); 736aa4dd815SMatthias Ringwald return 1; 737aa4dd815SMatthias Ringwald 7383deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 739a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break; 740a0ffb263SMatthias Ringwald if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 741a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL; 742*880e8fc6SMatthias Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 743a0ffb263SMatthias Ringwald } else if (has_hf_indicators_feature(hfp_connection)){ 744a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 745*880e8fc6SMatthias Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 746aa4dd815SMatthias Ringwald } else { 747*880e8fc6SMatthias Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 748a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 7493deb3ec6SMatthias Ringwald } 750aa4dd815SMatthias Ringwald return 1; 7513deb3ec6SMatthias Ringwald 752aa4dd815SMatthias Ringwald case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES: 753a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break; 754a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 755a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 756aa4dd815SMatthias Ringwald } 757485ac19eSMilanka Ringwald hfp_ag_send_retrieve_can_hold_call_cmd(hfp_connection->rfcomm_cid); 758a0ffb263SMatthias Ringwald if (!has_hf_indicators_feature(hfp_connection)){ 759a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 760ce263fc8SMatthias Ringwald } 761aa4dd815SMatthias Ringwald return 1; 762aa4dd815SMatthias Ringwald 763aa4dd815SMatthias Ringwald case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 764a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break; 765a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS; 766485ac19eSMilanka Ringwald hfp_ag_send_list_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 767aa4dd815SMatthias Ringwald return 1; 768aa4dd815SMatthias Ringwald 769aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 770a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break; 771a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 772485ac19eSMilanka Ringwald hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 773aa4dd815SMatthias Ringwald return 1; 774aa4dd815SMatthias Ringwald 775aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 776a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break; 777485ac19eSMilanka Ringwald hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 778a7666b02SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 779aa4dd815SMatthias Ringwald return 1; 7803deb3ec6SMatthias Ringwald default: 7813deb3ec6SMatthias Ringwald break; 7823deb3ec6SMatthias Ringwald } 783473ac565SMatthias Ringwald 784473ac565SMatthias Ringwald switch (hfp_connection->state){ 785473ac565SMatthias Ringwald case HFP_RETRIEVE_INDICATORS: { 786485ac19eSMilanka Ringwald int next_segment = hfp_ag_send_retrieve_indicators_cmd_via_generator(hfp_connection->rfcomm_cid, hfp_connection, hfp_connection->send_ag_indicators_segment); 787473ac565SMatthias Ringwald int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection); 788473ac565SMatthias Ringwald log_info("HFP_CMD_RETRIEVE_AG_INDICATORS next segment %u, num_segments %u", next_segment, num_segments); 789473ac565SMatthias Ringwald if (next_segment < num_segments){ 790473ac565SMatthias Ringwald // prepare sending of next segment 791473ac565SMatthias Ringwald hfp_connection->send_ag_indicators_segment = next_segment; 792473ac565SMatthias Ringwald log_info("HFP_CMD_RETRIEVE_AG_INDICATORS more. command %u, next seg %u", hfp_connection->command, next_segment); 793473ac565SMatthias Ringwald } else { 794473ac565SMatthias Ringwald // done, go to next state 795473ac565SMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0; 796473ac565SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS; 797473ac565SMatthias Ringwald } 798473ac565SMatthias Ringwald return 1; 799473ac565SMatthias Ringwald } 800473ac565SMatthias Ringwald default: 801473ac565SMatthias Ringwald break; 802473ac565SMatthias Ringwald } 803c04cf7ffSMilanka Ringwald return 0; 8043deb3ec6SMatthias Ringwald } 8053deb3ec6SMatthias Ringwald 8069cd26862SMilanka Ringwald static bool hfp_ag_vra_flag_supported(hfp_connection_t * hfp_connection){ 80714c148caSMilanka Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION); 80814c148caSMilanka Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION); 80914c148caSMilanka Ringwald return hf && ag; 81014c148caSMilanka Ringwald } 81114c148caSMilanka Ringwald 8129cd26862SMilanka Ringwald static bool hfp_ag_enhanced_vra_flag_supported(hfp_connection_t * hfp_connection){ 81314c148caSMilanka Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS); 81414c148caSMilanka Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS); 81514c148caSMilanka Ringwald return hf && ag; 81614c148caSMilanka Ringwald } 81714c148caSMilanka Ringwald 8189cd26862SMilanka Ringwald static bool hfp_ag_can_send_enhanced_vra_message_flag_supported(hfp_connection_t * hfp_connection){ 819f71d2aacSMilanka Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_VOICE_RECOGNITION_TEXT); 820f71d2aacSMilanka Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_TEXT); 821f71d2aacSMilanka Ringwald return hf && ag; 82208a0b01cSMilanka Ringwald } 823f71d2aacSMilanka Ringwald 824135d9718SMilanka Ringwald static bool hfp_ag_can_activate_voice_recognition(hfp_connection_t * hfp_connection){ 825f71d2aacSMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){ 826135d9718SMilanka Ringwald return false; 827d84719b1SMilanka Ringwald } 8280c738fd4SMilanka Ringwald if (hfp_connection->vra_state != HFP_VRA_VOICE_RECOGNITION_OFF){ 829135d9718SMilanka Ringwald return false; 830d84719b1SMilanka Ringwald } 83108dc7cc6SMatthias Ringwald if (hfp_connection->ag_vra_send_command){ 83208dc7cc6SMatthias Ringwald return false; 83308dc7cc6SMatthias Ringwald } 834135d9718SMilanka Ringwald return true; 835d84719b1SMilanka Ringwald } 836d84719b1SMilanka Ringwald 837135d9718SMilanka Ringwald static bool hfp_ag_voice_recognition_session_active(hfp_connection_t * hfp_connection){ 83808a0b01cSMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){ 839135d9718SMilanka Ringwald return false; 84008a0b01cSMilanka Ringwald } 8410c738fd4SMilanka Ringwald if (hfp_connection->vra_state != HFP_VRA_VOICE_RECOGNITION_ACTIVATED){ 842de9e0ea7SMilanka Ringwald if (hfp_connection->vra_state == HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO){ 843135d9718SMilanka Ringwald return true; 844de9e0ea7SMilanka Ringwald } 845135d9718SMilanka Ringwald return false; 846d84719b1SMilanka Ringwald } 847135d9718SMilanka Ringwald return true; 848d84719b1SMilanka Ringwald } 849754910caSMilanka Ringwald 8500b4debbfSMilanka Ringwald static bool hfp_ag_is_audio_connection_active(hfp_connection_t * hfp_connection){ 8510b4debbfSMilanka Ringwald switch (hfp_connection->state){ 8520b4debbfSMilanka Ringwald case HFP_W2_CONNECT_SCO: 8530b4debbfSMilanka Ringwald case HFP_W4_SCO_CONNECTED: 8540b4debbfSMilanka Ringwald case HFP_AUDIO_CONNECTION_ESTABLISHED: 8550b4debbfSMilanka Ringwald return true; 856754910caSMilanka Ringwald default: 8570b4debbfSMilanka Ringwald return false; 858754910caSMilanka Ringwald } 859754910caSMilanka Ringwald } 860754910caSMilanka Ringwald 861b95cac54SMilanka Ringwald static void hfp_ag_emit_enhanced_voice_recognition_msg_sent_event(hfp_connection_t * hfp_connection, uint8_t status){ 862b95cac54SMilanka Ringwald hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 863b95cac54SMilanka Ringwald 864b95cac54SMilanka Ringwald uint8_t event[6]; 865b95cac54SMilanka Ringwald event[0] = HCI_EVENT_HFP_META; 866b95cac54SMilanka Ringwald event[1] = sizeof(event) - 2; 867c95b5b3cSMilanka Ringwald event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_MESSAGE_SENT; 868b95cac54SMilanka Ringwald little_endian_store_16(event, 3, acl_handle); 869b95cac54SMilanka Ringwald event[5] = status; 870b95cac54SMilanka Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 871b95cac54SMilanka Ringwald } 872b95cac54SMilanka Ringwald 873347d46c8SMilanka Ringwald 874585db78dSMilanka Ringwald static void hfp_ag_emit_hf_indicator_value(hfp_connection_t * hfp_connection, uint16_t uuid, uint8_t value){ 875347d46c8SMilanka Ringwald hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 876347d46c8SMilanka Ringwald 877347d46c8SMilanka Ringwald uint8_t event[8]; 878347d46c8SMilanka Ringwald event[0] = HCI_EVENT_HFP_META; 879347d46c8SMilanka Ringwald event[1] = sizeof(event) - 2; 880347d46c8SMilanka Ringwald event[2] = HFP_SUBEVENT_HF_INDICATOR; 881347d46c8SMilanka Ringwald little_endian_store_16(event, 3, acl_handle); 882347d46c8SMilanka Ringwald little_endian_store_16(event, 5, uuid); 883347d46c8SMilanka Ringwald event[7] = value; 884347d46c8SMilanka Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 885347d46c8SMilanka Ringwald } 886347d46c8SMilanka Ringwald 887a4f85bd2SMilanka Ringwald static void hfp_ag_emit_general_simple_event(uint8_t event_subtype){ 888a4f85bd2SMilanka Ringwald if (!hfp_ag_callback) return; 889a4f85bd2SMilanka Ringwald 890a4f85bd2SMilanka Ringwald uint8_t event[5]; 891a4f85bd2SMilanka Ringwald event[0] = HCI_EVENT_HFP_META; 892a4f85bd2SMilanka Ringwald event[1] = sizeof(event) - 2; 893a4f85bd2SMilanka Ringwald event[2] = event_subtype; 894a4f85bd2SMilanka Ringwald little_endian_store_16(event, 3, HCI_CON_HANDLE_INVALID); 895a4f85bd2SMilanka Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 896a4f85bd2SMilanka Ringwald } 897a4f85bd2SMilanka Ringwald 898471dea41SMatthias Ringwald static void hfp_ag_emit_custom_command_event(hfp_connection_t * hfp_connection){ 899471dea41SMatthias Ringwald btstack_assert(sizeof(hfp_connection->line_buffer) < (255-5)); 900471dea41SMatthias Ringwald 9011a3bc516SMatthias Ringwald uint16_t line_len = (uint16_t) strlen((const char*)hfp_connection->line_buffer) + 1; 902471dea41SMatthias Ringwald uint8_t event[7 + sizeof(hfp_connection->line_buffer)]; 903471dea41SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 904471dea41SMatthias Ringwald event[1] = 5 + line_len; 905471dea41SMatthias Ringwald event[2] = HFP_SUBEVENT_CUSTOM_AT_COMMAND; 906471dea41SMatthias Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 9076d9a41f9SMatthias Ringwald little_endian_store_16(event, 5, hfp_connection->custom_at_command_id); 908471dea41SMatthias Ringwald memcpy(&event[7], hfp_connection->line_buffer, line_len); 909471dea41SMatthias Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, 7 + line_len); 910471dea41SMatthias Ringwald } 911471dea41SMatthias Ringwald 912774e3016SMatthias Ringwald // @return status 913774e3016SMatthias Ringwald static uint8_t hfp_ag_vra_state_machine_two(hfp_connection_t * hfp_connection){ 914774e3016SMatthias Ringwald uint8_t status = ERROR_CODE_SUCCESS; 915774e3016SMatthias Ringwald switch (hfp_connection->vra_state_requested){ 916774e3016SMatthias Ringwald case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 917774e3016SMatthias Ringwald if (!hfp_ag_is_audio_connection_active(hfp_connection)){ 918774e3016SMatthias Ringwald status = hfp_ag_setup_audio_connection(hfp_connection); 919774e3016SMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 920774e3016SMatthias Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 921774e3016SMatthias Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, status); 922774e3016SMatthias Ringwald break; 923774e3016SMatthias Ringwald } 924774e3016SMatthias Ringwald } 925774e3016SMatthias Ringwald hfp_connection->enhanced_voice_recognition_enabled = true; 926774e3016SMatthias Ringwald hfp_connection->vra_state = HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 927774e3016SMatthias Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 928774e3016SMatthias Ringwald hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_SUCCESS); 929774e3016SMatthias Ringwald break; 930774e3016SMatthias Ringwald 931774e3016SMatthias Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 932774e3016SMatthias Ringwald hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF; 933774e3016SMatthias Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 934774e3016SMatthias Ringwald 935774e3016SMatthias Ringwald // release audio connection only if it was opened after audio VR activated 936774e3016SMatthias Ringwald if (hfp_ag_is_audio_connection_active(hfp_connection) && !hfp_connection->ag_audio_connection_opened_before_vra){ 937774e3016SMatthias Ringwald hfp_trigger_release_audio_connection(hfp_connection); 938774e3016SMatthias Ringwald } 939774e3016SMatthias Ringwald 940774e3016SMatthias Ringwald hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS); 941774e3016SMatthias Ringwald break; 942774e3016SMatthias Ringwald 943774e3016SMatthias Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED: 944774e3016SMatthias Ringwald // open only if audio connection does not already exist 945774e3016SMatthias Ringwald if (!hfp_ag_is_audio_connection_active(hfp_connection)){ 946774e3016SMatthias Ringwald status = hfp_ag_setup_audio_connection(hfp_connection); 947774e3016SMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 948774e3016SMatthias Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 949774e3016SMatthias Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, status); 950774e3016SMatthias Ringwald break; 951774e3016SMatthias Ringwald } 952774e3016SMatthias Ringwald } 953774e3016SMatthias Ringwald 954774e3016SMatthias Ringwald hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_ACTIVATED; 955774e3016SMatthias Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 956774e3016SMatthias Ringwald hfp_connection->enhanced_voice_recognition_enabled = hfp_ag_enhanced_vra_flag_supported(hfp_connection); 95784fb9ac1SMilanka Ringwald 95884fb9ac1SMilanka Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 959774e3016SMatthias Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS); 96084fb9ac1SMilanka Ringwald } else { 96184fb9ac1SMilanka Ringwald hfp_connection->emit_vra_enabled_after_audio_established = true; 96284fb9ac1SMilanka Ringwald } 963774e3016SMatthias Ringwald break; 964774e3016SMatthias Ringwald 965774e3016SMatthias Ringwald default: 966774e3016SMatthias Ringwald break; 967774e3016SMatthias Ringwald } 968774e3016SMatthias Ringwald return status; 969774e3016SMatthias Ringwald } 970774e3016SMatthias Ringwald 97108dc7cc6SMatthias Ringwald static uint8_t hfp_ag_vra_send_command(hfp_connection_t * hfp_connection){ 97208dc7cc6SMatthias Ringwald uint8_t done = 0; 973754910caSMilanka Ringwald uint8_t status; 974754910caSMilanka Ringwald 975cf75be85SMilanka Ringwald switch (hfp_connection->vra_state_requested){ 976b95cac54SMilanka Ringwald case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_MSG: 977b95cac54SMilanka Ringwald done = hfp_ag_send_enhanced_voice_recognition_msg_cmd(hfp_connection); 978b95cac54SMilanka Ringwald if (done == 0){ 979b95cac54SMilanka Ringwald hfp_ag_emit_enhanced_voice_recognition_msg_sent_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 980b95cac54SMilanka Ringwald } else { 981b95cac54SMilanka Ringwald hfp_ag_emit_enhanced_voice_recognition_msg_sent_event(hfp_connection, ERROR_CODE_SUCCESS); 982b95cac54SMilanka Ringwald } 983b95cac54SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 984b95cac54SMilanka Ringwald return done; 985b95cac54SMilanka Ringwald 986cf75be85SMilanka Ringwald case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_STATUS: 987cf75be85SMilanka Ringwald done = hfp_ag_send_enhanced_voice_recognition_state_cmd(hfp_connection); 988cf75be85SMilanka Ringwald if (done == 0){ 989cf75be85SMilanka Ringwald hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 990cf75be85SMilanka Ringwald } else { 991cf75be85SMilanka Ringwald hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS); 992cf75be85SMilanka Ringwald } 993cf75be85SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 994b95cac54SMilanka Ringwald return done; 995b95cac54SMilanka Ringwald 9966e13e408SMatthias Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED: 9976e13e408SMatthias Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 998d1b50446SMilanka Ringwald done = hfp_ag_send_voice_recognition_cmd(hfp_connection, hfp_connection->ag_activate_voice_recognition_value); 9990c738fd4SMilanka Ringwald if (done == 0){ 10000c738fd4SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 1001553a4a56SMilanka Ringwald 1002553a4a56SMilanka Ringwald if (hfp_connection->ag_activate_voice_recognition_value == 1){ 1003553a4a56SMilanka Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, done); 1004553a4a56SMilanka Ringwald } else { 1005553a4a56SMilanka Ringwald hfp_emit_voice_recognition_disabled(hfp_connection, done); 1006553a4a56SMilanka Ringwald } 1007fd4151d1SMilanka Ringwald return 0; 1008fd4151d1SMilanka Ringwald } 100908dc7cc6SMatthias Ringwald status = hfp_ag_vra_state_machine_two(hfp_connection); 101008dc7cc6SMatthias Ringwald return (status == ERROR_CODE_SUCCESS) ? 1 : 0; 101108dc7cc6SMatthias Ringwald 10126e13e408SMatthias Ringwald default: 10136e13e408SMatthias Ringwald log_error("state %u", hfp_connection->vra_state_requested); 10146e13e408SMatthias Ringwald btstack_assert(false); 101508dc7cc6SMatthias Ringwald return 0; 1016cf75be85SMilanka Ringwald } 101708dc7cc6SMatthias Ringwald } 101808dc7cc6SMatthias Ringwald 101908dc7cc6SMatthias Ringwald static int hfp_ag_voice_recognition_state_machine(hfp_connection_t * hfp_connection){ 102008dc7cc6SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) { 102108dc7cc6SMatthias Ringwald return 0; 102208dc7cc6SMatthias Ringwald } 102308dc7cc6SMatthias Ringwald int done = 0; 102408dc7cc6SMatthias Ringwald uint8_t status; 102508dc7cc6SMatthias Ringwald 10268f8818a4SMatthias Ringwald // VRA action initiated by AG 10278f8818a4SMatthias Ringwald if (hfp_connection->ag_vra_send_command){ 10288f8818a4SMatthias Ringwald hfp_connection->ag_vra_send_command = false; 10298f8818a4SMatthias Ringwald return hfp_ag_vra_send_command(hfp_connection); 10308f8818a4SMatthias Ringwald } 10318f8818a4SMatthias Ringwald 1032e7c46708SMatthias Ringwald if (hfp_connection->ag_vra_requested_by_hf){ 1033e7c46708SMatthias Ringwald hfp_connection->ag_vra_requested_by_hf = false; 1034e7c46708SMatthias Ringwald 1035734ef2bdSMilanka Ringwald // HF initiatied voice recognition, parser extracted activation value 1036de9e0ea7SMilanka Ringwald switch (hfp_connection->ag_activate_voice_recognition_value){ 1037de9e0ea7SMilanka Ringwald case 0: 1038135d9718SMilanka Ringwald if (hfp_ag_voice_recognition_session_active(hfp_connection)){ 1039fd4151d1SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF; 1040fd4151d1SMilanka Ringwald done = hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1041fd4151d1SMilanka Ringwald } 1042de9e0ea7SMilanka Ringwald break; 1043de9e0ea7SMilanka Ringwald 1044de9e0ea7SMilanka Ringwald case 1: 1045135d9718SMilanka Ringwald if (hfp_ag_can_activate_voice_recognition(hfp_connection)){ 1046fd4151d1SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED; 1047fd4151d1SMilanka Ringwald done = hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1048fd4151d1SMilanka Ringwald } 1049de9e0ea7SMilanka Ringwald break; 1050de9e0ea7SMilanka Ringwald 1051de9e0ea7SMilanka Ringwald case 2: 1052135d9718SMilanka Ringwald if (hfp_ag_voice_recognition_session_active(hfp_connection)){ 1053de9e0ea7SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 1054de9e0ea7SMilanka Ringwald done = hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1055de9e0ea7SMilanka Ringwald } 1056de9e0ea7SMilanka Ringwald break; 1057de9e0ea7SMilanka Ringwald default: 1058de9e0ea7SMilanka Ringwald break; 1059fd4151d1SMilanka Ringwald } 1060fd4151d1SMilanka Ringwald if (done == 0){ 1061fd4151d1SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 1062fd4151d1SMilanka Ringwald done = hfp_ag_send_error(hfp_connection->rfcomm_cid); 1063fd4151d1SMilanka Ringwald return 1; 1064fd4151d1SMilanka Ringwald } 1065754910caSMilanka Ringwald 1066774e3016SMatthias Ringwald status = hfp_ag_vra_state_machine_two(hfp_connection); 10672cc52945SMatthias Ringwald return (status == ERROR_CODE_SUCCESS) ? 1 : 0; 1068de9e0ea7SMilanka Ringwald } 10692cc52945SMatthias Ringwald 10702cc52945SMatthias Ringwald return 0; 1071754910caSMilanka Ringwald } 1072754910caSMilanka Ringwald 1073a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){ 1074dc6af379SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) { 1075dc6af379SMatthias Ringwald return 0; 1076dc6af379SMatthias Ringwald } 1077dc6af379SMatthias Ringwald 1078c04cf7ffSMilanka Ringwald int sent = codecs_exchange_state_machine(hfp_connection); 1079c04cf7ffSMilanka Ringwald if (sent) return 1; 1080aa4dd815SMatthias Ringwald 108140a8ee13SMatthias Ringwald if (has_in_band_ring_tone()){ 108240a8ee13SMatthias Ringwald if (hfp_connection->ag_in_band_ring_tone_active != hfp_ag_in_band_ring_tone_active) { 108340a8ee13SMatthias Ringwald hfp_connection->ag_in_band_ring_tone_active = hfp_ag_in_band_ring_tone_active; 1084f197e761SMatthias Ringwald hfp_ag_send_change_in_band_ring_tone_setting_cmd(hfp_connection); 1085aa4dd815SMatthias Ringwald return 1; 10869430c71eSMatthias Ringwald } 108740a8ee13SMatthias Ringwald } 10889430c71eSMatthias Ringwald 10899430c71eSMatthias Ringwald switch(hfp_connection->command){ 1090aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1091485ac19eSMilanka Ringwald hfp_ag_send_report_network_operator_name_cmd(hfp_connection->rfcomm_cid, hfp_connection->network_operator); 1092aa4dd815SMatthias Ringwald return 1; 1093aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 1094a0ffb263SMatthias Ringwald if (hfp_connection->network_operator.format != 0){ 1095485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 1096aa4dd815SMatthias Ringwald } else { 1097485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 10983deb3ec6SMatthias Ringwald } 1099aa4dd815SMatthias Ringwald return 1; 1100aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE: 1101485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1102aa4dd815SMatthias Ringwald return 1; 11033deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR: 1104df9853c5SMatthias Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1105df9853c5SMatthias Ringwald return 1; 11063deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 1107485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1108ce263fc8SMatthias Ringwald return 1; 11093deb3ec6SMatthias Ringwald default: 11103deb3ec6SMatthias Ringwald break; 11113deb3ec6SMatthias Ringwald } 1112aa4dd815SMatthias Ringwald return 0; 11133deb3ec6SMatthias Ringwald } 11143deb3ec6SMatthias Ringwald 1115a0ffb263SMatthias Ringwald static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){ 1116505f1c30SMatthias Ringwald if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) || 1117505f1c30SMatthias Ringwald (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0; 11183deb3ec6SMatthias Ringwald 1119a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 112052cbdd6dSMilanka Ringwald 112152cbdd6dSMilanka Ringwald if (hfp_connection->release_audio_connection){ 112252cbdd6dSMilanka Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 112352cbdd6dSMilanka Ringwald hfp_connection->release_audio_connection = 0; 112452cbdd6dSMilanka Ringwald gap_disconnect(hfp_connection->sco_handle); 112552cbdd6dSMilanka Ringwald return 1; 112652cbdd6dSMilanka Ringwald } 1127aa4dd815SMatthias Ringwald 112829cddf58SMatthias Ringwald // accept incoming audio connection (codec negotiation is not used) 1129447743f7SMatthias Ringwald if (hfp_connection->accept_sco && (hfp_sco_setup_active() == false)){ 113029cddf58SMatthias Ringwald // notify about codec selection if not done already 113129cddf58SMatthias Ringwald if (hfp_connection->negotiated_codec == 0){ 113229cddf58SMatthias Ringwald hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 113329cddf58SMatthias Ringwald } 113429cddf58SMatthias Ringwald // 113529cddf58SMatthias Ringwald bool incoming_eSCO = hfp_connection->accept_sco == 2; 113629cddf58SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 113729cddf58SMatthias Ringwald hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO); 113829cddf58SMatthias Ringwald return 1; 113929cddf58SMatthias Ringwald } 114029cddf58SMatthias Ringwald 1141aa4dd815SMatthias Ringwald // run codecs exchange 1142c04cf7ffSMilanka Ringwald int sent = codecs_exchange_state_machine(hfp_connection); 1143c04cf7ffSMilanka Ringwald if (sent) return 1; 1144aa4dd815SMatthias Ringwald 1145c04cf7ffSMilanka Ringwald if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0; 1146447743f7SMatthias Ringwald if (hfp_sco_setup_active()) return 0; 1147d2e34ffbSMatthias Ringwald if (hci_can_send_command_packet_now() == false) return 0; 1148a0ffb263SMatthias Ringwald if (hfp_connection->establish_audio_connection){ 1149a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 1150a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 1151eddcd308SMatthias Ringwald hfp_setup_synchronous_connection(hfp_connection); 1152aa4dd815SMatthias Ringwald return 1; 1153aa4dd815SMatthias Ringwald } 1154aa4dd815SMatthias Ringwald return 0; 1155aa4dd815SMatthias Ringwald } 1156aa4dd815SMatthias Ringwald 1157056efd45SMatthias Ringwald static void hfp_ag_set_callsetup_indicator(void){ 1158056efd45SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup"); 1159056efd45SMatthias Ringwald if (!indicator){ 1160056efd45SMatthias Ringwald log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing"); 1161056efd45SMatthias Ringwald return; 1162056efd45SMatthias Ringwald }; 1163056efd45SMatthias Ringwald indicator->status = hfp_gsm_callsetup_status(); 1164056efd45SMatthias Ringwald } 1165056efd45SMatthias Ringwald 1166056efd45SMatthias Ringwald static void hfp_ag_set_callheld_indicator(void){ 1167056efd45SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld"); 1168056efd45SMatthias Ringwald if (!indicator){ 1169056efd45SMatthias Ringwald log_error("hfp_ag_set_callheld_state: callheld indicator is missing"); 1170056efd45SMatthias Ringwald return; 1171056efd45SMatthias Ringwald }; 1172056efd45SMatthias Ringwald indicator->status = hfp_gsm_callheld_status(); 1173056efd45SMatthias Ringwald } 1174056efd45SMatthias Ringwald 1175aa4dd815SMatthias Ringwald // 1176fcd59412SMilanka Ringwald // transition implementations for hfp_ag_call_state_machine 1177aa4dd815SMatthias Ringwald // 1178aa4dd815SMatthias Ringwald 11792ff1d7d7SMilanka Ringwald static void hfp_ag_hf_start_ringing_incoming(hfp_connection_t * hfp_connection){ 1180f197e761SMatthias Ringwald if (use_in_band_tone(hfp_connection)){ 1181a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING; 1182d97d752dSMilanka Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 1183aa4dd815SMatthias Ringwald } else { 11849493406bSMilanka Ringwald hfp_connection->call_state = HFP_CALL_INCOMING_RINGING; 1185aa4dd815SMatthias Ringwald } 1186aa4dd815SMatthias Ringwald } 1187aa4dd815SMatthias Ringwald 1188fcd59412SMilanka Ringwald static void hfp_ag_hf_start_ringing_outgoing(hfp_connection_t * hfp_connection){ 1189fcd59412SMilanka Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING; 1190fcd59412SMilanka Ringwald } 1191fcd59412SMilanka Ringwald 1192a0ffb263SMatthias Ringwald static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){ 1193a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 0; 1194a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = 0; 1195aa4dd815SMatthias Ringwald } 1196aa4dd815SMatthias Ringwald 1197a4f85bd2SMilanka Ringwald 119831f55b91SMilanka Ringwald static void hfp_ag_trigger_incoming_call_during_active_one(void){ 119931f55b91SMilanka Ringwald btstack_linked_list_iterator_t it; 120031f55b91SMilanka Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 120131f55b91SMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 120231f55b91SMilanka Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 120331f55b91SMilanka Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 120431f55b91SMilanka Ringwald if (hfp_connection->call_state != HFP_CALL_ACTIVE) continue; 120531f55b91SMilanka Ringwald 120631f55b91SMilanka Ringwald hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING; 120731f55b91SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 120831f55b91SMilanka Ringwald } 120931f55b91SMilanka Ringwald } 121031f55b91SMilanka Ringwald 1211d3709f3eSMatthias Ringwald static void hfp_ag_trigger_ring_and_clip(void) { 1212d3709f3eSMatthias Ringwald btstack_linked_list_iterator_t it; 1213d3709f3eSMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1214d3709f3eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1215d3709f3eSMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1216d3709f3eSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1217d3709f3eSMatthias Ringwald switch (hfp_connection->call_state){ 1218d3709f3eSMatthias Ringwald case HFP_CALL_INCOMING_RINGING: 1219d3709f3eSMatthias Ringwald case HFP_CALL_OUTGOING_RINGING: 1220d3709f3eSMatthias Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING: 12213561686eSMatthias Ringwald // Queue RING on this connection 1222a4f85bd2SMilanka Ringwald hfp_connection->ag_ring = 1; 12233561686eSMatthias Ringwald 12243561686eSMatthias Ringwald // HFP v1.8, 4.23 Calling Line Identification (CLI) Notification 12253561686eSMatthias Ringwald // "If the calling subscriber number information is available from the network, the AG shall issue the 12263561686eSMatthias Ringwald // +CLIP unsolicited result code just after every RING indication when the HF is alerted in an incoming call." 12273561686eSMatthias Ringwald hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled; 12283561686eSMatthias Ringwald 12293561686eSMatthias Ringwald // trigger next message 1230a4f85bd2SMilanka Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 1231b7e27351SMilanka Ringwald break; 1232b7e27351SMilanka Ringwald default: 1233b7e27351SMilanka Ringwald break; 1234b7e27351SMilanka Ringwald } 1235a4f85bd2SMilanka Ringwald } 1236a4f85bd2SMilanka Ringwald } 1237a4f85bd2SMilanka Ringwald 123850b9afa4SMilanka Ringwald // trigger RING and CLIP messages on connections that are ringing 123950b9afa4SMilanka Ringwald static void hfp_ag_ring_timeout_handler(btstack_timer_source_t * timer){ 124050b9afa4SMilanka Ringwald hfp_ag_trigger_ring_and_clip(); 124150b9afa4SMilanka Ringwald 12426db648bcSMilanka Ringwald btstack_run_loop_set_timer(timer, HFP_RING_PERIOD_MS); // 2 seconds timeout 124350b9afa4SMilanka Ringwald btstack_run_loop_add_timer(timer); 1244a4f85bd2SMilanka Ringwald } 1245a4f85bd2SMilanka Ringwald 1246a4f85bd2SMilanka Ringwald static void hfp_ag_ring_timeout_stop(void){ 1247a4f85bd2SMilanka Ringwald btstack_run_loop_remove_timer(&hfp_ag_ring_timeout); 1248a4f85bd2SMilanka Ringwald } 1249a4f85bd2SMilanka Ringwald 1250a4f85bd2SMilanka Ringwald static void hfp_ag_start_ringing(void){ 125150b9afa4SMilanka Ringwald // setup ring timer 125250b9afa4SMilanka Ringwald btstack_run_loop_remove_timer(&hfp_ag_ring_timeout); 125350b9afa4SMilanka Ringwald btstack_run_loop_set_timer_handler(&hfp_ag_ring_timeout, hfp_ag_ring_timeout_handler); 12546db648bcSMilanka Ringwald btstack_run_loop_set_timer(&hfp_ag_ring_timeout, HFP_RING_PERIOD_MS); // 2 seconds timeout 125550b9afa4SMilanka Ringwald btstack_run_loop_add_timer(&hfp_ag_ring_timeout); 125650b9afa4SMilanka Ringwald 125750b9afa4SMilanka Ringwald // emit start ringing 1258a4f85bd2SMilanka Ringwald hfp_ag_emit_general_simple_event(HFP_SUBEVENT_START_RINGING); 125950b9afa4SMilanka Ringwald 126050b9afa4SMilanka Ringwald // send initial RING + CLIP 126150b9afa4SMilanka Ringwald hfp_ag_trigger_ring_and_clip(); 1262a4f85bd2SMilanka Ringwald } 1263a4f85bd2SMilanka Ringwald 1264a4f85bd2SMilanka Ringwald static void hfp_ag_stop_ringing(void){ 1265a4f85bd2SMilanka Ringwald hfp_ag_ring_timeout_stop(); 1266a4f85bd2SMilanka Ringwald hfp_ag_emit_general_simple_event(HFP_SUBEVENT_STOP_RINGING); 1267a4f85bd2SMilanka Ringwald 1268a4f85bd2SMilanka Ringwald btstack_linked_list_iterator_t it; 1269a4f85bd2SMilanka Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1270a4f85bd2SMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1271a4f85bd2SMilanka Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1272a4f85bd2SMilanka Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1273a4f85bd2SMilanka Ringwald if ((hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) && 1274a4f85bd2SMilanka Ringwald (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue; 1275a4f85bd2SMilanka Ringwald hfp_ag_hf_stop_ringing(hfp_connection); 1276a4f85bd2SMilanka Ringwald } 1277a4f85bd2SMilanka Ringwald } 1278a4f85bd2SMilanka Ringwald 127931f55b91SMilanka Ringwald static void hfp_ag_trigger_incoming_call_idle(void){ 1280aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name("callsetup"); 1281aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 1282aa4dd815SMatthias Ringwald 12833561686eSMatthias Ringwald // update callsetup state in connections 1284665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1285665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1286665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1287a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 128866c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 128931f55b91SMilanka Ringwald if (hfp_connection->call_state != HFP_CALL_IDLE) continue; 129031f55b91SMilanka Ringwald 1291a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1292a4f85bd2SMilanka Ringwald 1293a4f85bd2SMilanka Ringwald hfp_ag_hf_start_ringing_incoming(hfp_connection); 1294a4f85bd2SMilanka Ringwald 1295f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1296aa4dd815SMatthias Ringwald } 12973561686eSMatthias Ringwald 12983561686eSMatthias Ringwald // start local ringing - started after connection state has been updated 12993561686eSMatthias Ringwald hfp_ag_start_ringing(); 1300aa4dd815SMatthias Ringwald } 1301aa4dd815SMatthias Ringwald 1302fe899794SMatthias Ringwald static void hfp_ag_trigger_outgoing_call(void){ 1303fe899794SMatthias Ringwald btstack_linked_list_iterator_t it; 1304fe899794SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1305fe899794SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1306fe899794SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1307fe899794SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1308fe899794SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 1309fe899794SMatthias Ringwald if (hfp_connection->call_state == HFP_CALL_IDLE){ 1310fe899794SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 1311fe899794SMatthias Ringwald } 1312fe899794SMatthias Ringwald } 1313fe899794SMatthias Ringwald } 13145a7033e2SMatthias Ringwald 13155a7033e2SMatthias Ringwald static void hfp_ag_handle_outgoing_call_accepted(hfp_connection_t * hfp_connection){ 13165a7033e2SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING; 13175a7033e2SMatthias Ringwald 13185a7033e2SMatthias Ringwald // trigger callsetup to be 13195a7033e2SMatthias Ringwald int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT; 13205a7033e2SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_ACCEPTED, 0, 0, NULL); 13215a7033e2SMatthias Ringwald 13225a7033e2SMatthias Ringwald int indicator_index ; 13235a7033e2SMatthias Ringwald 13245a7033e2SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 13255a7033e2SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callsetup"); 13265a7033e2SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 13275a7033e2SMatthias Ringwald 13285a7033e2SMatthias Ringwald // put current call on hold if active 13295a7033e2SMatthias Ringwald if (put_call_on_hold){ 13305a7033e2SMatthias Ringwald log_info("AG putting current call on hold for new outgoing call"); 13315a7033e2SMatthias Ringwald hfp_ag_set_callheld_indicator(); 13325a7033e2SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callheld"); 13335a7033e2SMatthias Ringwald hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]); 13345a7033e2SMatthias Ringwald } 13355a7033e2SMatthias Ringwald 13365a7033e2SMatthias Ringwald // start audio if needed 13375a7033e2SMatthias Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 13385a7033e2SMatthias Ringwald } 1339fe899794SMatthias Ringwald 13404a2e058fSMatthias Ringwald static void hfp_ag_transfer_indicator(const char * name){ 13414a2e058fSMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name(name); 1342aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 1343aa4dd815SMatthias Ringwald 1344665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1345665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1346665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1347a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 134866c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1349a0ffb263SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 1350a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1351f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1352aa4dd815SMatthias Ringwald } 1353aa4dd815SMatthias Ringwald } 1354aa4dd815SMatthias Ringwald 13554a2e058fSMatthias Ringwald static void hfp_ag_transfer_callsetup_state(void){ 13564a2e058fSMatthias Ringwald hfp_ag_transfer_indicator("callsetup"); 13574a2e058fSMatthias Ringwald } 13584a2e058fSMatthias Ringwald 1359aa4dd815SMatthias Ringwald static void hfp_ag_transfer_call_state(void){ 13604a2e058fSMatthias Ringwald hfp_ag_transfer_indicator("call"); 1361aa4dd815SMatthias Ringwald } 1362aa4dd815SMatthias Ringwald 1363aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callheld_state(void){ 13644a2e058fSMatthias Ringwald hfp_ag_transfer_indicator("callheld"); 1365aa4dd815SMatthias Ringwald } 1366aa4dd815SMatthias Ringwald 1367aa4dd815SMatthias Ringwald static void hfp_ag_hf_accept_call(hfp_connection_t * source){ 1368aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 1369aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1370aa4dd815SMatthias Ringwald 1371a4f85bd2SMilanka Ringwald hfp_ag_stop_ringing(); 1372a4f85bd2SMilanka Ringwald 1373665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1374665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1375665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1376a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 137766c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 13789493406bSMilanka Ringwald if ((hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) && 1379505f1c30SMatthias Ringwald (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue; 1380aa4dd815SMatthias Ringwald 1381a0ffb263SMatthias Ringwald if (hfp_connection == source){ 1382aa4dd815SMatthias Ringwald 1383f197e761SMatthias Ringwald if (use_in_band_tone(hfp_connection)){ 1384a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1385aa4dd815SMatthias Ringwald } else { 1386a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE; 1387d97d752dSMilanka Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 1388aa4dd815SMatthias Ringwald } 1389aa4dd815SMatthias Ringwald 1390a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1391a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1392aa4dd815SMatthias Ringwald 1393aa4dd815SMatthias Ringwald } else { 1394a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 1395aa4dd815SMatthias Ringwald } 1396f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1397aa4dd815SMatthias Ringwald } 1398aa4dd815SMatthias Ringwald } 1399aa4dd815SMatthias Ringwald 1400aa4dd815SMatthias Ringwald static void hfp_ag_ag_accept_call(void){ 1401aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 1402aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1403aa4dd815SMatthias Ringwald 1404a4f85bd2SMilanka Ringwald hfp_ag_stop_ringing(); 1405a4f85bd2SMilanka Ringwald 1406665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1407665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1408665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1409a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 141066c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1411a5764cd2SMatthias Ringwald if ((hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) && 1412a5764cd2SMatthias Ringwald (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue; 1413aa4dd815SMatthias Ringwald 1414a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION; 1415aa4dd815SMatthias Ringwald 1416a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1417a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1418aa4dd815SMatthias Ringwald 1419f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1420aa4dd815SMatthias Ringwald break; // only single 1421aa4dd815SMatthias Ringwald } 1422aa4dd815SMatthias Ringwald } 1423aa4dd815SMatthias Ringwald 1424aa4dd815SMatthias Ringwald static void hfp_ag_trigger_reject_call(void){ 1425aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1426a4f85bd2SMilanka Ringwald 1427a4f85bd2SMilanka Ringwald hfp_ag_stop_ringing(); 1428a4f85bd2SMilanka Ringwald 1429665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1430665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1431665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1432665d90f2SMatthias Ringwald hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 143366c5995fSMatthias Ringwald if (connection->local_role != HFP_ROLE_AG) continue; 143407a44c5eSMilanka Ringwald 143507a44c5eSMilanka Ringwald switch (connection->call_state){ 143607a44c5eSMilanka Ringwald case HFP_CALL_INCOMING_RINGING: 143707a44c5eSMilanka Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING: 143807a44c5eSMilanka Ringwald case HFP_CALL_OUTGOING_RINGING: 143907a44c5eSMilanka Ringwald case HFP_CALL_OUTGOING_INITIATED: 144007a44c5eSMilanka Ringwald case HFP_CALL_OUTGOING_DIALING: 144107a44c5eSMilanka Ringwald break; 144207a44c5eSMilanka Ringwald default: 144307a44c5eSMilanka Ringwald continue; 144407a44c5eSMilanka Ringwald } 1445095a9f2bSMilanka Ringwald 1446aa4dd815SMatthias Ringwald connection->call_state = HFP_CALL_IDLE; 1447095a9f2bSMilanka Ringwald connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1448095a9f2bSMilanka Ringwald 1449095a9f2bSMilanka Ringwald if (connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 1450095a9f2bSMilanka Ringwald connection->release_audio_connection = 1; 1451095a9f2bSMilanka Ringwald } 1452095a9f2bSMilanka Ringwald hfp_emit_simple_event(connection, HFP_SUBEVENT_CALL_TERMINATED); 1453f0aeb307SMatthias Ringwald hfp_ag_run_for_context(connection); 1454095a9f2bSMilanka Ringwald rfcomm_request_can_send_now_event(connection->rfcomm_cid); 1455aa4dd815SMatthias Ringwald } 1456aa4dd815SMatthias Ringwald } 1457aa4dd815SMatthias Ringwald 1458aa4dd815SMatthias Ringwald static void hfp_ag_trigger_terminate_call(void){ 1459aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 1460aa4dd815SMatthias Ringwald 1461a4f85bd2SMilanka Ringwald // no ringing during call 1462a4f85bd2SMilanka Ringwald 1463665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1464665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1465665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1466a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 146766c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1468a0ffb263SMatthias Ringwald if (hfp_connection->call_state == HFP_CALL_IDLE) continue; 1469fb75b483SMilanka Ringwald 1470a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 1471a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1472095a9f2bSMilanka Ringwald 14736d78145cSMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 1474a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 1; 14756d78145cSMatthias Ringwald } 1476095a9f2bSMilanka Ringwald 1477fb75b483SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED); 1478f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1479a13ff7d1SMilanka Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 1480aa4dd815SMatthias Ringwald } 1481aa4dd815SMatthias Ringwald } 1482aa4dd815SMatthias Ringwald 14830cb5b971SMatthias Ringwald static void hfp_ag_set_call_indicator(void){ 1484aa4dd815SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call"); 1485aa4dd815SMatthias Ringwald if (!indicator){ 1486aa4dd815SMatthias Ringwald log_error("hfp_ag_set_call_state: call indicator is missing"); 148745718b6fSMatthias Ringwald return; 1488aa4dd815SMatthias Ringwald }; 1489d210d9c4SMatthias Ringwald indicator->status = hfp_gsm_call_status(); 1490aa4dd815SMatthias Ringwald } 1491aa4dd815SMatthias Ringwald 1492aa4dd815SMatthias Ringwald static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){ 1493665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1494665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1495665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1496a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 149766c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1498a0ffb263SMatthias Ringwald if (hfp_connection->call_state == call_state) return hfp_connection; 1499aa4dd815SMatthias Ringwald } 1500aa4dd815SMatthias Ringwald return NULL; 1501aa4dd815SMatthias Ringwald } 1502aa4dd815SMatthias Ringwald 1503a5bdcda8SMatthias Ringwald static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){ 1504665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1505665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1506665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1507a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 150866c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1509a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = state + 1; 1510ce263fc8SMatthias Ringwald } 1511ce263fc8SMatthias Ringwald } 1512ce263fc8SMatthias Ringwald 1513a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 1514aa4dd815SMatthias Ringwald int indicator_index; 1515a0ffb263SMatthias Ringwald switch (hfp_connection->call_state){ 1516aa4dd815SMatthias Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING: 1517a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 1518a0ffb263SMatthias Ringwald // we got event: audio hfp_connection established 15199493406bSMilanka Ringwald hfp_connection->call_state = HFP_CALL_INCOMING_RINGING; 1520aa4dd815SMatthias Ringwald break; 1521aa4dd815SMatthias Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE: 1522a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 1523a0ffb263SMatthias Ringwald // we got event: audio hfp_connection established 1524a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1525aa4dd815SMatthias Ringwald break; 1526aa4dd815SMatthias Ringwald case HFP_CALL_W2_SEND_CALL_WAITING: 1527a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_CHLD; 1528a0ffb263SMatthias Ringwald hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid); 1529aa4dd815SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callsetup"); 1530a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1531aa4dd815SMatthias Ringwald break; 1532aa4dd815SMatthias Ringwald default: 1533aa4dd815SMatthias Ringwald break; 1534aa4dd815SMatthias Ringwald } 1535aa4dd815SMatthias Ringwald return 0; 1536aa4dd815SMatthias Ringwald } 153739426febSMatthias Ringwald 1538e84fa067SMatthias Ringwald // queue 'error' to HF 1539e84fa067SMatthias Ringwald static void hfp_ag_queue_error(hfp_connection_t * hfp_connection){ 15405e87d1ceSMatthias Ringwald hfp_connection->send_error++; 1541e84fa067SMatthias Ringwald } 1542e84fa067SMatthias Ringwald 1543e84fa067SMatthias Ringwald // queue 'ok' to HF 1544e84fa067SMatthias Ringwald static void hfp_ag_queue_ok(hfp_connection_t * hfp_connection){ 15455e87d1ceSMatthias Ringwald hfp_connection->ok_pending++; 1546e84fa067SMatthias Ringwald } 1547e84fa067SMatthias Ringwald 154839426febSMatthias Ringwald // functions extracted from hfp_ag_call_sm below 15499f9621cfSMatthias Ringwald static void hfp_ag_handle_reject_outgoing_call(void){ 155039426febSMatthias Ringwald hfp_connection_t * hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 155139426febSMatthias Ringwald if (!hfp_connection){ 155239426febSMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state"); 155339426febSMatthias Ringwald return; 155439426febSMatthias Ringwald } 155539426febSMatthias Ringwald 1556f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_REJECTED, 0, 0, NULL); 155739426febSMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 1558e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 155939426febSMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 156039426febSMatthias Ringwald } 156139426febSMatthias Ringwald 1562a0ffb263SMatthias Ringwald // hfp_connection is used to identify originating HF 1563a0ffb263SMatthias Ringwald static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){ 156481e25d0aSMatthias Ringwald 156581e25d0aSMatthias Ringwald // allow to intercept call statemachine events 156681e25d0aSMatthias Ringwald if (hfp_ag_custom_call_sm_handler != NULL){ 156781e25d0aSMatthias Ringwald bool handle_event = (*hfp_ag_custom_call_sm_handler)(event); 156881e25d0aSMatthias Ringwald if (!handle_event) return; 156981e25d0aSMatthias Ringwald } 157081e25d0aSMatthias Ringwald 1571d210d9c4SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1572d210d9c4SMatthias Ringwald int callheld_indicator_index = get_ag_indicator_index_for_name("callheld"); 1573d210d9c4SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 157474386ee0SMatthias Ringwald 1575aa4dd815SMatthias Ringwald switch (event){ 1576aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL: 1577d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1578aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1579d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 158012cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1581f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL, 0, 0, NULL); 1582d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 158331f55b91SMilanka Ringwald hfp_ag_trigger_incoming_call_idle(); 158460ebb071SMilanka Ringwald log_info("AG rings"); 1585aa4dd815SMatthias Ringwald break; 1586aa4dd815SMatthias Ringwald default: 15873deb3ec6SMatthias Ringwald break; 15883deb3ec6SMatthias Ringwald } 15893deb3ec6SMatthias Ringwald break; 1590aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1591d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 159212cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1593f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL, 0, 0, NULL); 1594d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 159531f55b91SMilanka Ringwald hfp_ag_trigger_incoming_call_during_active_one(); 159660ebb071SMilanka Ringwald log_info("AG call waiting"); 1597aa4dd815SMatthias Ringwald break; 1598aa4dd815SMatthias Ringwald default: 15993deb3ec6SMatthias Ringwald break; 16003deb3ec6SMatthias Ringwald } 16013deb3ec6SMatthias Ringwald break; 16027bbeb3adSMilanka Ringwald default: 16037bbeb3adSMilanka Ringwald break; 1604aa4dd815SMatthias Ringwald } 1605aa4dd815SMatthias Ringwald break; 1606aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG: 1607d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1608aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1609d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 161012cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1611f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, 0, 0, NULL); 1612d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1613d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1614aa4dd815SMatthias Ringwald hfp_ag_ag_accept_call(); 161560ebb071SMilanka Ringwald log_info("AG answers call, accept call by GSM"); 1616aa4dd815SMatthias Ringwald break; 1617aa4dd815SMatthias Ringwald default: 16183deb3ec6SMatthias Ringwald break; 16193deb3ec6SMatthias Ringwald } 1620aa4dd815SMatthias Ringwald break; 1621aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1622d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1623aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 162460ebb071SMilanka Ringwald log_info("AG: current call is placed on hold, incoming call gets active"); 1625f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, 0, 0, NULL); 1626d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1627d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1628ce263fc8SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1629ce263fc8SMatthias Ringwald hfp_ag_transfer_callheld_state(); 1630aa4dd815SMatthias Ringwald break; 1631aa4dd815SMatthias Ringwald default: 1632aa4dd815SMatthias Ringwald break; 1633aa4dd815SMatthias Ringwald } 1634aa4dd815SMatthias Ringwald break; 16357bbeb3adSMilanka Ringwald default: 16367bbeb3adSMilanka Ringwald break; 1637aa4dd815SMatthias Ringwald } 1638aa4dd815SMatthias Ringwald break; 1639ce263fc8SMatthias Ringwald 1640ce263fc8SMatthias Ringwald case HFP_AG_HELD_CALL_JOINED_BY_AG: 1641d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1642ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1643d0c20769SMatthias Ringwald switch (hfp_gsm_callheld_status()){ 1644ce263fc8SMatthias Ringwald case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED: 164560ebb071SMilanka Ringwald log_info("AG: joining held call with active call"); 1646f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_HELD_CALL_JOINED_BY_AG, 0, 0, NULL); 1647d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1648ce263fc8SMatthias Ringwald hfp_ag_transfer_callheld_state(); 1649ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL); 1650ce263fc8SMatthias Ringwald break; 1651ce263fc8SMatthias Ringwald default: 1652ce263fc8SMatthias Ringwald break; 1653ce263fc8SMatthias Ringwald } 1654ce263fc8SMatthias Ringwald break; 1655ce263fc8SMatthias Ringwald default: 1656ce263fc8SMatthias Ringwald break; 1657ce263fc8SMatthias Ringwald } 1658ce263fc8SMatthias Ringwald break; 1659ce263fc8SMatthias Ringwald 1660aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF: 1661d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1662aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1663d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 166412cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1665f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, 0, 0, NULL); 1666d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1667d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1668a0ffb263SMatthias Ringwald hfp_ag_hf_accept_call(hfp_connection); 1669e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 167060ebb071SMilanka Ringwald log_info("HF answers call, accept call by GSM"); 1671ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED); 1672aa4dd815SMatthias Ringwald break; 1673aa4dd815SMatthias Ringwald default: 1674aa4dd815SMatthias Ringwald break; 1675aa4dd815SMatthias Ringwald } 16763deb3ec6SMatthias Ringwald break; 16773deb3ec6SMatthias Ringwald default: 16783deb3ec6SMatthias Ringwald break; 16793deb3ec6SMatthias Ringwald } 16803deb3ec6SMatthias Ringwald break; 16813deb3ec6SMatthias Ringwald 1682ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG: 1683d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1684ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1685d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 168612cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1687f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, 0, 0, NULL); 168827bb1817SMatthias Ringwald hfp_ag_response_and_hold_active = true; 1689ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1690a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1691ac6f828eSMilanka Ringwald // as with regular call 1692d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1693d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1694ce263fc8SMatthias Ringwald hfp_ag_ag_accept_call(); 169560ebb071SMilanka Ringwald log_info("AG response and hold - hold by AG"); 1696ce263fc8SMatthias Ringwald break; 1697ce263fc8SMatthias Ringwald default: 1698ce263fc8SMatthias Ringwald break; 1699ce263fc8SMatthias Ringwald } 1700ce263fc8SMatthias Ringwald break; 1701ce263fc8SMatthias Ringwald default: 1702ce263fc8SMatthias Ringwald break; 1703ce263fc8SMatthias Ringwald } 1704ce263fc8SMatthias Ringwald break; 1705ce263fc8SMatthias Ringwald 1706ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF: 1707d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1708ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1709d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 171012cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1711f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, 0, 0, NULL); 171227bb1817SMatthias Ringwald hfp_ag_response_and_hold_active = true; 1713ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1714a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1715ac6f828eSMilanka Ringwald // as with regular call 1716d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1717d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1718a0ffb263SMatthias Ringwald hfp_ag_hf_accept_call(hfp_connection); 171960ebb071SMilanka Ringwald log_info("AG response and hold - hold by HF"); 1720ce263fc8SMatthias Ringwald break; 1721ce263fc8SMatthias Ringwald default: 1722ce263fc8SMatthias Ringwald break; 1723ce263fc8SMatthias Ringwald } 1724ce263fc8SMatthias Ringwald break; 1725ce263fc8SMatthias Ringwald default: 1726ce263fc8SMatthias Ringwald break; 1727ce263fc8SMatthias Ringwald } 1728ce263fc8SMatthias Ringwald break; 1729ce263fc8SMatthias Ringwald 1730ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG: 1731ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF: 1732ce263fc8SMatthias Ringwald if (!hfp_ag_response_and_hold_active) break; 1733ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1734f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, 0, 0, NULL); 173527bb1817SMatthias Ringwald hfp_ag_response_and_hold_active = false; 1736ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED; 1737a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 173860ebb071SMilanka Ringwald log_info("Held Call accepted and active"); 1739ce263fc8SMatthias Ringwald break; 1740ce263fc8SMatthias Ringwald 1741ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG: 1742ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF: 1743ce263fc8SMatthias Ringwald if (!hfp_ag_response_and_hold_active) break; 1744ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1745f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, 0, 0, NULL); 174627bb1817SMatthias Ringwald hfp_ag_response_and_hold_active = false; 1747ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1748a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1749ce263fc8SMatthias Ringwald // from terminate by ag 1750d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1751ce263fc8SMatthias Ringwald hfp_ag_trigger_terminate_call(); 1752ce263fc8SMatthias Ringwald break; 1753ce263fc8SMatthias Ringwald 1754aa4dd815SMatthias Ringwald case HFP_AG_TERMINATE_CALL_BY_HF: 1755d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1756aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1757d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 175812cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 175907a44c5eSMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 176007a44c5eSMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 1761f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL); 1762d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1763aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1764aa4dd815SMatthias Ringwald hfp_ag_trigger_reject_call(); 176560ebb071SMilanka Ringwald log_info("HF Rejected Incoming call, AG terminate call"); 1766aa4dd815SMatthias Ringwald break; 1767aa4dd815SMatthias Ringwald default: 1768aa4dd815SMatthias Ringwald break; 1769aa4dd815SMatthias Ringwald } 1770aa4dd815SMatthias Ringwald break; 177112cbbeeeSMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1772f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL); 17731ec112deSMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1774d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 17751ec112deSMatthias Ringwald hfp_ag_trigger_terminate_call(); 177660ebb071SMilanka Ringwald log_info("AG terminate call"); 1777aa4dd815SMatthias Ringwald break; 17787bbeb3adSMilanka Ringwald default: 17797bbeb3adSMilanka Ringwald break; 1780aa4dd815SMatthias Ringwald } 1781aa4dd815SMatthias Ringwald break; 17823deb3ec6SMatthias Ringwald 1783aa4dd815SMatthias Ringwald case HFP_AG_TERMINATE_CALL_BY_AG: 1784d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1785aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1786d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 178707a44c5eSMilanka Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 178807a44c5eSMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 178907a44c5eSMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1790f2aa99a9SMilanka Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1791f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_AG, 0, 0, NULL); 1792d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1793aa4dd815SMatthias Ringwald hfp_ag_trigger_reject_call(); 179460ebb071SMilanka Ringwald log_info("AG Rejected Incoming call, AG terminate call"); 1795aa4dd815SMatthias Ringwald break; 1796aa4dd815SMatthias Ringwald default: 1797aa4dd815SMatthias Ringwald break; 17983deb3ec6SMatthias Ringwald } 1799202c8a4cSMatthias Ringwald break; 180012cbbeeeSMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1801f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_AG, 0, 0, NULL); 1802d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1803d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1804aa4dd815SMatthias Ringwald hfp_ag_trigger_terminate_call(); 180560ebb071SMilanka Ringwald log_info("AG terminate call"); 1806aa4dd815SMatthias Ringwald break; 1807aa4dd815SMatthias Ringwald default: 18083deb3ec6SMatthias Ringwald break; 18093deb3ec6SMatthias Ringwald } 18103deb3ec6SMatthias Ringwald break; 1811aa4dd815SMatthias Ringwald case HFP_AG_CALL_DROPPED: 1812d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1813aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1814d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1815aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1816aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1817fb75b483SMilanka Ringwald hfp_ag_stop_ringing(); 1818fb75b483SMilanka Ringwald break; 1819fb75b483SMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 1820fb75b483SMilanka Ringwald log_info("Outgoing call interrupted\n"); 1821aa4dd815SMatthias Ringwald break; 1822aa4dd815SMatthias Ringwald default: 18233deb3ec6SMatthias Ringwald break; 18243deb3ec6SMatthias Ringwald } 1825f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL); 1826d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1827aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 182810c236bfSMatthias Ringwald hfp_ag_trigger_terminate_call(); 1829aa4dd815SMatthias Ringwald break; 1830aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1831ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_active) { 1832f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL); 1833ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1834a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1835ce263fc8SMatthias Ringwald } 1836f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL); 1837d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1838d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1839aa4dd815SMatthias Ringwald hfp_ag_trigger_terminate_call(); 184060ebb071SMilanka Ringwald log_info("AG notify call dropped"); 1841aa4dd815SMatthias Ringwald break; 1842aa4dd815SMatthias Ringwald default: 1843aa4dd815SMatthias Ringwald break; 1844aa4dd815SMatthias Ringwald } 1845aa4dd815SMatthias Ringwald break; 1846aa4dd815SMatthias Ringwald 18479ff73f41SMatthias Ringwald case HFP_AG_OUTGOING_CALL_INITIATED_BY_HF: 1848d210d9c4SMatthias Ringwald // directly reject call if number of free slots is exceeded 1849d210d9c4SMatthias Ringwald if (!hfp_gsm_call_possible()){ 1850e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 1851f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1852d210d9c4SMatthias Ringwald break; 1853d210d9c4SMatthias Ringwald } 1854fe899794SMatthias Ringwald 1855fe899794SMatthias Ringwald // note: number not used currently 18569ff73f41SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_INITIATED_BY_HF, 0, 0, (const char *) &hfp_connection->line_buffer[3]); 18579cae807eSMatthias Ringwald 1858a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 185974386ee0SMatthias Ringwald 1860ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]); 1861aa4dd815SMatthias Ringwald break; 1862aa4dd815SMatthias Ringwald 1863fe899794SMatthias Ringwald case HFP_AG_OUTGOING_CALL_INITIATED_BY_AG: 1864fe899794SMatthias Ringwald // directly reject call if number of free slots is exceeded 1865fe899794SMatthias Ringwald if (!hfp_gsm_call_possible()) { 1866fe899794SMatthias Ringwald // TODO: no error for user 1867fe899794SMatthias Ringwald break; 1868fe899794SMatthias Ringwald } 1869fe899794SMatthias Ringwald switch (hfp_gsm_call_status()) { 1870fe899794SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1871fe899794SMatthias Ringwald switch (hfp_gsm_callsetup_status()) { 1872fe899794SMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1873fe899794SMatthias Ringwald // note: number not used currently 1874fe899794SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_INITIATED_BY_AG, 0, 0, "1234567"); 1875fe899794SMatthias Ringwald // init call state for all connections 1876fe899794SMatthias Ringwald hfp_ag_trigger_outgoing_call(); 1877fe899794SMatthias Ringwald // get first one and accept call 1878fe899794SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 1879fe899794SMatthias Ringwald if (hfp_connection) { 1880fe899794SMatthias Ringwald hfp_ag_handle_outgoing_call_accepted(hfp_connection); 1881fe899794SMatthias Ringwald } 1882fe899794SMatthias Ringwald break; 1883fe899794SMatthias Ringwald default: 1884fe899794SMatthias Ringwald // TODO: no error for user 1885fe899794SMatthias Ringwald break; 1886fe899794SMatthias Ringwald } 1887fe899794SMatthias Ringwald break; 1888fe899794SMatthias Ringwald default: 1889fe899794SMatthias Ringwald // TODO: no error for user 1890fe899794SMatthias Ringwald break; 1891fe899794SMatthias Ringwald } 1892fe899794SMatthias Ringwald break; 18939cae807eSMatthias Ringwald case HFP_AG_OUTGOING_REDIAL_INITIATED:{ 1894d210d9c4SMatthias Ringwald // directly reject call if number of free slots is exceeded 1895d210d9c4SMatthias Ringwald if (!hfp_gsm_call_possible()){ 1896e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 1897f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1898d210d9c4SMatthias Ringwald break; 1899d210d9c4SMatthias Ringwald } 1900d210d9c4SMatthias Ringwald 1901f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_REDIAL_INITIATED, 0, 0, NULL); 1902a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 190374386ee0SMatthias Ringwald 190460ebb071SMilanka Ringwald log_info("Redial last number"); 19059cae807eSMatthias Ringwald char * last_dialed_number = hfp_gsm_last_dialed_number(); 1906aa4dd815SMatthias Ringwald 19079cae807eSMatthias Ringwald if (strlen(last_dialed_number) > 0){ 190860ebb071SMilanka Ringwald log_info("Last number exists: accept call"); 1909ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number); 19109cae807eSMatthias Ringwald } else { 191160ebb071SMilanka Ringwald log_info("log_infoLast number missing: reject call"); 19129f9621cfSMatthias Ringwald hfp_ag_handle_reject_outgoing_call(); 19139cae807eSMatthias Ringwald } 19149cae807eSMatthias Ringwald break; 19159cae807eSMatthias Ringwald } 1916aa4dd815SMatthias Ringwald case HFP_AG_OUTGOING_CALL_REJECTED: 19179f9621cfSMatthias Ringwald hfp_ag_handle_reject_outgoing_call(); 1918aa4dd815SMatthias Ringwald break; 1919aa4dd815SMatthias Ringwald 1920d210d9c4SMatthias Ringwald case HFP_AG_OUTGOING_CALL_ACCEPTED:{ 1921a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 1922a0ffb263SMatthias Ringwald if (!hfp_connection){ 1923a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state"); 1924aa4dd815SMatthias Ringwald break; 1925aa4dd815SMatthias Ringwald } 1926aa4dd815SMatthias Ringwald 1927e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 19285a7033e2SMatthias Ringwald hfp_ag_handle_outgoing_call_accepted(hfp_connection); 1929aa4dd815SMatthias Ringwald break; 1930d210d9c4SMatthias Ringwald } 1931aa4dd815SMatthias Ringwald case HFP_AG_OUTGOING_CALL_RINGING: 1932a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1933a0ffb263SMatthias Ringwald if (!hfp_connection){ 1934a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state"); 1935aa4dd815SMatthias Ringwald break; 1936aa4dd815SMatthias Ringwald } 1937d210d9c4SMatthias Ringwald 1938f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_RINGING, 0, 0, NULL); 1939a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING; 1940d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1941aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1942fcd59412SMilanka Ringwald hfp_ag_hf_start_ringing_outgoing(hfp_connection); 1943aa4dd815SMatthias Ringwald break; 1944aa4dd815SMatthias Ringwald 1945d210d9c4SMatthias Ringwald case HFP_AG_OUTGOING_CALL_ESTABLISHED:{ 1946aa4dd815SMatthias Ringwald // get outgoing call 1947a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING); 1948a0ffb263SMatthias Ringwald if (!hfp_connection){ 1949a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1950aa4dd815SMatthias Ringwald } 1951a0ffb263SMatthias Ringwald if (!hfp_connection){ 1952a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection"); 1953aa4dd815SMatthias Ringwald break; 1954aa4dd815SMatthias Ringwald } 1955d210d9c4SMatthias Ringwald 1956addc49c4SMilanka Ringwald bool callheld_status_call_on_hold_and_no_active_calls = hfp_gsm_callheld_status() == HFP_CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS; 1957f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_ESTABLISHED, 0, 0, NULL); 1958a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1959d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1960d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1961aa4dd815SMatthias Ringwald hfp_ag_transfer_call_state(); 1962aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1963addc49c4SMilanka Ringwald if (callheld_status_call_on_hold_and_no_active_calls){ 1964d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1965aa4dd815SMatthias Ringwald hfp_ag_transfer_callheld_state(); 19663deb3ec6SMatthias Ringwald } 1967addc49c4SMilanka Ringwald hfp_ag_hf_stop_ringing(hfp_connection); 1968674ebed5SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED); 19693deb3ec6SMatthias Ringwald break; 1970d210d9c4SMatthias Ringwald } 1971d210d9c4SMatthias Ringwald 197212cbbeeeSMatthias Ringwald case HFP_AG_CALL_HOLD_USER_BUSY: 1973f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_USER_BUSY, 0, 0, NULL); 1974d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1975a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1976a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 197760ebb071SMilanka Ringwald log_info("AG: Call Waiting, User Busy"); 1978d210d9c4SMatthias Ringwald break; 1979d210d9c4SMatthias Ringwald 1980d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{ 1981d0c20769SMatthias Ringwald int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1982d0c20769SMatthias Ringwald int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD; 1983d210d9c4SMatthias Ringwald 1984d210d9c4SMatthias Ringwald // Releases all active calls (if any exist) and accepts the other (held or waiting) call. 1985d0c20769SMatthias Ringwald if (call_held || call_setup_in_progress){ 1986f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index, 1987f8737b81SMatthias Ringwald 0, NULL); 1988d0c20769SMatthias Ringwald 1989d0c20769SMatthias Ringwald } 1990d0c20769SMatthias Ringwald 1991d210d9c4SMatthias Ringwald if (call_setup_in_progress){ 199260ebb071SMilanka Ringwald log_info("AG: Call Dropped, Accept new call"); 1993d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1994a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1995d210d9c4SMatthias Ringwald } else { 199660ebb071SMilanka Ringwald log_info("AG: Call Dropped, Resume held call"); 1997d210d9c4SMatthias Ringwald } 1998d210d9c4SMatthias Ringwald if (call_held){ 1999d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 2000a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 2001d210d9c4SMatthias Ringwald } 2002d0c20769SMatthias Ringwald 2003a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 2004d210d9c4SMatthias Ringwald break; 2005d210d9c4SMatthias Ringwald } 2006d0c20769SMatthias Ringwald 2007d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{ 2008d210d9c4SMatthias Ringwald // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call. 2009d210d9c4SMatthias Ringwald // only update if callsetup changed 2010d0c20769SMatthias Ringwald int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 2011f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index, 0, 2012f8737b81SMatthias Ringwald NULL); 2013d0c20769SMatthias Ringwald 2014d210d9c4SMatthias Ringwald if (call_setup_in_progress){ 201560ebb071SMilanka Ringwald log_info("AG: Call on Hold, Accept new call"); 2016d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 2017a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 2018d210d9c4SMatthias Ringwald } else { 201960ebb071SMilanka Ringwald log_info("AG: Swap calls"); 2020d210d9c4SMatthias Ringwald } 2021d0c20769SMatthias Ringwald 2022d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 2023d0c20769SMatthias Ringwald // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED); 2024a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 2025a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 2026d210d9c4SMatthias Ringwald break; 2027d210d9c4SMatthias Ringwald } 2028d0c20769SMatthias Ringwald 2029d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_ADD_HELD_CALL: 2030d210d9c4SMatthias Ringwald // Adds a held call to the conversation. 2031d0c20769SMatthias Ringwald if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){ 203260ebb071SMilanka Ringwald log_info("AG: Join 3-way-call"); 2033f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_ADD_HELD_CALL, 0, 0, NULL); 2034d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 2035a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 2036ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL); 2037d210d9c4SMatthias Ringwald } 2038a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 2039d210d9c4SMatthias Ringwald break; 2040d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS: 2041d210d9c4SMatthias Ringwald // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer) 2042f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, 0, 0, NULL); 204360ebb071SMilanka Ringwald log_info("AG: Transfer call -> Connect two calls and disconnect"); 2044d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 2045d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 2046a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 2047a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 2048a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 2049d210d9c4SMatthias Ringwald break; 2050ce263fc8SMatthias Ringwald 20513deb3ec6SMatthias Ringwald default: 20523deb3ec6SMatthias Ringwald break; 20533deb3ec6SMatthias Ringwald } 2054d210d9c4SMatthias Ringwald 2055d0c20769SMatthias Ringwald 20563deb3ec6SMatthias Ringwald } 20573deb3ec6SMatthias Ringwald 20589cae807eSMatthias Ringwald 2059a0ffb263SMatthias Ringwald static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){ 20609cae807eSMatthias Ringwald hfp_gsm_call_t * active_call = hfp_gsm_call(call_index); 20619cae807eSMatthias Ringwald if (!active_call) return; 20629cae807eSMatthias Ringwald 20639cae807eSMatthias Ringwald int idx = active_call->index; 20649cae807eSMatthias Ringwald hfp_enhanced_call_dir_t dir = active_call->direction; 20659cae807eSMatthias Ringwald hfp_enhanced_call_status_t status = active_call->enhanced_status; 20669cae807eSMatthias Ringwald hfp_enhanced_call_mode_t mode = active_call->mode; 20679cae807eSMatthias Ringwald hfp_enhanced_call_mpty_t mpty = active_call->mpty; 20689cae807eSMatthias Ringwald uint8_t type = active_call->clip_type; 20699cae807eSMatthias Ringwald char * number = active_call->clip_number; 20709cae807eSMatthias Ringwald 20719cae807eSMatthias Ringwald char buffer[100]; 20729cae807eSMatthias Ringwald // TODO: check length of a buffer, to fit the MTU 20739cae807eSMatthias Ringwald int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty); 20749cae807eSMatthias Ringwald if (number){ 20751cc1d9e9SMilanka Ringwald offset += snprintf(buffer+offset, sizeof(buffer)-offset-3, ", \"%s\",%u", number, type); 20769cae807eSMatthias Ringwald } 20779cae807eSMatthias Ringwald snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 207860ebb071SMilanka Ringwald log_info("hfp_ag_send_current_call_status 000 index %d, dir %d, status %d, mode %d, mpty %d, type %d, number %s", idx, dir, status, 20799cae807eSMatthias Ringwald mode, mpty, type, number); 2080a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 20819cae807eSMatthias Ringwald } 20829cae807eSMatthias Ringwald 2083febc14f5SMatthias Ringwald // sends pending command, returns if command was sent 2084febc14f5SMatthias Ringwald static int hfp_ag_send_commands(hfp_connection_t *hfp_connection){ 2085a0ffb263SMatthias Ringwald if (hfp_connection->send_status_of_current_calls){ 2086a0ffb263SMatthias Ringwald if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){ 2087a0ffb263SMatthias Ringwald hfp_connection->next_call_index++; 2088a0ffb263SMatthias Ringwald hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index); 2089febc14f5SMatthias Ringwald return 1; 20909cae807eSMatthias Ringwald } else { 2091febc14f5SMatthias Ringwald // TODO: this code should be removed. check a) before setting send_status_of_current_calls, or b) right before hfp_ag_send_call_status above 2092a0ffb263SMatthias Ringwald hfp_connection->next_call_index = 0; 2093e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2094a0ffb263SMatthias Ringwald hfp_connection->send_status_of_current_calls = 0; 20959cae807eSMatthias Ringwald } 2096ce263fc8SMatthias Ringwald } 2097ce263fc8SMatthias Ringwald 2098a0ffb263SMatthias Ringwald if (hfp_connection->ag_notify_incoming_call_waiting){ 2099a0ffb263SMatthias Ringwald hfp_connection->ag_notify_incoming_call_waiting = 0; 2100a0ffb263SMatthias Ringwald hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid); 2101febc14f5SMatthias Ringwald return 1; 2102aa4dd815SMatthias Ringwald } 2103aa4dd815SMatthias Ringwald 21045e87d1ceSMatthias Ringwald if (hfp_connection->send_error > 0){ 21055e87d1ceSMatthias Ringwald hfp_connection->send_error--; 2106a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2107485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 2108febc14f5SMatthias Ringwald return 1; 2109aa4dd815SMatthias Ringwald } 2110aa4dd815SMatthias Ringwald 2111ce263fc8SMatthias Ringwald // note: before update AG indicators and ok_pending 2112a0ffb263SMatthias Ringwald if (hfp_connection->send_response_and_hold_status){ 2113a0ffb263SMatthias Ringwald int status = hfp_connection->send_response_and_hold_status - 1; 2114a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = 0; 2115485ac19eSMilanka Ringwald hfp_ag_send_set_response_and_hold(hfp_connection->rfcomm_cid, status); 2116febc14f5SMatthias Ringwald return 1; 2117ce263fc8SMatthias Ringwald } 2118ce263fc8SMatthias Ringwald 21195be6b47aSMatthias Ringwald // note: before ok_pending and send_error to allow for unsolicited result on custom command 21205be6b47aSMatthias Ringwald if (hfp_connection->send_custom_message != NULL){ 21215be6b47aSMatthias Ringwald const char * message = hfp_connection->send_custom_message; 21225be6b47aSMatthias Ringwald hfp_connection->send_custom_message = NULL; 21235be6b47aSMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, message); 21247f8f1191SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_CUSTOM_AT_MESSAGE_SENT, ERROR_CODE_SUCCESS); 21255be6b47aSMatthias Ringwald return 1; 21265be6b47aSMatthias Ringwald } 21275be6b47aSMatthias Ringwald 21285e87d1ceSMatthias Ringwald if (hfp_connection->ok_pending > 0){ 21295e87d1ceSMatthias Ringwald hfp_connection->ok_pending--; 2130a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2131485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 2132febc14f5SMatthias Ringwald return 1; 2133ce263fc8SMatthias Ringwald } 2134ce263fc8SMatthias Ringwald 213569640b04SMatthias Ringwald // update AG indicators only if enabled by AT+CMER=3,0,0,1 213669640b04SMatthias Ringwald if ((hfp_connection->enable_status_update_for_ag_indicators == 1) && (hfp_connection->ag_indicators_status_update_bitmap != 0)){ 213769640b04SMatthias Ringwald uint16_t i; 2138a0ffb263SMatthias Ringwald for (i=0;i<hfp_connection->ag_indicators_nr;i++){ 2139a0ffb263SMatthias Ringwald if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){ 2140a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0); 2141485ac19eSMilanka Ringwald hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]); 2142febc14f5SMatthias Ringwald return 1; 2143aa4dd815SMatthias Ringwald } 2144aa4dd815SMatthias Ringwald } 2145aa4dd815SMatthias Ringwald } 2146aa4dd815SMatthias Ringwald 2147245852b7SMilanka Ringwald if (hfp_connection->ag_send_no_carrier){ 2148245852b7SMilanka Ringwald hfp_connection->ag_send_no_carrier = false; 2149245852b7SMilanka Ringwald hfp_ag_send_no_carrier(hfp_connection->rfcomm_cid); 2150245852b7SMilanka Ringwald return 1; 2151245852b7SMilanka Ringwald } 2152245852b7SMilanka Ringwald 2153a0ffb263SMatthias Ringwald if (hfp_connection->send_phone_number_for_voice_tag){ 2154a0ffb263SMatthias Ringwald hfp_connection->send_phone_number_for_voice_tag = 0; 2155a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2156e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2157a0ffb263SMatthias Ringwald hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid); 2158febc14f5SMatthias Ringwald return 1; 2159aa4dd815SMatthias Ringwald } 2160aa4dd815SMatthias Ringwald 2161a0ffb263SMatthias Ringwald if (hfp_connection->send_subscriber_number){ 2162aeb0f0feSMatthias Ringwald if (hfp_connection->next_subscriber_number_to_send < hfp_ag_subscriber_numbers_count){ 2163aeb0f0feSMatthias Ringwald hfp_phone_number_t phone = hfp_ag_subscriber_numbers[hfp_connection->next_subscriber_number_to_send++]; 2164a0ffb263SMatthias Ringwald hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number); 2165ce263fc8SMatthias Ringwald } else { 2166a0ffb263SMatthias Ringwald hfp_connection->send_subscriber_number = 0; 2167a0ffb263SMatthias Ringwald hfp_connection->next_subscriber_number_to_send = 0; 2168485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 2169ce263fc8SMatthias Ringwald } 2170a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2171febc14f5SMatthias Ringwald return 1; 2172ce263fc8SMatthias Ringwald } 2173ce263fc8SMatthias Ringwald 2174a0ffb263SMatthias Ringwald if (hfp_connection->send_microphone_gain){ 2175a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 0; 2176a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2177485ac19eSMilanka Ringwald hfp_ag_send_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 2178febc14f5SMatthias Ringwald return 1; 2179aa4dd815SMatthias Ringwald } 2180aa4dd815SMatthias Ringwald 2181a0ffb263SMatthias Ringwald if (hfp_connection->send_speaker_gain){ 2182a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 0; 2183a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2184485ac19eSMilanka Ringwald hfp_ag_send_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 2185febc14f5SMatthias Ringwald return 1; 21863deb3ec6SMatthias Ringwald } 21873deb3ec6SMatthias Ringwald 2188a0ffb263SMatthias Ringwald if (hfp_connection->send_ag_status_indicators){ 2189a0ffb263SMatthias Ringwald hfp_connection->send_ag_status_indicators = 0; 2190485ac19eSMilanka Ringwald hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid); 2191febc14f5SMatthias Ringwald return 1; 2192febc14f5SMatthias Ringwald } 2193febc14f5SMatthias Ringwald 219410ae9dfcSMatthias Ringwald if (hfp_connection->extended_audio_gateway_error){ 219510ae9dfcSMatthias Ringwald uint8_t extended_audio_gateway_error = hfp_connection->extended_audio_gateway_error; 219610ae9dfcSMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 219710ae9dfcSMatthias Ringwald hfp_ag_send_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, extended_audio_gateway_error); 219810ae9dfcSMatthias Ringwald 219910ae9dfcSMatthias Ringwald } 2200febc14f5SMatthias Ringwald return 0; 2201febc14f5SMatthias Ringwald } 2202febc14f5SMatthias Ringwald 2203c1eef992SMatthias Ringwald // sends pending command, returns if command was sent 2204c1eef992SMatthias Ringwald static int hfp_ag_run_ring_and_clip(hfp_connection_t *hfp_connection){ 2205c1eef992SMatthias Ringwald // delay RING/CLIP until audio connection has been established for incoming calls 2206c1eef992SMatthias Ringwald // hfp_ag_hf_trigger_ring_and_clip is called in call_setup_state_machine 2207c1eef992SMatthias Ringwald if (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING){ 2208c1eef992SMatthias Ringwald if (hfp_connection->ag_ring){ 2209c1eef992SMatthias Ringwald hfp_connection->ag_ring = 0; 2210c1eef992SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2211c1eef992SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING); 2212c1eef992SMatthias Ringwald hfp_ag_send_ring(hfp_connection->rfcomm_cid); 2213c1eef992SMatthias Ringwald return 1; 2214c1eef992SMatthias Ringwald } 2215c1eef992SMatthias Ringwald 2216c1eef992SMatthias Ringwald if (hfp_connection->ag_send_clip){ 2217c1eef992SMatthias Ringwald hfp_connection->ag_send_clip = 0; 2218c1eef992SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2219c1eef992SMatthias Ringwald hfp_ag_send_clip(hfp_connection->rfcomm_cid); 2220c1eef992SMatthias Ringwald return 1; 2221c1eef992SMatthias Ringwald } 2222c1eef992SMatthias Ringwald } 2223c1eef992SMatthias Ringwald return 0; 2224c1eef992SMatthias Ringwald } 2225c1eef992SMatthias Ringwald 2226febc14f5SMatthias Ringwald static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection){ 2227febc14f5SMatthias Ringwald 222876cc1527SMatthias Ringwald btstack_assert(hfp_connection != NULL); 222976cc1527SMatthias Ringwald btstack_assert(hfp_connection->local_role == HFP_ROLE_AG); 2230febc14f5SMatthias Ringwald 223176cc1527SMatthias Ringwald // during SDP query, RFCOMM CID is not set 223276cc1527SMatthias Ringwald if (hfp_connection->rfcomm_cid == 0) return; 2233ce263fc8SMatthias Ringwald 223484fb9ac1SMilanka Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->emit_vra_enabled_after_audio_established){ 223584fb9ac1SMilanka Ringwald hfp_connection->emit_vra_enabled_after_audio_established = false; 223684fb9ac1SMilanka Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS); 223784fb9ac1SMilanka Ringwald } 223884fb9ac1SMilanka Ringwald 2239f9f3ba28SMilanka Ringwald if ((hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) && hfp_connection->release_audio_connection){ 2240f9f3ba28SMilanka Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 2241f9f3ba28SMilanka Ringwald hfp_connection->release_audio_connection = 0; 2242f9f3ba28SMilanka Ringwald gap_disconnect(hfp_connection->sco_handle); 2243f9f3ba28SMilanka Ringwald return; 2244f9f3ba28SMilanka Ringwald } 2245f9f3ba28SMilanka Ringwald 2246b7802cc8SMatthias Ringwald // configure NBS/WBS if needed using vendor-specific HCI commands 2247b7802cc8SMatthias Ringwald if (hci_can_send_command_packet_now()) { 22483721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP 22493721a235SMatthias Ringwald // WBS Disassociate 22503721a235SMatthias Ringwald if (hfp_connection->cc256x_send_wbs_disassociate){ 22513721a235SMatthias Ringwald hfp_connection->cc256x_send_wbs_disassociate = false; 22523721a235SMatthias Ringwald hci_send_cmd(&hci_ti_wbs_disassociate); 22533721a235SMatthias Ringwald return; 22543721a235SMatthias Ringwald } 22553721a235SMatthias Ringwald // Write Codec Config 22563721a235SMatthias Ringwald if (hfp_connection->cc256x_send_write_codec_config){ 22573721a235SMatthias Ringwald hfp_connection->cc256x_send_write_codec_config = false; 22583721a235SMatthias Ringwald hfp_cc256x_write_codec_config(hfp_connection); 22593721a235SMatthias Ringwald return; 22603721a235SMatthias Ringwald } 22613721a235SMatthias Ringwald // WBS Associate 22623721a235SMatthias Ringwald if (hfp_connection->cc256x_send_wbs_associate){ 22633721a235SMatthias Ringwald hfp_connection->cc256x_send_wbs_associate = false; 22643721a235SMatthias Ringwald hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle); 22653721a235SMatthias Ringwald return; 22663721a235SMatthias Ringwald } 22673721a235SMatthias Ringwald #endif 2268689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 2269689d4323SMatthias Ringwald // Enable WBS 2270689d4323SMatthias Ringwald if (hfp_connection->bcm_send_enable_wbs){ 2271689d4323SMatthias Ringwald hfp_connection->bcm_send_enable_wbs = false; 2272689d4323SMatthias Ringwald hci_send_cmd(&hci_bcm_enable_wbs, 1, 2); 2273689d4323SMatthias Ringwald return; 2274689d4323SMatthias Ringwald } 2275689d4323SMatthias Ringwald // Write I2S/PCM params 2276689d4323SMatthias Ringwald if (hfp_connection->bcm_send_write_i2spcm_interface_param){ 2277689d4323SMatthias Ringwald hfp_connection->bcm_send_write_i2spcm_interface_param = false; 2278689d4323SMatthias Ringwald hfp_bcm_write_i2spcm_interface_param(hfp_connection); 2279689d4323SMatthias Ringwald return; 2280689d4323SMatthias Ringwald } 2281689d4323SMatthias Ringwald // Disable WBS 2282689d4323SMatthias Ringwald if (hfp_connection->bcm_send_disable_wbs){ 2283689d4323SMatthias Ringwald hfp_connection->bcm_send_disable_wbs = false; 2284689d4323SMatthias Ringwald hci_send_cmd(&hci_bcm_enable_wbs, 0, 2); 2285689d4323SMatthias Ringwald return; 2286689d4323SMatthias Ringwald } 2287689d4323SMatthias Ringwald #endif 22882b5f92fdSMatthias Ringwald #ifdef ENABLE_RTK_PCM_WBS 22892b5f92fdSMatthias Ringwald // Configure CVSD vs. mSBC 22902b5f92fdSMatthias Ringwald if (hfp_connection->rtk_send_sco_config){ 22912b5f92fdSMatthias Ringwald hfp_connection->rtk_send_sco_config = false; 22922b5f92fdSMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 22932b5f92fdSMatthias Ringwald log_info("RTK SCO: 16k + mSBC"); 22942b5f92fdSMatthias Ringwald hci_send_cmd(&hci_rtk_configure_sco_routing, 0x81, 0x90, 0x00, 0x00, 0x1a, 0x0c, 0x00, 0x00, 0x41); 22952b5f92fdSMatthias Ringwald } else { 22962b5f92fdSMatthias Ringwald log_info("RTK SCO: 16k + CVSD"); 22972b5f92fdSMatthias Ringwald hci_send_cmd(&hci_rtk_configure_sco_routing, 0x81, 0x90, 0x00, 0x00, 0x1a, 0x0c, 0x0c, 0x00, 0x01); 22982b5f92fdSMatthias Ringwald } 22992b5f92fdSMatthias Ringwald return; 23002b5f92fdSMatthias Ringwald } 23012b5f92fdSMatthias Ringwald #endif 23025fd6f360SMatthias Ringwald #ifdef ENABLE_NXP_PCM_WBS 23035fd6f360SMatthias Ringwald if (hfp_connection->nxp_start_audio_handle != HCI_CON_HANDLE_INVALID){ 23045fd6f360SMatthias Ringwald hci_con_handle_t sco_handle = hfp_connection->nxp_start_audio_handle; 23055fd6f360SMatthias Ringwald hfp_connection->nxp_start_audio_handle = HCI_CON_HANDLE_INVALID; 23065fd6f360SMatthias Ringwald hci_send_cmd(&hci_nxp_host_pcm_i2s_audio_config, 0, 0, sco_handle, 0); 23075fd6f360SMatthias Ringwald return; 23085fd6f360SMatthias Ringwald } 23095fd6f360SMatthias Ringwald if (hfp_connection->nxp_stop_audio_handle != HCI_CON_HANDLE_INVALID){ 23105fd6f360SMatthias Ringwald hci_con_handle_t sco_handle = hfp_connection->nxp_stop_audio_handle; 23115fd6f360SMatthias Ringwald hfp_connection->nxp_stop_audio_handle = HCI_CON_HANDLE_INVALID; 23125fd6f360SMatthias Ringwald hci_send_cmd(&hci_nxp_host_pcm_i2s_audio_config, 1, 0, sco_handle, 0); 23135fd6f360SMatthias Ringwald return; 23145fd6f360SMatthias Ringwald } 23155fd6f360SMatthias Ringwald #endif 2316b7802cc8SMatthias Ringwald } 2317b7802cc8SMatthias Ringwald 231848e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS) 231948e6eeeeSMatthias Ringwald if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){ 232048e6eeeeSMatthias Ringwald hfp_finalize_connection_context(hfp_connection); 232148e6eeeeSMatthias Ringwald return; 232248e6eeeeSMatthias Ringwald } 232348e6eeeeSMatthias Ringwald #endif 23243721a235SMatthias Ringwald 2325febc14f5SMatthias Ringwald if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) { 2326febc14f5SMatthias Ringwald log_info("hfp_ag_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid); 2327febc14f5SMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 2328febc14f5SMatthias Ringwald return; 2329febc14f5SMatthias Ringwald } 2330febc14f5SMatthias Ringwald 2331febc14f5SMatthias Ringwald int cmd_sent = hfp_ag_send_commands(hfp_connection); 2332febc14f5SMatthias Ringwald 2333febc14f5SMatthias Ringwald if (!cmd_sent){ 2334febc14f5SMatthias Ringwald cmd_sent = hfp_ag_run_for_context_service_level_connection(hfp_connection); 2335febc14f5SMatthias Ringwald } 2336febc14f5SMatthias Ringwald 2337c04cf7ffSMilanka Ringwald if (!cmd_sent){ 2338c04cf7ffSMilanka Ringwald cmd_sent = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection); 23393deb3ec6SMatthias Ringwald } 23403deb3ec6SMatthias Ringwald 2341c04cf7ffSMilanka Ringwald if (!cmd_sent){ 2342c04cf7ffSMilanka Ringwald cmd_sent = call_setup_state_machine(hfp_connection); 2343aa4dd815SMatthias Ringwald } 2344aa4dd815SMatthias Ringwald 2345b956fff3SMatthias Ringwald if (!cmd_sent){ 2346b956fff3SMatthias Ringwald cmd_sent = hfp_ag_run_for_audio_connection(hfp_connection); 2347b956fff3SMatthias Ringwald } 2348b956fff3SMatthias Ringwald 2349c95b5b3cSMilanka Ringwald if (!cmd_sent){ 2350c1eef992SMatthias Ringwald cmd_sent = hfp_ag_run_ring_and_clip(hfp_connection); 2351c1eef992SMatthias Ringwald } 2352c1eef992SMatthias Ringwald 2353c1eef992SMatthias Ringwald if (!cmd_sent){ 2354c95b5b3cSMilanka Ringwald cmd_sent = hfp_ag_voice_recognition_state_machine(hfp_connection); 2355c95b5b3cSMilanka Ringwald } 2356b956fff3SMatthias Ringwald 2357d0a0eceeSMatthias Ringwald // disconnect 2358d0a0eceeSMatthias Ringwald if (!cmd_sent && (hfp_connection->command == HFP_CMD_NONE) && (hfp_connection->state == HFP_W2_DISCONNECT_RFCOMM)){ 2359d0a0eceeSMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 2360d0a0eceeSMatthias Ringwald rfcomm_disconnect(hfp_connection->rfcomm_cid); 2361d0a0eceeSMatthias Ringwald } 2362d0a0eceeSMatthias Ringwald 2363c04cf7ffSMilanka Ringwald if (cmd_sent){ 2364a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2365473ac565SMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 2366473ac565SMatthias Ringwald } 23673deb3ec6SMatthias Ringwald } 2368d68dcce1SMatthias Ringwald 2369e0d09929SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){ 2370c1ab6cc1SMatthias Ringwald return (byte == '\n') || (byte == '\r'); 2371e0d09929SMatthias Ringwald } 2372e0d09929SMatthias Ringwald 2373fdf18f86SMilanka Ringwald static void hfp_ag_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){ 23748a46ec40SMatthias Ringwald // assertion: size >= 1 as rfcomm.c does not deliver empty packets 2375fdf18f86SMilanka Ringwald if (size < 1) return; 2376347d46c8SMilanka Ringwald uint8_t status = ERROR_CODE_SUCCESS; 23771e35c04dSMatthias Ringwald 2378186dd3d2SMatthias Ringwald hfp_log_rfcomm_message("HFP_AG_RX", packet, size); 2379e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES 2380e43d1938SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet); 2381e43d1938SMatthias Ringwald #endif 23821e35c04dSMatthias Ringwald 23838a46ec40SMatthias Ringwald // process messages byte-wise 23843deb3ec6SMatthias Ringwald int pos; 23853deb3ec6SMatthias Ringwald for (pos = 0; pos < size ; pos++){ 2386a0ffb263SMatthias Ringwald hfp_parse(hfp_connection, packet[pos], 0); 2387186dd3d2SMatthias Ringwald 2388e0d09929SMatthias Ringwald // parse until end of line 2389e0d09929SMatthias Ringwald if (!hfp_parser_is_end_of_line(packet[pos])) continue; 2390e0d09929SMatthias Ringwald 2391186dd3d2SMatthias Ringwald hfp_generic_status_indicator_t * indicator; 2392a0ffb263SMatthias Ringwald switch(hfp_connection->command){ 239345796ff1SMilanka Ringwald case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION: 2394e7c46708SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2395e7c46708SMatthias Ringwald hfp_connection->ag_vra_requested_by_hf = true; 239645796ff1SMilanka Ringwald break; 2397ce263fc8SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_QUERY: 2398fd66594dSMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2399ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_active){ 2400a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1; 2401ce263fc8SMatthias Ringwald } 2402e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2403ce263fc8SMatthias Ringwald break; 2404ce263fc8SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_COMMAND: 2405fd66594dSMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 24060222a807SMatthias Ringwald switch(hfp_connection->ag_response_and_hold_action){ 2407ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD: 2408a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection); 2409ce263fc8SMatthias Ringwald break; 2410ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED: 2411a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection); 2412ce263fc8SMatthias Ringwald break; 2413ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED: 2414a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection); 2415ce263fc8SMatthias Ringwald break; 2416ce263fc8SMatthias Ringwald default: 2417ce263fc8SMatthias Ringwald break; 2418ce263fc8SMatthias Ringwald } 2419e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2420ce263fc8SMatthias Ringwald break; 2421ce263fc8SMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 2422a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2423347d46c8SMilanka Ringwald 2424aeb0f0feSMatthias Ringwald if (hfp_connection->parser_indicator_index < hfp_ag_generic_status_indicators_nr){ 2425aeb0f0feSMatthias Ringwald indicator = &hfp_ag_generic_status_indicators[hfp_connection->parser_indicator_index]; 2426ce263fc8SMatthias Ringwald switch (indicator->uuid){ 2427347d46c8SMilanka Ringwald case HFP_HF_INDICATOR_UUID_ENHANCED_SAFETY: 24280222a807SMatthias Ringwald if (hfp_connection->parser_indicator_value > 1) { 2429e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 2430ce263fc8SMatthias Ringwald break; 2431ce263fc8SMatthias Ringwald } 2432e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2433585db78dSMilanka Ringwald hfp_ag_emit_hf_indicator_value(hfp_connection, indicator->uuid, hfp_connection->parser_indicator_value); 2434347d46c8SMilanka Ringwald break; 2435347d46c8SMilanka Ringwald case HFP_HF_INDICATOR_UUID_BATTERY_LEVEL: 2436347d46c8SMilanka Ringwald if (hfp_connection->parser_indicator_value > 100){ 2437e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 2438347d46c8SMilanka Ringwald break; 2439347d46c8SMilanka Ringwald } 2440e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2441585db78dSMilanka Ringwald hfp_ag_emit_hf_indicator_value(hfp_connection, indicator->uuid, hfp_connection->parser_indicator_value); 2442347d46c8SMilanka Ringwald break; 2443347d46c8SMilanka Ringwald default: 2444e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 2445347d46c8SMilanka Ringwald break; 2446347d46c8SMilanka Ringwald } 2447347d46c8SMilanka Ringwald } else { 2448e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 2449347d46c8SMilanka Ringwald } 2450ce263fc8SMatthias Ringwald break; 2451ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 2452ce263fc8SMatthias Ringwald // expected by SLC state machine 2453a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break; 2454a0ffb263SMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0; 2455a0ffb263SMatthias Ringwald hfp_connection->send_ag_status_indicators = 1; 2456ce263fc8SMatthias Ringwald break; 2457ce263fc8SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 2458a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2459a0ffb263SMatthias Ringwald hfp_connection->next_call_index = 0; 2460a0ffb263SMatthias Ringwald hfp_connection->send_status_of_current_calls = 1; 2461ce263fc8SMatthias Ringwald break; 2462ce263fc8SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 2463fd66594dSMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2464aeb0f0feSMatthias Ringwald if (hfp_ag_subscriber_numbers_count == 0){ 2465485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 2466ce263fc8SMatthias Ringwald break; 2467ce263fc8SMatthias Ringwald } 2468a0ffb263SMatthias Ringwald hfp_connection->next_subscriber_number_to_send = 0; 2469a0ffb263SMatthias Ringwald hfp_connection->send_subscriber_number = 1; 2470ce263fc8SMatthias Ringwald break; 2471c1797c7dSMatthias Ringwald case HFP_CMD_TRANSMIT_DTMF_CODES: 24720222a807SMatthias Ringwald { 2473a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 24740222a807SMatthias Ringwald char buffer[2]; 24750222a807SMatthias Ringwald buffer[0] = (char) hfp_connection->ag_dtmf_code; 24760222a807SMatthias Ringwald buffer[1] = 0; 24770222a807SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, buffer); 2478c1797c7dSMatthias Ringwald break; 24790222a807SMatthias Ringwald } 2480aa4dd815SMatthias Ringwald case HFP_CMD_HF_REQUEST_PHONE_NUMBER: 2481a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2482ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG); 2483aa4dd815SMatthias Ringwald break; 2484aa4dd815SMatthias Ringwald case HFP_CMD_TURN_OFF_EC_AND_NR: 2485a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2486347d46c8SMilanka Ringwald 2487aeb0f0feSMatthias Ringwald if (get_bit(hfp_ag_supported_features, HFP_AGSF_EC_NR_FUNCTION)){ 2488e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2489347d46c8SMilanka Ringwald status = ERROR_CODE_SUCCESS; 2490aa4dd815SMatthias Ringwald } else { 2491e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 2492347d46c8SMilanka Ringwald status = ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 2493aa4dd815SMatthias Ringwald } 2494347d46c8SMilanka Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE, status); 2495aa4dd815SMatthias Ringwald break; 2496aa4dd815SMatthias Ringwald case HFP_CMD_CALL_ANSWERED: 2497a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 249860ebb071SMilanka Ringwald log_info("HFP: ATA"); 2499a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection); 2500aa4dd815SMatthias Ringwald break; 2501aa4dd815SMatthias Ringwald case HFP_CMD_HANG_UP_CALL: 2502a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2503e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2504a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection); 2505aa4dd815SMatthias Ringwald break; 2506aa4dd815SMatthias Ringwald case HFP_CMD_CALL_HOLD: { 2507a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2508e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2509d0c20769SMatthias Ringwald 25100222a807SMatthias Ringwald switch (hfp_connection->ag_call_hold_action){ 25110222a807SMatthias Ringwald case 0: 2512d0c20769SMatthias Ringwald // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call. 2513a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection); 2514aa4dd815SMatthias Ringwald break; 25150222a807SMatthias Ringwald case 1: 2516d0c20769SMatthias Ringwald // Releases all active calls (if any exist) and accepts the other (held or waiting) call. 2517d0c20769SMatthias Ringwald // Where both a held and a waiting call exist, the above procedures shall apply to the 2518d0c20769SMatthias Ringwald // waiting call (i.e., not to the held call) in conflicting situation. 2519a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection); 2520aa4dd815SMatthias Ringwald break; 25210222a807SMatthias Ringwald case 2: 2522d0c20769SMatthias Ringwald // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call. 2523d0c20769SMatthias Ringwald // Where both a held and a waiting call exist, the above procedures shall apply to the 2524d0c20769SMatthias Ringwald // waiting call (i.e., not to the held call) in conflicting situation. 2525a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection); 2526aa4dd815SMatthias Ringwald break; 25270222a807SMatthias Ringwald case 3: 2528d0c20769SMatthias Ringwald // Adds a held call to the conversation. 2529a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection); 2530aa4dd815SMatthias Ringwald break; 25310222a807SMatthias Ringwald case 4: 2532d0c20769SMatthias Ringwald // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer). 2533a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection); 2534aa4dd815SMatthias Ringwald break; 2535aa4dd815SMatthias Ringwald default: 2536aa4dd815SMatthias Ringwald break; 2537aa4dd815SMatthias Ringwald } 25380222a807SMatthias Ringwald hfp_connection->call_index = 0; 253935e92150SMatthias Ringwald break; 2540aa4dd815SMatthias Ringwald } 2541aa4dd815SMatthias Ringwald case HFP_CMD_CALL_PHONE_NUMBER: 2542a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 25439ff73f41SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED_BY_HF, hfp_connection); 2544aa4dd815SMatthias Ringwald break; 2545aa4dd815SMatthias Ringwald case HFP_CMD_REDIAL_LAST_NUMBER: 2546a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2547a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection); 2548aa4dd815SMatthias Ringwald break; 2549aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_CLIP: 2550a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2551a0ffb263SMatthias Ringwald log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled); 2552e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2553aa4dd815SMatthias Ringwald break; 2554aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION: 2555a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2556a0ffb263SMatthias Ringwald log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled); 2557e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2558aa4dd815SMatthias Ringwald break; 2559aa4dd815SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 2560a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2561e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 256260ebb071SMilanka Ringwald log_info("HF speaker gain = %u", hfp_connection->speaker_gain); 25633db60f78SBjoern Hartmann hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_connection->speaker_gain); 2564aa4dd815SMatthias Ringwald break; 2565aa4dd815SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 2566a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2567e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 256860ebb071SMilanka Ringwald log_info("HF microphone gain = %u", hfp_connection->microphone_gain); 25692abbd98dSMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_connection->microphone_gain); 2570aa4dd815SMatthias Ringwald break; 2571471dea41SMatthias Ringwald case HFP_CMD_CUSTOM_MESSAGE: 2572471dea41SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2573471dea41SMatthias Ringwald hfp_parser_reset_line_buffer(hfp_connection); 25746d9a41f9SMatthias Ringwald log_info("Custom AT Command ID 0x%04x", hfp_connection->custom_at_command_id); 2575471dea41SMatthias Ringwald hfp_ag_emit_custom_command_event(hfp_connection); 2576471dea41SMatthias Ringwald break; 25773f38f554SMatthias Ringwald case HFP_CMD_UNKNOWN: 25783f38f554SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2579e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 25803f38f554SMatthias Ringwald break; 2581aa4dd815SMatthias Ringwald default: 2582aa4dd815SMatthias Ringwald break; 25833deb3ec6SMatthias Ringwald } 25843deb3ec6SMatthias Ringwald } 25850cef86faSMatthias Ringwald } 25863deb3ec6SMatthias Ringwald 25871c6a0fc0SMatthias Ringwald static void hfp_ag_run(void){ 2588d63c37a1SMatthias Ringwald btstack_linked_list_iterator_t it; 2589d63c37a1SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2590d63c37a1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2591d63c37a1SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 259222387625SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2593f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 25943deb3ec6SMatthias Ringwald } 25953deb3ec6SMatthias Ringwald } 25963deb3ec6SMatthias Ringwald 25971c6a0fc0SMatthias Ringwald static void hfp_ag_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2598fdf18f86SMilanka Ringwald hfp_connection_t * hfp_connection = NULL; 25993deb3ec6SMatthias Ringwald switch (packet_type){ 26003deb3ec6SMatthias Ringwald case RFCOMM_DATA_PACKET: 2601fdf18f86SMilanka Ringwald hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 2602fd075f20SMilanka Ringwald btstack_assert(hfp_connection != NULL); 2603fdf18f86SMilanka Ringwald 2604fdf18f86SMilanka Ringwald hfp_ag_handle_rfcomm_data(hfp_connection, packet, size); 2605fdf18f86SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 2606fdf18f86SMilanka Ringwald return; 26073deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 2608e30a6a47SMatthias Ringwald if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 2609e30a6a47SMatthias Ringwald uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 2610fd075f20SMilanka Ringwald hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid); 2611fd075f20SMilanka Ringwald btstack_assert(hfp_connection != NULL); 2612fd075f20SMilanka Ringwald 2613fd075f20SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 2614e30a6a47SMatthias Ringwald return; 2615e30a6a47SMatthias Ringwald } 261627950165SMatthias Ringwald hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_AG); 2617aa4dd815SMatthias Ringwald break; 26183deb3ec6SMatthias Ringwald default: 26193deb3ec6SMatthias Ringwald break; 26203deb3ec6SMatthias Ringwald } 26213deb3ec6SMatthias Ringwald 26221c6a0fc0SMatthias Ringwald hfp_ag_run(); 26233deb3ec6SMatthias Ringwald } 26243deb3ec6SMatthias Ringwald 26251c6a0fc0SMatthias Ringwald static void hfp_ag_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2626405014fbSMatthias Ringwald hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_AG); 26271c6a0fc0SMatthias Ringwald hfp_ag_run(); 2628405014fbSMatthias Ringwald } 2629405014fbSMatthias Ringwald 2630aa10b9cbSMatthias Ringwald void hfp_ag_init_codecs(uint8_t codecs_nr, const uint8_t * codecs){ 2631aa10b9cbSMatthias Ringwald btstack_assert(codecs_nr <= HFP_MAX_NUM_CODECS); 26328cd06fb6SMatthias Ringwald if (codecs_nr > HFP_MAX_NUM_CODECS) return; 2633aa10b9cbSMatthias Ringwald 2634aeb0f0feSMatthias Ringwald hfp_ag_codecs_nr = codecs_nr; 2635aa10b9cbSMatthias Ringwald uint8_t i; 26363deb3ec6SMatthias Ringwald for (i=0; i < codecs_nr; i++){ 2637aeb0f0feSMatthias Ringwald hfp_ag_codecs[i] = codecs[i]; 26383deb3ec6SMatthias Ringwald } 2639a0ffb263SMatthias Ringwald } 26403deb3ec6SMatthias Ringwald 2641a0ffb263SMatthias Ringwald void hfp_ag_init_supported_features(uint32_t supported_features){ 2642aeb0f0feSMatthias Ringwald hfp_ag_supported_features = supported_features; 264340a8ee13SMatthias Ringwald hfp_ag_in_band_ring_tone_active = has_in_band_ring_tone(); 2644a0ffb263SMatthias Ringwald } 26453deb3ec6SMatthias Ringwald 26467ca89cabSMatthias Ringwald void hfp_ag_init_ag_indicators(int ag_indicators_nr, const hfp_ag_indicator_t * ag_indicators){ 26478cd06fb6SMatthias Ringwald btstack_assert(ag_indicators_nr <= HFP_MAX_NUM_INDICATORS); 26488cd06fb6SMatthias Ringwald if (ag_indicators_nr > HFP_MAX_NUM_CODECS) return; 26498cd06fb6SMatthias Ringwald 2650a0ffb263SMatthias Ringwald hfp_ag_indicators_nr = ag_indicators_nr; 26516535961aSMatthias Ringwald (void)memcpy(hfp_ag_indicators, ag_indicators, 26526535961aSMatthias Ringwald ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 2653a0ffb263SMatthias Ringwald } 26543deb3ec6SMatthias Ringwald 26557ca89cabSMatthias Ringwald void hfp_ag_init_hf_indicators(int hf_indicators_nr, const hfp_generic_status_indicator_t * hf_indicators){ 26568cd06fb6SMatthias Ringwald btstack_assert(hf_indicators_nr <= HFP_MAX_NUM_INDICATORS); 26578cd06fb6SMatthias Ringwald if (hf_indicators_nr > HFP_MAX_NUM_CODECS) return; 26588cd06fb6SMatthias Ringwald 2659aeb0f0feSMatthias Ringwald hfp_ag_generic_status_indicators_nr = hf_indicators_nr; 2660aeb0f0feSMatthias Ringwald (void)memcpy(hfp_ag_generic_status_indicators, hf_indicators, 26616535961aSMatthias Ringwald hf_indicators_nr * sizeof(hfp_generic_status_indicator_t)); 2662a0ffb263SMatthias Ringwald } 2663a0ffb263SMatthias Ringwald 2664a0ffb263SMatthias Ringwald void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){ 26653deb3ec6SMatthias Ringwald hfp_ag_call_hold_services_nr = call_hold_services_nr; 26666535961aSMatthias Ringwald (void)memcpy(hfp_ag_call_hold_services, call_hold_services, 26676535961aSMatthias Ringwald call_hold_services_nr * sizeof(char *)); 2668a0ffb263SMatthias Ringwald } 2669a0ffb263SMatthias Ringwald 2670a0ffb263SMatthias Ringwald 2671ab2445a0SMatthias Ringwald void hfp_ag_init(uint8_t rfcomm_channel_nr){ 267227950165SMatthias Ringwald 2673520c92d5SMatthias Ringwald hfp_init(); 267420b2edb6SMatthias Ringwald hfp_ag_call_hold_services_nr = 0; 267527bb1817SMatthias Ringwald hfp_ag_response_and_hold_active = false; 267620b2edb6SMatthias Ringwald hfp_ag_indicators_nr = 0; 2677aeb0f0feSMatthias Ringwald hfp_ag_codecs_nr = 0; 2678aeb0f0feSMatthias Ringwald hfp_ag_supported_features = HFP_DEFAULT_AG_SUPPORTED_FEATURES; 267940a8ee13SMatthias Ringwald hfp_ag_in_band_ring_tone_active = has_in_band_ring_tone(); 2680aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers = NULL; 2681aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers_count = 0; 2682d63c37a1SMatthias Ringwald 26831c6a0fc0SMatthias Ringwald hfp_ag_hci_event_callback_registration.callback = &hfp_ag_hci_event_packet_handler; 26841c6a0fc0SMatthias Ringwald hci_add_event_handler(&hfp_ag_hci_event_callback_registration); 268527950165SMatthias Ringwald 26861c6a0fc0SMatthias Ringwald rfcomm_register_service(&hfp_ag_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 268727950165SMatthias Ringwald 268827950165SMatthias Ringwald // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us 26891c6a0fc0SMatthias Ringwald hfp_set_ag_rfcomm_packet_handler(&hfp_ag_rfcomm_packet_handler); 2690aa4dd815SMatthias Ringwald 2691d210d9c4SMatthias Ringwald hfp_gsm_init(); 26923deb3ec6SMatthias Ringwald } 26933deb3ec6SMatthias Ringwald 269420b2edb6SMatthias Ringwald void hfp_ag_deinit(void){ 269520b2edb6SMatthias Ringwald hfp_deinit(); 269620b2edb6SMatthias Ringwald hfp_gsm_deinit(); 2697aeb0f0feSMatthias Ringwald 2698aeb0f0feSMatthias Ringwald hfp_ag_callback = NULL; 2699aeb0f0feSMatthias Ringwald hfp_ag_supported_features = 0; 2700aeb0f0feSMatthias Ringwald hfp_ag_codecs_nr = 0; 2701aeb0f0feSMatthias Ringwald hfp_ag_indicators_nr = 0; 2702aeb0f0feSMatthias Ringwald hfp_ag_call_hold_services_nr = 0; 270320b2edb6SMatthias Ringwald (void) memset(&hfp_ag_call_hold_services, 0, sizeof(hfp_ag_call_hold_services)); 2704aeb0f0feSMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 270520b2edb6SMatthias Ringwald (void) memset(&hfp_ag_response_and_hold_state, 0, sizeof(hfp_response_and_hold_state_t)); 2706aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers = NULL; 2707aeb0f0feSMatthias Ringwald (void) memset(&hfp_ag_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t)); 270881e25d0aSMatthias Ringwald hfp_ag_custom_call_sm_handler = NULL; 270920b2edb6SMatthias Ringwald } 271020b2edb6SMatthias Ringwald 27114eb3f1d8SMilanka Ringwald uint8_t hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){ 27124eb3f1d8SMilanka Ringwald return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE, HFP_ROLE_AG); 27133deb3ec6SMatthias Ringwald } 27143deb3ec6SMatthias Ringwald 2715657bc59fSMilanka Ringwald uint8_t hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){ 27169c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2717a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2718657bc59fSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2719a33eb0c4SMilanka Ringwald } 2720c5fa3c94SMilanka Ringwald 27211ffa0dd9SMilanka Ringwald hfp_trigger_release_service_level_connection(hfp_connection); 2722f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2723657bc59fSMilanka Ringwald return ERROR_CODE_SUCCESS; 27243deb3ec6SMatthias Ringwald } 27253deb3ec6SMatthias Ringwald 2726c5fa3c94SMilanka Ringwald uint8_t hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, hfp_cme_error_t error){ 27279c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2728a0ffb263SMatthias Ringwald if (!hfp_connection){ 2729c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 27303deb3ec6SMatthias Ringwald } 27313deb3ec6SMatthias Ringwald 2732c5fa3c94SMilanka Ringwald if (!hfp_connection->enable_extended_audio_gateway_error_report){ 2733c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2734c5fa3c94SMilanka Ringwald } 2735c5fa3c94SMilanka Ringwald 2736c5fa3c94SMilanka Ringwald hfp_connection->extended_audio_gateway_error = error; 2737c5fa3c94SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 2738c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2739c5fa3c94SMilanka Ringwald } 2740c5fa3c94SMilanka Ringwald 2741c5fa3c94SMilanka Ringwald static uint8_t hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){ 2742c5fa3c94SMilanka Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 2743c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2744c5fa3c94SMilanka Ringwald } 2745c5fa3c94SMilanka Ringwald if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){ 2746c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2747c5fa3c94SMilanka Ringwald } 27483deb3ec6SMatthias Ringwald 2749a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 1; 27502561704cSMatthias Ringwald hfp_connection->sco_for_msbc_failed = 0; 2751a0ffb263SMatthias Ringwald if (!has_codec_negotiation_feature(hfp_connection)){ 2752d6ff09e1SMatthias Ringwald log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using CVSD"); 2753d6ff09e1SMatthias Ringwald hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 2754a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 27557522e673SMatthias Ringwald // now, pick link settings 2756afac2a04SMilanka Ringwald hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection)); 27573721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP 27583721a235SMatthias Ringwald hfp_cc256x_prepare_for_sco(hfp_connection); 27593721a235SMatthias Ringwald #endif 2760c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 27613deb3ec6SMatthias Ringwald } 2762aa4dd815SMatthias Ringwald 2763411a9776SMilanka Ringwald uint8_t i; 2764411a9776SMilanka Ringwald bool codec_was_in_use = false; 2765411a9776SMilanka Ringwald bool better_codec_can_be_used = false; 2766411a9776SMilanka Ringwald 2767411a9776SMilanka Ringwald for (i = 0; i<hfp_connection->remote_codecs_nr; i++){ 2768411a9776SMilanka Ringwald if (hfp_connection->negotiated_codec == hfp_connection->remote_codecs[i]){ 2769411a9776SMilanka Ringwald codec_was_in_use = true; 2770411a9776SMilanka Ringwald } else if (hfp_connection->negotiated_codec < hfp_connection->remote_codecs[i]){ 2771411a9776SMilanka Ringwald better_codec_can_be_used = true; 2772411a9776SMilanka Ringwald } 2773411a9776SMilanka Ringwald } 2774411a9776SMilanka Ringwald 2775411a9776SMilanka Ringwald if (!codec_was_in_use || better_codec_can_be_used){ 2776bc94e48cSMatthias Ringwald hfp_connection->ag_send_common_codec = true; 2777411a9776SMilanka Ringwald hfp_connection->codecs_state = HFP_CODECS_IDLE; 2778411a9776SMilanka Ringwald } 2779c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2780aa4dd815SMatthias Ringwald } 2781aa4dd815SMatthias Ringwald 2782c5fa3c94SMilanka Ringwald uint8_t hfp_ag_establish_audio_connection(hci_con_handle_t acl_handle){ 27839c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2784a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2785c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2786a33eb0c4SMilanka Ringwald } 2787c5fa3c94SMilanka Ringwald uint8_t status = hfp_ag_setup_audio_connection(hfp_connection); 2788c5fa3c94SMilanka Ringwald if (status != ERROR_CODE_SUCCESS){ 2789c5fa3c94SMilanka Ringwald return status; 2790c5fa3c94SMilanka Ringwald } 2791f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2792c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 27933deb3ec6SMatthias Ringwald } 27943deb3ec6SMatthias Ringwald 2795c5fa3c94SMilanka Ringwald uint8_t hfp_ag_release_audio_connection(hci_con_handle_t acl_handle){ 27969c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2797a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2798c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2799a33eb0c4SMilanka Ringwald } 2800c5fa3c94SMilanka Ringwald 28010b4debbfSMilanka Ringwald if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){ 28020b4debbfSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 28030b4debbfSMilanka Ringwald } 28041a26de69SMilanka Ringwald 28050b4debbfSMilanka Ringwald uint8_t status = hfp_trigger_release_audio_connection(hfp_connection); 28060b4debbfSMilanka Ringwald if (status == ERROR_CODE_SUCCESS){ 2807f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 28080b4debbfSMilanka Ringwald } 28090b4debbfSMilanka Ringwald return status; 28103deb3ec6SMatthias Ringwald } 2811aa4dd815SMatthias Ringwald 2812aa4dd815SMatthias Ringwald /** 2813aa4dd815SMatthias Ringwald * @brief Enable in-band ring tone 2814aa4dd815SMatthias Ringwald */ 2815aa4dd815SMatthias Ringwald void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){ 281640a8ee13SMatthias Ringwald if (has_in_band_ring_tone() == false){ 281740a8ee13SMatthias Ringwald return; 281840a8ee13SMatthias Ringwald } 281940a8ee13SMatthias Ringwald if (hfp_ag_in_band_ring_tone_active == use_in_band_ring_tone){ 2820aa4dd815SMatthias Ringwald return; 2821aa4dd815SMatthias Ringwald } 2822c5fa3c94SMilanka Ringwald 282340a8ee13SMatthias Ringwald hfp_ag_in_band_ring_tone_active = use_in_band_ring_tone; 2824aa4dd815SMatthias Ringwald 2825665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 2826665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2827665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2828a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 282966c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 28309430c71eSMatthias Ringwald hfp_connection->ag_send_in_band_ring_tone_setting = true; 2831f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2832aa4dd815SMatthias Ringwald } 2833aa4dd815SMatthias Ringwald } 2834aa4dd815SMatthias Ringwald 2835aa4dd815SMatthias Ringwald /** 2836aa4dd815SMatthias Ringwald * @brief Called from GSM 2837aa4dd815SMatthias Ringwald */ 2838aa4dd815SMatthias Ringwald void hfp_ag_incoming_call(void){ 2839aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL); 2840aa4dd815SMatthias Ringwald } 2841aa4dd815SMatthias Ringwald 28420f1da533SMatthias Ringwald void hfp_ag_outgoing_call_initiated(void) { 2843fe899794SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED_BY_AG, NULL); 2844fe899794SMatthias Ringwald } 2845fe899794SMatthias Ringwald 2846aa4dd815SMatthias Ringwald /** 2847aa4dd815SMatthias Ringwald * @brief number is stored. 2848aa4dd815SMatthias Ringwald */ 2849aa4dd815SMatthias Ringwald void hfp_ag_set_clip(uint8_t type, const char * number){ 2850f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_SET_CLIP, 0, type, number); 2851aa4dd815SMatthias Ringwald } 2852aa4dd815SMatthias Ringwald 2853aa4dd815SMatthias Ringwald void hfp_ag_call_dropped(void){ 2854aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL); 2855aa4dd815SMatthias Ringwald } 2856aa4dd815SMatthias Ringwald 2857aa4dd815SMatthias Ringwald // call from AG UI 2858aa4dd815SMatthias Ringwald void hfp_ag_answer_incoming_call(void){ 2859aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL); 2860aa4dd815SMatthias Ringwald } 2861aa4dd815SMatthias Ringwald 2862ce263fc8SMatthias Ringwald void hfp_ag_join_held_call(void){ 2863ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL); 2864ce263fc8SMatthias Ringwald } 2865ce263fc8SMatthias Ringwald 2866aa4dd815SMatthias Ringwald void hfp_ag_terminate_call(void){ 2867aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL); 2868aa4dd815SMatthias Ringwald } 2869aa4dd815SMatthias Ringwald 2870aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_ringing(void){ 2871aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL); 2872aa4dd815SMatthias Ringwald } 2873aa4dd815SMatthias Ringwald 2874aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_established(void){ 2875aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL); 2876aa4dd815SMatthias Ringwald } 2877aa4dd815SMatthias Ringwald 2878aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_rejected(void){ 2879aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL); 2880aa4dd815SMatthias Ringwald } 2881aa4dd815SMatthias Ringwald 2882aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_accepted(void){ 2883aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL); 2884aa4dd815SMatthias Ringwald } 2885ce263fc8SMatthias Ringwald 2886ce263fc8SMatthias Ringwald void hfp_ag_hold_incoming_call(void){ 2887ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL); 2888ce263fc8SMatthias Ringwald } 2889ce263fc8SMatthias Ringwald 2890ce263fc8SMatthias Ringwald void hfp_ag_accept_held_incoming_call(void) { 2891ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL); 2892ce263fc8SMatthias Ringwald } 2893ce263fc8SMatthias Ringwald 2894ce263fc8SMatthias Ringwald void hfp_ag_reject_held_incoming_call(void){ 2895ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL); 2896ce263fc8SMatthias Ringwald } 2897ce263fc8SMatthias Ringwald 2898aa4dd815SMatthias Ringwald static void hfp_ag_set_ag_indicator(const char * name, int value){ 2899aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name(name); 2900aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 2901aa4dd815SMatthias Ringwald hfp_ag_indicators[indicator_index].status = value; 2902aa4dd815SMatthias Ringwald 2903665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 2904665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2905665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2906a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 290766c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2908a0ffb263SMatthias Ringwald if (!hfp_connection->ag_indicators[indicator_index].enabled) { 2909245852b7SMilanka Ringwald log_info("Requested AG indicator '%s' update to %u, but it is not enabled", hfp_ag_indicators[indicator_index].name, value); 2910ce263fc8SMatthias Ringwald continue; 2911ce263fc8SMatthias Ringwald } 2912245852b7SMilanka Ringwald log_info("AG indicator '%s' changed to %u, request transfer status", hfp_ag_indicators[indicator_index].name, value); 2913a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 2914f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2915aa4dd815SMatthias Ringwald } 2916aa4dd815SMatthias Ringwald } 2917aa4dd815SMatthias Ringwald 2918c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_registration_status(int registration_status){ 2919c5fa3c94SMilanka Ringwald if ((registration_status < 0) || (registration_status > 1)){ 2920c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2921c5fa3c94SMilanka Ringwald } 2922c5fa3c94SMilanka Ringwald 2923c5fa3c94SMilanka Ringwald if ( (registration_status == 0) && (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 2924245852b7SMilanka Ringwald 2925245852b7SMilanka Ringwald // if network goes away wihle a call is active: 2926245852b7SMilanka Ringwald // - the call gets dropped 2927245852b7SMilanka Ringwald // - we send NO CARRIER 2928245852b7SMilanka Ringwald // NOTE: the CALL=0 has to be sent before NO CARRIER 2929245852b7SMilanka Ringwald 2930245852b7SMilanka Ringwald hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL); 2931245852b7SMilanka Ringwald 2932245852b7SMilanka Ringwald btstack_linked_list_iterator_t it; 2933245852b7SMilanka Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2934245852b7SMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2935245852b7SMilanka Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 2936245852b7SMilanka Ringwald hfp_connection->ag_send_no_carrier = true; 2937245852b7SMilanka Ringwald } 2938245852b7SMilanka Ringwald } 2939c5fa3c94SMilanka Ringwald hfp_ag_set_ag_indicator("service", registration_status); 2940c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2941aa4dd815SMatthias Ringwald } 2942aa4dd815SMatthias Ringwald 2943c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_signal_strength(int signal_strength){ 2944c5fa3c94SMilanka Ringwald if ((signal_strength < 0) || (signal_strength > 5)){ 2945c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2946aa4dd815SMatthias Ringwald } 2947aa4dd815SMatthias Ringwald 2948c5fa3c94SMilanka Ringwald hfp_ag_set_ag_indicator("signal", signal_strength); 2949c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2950aa4dd815SMatthias Ringwald } 2951aa4dd815SMatthias Ringwald 2952c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_roaming_status(int roaming_status){ 2953c5fa3c94SMilanka Ringwald if ((roaming_status < 0) || (roaming_status > 1)){ 2954c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2955aa4dd815SMatthias Ringwald } 2956aa4dd815SMatthias Ringwald 2957c5fa3c94SMilanka Ringwald hfp_ag_set_ag_indicator("roam", roaming_status); 2958c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2959c5fa3c94SMilanka Ringwald } 2960c5fa3c94SMilanka Ringwald 2961c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_battery_level(int battery_level){ 2962c5fa3c94SMilanka Ringwald if ((battery_level < 0) || (battery_level > 5)){ 2963c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2964c5fa3c94SMilanka Ringwald } 2965c5fa3c94SMilanka Ringwald hfp_ag_set_ag_indicator("battchg", battery_level); 2966c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2967c5fa3c94SMilanka Ringwald } 2968c5fa3c94SMilanka Ringwald 29696b8275b0SMilanka Ringwald uint8_t hfp_ag_activate_voice_recognition(hci_con_handle_t acl_handle){ 297014c148caSMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 297114c148caSMilanka Ringwald if (!hfp_connection){ 297214c148caSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 29736b8275b0SMilanka Ringwald } 2974f71d2aacSMilanka Ringwald 297584fb9ac1SMilanka Ringwald if (hfp_connection->emit_vra_enabled_after_audio_established){ 297684fb9ac1SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 297784fb9ac1SMilanka Ringwald } 297884fb9ac1SMilanka Ringwald 29799cd26862SMilanka Ringwald bool enhanced_vra_supported = hfp_ag_enhanced_vra_flag_supported(hfp_connection); 29809cd26862SMilanka Ringwald bool legacy_vra_supported = hfp_ag_vra_flag_supported(hfp_connection); 2981f71d2aacSMilanka Ringwald if (!enhanced_vra_supported && !legacy_vra_supported){ 298214c148caSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 29836b8275b0SMilanka Ringwald } 29846b8275b0SMilanka Ringwald 2985135d9718SMilanka Ringwald if (!hfp_ag_can_activate_voice_recognition(hfp_connection)){ 2986135d9718SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2987f71d2aacSMilanka Ringwald } 2988f71d2aacSMilanka Ringwald 2989f71d2aacSMilanka Ringwald hfp_connection->ag_activate_voice_recognition_value = 1; 2990f71d2aacSMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED; 2991fcf4ede6SMilanka Ringwald hfp_connection->enhanced_voice_recognition_enabled = enhanced_vra_supported; 2992f71d2aacSMilanka Ringwald hfp_connection->ag_audio_connection_opened_before_vra = hfp_ag_is_audio_connection_active(hfp_connection); 2993f71d2aacSMilanka Ringwald hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY; 29948f8818a4SMatthias Ringwald hfp_connection->ag_vra_send_command = true; 2995f71d2aacSMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 2996f71d2aacSMilanka Ringwald return ERROR_CODE_SUCCESS; 2997f71d2aacSMilanka Ringwald } 2998f71d2aacSMilanka Ringwald 2999d84719b1SMilanka Ringwald uint8_t hfp_ag_deactivate_voice_recognition(hci_con_handle_t acl_handle){ 300014c148caSMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 300114c148caSMilanka Ringwald if (!hfp_connection){ 300214c148caSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 300345796ff1SMilanka Ringwald } 3004f71d2aacSMilanka Ringwald 300584fb9ac1SMilanka Ringwald if (hfp_connection->emit_vra_enabled_after_audio_established){ 300684fb9ac1SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 300784fb9ac1SMilanka Ringwald } 300884fb9ac1SMilanka Ringwald 30099cd26862SMilanka Ringwald bool enhanced_vra_supported = hfp_ag_enhanced_vra_flag_supported(hfp_connection); 30109cd26862SMilanka Ringwald bool legacy_vra_supported = hfp_ag_vra_flag_supported(hfp_connection); 30119cd26862SMilanka Ringwald 3012f71d2aacSMilanka Ringwald if (!enhanced_vra_supported && !legacy_vra_supported){ 301314c148caSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 301445796ff1SMilanka Ringwald } 301545796ff1SMilanka Ringwald 3016135d9718SMilanka Ringwald if (!hfp_ag_voice_recognition_session_active(hfp_connection)){ 3017135d9718SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3018f71d2aacSMilanka Ringwald } 3019f71d2aacSMilanka Ringwald 3020f71d2aacSMilanka Ringwald hfp_connection->ag_activate_voice_recognition_value = 0; 3021f71d2aacSMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF; 3022f71d2aacSMilanka Ringwald hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY; 30238f8818a4SMatthias Ringwald hfp_connection->ag_vra_send_command = true; 3024f71d2aacSMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 3025f71d2aacSMilanka Ringwald return ERROR_CODE_SUCCESS; 3026f71d2aacSMilanka Ringwald } 3027f71d2aacSMilanka Ringwald 3028cf75be85SMilanka Ringwald static uint8_t hfp_ag_enhanced_voice_recognition_send_state(hci_con_handle_t acl_handle, hfp_voice_recognition_state_t state){ 302945796ff1SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 303045796ff1SMilanka Ringwald if (!hfp_connection){ 3031c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 303245796ff1SMilanka Ringwald } 3033c5fa3c94SMilanka Ringwald 303484fb9ac1SMilanka Ringwald if (hfp_connection->emit_vra_enabled_after_audio_established){ 303584fb9ac1SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3036f71d2aacSMilanka Ringwald } 3037f71d2aacSMilanka Ringwald 3038645bfa78SMilanka Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){ 3039645bfa78SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3040645bfa78SMilanka Ringwald } 3041645bfa78SMilanka Ringwald 3042645bfa78SMilanka Ringwald if (hfp_connection->vra_state != HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO){ 3043135d9718SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 30449cd26862SMilanka Ringwald } 30459cd26862SMilanka Ringwald 304684fb9ac1SMilanka Ringwald bool enhanced_vra_supported = hfp_ag_enhanced_vra_flag_supported(hfp_connection); 304784fb9ac1SMilanka Ringwald if (!enhanced_vra_supported ){ 304884fb9ac1SMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 304984fb9ac1SMilanka Ringwald } 305084fb9ac1SMilanka Ringwald 305184fb9ac1SMilanka Ringwald 305245796ff1SMilanka Ringwald hfp_connection->ag_vra_state = state; 3053cf75be85SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_STATUS; 30548f8818a4SMatthias Ringwald hfp_connection->ag_vra_send_command = true; 305545796ff1SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 30569cd26862SMilanka Ringwald return ERROR_CODE_SUCCESS; 305745796ff1SMilanka Ringwald } 305845796ff1SMilanka Ringwald 3059f0c27a95SMilanka Ringwald uint8_t hfp_ag_enhanced_voice_recognition_report_sending_audio(hci_con_handle_t acl_handle){ 3060cf75be85SMilanka Ringwald return hfp_ag_enhanced_voice_recognition_send_state(acl_handle, HFP_VOICE_RECOGNITION_STATE_AG_IS_STARTING_SOUND); 306145796ff1SMilanka Ringwald } 3062f0c27a95SMilanka Ringwald uint8_t hfp_ag_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle){ 3063cf75be85SMilanka Ringwald return hfp_ag_enhanced_voice_recognition_send_state(acl_handle, HFP_VOICE_RECOGNITION_STATE_AG_READY_TO_ACCEPT_AUDIO_INPUT); 306445796ff1SMilanka Ringwald } 3065cf75be85SMilanka Ringwald uint8_t hfp_ag_enhanced_voice_recognition_report_processing_input(hci_con_handle_t acl_handle){ 3066cf75be85SMilanka Ringwald return hfp_ag_enhanced_voice_recognition_send_state(acl_handle, HFP_VOICE_RECOGNITION_STATE_AG_IS_PROCESSING_AUDIO_INPUT); 306745796ff1SMilanka Ringwald } 306845796ff1SMilanka Ringwald 3069b95cac54SMilanka Ringwald 3070b95cac54SMilanka Ringwald uint8_t hfp_ag_enhanced_voice_recognition_send_message(hci_con_handle_t acl_handle, hfp_voice_recognition_state_t state, hfp_voice_recognition_message_t msg){ 3071b95cac54SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3072b95cac54SMilanka Ringwald if (!hfp_connection){ 3073b95cac54SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3074b95cac54SMilanka Ringwald } 3075b95cac54SMilanka Ringwald 307684fb9ac1SMilanka Ringwald if (hfp_connection->emit_vra_enabled_after_audio_established){ 307784fb9ac1SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3078f71d2aacSMilanka Ringwald } 3079f71d2aacSMilanka Ringwald 3080645bfa78SMilanka Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){ 3081645bfa78SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3082645bfa78SMilanka Ringwald } 3083645bfa78SMilanka Ringwald 3084645bfa78SMilanka Ringwald if (hfp_connection->vra_state != HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO){ 3085135d9718SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3086f71d2aacSMilanka Ringwald } 3087f71d2aacSMilanka Ringwald 308884fb9ac1SMilanka Ringwald bool enhanced_vra_supported = hfp_ag_enhanced_vra_flag_supported(hfp_connection); 308984fb9ac1SMilanka Ringwald if (!enhanced_vra_supported ){ 309084fb9ac1SMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 309184fb9ac1SMilanka Ringwald } 309284fb9ac1SMilanka Ringwald 309384fb9ac1SMilanka Ringwald bool enhanced_vra_msg_supported = hfp_ag_can_send_enhanced_vra_message_flag_supported(hfp_connection); 309484fb9ac1SMilanka Ringwald if (!enhanced_vra_msg_supported ){ 309584fb9ac1SMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 309684fb9ac1SMilanka Ringwald } 309784fb9ac1SMilanka Ringwald 3098ab2445a0SMatthias Ringwald uint16_t message_len = (uint16_t) strlen(msg.text); 309951aa5d5aSMilanka Ringwald 310089e7c136SMilanka Ringwald if (message_len > HFP_MAX_VR_TEXT_SIZE){ 31016504a03aSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 31026504a03aSMilanka Ringwald } 31036504a03aSMilanka Ringwald 31046504a03aSMilanka Ringwald if ((HFP_VR_TEXT_HEADER_SIZE + message_len) > hfp_connection->rfcomm_mtu){ 310551aa5d5aSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 310651aa5d5aSMilanka Ringwald } 310751aa5d5aSMilanka Ringwald 3108b95cac54SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_MSG; 3109b95cac54SMilanka Ringwald hfp_connection->ag_msg = msg; 3110b95cac54SMilanka Ringwald hfp_connection->ag_vra_state = state; 31118f8818a4SMatthias Ringwald hfp_connection->ag_vra_send_command = true; 3112b95cac54SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 3113f71d2aacSMilanka Ringwald 3114f71d2aacSMilanka Ringwald return ERROR_CODE_SUCCESS; 3115b95cac54SMilanka Ringwald } 3116b95cac54SMilanka Ringwald 3117b95cac54SMilanka Ringwald 3118c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 31199c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3120a33eb0c4SMilanka Ringwald if (!hfp_connection){ 3121c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3122a33eb0c4SMilanka Ringwald } 3123c5fa3c94SMilanka Ringwald 3124c5fa3c94SMilanka Ringwald if ((gain < 0) || (gain > 15)){ 3125c5fa3c94SMilanka Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 3126c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3127c5fa3c94SMilanka Ringwald } 3128c5fa3c94SMilanka Ringwald 3129a0ffb263SMatthias Ringwald if (hfp_connection->microphone_gain != gain){ 3130a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = gain; 3131a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 3132aa4dd815SMatthias Ringwald } 3133f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 3134c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 3135aa4dd815SMatthias Ringwald } 3136aa4dd815SMatthias Ringwald 3137c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 31389c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3139a33eb0c4SMilanka Ringwald if (!hfp_connection){ 3140c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3141a33eb0c4SMilanka Ringwald } 3142c5fa3c94SMilanka Ringwald 3143c5fa3c94SMilanka Ringwald if ((gain < 0) || (gain > 15)){ 3144c5fa3c94SMilanka Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 3145c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3146c5fa3c94SMilanka Ringwald } 3147c5fa3c94SMilanka Ringwald 3148a0ffb263SMatthias Ringwald if (hfp_connection->speaker_gain != gain){ 3149a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = gain; 3150a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 3151aa4dd815SMatthias Ringwald } 3152f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 3153c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 3154aa4dd815SMatthias Ringwald } 3155aa4dd815SMatthias Ringwald 3156c5fa3c94SMilanka Ringwald uint8_t hfp_ag_send_phone_number_for_voice_tag(hci_con_handle_t acl_handle, const char * number){ 31579c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3158a33eb0c4SMilanka Ringwald if (!hfp_connection){ 3159c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3160a33eb0c4SMilanka Ringwald } 3161c5fa3c94SMilanka Ringwald 3162aa4dd815SMatthias Ringwald hfp_ag_set_clip(0, number); 3163a0ffb263SMatthias Ringwald hfp_connection->send_phone_number_for_voice_tag = 1; 3164c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 3165aa4dd815SMatthias Ringwald } 3166aa4dd815SMatthias Ringwald 3167c5fa3c94SMilanka Ringwald uint8_t hfp_ag_reject_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 31689c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3169a33eb0c4SMilanka Ringwald if (!hfp_connection){ 3170c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3171a33eb0c4SMilanka Ringwald } 3172c5fa3c94SMilanka Ringwald 3173e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 3174c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 3175aa4dd815SMatthias Ringwald } 3176aa4dd815SMatthias Ringwald 3177c5fa3c94SMilanka Ringwald uint8_t hfp_ag_send_dtmf_code_done(hci_con_handle_t acl_handle){ 31789c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3179a33eb0c4SMilanka Ringwald if (!hfp_connection){ 3180c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3181a33eb0c4SMilanka Ringwald } 3182c5fa3c94SMilanka Ringwald 3183e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 3184c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 3185c1797c7dSMatthias Ringwald } 3186aa4dd815SMatthias Ringwald 31875be6b47aSMatthias Ringwald uint8_t hfp_ag_send_unsolicited_result_code(hci_con_handle_t acl_handle, const char * unsolicited_result_code){ 31885be6b47aSMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 31895be6b47aSMatthias Ringwald if (!hfp_connection){ 31905be6b47aSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 31915be6b47aSMatthias Ringwald } 31925be6b47aSMatthias Ringwald if (hfp_connection->send_custom_message != NULL){ 31935be6b47aSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 31945be6b47aSMatthias Ringwald } 31955be6b47aSMatthias Ringwald hfp_connection->send_custom_message = unsolicited_result_code; 31965be6b47aSMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 31975be6b47aSMatthias Ringwald return ERROR_CODE_SUCCESS; 31985be6b47aSMatthias Ringwald } 31995be6b47aSMatthias Ringwald 3200471dea41SMatthias Ringwald uint8_t hfp_ag_send_command_result_code(hci_con_handle_t acl_handle, bool ok){ 3201471dea41SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3202471dea41SMatthias Ringwald if (!hfp_connection){ 3203471dea41SMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3204471dea41SMatthias Ringwald } 3205471dea41SMatthias Ringwald if (ok){ 3206e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 3207471dea41SMatthias Ringwald } else { 3208e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 3209471dea41SMatthias Ringwald } 3210471dea41SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 3211471dea41SMatthias Ringwald return ERROR_CODE_SUCCESS; 3212471dea41SMatthias Ringwald } 3213471dea41SMatthias Ringwald 3214ce263fc8SMatthias Ringwald void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){ 3215aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers = numbers; 3216aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers_count = numbers_count; 3217ce263fc8SMatthias Ringwald } 3218ce263fc8SMatthias Ringwald 32199cae807eSMatthias Ringwald void hfp_ag_clear_last_dialed_number(void){ 32209cae807eSMatthias Ringwald hfp_gsm_clear_last_dialed_number(); 3221ce263fc8SMatthias Ringwald } 3222ce263fc8SMatthias Ringwald 32239de679b7SMilanka Ringwald void hfp_ag_set_last_dialed_number(const char * number){ 32249de679b7SMilanka Ringwald hfp_gsm_set_last_dialed_number(number); 32259de679b7SMilanka Ringwald } 32269de679b7SMilanka Ringwald 3227c5fa3c94SMilanka Ringwald uint8_t hfp_ag_notify_incoming_call_waiting(hci_con_handle_t acl_handle){ 32289c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3229a33eb0c4SMilanka Ringwald if (!hfp_connection){ 3230c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3231a33eb0c4SMilanka Ringwald } 3232c5fa3c94SMilanka Ringwald 3233c5fa3c94SMilanka Ringwald if (!hfp_connection->call_waiting_notification_enabled){ 3234c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3235c5fa3c94SMilanka Ringwald } 3236a0ffb263SMatthias Ringwald 3237a0ffb263SMatthias Ringwald hfp_connection->ag_notify_incoming_call_waiting = 1; 3238f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 3239c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 3240a0ffb263SMatthias Ringwald } 3241aa10b9cbSMatthias Ringwald void hfp_ag_create_sdp_record_with_codecs(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr, 3242aa10b9cbSMatthias Ringwald const char * name, uint8_t ability_to_reject_call, uint16_t supported_features, 3243aa10b9cbSMatthias Ringwald uint8_t codecs_nr, const uint8_t * codecs){ 324476cc1527SMatthias Ringwald if (!name){ 3245aeb0f0feSMatthias Ringwald name = hfp_ag_default_service_name; 324676cc1527SMatthias Ringwald } 324776cc1527SMatthias Ringwald hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, rfcomm_channel_nr, name); 324876cc1527SMatthias Ringwald 324976cc1527SMatthias Ringwald /* 325076cc1527SMatthias Ringwald * 0x01 – Ability to reject a call 325176cc1527SMatthias Ringwald * 0x00 – No ability to reject a call 325276cc1527SMatthias Ringwald */ 325376cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301); // Hands-Free Profile - Network 325476cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call); 325576cc1527SMatthias Ringwald 325676cc1527SMatthias Ringwald // Construct SupportedFeatures for SDP bitmap: 325776cc1527SMatthias Ringwald // 325876cc1527SMatthias Ringwald // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 325976cc1527SMatthias Ringwald // of the Bits 0 to 4 of the unsolicited result code +BRSF" 326076cc1527SMatthias Ringwald // 3261aa10b9cbSMatthias Ringwald // Wide band speech (bit 5) and LC3-SWB (bit 8) require Codec negotiation 326276cc1527SMatthias Ringwald // 326376cc1527SMatthias Ringwald uint16_t sdp_features = supported_features & 0x1f; 3264ef3ae4ebSMilanka Ringwald 3265ef3ae4ebSMilanka Ringwald if (supported_features & (1 << HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS)){ 3266ef3ae4ebSMilanka Ringwald sdp_features |= 1 << 6; 3267ef3ae4ebSMilanka Ringwald } 3268ef3ae4ebSMilanka Ringwald 3269ef3ae4ebSMilanka Ringwald if (supported_features & (1 << HFP_AGSF_VOICE_RECOGNITION_TEXT)){ 3270ef3ae4ebSMilanka Ringwald sdp_features |= 1 << 7; 3271ef3ae4ebSMilanka Ringwald } 3272ef3ae4ebSMilanka Ringwald 3273aa10b9cbSMatthias Ringwald // codecs 3274aa10b9cbSMatthias Ringwald if ((supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION)) != 0){ 3275aa10b9cbSMatthias Ringwald uint8_t i; 3276aa10b9cbSMatthias Ringwald for (i=0;i<codecs_nr;i++){ 3277aa10b9cbSMatthias Ringwald switch (codecs[i]){ 3278aa10b9cbSMatthias Ringwald case HFP_CODEC_MSBC: 3279aa10b9cbSMatthias Ringwald sdp_features |= 1 << 5; 3280aa10b9cbSMatthias Ringwald break; 3281aa10b9cbSMatthias Ringwald case HFP_CODEC_LC3_SWB: 3282aa10b9cbSMatthias Ringwald sdp_features |= 1 << 8; 3283aa10b9cbSMatthias Ringwald break; 328475389f80SMatthias Ringwald default: 328575389f80SMatthias Ringwald break; 3286aa10b9cbSMatthias Ringwald } 3287aa10b9cbSMatthias Ringwald } 3288aa10b9cbSMatthias Ringwald } 3289aa10b9cbSMatthias Ringwald 329076cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 329176cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 329276cc1527SMatthias Ringwald } 329376cc1527SMatthias Ringwald 3294aa10b9cbSMatthias Ringwald void hfp_ag_create_sdp_record(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr, const char * name, 3295aa10b9cbSMatthias Ringwald uint8_t ability_to_reject_call, uint16_t supported_features, int wide_band_speech){ 3296aa10b9cbSMatthias Ringwald uint8_t codecs_nr; 3297aa10b9cbSMatthias Ringwald const uint8_t * codecs; 3298aa10b9cbSMatthias Ringwald const uint8_t wide_band_codecs[] = { HFP_CODEC_MSBC }; 3299aa10b9cbSMatthias Ringwald if (wide_band_speech == 0){ 3300aa10b9cbSMatthias Ringwald codecs_nr = 0; 3301aa10b9cbSMatthias Ringwald codecs = NULL; 3302aa10b9cbSMatthias Ringwald } else { 3303aa10b9cbSMatthias Ringwald codecs_nr = 1; 3304aa10b9cbSMatthias Ringwald codecs = wide_band_codecs; 3305aa10b9cbSMatthias Ringwald } 3306aa10b9cbSMatthias Ringwald hfp_ag_create_sdp_record_with_codecs(service, service_record_handle, rfcomm_channel_nr, name, 3307aa10b9cbSMatthias Ringwald ability_to_reject_call, supported_features, codecs_nr, codecs); 3308aa10b9cbSMatthias Ringwald } 3309aa10b9cbSMatthias Ringwald 331076cc1527SMatthias Ringwald void hfp_ag_register_packet_handler(btstack_packet_handler_t callback){ 331168466199SMilanka Ringwald btstack_assert(callback != NULL); 331268466199SMilanka Ringwald 331376cc1527SMatthias Ringwald hfp_ag_callback = callback; 331476cc1527SMatthias Ringwald hfp_set_ag_callback(callback); 331576cc1527SMatthias Ringwald } 331681e25d0aSMatthias Ringwald 331781e25d0aSMatthias Ringwald void hfp_ag_register_custom_call_sm_handler(bool (*handler)(hfp_ag_call_event_t event)){ 331881e25d0aSMatthias Ringwald hfp_ag_custom_call_sm_handler = handler; 331981e25d0aSMatthias Ringwald } 3320471dea41SMatthias Ringwald 3321471dea41SMatthias Ringwald void hfp_ag_register_custom_at_command(hfp_custom_at_command_t * custom_at_command){ 3322471dea41SMatthias Ringwald hfp_register_custom_ag_command(custom_at_command); 3323471dea41SMatthias Ringwald } 3324