13deb3ec6SMatthias Ringwald /* 23deb3ec6SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 33deb3ec6SMatthias Ringwald * 43deb3ec6SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 53deb3ec6SMatthias Ringwald * modification, are permitted provided that the following conditions 63deb3ec6SMatthias Ringwald * are met: 73deb3ec6SMatthias Ringwald * 83deb3ec6SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 93deb3ec6SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 103deb3ec6SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 113deb3ec6SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 123deb3ec6SMatthias Ringwald * documentation and/or other materials provided with the distribution. 133deb3ec6SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 143deb3ec6SMatthias Ringwald * contributors may be used to endorse or promote products derived 153deb3ec6SMatthias Ringwald * from this software without specific prior written permission. 163deb3ec6SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 173deb3ec6SMatthias Ringwald * personal benefit and not for any commercial purpose or for 183deb3ec6SMatthias Ringwald * monetary gain. 193deb3ec6SMatthias Ringwald * 203deb3ec6SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 213deb3ec6SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 223deb3ec6SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 232fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 242fca4dadSMilanka Ringwald * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 253deb3ec6SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 263deb3ec6SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 273deb3ec6SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 283deb3ec6SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 293deb3ec6SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 303deb3ec6SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 313deb3ec6SMatthias Ringwald * SUCH DAMAGE. 323deb3ec6SMatthias Ringwald * 333deb3ec6SMatthias Ringwald * Please inquire about commercial licensing options at 343deb3ec6SMatthias Ringwald * [email protected] 353deb3ec6SMatthias Ringwald * 363deb3ec6SMatthias Ringwald */ 37ab2c6ae4SMatthias Ringwald 38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "hfp_ag.c" 393deb3ec6SMatthias Ringwald 403deb3ec6SMatthias Ringwald // ***************************************************************************** 413deb3ec6SMatthias Ringwald // 4266a048abSMatthias Ringwald // HFP Audio Gateway (AG) unit 433deb3ec6SMatthias Ringwald // 443deb3ec6SMatthias Ringwald // ***************************************************************************** 453deb3ec6SMatthias Ringwald 467907f069SMatthias Ringwald #include "btstack_config.h" 473deb3ec6SMatthias Ringwald 483deb3ec6SMatthias Ringwald #include <stdint.h> 493cfa4086SMatthias Ringwald #include <stdio.h> 503deb3ec6SMatthias Ringwald #include <string.h> 513deb3ec6SMatthias Ringwald 5256042629SMatthias Ringwald #include "hci_cmd.h" 5382636622SMatthias Ringwald #include "btstack_run_loop.h" 543deb3ec6SMatthias Ringwald 55235946f1SMatthias Ringwald #include "bluetooth_sdp.h" 563deb3ec6SMatthias Ringwald #include "hci.h" 573deb3ec6SMatthias Ringwald #include "btstack_memory.h" 583deb3ec6SMatthias Ringwald #include "hci_dump.h" 593deb3ec6SMatthias Ringwald #include "l2cap.h" 6016ece135SMatthias Ringwald #include "btstack_debug.h" 61e30a6a47SMatthias Ringwald #include "btstack_event.h" 6259c6af15SMatthias Ringwald #include "classic/core.h" 633edc84c5SMatthias Ringwald #include "classic/hfp.h" 643edc84c5SMatthias Ringwald #include "classic/hfp_ag.h" 65023f2764SMatthias Ringwald #include "classic/hfp_gsm_model.h" 66efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h" 67023f2764SMatthias Ringwald #include "classic/sdp_server.h" 68023f2764SMatthias Ringwald #include "classic/sdp_util.h" 693deb3ec6SMatthias Ringwald 70c5b64319SMatthias Ringwald // private prototypes 71f0aeb307SMatthias Ringwald static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection); 722ff1d7d7SMilanka Ringwald static void hfp_ag_hf_start_ringing_incoming(hfp_connection_t * hfp_connection); 73fcd59412SMilanka Ringwald static void hfp_ag_hf_start_ringing_outgoing(hfp_connection_t * hfp_connection); 74c5fa3c94SMilanka Ringwald static uint8_t hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection); 75c5b64319SMatthias Ringwald 76c5b64319SMatthias Ringwald // public prototypes 7735833313SMatthias Ringwald hfp_generic_status_indicator_t * get_hfp_generic_status_indicators(void); 7835833313SMatthias Ringwald int get_hfp_generic_status_indicators_nr(void); 79c5b64319SMatthias Ringwald void set_hfp_generic_status_indicators(hfp_generic_status_indicator_t * indicators, int indicator_nr); 80c5b64319SMatthias Ringwald void set_hfp_ag_indicators(hfp_ag_indicator_t * indicators, int indicator_nr); 81c5b64319SMatthias Ringwald int get_hfp_ag_indicators_nr(hfp_connection_t * context); 82c5b64319SMatthias Ringwald hfp_ag_indicator_t * get_hfp_ag_indicators(hfp_connection_t * context); 83c5b64319SMatthias Ringwald 8499af1e28SMilanka Ringwald #define HFP_SUBEVENT_INVALID 0xFFFF 8527950165SMatthias Ringwald 8620b2edb6SMatthias Ringwald // const 87aeb0f0feSMatthias Ringwald static const char hfp_ag_default_service_name[] = "Voice gateway"; 88aa4dd815SMatthias Ringwald 8920b2edb6SMatthias Ringwald // globals 90aeb0f0feSMatthias Ringwald 91aeb0f0feSMatthias Ringwald // higher layer callbacks 92aeb0f0feSMatthias Ringwald static btstack_packet_handler_t hfp_ag_callback; 93aeb0f0feSMatthias Ringwald 9481e25d0aSMatthias Ringwald static bool (*hfp_ag_custom_call_sm_handler)(hfp_ag_call_event_t event); 9581e25d0aSMatthias Ringwald 9620b2edb6SMatthias Ringwald static btstack_packet_callback_registration_t hfp_ag_hci_event_callback_registration; 97aa4dd815SMatthias Ringwald 98aeb0f0feSMatthias Ringwald static uint16_t hfp_ag_supported_features; 9920b2edb6SMatthias Ringwald 10040a8ee13SMatthias Ringwald // in-band ring tone is active on SLC if supported 10140a8ee13SMatthias Ringwald static bool hfp_ag_in_band_ring_tone_active; 10240a8ee13SMatthias Ringwald 103aeb0f0feSMatthias Ringwald // codecs 104aeb0f0feSMatthias Ringwald static uint8_t hfp_ag_codecs_nr; 105aeb0f0feSMatthias Ringwald static uint8_t hfp_ag_codecs[HFP_MAX_NUM_CODECS]; 1063deb3ec6SMatthias Ringwald 107aeb0f0feSMatthias Ringwald // AG indicators 10820b2edb6SMatthias Ringwald static int hfp_ag_indicators_nr; 10925789943SMilanka Ringwald static hfp_ag_indicator_t hfp_ag_indicators[HFP_MAX_NUM_INDICATORS]; 1103deb3ec6SMatthias Ringwald 111aeb0f0feSMatthias Ringwald // generic status indicators 112aeb0f0feSMatthias Ringwald static int hfp_ag_generic_status_indicators_nr; 113aeb0f0feSMatthias Ringwald static hfp_generic_status_indicator_t hfp_ag_generic_status_indicators[HFP_MAX_NUM_INDICATORS]; 114a0ffb263SMatthias Ringwald 11520b2edb6SMatthias Ringwald static int hfp_ag_call_hold_services_nr; 1163deb3ec6SMatthias Ringwald static char * hfp_ag_call_hold_services[6]; 1173deb3ec6SMatthias Ringwald 118ce263fc8SMatthias Ringwald static hfp_response_and_hold_state_t hfp_ag_response_and_hold_state; 11927bb1817SMatthias Ringwald static bool hfp_ag_response_and_hold_active = false; 120aa4dd815SMatthias Ringwald 12120b2edb6SMatthias Ringwald // Subscriber information entries 122aeb0f0feSMatthias Ringwald static hfp_phone_number_t * hfp_ag_subscriber_numbers; 123aeb0f0feSMatthias Ringwald static int hfp_ag_subscriber_numbers_count; 124ce263fc8SMatthias Ringwald 125a4f85bd2SMilanka Ringwald // call state 126a4f85bd2SMilanka Ringwald static btstack_timer_source_t hfp_ag_ring_timeout; 127a4f85bd2SMilanka Ringwald 12820b2edb6SMatthias Ringwald // code 129a0ffb263SMatthias Ringwald static int hfp_ag_get_ag_indicators_nr(hfp_connection_t * hfp_connection){ 130a0ffb263SMatthias Ringwald if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){ 131a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr; 1326535961aSMatthias Ringwald (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, 1336535961aSMatthias Ringwald hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 1343deb3ec6SMatthias Ringwald } 135a0ffb263SMatthias Ringwald return hfp_connection->ag_indicators_nr; 136a0ffb263SMatthias Ringwald } 137a0ffb263SMatthias Ringwald 138a0ffb263SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection){ 139a0ffb263SMatthias Ringwald // TODO: save only value, and value changed in the hfp_connection? 140a0ffb263SMatthias Ringwald if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){ 141a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr; 1426535961aSMatthias Ringwald (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, 1436535961aSMatthias Ringwald hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 144a0ffb263SMatthias Ringwald } 145a0ffb263SMatthias Ringwald return (hfp_ag_indicator_t *)&(hfp_connection->ag_indicators); 1463deb3ec6SMatthias Ringwald } 1473deb3ec6SMatthias Ringwald 148aa4dd815SMatthias Ringwald static hfp_ag_indicator_t * get_ag_indicator_for_name(const char * name){ 149aa4dd815SMatthias Ringwald int i; 150aa4dd815SMatthias Ringwald for (i = 0; i < hfp_ag_indicators_nr; i++){ 151aa4dd815SMatthias Ringwald if (strcmp(hfp_ag_indicators[i].name, name) == 0){ 152aa4dd815SMatthias Ringwald return &hfp_ag_indicators[i]; 153aa4dd815SMatthias Ringwald } 154aa4dd815SMatthias Ringwald } 155aa4dd815SMatthias Ringwald return NULL; 156aa4dd815SMatthias Ringwald } 157aa4dd815SMatthias Ringwald 158aa4dd815SMatthias Ringwald static int get_ag_indicator_index_for_name(const char * name){ 159aa4dd815SMatthias Ringwald int i; 160aa4dd815SMatthias Ringwald for (i = 0; i < hfp_ag_indicators_nr; i++){ 161aa4dd815SMatthias Ringwald if (strcmp(hfp_ag_indicators[i].name, name) == 0){ 162aa4dd815SMatthias Ringwald return i; 163aa4dd815SMatthias Ringwald } 164aa4dd815SMatthias Ringwald } 165aa4dd815SMatthias Ringwald return -1; 166aa4dd815SMatthias Ringwald } 167aa4dd815SMatthias Ringwald 1689c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_ag_connection_context_for_acl_handle(uint16_t handle){ 1699c9c64c1SMatthias Ringwald btstack_linked_list_iterator_t it; 1709c9c64c1SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1719c9c64c1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1729c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1739c9c64c1SMatthias Ringwald if (hfp_connection->acl_handle != handle) continue; 1749c9c64c1SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1759c9c64c1SMatthias Ringwald return hfp_connection; 1769c9c64c1SMatthias Ringwald } 1779c9c64c1SMatthias Ringwald return NULL; 1789c9c64c1SMatthias Ringwald } 1793deb3ec6SMatthias Ringwald 18040a8ee13SMatthias Ringwald static bool has_in_band_ring_tone(void){ 18140a8ee13SMatthias Ringwald return get_bit(hfp_ag_supported_features, HFP_AGSF_IN_BAND_RING_TONE) != 0; 18240a8ee13SMatthias Ringwald } 18340a8ee13SMatthias Ringwald 184f197e761SMatthias Ringwald static int use_in_band_tone(hfp_connection_t *hfp_connection) { 18540a8ee13SMatthias Ringwald return hfp_connection->ag_in_band_ring_tone_active ? 1 : 0; 1863deb3ec6SMatthias Ringwald } 1873deb3ec6SMatthias Ringwald 188a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){ 189a0ffb263SMatthias Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_CODEC_NEGOTIATION); 190aeb0f0feSMatthias Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_CODEC_NEGOTIATION); 1913deb3ec6SMatthias Ringwald return hf && ag; 1923deb3ec6SMatthias Ringwald } 1933deb3ec6SMatthias Ringwald 194a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){ 195a0ffb263SMatthias Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_THREE_WAY_CALLING); 196aeb0f0feSMatthias Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_THREE_WAY_CALLING); 1973deb3ec6SMatthias Ringwald return hf && ag; 1983deb3ec6SMatthias Ringwald } 1993deb3ec6SMatthias Ringwald 200a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){ 201a0ffb263SMatthias Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_HF_INDICATORS); 202aeb0f0feSMatthias Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_HF_INDICATORS); 2033deb3ec6SMatthias Ringwald return hf && ag; 2043deb3ec6SMatthias Ringwald } 2053deb3ec6SMatthias Ringwald 20676cc1527SMatthias Ringwald /* unsolicited responses */ 207aa4dd815SMatthias Ringwald 208f197e761SMatthias Ringwald static int hfp_ag_send_change_in_band_ring_tone_setting_cmd(hfp_connection_t * hfp_connection){ 2098d860c8fSMatthias Ringwald char buffer[40]; 210ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: %d\r\n", 211f197e761SMatthias Ringwald HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone(hfp_connection)); 212ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 213f197e761SMatthias Ringwald return send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 2143deb3ec6SMatthias Ringwald } 2153deb3ec6SMatthias Ringwald 2163deb3ec6SMatthias Ringwald static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){ 2173deb3ec6SMatthias Ringwald char buffer[40]; 218ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n\r\nOK\r\n", 219aeb0f0feSMatthias Ringwald HFP_SUPPORTED_FEATURES, hfp_ag_supported_features); 220ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 2213deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2223deb3ec6SMatthias Ringwald } 2233deb3ec6SMatthias Ringwald 224485ac19eSMilanka Ringwald static int hfp_ag_send_ok(uint16_t cid){ 2253deb3ec6SMatthias Ringwald char buffer[10]; 226ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\nOK\r\n"); 227ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 2283deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2293deb3ec6SMatthias Ringwald } 2303deb3ec6SMatthias Ringwald 231485ac19eSMilanka Ringwald static int hfp_ag_send_ring(uint16_t cid){ 232aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, (char *) "\r\nRING\r\n"); 233aa4dd815SMatthias Ringwald } 234aa4dd815SMatthias Ringwald 235245852b7SMilanka Ringwald static int hfp_ag_send_no_carrier(uint16_t cid){ 236245852b7SMilanka Ringwald char buffer[15]; 237245852b7SMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\nNO CARRIER\r\n"); 238245852b7SMilanka Ringwald buffer[sizeof(buffer) - 1] = 0; 239245852b7SMilanka Ringwald return send_str_over_rfcomm(cid, buffer); 240245852b7SMilanka Ringwald } 241245852b7SMilanka Ringwald 242aa4dd815SMatthias Ringwald static int hfp_ag_send_clip(uint16_t cid){ 243aa4dd815SMatthias Ringwald char buffer[50]; 244ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP, 245ff7d6aeaSMatthias Ringwald hfp_gsm_clip_number(), hfp_gsm_clip_type()); 246ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 247aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 248aa4dd815SMatthias Ringwald } 249aa4dd815SMatthias Ringwald 250ce263fc8SMatthias Ringwald static int hfp_send_subscriber_number_cmd(uint16_t cid, uint8_t type, const char * number){ 251ce263fc8SMatthias Ringwald char buffer[50]; 252ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: ,\"%s\",%u, , \r\n", 253ff7d6aeaSMatthias Ringwald HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type); 254ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 255ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 256ce263fc8SMatthias Ringwald } 257ce263fc8SMatthias Ringwald 258c1797c7dSMatthias Ringwald static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){ 259aa4dd815SMatthias Ringwald char buffer[50]; 260ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: %s\r\n", 261ff7d6aeaSMatthias Ringwald HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number()); 262ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 263aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 264aa4dd815SMatthias Ringwald } 265aa4dd815SMatthias Ringwald 266aa4dd815SMatthias Ringwald static int hfp_ag_send_call_waiting_notification(uint16_t cid){ 267aa4dd815SMatthias Ringwald char buffer[50]; 268ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: \"%s\",%u\r\n", 269ff7d6aeaSMatthias Ringwald HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(), 270ff7d6aeaSMatthias Ringwald hfp_gsm_clip_type()); 271ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 272aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 273aa4dd815SMatthias Ringwald } 274aa4dd815SMatthias Ringwald 275485ac19eSMilanka Ringwald static int hfp_ag_send_error(uint16_t cid){ 2763deb3ec6SMatthias Ringwald char buffer[10]; 277ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\nERROR\r\n"); 278ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 2793deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2803deb3ec6SMatthias Ringwald } 2813deb3ec6SMatthias Ringwald 282485ac19eSMilanka Ringwald static int hfp_ag_send_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){ 2833deb3ec6SMatthias Ringwald char buffer[20]; 284ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s=%d\r\n", 285ff7d6aeaSMatthias Ringwald HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error); 286ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 2873deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2883deb3ec6SMatthias Ringwald } 2893deb3ec6SMatthias Ringwald 290ad902e3dSMatthias Ringwald // get size for indicator string 291a0ffb263SMatthias Ringwald static int hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection, int i){ 292ad902e3dSMatthias Ringwald // template: ("$NAME",($MIN,$MAX)) 29345718b6fSMatthias Ringwald return 8 + (int) strlen(hfp_ag_get_ag_indicators(hfp_connection)[i].name) 294a0ffb263SMatthias Ringwald + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range) 295a0ffb263SMatthias Ringwald + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range); 296ad902e3dSMatthias Ringwald } 297ad902e3dSMatthias Ringwald 298ad902e3dSMatthias Ringwald // store indicator 2991167ff83SMatthias Ringwald static void hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection, int i, uint8_t * buffer, uint16_t buffer_size){ 3001167ff83SMatthias Ringwald snprintf((char *)buffer, buffer_size, "(\"%s\",(%d,%d)),", 301a0ffb263SMatthias Ringwald hfp_ag_get_ag_indicators(hfp_connection)[i].name, 302a0ffb263SMatthias Ringwald hfp_ag_get_ag_indicators(hfp_connection)[i].min_range, 303a0ffb263SMatthias Ringwald hfp_ag_get_ag_indicators(hfp_connection)[i].max_range); 3041167ff83SMatthias Ringwald ((char *)buffer)[buffer_size - 1] = 0; 3053deb3ec6SMatthias Ringwald } 306ad902e3dSMatthias Ringwald 307ad902e3dSMatthias Ringwald // structure: header [indicator [comma indicator]] footer 308a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_num_segments(hfp_connection_t * hfp_connection){ 309a0ffb263SMatthias Ringwald int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 310ad902e3dSMatthias Ringwald if (!num_indicators) return 2; 311c1ab6cc1SMatthias Ringwald return 3 + ((num_indicators-1) * 2); 3123deb3ec6SMatthias Ringwald } 313ad902e3dSMatthias Ringwald 314ad902e3dSMatthias Ringwald // get size of individual segment for hfp_ag_retrieve_indicators_cmd 315a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_get_segment_len(hfp_connection_t * hfp_connection, int index){ 316ad902e3dSMatthias Ringwald if (index == 0) { 317ab2445a0SMatthias Ringwald return (uint16_t) strlen(HFP_INDICATOR) + 3; // "\n\r%s:"" 318ad902e3dSMatthias Ringwald } 319ad902e3dSMatthias Ringwald index--; 320a0ffb263SMatthias Ringwald int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 321ad902e3dSMatthias Ringwald int indicator_index = index >> 1; 322ad902e3dSMatthias Ringwald if ((index & 1) == 0){ 323a0ffb263SMatthias Ringwald return hfp_ag_indicators_string_size(hfp_connection, indicator_index); 324ad902e3dSMatthias Ringwald } 325c1ab6cc1SMatthias Ringwald if (indicator_index == (num_indicators - 1)){ 326ad902e3dSMatthias Ringwald return 8; // "\r\n\r\nOK\r\n" 327ad902e3dSMatthias Ringwald } 328ad902e3dSMatthias Ringwald return 1; // comma 329ad902e3dSMatthias Ringwald } 330ad902e3dSMatthias Ringwald 3311167ff83SMatthias Ringwald static void hfp_ag_indicators_cmd_generator_store_segment(hfp_connection_t * hfp_connection, int index, uint8_t * buffer, uint16_t buffer_size){ 332ad902e3dSMatthias Ringwald if (index == 0){ 333ad902e3dSMatthias Ringwald *buffer++ = '\r'; 33474386ee0SMatthias Ringwald *buffer++ = '\n'; 335ab2445a0SMatthias Ringwald int len = (uint16_t) strlen(HFP_INDICATOR); 3366535961aSMatthias Ringwald (void)memcpy(buffer, HFP_INDICATOR, len); 337ad902e3dSMatthias Ringwald buffer += len; 338ad902e3dSMatthias Ringwald *buffer++ = ':'; 339ad902e3dSMatthias Ringwald return; 340ad902e3dSMatthias Ringwald } 341ad902e3dSMatthias Ringwald index--; 342a0ffb263SMatthias Ringwald int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 343ad902e3dSMatthias Ringwald int indicator_index = index >> 1; 344ad902e3dSMatthias Ringwald if ((index & 1) == 0){ 3451167ff83SMatthias Ringwald hfp_ag_indicators_string_store(hfp_connection, indicator_index, buffer, buffer_size); 346ad902e3dSMatthias Ringwald return; 347ad902e3dSMatthias Ringwald } 348c1ab6cc1SMatthias Ringwald if (indicator_index == (num_indicators-1)){ 3496535961aSMatthias Ringwald (void)memcpy(buffer, "\r\n\r\nOK\r\n", 8); 350ad902e3dSMatthias Ringwald return; 351ad902e3dSMatthias Ringwald } 352ad902e3dSMatthias Ringwald *buffer = ','; 3533deb3ec6SMatthias Ringwald } 3543deb3ec6SMatthias Ringwald 355aeb0f0feSMatthias Ringwald static int hfp_ag_generic_indicators_join(char * buffer, int buffer_size){ 356c1ab6cc1SMatthias Ringwald if (buffer_size < (hfp_ag_indicators_nr * 3)) return 0; 3573deb3ec6SMatthias Ringwald int i; 3583deb3ec6SMatthias Ringwald int offset = 0; 359aeb0f0feSMatthias Ringwald for (i = 0; i < (hfp_ag_generic_status_indicators_nr - 1); i++) { 360aeb0f0feSMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_generic_status_indicators[i].uuid); 3613deb3ec6SMatthias Ringwald } 362aeb0f0feSMatthias Ringwald if (i < hfp_ag_generic_status_indicators_nr){ 363aeb0f0feSMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_generic_status_indicators[i].uuid); 3643deb3ec6SMatthias Ringwald } 3653deb3ec6SMatthias Ringwald return offset; 3663deb3ec6SMatthias Ringwald } 3673deb3ec6SMatthias Ringwald 368aeb0f0feSMatthias Ringwald static int hfp_ag_generic_indicators_initial_status_join(char * buffer, int buffer_size){ 369aeb0f0feSMatthias Ringwald if (buffer_size < (hfp_ag_generic_status_indicators_nr * 3)) return 0; 3703deb3ec6SMatthias Ringwald int i; 3713deb3ec6SMatthias Ringwald int offset = 0; 372aeb0f0feSMatthias Ringwald for (i = 0; i < hfp_ag_generic_status_indicators_nr; i++) { 373aeb0f0feSMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "\r\n%s:%d,%d\r\n", HFP_GENERIC_STATUS_INDICATOR, hfp_ag_generic_status_indicators[i].uuid, hfp_ag_generic_status_indicators[i].state); 3743deb3ec6SMatthias Ringwald } 3753deb3ec6SMatthias Ringwald return offset; 3763deb3ec6SMatthias Ringwald } 3773deb3ec6SMatthias Ringwald 3783deb3ec6SMatthias Ringwald static int hfp_ag_indicators_status_join(char * buffer, int buffer_size){ 379c1ab6cc1SMatthias Ringwald if (buffer_size < (hfp_ag_indicators_nr * 3)) return 0; 3803deb3ec6SMatthias Ringwald int i; 3813deb3ec6SMatthias Ringwald int offset = 0; 382c1ab6cc1SMatthias Ringwald for (i = 0; i < (hfp_ag_indicators_nr-1); i++) { 3833deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_indicators[i].status); 3843deb3ec6SMatthias Ringwald } 3853deb3ec6SMatthias Ringwald if (i<hfp_ag_indicators_nr){ 3863deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_indicators[i].status); 3873deb3ec6SMatthias Ringwald } 3883deb3ec6SMatthias Ringwald return offset; 3893deb3ec6SMatthias Ringwald } 3903deb3ec6SMatthias Ringwald 3913deb3ec6SMatthias Ringwald static int hfp_ag_call_services_join(char * buffer, int buffer_size){ 392c1ab6cc1SMatthias Ringwald if (buffer_size < (hfp_ag_call_hold_services_nr * 3)) return 0; 3933deb3ec6SMatthias Ringwald int i; 3943deb3ec6SMatthias Ringwald int offset = snprintf(buffer, buffer_size, "("); 395c1ab6cc1SMatthias Ringwald for (i = 0; i < (hfp_ag_call_hold_services_nr-1); i++) { 3963deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%s,", hfp_ag_call_hold_services[i]); 3973deb3ec6SMatthias Ringwald } 3983deb3ec6SMatthias Ringwald if (i<hfp_ag_call_hold_services_nr){ 3993deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%s)", hfp_ag_call_hold_services[i]); 4003deb3ec6SMatthias Ringwald } 4013deb3ec6SMatthias Ringwald return offset; 4023deb3ec6SMatthias Ringwald } 4033deb3ec6SMatthias Ringwald 404485ac19eSMilanka Ringwald static int hfp_ag_send_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, 405ad902e3dSMatthias Ringwald int start_segment, int num_segments, 406a0ffb263SMatthias Ringwald int (*get_segment_len)(hfp_connection_t * hfp_connection, int segment), 4071167ff83SMatthias Ringwald void (*store_segment) (hfp_connection_t * hfp_connection, int segment, uint8_t * buffer, uint16_t buffer_size)){ 4083deb3ec6SMatthias Ringwald 409ad902e3dSMatthias Ringwald // assumes: can send now == true 410ad902e3dSMatthias Ringwald // assumes: num segments > 0 411aba39421SMatthias Ringwald // assumes: individual segments are smaller than MTU 412ad902e3dSMatthias Ringwald rfcomm_reserve_packet_buffer(); 413ad902e3dSMatthias Ringwald int mtu = rfcomm_get_max_frame_size(cid); 414ad902e3dSMatthias Ringwald uint8_t * data = rfcomm_get_outgoing_buffer(); 415ad902e3dSMatthias Ringwald int offset = 0; 416ad902e3dSMatthias Ringwald int segment = start_segment; 417ad902e3dSMatthias Ringwald while (segment < num_segments){ 418a0ffb263SMatthias Ringwald int segment_len = get_segment_len(hfp_connection, segment); 4191167ff83SMatthias Ringwald if ((offset + segment_len + 1) > mtu) break; 4201167ff83SMatthias Ringwald // append segment. As it appends a '\0', we provide a buffer one byte larger 4211167ff83SMatthias Ringwald store_segment(hfp_connection, segment, data+offset, segment_len + 1); 422ad902e3dSMatthias Ringwald offset += segment_len; 423ad902e3dSMatthias Ringwald segment++; 424ad902e3dSMatthias Ringwald } 425ad902e3dSMatthias Ringwald rfcomm_send_prepared(cid, offset); 426f20dd2aeSBjoern Hartmann #ifdef ENABLE_HFP_AT_MESSAGES 427f20dd2aeSBjoern Hartmann hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_SENT, (char *) data); 428f20dd2aeSBjoern Hartmann #endif 429ad902e3dSMatthias Ringwald return segment; 430ad902e3dSMatthias Ringwald } 4313deb3ec6SMatthias Ringwald 432ad902e3dSMatthias Ringwald // returns next segment to store 433485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, int start_segment){ 434a0ffb263SMatthias Ringwald int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection); 435485ac19eSMilanka Ringwald return hfp_ag_send_cmd_via_generator(cid, hfp_connection, start_segment, num_segments, 436894d499cSMatthias Ringwald hfp_ag_indicators_cmd_generator_get_segment_len, 437894d499cSMatthias Ringwald hfp_ag_indicators_cmd_generator_store_segment); 4383deb3ec6SMatthias Ringwald } 4393deb3ec6SMatthias Ringwald 440485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_status_cmd(uint16_t cid){ 4413deb3ec6SMatthias Ringwald char buffer[40]; 44289425bfcSMilanka Ringwald const int size = sizeof(buffer); 44389425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "\r\n%s:", HFP_INDICATOR); 44489425bfcSMilanka Ringwald offset += hfp_ag_indicators_status_join(buffer+offset, size-offset-9); 44589425bfcSMilanka Ringwald offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n"); 4463deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4473deb3ec6SMatthias Ringwald } 4483deb3ec6SMatthias Ringwald 449485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_can_hold_call_cmd(uint16_t cid){ 450ad902e3dSMatthias Ringwald char buffer[40]; 45189425bfcSMilanka Ringwald const int size = sizeof(buffer); 45289425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "\r\n%s:", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES); 45389425bfcSMilanka Ringwald offset += hfp_ag_call_services_join(buffer+offset, size-offset-9); 45489425bfcSMilanka Ringwald offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n"); 4553deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4563deb3ec6SMatthias Ringwald } 4573deb3ec6SMatthias Ringwald 4583deb3ec6SMatthias Ringwald 459485ac19eSMilanka Ringwald static int hfp_ag_send_list_supported_generic_status_indicators_cmd(uint16_t cid){ 460485ac19eSMilanka Ringwald return hfp_ag_send_ok(cid); 4613deb3ec6SMatthias Ringwald } 4623deb3ec6SMatthias Ringwald 463485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(uint16_t cid){ 4643deb3ec6SMatthias Ringwald char buffer[40]; 46589425bfcSMilanka Ringwald const int size = sizeof(buffer); 46689425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "\r\n%s:(", HFP_GENERIC_STATUS_INDICATOR); 467aeb0f0feSMatthias Ringwald offset += hfp_ag_generic_indicators_join(buffer + offset, size - offset - 10); 46889425bfcSMilanka Ringwald offset += snprintf(buffer+offset, size-offset, ")\r\n\r\nOK\r\n"); 4693deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4703deb3ec6SMatthias Ringwald } 4713deb3ec6SMatthias Ringwald 472485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid){ 4733deb3ec6SMatthias Ringwald char buffer[40]; 474aeb0f0feSMatthias Ringwald int offset = hfp_ag_generic_indicators_initial_status_join(buffer, sizeof(buffer) - 7); 4751cc1d9e9SMilanka Ringwald snprintf(buffer+offset, sizeof(buffer)-offset, "\r\nOK\r\n"); 4763deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4773deb3ec6SMatthias Ringwald } 4783deb3ec6SMatthias Ringwald 479485ac19eSMilanka Ringwald static int hfp_ag_send_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){ 4803deb3ec6SMatthias Ringwald char buffer[20]; 481ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s:%d,%d\r\n", 482ff7d6aeaSMatthias Ringwald HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index, 483ff7d6aeaSMatthias Ringwald indicator->status); 484ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 4853deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4863deb3ec6SMatthias Ringwald } 4873deb3ec6SMatthias Ringwald 488485ac19eSMilanka Ringwald static int hfp_ag_send_report_network_operator_name_cmd(uint16_t cid, hfp_network_opearator_t op){ 4895e71456cSMilanka Ringwald char buffer[41]; 4903deb3ec6SMatthias Ringwald if (strlen(op.name) == 0){ 49189425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s:%d,,\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode); 4923deb3ec6SMatthias Ringwald } else { 49389425bfcSMilanka Ringwald int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:%d,%d,", HFP_QUERY_OPERATOR_SELECTION, op.mode, op.format); 4945e71456cSMilanka Ringwald offset += snprintf(buffer+offset, 16, "%s", op.name); 49589425bfcSMilanka Ringwald snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n"); 4963deb3ec6SMatthias Ringwald } 4973deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4983deb3ec6SMatthias Ringwald } 4993deb3ec6SMatthias Ringwald 500a04aecb1SMilanka Ringwald static inline int hfp_ag_send_cmd_with_space_and_int(uint16_t cid, const char * cmd, uint8_t value){ 501a04aecb1SMilanka Ringwald char buffer[30]; 502a04aecb1SMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: %d\r\n", cmd, value); 503a04aecb1SMilanka Ringwald return send_str_over_rfcomm(cid, buffer); 504a04aecb1SMilanka Ringwald } 505a04aecb1SMilanka Ringwald 506a04aecb1SMilanka Ringwald 5071cc1d9e9SMilanka Ringwald static inline int hfp_ag_send_cmd_with_int(uint16_t cid, const char * cmd, uint8_t value){ 5081cc1d9e9SMilanka Ringwald char buffer[30]; 50989425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n", cmd, value); 5101cc1d9e9SMilanka Ringwald return send_str_over_rfcomm(cid, buffer); 5111cc1d9e9SMilanka Ringwald } 5123deb3ec6SMatthias Ringwald 513485ac19eSMilanka Ringwald static int hfp_ag_send_suggest_codec_cmd(uint16_t cid, uint8_t codec){ 5141cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec); 5153deb3ec6SMatthias Ringwald } 5163deb3ec6SMatthias Ringwald 517485ac19eSMilanka Ringwald static int hfp_ag_send_set_speaker_gain_cmd(uint16_t cid, uint8_t gain){ 5181cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain); 519aa4dd815SMatthias Ringwald } 520aa4dd815SMatthias Ringwald 521485ac19eSMilanka Ringwald static int hfp_ag_send_set_microphone_gain_cmd(uint16_t cid, uint8_t gain){ 5221cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain); 523aa4dd815SMatthias Ringwald } 524aa4dd815SMatthias Ringwald 525485ac19eSMilanka Ringwald static int hfp_ag_send_set_response_and_hold(uint16_t cid, int state){ 52666191aa0SMilanka Ringwald return hfp_ag_send_cmd_with_space_and_int(cid, HFP_RESPONSE_AND_HOLD, state); 527ce263fc8SMatthias Ringwald } 528ce263fc8SMatthias Ringwald 529cf75be85SMilanka Ringwald static int hfp_ag_send_enhanced_voice_recognition_state_cmd(hfp_connection_t * hfp_connection){ 530cf75be85SMilanka Ringwald char buffer[30]; 531fcf4ede6SMilanka Ringwald uint8_t evra_enabled = hfp_connection->enhanced_voice_recognition_enabled ? 1 : 0; 532fcf4ede6SMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, evra_enabled, hfp_connection->ag_vra_state); 533cf75be85SMilanka Ringwald return send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 534cf75be85SMilanka Ringwald } 535cf75be85SMilanka Ringwald 536d1b50446SMilanka Ringwald static int hfp_ag_send_voice_recognition_cmd(hfp_connection_t * hfp_connection, uint8_t activate_voice_recognition){ 53745796ff1SMilanka Ringwald char buffer[30]; 538d1b50446SMilanka Ringwald if (hfp_connection->enhanced_voice_recognition_enabled){ 539d1b50446SMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition, hfp_connection->ag_vra_state); 540d1b50446SMilanka Ringwald } else { 541d1b50446SMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: %d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition); 542d1b50446SMilanka Ringwald } 543d1b50446SMilanka Ringwald return send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 54445796ff1SMilanka Ringwald } 54545796ff1SMilanka Ringwald 54645796ff1SMilanka Ringwald static int hfp_ag_send_enhanced_voice_recognition_msg_cmd(hfp_connection_t * hfp_connection){ 547a2e701c0SMilanka Ringwald char buffer[HFP_VR_TEXT_HEADER_SIZE + HFP_MAX_VR_TEXT_SIZE]; 54845796ff1SMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: 1,%d,%X,%d,%d,\"%s\"\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, 54945796ff1SMilanka Ringwald hfp_connection->ag_vra_state, 55045796ff1SMilanka Ringwald hfp_connection->ag_msg.text_id, 55145796ff1SMilanka Ringwald hfp_connection->ag_msg.text_type, 55245796ff1SMilanka Ringwald hfp_connection->ag_msg.text_operation, 55345796ff1SMilanka Ringwald hfp_connection->ag_msg.text); 55445796ff1SMilanka Ringwald return send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 55545796ff1SMilanka Ringwald } 55645796ff1SMilanka Ringwald 557a0ffb263SMatthias Ringwald static uint8_t hfp_ag_suggest_codec(hfp_connection_t *hfp_connection){ 558bc1b1537SMilanka Ringwald if (hfp_connection->sco_for_msbc_failed) return HFP_CODEC_CVSD; 559bc1b1537SMilanka Ringwald 5602085619dSMatthias Ringwald #ifdef ENABLE_HFP_SUPER_WIDE_BAND_SPEECH 5612085619dSMatthias Ringwald if (hfp_supports_codec(HFP_CODEC_LC3_SWB, hfp_ag_codecs_nr, hfp_ag_codecs)){ 5622085619dSMatthias Ringwald if (hfp_supports_codec(HFP_CODEC_LC3_SWB, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){ 5632085619dSMatthias Ringwald return HFP_CODEC_LC3_SWB; 5642085619dSMatthias Ringwald } 5652085619dSMatthias Ringwald } 5662085619dSMatthias Ringwald #endif 5672085619dSMatthias Ringwald 5682085619dSMatthias Ringwald #ifdef ENABLE_HFP_WIDE_BAND_SPEECH 569aeb0f0feSMatthias Ringwald if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_ag_codecs_nr, hfp_ag_codecs)){ 5706a7f44bdSMilanka Ringwald if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){ 571df327ff3SMilanka Ringwald return HFP_CODEC_MSBC; 5723deb3ec6SMatthias Ringwald } 5733deb3ec6SMatthias Ringwald } 5742085619dSMatthias Ringwald #endif 5752085619dSMatthias Ringwald 576df327ff3SMilanka Ringwald return HFP_CODEC_CVSD; 5773deb3ec6SMatthias Ringwald } 5783deb3ec6SMatthias Ringwald 57976cc1527SMatthias Ringwald /* state machines */ 58076cc1527SMatthias Ringwald 581afac2a04SMilanka Ringwald static uint8_t hfp_ag_esco_s4_supported(hfp_connection_t * hfp_connection){ 582aeb0f0feSMatthias Ringwald return (hfp_connection->remote_supported_features & (1<<HFP_HFSF_ESCO_S4)) && (hfp_ag_supported_features & (1 << HFP_AGSF_ESCO_S4)); 5837522e673SMatthias Ringwald } 5847522e673SMatthias Ringwald 585a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){ 586aa4dd815SMatthias Ringwald /* events ( == commands): 587aa4dd815SMatthias Ringwald HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs 588aa4dd815SMatthias Ringwald HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 589aa4dd815SMatthias Ringwald hf_trigger_codec_connection_setup == received BCC 590aa4dd815SMatthias Ringwald ag_trigger_codec_connection_setup == received from AG to send BCS 591aa4dd815SMatthias Ringwald HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS 592aa4dd815SMatthias Ringwald */ 593a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 5943e3b9207SMilanka Ringwald case HFP_CODECS_EXCHANGED: 5953e3b9207SMilanka Ringwald if (hfp_connection->command == HFP_CMD_AVAILABLE_CODECS){ 5963e3b9207SMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 5973e3b9207SMilanka Ringwald return 1; 5983e3b9207SMilanka Ringwald } 5993e3b9207SMilanka Ringwald break; 6003e3b9207SMilanka Ringwald 601aa4dd815SMatthias Ringwald case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 602aa4dd815SMatthias Ringwald case HFP_CODECS_AG_RESEND_COMMON_CODEC: 603a814da6aSMatthias Ringwald hfp_connection->ag_send_common_codec = true; 604aa4dd815SMatthias Ringwald break; 605aa4dd815SMatthias Ringwald default: 606aa4dd815SMatthias Ringwald break; 607aa4dd815SMatthias Ringwald } 608aa4dd815SMatthias Ringwald 609a814da6aSMatthias Ringwald if (hfp_connection->ag_send_common_codec){ 610a814da6aSMatthias Ringwald hfp_connection->ag_send_common_codec = false; 611a814da6aSMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_AG_SENT_COMMON_CODEC; 612a814da6aSMatthias Ringwald hfp_connection->suggested_codec = hfp_ag_suggest_codec(hfp_connection); 613a814da6aSMatthias Ringwald hfp_ag_send_suggest_codec_cmd(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec); 614a814da6aSMatthias Ringwald return 1; 615a814da6aSMatthias Ringwald } 616a814da6aSMatthias Ringwald 617a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 618aa4dd815SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 619a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){ 620a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_LIST; 621485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 622aa4dd815SMatthias Ringwald return 1; 623aa4dd815SMatthias Ringwald } 624aa4dd815SMatthias Ringwald 625a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 626aa4dd815SMatthias Ringwald case HFP_CODECS_AG_SENT_COMMON_CODEC: 627a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_AG_RESEND_COMMON_CODEC; 628aa4dd815SMatthias Ringwald break; 629aa4dd815SMatthias Ringwald default: 630aa4dd815SMatthias Ringwald break; 631aa4dd815SMatthias Ringwald } 632485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 633aa4dd815SMatthias Ringwald return 1; 634aa4dd815SMatthias Ringwald 635aa4dd815SMatthias Ringwald case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 636a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 63740e8d6c5SMatthias Ringwald hfp_connection->establish_audio_connection = 1; 638485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 639aa4dd815SMatthias Ringwald return 1; 640aa4dd815SMatthias Ringwald 641aa4dd815SMatthias Ringwald case HFP_CMD_HF_CONFIRMED_CODEC: 642a0ffb263SMatthias Ringwald if (hfp_connection->codec_confirmed != hfp_connection->suggested_codec){ 643a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_ERROR; 644485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 645aa4dd815SMatthias Ringwald return 1; 646aa4dd815SMatthias Ringwald } 647a0ffb263SMatthias Ringwald hfp_connection->negotiated_codec = hfp_connection->codec_confirmed; 648a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 649c1ab6cc1SMatthias Ringwald log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD"); 650485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 6517522e673SMatthias Ringwald // now, pick link settings 652afac2a04SMilanka Ringwald hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection)); 6530327df7dSMatthias Ringwald // configure SBC coded if needed 6540327df7dSMatthias Ringwald hfp_prepare_for_sco(hfp_connection); 655aa4dd815SMatthias Ringwald return 1; 656aa4dd815SMatthias Ringwald default: 657aa4dd815SMatthias Ringwald break; 658aa4dd815SMatthias Ringwald } 659aa4dd815SMatthias Ringwald return 0; 660aa4dd815SMatthias Ringwald } 661aa4dd815SMatthias Ringwald 662a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){ 663a0ffb263SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 6647095467fSMatthias Ringwald hfp_emit_slc_connection_event(hfp_connection->local_role, 0, hfp_connection->acl_handle, hfp_connection->remote_addr); 665aa4dd815SMatthias Ringwald 66640a8ee13SMatthias Ringwald // in-band ring tone is active if supported 66740a8ee13SMatthias Ringwald hfp_connection->ag_in_band_ring_tone_active = has_in_band_ring_tone(); 66840a8ee13SMatthias Ringwald 66980702f97SMatthias Ringwald // HFP 4.35: "When [...] a new Service Level Connection is established all indicators are activated by default." 67080702f97SMatthias Ringwald uint16_t i; 67180702f97SMatthias Ringwald for (i=0;i<hfp_connection->ag_indicators_nr;i++){ 67280702f97SMatthias Ringwald hfp_connection->ag_indicators[i].enabled = 1; 67380702f97SMatthias Ringwald } 67480702f97SMatthias Ringwald 675a0ffb263SMatthias Ringwald // if active call exist, set per-hfp_connection state active, too (when audio is on) 676d0c20769SMatthias Ringwald if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 677a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE; 678aa4dd815SMatthias Ringwald } 679ce263fc8SMatthias Ringwald // if AG is ringing, also start ringing on the HF 680c4f8fc1cSMilanka Ringwald if (hfp_gsm_call_status() == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS){ 681c4f8fc1cSMilanka Ringwald switch (hfp_gsm_callsetup_status()){ 682c4f8fc1cSMilanka Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 6832ff1d7d7SMilanka Ringwald hfp_ag_hf_start_ringing_incoming(hfp_connection); 684c4f8fc1cSMilanka Ringwald break; 685fcd59412SMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 686fcd59412SMilanka Ringwald hfp_ag_hf_start_ringing_outgoing(hfp_connection); 687fcd59412SMilanka Ringwald break; 688c4f8fc1cSMilanka Ringwald default: 689c4f8fc1cSMilanka Ringwald break; 690c4f8fc1cSMilanka Ringwald } 691ce263fc8SMatthias Ringwald } 692aa4dd815SMatthias Ringwald } 6933deb3ec6SMatthias Ringwald 694a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){ 695c79b4cabSMatthias Ringwald // log_info("hfp_ag_run_for_context_service_level_connection state %u, command %u", hfp_connection->state, hfp_connection->command); 696a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 697c04cf7ffSMilanka Ringwald int sent = 0; 698a0ffb263SMatthias Ringwald switch(hfp_connection->command){ 6993deb3ec6SMatthias Ringwald case HFP_CMD_SUPPORTED_FEATURES: 700a0ffb263SMatthias Ringwald switch(hfp_connection->state){ 7013deb3ec6SMatthias Ringwald case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 702aa4dd815SMatthias Ringwald case HFP_EXCHANGE_SUPPORTED_FEATURES: 703aeb0f0feSMatthias Ringwald hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_ag_codecs, &hfp_ag_codecs_nr); 704a0ffb263SMatthias Ringwald if (has_codec_negotiation_feature(hfp_connection)){ 705a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS; 706aa4dd815SMatthias Ringwald } else { 707a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 708aa4dd815SMatthias Ringwald } 709a0ffb263SMatthias Ringwald hfp_ag_exchange_supported_features_cmd(hfp_connection->rfcomm_cid); 710aa4dd815SMatthias Ringwald return 1; 7113deb3ec6SMatthias Ringwald default: 7123deb3ec6SMatthias Ringwald break; 7133deb3ec6SMatthias Ringwald } 7143deb3ec6SMatthias Ringwald break; 7153deb3ec6SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 716c04cf7ffSMilanka Ringwald sent = codecs_exchange_state_machine(hfp_connection); 717a0ffb263SMatthias Ringwald if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){ 718a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 7193deb3ec6SMatthias Ringwald } 720c04cf7ffSMilanka Ringwald return sent; 721aa4dd815SMatthias Ringwald 722aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 723a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_W4_RETRIEVE_INDICATORS) { 724473ac565SMatthias Ringwald // HF requested AG Indicators and we did expect it 72584a0c24eSMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0; 726473ac565SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 727473ac565SMatthias Ringwald // continue below in state switch 728ad902e3dSMatthias Ringwald } 729ad902e3dSMatthias Ringwald break; 730aa4dd815SMatthias Ringwald 731aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 732a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break; 733a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE; 734485ac19eSMilanka Ringwald hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid); 735aa4dd815SMatthias Ringwald return 1; 736aa4dd815SMatthias Ringwald 7373deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 738a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break; 739a0ffb263SMatthias Ringwald if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 740a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL; 741a0ffb263SMatthias Ringwald } else if (has_hf_indicators_feature(hfp_connection)){ 742a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 743aa4dd815SMatthias Ringwald } else { 744a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 7453deb3ec6SMatthias Ringwald } 7468a46ec40SMatthias Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 747aa4dd815SMatthias Ringwald return 1; 7483deb3ec6SMatthias Ringwald 749aa4dd815SMatthias Ringwald case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES: 750a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break; 751a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 752a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 753aa4dd815SMatthias Ringwald } 754485ac19eSMilanka Ringwald hfp_ag_send_retrieve_can_hold_call_cmd(hfp_connection->rfcomm_cid); 755a0ffb263SMatthias Ringwald if (!has_hf_indicators_feature(hfp_connection)){ 756a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 757ce263fc8SMatthias Ringwald } 758aa4dd815SMatthias Ringwald return 1; 759aa4dd815SMatthias Ringwald 760aa4dd815SMatthias Ringwald case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 761a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break; 762a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS; 763485ac19eSMilanka Ringwald hfp_ag_send_list_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 764aa4dd815SMatthias Ringwald return 1; 765aa4dd815SMatthias Ringwald 766aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 767a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break; 768a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 769485ac19eSMilanka Ringwald hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 770aa4dd815SMatthias Ringwald return 1; 771aa4dd815SMatthias Ringwald 772aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 773a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break; 774485ac19eSMilanka Ringwald hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 775a7666b02SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 776aa4dd815SMatthias Ringwald return 1; 7773deb3ec6SMatthias Ringwald default: 7783deb3ec6SMatthias Ringwald break; 7793deb3ec6SMatthias Ringwald } 780473ac565SMatthias Ringwald 781473ac565SMatthias Ringwald switch (hfp_connection->state){ 782473ac565SMatthias Ringwald case HFP_RETRIEVE_INDICATORS: { 783485ac19eSMilanka Ringwald int next_segment = hfp_ag_send_retrieve_indicators_cmd_via_generator(hfp_connection->rfcomm_cid, hfp_connection, hfp_connection->send_ag_indicators_segment); 784473ac565SMatthias Ringwald int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection); 785473ac565SMatthias Ringwald log_info("HFP_CMD_RETRIEVE_AG_INDICATORS next segment %u, num_segments %u", next_segment, num_segments); 786473ac565SMatthias Ringwald if (next_segment < num_segments){ 787473ac565SMatthias Ringwald // prepare sending of next segment 788473ac565SMatthias Ringwald hfp_connection->send_ag_indicators_segment = next_segment; 789473ac565SMatthias Ringwald log_info("HFP_CMD_RETRIEVE_AG_INDICATORS more. command %u, next seg %u", hfp_connection->command, next_segment); 790473ac565SMatthias Ringwald } else { 791473ac565SMatthias Ringwald // done, go to next state 792473ac565SMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0; 793473ac565SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS; 794473ac565SMatthias Ringwald } 795473ac565SMatthias Ringwald return 1; 796473ac565SMatthias Ringwald } 797473ac565SMatthias Ringwald default: 798473ac565SMatthias Ringwald break; 799473ac565SMatthias Ringwald } 800c04cf7ffSMilanka Ringwald return 0; 8013deb3ec6SMatthias Ringwald } 8023deb3ec6SMatthias Ringwald 8039cd26862SMilanka Ringwald static bool hfp_ag_vra_flag_supported(hfp_connection_t * hfp_connection){ 80414c148caSMilanka Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION); 80514c148caSMilanka Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION); 80614c148caSMilanka Ringwald return hf && ag; 80714c148caSMilanka Ringwald } 80814c148caSMilanka Ringwald 8099cd26862SMilanka Ringwald static bool hfp_ag_enhanced_vra_flag_supported(hfp_connection_t * hfp_connection){ 81014c148caSMilanka Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS); 81114c148caSMilanka Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS); 81214c148caSMilanka Ringwald return hf && ag; 81314c148caSMilanka Ringwald } 81414c148caSMilanka Ringwald 8159cd26862SMilanka Ringwald static bool hfp_ag_can_send_enhanced_vra_message_flag_supported(hfp_connection_t * hfp_connection){ 816f71d2aacSMilanka Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_VOICE_RECOGNITION_TEXT); 817f71d2aacSMilanka Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_TEXT); 818f71d2aacSMilanka Ringwald return hf && ag; 81908a0b01cSMilanka Ringwald } 820f71d2aacSMilanka Ringwald 821135d9718SMilanka Ringwald static bool hfp_ag_can_activate_voice_recognition(hfp_connection_t * hfp_connection){ 822f71d2aacSMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){ 823135d9718SMilanka Ringwald return false; 824d84719b1SMilanka Ringwald } 8250c738fd4SMilanka Ringwald if (hfp_connection->vra_state != HFP_VRA_VOICE_RECOGNITION_OFF){ 826135d9718SMilanka Ringwald return false; 827d84719b1SMilanka Ringwald } 82808dc7cc6SMatthias Ringwald if (hfp_connection->ag_vra_send_command){ 82908dc7cc6SMatthias Ringwald return false; 83008dc7cc6SMatthias Ringwald } 831135d9718SMilanka Ringwald return true; 832d84719b1SMilanka Ringwald } 833d84719b1SMilanka Ringwald 834135d9718SMilanka Ringwald static bool hfp_ag_voice_recognition_session_active(hfp_connection_t * hfp_connection){ 83508a0b01cSMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){ 836135d9718SMilanka Ringwald return false; 83708a0b01cSMilanka Ringwald } 8380c738fd4SMilanka Ringwald if (hfp_connection->vra_state != HFP_VRA_VOICE_RECOGNITION_ACTIVATED){ 839de9e0ea7SMilanka Ringwald if (hfp_connection->vra_state == HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO){ 840135d9718SMilanka Ringwald return true; 841de9e0ea7SMilanka Ringwald } 842135d9718SMilanka Ringwald return false; 843d84719b1SMilanka Ringwald } 844135d9718SMilanka Ringwald return true; 845d84719b1SMilanka Ringwald } 846754910caSMilanka Ringwald 8470b4debbfSMilanka Ringwald static bool hfp_ag_is_audio_connection_active(hfp_connection_t * hfp_connection){ 8480b4debbfSMilanka Ringwald switch (hfp_connection->state){ 8490b4debbfSMilanka Ringwald case HFP_W2_CONNECT_SCO: 8500b4debbfSMilanka Ringwald case HFP_W4_SCO_CONNECTED: 8510b4debbfSMilanka Ringwald case HFP_AUDIO_CONNECTION_ESTABLISHED: 8520b4debbfSMilanka Ringwald return true; 853754910caSMilanka Ringwald default: 8540b4debbfSMilanka Ringwald return false; 855754910caSMilanka Ringwald } 856754910caSMilanka Ringwald } 857754910caSMilanka Ringwald 858b95cac54SMilanka Ringwald static void hfp_ag_emit_enhanced_voice_recognition_msg_sent_event(hfp_connection_t * hfp_connection, uint8_t status){ 859b95cac54SMilanka Ringwald hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 860b95cac54SMilanka Ringwald 861b95cac54SMilanka Ringwald uint8_t event[6]; 862b95cac54SMilanka Ringwald event[0] = HCI_EVENT_HFP_META; 863b95cac54SMilanka Ringwald event[1] = sizeof(event) - 2; 864c95b5b3cSMilanka Ringwald event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_MESSAGE_SENT; 865b95cac54SMilanka Ringwald little_endian_store_16(event, 3, acl_handle); 866b95cac54SMilanka Ringwald event[5] = status; 867b95cac54SMilanka Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 868b95cac54SMilanka Ringwald } 869b95cac54SMilanka Ringwald 870347d46c8SMilanka Ringwald 871585db78dSMilanka Ringwald static void hfp_ag_emit_hf_indicator_value(hfp_connection_t * hfp_connection, uint16_t uuid, uint8_t value){ 872347d46c8SMilanka Ringwald hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 873347d46c8SMilanka Ringwald 874347d46c8SMilanka Ringwald uint8_t event[8]; 875347d46c8SMilanka Ringwald event[0] = HCI_EVENT_HFP_META; 876347d46c8SMilanka Ringwald event[1] = sizeof(event) - 2; 877347d46c8SMilanka Ringwald event[2] = HFP_SUBEVENT_HF_INDICATOR; 878347d46c8SMilanka Ringwald little_endian_store_16(event, 3, acl_handle); 879347d46c8SMilanka Ringwald little_endian_store_16(event, 5, uuid); 880347d46c8SMilanka Ringwald event[7] = value; 881347d46c8SMilanka Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 882347d46c8SMilanka Ringwald } 883347d46c8SMilanka Ringwald 884a4f85bd2SMilanka Ringwald static void hfp_ag_emit_general_simple_event(uint8_t event_subtype){ 885a4f85bd2SMilanka Ringwald if (!hfp_ag_callback) return; 886a4f85bd2SMilanka Ringwald 887a4f85bd2SMilanka Ringwald uint8_t event[5]; 888a4f85bd2SMilanka Ringwald event[0] = HCI_EVENT_HFP_META; 889a4f85bd2SMilanka Ringwald event[1] = sizeof(event) - 2; 890a4f85bd2SMilanka Ringwald event[2] = event_subtype; 891a4f85bd2SMilanka Ringwald little_endian_store_16(event, 3, HCI_CON_HANDLE_INVALID); 892a4f85bd2SMilanka Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 893a4f85bd2SMilanka Ringwald } 894a4f85bd2SMilanka Ringwald 895471dea41SMatthias Ringwald static void hfp_ag_emit_custom_command_event(hfp_connection_t * hfp_connection){ 896471dea41SMatthias Ringwald btstack_assert(sizeof(hfp_connection->line_buffer) < (255-5)); 897471dea41SMatthias Ringwald 898471dea41SMatthias Ringwald uint16_t line_len = strlen((const char*)hfp_connection->line_buffer) + 1; 899471dea41SMatthias Ringwald uint8_t event[7 + sizeof(hfp_connection->line_buffer)]; 900471dea41SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 901471dea41SMatthias Ringwald event[1] = 5 + line_len; 902471dea41SMatthias Ringwald event[2] = HFP_SUBEVENT_CUSTOM_AT_COMMAND; 903471dea41SMatthias Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 9046d9a41f9SMatthias Ringwald little_endian_store_16(event, 5, hfp_connection->custom_at_command_id); 905471dea41SMatthias Ringwald memcpy(&event[7], hfp_connection->line_buffer, line_len); 906471dea41SMatthias Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, 7 + line_len); 907471dea41SMatthias Ringwald } 908471dea41SMatthias Ringwald 909774e3016SMatthias Ringwald // @return status 910774e3016SMatthias Ringwald static uint8_t hfp_ag_vra_state_machine_two(hfp_connection_t * hfp_connection){ 911774e3016SMatthias Ringwald uint8_t status = ERROR_CODE_SUCCESS; 912774e3016SMatthias Ringwald switch (hfp_connection->vra_state_requested){ 913774e3016SMatthias Ringwald case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 914774e3016SMatthias Ringwald if (!hfp_ag_is_audio_connection_active(hfp_connection)){ 915774e3016SMatthias Ringwald status = hfp_ag_setup_audio_connection(hfp_connection); 916774e3016SMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 917774e3016SMatthias Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 918774e3016SMatthias Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, status); 919774e3016SMatthias Ringwald break; 920774e3016SMatthias Ringwald } 921774e3016SMatthias Ringwald } 922774e3016SMatthias Ringwald hfp_connection->enhanced_voice_recognition_enabled = true; 923774e3016SMatthias Ringwald hfp_connection->vra_state = HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 924774e3016SMatthias Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 925774e3016SMatthias Ringwald hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_SUCCESS); 926774e3016SMatthias Ringwald break; 927774e3016SMatthias Ringwald 928774e3016SMatthias Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 929774e3016SMatthias Ringwald hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF; 930774e3016SMatthias Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 931774e3016SMatthias Ringwald 932774e3016SMatthias Ringwald // release audio connection only if it was opened after audio VR activated 933774e3016SMatthias Ringwald if (hfp_ag_is_audio_connection_active(hfp_connection) && !hfp_connection->ag_audio_connection_opened_before_vra){ 934774e3016SMatthias Ringwald hfp_trigger_release_audio_connection(hfp_connection); 935774e3016SMatthias Ringwald } 936774e3016SMatthias Ringwald 937774e3016SMatthias Ringwald hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS); 938774e3016SMatthias Ringwald break; 939774e3016SMatthias Ringwald 940774e3016SMatthias Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED: 941774e3016SMatthias Ringwald // open only if audio connection does not already exist 942774e3016SMatthias Ringwald if (!hfp_ag_is_audio_connection_active(hfp_connection)){ 943774e3016SMatthias Ringwald status = hfp_ag_setup_audio_connection(hfp_connection); 944774e3016SMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 945774e3016SMatthias Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 946774e3016SMatthias Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, status); 947774e3016SMatthias Ringwald break; 948774e3016SMatthias Ringwald } 949774e3016SMatthias Ringwald } 950774e3016SMatthias Ringwald 951774e3016SMatthias Ringwald hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_ACTIVATED; 952774e3016SMatthias Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 953774e3016SMatthias Ringwald hfp_connection->enhanced_voice_recognition_enabled = hfp_ag_enhanced_vra_flag_supported(hfp_connection); 95484fb9ac1SMilanka Ringwald 95584fb9ac1SMilanka Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 956774e3016SMatthias Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS); 95784fb9ac1SMilanka Ringwald } else { 95884fb9ac1SMilanka Ringwald hfp_connection->emit_vra_enabled_after_audio_established = true; 95984fb9ac1SMilanka Ringwald } 960774e3016SMatthias Ringwald break; 961774e3016SMatthias Ringwald 962774e3016SMatthias Ringwald default: 963774e3016SMatthias Ringwald break; 964774e3016SMatthias Ringwald } 965774e3016SMatthias Ringwald return status; 966774e3016SMatthias Ringwald } 967774e3016SMatthias Ringwald 96808dc7cc6SMatthias Ringwald static uint8_t hfp_ag_vra_send_command(hfp_connection_t * hfp_connection){ 96908dc7cc6SMatthias Ringwald uint8_t done = 0; 970754910caSMilanka Ringwald uint8_t status; 971754910caSMilanka Ringwald 972cf75be85SMilanka Ringwald switch (hfp_connection->vra_state_requested){ 973b95cac54SMilanka Ringwald case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_MSG: 974b95cac54SMilanka Ringwald done = hfp_ag_send_enhanced_voice_recognition_msg_cmd(hfp_connection); 975b95cac54SMilanka Ringwald if (done == 0){ 976b95cac54SMilanka Ringwald hfp_ag_emit_enhanced_voice_recognition_msg_sent_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 977b95cac54SMilanka Ringwald } else { 978b95cac54SMilanka Ringwald hfp_ag_emit_enhanced_voice_recognition_msg_sent_event(hfp_connection, ERROR_CODE_SUCCESS); 979b95cac54SMilanka Ringwald } 980b95cac54SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 981b95cac54SMilanka Ringwald return done; 982b95cac54SMilanka Ringwald 983cf75be85SMilanka Ringwald case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_STATUS: 984cf75be85SMilanka Ringwald done = hfp_ag_send_enhanced_voice_recognition_state_cmd(hfp_connection); 985cf75be85SMilanka Ringwald if (done == 0){ 986cf75be85SMilanka Ringwald hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 987cf75be85SMilanka Ringwald } else { 988cf75be85SMilanka Ringwald hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS); 989cf75be85SMilanka Ringwald } 990cf75be85SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 991b95cac54SMilanka Ringwald return done; 992b95cac54SMilanka Ringwald 9936e13e408SMatthias Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED: 9946e13e408SMatthias Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 995d1b50446SMilanka Ringwald done = hfp_ag_send_voice_recognition_cmd(hfp_connection, hfp_connection->ag_activate_voice_recognition_value); 9960c738fd4SMilanka Ringwald if (done == 0){ 9970c738fd4SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 998553a4a56SMilanka Ringwald 999553a4a56SMilanka Ringwald if (hfp_connection->ag_activate_voice_recognition_value == 1){ 1000553a4a56SMilanka Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, done); 1001553a4a56SMilanka Ringwald } else { 1002553a4a56SMilanka Ringwald hfp_emit_voice_recognition_disabled(hfp_connection, done); 1003553a4a56SMilanka Ringwald } 1004fd4151d1SMilanka Ringwald return 0; 1005fd4151d1SMilanka Ringwald } 100608dc7cc6SMatthias Ringwald status = hfp_ag_vra_state_machine_two(hfp_connection); 100708dc7cc6SMatthias Ringwald return (status == ERROR_CODE_SUCCESS) ? 1 : 0; 100808dc7cc6SMatthias Ringwald 10096e13e408SMatthias Ringwald default: 10106e13e408SMatthias Ringwald log_error("state %u", hfp_connection->vra_state_requested); 10116e13e408SMatthias Ringwald btstack_assert(false); 101208dc7cc6SMatthias Ringwald return 0; 1013cf75be85SMilanka Ringwald } 101408dc7cc6SMatthias Ringwald } 101508dc7cc6SMatthias Ringwald 101608dc7cc6SMatthias Ringwald static int hfp_ag_voice_recognition_state_machine(hfp_connection_t * hfp_connection){ 101708dc7cc6SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) { 101808dc7cc6SMatthias Ringwald return 0; 101908dc7cc6SMatthias Ringwald } 102008dc7cc6SMatthias Ringwald int done = 0; 102108dc7cc6SMatthias Ringwald uint8_t status; 102208dc7cc6SMatthias Ringwald 10238f8818a4SMatthias Ringwald // VRA action initiated by AG 10248f8818a4SMatthias Ringwald if (hfp_connection->ag_vra_send_command){ 10258f8818a4SMatthias Ringwald hfp_connection->ag_vra_send_command = false; 10268f8818a4SMatthias Ringwald return hfp_ag_vra_send_command(hfp_connection); 10278f8818a4SMatthias Ringwald } 10288f8818a4SMatthias Ringwald 1029e7c46708SMatthias Ringwald if (hfp_connection->ag_vra_requested_by_hf){ 1030e7c46708SMatthias Ringwald hfp_connection->ag_vra_requested_by_hf = false; 1031e7c46708SMatthias Ringwald 1032734ef2bdSMilanka Ringwald // HF initiatied voice recognition, parser extracted activation value 1033de9e0ea7SMilanka Ringwald switch (hfp_connection->ag_activate_voice_recognition_value){ 1034de9e0ea7SMilanka Ringwald case 0: 1035135d9718SMilanka Ringwald if (hfp_ag_voice_recognition_session_active(hfp_connection)){ 1036fd4151d1SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF; 1037fd4151d1SMilanka Ringwald done = hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1038fd4151d1SMilanka Ringwald } 1039de9e0ea7SMilanka Ringwald break; 1040de9e0ea7SMilanka Ringwald 1041de9e0ea7SMilanka Ringwald case 1: 1042135d9718SMilanka Ringwald if (hfp_ag_can_activate_voice_recognition(hfp_connection)){ 1043fd4151d1SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED; 1044fd4151d1SMilanka Ringwald done = hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1045fd4151d1SMilanka Ringwald } 1046de9e0ea7SMilanka Ringwald break; 1047de9e0ea7SMilanka Ringwald 1048de9e0ea7SMilanka Ringwald case 2: 1049135d9718SMilanka Ringwald if (hfp_ag_voice_recognition_session_active(hfp_connection)){ 1050de9e0ea7SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 1051de9e0ea7SMilanka Ringwald done = hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1052de9e0ea7SMilanka Ringwald } 1053de9e0ea7SMilanka Ringwald break; 1054de9e0ea7SMilanka Ringwald default: 1055de9e0ea7SMilanka Ringwald break; 1056fd4151d1SMilanka Ringwald } 1057fd4151d1SMilanka Ringwald if (done == 0){ 1058fd4151d1SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 1059fd4151d1SMilanka Ringwald done = hfp_ag_send_error(hfp_connection->rfcomm_cid); 1060fd4151d1SMilanka Ringwald return 1; 1061fd4151d1SMilanka Ringwald } 1062754910caSMilanka Ringwald 1063774e3016SMatthias Ringwald status = hfp_ag_vra_state_machine_two(hfp_connection); 10642cc52945SMatthias Ringwald return (status == ERROR_CODE_SUCCESS) ? 1 : 0; 1065de9e0ea7SMilanka Ringwald } 10662cc52945SMatthias Ringwald 10672cc52945SMatthias Ringwald return 0; 1068754910caSMilanka Ringwald } 1069754910caSMilanka Ringwald 1070a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){ 1071dc6af379SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) { 1072dc6af379SMatthias Ringwald return 0; 1073dc6af379SMatthias Ringwald } 1074dc6af379SMatthias Ringwald 1075c04cf7ffSMilanka Ringwald int sent = codecs_exchange_state_machine(hfp_connection); 1076c04cf7ffSMilanka Ringwald if (sent) return 1; 1077aa4dd815SMatthias Ringwald 107840a8ee13SMatthias Ringwald if (has_in_band_ring_tone()){ 107940a8ee13SMatthias Ringwald if (hfp_connection->ag_in_band_ring_tone_active != hfp_ag_in_band_ring_tone_active) { 108040a8ee13SMatthias Ringwald hfp_connection->ag_in_band_ring_tone_active = hfp_ag_in_band_ring_tone_active; 1081f197e761SMatthias Ringwald hfp_ag_send_change_in_band_ring_tone_setting_cmd(hfp_connection); 1082aa4dd815SMatthias Ringwald return 1; 10839430c71eSMatthias Ringwald } 108440a8ee13SMatthias Ringwald } 10859430c71eSMatthias Ringwald 10869430c71eSMatthias Ringwald switch(hfp_connection->command){ 1087aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1088485ac19eSMilanka Ringwald hfp_ag_send_report_network_operator_name_cmd(hfp_connection->rfcomm_cid, hfp_connection->network_operator); 1089aa4dd815SMatthias Ringwald return 1; 1090aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 1091a0ffb263SMatthias Ringwald if (hfp_connection->network_operator.format != 0){ 1092485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 1093aa4dd815SMatthias Ringwald } else { 1094485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 10953deb3ec6SMatthias Ringwald } 1096aa4dd815SMatthias Ringwald return 1; 1097aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE: 1098485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1099aa4dd815SMatthias Ringwald return 1; 11003deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR: 1101a0ffb263SMatthias Ringwald if (hfp_connection->extended_audio_gateway_error){ 1102a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 1103485ac19eSMilanka Ringwald hfp_ag_send_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, hfp_connection->extended_audio_gateway_error_value); 1104aa4dd815SMatthias Ringwald return 1; 11053deb3ec6SMatthias Ringwald } 1106202c8a4cSMatthias Ringwald break; 11073deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 1108485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1109ce263fc8SMatthias Ringwald return 1; 11103deb3ec6SMatthias Ringwald default: 11113deb3ec6SMatthias Ringwald break; 11123deb3ec6SMatthias Ringwald } 1113aa4dd815SMatthias Ringwald return 0; 11143deb3ec6SMatthias Ringwald } 11153deb3ec6SMatthias Ringwald 1116a0ffb263SMatthias Ringwald static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){ 1117505f1c30SMatthias Ringwald if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) || 1118505f1c30SMatthias Ringwald (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0; 11193deb3ec6SMatthias Ringwald 1120a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 112152cbdd6dSMilanka Ringwald 112252cbdd6dSMilanka Ringwald if (hfp_connection->release_audio_connection){ 112352cbdd6dSMilanka Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 112452cbdd6dSMilanka Ringwald hfp_connection->release_audio_connection = 0; 112552cbdd6dSMilanka Ringwald gap_disconnect(hfp_connection->sco_handle); 112652cbdd6dSMilanka Ringwald return 1; 112752cbdd6dSMilanka Ringwald } 1128aa4dd815SMatthias Ringwald 112929cddf58SMatthias Ringwald // accept incoming audio connection (codec negotiation is not used) 1130447743f7SMatthias Ringwald if (hfp_connection->accept_sco && (hfp_sco_setup_active() == false)){ 113129cddf58SMatthias Ringwald // notify about codec selection if not done already 113229cddf58SMatthias Ringwald if (hfp_connection->negotiated_codec == 0){ 113329cddf58SMatthias Ringwald hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 113429cddf58SMatthias Ringwald } 113529cddf58SMatthias Ringwald // 113629cddf58SMatthias Ringwald bool incoming_eSCO = hfp_connection->accept_sco == 2; 113729cddf58SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 113829cddf58SMatthias Ringwald hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO); 113929cddf58SMatthias Ringwald return 1; 114029cddf58SMatthias Ringwald } 114129cddf58SMatthias Ringwald 1142aa4dd815SMatthias Ringwald // run codecs exchange 1143c04cf7ffSMilanka Ringwald int sent = codecs_exchange_state_machine(hfp_connection); 1144c04cf7ffSMilanka Ringwald if (sent) return 1; 1145aa4dd815SMatthias Ringwald 1146c04cf7ffSMilanka Ringwald if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0; 1147447743f7SMatthias Ringwald if (hfp_sco_setup_active()) return 0; 1148d2e34ffbSMatthias Ringwald if (hci_can_send_command_packet_now() == false) return 0; 1149a0ffb263SMatthias Ringwald if (hfp_connection->establish_audio_connection){ 1150a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 1151a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 1152eddcd308SMatthias Ringwald hfp_setup_synchronous_connection(hfp_connection); 1153aa4dd815SMatthias Ringwald return 1; 1154aa4dd815SMatthias Ringwald } 1155aa4dd815SMatthias Ringwald return 0; 1156aa4dd815SMatthias Ringwald } 1157aa4dd815SMatthias Ringwald 1158056efd45SMatthias Ringwald static void hfp_ag_set_callsetup_indicator(void){ 1159056efd45SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup"); 1160056efd45SMatthias Ringwald if (!indicator){ 1161056efd45SMatthias Ringwald log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing"); 1162056efd45SMatthias Ringwald return; 1163056efd45SMatthias Ringwald }; 1164056efd45SMatthias Ringwald indicator->status = hfp_gsm_callsetup_status(); 1165056efd45SMatthias Ringwald } 1166056efd45SMatthias Ringwald 1167056efd45SMatthias Ringwald static void hfp_ag_set_callheld_indicator(void){ 1168056efd45SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld"); 1169056efd45SMatthias Ringwald if (!indicator){ 1170056efd45SMatthias Ringwald log_error("hfp_ag_set_callheld_state: callheld indicator is missing"); 1171056efd45SMatthias Ringwald return; 1172056efd45SMatthias Ringwald }; 1173056efd45SMatthias Ringwald indicator->status = hfp_gsm_callheld_status(); 1174056efd45SMatthias Ringwald } 1175056efd45SMatthias Ringwald 1176aa4dd815SMatthias Ringwald // 1177fcd59412SMilanka Ringwald // transition implementations for hfp_ag_call_state_machine 1178aa4dd815SMatthias Ringwald // 1179aa4dd815SMatthias Ringwald 11802ff1d7d7SMilanka Ringwald static void hfp_ag_hf_start_ringing_incoming(hfp_connection_t * hfp_connection){ 1181f197e761SMatthias Ringwald if (use_in_band_tone(hfp_connection)){ 1182a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING; 1183d97d752dSMilanka Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 1184aa4dd815SMatthias Ringwald } else { 11859493406bSMilanka Ringwald hfp_connection->call_state = HFP_CALL_INCOMING_RINGING; 1186aa4dd815SMatthias Ringwald } 1187aa4dd815SMatthias Ringwald } 1188aa4dd815SMatthias Ringwald 1189fcd59412SMilanka Ringwald static void hfp_ag_hf_start_ringing_outgoing(hfp_connection_t * hfp_connection){ 1190fcd59412SMilanka Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING; 1191fcd59412SMilanka Ringwald } 1192fcd59412SMilanka Ringwald 1193a0ffb263SMatthias Ringwald static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){ 1194a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 0; 1195a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = 0; 1196aa4dd815SMatthias Ringwald } 1197aa4dd815SMatthias Ringwald 1198a4f85bd2SMilanka Ringwald 119931f55b91SMilanka Ringwald static void hfp_ag_trigger_incoming_call_during_active_one(void){ 120031f55b91SMilanka Ringwald btstack_linked_list_iterator_t it; 120131f55b91SMilanka Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 120231f55b91SMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 120331f55b91SMilanka Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 120431f55b91SMilanka Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 120531f55b91SMilanka Ringwald if (hfp_connection->call_state != HFP_CALL_ACTIVE) continue; 120631f55b91SMilanka Ringwald 120731f55b91SMilanka Ringwald hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING; 120831f55b91SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 120931f55b91SMilanka Ringwald } 121031f55b91SMilanka Ringwald } 121131f55b91SMilanka Ringwald 1212d3709f3eSMatthias Ringwald static void hfp_ag_trigger_ring_and_clip(void) { 1213d3709f3eSMatthias Ringwald btstack_linked_list_iterator_t it; 1214d3709f3eSMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1215d3709f3eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1216d3709f3eSMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1217d3709f3eSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1218d3709f3eSMatthias Ringwald switch (hfp_connection->call_state){ 1219d3709f3eSMatthias Ringwald case HFP_CALL_INCOMING_RINGING: 1220d3709f3eSMatthias Ringwald case HFP_CALL_OUTGOING_RINGING: 1221d3709f3eSMatthias Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING: 12223561686eSMatthias Ringwald // Queue RING on this connection 1223a4f85bd2SMilanka Ringwald hfp_connection->ag_ring = 1; 12243561686eSMatthias Ringwald 12253561686eSMatthias Ringwald // HFP v1.8, 4.23 Calling Line Identification (CLI) Notification 12263561686eSMatthias Ringwald // "If the calling subscriber number information is available from the network, the AG shall issue the 12273561686eSMatthias Ringwald // +CLIP unsolicited result code just after every RING indication when the HF is alerted in an incoming call." 12283561686eSMatthias Ringwald hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled; 12293561686eSMatthias Ringwald 12303561686eSMatthias Ringwald // trigger next message 1231a4f85bd2SMilanka Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 1232b7e27351SMilanka Ringwald break; 1233b7e27351SMilanka Ringwald default: 1234b7e27351SMilanka Ringwald break; 1235b7e27351SMilanka Ringwald } 1236a4f85bd2SMilanka Ringwald } 1237a4f85bd2SMilanka Ringwald } 1238a4f85bd2SMilanka Ringwald 123950b9afa4SMilanka Ringwald // trigger RING and CLIP messages on connections that are ringing 124050b9afa4SMilanka Ringwald static void hfp_ag_ring_timeout_handler(btstack_timer_source_t * timer){ 124150b9afa4SMilanka Ringwald hfp_ag_trigger_ring_and_clip(); 124250b9afa4SMilanka Ringwald 12436db648bcSMilanka Ringwald btstack_run_loop_set_timer(timer, HFP_RING_PERIOD_MS); // 2 seconds timeout 124450b9afa4SMilanka Ringwald btstack_run_loop_add_timer(timer); 1245a4f85bd2SMilanka Ringwald } 1246a4f85bd2SMilanka Ringwald 1247a4f85bd2SMilanka Ringwald static void hfp_ag_ring_timeout_stop(void){ 1248a4f85bd2SMilanka Ringwald btstack_run_loop_remove_timer(&hfp_ag_ring_timeout); 1249a4f85bd2SMilanka Ringwald } 1250a4f85bd2SMilanka Ringwald 1251a4f85bd2SMilanka Ringwald static void hfp_ag_start_ringing(void){ 125250b9afa4SMilanka Ringwald // setup ring timer 125350b9afa4SMilanka Ringwald btstack_run_loop_remove_timer(&hfp_ag_ring_timeout); 125450b9afa4SMilanka Ringwald btstack_run_loop_set_timer_handler(&hfp_ag_ring_timeout, hfp_ag_ring_timeout_handler); 12556db648bcSMilanka Ringwald btstack_run_loop_set_timer(&hfp_ag_ring_timeout, HFP_RING_PERIOD_MS); // 2 seconds timeout 125650b9afa4SMilanka Ringwald btstack_run_loop_add_timer(&hfp_ag_ring_timeout); 125750b9afa4SMilanka Ringwald 125850b9afa4SMilanka Ringwald // emit start ringing 1259a4f85bd2SMilanka Ringwald hfp_ag_emit_general_simple_event(HFP_SUBEVENT_START_RINGING); 126050b9afa4SMilanka Ringwald 126150b9afa4SMilanka Ringwald // send initial RING + CLIP 126250b9afa4SMilanka Ringwald hfp_ag_trigger_ring_and_clip(); 1263a4f85bd2SMilanka Ringwald } 1264a4f85bd2SMilanka Ringwald 1265a4f85bd2SMilanka Ringwald static void hfp_ag_stop_ringing(void){ 1266a4f85bd2SMilanka Ringwald hfp_ag_ring_timeout_stop(); 1267a4f85bd2SMilanka Ringwald hfp_ag_emit_general_simple_event(HFP_SUBEVENT_STOP_RINGING); 1268a4f85bd2SMilanka Ringwald 1269a4f85bd2SMilanka Ringwald btstack_linked_list_iterator_t it; 1270a4f85bd2SMilanka Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1271a4f85bd2SMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1272a4f85bd2SMilanka Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1273a4f85bd2SMilanka Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1274a4f85bd2SMilanka Ringwald if ((hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) && 1275a4f85bd2SMilanka Ringwald (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue; 1276a4f85bd2SMilanka Ringwald hfp_ag_hf_stop_ringing(hfp_connection); 1277a4f85bd2SMilanka Ringwald } 1278a4f85bd2SMilanka Ringwald } 1279a4f85bd2SMilanka Ringwald 128031f55b91SMilanka Ringwald static void hfp_ag_trigger_incoming_call_idle(void){ 1281aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name("callsetup"); 1282aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 1283aa4dd815SMatthias Ringwald 12843561686eSMatthias Ringwald // update callsetup state in connections 1285665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1286665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1287665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1288a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 128966c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 129031f55b91SMilanka Ringwald if (hfp_connection->call_state != HFP_CALL_IDLE) continue; 129131f55b91SMilanka Ringwald 1292a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1293a4f85bd2SMilanka Ringwald 1294a4f85bd2SMilanka Ringwald hfp_ag_hf_start_ringing_incoming(hfp_connection); 1295a4f85bd2SMilanka Ringwald 1296f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1297aa4dd815SMatthias Ringwald } 12983561686eSMatthias Ringwald 12993561686eSMatthias Ringwald // start local ringing - started after connection state has been updated 13003561686eSMatthias Ringwald hfp_ag_start_ringing(); 1301aa4dd815SMatthias Ringwald } 1302aa4dd815SMatthias Ringwald 1303fe899794SMatthias Ringwald static void hfp_ag_trigger_outgoing_call(void){ 1304fe899794SMatthias Ringwald btstack_linked_list_iterator_t it; 1305fe899794SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1306fe899794SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1307fe899794SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1308fe899794SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1309fe899794SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 1310fe899794SMatthias Ringwald if (hfp_connection->call_state == HFP_CALL_IDLE){ 1311fe899794SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 1312fe899794SMatthias Ringwald } 1313fe899794SMatthias Ringwald } 1314fe899794SMatthias Ringwald } 13155a7033e2SMatthias Ringwald 13165a7033e2SMatthias Ringwald static void hfp_ag_handle_outgoing_call_accepted(hfp_connection_t * hfp_connection){ 13175a7033e2SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING; 13185a7033e2SMatthias Ringwald 13195a7033e2SMatthias Ringwald // trigger callsetup to be 13205a7033e2SMatthias Ringwald int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT; 13215a7033e2SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_ACCEPTED, 0, 0, NULL); 13225a7033e2SMatthias Ringwald 13235a7033e2SMatthias Ringwald int indicator_index ; 13245a7033e2SMatthias Ringwald 13255a7033e2SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 13265a7033e2SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callsetup"); 13275a7033e2SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 13285a7033e2SMatthias Ringwald 13295a7033e2SMatthias Ringwald // put current call on hold if active 13305a7033e2SMatthias Ringwald if (put_call_on_hold){ 13315a7033e2SMatthias Ringwald log_info("AG putting current call on hold for new outgoing call"); 13325a7033e2SMatthias Ringwald hfp_ag_set_callheld_indicator(); 13335a7033e2SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callheld"); 13345a7033e2SMatthias Ringwald hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]); 13355a7033e2SMatthias Ringwald } 13365a7033e2SMatthias Ringwald 13375a7033e2SMatthias Ringwald // start audio if needed 13385a7033e2SMatthias Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 13395a7033e2SMatthias Ringwald } 1340fe899794SMatthias Ringwald 13414a2e058fSMatthias Ringwald static void hfp_ag_transfer_indicator(const char * name){ 13424a2e058fSMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name(name); 1343aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 1344aa4dd815SMatthias Ringwald 1345665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1346665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1347665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1348a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 134966c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1350a0ffb263SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 1351a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1352f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1353aa4dd815SMatthias Ringwald } 1354aa4dd815SMatthias Ringwald } 1355aa4dd815SMatthias Ringwald 13564a2e058fSMatthias Ringwald static void hfp_ag_transfer_callsetup_state(void){ 13574a2e058fSMatthias Ringwald hfp_ag_transfer_indicator("callsetup"); 13584a2e058fSMatthias Ringwald } 13594a2e058fSMatthias Ringwald 1360aa4dd815SMatthias Ringwald static void hfp_ag_transfer_call_state(void){ 13614a2e058fSMatthias Ringwald hfp_ag_transfer_indicator("call"); 1362aa4dd815SMatthias Ringwald } 1363aa4dd815SMatthias Ringwald 1364aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callheld_state(void){ 13654a2e058fSMatthias Ringwald hfp_ag_transfer_indicator("callheld"); 1366aa4dd815SMatthias Ringwald } 1367aa4dd815SMatthias Ringwald 1368aa4dd815SMatthias Ringwald static void hfp_ag_hf_accept_call(hfp_connection_t * source){ 1369aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 1370aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1371aa4dd815SMatthias Ringwald 1372a4f85bd2SMilanka Ringwald hfp_ag_stop_ringing(); 1373a4f85bd2SMilanka Ringwald 1374665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1375665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1376665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1377a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 137866c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 13799493406bSMilanka Ringwald if ((hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) && 1380505f1c30SMatthias Ringwald (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue; 1381aa4dd815SMatthias Ringwald 1382a0ffb263SMatthias Ringwald if (hfp_connection == source){ 1383aa4dd815SMatthias Ringwald 1384f197e761SMatthias Ringwald if (use_in_band_tone(hfp_connection)){ 1385a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1386aa4dd815SMatthias Ringwald } else { 1387a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE; 1388d97d752dSMilanka Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 1389aa4dd815SMatthias Ringwald } 1390aa4dd815SMatthias Ringwald 1391a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1392a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1393aa4dd815SMatthias Ringwald 1394aa4dd815SMatthias Ringwald } else { 1395a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 1396aa4dd815SMatthias Ringwald } 1397f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1398aa4dd815SMatthias Ringwald } 1399aa4dd815SMatthias Ringwald } 1400aa4dd815SMatthias Ringwald 1401aa4dd815SMatthias Ringwald static void hfp_ag_ag_accept_call(void){ 1402aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 1403aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1404aa4dd815SMatthias Ringwald 1405a4f85bd2SMilanka Ringwald hfp_ag_stop_ringing(); 1406a4f85bd2SMilanka Ringwald 1407665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1408665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1409665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1410a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 141166c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1412a5764cd2SMatthias Ringwald if ((hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) && 1413a5764cd2SMatthias Ringwald (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue; 1414aa4dd815SMatthias Ringwald 1415a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION; 1416aa4dd815SMatthias Ringwald 1417a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1418a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1419aa4dd815SMatthias Ringwald 1420f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1421aa4dd815SMatthias Ringwald break; // only single 1422aa4dd815SMatthias Ringwald } 1423aa4dd815SMatthias Ringwald } 1424aa4dd815SMatthias Ringwald 1425aa4dd815SMatthias Ringwald static void hfp_ag_trigger_reject_call(void){ 1426aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1427a4f85bd2SMilanka Ringwald 1428a4f85bd2SMilanka Ringwald hfp_ag_stop_ringing(); 1429a4f85bd2SMilanka Ringwald 1430665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1431665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1432665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1433665d90f2SMatthias Ringwald hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 143466c5995fSMatthias Ringwald if (connection->local_role != HFP_ROLE_AG) continue; 143507a44c5eSMilanka Ringwald 143607a44c5eSMilanka Ringwald switch (connection->call_state){ 143707a44c5eSMilanka Ringwald case HFP_CALL_INCOMING_RINGING: 143807a44c5eSMilanka Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING: 143907a44c5eSMilanka Ringwald case HFP_CALL_OUTGOING_RINGING: 144007a44c5eSMilanka Ringwald case HFP_CALL_OUTGOING_INITIATED: 144107a44c5eSMilanka Ringwald case HFP_CALL_OUTGOING_DIALING: 144207a44c5eSMilanka Ringwald break; 144307a44c5eSMilanka Ringwald default: 144407a44c5eSMilanka Ringwald continue; 144507a44c5eSMilanka Ringwald } 1446095a9f2bSMilanka Ringwald 1447aa4dd815SMatthias Ringwald connection->call_state = HFP_CALL_IDLE; 1448095a9f2bSMilanka Ringwald connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1449095a9f2bSMilanka Ringwald 1450095a9f2bSMilanka Ringwald if (connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 1451095a9f2bSMilanka Ringwald connection->release_audio_connection = 1; 1452095a9f2bSMilanka Ringwald } 1453095a9f2bSMilanka Ringwald hfp_emit_simple_event(connection, HFP_SUBEVENT_CALL_TERMINATED); 1454f0aeb307SMatthias Ringwald hfp_ag_run_for_context(connection); 1455095a9f2bSMilanka Ringwald rfcomm_request_can_send_now_event(connection->rfcomm_cid); 1456aa4dd815SMatthias Ringwald } 1457aa4dd815SMatthias Ringwald } 1458aa4dd815SMatthias Ringwald 1459aa4dd815SMatthias Ringwald static void hfp_ag_trigger_terminate_call(void){ 1460aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 1461aa4dd815SMatthias Ringwald 1462a4f85bd2SMilanka Ringwald // no ringing during call 1463a4f85bd2SMilanka Ringwald 1464665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1465665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1466665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1467a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 146866c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1469a0ffb263SMatthias Ringwald if (hfp_connection->call_state == HFP_CALL_IDLE) continue; 1470fb75b483SMilanka Ringwald 1471a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 1472a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1473095a9f2bSMilanka Ringwald 14746d78145cSMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 1475a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 1; 14766d78145cSMatthias Ringwald } 1477095a9f2bSMilanka Ringwald 1478fb75b483SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED); 1479f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1480a13ff7d1SMilanka Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 1481aa4dd815SMatthias Ringwald } 1482aa4dd815SMatthias Ringwald } 1483aa4dd815SMatthias Ringwald 14840cb5b971SMatthias Ringwald static void hfp_ag_set_call_indicator(void){ 1485aa4dd815SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call"); 1486aa4dd815SMatthias Ringwald if (!indicator){ 1487aa4dd815SMatthias Ringwald log_error("hfp_ag_set_call_state: call indicator is missing"); 148845718b6fSMatthias Ringwald return; 1489aa4dd815SMatthias Ringwald }; 1490d210d9c4SMatthias Ringwald indicator->status = hfp_gsm_call_status(); 1491aa4dd815SMatthias Ringwald } 1492aa4dd815SMatthias Ringwald 1493aa4dd815SMatthias Ringwald static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){ 1494665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1495665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1496665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1497a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 149866c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1499a0ffb263SMatthias Ringwald if (hfp_connection->call_state == call_state) return hfp_connection; 1500aa4dd815SMatthias Ringwald } 1501aa4dd815SMatthias Ringwald return NULL; 1502aa4dd815SMatthias Ringwald } 1503aa4dd815SMatthias Ringwald 1504a5bdcda8SMatthias Ringwald static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){ 1505665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1506665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1507665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1508a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 150966c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1510a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = state + 1; 1511ce263fc8SMatthias Ringwald } 1512ce263fc8SMatthias Ringwald } 1513ce263fc8SMatthias Ringwald 1514a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 1515aa4dd815SMatthias Ringwald int indicator_index; 1516a0ffb263SMatthias Ringwald switch (hfp_connection->call_state){ 1517aa4dd815SMatthias Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING: 1518a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 1519a0ffb263SMatthias Ringwald // we got event: audio hfp_connection established 15209493406bSMilanka Ringwald hfp_connection->call_state = HFP_CALL_INCOMING_RINGING; 1521aa4dd815SMatthias Ringwald break; 1522aa4dd815SMatthias Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE: 1523a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 1524a0ffb263SMatthias Ringwald // we got event: audio hfp_connection established 1525a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1526aa4dd815SMatthias Ringwald break; 1527aa4dd815SMatthias Ringwald case HFP_CALL_W2_SEND_CALL_WAITING: 1528a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_CHLD; 1529a0ffb263SMatthias Ringwald hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid); 1530aa4dd815SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callsetup"); 1531a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1532aa4dd815SMatthias Ringwald break; 1533aa4dd815SMatthias Ringwald default: 1534aa4dd815SMatthias Ringwald break; 1535aa4dd815SMatthias Ringwald } 1536aa4dd815SMatthias Ringwald return 0; 1537aa4dd815SMatthias Ringwald } 153839426febSMatthias Ringwald 153939426febSMatthias Ringwald // functions extracted from hfp_ag_call_sm below 15409f9621cfSMatthias Ringwald static void hfp_ag_handle_reject_outgoing_call(void){ 154139426febSMatthias Ringwald hfp_connection_t * hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 154239426febSMatthias Ringwald if (!hfp_connection){ 154339426febSMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state"); 154439426febSMatthias Ringwald return; 154539426febSMatthias Ringwald } 154639426febSMatthias Ringwald 1547f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_REJECTED, 0, 0, NULL); 154839426febSMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 154939426febSMatthias Ringwald hfp_connection->send_error = 1; 155039426febSMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 155139426febSMatthias Ringwald } 155239426febSMatthias Ringwald 1553a0ffb263SMatthias Ringwald // hfp_connection is used to identify originating HF 1554a0ffb263SMatthias Ringwald static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){ 155581e25d0aSMatthias Ringwald 155681e25d0aSMatthias Ringwald // allow to intercept call statemachine events 155781e25d0aSMatthias Ringwald if (hfp_ag_custom_call_sm_handler != NULL){ 155881e25d0aSMatthias Ringwald bool handle_event = (*hfp_ag_custom_call_sm_handler)(event); 155981e25d0aSMatthias Ringwald if (!handle_event) return; 156081e25d0aSMatthias Ringwald } 156181e25d0aSMatthias Ringwald 1562d210d9c4SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1563d210d9c4SMatthias Ringwald int callheld_indicator_index = get_ag_indicator_index_for_name("callheld"); 1564d210d9c4SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 156574386ee0SMatthias Ringwald 1566aa4dd815SMatthias Ringwald switch (event){ 1567aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL: 1568d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1569aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1570d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 157112cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1572f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL, 0, 0, NULL); 1573d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 157431f55b91SMilanka Ringwald hfp_ag_trigger_incoming_call_idle(); 157560ebb071SMilanka Ringwald log_info("AG rings"); 1576aa4dd815SMatthias Ringwald break; 1577aa4dd815SMatthias Ringwald default: 15783deb3ec6SMatthias Ringwald break; 15793deb3ec6SMatthias Ringwald } 15803deb3ec6SMatthias Ringwald break; 1581aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1582d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 158312cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1584f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL, 0, 0, NULL); 1585d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 158631f55b91SMilanka Ringwald hfp_ag_trigger_incoming_call_during_active_one(); 158760ebb071SMilanka Ringwald log_info("AG call waiting"); 1588aa4dd815SMatthias Ringwald break; 1589aa4dd815SMatthias Ringwald default: 15903deb3ec6SMatthias Ringwald break; 15913deb3ec6SMatthias Ringwald } 15923deb3ec6SMatthias Ringwald break; 15937bbeb3adSMilanka Ringwald default: 15947bbeb3adSMilanka Ringwald break; 1595aa4dd815SMatthias Ringwald } 1596aa4dd815SMatthias Ringwald break; 1597aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG: 1598d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1599aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1600d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 160112cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1602f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, 0, 0, NULL); 1603d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1604d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1605aa4dd815SMatthias Ringwald hfp_ag_ag_accept_call(); 160660ebb071SMilanka Ringwald log_info("AG answers call, accept call by GSM"); 1607aa4dd815SMatthias Ringwald break; 1608aa4dd815SMatthias Ringwald default: 16093deb3ec6SMatthias Ringwald break; 16103deb3ec6SMatthias Ringwald } 1611aa4dd815SMatthias Ringwald break; 1612aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1613d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1614aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 161560ebb071SMilanka Ringwald log_info("AG: current call is placed on hold, incoming call gets active"); 1616f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, 0, 0, NULL); 1617d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1618d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1619ce263fc8SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1620ce263fc8SMatthias Ringwald hfp_ag_transfer_callheld_state(); 1621aa4dd815SMatthias Ringwald break; 1622aa4dd815SMatthias Ringwald default: 1623aa4dd815SMatthias Ringwald break; 1624aa4dd815SMatthias Ringwald } 1625aa4dd815SMatthias Ringwald break; 16267bbeb3adSMilanka Ringwald default: 16277bbeb3adSMilanka Ringwald break; 1628aa4dd815SMatthias Ringwald } 1629aa4dd815SMatthias Ringwald break; 1630ce263fc8SMatthias Ringwald 1631ce263fc8SMatthias Ringwald case HFP_AG_HELD_CALL_JOINED_BY_AG: 1632d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1633ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1634d0c20769SMatthias Ringwald switch (hfp_gsm_callheld_status()){ 1635ce263fc8SMatthias Ringwald case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED: 163660ebb071SMilanka Ringwald log_info("AG: joining held call with active call"); 1637f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_HELD_CALL_JOINED_BY_AG, 0, 0, NULL); 1638d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1639ce263fc8SMatthias Ringwald hfp_ag_transfer_callheld_state(); 1640ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL); 1641ce263fc8SMatthias Ringwald break; 1642ce263fc8SMatthias Ringwald default: 1643ce263fc8SMatthias Ringwald break; 1644ce263fc8SMatthias Ringwald } 1645ce263fc8SMatthias Ringwald break; 1646ce263fc8SMatthias Ringwald default: 1647ce263fc8SMatthias Ringwald break; 1648ce263fc8SMatthias Ringwald } 1649ce263fc8SMatthias Ringwald break; 1650ce263fc8SMatthias Ringwald 1651aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF: 1652d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1653aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1654d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 165512cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1656f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, 0, 0, NULL); 1657d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1658d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1659a0ffb263SMatthias Ringwald hfp_ag_hf_accept_call(hfp_connection); 16606323f788SMatthias Ringwald hfp_connection->ok_pending = 1; 166160ebb071SMilanka Ringwald log_info("HF answers call, accept call by GSM"); 1662ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED); 1663aa4dd815SMatthias Ringwald break; 1664aa4dd815SMatthias Ringwald default: 1665aa4dd815SMatthias Ringwald break; 1666aa4dd815SMatthias Ringwald } 16673deb3ec6SMatthias Ringwald break; 16683deb3ec6SMatthias Ringwald default: 16693deb3ec6SMatthias Ringwald break; 16703deb3ec6SMatthias Ringwald } 16713deb3ec6SMatthias Ringwald break; 16723deb3ec6SMatthias Ringwald 1673ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG: 1674d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1675ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1676d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 167712cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1678f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, 0, 0, NULL); 167927bb1817SMatthias Ringwald hfp_ag_response_and_hold_active = true; 1680ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1681a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1682ac6f828eSMilanka Ringwald // as with regular call 1683d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1684d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1685ce263fc8SMatthias Ringwald hfp_ag_ag_accept_call(); 168660ebb071SMilanka Ringwald log_info("AG response and hold - hold by AG"); 1687ce263fc8SMatthias Ringwald break; 1688ce263fc8SMatthias Ringwald default: 1689ce263fc8SMatthias Ringwald break; 1690ce263fc8SMatthias Ringwald } 1691ce263fc8SMatthias Ringwald break; 1692ce263fc8SMatthias Ringwald default: 1693ce263fc8SMatthias Ringwald break; 1694ce263fc8SMatthias Ringwald } 1695ce263fc8SMatthias Ringwald break; 1696ce263fc8SMatthias Ringwald 1697ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF: 1698d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1699ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1700d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 170112cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1702f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, 0, 0, NULL); 170327bb1817SMatthias Ringwald hfp_ag_response_and_hold_active = true; 1704ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1705a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1706ac6f828eSMilanka Ringwald // as with regular call 1707d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1708d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1709a0ffb263SMatthias Ringwald hfp_ag_hf_accept_call(hfp_connection); 171060ebb071SMilanka Ringwald log_info("AG response and hold - hold by HF"); 1711ce263fc8SMatthias Ringwald break; 1712ce263fc8SMatthias Ringwald default: 1713ce263fc8SMatthias Ringwald break; 1714ce263fc8SMatthias Ringwald } 1715ce263fc8SMatthias Ringwald break; 1716ce263fc8SMatthias Ringwald default: 1717ce263fc8SMatthias Ringwald break; 1718ce263fc8SMatthias Ringwald } 1719ce263fc8SMatthias Ringwald break; 1720ce263fc8SMatthias Ringwald 1721ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG: 1722ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF: 1723ce263fc8SMatthias Ringwald if (!hfp_ag_response_and_hold_active) break; 1724ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1725f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, 0, 0, NULL); 172627bb1817SMatthias Ringwald hfp_ag_response_and_hold_active = false; 1727ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED; 1728a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 172960ebb071SMilanka Ringwald log_info("Held Call accepted and active"); 1730ce263fc8SMatthias Ringwald break; 1731ce263fc8SMatthias Ringwald 1732ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG: 1733ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF: 1734ce263fc8SMatthias Ringwald if (!hfp_ag_response_and_hold_active) break; 1735ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1736f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, 0, 0, NULL); 173727bb1817SMatthias Ringwald hfp_ag_response_and_hold_active = false; 1738ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1739a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1740ce263fc8SMatthias Ringwald // from terminate by ag 1741d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1742ce263fc8SMatthias Ringwald hfp_ag_trigger_terminate_call(); 1743ce263fc8SMatthias Ringwald break; 1744ce263fc8SMatthias Ringwald 1745aa4dd815SMatthias Ringwald case HFP_AG_TERMINATE_CALL_BY_HF: 1746d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1747aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1748d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 174912cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 175007a44c5eSMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 175107a44c5eSMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 1752f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL); 1753d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1754aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1755aa4dd815SMatthias Ringwald hfp_ag_trigger_reject_call(); 175660ebb071SMilanka Ringwald log_info("HF Rejected Incoming call, AG terminate call"); 1757aa4dd815SMatthias Ringwald break; 1758aa4dd815SMatthias Ringwald default: 1759aa4dd815SMatthias Ringwald break; 1760aa4dd815SMatthias Ringwald } 1761aa4dd815SMatthias Ringwald break; 176212cbbeeeSMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1763f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL); 17641ec112deSMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1765d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 17661ec112deSMatthias Ringwald hfp_ag_trigger_terminate_call(); 176760ebb071SMilanka Ringwald log_info("AG terminate call"); 1768aa4dd815SMatthias Ringwald break; 17697bbeb3adSMilanka Ringwald default: 17707bbeb3adSMilanka Ringwald break; 1771aa4dd815SMatthias Ringwald } 1772aa4dd815SMatthias Ringwald break; 17733deb3ec6SMatthias Ringwald 1774aa4dd815SMatthias Ringwald case HFP_AG_TERMINATE_CALL_BY_AG: 1775d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1776aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1777d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 177807a44c5eSMilanka Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 177907a44c5eSMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 178007a44c5eSMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1781f2aa99a9SMilanka Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1782f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_AG, 0, 0, NULL); 1783d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1784aa4dd815SMatthias Ringwald hfp_ag_trigger_reject_call(); 178560ebb071SMilanka Ringwald log_info("AG Rejected Incoming call, AG terminate call"); 1786aa4dd815SMatthias Ringwald break; 1787aa4dd815SMatthias Ringwald default: 1788aa4dd815SMatthias Ringwald break; 17893deb3ec6SMatthias Ringwald } 1790202c8a4cSMatthias Ringwald break; 179112cbbeeeSMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1792f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_AG, 0, 0, NULL); 1793d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1794d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1795aa4dd815SMatthias Ringwald hfp_ag_trigger_terminate_call(); 179660ebb071SMilanka Ringwald log_info("AG terminate call"); 1797aa4dd815SMatthias Ringwald break; 1798aa4dd815SMatthias Ringwald default: 17993deb3ec6SMatthias Ringwald break; 18003deb3ec6SMatthias Ringwald } 18013deb3ec6SMatthias Ringwald break; 1802aa4dd815SMatthias Ringwald case HFP_AG_CALL_DROPPED: 1803d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1804aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1805d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1806aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1807aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1808fb75b483SMilanka Ringwald hfp_ag_stop_ringing(); 1809fb75b483SMilanka Ringwald break; 1810fb75b483SMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 1811fb75b483SMilanka Ringwald log_info("Outgoing call interrupted\n"); 1812aa4dd815SMatthias Ringwald break; 1813aa4dd815SMatthias Ringwald default: 18143deb3ec6SMatthias Ringwald break; 18153deb3ec6SMatthias Ringwald } 1816f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL); 1817d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1818aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 181910c236bfSMatthias Ringwald hfp_ag_trigger_terminate_call(); 1820aa4dd815SMatthias Ringwald break; 1821aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1822ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_active) { 1823f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL); 1824ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1825a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1826ce263fc8SMatthias Ringwald } 1827f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL); 1828d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1829d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1830aa4dd815SMatthias Ringwald hfp_ag_trigger_terminate_call(); 183160ebb071SMilanka Ringwald log_info("AG notify call dropped"); 1832aa4dd815SMatthias Ringwald break; 1833aa4dd815SMatthias Ringwald default: 1834aa4dd815SMatthias Ringwald break; 1835aa4dd815SMatthias Ringwald } 1836aa4dd815SMatthias Ringwald break; 1837aa4dd815SMatthias Ringwald 18389ff73f41SMatthias Ringwald case HFP_AG_OUTGOING_CALL_INITIATED_BY_HF: 1839d210d9c4SMatthias Ringwald // directly reject call if number of free slots is exceeded 1840d210d9c4SMatthias Ringwald if (!hfp_gsm_call_possible()){ 1841a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1842f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1843d210d9c4SMatthias Ringwald break; 1844d210d9c4SMatthias Ringwald } 1845fe899794SMatthias Ringwald 1846fe899794SMatthias Ringwald // note: number not used currently 18479ff73f41SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_INITIATED_BY_HF, 0, 0, (const char *) &hfp_connection->line_buffer[3]); 18489cae807eSMatthias Ringwald 1849a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 185074386ee0SMatthias Ringwald 1851ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]); 1852aa4dd815SMatthias Ringwald break; 1853aa4dd815SMatthias Ringwald 1854fe899794SMatthias Ringwald case HFP_AG_OUTGOING_CALL_INITIATED_BY_AG: 1855fe899794SMatthias Ringwald // directly reject call if number of free slots is exceeded 1856fe899794SMatthias Ringwald if (!hfp_gsm_call_possible()) { 1857fe899794SMatthias Ringwald // TODO: no error for user 1858fe899794SMatthias Ringwald break; 1859fe899794SMatthias Ringwald } 1860fe899794SMatthias Ringwald switch (hfp_gsm_call_status()) { 1861fe899794SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1862fe899794SMatthias Ringwald switch (hfp_gsm_callsetup_status()) { 1863fe899794SMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1864fe899794SMatthias Ringwald // note: number not used currently 1865fe899794SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_INITIATED_BY_AG, 0, 0, "1234567"); 1866fe899794SMatthias Ringwald // init call state for all connections 1867fe899794SMatthias Ringwald hfp_ag_trigger_outgoing_call(); 1868fe899794SMatthias Ringwald // get first one and accept call 1869fe899794SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 1870fe899794SMatthias Ringwald if (hfp_connection) { 1871fe899794SMatthias Ringwald hfp_ag_handle_outgoing_call_accepted(hfp_connection); 1872fe899794SMatthias Ringwald } 1873fe899794SMatthias Ringwald break; 1874fe899794SMatthias Ringwald default: 1875fe899794SMatthias Ringwald // TODO: no error for user 1876fe899794SMatthias Ringwald break; 1877fe899794SMatthias Ringwald } 1878fe899794SMatthias Ringwald break; 1879fe899794SMatthias Ringwald default: 1880fe899794SMatthias Ringwald // TODO: no error for user 1881fe899794SMatthias Ringwald break; 1882fe899794SMatthias Ringwald } 1883fe899794SMatthias Ringwald break; 18849cae807eSMatthias Ringwald case HFP_AG_OUTGOING_REDIAL_INITIATED:{ 1885d210d9c4SMatthias Ringwald // directly reject call if number of free slots is exceeded 1886d210d9c4SMatthias Ringwald if (!hfp_gsm_call_possible()){ 1887a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1888f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1889d210d9c4SMatthias Ringwald break; 1890d210d9c4SMatthias Ringwald } 1891d210d9c4SMatthias Ringwald 1892f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_REDIAL_INITIATED, 0, 0, NULL); 1893a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 189474386ee0SMatthias Ringwald 189560ebb071SMilanka Ringwald log_info("Redial last number"); 18969cae807eSMatthias Ringwald char * last_dialed_number = hfp_gsm_last_dialed_number(); 1897aa4dd815SMatthias Ringwald 18989cae807eSMatthias Ringwald if (strlen(last_dialed_number) > 0){ 189960ebb071SMilanka Ringwald log_info("Last number exists: accept call"); 1900ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number); 19019cae807eSMatthias Ringwald } else { 190260ebb071SMilanka Ringwald log_info("log_infoLast number missing: reject call"); 19039f9621cfSMatthias Ringwald hfp_ag_handle_reject_outgoing_call(); 19049cae807eSMatthias Ringwald } 19059cae807eSMatthias Ringwald break; 19069cae807eSMatthias Ringwald } 1907aa4dd815SMatthias Ringwald case HFP_AG_OUTGOING_CALL_REJECTED: 19089f9621cfSMatthias Ringwald hfp_ag_handle_reject_outgoing_call(); 1909aa4dd815SMatthias Ringwald break; 1910aa4dd815SMatthias Ringwald 1911d210d9c4SMatthias Ringwald case HFP_AG_OUTGOING_CALL_ACCEPTED:{ 1912a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 1913a0ffb263SMatthias Ringwald if (!hfp_connection){ 1914a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state"); 1915aa4dd815SMatthias Ringwald break; 1916aa4dd815SMatthias Ringwald } 1917aa4dd815SMatthias Ringwald 1918a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 19195a7033e2SMatthias Ringwald hfp_ag_handle_outgoing_call_accepted(hfp_connection); 1920aa4dd815SMatthias Ringwald break; 1921d210d9c4SMatthias Ringwald } 1922aa4dd815SMatthias Ringwald case HFP_AG_OUTGOING_CALL_RINGING: 1923a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1924a0ffb263SMatthias Ringwald if (!hfp_connection){ 1925a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state"); 1926aa4dd815SMatthias Ringwald break; 1927aa4dd815SMatthias Ringwald } 1928d210d9c4SMatthias Ringwald 1929f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_RINGING, 0, 0, NULL); 1930a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING; 1931d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1932aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1933fcd59412SMilanka Ringwald hfp_ag_hf_start_ringing_outgoing(hfp_connection); 1934aa4dd815SMatthias Ringwald break; 1935aa4dd815SMatthias Ringwald 1936d210d9c4SMatthias Ringwald case HFP_AG_OUTGOING_CALL_ESTABLISHED:{ 1937aa4dd815SMatthias Ringwald // get outgoing call 1938a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING); 1939a0ffb263SMatthias Ringwald if (!hfp_connection){ 1940a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1941aa4dd815SMatthias Ringwald } 1942a0ffb263SMatthias Ringwald if (!hfp_connection){ 1943a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection"); 1944aa4dd815SMatthias Ringwald break; 1945aa4dd815SMatthias Ringwald } 1946d210d9c4SMatthias Ringwald 1947addc49c4SMilanka 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; 1948f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_ESTABLISHED, 0, 0, NULL); 1949a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1950d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1951d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1952aa4dd815SMatthias Ringwald hfp_ag_transfer_call_state(); 1953aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1954addc49c4SMilanka Ringwald if (callheld_status_call_on_hold_and_no_active_calls){ 1955d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1956aa4dd815SMatthias Ringwald hfp_ag_transfer_callheld_state(); 19573deb3ec6SMatthias Ringwald } 1958addc49c4SMilanka Ringwald hfp_ag_hf_stop_ringing(hfp_connection); 1959674ebed5SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED); 19603deb3ec6SMatthias Ringwald break; 1961d210d9c4SMatthias Ringwald } 1962d210d9c4SMatthias Ringwald 196312cbbeeeSMatthias Ringwald case HFP_AG_CALL_HOLD_USER_BUSY: 1964f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_USER_BUSY, 0, 0, NULL); 1965d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1966a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1967a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 196860ebb071SMilanka Ringwald log_info("AG: Call Waiting, User Busy"); 1969d210d9c4SMatthias Ringwald break; 1970d210d9c4SMatthias Ringwald 1971d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{ 1972d0c20769SMatthias Ringwald int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1973d0c20769SMatthias Ringwald int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD; 1974d210d9c4SMatthias Ringwald 1975d210d9c4SMatthias Ringwald // Releases all active calls (if any exist) and accepts the other (held or waiting) call. 1976d0c20769SMatthias Ringwald if (call_held || call_setup_in_progress){ 1977f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index, 1978f8737b81SMatthias Ringwald 0, NULL); 1979d0c20769SMatthias Ringwald 1980d0c20769SMatthias Ringwald } 1981d0c20769SMatthias Ringwald 1982d210d9c4SMatthias Ringwald if (call_setup_in_progress){ 198360ebb071SMilanka Ringwald log_info("AG: Call Dropped, Accept new call"); 1984d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1985a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1986d210d9c4SMatthias Ringwald } else { 198760ebb071SMilanka Ringwald log_info("AG: Call Dropped, Resume held call"); 1988d210d9c4SMatthias Ringwald } 1989d210d9c4SMatthias Ringwald if (call_held){ 1990d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1991a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1992d210d9c4SMatthias Ringwald } 1993d0c20769SMatthias Ringwald 1994a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1995d210d9c4SMatthias Ringwald break; 1996d210d9c4SMatthias Ringwald } 1997d0c20769SMatthias Ringwald 1998d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{ 1999d210d9c4SMatthias Ringwald // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call. 2000d210d9c4SMatthias Ringwald // only update if callsetup changed 2001d0c20769SMatthias Ringwald int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 2002f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index, 0, 2003f8737b81SMatthias Ringwald NULL); 2004d0c20769SMatthias Ringwald 2005d210d9c4SMatthias Ringwald if (call_setup_in_progress){ 200660ebb071SMilanka Ringwald log_info("AG: Call on Hold, Accept new call"); 2007d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 2008a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 2009d210d9c4SMatthias Ringwald } else { 201060ebb071SMilanka Ringwald log_info("AG: Swap calls"); 2011d210d9c4SMatthias Ringwald } 2012d0c20769SMatthias Ringwald 2013d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 2014d0c20769SMatthias Ringwald // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED); 2015a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 2016a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 2017d210d9c4SMatthias Ringwald break; 2018d210d9c4SMatthias Ringwald } 2019d0c20769SMatthias Ringwald 2020d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_ADD_HELD_CALL: 2021d210d9c4SMatthias Ringwald // Adds a held call to the conversation. 2022d0c20769SMatthias Ringwald if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){ 202360ebb071SMilanka Ringwald log_info("AG: Join 3-way-call"); 2024f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_ADD_HELD_CALL, 0, 0, NULL); 2025d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 2026a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 2027ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL); 2028d210d9c4SMatthias Ringwald } 2029a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 2030d210d9c4SMatthias Ringwald break; 2031d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS: 2032d210d9c4SMatthias Ringwald // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer) 2033f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, 0, 0, NULL); 203460ebb071SMilanka Ringwald log_info("AG: Transfer call -> Connect two calls and disconnect"); 2035d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 2036d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 2037a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 2038a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 2039a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 2040d210d9c4SMatthias Ringwald break; 2041ce263fc8SMatthias Ringwald 20423deb3ec6SMatthias Ringwald default: 20433deb3ec6SMatthias Ringwald break; 20443deb3ec6SMatthias Ringwald } 2045d210d9c4SMatthias Ringwald 2046d0c20769SMatthias Ringwald 20473deb3ec6SMatthias Ringwald } 20483deb3ec6SMatthias Ringwald 20499cae807eSMatthias Ringwald 2050a0ffb263SMatthias Ringwald static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){ 20519cae807eSMatthias Ringwald hfp_gsm_call_t * active_call = hfp_gsm_call(call_index); 20529cae807eSMatthias Ringwald if (!active_call) return; 20539cae807eSMatthias Ringwald 20549cae807eSMatthias Ringwald int idx = active_call->index; 20559cae807eSMatthias Ringwald hfp_enhanced_call_dir_t dir = active_call->direction; 20569cae807eSMatthias Ringwald hfp_enhanced_call_status_t status = active_call->enhanced_status; 20579cae807eSMatthias Ringwald hfp_enhanced_call_mode_t mode = active_call->mode; 20589cae807eSMatthias Ringwald hfp_enhanced_call_mpty_t mpty = active_call->mpty; 20599cae807eSMatthias Ringwald uint8_t type = active_call->clip_type; 20609cae807eSMatthias Ringwald char * number = active_call->clip_number; 20619cae807eSMatthias Ringwald 20629cae807eSMatthias Ringwald char buffer[100]; 20639cae807eSMatthias Ringwald // TODO: check length of a buffer, to fit the MTU 20649cae807eSMatthias Ringwald int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty); 20659cae807eSMatthias Ringwald if (number){ 20661cc1d9e9SMilanka Ringwald offset += snprintf(buffer+offset, sizeof(buffer)-offset-3, ", \"%s\",%u", number, type); 20679cae807eSMatthias Ringwald } 20689cae807eSMatthias Ringwald snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 206960ebb071SMilanka 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, 20709cae807eSMatthias Ringwald mode, mpty, type, number); 2071a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 20729cae807eSMatthias Ringwald } 20739cae807eSMatthias Ringwald 2074febc14f5SMatthias Ringwald // sends pending command, returns if command was sent 2075febc14f5SMatthias Ringwald static int hfp_ag_send_commands(hfp_connection_t *hfp_connection){ 2076a0ffb263SMatthias Ringwald if (hfp_connection->send_status_of_current_calls){ 2077a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 2078a0ffb263SMatthias Ringwald if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){ 2079a0ffb263SMatthias Ringwald hfp_connection->next_call_index++; 2080a0ffb263SMatthias Ringwald hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index); 2081febc14f5SMatthias Ringwald return 1; 20829cae807eSMatthias Ringwald } else { 2083febc14f5SMatthias 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 2084a0ffb263SMatthias Ringwald hfp_connection->next_call_index = 0; 2085a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2086a0ffb263SMatthias Ringwald hfp_connection->send_status_of_current_calls = 0; 20879cae807eSMatthias Ringwald } 2088ce263fc8SMatthias Ringwald } 2089ce263fc8SMatthias Ringwald 2090a0ffb263SMatthias Ringwald if (hfp_connection->ag_notify_incoming_call_waiting){ 2091a0ffb263SMatthias Ringwald hfp_connection->ag_notify_incoming_call_waiting = 0; 2092a0ffb263SMatthias Ringwald hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid); 2093febc14f5SMatthias Ringwald return 1; 2094aa4dd815SMatthias Ringwald } 2095aa4dd815SMatthias Ringwald 2096a0ffb263SMatthias Ringwald if (hfp_connection->send_error){ 2097a0ffb263SMatthias Ringwald hfp_connection->send_error = 0; 2098a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2099485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 2100febc14f5SMatthias Ringwald return 1; 2101aa4dd815SMatthias Ringwald } 2102aa4dd815SMatthias Ringwald 2103ce263fc8SMatthias Ringwald // note: before update AG indicators and ok_pending 2104a0ffb263SMatthias Ringwald if (hfp_connection->send_response_and_hold_status){ 2105a0ffb263SMatthias Ringwald int status = hfp_connection->send_response_and_hold_status - 1; 2106a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = 0; 2107485ac19eSMilanka Ringwald hfp_ag_send_set_response_and_hold(hfp_connection->rfcomm_cid, status); 2108febc14f5SMatthias Ringwald return 1; 2109ce263fc8SMatthias Ringwald } 2110ce263fc8SMatthias Ringwald 21115be6b47aSMatthias Ringwald // note: before ok_pending and send_error to allow for unsolicited result on custom command 21125be6b47aSMatthias Ringwald if (hfp_connection->send_custom_message != NULL){ 21135be6b47aSMatthias Ringwald const char * message = hfp_connection->send_custom_message; 21145be6b47aSMatthias Ringwald hfp_connection->send_custom_message = NULL; 21155be6b47aSMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, message); 21167f8f1191SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_CUSTOM_AT_MESSAGE_SENT, ERROR_CODE_SUCCESS); 21175be6b47aSMatthias Ringwald return 1; 21185be6b47aSMatthias Ringwald } 21195be6b47aSMatthias Ringwald 2120a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending){ 2121a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 2122a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2123485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 2124febc14f5SMatthias Ringwald return 1; 2125ce263fc8SMatthias Ringwald } 2126ce263fc8SMatthias Ringwald 212769640b04SMatthias Ringwald // update AG indicators only if enabled by AT+CMER=3,0,0,1 212869640b04SMatthias Ringwald if ((hfp_connection->enable_status_update_for_ag_indicators == 1) && (hfp_connection->ag_indicators_status_update_bitmap != 0)){ 212969640b04SMatthias Ringwald uint16_t i; 2130a0ffb263SMatthias Ringwald for (i=0;i<hfp_connection->ag_indicators_nr;i++){ 2131a0ffb263SMatthias Ringwald if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){ 2132a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0); 2133485ac19eSMilanka Ringwald hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]); 2134febc14f5SMatthias Ringwald return 1; 2135aa4dd815SMatthias Ringwald } 2136aa4dd815SMatthias Ringwald } 2137aa4dd815SMatthias Ringwald } 2138aa4dd815SMatthias Ringwald 2139245852b7SMilanka Ringwald if (hfp_connection->ag_send_no_carrier){ 2140245852b7SMilanka Ringwald hfp_connection->ag_send_no_carrier = false; 2141245852b7SMilanka Ringwald hfp_ag_send_no_carrier(hfp_connection->rfcomm_cid); 2142245852b7SMilanka Ringwald return 1; 2143245852b7SMilanka Ringwald } 2144245852b7SMilanka Ringwald 2145a0ffb263SMatthias Ringwald if (hfp_connection->send_phone_number_for_voice_tag){ 2146a0ffb263SMatthias Ringwald hfp_connection->send_phone_number_for_voice_tag = 0; 2147a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2148a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2149a0ffb263SMatthias Ringwald hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid); 2150febc14f5SMatthias Ringwald return 1; 2151aa4dd815SMatthias Ringwald } 2152aa4dd815SMatthias Ringwald 2153a0ffb263SMatthias Ringwald if (hfp_connection->send_subscriber_number){ 2154aeb0f0feSMatthias Ringwald if (hfp_connection->next_subscriber_number_to_send < hfp_ag_subscriber_numbers_count){ 2155aeb0f0feSMatthias Ringwald hfp_phone_number_t phone = hfp_ag_subscriber_numbers[hfp_connection->next_subscriber_number_to_send++]; 2156a0ffb263SMatthias Ringwald hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number); 2157ce263fc8SMatthias Ringwald } else { 2158a0ffb263SMatthias Ringwald hfp_connection->send_subscriber_number = 0; 2159a0ffb263SMatthias Ringwald hfp_connection->next_subscriber_number_to_send = 0; 2160485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 2161ce263fc8SMatthias Ringwald } 2162a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2163febc14f5SMatthias Ringwald return 1; 2164ce263fc8SMatthias Ringwald } 2165ce263fc8SMatthias Ringwald 2166a0ffb263SMatthias Ringwald if (hfp_connection->send_microphone_gain){ 2167a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 0; 2168a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2169485ac19eSMilanka Ringwald hfp_ag_send_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 2170febc14f5SMatthias Ringwald return 1; 2171aa4dd815SMatthias Ringwald } 2172aa4dd815SMatthias Ringwald 2173a0ffb263SMatthias Ringwald if (hfp_connection->send_speaker_gain){ 2174a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 0; 2175a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2176485ac19eSMilanka Ringwald hfp_ag_send_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 2177febc14f5SMatthias Ringwald return 1; 21783deb3ec6SMatthias Ringwald } 21793deb3ec6SMatthias Ringwald 2180a0ffb263SMatthias Ringwald if (hfp_connection->send_ag_status_indicators){ 2181a0ffb263SMatthias Ringwald hfp_connection->send_ag_status_indicators = 0; 2182485ac19eSMilanka Ringwald hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid); 2183febc14f5SMatthias Ringwald return 1; 2184febc14f5SMatthias Ringwald } 2185febc14f5SMatthias Ringwald 2186febc14f5SMatthias Ringwald return 0; 2187febc14f5SMatthias Ringwald } 2188febc14f5SMatthias Ringwald 2189c1eef992SMatthias Ringwald // sends pending command, returns if command was sent 2190c1eef992SMatthias Ringwald static int hfp_ag_run_ring_and_clip(hfp_connection_t *hfp_connection){ 2191c1eef992SMatthias Ringwald // delay RING/CLIP until audio connection has been established for incoming calls 2192c1eef992SMatthias Ringwald // hfp_ag_hf_trigger_ring_and_clip is called in call_setup_state_machine 2193c1eef992SMatthias Ringwald if (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING){ 2194c1eef992SMatthias Ringwald if (hfp_connection->ag_ring){ 2195c1eef992SMatthias Ringwald hfp_connection->ag_ring = 0; 2196c1eef992SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2197c1eef992SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING); 2198c1eef992SMatthias Ringwald hfp_ag_send_ring(hfp_connection->rfcomm_cid); 2199c1eef992SMatthias Ringwald return 1; 2200c1eef992SMatthias Ringwald } 2201c1eef992SMatthias Ringwald 2202c1eef992SMatthias Ringwald if (hfp_connection->ag_send_clip){ 2203c1eef992SMatthias Ringwald hfp_connection->ag_send_clip = 0; 2204c1eef992SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2205c1eef992SMatthias Ringwald hfp_ag_send_clip(hfp_connection->rfcomm_cid); 2206c1eef992SMatthias Ringwald return 1; 2207c1eef992SMatthias Ringwald } 2208c1eef992SMatthias Ringwald } 2209c1eef992SMatthias Ringwald return 0; 2210c1eef992SMatthias Ringwald } 2211c1eef992SMatthias Ringwald 2212febc14f5SMatthias Ringwald static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection){ 2213febc14f5SMatthias Ringwald 221476cc1527SMatthias Ringwald btstack_assert(hfp_connection != NULL); 221576cc1527SMatthias Ringwald btstack_assert(hfp_connection->local_role == HFP_ROLE_AG); 2216febc14f5SMatthias Ringwald 221776cc1527SMatthias Ringwald // during SDP query, RFCOMM CID is not set 221876cc1527SMatthias Ringwald if (hfp_connection->rfcomm_cid == 0) return; 2219ce263fc8SMatthias Ringwald 222084fb9ac1SMilanka Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->emit_vra_enabled_after_audio_established){ 222184fb9ac1SMilanka Ringwald hfp_connection->emit_vra_enabled_after_audio_established = false; 222284fb9ac1SMilanka Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS); 222384fb9ac1SMilanka Ringwald } 222484fb9ac1SMilanka Ringwald 2225f9f3ba28SMilanka Ringwald if ((hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) && hfp_connection->release_audio_connection){ 2226f9f3ba28SMilanka Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 2227f9f3ba28SMilanka Ringwald hfp_connection->release_audio_connection = 0; 2228f9f3ba28SMilanka Ringwald gap_disconnect(hfp_connection->sco_handle); 2229f9f3ba28SMilanka Ringwald return; 2230f9f3ba28SMilanka Ringwald } 2231f9f3ba28SMilanka Ringwald 2232b7802cc8SMatthias Ringwald // configure NBS/WBS if needed using vendor-specific HCI commands 2233b7802cc8SMatthias Ringwald if (hci_can_send_command_packet_now()) { 22343721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP 22353721a235SMatthias Ringwald // WBS Disassociate 22363721a235SMatthias Ringwald if (hfp_connection->cc256x_send_wbs_disassociate){ 22373721a235SMatthias Ringwald hfp_connection->cc256x_send_wbs_disassociate = false; 22383721a235SMatthias Ringwald hci_send_cmd(&hci_ti_wbs_disassociate); 22393721a235SMatthias Ringwald return; 22403721a235SMatthias Ringwald } 22413721a235SMatthias Ringwald // Write Codec Config 22423721a235SMatthias Ringwald if (hfp_connection->cc256x_send_write_codec_config){ 22433721a235SMatthias Ringwald hfp_connection->cc256x_send_write_codec_config = false; 22443721a235SMatthias Ringwald hfp_cc256x_write_codec_config(hfp_connection); 22453721a235SMatthias Ringwald return; 22463721a235SMatthias Ringwald } 22473721a235SMatthias Ringwald // WBS Associate 22483721a235SMatthias Ringwald if (hfp_connection->cc256x_send_wbs_associate){ 22493721a235SMatthias Ringwald hfp_connection->cc256x_send_wbs_associate = false; 22503721a235SMatthias Ringwald hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle); 22513721a235SMatthias Ringwald return; 22523721a235SMatthias Ringwald } 22533721a235SMatthias Ringwald #endif 2254689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 2255689d4323SMatthias Ringwald // Enable WBS 2256689d4323SMatthias Ringwald if (hfp_connection->bcm_send_enable_wbs){ 2257689d4323SMatthias Ringwald hfp_connection->bcm_send_enable_wbs = false; 2258689d4323SMatthias Ringwald hci_send_cmd(&hci_bcm_enable_wbs, 1, 2); 2259689d4323SMatthias Ringwald return; 2260689d4323SMatthias Ringwald } 2261689d4323SMatthias Ringwald // Write I2S/PCM params 2262689d4323SMatthias Ringwald if (hfp_connection->bcm_send_write_i2spcm_interface_param){ 2263689d4323SMatthias Ringwald hfp_connection->bcm_send_write_i2spcm_interface_param = false; 2264689d4323SMatthias Ringwald hfp_bcm_write_i2spcm_interface_param(hfp_connection); 2265689d4323SMatthias Ringwald return; 2266689d4323SMatthias Ringwald } 2267689d4323SMatthias Ringwald // Disable WBS 2268689d4323SMatthias Ringwald if (hfp_connection->bcm_send_disable_wbs){ 2269689d4323SMatthias Ringwald hfp_connection->bcm_send_disable_wbs = false; 2270689d4323SMatthias Ringwald hci_send_cmd(&hci_bcm_enable_wbs, 0, 2); 2271689d4323SMatthias Ringwald return; 2272689d4323SMatthias Ringwald } 2273689d4323SMatthias Ringwald #endif 22742b5f92fdSMatthias Ringwald #ifdef ENABLE_RTK_PCM_WBS 22752b5f92fdSMatthias Ringwald // Configure CVSD vs. mSBC 22762b5f92fdSMatthias Ringwald if (hfp_connection->rtk_send_sco_config){ 22772b5f92fdSMatthias Ringwald hfp_connection->rtk_send_sco_config = false; 22782b5f92fdSMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 22792b5f92fdSMatthias Ringwald log_info("RTK SCO: 16k + mSBC"); 22802b5f92fdSMatthias Ringwald hci_send_cmd(&hci_rtk_configure_sco_routing, 0x81, 0x90, 0x00, 0x00, 0x1a, 0x0c, 0x00, 0x00, 0x41); 22812b5f92fdSMatthias Ringwald } else { 22822b5f92fdSMatthias Ringwald log_info("RTK SCO: 16k + CVSD"); 22832b5f92fdSMatthias Ringwald hci_send_cmd(&hci_rtk_configure_sco_routing, 0x81, 0x90, 0x00, 0x00, 0x1a, 0x0c, 0x0c, 0x00, 0x01); 22842b5f92fdSMatthias Ringwald } 22852b5f92fdSMatthias Ringwald return; 22862b5f92fdSMatthias Ringwald } 22872b5f92fdSMatthias Ringwald #endif 22885fd6f360SMatthias Ringwald #ifdef ENABLE_NXP_PCM_WBS 22895fd6f360SMatthias Ringwald if (hfp_connection->nxp_start_audio_handle != HCI_CON_HANDLE_INVALID){ 22905fd6f360SMatthias Ringwald hci_con_handle_t sco_handle = hfp_connection->nxp_start_audio_handle; 22915fd6f360SMatthias Ringwald hfp_connection->nxp_start_audio_handle = HCI_CON_HANDLE_INVALID; 22925fd6f360SMatthias Ringwald hci_send_cmd(&hci_nxp_host_pcm_i2s_audio_config, 0, 0, sco_handle, 0); 22935fd6f360SMatthias Ringwald return; 22945fd6f360SMatthias Ringwald } 22955fd6f360SMatthias Ringwald if (hfp_connection->nxp_stop_audio_handle != HCI_CON_HANDLE_INVALID){ 22965fd6f360SMatthias Ringwald hci_con_handle_t sco_handle = hfp_connection->nxp_stop_audio_handle; 22975fd6f360SMatthias Ringwald hfp_connection->nxp_stop_audio_handle = HCI_CON_HANDLE_INVALID; 22985fd6f360SMatthias Ringwald hci_send_cmd(&hci_nxp_host_pcm_i2s_audio_config, 1, 0, sco_handle, 0); 22995fd6f360SMatthias Ringwald return; 23005fd6f360SMatthias Ringwald } 23015fd6f360SMatthias Ringwald #endif 2302b7802cc8SMatthias Ringwald } 2303b7802cc8SMatthias Ringwald 230448e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS) 230548e6eeeeSMatthias Ringwald if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){ 230648e6eeeeSMatthias Ringwald hfp_finalize_connection_context(hfp_connection); 230748e6eeeeSMatthias Ringwald return; 230848e6eeeeSMatthias Ringwald } 230948e6eeeeSMatthias Ringwald #endif 23103721a235SMatthias Ringwald 2311febc14f5SMatthias Ringwald if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) { 2312febc14f5SMatthias Ringwald log_info("hfp_ag_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid); 2313febc14f5SMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 2314febc14f5SMatthias Ringwald return; 2315febc14f5SMatthias Ringwald } 2316febc14f5SMatthias Ringwald 2317febc14f5SMatthias Ringwald int cmd_sent = hfp_ag_send_commands(hfp_connection); 2318febc14f5SMatthias Ringwald 2319febc14f5SMatthias Ringwald if (!cmd_sent){ 2320febc14f5SMatthias Ringwald cmd_sent = hfp_ag_run_for_context_service_level_connection(hfp_connection); 2321febc14f5SMatthias Ringwald } 2322febc14f5SMatthias Ringwald 2323c04cf7ffSMilanka Ringwald if (!cmd_sent){ 2324c04cf7ffSMilanka Ringwald cmd_sent = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection); 23253deb3ec6SMatthias Ringwald } 23263deb3ec6SMatthias Ringwald 2327c04cf7ffSMilanka Ringwald if (!cmd_sent){ 2328c04cf7ffSMilanka Ringwald cmd_sent = call_setup_state_machine(hfp_connection); 2329aa4dd815SMatthias Ringwald } 2330aa4dd815SMatthias Ringwald 2331b956fff3SMatthias Ringwald if (!cmd_sent){ 2332b956fff3SMatthias Ringwald cmd_sent = hfp_ag_run_for_audio_connection(hfp_connection); 2333b956fff3SMatthias Ringwald } 2334b956fff3SMatthias Ringwald 2335c95b5b3cSMilanka Ringwald if (!cmd_sent){ 2336c1eef992SMatthias Ringwald cmd_sent = hfp_ag_run_ring_and_clip(hfp_connection); 2337c1eef992SMatthias Ringwald } 2338c1eef992SMatthias Ringwald 2339c1eef992SMatthias Ringwald if (!cmd_sent){ 2340c95b5b3cSMilanka Ringwald cmd_sent = hfp_ag_voice_recognition_state_machine(hfp_connection); 2341c95b5b3cSMilanka Ringwald } 2342b956fff3SMatthias Ringwald 2343d0a0eceeSMatthias Ringwald // disconnect 2344d0a0eceeSMatthias Ringwald if (!cmd_sent && (hfp_connection->command == HFP_CMD_NONE) && (hfp_connection->state == HFP_W2_DISCONNECT_RFCOMM)){ 2345d0a0eceeSMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 2346d0a0eceeSMatthias Ringwald rfcomm_disconnect(hfp_connection->rfcomm_cid); 2347d0a0eceeSMatthias Ringwald } 2348d0a0eceeSMatthias Ringwald 2349c04cf7ffSMilanka Ringwald if (cmd_sent){ 2350a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2351473ac565SMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 2352473ac565SMatthias Ringwald } 23533deb3ec6SMatthias Ringwald } 2354d68dcce1SMatthias Ringwald 2355e0d09929SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){ 2356c1ab6cc1SMatthias Ringwald return (byte == '\n') || (byte == '\r'); 2357e0d09929SMatthias Ringwald } 2358e0d09929SMatthias Ringwald 2359fdf18f86SMilanka Ringwald static void hfp_ag_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){ 23608a46ec40SMatthias Ringwald // assertion: size >= 1 as rfcomm.c does not deliver empty packets 2361fdf18f86SMilanka Ringwald if (size < 1) return; 2362347d46c8SMilanka Ringwald uint8_t status = ERROR_CODE_SUCCESS; 23631e35c04dSMatthias Ringwald 2364186dd3d2SMatthias Ringwald hfp_log_rfcomm_message("HFP_AG_RX", packet, size); 2365e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES 2366e43d1938SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet); 2367e43d1938SMatthias Ringwald #endif 23681e35c04dSMatthias Ringwald 23698a46ec40SMatthias Ringwald // process messages byte-wise 23703deb3ec6SMatthias Ringwald int pos; 23713deb3ec6SMatthias Ringwald for (pos = 0; pos < size ; pos++){ 2372a0ffb263SMatthias Ringwald hfp_parse(hfp_connection, packet[pos], 0); 2373186dd3d2SMatthias Ringwald 2374e0d09929SMatthias Ringwald // parse until end of line 2375e0d09929SMatthias Ringwald if (!hfp_parser_is_end_of_line(packet[pos])) continue; 2376e0d09929SMatthias Ringwald 2377186dd3d2SMatthias Ringwald hfp_generic_status_indicator_t * indicator; 2378a0ffb263SMatthias Ringwald switch(hfp_connection->command){ 237945796ff1SMilanka Ringwald case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION: 2380e7c46708SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2381e7c46708SMatthias Ringwald hfp_connection->ag_vra_requested_by_hf = true; 238245796ff1SMilanka Ringwald break; 2383ce263fc8SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_QUERY: 2384fd66594dSMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2385ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_active){ 2386a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1; 2387ce263fc8SMatthias Ringwald } 2388a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2389ce263fc8SMatthias Ringwald break; 2390ce263fc8SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_COMMAND: 2391fd66594dSMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 23920222a807SMatthias Ringwald switch(hfp_connection->ag_response_and_hold_action){ 2393ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD: 2394a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection); 2395ce263fc8SMatthias Ringwald break; 2396ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED: 2397a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection); 2398ce263fc8SMatthias Ringwald break; 2399ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED: 2400a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection); 2401ce263fc8SMatthias Ringwald break; 2402ce263fc8SMatthias Ringwald default: 2403ce263fc8SMatthias Ringwald break; 2404ce263fc8SMatthias Ringwald } 2405a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2406ce263fc8SMatthias Ringwald break; 2407ce263fc8SMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 2408a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2409347d46c8SMilanka Ringwald 2410aeb0f0feSMatthias Ringwald if (hfp_connection->parser_indicator_index < hfp_ag_generic_status_indicators_nr){ 2411aeb0f0feSMatthias Ringwald indicator = &hfp_ag_generic_status_indicators[hfp_connection->parser_indicator_index]; 2412ce263fc8SMatthias Ringwald switch (indicator->uuid){ 2413347d46c8SMilanka Ringwald case HFP_HF_INDICATOR_UUID_ENHANCED_SAFETY: 24140222a807SMatthias Ringwald if (hfp_connection->parser_indicator_value > 1) { 2415a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 2416ce263fc8SMatthias Ringwald break; 2417ce263fc8SMatthias Ringwald } 2418a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2419585db78dSMilanka Ringwald hfp_ag_emit_hf_indicator_value(hfp_connection, indicator->uuid, hfp_connection->parser_indicator_value); 2420347d46c8SMilanka Ringwald break; 2421347d46c8SMilanka Ringwald case HFP_HF_INDICATOR_UUID_BATTERY_LEVEL: 2422347d46c8SMilanka Ringwald if (hfp_connection->parser_indicator_value > 100){ 2423347d46c8SMilanka Ringwald hfp_connection->send_error = 1; 2424347d46c8SMilanka Ringwald break; 2425347d46c8SMilanka Ringwald } 2426347d46c8SMilanka Ringwald hfp_connection->ok_pending = 1; 2427585db78dSMilanka Ringwald hfp_ag_emit_hf_indicator_value(hfp_connection, indicator->uuid, hfp_connection->parser_indicator_value); 2428347d46c8SMilanka Ringwald break; 2429347d46c8SMilanka Ringwald default: 2430585db78dSMilanka Ringwald hfp_connection->send_error = 1; 2431347d46c8SMilanka Ringwald break; 2432347d46c8SMilanka Ringwald } 2433347d46c8SMilanka Ringwald } else { 2434347d46c8SMilanka Ringwald hfp_connection->send_error = 1; 2435347d46c8SMilanka Ringwald } 2436ce263fc8SMatthias Ringwald break; 2437ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 2438ce263fc8SMatthias Ringwald // expected by SLC state machine 2439a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break; 2440a0ffb263SMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0; 2441a0ffb263SMatthias Ringwald hfp_connection->send_ag_status_indicators = 1; 2442ce263fc8SMatthias Ringwald break; 2443ce263fc8SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 2444a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2445a0ffb263SMatthias Ringwald hfp_connection->next_call_index = 0; 2446a0ffb263SMatthias Ringwald hfp_connection->send_status_of_current_calls = 1; 2447ce263fc8SMatthias Ringwald break; 2448ce263fc8SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 2449fd66594dSMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2450aeb0f0feSMatthias Ringwald if (hfp_ag_subscriber_numbers_count == 0){ 2451485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 2452ce263fc8SMatthias Ringwald break; 2453ce263fc8SMatthias Ringwald } 2454a0ffb263SMatthias Ringwald hfp_connection->next_subscriber_number_to_send = 0; 2455a0ffb263SMatthias Ringwald hfp_connection->send_subscriber_number = 1; 2456ce263fc8SMatthias Ringwald break; 2457c1797c7dSMatthias Ringwald case HFP_CMD_TRANSMIT_DTMF_CODES: 24580222a807SMatthias Ringwald { 2459a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 24600222a807SMatthias Ringwald char buffer[2]; 24610222a807SMatthias Ringwald buffer[0] = (char) hfp_connection->ag_dtmf_code; 24620222a807SMatthias Ringwald buffer[1] = 0; 24630222a807SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, buffer); 2464c1797c7dSMatthias Ringwald break; 24650222a807SMatthias Ringwald } 2466aa4dd815SMatthias Ringwald case HFP_CMD_HF_REQUEST_PHONE_NUMBER: 2467a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2468ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG); 2469aa4dd815SMatthias Ringwald break; 2470aa4dd815SMatthias Ringwald case HFP_CMD_TURN_OFF_EC_AND_NR: 2471a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2472347d46c8SMilanka Ringwald 2473aeb0f0feSMatthias Ringwald if (get_bit(hfp_ag_supported_features, HFP_AGSF_EC_NR_FUNCTION)){ 2474a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2475347d46c8SMilanka Ringwald status = ERROR_CODE_SUCCESS; 2476aa4dd815SMatthias Ringwald } else { 2477a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 2478347d46c8SMilanka Ringwald status = ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 2479aa4dd815SMatthias Ringwald } 2480347d46c8SMilanka Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE, status); 2481aa4dd815SMatthias Ringwald break; 2482aa4dd815SMatthias Ringwald case HFP_CMD_CALL_ANSWERED: 2483a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 248460ebb071SMilanka Ringwald log_info("HFP: ATA"); 2485a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection); 2486aa4dd815SMatthias Ringwald break; 2487aa4dd815SMatthias Ringwald case HFP_CMD_HANG_UP_CALL: 2488a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2489a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2490a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection); 2491aa4dd815SMatthias Ringwald break; 2492aa4dd815SMatthias Ringwald case HFP_CMD_CALL_HOLD: { 2493a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2494a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2495d0c20769SMatthias Ringwald 24960222a807SMatthias Ringwald switch (hfp_connection->ag_call_hold_action){ 24970222a807SMatthias Ringwald case 0: 2498d0c20769SMatthias Ringwald // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call. 2499a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection); 2500aa4dd815SMatthias Ringwald break; 25010222a807SMatthias Ringwald case 1: 2502d0c20769SMatthias Ringwald // Releases all active calls (if any exist) and accepts the other (held or waiting) call. 2503d0c20769SMatthias Ringwald // Where both a held and a waiting call exist, the above procedures shall apply to the 2504d0c20769SMatthias Ringwald // waiting call (i.e., not to the held call) in conflicting situation. 2505a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection); 2506aa4dd815SMatthias Ringwald break; 25070222a807SMatthias Ringwald case 2: 2508d0c20769SMatthias Ringwald // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call. 2509d0c20769SMatthias Ringwald // Where both a held and a waiting call exist, the above procedures shall apply to the 2510d0c20769SMatthias Ringwald // waiting call (i.e., not to the held call) in conflicting situation. 2511a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection); 2512aa4dd815SMatthias Ringwald break; 25130222a807SMatthias Ringwald case 3: 2514d0c20769SMatthias Ringwald // Adds a held call to the conversation. 2515a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection); 2516aa4dd815SMatthias Ringwald break; 25170222a807SMatthias Ringwald case 4: 2518d0c20769SMatthias Ringwald // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer). 2519a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection); 2520aa4dd815SMatthias Ringwald break; 2521aa4dd815SMatthias Ringwald default: 2522aa4dd815SMatthias Ringwald break; 2523aa4dd815SMatthias Ringwald } 25240222a807SMatthias Ringwald hfp_connection->call_index = 0; 252535e92150SMatthias Ringwald break; 2526aa4dd815SMatthias Ringwald } 2527aa4dd815SMatthias Ringwald case HFP_CMD_CALL_PHONE_NUMBER: 2528a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 25299ff73f41SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED_BY_HF, hfp_connection); 2530aa4dd815SMatthias Ringwald break; 2531aa4dd815SMatthias Ringwald case HFP_CMD_REDIAL_LAST_NUMBER: 2532a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2533a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection); 2534aa4dd815SMatthias Ringwald break; 2535aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_CLIP: 2536a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2537a0ffb263SMatthias Ringwald log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled); 2538a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2539aa4dd815SMatthias Ringwald break; 2540aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION: 2541a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2542a0ffb263SMatthias Ringwald log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled); 2543a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2544aa4dd815SMatthias Ringwald break; 2545aa4dd815SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 2546a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2547a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 254860ebb071SMilanka Ringwald log_info("HF speaker gain = %u", hfp_connection->speaker_gain); 25493db60f78SBjoern Hartmann hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_connection->speaker_gain); 2550aa4dd815SMatthias Ringwald break; 2551aa4dd815SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 2552a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2553a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 255460ebb071SMilanka Ringwald log_info("HF microphone gain = %u", hfp_connection->microphone_gain); 25552abbd98dSMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_connection->microphone_gain); 2556aa4dd815SMatthias Ringwald break; 2557471dea41SMatthias Ringwald case HFP_CMD_CUSTOM_MESSAGE: 2558471dea41SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2559471dea41SMatthias Ringwald hfp_parser_reset_line_buffer(hfp_connection); 25606d9a41f9SMatthias Ringwald log_info("Custom AT Command ID 0x%04x", hfp_connection->custom_at_command_id); 2561471dea41SMatthias Ringwald hfp_ag_emit_custom_command_event(hfp_connection); 2562471dea41SMatthias Ringwald break; 25633f38f554SMatthias Ringwald case HFP_CMD_UNKNOWN: 25643f38f554SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 25653f38f554SMatthias Ringwald hfp_connection->send_error = 1; 25663f38f554SMatthias Ringwald break; 2567aa4dd815SMatthias Ringwald default: 2568aa4dd815SMatthias Ringwald break; 25693deb3ec6SMatthias Ringwald } 25703deb3ec6SMatthias Ringwald } 25710cef86faSMatthias Ringwald } 25723deb3ec6SMatthias Ringwald 25731c6a0fc0SMatthias Ringwald static void hfp_ag_run(void){ 2574d63c37a1SMatthias Ringwald btstack_linked_list_iterator_t it; 2575d63c37a1SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2576d63c37a1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2577d63c37a1SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 257822387625SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2579f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 25803deb3ec6SMatthias Ringwald } 25813deb3ec6SMatthias Ringwald } 25823deb3ec6SMatthias Ringwald 25831c6a0fc0SMatthias Ringwald static void hfp_ag_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2584fdf18f86SMilanka Ringwald hfp_connection_t * hfp_connection = NULL; 25853deb3ec6SMatthias Ringwald switch (packet_type){ 25863deb3ec6SMatthias Ringwald case RFCOMM_DATA_PACKET: 2587fdf18f86SMilanka Ringwald hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 2588fd075f20SMilanka Ringwald btstack_assert(hfp_connection != NULL); 2589fdf18f86SMilanka Ringwald 2590fdf18f86SMilanka Ringwald hfp_ag_handle_rfcomm_data(hfp_connection, packet, size); 2591fdf18f86SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 2592fdf18f86SMilanka Ringwald return; 25933deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 2594e30a6a47SMatthias Ringwald if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 2595e30a6a47SMatthias Ringwald uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 2596fd075f20SMilanka Ringwald hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid); 2597fd075f20SMilanka Ringwald btstack_assert(hfp_connection != NULL); 2598fd075f20SMilanka Ringwald 2599fd075f20SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 2600e30a6a47SMatthias Ringwald return; 2601e30a6a47SMatthias Ringwald } 260227950165SMatthias Ringwald hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_AG); 2603aa4dd815SMatthias Ringwald break; 26043deb3ec6SMatthias Ringwald default: 26053deb3ec6SMatthias Ringwald break; 26063deb3ec6SMatthias Ringwald } 26073deb3ec6SMatthias Ringwald 26081c6a0fc0SMatthias Ringwald hfp_ag_run(); 26093deb3ec6SMatthias Ringwald } 26103deb3ec6SMatthias Ringwald 26111c6a0fc0SMatthias Ringwald static void hfp_ag_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2612405014fbSMatthias Ringwald hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_AG); 26131c6a0fc0SMatthias Ringwald hfp_ag_run(); 2614405014fbSMatthias Ringwald } 2615405014fbSMatthias Ringwald 2616*aa10b9cbSMatthias Ringwald void hfp_ag_init_codecs(uint8_t codecs_nr, const uint8_t * codecs){ 2617*aa10b9cbSMatthias Ringwald btstack_assert(codecs_nr <= HFP_MAX_NUM_CODECS); 2618*aa10b9cbSMatthias Ringwald 2619aeb0f0feSMatthias Ringwald hfp_ag_codecs_nr = codecs_nr; 2620*aa10b9cbSMatthias Ringwald uint8_t i; 26213deb3ec6SMatthias Ringwald for (i=0; i < codecs_nr; i++){ 2622aeb0f0feSMatthias Ringwald hfp_ag_codecs[i] = codecs[i]; 26233deb3ec6SMatthias Ringwald } 2624a0ffb263SMatthias Ringwald } 26253deb3ec6SMatthias Ringwald 2626a0ffb263SMatthias Ringwald void hfp_ag_init_supported_features(uint32_t supported_features){ 2627aeb0f0feSMatthias Ringwald hfp_ag_supported_features = supported_features; 262840a8ee13SMatthias Ringwald hfp_ag_in_band_ring_tone_active = has_in_band_ring_tone(); 2629a0ffb263SMatthias Ringwald } 26303deb3ec6SMatthias Ringwald 26317ca89cabSMatthias Ringwald void hfp_ag_init_ag_indicators(int ag_indicators_nr, const hfp_ag_indicator_t * ag_indicators){ 2632a0ffb263SMatthias Ringwald hfp_ag_indicators_nr = ag_indicators_nr; 26336535961aSMatthias Ringwald (void)memcpy(hfp_ag_indicators, ag_indicators, 26346535961aSMatthias Ringwald ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 2635a0ffb263SMatthias Ringwald } 26363deb3ec6SMatthias Ringwald 26377ca89cabSMatthias Ringwald void hfp_ag_init_hf_indicators(int hf_indicators_nr, const hfp_generic_status_indicator_t * hf_indicators){ 263825789943SMilanka Ringwald if (hf_indicators_nr > HFP_MAX_NUM_INDICATORS) return; 2639aeb0f0feSMatthias Ringwald hfp_ag_generic_status_indicators_nr = hf_indicators_nr; 2640aeb0f0feSMatthias Ringwald (void)memcpy(hfp_ag_generic_status_indicators, hf_indicators, 26416535961aSMatthias Ringwald hf_indicators_nr * sizeof(hfp_generic_status_indicator_t)); 2642a0ffb263SMatthias Ringwald } 2643a0ffb263SMatthias Ringwald 2644a0ffb263SMatthias Ringwald void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){ 26453deb3ec6SMatthias Ringwald hfp_ag_call_hold_services_nr = call_hold_services_nr; 26466535961aSMatthias Ringwald (void)memcpy(hfp_ag_call_hold_services, call_hold_services, 26476535961aSMatthias Ringwald call_hold_services_nr * sizeof(char *)); 2648a0ffb263SMatthias Ringwald } 2649a0ffb263SMatthias Ringwald 2650a0ffb263SMatthias Ringwald 2651ab2445a0SMatthias Ringwald void hfp_ag_init(uint8_t rfcomm_channel_nr){ 265227950165SMatthias Ringwald 2653520c92d5SMatthias Ringwald hfp_init(); 265420b2edb6SMatthias Ringwald hfp_ag_call_hold_services_nr = 0; 265527bb1817SMatthias Ringwald hfp_ag_response_and_hold_active = false; 265620b2edb6SMatthias Ringwald hfp_ag_indicators_nr = 0; 2657aeb0f0feSMatthias Ringwald hfp_ag_codecs_nr = 0; 2658aeb0f0feSMatthias Ringwald hfp_ag_supported_features = HFP_DEFAULT_AG_SUPPORTED_FEATURES; 265940a8ee13SMatthias Ringwald hfp_ag_in_band_ring_tone_active = has_in_band_ring_tone(); 2660aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers = NULL; 2661aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers_count = 0; 2662d63c37a1SMatthias Ringwald 26631c6a0fc0SMatthias Ringwald hfp_ag_hci_event_callback_registration.callback = &hfp_ag_hci_event_packet_handler; 26641c6a0fc0SMatthias Ringwald hci_add_event_handler(&hfp_ag_hci_event_callback_registration); 266527950165SMatthias Ringwald 26661c6a0fc0SMatthias Ringwald rfcomm_register_service(&hfp_ag_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 266727950165SMatthias Ringwald 266827950165SMatthias Ringwald // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us 26691c6a0fc0SMatthias Ringwald hfp_set_ag_rfcomm_packet_handler(&hfp_ag_rfcomm_packet_handler); 2670aa4dd815SMatthias Ringwald 2671d210d9c4SMatthias Ringwald hfp_gsm_init(); 26723deb3ec6SMatthias Ringwald } 26733deb3ec6SMatthias Ringwald 267420b2edb6SMatthias Ringwald void hfp_ag_deinit(void){ 267520b2edb6SMatthias Ringwald hfp_deinit(); 267620b2edb6SMatthias Ringwald hfp_gsm_deinit(); 2677aeb0f0feSMatthias Ringwald 2678aeb0f0feSMatthias Ringwald hfp_ag_callback = NULL; 2679aeb0f0feSMatthias Ringwald hfp_ag_supported_features = 0; 2680aeb0f0feSMatthias Ringwald hfp_ag_codecs_nr = 0; 2681aeb0f0feSMatthias Ringwald hfp_ag_indicators_nr = 0; 2682aeb0f0feSMatthias Ringwald hfp_ag_call_hold_services_nr = 0; 268320b2edb6SMatthias Ringwald (void) memset(&hfp_ag_call_hold_services, 0, sizeof(hfp_ag_call_hold_services)); 2684aeb0f0feSMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 268520b2edb6SMatthias Ringwald (void) memset(&hfp_ag_response_and_hold_state, 0, sizeof(hfp_response_and_hold_state_t)); 2686aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers = NULL; 2687aeb0f0feSMatthias Ringwald (void) memset(&hfp_ag_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t)); 268881e25d0aSMatthias Ringwald hfp_ag_custom_call_sm_handler = NULL; 268920b2edb6SMatthias Ringwald } 269020b2edb6SMatthias Ringwald 26914eb3f1d8SMilanka Ringwald uint8_t hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){ 26924eb3f1d8SMilanka Ringwald return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE, HFP_ROLE_AG); 26933deb3ec6SMatthias Ringwald } 26943deb3ec6SMatthias Ringwald 2695657bc59fSMilanka Ringwald uint8_t hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){ 26969c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2697a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2698657bc59fSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2699a33eb0c4SMilanka Ringwald } 2700c5fa3c94SMilanka Ringwald 27011ffa0dd9SMilanka Ringwald hfp_trigger_release_service_level_connection(hfp_connection); 2702f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2703657bc59fSMilanka Ringwald return ERROR_CODE_SUCCESS; 27043deb3ec6SMatthias Ringwald } 27053deb3ec6SMatthias Ringwald 2706c5fa3c94SMilanka Ringwald uint8_t hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, hfp_cme_error_t error){ 27079c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2708a0ffb263SMatthias Ringwald if (!hfp_connection){ 2709c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 27103deb3ec6SMatthias Ringwald } 27113deb3ec6SMatthias Ringwald 2712c5fa3c94SMilanka Ringwald hfp_connection->extended_audio_gateway_error = 0; 2713c5fa3c94SMilanka Ringwald if (!hfp_connection->enable_extended_audio_gateway_error_report){ 2714c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2715c5fa3c94SMilanka Ringwald } 2716c5fa3c94SMilanka Ringwald 2717c5fa3c94SMilanka Ringwald hfp_connection->extended_audio_gateway_error = error; 2718c5fa3c94SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 2719c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2720c5fa3c94SMilanka Ringwald } 2721c5fa3c94SMilanka Ringwald 2722c5fa3c94SMilanka Ringwald static uint8_t hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){ 2723c5fa3c94SMilanka Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 2724c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2725c5fa3c94SMilanka Ringwald } 2726c5fa3c94SMilanka Ringwald if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){ 2727c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2728c5fa3c94SMilanka Ringwald } 27293deb3ec6SMatthias Ringwald 2730a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 1; 2731a0ffb263SMatthias Ringwald if (!has_codec_negotiation_feature(hfp_connection)){ 2732d6ff09e1SMatthias Ringwald log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using CVSD"); 2733d6ff09e1SMatthias Ringwald hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 2734a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 27357522e673SMatthias Ringwald // now, pick link settings 2736afac2a04SMilanka Ringwald hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection)); 27373721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP 27383721a235SMatthias Ringwald hfp_cc256x_prepare_for_sco(hfp_connection); 27393721a235SMatthias Ringwald #endif 2740c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 27413deb3ec6SMatthias Ringwald } 2742aa4dd815SMatthias Ringwald 2743411a9776SMilanka Ringwald uint8_t i; 2744411a9776SMilanka Ringwald bool codec_was_in_use = false; 2745411a9776SMilanka Ringwald bool better_codec_can_be_used = false; 2746411a9776SMilanka Ringwald 2747411a9776SMilanka Ringwald for (i = 0; i<hfp_connection->remote_codecs_nr; i++){ 2748411a9776SMilanka Ringwald if (hfp_connection->negotiated_codec == hfp_connection->remote_codecs[i]){ 2749411a9776SMilanka Ringwald codec_was_in_use = true; 2750411a9776SMilanka Ringwald } else if (hfp_connection->negotiated_codec < hfp_connection->remote_codecs[i]){ 2751411a9776SMilanka Ringwald better_codec_can_be_used = true; 2752411a9776SMilanka Ringwald } 2753411a9776SMilanka Ringwald } 2754411a9776SMilanka Ringwald 2755411a9776SMilanka Ringwald if (!codec_was_in_use || better_codec_can_be_used){ 2756bc94e48cSMatthias Ringwald hfp_connection->ag_send_common_codec = true; 2757411a9776SMilanka Ringwald hfp_connection->codecs_state = HFP_CODECS_IDLE; 2758411a9776SMilanka Ringwald } 2759c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2760aa4dd815SMatthias Ringwald } 2761aa4dd815SMatthias Ringwald 2762c5fa3c94SMilanka Ringwald uint8_t hfp_ag_establish_audio_connection(hci_con_handle_t acl_handle){ 27639c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2764a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2765c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2766a33eb0c4SMilanka Ringwald } 2767c5fa3c94SMilanka Ringwald uint8_t status = hfp_ag_setup_audio_connection(hfp_connection); 2768c5fa3c94SMilanka Ringwald if (status != ERROR_CODE_SUCCESS){ 2769c5fa3c94SMilanka Ringwald return status; 2770c5fa3c94SMilanka Ringwald } 2771f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2772c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 27733deb3ec6SMatthias Ringwald } 27743deb3ec6SMatthias Ringwald 2775c5fa3c94SMilanka Ringwald uint8_t hfp_ag_release_audio_connection(hci_con_handle_t acl_handle){ 27769c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2777a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2778c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2779a33eb0c4SMilanka Ringwald } 2780c5fa3c94SMilanka Ringwald 27810b4debbfSMilanka Ringwald if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){ 27820b4debbfSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 27830b4debbfSMilanka Ringwald } 27841a26de69SMilanka Ringwald 27850b4debbfSMilanka Ringwald uint8_t status = hfp_trigger_release_audio_connection(hfp_connection); 27860b4debbfSMilanka Ringwald if (status == ERROR_CODE_SUCCESS){ 2787f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 27880b4debbfSMilanka Ringwald } 27890b4debbfSMilanka Ringwald return status; 27903deb3ec6SMatthias Ringwald } 2791aa4dd815SMatthias Ringwald 2792aa4dd815SMatthias Ringwald /** 2793aa4dd815SMatthias Ringwald * @brief Enable in-band ring tone 2794aa4dd815SMatthias Ringwald */ 2795aa4dd815SMatthias Ringwald void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){ 279640a8ee13SMatthias Ringwald if (has_in_band_ring_tone() == false){ 279740a8ee13SMatthias Ringwald return; 279840a8ee13SMatthias Ringwald } 279940a8ee13SMatthias Ringwald if (hfp_ag_in_band_ring_tone_active == use_in_band_ring_tone){ 2800aa4dd815SMatthias Ringwald return; 2801aa4dd815SMatthias Ringwald } 2802c5fa3c94SMilanka Ringwald 280340a8ee13SMatthias Ringwald hfp_ag_in_band_ring_tone_active = use_in_band_ring_tone; 2804aa4dd815SMatthias Ringwald 2805665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 2806665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2807665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2808a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 280966c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 28109430c71eSMatthias Ringwald hfp_connection->ag_send_in_band_ring_tone_setting = true; 2811f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2812aa4dd815SMatthias Ringwald } 2813aa4dd815SMatthias Ringwald } 2814aa4dd815SMatthias Ringwald 2815aa4dd815SMatthias Ringwald /** 2816aa4dd815SMatthias Ringwald * @brief Called from GSM 2817aa4dd815SMatthias Ringwald */ 2818aa4dd815SMatthias Ringwald void hfp_ag_incoming_call(void){ 2819aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL); 2820aa4dd815SMatthias Ringwald } 2821aa4dd815SMatthias Ringwald 28220f1da533SMatthias Ringwald void hfp_ag_outgoing_call_initiated(void) { 2823fe899794SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED_BY_AG, NULL); 2824fe899794SMatthias Ringwald } 2825fe899794SMatthias Ringwald 2826aa4dd815SMatthias Ringwald /** 2827aa4dd815SMatthias Ringwald * @brief number is stored. 2828aa4dd815SMatthias Ringwald */ 2829aa4dd815SMatthias Ringwald void hfp_ag_set_clip(uint8_t type, const char * number){ 2830f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_SET_CLIP, 0, type, number); 2831aa4dd815SMatthias Ringwald } 2832aa4dd815SMatthias Ringwald 2833aa4dd815SMatthias Ringwald void hfp_ag_call_dropped(void){ 2834aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL); 2835aa4dd815SMatthias Ringwald } 2836aa4dd815SMatthias Ringwald 2837aa4dd815SMatthias Ringwald // call from AG UI 2838aa4dd815SMatthias Ringwald void hfp_ag_answer_incoming_call(void){ 2839aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL); 2840aa4dd815SMatthias Ringwald } 2841aa4dd815SMatthias Ringwald 2842ce263fc8SMatthias Ringwald void hfp_ag_join_held_call(void){ 2843ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL); 2844ce263fc8SMatthias Ringwald } 2845ce263fc8SMatthias Ringwald 2846aa4dd815SMatthias Ringwald void hfp_ag_terminate_call(void){ 2847aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL); 2848aa4dd815SMatthias Ringwald } 2849aa4dd815SMatthias Ringwald 2850aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_ringing(void){ 2851aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL); 2852aa4dd815SMatthias Ringwald } 2853aa4dd815SMatthias Ringwald 2854aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_established(void){ 2855aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL); 2856aa4dd815SMatthias Ringwald } 2857aa4dd815SMatthias Ringwald 2858aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_rejected(void){ 2859aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL); 2860aa4dd815SMatthias Ringwald } 2861aa4dd815SMatthias Ringwald 2862aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_accepted(void){ 2863aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL); 2864aa4dd815SMatthias Ringwald } 2865ce263fc8SMatthias Ringwald 2866ce263fc8SMatthias Ringwald void hfp_ag_hold_incoming_call(void){ 2867ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL); 2868ce263fc8SMatthias Ringwald } 2869ce263fc8SMatthias Ringwald 2870ce263fc8SMatthias Ringwald void hfp_ag_accept_held_incoming_call(void) { 2871ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL); 2872ce263fc8SMatthias Ringwald } 2873ce263fc8SMatthias Ringwald 2874ce263fc8SMatthias Ringwald void hfp_ag_reject_held_incoming_call(void){ 2875ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL); 2876ce263fc8SMatthias Ringwald } 2877ce263fc8SMatthias Ringwald 2878aa4dd815SMatthias Ringwald static void hfp_ag_set_ag_indicator(const char * name, int value){ 2879aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name(name); 2880aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 2881aa4dd815SMatthias Ringwald hfp_ag_indicators[indicator_index].status = value; 2882aa4dd815SMatthias Ringwald 2883665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 2884665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2885665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2886a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 288766c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2888a0ffb263SMatthias Ringwald if (!hfp_connection->ag_indicators[indicator_index].enabled) { 2889245852b7SMilanka Ringwald log_info("Requested AG indicator '%s' update to %u, but it is not enabled", hfp_ag_indicators[indicator_index].name, value); 2890ce263fc8SMatthias Ringwald continue; 2891ce263fc8SMatthias Ringwald } 2892245852b7SMilanka Ringwald log_info("AG indicator '%s' changed to %u, request transfer status", hfp_ag_indicators[indicator_index].name, value); 2893a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 2894f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2895aa4dd815SMatthias Ringwald } 2896aa4dd815SMatthias Ringwald } 2897aa4dd815SMatthias Ringwald 2898c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_registration_status(int registration_status){ 2899c5fa3c94SMilanka Ringwald if ((registration_status < 0) || (registration_status > 1)){ 2900c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2901c5fa3c94SMilanka Ringwald } 2902c5fa3c94SMilanka Ringwald 2903c5fa3c94SMilanka Ringwald if ( (registration_status == 0) && (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 2904245852b7SMilanka Ringwald 2905245852b7SMilanka Ringwald // if network goes away wihle a call is active: 2906245852b7SMilanka Ringwald // - the call gets dropped 2907245852b7SMilanka Ringwald // - we send NO CARRIER 2908245852b7SMilanka Ringwald // NOTE: the CALL=0 has to be sent before NO CARRIER 2909245852b7SMilanka Ringwald 2910245852b7SMilanka Ringwald hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL); 2911245852b7SMilanka Ringwald 2912245852b7SMilanka Ringwald btstack_linked_list_iterator_t it; 2913245852b7SMilanka Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2914245852b7SMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2915245852b7SMilanka Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 2916245852b7SMilanka Ringwald hfp_connection->ag_send_no_carrier = true; 2917245852b7SMilanka Ringwald } 2918245852b7SMilanka Ringwald } 2919c5fa3c94SMilanka Ringwald hfp_ag_set_ag_indicator("service", registration_status); 2920c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2921aa4dd815SMatthias Ringwald } 2922aa4dd815SMatthias Ringwald 2923c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_signal_strength(int signal_strength){ 2924c5fa3c94SMilanka Ringwald if ((signal_strength < 0) || (signal_strength > 5)){ 2925c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2926aa4dd815SMatthias Ringwald } 2927aa4dd815SMatthias Ringwald 2928c5fa3c94SMilanka Ringwald hfp_ag_set_ag_indicator("signal", signal_strength); 2929c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2930aa4dd815SMatthias Ringwald } 2931aa4dd815SMatthias Ringwald 2932c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_roaming_status(int roaming_status){ 2933c5fa3c94SMilanka Ringwald if ((roaming_status < 0) || (roaming_status > 1)){ 2934c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2935aa4dd815SMatthias Ringwald } 2936aa4dd815SMatthias Ringwald 2937c5fa3c94SMilanka Ringwald hfp_ag_set_ag_indicator("roam", roaming_status); 2938c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2939c5fa3c94SMilanka Ringwald } 2940c5fa3c94SMilanka Ringwald 2941c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_battery_level(int battery_level){ 2942c5fa3c94SMilanka Ringwald if ((battery_level < 0) || (battery_level > 5)){ 2943c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2944c5fa3c94SMilanka Ringwald } 2945c5fa3c94SMilanka Ringwald hfp_ag_set_ag_indicator("battchg", battery_level); 2946c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2947c5fa3c94SMilanka Ringwald } 2948c5fa3c94SMilanka Ringwald 29496b8275b0SMilanka Ringwald uint8_t hfp_ag_activate_voice_recognition(hci_con_handle_t acl_handle){ 295014c148caSMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 295114c148caSMilanka Ringwald if (!hfp_connection){ 295214c148caSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 29536b8275b0SMilanka Ringwald } 2954f71d2aacSMilanka Ringwald 295584fb9ac1SMilanka Ringwald if (hfp_connection->emit_vra_enabled_after_audio_established){ 295684fb9ac1SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 295784fb9ac1SMilanka Ringwald } 295884fb9ac1SMilanka Ringwald 29599cd26862SMilanka Ringwald bool enhanced_vra_supported = hfp_ag_enhanced_vra_flag_supported(hfp_connection); 29609cd26862SMilanka Ringwald bool legacy_vra_supported = hfp_ag_vra_flag_supported(hfp_connection); 2961f71d2aacSMilanka Ringwald if (!enhanced_vra_supported && !legacy_vra_supported){ 296214c148caSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 29636b8275b0SMilanka Ringwald } 29646b8275b0SMilanka Ringwald 2965135d9718SMilanka Ringwald if (!hfp_ag_can_activate_voice_recognition(hfp_connection)){ 2966135d9718SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2967f71d2aacSMilanka Ringwald } 2968f71d2aacSMilanka Ringwald 2969f71d2aacSMilanka Ringwald hfp_connection->ag_activate_voice_recognition_value = 1; 2970f71d2aacSMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED; 2971fcf4ede6SMilanka Ringwald hfp_connection->enhanced_voice_recognition_enabled = enhanced_vra_supported; 2972f71d2aacSMilanka Ringwald hfp_connection->ag_audio_connection_opened_before_vra = hfp_ag_is_audio_connection_active(hfp_connection); 2973f71d2aacSMilanka Ringwald hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY; 29748f8818a4SMatthias Ringwald hfp_connection->ag_vra_send_command = true; 2975f71d2aacSMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 2976f71d2aacSMilanka Ringwald return ERROR_CODE_SUCCESS; 2977f71d2aacSMilanka Ringwald } 2978f71d2aacSMilanka Ringwald 2979d84719b1SMilanka Ringwald uint8_t hfp_ag_deactivate_voice_recognition(hci_con_handle_t acl_handle){ 298014c148caSMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 298114c148caSMilanka Ringwald if (!hfp_connection){ 298214c148caSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 298345796ff1SMilanka Ringwald } 2984f71d2aacSMilanka Ringwald 298584fb9ac1SMilanka Ringwald if (hfp_connection->emit_vra_enabled_after_audio_established){ 298684fb9ac1SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 298784fb9ac1SMilanka Ringwald } 298884fb9ac1SMilanka Ringwald 29899cd26862SMilanka Ringwald bool enhanced_vra_supported = hfp_ag_enhanced_vra_flag_supported(hfp_connection); 29909cd26862SMilanka Ringwald bool legacy_vra_supported = hfp_ag_vra_flag_supported(hfp_connection); 29919cd26862SMilanka Ringwald 2992f71d2aacSMilanka Ringwald if (!enhanced_vra_supported && !legacy_vra_supported){ 299314c148caSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 299445796ff1SMilanka Ringwald } 299545796ff1SMilanka Ringwald 2996135d9718SMilanka Ringwald if (!hfp_ag_voice_recognition_session_active(hfp_connection)){ 2997135d9718SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2998f71d2aacSMilanka Ringwald } 2999f71d2aacSMilanka Ringwald 3000f71d2aacSMilanka Ringwald hfp_connection->ag_activate_voice_recognition_value = 0; 3001f71d2aacSMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF; 3002f71d2aacSMilanka Ringwald hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY; 30038f8818a4SMatthias Ringwald hfp_connection->ag_vra_send_command = true; 3004f71d2aacSMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 3005f71d2aacSMilanka Ringwald return ERROR_CODE_SUCCESS; 3006f71d2aacSMilanka Ringwald } 3007f71d2aacSMilanka Ringwald 3008cf75be85SMilanka Ringwald static uint8_t hfp_ag_enhanced_voice_recognition_send_state(hci_con_handle_t acl_handle, hfp_voice_recognition_state_t state){ 300945796ff1SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 301045796ff1SMilanka Ringwald if (!hfp_connection){ 3011c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 301245796ff1SMilanka Ringwald } 3013c5fa3c94SMilanka Ringwald 301484fb9ac1SMilanka Ringwald if (hfp_connection->emit_vra_enabled_after_audio_established){ 301584fb9ac1SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3016f71d2aacSMilanka Ringwald } 3017f71d2aacSMilanka Ringwald 3018645bfa78SMilanka Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){ 3019645bfa78SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3020645bfa78SMilanka Ringwald } 3021645bfa78SMilanka Ringwald 3022645bfa78SMilanka Ringwald if (hfp_connection->vra_state != HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO){ 3023135d9718SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 30249cd26862SMilanka Ringwald } 30259cd26862SMilanka Ringwald 302684fb9ac1SMilanka Ringwald bool enhanced_vra_supported = hfp_ag_enhanced_vra_flag_supported(hfp_connection); 302784fb9ac1SMilanka Ringwald if (!enhanced_vra_supported ){ 302884fb9ac1SMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 302984fb9ac1SMilanka Ringwald } 303084fb9ac1SMilanka Ringwald 303184fb9ac1SMilanka Ringwald 303245796ff1SMilanka Ringwald hfp_connection->ag_vra_state = state; 3033cf75be85SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_STATUS; 30348f8818a4SMatthias Ringwald hfp_connection->ag_vra_send_command = true; 303545796ff1SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 30369cd26862SMilanka Ringwald return ERROR_CODE_SUCCESS; 303745796ff1SMilanka Ringwald } 303845796ff1SMilanka Ringwald 3039f0c27a95SMilanka Ringwald uint8_t hfp_ag_enhanced_voice_recognition_report_sending_audio(hci_con_handle_t acl_handle){ 3040cf75be85SMilanka Ringwald return hfp_ag_enhanced_voice_recognition_send_state(acl_handle, HFP_VOICE_RECOGNITION_STATE_AG_IS_STARTING_SOUND); 304145796ff1SMilanka Ringwald } 3042f0c27a95SMilanka Ringwald uint8_t hfp_ag_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle){ 3043cf75be85SMilanka Ringwald return hfp_ag_enhanced_voice_recognition_send_state(acl_handle, HFP_VOICE_RECOGNITION_STATE_AG_READY_TO_ACCEPT_AUDIO_INPUT); 304445796ff1SMilanka Ringwald } 3045cf75be85SMilanka Ringwald uint8_t hfp_ag_enhanced_voice_recognition_report_processing_input(hci_con_handle_t acl_handle){ 3046cf75be85SMilanka Ringwald return hfp_ag_enhanced_voice_recognition_send_state(acl_handle, HFP_VOICE_RECOGNITION_STATE_AG_IS_PROCESSING_AUDIO_INPUT); 304745796ff1SMilanka Ringwald } 304845796ff1SMilanka Ringwald 3049b95cac54SMilanka Ringwald 3050b95cac54SMilanka 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){ 3051b95cac54SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3052b95cac54SMilanka Ringwald if (!hfp_connection){ 3053b95cac54SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3054b95cac54SMilanka Ringwald } 3055b95cac54SMilanka Ringwald 305684fb9ac1SMilanka Ringwald if (hfp_connection->emit_vra_enabled_after_audio_established){ 305784fb9ac1SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3058f71d2aacSMilanka Ringwald } 3059f71d2aacSMilanka Ringwald 3060645bfa78SMilanka Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){ 3061645bfa78SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3062645bfa78SMilanka Ringwald } 3063645bfa78SMilanka Ringwald 3064645bfa78SMilanka Ringwald if (hfp_connection->vra_state != HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO){ 3065135d9718SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3066f71d2aacSMilanka Ringwald } 3067f71d2aacSMilanka Ringwald 306884fb9ac1SMilanka Ringwald bool enhanced_vra_supported = hfp_ag_enhanced_vra_flag_supported(hfp_connection); 306984fb9ac1SMilanka Ringwald if (!enhanced_vra_supported ){ 307084fb9ac1SMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 307184fb9ac1SMilanka Ringwald } 307284fb9ac1SMilanka Ringwald 307384fb9ac1SMilanka Ringwald bool enhanced_vra_msg_supported = hfp_ag_can_send_enhanced_vra_message_flag_supported(hfp_connection); 307484fb9ac1SMilanka Ringwald if (!enhanced_vra_msg_supported ){ 307584fb9ac1SMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 307684fb9ac1SMilanka Ringwald } 307784fb9ac1SMilanka Ringwald 3078ab2445a0SMatthias Ringwald uint16_t message_len = (uint16_t) strlen(msg.text); 307951aa5d5aSMilanka Ringwald 308089e7c136SMilanka Ringwald if (message_len > HFP_MAX_VR_TEXT_SIZE){ 30816504a03aSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 30826504a03aSMilanka Ringwald } 30836504a03aSMilanka Ringwald 30846504a03aSMilanka Ringwald if ((HFP_VR_TEXT_HEADER_SIZE + message_len) > hfp_connection->rfcomm_mtu){ 308551aa5d5aSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 308651aa5d5aSMilanka Ringwald } 308751aa5d5aSMilanka Ringwald 3088b95cac54SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_MSG; 3089b95cac54SMilanka Ringwald hfp_connection->ag_msg = msg; 3090b95cac54SMilanka Ringwald hfp_connection->ag_vra_state = state; 30918f8818a4SMatthias Ringwald hfp_connection->ag_vra_send_command = true; 3092b95cac54SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 3093f71d2aacSMilanka Ringwald 3094f71d2aacSMilanka Ringwald return ERROR_CODE_SUCCESS; 3095b95cac54SMilanka Ringwald } 3096b95cac54SMilanka Ringwald 3097b95cac54SMilanka Ringwald 3098c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 30999c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3100a33eb0c4SMilanka Ringwald if (!hfp_connection){ 3101c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3102a33eb0c4SMilanka Ringwald } 3103c5fa3c94SMilanka Ringwald 3104c5fa3c94SMilanka Ringwald if ((gain < 0) || (gain > 15)){ 3105c5fa3c94SMilanka Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 3106c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3107c5fa3c94SMilanka Ringwald } 3108c5fa3c94SMilanka Ringwald 3109a0ffb263SMatthias Ringwald if (hfp_connection->microphone_gain != gain){ 3110a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = gain; 3111a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 3112aa4dd815SMatthias Ringwald } 3113f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 3114c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 3115aa4dd815SMatthias Ringwald } 3116aa4dd815SMatthias Ringwald 3117c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 31189c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3119a33eb0c4SMilanka Ringwald if (!hfp_connection){ 3120c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3121a33eb0c4SMilanka Ringwald } 3122c5fa3c94SMilanka Ringwald 3123c5fa3c94SMilanka Ringwald if ((gain < 0) || (gain > 15)){ 3124c5fa3c94SMilanka Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 3125c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3126c5fa3c94SMilanka Ringwald } 3127c5fa3c94SMilanka Ringwald 3128a0ffb263SMatthias Ringwald if (hfp_connection->speaker_gain != gain){ 3129a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = gain; 3130a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 3131aa4dd815SMatthias Ringwald } 3132f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 3133c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 3134aa4dd815SMatthias Ringwald } 3135aa4dd815SMatthias Ringwald 3136c5fa3c94SMilanka Ringwald uint8_t hfp_ag_send_phone_number_for_voice_tag(hci_con_handle_t acl_handle, const char * number){ 31379c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3138a33eb0c4SMilanka Ringwald if (!hfp_connection){ 3139c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3140a33eb0c4SMilanka Ringwald } 3141c5fa3c94SMilanka Ringwald 3142aa4dd815SMatthias Ringwald hfp_ag_set_clip(0, number); 3143a0ffb263SMatthias Ringwald hfp_connection->send_phone_number_for_voice_tag = 1; 3144c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 3145aa4dd815SMatthias Ringwald } 3146aa4dd815SMatthias Ringwald 3147c5fa3c94SMilanka Ringwald uint8_t hfp_ag_reject_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 31489c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3149a33eb0c4SMilanka Ringwald if (!hfp_connection){ 3150c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3151a33eb0c4SMilanka Ringwald } 3152c5fa3c94SMilanka Ringwald 3153a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 3154c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 3155aa4dd815SMatthias Ringwald } 3156aa4dd815SMatthias Ringwald 3157c5fa3c94SMilanka Ringwald uint8_t hfp_ag_send_dtmf_code_done(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 3163a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 3164c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 3165c1797c7dSMatthias Ringwald } 3166aa4dd815SMatthias Ringwald 31675be6b47aSMatthias Ringwald uint8_t hfp_ag_send_unsolicited_result_code(hci_con_handle_t acl_handle, const char * unsolicited_result_code){ 31685be6b47aSMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 31695be6b47aSMatthias Ringwald if (!hfp_connection){ 31705be6b47aSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 31715be6b47aSMatthias Ringwald } 31725be6b47aSMatthias Ringwald if (hfp_connection->send_custom_message != NULL){ 31735be6b47aSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 31745be6b47aSMatthias Ringwald } 31755be6b47aSMatthias Ringwald hfp_connection->send_custom_message = unsolicited_result_code; 31765be6b47aSMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 31775be6b47aSMatthias Ringwald return ERROR_CODE_SUCCESS; 31785be6b47aSMatthias Ringwald } 31795be6b47aSMatthias Ringwald 3180471dea41SMatthias Ringwald uint8_t hfp_ag_send_command_result_code(hci_con_handle_t acl_handle, bool ok){ 3181471dea41SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3182471dea41SMatthias Ringwald if (!hfp_connection){ 3183471dea41SMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3184471dea41SMatthias Ringwald } 3185471dea41SMatthias Ringwald if (ok){ 3186471dea41SMatthias Ringwald hfp_connection->ok_pending = 1; 3187471dea41SMatthias Ringwald } else { 3188471dea41SMatthias Ringwald hfp_connection->send_error = 1; 3189471dea41SMatthias Ringwald } 3190471dea41SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 3191471dea41SMatthias Ringwald return ERROR_CODE_SUCCESS; 3192471dea41SMatthias Ringwald } 3193471dea41SMatthias Ringwald 3194ce263fc8SMatthias Ringwald void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){ 3195aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers = numbers; 3196aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers_count = numbers_count; 3197ce263fc8SMatthias Ringwald } 3198ce263fc8SMatthias Ringwald 31999cae807eSMatthias Ringwald void hfp_ag_clear_last_dialed_number(void){ 32009cae807eSMatthias Ringwald hfp_gsm_clear_last_dialed_number(); 3201ce263fc8SMatthias Ringwald } 3202ce263fc8SMatthias Ringwald 32039de679b7SMilanka Ringwald void hfp_ag_set_last_dialed_number(const char * number){ 32049de679b7SMilanka Ringwald hfp_gsm_set_last_dialed_number(number); 32059de679b7SMilanka Ringwald } 32069de679b7SMilanka Ringwald 3207c5fa3c94SMilanka Ringwald uint8_t hfp_ag_notify_incoming_call_waiting(hci_con_handle_t acl_handle){ 32089c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3209a33eb0c4SMilanka Ringwald if (!hfp_connection){ 3210c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3211a33eb0c4SMilanka Ringwald } 3212c5fa3c94SMilanka Ringwald 3213c5fa3c94SMilanka Ringwald if (!hfp_connection->call_waiting_notification_enabled){ 3214c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3215c5fa3c94SMilanka Ringwald } 3216a0ffb263SMatthias Ringwald 3217a0ffb263SMatthias Ringwald hfp_connection->ag_notify_incoming_call_waiting = 1; 3218f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 3219c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 3220a0ffb263SMatthias Ringwald } 3221*aa10b9cbSMatthias Ringwald void hfp_ag_create_sdp_record_with_codecs(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr, 3222*aa10b9cbSMatthias Ringwald const char * name, uint8_t ability_to_reject_call, uint16_t supported_features, 3223*aa10b9cbSMatthias Ringwald uint8_t codecs_nr, const uint8_t * codecs){ 322476cc1527SMatthias Ringwald if (!name){ 3225aeb0f0feSMatthias Ringwald name = hfp_ag_default_service_name; 322676cc1527SMatthias Ringwald } 322776cc1527SMatthias Ringwald hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, rfcomm_channel_nr, name); 322876cc1527SMatthias Ringwald 322976cc1527SMatthias Ringwald /* 323076cc1527SMatthias Ringwald * 0x01 – Ability to reject a call 323176cc1527SMatthias Ringwald * 0x00 – No ability to reject a call 323276cc1527SMatthias Ringwald */ 323376cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301); // Hands-Free Profile - Network 323476cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call); 323576cc1527SMatthias Ringwald 323676cc1527SMatthias Ringwald // Construct SupportedFeatures for SDP bitmap: 323776cc1527SMatthias Ringwald // 323876cc1527SMatthias Ringwald // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 323976cc1527SMatthias Ringwald // of the Bits 0 to 4 of the unsolicited result code +BRSF" 324076cc1527SMatthias Ringwald // 3241*aa10b9cbSMatthias Ringwald // Wide band speech (bit 5) and LC3-SWB (bit 8) require Codec negotiation 324276cc1527SMatthias Ringwald // 324376cc1527SMatthias Ringwald uint16_t sdp_features = supported_features & 0x1f; 3244ef3ae4ebSMilanka Ringwald 3245ef3ae4ebSMilanka Ringwald if (supported_features & (1 << HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS)){ 3246ef3ae4ebSMilanka Ringwald sdp_features |= 1 << 6; 3247ef3ae4ebSMilanka Ringwald } 3248ef3ae4ebSMilanka Ringwald 3249ef3ae4ebSMilanka Ringwald if (supported_features & (1 << HFP_AGSF_VOICE_RECOGNITION_TEXT)){ 3250ef3ae4ebSMilanka Ringwald sdp_features |= 1 << 7; 3251ef3ae4ebSMilanka Ringwald } 3252ef3ae4ebSMilanka Ringwald 3253*aa10b9cbSMatthias Ringwald // codecs 3254*aa10b9cbSMatthias Ringwald if ((supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION)) != 0){ 3255*aa10b9cbSMatthias Ringwald uint8_t i; 3256*aa10b9cbSMatthias Ringwald for (i=0;i<codecs_nr;i++){ 3257*aa10b9cbSMatthias Ringwald switch (codecs[i]){ 3258*aa10b9cbSMatthias Ringwald case HFP_CODEC_MSBC: 3259*aa10b9cbSMatthias Ringwald sdp_features |= 1 << 5; 3260*aa10b9cbSMatthias Ringwald break; 3261*aa10b9cbSMatthias Ringwald case HFP_CODEC_LC3_SWB: 3262*aa10b9cbSMatthias Ringwald sdp_features |= 1 << 8; 3263*aa10b9cbSMatthias Ringwald break; 3264*aa10b9cbSMatthias Ringwald } 3265*aa10b9cbSMatthias Ringwald } 3266*aa10b9cbSMatthias Ringwald } 3267*aa10b9cbSMatthias Ringwald 326876cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 326976cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 327076cc1527SMatthias Ringwald } 327176cc1527SMatthias Ringwald 3272*aa10b9cbSMatthias Ringwald void hfp_ag_create_sdp_record(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr, const char * name, 3273*aa10b9cbSMatthias Ringwald uint8_t ability_to_reject_call, uint16_t supported_features, int wide_band_speech){ 3274*aa10b9cbSMatthias Ringwald uint8_t codecs_nr; 3275*aa10b9cbSMatthias Ringwald const uint8_t * codecs; 3276*aa10b9cbSMatthias Ringwald const uint8_t wide_band_codecs[] = { HFP_CODEC_MSBC }; 3277*aa10b9cbSMatthias Ringwald if (wide_band_speech == 0){ 3278*aa10b9cbSMatthias Ringwald codecs_nr = 0; 3279*aa10b9cbSMatthias Ringwald codecs = NULL; 3280*aa10b9cbSMatthias Ringwald } else { 3281*aa10b9cbSMatthias Ringwald codecs_nr = 1; 3282*aa10b9cbSMatthias Ringwald codecs = wide_band_codecs; 3283*aa10b9cbSMatthias Ringwald } 3284*aa10b9cbSMatthias Ringwald hfp_ag_create_sdp_record_with_codecs(service, service_record_handle, rfcomm_channel_nr, name, 3285*aa10b9cbSMatthias Ringwald ability_to_reject_call, supported_features, codecs_nr, codecs); 3286*aa10b9cbSMatthias Ringwald } 3287*aa10b9cbSMatthias Ringwald 328876cc1527SMatthias Ringwald void hfp_ag_register_packet_handler(btstack_packet_handler_t callback){ 328968466199SMilanka Ringwald btstack_assert(callback != NULL); 329068466199SMilanka Ringwald 329176cc1527SMatthias Ringwald hfp_ag_callback = callback; 329276cc1527SMatthias Ringwald hfp_set_ag_callback(callback); 329376cc1527SMatthias Ringwald } 329481e25d0aSMatthias Ringwald 329581e25d0aSMatthias Ringwald void hfp_ag_register_custom_call_sm_handler(bool (*handler)(hfp_ag_call_event_t event)){ 329681e25d0aSMatthias Ringwald hfp_ag_custom_call_sm_handler = handler; 329781e25d0aSMatthias Ringwald } 3298471dea41SMatthias Ringwald 3299471dea41SMatthias Ringwald void hfp_ag_register_custom_at_command(hfp_custom_at_command_t * custom_at_command){ 3300471dea41SMatthias Ringwald hfp_register_custom_ag_command(custom_at_command); 3301471dea41SMatthias Ringwald } 3302