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 10820b2edb6SMatthias Ringwald static int 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 112aeb0f0feSMatthias Ringwald static int 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 11520b2edb6SMatthias Ringwald static int 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; 638485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 639aa4dd815SMatthias Ringwald return 1; 640aa4dd815SMatthias Ringwald 641aa4dd815SMatthias Ringwald case HFP_CMD_HF_CONFIRMED_CODEC: 642a0ffb263SMatthias Ringwald if (hfp_connection->codec_confirmed != hfp_connection->suggested_codec){ 643a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_ERROR; 644485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 645aa4dd815SMatthias Ringwald return 1; 646aa4dd815SMatthias Ringwald } 647a0ffb263SMatthias Ringwald hfp_connection->negotiated_codec = hfp_connection->codec_confirmed; 648a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 649c1ab6cc1SMatthias Ringwald log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD"); 650485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 6517522e673SMatthias Ringwald // now, pick link settings 652afac2a04SMilanka Ringwald hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection)); 6530327df7dSMatthias Ringwald // configure SBC coded if needed 6540327df7dSMatthias Ringwald hfp_prepare_for_sco(hfp_connection); 655aa4dd815SMatthias Ringwald return 1; 656aa4dd815SMatthias Ringwald default: 657aa4dd815SMatthias Ringwald break; 658aa4dd815SMatthias Ringwald } 659aa4dd815SMatthias Ringwald return 0; 660aa4dd815SMatthias Ringwald } 661aa4dd815SMatthias Ringwald 662a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){ 663a0ffb263SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 6647095467fSMatthias Ringwald hfp_emit_slc_connection_event(hfp_connection->local_role, 0, hfp_connection->acl_handle, hfp_connection->remote_addr); 665aa4dd815SMatthias Ringwald 66640a8ee13SMatthias Ringwald // in-band ring tone is active if supported 66740a8ee13SMatthias Ringwald hfp_connection->ag_in_band_ring_tone_active = has_in_band_ring_tone(); 66840a8ee13SMatthias Ringwald 66980702f97SMatthias Ringwald // HFP 4.35: "When [...] a new Service Level Connection is established all indicators are activated by default." 67080702f97SMatthias Ringwald uint16_t i; 67180702f97SMatthias Ringwald for (i=0;i<hfp_connection->ag_indicators_nr;i++){ 67280702f97SMatthias Ringwald hfp_connection->ag_indicators[i].enabled = 1; 67380702f97SMatthias Ringwald } 67480702f97SMatthias Ringwald 675a0ffb263SMatthias Ringwald // if active call exist, set per-hfp_connection state active, too (when audio is on) 676d0c20769SMatthias Ringwald if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 677a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE; 678aa4dd815SMatthias Ringwald } 679ce263fc8SMatthias Ringwald // if AG is ringing, also start ringing on the HF 680c4f8fc1cSMilanka Ringwald if (hfp_gsm_call_status() == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS){ 681c4f8fc1cSMilanka Ringwald switch (hfp_gsm_callsetup_status()){ 682c4f8fc1cSMilanka Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 6832ff1d7d7SMilanka Ringwald hfp_ag_hf_start_ringing_incoming(hfp_connection); 684c4f8fc1cSMilanka Ringwald break; 685fcd59412SMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 686fcd59412SMilanka Ringwald hfp_ag_hf_start_ringing_outgoing(hfp_connection); 687fcd59412SMilanka Ringwald break; 688c4f8fc1cSMilanka Ringwald default: 689c4f8fc1cSMilanka Ringwald break; 690c4f8fc1cSMilanka Ringwald } 691ce263fc8SMatthias Ringwald } 692aa4dd815SMatthias Ringwald } 6933deb3ec6SMatthias Ringwald 694a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){ 695c79b4cabSMatthias Ringwald // log_info("hfp_ag_run_for_context_service_level_connection state %u, command %u", hfp_connection->state, hfp_connection->command); 696a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 697c04cf7ffSMilanka Ringwald int sent = 0; 698a0ffb263SMatthias Ringwald switch(hfp_connection->command){ 6993deb3ec6SMatthias Ringwald case HFP_CMD_SUPPORTED_FEATURES: 700a0ffb263SMatthias Ringwald switch(hfp_connection->state){ 7013deb3ec6SMatthias Ringwald case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 702aa4dd815SMatthias Ringwald case HFP_EXCHANGE_SUPPORTED_FEATURES: 703aeb0f0feSMatthias Ringwald hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_ag_codecs, &hfp_ag_codecs_nr); 704a0ffb263SMatthias Ringwald if (has_codec_negotiation_feature(hfp_connection)){ 705a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS; 706aa4dd815SMatthias Ringwald } else { 707a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 708aa4dd815SMatthias Ringwald } 709a0ffb263SMatthias Ringwald hfp_ag_exchange_supported_features_cmd(hfp_connection->rfcomm_cid); 710aa4dd815SMatthias Ringwald return 1; 7113deb3ec6SMatthias Ringwald default: 7123deb3ec6SMatthias Ringwald break; 7133deb3ec6SMatthias Ringwald } 7143deb3ec6SMatthias Ringwald break; 7153deb3ec6SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 716c04cf7ffSMilanka Ringwald sent = codecs_exchange_state_machine(hfp_connection); 717a0ffb263SMatthias Ringwald if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){ 718a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 7193deb3ec6SMatthias Ringwald } 720c04cf7ffSMilanka Ringwald return sent; 721aa4dd815SMatthias Ringwald 722aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 723a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_W4_RETRIEVE_INDICATORS) { 724473ac565SMatthias Ringwald // HF requested AG Indicators and we did expect it 72584a0c24eSMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0; 726473ac565SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 727473ac565SMatthias Ringwald // continue below in state switch 728ad902e3dSMatthias Ringwald } 729ad902e3dSMatthias Ringwald break; 730aa4dd815SMatthias Ringwald 731aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 732a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break; 733a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE; 734485ac19eSMilanka Ringwald hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid); 735aa4dd815SMatthias Ringwald return 1; 736aa4dd815SMatthias Ringwald 7373deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 738a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break; 739a0ffb263SMatthias Ringwald if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 740a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL; 741a0ffb263SMatthias Ringwald } else if (has_hf_indicators_feature(hfp_connection)){ 742a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 743aa4dd815SMatthias Ringwald } else { 744a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 7453deb3ec6SMatthias Ringwald } 7468a46ec40SMatthias Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 747aa4dd815SMatthias Ringwald return 1; 7483deb3ec6SMatthias Ringwald 749aa4dd815SMatthias Ringwald case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES: 750a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break; 751a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 752a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 753aa4dd815SMatthias Ringwald } 754485ac19eSMilanka Ringwald hfp_ag_send_retrieve_can_hold_call_cmd(hfp_connection->rfcomm_cid); 755a0ffb263SMatthias Ringwald if (!has_hf_indicators_feature(hfp_connection)){ 756a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 757ce263fc8SMatthias Ringwald } 758aa4dd815SMatthias Ringwald return 1; 759aa4dd815SMatthias Ringwald 760aa4dd815SMatthias Ringwald case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 761a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break; 762a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS; 763485ac19eSMilanka Ringwald hfp_ag_send_list_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 764aa4dd815SMatthias Ringwald return 1; 765aa4dd815SMatthias Ringwald 766aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 767a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break; 768a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 769485ac19eSMilanka Ringwald hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 770aa4dd815SMatthias Ringwald return 1; 771aa4dd815SMatthias Ringwald 772aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 773a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break; 774485ac19eSMilanka Ringwald hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 775a7666b02SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 776aa4dd815SMatthias Ringwald return 1; 7773deb3ec6SMatthias Ringwald default: 7783deb3ec6SMatthias Ringwald break; 7793deb3ec6SMatthias Ringwald } 780473ac565SMatthias Ringwald 781473ac565SMatthias Ringwald switch (hfp_connection->state){ 782473ac565SMatthias Ringwald case HFP_RETRIEVE_INDICATORS: { 783485ac19eSMilanka Ringwald int next_segment = hfp_ag_send_retrieve_indicators_cmd_via_generator(hfp_connection->rfcomm_cid, hfp_connection, hfp_connection->send_ag_indicators_segment); 784473ac565SMatthias Ringwald int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection); 785473ac565SMatthias Ringwald log_info("HFP_CMD_RETRIEVE_AG_INDICATORS next segment %u, num_segments %u", next_segment, num_segments); 786473ac565SMatthias Ringwald if (next_segment < num_segments){ 787473ac565SMatthias Ringwald // prepare sending of next segment 788473ac565SMatthias Ringwald hfp_connection->send_ag_indicators_segment = next_segment; 789473ac565SMatthias Ringwald log_info("HFP_CMD_RETRIEVE_AG_INDICATORS more. command %u, next seg %u", hfp_connection->command, next_segment); 790473ac565SMatthias Ringwald } else { 791473ac565SMatthias Ringwald // done, go to next state 792473ac565SMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0; 793473ac565SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS; 794473ac565SMatthias Ringwald } 795473ac565SMatthias Ringwald return 1; 796473ac565SMatthias Ringwald } 797473ac565SMatthias Ringwald default: 798473ac565SMatthias Ringwald break; 799473ac565SMatthias Ringwald } 800c04cf7ffSMilanka Ringwald return 0; 8013deb3ec6SMatthias Ringwald } 8023deb3ec6SMatthias Ringwald 8039cd26862SMilanka Ringwald static bool hfp_ag_vra_flag_supported(hfp_connection_t * hfp_connection){ 80414c148caSMilanka Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION); 80514c148caSMilanka Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION); 80614c148caSMilanka Ringwald return hf && ag; 80714c148caSMilanka Ringwald } 80814c148caSMilanka Ringwald 8099cd26862SMilanka Ringwald static bool hfp_ag_enhanced_vra_flag_supported(hfp_connection_t * hfp_connection){ 81014c148caSMilanka Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS); 81114c148caSMilanka Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS); 81214c148caSMilanka Ringwald return hf && ag; 81314c148caSMilanka Ringwald } 81414c148caSMilanka Ringwald 8159cd26862SMilanka Ringwald static bool hfp_ag_can_send_enhanced_vra_message_flag_supported(hfp_connection_t * hfp_connection){ 816f71d2aacSMilanka Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_VOICE_RECOGNITION_TEXT); 817f71d2aacSMilanka Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_TEXT); 818f71d2aacSMilanka Ringwald return hf && ag; 81908a0b01cSMilanka Ringwald } 820f71d2aacSMilanka Ringwald 821135d9718SMilanka Ringwald static bool hfp_ag_can_activate_voice_recognition(hfp_connection_t * hfp_connection){ 822f71d2aacSMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){ 823135d9718SMilanka Ringwald return false; 824d84719b1SMilanka Ringwald } 8250c738fd4SMilanka Ringwald if (hfp_connection->vra_state != HFP_VRA_VOICE_RECOGNITION_OFF){ 826135d9718SMilanka Ringwald return false; 827d84719b1SMilanka Ringwald } 82808dc7cc6SMatthias Ringwald if (hfp_connection->ag_vra_send_command){ 82908dc7cc6SMatthias Ringwald return false; 83008dc7cc6SMatthias Ringwald } 831135d9718SMilanka Ringwald return true; 832d84719b1SMilanka Ringwald } 833d84719b1SMilanka Ringwald 834135d9718SMilanka Ringwald static bool hfp_ag_voice_recognition_session_active(hfp_connection_t * hfp_connection){ 83508a0b01cSMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){ 836135d9718SMilanka Ringwald return false; 83708a0b01cSMilanka Ringwald } 8380c738fd4SMilanka Ringwald if (hfp_connection->vra_state != HFP_VRA_VOICE_RECOGNITION_ACTIVATED){ 839de9e0ea7SMilanka Ringwald if (hfp_connection->vra_state == HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO){ 840135d9718SMilanka Ringwald return true; 841de9e0ea7SMilanka Ringwald } 842135d9718SMilanka Ringwald return false; 843d84719b1SMilanka Ringwald } 844135d9718SMilanka Ringwald return true; 845d84719b1SMilanka Ringwald } 846754910caSMilanka Ringwald 8470b4debbfSMilanka Ringwald static bool hfp_ag_is_audio_connection_active(hfp_connection_t * hfp_connection){ 8480b4debbfSMilanka Ringwald switch (hfp_connection->state){ 8490b4debbfSMilanka Ringwald case HFP_W2_CONNECT_SCO: 8500b4debbfSMilanka Ringwald case HFP_W4_SCO_CONNECTED: 8510b4debbfSMilanka Ringwald case HFP_AUDIO_CONNECTION_ESTABLISHED: 8520b4debbfSMilanka Ringwald return true; 853754910caSMilanka Ringwald default: 8540b4debbfSMilanka Ringwald return false; 855754910caSMilanka Ringwald } 856754910caSMilanka Ringwald } 857754910caSMilanka Ringwald 858b95cac54SMilanka Ringwald static void hfp_ag_emit_enhanced_voice_recognition_msg_sent_event(hfp_connection_t * hfp_connection, uint8_t status){ 859b95cac54SMilanka Ringwald hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 860b95cac54SMilanka Ringwald 861b95cac54SMilanka Ringwald uint8_t event[6]; 862b95cac54SMilanka Ringwald event[0] = HCI_EVENT_HFP_META; 863b95cac54SMilanka Ringwald event[1] = sizeof(event) - 2; 864c95b5b3cSMilanka Ringwald event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_MESSAGE_SENT; 865b95cac54SMilanka Ringwald little_endian_store_16(event, 3, acl_handle); 866b95cac54SMilanka Ringwald event[5] = status; 867b95cac54SMilanka Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 868b95cac54SMilanka Ringwald } 869b95cac54SMilanka Ringwald 870347d46c8SMilanka Ringwald 871585db78dSMilanka Ringwald static void hfp_ag_emit_hf_indicator_value(hfp_connection_t * hfp_connection, uint16_t uuid, uint8_t value){ 872347d46c8SMilanka Ringwald hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 873347d46c8SMilanka Ringwald 874347d46c8SMilanka Ringwald uint8_t event[8]; 875347d46c8SMilanka Ringwald event[0] = HCI_EVENT_HFP_META; 876347d46c8SMilanka Ringwald event[1] = sizeof(event) - 2; 877347d46c8SMilanka Ringwald event[2] = HFP_SUBEVENT_HF_INDICATOR; 878347d46c8SMilanka Ringwald little_endian_store_16(event, 3, acl_handle); 879347d46c8SMilanka Ringwald little_endian_store_16(event, 5, uuid); 880347d46c8SMilanka Ringwald event[7] = value; 881347d46c8SMilanka Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 882347d46c8SMilanka Ringwald } 883347d46c8SMilanka Ringwald 884a4f85bd2SMilanka Ringwald static void hfp_ag_emit_general_simple_event(uint8_t event_subtype){ 885a4f85bd2SMilanka Ringwald if (!hfp_ag_callback) return; 886a4f85bd2SMilanka Ringwald 887a4f85bd2SMilanka Ringwald uint8_t event[5]; 888a4f85bd2SMilanka Ringwald event[0] = HCI_EVENT_HFP_META; 889a4f85bd2SMilanka Ringwald event[1] = sizeof(event) - 2; 890a4f85bd2SMilanka Ringwald event[2] = event_subtype; 891a4f85bd2SMilanka Ringwald little_endian_store_16(event, 3, HCI_CON_HANDLE_INVALID); 892a4f85bd2SMilanka Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 893a4f85bd2SMilanka Ringwald } 894a4f85bd2SMilanka Ringwald 895471dea41SMatthias Ringwald static void hfp_ag_emit_custom_command_event(hfp_connection_t * hfp_connection){ 896471dea41SMatthias Ringwald btstack_assert(sizeof(hfp_connection->line_buffer) < (255-5)); 897471dea41SMatthias Ringwald 898471dea41SMatthias Ringwald uint16_t line_len = strlen((const char*)hfp_connection->line_buffer) + 1; 899471dea41SMatthias Ringwald uint8_t event[7 + sizeof(hfp_connection->line_buffer)]; 900471dea41SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 901471dea41SMatthias Ringwald event[1] = 5 + line_len; 902471dea41SMatthias Ringwald event[2] = HFP_SUBEVENT_CUSTOM_AT_COMMAND; 903471dea41SMatthias Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 9046d9a41f9SMatthias Ringwald little_endian_store_16(event, 5, hfp_connection->custom_at_command_id); 905471dea41SMatthias Ringwald memcpy(&event[7], hfp_connection->line_buffer, line_len); 906471dea41SMatthias Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, 7 + line_len); 907471dea41SMatthias Ringwald } 908471dea41SMatthias Ringwald 909774e3016SMatthias Ringwald // @return status 910774e3016SMatthias Ringwald static uint8_t hfp_ag_vra_state_machine_two(hfp_connection_t * hfp_connection){ 911774e3016SMatthias Ringwald uint8_t status = ERROR_CODE_SUCCESS; 912774e3016SMatthias Ringwald switch (hfp_connection->vra_state_requested){ 913774e3016SMatthias Ringwald case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 914774e3016SMatthias Ringwald if (!hfp_ag_is_audio_connection_active(hfp_connection)){ 915774e3016SMatthias Ringwald status = hfp_ag_setup_audio_connection(hfp_connection); 916774e3016SMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 917774e3016SMatthias Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 918774e3016SMatthias Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, status); 919774e3016SMatthias Ringwald break; 920774e3016SMatthias Ringwald } 921774e3016SMatthias Ringwald } 922774e3016SMatthias Ringwald hfp_connection->enhanced_voice_recognition_enabled = true; 923774e3016SMatthias Ringwald hfp_connection->vra_state = HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 924774e3016SMatthias Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 925774e3016SMatthias Ringwald hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_SUCCESS); 926774e3016SMatthias Ringwald break; 927774e3016SMatthias Ringwald 928774e3016SMatthias Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 929774e3016SMatthias Ringwald hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF; 930774e3016SMatthias Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 931774e3016SMatthias Ringwald 932774e3016SMatthias Ringwald // release audio connection only if it was opened after audio VR activated 933774e3016SMatthias Ringwald if (hfp_ag_is_audio_connection_active(hfp_connection) && !hfp_connection->ag_audio_connection_opened_before_vra){ 934774e3016SMatthias Ringwald hfp_trigger_release_audio_connection(hfp_connection); 935774e3016SMatthias Ringwald } 936774e3016SMatthias Ringwald 937774e3016SMatthias Ringwald hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS); 938774e3016SMatthias Ringwald break; 939774e3016SMatthias Ringwald 940774e3016SMatthias Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED: 941774e3016SMatthias Ringwald // open only if audio connection does not already exist 942774e3016SMatthias Ringwald if (!hfp_ag_is_audio_connection_active(hfp_connection)){ 943774e3016SMatthias Ringwald status = hfp_ag_setup_audio_connection(hfp_connection); 944774e3016SMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 945774e3016SMatthias Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 946774e3016SMatthias Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, status); 947774e3016SMatthias Ringwald break; 948774e3016SMatthias Ringwald } 949774e3016SMatthias Ringwald } 950774e3016SMatthias Ringwald 951774e3016SMatthias Ringwald hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_ACTIVATED; 952774e3016SMatthias Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 953774e3016SMatthias Ringwald hfp_connection->enhanced_voice_recognition_enabled = hfp_ag_enhanced_vra_flag_supported(hfp_connection); 95484fb9ac1SMilanka Ringwald 95584fb9ac1SMilanka Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 956774e3016SMatthias Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS); 95784fb9ac1SMilanka Ringwald } else { 95884fb9ac1SMilanka Ringwald hfp_connection->emit_vra_enabled_after_audio_established = true; 95984fb9ac1SMilanka Ringwald } 960774e3016SMatthias Ringwald break; 961774e3016SMatthias Ringwald 962774e3016SMatthias Ringwald default: 963774e3016SMatthias Ringwald break; 964774e3016SMatthias Ringwald } 965774e3016SMatthias Ringwald return status; 966774e3016SMatthias Ringwald } 967774e3016SMatthias Ringwald 96808dc7cc6SMatthias Ringwald static uint8_t hfp_ag_vra_send_command(hfp_connection_t * hfp_connection){ 96908dc7cc6SMatthias Ringwald uint8_t done = 0; 970754910caSMilanka Ringwald uint8_t status; 971754910caSMilanka Ringwald 972cf75be85SMilanka Ringwald switch (hfp_connection->vra_state_requested){ 973b95cac54SMilanka Ringwald case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_MSG: 974b95cac54SMilanka Ringwald done = hfp_ag_send_enhanced_voice_recognition_msg_cmd(hfp_connection); 975b95cac54SMilanka Ringwald if (done == 0){ 976b95cac54SMilanka Ringwald hfp_ag_emit_enhanced_voice_recognition_msg_sent_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 977b95cac54SMilanka Ringwald } else { 978b95cac54SMilanka Ringwald hfp_ag_emit_enhanced_voice_recognition_msg_sent_event(hfp_connection, ERROR_CODE_SUCCESS); 979b95cac54SMilanka Ringwald } 980b95cac54SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 981b95cac54SMilanka Ringwald return done; 982b95cac54SMilanka Ringwald 983cf75be85SMilanka Ringwald case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_STATUS: 984cf75be85SMilanka Ringwald done = hfp_ag_send_enhanced_voice_recognition_state_cmd(hfp_connection); 985cf75be85SMilanka Ringwald if (done == 0){ 986cf75be85SMilanka Ringwald hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 987cf75be85SMilanka Ringwald } else { 988cf75be85SMilanka Ringwald hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS); 989cf75be85SMilanka Ringwald } 990cf75be85SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 991b95cac54SMilanka Ringwald return done; 992b95cac54SMilanka Ringwald 9936e13e408SMatthias Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED: 9946e13e408SMatthias Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 995d1b50446SMilanka Ringwald done = hfp_ag_send_voice_recognition_cmd(hfp_connection, hfp_connection->ag_activate_voice_recognition_value); 9960c738fd4SMilanka Ringwald if (done == 0){ 9970c738fd4SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 998553a4a56SMilanka Ringwald 999553a4a56SMilanka Ringwald if (hfp_connection->ag_activate_voice_recognition_value == 1){ 1000553a4a56SMilanka Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, done); 1001553a4a56SMilanka Ringwald } else { 1002553a4a56SMilanka Ringwald hfp_emit_voice_recognition_disabled(hfp_connection, done); 1003553a4a56SMilanka Ringwald } 1004fd4151d1SMilanka Ringwald return 0; 1005fd4151d1SMilanka Ringwald } 100608dc7cc6SMatthias Ringwald status = hfp_ag_vra_state_machine_two(hfp_connection); 100708dc7cc6SMatthias Ringwald return (status == ERROR_CODE_SUCCESS) ? 1 : 0; 100808dc7cc6SMatthias Ringwald 10096e13e408SMatthias Ringwald default: 10106e13e408SMatthias Ringwald log_error("state %u", hfp_connection->vra_state_requested); 10116e13e408SMatthias Ringwald btstack_assert(false); 101208dc7cc6SMatthias Ringwald return 0; 1013cf75be85SMilanka Ringwald } 101408dc7cc6SMatthias Ringwald } 101508dc7cc6SMatthias Ringwald 101608dc7cc6SMatthias Ringwald static int hfp_ag_voice_recognition_state_machine(hfp_connection_t * hfp_connection){ 101708dc7cc6SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) { 101808dc7cc6SMatthias Ringwald return 0; 101908dc7cc6SMatthias Ringwald } 102008dc7cc6SMatthias Ringwald int done = 0; 102108dc7cc6SMatthias Ringwald uint8_t status; 102208dc7cc6SMatthias Ringwald 10238f8818a4SMatthias Ringwald // VRA action initiated by AG 10248f8818a4SMatthias Ringwald if (hfp_connection->ag_vra_send_command){ 10258f8818a4SMatthias Ringwald hfp_connection->ag_vra_send_command = false; 10268f8818a4SMatthias Ringwald return hfp_ag_vra_send_command(hfp_connection); 10278f8818a4SMatthias Ringwald } 10288f8818a4SMatthias Ringwald 1029e7c46708SMatthias Ringwald if (hfp_connection->ag_vra_requested_by_hf){ 1030e7c46708SMatthias Ringwald hfp_connection->ag_vra_requested_by_hf = false; 1031e7c46708SMatthias Ringwald 1032734ef2bdSMilanka Ringwald // HF initiatied voice recognition, parser extracted activation value 1033de9e0ea7SMilanka Ringwald switch (hfp_connection->ag_activate_voice_recognition_value){ 1034de9e0ea7SMilanka Ringwald case 0: 1035135d9718SMilanka Ringwald if (hfp_ag_voice_recognition_session_active(hfp_connection)){ 1036fd4151d1SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF; 1037fd4151d1SMilanka Ringwald done = hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1038fd4151d1SMilanka Ringwald } 1039de9e0ea7SMilanka Ringwald break; 1040de9e0ea7SMilanka Ringwald 1041de9e0ea7SMilanka Ringwald case 1: 1042135d9718SMilanka Ringwald if (hfp_ag_can_activate_voice_recognition(hfp_connection)){ 1043fd4151d1SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED; 1044fd4151d1SMilanka Ringwald done = hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1045fd4151d1SMilanka Ringwald } 1046de9e0ea7SMilanka Ringwald break; 1047de9e0ea7SMilanka Ringwald 1048de9e0ea7SMilanka Ringwald case 2: 1049135d9718SMilanka Ringwald if (hfp_ag_voice_recognition_session_active(hfp_connection)){ 1050de9e0ea7SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 1051de9e0ea7SMilanka Ringwald done = hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1052de9e0ea7SMilanka Ringwald } 1053de9e0ea7SMilanka Ringwald break; 1054de9e0ea7SMilanka Ringwald default: 1055de9e0ea7SMilanka Ringwald break; 1056fd4151d1SMilanka Ringwald } 1057fd4151d1SMilanka Ringwald if (done == 0){ 1058fd4151d1SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 1059fd4151d1SMilanka Ringwald done = hfp_ag_send_error(hfp_connection->rfcomm_cid); 1060fd4151d1SMilanka Ringwald return 1; 1061fd4151d1SMilanka Ringwald } 1062754910caSMilanka Ringwald 1063774e3016SMatthias Ringwald status = hfp_ag_vra_state_machine_two(hfp_connection); 10642cc52945SMatthias Ringwald return (status == ERROR_CODE_SUCCESS) ? 1 : 0; 1065de9e0ea7SMilanka Ringwald } 10662cc52945SMatthias Ringwald 10672cc52945SMatthias Ringwald return 0; 1068754910caSMilanka Ringwald } 1069754910caSMilanka Ringwald 1070a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){ 1071dc6af379SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) { 1072dc6af379SMatthias Ringwald return 0; 1073dc6af379SMatthias Ringwald } 1074dc6af379SMatthias Ringwald 1075c04cf7ffSMilanka Ringwald int sent = codecs_exchange_state_machine(hfp_connection); 1076c04cf7ffSMilanka Ringwald if (sent) return 1; 1077aa4dd815SMatthias Ringwald 107840a8ee13SMatthias Ringwald if (has_in_band_ring_tone()){ 107940a8ee13SMatthias Ringwald if (hfp_connection->ag_in_band_ring_tone_active != hfp_ag_in_band_ring_tone_active) { 108040a8ee13SMatthias Ringwald hfp_connection->ag_in_band_ring_tone_active = hfp_ag_in_band_ring_tone_active; 1081f197e761SMatthias Ringwald hfp_ag_send_change_in_band_ring_tone_setting_cmd(hfp_connection); 1082aa4dd815SMatthias Ringwald return 1; 10839430c71eSMatthias Ringwald } 108440a8ee13SMatthias Ringwald } 10859430c71eSMatthias Ringwald 10869430c71eSMatthias Ringwald switch(hfp_connection->command){ 1087aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1088485ac19eSMilanka Ringwald hfp_ag_send_report_network_operator_name_cmd(hfp_connection->rfcomm_cid, hfp_connection->network_operator); 1089aa4dd815SMatthias Ringwald return 1; 1090aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 1091a0ffb263SMatthias Ringwald if (hfp_connection->network_operator.format != 0){ 1092485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 1093aa4dd815SMatthias Ringwald } else { 1094485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 10953deb3ec6SMatthias Ringwald } 1096aa4dd815SMatthias Ringwald return 1; 1097aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE: 1098485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1099aa4dd815SMatthias Ringwald return 1; 11003deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR: 1101a0ffb263SMatthias Ringwald if (hfp_connection->extended_audio_gateway_error){ 1102a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 1103485ac19eSMilanka Ringwald hfp_ag_send_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, hfp_connection->extended_audio_gateway_error_value); 1104aa4dd815SMatthias Ringwald return 1; 11053deb3ec6SMatthias Ringwald } 1106202c8a4cSMatthias Ringwald break; 11073deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 1108485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1109ce263fc8SMatthias Ringwald return 1; 11103deb3ec6SMatthias Ringwald default: 11113deb3ec6SMatthias Ringwald break; 11123deb3ec6SMatthias Ringwald } 1113aa4dd815SMatthias Ringwald return 0; 11143deb3ec6SMatthias Ringwald } 11153deb3ec6SMatthias Ringwald 1116a0ffb263SMatthias Ringwald static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){ 1117505f1c30SMatthias Ringwald if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) || 1118505f1c30SMatthias Ringwald (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0; 11193deb3ec6SMatthias Ringwald 1120a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 112152cbdd6dSMilanka Ringwald 112252cbdd6dSMilanka Ringwald if (hfp_connection->release_audio_connection){ 112352cbdd6dSMilanka Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 112452cbdd6dSMilanka Ringwald hfp_connection->release_audio_connection = 0; 112552cbdd6dSMilanka Ringwald gap_disconnect(hfp_connection->sco_handle); 112652cbdd6dSMilanka Ringwald return 1; 112752cbdd6dSMilanka Ringwald } 1128aa4dd815SMatthias Ringwald 112929cddf58SMatthias Ringwald // accept incoming audio connection (codec negotiation is not used) 1130447743f7SMatthias Ringwald if (hfp_connection->accept_sco && (hfp_sco_setup_active() == false)){ 113129cddf58SMatthias Ringwald // notify about codec selection if not done already 113229cddf58SMatthias Ringwald if (hfp_connection->negotiated_codec == 0){ 113329cddf58SMatthias Ringwald hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 113429cddf58SMatthias Ringwald } 113529cddf58SMatthias Ringwald // 113629cddf58SMatthias Ringwald bool incoming_eSCO = hfp_connection->accept_sco == 2; 113729cddf58SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 113829cddf58SMatthias Ringwald hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO); 113929cddf58SMatthias Ringwald return 1; 114029cddf58SMatthias Ringwald } 114129cddf58SMatthias Ringwald 1142aa4dd815SMatthias Ringwald // run codecs exchange 1143c04cf7ffSMilanka Ringwald int sent = codecs_exchange_state_machine(hfp_connection); 1144c04cf7ffSMilanka Ringwald if (sent) return 1; 1145aa4dd815SMatthias Ringwald 1146c04cf7ffSMilanka Ringwald if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0; 1147447743f7SMatthias Ringwald if (hfp_sco_setup_active()) return 0; 1148d2e34ffbSMatthias Ringwald if (hci_can_send_command_packet_now() == false) return 0; 1149a0ffb263SMatthias Ringwald if (hfp_connection->establish_audio_connection){ 1150a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 1151a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 1152eddcd308SMatthias Ringwald hfp_setup_synchronous_connection(hfp_connection); 1153aa4dd815SMatthias Ringwald return 1; 1154aa4dd815SMatthias Ringwald } 1155aa4dd815SMatthias Ringwald return 0; 1156aa4dd815SMatthias Ringwald } 1157aa4dd815SMatthias Ringwald 1158056efd45SMatthias Ringwald static void hfp_ag_set_callsetup_indicator(void){ 1159056efd45SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup"); 1160056efd45SMatthias Ringwald if (!indicator){ 1161056efd45SMatthias Ringwald log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing"); 1162056efd45SMatthias Ringwald return; 1163056efd45SMatthias Ringwald }; 1164056efd45SMatthias Ringwald indicator->status = hfp_gsm_callsetup_status(); 1165056efd45SMatthias Ringwald } 1166056efd45SMatthias Ringwald 1167056efd45SMatthias Ringwald static void hfp_ag_set_callheld_indicator(void){ 1168056efd45SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld"); 1169056efd45SMatthias Ringwald if (!indicator){ 1170056efd45SMatthias Ringwald log_error("hfp_ag_set_callheld_state: callheld indicator is missing"); 1171056efd45SMatthias Ringwald return; 1172056efd45SMatthias Ringwald }; 1173056efd45SMatthias Ringwald indicator->status = hfp_gsm_callheld_status(); 1174056efd45SMatthias Ringwald } 1175056efd45SMatthias Ringwald 1176aa4dd815SMatthias Ringwald // 1177fcd59412SMilanka Ringwald // transition implementations for hfp_ag_call_state_machine 1178aa4dd815SMatthias Ringwald // 1179aa4dd815SMatthias Ringwald 11802ff1d7d7SMilanka Ringwald static void hfp_ag_hf_start_ringing_incoming(hfp_connection_t * hfp_connection){ 1181f197e761SMatthias Ringwald if (use_in_band_tone(hfp_connection)){ 1182a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING; 1183d97d752dSMilanka Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 1184aa4dd815SMatthias Ringwald } else { 11859493406bSMilanka Ringwald hfp_connection->call_state = HFP_CALL_INCOMING_RINGING; 1186aa4dd815SMatthias Ringwald } 1187aa4dd815SMatthias Ringwald } 1188aa4dd815SMatthias Ringwald 1189fcd59412SMilanka Ringwald static void hfp_ag_hf_start_ringing_outgoing(hfp_connection_t * hfp_connection){ 1190fcd59412SMilanka Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING; 1191fcd59412SMilanka Ringwald } 1192fcd59412SMilanka Ringwald 1193a0ffb263SMatthias Ringwald static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){ 1194a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 0; 1195a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = 0; 1196aa4dd815SMatthias Ringwald } 1197aa4dd815SMatthias Ringwald 1198a4f85bd2SMilanka Ringwald 119931f55b91SMilanka Ringwald static void hfp_ag_trigger_incoming_call_during_active_one(void){ 120031f55b91SMilanka Ringwald btstack_linked_list_iterator_t it; 120131f55b91SMilanka Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 120231f55b91SMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 120331f55b91SMilanka Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 120431f55b91SMilanka Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 120531f55b91SMilanka Ringwald if (hfp_connection->call_state != HFP_CALL_ACTIVE) continue; 120631f55b91SMilanka Ringwald 120731f55b91SMilanka Ringwald hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING; 120831f55b91SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 120931f55b91SMilanka Ringwald } 121031f55b91SMilanka Ringwald } 121131f55b91SMilanka Ringwald 1212d3709f3eSMatthias Ringwald static void hfp_ag_trigger_ring_and_clip(void) { 1213d3709f3eSMatthias Ringwald btstack_linked_list_iterator_t it; 1214d3709f3eSMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1215d3709f3eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1216d3709f3eSMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1217d3709f3eSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1218d3709f3eSMatthias Ringwald switch (hfp_connection->call_state){ 1219d3709f3eSMatthias Ringwald case HFP_CALL_INCOMING_RINGING: 1220d3709f3eSMatthias Ringwald case HFP_CALL_OUTGOING_RINGING: 1221d3709f3eSMatthias Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING: 12223561686eSMatthias Ringwald // Queue RING on this connection 1223a4f85bd2SMilanka Ringwald hfp_connection->ag_ring = 1; 12243561686eSMatthias Ringwald 12253561686eSMatthias Ringwald // HFP v1.8, 4.23 Calling Line Identification (CLI) Notification 12263561686eSMatthias Ringwald // "If the calling subscriber number information is available from the network, the AG shall issue the 12273561686eSMatthias Ringwald // +CLIP unsolicited result code just after every RING indication when the HF is alerted in an incoming call." 12283561686eSMatthias Ringwald hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled; 12293561686eSMatthias Ringwald 12303561686eSMatthias Ringwald // trigger next message 1231a4f85bd2SMilanka Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 1232b7e27351SMilanka Ringwald break; 1233b7e27351SMilanka Ringwald default: 1234b7e27351SMilanka Ringwald break; 1235b7e27351SMilanka Ringwald } 1236a4f85bd2SMilanka Ringwald } 1237a4f85bd2SMilanka Ringwald } 1238a4f85bd2SMilanka Ringwald 123950b9afa4SMilanka Ringwald // trigger RING and CLIP messages on connections that are ringing 124050b9afa4SMilanka Ringwald static void hfp_ag_ring_timeout_handler(btstack_timer_source_t * timer){ 124150b9afa4SMilanka Ringwald hfp_ag_trigger_ring_and_clip(); 124250b9afa4SMilanka Ringwald 12436db648bcSMilanka Ringwald btstack_run_loop_set_timer(timer, HFP_RING_PERIOD_MS); // 2 seconds timeout 124450b9afa4SMilanka Ringwald btstack_run_loop_add_timer(timer); 1245a4f85bd2SMilanka Ringwald } 1246a4f85bd2SMilanka Ringwald 1247a4f85bd2SMilanka Ringwald static void hfp_ag_ring_timeout_stop(void){ 1248a4f85bd2SMilanka Ringwald btstack_run_loop_remove_timer(&hfp_ag_ring_timeout); 1249a4f85bd2SMilanka Ringwald } 1250a4f85bd2SMilanka Ringwald 1251a4f85bd2SMilanka Ringwald static void hfp_ag_start_ringing(void){ 125250b9afa4SMilanka Ringwald // setup ring timer 125350b9afa4SMilanka Ringwald btstack_run_loop_remove_timer(&hfp_ag_ring_timeout); 125450b9afa4SMilanka Ringwald btstack_run_loop_set_timer_handler(&hfp_ag_ring_timeout, hfp_ag_ring_timeout_handler); 12556db648bcSMilanka Ringwald btstack_run_loop_set_timer(&hfp_ag_ring_timeout, HFP_RING_PERIOD_MS); // 2 seconds timeout 125650b9afa4SMilanka Ringwald btstack_run_loop_add_timer(&hfp_ag_ring_timeout); 125750b9afa4SMilanka Ringwald 125850b9afa4SMilanka Ringwald // emit start ringing 1259a4f85bd2SMilanka Ringwald hfp_ag_emit_general_simple_event(HFP_SUBEVENT_START_RINGING); 126050b9afa4SMilanka Ringwald 126150b9afa4SMilanka Ringwald // send initial RING + CLIP 126250b9afa4SMilanka Ringwald hfp_ag_trigger_ring_and_clip(); 1263a4f85bd2SMilanka Ringwald } 1264a4f85bd2SMilanka Ringwald 1265a4f85bd2SMilanka Ringwald static void hfp_ag_stop_ringing(void){ 1266a4f85bd2SMilanka Ringwald hfp_ag_ring_timeout_stop(); 1267a4f85bd2SMilanka Ringwald hfp_ag_emit_general_simple_event(HFP_SUBEVENT_STOP_RINGING); 1268a4f85bd2SMilanka Ringwald 1269a4f85bd2SMilanka Ringwald btstack_linked_list_iterator_t it; 1270a4f85bd2SMilanka Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1271a4f85bd2SMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1272a4f85bd2SMilanka Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1273a4f85bd2SMilanka Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1274a4f85bd2SMilanka Ringwald if ((hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) && 1275a4f85bd2SMilanka Ringwald (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue; 1276a4f85bd2SMilanka Ringwald hfp_ag_hf_stop_ringing(hfp_connection); 1277a4f85bd2SMilanka Ringwald } 1278a4f85bd2SMilanka Ringwald } 1279a4f85bd2SMilanka Ringwald 128031f55b91SMilanka Ringwald static void hfp_ag_trigger_incoming_call_idle(void){ 1281aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name("callsetup"); 1282aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 1283aa4dd815SMatthias Ringwald 12843561686eSMatthias Ringwald // update callsetup state in connections 1285665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1286665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1287665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1288a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 128966c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 129031f55b91SMilanka Ringwald if (hfp_connection->call_state != HFP_CALL_IDLE) continue; 129131f55b91SMilanka Ringwald 1292a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1293a4f85bd2SMilanka Ringwald 1294a4f85bd2SMilanka Ringwald hfp_ag_hf_start_ringing_incoming(hfp_connection); 1295a4f85bd2SMilanka Ringwald 1296f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1297aa4dd815SMatthias Ringwald } 12983561686eSMatthias Ringwald 12993561686eSMatthias Ringwald // start local ringing - started after connection state has been updated 13003561686eSMatthias Ringwald hfp_ag_start_ringing(); 1301aa4dd815SMatthias Ringwald } 1302aa4dd815SMatthias Ringwald 1303fe899794SMatthias Ringwald static void hfp_ag_trigger_outgoing_call(void){ 1304fe899794SMatthias Ringwald btstack_linked_list_iterator_t it; 1305fe899794SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1306fe899794SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1307fe899794SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1308fe899794SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1309fe899794SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 1310fe899794SMatthias Ringwald if (hfp_connection->call_state == HFP_CALL_IDLE){ 1311fe899794SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 1312fe899794SMatthias Ringwald } 1313fe899794SMatthias Ringwald } 1314fe899794SMatthias Ringwald } 13155a7033e2SMatthias Ringwald 13165a7033e2SMatthias Ringwald static void hfp_ag_handle_outgoing_call_accepted(hfp_connection_t * hfp_connection){ 13175a7033e2SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING; 13185a7033e2SMatthias Ringwald 13195a7033e2SMatthias Ringwald // trigger callsetup to be 13205a7033e2SMatthias Ringwald int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT; 13215a7033e2SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_ACCEPTED, 0, 0, NULL); 13225a7033e2SMatthias Ringwald 13235a7033e2SMatthias Ringwald int indicator_index ; 13245a7033e2SMatthias Ringwald 13255a7033e2SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 13265a7033e2SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callsetup"); 13275a7033e2SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 13285a7033e2SMatthias Ringwald 13295a7033e2SMatthias Ringwald // put current call on hold if active 13305a7033e2SMatthias Ringwald if (put_call_on_hold){ 13315a7033e2SMatthias Ringwald log_info("AG putting current call on hold for new outgoing call"); 13325a7033e2SMatthias Ringwald hfp_ag_set_callheld_indicator(); 13335a7033e2SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callheld"); 13345a7033e2SMatthias Ringwald hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]); 13355a7033e2SMatthias Ringwald } 13365a7033e2SMatthias Ringwald 13375a7033e2SMatthias Ringwald // start audio if needed 13385a7033e2SMatthias Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 13395a7033e2SMatthias Ringwald } 1340fe899794SMatthias Ringwald 13414a2e058fSMatthias Ringwald static void hfp_ag_transfer_indicator(const char * name){ 13424a2e058fSMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name(name); 1343aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 1344aa4dd815SMatthias Ringwald 1345665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1346665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1347665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1348a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 134966c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1350a0ffb263SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 1351a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1352f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1353aa4dd815SMatthias Ringwald } 1354aa4dd815SMatthias Ringwald } 1355aa4dd815SMatthias Ringwald 13564a2e058fSMatthias Ringwald static void hfp_ag_transfer_callsetup_state(void){ 13574a2e058fSMatthias Ringwald hfp_ag_transfer_indicator("callsetup"); 13584a2e058fSMatthias Ringwald } 13594a2e058fSMatthias Ringwald 1360aa4dd815SMatthias Ringwald static void hfp_ag_transfer_call_state(void){ 13614a2e058fSMatthias Ringwald hfp_ag_transfer_indicator("call"); 1362aa4dd815SMatthias Ringwald } 1363aa4dd815SMatthias Ringwald 1364aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callheld_state(void){ 13654a2e058fSMatthias Ringwald hfp_ag_transfer_indicator("callheld"); 1366aa4dd815SMatthias Ringwald } 1367aa4dd815SMatthias Ringwald 1368aa4dd815SMatthias Ringwald static void hfp_ag_hf_accept_call(hfp_connection_t * source){ 1369aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 1370aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1371aa4dd815SMatthias Ringwald 1372a4f85bd2SMilanka Ringwald hfp_ag_stop_ringing(); 1373a4f85bd2SMilanka Ringwald 1374665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1375665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1376665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1377a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 137866c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 13799493406bSMilanka Ringwald if ((hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) && 1380505f1c30SMatthias Ringwald (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue; 1381aa4dd815SMatthias Ringwald 1382a0ffb263SMatthias Ringwald if (hfp_connection == source){ 1383aa4dd815SMatthias Ringwald 1384f197e761SMatthias Ringwald if (use_in_band_tone(hfp_connection)){ 1385a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1386aa4dd815SMatthias Ringwald } else { 1387a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE; 1388d97d752dSMilanka Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 1389aa4dd815SMatthias Ringwald } 1390aa4dd815SMatthias Ringwald 1391a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1392a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1393aa4dd815SMatthias Ringwald 1394aa4dd815SMatthias Ringwald } else { 1395a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 1396aa4dd815SMatthias Ringwald } 1397f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1398aa4dd815SMatthias Ringwald } 1399aa4dd815SMatthias Ringwald } 1400aa4dd815SMatthias Ringwald 1401aa4dd815SMatthias Ringwald static void hfp_ag_ag_accept_call(void){ 1402aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 1403aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1404aa4dd815SMatthias Ringwald 1405a4f85bd2SMilanka Ringwald hfp_ag_stop_ringing(); 1406a4f85bd2SMilanka Ringwald 1407665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1408665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1409665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1410a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 141166c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1412a5764cd2SMatthias Ringwald if ((hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) && 1413a5764cd2SMatthias Ringwald (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue; 1414aa4dd815SMatthias Ringwald 1415a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION; 1416aa4dd815SMatthias Ringwald 1417a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1418a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1419aa4dd815SMatthias Ringwald 1420f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1421aa4dd815SMatthias Ringwald break; // only single 1422aa4dd815SMatthias Ringwald } 1423aa4dd815SMatthias Ringwald } 1424aa4dd815SMatthias Ringwald 1425aa4dd815SMatthias Ringwald static void hfp_ag_trigger_reject_call(void){ 1426aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1427a4f85bd2SMilanka Ringwald 1428a4f85bd2SMilanka Ringwald hfp_ag_stop_ringing(); 1429a4f85bd2SMilanka Ringwald 1430665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1431665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1432665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1433665d90f2SMatthias Ringwald hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 143466c5995fSMatthias Ringwald if (connection->local_role != HFP_ROLE_AG) continue; 143507a44c5eSMilanka Ringwald 143607a44c5eSMilanka Ringwald switch (connection->call_state){ 143707a44c5eSMilanka Ringwald case HFP_CALL_INCOMING_RINGING: 143807a44c5eSMilanka Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING: 143907a44c5eSMilanka Ringwald case HFP_CALL_OUTGOING_RINGING: 144007a44c5eSMilanka Ringwald case HFP_CALL_OUTGOING_INITIATED: 144107a44c5eSMilanka Ringwald case HFP_CALL_OUTGOING_DIALING: 144207a44c5eSMilanka Ringwald break; 144307a44c5eSMilanka Ringwald default: 144407a44c5eSMilanka Ringwald continue; 144507a44c5eSMilanka Ringwald } 1446095a9f2bSMilanka Ringwald 1447aa4dd815SMatthias Ringwald connection->call_state = HFP_CALL_IDLE; 1448095a9f2bSMilanka Ringwald connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1449095a9f2bSMilanka Ringwald 1450095a9f2bSMilanka Ringwald if (connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 1451095a9f2bSMilanka Ringwald connection->release_audio_connection = 1; 1452095a9f2bSMilanka Ringwald } 1453095a9f2bSMilanka Ringwald hfp_emit_simple_event(connection, HFP_SUBEVENT_CALL_TERMINATED); 1454f0aeb307SMatthias Ringwald hfp_ag_run_for_context(connection); 1455095a9f2bSMilanka Ringwald rfcomm_request_can_send_now_event(connection->rfcomm_cid); 1456aa4dd815SMatthias Ringwald } 1457aa4dd815SMatthias Ringwald } 1458aa4dd815SMatthias Ringwald 1459aa4dd815SMatthias Ringwald static void hfp_ag_trigger_terminate_call(void){ 1460aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 1461aa4dd815SMatthias Ringwald 1462a4f85bd2SMilanka Ringwald // no ringing during call 1463a4f85bd2SMilanka Ringwald 1464665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1465665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1466665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1467a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 146866c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1469a0ffb263SMatthias Ringwald if (hfp_connection->call_state == HFP_CALL_IDLE) continue; 1470fb75b483SMilanka Ringwald 1471a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 1472a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1473095a9f2bSMilanka Ringwald 14746d78145cSMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 1475a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 1; 14766d78145cSMatthias Ringwald } 1477095a9f2bSMilanka Ringwald 1478fb75b483SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED); 1479f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1480a13ff7d1SMilanka Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 1481aa4dd815SMatthias Ringwald } 1482aa4dd815SMatthias Ringwald } 1483aa4dd815SMatthias Ringwald 14840cb5b971SMatthias Ringwald static void hfp_ag_set_call_indicator(void){ 1485aa4dd815SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call"); 1486aa4dd815SMatthias Ringwald if (!indicator){ 1487aa4dd815SMatthias Ringwald log_error("hfp_ag_set_call_state: call indicator is missing"); 148845718b6fSMatthias Ringwald return; 1489aa4dd815SMatthias Ringwald }; 1490d210d9c4SMatthias Ringwald indicator->status = hfp_gsm_call_status(); 1491aa4dd815SMatthias Ringwald } 1492aa4dd815SMatthias Ringwald 1493aa4dd815SMatthias Ringwald static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){ 1494665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1495665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1496665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1497a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 149866c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1499a0ffb263SMatthias Ringwald if (hfp_connection->call_state == call_state) return hfp_connection; 1500aa4dd815SMatthias Ringwald } 1501aa4dd815SMatthias Ringwald return NULL; 1502aa4dd815SMatthias Ringwald } 1503aa4dd815SMatthias Ringwald 1504a5bdcda8SMatthias Ringwald static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){ 1505665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1506665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1507665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1508a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 150966c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1510a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = state + 1; 1511ce263fc8SMatthias Ringwald } 1512ce263fc8SMatthias Ringwald } 1513ce263fc8SMatthias Ringwald 1514a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 1515aa4dd815SMatthias Ringwald int indicator_index; 1516a0ffb263SMatthias Ringwald switch (hfp_connection->call_state){ 1517aa4dd815SMatthias Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING: 1518a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 1519a0ffb263SMatthias Ringwald // we got event: audio hfp_connection established 15209493406bSMilanka Ringwald hfp_connection->call_state = HFP_CALL_INCOMING_RINGING; 1521aa4dd815SMatthias Ringwald break; 1522aa4dd815SMatthias Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE: 1523a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 1524a0ffb263SMatthias Ringwald // we got event: audio hfp_connection established 1525a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1526aa4dd815SMatthias Ringwald break; 1527aa4dd815SMatthias Ringwald case HFP_CALL_W2_SEND_CALL_WAITING: 1528a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_CHLD; 1529a0ffb263SMatthias Ringwald hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid); 1530aa4dd815SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callsetup"); 1531a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1532aa4dd815SMatthias Ringwald break; 1533aa4dd815SMatthias Ringwald default: 1534aa4dd815SMatthias Ringwald break; 1535aa4dd815SMatthias Ringwald } 1536aa4dd815SMatthias Ringwald return 0; 1537aa4dd815SMatthias Ringwald } 153839426febSMatthias Ringwald 1539e84fa067SMatthias Ringwald // queue 'error' to HF 1540e84fa067SMatthias Ringwald static void hfp_ag_queue_error(hfp_connection_t * hfp_connection){ 1541*5e87d1ceSMatthias Ringwald hfp_connection->send_error++; 1542e84fa067SMatthias Ringwald } 1543e84fa067SMatthias Ringwald 1544e84fa067SMatthias Ringwald // queue 'ok' to HF 1545e84fa067SMatthias Ringwald static void hfp_ag_queue_ok(hfp_connection_t * hfp_connection){ 1546*5e87d1ceSMatthias Ringwald hfp_connection->ok_pending++; 1547e84fa067SMatthias Ringwald } 1548e84fa067SMatthias Ringwald 154939426febSMatthias Ringwald // functions extracted from hfp_ag_call_sm below 15509f9621cfSMatthias Ringwald static void hfp_ag_handle_reject_outgoing_call(void){ 155139426febSMatthias Ringwald hfp_connection_t * hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 155239426febSMatthias Ringwald if (!hfp_connection){ 155339426febSMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state"); 155439426febSMatthias Ringwald return; 155539426febSMatthias Ringwald } 155639426febSMatthias Ringwald 1557f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_REJECTED, 0, 0, NULL); 155839426febSMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 1559e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 156039426febSMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 156139426febSMatthias Ringwald } 156239426febSMatthias Ringwald 1563a0ffb263SMatthias Ringwald // hfp_connection is used to identify originating HF 1564a0ffb263SMatthias Ringwald static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){ 156581e25d0aSMatthias Ringwald 156681e25d0aSMatthias Ringwald // allow to intercept call statemachine events 156781e25d0aSMatthias Ringwald if (hfp_ag_custom_call_sm_handler != NULL){ 156881e25d0aSMatthias Ringwald bool handle_event = (*hfp_ag_custom_call_sm_handler)(event); 156981e25d0aSMatthias Ringwald if (!handle_event) return; 157081e25d0aSMatthias Ringwald } 157181e25d0aSMatthias Ringwald 1572d210d9c4SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1573d210d9c4SMatthias Ringwald int callheld_indicator_index = get_ag_indicator_index_for_name("callheld"); 1574d210d9c4SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 157574386ee0SMatthias Ringwald 1576aa4dd815SMatthias Ringwald switch (event){ 1577aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL: 1578d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1579aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1580d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 158112cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1582f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL, 0, 0, NULL); 1583d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 158431f55b91SMilanka Ringwald hfp_ag_trigger_incoming_call_idle(); 158560ebb071SMilanka Ringwald log_info("AG rings"); 1586aa4dd815SMatthias Ringwald break; 1587aa4dd815SMatthias Ringwald default: 15883deb3ec6SMatthias Ringwald break; 15893deb3ec6SMatthias Ringwald } 15903deb3ec6SMatthias Ringwald break; 1591aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1592d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 159312cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1594f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL, 0, 0, NULL); 1595d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 159631f55b91SMilanka Ringwald hfp_ag_trigger_incoming_call_during_active_one(); 159760ebb071SMilanka Ringwald log_info("AG call waiting"); 1598aa4dd815SMatthias Ringwald break; 1599aa4dd815SMatthias Ringwald default: 16003deb3ec6SMatthias Ringwald break; 16013deb3ec6SMatthias Ringwald } 16023deb3ec6SMatthias Ringwald break; 16037bbeb3adSMilanka Ringwald default: 16047bbeb3adSMilanka Ringwald break; 1605aa4dd815SMatthias Ringwald } 1606aa4dd815SMatthias Ringwald break; 1607aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG: 1608d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1609aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1610d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 161112cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1612f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, 0, 0, NULL); 1613d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1614d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1615aa4dd815SMatthias Ringwald hfp_ag_ag_accept_call(); 161660ebb071SMilanka Ringwald log_info("AG answers call, accept call by GSM"); 1617aa4dd815SMatthias Ringwald break; 1618aa4dd815SMatthias Ringwald default: 16193deb3ec6SMatthias Ringwald break; 16203deb3ec6SMatthias Ringwald } 1621aa4dd815SMatthias Ringwald break; 1622aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1623d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1624aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 162560ebb071SMilanka Ringwald log_info("AG: current call is placed on hold, incoming call gets active"); 1626f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, 0, 0, NULL); 1627d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1628d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1629ce263fc8SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1630ce263fc8SMatthias Ringwald hfp_ag_transfer_callheld_state(); 1631aa4dd815SMatthias Ringwald break; 1632aa4dd815SMatthias Ringwald default: 1633aa4dd815SMatthias Ringwald break; 1634aa4dd815SMatthias Ringwald } 1635aa4dd815SMatthias Ringwald break; 16367bbeb3adSMilanka Ringwald default: 16377bbeb3adSMilanka Ringwald break; 1638aa4dd815SMatthias Ringwald } 1639aa4dd815SMatthias Ringwald break; 1640ce263fc8SMatthias Ringwald 1641ce263fc8SMatthias Ringwald case HFP_AG_HELD_CALL_JOINED_BY_AG: 1642d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1643ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1644d0c20769SMatthias Ringwald switch (hfp_gsm_callheld_status()){ 1645ce263fc8SMatthias Ringwald case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED: 164660ebb071SMilanka Ringwald log_info("AG: joining held call with active call"); 1647f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_HELD_CALL_JOINED_BY_AG, 0, 0, NULL); 1648d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1649ce263fc8SMatthias Ringwald hfp_ag_transfer_callheld_state(); 1650ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL); 1651ce263fc8SMatthias Ringwald break; 1652ce263fc8SMatthias Ringwald default: 1653ce263fc8SMatthias Ringwald break; 1654ce263fc8SMatthias Ringwald } 1655ce263fc8SMatthias Ringwald break; 1656ce263fc8SMatthias Ringwald default: 1657ce263fc8SMatthias Ringwald break; 1658ce263fc8SMatthias Ringwald } 1659ce263fc8SMatthias Ringwald break; 1660ce263fc8SMatthias Ringwald 1661aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF: 1662d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1663aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1664d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 166512cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1666f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, 0, 0, NULL); 1667d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1668d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1669a0ffb263SMatthias Ringwald hfp_ag_hf_accept_call(hfp_connection); 1670e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 167160ebb071SMilanka Ringwald log_info("HF answers call, accept call by GSM"); 1672ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED); 1673aa4dd815SMatthias Ringwald break; 1674aa4dd815SMatthias Ringwald default: 1675aa4dd815SMatthias Ringwald break; 1676aa4dd815SMatthias Ringwald } 16773deb3ec6SMatthias Ringwald break; 16783deb3ec6SMatthias Ringwald default: 16793deb3ec6SMatthias Ringwald break; 16803deb3ec6SMatthias Ringwald } 16813deb3ec6SMatthias Ringwald break; 16823deb3ec6SMatthias Ringwald 1683ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG: 1684d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1685ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1686d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 168712cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1688f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, 0, 0, NULL); 168927bb1817SMatthias Ringwald hfp_ag_response_and_hold_active = true; 1690ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1691a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1692ac6f828eSMilanka Ringwald // as with regular call 1693d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1694d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1695ce263fc8SMatthias Ringwald hfp_ag_ag_accept_call(); 169660ebb071SMilanka Ringwald log_info("AG response and hold - hold by AG"); 1697ce263fc8SMatthias Ringwald break; 1698ce263fc8SMatthias Ringwald default: 1699ce263fc8SMatthias Ringwald break; 1700ce263fc8SMatthias Ringwald } 1701ce263fc8SMatthias Ringwald break; 1702ce263fc8SMatthias Ringwald default: 1703ce263fc8SMatthias Ringwald break; 1704ce263fc8SMatthias Ringwald } 1705ce263fc8SMatthias Ringwald break; 1706ce263fc8SMatthias Ringwald 1707ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF: 1708d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1709ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1710d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 171112cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1712f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, 0, 0, NULL); 171327bb1817SMatthias Ringwald hfp_ag_response_and_hold_active = true; 1714ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1715a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1716ac6f828eSMilanka Ringwald // as with regular call 1717d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1718d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1719a0ffb263SMatthias Ringwald hfp_ag_hf_accept_call(hfp_connection); 172060ebb071SMilanka Ringwald log_info("AG response and hold - hold by HF"); 1721ce263fc8SMatthias Ringwald break; 1722ce263fc8SMatthias Ringwald default: 1723ce263fc8SMatthias Ringwald break; 1724ce263fc8SMatthias Ringwald } 1725ce263fc8SMatthias Ringwald break; 1726ce263fc8SMatthias Ringwald default: 1727ce263fc8SMatthias Ringwald break; 1728ce263fc8SMatthias Ringwald } 1729ce263fc8SMatthias Ringwald break; 1730ce263fc8SMatthias Ringwald 1731ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG: 1732ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF: 1733ce263fc8SMatthias Ringwald if (!hfp_ag_response_and_hold_active) break; 1734ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1735f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, 0, 0, NULL); 173627bb1817SMatthias Ringwald hfp_ag_response_and_hold_active = false; 1737ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED; 1738a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 173960ebb071SMilanka Ringwald log_info("Held Call accepted and active"); 1740ce263fc8SMatthias Ringwald break; 1741ce263fc8SMatthias Ringwald 1742ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG: 1743ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF: 1744ce263fc8SMatthias Ringwald if (!hfp_ag_response_and_hold_active) break; 1745ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1746f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, 0, 0, NULL); 174727bb1817SMatthias Ringwald hfp_ag_response_and_hold_active = false; 1748ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1749a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1750ce263fc8SMatthias Ringwald // from terminate by ag 1751d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1752ce263fc8SMatthias Ringwald hfp_ag_trigger_terminate_call(); 1753ce263fc8SMatthias Ringwald break; 1754ce263fc8SMatthias Ringwald 1755aa4dd815SMatthias Ringwald case HFP_AG_TERMINATE_CALL_BY_HF: 1756d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1757aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1758d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 175912cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 176007a44c5eSMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 176107a44c5eSMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 1762f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL); 1763d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1764aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1765aa4dd815SMatthias Ringwald hfp_ag_trigger_reject_call(); 176660ebb071SMilanka Ringwald log_info("HF Rejected Incoming call, AG terminate call"); 1767aa4dd815SMatthias Ringwald break; 1768aa4dd815SMatthias Ringwald default: 1769aa4dd815SMatthias Ringwald break; 1770aa4dd815SMatthias Ringwald } 1771aa4dd815SMatthias Ringwald break; 177212cbbeeeSMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1773f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL); 17741ec112deSMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1775d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 17761ec112deSMatthias Ringwald hfp_ag_trigger_terminate_call(); 177760ebb071SMilanka Ringwald log_info("AG terminate call"); 1778aa4dd815SMatthias Ringwald break; 17797bbeb3adSMilanka Ringwald default: 17807bbeb3adSMilanka Ringwald break; 1781aa4dd815SMatthias Ringwald } 1782aa4dd815SMatthias Ringwald break; 17833deb3ec6SMatthias Ringwald 1784aa4dd815SMatthias Ringwald case HFP_AG_TERMINATE_CALL_BY_AG: 1785d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1786aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1787d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 178807a44c5eSMilanka Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 178907a44c5eSMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 179007a44c5eSMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1791f2aa99a9SMilanka Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1792f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_AG, 0, 0, NULL); 1793d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1794aa4dd815SMatthias Ringwald hfp_ag_trigger_reject_call(); 179560ebb071SMilanka Ringwald log_info("AG Rejected Incoming call, AG terminate call"); 1796aa4dd815SMatthias Ringwald break; 1797aa4dd815SMatthias Ringwald default: 1798aa4dd815SMatthias Ringwald break; 17993deb3ec6SMatthias Ringwald } 1800202c8a4cSMatthias Ringwald break; 180112cbbeeeSMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1802f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_AG, 0, 0, NULL); 1803d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1804d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1805aa4dd815SMatthias Ringwald hfp_ag_trigger_terminate_call(); 180660ebb071SMilanka Ringwald log_info("AG terminate call"); 1807aa4dd815SMatthias Ringwald break; 1808aa4dd815SMatthias Ringwald default: 18093deb3ec6SMatthias Ringwald break; 18103deb3ec6SMatthias Ringwald } 18113deb3ec6SMatthias Ringwald break; 1812aa4dd815SMatthias Ringwald case HFP_AG_CALL_DROPPED: 1813d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1814aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1815d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1816aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1817aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1818fb75b483SMilanka Ringwald hfp_ag_stop_ringing(); 1819fb75b483SMilanka Ringwald break; 1820fb75b483SMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 1821fb75b483SMilanka Ringwald log_info("Outgoing call interrupted\n"); 1822aa4dd815SMatthias Ringwald break; 1823aa4dd815SMatthias Ringwald default: 18243deb3ec6SMatthias Ringwald break; 18253deb3ec6SMatthias Ringwald } 1826f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL); 1827d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1828aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 182910c236bfSMatthias Ringwald hfp_ag_trigger_terminate_call(); 1830aa4dd815SMatthias Ringwald break; 1831aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1832ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_active) { 1833f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL); 1834ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1835a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1836ce263fc8SMatthias Ringwald } 1837f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL); 1838d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1839d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1840aa4dd815SMatthias Ringwald hfp_ag_trigger_terminate_call(); 184160ebb071SMilanka Ringwald log_info("AG notify call dropped"); 1842aa4dd815SMatthias Ringwald break; 1843aa4dd815SMatthias Ringwald default: 1844aa4dd815SMatthias Ringwald break; 1845aa4dd815SMatthias Ringwald } 1846aa4dd815SMatthias Ringwald break; 1847aa4dd815SMatthias Ringwald 18489ff73f41SMatthias Ringwald case HFP_AG_OUTGOING_CALL_INITIATED_BY_HF: 1849d210d9c4SMatthias Ringwald // directly reject call if number of free slots is exceeded 1850d210d9c4SMatthias Ringwald if (!hfp_gsm_call_possible()){ 1851e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 1852f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1853d210d9c4SMatthias Ringwald break; 1854d210d9c4SMatthias Ringwald } 1855fe899794SMatthias Ringwald 1856fe899794SMatthias Ringwald // note: number not used currently 18579ff73f41SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_INITIATED_BY_HF, 0, 0, (const char *) &hfp_connection->line_buffer[3]); 18589cae807eSMatthias Ringwald 1859a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 186074386ee0SMatthias Ringwald 1861ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]); 1862aa4dd815SMatthias Ringwald break; 1863aa4dd815SMatthias Ringwald 1864fe899794SMatthias Ringwald case HFP_AG_OUTGOING_CALL_INITIATED_BY_AG: 1865fe899794SMatthias Ringwald // directly reject call if number of free slots is exceeded 1866fe899794SMatthias Ringwald if (!hfp_gsm_call_possible()) { 1867fe899794SMatthias Ringwald // TODO: no error for user 1868fe899794SMatthias Ringwald break; 1869fe899794SMatthias Ringwald } 1870fe899794SMatthias Ringwald switch (hfp_gsm_call_status()) { 1871fe899794SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1872fe899794SMatthias Ringwald switch (hfp_gsm_callsetup_status()) { 1873fe899794SMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1874fe899794SMatthias Ringwald // note: number not used currently 1875fe899794SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_INITIATED_BY_AG, 0, 0, "1234567"); 1876fe899794SMatthias Ringwald // init call state for all connections 1877fe899794SMatthias Ringwald hfp_ag_trigger_outgoing_call(); 1878fe899794SMatthias Ringwald // get first one and accept call 1879fe899794SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 1880fe899794SMatthias Ringwald if (hfp_connection) { 1881fe899794SMatthias Ringwald hfp_ag_handle_outgoing_call_accepted(hfp_connection); 1882fe899794SMatthias Ringwald } 1883fe899794SMatthias Ringwald break; 1884fe899794SMatthias Ringwald default: 1885fe899794SMatthias Ringwald // TODO: no error for user 1886fe899794SMatthias Ringwald break; 1887fe899794SMatthias Ringwald } 1888fe899794SMatthias Ringwald break; 1889fe899794SMatthias Ringwald default: 1890fe899794SMatthias Ringwald // TODO: no error for user 1891fe899794SMatthias Ringwald break; 1892fe899794SMatthias Ringwald } 1893fe899794SMatthias Ringwald break; 18949cae807eSMatthias Ringwald case HFP_AG_OUTGOING_REDIAL_INITIATED:{ 1895d210d9c4SMatthias Ringwald // directly reject call if number of free slots is exceeded 1896d210d9c4SMatthias Ringwald if (!hfp_gsm_call_possible()){ 1897e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 1898f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1899d210d9c4SMatthias Ringwald break; 1900d210d9c4SMatthias Ringwald } 1901d210d9c4SMatthias Ringwald 1902f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_REDIAL_INITIATED, 0, 0, NULL); 1903a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 190474386ee0SMatthias Ringwald 190560ebb071SMilanka Ringwald log_info("Redial last number"); 19069cae807eSMatthias Ringwald char * last_dialed_number = hfp_gsm_last_dialed_number(); 1907aa4dd815SMatthias Ringwald 19089cae807eSMatthias Ringwald if (strlen(last_dialed_number) > 0){ 190960ebb071SMilanka Ringwald log_info("Last number exists: accept call"); 1910ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number); 19119cae807eSMatthias Ringwald } else { 191260ebb071SMilanka Ringwald log_info("log_infoLast number missing: reject call"); 19139f9621cfSMatthias Ringwald hfp_ag_handle_reject_outgoing_call(); 19149cae807eSMatthias Ringwald } 19159cae807eSMatthias Ringwald break; 19169cae807eSMatthias Ringwald } 1917aa4dd815SMatthias Ringwald case HFP_AG_OUTGOING_CALL_REJECTED: 19189f9621cfSMatthias Ringwald hfp_ag_handle_reject_outgoing_call(); 1919aa4dd815SMatthias Ringwald break; 1920aa4dd815SMatthias Ringwald 1921d210d9c4SMatthias Ringwald case HFP_AG_OUTGOING_CALL_ACCEPTED:{ 1922a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 1923a0ffb263SMatthias Ringwald if (!hfp_connection){ 1924a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state"); 1925aa4dd815SMatthias Ringwald break; 1926aa4dd815SMatthias Ringwald } 1927aa4dd815SMatthias Ringwald 1928e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 19295a7033e2SMatthias Ringwald hfp_ag_handle_outgoing_call_accepted(hfp_connection); 1930aa4dd815SMatthias Ringwald break; 1931d210d9c4SMatthias Ringwald } 1932aa4dd815SMatthias Ringwald case HFP_AG_OUTGOING_CALL_RINGING: 1933a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1934a0ffb263SMatthias Ringwald if (!hfp_connection){ 1935a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state"); 1936aa4dd815SMatthias Ringwald break; 1937aa4dd815SMatthias Ringwald } 1938d210d9c4SMatthias Ringwald 1939f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_RINGING, 0, 0, NULL); 1940a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING; 1941d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1942aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1943fcd59412SMilanka Ringwald hfp_ag_hf_start_ringing_outgoing(hfp_connection); 1944aa4dd815SMatthias Ringwald break; 1945aa4dd815SMatthias Ringwald 1946d210d9c4SMatthias Ringwald case HFP_AG_OUTGOING_CALL_ESTABLISHED:{ 1947aa4dd815SMatthias Ringwald // get outgoing call 1948a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING); 1949a0ffb263SMatthias Ringwald if (!hfp_connection){ 1950a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1951aa4dd815SMatthias Ringwald } 1952a0ffb263SMatthias Ringwald if (!hfp_connection){ 1953a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection"); 1954aa4dd815SMatthias Ringwald break; 1955aa4dd815SMatthias Ringwald } 1956d210d9c4SMatthias Ringwald 1957addc49c4SMilanka 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; 1958f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_ESTABLISHED, 0, 0, NULL); 1959a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1960d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1961d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1962aa4dd815SMatthias Ringwald hfp_ag_transfer_call_state(); 1963aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1964addc49c4SMilanka Ringwald if (callheld_status_call_on_hold_and_no_active_calls){ 1965d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1966aa4dd815SMatthias Ringwald hfp_ag_transfer_callheld_state(); 19673deb3ec6SMatthias Ringwald } 1968addc49c4SMilanka Ringwald hfp_ag_hf_stop_ringing(hfp_connection); 1969674ebed5SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED); 19703deb3ec6SMatthias Ringwald break; 1971d210d9c4SMatthias Ringwald } 1972d210d9c4SMatthias Ringwald 197312cbbeeeSMatthias Ringwald case HFP_AG_CALL_HOLD_USER_BUSY: 1974f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_USER_BUSY, 0, 0, NULL); 1975d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1976a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1977a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 197860ebb071SMilanka Ringwald log_info("AG: Call Waiting, User Busy"); 1979d210d9c4SMatthias Ringwald break; 1980d210d9c4SMatthias Ringwald 1981d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{ 1982d0c20769SMatthias Ringwald int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1983d0c20769SMatthias Ringwald int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD; 1984d210d9c4SMatthias Ringwald 1985d210d9c4SMatthias Ringwald // Releases all active calls (if any exist) and accepts the other (held or waiting) call. 1986d0c20769SMatthias Ringwald if (call_held || call_setup_in_progress){ 1987f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index, 1988f8737b81SMatthias Ringwald 0, NULL); 1989d0c20769SMatthias Ringwald 1990d0c20769SMatthias Ringwald } 1991d0c20769SMatthias Ringwald 1992d210d9c4SMatthias Ringwald if (call_setup_in_progress){ 199360ebb071SMilanka Ringwald log_info("AG: Call Dropped, Accept new call"); 1994d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1995a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1996d210d9c4SMatthias Ringwald } else { 199760ebb071SMilanka Ringwald log_info("AG: Call Dropped, Resume held call"); 1998d210d9c4SMatthias Ringwald } 1999d210d9c4SMatthias Ringwald if (call_held){ 2000d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 2001a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 2002d210d9c4SMatthias Ringwald } 2003d0c20769SMatthias Ringwald 2004a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 2005d210d9c4SMatthias Ringwald break; 2006d210d9c4SMatthias Ringwald } 2007d0c20769SMatthias Ringwald 2008d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{ 2009d210d9c4SMatthias Ringwald // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call. 2010d210d9c4SMatthias Ringwald // only update if callsetup changed 2011d0c20769SMatthias Ringwald int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 2012f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index, 0, 2013f8737b81SMatthias Ringwald NULL); 2014d0c20769SMatthias Ringwald 2015d210d9c4SMatthias Ringwald if (call_setup_in_progress){ 201660ebb071SMilanka Ringwald log_info("AG: Call on Hold, Accept new call"); 2017d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 2018a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 2019d210d9c4SMatthias Ringwald } else { 202060ebb071SMilanka Ringwald log_info("AG: Swap calls"); 2021d210d9c4SMatthias Ringwald } 2022d0c20769SMatthias Ringwald 2023d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 2024d0c20769SMatthias Ringwald // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED); 2025a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 2026a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 2027d210d9c4SMatthias Ringwald break; 2028d210d9c4SMatthias Ringwald } 2029d0c20769SMatthias Ringwald 2030d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_ADD_HELD_CALL: 2031d210d9c4SMatthias Ringwald // Adds a held call to the conversation. 2032d0c20769SMatthias Ringwald if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){ 203360ebb071SMilanka Ringwald log_info("AG: Join 3-way-call"); 2034f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_ADD_HELD_CALL, 0, 0, NULL); 2035d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 2036a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 2037ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL); 2038d210d9c4SMatthias Ringwald } 2039a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 2040d210d9c4SMatthias Ringwald break; 2041d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS: 2042d210d9c4SMatthias Ringwald // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer) 2043f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, 0, 0, NULL); 204460ebb071SMilanka Ringwald log_info("AG: Transfer call -> Connect two calls and disconnect"); 2045d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 2046d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 2047a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 2048a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 2049a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 2050d210d9c4SMatthias Ringwald break; 2051ce263fc8SMatthias Ringwald 20523deb3ec6SMatthias Ringwald default: 20533deb3ec6SMatthias Ringwald break; 20543deb3ec6SMatthias Ringwald } 2055d210d9c4SMatthias Ringwald 2056d0c20769SMatthias Ringwald 20573deb3ec6SMatthias Ringwald } 20583deb3ec6SMatthias Ringwald 20599cae807eSMatthias Ringwald 2060a0ffb263SMatthias Ringwald static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){ 20619cae807eSMatthias Ringwald hfp_gsm_call_t * active_call = hfp_gsm_call(call_index); 20629cae807eSMatthias Ringwald if (!active_call) return; 20639cae807eSMatthias Ringwald 20649cae807eSMatthias Ringwald int idx = active_call->index; 20659cae807eSMatthias Ringwald hfp_enhanced_call_dir_t dir = active_call->direction; 20669cae807eSMatthias Ringwald hfp_enhanced_call_status_t status = active_call->enhanced_status; 20679cae807eSMatthias Ringwald hfp_enhanced_call_mode_t mode = active_call->mode; 20689cae807eSMatthias Ringwald hfp_enhanced_call_mpty_t mpty = active_call->mpty; 20699cae807eSMatthias Ringwald uint8_t type = active_call->clip_type; 20709cae807eSMatthias Ringwald char * number = active_call->clip_number; 20719cae807eSMatthias Ringwald 20729cae807eSMatthias Ringwald char buffer[100]; 20739cae807eSMatthias Ringwald // TODO: check length of a buffer, to fit the MTU 20749cae807eSMatthias Ringwald int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty); 20759cae807eSMatthias Ringwald if (number){ 20761cc1d9e9SMilanka Ringwald offset += snprintf(buffer+offset, sizeof(buffer)-offset-3, ", \"%s\",%u", number, type); 20779cae807eSMatthias Ringwald } 20789cae807eSMatthias Ringwald snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 207960ebb071SMilanka 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, 20809cae807eSMatthias Ringwald mode, mpty, type, number); 2081a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 20829cae807eSMatthias Ringwald } 20839cae807eSMatthias Ringwald 2084febc14f5SMatthias Ringwald // sends pending command, returns if command was sent 2085febc14f5SMatthias Ringwald static int hfp_ag_send_commands(hfp_connection_t *hfp_connection){ 2086a0ffb263SMatthias Ringwald if (hfp_connection->send_status_of_current_calls){ 2087a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 2088a0ffb263SMatthias Ringwald if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){ 2089a0ffb263SMatthias Ringwald hfp_connection->next_call_index++; 2090a0ffb263SMatthias Ringwald hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index); 2091febc14f5SMatthias Ringwald return 1; 20929cae807eSMatthias Ringwald } else { 2093febc14f5SMatthias 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 2094a0ffb263SMatthias Ringwald hfp_connection->next_call_index = 0; 2095e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2096a0ffb263SMatthias Ringwald hfp_connection->send_status_of_current_calls = 0; 20979cae807eSMatthias Ringwald } 2098ce263fc8SMatthias Ringwald } 2099ce263fc8SMatthias Ringwald 2100a0ffb263SMatthias Ringwald if (hfp_connection->ag_notify_incoming_call_waiting){ 2101a0ffb263SMatthias Ringwald hfp_connection->ag_notify_incoming_call_waiting = 0; 2102a0ffb263SMatthias Ringwald hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid); 2103febc14f5SMatthias Ringwald return 1; 2104aa4dd815SMatthias Ringwald } 2105aa4dd815SMatthias Ringwald 2106*5e87d1ceSMatthias Ringwald if (hfp_connection->send_error > 0){ 2107*5e87d1ceSMatthias Ringwald hfp_connection->send_error--; 2108a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2109485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 2110febc14f5SMatthias Ringwald return 1; 2111aa4dd815SMatthias Ringwald } 2112aa4dd815SMatthias Ringwald 2113ce263fc8SMatthias Ringwald // note: before update AG indicators and ok_pending 2114a0ffb263SMatthias Ringwald if (hfp_connection->send_response_and_hold_status){ 2115a0ffb263SMatthias Ringwald int status = hfp_connection->send_response_and_hold_status - 1; 2116a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = 0; 2117485ac19eSMilanka Ringwald hfp_ag_send_set_response_and_hold(hfp_connection->rfcomm_cid, status); 2118febc14f5SMatthias Ringwald return 1; 2119ce263fc8SMatthias Ringwald } 2120ce263fc8SMatthias Ringwald 21215be6b47aSMatthias Ringwald // note: before ok_pending and send_error to allow for unsolicited result on custom command 21225be6b47aSMatthias Ringwald if (hfp_connection->send_custom_message != NULL){ 21235be6b47aSMatthias Ringwald const char * message = hfp_connection->send_custom_message; 21245be6b47aSMatthias Ringwald hfp_connection->send_custom_message = NULL; 21255be6b47aSMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, message); 21267f8f1191SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_CUSTOM_AT_MESSAGE_SENT, ERROR_CODE_SUCCESS); 21275be6b47aSMatthias Ringwald return 1; 21285be6b47aSMatthias Ringwald } 21295be6b47aSMatthias Ringwald 2130*5e87d1ceSMatthias Ringwald if (hfp_connection->ok_pending > 0){ 2131*5e87d1ceSMatthias Ringwald hfp_connection->ok_pending--; 2132a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2133485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 2134febc14f5SMatthias Ringwald return 1; 2135ce263fc8SMatthias Ringwald } 2136ce263fc8SMatthias Ringwald 213769640b04SMatthias Ringwald // update AG indicators only if enabled by AT+CMER=3,0,0,1 213869640b04SMatthias Ringwald if ((hfp_connection->enable_status_update_for_ag_indicators == 1) && (hfp_connection->ag_indicators_status_update_bitmap != 0)){ 213969640b04SMatthias Ringwald uint16_t i; 2140a0ffb263SMatthias Ringwald for (i=0;i<hfp_connection->ag_indicators_nr;i++){ 2141a0ffb263SMatthias Ringwald if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){ 2142a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0); 2143485ac19eSMilanka Ringwald hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]); 2144febc14f5SMatthias Ringwald return 1; 2145aa4dd815SMatthias Ringwald } 2146aa4dd815SMatthias Ringwald } 2147aa4dd815SMatthias Ringwald } 2148aa4dd815SMatthias Ringwald 2149245852b7SMilanka Ringwald if (hfp_connection->ag_send_no_carrier){ 2150245852b7SMilanka Ringwald hfp_connection->ag_send_no_carrier = false; 2151245852b7SMilanka Ringwald hfp_ag_send_no_carrier(hfp_connection->rfcomm_cid); 2152245852b7SMilanka Ringwald return 1; 2153245852b7SMilanka Ringwald } 2154245852b7SMilanka Ringwald 2155a0ffb263SMatthias Ringwald if (hfp_connection->send_phone_number_for_voice_tag){ 2156a0ffb263SMatthias Ringwald hfp_connection->send_phone_number_for_voice_tag = 0; 2157a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2158e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2159a0ffb263SMatthias Ringwald hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid); 2160febc14f5SMatthias Ringwald return 1; 2161aa4dd815SMatthias Ringwald } 2162aa4dd815SMatthias Ringwald 2163a0ffb263SMatthias Ringwald if (hfp_connection->send_subscriber_number){ 2164aeb0f0feSMatthias Ringwald if (hfp_connection->next_subscriber_number_to_send < hfp_ag_subscriber_numbers_count){ 2165aeb0f0feSMatthias Ringwald hfp_phone_number_t phone = hfp_ag_subscriber_numbers[hfp_connection->next_subscriber_number_to_send++]; 2166a0ffb263SMatthias Ringwald hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number); 2167ce263fc8SMatthias Ringwald } else { 2168a0ffb263SMatthias Ringwald hfp_connection->send_subscriber_number = 0; 2169a0ffb263SMatthias Ringwald hfp_connection->next_subscriber_number_to_send = 0; 2170485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 2171ce263fc8SMatthias Ringwald } 2172a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2173febc14f5SMatthias Ringwald return 1; 2174ce263fc8SMatthias Ringwald } 2175ce263fc8SMatthias Ringwald 2176a0ffb263SMatthias Ringwald if (hfp_connection->send_microphone_gain){ 2177a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 0; 2178a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2179485ac19eSMilanka Ringwald hfp_ag_send_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 2180febc14f5SMatthias Ringwald return 1; 2181aa4dd815SMatthias Ringwald } 2182aa4dd815SMatthias Ringwald 2183a0ffb263SMatthias Ringwald if (hfp_connection->send_speaker_gain){ 2184a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 0; 2185a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2186485ac19eSMilanka Ringwald hfp_ag_send_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 2187febc14f5SMatthias Ringwald return 1; 21883deb3ec6SMatthias Ringwald } 21893deb3ec6SMatthias Ringwald 2190a0ffb263SMatthias Ringwald if (hfp_connection->send_ag_status_indicators){ 2191a0ffb263SMatthias Ringwald hfp_connection->send_ag_status_indicators = 0; 2192485ac19eSMilanka Ringwald hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid); 2193febc14f5SMatthias Ringwald return 1; 2194febc14f5SMatthias Ringwald } 2195febc14f5SMatthias Ringwald 2196febc14f5SMatthias Ringwald return 0; 2197febc14f5SMatthias Ringwald } 2198febc14f5SMatthias Ringwald 2199c1eef992SMatthias Ringwald // sends pending command, returns if command was sent 2200c1eef992SMatthias Ringwald static int hfp_ag_run_ring_and_clip(hfp_connection_t *hfp_connection){ 2201c1eef992SMatthias Ringwald // delay RING/CLIP until audio connection has been established for incoming calls 2202c1eef992SMatthias Ringwald // hfp_ag_hf_trigger_ring_and_clip is called in call_setup_state_machine 2203c1eef992SMatthias Ringwald if (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING){ 2204c1eef992SMatthias Ringwald if (hfp_connection->ag_ring){ 2205c1eef992SMatthias Ringwald hfp_connection->ag_ring = 0; 2206c1eef992SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2207c1eef992SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING); 2208c1eef992SMatthias Ringwald hfp_ag_send_ring(hfp_connection->rfcomm_cid); 2209c1eef992SMatthias Ringwald return 1; 2210c1eef992SMatthias Ringwald } 2211c1eef992SMatthias Ringwald 2212c1eef992SMatthias Ringwald if (hfp_connection->ag_send_clip){ 2213c1eef992SMatthias Ringwald hfp_connection->ag_send_clip = 0; 2214c1eef992SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2215c1eef992SMatthias Ringwald hfp_ag_send_clip(hfp_connection->rfcomm_cid); 2216c1eef992SMatthias Ringwald return 1; 2217c1eef992SMatthias Ringwald } 2218c1eef992SMatthias Ringwald } 2219c1eef992SMatthias Ringwald return 0; 2220c1eef992SMatthias Ringwald } 2221c1eef992SMatthias Ringwald 2222febc14f5SMatthias Ringwald static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection){ 2223febc14f5SMatthias Ringwald 222476cc1527SMatthias Ringwald btstack_assert(hfp_connection != NULL); 222576cc1527SMatthias Ringwald btstack_assert(hfp_connection->local_role == HFP_ROLE_AG); 2226febc14f5SMatthias Ringwald 222776cc1527SMatthias Ringwald // during SDP query, RFCOMM CID is not set 222876cc1527SMatthias Ringwald if (hfp_connection->rfcomm_cid == 0) return; 2229ce263fc8SMatthias Ringwald 223084fb9ac1SMilanka Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->emit_vra_enabled_after_audio_established){ 223184fb9ac1SMilanka Ringwald hfp_connection->emit_vra_enabled_after_audio_established = false; 223284fb9ac1SMilanka Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS); 223384fb9ac1SMilanka Ringwald } 223484fb9ac1SMilanka Ringwald 2235f9f3ba28SMilanka Ringwald if ((hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) && hfp_connection->release_audio_connection){ 2236f9f3ba28SMilanka Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 2237f9f3ba28SMilanka Ringwald hfp_connection->release_audio_connection = 0; 2238f9f3ba28SMilanka Ringwald gap_disconnect(hfp_connection->sco_handle); 2239f9f3ba28SMilanka Ringwald return; 2240f9f3ba28SMilanka Ringwald } 2241f9f3ba28SMilanka Ringwald 2242b7802cc8SMatthias Ringwald // configure NBS/WBS if needed using vendor-specific HCI commands 2243b7802cc8SMatthias Ringwald if (hci_can_send_command_packet_now()) { 22443721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP 22453721a235SMatthias Ringwald // WBS Disassociate 22463721a235SMatthias Ringwald if (hfp_connection->cc256x_send_wbs_disassociate){ 22473721a235SMatthias Ringwald hfp_connection->cc256x_send_wbs_disassociate = false; 22483721a235SMatthias Ringwald hci_send_cmd(&hci_ti_wbs_disassociate); 22493721a235SMatthias Ringwald return; 22503721a235SMatthias Ringwald } 22513721a235SMatthias Ringwald // Write Codec Config 22523721a235SMatthias Ringwald if (hfp_connection->cc256x_send_write_codec_config){ 22533721a235SMatthias Ringwald hfp_connection->cc256x_send_write_codec_config = false; 22543721a235SMatthias Ringwald hfp_cc256x_write_codec_config(hfp_connection); 22553721a235SMatthias Ringwald return; 22563721a235SMatthias Ringwald } 22573721a235SMatthias Ringwald // WBS Associate 22583721a235SMatthias Ringwald if (hfp_connection->cc256x_send_wbs_associate){ 22593721a235SMatthias Ringwald hfp_connection->cc256x_send_wbs_associate = false; 22603721a235SMatthias Ringwald hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle); 22613721a235SMatthias Ringwald return; 22623721a235SMatthias Ringwald } 22633721a235SMatthias Ringwald #endif 2264689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 2265689d4323SMatthias Ringwald // Enable WBS 2266689d4323SMatthias Ringwald if (hfp_connection->bcm_send_enable_wbs){ 2267689d4323SMatthias Ringwald hfp_connection->bcm_send_enable_wbs = false; 2268689d4323SMatthias Ringwald hci_send_cmd(&hci_bcm_enable_wbs, 1, 2); 2269689d4323SMatthias Ringwald return; 2270689d4323SMatthias Ringwald } 2271689d4323SMatthias Ringwald // Write I2S/PCM params 2272689d4323SMatthias Ringwald if (hfp_connection->bcm_send_write_i2spcm_interface_param){ 2273689d4323SMatthias Ringwald hfp_connection->bcm_send_write_i2spcm_interface_param = false; 2274689d4323SMatthias Ringwald hfp_bcm_write_i2spcm_interface_param(hfp_connection); 2275689d4323SMatthias Ringwald return; 2276689d4323SMatthias Ringwald } 2277689d4323SMatthias Ringwald // Disable WBS 2278689d4323SMatthias Ringwald if (hfp_connection->bcm_send_disable_wbs){ 2279689d4323SMatthias Ringwald hfp_connection->bcm_send_disable_wbs = false; 2280689d4323SMatthias Ringwald hci_send_cmd(&hci_bcm_enable_wbs, 0, 2); 2281689d4323SMatthias Ringwald return; 2282689d4323SMatthias Ringwald } 2283689d4323SMatthias Ringwald #endif 22842b5f92fdSMatthias Ringwald #ifdef ENABLE_RTK_PCM_WBS 22852b5f92fdSMatthias Ringwald // Configure CVSD vs. mSBC 22862b5f92fdSMatthias Ringwald if (hfp_connection->rtk_send_sco_config){ 22872b5f92fdSMatthias Ringwald hfp_connection->rtk_send_sco_config = false; 22882b5f92fdSMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 22892b5f92fdSMatthias Ringwald log_info("RTK SCO: 16k + mSBC"); 22902b5f92fdSMatthias Ringwald hci_send_cmd(&hci_rtk_configure_sco_routing, 0x81, 0x90, 0x00, 0x00, 0x1a, 0x0c, 0x00, 0x00, 0x41); 22912b5f92fdSMatthias Ringwald } else { 22922b5f92fdSMatthias Ringwald log_info("RTK SCO: 16k + CVSD"); 22932b5f92fdSMatthias Ringwald hci_send_cmd(&hci_rtk_configure_sco_routing, 0x81, 0x90, 0x00, 0x00, 0x1a, 0x0c, 0x0c, 0x00, 0x01); 22942b5f92fdSMatthias Ringwald } 22952b5f92fdSMatthias Ringwald return; 22962b5f92fdSMatthias Ringwald } 22972b5f92fdSMatthias Ringwald #endif 22985fd6f360SMatthias Ringwald #ifdef ENABLE_NXP_PCM_WBS 22995fd6f360SMatthias Ringwald if (hfp_connection->nxp_start_audio_handle != HCI_CON_HANDLE_INVALID){ 23005fd6f360SMatthias Ringwald hci_con_handle_t sco_handle = hfp_connection->nxp_start_audio_handle; 23015fd6f360SMatthias Ringwald hfp_connection->nxp_start_audio_handle = HCI_CON_HANDLE_INVALID; 23025fd6f360SMatthias Ringwald hci_send_cmd(&hci_nxp_host_pcm_i2s_audio_config, 0, 0, sco_handle, 0); 23035fd6f360SMatthias Ringwald return; 23045fd6f360SMatthias Ringwald } 23055fd6f360SMatthias Ringwald if (hfp_connection->nxp_stop_audio_handle != HCI_CON_HANDLE_INVALID){ 23065fd6f360SMatthias Ringwald hci_con_handle_t sco_handle = hfp_connection->nxp_stop_audio_handle; 23075fd6f360SMatthias Ringwald hfp_connection->nxp_stop_audio_handle = HCI_CON_HANDLE_INVALID; 23085fd6f360SMatthias Ringwald hci_send_cmd(&hci_nxp_host_pcm_i2s_audio_config, 1, 0, sco_handle, 0); 23095fd6f360SMatthias Ringwald return; 23105fd6f360SMatthias Ringwald } 23115fd6f360SMatthias Ringwald #endif 2312b7802cc8SMatthias Ringwald } 2313b7802cc8SMatthias Ringwald 231448e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS) 231548e6eeeeSMatthias Ringwald if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){ 231648e6eeeeSMatthias Ringwald hfp_finalize_connection_context(hfp_connection); 231748e6eeeeSMatthias Ringwald return; 231848e6eeeeSMatthias Ringwald } 231948e6eeeeSMatthias Ringwald #endif 23203721a235SMatthias Ringwald 2321febc14f5SMatthias Ringwald if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) { 2322febc14f5SMatthias Ringwald log_info("hfp_ag_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid); 2323febc14f5SMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 2324febc14f5SMatthias Ringwald return; 2325febc14f5SMatthias Ringwald } 2326febc14f5SMatthias Ringwald 2327febc14f5SMatthias Ringwald int cmd_sent = hfp_ag_send_commands(hfp_connection); 2328febc14f5SMatthias Ringwald 2329febc14f5SMatthias Ringwald if (!cmd_sent){ 2330febc14f5SMatthias Ringwald cmd_sent = hfp_ag_run_for_context_service_level_connection(hfp_connection); 2331febc14f5SMatthias Ringwald } 2332febc14f5SMatthias Ringwald 2333c04cf7ffSMilanka Ringwald if (!cmd_sent){ 2334c04cf7ffSMilanka Ringwald cmd_sent = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection); 23353deb3ec6SMatthias Ringwald } 23363deb3ec6SMatthias Ringwald 2337c04cf7ffSMilanka Ringwald if (!cmd_sent){ 2338c04cf7ffSMilanka Ringwald cmd_sent = call_setup_state_machine(hfp_connection); 2339aa4dd815SMatthias Ringwald } 2340aa4dd815SMatthias Ringwald 2341b956fff3SMatthias Ringwald if (!cmd_sent){ 2342b956fff3SMatthias Ringwald cmd_sent = hfp_ag_run_for_audio_connection(hfp_connection); 2343b956fff3SMatthias Ringwald } 2344b956fff3SMatthias Ringwald 2345c95b5b3cSMilanka Ringwald if (!cmd_sent){ 2346c1eef992SMatthias Ringwald cmd_sent = hfp_ag_run_ring_and_clip(hfp_connection); 2347c1eef992SMatthias Ringwald } 2348c1eef992SMatthias Ringwald 2349c1eef992SMatthias Ringwald if (!cmd_sent){ 2350c95b5b3cSMilanka Ringwald cmd_sent = hfp_ag_voice_recognition_state_machine(hfp_connection); 2351c95b5b3cSMilanka Ringwald } 2352b956fff3SMatthias Ringwald 2353d0a0eceeSMatthias Ringwald // disconnect 2354d0a0eceeSMatthias Ringwald if (!cmd_sent && (hfp_connection->command == HFP_CMD_NONE) && (hfp_connection->state == HFP_W2_DISCONNECT_RFCOMM)){ 2355d0a0eceeSMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 2356d0a0eceeSMatthias Ringwald rfcomm_disconnect(hfp_connection->rfcomm_cid); 2357d0a0eceeSMatthias Ringwald } 2358d0a0eceeSMatthias Ringwald 2359c04cf7ffSMilanka Ringwald if (cmd_sent){ 2360a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2361473ac565SMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 2362473ac565SMatthias Ringwald } 23633deb3ec6SMatthias Ringwald } 2364d68dcce1SMatthias Ringwald 2365e0d09929SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){ 2366c1ab6cc1SMatthias Ringwald return (byte == '\n') || (byte == '\r'); 2367e0d09929SMatthias Ringwald } 2368e0d09929SMatthias Ringwald 2369fdf18f86SMilanka Ringwald static void hfp_ag_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){ 23708a46ec40SMatthias Ringwald // assertion: size >= 1 as rfcomm.c does not deliver empty packets 2371fdf18f86SMilanka Ringwald if (size < 1) return; 2372347d46c8SMilanka Ringwald uint8_t status = ERROR_CODE_SUCCESS; 23731e35c04dSMatthias Ringwald 2374186dd3d2SMatthias Ringwald hfp_log_rfcomm_message("HFP_AG_RX", packet, size); 2375e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES 2376e43d1938SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet); 2377e43d1938SMatthias Ringwald #endif 23781e35c04dSMatthias Ringwald 23798a46ec40SMatthias Ringwald // process messages byte-wise 23803deb3ec6SMatthias Ringwald int pos; 23813deb3ec6SMatthias Ringwald for (pos = 0; pos < size ; pos++){ 2382a0ffb263SMatthias Ringwald hfp_parse(hfp_connection, packet[pos], 0); 2383186dd3d2SMatthias Ringwald 2384e0d09929SMatthias Ringwald // parse until end of line 2385e0d09929SMatthias Ringwald if (!hfp_parser_is_end_of_line(packet[pos])) continue; 2386e0d09929SMatthias Ringwald 2387186dd3d2SMatthias Ringwald hfp_generic_status_indicator_t * indicator; 2388a0ffb263SMatthias Ringwald switch(hfp_connection->command){ 238945796ff1SMilanka Ringwald case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION: 2390e7c46708SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2391e7c46708SMatthias Ringwald hfp_connection->ag_vra_requested_by_hf = true; 239245796ff1SMilanka Ringwald break; 2393ce263fc8SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_QUERY: 2394fd66594dSMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2395ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_active){ 2396a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1; 2397ce263fc8SMatthias Ringwald } 2398e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2399ce263fc8SMatthias Ringwald break; 2400ce263fc8SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_COMMAND: 2401fd66594dSMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 24020222a807SMatthias Ringwald switch(hfp_connection->ag_response_and_hold_action){ 2403ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD: 2404a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection); 2405ce263fc8SMatthias Ringwald break; 2406ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED: 2407a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection); 2408ce263fc8SMatthias Ringwald break; 2409ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED: 2410a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection); 2411ce263fc8SMatthias Ringwald break; 2412ce263fc8SMatthias Ringwald default: 2413ce263fc8SMatthias Ringwald break; 2414ce263fc8SMatthias Ringwald } 2415e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2416ce263fc8SMatthias Ringwald break; 2417ce263fc8SMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 2418a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2419347d46c8SMilanka Ringwald 2420aeb0f0feSMatthias Ringwald if (hfp_connection->parser_indicator_index < hfp_ag_generic_status_indicators_nr){ 2421aeb0f0feSMatthias Ringwald indicator = &hfp_ag_generic_status_indicators[hfp_connection->parser_indicator_index]; 2422ce263fc8SMatthias Ringwald switch (indicator->uuid){ 2423347d46c8SMilanka Ringwald case HFP_HF_INDICATOR_UUID_ENHANCED_SAFETY: 24240222a807SMatthias Ringwald if (hfp_connection->parser_indicator_value > 1) { 2425e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 2426ce263fc8SMatthias Ringwald break; 2427ce263fc8SMatthias Ringwald } 2428e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2429585db78dSMilanka Ringwald hfp_ag_emit_hf_indicator_value(hfp_connection, indicator->uuid, hfp_connection->parser_indicator_value); 2430347d46c8SMilanka Ringwald break; 2431347d46c8SMilanka Ringwald case HFP_HF_INDICATOR_UUID_BATTERY_LEVEL: 2432347d46c8SMilanka Ringwald if (hfp_connection->parser_indicator_value > 100){ 2433e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 2434347d46c8SMilanka Ringwald break; 2435347d46c8SMilanka Ringwald } 2436e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2437585db78dSMilanka Ringwald hfp_ag_emit_hf_indicator_value(hfp_connection, indicator->uuid, hfp_connection->parser_indicator_value); 2438347d46c8SMilanka Ringwald break; 2439347d46c8SMilanka Ringwald default: 2440e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 2441347d46c8SMilanka Ringwald break; 2442347d46c8SMilanka Ringwald } 2443347d46c8SMilanka Ringwald } else { 2444e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 2445347d46c8SMilanka Ringwald } 2446ce263fc8SMatthias Ringwald break; 2447ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 2448ce263fc8SMatthias Ringwald // expected by SLC state machine 2449a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break; 2450a0ffb263SMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0; 2451a0ffb263SMatthias Ringwald hfp_connection->send_ag_status_indicators = 1; 2452ce263fc8SMatthias Ringwald break; 2453ce263fc8SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 2454a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2455a0ffb263SMatthias Ringwald hfp_connection->next_call_index = 0; 2456a0ffb263SMatthias Ringwald hfp_connection->send_status_of_current_calls = 1; 2457ce263fc8SMatthias Ringwald break; 2458ce263fc8SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 2459fd66594dSMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2460aeb0f0feSMatthias Ringwald if (hfp_ag_subscriber_numbers_count == 0){ 2461485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 2462ce263fc8SMatthias Ringwald break; 2463ce263fc8SMatthias Ringwald } 2464a0ffb263SMatthias Ringwald hfp_connection->next_subscriber_number_to_send = 0; 2465a0ffb263SMatthias Ringwald hfp_connection->send_subscriber_number = 1; 2466ce263fc8SMatthias Ringwald break; 2467c1797c7dSMatthias Ringwald case HFP_CMD_TRANSMIT_DTMF_CODES: 24680222a807SMatthias Ringwald { 2469a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 24700222a807SMatthias Ringwald char buffer[2]; 24710222a807SMatthias Ringwald buffer[0] = (char) hfp_connection->ag_dtmf_code; 24720222a807SMatthias Ringwald buffer[1] = 0; 24730222a807SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, buffer); 2474c1797c7dSMatthias Ringwald break; 24750222a807SMatthias Ringwald } 2476aa4dd815SMatthias Ringwald case HFP_CMD_HF_REQUEST_PHONE_NUMBER: 2477a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2478ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG); 2479aa4dd815SMatthias Ringwald break; 2480aa4dd815SMatthias Ringwald case HFP_CMD_TURN_OFF_EC_AND_NR: 2481a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2482347d46c8SMilanka Ringwald 2483aeb0f0feSMatthias Ringwald if (get_bit(hfp_ag_supported_features, HFP_AGSF_EC_NR_FUNCTION)){ 2484e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2485347d46c8SMilanka Ringwald status = ERROR_CODE_SUCCESS; 2486aa4dd815SMatthias Ringwald } else { 2487e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 2488347d46c8SMilanka Ringwald status = ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 2489aa4dd815SMatthias Ringwald } 2490347d46c8SMilanka Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE, status); 2491aa4dd815SMatthias Ringwald break; 2492aa4dd815SMatthias Ringwald case HFP_CMD_CALL_ANSWERED: 2493a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 249460ebb071SMilanka Ringwald log_info("HFP: ATA"); 2495a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection); 2496aa4dd815SMatthias Ringwald break; 2497aa4dd815SMatthias Ringwald case HFP_CMD_HANG_UP_CALL: 2498a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2499e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2500a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection); 2501aa4dd815SMatthias Ringwald break; 2502aa4dd815SMatthias Ringwald case HFP_CMD_CALL_HOLD: { 2503a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2504e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2505d0c20769SMatthias Ringwald 25060222a807SMatthias Ringwald switch (hfp_connection->ag_call_hold_action){ 25070222a807SMatthias Ringwald case 0: 2508d0c20769SMatthias Ringwald // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call. 2509a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection); 2510aa4dd815SMatthias Ringwald break; 25110222a807SMatthias Ringwald case 1: 2512d0c20769SMatthias Ringwald // Releases all active calls (if any exist) and accepts the other (held or waiting) call. 2513d0c20769SMatthias Ringwald // Where both a held and a waiting call exist, the above procedures shall apply to the 2514d0c20769SMatthias Ringwald // waiting call (i.e., not to the held call) in conflicting situation. 2515a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection); 2516aa4dd815SMatthias Ringwald break; 25170222a807SMatthias Ringwald case 2: 2518d0c20769SMatthias Ringwald // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call. 2519d0c20769SMatthias Ringwald // Where both a held and a waiting call exist, the above procedures shall apply to the 2520d0c20769SMatthias Ringwald // waiting call (i.e., not to the held call) in conflicting situation. 2521a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection); 2522aa4dd815SMatthias Ringwald break; 25230222a807SMatthias Ringwald case 3: 2524d0c20769SMatthias Ringwald // Adds a held call to the conversation. 2525a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection); 2526aa4dd815SMatthias Ringwald break; 25270222a807SMatthias Ringwald case 4: 2528d0c20769SMatthias Ringwald // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer). 2529a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection); 2530aa4dd815SMatthias Ringwald break; 2531aa4dd815SMatthias Ringwald default: 2532aa4dd815SMatthias Ringwald break; 2533aa4dd815SMatthias Ringwald } 25340222a807SMatthias Ringwald hfp_connection->call_index = 0; 253535e92150SMatthias Ringwald break; 2536aa4dd815SMatthias Ringwald } 2537aa4dd815SMatthias Ringwald case HFP_CMD_CALL_PHONE_NUMBER: 2538a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 25399ff73f41SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED_BY_HF, hfp_connection); 2540aa4dd815SMatthias Ringwald break; 2541aa4dd815SMatthias Ringwald case HFP_CMD_REDIAL_LAST_NUMBER: 2542a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2543a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection); 2544aa4dd815SMatthias Ringwald break; 2545aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_CLIP: 2546a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2547a0ffb263SMatthias Ringwald log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled); 2548e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2549aa4dd815SMatthias Ringwald break; 2550aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION: 2551a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2552a0ffb263SMatthias Ringwald log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled); 2553e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2554aa4dd815SMatthias Ringwald break; 2555aa4dd815SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 2556a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2557e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 255860ebb071SMilanka Ringwald log_info("HF speaker gain = %u", hfp_connection->speaker_gain); 25593db60f78SBjoern Hartmann hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_connection->speaker_gain); 2560aa4dd815SMatthias Ringwald break; 2561aa4dd815SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 2562a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2563e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 256460ebb071SMilanka Ringwald log_info("HF microphone gain = %u", hfp_connection->microphone_gain); 25652abbd98dSMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_connection->microphone_gain); 2566aa4dd815SMatthias Ringwald break; 2567471dea41SMatthias Ringwald case HFP_CMD_CUSTOM_MESSAGE: 2568471dea41SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2569471dea41SMatthias Ringwald hfp_parser_reset_line_buffer(hfp_connection); 25706d9a41f9SMatthias Ringwald log_info("Custom AT Command ID 0x%04x", hfp_connection->custom_at_command_id); 2571471dea41SMatthias Ringwald hfp_ag_emit_custom_command_event(hfp_connection); 2572471dea41SMatthias Ringwald break; 25733f38f554SMatthias Ringwald case HFP_CMD_UNKNOWN: 25743f38f554SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2575e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 25763f38f554SMatthias Ringwald break; 2577aa4dd815SMatthias Ringwald default: 2578aa4dd815SMatthias Ringwald break; 25793deb3ec6SMatthias Ringwald } 25803deb3ec6SMatthias Ringwald } 25810cef86faSMatthias Ringwald } 25823deb3ec6SMatthias Ringwald 25831c6a0fc0SMatthias Ringwald static void hfp_ag_run(void){ 2584d63c37a1SMatthias Ringwald btstack_linked_list_iterator_t it; 2585d63c37a1SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2586d63c37a1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2587d63c37a1SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 258822387625SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2589f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 25903deb3ec6SMatthias Ringwald } 25913deb3ec6SMatthias Ringwald } 25923deb3ec6SMatthias Ringwald 25931c6a0fc0SMatthias Ringwald static void hfp_ag_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2594fdf18f86SMilanka Ringwald hfp_connection_t * hfp_connection = NULL; 25953deb3ec6SMatthias Ringwald switch (packet_type){ 25963deb3ec6SMatthias Ringwald case RFCOMM_DATA_PACKET: 2597fdf18f86SMilanka Ringwald hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 2598fd075f20SMilanka Ringwald btstack_assert(hfp_connection != NULL); 2599fdf18f86SMilanka Ringwald 2600fdf18f86SMilanka Ringwald hfp_ag_handle_rfcomm_data(hfp_connection, packet, size); 2601fdf18f86SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 2602fdf18f86SMilanka Ringwald return; 26033deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 2604e30a6a47SMatthias Ringwald if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 2605e30a6a47SMatthias Ringwald uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 2606fd075f20SMilanka Ringwald hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid); 2607fd075f20SMilanka Ringwald btstack_assert(hfp_connection != NULL); 2608fd075f20SMilanka Ringwald 2609fd075f20SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 2610e30a6a47SMatthias Ringwald return; 2611e30a6a47SMatthias Ringwald } 261227950165SMatthias Ringwald hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_AG); 2613aa4dd815SMatthias Ringwald break; 26143deb3ec6SMatthias Ringwald default: 26153deb3ec6SMatthias Ringwald break; 26163deb3ec6SMatthias Ringwald } 26173deb3ec6SMatthias Ringwald 26181c6a0fc0SMatthias Ringwald hfp_ag_run(); 26193deb3ec6SMatthias Ringwald } 26203deb3ec6SMatthias Ringwald 26211c6a0fc0SMatthias Ringwald static void hfp_ag_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2622405014fbSMatthias Ringwald hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_AG); 26231c6a0fc0SMatthias Ringwald hfp_ag_run(); 2624405014fbSMatthias Ringwald } 2625405014fbSMatthias Ringwald 2626aa10b9cbSMatthias Ringwald void hfp_ag_init_codecs(uint8_t codecs_nr, const uint8_t * codecs){ 2627aa10b9cbSMatthias Ringwald btstack_assert(codecs_nr <= HFP_MAX_NUM_CODECS); 2628aa10b9cbSMatthias Ringwald 2629aeb0f0feSMatthias Ringwald hfp_ag_codecs_nr = codecs_nr; 2630aa10b9cbSMatthias Ringwald uint8_t i; 26313deb3ec6SMatthias Ringwald for (i=0; i < codecs_nr; i++){ 2632aeb0f0feSMatthias Ringwald hfp_ag_codecs[i] = codecs[i]; 26333deb3ec6SMatthias Ringwald } 2634a0ffb263SMatthias Ringwald } 26353deb3ec6SMatthias Ringwald 2636a0ffb263SMatthias Ringwald void hfp_ag_init_supported_features(uint32_t supported_features){ 2637aeb0f0feSMatthias Ringwald hfp_ag_supported_features = supported_features; 263840a8ee13SMatthias Ringwald hfp_ag_in_band_ring_tone_active = has_in_band_ring_tone(); 2639a0ffb263SMatthias Ringwald } 26403deb3ec6SMatthias Ringwald 26417ca89cabSMatthias Ringwald void hfp_ag_init_ag_indicators(int ag_indicators_nr, const hfp_ag_indicator_t * ag_indicators){ 2642a0ffb263SMatthias Ringwald hfp_ag_indicators_nr = ag_indicators_nr; 26436535961aSMatthias Ringwald (void)memcpy(hfp_ag_indicators, ag_indicators, 26446535961aSMatthias Ringwald ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 2645a0ffb263SMatthias Ringwald } 26463deb3ec6SMatthias Ringwald 26477ca89cabSMatthias Ringwald void hfp_ag_init_hf_indicators(int hf_indicators_nr, const hfp_generic_status_indicator_t * hf_indicators){ 264825789943SMilanka Ringwald if (hf_indicators_nr > HFP_MAX_NUM_INDICATORS) return; 2649aeb0f0feSMatthias Ringwald hfp_ag_generic_status_indicators_nr = hf_indicators_nr; 2650aeb0f0feSMatthias Ringwald (void)memcpy(hfp_ag_generic_status_indicators, hf_indicators, 26516535961aSMatthias Ringwald hf_indicators_nr * sizeof(hfp_generic_status_indicator_t)); 2652a0ffb263SMatthias Ringwald } 2653a0ffb263SMatthias Ringwald 2654a0ffb263SMatthias Ringwald void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){ 26553deb3ec6SMatthias Ringwald hfp_ag_call_hold_services_nr = call_hold_services_nr; 26566535961aSMatthias Ringwald (void)memcpy(hfp_ag_call_hold_services, call_hold_services, 26576535961aSMatthias Ringwald call_hold_services_nr * sizeof(char *)); 2658a0ffb263SMatthias Ringwald } 2659a0ffb263SMatthias Ringwald 2660a0ffb263SMatthias Ringwald 2661ab2445a0SMatthias Ringwald void hfp_ag_init(uint8_t rfcomm_channel_nr){ 266227950165SMatthias Ringwald 2663520c92d5SMatthias Ringwald hfp_init(); 266420b2edb6SMatthias Ringwald hfp_ag_call_hold_services_nr = 0; 266527bb1817SMatthias Ringwald hfp_ag_response_and_hold_active = false; 266620b2edb6SMatthias Ringwald hfp_ag_indicators_nr = 0; 2667aeb0f0feSMatthias Ringwald hfp_ag_codecs_nr = 0; 2668aeb0f0feSMatthias Ringwald hfp_ag_supported_features = HFP_DEFAULT_AG_SUPPORTED_FEATURES; 266940a8ee13SMatthias Ringwald hfp_ag_in_band_ring_tone_active = has_in_band_ring_tone(); 2670aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers = NULL; 2671aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers_count = 0; 2672d63c37a1SMatthias Ringwald 26731c6a0fc0SMatthias Ringwald hfp_ag_hci_event_callback_registration.callback = &hfp_ag_hci_event_packet_handler; 26741c6a0fc0SMatthias Ringwald hci_add_event_handler(&hfp_ag_hci_event_callback_registration); 267527950165SMatthias Ringwald 26761c6a0fc0SMatthias Ringwald rfcomm_register_service(&hfp_ag_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 267727950165SMatthias Ringwald 267827950165SMatthias Ringwald // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us 26791c6a0fc0SMatthias Ringwald hfp_set_ag_rfcomm_packet_handler(&hfp_ag_rfcomm_packet_handler); 2680aa4dd815SMatthias Ringwald 2681d210d9c4SMatthias Ringwald hfp_gsm_init(); 26823deb3ec6SMatthias Ringwald } 26833deb3ec6SMatthias Ringwald 268420b2edb6SMatthias Ringwald void hfp_ag_deinit(void){ 268520b2edb6SMatthias Ringwald hfp_deinit(); 268620b2edb6SMatthias Ringwald hfp_gsm_deinit(); 2687aeb0f0feSMatthias Ringwald 2688aeb0f0feSMatthias Ringwald hfp_ag_callback = NULL; 2689aeb0f0feSMatthias Ringwald hfp_ag_supported_features = 0; 2690aeb0f0feSMatthias Ringwald hfp_ag_codecs_nr = 0; 2691aeb0f0feSMatthias Ringwald hfp_ag_indicators_nr = 0; 2692aeb0f0feSMatthias Ringwald hfp_ag_call_hold_services_nr = 0; 269320b2edb6SMatthias Ringwald (void) memset(&hfp_ag_call_hold_services, 0, sizeof(hfp_ag_call_hold_services)); 2694aeb0f0feSMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 269520b2edb6SMatthias Ringwald (void) memset(&hfp_ag_response_and_hold_state, 0, sizeof(hfp_response_and_hold_state_t)); 2696aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers = NULL; 2697aeb0f0feSMatthias Ringwald (void) memset(&hfp_ag_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t)); 269881e25d0aSMatthias Ringwald hfp_ag_custom_call_sm_handler = NULL; 269920b2edb6SMatthias Ringwald } 270020b2edb6SMatthias Ringwald 27014eb3f1d8SMilanka Ringwald uint8_t hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){ 27024eb3f1d8SMilanka Ringwald return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE, HFP_ROLE_AG); 27033deb3ec6SMatthias Ringwald } 27043deb3ec6SMatthias Ringwald 2705657bc59fSMilanka Ringwald uint8_t hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){ 27069c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2707a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2708657bc59fSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2709a33eb0c4SMilanka Ringwald } 2710c5fa3c94SMilanka Ringwald 27111ffa0dd9SMilanka Ringwald hfp_trigger_release_service_level_connection(hfp_connection); 2712f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2713657bc59fSMilanka Ringwald return ERROR_CODE_SUCCESS; 27143deb3ec6SMatthias Ringwald } 27153deb3ec6SMatthias Ringwald 2716c5fa3c94SMilanka Ringwald uint8_t hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, hfp_cme_error_t error){ 27179c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2718a0ffb263SMatthias Ringwald if (!hfp_connection){ 2719c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 27203deb3ec6SMatthias Ringwald } 27213deb3ec6SMatthias Ringwald 2722c5fa3c94SMilanka Ringwald hfp_connection->extended_audio_gateway_error = 0; 2723c5fa3c94SMilanka Ringwald if (!hfp_connection->enable_extended_audio_gateway_error_report){ 2724c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2725c5fa3c94SMilanka Ringwald } 2726c5fa3c94SMilanka Ringwald 2727c5fa3c94SMilanka Ringwald hfp_connection->extended_audio_gateway_error = error; 2728c5fa3c94SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 2729c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2730c5fa3c94SMilanka Ringwald } 2731c5fa3c94SMilanka Ringwald 2732c5fa3c94SMilanka Ringwald static uint8_t hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){ 2733c5fa3c94SMilanka Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 2734c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2735c5fa3c94SMilanka Ringwald } 2736c5fa3c94SMilanka Ringwald if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){ 2737c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2738c5fa3c94SMilanka Ringwald } 27393deb3ec6SMatthias Ringwald 2740a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 1; 2741a0ffb263SMatthias Ringwald if (!has_codec_negotiation_feature(hfp_connection)){ 2742d6ff09e1SMatthias Ringwald log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using CVSD"); 2743d6ff09e1SMatthias Ringwald hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 2744a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 27457522e673SMatthias Ringwald // now, pick link settings 2746afac2a04SMilanka Ringwald hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection)); 27473721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP 27483721a235SMatthias Ringwald hfp_cc256x_prepare_for_sco(hfp_connection); 27493721a235SMatthias Ringwald #endif 2750c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 27513deb3ec6SMatthias Ringwald } 2752aa4dd815SMatthias Ringwald 2753411a9776SMilanka Ringwald uint8_t i; 2754411a9776SMilanka Ringwald bool codec_was_in_use = false; 2755411a9776SMilanka Ringwald bool better_codec_can_be_used = false; 2756411a9776SMilanka Ringwald 2757411a9776SMilanka Ringwald for (i = 0; i<hfp_connection->remote_codecs_nr; i++){ 2758411a9776SMilanka Ringwald if (hfp_connection->negotiated_codec == hfp_connection->remote_codecs[i]){ 2759411a9776SMilanka Ringwald codec_was_in_use = true; 2760411a9776SMilanka Ringwald } else if (hfp_connection->negotiated_codec < hfp_connection->remote_codecs[i]){ 2761411a9776SMilanka Ringwald better_codec_can_be_used = true; 2762411a9776SMilanka Ringwald } 2763411a9776SMilanka Ringwald } 2764411a9776SMilanka Ringwald 2765411a9776SMilanka Ringwald if (!codec_was_in_use || better_codec_can_be_used){ 2766bc94e48cSMatthias Ringwald hfp_connection->ag_send_common_codec = true; 2767411a9776SMilanka Ringwald hfp_connection->codecs_state = HFP_CODECS_IDLE; 2768411a9776SMilanka Ringwald } 2769c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2770aa4dd815SMatthias Ringwald } 2771aa4dd815SMatthias Ringwald 2772c5fa3c94SMilanka Ringwald uint8_t hfp_ag_establish_audio_connection(hci_con_handle_t acl_handle){ 27739c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2774a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2775c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2776a33eb0c4SMilanka Ringwald } 2777c5fa3c94SMilanka Ringwald uint8_t status = hfp_ag_setup_audio_connection(hfp_connection); 2778c5fa3c94SMilanka Ringwald if (status != ERROR_CODE_SUCCESS){ 2779c5fa3c94SMilanka Ringwald return status; 2780c5fa3c94SMilanka Ringwald } 2781f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2782c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 27833deb3ec6SMatthias Ringwald } 27843deb3ec6SMatthias Ringwald 2785c5fa3c94SMilanka Ringwald uint8_t hfp_ag_release_audio_connection(hci_con_handle_t acl_handle){ 27869c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2787a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2788c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2789a33eb0c4SMilanka Ringwald } 2790c5fa3c94SMilanka Ringwald 27910b4debbfSMilanka Ringwald if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){ 27920b4debbfSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 27930b4debbfSMilanka Ringwald } 27941a26de69SMilanka Ringwald 27950b4debbfSMilanka Ringwald uint8_t status = hfp_trigger_release_audio_connection(hfp_connection); 27960b4debbfSMilanka Ringwald if (status == ERROR_CODE_SUCCESS){ 2797f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 27980b4debbfSMilanka Ringwald } 27990b4debbfSMilanka Ringwald return status; 28003deb3ec6SMatthias Ringwald } 2801aa4dd815SMatthias Ringwald 2802aa4dd815SMatthias Ringwald /** 2803aa4dd815SMatthias Ringwald * @brief Enable in-band ring tone 2804aa4dd815SMatthias Ringwald */ 2805aa4dd815SMatthias Ringwald void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){ 280640a8ee13SMatthias Ringwald if (has_in_band_ring_tone() == false){ 280740a8ee13SMatthias Ringwald return; 280840a8ee13SMatthias Ringwald } 280940a8ee13SMatthias Ringwald if (hfp_ag_in_band_ring_tone_active == use_in_band_ring_tone){ 2810aa4dd815SMatthias Ringwald return; 2811aa4dd815SMatthias Ringwald } 2812c5fa3c94SMilanka Ringwald 281340a8ee13SMatthias Ringwald hfp_ag_in_band_ring_tone_active = use_in_band_ring_tone; 2814aa4dd815SMatthias Ringwald 2815665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 2816665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2817665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2818a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 281966c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 28209430c71eSMatthias Ringwald hfp_connection->ag_send_in_band_ring_tone_setting = true; 2821f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2822aa4dd815SMatthias Ringwald } 2823aa4dd815SMatthias Ringwald } 2824aa4dd815SMatthias Ringwald 2825aa4dd815SMatthias Ringwald /** 2826aa4dd815SMatthias Ringwald * @brief Called from GSM 2827aa4dd815SMatthias Ringwald */ 2828aa4dd815SMatthias Ringwald void hfp_ag_incoming_call(void){ 2829aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL); 2830aa4dd815SMatthias Ringwald } 2831aa4dd815SMatthias Ringwald 28320f1da533SMatthias Ringwald void hfp_ag_outgoing_call_initiated(void) { 2833fe899794SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED_BY_AG, NULL); 2834fe899794SMatthias Ringwald } 2835fe899794SMatthias Ringwald 2836aa4dd815SMatthias Ringwald /** 2837aa4dd815SMatthias Ringwald * @brief number is stored. 2838aa4dd815SMatthias Ringwald */ 2839aa4dd815SMatthias Ringwald void hfp_ag_set_clip(uint8_t type, const char * number){ 2840f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_SET_CLIP, 0, type, number); 2841aa4dd815SMatthias Ringwald } 2842aa4dd815SMatthias Ringwald 2843aa4dd815SMatthias Ringwald void hfp_ag_call_dropped(void){ 2844aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL); 2845aa4dd815SMatthias Ringwald } 2846aa4dd815SMatthias Ringwald 2847aa4dd815SMatthias Ringwald // call from AG UI 2848aa4dd815SMatthias Ringwald void hfp_ag_answer_incoming_call(void){ 2849aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL); 2850aa4dd815SMatthias Ringwald } 2851aa4dd815SMatthias Ringwald 2852ce263fc8SMatthias Ringwald void hfp_ag_join_held_call(void){ 2853ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL); 2854ce263fc8SMatthias Ringwald } 2855ce263fc8SMatthias Ringwald 2856aa4dd815SMatthias Ringwald void hfp_ag_terminate_call(void){ 2857aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL); 2858aa4dd815SMatthias Ringwald } 2859aa4dd815SMatthias Ringwald 2860aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_ringing(void){ 2861aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL); 2862aa4dd815SMatthias Ringwald } 2863aa4dd815SMatthias Ringwald 2864aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_established(void){ 2865aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL); 2866aa4dd815SMatthias Ringwald } 2867aa4dd815SMatthias Ringwald 2868aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_rejected(void){ 2869aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL); 2870aa4dd815SMatthias Ringwald } 2871aa4dd815SMatthias Ringwald 2872aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_accepted(void){ 2873aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL); 2874aa4dd815SMatthias Ringwald } 2875ce263fc8SMatthias Ringwald 2876ce263fc8SMatthias Ringwald void hfp_ag_hold_incoming_call(void){ 2877ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL); 2878ce263fc8SMatthias Ringwald } 2879ce263fc8SMatthias Ringwald 2880ce263fc8SMatthias Ringwald void hfp_ag_accept_held_incoming_call(void) { 2881ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL); 2882ce263fc8SMatthias Ringwald } 2883ce263fc8SMatthias Ringwald 2884ce263fc8SMatthias Ringwald void hfp_ag_reject_held_incoming_call(void){ 2885ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL); 2886ce263fc8SMatthias Ringwald } 2887ce263fc8SMatthias Ringwald 2888aa4dd815SMatthias Ringwald static void hfp_ag_set_ag_indicator(const char * name, int value){ 2889aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name(name); 2890aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 2891aa4dd815SMatthias Ringwald hfp_ag_indicators[indicator_index].status = value; 2892aa4dd815SMatthias Ringwald 2893665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 2894665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2895665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2896a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 289766c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2898a0ffb263SMatthias Ringwald if (!hfp_connection->ag_indicators[indicator_index].enabled) { 2899245852b7SMilanka Ringwald log_info("Requested AG indicator '%s' update to %u, but it is not enabled", hfp_ag_indicators[indicator_index].name, value); 2900ce263fc8SMatthias Ringwald continue; 2901ce263fc8SMatthias Ringwald } 2902245852b7SMilanka Ringwald log_info("AG indicator '%s' changed to %u, request transfer status", hfp_ag_indicators[indicator_index].name, value); 2903a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 2904f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2905aa4dd815SMatthias Ringwald } 2906aa4dd815SMatthias Ringwald } 2907aa4dd815SMatthias Ringwald 2908c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_registration_status(int registration_status){ 2909c5fa3c94SMilanka Ringwald if ((registration_status < 0) || (registration_status > 1)){ 2910c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2911c5fa3c94SMilanka Ringwald } 2912c5fa3c94SMilanka Ringwald 2913c5fa3c94SMilanka Ringwald if ( (registration_status == 0) && (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 2914245852b7SMilanka Ringwald 2915245852b7SMilanka Ringwald // if network goes away wihle a call is active: 2916245852b7SMilanka Ringwald // - the call gets dropped 2917245852b7SMilanka Ringwald // - we send NO CARRIER 2918245852b7SMilanka Ringwald // NOTE: the CALL=0 has to be sent before NO CARRIER 2919245852b7SMilanka Ringwald 2920245852b7SMilanka Ringwald hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL); 2921245852b7SMilanka Ringwald 2922245852b7SMilanka Ringwald btstack_linked_list_iterator_t it; 2923245852b7SMilanka Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2924245852b7SMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2925245852b7SMilanka Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 2926245852b7SMilanka Ringwald hfp_connection->ag_send_no_carrier = true; 2927245852b7SMilanka Ringwald } 2928245852b7SMilanka Ringwald } 2929c5fa3c94SMilanka Ringwald hfp_ag_set_ag_indicator("service", registration_status); 2930c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2931aa4dd815SMatthias Ringwald } 2932aa4dd815SMatthias Ringwald 2933c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_signal_strength(int signal_strength){ 2934c5fa3c94SMilanka Ringwald if ((signal_strength < 0) || (signal_strength > 5)){ 2935c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2936aa4dd815SMatthias Ringwald } 2937aa4dd815SMatthias Ringwald 2938c5fa3c94SMilanka Ringwald hfp_ag_set_ag_indicator("signal", signal_strength); 2939c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2940aa4dd815SMatthias Ringwald } 2941aa4dd815SMatthias Ringwald 2942c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_roaming_status(int roaming_status){ 2943c5fa3c94SMilanka Ringwald if ((roaming_status < 0) || (roaming_status > 1)){ 2944c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2945aa4dd815SMatthias Ringwald } 2946aa4dd815SMatthias Ringwald 2947c5fa3c94SMilanka Ringwald hfp_ag_set_ag_indicator("roam", roaming_status); 2948c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2949c5fa3c94SMilanka Ringwald } 2950c5fa3c94SMilanka Ringwald 2951c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_battery_level(int battery_level){ 2952c5fa3c94SMilanka Ringwald if ((battery_level < 0) || (battery_level > 5)){ 2953c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2954c5fa3c94SMilanka Ringwald } 2955c5fa3c94SMilanka Ringwald hfp_ag_set_ag_indicator("battchg", battery_level); 2956c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2957c5fa3c94SMilanka Ringwald } 2958c5fa3c94SMilanka Ringwald 29596b8275b0SMilanka Ringwald uint8_t hfp_ag_activate_voice_recognition(hci_con_handle_t acl_handle){ 296014c148caSMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 296114c148caSMilanka Ringwald if (!hfp_connection){ 296214c148caSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 29636b8275b0SMilanka Ringwald } 2964f71d2aacSMilanka Ringwald 296584fb9ac1SMilanka Ringwald if (hfp_connection->emit_vra_enabled_after_audio_established){ 296684fb9ac1SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 296784fb9ac1SMilanka Ringwald } 296884fb9ac1SMilanka Ringwald 29699cd26862SMilanka Ringwald bool enhanced_vra_supported = hfp_ag_enhanced_vra_flag_supported(hfp_connection); 29709cd26862SMilanka Ringwald bool legacy_vra_supported = hfp_ag_vra_flag_supported(hfp_connection); 2971f71d2aacSMilanka Ringwald if (!enhanced_vra_supported && !legacy_vra_supported){ 297214c148caSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 29736b8275b0SMilanka Ringwald } 29746b8275b0SMilanka Ringwald 2975135d9718SMilanka Ringwald if (!hfp_ag_can_activate_voice_recognition(hfp_connection)){ 2976135d9718SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2977f71d2aacSMilanka Ringwald } 2978f71d2aacSMilanka Ringwald 2979f71d2aacSMilanka Ringwald hfp_connection->ag_activate_voice_recognition_value = 1; 2980f71d2aacSMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED; 2981fcf4ede6SMilanka Ringwald hfp_connection->enhanced_voice_recognition_enabled = enhanced_vra_supported; 2982f71d2aacSMilanka Ringwald hfp_connection->ag_audio_connection_opened_before_vra = hfp_ag_is_audio_connection_active(hfp_connection); 2983f71d2aacSMilanka Ringwald hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY; 29848f8818a4SMatthias Ringwald hfp_connection->ag_vra_send_command = true; 2985f71d2aacSMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 2986f71d2aacSMilanka Ringwald return ERROR_CODE_SUCCESS; 2987f71d2aacSMilanka Ringwald } 2988f71d2aacSMilanka Ringwald 2989d84719b1SMilanka Ringwald uint8_t hfp_ag_deactivate_voice_recognition(hci_con_handle_t acl_handle){ 299014c148caSMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 299114c148caSMilanka Ringwald if (!hfp_connection){ 299214c148caSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 299345796ff1SMilanka Ringwald } 2994f71d2aacSMilanka Ringwald 299584fb9ac1SMilanka Ringwald if (hfp_connection->emit_vra_enabled_after_audio_established){ 299684fb9ac1SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 299784fb9ac1SMilanka Ringwald } 299884fb9ac1SMilanka Ringwald 29999cd26862SMilanka Ringwald bool enhanced_vra_supported = hfp_ag_enhanced_vra_flag_supported(hfp_connection); 30009cd26862SMilanka Ringwald bool legacy_vra_supported = hfp_ag_vra_flag_supported(hfp_connection); 30019cd26862SMilanka Ringwald 3002f71d2aacSMilanka Ringwald if (!enhanced_vra_supported && !legacy_vra_supported){ 300314c148caSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 300445796ff1SMilanka Ringwald } 300545796ff1SMilanka Ringwald 3006135d9718SMilanka Ringwald if (!hfp_ag_voice_recognition_session_active(hfp_connection)){ 3007135d9718SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3008f71d2aacSMilanka Ringwald } 3009f71d2aacSMilanka Ringwald 3010f71d2aacSMilanka Ringwald hfp_connection->ag_activate_voice_recognition_value = 0; 3011f71d2aacSMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF; 3012f71d2aacSMilanka Ringwald hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY; 30138f8818a4SMatthias Ringwald hfp_connection->ag_vra_send_command = true; 3014f71d2aacSMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 3015f71d2aacSMilanka Ringwald return ERROR_CODE_SUCCESS; 3016f71d2aacSMilanka Ringwald } 3017f71d2aacSMilanka Ringwald 3018cf75be85SMilanka Ringwald static uint8_t hfp_ag_enhanced_voice_recognition_send_state(hci_con_handle_t acl_handle, hfp_voice_recognition_state_t state){ 301945796ff1SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 302045796ff1SMilanka Ringwald if (!hfp_connection){ 3021c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 302245796ff1SMilanka Ringwald } 3023c5fa3c94SMilanka Ringwald 302484fb9ac1SMilanka Ringwald if (hfp_connection->emit_vra_enabled_after_audio_established){ 302584fb9ac1SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3026f71d2aacSMilanka Ringwald } 3027f71d2aacSMilanka Ringwald 3028645bfa78SMilanka Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){ 3029645bfa78SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3030645bfa78SMilanka Ringwald } 3031645bfa78SMilanka Ringwald 3032645bfa78SMilanka Ringwald if (hfp_connection->vra_state != HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO){ 3033135d9718SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 30349cd26862SMilanka Ringwald } 30359cd26862SMilanka Ringwald 303684fb9ac1SMilanka Ringwald bool enhanced_vra_supported = hfp_ag_enhanced_vra_flag_supported(hfp_connection); 303784fb9ac1SMilanka Ringwald if (!enhanced_vra_supported ){ 303884fb9ac1SMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 303984fb9ac1SMilanka Ringwald } 304084fb9ac1SMilanka Ringwald 304184fb9ac1SMilanka Ringwald 304245796ff1SMilanka Ringwald hfp_connection->ag_vra_state = state; 3043cf75be85SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_STATUS; 30448f8818a4SMatthias Ringwald hfp_connection->ag_vra_send_command = true; 304545796ff1SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 30469cd26862SMilanka Ringwald return ERROR_CODE_SUCCESS; 304745796ff1SMilanka Ringwald } 304845796ff1SMilanka Ringwald 3049f0c27a95SMilanka Ringwald uint8_t hfp_ag_enhanced_voice_recognition_report_sending_audio(hci_con_handle_t acl_handle){ 3050cf75be85SMilanka Ringwald return hfp_ag_enhanced_voice_recognition_send_state(acl_handle, HFP_VOICE_RECOGNITION_STATE_AG_IS_STARTING_SOUND); 305145796ff1SMilanka Ringwald } 3052f0c27a95SMilanka Ringwald uint8_t hfp_ag_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle){ 3053cf75be85SMilanka Ringwald return hfp_ag_enhanced_voice_recognition_send_state(acl_handle, HFP_VOICE_RECOGNITION_STATE_AG_READY_TO_ACCEPT_AUDIO_INPUT); 305445796ff1SMilanka Ringwald } 3055cf75be85SMilanka Ringwald uint8_t hfp_ag_enhanced_voice_recognition_report_processing_input(hci_con_handle_t acl_handle){ 3056cf75be85SMilanka Ringwald return hfp_ag_enhanced_voice_recognition_send_state(acl_handle, HFP_VOICE_RECOGNITION_STATE_AG_IS_PROCESSING_AUDIO_INPUT); 305745796ff1SMilanka Ringwald } 305845796ff1SMilanka Ringwald 3059b95cac54SMilanka Ringwald 3060b95cac54SMilanka 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){ 3061b95cac54SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3062b95cac54SMilanka Ringwald if (!hfp_connection){ 3063b95cac54SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3064b95cac54SMilanka Ringwald } 3065b95cac54SMilanka Ringwald 306684fb9ac1SMilanka Ringwald if (hfp_connection->emit_vra_enabled_after_audio_established){ 306784fb9ac1SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3068f71d2aacSMilanka Ringwald } 3069f71d2aacSMilanka Ringwald 3070645bfa78SMilanka Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){ 3071645bfa78SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3072645bfa78SMilanka Ringwald } 3073645bfa78SMilanka Ringwald 3074645bfa78SMilanka Ringwald if (hfp_connection->vra_state != HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO){ 3075135d9718SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3076f71d2aacSMilanka Ringwald } 3077f71d2aacSMilanka Ringwald 307884fb9ac1SMilanka Ringwald bool enhanced_vra_supported = hfp_ag_enhanced_vra_flag_supported(hfp_connection); 307984fb9ac1SMilanka Ringwald if (!enhanced_vra_supported ){ 308084fb9ac1SMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 308184fb9ac1SMilanka Ringwald } 308284fb9ac1SMilanka Ringwald 308384fb9ac1SMilanka Ringwald bool enhanced_vra_msg_supported = hfp_ag_can_send_enhanced_vra_message_flag_supported(hfp_connection); 308484fb9ac1SMilanka Ringwald if (!enhanced_vra_msg_supported ){ 308584fb9ac1SMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 308684fb9ac1SMilanka Ringwald } 308784fb9ac1SMilanka Ringwald 3088ab2445a0SMatthias Ringwald uint16_t message_len = (uint16_t) strlen(msg.text); 308951aa5d5aSMilanka Ringwald 309089e7c136SMilanka Ringwald if (message_len > HFP_MAX_VR_TEXT_SIZE){ 30916504a03aSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 30926504a03aSMilanka Ringwald } 30936504a03aSMilanka Ringwald 30946504a03aSMilanka Ringwald if ((HFP_VR_TEXT_HEADER_SIZE + message_len) > hfp_connection->rfcomm_mtu){ 309551aa5d5aSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 309651aa5d5aSMilanka Ringwald } 309751aa5d5aSMilanka Ringwald 3098b95cac54SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_MSG; 3099b95cac54SMilanka Ringwald hfp_connection->ag_msg = msg; 3100b95cac54SMilanka Ringwald hfp_connection->ag_vra_state = state; 31018f8818a4SMatthias Ringwald hfp_connection->ag_vra_send_command = true; 3102b95cac54SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 3103f71d2aacSMilanka Ringwald 3104f71d2aacSMilanka Ringwald return ERROR_CODE_SUCCESS; 3105b95cac54SMilanka Ringwald } 3106b95cac54SMilanka Ringwald 3107b95cac54SMilanka Ringwald 3108c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 31099c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3110a33eb0c4SMilanka Ringwald if (!hfp_connection){ 3111c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3112a33eb0c4SMilanka Ringwald } 3113c5fa3c94SMilanka Ringwald 3114c5fa3c94SMilanka Ringwald if ((gain < 0) || (gain > 15)){ 3115c5fa3c94SMilanka Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 3116c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3117c5fa3c94SMilanka Ringwald } 3118c5fa3c94SMilanka Ringwald 3119a0ffb263SMatthias Ringwald if (hfp_connection->microphone_gain != gain){ 3120a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = gain; 3121a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 3122aa4dd815SMatthias Ringwald } 3123f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 3124c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 3125aa4dd815SMatthias Ringwald } 3126aa4dd815SMatthias Ringwald 3127c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 31289c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3129a33eb0c4SMilanka Ringwald if (!hfp_connection){ 3130c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3131a33eb0c4SMilanka Ringwald } 3132c5fa3c94SMilanka Ringwald 3133c5fa3c94SMilanka Ringwald if ((gain < 0) || (gain > 15)){ 3134c5fa3c94SMilanka Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 3135c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3136c5fa3c94SMilanka Ringwald } 3137c5fa3c94SMilanka Ringwald 3138a0ffb263SMatthias Ringwald if (hfp_connection->speaker_gain != gain){ 3139a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = gain; 3140a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 3141aa4dd815SMatthias Ringwald } 3142f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 3143c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 3144aa4dd815SMatthias Ringwald } 3145aa4dd815SMatthias Ringwald 3146c5fa3c94SMilanka Ringwald uint8_t hfp_ag_send_phone_number_for_voice_tag(hci_con_handle_t acl_handle, const char * number){ 31479c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3148a33eb0c4SMilanka Ringwald if (!hfp_connection){ 3149c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3150a33eb0c4SMilanka Ringwald } 3151c5fa3c94SMilanka Ringwald 3152aa4dd815SMatthias Ringwald hfp_ag_set_clip(0, number); 3153a0ffb263SMatthias Ringwald hfp_connection->send_phone_number_for_voice_tag = 1; 3154c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 3155aa4dd815SMatthias Ringwald } 3156aa4dd815SMatthias Ringwald 3157c5fa3c94SMilanka Ringwald uint8_t hfp_ag_reject_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 31589c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3159a33eb0c4SMilanka Ringwald if (!hfp_connection){ 3160c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3161a33eb0c4SMilanka Ringwald } 3162c5fa3c94SMilanka Ringwald 3163e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 3164c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 3165aa4dd815SMatthias Ringwald } 3166aa4dd815SMatthias Ringwald 3167c5fa3c94SMilanka Ringwald uint8_t hfp_ag_send_dtmf_code_done(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_ok(hfp_connection); 3174c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 3175c1797c7dSMatthias Ringwald } 3176aa4dd815SMatthias Ringwald 31775be6b47aSMatthias Ringwald uint8_t hfp_ag_send_unsolicited_result_code(hci_con_handle_t acl_handle, const char * unsolicited_result_code){ 31785be6b47aSMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 31795be6b47aSMatthias Ringwald if (!hfp_connection){ 31805be6b47aSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 31815be6b47aSMatthias Ringwald } 31825be6b47aSMatthias Ringwald if (hfp_connection->send_custom_message != NULL){ 31835be6b47aSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 31845be6b47aSMatthias Ringwald } 31855be6b47aSMatthias Ringwald hfp_connection->send_custom_message = unsolicited_result_code; 31865be6b47aSMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 31875be6b47aSMatthias Ringwald return ERROR_CODE_SUCCESS; 31885be6b47aSMatthias Ringwald } 31895be6b47aSMatthias Ringwald 3190471dea41SMatthias Ringwald uint8_t hfp_ag_send_command_result_code(hci_con_handle_t acl_handle, bool ok){ 3191471dea41SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3192471dea41SMatthias Ringwald if (!hfp_connection){ 3193471dea41SMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3194471dea41SMatthias Ringwald } 3195471dea41SMatthias Ringwald if (ok){ 3196e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 3197471dea41SMatthias Ringwald } else { 3198e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 3199471dea41SMatthias Ringwald } 3200471dea41SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 3201471dea41SMatthias Ringwald return ERROR_CODE_SUCCESS; 3202471dea41SMatthias Ringwald } 3203471dea41SMatthias Ringwald 3204ce263fc8SMatthias Ringwald void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){ 3205aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers = numbers; 3206aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers_count = numbers_count; 3207ce263fc8SMatthias Ringwald } 3208ce263fc8SMatthias Ringwald 32099cae807eSMatthias Ringwald void hfp_ag_clear_last_dialed_number(void){ 32109cae807eSMatthias Ringwald hfp_gsm_clear_last_dialed_number(); 3211ce263fc8SMatthias Ringwald } 3212ce263fc8SMatthias Ringwald 32139de679b7SMilanka Ringwald void hfp_ag_set_last_dialed_number(const char * number){ 32149de679b7SMilanka Ringwald hfp_gsm_set_last_dialed_number(number); 32159de679b7SMilanka Ringwald } 32169de679b7SMilanka Ringwald 3217c5fa3c94SMilanka Ringwald uint8_t hfp_ag_notify_incoming_call_waiting(hci_con_handle_t acl_handle){ 32189c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3219a33eb0c4SMilanka Ringwald if (!hfp_connection){ 3220c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3221a33eb0c4SMilanka Ringwald } 3222c5fa3c94SMilanka Ringwald 3223c5fa3c94SMilanka Ringwald if (!hfp_connection->call_waiting_notification_enabled){ 3224c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3225c5fa3c94SMilanka Ringwald } 3226a0ffb263SMatthias Ringwald 3227a0ffb263SMatthias Ringwald hfp_connection->ag_notify_incoming_call_waiting = 1; 3228f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 3229c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 3230a0ffb263SMatthias Ringwald } 3231aa10b9cbSMatthias Ringwald void hfp_ag_create_sdp_record_with_codecs(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr, 3232aa10b9cbSMatthias Ringwald const char * name, uint8_t ability_to_reject_call, uint16_t supported_features, 3233aa10b9cbSMatthias Ringwald uint8_t codecs_nr, const uint8_t * codecs){ 323476cc1527SMatthias Ringwald if (!name){ 3235aeb0f0feSMatthias Ringwald name = hfp_ag_default_service_name; 323676cc1527SMatthias Ringwald } 323776cc1527SMatthias Ringwald hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, rfcomm_channel_nr, name); 323876cc1527SMatthias Ringwald 323976cc1527SMatthias Ringwald /* 324076cc1527SMatthias Ringwald * 0x01 – Ability to reject a call 324176cc1527SMatthias Ringwald * 0x00 – No ability to reject a call 324276cc1527SMatthias Ringwald */ 324376cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301); // Hands-Free Profile - Network 324476cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call); 324576cc1527SMatthias Ringwald 324676cc1527SMatthias Ringwald // Construct SupportedFeatures for SDP bitmap: 324776cc1527SMatthias Ringwald // 324876cc1527SMatthias Ringwald // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 324976cc1527SMatthias Ringwald // of the Bits 0 to 4 of the unsolicited result code +BRSF" 325076cc1527SMatthias Ringwald // 3251aa10b9cbSMatthias Ringwald // Wide band speech (bit 5) and LC3-SWB (bit 8) require Codec negotiation 325276cc1527SMatthias Ringwald // 325376cc1527SMatthias Ringwald uint16_t sdp_features = supported_features & 0x1f; 3254ef3ae4ebSMilanka Ringwald 3255ef3ae4ebSMilanka Ringwald if (supported_features & (1 << HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS)){ 3256ef3ae4ebSMilanka Ringwald sdp_features |= 1 << 6; 3257ef3ae4ebSMilanka Ringwald } 3258ef3ae4ebSMilanka Ringwald 3259ef3ae4ebSMilanka Ringwald if (supported_features & (1 << HFP_AGSF_VOICE_RECOGNITION_TEXT)){ 3260ef3ae4ebSMilanka Ringwald sdp_features |= 1 << 7; 3261ef3ae4ebSMilanka Ringwald } 3262ef3ae4ebSMilanka Ringwald 3263aa10b9cbSMatthias Ringwald // codecs 3264aa10b9cbSMatthias Ringwald if ((supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION)) != 0){ 3265aa10b9cbSMatthias Ringwald uint8_t i; 3266aa10b9cbSMatthias Ringwald for (i=0;i<codecs_nr;i++){ 3267aa10b9cbSMatthias Ringwald switch (codecs[i]){ 3268aa10b9cbSMatthias Ringwald case HFP_CODEC_MSBC: 3269aa10b9cbSMatthias Ringwald sdp_features |= 1 << 5; 3270aa10b9cbSMatthias Ringwald break; 3271aa10b9cbSMatthias Ringwald case HFP_CODEC_LC3_SWB: 3272aa10b9cbSMatthias Ringwald sdp_features |= 1 << 8; 3273aa10b9cbSMatthias Ringwald break; 3274aa10b9cbSMatthias Ringwald } 3275aa10b9cbSMatthias Ringwald } 3276aa10b9cbSMatthias Ringwald } 3277aa10b9cbSMatthias Ringwald 327876cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 327976cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 328076cc1527SMatthias Ringwald } 328176cc1527SMatthias Ringwald 3282aa10b9cbSMatthias Ringwald void hfp_ag_create_sdp_record(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr, const char * name, 3283aa10b9cbSMatthias Ringwald uint8_t ability_to_reject_call, uint16_t supported_features, int wide_band_speech){ 3284aa10b9cbSMatthias Ringwald uint8_t codecs_nr; 3285aa10b9cbSMatthias Ringwald const uint8_t * codecs; 3286aa10b9cbSMatthias Ringwald const uint8_t wide_band_codecs[] = { HFP_CODEC_MSBC }; 3287aa10b9cbSMatthias Ringwald if (wide_band_speech == 0){ 3288aa10b9cbSMatthias Ringwald codecs_nr = 0; 3289aa10b9cbSMatthias Ringwald codecs = NULL; 3290aa10b9cbSMatthias Ringwald } else { 3291aa10b9cbSMatthias Ringwald codecs_nr = 1; 3292aa10b9cbSMatthias Ringwald codecs = wide_band_codecs; 3293aa10b9cbSMatthias Ringwald } 3294aa10b9cbSMatthias Ringwald hfp_ag_create_sdp_record_with_codecs(service, service_record_handle, rfcomm_channel_nr, name, 3295aa10b9cbSMatthias Ringwald ability_to_reject_call, supported_features, codecs_nr, codecs); 3296aa10b9cbSMatthias Ringwald } 3297aa10b9cbSMatthias Ringwald 329876cc1527SMatthias Ringwald void hfp_ag_register_packet_handler(btstack_packet_handler_t callback){ 329968466199SMilanka Ringwald btstack_assert(callback != NULL); 330068466199SMilanka Ringwald 330176cc1527SMatthias Ringwald hfp_ag_callback = callback; 330276cc1527SMatthias Ringwald hfp_set_ag_callback(callback); 330376cc1527SMatthias Ringwald } 330481e25d0aSMatthias Ringwald 330581e25d0aSMatthias Ringwald void hfp_ag_register_custom_call_sm_handler(bool (*handler)(hfp_ag_call_event_t event)){ 330681e25d0aSMatthias Ringwald hfp_ag_custom_call_sm_handler = handler; 330781e25d0aSMatthias Ringwald } 3308471dea41SMatthias Ringwald 3309471dea41SMatthias Ringwald void hfp_ag_register_custom_at_command(hfp_custom_at_command_t * custom_at_command){ 3310471dea41SMatthias Ringwald hfp_register_custom_ag_command(custom_at_command); 3311471dea41SMatthias Ringwald } 3312