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: 1101df9853c5SMatthias Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1102df9853c5SMatthias Ringwald return 1; 11033deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 1104485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1105ce263fc8SMatthias Ringwald return 1; 11063deb3ec6SMatthias Ringwald default: 11073deb3ec6SMatthias Ringwald break; 11083deb3ec6SMatthias Ringwald } 1109aa4dd815SMatthias Ringwald return 0; 11103deb3ec6SMatthias Ringwald } 11113deb3ec6SMatthias Ringwald 1112a0ffb263SMatthias Ringwald static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){ 1113505f1c30SMatthias Ringwald if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) || 1114505f1c30SMatthias Ringwald (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0; 11153deb3ec6SMatthias Ringwald 1116a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 111752cbdd6dSMilanka Ringwald 111852cbdd6dSMilanka Ringwald if (hfp_connection->release_audio_connection){ 111952cbdd6dSMilanka Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 112052cbdd6dSMilanka Ringwald hfp_connection->release_audio_connection = 0; 112152cbdd6dSMilanka Ringwald gap_disconnect(hfp_connection->sco_handle); 112252cbdd6dSMilanka Ringwald return 1; 112352cbdd6dSMilanka Ringwald } 1124aa4dd815SMatthias Ringwald 112529cddf58SMatthias Ringwald // accept incoming audio connection (codec negotiation is not used) 1126447743f7SMatthias Ringwald if (hfp_connection->accept_sco && (hfp_sco_setup_active() == false)){ 112729cddf58SMatthias Ringwald // notify about codec selection if not done already 112829cddf58SMatthias Ringwald if (hfp_connection->negotiated_codec == 0){ 112929cddf58SMatthias Ringwald hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 113029cddf58SMatthias Ringwald } 113129cddf58SMatthias Ringwald // 113229cddf58SMatthias Ringwald bool incoming_eSCO = hfp_connection->accept_sco == 2; 113329cddf58SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 113429cddf58SMatthias Ringwald hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO); 113529cddf58SMatthias Ringwald return 1; 113629cddf58SMatthias Ringwald } 113729cddf58SMatthias Ringwald 1138aa4dd815SMatthias Ringwald // run codecs exchange 1139c04cf7ffSMilanka Ringwald int sent = codecs_exchange_state_machine(hfp_connection); 1140c04cf7ffSMilanka Ringwald if (sent) return 1; 1141aa4dd815SMatthias Ringwald 1142c04cf7ffSMilanka Ringwald if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0; 1143447743f7SMatthias Ringwald if (hfp_sco_setup_active()) return 0; 1144d2e34ffbSMatthias Ringwald if (hci_can_send_command_packet_now() == false) return 0; 1145a0ffb263SMatthias Ringwald if (hfp_connection->establish_audio_connection){ 1146a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 1147a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 1148eddcd308SMatthias Ringwald hfp_setup_synchronous_connection(hfp_connection); 1149aa4dd815SMatthias Ringwald return 1; 1150aa4dd815SMatthias Ringwald } 1151aa4dd815SMatthias Ringwald return 0; 1152aa4dd815SMatthias Ringwald } 1153aa4dd815SMatthias Ringwald 1154056efd45SMatthias Ringwald static void hfp_ag_set_callsetup_indicator(void){ 1155056efd45SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup"); 1156056efd45SMatthias Ringwald if (!indicator){ 1157056efd45SMatthias Ringwald log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing"); 1158056efd45SMatthias Ringwald return; 1159056efd45SMatthias Ringwald }; 1160056efd45SMatthias Ringwald indicator->status = hfp_gsm_callsetup_status(); 1161056efd45SMatthias Ringwald } 1162056efd45SMatthias Ringwald 1163056efd45SMatthias Ringwald static void hfp_ag_set_callheld_indicator(void){ 1164056efd45SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld"); 1165056efd45SMatthias Ringwald if (!indicator){ 1166056efd45SMatthias Ringwald log_error("hfp_ag_set_callheld_state: callheld indicator is missing"); 1167056efd45SMatthias Ringwald return; 1168056efd45SMatthias Ringwald }; 1169056efd45SMatthias Ringwald indicator->status = hfp_gsm_callheld_status(); 1170056efd45SMatthias Ringwald } 1171056efd45SMatthias Ringwald 1172aa4dd815SMatthias Ringwald // 1173fcd59412SMilanka Ringwald // transition implementations for hfp_ag_call_state_machine 1174aa4dd815SMatthias Ringwald // 1175aa4dd815SMatthias Ringwald 11762ff1d7d7SMilanka Ringwald static void hfp_ag_hf_start_ringing_incoming(hfp_connection_t * hfp_connection){ 1177f197e761SMatthias Ringwald if (use_in_band_tone(hfp_connection)){ 1178a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING; 1179d97d752dSMilanka Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 1180aa4dd815SMatthias Ringwald } else { 11819493406bSMilanka Ringwald hfp_connection->call_state = HFP_CALL_INCOMING_RINGING; 1182aa4dd815SMatthias Ringwald } 1183aa4dd815SMatthias Ringwald } 1184aa4dd815SMatthias Ringwald 1185fcd59412SMilanka Ringwald static void hfp_ag_hf_start_ringing_outgoing(hfp_connection_t * hfp_connection){ 1186fcd59412SMilanka Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING; 1187fcd59412SMilanka Ringwald } 1188fcd59412SMilanka Ringwald 1189a0ffb263SMatthias Ringwald static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){ 1190a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 0; 1191a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = 0; 1192aa4dd815SMatthias Ringwald } 1193aa4dd815SMatthias Ringwald 1194a4f85bd2SMilanka Ringwald 119531f55b91SMilanka Ringwald static void hfp_ag_trigger_incoming_call_during_active_one(void){ 119631f55b91SMilanka Ringwald btstack_linked_list_iterator_t it; 119731f55b91SMilanka Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 119831f55b91SMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 119931f55b91SMilanka Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 120031f55b91SMilanka Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 120131f55b91SMilanka Ringwald if (hfp_connection->call_state != HFP_CALL_ACTIVE) continue; 120231f55b91SMilanka Ringwald 120331f55b91SMilanka Ringwald hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING; 120431f55b91SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 120531f55b91SMilanka Ringwald } 120631f55b91SMilanka Ringwald } 120731f55b91SMilanka Ringwald 1208d3709f3eSMatthias Ringwald static void hfp_ag_trigger_ring_and_clip(void) { 1209d3709f3eSMatthias Ringwald btstack_linked_list_iterator_t it; 1210d3709f3eSMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1211d3709f3eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1212d3709f3eSMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1213d3709f3eSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1214d3709f3eSMatthias Ringwald switch (hfp_connection->call_state){ 1215d3709f3eSMatthias Ringwald case HFP_CALL_INCOMING_RINGING: 1216d3709f3eSMatthias Ringwald case HFP_CALL_OUTGOING_RINGING: 1217d3709f3eSMatthias Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING: 12183561686eSMatthias Ringwald // Queue RING on this connection 1219a4f85bd2SMilanka Ringwald hfp_connection->ag_ring = 1; 12203561686eSMatthias Ringwald 12213561686eSMatthias Ringwald // HFP v1.8, 4.23 Calling Line Identification (CLI) Notification 12223561686eSMatthias Ringwald // "If the calling subscriber number information is available from the network, the AG shall issue the 12233561686eSMatthias Ringwald // +CLIP unsolicited result code just after every RING indication when the HF is alerted in an incoming call." 12243561686eSMatthias Ringwald hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled; 12253561686eSMatthias Ringwald 12263561686eSMatthias Ringwald // trigger next message 1227a4f85bd2SMilanka Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 1228b7e27351SMilanka Ringwald break; 1229b7e27351SMilanka Ringwald default: 1230b7e27351SMilanka Ringwald break; 1231b7e27351SMilanka Ringwald } 1232a4f85bd2SMilanka Ringwald } 1233a4f85bd2SMilanka Ringwald } 1234a4f85bd2SMilanka Ringwald 123550b9afa4SMilanka Ringwald // trigger RING and CLIP messages on connections that are ringing 123650b9afa4SMilanka Ringwald static void hfp_ag_ring_timeout_handler(btstack_timer_source_t * timer){ 123750b9afa4SMilanka Ringwald hfp_ag_trigger_ring_and_clip(); 123850b9afa4SMilanka Ringwald 12396db648bcSMilanka Ringwald btstack_run_loop_set_timer(timer, HFP_RING_PERIOD_MS); // 2 seconds timeout 124050b9afa4SMilanka Ringwald btstack_run_loop_add_timer(timer); 1241a4f85bd2SMilanka Ringwald } 1242a4f85bd2SMilanka Ringwald 1243a4f85bd2SMilanka Ringwald static void hfp_ag_ring_timeout_stop(void){ 1244a4f85bd2SMilanka Ringwald btstack_run_loop_remove_timer(&hfp_ag_ring_timeout); 1245a4f85bd2SMilanka Ringwald } 1246a4f85bd2SMilanka Ringwald 1247a4f85bd2SMilanka Ringwald static void hfp_ag_start_ringing(void){ 124850b9afa4SMilanka Ringwald // setup ring timer 124950b9afa4SMilanka Ringwald btstack_run_loop_remove_timer(&hfp_ag_ring_timeout); 125050b9afa4SMilanka Ringwald btstack_run_loop_set_timer_handler(&hfp_ag_ring_timeout, hfp_ag_ring_timeout_handler); 12516db648bcSMilanka Ringwald btstack_run_loop_set_timer(&hfp_ag_ring_timeout, HFP_RING_PERIOD_MS); // 2 seconds timeout 125250b9afa4SMilanka Ringwald btstack_run_loop_add_timer(&hfp_ag_ring_timeout); 125350b9afa4SMilanka Ringwald 125450b9afa4SMilanka Ringwald // emit start ringing 1255a4f85bd2SMilanka Ringwald hfp_ag_emit_general_simple_event(HFP_SUBEVENT_START_RINGING); 125650b9afa4SMilanka Ringwald 125750b9afa4SMilanka Ringwald // send initial RING + CLIP 125850b9afa4SMilanka Ringwald hfp_ag_trigger_ring_and_clip(); 1259a4f85bd2SMilanka Ringwald } 1260a4f85bd2SMilanka Ringwald 1261a4f85bd2SMilanka Ringwald static void hfp_ag_stop_ringing(void){ 1262a4f85bd2SMilanka Ringwald hfp_ag_ring_timeout_stop(); 1263a4f85bd2SMilanka Ringwald hfp_ag_emit_general_simple_event(HFP_SUBEVENT_STOP_RINGING); 1264a4f85bd2SMilanka Ringwald 1265a4f85bd2SMilanka Ringwald btstack_linked_list_iterator_t it; 1266a4f85bd2SMilanka Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1267a4f85bd2SMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1268a4f85bd2SMilanka Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1269a4f85bd2SMilanka Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1270a4f85bd2SMilanka Ringwald if ((hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) && 1271a4f85bd2SMilanka Ringwald (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue; 1272a4f85bd2SMilanka Ringwald hfp_ag_hf_stop_ringing(hfp_connection); 1273a4f85bd2SMilanka Ringwald } 1274a4f85bd2SMilanka Ringwald } 1275a4f85bd2SMilanka Ringwald 127631f55b91SMilanka Ringwald static void hfp_ag_trigger_incoming_call_idle(void){ 1277aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name("callsetup"); 1278aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 1279aa4dd815SMatthias Ringwald 12803561686eSMatthias Ringwald // update callsetup state in connections 1281665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1282665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1283665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1284a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 128566c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 128631f55b91SMilanka Ringwald if (hfp_connection->call_state != HFP_CALL_IDLE) continue; 128731f55b91SMilanka Ringwald 1288a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1289a4f85bd2SMilanka Ringwald 1290a4f85bd2SMilanka Ringwald hfp_ag_hf_start_ringing_incoming(hfp_connection); 1291a4f85bd2SMilanka Ringwald 1292f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1293aa4dd815SMatthias Ringwald } 12943561686eSMatthias Ringwald 12953561686eSMatthias Ringwald // start local ringing - started after connection state has been updated 12963561686eSMatthias Ringwald hfp_ag_start_ringing(); 1297aa4dd815SMatthias Ringwald } 1298aa4dd815SMatthias Ringwald 1299fe899794SMatthias Ringwald static void hfp_ag_trigger_outgoing_call(void){ 1300fe899794SMatthias Ringwald btstack_linked_list_iterator_t it; 1301fe899794SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1302fe899794SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1303fe899794SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1304fe899794SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1305fe899794SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 1306fe899794SMatthias Ringwald if (hfp_connection->call_state == HFP_CALL_IDLE){ 1307fe899794SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 1308fe899794SMatthias Ringwald } 1309fe899794SMatthias Ringwald } 1310fe899794SMatthias Ringwald } 13115a7033e2SMatthias Ringwald 13125a7033e2SMatthias Ringwald static void hfp_ag_handle_outgoing_call_accepted(hfp_connection_t * hfp_connection){ 13135a7033e2SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING; 13145a7033e2SMatthias Ringwald 13155a7033e2SMatthias Ringwald // trigger callsetup to be 13165a7033e2SMatthias Ringwald int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT; 13175a7033e2SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_ACCEPTED, 0, 0, NULL); 13185a7033e2SMatthias Ringwald 13195a7033e2SMatthias Ringwald int indicator_index ; 13205a7033e2SMatthias Ringwald 13215a7033e2SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 13225a7033e2SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callsetup"); 13235a7033e2SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 13245a7033e2SMatthias Ringwald 13255a7033e2SMatthias Ringwald // put current call on hold if active 13265a7033e2SMatthias Ringwald if (put_call_on_hold){ 13275a7033e2SMatthias Ringwald log_info("AG putting current call on hold for new outgoing call"); 13285a7033e2SMatthias Ringwald hfp_ag_set_callheld_indicator(); 13295a7033e2SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callheld"); 13305a7033e2SMatthias Ringwald hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]); 13315a7033e2SMatthias Ringwald } 13325a7033e2SMatthias Ringwald 13335a7033e2SMatthias Ringwald // start audio if needed 13345a7033e2SMatthias Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 13355a7033e2SMatthias Ringwald } 1336fe899794SMatthias Ringwald 13374a2e058fSMatthias Ringwald static void hfp_ag_transfer_indicator(const char * name){ 13384a2e058fSMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name(name); 1339aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 1340aa4dd815SMatthias Ringwald 1341665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1342665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1343665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1344a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 134566c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1346a0ffb263SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 1347a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1348f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1349aa4dd815SMatthias Ringwald } 1350aa4dd815SMatthias Ringwald } 1351aa4dd815SMatthias Ringwald 13524a2e058fSMatthias Ringwald static void hfp_ag_transfer_callsetup_state(void){ 13534a2e058fSMatthias Ringwald hfp_ag_transfer_indicator("callsetup"); 13544a2e058fSMatthias Ringwald } 13554a2e058fSMatthias Ringwald 1356aa4dd815SMatthias Ringwald static void hfp_ag_transfer_call_state(void){ 13574a2e058fSMatthias Ringwald hfp_ag_transfer_indicator("call"); 1358aa4dd815SMatthias Ringwald } 1359aa4dd815SMatthias Ringwald 1360aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callheld_state(void){ 13614a2e058fSMatthias Ringwald hfp_ag_transfer_indicator("callheld"); 1362aa4dd815SMatthias Ringwald } 1363aa4dd815SMatthias Ringwald 1364aa4dd815SMatthias Ringwald static void hfp_ag_hf_accept_call(hfp_connection_t * source){ 1365aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 1366aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1367aa4dd815SMatthias Ringwald 1368a4f85bd2SMilanka Ringwald hfp_ag_stop_ringing(); 1369a4f85bd2SMilanka Ringwald 1370665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1371665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1372665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1373a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 137466c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 13759493406bSMilanka Ringwald if ((hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) && 1376505f1c30SMatthias Ringwald (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue; 1377aa4dd815SMatthias Ringwald 1378a0ffb263SMatthias Ringwald if (hfp_connection == source){ 1379aa4dd815SMatthias Ringwald 1380f197e761SMatthias Ringwald if (use_in_band_tone(hfp_connection)){ 1381a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1382aa4dd815SMatthias Ringwald } else { 1383a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE; 1384d97d752dSMilanka Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 1385aa4dd815SMatthias Ringwald } 1386aa4dd815SMatthias Ringwald 1387a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1388a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1389aa4dd815SMatthias Ringwald 1390aa4dd815SMatthias Ringwald } else { 1391a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 1392aa4dd815SMatthias Ringwald } 1393f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1394aa4dd815SMatthias Ringwald } 1395aa4dd815SMatthias Ringwald } 1396aa4dd815SMatthias Ringwald 1397aa4dd815SMatthias Ringwald static void hfp_ag_ag_accept_call(void){ 1398aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 1399aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1400aa4dd815SMatthias Ringwald 1401a4f85bd2SMilanka Ringwald hfp_ag_stop_ringing(); 1402a4f85bd2SMilanka Ringwald 1403665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1404665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1405665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1406a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 140766c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1408a5764cd2SMatthias Ringwald if ((hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) && 1409a5764cd2SMatthias Ringwald (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue; 1410aa4dd815SMatthias Ringwald 1411a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION; 1412aa4dd815SMatthias Ringwald 1413a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1414a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1415aa4dd815SMatthias Ringwald 1416f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1417aa4dd815SMatthias Ringwald break; // only single 1418aa4dd815SMatthias Ringwald } 1419aa4dd815SMatthias Ringwald } 1420aa4dd815SMatthias Ringwald 1421aa4dd815SMatthias Ringwald static void hfp_ag_trigger_reject_call(void){ 1422aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1423a4f85bd2SMilanka Ringwald 1424a4f85bd2SMilanka Ringwald hfp_ag_stop_ringing(); 1425a4f85bd2SMilanka Ringwald 1426665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1427665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1428665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1429665d90f2SMatthias Ringwald hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 143066c5995fSMatthias Ringwald if (connection->local_role != HFP_ROLE_AG) continue; 143107a44c5eSMilanka Ringwald 143207a44c5eSMilanka Ringwald switch (connection->call_state){ 143307a44c5eSMilanka Ringwald case HFP_CALL_INCOMING_RINGING: 143407a44c5eSMilanka Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING: 143507a44c5eSMilanka Ringwald case HFP_CALL_OUTGOING_RINGING: 143607a44c5eSMilanka Ringwald case HFP_CALL_OUTGOING_INITIATED: 143707a44c5eSMilanka Ringwald case HFP_CALL_OUTGOING_DIALING: 143807a44c5eSMilanka Ringwald break; 143907a44c5eSMilanka Ringwald default: 144007a44c5eSMilanka Ringwald continue; 144107a44c5eSMilanka Ringwald } 1442095a9f2bSMilanka Ringwald 1443aa4dd815SMatthias Ringwald connection->call_state = HFP_CALL_IDLE; 1444095a9f2bSMilanka Ringwald connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1445095a9f2bSMilanka Ringwald 1446095a9f2bSMilanka Ringwald if (connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 1447095a9f2bSMilanka Ringwald connection->release_audio_connection = 1; 1448095a9f2bSMilanka Ringwald } 1449095a9f2bSMilanka Ringwald hfp_emit_simple_event(connection, HFP_SUBEVENT_CALL_TERMINATED); 1450f0aeb307SMatthias Ringwald hfp_ag_run_for_context(connection); 1451095a9f2bSMilanka Ringwald rfcomm_request_can_send_now_event(connection->rfcomm_cid); 1452aa4dd815SMatthias Ringwald } 1453aa4dd815SMatthias Ringwald } 1454aa4dd815SMatthias Ringwald 1455aa4dd815SMatthias Ringwald static void hfp_ag_trigger_terminate_call(void){ 1456aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 1457aa4dd815SMatthias Ringwald 1458a4f85bd2SMilanka Ringwald // no ringing during call 1459a4f85bd2SMilanka Ringwald 1460665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1461665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1462665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1463a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 146466c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1465a0ffb263SMatthias Ringwald if (hfp_connection->call_state == HFP_CALL_IDLE) continue; 1466fb75b483SMilanka Ringwald 1467a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 1468a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1469095a9f2bSMilanka Ringwald 14706d78145cSMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 1471a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 1; 14726d78145cSMatthias Ringwald } 1473095a9f2bSMilanka Ringwald 1474fb75b483SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED); 1475f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1476a13ff7d1SMilanka Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 1477aa4dd815SMatthias Ringwald } 1478aa4dd815SMatthias Ringwald } 1479aa4dd815SMatthias Ringwald 14800cb5b971SMatthias Ringwald static void hfp_ag_set_call_indicator(void){ 1481aa4dd815SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call"); 1482aa4dd815SMatthias Ringwald if (!indicator){ 1483aa4dd815SMatthias Ringwald log_error("hfp_ag_set_call_state: call indicator is missing"); 148445718b6fSMatthias Ringwald return; 1485aa4dd815SMatthias Ringwald }; 1486d210d9c4SMatthias Ringwald indicator->status = hfp_gsm_call_status(); 1487aa4dd815SMatthias Ringwald } 1488aa4dd815SMatthias Ringwald 1489aa4dd815SMatthias Ringwald static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){ 1490665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1491665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1492665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1493a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 149466c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1495a0ffb263SMatthias Ringwald if (hfp_connection->call_state == call_state) return hfp_connection; 1496aa4dd815SMatthias Ringwald } 1497aa4dd815SMatthias Ringwald return NULL; 1498aa4dd815SMatthias Ringwald } 1499aa4dd815SMatthias Ringwald 1500a5bdcda8SMatthias Ringwald static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){ 1501665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1502665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1503665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1504a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 150566c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1506a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = state + 1; 1507ce263fc8SMatthias Ringwald } 1508ce263fc8SMatthias Ringwald } 1509ce263fc8SMatthias Ringwald 1510a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 1511aa4dd815SMatthias Ringwald int indicator_index; 1512a0ffb263SMatthias Ringwald switch (hfp_connection->call_state){ 1513aa4dd815SMatthias Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING: 1514a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 1515a0ffb263SMatthias Ringwald // we got event: audio hfp_connection established 15169493406bSMilanka Ringwald hfp_connection->call_state = HFP_CALL_INCOMING_RINGING; 1517aa4dd815SMatthias Ringwald break; 1518aa4dd815SMatthias Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE: 1519a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 1520a0ffb263SMatthias Ringwald // we got event: audio hfp_connection established 1521a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1522aa4dd815SMatthias Ringwald break; 1523aa4dd815SMatthias Ringwald case HFP_CALL_W2_SEND_CALL_WAITING: 1524a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_CHLD; 1525a0ffb263SMatthias Ringwald hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid); 1526aa4dd815SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callsetup"); 1527a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1528aa4dd815SMatthias Ringwald break; 1529aa4dd815SMatthias Ringwald default: 1530aa4dd815SMatthias Ringwald break; 1531aa4dd815SMatthias Ringwald } 1532aa4dd815SMatthias Ringwald return 0; 1533aa4dd815SMatthias Ringwald } 153439426febSMatthias Ringwald 1535e84fa067SMatthias Ringwald // queue 'error' to HF 1536e84fa067SMatthias Ringwald static void hfp_ag_queue_error(hfp_connection_t * hfp_connection){ 15375e87d1ceSMatthias Ringwald hfp_connection->send_error++; 1538e84fa067SMatthias Ringwald } 1539e84fa067SMatthias Ringwald 1540e84fa067SMatthias Ringwald // queue 'ok' to HF 1541e84fa067SMatthias Ringwald static void hfp_ag_queue_ok(hfp_connection_t * hfp_connection){ 15425e87d1ceSMatthias Ringwald hfp_connection->ok_pending++; 1543e84fa067SMatthias Ringwald } 1544e84fa067SMatthias Ringwald 154539426febSMatthias Ringwald // functions extracted from hfp_ag_call_sm below 15469f9621cfSMatthias Ringwald static void hfp_ag_handle_reject_outgoing_call(void){ 154739426febSMatthias Ringwald hfp_connection_t * hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 154839426febSMatthias Ringwald if (!hfp_connection){ 154939426febSMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state"); 155039426febSMatthias Ringwald return; 155139426febSMatthias Ringwald } 155239426febSMatthias Ringwald 1553f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_REJECTED, 0, 0, NULL); 155439426febSMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 1555e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 155639426febSMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 155739426febSMatthias Ringwald } 155839426febSMatthias Ringwald 1559a0ffb263SMatthias Ringwald // hfp_connection is used to identify originating HF 1560a0ffb263SMatthias Ringwald static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){ 156181e25d0aSMatthias Ringwald 156281e25d0aSMatthias Ringwald // allow to intercept call statemachine events 156381e25d0aSMatthias Ringwald if (hfp_ag_custom_call_sm_handler != NULL){ 156481e25d0aSMatthias Ringwald bool handle_event = (*hfp_ag_custom_call_sm_handler)(event); 156581e25d0aSMatthias Ringwald if (!handle_event) return; 156681e25d0aSMatthias Ringwald } 156781e25d0aSMatthias Ringwald 1568d210d9c4SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1569d210d9c4SMatthias Ringwald int callheld_indicator_index = get_ag_indicator_index_for_name("callheld"); 1570d210d9c4SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 157174386ee0SMatthias Ringwald 1572aa4dd815SMatthias Ringwald switch (event){ 1573aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL: 1574d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1575aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1576d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 157712cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1578f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL, 0, 0, NULL); 1579d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 158031f55b91SMilanka Ringwald hfp_ag_trigger_incoming_call_idle(); 158160ebb071SMilanka Ringwald log_info("AG rings"); 1582aa4dd815SMatthias Ringwald break; 1583aa4dd815SMatthias Ringwald default: 15843deb3ec6SMatthias Ringwald break; 15853deb3ec6SMatthias Ringwald } 15863deb3ec6SMatthias Ringwald break; 1587aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1588d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 158912cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1590f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL, 0, 0, NULL); 1591d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 159231f55b91SMilanka Ringwald hfp_ag_trigger_incoming_call_during_active_one(); 159360ebb071SMilanka Ringwald log_info("AG call waiting"); 1594aa4dd815SMatthias Ringwald break; 1595aa4dd815SMatthias Ringwald default: 15963deb3ec6SMatthias Ringwald break; 15973deb3ec6SMatthias Ringwald } 15983deb3ec6SMatthias Ringwald break; 15997bbeb3adSMilanka Ringwald default: 16007bbeb3adSMilanka Ringwald break; 1601aa4dd815SMatthias Ringwald } 1602aa4dd815SMatthias Ringwald break; 1603aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG: 1604d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1605aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1606d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 160712cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1608f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, 0, 0, NULL); 1609d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1610d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1611aa4dd815SMatthias Ringwald hfp_ag_ag_accept_call(); 161260ebb071SMilanka Ringwald log_info("AG answers call, accept call by GSM"); 1613aa4dd815SMatthias Ringwald break; 1614aa4dd815SMatthias Ringwald default: 16153deb3ec6SMatthias Ringwald break; 16163deb3ec6SMatthias Ringwald } 1617aa4dd815SMatthias Ringwald break; 1618aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1619d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1620aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 162160ebb071SMilanka Ringwald log_info("AG: current call is placed on hold, incoming call gets active"); 1622f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, 0, 0, NULL); 1623d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1624d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1625ce263fc8SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1626ce263fc8SMatthias Ringwald hfp_ag_transfer_callheld_state(); 1627aa4dd815SMatthias Ringwald break; 1628aa4dd815SMatthias Ringwald default: 1629aa4dd815SMatthias Ringwald break; 1630aa4dd815SMatthias Ringwald } 1631aa4dd815SMatthias Ringwald break; 16327bbeb3adSMilanka Ringwald default: 16337bbeb3adSMilanka Ringwald break; 1634aa4dd815SMatthias Ringwald } 1635aa4dd815SMatthias Ringwald break; 1636ce263fc8SMatthias Ringwald 1637ce263fc8SMatthias Ringwald case HFP_AG_HELD_CALL_JOINED_BY_AG: 1638d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1639ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1640d0c20769SMatthias Ringwald switch (hfp_gsm_callheld_status()){ 1641ce263fc8SMatthias Ringwald case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED: 164260ebb071SMilanka Ringwald log_info("AG: joining held call with active call"); 1643f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_HELD_CALL_JOINED_BY_AG, 0, 0, NULL); 1644d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1645ce263fc8SMatthias Ringwald hfp_ag_transfer_callheld_state(); 1646ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL); 1647ce263fc8SMatthias Ringwald break; 1648ce263fc8SMatthias Ringwald default: 1649ce263fc8SMatthias Ringwald break; 1650ce263fc8SMatthias Ringwald } 1651ce263fc8SMatthias Ringwald break; 1652ce263fc8SMatthias Ringwald default: 1653ce263fc8SMatthias Ringwald break; 1654ce263fc8SMatthias Ringwald } 1655ce263fc8SMatthias Ringwald break; 1656ce263fc8SMatthias Ringwald 1657aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF: 1658d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1659aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1660d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 166112cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1662f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, 0, 0, NULL); 1663d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1664d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1665a0ffb263SMatthias Ringwald hfp_ag_hf_accept_call(hfp_connection); 1666e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 166760ebb071SMilanka Ringwald log_info("HF answers call, accept call by GSM"); 1668ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED); 1669aa4dd815SMatthias Ringwald break; 1670aa4dd815SMatthias Ringwald default: 1671aa4dd815SMatthias Ringwald break; 1672aa4dd815SMatthias Ringwald } 16733deb3ec6SMatthias Ringwald break; 16743deb3ec6SMatthias Ringwald default: 16753deb3ec6SMatthias Ringwald break; 16763deb3ec6SMatthias Ringwald } 16773deb3ec6SMatthias Ringwald break; 16783deb3ec6SMatthias Ringwald 1679ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG: 1680d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1681ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1682d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 168312cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1684f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, 0, 0, NULL); 168527bb1817SMatthias Ringwald hfp_ag_response_and_hold_active = true; 1686ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1687a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1688ac6f828eSMilanka Ringwald // as with regular call 1689d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1690d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1691ce263fc8SMatthias Ringwald hfp_ag_ag_accept_call(); 169260ebb071SMilanka Ringwald log_info("AG response and hold - hold by AG"); 1693ce263fc8SMatthias Ringwald break; 1694ce263fc8SMatthias Ringwald default: 1695ce263fc8SMatthias Ringwald break; 1696ce263fc8SMatthias Ringwald } 1697ce263fc8SMatthias Ringwald break; 1698ce263fc8SMatthias Ringwald default: 1699ce263fc8SMatthias Ringwald break; 1700ce263fc8SMatthias Ringwald } 1701ce263fc8SMatthias Ringwald break; 1702ce263fc8SMatthias Ringwald 1703ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF: 1704d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1705ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1706d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 170712cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1708f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, 0, 0, NULL); 170927bb1817SMatthias Ringwald hfp_ag_response_and_hold_active = true; 1710ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1711a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1712ac6f828eSMilanka Ringwald // as with regular call 1713d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1714d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1715a0ffb263SMatthias Ringwald hfp_ag_hf_accept_call(hfp_connection); 171660ebb071SMilanka Ringwald log_info("AG response and hold - hold by HF"); 1717ce263fc8SMatthias Ringwald break; 1718ce263fc8SMatthias Ringwald default: 1719ce263fc8SMatthias Ringwald break; 1720ce263fc8SMatthias Ringwald } 1721ce263fc8SMatthias Ringwald break; 1722ce263fc8SMatthias Ringwald default: 1723ce263fc8SMatthias Ringwald break; 1724ce263fc8SMatthias Ringwald } 1725ce263fc8SMatthias Ringwald break; 1726ce263fc8SMatthias Ringwald 1727ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG: 1728ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF: 1729ce263fc8SMatthias Ringwald if (!hfp_ag_response_and_hold_active) break; 1730ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1731f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, 0, 0, NULL); 173227bb1817SMatthias Ringwald hfp_ag_response_and_hold_active = false; 1733ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED; 1734a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 173560ebb071SMilanka Ringwald log_info("Held Call accepted and active"); 1736ce263fc8SMatthias Ringwald break; 1737ce263fc8SMatthias Ringwald 1738ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG: 1739ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF: 1740ce263fc8SMatthias Ringwald if (!hfp_ag_response_and_hold_active) break; 1741ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1742f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, 0, 0, NULL); 174327bb1817SMatthias Ringwald hfp_ag_response_and_hold_active = false; 1744ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1745a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1746ce263fc8SMatthias Ringwald // from terminate by ag 1747d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1748ce263fc8SMatthias Ringwald hfp_ag_trigger_terminate_call(); 1749ce263fc8SMatthias Ringwald break; 1750ce263fc8SMatthias Ringwald 1751aa4dd815SMatthias Ringwald case HFP_AG_TERMINATE_CALL_BY_HF: 1752d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1753aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1754d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 175512cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 175607a44c5eSMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 175707a44c5eSMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 1758f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL); 1759d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1760aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1761aa4dd815SMatthias Ringwald hfp_ag_trigger_reject_call(); 176260ebb071SMilanka Ringwald log_info("HF Rejected Incoming call, AG terminate call"); 1763aa4dd815SMatthias Ringwald break; 1764aa4dd815SMatthias Ringwald default: 1765aa4dd815SMatthias Ringwald break; 1766aa4dd815SMatthias Ringwald } 1767aa4dd815SMatthias Ringwald break; 176812cbbeeeSMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1769f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL); 17701ec112deSMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1771d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 17721ec112deSMatthias Ringwald hfp_ag_trigger_terminate_call(); 177360ebb071SMilanka Ringwald log_info("AG terminate call"); 1774aa4dd815SMatthias Ringwald break; 17757bbeb3adSMilanka Ringwald default: 17767bbeb3adSMilanka Ringwald break; 1777aa4dd815SMatthias Ringwald } 1778aa4dd815SMatthias Ringwald break; 17793deb3ec6SMatthias Ringwald 1780aa4dd815SMatthias Ringwald case HFP_AG_TERMINATE_CALL_BY_AG: 1781d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1782aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1783d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 178407a44c5eSMilanka Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 178507a44c5eSMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 178607a44c5eSMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1787f2aa99a9SMilanka Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1788f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_AG, 0, 0, NULL); 1789d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1790aa4dd815SMatthias Ringwald hfp_ag_trigger_reject_call(); 179160ebb071SMilanka Ringwald log_info("AG Rejected Incoming call, AG terminate call"); 1792aa4dd815SMatthias Ringwald break; 1793aa4dd815SMatthias Ringwald default: 1794aa4dd815SMatthias Ringwald break; 17953deb3ec6SMatthias Ringwald } 1796202c8a4cSMatthias Ringwald break; 179712cbbeeeSMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1798f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_AG, 0, 0, NULL); 1799d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1800d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1801aa4dd815SMatthias Ringwald hfp_ag_trigger_terminate_call(); 180260ebb071SMilanka Ringwald log_info("AG terminate call"); 1803aa4dd815SMatthias Ringwald break; 1804aa4dd815SMatthias Ringwald default: 18053deb3ec6SMatthias Ringwald break; 18063deb3ec6SMatthias Ringwald } 18073deb3ec6SMatthias Ringwald break; 1808aa4dd815SMatthias Ringwald case HFP_AG_CALL_DROPPED: 1809d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1810aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1811d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1812aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1813aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1814fb75b483SMilanka Ringwald hfp_ag_stop_ringing(); 1815fb75b483SMilanka Ringwald break; 1816fb75b483SMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 1817fb75b483SMilanka Ringwald log_info("Outgoing call interrupted\n"); 1818aa4dd815SMatthias Ringwald break; 1819aa4dd815SMatthias Ringwald default: 18203deb3ec6SMatthias Ringwald break; 18213deb3ec6SMatthias Ringwald } 1822f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL); 1823d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1824aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 182510c236bfSMatthias Ringwald hfp_ag_trigger_terminate_call(); 1826aa4dd815SMatthias Ringwald break; 1827aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1828ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_active) { 1829f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL); 1830ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1831a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1832ce263fc8SMatthias Ringwald } 1833f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL); 1834d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1835d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1836aa4dd815SMatthias Ringwald hfp_ag_trigger_terminate_call(); 183760ebb071SMilanka Ringwald log_info("AG notify call dropped"); 1838aa4dd815SMatthias Ringwald break; 1839aa4dd815SMatthias Ringwald default: 1840aa4dd815SMatthias Ringwald break; 1841aa4dd815SMatthias Ringwald } 1842aa4dd815SMatthias Ringwald break; 1843aa4dd815SMatthias Ringwald 18449ff73f41SMatthias Ringwald case HFP_AG_OUTGOING_CALL_INITIATED_BY_HF: 1845d210d9c4SMatthias Ringwald // directly reject call if number of free slots is exceeded 1846d210d9c4SMatthias Ringwald if (!hfp_gsm_call_possible()){ 1847e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 1848f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1849d210d9c4SMatthias Ringwald break; 1850d210d9c4SMatthias Ringwald } 1851fe899794SMatthias Ringwald 1852fe899794SMatthias Ringwald // note: number not used currently 18539ff73f41SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_INITIATED_BY_HF, 0, 0, (const char *) &hfp_connection->line_buffer[3]); 18549cae807eSMatthias Ringwald 1855a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 185674386ee0SMatthias Ringwald 1857ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]); 1858aa4dd815SMatthias Ringwald break; 1859aa4dd815SMatthias Ringwald 1860fe899794SMatthias Ringwald case HFP_AG_OUTGOING_CALL_INITIATED_BY_AG: 1861fe899794SMatthias Ringwald // directly reject call if number of free slots is exceeded 1862fe899794SMatthias Ringwald if (!hfp_gsm_call_possible()) { 1863fe899794SMatthias Ringwald // TODO: no error for user 1864fe899794SMatthias Ringwald break; 1865fe899794SMatthias Ringwald } 1866fe899794SMatthias Ringwald switch (hfp_gsm_call_status()) { 1867fe899794SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1868fe899794SMatthias Ringwald switch (hfp_gsm_callsetup_status()) { 1869fe899794SMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1870fe899794SMatthias Ringwald // note: number not used currently 1871fe899794SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_INITIATED_BY_AG, 0, 0, "1234567"); 1872fe899794SMatthias Ringwald // init call state for all connections 1873fe899794SMatthias Ringwald hfp_ag_trigger_outgoing_call(); 1874fe899794SMatthias Ringwald // get first one and accept call 1875fe899794SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 1876fe899794SMatthias Ringwald if (hfp_connection) { 1877fe899794SMatthias Ringwald hfp_ag_handle_outgoing_call_accepted(hfp_connection); 1878fe899794SMatthias Ringwald } 1879fe899794SMatthias Ringwald break; 1880fe899794SMatthias Ringwald default: 1881fe899794SMatthias Ringwald // TODO: no error for user 1882fe899794SMatthias Ringwald break; 1883fe899794SMatthias Ringwald } 1884fe899794SMatthias Ringwald break; 1885fe899794SMatthias Ringwald default: 1886fe899794SMatthias Ringwald // TODO: no error for user 1887fe899794SMatthias Ringwald break; 1888fe899794SMatthias Ringwald } 1889fe899794SMatthias Ringwald break; 18909cae807eSMatthias Ringwald case HFP_AG_OUTGOING_REDIAL_INITIATED:{ 1891d210d9c4SMatthias Ringwald // directly reject call if number of free slots is exceeded 1892d210d9c4SMatthias Ringwald if (!hfp_gsm_call_possible()){ 1893e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 1894f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1895d210d9c4SMatthias Ringwald break; 1896d210d9c4SMatthias Ringwald } 1897d210d9c4SMatthias Ringwald 1898f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_REDIAL_INITIATED, 0, 0, NULL); 1899a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 190074386ee0SMatthias Ringwald 190160ebb071SMilanka Ringwald log_info("Redial last number"); 19029cae807eSMatthias Ringwald char * last_dialed_number = hfp_gsm_last_dialed_number(); 1903aa4dd815SMatthias Ringwald 19049cae807eSMatthias Ringwald if (strlen(last_dialed_number) > 0){ 190560ebb071SMilanka Ringwald log_info("Last number exists: accept call"); 1906ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number); 19079cae807eSMatthias Ringwald } else { 190860ebb071SMilanka Ringwald log_info("log_infoLast number missing: reject call"); 19099f9621cfSMatthias Ringwald hfp_ag_handle_reject_outgoing_call(); 19109cae807eSMatthias Ringwald } 19119cae807eSMatthias Ringwald break; 19129cae807eSMatthias Ringwald } 1913aa4dd815SMatthias Ringwald case HFP_AG_OUTGOING_CALL_REJECTED: 19149f9621cfSMatthias Ringwald hfp_ag_handle_reject_outgoing_call(); 1915aa4dd815SMatthias Ringwald break; 1916aa4dd815SMatthias Ringwald 1917d210d9c4SMatthias Ringwald case HFP_AG_OUTGOING_CALL_ACCEPTED:{ 1918a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 1919a0ffb263SMatthias Ringwald if (!hfp_connection){ 1920a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state"); 1921aa4dd815SMatthias Ringwald break; 1922aa4dd815SMatthias Ringwald } 1923aa4dd815SMatthias Ringwald 1924e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 19255a7033e2SMatthias Ringwald hfp_ag_handle_outgoing_call_accepted(hfp_connection); 1926aa4dd815SMatthias Ringwald break; 1927d210d9c4SMatthias Ringwald } 1928aa4dd815SMatthias Ringwald case HFP_AG_OUTGOING_CALL_RINGING: 1929a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1930a0ffb263SMatthias Ringwald if (!hfp_connection){ 1931a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state"); 1932aa4dd815SMatthias Ringwald break; 1933aa4dd815SMatthias Ringwald } 1934d210d9c4SMatthias Ringwald 1935f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_RINGING, 0, 0, NULL); 1936a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING; 1937d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1938aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1939fcd59412SMilanka Ringwald hfp_ag_hf_start_ringing_outgoing(hfp_connection); 1940aa4dd815SMatthias Ringwald break; 1941aa4dd815SMatthias Ringwald 1942d210d9c4SMatthias Ringwald case HFP_AG_OUTGOING_CALL_ESTABLISHED:{ 1943aa4dd815SMatthias Ringwald // get outgoing call 1944a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING); 1945a0ffb263SMatthias Ringwald if (!hfp_connection){ 1946a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1947aa4dd815SMatthias Ringwald } 1948a0ffb263SMatthias Ringwald if (!hfp_connection){ 1949a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection"); 1950aa4dd815SMatthias Ringwald break; 1951aa4dd815SMatthias Ringwald } 1952d210d9c4SMatthias Ringwald 1953addc49c4SMilanka 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; 1954f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_ESTABLISHED, 0, 0, NULL); 1955a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1956d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1957d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1958aa4dd815SMatthias Ringwald hfp_ag_transfer_call_state(); 1959aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1960addc49c4SMilanka Ringwald if (callheld_status_call_on_hold_and_no_active_calls){ 1961d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1962aa4dd815SMatthias Ringwald hfp_ag_transfer_callheld_state(); 19633deb3ec6SMatthias Ringwald } 1964addc49c4SMilanka Ringwald hfp_ag_hf_stop_ringing(hfp_connection); 1965674ebed5SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED); 19663deb3ec6SMatthias Ringwald break; 1967d210d9c4SMatthias Ringwald } 1968d210d9c4SMatthias Ringwald 196912cbbeeeSMatthias Ringwald case HFP_AG_CALL_HOLD_USER_BUSY: 1970f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_USER_BUSY, 0, 0, NULL); 1971d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1972a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1973a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 197460ebb071SMilanka Ringwald log_info("AG: Call Waiting, User Busy"); 1975d210d9c4SMatthias Ringwald break; 1976d210d9c4SMatthias Ringwald 1977d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{ 1978d0c20769SMatthias Ringwald int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1979d0c20769SMatthias Ringwald int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD; 1980d210d9c4SMatthias Ringwald 1981d210d9c4SMatthias Ringwald // Releases all active calls (if any exist) and accepts the other (held or waiting) call. 1982d0c20769SMatthias Ringwald if (call_held || call_setup_in_progress){ 1983f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index, 1984f8737b81SMatthias Ringwald 0, NULL); 1985d0c20769SMatthias Ringwald 1986d0c20769SMatthias Ringwald } 1987d0c20769SMatthias Ringwald 1988d210d9c4SMatthias Ringwald if (call_setup_in_progress){ 198960ebb071SMilanka Ringwald log_info("AG: Call Dropped, Accept new call"); 1990d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1991a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1992d210d9c4SMatthias Ringwald } else { 199360ebb071SMilanka Ringwald log_info("AG: Call Dropped, Resume held call"); 1994d210d9c4SMatthias Ringwald } 1995d210d9c4SMatthias Ringwald if (call_held){ 1996d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1997a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1998d210d9c4SMatthias Ringwald } 1999d0c20769SMatthias Ringwald 2000a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 2001d210d9c4SMatthias Ringwald break; 2002d210d9c4SMatthias Ringwald } 2003d0c20769SMatthias Ringwald 2004d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{ 2005d210d9c4SMatthias Ringwald // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call. 2006d210d9c4SMatthias Ringwald // only update if callsetup changed 2007d0c20769SMatthias Ringwald int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 2008f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index, 0, 2009f8737b81SMatthias Ringwald NULL); 2010d0c20769SMatthias Ringwald 2011d210d9c4SMatthias Ringwald if (call_setup_in_progress){ 201260ebb071SMilanka Ringwald log_info("AG: Call on Hold, Accept new call"); 2013d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 2014a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 2015d210d9c4SMatthias Ringwald } else { 201660ebb071SMilanka Ringwald log_info("AG: Swap calls"); 2017d210d9c4SMatthias Ringwald } 2018d0c20769SMatthias Ringwald 2019d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 2020d0c20769SMatthias Ringwald // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED); 2021a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 2022a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 2023d210d9c4SMatthias Ringwald break; 2024d210d9c4SMatthias Ringwald } 2025d0c20769SMatthias Ringwald 2026d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_ADD_HELD_CALL: 2027d210d9c4SMatthias Ringwald // Adds a held call to the conversation. 2028d0c20769SMatthias Ringwald if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){ 202960ebb071SMilanka Ringwald log_info("AG: Join 3-way-call"); 2030f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_ADD_HELD_CALL, 0, 0, NULL); 2031d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 2032a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 2033ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL); 2034d210d9c4SMatthias Ringwald } 2035a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 2036d210d9c4SMatthias Ringwald break; 2037d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS: 2038d210d9c4SMatthias Ringwald // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer) 2039f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, 0, 0, NULL); 204060ebb071SMilanka Ringwald log_info("AG: Transfer call -> Connect two calls and disconnect"); 2041d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 2042d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 2043a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 2044a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 2045a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 2046d210d9c4SMatthias Ringwald break; 2047ce263fc8SMatthias Ringwald 20483deb3ec6SMatthias Ringwald default: 20493deb3ec6SMatthias Ringwald break; 20503deb3ec6SMatthias Ringwald } 2051d210d9c4SMatthias Ringwald 2052d0c20769SMatthias Ringwald 20533deb3ec6SMatthias Ringwald } 20543deb3ec6SMatthias Ringwald 20559cae807eSMatthias Ringwald 2056a0ffb263SMatthias Ringwald static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){ 20579cae807eSMatthias Ringwald hfp_gsm_call_t * active_call = hfp_gsm_call(call_index); 20589cae807eSMatthias Ringwald if (!active_call) return; 20599cae807eSMatthias Ringwald 20609cae807eSMatthias Ringwald int idx = active_call->index; 20619cae807eSMatthias Ringwald hfp_enhanced_call_dir_t dir = active_call->direction; 20629cae807eSMatthias Ringwald hfp_enhanced_call_status_t status = active_call->enhanced_status; 20639cae807eSMatthias Ringwald hfp_enhanced_call_mode_t mode = active_call->mode; 20649cae807eSMatthias Ringwald hfp_enhanced_call_mpty_t mpty = active_call->mpty; 20659cae807eSMatthias Ringwald uint8_t type = active_call->clip_type; 20669cae807eSMatthias Ringwald char * number = active_call->clip_number; 20679cae807eSMatthias Ringwald 20689cae807eSMatthias Ringwald char buffer[100]; 20699cae807eSMatthias Ringwald // TODO: check length of a buffer, to fit the MTU 20709cae807eSMatthias Ringwald int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty); 20719cae807eSMatthias Ringwald if (number){ 20721cc1d9e9SMilanka Ringwald offset += snprintf(buffer+offset, sizeof(buffer)-offset-3, ", \"%s\",%u", number, type); 20739cae807eSMatthias Ringwald } 20749cae807eSMatthias Ringwald snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 207560ebb071SMilanka 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, 20769cae807eSMatthias Ringwald mode, mpty, type, number); 2077a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 20789cae807eSMatthias Ringwald } 20799cae807eSMatthias Ringwald 2080febc14f5SMatthias Ringwald // sends pending command, returns if command was sent 2081febc14f5SMatthias Ringwald static int hfp_ag_send_commands(hfp_connection_t *hfp_connection){ 2082a0ffb263SMatthias Ringwald if (hfp_connection->send_status_of_current_calls){ 2083a0ffb263SMatthias Ringwald if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){ 2084a0ffb263SMatthias Ringwald hfp_connection->next_call_index++; 2085a0ffb263SMatthias Ringwald hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index); 2086febc14f5SMatthias Ringwald return 1; 20879cae807eSMatthias Ringwald } else { 2088febc14f5SMatthias 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 2089a0ffb263SMatthias Ringwald hfp_connection->next_call_index = 0; 2090e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2091a0ffb263SMatthias Ringwald hfp_connection->send_status_of_current_calls = 0; 20929cae807eSMatthias Ringwald } 2093ce263fc8SMatthias Ringwald } 2094ce263fc8SMatthias Ringwald 2095a0ffb263SMatthias Ringwald if (hfp_connection->ag_notify_incoming_call_waiting){ 2096a0ffb263SMatthias Ringwald hfp_connection->ag_notify_incoming_call_waiting = 0; 2097a0ffb263SMatthias Ringwald hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid); 2098febc14f5SMatthias Ringwald return 1; 2099aa4dd815SMatthias Ringwald } 2100aa4dd815SMatthias Ringwald 21015e87d1ceSMatthias Ringwald if (hfp_connection->send_error > 0){ 21025e87d1ceSMatthias Ringwald hfp_connection->send_error--; 2103a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2104485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 2105febc14f5SMatthias Ringwald return 1; 2106aa4dd815SMatthias Ringwald } 2107aa4dd815SMatthias Ringwald 2108ce263fc8SMatthias Ringwald // note: before update AG indicators and ok_pending 2109a0ffb263SMatthias Ringwald if (hfp_connection->send_response_and_hold_status){ 2110a0ffb263SMatthias Ringwald int status = hfp_connection->send_response_and_hold_status - 1; 2111a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = 0; 2112485ac19eSMilanka Ringwald hfp_ag_send_set_response_and_hold(hfp_connection->rfcomm_cid, status); 2113febc14f5SMatthias Ringwald return 1; 2114ce263fc8SMatthias Ringwald } 2115ce263fc8SMatthias Ringwald 21165be6b47aSMatthias Ringwald // note: before ok_pending and send_error to allow for unsolicited result on custom command 21175be6b47aSMatthias Ringwald if (hfp_connection->send_custom_message != NULL){ 21185be6b47aSMatthias Ringwald const char * message = hfp_connection->send_custom_message; 21195be6b47aSMatthias Ringwald hfp_connection->send_custom_message = NULL; 21205be6b47aSMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, message); 21217f8f1191SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_CUSTOM_AT_MESSAGE_SENT, ERROR_CODE_SUCCESS); 21225be6b47aSMatthias Ringwald return 1; 21235be6b47aSMatthias Ringwald } 21245be6b47aSMatthias Ringwald 21255e87d1ceSMatthias Ringwald if (hfp_connection->ok_pending > 0){ 21265e87d1ceSMatthias Ringwald hfp_connection->ok_pending--; 2127a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2128485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 2129febc14f5SMatthias Ringwald return 1; 2130ce263fc8SMatthias Ringwald } 2131ce263fc8SMatthias Ringwald 213269640b04SMatthias Ringwald // update AG indicators only if enabled by AT+CMER=3,0,0,1 213369640b04SMatthias Ringwald if ((hfp_connection->enable_status_update_for_ag_indicators == 1) && (hfp_connection->ag_indicators_status_update_bitmap != 0)){ 213469640b04SMatthias Ringwald uint16_t i; 2135a0ffb263SMatthias Ringwald for (i=0;i<hfp_connection->ag_indicators_nr;i++){ 2136a0ffb263SMatthias Ringwald if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){ 2137a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0); 2138485ac19eSMilanka Ringwald hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]); 2139febc14f5SMatthias Ringwald return 1; 2140aa4dd815SMatthias Ringwald } 2141aa4dd815SMatthias Ringwald } 2142aa4dd815SMatthias Ringwald } 2143aa4dd815SMatthias Ringwald 2144245852b7SMilanka Ringwald if (hfp_connection->ag_send_no_carrier){ 2145245852b7SMilanka Ringwald hfp_connection->ag_send_no_carrier = false; 2146245852b7SMilanka Ringwald hfp_ag_send_no_carrier(hfp_connection->rfcomm_cid); 2147245852b7SMilanka Ringwald return 1; 2148245852b7SMilanka Ringwald } 2149245852b7SMilanka Ringwald 2150a0ffb263SMatthias Ringwald if (hfp_connection->send_phone_number_for_voice_tag){ 2151a0ffb263SMatthias Ringwald hfp_connection->send_phone_number_for_voice_tag = 0; 2152a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2153e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2154a0ffb263SMatthias Ringwald hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid); 2155febc14f5SMatthias Ringwald return 1; 2156aa4dd815SMatthias Ringwald } 2157aa4dd815SMatthias Ringwald 2158a0ffb263SMatthias Ringwald if (hfp_connection->send_subscriber_number){ 2159aeb0f0feSMatthias Ringwald if (hfp_connection->next_subscriber_number_to_send < hfp_ag_subscriber_numbers_count){ 2160aeb0f0feSMatthias Ringwald hfp_phone_number_t phone = hfp_ag_subscriber_numbers[hfp_connection->next_subscriber_number_to_send++]; 2161a0ffb263SMatthias Ringwald hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number); 2162ce263fc8SMatthias Ringwald } else { 2163a0ffb263SMatthias Ringwald hfp_connection->send_subscriber_number = 0; 2164a0ffb263SMatthias Ringwald hfp_connection->next_subscriber_number_to_send = 0; 2165485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 2166ce263fc8SMatthias Ringwald } 2167a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2168febc14f5SMatthias Ringwald return 1; 2169ce263fc8SMatthias Ringwald } 2170ce263fc8SMatthias Ringwald 2171a0ffb263SMatthias Ringwald if (hfp_connection->send_microphone_gain){ 2172a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 0; 2173a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2174485ac19eSMilanka Ringwald hfp_ag_send_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 2175febc14f5SMatthias Ringwald return 1; 2176aa4dd815SMatthias Ringwald } 2177aa4dd815SMatthias Ringwald 2178a0ffb263SMatthias Ringwald if (hfp_connection->send_speaker_gain){ 2179a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 0; 2180a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2181485ac19eSMilanka Ringwald hfp_ag_send_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 2182febc14f5SMatthias Ringwald return 1; 21833deb3ec6SMatthias Ringwald } 21843deb3ec6SMatthias Ringwald 2185a0ffb263SMatthias Ringwald if (hfp_connection->send_ag_status_indicators){ 2186a0ffb263SMatthias Ringwald hfp_connection->send_ag_status_indicators = 0; 2187485ac19eSMilanka Ringwald hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid); 2188febc14f5SMatthias Ringwald return 1; 2189febc14f5SMatthias Ringwald } 2190febc14f5SMatthias Ringwald 2191*10ae9dfcSMatthias Ringwald if (hfp_connection->extended_audio_gateway_error){ 2192*10ae9dfcSMatthias Ringwald uint8_t extended_audio_gateway_error = hfp_connection->extended_audio_gateway_error; 2193*10ae9dfcSMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 2194*10ae9dfcSMatthias Ringwald hfp_ag_send_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, extended_audio_gateway_error); 2195*10ae9dfcSMatthias Ringwald 2196*10ae9dfcSMatthias Ringwald } 2197febc14f5SMatthias Ringwald return 0; 2198febc14f5SMatthias Ringwald } 2199febc14f5SMatthias Ringwald 2200c1eef992SMatthias Ringwald // sends pending command, returns if command was sent 2201c1eef992SMatthias Ringwald static int hfp_ag_run_ring_and_clip(hfp_connection_t *hfp_connection){ 2202c1eef992SMatthias Ringwald // delay RING/CLIP until audio connection has been established for incoming calls 2203c1eef992SMatthias Ringwald // hfp_ag_hf_trigger_ring_and_clip is called in call_setup_state_machine 2204c1eef992SMatthias Ringwald if (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING){ 2205c1eef992SMatthias Ringwald if (hfp_connection->ag_ring){ 2206c1eef992SMatthias Ringwald hfp_connection->ag_ring = 0; 2207c1eef992SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2208c1eef992SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING); 2209c1eef992SMatthias Ringwald hfp_ag_send_ring(hfp_connection->rfcomm_cid); 2210c1eef992SMatthias Ringwald return 1; 2211c1eef992SMatthias Ringwald } 2212c1eef992SMatthias Ringwald 2213c1eef992SMatthias Ringwald if (hfp_connection->ag_send_clip){ 2214c1eef992SMatthias Ringwald hfp_connection->ag_send_clip = 0; 2215c1eef992SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2216c1eef992SMatthias Ringwald hfp_ag_send_clip(hfp_connection->rfcomm_cid); 2217c1eef992SMatthias Ringwald return 1; 2218c1eef992SMatthias Ringwald } 2219c1eef992SMatthias Ringwald } 2220c1eef992SMatthias Ringwald return 0; 2221c1eef992SMatthias Ringwald } 2222c1eef992SMatthias Ringwald 2223febc14f5SMatthias Ringwald static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection){ 2224febc14f5SMatthias Ringwald 222576cc1527SMatthias Ringwald btstack_assert(hfp_connection != NULL); 222676cc1527SMatthias Ringwald btstack_assert(hfp_connection->local_role == HFP_ROLE_AG); 2227febc14f5SMatthias Ringwald 222876cc1527SMatthias Ringwald // during SDP query, RFCOMM CID is not set 222976cc1527SMatthias Ringwald if (hfp_connection->rfcomm_cid == 0) return; 2230ce263fc8SMatthias Ringwald 223184fb9ac1SMilanka Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->emit_vra_enabled_after_audio_established){ 223284fb9ac1SMilanka Ringwald hfp_connection->emit_vra_enabled_after_audio_established = false; 223384fb9ac1SMilanka Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS); 223484fb9ac1SMilanka Ringwald } 223584fb9ac1SMilanka Ringwald 2236f9f3ba28SMilanka Ringwald if ((hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) && hfp_connection->release_audio_connection){ 2237f9f3ba28SMilanka Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 2238f9f3ba28SMilanka Ringwald hfp_connection->release_audio_connection = 0; 2239f9f3ba28SMilanka Ringwald gap_disconnect(hfp_connection->sco_handle); 2240f9f3ba28SMilanka Ringwald return; 2241f9f3ba28SMilanka Ringwald } 2242f9f3ba28SMilanka Ringwald 2243b7802cc8SMatthias Ringwald // configure NBS/WBS if needed using vendor-specific HCI commands 2244b7802cc8SMatthias Ringwald if (hci_can_send_command_packet_now()) { 22453721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP 22463721a235SMatthias Ringwald // WBS Disassociate 22473721a235SMatthias Ringwald if (hfp_connection->cc256x_send_wbs_disassociate){ 22483721a235SMatthias Ringwald hfp_connection->cc256x_send_wbs_disassociate = false; 22493721a235SMatthias Ringwald hci_send_cmd(&hci_ti_wbs_disassociate); 22503721a235SMatthias Ringwald return; 22513721a235SMatthias Ringwald } 22523721a235SMatthias Ringwald // Write Codec Config 22533721a235SMatthias Ringwald if (hfp_connection->cc256x_send_write_codec_config){ 22543721a235SMatthias Ringwald hfp_connection->cc256x_send_write_codec_config = false; 22553721a235SMatthias Ringwald hfp_cc256x_write_codec_config(hfp_connection); 22563721a235SMatthias Ringwald return; 22573721a235SMatthias Ringwald } 22583721a235SMatthias Ringwald // WBS Associate 22593721a235SMatthias Ringwald if (hfp_connection->cc256x_send_wbs_associate){ 22603721a235SMatthias Ringwald hfp_connection->cc256x_send_wbs_associate = false; 22613721a235SMatthias Ringwald hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle); 22623721a235SMatthias Ringwald return; 22633721a235SMatthias Ringwald } 22643721a235SMatthias Ringwald #endif 2265689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 2266689d4323SMatthias Ringwald // Enable WBS 2267689d4323SMatthias Ringwald if (hfp_connection->bcm_send_enable_wbs){ 2268689d4323SMatthias Ringwald hfp_connection->bcm_send_enable_wbs = false; 2269689d4323SMatthias Ringwald hci_send_cmd(&hci_bcm_enable_wbs, 1, 2); 2270689d4323SMatthias Ringwald return; 2271689d4323SMatthias Ringwald } 2272689d4323SMatthias Ringwald // Write I2S/PCM params 2273689d4323SMatthias Ringwald if (hfp_connection->bcm_send_write_i2spcm_interface_param){ 2274689d4323SMatthias Ringwald hfp_connection->bcm_send_write_i2spcm_interface_param = false; 2275689d4323SMatthias Ringwald hfp_bcm_write_i2spcm_interface_param(hfp_connection); 2276689d4323SMatthias Ringwald return; 2277689d4323SMatthias Ringwald } 2278689d4323SMatthias Ringwald // Disable WBS 2279689d4323SMatthias Ringwald if (hfp_connection->bcm_send_disable_wbs){ 2280689d4323SMatthias Ringwald hfp_connection->bcm_send_disable_wbs = false; 2281689d4323SMatthias Ringwald hci_send_cmd(&hci_bcm_enable_wbs, 0, 2); 2282689d4323SMatthias Ringwald return; 2283689d4323SMatthias Ringwald } 2284689d4323SMatthias Ringwald #endif 22852b5f92fdSMatthias Ringwald #ifdef ENABLE_RTK_PCM_WBS 22862b5f92fdSMatthias Ringwald // Configure CVSD vs. mSBC 22872b5f92fdSMatthias Ringwald if (hfp_connection->rtk_send_sco_config){ 22882b5f92fdSMatthias Ringwald hfp_connection->rtk_send_sco_config = false; 22892b5f92fdSMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 22902b5f92fdSMatthias Ringwald log_info("RTK SCO: 16k + mSBC"); 22912b5f92fdSMatthias Ringwald hci_send_cmd(&hci_rtk_configure_sco_routing, 0x81, 0x90, 0x00, 0x00, 0x1a, 0x0c, 0x00, 0x00, 0x41); 22922b5f92fdSMatthias Ringwald } else { 22932b5f92fdSMatthias Ringwald log_info("RTK SCO: 16k + CVSD"); 22942b5f92fdSMatthias Ringwald hci_send_cmd(&hci_rtk_configure_sco_routing, 0x81, 0x90, 0x00, 0x00, 0x1a, 0x0c, 0x0c, 0x00, 0x01); 22952b5f92fdSMatthias Ringwald } 22962b5f92fdSMatthias Ringwald return; 22972b5f92fdSMatthias Ringwald } 22982b5f92fdSMatthias Ringwald #endif 22995fd6f360SMatthias Ringwald #ifdef ENABLE_NXP_PCM_WBS 23005fd6f360SMatthias Ringwald if (hfp_connection->nxp_start_audio_handle != HCI_CON_HANDLE_INVALID){ 23015fd6f360SMatthias Ringwald hci_con_handle_t sco_handle = hfp_connection->nxp_start_audio_handle; 23025fd6f360SMatthias Ringwald hfp_connection->nxp_start_audio_handle = HCI_CON_HANDLE_INVALID; 23035fd6f360SMatthias Ringwald hci_send_cmd(&hci_nxp_host_pcm_i2s_audio_config, 0, 0, sco_handle, 0); 23045fd6f360SMatthias Ringwald return; 23055fd6f360SMatthias Ringwald } 23065fd6f360SMatthias Ringwald if (hfp_connection->nxp_stop_audio_handle != HCI_CON_HANDLE_INVALID){ 23075fd6f360SMatthias Ringwald hci_con_handle_t sco_handle = hfp_connection->nxp_stop_audio_handle; 23085fd6f360SMatthias Ringwald hfp_connection->nxp_stop_audio_handle = HCI_CON_HANDLE_INVALID; 23095fd6f360SMatthias Ringwald hci_send_cmd(&hci_nxp_host_pcm_i2s_audio_config, 1, 0, sco_handle, 0); 23105fd6f360SMatthias Ringwald return; 23115fd6f360SMatthias Ringwald } 23125fd6f360SMatthias Ringwald #endif 2313b7802cc8SMatthias Ringwald } 2314b7802cc8SMatthias Ringwald 231548e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS) 231648e6eeeeSMatthias Ringwald if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){ 231748e6eeeeSMatthias Ringwald hfp_finalize_connection_context(hfp_connection); 231848e6eeeeSMatthias Ringwald return; 231948e6eeeeSMatthias Ringwald } 232048e6eeeeSMatthias Ringwald #endif 23213721a235SMatthias Ringwald 2322febc14f5SMatthias Ringwald if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) { 2323febc14f5SMatthias Ringwald log_info("hfp_ag_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid); 2324febc14f5SMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 2325febc14f5SMatthias Ringwald return; 2326febc14f5SMatthias Ringwald } 2327febc14f5SMatthias Ringwald 2328febc14f5SMatthias Ringwald int cmd_sent = hfp_ag_send_commands(hfp_connection); 2329febc14f5SMatthias Ringwald 2330febc14f5SMatthias Ringwald if (!cmd_sent){ 2331febc14f5SMatthias Ringwald cmd_sent = hfp_ag_run_for_context_service_level_connection(hfp_connection); 2332febc14f5SMatthias Ringwald } 2333febc14f5SMatthias Ringwald 2334c04cf7ffSMilanka Ringwald if (!cmd_sent){ 2335c04cf7ffSMilanka Ringwald cmd_sent = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection); 23363deb3ec6SMatthias Ringwald } 23373deb3ec6SMatthias Ringwald 2338c04cf7ffSMilanka Ringwald if (!cmd_sent){ 2339c04cf7ffSMilanka Ringwald cmd_sent = call_setup_state_machine(hfp_connection); 2340aa4dd815SMatthias Ringwald } 2341aa4dd815SMatthias Ringwald 2342b956fff3SMatthias Ringwald if (!cmd_sent){ 2343b956fff3SMatthias Ringwald cmd_sent = hfp_ag_run_for_audio_connection(hfp_connection); 2344b956fff3SMatthias Ringwald } 2345b956fff3SMatthias Ringwald 2346c95b5b3cSMilanka Ringwald if (!cmd_sent){ 2347c1eef992SMatthias Ringwald cmd_sent = hfp_ag_run_ring_and_clip(hfp_connection); 2348c1eef992SMatthias Ringwald } 2349c1eef992SMatthias Ringwald 2350c1eef992SMatthias Ringwald if (!cmd_sent){ 2351c95b5b3cSMilanka Ringwald cmd_sent = hfp_ag_voice_recognition_state_machine(hfp_connection); 2352c95b5b3cSMilanka Ringwald } 2353b956fff3SMatthias Ringwald 2354d0a0eceeSMatthias Ringwald // disconnect 2355d0a0eceeSMatthias Ringwald if (!cmd_sent && (hfp_connection->command == HFP_CMD_NONE) && (hfp_connection->state == HFP_W2_DISCONNECT_RFCOMM)){ 2356d0a0eceeSMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 2357d0a0eceeSMatthias Ringwald rfcomm_disconnect(hfp_connection->rfcomm_cid); 2358d0a0eceeSMatthias Ringwald } 2359d0a0eceeSMatthias Ringwald 2360c04cf7ffSMilanka Ringwald if (cmd_sent){ 2361a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2362473ac565SMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 2363473ac565SMatthias Ringwald } 23643deb3ec6SMatthias Ringwald } 2365d68dcce1SMatthias Ringwald 2366e0d09929SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){ 2367c1ab6cc1SMatthias Ringwald return (byte == '\n') || (byte == '\r'); 2368e0d09929SMatthias Ringwald } 2369e0d09929SMatthias Ringwald 2370fdf18f86SMilanka Ringwald static void hfp_ag_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){ 23718a46ec40SMatthias Ringwald // assertion: size >= 1 as rfcomm.c does not deliver empty packets 2372fdf18f86SMilanka Ringwald if (size < 1) return; 2373347d46c8SMilanka Ringwald uint8_t status = ERROR_CODE_SUCCESS; 23741e35c04dSMatthias Ringwald 2375186dd3d2SMatthias Ringwald hfp_log_rfcomm_message("HFP_AG_RX", packet, size); 2376e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES 2377e43d1938SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet); 2378e43d1938SMatthias Ringwald #endif 23791e35c04dSMatthias Ringwald 23808a46ec40SMatthias Ringwald // process messages byte-wise 23813deb3ec6SMatthias Ringwald int pos; 23823deb3ec6SMatthias Ringwald for (pos = 0; pos < size ; pos++){ 2383a0ffb263SMatthias Ringwald hfp_parse(hfp_connection, packet[pos], 0); 2384186dd3d2SMatthias Ringwald 2385e0d09929SMatthias Ringwald // parse until end of line 2386e0d09929SMatthias Ringwald if (!hfp_parser_is_end_of_line(packet[pos])) continue; 2387e0d09929SMatthias Ringwald 2388186dd3d2SMatthias Ringwald hfp_generic_status_indicator_t * indicator; 2389a0ffb263SMatthias Ringwald switch(hfp_connection->command){ 239045796ff1SMilanka Ringwald case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION: 2391e7c46708SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2392e7c46708SMatthias Ringwald hfp_connection->ag_vra_requested_by_hf = true; 239345796ff1SMilanka Ringwald break; 2394ce263fc8SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_QUERY: 2395fd66594dSMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2396ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_active){ 2397a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1; 2398ce263fc8SMatthias Ringwald } 2399e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2400ce263fc8SMatthias Ringwald break; 2401ce263fc8SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_COMMAND: 2402fd66594dSMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 24030222a807SMatthias Ringwald switch(hfp_connection->ag_response_and_hold_action){ 2404ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD: 2405a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection); 2406ce263fc8SMatthias Ringwald break; 2407ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED: 2408a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection); 2409ce263fc8SMatthias Ringwald break; 2410ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED: 2411a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection); 2412ce263fc8SMatthias Ringwald break; 2413ce263fc8SMatthias Ringwald default: 2414ce263fc8SMatthias Ringwald break; 2415ce263fc8SMatthias Ringwald } 2416e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2417ce263fc8SMatthias Ringwald break; 2418ce263fc8SMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 2419a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2420347d46c8SMilanka Ringwald 2421aeb0f0feSMatthias Ringwald if (hfp_connection->parser_indicator_index < hfp_ag_generic_status_indicators_nr){ 2422aeb0f0feSMatthias Ringwald indicator = &hfp_ag_generic_status_indicators[hfp_connection->parser_indicator_index]; 2423ce263fc8SMatthias Ringwald switch (indicator->uuid){ 2424347d46c8SMilanka Ringwald case HFP_HF_INDICATOR_UUID_ENHANCED_SAFETY: 24250222a807SMatthias Ringwald if (hfp_connection->parser_indicator_value > 1) { 2426e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 2427ce263fc8SMatthias Ringwald break; 2428ce263fc8SMatthias Ringwald } 2429e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2430585db78dSMilanka Ringwald hfp_ag_emit_hf_indicator_value(hfp_connection, indicator->uuid, hfp_connection->parser_indicator_value); 2431347d46c8SMilanka Ringwald break; 2432347d46c8SMilanka Ringwald case HFP_HF_INDICATOR_UUID_BATTERY_LEVEL: 2433347d46c8SMilanka Ringwald if (hfp_connection->parser_indicator_value > 100){ 2434e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 2435347d46c8SMilanka Ringwald break; 2436347d46c8SMilanka Ringwald } 2437e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2438585db78dSMilanka Ringwald hfp_ag_emit_hf_indicator_value(hfp_connection, indicator->uuid, hfp_connection->parser_indicator_value); 2439347d46c8SMilanka Ringwald break; 2440347d46c8SMilanka Ringwald default: 2441e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 2442347d46c8SMilanka Ringwald break; 2443347d46c8SMilanka Ringwald } 2444347d46c8SMilanka Ringwald } else { 2445e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 2446347d46c8SMilanka Ringwald } 2447ce263fc8SMatthias Ringwald break; 2448ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 2449ce263fc8SMatthias Ringwald // expected by SLC state machine 2450a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break; 2451a0ffb263SMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0; 2452a0ffb263SMatthias Ringwald hfp_connection->send_ag_status_indicators = 1; 2453ce263fc8SMatthias Ringwald break; 2454ce263fc8SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 2455a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2456a0ffb263SMatthias Ringwald hfp_connection->next_call_index = 0; 2457a0ffb263SMatthias Ringwald hfp_connection->send_status_of_current_calls = 1; 2458ce263fc8SMatthias Ringwald break; 2459ce263fc8SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 2460fd66594dSMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2461aeb0f0feSMatthias Ringwald if (hfp_ag_subscriber_numbers_count == 0){ 2462485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 2463ce263fc8SMatthias Ringwald break; 2464ce263fc8SMatthias Ringwald } 2465a0ffb263SMatthias Ringwald hfp_connection->next_subscriber_number_to_send = 0; 2466a0ffb263SMatthias Ringwald hfp_connection->send_subscriber_number = 1; 2467ce263fc8SMatthias Ringwald break; 2468c1797c7dSMatthias Ringwald case HFP_CMD_TRANSMIT_DTMF_CODES: 24690222a807SMatthias Ringwald { 2470a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 24710222a807SMatthias Ringwald char buffer[2]; 24720222a807SMatthias Ringwald buffer[0] = (char) hfp_connection->ag_dtmf_code; 24730222a807SMatthias Ringwald buffer[1] = 0; 24740222a807SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, buffer); 2475c1797c7dSMatthias Ringwald break; 24760222a807SMatthias Ringwald } 2477aa4dd815SMatthias Ringwald case HFP_CMD_HF_REQUEST_PHONE_NUMBER: 2478a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2479ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG); 2480aa4dd815SMatthias Ringwald break; 2481aa4dd815SMatthias Ringwald case HFP_CMD_TURN_OFF_EC_AND_NR: 2482a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2483347d46c8SMilanka Ringwald 2484aeb0f0feSMatthias Ringwald if (get_bit(hfp_ag_supported_features, HFP_AGSF_EC_NR_FUNCTION)){ 2485e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2486347d46c8SMilanka Ringwald status = ERROR_CODE_SUCCESS; 2487aa4dd815SMatthias Ringwald } else { 2488e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 2489347d46c8SMilanka Ringwald status = ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 2490aa4dd815SMatthias Ringwald } 2491347d46c8SMilanka Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE, status); 2492aa4dd815SMatthias Ringwald break; 2493aa4dd815SMatthias Ringwald case HFP_CMD_CALL_ANSWERED: 2494a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 249560ebb071SMilanka Ringwald log_info("HFP: ATA"); 2496a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection); 2497aa4dd815SMatthias Ringwald break; 2498aa4dd815SMatthias Ringwald case HFP_CMD_HANG_UP_CALL: 2499a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2500e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2501a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection); 2502aa4dd815SMatthias Ringwald break; 2503aa4dd815SMatthias Ringwald case HFP_CMD_CALL_HOLD: { 2504a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2505e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2506d0c20769SMatthias Ringwald 25070222a807SMatthias Ringwald switch (hfp_connection->ag_call_hold_action){ 25080222a807SMatthias Ringwald case 0: 2509d0c20769SMatthias Ringwald // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call. 2510a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection); 2511aa4dd815SMatthias Ringwald break; 25120222a807SMatthias Ringwald case 1: 2513d0c20769SMatthias Ringwald // Releases all active calls (if any exist) and accepts the other (held or waiting) call. 2514d0c20769SMatthias Ringwald // Where both a held and a waiting call exist, the above procedures shall apply to the 2515d0c20769SMatthias Ringwald // waiting call (i.e., not to the held call) in conflicting situation. 2516a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection); 2517aa4dd815SMatthias Ringwald break; 25180222a807SMatthias Ringwald case 2: 2519d0c20769SMatthias Ringwald // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call. 2520d0c20769SMatthias Ringwald // Where both a held and a waiting call exist, the above procedures shall apply to the 2521d0c20769SMatthias Ringwald // waiting call (i.e., not to the held call) in conflicting situation. 2522a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection); 2523aa4dd815SMatthias Ringwald break; 25240222a807SMatthias Ringwald case 3: 2525d0c20769SMatthias Ringwald // Adds a held call to the conversation. 2526a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection); 2527aa4dd815SMatthias Ringwald break; 25280222a807SMatthias Ringwald case 4: 2529d0c20769SMatthias Ringwald // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer). 2530a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection); 2531aa4dd815SMatthias Ringwald break; 2532aa4dd815SMatthias Ringwald default: 2533aa4dd815SMatthias Ringwald break; 2534aa4dd815SMatthias Ringwald } 25350222a807SMatthias Ringwald hfp_connection->call_index = 0; 253635e92150SMatthias Ringwald break; 2537aa4dd815SMatthias Ringwald } 2538aa4dd815SMatthias Ringwald case HFP_CMD_CALL_PHONE_NUMBER: 2539a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 25409ff73f41SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED_BY_HF, hfp_connection); 2541aa4dd815SMatthias Ringwald break; 2542aa4dd815SMatthias Ringwald case HFP_CMD_REDIAL_LAST_NUMBER: 2543a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2544a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection); 2545aa4dd815SMatthias Ringwald break; 2546aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_CLIP: 2547a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2548a0ffb263SMatthias Ringwald log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled); 2549e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2550aa4dd815SMatthias Ringwald break; 2551aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION: 2552a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2553a0ffb263SMatthias Ringwald log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled); 2554e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 2555aa4dd815SMatthias Ringwald break; 2556aa4dd815SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 2557a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2558e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 255960ebb071SMilanka Ringwald log_info("HF speaker gain = %u", hfp_connection->speaker_gain); 25603db60f78SBjoern Hartmann hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_connection->speaker_gain); 2561aa4dd815SMatthias Ringwald break; 2562aa4dd815SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 2563a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2564e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection); 256560ebb071SMilanka Ringwald log_info("HF microphone gain = %u", hfp_connection->microphone_gain); 25662abbd98dSMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_connection->microphone_gain); 2567aa4dd815SMatthias Ringwald break; 2568471dea41SMatthias Ringwald case HFP_CMD_CUSTOM_MESSAGE: 2569471dea41SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2570471dea41SMatthias Ringwald hfp_parser_reset_line_buffer(hfp_connection); 25716d9a41f9SMatthias Ringwald log_info("Custom AT Command ID 0x%04x", hfp_connection->custom_at_command_id); 2572471dea41SMatthias Ringwald hfp_ag_emit_custom_command_event(hfp_connection); 2573471dea41SMatthias Ringwald break; 25743f38f554SMatthias Ringwald case HFP_CMD_UNKNOWN: 25753f38f554SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2576e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection); 25773f38f554SMatthias Ringwald break; 2578aa4dd815SMatthias Ringwald default: 2579aa4dd815SMatthias Ringwald break; 25803deb3ec6SMatthias Ringwald } 25813deb3ec6SMatthias Ringwald } 25820cef86faSMatthias Ringwald } 25833deb3ec6SMatthias Ringwald 25841c6a0fc0SMatthias Ringwald static void hfp_ag_run(void){ 2585d63c37a1SMatthias Ringwald btstack_linked_list_iterator_t it; 2586d63c37a1SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2587d63c37a1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2588d63c37a1SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 258922387625SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2590f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 25913deb3ec6SMatthias Ringwald } 25923deb3ec6SMatthias Ringwald } 25933deb3ec6SMatthias Ringwald 25941c6a0fc0SMatthias Ringwald static void hfp_ag_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2595fdf18f86SMilanka Ringwald hfp_connection_t * hfp_connection = NULL; 25963deb3ec6SMatthias Ringwald switch (packet_type){ 25973deb3ec6SMatthias Ringwald case RFCOMM_DATA_PACKET: 2598fdf18f86SMilanka Ringwald hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 2599fd075f20SMilanka Ringwald btstack_assert(hfp_connection != NULL); 2600fdf18f86SMilanka Ringwald 2601fdf18f86SMilanka Ringwald hfp_ag_handle_rfcomm_data(hfp_connection, packet, size); 2602fdf18f86SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 2603fdf18f86SMilanka Ringwald return; 26043deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 2605e30a6a47SMatthias Ringwald if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 2606e30a6a47SMatthias Ringwald uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 2607fd075f20SMilanka Ringwald hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid); 2608fd075f20SMilanka Ringwald btstack_assert(hfp_connection != NULL); 2609fd075f20SMilanka Ringwald 2610fd075f20SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 2611e30a6a47SMatthias Ringwald return; 2612e30a6a47SMatthias Ringwald } 261327950165SMatthias Ringwald hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_AG); 2614aa4dd815SMatthias Ringwald break; 26153deb3ec6SMatthias Ringwald default: 26163deb3ec6SMatthias Ringwald break; 26173deb3ec6SMatthias Ringwald } 26183deb3ec6SMatthias Ringwald 26191c6a0fc0SMatthias Ringwald hfp_ag_run(); 26203deb3ec6SMatthias Ringwald } 26213deb3ec6SMatthias Ringwald 26221c6a0fc0SMatthias Ringwald static void hfp_ag_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2623405014fbSMatthias Ringwald hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_AG); 26241c6a0fc0SMatthias Ringwald hfp_ag_run(); 2625405014fbSMatthias Ringwald } 2626405014fbSMatthias Ringwald 2627aa10b9cbSMatthias Ringwald void hfp_ag_init_codecs(uint8_t codecs_nr, const uint8_t * codecs){ 2628aa10b9cbSMatthias Ringwald btstack_assert(codecs_nr <= HFP_MAX_NUM_CODECS); 2629aa10b9cbSMatthias Ringwald 2630aeb0f0feSMatthias Ringwald hfp_ag_codecs_nr = codecs_nr; 2631aa10b9cbSMatthias Ringwald uint8_t i; 26323deb3ec6SMatthias Ringwald for (i=0; i < codecs_nr; i++){ 2633aeb0f0feSMatthias Ringwald hfp_ag_codecs[i] = codecs[i]; 26343deb3ec6SMatthias Ringwald } 2635a0ffb263SMatthias Ringwald } 26363deb3ec6SMatthias Ringwald 2637a0ffb263SMatthias Ringwald void hfp_ag_init_supported_features(uint32_t supported_features){ 2638aeb0f0feSMatthias Ringwald hfp_ag_supported_features = supported_features; 263940a8ee13SMatthias Ringwald hfp_ag_in_band_ring_tone_active = has_in_band_ring_tone(); 2640a0ffb263SMatthias Ringwald } 26413deb3ec6SMatthias Ringwald 26427ca89cabSMatthias Ringwald void hfp_ag_init_ag_indicators(int ag_indicators_nr, const hfp_ag_indicator_t * ag_indicators){ 2643a0ffb263SMatthias Ringwald hfp_ag_indicators_nr = ag_indicators_nr; 26446535961aSMatthias Ringwald (void)memcpy(hfp_ag_indicators, ag_indicators, 26456535961aSMatthias Ringwald ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 2646a0ffb263SMatthias Ringwald } 26473deb3ec6SMatthias Ringwald 26487ca89cabSMatthias Ringwald void hfp_ag_init_hf_indicators(int hf_indicators_nr, const hfp_generic_status_indicator_t * hf_indicators){ 264925789943SMilanka Ringwald if (hf_indicators_nr > HFP_MAX_NUM_INDICATORS) return; 2650aeb0f0feSMatthias Ringwald hfp_ag_generic_status_indicators_nr = hf_indicators_nr; 2651aeb0f0feSMatthias Ringwald (void)memcpy(hfp_ag_generic_status_indicators, hf_indicators, 26526535961aSMatthias Ringwald hf_indicators_nr * sizeof(hfp_generic_status_indicator_t)); 2653a0ffb263SMatthias Ringwald } 2654a0ffb263SMatthias Ringwald 2655a0ffb263SMatthias Ringwald void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){ 26563deb3ec6SMatthias Ringwald hfp_ag_call_hold_services_nr = call_hold_services_nr; 26576535961aSMatthias Ringwald (void)memcpy(hfp_ag_call_hold_services, call_hold_services, 26586535961aSMatthias Ringwald call_hold_services_nr * sizeof(char *)); 2659a0ffb263SMatthias Ringwald } 2660a0ffb263SMatthias Ringwald 2661a0ffb263SMatthias Ringwald 2662ab2445a0SMatthias Ringwald void hfp_ag_init(uint8_t rfcomm_channel_nr){ 266327950165SMatthias Ringwald 2664520c92d5SMatthias Ringwald hfp_init(); 266520b2edb6SMatthias Ringwald hfp_ag_call_hold_services_nr = 0; 266627bb1817SMatthias Ringwald hfp_ag_response_and_hold_active = false; 266720b2edb6SMatthias Ringwald hfp_ag_indicators_nr = 0; 2668aeb0f0feSMatthias Ringwald hfp_ag_codecs_nr = 0; 2669aeb0f0feSMatthias Ringwald hfp_ag_supported_features = HFP_DEFAULT_AG_SUPPORTED_FEATURES; 267040a8ee13SMatthias Ringwald hfp_ag_in_band_ring_tone_active = has_in_band_ring_tone(); 2671aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers = NULL; 2672aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers_count = 0; 2673d63c37a1SMatthias Ringwald 26741c6a0fc0SMatthias Ringwald hfp_ag_hci_event_callback_registration.callback = &hfp_ag_hci_event_packet_handler; 26751c6a0fc0SMatthias Ringwald hci_add_event_handler(&hfp_ag_hci_event_callback_registration); 267627950165SMatthias Ringwald 26771c6a0fc0SMatthias Ringwald rfcomm_register_service(&hfp_ag_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 267827950165SMatthias Ringwald 267927950165SMatthias Ringwald // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us 26801c6a0fc0SMatthias Ringwald hfp_set_ag_rfcomm_packet_handler(&hfp_ag_rfcomm_packet_handler); 2681aa4dd815SMatthias Ringwald 2682d210d9c4SMatthias Ringwald hfp_gsm_init(); 26833deb3ec6SMatthias Ringwald } 26843deb3ec6SMatthias Ringwald 268520b2edb6SMatthias Ringwald void hfp_ag_deinit(void){ 268620b2edb6SMatthias Ringwald hfp_deinit(); 268720b2edb6SMatthias Ringwald hfp_gsm_deinit(); 2688aeb0f0feSMatthias Ringwald 2689aeb0f0feSMatthias Ringwald hfp_ag_callback = NULL; 2690aeb0f0feSMatthias Ringwald hfp_ag_supported_features = 0; 2691aeb0f0feSMatthias Ringwald hfp_ag_codecs_nr = 0; 2692aeb0f0feSMatthias Ringwald hfp_ag_indicators_nr = 0; 2693aeb0f0feSMatthias Ringwald hfp_ag_call_hold_services_nr = 0; 269420b2edb6SMatthias Ringwald (void) memset(&hfp_ag_call_hold_services, 0, sizeof(hfp_ag_call_hold_services)); 2695aeb0f0feSMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 269620b2edb6SMatthias Ringwald (void) memset(&hfp_ag_response_and_hold_state, 0, sizeof(hfp_response_and_hold_state_t)); 2697aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers = NULL; 2698aeb0f0feSMatthias Ringwald (void) memset(&hfp_ag_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t)); 269981e25d0aSMatthias Ringwald hfp_ag_custom_call_sm_handler = NULL; 270020b2edb6SMatthias Ringwald } 270120b2edb6SMatthias Ringwald 27024eb3f1d8SMilanka Ringwald uint8_t hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){ 27034eb3f1d8SMilanka Ringwald return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE, HFP_ROLE_AG); 27043deb3ec6SMatthias Ringwald } 27053deb3ec6SMatthias Ringwald 2706657bc59fSMilanka Ringwald uint8_t hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){ 27079c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2708a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2709657bc59fSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2710a33eb0c4SMilanka Ringwald } 2711c5fa3c94SMilanka Ringwald 27121ffa0dd9SMilanka Ringwald hfp_trigger_release_service_level_connection(hfp_connection); 2713f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2714657bc59fSMilanka Ringwald return ERROR_CODE_SUCCESS; 27153deb3ec6SMatthias Ringwald } 27163deb3ec6SMatthias Ringwald 2717c5fa3c94SMilanka Ringwald uint8_t hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, hfp_cme_error_t error){ 27189c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2719a0ffb263SMatthias Ringwald if (!hfp_connection){ 2720c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 27213deb3ec6SMatthias Ringwald } 27223deb3ec6SMatthias Ringwald 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