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 233deb3ec6SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 243deb3ec6SMatthias Ringwald * RINGWALD 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 9420b2edb6SMatthias Ringwald static btstack_packet_callback_registration_t hfp_ag_hci_event_callback_registration; 95aa4dd815SMatthias Ringwald 96aeb0f0feSMatthias Ringwald static uint16_t hfp_ag_supported_features; 9720b2edb6SMatthias Ringwald 98aeb0f0feSMatthias Ringwald // codecs 99aeb0f0feSMatthias Ringwald static uint8_t hfp_ag_codecs_nr; 100aeb0f0feSMatthias Ringwald static uint8_t hfp_ag_codecs[HFP_MAX_NUM_CODECS]; 1013deb3ec6SMatthias Ringwald 102aeb0f0feSMatthias Ringwald // AG indicators 10320b2edb6SMatthias Ringwald static int hfp_ag_indicators_nr; 10425789943SMilanka Ringwald static hfp_ag_indicator_t hfp_ag_indicators[HFP_MAX_NUM_INDICATORS]; 1053deb3ec6SMatthias Ringwald 106aeb0f0feSMatthias Ringwald // generic status indicators 107aeb0f0feSMatthias Ringwald static int hfp_ag_generic_status_indicators_nr; 108aeb0f0feSMatthias Ringwald static hfp_generic_status_indicator_t hfp_ag_generic_status_indicators[HFP_MAX_NUM_INDICATORS]; 109a0ffb263SMatthias Ringwald 11020b2edb6SMatthias Ringwald static int hfp_ag_call_hold_services_nr; 1113deb3ec6SMatthias Ringwald static char * hfp_ag_call_hold_services[6]; 1123deb3ec6SMatthias Ringwald 113ce263fc8SMatthias Ringwald static hfp_response_and_hold_state_t hfp_ag_response_and_hold_state; 11420b2edb6SMatthias Ringwald static int hfp_ag_response_and_hold_active; 115aa4dd815SMatthias Ringwald 11620b2edb6SMatthias Ringwald // Subscriber information entries 117aeb0f0feSMatthias Ringwald static hfp_phone_number_t * hfp_ag_subscriber_numbers; 118aeb0f0feSMatthias Ringwald static int hfp_ag_subscriber_numbers_count; 119ce263fc8SMatthias Ringwald 120a4f85bd2SMilanka Ringwald // call state 121a4f85bd2SMilanka Ringwald static btstack_timer_source_t hfp_ag_ring_timeout; 122a4f85bd2SMilanka Ringwald 12320b2edb6SMatthias Ringwald // code 124a0ffb263SMatthias Ringwald static int hfp_ag_get_ag_indicators_nr(hfp_connection_t * hfp_connection){ 125a0ffb263SMatthias Ringwald if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){ 126a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr; 1276535961aSMatthias Ringwald (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, 1286535961aSMatthias Ringwald hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 1293deb3ec6SMatthias Ringwald } 130a0ffb263SMatthias Ringwald return hfp_connection->ag_indicators_nr; 131a0ffb263SMatthias Ringwald } 132a0ffb263SMatthias Ringwald 133a0ffb263SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection){ 134a0ffb263SMatthias Ringwald // TODO: save only value, and value changed in the hfp_connection? 135a0ffb263SMatthias Ringwald if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){ 136a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr; 1376535961aSMatthias Ringwald (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, 1386535961aSMatthias Ringwald hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 139a0ffb263SMatthias Ringwald } 140a0ffb263SMatthias Ringwald return (hfp_ag_indicator_t *)&(hfp_connection->ag_indicators); 1413deb3ec6SMatthias Ringwald } 1423deb3ec6SMatthias Ringwald 143aa4dd815SMatthias Ringwald static hfp_ag_indicator_t * get_ag_indicator_for_name(const char * name){ 144aa4dd815SMatthias Ringwald int i; 145aa4dd815SMatthias Ringwald for (i = 0; i < hfp_ag_indicators_nr; i++){ 146aa4dd815SMatthias Ringwald if (strcmp(hfp_ag_indicators[i].name, name) == 0){ 147aa4dd815SMatthias Ringwald return &hfp_ag_indicators[i]; 148aa4dd815SMatthias Ringwald } 149aa4dd815SMatthias Ringwald } 150aa4dd815SMatthias Ringwald return NULL; 151aa4dd815SMatthias Ringwald } 152aa4dd815SMatthias Ringwald 153aa4dd815SMatthias Ringwald static int get_ag_indicator_index_for_name(const char * name){ 154aa4dd815SMatthias Ringwald int i; 155aa4dd815SMatthias Ringwald for (i = 0; i < hfp_ag_indicators_nr; i++){ 156aa4dd815SMatthias Ringwald if (strcmp(hfp_ag_indicators[i].name, name) == 0){ 157aa4dd815SMatthias Ringwald return i; 158aa4dd815SMatthias Ringwald } 159aa4dd815SMatthias Ringwald } 160aa4dd815SMatthias Ringwald return -1; 161aa4dd815SMatthias Ringwald } 162aa4dd815SMatthias Ringwald 1639c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_ag_connection_context_for_acl_handle(uint16_t handle){ 1649c9c64c1SMatthias Ringwald btstack_linked_list_iterator_t it; 1659c9c64c1SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1669c9c64c1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1679c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1689c9c64c1SMatthias Ringwald if (hfp_connection->acl_handle != handle) continue; 1699c9c64c1SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1709c9c64c1SMatthias Ringwald return hfp_connection; 1719c9c64c1SMatthias Ringwald } 1729c9c64c1SMatthias Ringwald return NULL; 1739c9c64c1SMatthias Ringwald } 1743deb3ec6SMatthias Ringwald 1750cb5b971SMatthias Ringwald static int use_in_band_tone(void){ 176aeb0f0feSMatthias Ringwald return get_bit(hfp_ag_supported_features, HFP_AGSF_IN_BAND_RING_TONE); 1773deb3ec6SMatthias Ringwald } 1783deb3ec6SMatthias Ringwald 179a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){ 180a0ffb263SMatthias Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_CODEC_NEGOTIATION); 181aeb0f0feSMatthias Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_CODEC_NEGOTIATION); 1823deb3ec6SMatthias Ringwald return hf && ag; 1833deb3ec6SMatthias Ringwald } 1843deb3ec6SMatthias Ringwald 185a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){ 186a0ffb263SMatthias Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_THREE_WAY_CALLING); 187aeb0f0feSMatthias Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_THREE_WAY_CALLING); 1883deb3ec6SMatthias Ringwald return hf && ag; 1893deb3ec6SMatthias Ringwald } 1903deb3ec6SMatthias Ringwald 191a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){ 192a0ffb263SMatthias Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_HF_INDICATORS); 193aeb0f0feSMatthias Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_HF_INDICATORS); 1943deb3ec6SMatthias Ringwald return hf && ag; 1953deb3ec6SMatthias Ringwald } 1963deb3ec6SMatthias Ringwald 19776cc1527SMatthias Ringwald /* unsolicited responses */ 198aa4dd815SMatthias Ringwald 199485ac19eSMilanka Ringwald static int hfp_ag_send_change_in_band_ring_tone_setting_cmd(uint16_t cid){ 200aa4dd815SMatthias Ringwald char buffer[20]; 201ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: %d\r\n", 202ff7d6aeaSMatthias Ringwald HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone()); 203ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 204aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2053deb3ec6SMatthias Ringwald } 2063deb3ec6SMatthias Ringwald 2073deb3ec6SMatthias Ringwald static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){ 2083deb3ec6SMatthias Ringwald char buffer[40]; 209ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n\r\nOK\r\n", 210aeb0f0feSMatthias Ringwald HFP_SUPPORTED_FEATURES, hfp_ag_supported_features); 211ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 2123deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2133deb3ec6SMatthias Ringwald } 2143deb3ec6SMatthias Ringwald 215485ac19eSMilanka Ringwald static int hfp_ag_send_ok(uint16_t cid){ 2163deb3ec6SMatthias Ringwald char buffer[10]; 217ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\nOK\r\n"); 218ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 2193deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2203deb3ec6SMatthias Ringwald } 2213deb3ec6SMatthias Ringwald 222485ac19eSMilanka Ringwald static int hfp_ag_send_ring(uint16_t cid){ 223aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, (char *) "\r\nRING\r\n"); 224aa4dd815SMatthias Ringwald } 225aa4dd815SMatthias Ringwald 226245852b7SMilanka Ringwald static int hfp_ag_send_no_carrier(uint16_t cid){ 227245852b7SMilanka Ringwald char buffer[15]; 228245852b7SMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\nNO CARRIER\r\n"); 229245852b7SMilanka Ringwald buffer[sizeof(buffer) - 1] = 0; 230245852b7SMilanka Ringwald return send_str_over_rfcomm(cid, buffer); 231245852b7SMilanka Ringwald } 232245852b7SMilanka Ringwald 233aa4dd815SMatthias Ringwald static int hfp_ag_send_clip(uint16_t cid){ 234aa4dd815SMatthias Ringwald char buffer[50]; 235ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP, 236ff7d6aeaSMatthias Ringwald hfp_gsm_clip_number(), hfp_gsm_clip_type()); 237ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 238aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 239aa4dd815SMatthias Ringwald } 240aa4dd815SMatthias Ringwald 241ce263fc8SMatthias Ringwald static int hfp_send_subscriber_number_cmd(uint16_t cid, uint8_t type, const char * number){ 242ce263fc8SMatthias Ringwald char buffer[50]; 243ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: ,\"%s\",%u, , \r\n", 244ff7d6aeaSMatthias Ringwald HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type); 245ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 246ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 247ce263fc8SMatthias Ringwald } 248ce263fc8SMatthias Ringwald 249c1797c7dSMatthias Ringwald static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){ 250aa4dd815SMatthias Ringwald char buffer[50]; 251ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: %s\r\n", 252ff7d6aeaSMatthias Ringwald HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number()); 253ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 254aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 255aa4dd815SMatthias Ringwald } 256aa4dd815SMatthias Ringwald 257aa4dd815SMatthias Ringwald static int hfp_ag_send_call_waiting_notification(uint16_t cid){ 258aa4dd815SMatthias Ringwald char buffer[50]; 259ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: \"%s\",%u\r\n", 260ff7d6aeaSMatthias Ringwald HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(), 261ff7d6aeaSMatthias Ringwald hfp_gsm_clip_type()); 262ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 263aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 264aa4dd815SMatthias Ringwald } 265aa4dd815SMatthias Ringwald 266485ac19eSMilanka Ringwald static int hfp_ag_send_error(uint16_t cid){ 2673deb3ec6SMatthias Ringwald char buffer[10]; 268ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\nERROR\r\n"); 269ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 2703deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2713deb3ec6SMatthias Ringwald } 2723deb3ec6SMatthias Ringwald 273485ac19eSMilanka Ringwald static int hfp_ag_send_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){ 2743deb3ec6SMatthias Ringwald char buffer[20]; 275ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s=%d\r\n", 276ff7d6aeaSMatthias Ringwald HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error); 277ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 2783deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2793deb3ec6SMatthias Ringwald } 2803deb3ec6SMatthias Ringwald 281ad902e3dSMatthias Ringwald // get size for indicator string 282a0ffb263SMatthias Ringwald static int hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection, int i){ 283ad902e3dSMatthias Ringwald // template: ("$NAME",($MIN,$MAX)) 28445718b6fSMatthias Ringwald return 8 + (int) strlen(hfp_ag_get_ag_indicators(hfp_connection)[i].name) 285a0ffb263SMatthias Ringwald + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range) 286a0ffb263SMatthias Ringwald + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range); 287ad902e3dSMatthias Ringwald } 288ad902e3dSMatthias Ringwald 289ad902e3dSMatthias Ringwald // store indicator 2901167ff83SMatthias Ringwald static void hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection, int i, uint8_t * buffer, uint16_t buffer_size){ 2911167ff83SMatthias Ringwald snprintf((char *)buffer, buffer_size, "(\"%s\",(%d,%d)),", 292a0ffb263SMatthias Ringwald hfp_ag_get_ag_indicators(hfp_connection)[i].name, 293a0ffb263SMatthias Ringwald hfp_ag_get_ag_indicators(hfp_connection)[i].min_range, 294a0ffb263SMatthias Ringwald hfp_ag_get_ag_indicators(hfp_connection)[i].max_range); 2951167ff83SMatthias Ringwald ((char *)buffer)[buffer_size - 1] = 0; 2963deb3ec6SMatthias Ringwald } 297ad902e3dSMatthias Ringwald 298ad902e3dSMatthias Ringwald // structure: header [indicator [comma indicator]] footer 299a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_num_segments(hfp_connection_t * hfp_connection){ 300a0ffb263SMatthias Ringwald int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 301ad902e3dSMatthias Ringwald if (!num_indicators) return 2; 302c1ab6cc1SMatthias Ringwald return 3 + ((num_indicators-1) * 2); 3033deb3ec6SMatthias Ringwald } 304ad902e3dSMatthias Ringwald 305ad902e3dSMatthias Ringwald // get size of individual segment for hfp_ag_retrieve_indicators_cmd 306a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_get_segment_len(hfp_connection_t * hfp_connection, int index){ 307ad902e3dSMatthias Ringwald if (index == 0) { 308ad902e3dSMatthias Ringwald return strlen(HFP_INDICATOR) + 3; // "\n\r%s:"" 309ad902e3dSMatthias Ringwald } 310ad902e3dSMatthias Ringwald index--; 311a0ffb263SMatthias Ringwald int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 312ad902e3dSMatthias Ringwald int indicator_index = index >> 1; 313ad902e3dSMatthias Ringwald if ((index & 1) == 0){ 314a0ffb263SMatthias Ringwald return hfp_ag_indicators_string_size(hfp_connection, indicator_index); 315ad902e3dSMatthias Ringwald } 316c1ab6cc1SMatthias Ringwald if (indicator_index == (num_indicators - 1)){ 317ad902e3dSMatthias Ringwald return 8; // "\r\n\r\nOK\r\n" 318ad902e3dSMatthias Ringwald } 319ad902e3dSMatthias Ringwald return 1; // comma 320ad902e3dSMatthias Ringwald } 321ad902e3dSMatthias Ringwald 3221167ff83SMatthias Ringwald static void hfp_ag_indicators_cmd_generator_store_segment(hfp_connection_t * hfp_connection, int index, uint8_t * buffer, uint16_t buffer_size){ 323ad902e3dSMatthias Ringwald if (index == 0){ 324ad902e3dSMatthias Ringwald *buffer++ = '\r'; 32574386ee0SMatthias Ringwald *buffer++ = '\n'; 326ad902e3dSMatthias Ringwald int len = strlen(HFP_INDICATOR); 3276535961aSMatthias Ringwald (void)memcpy(buffer, HFP_INDICATOR, len); 328ad902e3dSMatthias Ringwald buffer += len; 329ad902e3dSMatthias Ringwald *buffer++ = ':'; 330ad902e3dSMatthias Ringwald return; 331ad902e3dSMatthias Ringwald } 332ad902e3dSMatthias Ringwald index--; 333a0ffb263SMatthias Ringwald int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 334ad902e3dSMatthias Ringwald int indicator_index = index >> 1; 335ad902e3dSMatthias Ringwald if ((index & 1) == 0){ 3361167ff83SMatthias Ringwald hfp_ag_indicators_string_store(hfp_connection, indicator_index, buffer, buffer_size); 337ad902e3dSMatthias Ringwald return; 338ad902e3dSMatthias Ringwald } 339c1ab6cc1SMatthias Ringwald if (indicator_index == (num_indicators-1)){ 3406535961aSMatthias Ringwald (void)memcpy(buffer, "\r\n\r\nOK\r\n", 8); 341ad902e3dSMatthias Ringwald return; 342ad902e3dSMatthias Ringwald } 343ad902e3dSMatthias Ringwald *buffer = ','; 3443deb3ec6SMatthias Ringwald } 3453deb3ec6SMatthias Ringwald 346aeb0f0feSMatthias Ringwald static int hfp_ag_generic_indicators_join(char * buffer, int buffer_size){ 347c1ab6cc1SMatthias Ringwald if (buffer_size < (hfp_ag_indicators_nr * 3)) return 0; 3483deb3ec6SMatthias Ringwald int i; 3493deb3ec6SMatthias Ringwald int offset = 0; 350aeb0f0feSMatthias Ringwald for (i = 0; i < (hfp_ag_generic_status_indicators_nr - 1); i++) { 351aeb0f0feSMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_generic_status_indicators[i].uuid); 3523deb3ec6SMatthias Ringwald } 353aeb0f0feSMatthias Ringwald if (i < hfp_ag_generic_status_indicators_nr){ 354aeb0f0feSMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_generic_status_indicators[i].uuid); 3553deb3ec6SMatthias Ringwald } 3563deb3ec6SMatthias Ringwald return offset; 3573deb3ec6SMatthias Ringwald } 3583deb3ec6SMatthias Ringwald 359aeb0f0feSMatthias Ringwald static int hfp_ag_generic_indicators_initial_status_join(char * buffer, int buffer_size){ 360aeb0f0feSMatthias Ringwald if (buffer_size < (hfp_ag_generic_status_indicators_nr * 3)) return 0; 3613deb3ec6SMatthias Ringwald int i; 3623deb3ec6SMatthias Ringwald int offset = 0; 363aeb0f0feSMatthias Ringwald for (i = 0; i < hfp_ag_generic_status_indicators_nr; i++) { 364aeb0f0feSMatthias 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); 3653deb3ec6SMatthias Ringwald } 3663deb3ec6SMatthias Ringwald return offset; 3673deb3ec6SMatthias Ringwald } 3683deb3ec6SMatthias Ringwald 3693deb3ec6SMatthias Ringwald static int hfp_ag_indicators_status_join(char * buffer, int buffer_size){ 370c1ab6cc1SMatthias Ringwald if (buffer_size < (hfp_ag_indicators_nr * 3)) return 0; 3713deb3ec6SMatthias Ringwald int i; 3723deb3ec6SMatthias Ringwald int offset = 0; 373c1ab6cc1SMatthias Ringwald for (i = 0; i < (hfp_ag_indicators_nr-1); i++) { 3743deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_indicators[i].status); 3753deb3ec6SMatthias Ringwald } 3763deb3ec6SMatthias Ringwald if (i<hfp_ag_indicators_nr){ 3773deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_indicators[i].status); 3783deb3ec6SMatthias Ringwald } 3793deb3ec6SMatthias Ringwald return offset; 3803deb3ec6SMatthias Ringwald } 3813deb3ec6SMatthias Ringwald 3823deb3ec6SMatthias Ringwald static int hfp_ag_call_services_join(char * buffer, int buffer_size){ 383c1ab6cc1SMatthias Ringwald if (buffer_size < (hfp_ag_call_hold_services_nr * 3)) return 0; 3843deb3ec6SMatthias Ringwald int i; 3853deb3ec6SMatthias Ringwald int offset = snprintf(buffer, buffer_size, "("); 386c1ab6cc1SMatthias Ringwald for (i = 0; i < (hfp_ag_call_hold_services_nr-1); i++) { 3873deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%s,", hfp_ag_call_hold_services[i]); 3883deb3ec6SMatthias Ringwald } 3893deb3ec6SMatthias Ringwald if (i<hfp_ag_call_hold_services_nr){ 3903deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%s)", hfp_ag_call_hold_services[i]); 3913deb3ec6SMatthias Ringwald } 3923deb3ec6SMatthias Ringwald return offset; 3933deb3ec6SMatthias Ringwald } 3943deb3ec6SMatthias Ringwald 395485ac19eSMilanka Ringwald static int hfp_ag_send_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, 396ad902e3dSMatthias Ringwald int start_segment, int num_segments, 397a0ffb263SMatthias Ringwald int (*get_segment_len)(hfp_connection_t * hfp_connection, int segment), 3981167ff83SMatthias Ringwald void (*store_segment) (hfp_connection_t * hfp_connection, int segment, uint8_t * buffer, uint16_t buffer_size)){ 3993deb3ec6SMatthias Ringwald 400ad902e3dSMatthias Ringwald // assumes: can send now == true 401ad902e3dSMatthias Ringwald // assumes: num segments > 0 402aba39421SMatthias Ringwald // assumes: individual segments are smaller than MTU 403ad902e3dSMatthias Ringwald rfcomm_reserve_packet_buffer(); 404ad902e3dSMatthias Ringwald int mtu = rfcomm_get_max_frame_size(cid); 405ad902e3dSMatthias Ringwald uint8_t * data = rfcomm_get_outgoing_buffer(); 406ad902e3dSMatthias Ringwald int offset = 0; 407ad902e3dSMatthias Ringwald int segment = start_segment; 408ad902e3dSMatthias Ringwald while (segment < num_segments){ 409a0ffb263SMatthias Ringwald int segment_len = get_segment_len(hfp_connection, segment); 4101167ff83SMatthias Ringwald if ((offset + segment_len + 1) > mtu) break; 4111167ff83SMatthias Ringwald // append segment. As it appends a '\0', we provide a buffer one byte larger 4121167ff83SMatthias Ringwald store_segment(hfp_connection, segment, data+offset, segment_len + 1); 413ad902e3dSMatthias Ringwald offset += segment_len; 414ad902e3dSMatthias Ringwald segment++; 415ad902e3dSMatthias Ringwald } 416ad902e3dSMatthias Ringwald rfcomm_send_prepared(cid, offset); 417f20dd2aeSBjoern Hartmann #ifdef ENABLE_HFP_AT_MESSAGES 418f20dd2aeSBjoern Hartmann hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_SENT, (char *) data); 419f20dd2aeSBjoern Hartmann #endif 420ad902e3dSMatthias Ringwald return segment; 421ad902e3dSMatthias Ringwald } 4223deb3ec6SMatthias Ringwald 423ad902e3dSMatthias Ringwald // returns next segment to store 424485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, int start_segment){ 425a0ffb263SMatthias Ringwald int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection); 426485ac19eSMilanka Ringwald return hfp_ag_send_cmd_via_generator(cid, hfp_connection, start_segment, num_segments, 427894d499cSMatthias Ringwald hfp_ag_indicators_cmd_generator_get_segment_len, 428894d499cSMatthias Ringwald hfp_ag_indicators_cmd_generator_store_segment); 4293deb3ec6SMatthias Ringwald } 4303deb3ec6SMatthias Ringwald 431485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_status_cmd(uint16_t cid){ 4323deb3ec6SMatthias Ringwald char buffer[40]; 43389425bfcSMilanka Ringwald const int size = sizeof(buffer); 43489425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "\r\n%s:", HFP_INDICATOR); 43589425bfcSMilanka Ringwald offset += hfp_ag_indicators_status_join(buffer+offset, size-offset-9); 43689425bfcSMilanka Ringwald offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n"); 4373deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4383deb3ec6SMatthias Ringwald } 4393deb3ec6SMatthias Ringwald 440485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_can_hold_call_cmd(uint16_t cid){ 441ad902e3dSMatthias Ringwald char buffer[40]; 44289425bfcSMilanka Ringwald const int size = sizeof(buffer); 44389425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "\r\n%s:", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES); 44489425bfcSMilanka Ringwald offset += hfp_ag_call_services_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 4493deb3ec6SMatthias Ringwald 450485ac19eSMilanka Ringwald static int hfp_ag_send_list_supported_generic_status_indicators_cmd(uint16_t cid){ 451485ac19eSMilanka Ringwald return hfp_ag_send_ok(cid); 4523deb3ec6SMatthias Ringwald } 4533deb3ec6SMatthias Ringwald 454485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(uint16_t cid){ 4553deb3ec6SMatthias Ringwald char buffer[40]; 45689425bfcSMilanka Ringwald const int size = sizeof(buffer); 45789425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "\r\n%s:(", HFP_GENERIC_STATUS_INDICATOR); 458aeb0f0feSMatthias Ringwald offset += hfp_ag_generic_indicators_join(buffer + offset, size - offset - 10); 45989425bfcSMilanka Ringwald offset += snprintf(buffer+offset, size-offset, ")\r\n\r\nOK\r\n"); 4603deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4613deb3ec6SMatthias Ringwald } 4623deb3ec6SMatthias Ringwald 463485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid){ 4643deb3ec6SMatthias Ringwald char buffer[40]; 465aeb0f0feSMatthias Ringwald int offset = hfp_ag_generic_indicators_initial_status_join(buffer, sizeof(buffer) - 7); 4661cc1d9e9SMilanka Ringwald snprintf(buffer+offset, sizeof(buffer)-offset, "\r\nOK\r\n"); 4673deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4683deb3ec6SMatthias Ringwald } 4693deb3ec6SMatthias Ringwald 470485ac19eSMilanka Ringwald static int hfp_ag_send_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){ 4713deb3ec6SMatthias Ringwald char buffer[20]; 472ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s:%d,%d\r\n", 473ff7d6aeaSMatthias Ringwald HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index, 474ff7d6aeaSMatthias Ringwald indicator->status); 475ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 4763deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4773deb3ec6SMatthias Ringwald } 4783deb3ec6SMatthias Ringwald 479485ac19eSMilanka Ringwald static int hfp_ag_send_report_network_operator_name_cmd(uint16_t cid, hfp_network_opearator_t op){ 4805e71456cSMilanka Ringwald char buffer[41]; 4813deb3ec6SMatthias Ringwald if (strlen(op.name) == 0){ 48289425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s:%d,,\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode); 4833deb3ec6SMatthias Ringwald } else { 48489425bfcSMilanka Ringwald int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:%d,%d,", HFP_QUERY_OPERATOR_SELECTION, op.mode, op.format); 4855e71456cSMilanka Ringwald offset += snprintf(buffer+offset, 16, "%s", op.name); 48689425bfcSMilanka Ringwald snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n"); 4873deb3ec6SMatthias Ringwald } 4883deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4893deb3ec6SMatthias Ringwald } 4903deb3ec6SMatthias Ringwald 491a04aecb1SMilanka Ringwald static inline int hfp_ag_send_cmd_with_space_and_int(uint16_t cid, const char * cmd, uint8_t value){ 492a04aecb1SMilanka Ringwald char buffer[30]; 493a04aecb1SMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: %d\r\n", cmd, value); 494a04aecb1SMilanka Ringwald return send_str_over_rfcomm(cid, buffer); 495a04aecb1SMilanka Ringwald } 496a04aecb1SMilanka Ringwald 497a04aecb1SMilanka Ringwald 4981cc1d9e9SMilanka Ringwald static inline int hfp_ag_send_cmd_with_int(uint16_t cid, const char * cmd, uint8_t value){ 4991cc1d9e9SMilanka Ringwald char buffer[30]; 50089425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n", cmd, value); 5011cc1d9e9SMilanka Ringwald return send_str_over_rfcomm(cid, buffer); 5021cc1d9e9SMilanka Ringwald } 5033deb3ec6SMatthias Ringwald 504485ac19eSMilanka Ringwald static int hfp_ag_send_suggest_codec_cmd(uint16_t cid, uint8_t codec){ 5051cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec); 5063deb3ec6SMatthias Ringwald } 5073deb3ec6SMatthias Ringwald 508485ac19eSMilanka Ringwald static int hfp_ag_send_set_speaker_gain_cmd(uint16_t cid, uint8_t gain){ 5091cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain); 510aa4dd815SMatthias Ringwald } 511aa4dd815SMatthias Ringwald 512485ac19eSMilanka Ringwald static int hfp_ag_send_set_microphone_gain_cmd(uint16_t cid, uint8_t gain){ 5131cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain); 514aa4dd815SMatthias Ringwald } 515aa4dd815SMatthias Ringwald 516485ac19eSMilanka Ringwald static int hfp_ag_send_set_response_and_hold(uint16_t cid, int state){ 51766191aa0SMilanka Ringwald return hfp_ag_send_cmd_with_space_and_int(cid, HFP_RESPONSE_AND_HOLD, state); 518ce263fc8SMatthias Ringwald } 519ce263fc8SMatthias Ringwald 520cf75be85SMilanka Ringwald static int hfp_ag_send_enhanced_voice_recognition_state_cmd(hfp_connection_t * hfp_connection){ 521cf75be85SMilanka Ringwald char buffer[30]; 522fcf4ede6SMilanka Ringwald uint8_t evra_enabled = hfp_connection->enhanced_voice_recognition_enabled ? 1 : 0; 523fcf4ede6SMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, evra_enabled, hfp_connection->ag_vra_state); 524cf75be85SMilanka Ringwald return send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 525cf75be85SMilanka Ringwald } 526cf75be85SMilanka Ringwald 527d1b50446SMilanka Ringwald static int hfp_ag_send_voice_recognition_cmd(hfp_connection_t * hfp_connection, uint8_t activate_voice_recognition){ 52845796ff1SMilanka Ringwald char buffer[30]; 529d1b50446SMilanka Ringwald if (hfp_connection->enhanced_voice_recognition_enabled){ 530d1b50446SMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition, hfp_connection->ag_vra_state); 531d1b50446SMilanka Ringwald } else { 532d1b50446SMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: %d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition); 533d1b50446SMilanka Ringwald } 534d1b50446SMilanka Ringwald return send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 53545796ff1SMilanka Ringwald } 53645796ff1SMilanka Ringwald 53745796ff1SMilanka Ringwald static int hfp_ag_send_enhanced_voice_recognition_msg_cmd(hfp_connection_t * hfp_connection){ 538a2e701c0SMilanka Ringwald char buffer[HFP_VR_TEXT_HEADER_SIZE + HFP_MAX_VR_TEXT_SIZE]; 53945796ff1SMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: 1,%d,%X,%d,%d,\"%s\"\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, 54045796ff1SMilanka Ringwald hfp_connection->ag_vra_state, 54145796ff1SMilanka Ringwald hfp_connection->ag_msg.text_id, 54245796ff1SMilanka Ringwald hfp_connection->ag_msg.text_type, 54345796ff1SMilanka Ringwald hfp_connection->ag_msg.text_operation, 54445796ff1SMilanka Ringwald hfp_connection->ag_msg.text); 54545796ff1SMilanka Ringwald return send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 54645796ff1SMilanka Ringwald } 54745796ff1SMilanka Ringwald 548a0ffb263SMatthias Ringwald static uint8_t hfp_ag_suggest_codec(hfp_connection_t *hfp_connection){ 549bc1b1537SMilanka Ringwald if (hfp_connection->sco_for_msbc_failed) return HFP_CODEC_CVSD; 550bc1b1537SMilanka Ringwald 551aeb0f0feSMatthias Ringwald if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_ag_codecs_nr, hfp_ag_codecs)){ 5526a7f44bdSMilanka Ringwald if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){ 553df327ff3SMilanka Ringwald return HFP_CODEC_MSBC; 5543deb3ec6SMatthias Ringwald } 5553deb3ec6SMatthias Ringwald } 556df327ff3SMilanka Ringwald return HFP_CODEC_CVSD; 5573deb3ec6SMatthias Ringwald } 5583deb3ec6SMatthias Ringwald 55976cc1527SMatthias Ringwald /* state machines */ 56076cc1527SMatthias Ringwald 561afac2a04SMilanka Ringwald static uint8_t hfp_ag_esco_s4_supported(hfp_connection_t * hfp_connection){ 562aeb0f0feSMatthias Ringwald return (hfp_connection->remote_supported_features & (1<<HFP_HFSF_ESCO_S4)) && (hfp_ag_supported_features & (1 << HFP_AGSF_ESCO_S4)); 5637522e673SMatthias Ringwald } 5647522e673SMatthias Ringwald 565a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){ 566aa4dd815SMatthias Ringwald /* events ( == commands): 567aa4dd815SMatthias Ringwald HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs 568aa4dd815SMatthias Ringwald HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 569aa4dd815SMatthias Ringwald hf_trigger_codec_connection_setup == received BCC 570aa4dd815SMatthias Ringwald ag_trigger_codec_connection_setup == received from AG to send BCS 571aa4dd815SMatthias Ringwald HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS 572aa4dd815SMatthias Ringwald */ 573a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 5743e3b9207SMilanka Ringwald case HFP_CODECS_EXCHANGED: 5753e3b9207SMilanka Ringwald if (hfp_connection->command == HFP_CMD_AVAILABLE_CODECS){ 5763e3b9207SMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 5773e3b9207SMilanka Ringwald return 1; 5783e3b9207SMilanka Ringwald } 5793e3b9207SMilanka Ringwald break; 5803e3b9207SMilanka Ringwald 581aa4dd815SMatthias Ringwald case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 582a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC; 583aa4dd815SMatthias Ringwald break; 584aa4dd815SMatthias Ringwald case HFP_CODECS_AG_RESEND_COMMON_CODEC: 585a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC; 586aa4dd815SMatthias Ringwald break; 587aa4dd815SMatthias Ringwald default: 588aa4dd815SMatthias Ringwald break; 589aa4dd815SMatthias Ringwald } 590aa4dd815SMatthias Ringwald 591a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 592aa4dd815SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 593a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){ 594a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_LIST; 595485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 596aa4dd815SMatthias Ringwald return 1; 597aa4dd815SMatthias Ringwald } 598aa4dd815SMatthias Ringwald 599a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 600aa4dd815SMatthias Ringwald case HFP_CODECS_AG_SENT_COMMON_CODEC: 601a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_AG_RESEND_COMMON_CODEC; 602aa4dd815SMatthias Ringwald break; 603aa4dd815SMatthias Ringwald default: 604aa4dd815SMatthias Ringwald break; 605aa4dd815SMatthias Ringwald } 606485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 607aa4dd815SMatthias Ringwald return 1; 608aa4dd815SMatthias Ringwald 609aa4dd815SMatthias Ringwald case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 610a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 61140e8d6c5SMatthias Ringwald hfp_connection->establish_audio_connection = 1; 612485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 613aa4dd815SMatthias Ringwald return 1; 614aa4dd815SMatthias Ringwald 615aa4dd815SMatthias Ringwald case HFP_CMD_AG_SEND_COMMON_CODEC: 616a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_AG_SENT_COMMON_CODEC; 617a0ffb263SMatthias Ringwald hfp_connection->suggested_codec = hfp_ag_suggest_codec(hfp_connection); 618485ac19eSMilanka Ringwald hfp_ag_send_suggest_codec_cmd(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec); 619aa4dd815SMatthias Ringwald return 1; 620aa4dd815SMatthias Ringwald 621aa4dd815SMatthias Ringwald case HFP_CMD_HF_CONFIRMED_CODEC: 622a0ffb263SMatthias Ringwald if (hfp_connection->codec_confirmed != hfp_connection->suggested_codec){ 623a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_ERROR; 624485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 625aa4dd815SMatthias Ringwald return 1; 626aa4dd815SMatthias Ringwald } 627a0ffb263SMatthias Ringwald hfp_connection->negotiated_codec = hfp_connection->codec_confirmed; 628a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 629c1ab6cc1SMatthias Ringwald log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD"); 630485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 6317522e673SMatthias Ringwald // now, pick link settings 632afac2a04SMilanka Ringwald hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection)); 6333721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP 6343721a235SMatthias Ringwald hfp_cc256x_prepare_for_sco(hfp_connection); 6353721a235SMatthias Ringwald #endif 636aa4dd815SMatthias Ringwald return 1; 637aa4dd815SMatthias Ringwald default: 638aa4dd815SMatthias Ringwald break; 639aa4dd815SMatthias Ringwald } 640aa4dd815SMatthias Ringwald return 0; 641aa4dd815SMatthias Ringwald } 642aa4dd815SMatthias Ringwald 643a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){ 644a0ffb263SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 645ca59be51SMatthias Ringwald hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr); 646aa4dd815SMatthias Ringwald 647a0ffb263SMatthias Ringwald // if active call exist, set per-hfp_connection state active, too (when audio is on) 648d0c20769SMatthias Ringwald if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 649a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE; 650aa4dd815SMatthias Ringwald } 651ce263fc8SMatthias Ringwald // if AG is ringing, also start ringing on the HF 652c4f8fc1cSMilanka Ringwald if (hfp_gsm_call_status() == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS){ 653c4f8fc1cSMilanka Ringwald switch (hfp_gsm_callsetup_status()){ 654c4f8fc1cSMilanka Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 6552ff1d7d7SMilanka Ringwald hfp_ag_hf_start_ringing_incoming(hfp_connection); 656c4f8fc1cSMilanka Ringwald break; 657fcd59412SMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 658fcd59412SMilanka Ringwald hfp_ag_hf_start_ringing_outgoing(hfp_connection); 659fcd59412SMilanka Ringwald break; 660c4f8fc1cSMilanka Ringwald default: 661c4f8fc1cSMilanka Ringwald break; 662c4f8fc1cSMilanka Ringwald } 663ce263fc8SMatthias Ringwald } 664aa4dd815SMatthias Ringwald } 6653deb3ec6SMatthias Ringwald 666a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){ 667c79b4cabSMatthias Ringwald // log_info("hfp_ag_run_for_context_service_level_connection state %u, command %u", hfp_connection->state, hfp_connection->command); 668a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 669c04cf7ffSMilanka Ringwald int sent = 0; 670a0ffb263SMatthias Ringwald switch(hfp_connection->command){ 6713deb3ec6SMatthias Ringwald case HFP_CMD_SUPPORTED_FEATURES: 672a0ffb263SMatthias Ringwald switch(hfp_connection->state){ 6733deb3ec6SMatthias Ringwald case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 674aa4dd815SMatthias Ringwald case HFP_EXCHANGE_SUPPORTED_FEATURES: 675aeb0f0feSMatthias Ringwald hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_ag_codecs, &hfp_ag_codecs_nr); 676a0ffb263SMatthias Ringwald if (has_codec_negotiation_feature(hfp_connection)){ 677a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS; 678aa4dd815SMatthias Ringwald } else { 679a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 680aa4dd815SMatthias Ringwald } 681a0ffb263SMatthias Ringwald hfp_ag_exchange_supported_features_cmd(hfp_connection->rfcomm_cid); 682aa4dd815SMatthias Ringwald return 1; 6833deb3ec6SMatthias Ringwald default: 6843deb3ec6SMatthias Ringwald break; 6853deb3ec6SMatthias Ringwald } 6863deb3ec6SMatthias Ringwald break; 6873deb3ec6SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 688c04cf7ffSMilanka Ringwald sent = codecs_exchange_state_machine(hfp_connection); 689a0ffb263SMatthias Ringwald if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){ 690a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 6913deb3ec6SMatthias Ringwald } 692c04cf7ffSMilanka Ringwald return sent; 693aa4dd815SMatthias Ringwald 694aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 695a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_W4_RETRIEVE_INDICATORS) { 696473ac565SMatthias Ringwald // HF requested AG Indicators and we did expect it 69784a0c24eSMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0; 698473ac565SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 699473ac565SMatthias Ringwald // continue below in state switch 700ad902e3dSMatthias Ringwald } 701ad902e3dSMatthias Ringwald break; 702aa4dd815SMatthias Ringwald 703aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 704a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break; 705a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE; 706485ac19eSMilanka Ringwald hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid); 707aa4dd815SMatthias Ringwald return 1; 708aa4dd815SMatthias Ringwald 7093deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 710a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break; 711a0ffb263SMatthias Ringwald if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 712a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL; 713a0ffb263SMatthias Ringwald } else if (has_hf_indicators_feature(hfp_connection)){ 714a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 715aa4dd815SMatthias Ringwald } else { 716a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 7173deb3ec6SMatthias Ringwald } 7188a46ec40SMatthias Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 719aa4dd815SMatthias Ringwald return 1; 7203deb3ec6SMatthias Ringwald 721aa4dd815SMatthias Ringwald case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES: 722a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break; 723a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 724a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 725aa4dd815SMatthias Ringwald } 726485ac19eSMilanka Ringwald hfp_ag_send_retrieve_can_hold_call_cmd(hfp_connection->rfcomm_cid); 727a0ffb263SMatthias Ringwald if (!has_hf_indicators_feature(hfp_connection)){ 728a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 729ce263fc8SMatthias Ringwald } 730aa4dd815SMatthias Ringwald return 1; 731aa4dd815SMatthias Ringwald 732aa4dd815SMatthias Ringwald case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 733a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break; 734a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS; 735485ac19eSMilanka Ringwald hfp_ag_send_list_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 736aa4dd815SMatthias Ringwald return 1; 737aa4dd815SMatthias Ringwald 738aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 739a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break; 740a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 741485ac19eSMilanka Ringwald hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 742aa4dd815SMatthias Ringwald return 1; 743aa4dd815SMatthias Ringwald 744aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 745a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break; 746a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 747485ac19eSMilanka Ringwald hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 748aa4dd815SMatthias Ringwald return 1; 7493deb3ec6SMatthias Ringwald default: 7503deb3ec6SMatthias Ringwald break; 7513deb3ec6SMatthias Ringwald } 752473ac565SMatthias Ringwald 753473ac565SMatthias Ringwald switch (hfp_connection->state){ 754473ac565SMatthias Ringwald case HFP_RETRIEVE_INDICATORS: { 755485ac19eSMilanka Ringwald int next_segment = hfp_ag_send_retrieve_indicators_cmd_via_generator(hfp_connection->rfcomm_cid, hfp_connection, hfp_connection->send_ag_indicators_segment); 756473ac565SMatthias Ringwald int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection); 757473ac565SMatthias Ringwald log_info("HFP_CMD_RETRIEVE_AG_INDICATORS next segment %u, num_segments %u", next_segment, num_segments); 758473ac565SMatthias Ringwald if (next_segment < num_segments){ 759473ac565SMatthias Ringwald // prepare sending of next segment 760473ac565SMatthias Ringwald hfp_connection->send_ag_indicators_segment = next_segment; 761473ac565SMatthias Ringwald log_info("HFP_CMD_RETRIEVE_AG_INDICATORS more. command %u, next seg %u", hfp_connection->command, next_segment); 762473ac565SMatthias Ringwald } else { 763473ac565SMatthias Ringwald // done, go to next state 764473ac565SMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0; 765473ac565SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS; 766473ac565SMatthias Ringwald } 767473ac565SMatthias Ringwald return 1; 768473ac565SMatthias Ringwald } 769473ac565SMatthias Ringwald default: 770473ac565SMatthias Ringwald break; 771473ac565SMatthias Ringwald } 772c04cf7ffSMilanka Ringwald return 0; 7733deb3ec6SMatthias Ringwald } 7743deb3ec6SMatthias Ringwald 7759cd26862SMilanka Ringwald static bool hfp_ag_vra_flag_supported(hfp_connection_t * hfp_connection){ 77614c148caSMilanka Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION); 77714c148caSMilanka Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION); 77814c148caSMilanka Ringwald return hf && ag; 77914c148caSMilanka Ringwald } 78014c148caSMilanka Ringwald 7819cd26862SMilanka Ringwald static bool hfp_ag_enhanced_vra_flag_supported(hfp_connection_t * hfp_connection){ 78214c148caSMilanka Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS); 78314c148caSMilanka Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS); 78414c148caSMilanka Ringwald return hf && ag; 78514c148caSMilanka Ringwald } 78614c148caSMilanka Ringwald 7879cd26862SMilanka Ringwald static bool hfp_ag_can_send_enhanced_vra_message_flag_supported(hfp_connection_t * hfp_connection){ 788f71d2aacSMilanka Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_VOICE_RECOGNITION_TEXT); 789f71d2aacSMilanka Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_TEXT); 790f71d2aacSMilanka Ringwald return hf && ag; 79108a0b01cSMilanka Ringwald } 792f71d2aacSMilanka Ringwald 793135d9718SMilanka Ringwald static bool hfp_ag_can_activate_voice_recognition(hfp_connection_t * hfp_connection){ 794f71d2aacSMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){ 795135d9718SMilanka Ringwald return false; 796d84719b1SMilanka Ringwald } 7970c738fd4SMilanka Ringwald if (hfp_connection->vra_state != HFP_VRA_VOICE_RECOGNITION_OFF){ 798135d9718SMilanka Ringwald return false; 799d84719b1SMilanka Ringwald } 800*08dc7cc6SMatthias Ringwald if (hfp_connection->ag_vra_send_command){ 801*08dc7cc6SMatthias Ringwald return false; 802*08dc7cc6SMatthias Ringwald } 803135d9718SMilanka Ringwald return true; 804d84719b1SMilanka Ringwald } 805d84719b1SMilanka Ringwald 806135d9718SMilanka Ringwald static bool hfp_ag_voice_recognition_session_active(hfp_connection_t * hfp_connection){ 80708a0b01cSMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){ 808135d9718SMilanka Ringwald return false; 80908a0b01cSMilanka Ringwald } 8100c738fd4SMilanka Ringwald if (hfp_connection->vra_state != HFP_VRA_VOICE_RECOGNITION_ACTIVATED){ 811de9e0ea7SMilanka Ringwald if (hfp_connection->vra_state == HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO){ 812135d9718SMilanka Ringwald return true; 813de9e0ea7SMilanka Ringwald } 814135d9718SMilanka Ringwald return false; 815d84719b1SMilanka Ringwald } 816135d9718SMilanka Ringwald return true; 817d84719b1SMilanka Ringwald } 818754910caSMilanka Ringwald 8190b4debbfSMilanka Ringwald static bool hfp_ag_is_audio_connection_active(hfp_connection_t * hfp_connection){ 8200b4debbfSMilanka Ringwald switch (hfp_connection->state){ 8210b4debbfSMilanka Ringwald case HFP_W2_CONNECT_SCO: 8220b4debbfSMilanka Ringwald case HFP_W4_SCO_CONNECTED: 8230b4debbfSMilanka Ringwald case HFP_AUDIO_CONNECTION_ESTABLISHED: 8240b4debbfSMilanka Ringwald return true; 825754910caSMilanka Ringwald default: 8260b4debbfSMilanka Ringwald return false; 827754910caSMilanka Ringwald } 828754910caSMilanka Ringwald } 829754910caSMilanka Ringwald 830b95cac54SMilanka Ringwald static void hfp_ag_emit_enhanced_voice_recognition_msg_sent_event(hfp_connection_t * hfp_connection, uint8_t status){ 831b95cac54SMilanka Ringwald hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 832b95cac54SMilanka Ringwald 833b95cac54SMilanka Ringwald uint8_t event[6]; 834b95cac54SMilanka Ringwald event[0] = HCI_EVENT_HFP_META; 835b95cac54SMilanka Ringwald event[1] = sizeof(event) - 2; 836c95b5b3cSMilanka Ringwald event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_MESSAGE_SENT; 837b95cac54SMilanka Ringwald little_endian_store_16(event, 3, acl_handle); 838b95cac54SMilanka Ringwald event[5] = status; 839b95cac54SMilanka Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 840b95cac54SMilanka Ringwald } 841b95cac54SMilanka Ringwald 842347d46c8SMilanka Ringwald 843585db78dSMilanka Ringwald static void hfp_ag_emit_hf_indicator_value(hfp_connection_t * hfp_connection, uint16_t uuid, uint8_t value){ 844347d46c8SMilanka Ringwald hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 845347d46c8SMilanka Ringwald 846347d46c8SMilanka Ringwald uint8_t event[8]; 847347d46c8SMilanka Ringwald event[0] = HCI_EVENT_HFP_META; 848347d46c8SMilanka Ringwald event[1] = sizeof(event) - 2; 849347d46c8SMilanka Ringwald event[2] = HFP_SUBEVENT_HF_INDICATOR; 850347d46c8SMilanka Ringwald little_endian_store_16(event, 3, acl_handle); 851347d46c8SMilanka Ringwald little_endian_store_16(event, 5, uuid); 852347d46c8SMilanka Ringwald event[7] = value; 853347d46c8SMilanka Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 854347d46c8SMilanka Ringwald } 855347d46c8SMilanka Ringwald 856a4f85bd2SMilanka Ringwald static void hfp_ag_emit_general_simple_event(uint8_t event_subtype){ 857a4f85bd2SMilanka Ringwald if (!hfp_ag_callback) return; 858a4f85bd2SMilanka Ringwald 859a4f85bd2SMilanka Ringwald uint8_t event[5]; 860a4f85bd2SMilanka Ringwald event[0] = HCI_EVENT_HFP_META; 861a4f85bd2SMilanka Ringwald event[1] = sizeof(event) - 2; 862a4f85bd2SMilanka Ringwald event[2] = event_subtype; 863a4f85bd2SMilanka Ringwald little_endian_store_16(event, 3, HCI_CON_HANDLE_INVALID); 864a4f85bd2SMilanka Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 865a4f85bd2SMilanka Ringwald } 866a4f85bd2SMilanka Ringwald 867774e3016SMatthias Ringwald // @return status 868774e3016SMatthias Ringwald static uint8_t hfp_ag_vra_state_machine_two(hfp_connection_t * hfp_connection){ 869774e3016SMatthias Ringwald uint8_t status = ERROR_CODE_SUCCESS; 870774e3016SMatthias Ringwald switch (hfp_connection->vra_state_requested){ 871774e3016SMatthias Ringwald case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 872774e3016SMatthias Ringwald if (!hfp_ag_is_audio_connection_active(hfp_connection)){ 873774e3016SMatthias Ringwald status = hfp_ag_setup_audio_connection(hfp_connection); 874774e3016SMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 875774e3016SMatthias Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 876774e3016SMatthias Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, status); 877774e3016SMatthias Ringwald break; 878774e3016SMatthias Ringwald } 879774e3016SMatthias Ringwald } 880774e3016SMatthias Ringwald hfp_connection->enhanced_voice_recognition_enabled = true; 881774e3016SMatthias Ringwald hfp_connection->vra_state = HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 882774e3016SMatthias Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 883774e3016SMatthias Ringwald hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_SUCCESS); 884774e3016SMatthias Ringwald break; 885774e3016SMatthias Ringwald 886774e3016SMatthias Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 887774e3016SMatthias Ringwald hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF; 888774e3016SMatthias Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 889774e3016SMatthias Ringwald 890774e3016SMatthias Ringwald // release audio connection only if it was opened after audio VR activated 891774e3016SMatthias Ringwald if (hfp_ag_is_audio_connection_active(hfp_connection) && !hfp_connection->ag_audio_connection_opened_before_vra){ 892774e3016SMatthias Ringwald hfp_trigger_release_audio_connection(hfp_connection); 893774e3016SMatthias Ringwald } 894774e3016SMatthias Ringwald 895774e3016SMatthias Ringwald hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS); 896774e3016SMatthias Ringwald break; 897774e3016SMatthias Ringwald 898774e3016SMatthias Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED: 899774e3016SMatthias Ringwald // open only if audio connection does not already exist 900774e3016SMatthias Ringwald if (!hfp_ag_is_audio_connection_active(hfp_connection)){ 901774e3016SMatthias Ringwald status = hfp_ag_setup_audio_connection(hfp_connection); 902774e3016SMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 903774e3016SMatthias Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 904774e3016SMatthias Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, status); 905774e3016SMatthias Ringwald break; 906774e3016SMatthias Ringwald } 907774e3016SMatthias Ringwald } 908774e3016SMatthias Ringwald 909774e3016SMatthias Ringwald hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_ACTIVATED; 910774e3016SMatthias Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 911774e3016SMatthias Ringwald hfp_connection->enhanced_voice_recognition_enabled = hfp_ag_enhanced_vra_flag_supported(hfp_connection); 912774e3016SMatthias Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS); 913774e3016SMatthias Ringwald break; 914774e3016SMatthias Ringwald 915774e3016SMatthias Ringwald default: 916774e3016SMatthias Ringwald break; 917774e3016SMatthias Ringwald } 918774e3016SMatthias Ringwald return status; 919774e3016SMatthias Ringwald } 920774e3016SMatthias Ringwald 921*08dc7cc6SMatthias Ringwald static uint8_t hfp_ag_vra_send_command(hfp_connection_t * hfp_connection){ 922*08dc7cc6SMatthias Ringwald uint8_t done = 0; 923754910caSMilanka Ringwald uint8_t status; 924754910caSMilanka Ringwald 925cf75be85SMilanka Ringwald switch (hfp_connection->vra_state_requested){ 926b95cac54SMilanka Ringwald case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_MSG: 927b95cac54SMilanka Ringwald done = hfp_ag_send_enhanced_voice_recognition_msg_cmd(hfp_connection); 928b95cac54SMilanka Ringwald if (done == 0){ 929b95cac54SMilanka Ringwald hfp_ag_emit_enhanced_voice_recognition_msg_sent_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 930b95cac54SMilanka Ringwald } else { 931b95cac54SMilanka Ringwald hfp_ag_emit_enhanced_voice_recognition_msg_sent_event(hfp_connection, ERROR_CODE_SUCCESS); 932b95cac54SMilanka Ringwald } 933b95cac54SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 934b95cac54SMilanka Ringwald return done; 935b95cac54SMilanka Ringwald 936cf75be85SMilanka Ringwald case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_STATUS: 937cf75be85SMilanka Ringwald done = hfp_ag_send_enhanced_voice_recognition_state_cmd(hfp_connection); 938cf75be85SMilanka Ringwald if (done == 0){ 939cf75be85SMilanka Ringwald hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 940cf75be85SMilanka Ringwald } else { 941cf75be85SMilanka Ringwald hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS); 942cf75be85SMilanka Ringwald } 943cf75be85SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 944b95cac54SMilanka Ringwald return done; 945b95cac54SMilanka Ringwald 9466e13e408SMatthias Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED: 9476e13e408SMatthias Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 948d1b50446SMilanka Ringwald done = hfp_ag_send_voice_recognition_cmd(hfp_connection, hfp_connection->ag_activate_voice_recognition_value); 9490c738fd4SMilanka Ringwald if (done == 0){ 9500c738fd4SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 951553a4a56SMilanka Ringwald 952553a4a56SMilanka Ringwald if (hfp_connection->ag_activate_voice_recognition_value == 1){ 953553a4a56SMilanka Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, done); 954553a4a56SMilanka Ringwald } else { 955553a4a56SMilanka Ringwald hfp_emit_voice_recognition_disabled(hfp_connection, done); 956553a4a56SMilanka Ringwald } 957fd4151d1SMilanka Ringwald return 0; 958fd4151d1SMilanka Ringwald } 959*08dc7cc6SMatthias Ringwald status = hfp_ag_vra_state_machine_two(hfp_connection); 960*08dc7cc6SMatthias Ringwald return (status == ERROR_CODE_SUCCESS) ? 1 : 0; 961*08dc7cc6SMatthias Ringwald 9626e13e408SMatthias Ringwald default: 9636e13e408SMatthias Ringwald log_error("state %u", hfp_connection->vra_state_requested); 9646e13e408SMatthias Ringwald btstack_assert(false); 965*08dc7cc6SMatthias Ringwald return 0; 966cf75be85SMilanka Ringwald } 967*08dc7cc6SMatthias Ringwald } 968*08dc7cc6SMatthias Ringwald 969*08dc7cc6SMatthias Ringwald static int hfp_ag_voice_recognition_state_machine(hfp_connection_t * hfp_connection){ 970*08dc7cc6SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) { 971*08dc7cc6SMatthias Ringwald return 0; 972*08dc7cc6SMatthias Ringwald } 973*08dc7cc6SMatthias Ringwald int done = 0; 974*08dc7cc6SMatthias Ringwald uint8_t status; 975*08dc7cc6SMatthias Ringwald 976*08dc7cc6SMatthias Ringwald switch (hfp_connection->command){ 977*08dc7cc6SMatthias Ringwald case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION: 978*08dc7cc6SMatthias Ringwald return hfp_ag_vra_send_command(hfp_connection); 979fd4151d1SMilanka Ringwald 980fd4151d1SMilanka Ringwald case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION: 981734ef2bdSMilanka Ringwald // HF initiatied voice recognition, parser extracted activation value 982de9e0ea7SMilanka Ringwald switch (hfp_connection->ag_activate_voice_recognition_value){ 983de9e0ea7SMilanka Ringwald case 0: 984135d9718SMilanka Ringwald if (hfp_ag_voice_recognition_session_active(hfp_connection)){ 985fd4151d1SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF; 986fd4151d1SMilanka Ringwald done = hfp_ag_send_ok(hfp_connection->rfcomm_cid); 987fd4151d1SMilanka Ringwald } 988de9e0ea7SMilanka Ringwald break; 989de9e0ea7SMilanka Ringwald 990de9e0ea7SMilanka Ringwald case 1: 991135d9718SMilanka Ringwald if (hfp_ag_can_activate_voice_recognition(hfp_connection)){ 992fd4151d1SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED; 993fd4151d1SMilanka Ringwald done = hfp_ag_send_ok(hfp_connection->rfcomm_cid); 994fd4151d1SMilanka Ringwald } 995de9e0ea7SMilanka Ringwald break; 996de9e0ea7SMilanka Ringwald 997de9e0ea7SMilanka Ringwald case 2: 998135d9718SMilanka Ringwald if (hfp_ag_voice_recognition_session_active(hfp_connection)){ 999de9e0ea7SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 1000de9e0ea7SMilanka Ringwald done = hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1001de9e0ea7SMilanka Ringwald } 1002de9e0ea7SMilanka Ringwald break; 1003de9e0ea7SMilanka Ringwald default: 1004de9e0ea7SMilanka Ringwald break; 1005fd4151d1SMilanka Ringwald } 1006fd4151d1SMilanka Ringwald if (done == 0){ 1007fd4151d1SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 1008fd4151d1SMilanka Ringwald done = hfp_ag_send_error(hfp_connection->rfcomm_cid); 1009fd4151d1SMilanka Ringwald return 1; 1010fd4151d1SMilanka Ringwald } 1011fd4151d1SMilanka Ringwald break; 1012fd4151d1SMilanka Ringwald 1013fd4151d1SMilanka Ringwald default: 1014754910caSMilanka Ringwald return 0; 1015754910caSMilanka Ringwald } 1016754910caSMilanka Ringwald 1017774e3016SMatthias Ringwald status = hfp_ag_vra_state_machine_two(hfp_connection); 1018de9e0ea7SMilanka Ringwald if (status != ERROR_CODE_SUCCESS){ 1019de9e0ea7SMilanka Ringwald return 0; 1020de9e0ea7SMilanka Ringwald } 1021754910caSMilanka Ringwald return done; 1022754910caSMilanka Ringwald } 1023754910caSMilanka Ringwald 1024a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){ 1025c04cf7ffSMilanka Ringwald int sent = codecs_exchange_state_machine(hfp_connection); 1026c04cf7ffSMilanka Ringwald if (sent) return 1; 1027aa4dd815SMatthias Ringwald 1028a0ffb263SMatthias Ringwald switch(hfp_connection->command){ 102945796ff1SMilanka Ringwald 1030aa4dd815SMatthias Ringwald case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING: 1031485ac19eSMilanka Ringwald hfp_ag_send_change_in_band_ring_tone_setting_cmd(hfp_connection->rfcomm_cid); 1032aa4dd815SMatthias Ringwald return 1; 1033aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1034485ac19eSMilanka Ringwald hfp_ag_send_report_network_operator_name_cmd(hfp_connection->rfcomm_cid, hfp_connection->network_operator); 1035aa4dd815SMatthias Ringwald return 1; 1036aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 1037a0ffb263SMatthias Ringwald if (hfp_connection->network_operator.format != 0){ 1038485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 1039aa4dd815SMatthias Ringwald } else { 1040485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 10413deb3ec6SMatthias Ringwald } 1042aa4dd815SMatthias Ringwald return 1; 1043aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE: 1044485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1045aa4dd815SMatthias Ringwald return 1; 10463deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR: 1047a0ffb263SMatthias Ringwald if (hfp_connection->extended_audio_gateway_error){ 1048a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 1049485ac19eSMilanka Ringwald hfp_ag_send_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, hfp_connection->extended_audio_gateway_error_value); 1050aa4dd815SMatthias Ringwald return 1; 10513deb3ec6SMatthias Ringwald } 1052202c8a4cSMatthias Ringwald break; 10533deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 1054485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1055ce263fc8SMatthias Ringwald return 1; 10563deb3ec6SMatthias Ringwald default: 10573deb3ec6SMatthias Ringwald break; 10583deb3ec6SMatthias Ringwald } 1059aa4dd815SMatthias Ringwald return 0; 10603deb3ec6SMatthias Ringwald } 10613deb3ec6SMatthias Ringwald 1062a0ffb263SMatthias Ringwald static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){ 1063505f1c30SMatthias Ringwald if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) || 1064505f1c30SMatthias Ringwald (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0; 10653deb3ec6SMatthias Ringwald 1066a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 106752cbdd6dSMilanka Ringwald 106852cbdd6dSMilanka Ringwald if (hfp_connection->release_audio_connection){ 106952cbdd6dSMilanka Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 107052cbdd6dSMilanka Ringwald hfp_connection->release_audio_connection = 0; 107152cbdd6dSMilanka Ringwald gap_disconnect(hfp_connection->sco_handle); 107252cbdd6dSMilanka Ringwald return 1; 107352cbdd6dSMilanka Ringwald } 1074aa4dd815SMatthias Ringwald 107529cddf58SMatthias Ringwald // accept incoming audio connection (codec negotiation is not used) 107629cddf58SMatthias Ringwald if (hfp_connection->accept_sco){ 107729cddf58SMatthias Ringwald // notify about codec selection if not done already 107829cddf58SMatthias Ringwald if (hfp_connection->negotiated_codec == 0){ 107929cddf58SMatthias Ringwald hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 108029cddf58SMatthias Ringwald } 108129cddf58SMatthias Ringwald // 108229cddf58SMatthias Ringwald bool incoming_eSCO = hfp_connection->accept_sco == 2; 108329cddf58SMatthias Ringwald hfp_connection->accept_sco = 0; 108429cddf58SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 108529cddf58SMatthias Ringwald hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO); 108629cddf58SMatthias Ringwald return 1; 108729cddf58SMatthias Ringwald } 108829cddf58SMatthias Ringwald 1089aa4dd815SMatthias Ringwald // run codecs exchange 1090c04cf7ffSMilanka Ringwald int sent = codecs_exchange_state_machine(hfp_connection); 1091c04cf7ffSMilanka Ringwald if (sent) return 1; 1092aa4dd815SMatthias Ringwald 1093c04cf7ffSMilanka Ringwald if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0; 1094a0ffb263SMatthias Ringwald if (hfp_connection->establish_audio_connection){ 1095a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 1096a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 1097eddcd308SMatthias Ringwald hfp_setup_synchronous_connection(hfp_connection); 1098aa4dd815SMatthias Ringwald return 1; 1099aa4dd815SMatthias Ringwald } 1100aa4dd815SMatthias Ringwald return 0; 1101aa4dd815SMatthias Ringwald } 1102aa4dd815SMatthias Ringwald 1103056efd45SMatthias Ringwald static void hfp_ag_set_callsetup_indicator(void){ 1104056efd45SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup"); 1105056efd45SMatthias Ringwald if (!indicator){ 1106056efd45SMatthias Ringwald log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing"); 1107056efd45SMatthias Ringwald return; 1108056efd45SMatthias Ringwald }; 1109056efd45SMatthias Ringwald indicator->status = hfp_gsm_callsetup_status(); 1110056efd45SMatthias Ringwald } 1111056efd45SMatthias Ringwald 1112056efd45SMatthias Ringwald static void hfp_ag_set_callheld_indicator(void){ 1113056efd45SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld"); 1114056efd45SMatthias Ringwald if (!indicator){ 1115056efd45SMatthias Ringwald log_error("hfp_ag_set_callheld_state: callheld indicator is missing"); 1116056efd45SMatthias Ringwald return; 1117056efd45SMatthias Ringwald }; 1118056efd45SMatthias Ringwald indicator->status = hfp_gsm_callheld_status(); 1119056efd45SMatthias Ringwald } 1120056efd45SMatthias Ringwald 1121aa4dd815SMatthias Ringwald // 1122fcd59412SMilanka Ringwald // transition implementations for hfp_ag_call_state_machine 1123aa4dd815SMatthias Ringwald // 1124aa4dd815SMatthias Ringwald 11252ff1d7d7SMilanka Ringwald static void hfp_ag_hf_start_ringing_incoming(hfp_connection_t * hfp_connection){ 1126aa4dd815SMatthias Ringwald if (use_in_band_tone()){ 1127a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING; 1128d97d752dSMilanka Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 1129aa4dd815SMatthias Ringwald } else { 11309493406bSMilanka Ringwald hfp_connection->call_state = HFP_CALL_INCOMING_RINGING; 1131aa4dd815SMatthias Ringwald } 1132aa4dd815SMatthias Ringwald } 1133aa4dd815SMatthias Ringwald 1134fcd59412SMilanka Ringwald static void hfp_ag_hf_start_ringing_outgoing(hfp_connection_t * hfp_connection){ 1135fcd59412SMilanka Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING; 1136fcd59412SMilanka Ringwald } 1137fcd59412SMilanka Ringwald 1138a0ffb263SMatthias Ringwald static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){ 1139a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 0; 1140a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = 0; 1141aa4dd815SMatthias Ringwald } 1142aa4dd815SMatthias Ringwald 1143a4f85bd2SMilanka Ringwald 114431f55b91SMilanka Ringwald static void hfp_ag_trigger_incoming_call_during_active_one(void){ 114531f55b91SMilanka Ringwald btstack_linked_list_iterator_t it; 114631f55b91SMilanka Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 114731f55b91SMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 114831f55b91SMilanka Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 114931f55b91SMilanka Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 115031f55b91SMilanka Ringwald if (hfp_connection->call_state != HFP_CALL_ACTIVE) continue; 115131f55b91SMilanka Ringwald 115231f55b91SMilanka Ringwald hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING; 115331f55b91SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 115431f55b91SMilanka Ringwald } 115531f55b91SMilanka Ringwald } 115631f55b91SMilanka Ringwald 1157447b8216SMilanka Ringwald static void hfp_ag_trigger_ring_and_clip(void) { 1158a4f85bd2SMilanka Ringwald btstack_linked_list_iterator_t it; 1159a4f85bd2SMilanka Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1160a4f85bd2SMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1161a4f85bd2SMilanka Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1162a4f85bd2SMilanka Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1163b7e27351SMilanka Ringwald switch (hfp_connection->call_state){ 1164b7e27351SMilanka Ringwald case HFP_CALL_INCOMING_RINGING: 1165b7e27351SMilanka Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING: 1166b7e27351SMilanka Ringwald case HFP_CALL_OUTGOING_RINGING: 11673561686eSMatthias Ringwald // Queue RING on this connection 1168a4f85bd2SMilanka Ringwald hfp_connection->ag_ring = 1; 11693561686eSMatthias Ringwald 11703561686eSMatthias Ringwald // HFP v1.8, 4.23 Calling Line Identification (CLI) Notification 11713561686eSMatthias Ringwald // "If the calling subscriber number information is available from the network, the AG shall issue the 11723561686eSMatthias Ringwald // +CLIP unsolicited result code just after every RING indication when the HF is alerted in an incoming call." 11733561686eSMatthias Ringwald hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled; 11743561686eSMatthias Ringwald 11753561686eSMatthias Ringwald // trigger next message 1176a4f85bd2SMilanka Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 1177b7e27351SMilanka Ringwald break; 1178b7e27351SMilanka Ringwald default: 1179b7e27351SMilanka Ringwald break; 1180b7e27351SMilanka Ringwald } 1181a4f85bd2SMilanka Ringwald } 1182a4f85bd2SMilanka Ringwald } 1183a4f85bd2SMilanka Ringwald 118450b9afa4SMilanka Ringwald // trigger RING and CLIP messages on connections that are ringing 118550b9afa4SMilanka Ringwald static void hfp_ag_ring_timeout_handler(btstack_timer_source_t * timer){ 118650b9afa4SMilanka Ringwald hfp_ag_trigger_ring_and_clip(); 118750b9afa4SMilanka Ringwald 11886db648bcSMilanka Ringwald btstack_run_loop_set_timer(timer, HFP_RING_PERIOD_MS); // 2 seconds timeout 118950b9afa4SMilanka Ringwald btstack_run_loop_add_timer(timer); 1190a4f85bd2SMilanka Ringwald } 1191a4f85bd2SMilanka Ringwald 1192a4f85bd2SMilanka Ringwald static void hfp_ag_ring_timeout_stop(void){ 1193a4f85bd2SMilanka Ringwald btstack_run_loop_remove_timer(&hfp_ag_ring_timeout); 1194a4f85bd2SMilanka Ringwald } 1195a4f85bd2SMilanka Ringwald 1196a4f85bd2SMilanka Ringwald static void hfp_ag_start_ringing(void){ 119750b9afa4SMilanka Ringwald // setup ring timer 119850b9afa4SMilanka Ringwald btstack_run_loop_remove_timer(&hfp_ag_ring_timeout); 119950b9afa4SMilanka Ringwald btstack_run_loop_set_timer_handler(&hfp_ag_ring_timeout, hfp_ag_ring_timeout_handler); 12006db648bcSMilanka Ringwald btstack_run_loop_set_timer(&hfp_ag_ring_timeout, HFP_RING_PERIOD_MS); // 2 seconds timeout 120150b9afa4SMilanka Ringwald btstack_run_loop_add_timer(&hfp_ag_ring_timeout); 120250b9afa4SMilanka Ringwald 120350b9afa4SMilanka Ringwald // emit start ringing 1204a4f85bd2SMilanka Ringwald hfp_ag_emit_general_simple_event(HFP_SUBEVENT_START_RINGING); 120550b9afa4SMilanka Ringwald 120650b9afa4SMilanka Ringwald // send initial RING + CLIP 120750b9afa4SMilanka Ringwald hfp_ag_trigger_ring_and_clip(); 1208a4f85bd2SMilanka Ringwald } 1209a4f85bd2SMilanka Ringwald 1210a4f85bd2SMilanka Ringwald static void hfp_ag_stop_ringing(void){ 1211a4f85bd2SMilanka Ringwald hfp_ag_ring_timeout_stop(); 1212a4f85bd2SMilanka Ringwald hfp_ag_emit_general_simple_event(HFP_SUBEVENT_STOP_RINGING); 1213a4f85bd2SMilanka Ringwald 1214a4f85bd2SMilanka Ringwald btstack_linked_list_iterator_t it; 1215a4f85bd2SMilanka Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1216a4f85bd2SMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1217a4f85bd2SMilanka Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1218a4f85bd2SMilanka Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1219a4f85bd2SMilanka Ringwald if ((hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) && 1220a4f85bd2SMilanka Ringwald (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue; 1221a4f85bd2SMilanka Ringwald hfp_ag_hf_stop_ringing(hfp_connection); 1222a4f85bd2SMilanka Ringwald } 1223a4f85bd2SMilanka Ringwald } 1224a4f85bd2SMilanka Ringwald 122531f55b91SMilanka Ringwald static void hfp_ag_trigger_incoming_call_idle(void){ 1226aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name("callsetup"); 1227aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 1228aa4dd815SMatthias Ringwald 12293561686eSMatthias Ringwald // update callsetup state in connections 1230665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1231665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1232665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1233a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 123466c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 123531f55b91SMilanka Ringwald if (hfp_connection->call_state != HFP_CALL_IDLE) continue; 123631f55b91SMilanka Ringwald 1237a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1238a4f85bd2SMilanka Ringwald 1239a4f85bd2SMilanka Ringwald hfp_ag_hf_start_ringing_incoming(hfp_connection); 1240a4f85bd2SMilanka Ringwald 1241f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1242aa4dd815SMatthias Ringwald } 12433561686eSMatthias Ringwald 12443561686eSMatthias Ringwald // start local ringing - started after connection state has been updated 12453561686eSMatthias Ringwald hfp_ag_start_ringing(); 1246aa4dd815SMatthias Ringwald } 1247aa4dd815SMatthias Ringwald 1248fe899794SMatthias Ringwald static void hfp_ag_trigger_outgoing_call(void){ 1249fe899794SMatthias Ringwald btstack_linked_list_iterator_t it; 1250fe899794SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1251fe899794SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1252fe899794SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1253fe899794SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1254fe899794SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 1255fe899794SMatthias Ringwald if (hfp_connection->call_state == HFP_CALL_IDLE){ 1256fe899794SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 1257fe899794SMatthias Ringwald } 1258fe899794SMatthias Ringwald } 1259fe899794SMatthias Ringwald } 12605a7033e2SMatthias Ringwald 12615a7033e2SMatthias Ringwald static void hfp_ag_handle_outgoing_call_accepted(hfp_connection_t * hfp_connection){ 12625a7033e2SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING; 12635a7033e2SMatthias Ringwald 12645a7033e2SMatthias Ringwald // trigger callsetup to be 12655a7033e2SMatthias Ringwald int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT; 12665a7033e2SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_ACCEPTED, 0, 0, NULL); 12675a7033e2SMatthias Ringwald 12685a7033e2SMatthias Ringwald int indicator_index ; 12695a7033e2SMatthias Ringwald 12705a7033e2SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 12715a7033e2SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callsetup"); 12725a7033e2SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 12735a7033e2SMatthias Ringwald 12745a7033e2SMatthias Ringwald // put current call on hold if active 12755a7033e2SMatthias Ringwald if (put_call_on_hold){ 12765a7033e2SMatthias Ringwald log_info("AG putting current call on hold for new outgoing call"); 12775a7033e2SMatthias Ringwald hfp_ag_set_callheld_indicator(); 12785a7033e2SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callheld"); 12795a7033e2SMatthias Ringwald hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]); 12805a7033e2SMatthias Ringwald } 12815a7033e2SMatthias Ringwald 12825a7033e2SMatthias Ringwald // start audio if needed 12835a7033e2SMatthias Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 12845a7033e2SMatthias Ringwald } 1285fe899794SMatthias Ringwald 12864a2e058fSMatthias Ringwald static void hfp_ag_transfer_indicator(const char * name){ 12874a2e058fSMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name(name); 1288aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 1289aa4dd815SMatthias Ringwald 1290665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1291665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1292665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1293a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 129466c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1295a0ffb263SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 1296a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1297f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1298aa4dd815SMatthias Ringwald } 1299aa4dd815SMatthias Ringwald } 1300aa4dd815SMatthias Ringwald 13014a2e058fSMatthias Ringwald static void hfp_ag_transfer_callsetup_state(void){ 13024a2e058fSMatthias Ringwald hfp_ag_transfer_indicator("callsetup"); 13034a2e058fSMatthias Ringwald } 13044a2e058fSMatthias Ringwald 1305aa4dd815SMatthias Ringwald static void hfp_ag_transfer_call_state(void){ 13064a2e058fSMatthias Ringwald hfp_ag_transfer_indicator("call"); 1307aa4dd815SMatthias Ringwald } 1308aa4dd815SMatthias Ringwald 1309aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callheld_state(void){ 13104a2e058fSMatthias Ringwald hfp_ag_transfer_indicator("callheld"); 1311aa4dd815SMatthias Ringwald } 1312aa4dd815SMatthias Ringwald 1313aa4dd815SMatthias Ringwald static void hfp_ag_hf_accept_call(hfp_connection_t * source){ 1314aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 1315aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1316aa4dd815SMatthias Ringwald 1317a4f85bd2SMilanka Ringwald hfp_ag_stop_ringing(); 1318a4f85bd2SMilanka Ringwald 1319665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1320665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1321665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1322a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 132366c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 13249493406bSMilanka Ringwald if ((hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) && 1325505f1c30SMatthias Ringwald (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue; 1326aa4dd815SMatthias Ringwald 1327a0ffb263SMatthias Ringwald if (hfp_connection == source){ 1328aa4dd815SMatthias Ringwald 1329aa4dd815SMatthias Ringwald if (use_in_band_tone()){ 1330a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1331aa4dd815SMatthias Ringwald } else { 1332a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE; 1333d97d752dSMilanka Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 1334aa4dd815SMatthias Ringwald } 1335aa4dd815SMatthias Ringwald 1336a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1337a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1338aa4dd815SMatthias Ringwald 1339aa4dd815SMatthias Ringwald } else { 1340a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 1341aa4dd815SMatthias Ringwald } 1342f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1343aa4dd815SMatthias Ringwald } 1344aa4dd815SMatthias Ringwald } 1345aa4dd815SMatthias Ringwald 1346aa4dd815SMatthias Ringwald static void hfp_ag_ag_accept_call(void){ 1347aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 1348aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1349aa4dd815SMatthias Ringwald 1350a4f85bd2SMilanka Ringwald hfp_ag_stop_ringing(); 1351a4f85bd2SMilanka Ringwald 1352665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1353665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1354665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1355a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 135666c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 13579493406bSMilanka Ringwald if (hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) continue; 1358aa4dd815SMatthias Ringwald 1359a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION; 1360aa4dd815SMatthias Ringwald 1361a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1362a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1363aa4dd815SMatthias Ringwald 1364f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1365aa4dd815SMatthias Ringwald break; // only single 1366aa4dd815SMatthias Ringwald } 1367aa4dd815SMatthias Ringwald } 1368aa4dd815SMatthias Ringwald 1369aa4dd815SMatthias Ringwald static void hfp_ag_trigger_reject_call(void){ 1370aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1371a4f85bd2SMilanka 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)){ 1377665d90f2SMatthias Ringwald hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 137866c5995fSMatthias Ringwald if (connection->local_role != HFP_ROLE_AG) continue; 13799493406bSMilanka Ringwald if ((connection->call_state != HFP_CALL_INCOMING_RINGING) && 1380505f1c30SMatthias Ringwald (connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue; 1381095a9f2bSMilanka Ringwald 1382aa4dd815SMatthias Ringwald connection->call_state = HFP_CALL_IDLE; 1383095a9f2bSMilanka Ringwald connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1384095a9f2bSMilanka Ringwald 1385095a9f2bSMilanka Ringwald if (connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 1386095a9f2bSMilanka Ringwald connection->release_audio_connection = 1; 1387095a9f2bSMilanka Ringwald } 1388095a9f2bSMilanka Ringwald hfp_emit_simple_event(connection, HFP_SUBEVENT_CALL_TERMINATED); 1389f0aeb307SMatthias Ringwald hfp_ag_run_for_context(connection); 1390095a9f2bSMilanka Ringwald rfcomm_request_can_send_now_event(connection->rfcomm_cid); 1391aa4dd815SMatthias Ringwald } 1392aa4dd815SMatthias Ringwald } 1393aa4dd815SMatthias Ringwald 1394aa4dd815SMatthias Ringwald static void hfp_ag_trigger_terminate_call(void){ 1395aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 1396aa4dd815SMatthias Ringwald 1397a4f85bd2SMilanka Ringwald // no ringing during call 1398a4f85bd2SMilanka Ringwald 1399665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1400665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1401665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1402a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 140366c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1404a0ffb263SMatthias Ringwald if (hfp_connection->call_state == HFP_CALL_IDLE) continue; 1405fb75b483SMilanka Ringwald 1406a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 1407a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1408095a9f2bSMilanka Ringwald 14096d78145cSMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 1410a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 1; 14116d78145cSMatthias Ringwald } 1412095a9f2bSMilanka Ringwald 1413fb75b483SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED); 1414f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1415a13ff7d1SMilanka Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 1416aa4dd815SMatthias Ringwald } 1417aa4dd815SMatthias Ringwald } 1418aa4dd815SMatthias Ringwald 14190cb5b971SMatthias Ringwald static void hfp_ag_set_call_indicator(void){ 1420aa4dd815SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call"); 1421aa4dd815SMatthias Ringwald if (!indicator){ 1422aa4dd815SMatthias Ringwald log_error("hfp_ag_set_call_state: call indicator is missing"); 142345718b6fSMatthias Ringwald return; 1424aa4dd815SMatthias Ringwald }; 1425d210d9c4SMatthias Ringwald indicator->status = hfp_gsm_call_status(); 1426aa4dd815SMatthias Ringwald } 1427aa4dd815SMatthias Ringwald 1428aa4dd815SMatthias Ringwald static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){ 1429665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1430665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1431665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1432a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 143366c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1434a0ffb263SMatthias Ringwald if (hfp_connection->call_state == call_state) return hfp_connection; 1435aa4dd815SMatthias Ringwald } 1436aa4dd815SMatthias Ringwald return NULL; 1437aa4dd815SMatthias Ringwald } 1438aa4dd815SMatthias Ringwald 1439a5bdcda8SMatthias Ringwald static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){ 1440665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1441665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1442665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1443a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 144466c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1445a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = state + 1; 1446ce263fc8SMatthias Ringwald } 1447ce263fc8SMatthias Ringwald } 1448ce263fc8SMatthias Ringwald 1449a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 1450aa4dd815SMatthias Ringwald int indicator_index; 1451a0ffb263SMatthias Ringwald switch (hfp_connection->call_state){ 1452aa4dd815SMatthias Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING: 1453a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 1454a4f85bd2SMilanka Ringwald 1455a0ffb263SMatthias Ringwald // we got event: audio hfp_connection established 14569493406bSMilanka Ringwald hfp_connection->call_state = HFP_CALL_INCOMING_RINGING; 1457aa4dd815SMatthias Ringwald break; 1458aa4dd815SMatthias Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE: 1459a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 1460a0ffb263SMatthias Ringwald // we got event: audio hfp_connection established 1461a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1462aa4dd815SMatthias Ringwald break; 1463aa4dd815SMatthias Ringwald case HFP_CALL_W2_SEND_CALL_WAITING: 1464a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_CHLD; 1465a0ffb263SMatthias Ringwald hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid); 1466aa4dd815SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callsetup"); 1467a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1468aa4dd815SMatthias Ringwald break; 1469aa4dd815SMatthias Ringwald default: 1470aa4dd815SMatthias Ringwald break; 1471aa4dd815SMatthias Ringwald } 1472aa4dd815SMatthias Ringwald return 0; 1473aa4dd815SMatthias Ringwald } 147439426febSMatthias Ringwald 147539426febSMatthias Ringwald // functions extracted from hfp_ag_call_sm below 14769f9621cfSMatthias Ringwald static void hfp_ag_handle_reject_outgoing_call(void){ 147739426febSMatthias Ringwald hfp_connection_t * hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 147839426febSMatthias Ringwald if (!hfp_connection){ 147939426febSMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state"); 148039426febSMatthias Ringwald return; 148139426febSMatthias Ringwald } 148239426febSMatthias Ringwald 1483f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_REJECTED, 0, 0, NULL); 148439426febSMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 148539426febSMatthias Ringwald hfp_connection->send_error = 1; 148639426febSMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 148739426febSMatthias Ringwald } 148839426febSMatthias Ringwald 1489a0ffb263SMatthias Ringwald // hfp_connection is used to identify originating HF 1490a0ffb263SMatthias Ringwald static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){ 1491d210d9c4SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1492d210d9c4SMatthias Ringwald int callheld_indicator_index = get_ag_indicator_index_for_name("callheld"); 1493d210d9c4SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 149474386ee0SMatthias Ringwald 1495aa4dd815SMatthias Ringwald switch (event){ 1496aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL: 1497d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1498aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1499d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 150012cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1501f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL, 0, 0, NULL); 1502d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 150331f55b91SMilanka Ringwald hfp_ag_trigger_incoming_call_idle(); 150460ebb071SMilanka Ringwald log_info("AG rings"); 1505aa4dd815SMatthias Ringwald break; 1506aa4dd815SMatthias Ringwald default: 15073deb3ec6SMatthias Ringwald break; 15083deb3ec6SMatthias Ringwald } 15093deb3ec6SMatthias Ringwald break; 1510aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1511d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 151212cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1513f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL, 0, 0, NULL); 1514d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 151531f55b91SMilanka Ringwald hfp_ag_trigger_incoming_call_during_active_one(); 151660ebb071SMilanka Ringwald log_info("AG call waiting"); 1517aa4dd815SMatthias Ringwald break; 1518aa4dd815SMatthias Ringwald default: 15193deb3ec6SMatthias Ringwald break; 15203deb3ec6SMatthias Ringwald } 15213deb3ec6SMatthias Ringwald break; 15227bbeb3adSMilanka Ringwald default: 15237bbeb3adSMilanka Ringwald break; 1524aa4dd815SMatthias Ringwald } 1525aa4dd815SMatthias Ringwald break; 1526aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG: 1527d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1528aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1529d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 153012cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1531f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, 0, 0, NULL); 1532d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1533d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1534aa4dd815SMatthias Ringwald hfp_ag_ag_accept_call(); 153560ebb071SMilanka Ringwald log_info("AG answers call, accept call by GSM"); 1536aa4dd815SMatthias Ringwald break; 1537aa4dd815SMatthias Ringwald default: 15383deb3ec6SMatthias Ringwald break; 15393deb3ec6SMatthias Ringwald } 1540aa4dd815SMatthias Ringwald break; 1541aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1542d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1543aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 154460ebb071SMilanka Ringwald log_info("AG: current call is placed on hold, incoming call gets active"); 1545f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, 0, 0, NULL); 1546d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1547d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1548ce263fc8SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1549ce263fc8SMatthias Ringwald hfp_ag_transfer_callheld_state(); 1550aa4dd815SMatthias Ringwald break; 1551aa4dd815SMatthias Ringwald default: 1552aa4dd815SMatthias Ringwald break; 1553aa4dd815SMatthias Ringwald } 1554aa4dd815SMatthias Ringwald break; 15557bbeb3adSMilanka Ringwald default: 15567bbeb3adSMilanka Ringwald break; 1557aa4dd815SMatthias Ringwald } 1558aa4dd815SMatthias Ringwald break; 1559ce263fc8SMatthias Ringwald 1560ce263fc8SMatthias Ringwald case HFP_AG_HELD_CALL_JOINED_BY_AG: 1561d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1562ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1563d0c20769SMatthias Ringwald switch (hfp_gsm_callheld_status()){ 1564ce263fc8SMatthias Ringwald case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED: 156560ebb071SMilanka Ringwald log_info("AG: joining held call with active call"); 1566f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_HELD_CALL_JOINED_BY_AG, 0, 0, NULL); 1567d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1568ce263fc8SMatthias Ringwald hfp_ag_transfer_callheld_state(); 1569ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL); 1570ce263fc8SMatthias Ringwald break; 1571ce263fc8SMatthias Ringwald default: 1572ce263fc8SMatthias Ringwald break; 1573ce263fc8SMatthias Ringwald } 1574ce263fc8SMatthias Ringwald break; 1575ce263fc8SMatthias Ringwald default: 1576ce263fc8SMatthias Ringwald break; 1577ce263fc8SMatthias Ringwald } 1578ce263fc8SMatthias Ringwald break; 1579ce263fc8SMatthias Ringwald 1580aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF: 1581d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1582aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1583d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 158412cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1585f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, 0, 0, NULL); 1586d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1587d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1588a0ffb263SMatthias Ringwald hfp_ag_hf_accept_call(hfp_connection); 15896323f788SMatthias Ringwald hfp_connection->ok_pending = 1; 159060ebb071SMilanka Ringwald log_info("HF answers call, accept call by GSM"); 1591ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED); 1592aa4dd815SMatthias Ringwald break; 1593aa4dd815SMatthias Ringwald default: 1594aa4dd815SMatthias Ringwald break; 1595aa4dd815SMatthias Ringwald } 15963deb3ec6SMatthias Ringwald break; 15973deb3ec6SMatthias Ringwald default: 15983deb3ec6SMatthias Ringwald break; 15993deb3ec6SMatthias Ringwald } 16003deb3ec6SMatthias Ringwald break; 16013deb3ec6SMatthias Ringwald 1602ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG: 1603d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1604ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1605d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 160612cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1607f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, 0, 0, NULL); 1608ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_active = 1; 1609ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1610a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1611ac6f828eSMilanka Ringwald // as with regular call 1612d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1613d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1614ce263fc8SMatthias Ringwald hfp_ag_ag_accept_call(); 161560ebb071SMilanka Ringwald log_info("AG response and hold - hold by AG"); 1616ce263fc8SMatthias Ringwald break; 1617ce263fc8SMatthias Ringwald default: 1618ce263fc8SMatthias Ringwald break; 1619ce263fc8SMatthias Ringwald } 1620ce263fc8SMatthias Ringwald break; 1621ce263fc8SMatthias Ringwald default: 1622ce263fc8SMatthias Ringwald break; 1623ce263fc8SMatthias Ringwald } 1624ce263fc8SMatthias Ringwald break; 1625ce263fc8SMatthias Ringwald 1626ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF: 1627d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1628ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1629d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 163012cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1631f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, 0, 0, NULL); 1632ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_active = 1; 1633ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1634a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1635ac6f828eSMilanka Ringwald // as with regular call 1636d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1637d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1638a0ffb263SMatthias Ringwald hfp_ag_hf_accept_call(hfp_connection); 163960ebb071SMilanka Ringwald log_info("AG response and hold - hold by HF"); 1640ce263fc8SMatthias Ringwald break; 1641ce263fc8SMatthias Ringwald default: 1642ce263fc8SMatthias Ringwald break; 1643ce263fc8SMatthias Ringwald } 1644ce263fc8SMatthias Ringwald break; 1645ce263fc8SMatthias Ringwald default: 1646ce263fc8SMatthias Ringwald break; 1647ce263fc8SMatthias Ringwald } 1648ce263fc8SMatthias Ringwald break; 1649ce263fc8SMatthias Ringwald 1650ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG: 1651ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF: 1652ce263fc8SMatthias Ringwald if (!hfp_ag_response_and_hold_active) break; 1653ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1654f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, 0, 0, NULL); 1655a5bdcda8SMatthias Ringwald hfp_ag_response_and_hold_active = 0; 1656ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED; 1657a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 165860ebb071SMilanka Ringwald log_info("Held Call accepted and active"); 1659ce263fc8SMatthias Ringwald break; 1660ce263fc8SMatthias Ringwald 1661ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG: 1662ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF: 1663ce263fc8SMatthias Ringwald if (!hfp_ag_response_and_hold_active) break; 1664ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1665f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, 0, 0, NULL); 1666a5bdcda8SMatthias Ringwald hfp_ag_response_and_hold_active = 0; 1667ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1668a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1669ce263fc8SMatthias Ringwald // from terminate by ag 1670d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1671ce263fc8SMatthias Ringwald hfp_ag_trigger_terminate_call(); 1672ce263fc8SMatthias Ringwald break; 1673ce263fc8SMatthias Ringwald 1674aa4dd815SMatthias Ringwald case HFP_AG_TERMINATE_CALL_BY_HF: 1675d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1676aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1677d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 167812cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1679f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL); 1680d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1681aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1682aa4dd815SMatthias Ringwald hfp_ag_trigger_reject_call(); 168360ebb071SMilanka Ringwald log_info("HF Rejected Incoming call, AG terminate call"); 1684aa4dd815SMatthias Ringwald break; 1685aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 168612cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1687f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL); 1688d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1689aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 169060ebb071SMilanka Ringwald log_info("AG terminate outgoing call process"); 1691202c8a4cSMatthias Ringwald break; 1692aa4dd815SMatthias Ringwald default: 1693aa4dd815SMatthias Ringwald break; 1694aa4dd815SMatthias Ringwald } 1695aa4dd815SMatthias Ringwald break; 169612cbbeeeSMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1697f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL); 16981ec112deSMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1699d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 17001ec112deSMatthias Ringwald hfp_ag_trigger_terminate_call(); 170160ebb071SMilanka Ringwald log_info("AG terminate call"); 1702aa4dd815SMatthias Ringwald break; 17037bbeb3adSMilanka Ringwald default: 17047bbeb3adSMilanka Ringwald break; 1705aa4dd815SMatthias Ringwald } 1706aa4dd815SMatthias Ringwald break; 17073deb3ec6SMatthias Ringwald 1708aa4dd815SMatthias Ringwald case HFP_AG_TERMINATE_CALL_BY_AG: 1709d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1710aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1711d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 171212cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1713f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_AG, 0, 0, NULL); 1714d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1715aa4dd815SMatthias Ringwald hfp_ag_trigger_reject_call(); 171660ebb071SMilanka Ringwald log_info("AG Rejected Incoming call, AG terminate call"); 1717aa4dd815SMatthias Ringwald break; 1718aa4dd815SMatthias Ringwald default: 1719aa4dd815SMatthias Ringwald break; 17203deb3ec6SMatthias Ringwald } 1721202c8a4cSMatthias Ringwald break; 172212cbbeeeSMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1723f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_AG, 0, 0, NULL); 1724d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1725d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1726aa4dd815SMatthias Ringwald hfp_ag_trigger_terminate_call(); 172760ebb071SMilanka Ringwald log_info("AG terminate call"); 1728aa4dd815SMatthias Ringwald break; 1729aa4dd815SMatthias Ringwald default: 17303deb3ec6SMatthias Ringwald break; 17313deb3ec6SMatthias Ringwald } 17323deb3ec6SMatthias Ringwald break; 1733aa4dd815SMatthias Ringwald case HFP_AG_CALL_DROPPED: 1734d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1735aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1736d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1737aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1738aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1739fb75b483SMilanka Ringwald hfp_ag_stop_ringing(); 1740fb75b483SMilanka Ringwald break; 1741fb75b483SMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 1742fb75b483SMilanka Ringwald log_info("Outgoing call interrupted\n"); 1743aa4dd815SMatthias Ringwald break; 1744aa4dd815SMatthias Ringwald default: 17453deb3ec6SMatthias Ringwald break; 17463deb3ec6SMatthias Ringwald } 1747f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL); 1748d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1749aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 175010c236bfSMatthias Ringwald hfp_ag_trigger_terminate_call(); 1751aa4dd815SMatthias Ringwald break; 1752aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1753ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_active) { 1754f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL); 1755ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1756a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1757ce263fc8SMatthias Ringwald } 1758f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL); 1759d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1760d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1761aa4dd815SMatthias Ringwald hfp_ag_trigger_terminate_call(); 176260ebb071SMilanka Ringwald log_info("AG notify call dropped"); 1763aa4dd815SMatthias Ringwald break; 1764aa4dd815SMatthias Ringwald default: 1765aa4dd815SMatthias Ringwald break; 1766aa4dd815SMatthias Ringwald } 1767aa4dd815SMatthias Ringwald break; 1768aa4dd815SMatthias Ringwald 17699ff73f41SMatthias Ringwald case HFP_AG_OUTGOING_CALL_INITIATED_BY_HF: 1770d210d9c4SMatthias Ringwald // directly reject call if number of free slots is exceeded 1771d210d9c4SMatthias Ringwald if (!hfp_gsm_call_possible()){ 1772a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1773f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1774d210d9c4SMatthias Ringwald break; 1775d210d9c4SMatthias Ringwald } 1776fe899794SMatthias Ringwald 1777fe899794SMatthias Ringwald // note: number not used currently 17789ff73f41SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_INITIATED_BY_HF, 0, 0, (const char *) &hfp_connection->line_buffer[3]); 17799cae807eSMatthias Ringwald 1780a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 178174386ee0SMatthias Ringwald 1782ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]); 1783aa4dd815SMatthias Ringwald break; 1784aa4dd815SMatthias Ringwald 1785fe899794SMatthias Ringwald case HFP_AG_OUTGOING_CALL_INITIATED_BY_AG: 1786fe899794SMatthias Ringwald // directly reject call if number of free slots is exceeded 1787fe899794SMatthias Ringwald if (!hfp_gsm_call_possible()) { 1788fe899794SMatthias Ringwald // TODO: no error for user 1789fe899794SMatthias Ringwald break; 1790fe899794SMatthias Ringwald } 1791fe899794SMatthias Ringwald switch (hfp_gsm_call_status()) { 1792fe899794SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1793fe899794SMatthias Ringwald switch (hfp_gsm_callsetup_status()) { 1794fe899794SMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1795fe899794SMatthias Ringwald // note: number not used currently 1796fe899794SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_INITIATED_BY_AG, 0, 0, "1234567"); 1797fe899794SMatthias Ringwald // init call state for all connections 1798fe899794SMatthias Ringwald hfp_ag_trigger_outgoing_call(); 1799fe899794SMatthias Ringwald // get first one and accept call 1800fe899794SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 1801fe899794SMatthias Ringwald if (hfp_connection) { 1802fe899794SMatthias Ringwald hfp_ag_handle_outgoing_call_accepted(hfp_connection); 1803fe899794SMatthias Ringwald } 1804fe899794SMatthias Ringwald break; 1805fe899794SMatthias Ringwald default: 1806fe899794SMatthias Ringwald // TODO: no error for user 1807fe899794SMatthias Ringwald break; 1808fe899794SMatthias Ringwald } 1809fe899794SMatthias Ringwald break; 1810fe899794SMatthias Ringwald default: 1811fe899794SMatthias Ringwald // TODO: no error for user 1812fe899794SMatthias Ringwald break; 1813fe899794SMatthias Ringwald } 1814fe899794SMatthias Ringwald break; 18159cae807eSMatthias Ringwald case HFP_AG_OUTGOING_REDIAL_INITIATED:{ 1816d210d9c4SMatthias Ringwald // directly reject call if number of free slots is exceeded 1817d210d9c4SMatthias Ringwald if (!hfp_gsm_call_possible()){ 1818a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1819f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1820d210d9c4SMatthias Ringwald break; 1821d210d9c4SMatthias Ringwald } 1822d210d9c4SMatthias Ringwald 1823f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_REDIAL_INITIATED, 0, 0, NULL); 1824a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 182574386ee0SMatthias Ringwald 182660ebb071SMilanka Ringwald log_info("Redial last number"); 18279cae807eSMatthias Ringwald char * last_dialed_number = hfp_gsm_last_dialed_number(); 1828aa4dd815SMatthias Ringwald 18299cae807eSMatthias Ringwald if (strlen(last_dialed_number) > 0){ 183060ebb071SMilanka Ringwald log_info("Last number exists: accept call"); 1831ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number); 18329cae807eSMatthias Ringwald } else { 183360ebb071SMilanka Ringwald log_info("log_infoLast number missing: reject call"); 18349f9621cfSMatthias Ringwald hfp_ag_handle_reject_outgoing_call(); 18359cae807eSMatthias Ringwald } 18369cae807eSMatthias Ringwald break; 18379cae807eSMatthias Ringwald } 1838aa4dd815SMatthias Ringwald case HFP_AG_OUTGOING_CALL_REJECTED: 18399f9621cfSMatthias Ringwald hfp_ag_handle_reject_outgoing_call(); 1840aa4dd815SMatthias Ringwald break; 1841aa4dd815SMatthias Ringwald 1842d210d9c4SMatthias Ringwald case HFP_AG_OUTGOING_CALL_ACCEPTED:{ 1843a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 1844a0ffb263SMatthias Ringwald if (!hfp_connection){ 1845a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state"); 1846aa4dd815SMatthias Ringwald break; 1847aa4dd815SMatthias Ringwald } 1848aa4dd815SMatthias Ringwald 1849a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 18505a7033e2SMatthias Ringwald hfp_ag_handle_outgoing_call_accepted(hfp_connection); 1851aa4dd815SMatthias Ringwald break; 1852d210d9c4SMatthias Ringwald } 1853aa4dd815SMatthias Ringwald case HFP_AG_OUTGOING_CALL_RINGING: 1854a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1855a0ffb263SMatthias Ringwald if (!hfp_connection){ 1856a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state"); 1857aa4dd815SMatthias Ringwald break; 1858aa4dd815SMatthias Ringwald } 1859d210d9c4SMatthias Ringwald 1860f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_RINGING, 0, 0, NULL); 1861a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING; 1862d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1863aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1864fcd59412SMilanka Ringwald hfp_ag_hf_start_ringing_outgoing(hfp_connection); 1865aa4dd815SMatthias Ringwald break; 1866aa4dd815SMatthias Ringwald 1867d210d9c4SMatthias Ringwald case HFP_AG_OUTGOING_CALL_ESTABLISHED:{ 1868aa4dd815SMatthias Ringwald // get outgoing call 1869a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING); 1870a0ffb263SMatthias Ringwald if (!hfp_connection){ 1871a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1872aa4dd815SMatthias Ringwald } 1873a0ffb263SMatthias Ringwald if (!hfp_connection){ 1874a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection"); 1875aa4dd815SMatthias Ringwald break; 1876aa4dd815SMatthias Ringwald } 1877d210d9c4SMatthias Ringwald 1878addc49c4SMilanka 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; 1879f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_ESTABLISHED, 0, 0, NULL); 1880a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1881d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1882d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1883aa4dd815SMatthias Ringwald hfp_ag_transfer_call_state(); 1884aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1885addc49c4SMilanka Ringwald if (callheld_status_call_on_hold_and_no_active_calls){ 1886d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1887aa4dd815SMatthias Ringwald hfp_ag_transfer_callheld_state(); 18883deb3ec6SMatthias Ringwald } 1889addc49c4SMilanka Ringwald hfp_ag_hf_stop_ringing(hfp_connection); 1890674ebed5SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED); 18913deb3ec6SMatthias Ringwald break; 1892d210d9c4SMatthias Ringwald } 1893d210d9c4SMatthias Ringwald 189412cbbeeeSMatthias Ringwald case HFP_AG_CALL_HOLD_USER_BUSY: 1895f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_USER_BUSY, 0, 0, NULL); 1896d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1897a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1898a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 189960ebb071SMilanka Ringwald log_info("AG: Call Waiting, User Busy"); 1900d210d9c4SMatthias Ringwald break; 1901d210d9c4SMatthias Ringwald 1902d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{ 1903d0c20769SMatthias Ringwald int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1904d0c20769SMatthias Ringwald int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD; 1905d210d9c4SMatthias Ringwald 1906d210d9c4SMatthias Ringwald // Releases all active calls (if any exist) and accepts the other (held or waiting) call. 1907d0c20769SMatthias Ringwald if (call_held || call_setup_in_progress){ 1908f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index, 1909f8737b81SMatthias Ringwald 0, NULL); 1910d0c20769SMatthias Ringwald 1911d0c20769SMatthias Ringwald } 1912d0c20769SMatthias Ringwald 1913d210d9c4SMatthias Ringwald if (call_setup_in_progress){ 191460ebb071SMilanka Ringwald log_info("AG: Call Dropped, Accept new call"); 1915d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1916a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1917d210d9c4SMatthias Ringwald } else { 191860ebb071SMilanka Ringwald log_info("AG: Call Dropped, Resume held call"); 1919d210d9c4SMatthias Ringwald } 1920d210d9c4SMatthias Ringwald if (call_held){ 1921d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1922a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1923d210d9c4SMatthias Ringwald } 1924d0c20769SMatthias Ringwald 1925a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1926d210d9c4SMatthias Ringwald break; 1927d210d9c4SMatthias Ringwald } 1928d0c20769SMatthias Ringwald 1929d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{ 1930d210d9c4SMatthias Ringwald // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call. 1931d210d9c4SMatthias Ringwald // only update if callsetup changed 1932d0c20769SMatthias Ringwald int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1933f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index, 0, 1934f8737b81SMatthias Ringwald NULL); 1935d0c20769SMatthias Ringwald 1936d210d9c4SMatthias Ringwald if (call_setup_in_progress){ 193760ebb071SMilanka Ringwald log_info("AG: Call on Hold, Accept new call"); 1938d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1939a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1940d210d9c4SMatthias Ringwald } else { 194160ebb071SMilanka Ringwald log_info("AG: Swap calls"); 1942d210d9c4SMatthias Ringwald } 1943d0c20769SMatthias Ringwald 1944d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1945d0c20769SMatthias Ringwald // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED); 1946a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1947a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1948d210d9c4SMatthias Ringwald break; 1949d210d9c4SMatthias Ringwald } 1950d0c20769SMatthias Ringwald 1951d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_ADD_HELD_CALL: 1952d210d9c4SMatthias Ringwald // Adds a held call to the conversation. 1953d0c20769SMatthias Ringwald if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){ 195460ebb071SMilanka Ringwald log_info("AG: Join 3-way-call"); 1955f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_ADD_HELD_CALL, 0, 0, NULL); 1956d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1957a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1958ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL); 1959d210d9c4SMatthias Ringwald } 1960a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1961d210d9c4SMatthias Ringwald break; 1962d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS: 1963d210d9c4SMatthias Ringwald // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer) 1964f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, 0, 0, NULL); 196560ebb071SMilanka Ringwald log_info("AG: Transfer call -> Connect two calls and disconnect"); 1966d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1967d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1968a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1969a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1970a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 1971d210d9c4SMatthias Ringwald break; 1972ce263fc8SMatthias Ringwald 19733deb3ec6SMatthias Ringwald default: 19743deb3ec6SMatthias Ringwald break; 19753deb3ec6SMatthias Ringwald } 1976d210d9c4SMatthias Ringwald 1977d0c20769SMatthias Ringwald 19783deb3ec6SMatthias Ringwald } 19793deb3ec6SMatthias Ringwald 19809cae807eSMatthias Ringwald 1981a0ffb263SMatthias Ringwald static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){ 19829cae807eSMatthias Ringwald hfp_gsm_call_t * active_call = hfp_gsm_call(call_index); 19839cae807eSMatthias Ringwald if (!active_call) return; 19849cae807eSMatthias Ringwald 19859cae807eSMatthias Ringwald int idx = active_call->index; 19869cae807eSMatthias Ringwald hfp_enhanced_call_dir_t dir = active_call->direction; 19879cae807eSMatthias Ringwald hfp_enhanced_call_status_t status = active_call->enhanced_status; 19889cae807eSMatthias Ringwald hfp_enhanced_call_mode_t mode = active_call->mode; 19899cae807eSMatthias Ringwald hfp_enhanced_call_mpty_t mpty = active_call->mpty; 19909cae807eSMatthias Ringwald uint8_t type = active_call->clip_type; 19919cae807eSMatthias Ringwald char * number = active_call->clip_number; 19929cae807eSMatthias Ringwald 19939cae807eSMatthias Ringwald char buffer[100]; 19949cae807eSMatthias Ringwald // TODO: check length of a buffer, to fit the MTU 19959cae807eSMatthias Ringwald int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty); 19969cae807eSMatthias Ringwald if (number){ 19971cc1d9e9SMilanka Ringwald offset += snprintf(buffer+offset, sizeof(buffer)-offset-3, ", \"%s\",%u", number, type); 19989cae807eSMatthias Ringwald } 19999cae807eSMatthias Ringwald snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 200060ebb071SMilanka 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, 20019cae807eSMatthias Ringwald mode, mpty, type, number); 2002a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 20039cae807eSMatthias Ringwald } 20049cae807eSMatthias Ringwald 2005febc14f5SMatthias Ringwald // sends pending command, returns if command was sent 2006febc14f5SMatthias Ringwald static int hfp_ag_send_commands(hfp_connection_t *hfp_connection){ 2007a0ffb263SMatthias Ringwald if (hfp_connection->send_status_of_current_calls){ 2008a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 2009a0ffb263SMatthias Ringwald if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){ 2010a0ffb263SMatthias Ringwald hfp_connection->next_call_index++; 2011a0ffb263SMatthias Ringwald hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index); 2012febc14f5SMatthias Ringwald return 1; 20139cae807eSMatthias Ringwald } else { 2014febc14f5SMatthias 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 2015a0ffb263SMatthias Ringwald hfp_connection->next_call_index = 0; 2016a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2017a0ffb263SMatthias Ringwald hfp_connection->send_status_of_current_calls = 0; 20189cae807eSMatthias Ringwald } 2019ce263fc8SMatthias Ringwald } 2020ce263fc8SMatthias Ringwald 2021a0ffb263SMatthias Ringwald if (hfp_connection->ag_notify_incoming_call_waiting){ 2022a0ffb263SMatthias Ringwald hfp_connection->ag_notify_incoming_call_waiting = 0; 2023a0ffb263SMatthias Ringwald hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid); 2024febc14f5SMatthias Ringwald return 1; 2025aa4dd815SMatthias Ringwald } 2026aa4dd815SMatthias Ringwald 2027a0ffb263SMatthias Ringwald if (hfp_connection->command == HFP_CMD_UNKNOWN){ 2028a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 2029a0ffb263SMatthias Ringwald hfp_connection->send_error = 0; 2030a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2031485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 2032febc14f5SMatthias Ringwald return 1; 2033a0ffb263SMatthias Ringwald } 2034a0ffb263SMatthias Ringwald 2035a0ffb263SMatthias Ringwald if (hfp_connection->send_error){ 2036a0ffb263SMatthias Ringwald hfp_connection->send_error = 0; 2037a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2038485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 2039febc14f5SMatthias Ringwald return 1; 2040aa4dd815SMatthias Ringwald } 2041aa4dd815SMatthias Ringwald 2042ce263fc8SMatthias Ringwald // note: before update AG indicators and ok_pending 2043a0ffb263SMatthias Ringwald if (hfp_connection->send_response_and_hold_status){ 2044a0ffb263SMatthias Ringwald int status = hfp_connection->send_response_and_hold_status - 1; 2045a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = 0; 2046485ac19eSMilanka Ringwald hfp_ag_send_set_response_and_hold(hfp_connection->rfcomm_cid, status); 2047febc14f5SMatthias Ringwald return 1; 2048ce263fc8SMatthias Ringwald } 2049ce263fc8SMatthias Ringwald 2050a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending){ 2051a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 2052a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2053485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 2054febc14f5SMatthias Ringwald return 1; 2055ce263fc8SMatthias Ringwald } 2056ce263fc8SMatthias Ringwald 2057aa4dd815SMatthias Ringwald // update AG indicators 2058a0ffb263SMatthias Ringwald if (hfp_connection->ag_indicators_status_update_bitmap){ 2059aa4dd815SMatthias Ringwald int i; 2060a0ffb263SMatthias Ringwald for (i=0;i<hfp_connection->ag_indicators_nr;i++){ 2061a0ffb263SMatthias Ringwald if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){ 2062a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0); 2063a0ffb263SMatthias Ringwald if (!hfp_connection->enable_status_update_for_ag_indicators) { 2064ce263fc8SMatthias Ringwald log_info("+CMER:3,0,0,0 - not sending update for '%s'", hfp_ag_indicators[i].name); 2065ce263fc8SMatthias Ringwald break; 2066ce263fc8SMatthias Ringwald } 2067485ac19eSMilanka Ringwald hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]); 2068febc14f5SMatthias Ringwald return 1; 2069aa4dd815SMatthias Ringwald } 2070aa4dd815SMatthias Ringwald } 2071aa4dd815SMatthias Ringwald } 2072aa4dd815SMatthias Ringwald 2073245852b7SMilanka Ringwald if (hfp_connection->ag_send_no_carrier){ 2074245852b7SMilanka Ringwald hfp_connection->ag_send_no_carrier = false; 2075245852b7SMilanka Ringwald hfp_ag_send_no_carrier(hfp_connection->rfcomm_cid); 2076245852b7SMilanka Ringwald return 1; 2077245852b7SMilanka Ringwald } 2078245852b7SMilanka Ringwald 2079a0ffb263SMatthias Ringwald if (hfp_connection->ag_ring){ 2080a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 0; 2081a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 20820d38e7e1SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING); 2083485ac19eSMilanka Ringwald hfp_ag_send_ring(hfp_connection->rfcomm_cid); 2084febc14f5SMatthias Ringwald return 1; 2085aa4dd815SMatthias Ringwald } 2086aa4dd815SMatthias Ringwald 2087a0ffb263SMatthias Ringwald if (hfp_connection->ag_send_clip){ 2088a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = 0; 2089a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2090a0ffb263SMatthias Ringwald hfp_ag_send_clip(hfp_connection->rfcomm_cid); 2091febc14f5SMatthias Ringwald return 1; 2092aa4dd815SMatthias Ringwald } 2093aa4dd815SMatthias Ringwald 2094a0ffb263SMatthias Ringwald if (hfp_connection->send_phone_number_for_voice_tag){ 2095a0ffb263SMatthias Ringwald hfp_connection->send_phone_number_for_voice_tag = 0; 2096a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2097a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2098a0ffb263SMatthias Ringwald hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid); 2099febc14f5SMatthias Ringwald return 1; 2100aa4dd815SMatthias Ringwald } 2101aa4dd815SMatthias Ringwald 2102a0ffb263SMatthias Ringwald if (hfp_connection->send_subscriber_number){ 2103aeb0f0feSMatthias Ringwald if (hfp_connection->next_subscriber_number_to_send < hfp_ag_subscriber_numbers_count){ 2104aeb0f0feSMatthias Ringwald hfp_phone_number_t phone = hfp_ag_subscriber_numbers[hfp_connection->next_subscriber_number_to_send++]; 2105a0ffb263SMatthias Ringwald hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number); 2106ce263fc8SMatthias Ringwald } else { 2107a0ffb263SMatthias Ringwald hfp_connection->send_subscriber_number = 0; 2108a0ffb263SMatthias Ringwald hfp_connection->next_subscriber_number_to_send = 0; 2109485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 2110ce263fc8SMatthias Ringwald } 2111a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2112febc14f5SMatthias Ringwald return 1; 2113ce263fc8SMatthias Ringwald } 2114ce263fc8SMatthias Ringwald 2115a0ffb263SMatthias Ringwald if (hfp_connection->send_microphone_gain){ 2116a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 0; 2117a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2118485ac19eSMilanka Ringwald hfp_ag_send_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 2119febc14f5SMatthias Ringwald return 1; 2120aa4dd815SMatthias Ringwald } 2121aa4dd815SMatthias Ringwald 2122a0ffb263SMatthias Ringwald if (hfp_connection->send_speaker_gain){ 2123a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 0; 2124a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2125485ac19eSMilanka Ringwald hfp_ag_send_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 2126febc14f5SMatthias Ringwald return 1; 21273deb3ec6SMatthias Ringwald } 21283deb3ec6SMatthias Ringwald 2129a0ffb263SMatthias Ringwald if (hfp_connection->send_ag_status_indicators){ 2130a0ffb263SMatthias Ringwald hfp_connection->send_ag_status_indicators = 0; 2131485ac19eSMilanka Ringwald hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid); 2132febc14f5SMatthias Ringwald return 1; 2133febc14f5SMatthias Ringwald } 2134febc14f5SMatthias Ringwald 2135febc14f5SMatthias Ringwald return 0; 2136febc14f5SMatthias Ringwald } 2137febc14f5SMatthias Ringwald 2138febc14f5SMatthias Ringwald static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection){ 2139febc14f5SMatthias Ringwald 214076cc1527SMatthias Ringwald btstack_assert(hfp_connection != NULL); 214176cc1527SMatthias Ringwald btstack_assert(hfp_connection->local_role == HFP_ROLE_AG); 2142febc14f5SMatthias Ringwald 214376cc1527SMatthias Ringwald // during SDP query, RFCOMM CID is not set 214476cc1527SMatthias Ringwald if (hfp_connection->rfcomm_cid == 0) return; 2145ce263fc8SMatthias Ringwald 21463721a235SMatthias Ringwald // assert command could be sent 21473721a235SMatthias Ringwald if (hci_can_send_command_packet_now() == 0) return; 21483721a235SMatthias Ringwald 2149f9f3ba28SMilanka Ringwald if ((hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) && hfp_connection->release_audio_connection){ 2150f9f3ba28SMilanka Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 2151f9f3ba28SMilanka Ringwald hfp_connection->release_audio_connection = 0; 2152f9f3ba28SMilanka Ringwald gap_disconnect(hfp_connection->sco_handle); 2153f9f3ba28SMilanka Ringwald return; 2154f9f3ba28SMilanka Ringwald } 2155f9f3ba28SMilanka Ringwald 21563721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP 21573721a235SMatthias Ringwald // WBS Disassociate 21583721a235SMatthias Ringwald if (hfp_connection->cc256x_send_wbs_disassociate){ 21593721a235SMatthias Ringwald hfp_connection->cc256x_send_wbs_disassociate = false; 21603721a235SMatthias Ringwald hci_send_cmd(&hci_ti_wbs_disassociate); 21613721a235SMatthias Ringwald return; 21623721a235SMatthias Ringwald } 21633721a235SMatthias Ringwald // Write Codec Config 21643721a235SMatthias Ringwald if (hfp_connection->cc256x_send_write_codec_config){ 21653721a235SMatthias Ringwald hfp_connection->cc256x_send_write_codec_config = false; 21663721a235SMatthias Ringwald hfp_cc256x_write_codec_config(hfp_connection); 21673721a235SMatthias Ringwald return; 21683721a235SMatthias Ringwald } 21693721a235SMatthias Ringwald // WBS Associate 21703721a235SMatthias Ringwald if (hfp_connection->cc256x_send_wbs_associate){ 21713721a235SMatthias Ringwald hfp_connection->cc256x_send_wbs_associate = false; 21723721a235SMatthias Ringwald hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle); 21733721a235SMatthias Ringwald return; 21743721a235SMatthias Ringwald } 21753721a235SMatthias Ringwald #endif 2176689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 2177689d4323SMatthias Ringwald // Enable WBS 2178689d4323SMatthias Ringwald if (hfp_connection->bcm_send_enable_wbs){ 2179689d4323SMatthias Ringwald hfp_connection->bcm_send_enable_wbs = false; 2180689d4323SMatthias Ringwald hci_send_cmd(&hci_bcm_enable_wbs, 1, 2); 2181689d4323SMatthias Ringwald return; 2182689d4323SMatthias Ringwald } 2183689d4323SMatthias Ringwald // Write I2S/PCM params 2184689d4323SMatthias Ringwald if (hfp_connection->bcm_send_write_i2spcm_interface_param){ 2185689d4323SMatthias Ringwald hfp_connection->bcm_send_write_i2spcm_interface_param = false; 2186689d4323SMatthias Ringwald hfp_bcm_write_i2spcm_interface_param(hfp_connection); 2187689d4323SMatthias Ringwald return; 2188689d4323SMatthias Ringwald } 2189689d4323SMatthias Ringwald // Disable WBS 2190689d4323SMatthias Ringwald if (hfp_connection->bcm_send_disable_wbs){ 2191689d4323SMatthias Ringwald hfp_connection->bcm_send_disable_wbs = false; 2192689d4323SMatthias Ringwald hci_send_cmd(&hci_bcm_enable_wbs, 0, 2); 2193689d4323SMatthias Ringwald return; 2194689d4323SMatthias Ringwald } 2195689d4323SMatthias Ringwald #endif 219648e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS) 219748e6eeeeSMatthias Ringwald if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){ 219848e6eeeeSMatthias Ringwald hfp_finalize_connection_context(hfp_connection); 219948e6eeeeSMatthias Ringwald return; 220048e6eeeeSMatthias Ringwald } 220148e6eeeeSMatthias Ringwald #endif 22023721a235SMatthias Ringwald 2203febc14f5SMatthias Ringwald if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) { 2204febc14f5SMatthias Ringwald log_info("hfp_ag_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid); 2205febc14f5SMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 2206febc14f5SMatthias Ringwald return; 2207febc14f5SMatthias Ringwald } 2208febc14f5SMatthias Ringwald 2209febc14f5SMatthias Ringwald int cmd_sent = hfp_ag_send_commands(hfp_connection); 2210febc14f5SMatthias Ringwald 2211febc14f5SMatthias Ringwald if (!cmd_sent){ 2212febc14f5SMatthias Ringwald cmd_sent = hfp_ag_run_for_context_service_level_connection(hfp_connection); 2213febc14f5SMatthias Ringwald } 2214febc14f5SMatthias Ringwald 2215c04cf7ffSMilanka Ringwald if (!cmd_sent){ 2216c04cf7ffSMilanka Ringwald cmd_sent = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection); 22173deb3ec6SMatthias Ringwald } 22183deb3ec6SMatthias Ringwald 2219c04cf7ffSMilanka Ringwald if (!cmd_sent){ 2220c04cf7ffSMilanka Ringwald cmd_sent = call_setup_state_machine(hfp_connection); 2221aa4dd815SMatthias Ringwald } 2222aa4dd815SMatthias Ringwald 2223b956fff3SMatthias Ringwald // trigger codec exchange (must be before hfp_ag_run_for_audio_connection) 2224d0a0eceeSMatthias Ringwald if (!cmd_sent && (hfp_connection->command == HFP_CMD_NONE) && hfp_connection->trigger_codec_exchange){ 2225d0a0eceeSMatthias Ringwald switch (hfp_connection->codecs_state){ 2226d0a0eceeSMatthias Ringwald case HFP_CODECS_IDLE: 2227d0a0eceeSMatthias Ringwald case HFP_CODECS_RECEIVED_LIST: 2228d0a0eceeSMatthias Ringwald case HFP_CODECS_AG_RESEND_COMMON_CODEC: 2229d0a0eceeSMatthias Ringwald case HFP_CODECS_ERROR: 2230d0a0eceeSMatthias Ringwald hfp_connection->trigger_codec_exchange = 0; 2231d0a0eceeSMatthias Ringwald hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC; 22323deb3ec6SMatthias Ringwald break; 22333deb3ec6SMatthias Ringwald default: 22343deb3ec6SMatthias Ringwald break; 22353deb3ec6SMatthias Ringwald } 22363deb3ec6SMatthias Ringwald } 2237c04cf7ffSMilanka Ringwald 2238b956fff3SMatthias Ringwald if (!cmd_sent){ 2239b956fff3SMatthias Ringwald cmd_sent = hfp_ag_run_for_audio_connection(hfp_connection); 2240b956fff3SMatthias Ringwald } 2241b956fff3SMatthias Ringwald 2242c95b5b3cSMilanka Ringwald if (!cmd_sent){ 2243c95b5b3cSMilanka Ringwald cmd_sent = hfp_ag_voice_recognition_state_machine(hfp_connection); 2244c95b5b3cSMilanka Ringwald } 2245b956fff3SMatthias Ringwald 2246d0a0eceeSMatthias Ringwald // disconnect 2247d0a0eceeSMatthias Ringwald if (!cmd_sent && (hfp_connection->command == HFP_CMD_NONE) && (hfp_connection->state == HFP_W2_DISCONNECT_RFCOMM)){ 2248d0a0eceeSMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 2249d0a0eceeSMatthias Ringwald rfcomm_disconnect(hfp_connection->rfcomm_cid); 2250d0a0eceeSMatthias Ringwald } 2251d0a0eceeSMatthias Ringwald 2252c04cf7ffSMilanka Ringwald if (cmd_sent){ 2253a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2254473ac565SMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 2255473ac565SMatthias Ringwald } 22563deb3ec6SMatthias Ringwald } 2257d68dcce1SMatthias Ringwald 2258e0d09929SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){ 2259c1ab6cc1SMatthias Ringwald return (byte == '\n') || (byte == '\r'); 2260e0d09929SMatthias Ringwald } 2261e0d09929SMatthias Ringwald 2262fdf18f86SMilanka Ringwald static void hfp_ag_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){ 22638a46ec40SMatthias Ringwald // assertion: size >= 1 as rfcomm.c does not deliver empty packets 2264fdf18f86SMilanka Ringwald if (size < 1) return; 2265347d46c8SMilanka Ringwald uint8_t status = ERROR_CODE_SUCCESS; 22661e35c04dSMatthias Ringwald 2267186dd3d2SMatthias Ringwald hfp_log_rfcomm_message("HFP_AG_RX", packet, size); 2268e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES 2269e43d1938SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet); 2270e43d1938SMatthias Ringwald #endif 22711e35c04dSMatthias Ringwald 22728a46ec40SMatthias Ringwald // process messages byte-wise 22733deb3ec6SMatthias Ringwald int pos; 22743deb3ec6SMatthias Ringwald for (pos = 0; pos < size ; pos++){ 2275a0ffb263SMatthias Ringwald hfp_parse(hfp_connection, packet[pos], 0); 2276186dd3d2SMatthias Ringwald 2277e0d09929SMatthias Ringwald // parse until end of line 2278e0d09929SMatthias Ringwald if (!hfp_parser_is_end_of_line(packet[pos])) continue; 2279e0d09929SMatthias Ringwald 2280186dd3d2SMatthias Ringwald hfp_generic_status_indicator_t * indicator; 2281a0ffb263SMatthias Ringwald switch(hfp_connection->command){ 228245796ff1SMilanka Ringwald case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION: 2283fd4151d1SMilanka Ringwald // dealt with in hfp_ag_voice_recognition_state_machine 228445796ff1SMilanka Ringwald break; 2285ce263fc8SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_QUERY: 2286ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_active){ 2287a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1; 2288ce263fc8SMatthias Ringwald } 2289a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2290ce263fc8SMatthias Ringwald break; 2291ce263fc8SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_COMMAND: 22920222a807SMatthias Ringwald switch(hfp_connection->ag_response_and_hold_action){ 2293ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD: 2294a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection); 2295ce263fc8SMatthias Ringwald break; 2296ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED: 2297a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection); 2298ce263fc8SMatthias Ringwald break; 2299ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED: 2300a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection); 2301ce263fc8SMatthias Ringwald break; 2302ce263fc8SMatthias Ringwald default: 2303ce263fc8SMatthias Ringwald break; 2304ce263fc8SMatthias Ringwald } 2305a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2306ce263fc8SMatthias Ringwald break; 2307ce263fc8SMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 2308a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2309347d46c8SMilanka Ringwald 2310aeb0f0feSMatthias Ringwald if (hfp_connection->parser_indicator_index < hfp_ag_generic_status_indicators_nr){ 2311aeb0f0feSMatthias Ringwald indicator = &hfp_ag_generic_status_indicators[hfp_connection->parser_indicator_index]; 2312ce263fc8SMatthias Ringwald switch (indicator->uuid){ 2313347d46c8SMilanka Ringwald case HFP_HF_INDICATOR_UUID_ENHANCED_SAFETY: 23140222a807SMatthias Ringwald if (hfp_connection->parser_indicator_value > 1) { 2315a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 2316ce263fc8SMatthias Ringwald break; 2317ce263fc8SMatthias Ringwald } 2318a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2319585db78dSMilanka Ringwald hfp_ag_emit_hf_indicator_value(hfp_connection, indicator->uuid, hfp_connection->parser_indicator_value); 2320347d46c8SMilanka Ringwald break; 2321347d46c8SMilanka Ringwald case HFP_HF_INDICATOR_UUID_BATTERY_LEVEL: 2322347d46c8SMilanka Ringwald if (hfp_connection->parser_indicator_value > 100){ 2323347d46c8SMilanka Ringwald hfp_connection->send_error = 1; 2324347d46c8SMilanka Ringwald break; 2325347d46c8SMilanka Ringwald } 2326347d46c8SMilanka Ringwald hfp_connection->ok_pending = 1; 2327585db78dSMilanka Ringwald hfp_ag_emit_hf_indicator_value(hfp_connection, indicator->uuid, hfp_connection->parser_indicator_value); 2328347d46c8SMilanka Ringwald break; 2329347d46c8SMilanka Ringwald default: 2330585db78dSMilanka Ringwald hfp_connection->send_error = 1; 2331347d46c8SMilanka Ringwald break; 2332347d46c8SMilanka Ringwald } 2333347d46c8SMilanka Ringwald } else { 2334347d46c8SMilanka Ringwald hfp_connection->send_error = 1; 2335347d46c8SMilanka Ringwald } 2336ce263fc8SMatthias Ringwald break; 2337ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 2338ce263fc8SMatthias Ringwald // expected by SLC state machine 2339a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break; 2340a0ffb263SMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0; 2341a0ffb263SMatthias Ringwald hfp_connection->send_ag_status_indicators = 1; 2342ce263fc8SMatthias Ringwald break; 2343ce263fc8SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 2344a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2345a0ffb263SMatthias Ringwald hfp_connection->next_call_index = 0; 2346a0ffb263SMatthias Ringwald hfp_connection->send_status_of_current_calls = 1; 2347ce263fc8SMatthias Ringwald break; 2348ce263fc8SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 2349aeb0f0feSMatthias Ringwald if (hfp_ag_subscriber_numbers_count == 0){ 2350485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 2351ce263fc8SMatthias Ringwald break; 2352ce263fc8SMatthias Ringwald } 2353a0ffb263SMatthias Ringwald hfp_connection->next_subscriber_number_to_send = 0; 2354a0ffb263SMatthias Ringwald hfp_connection->send_subscriber_number = 1; 2355ce263fc8SMatthias Ringwald break; 2356c1797c7dSMatthias Ringwald case HFP_CMD_TRANSMIT_DTMF_CODES: 23570222a807SMatthias Ringwald { 2358a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 23590222a807SMatthias Ringwald char buffer[2]; 23600222a807SMatthias Ringwald buffer[0] = (char) hfp_connection->ag_dtmf_code; 23610222a807SMatthias Ringwald buffer[1] = 0; 23620222a807SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, buffer); 2363c1797c7dSMatthias Ringwald break; 23640222a807SMatthias Ringwald } 2365aa4dd815SMatthias Ringwald case HFP_CMD_HF_REQUEST_PHONE_NUMBER: 2366a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2367ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG); 2368aa4dd815SMatthias Ringwald break; 2369aa4dd815SMatthias Ringwald case HFP_CMD_TURN_OFF_EC_AND_NR: 2370a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2371347d46c8SMilanka Ringwald 2372aeb0f0feSMatthias Ringwald if (get_bit(hfp_ag_supported_features, HFP_AGSF_EC_NR_FUNCTION)){ 2373a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2374aeb0f0feSMatthias Ringwald hfp_ag_supported_features = store_bit(hfp_ag_supported_features, HFP_AGSF_EC_NR_FUNCTION, hfp_connection->ag_echo_and_noise_reduction); 2375347d46c8SMilanka Ringwald status = ERROR_CODE_SUCCESS; 2376aa4dd815SMatthias Ringwald } else { 2377a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 2378347d46c8SMilanka Ringwald status = ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 2379aa4dd815SMatthias Ringwald } 2380347d46c8SMilanka Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE, status); 2381aa4dd815SMatthias Ringwald break; 2382aa4dd815SMatthias Ringwald case HFP_CMD_CALL_ANSWERED: 2383a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 238460ebb071SMilanka Ringwald log_info("HFP: ATA"); 2385a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection); 2386aa4dd815SMatthias Ringwald break; 2387aa4dd815SMatthias Ringwald case HFP_CMD_HANG_UP_CALL: 2388a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2389a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2390a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection); 2391aa4dd815SMatthias Ringwald break; 2392aa4dd815SMatthias Ringwald case HFP_CMD_CALL_HOLD: { 2393a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2394a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2395d0c20769SMatthias Ringwald 23960222a807SMatthias Ringwald switch (hfp_connection->ag_call_hold_action){ 23970222a807SMatthias Ringwald case 0: 2398d0c20769SMatthias Ringwald // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call. 2399a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection); 2400aa4dd815SMatthias Ringwald break; 24010222a807SMatthias Ringwald case 1: 2402d0c20769SMatthias Ringwald // Releases all active calls (if any exist) and accepts the other (held or waiting) call. 2403d0c20769SMatthias Ringwald // Where both a held and a waiting call exist, the above procedures shall apply to the 2404d0c20769SMatthias Ringwald // waiting call (i.e., not to the held call) in conflicting situation. 2405a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection); 2406aa4dd815SMatthias Ringwald break; 24070222a807SMatthias Ringwald case 2: 2408d0c20769SMatthias Ringwald // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call. 2409d0c20769SMatthias Ringwald // Where both a held and a waiting call exist, the above procedures shall apply to the 2410d0c20769SMatthias Ringwald // waiting call (i.e., not to the held call) in conflicting situation. 2411a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection); 2412aa4dd815SMatthias Ringwald break; 24130222a807SMatthias Ringwald case 3: 2414d0c20769SMatthias Ringwald // Adds a held call to the conversation. 2415a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection); 2416aa4dd815SMatthias Ringwald break; 24170222a807SMatthias Ringwald case 4: 2418d0c20769SMatthias Ringwald // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer). 2419a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection); 2420aa4dd815SMatthias Ringwald break; 2421aa4dd815SMatthias Ringwald default: 2422aa4dd815SMatthias Ringwald break; 2423aa4dd815SMatthias Ringwald } 24240222a807SMatthias Ringwald hfp_connection->call_index = 0; 242535e92150SMatthias Ringwald break; 2426aa4dd815SMatthias Ringwald } 2427aa4dd815SMatthias Ringwald case HFP_CMD_CALL_PHONE_NUMBER: 2428a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 24299ff73f41SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED_BY_HF, hfp_connection); 2430aa4dd815SMatthias Ringwald break; 2431aa4dd815SMatthias Ringwald case HFP_CMD_REDIAL_LAST_NUMBER: 2432a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2433a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection); 2434aa4dd815SMatthias Ringwald break; 2435aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_CLIP: 2436a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2437a0ffb263SMatthias Ringwald log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled); 2438a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2439aa4dd815SMatthias Ringwald break; 2440aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION: 2441a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2442a0ffb263SMatthias Ringwald log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled); 2443a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2444aa4dd815SMatthias Ringwald break; 2445aa4dd815SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 2446a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2447a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 244860ebb071SMilanka Ringwald log_info("HF speaker gain = %u", hfp_connection->speaker_gain); 24493db60f78SBjoern Hartmann hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_connection->speaker_gain); 2450aa4dd815SMatthias Ringwald break; 2451aa4dd815SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 2452a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2453a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 245460ebb071SMilanka Ringwald log_info("HF microphone gain = %u", hfp_connection->microphone_gain); 24552abbd98dSMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_connection->microphone_gain); 2456aa4dd815SMatthias Ringwald break; 2457aa4dd815SMatthias Ringwald default: 2458aa4dd815SMatthias Ringwald break; 24593deb3ec6SMatthias Ringwald } 24603deb3ec6SMatthias Ringwald } 24610cef86faSMatthias Ringwald } 24623deb3ec6SMatthias Ringwald 24631c6a0fc0SMatthias Ringwald static void hfp_ag_run(void){ 2464d63c37a1SMatthias Ringwald btstack_linked_list_iterator_t it; 2465d63c37a1SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2466d63c37a1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2467d63c37a1SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 246822387625SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2469f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 24703deb3ec6SMatthias Ringwald } 24713deb3ec6SMatthias Ringwald } 24723deb3ec6SMatthias Ringwald 24731c6a0fc0SMatthias Ringwald static void hfp_ag_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2474fdf18f86SMilanka Ringwald hfp_connection_t * hfp_connection = NULL; 24753deb3ec6SMatthias Ringwald switch (packet_type){ 24763deb3ec6SMatthias Ringwald case RFCOMM_DATA_PACKET: 2477fdf18f86SMilanka Ringwald hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 2478fd075f20SMilanka Ringwald btstack_assert(hfp_connection != NULL); 2479fdf18f86SMilanka Ringwald 2480fdf18f86SMilanka Ringwald hfp_ag_handle_rfcomm_data(hfp_connection, packet, size); 2481fdf18f86SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 2482fdf18f86SMilanka Ringwald return; 24833deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 2484e30a6a47SMatthias Ringwald if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 2485e30a6a47SMatthias Ringwald uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 2486fd075f20SMilanka Ringwald hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid); 2487fd075f20SMilanka Ringwald btstack_assert(hfp_connection != NULL); 2488fd075f20SMilanka Ringwald 2489fd075f20SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 2490e30a6a47SMatthias Ringwald return; 2491e30a6a47SMatthias Ringwald } 249227950165SMatthias Ringwald hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_AG); 2493aa4dd815SMatthias Ringwald break; 24943deb3ec6SMatthias Ringwald default: 24953deb3ec6SMatthias Ringwald break; 24963deb3ec6SMatthias Ringwald } 24973deb3ec6SMatthias Ringwald 24981c6a0fc0SMatthias Ringwald hfp_ag_run(); 24993deb3ec6SMatthias Ringwald } 25003deb3ec6SMatthias Ringwald 25011c6a0fc0SMatthias Ringwald static void hfp_ag_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2502405014fbSMatthias Ringwald hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_AG); 25031c6a0fc0SMatthias Ringwald hfp_ag_run(); 2504405014fbSMatthias Ringwald } 2505405014fbSMatthias Ringwald 25067ca89cabSMatthias Ringwald void hfp_ag_init_codecs(int codecs_nr, const uint8_t * codecs){ 25073deb3ec6SMatthias Ringwald if (codecs_nr > HFP_MAX_NUM_CODECS){ 25083deb3ec6SMatthias Ringwald log_error("hfp_init: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS); 25093deb3ec6SMatthias Ringwald return; 25103deb3ec6SMatthias Ringwald } 25113deb3ec6SMatthias Ringwald int i; 2512aeb0f0feSMatthias Ringwald hfp_ag_codecs_nr = codecs_nr; 25133deb3ec6SMatthias Ringwald for (i=0; i < codecs_nr; i++){ 2514aeb0f0feSMatthias Ringwald hfp_ag_codecs[i] = codecs[i]; 25153deb3ec6SMatthias Ringwald } 2516a0ffb263SMatthias Ringwald } 25173deb3ec6SMatthias Ringwald 2518a0ffb263SMatthias Ringwald void hfp_ag_init_supported_features(uint32_t supported_features){ 2519aeb0f0feSMatthias Ringwald hfp_ag_supported_features = supported_features; 2520a0ffb263SMatthias Ringwald } 25213deb3ec6SMatthias Ringwald 25227ca89cabSMatthias Ringwald void hfp_ag_init_ag_indicators(int ag_indicators_nr, const hfp_ag_indicator_t * ag_indicators){ 2523a0ffb263SMatthias Ringwald hfp_ag_indicators_nr = ag_indicators_nr; 25246535961aSMatthias Ringwald (void)memcpy(hfp_ag_indicators, ag_indicators, 25256535961aSMatthias Ringwald ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 2526a0ffb263SMatthias Ringwald } 25273deb3ec6SMatthias Ringwald 25287ca89cabSMatthias Ringwald void hfp_ag_init_hf_indicators(int hf_indicators_nr, const hfp_generic_status_indicator_t * hf_indicators){ 252925789943SMilanka Ringwald if (hf_indicators_nr > HFP_MAX_NUM_INDICATORS) return; 2530aeb0f0feSMatthias Ringwald hfp_ag_generic_status_indicators_nr = hf_indicators_nr; 2531aeb0f0feSMatthias Ringwald (void)memcpy(hfp_ag_generic_status_indicators, hf_indicators, 25326535961aSMatthias Ringwald hf_indicators_nr * sizeof(hfp_generic_status_indicator_t)); 2533a0ffb263SMatthias Ringwald } 2534a0ffb263SMatthias Ringwald 2535a0ffb263SMatthias Ringwald void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){ 25363deb3ec6SMatthias Ringwald hfp_ag_call_hold_services_nr = call_hold_services_nr; 25376535961aSMatthias Ringwald (void)memcpy(hfp_ag_call_hold_services, call_hold_services, 25386535961aSMatthias Ringwald call_hold_services_nr * sizeof(char *)); 2539a0ffb263SMatthias Ringwald } 2540a0ffb263SMatthias Ringwald 2541a0ffb263SMatthias Ringwald 2542a0ffb263SMatthias Ringwald void hfp_ag_init(uint16_t rfcomm_channel_nr){ 254327950165SMatthias Ringwald 2544520c92d5SMatthias Ringwald hfp_init(); 254520b2edb6SMatthias Ringwald hfp_ag_call_hold_services_nr = 0; 254620b2edb6SMatthias Ringwald hfp_ag_response_and_hold_active = 0; 254720b2edb6SMatthias Ringwald hfp_ag_indicators_nr = 0; 2548aeb0f0feSMatthias Ringwald hfp_ag_codecs_nr = 0; 2549aeb0f0feSMatthias Ringwald hfp_ag_supported_features = HFP_DEFAULT_AG_SUPPORTED_FEATURES; 2550aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers = NULL; 2551aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers_count = 0; 2552d63c37a1SMatthias Ringwald 25531c6a0fc0SMatthias Ringwald hfp_ag_hci_event_callback_registration.callback = &hfp_ag_hci_event_packet_handler; 25541c6a0fc0SMatthias Ringwald hci_add_event_handler(&hfp_ag_hci_event_callback_registration); 255527950165SMatthias Ringwald 25561c6a0fc0SMatthias Ringwald rfcomm_register_service(&hfp_ag_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 255727950165SMatthias Ringwald 255827950165SMatthias Ringwald // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us 25591c6a0fc0SMatthias Ringwald hfp_set_ag_rfcomm_packet_handler(&hfp_ag_rfcomm_packet_handler); 2560aa4dd815SMatthias Ringwald 2561d210d9c4SMatthias Ringwald hfp_gsm_init(); 25623deb3ec6SMatthias Ringwald } 25633deb3ec6SMatthias Ringwald 256420b2edb6SMatthias Ringwald void hfp_ag_deinit(void){ 256520b2edb6SMatthias Ringwald hfp_deinit(); 256620b2edb6SMatthias Ringwald hfp_gsm_deinit(); 2567aeb0f0feSMatthias Ringwald 2568aeb0f0feSMatthias Ringwald hfp_ag_callback = NULL; 2569aeb0f0feSMatthias Ringwald hfp_ag_supported_features = 0; 2570aeb0f0feSMatthias Ringwald hfp_ag_codecs_nr = 0; 2571aeb0f0feSMatthias Ringwald hfp_ag_indicators_nr = 0; 2572aeb0f0feSMatthias Ringwald hfp_ag_call_hold_services_nr = 0; 257320b2edb6SMatthias Ringwald (void) memset(&hfp_ag_call_hold_services, 0, sizeof(hfp_ag_call_hold_services)); 2574aeb0f0feSMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 257520b2edb6SMatthias Ringwald (void) memset(&hfp_ag_response_and_hold_state, 0, sizeof(hfp_response_and_hold_state_t)); 2576aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers = NULL; 2577aeb0f0feSMatthias Ringwald (void) memset(&hfp_ag_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t)); 257820b2edb6SMatthias Ringwald } 257920b2edb6SMatthias Ringwald 25804eb3f1d8SMilanka Ringwald uint8_t hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){ 25814eb3f1d8SMilanka Ringwald return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE, HFP_ROLE_AG); 25823deb3ec6SMatthias Ringwald } 25833deb3ec6SMatthias Ringwald 2584657bc59fSMilanka Ringwald uint8_t hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){ 25859c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2586a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2587657bc59fSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2588a33eb0c4SMilanka Ringwald } 2589c5fa3c94SMilanka Ringwald 25901ffa0dd9SMilanka Ringwald hfp_trigger_release_service_level_connection(hfp_connection); 2591f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2592657bc59fSMilanka Ringwald return ERROR_CODE_SUCCESS; 25933deb3ec6SMatthias Ringwald } 25943deb3ec6SMatthias Ringwald 2595c5fa3c94SMilanka Ringwald uint8_t hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, hfp_cme_error_t error){ 25969c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2597a0ffb263SMatthias Ringwald if (!hfp_connection){ 2598c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 25993deb3ec6SMatthias Ringwald } 26003deb3ec6SMatthias Ringwald 2601c5fa3c94SMilanka Ringwald hfp_connection->extended_audio_gateway_error = 0; 2602c5fa3c94SMilanka Ringwald if (!hfp_connection->enable_extended_audio_gateway_error_report){ 2603c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2604c5fa3c94SMilanka Ringwald } 2605c5fa3c94SMilanka Ringwald 2606c5fa3c94SMilanka Ringwald hfp_connection->extended_audio_gateway_error = error; 2607c5fa3c94SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 2608c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2609c5fa3c94SMilanka Ringwald } 2610c5fa3c94SMilanka Ringwald 2611c5fa3c94SMilanka Ringwald static uint8_t hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){ 2612c5fa3c94SMilanka Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 2613c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2614c5fa3c94SMilanka Ringwald } 2615c5fa3c94SMilanka Ringwald if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){ 2616c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2617c5fa3c94SMilanka Ringwald } 26183deb3ec6SMatthias Ringwald 2619a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 1; 2620a0ffb263SMatthias Ringwald if (!has_codec_negotiation_feature(hfp_connection)){ 2621d6ff09e1SMatthias Ringwald log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using CVSD"); 2622d6ff09e1SMatthias Ringwald hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 2623a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 26247522e673SMatthias Ringwald // now, pick link settings 2625afac2a04SMilanka Ringwald hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection)); 26263721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP 26273721a235SMatthias Ringwald hfp_cc256x_prepare_for_sco(hfp_connection); 26283721a235SMatthias Ringwald #endif 2629c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 26303deb3ec6SMatthias Ringwald } 2631aa4dd815SMatthias Ringwald 2632411a9776SMilanka Ringwald uint8_t i; 2633411a9776SMilanka Ringwald bool codec_was_in_use = false; 2634411a9776SMilanka Ringwald bool better_codec_can_be_used = false; 2635411a9776SMilanka Ringwald 2636411a9776SMilanka Ringwald for (i = 0; i<hfp_connection->remote_codecs_nr; i++){ 2637411a9776SMilanka Ringwald if (hfp_connection->negotiated_codec == hfp_connection->remote_codecs[i]){ 2638411a9776SMilanka Ringwald codec_was_in_use = true; 2639411a9776SMilanka Ringwald } else if (hfp_connection->negotiated_codec < hfp_connection->remote_codecs[i]){ 2640411a9776SMilanka Ringwald better_codec_can_be_used = true; 2641411a9776SMilanka Ringwald } 2642411a9776SMilanka Ringwald } 2643411a9776SMilanka Ringwald 2644411a9776SMilanka Ringwald if (!codec_was_in_use || better_codec_can_be_used){ 26452ee45eb2SMilanka Ringwald hfp_connection->trigger_codec_exchange = 1; 2646411a9776SMilanka Ringwald hfp_connection->codecs_state = HFP_CODECS_IDLE; 2647411a9776SMilanka Ringwald } 2648c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2649aa4dd815SMatthias Ringwald } 2650aa4dd815SMatthias Ringwald 2651c5fa3c94SMilanka Ringwald uint8_t hfp_ag_establish_audio_connection(hci_con_handle_t acl_handle){ 26529c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2653a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2654c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2655a33eb0c4SMilanka Ringwald } 2656c5fa3c94SMilanka Ringwald uint8_t status = hfp_ag_setup_audio_connection(hfp_connection); 2657c5fa3c94SMilanka Ringwald if (status != ERROR_CODE_SUCCESS){ 2658c5fa3c94SMilanka Ringwald return status; 2659c5fa3c94SMilanka Ringwald } 2660f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2661c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 26623deb3ec6SMatthias Ringwald } 26633deb3ec6SMatthias Ringwald 2664c5fa3c94SMilanka Ringwald uint8_t hfp_ag_release_audio_connection(hci_con_handle_t acl_handle){ 26659c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2666a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2667c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2668a33eb0c4SMilanka Ringwald } 2669c5fa3c94SMilanka Ringwald 26700b4debbfSMilanka Ringwald if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){ 26710b4debbfSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 26720b4debbfSMilanka Ringwald } 26731a26de69SMilanka Ringwald 26740b4debbfSMilanka Ringwald uint8_t status = hfp_trigger_release_audio_connection(hfp_connection); 26750b4debbfSMilanka Ringwald if (status == ERROR_CODE_SUCCESS){ 2676f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 26770b4debbfSMilanka Ringwald } 26780b4debbfSMilanka Ringwald return status; 26793deb3ec6SMatthias Ringwald } 2680aa4dd815SMatthias Ringwald 2681aa4dd815SMatthias Ringwald /** 2682aa4dd815SMatthias Ringwald * @brief Enable in-band ring tone 2683aa4dd815SMatthias Ringwald */ 2684aa4dd815SMatthias Ringwald void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){ 2685aeb0f0feSMatthias Ringwald if (get_bit(hfp_ag_supported_features, HFP_AGSF_IN_BAND_RING_TONE) == use_in_band_ring_tone){ 2686aa4dd815SMatthias Ringwald return; 2687aa4dd815SMatthias Ringwald } 2688c5fa3c94SMilanka Ringwald 2689aeb0f0feSMatthias Ringwald hfp_ag_supported_features = store_bit(hfp_ag_supported_features, HFP_AGSF_IN_BAND_RING_TONE, use_in_band_ring_tone); 2690aa4dd815SMatthias Ringwald 2691665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 2692665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2693665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2694a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 269566c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2696a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING; 2697f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2698aa4dd815SMatthias Ringwald } 2699aa4dd815SMatthias Ringwald } 2700aa4dd815SMatthias Ringwald 2701aa4dd815SMatthias Ringwald /** 2702aa4dd815SMatthias Ringwald * @brief Called from GSM 2703aa4dd815SMatthias Ringwald */ 2704aa4dd815SMatthias Ringwald void hfp_ag_incoming_call(void){ 2705aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL); 2706aa4dd815SMatthias Ringwald } 2707aa4dd815SMatthias Ringwald 2708fe899794SMatthias Ringwald void hfp_ag_outgoing_call_initiated(const char * number) { 2709c5753b03SMatthias Ringwald UNUSED(number); 2710fe899794SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED_BY_AG, NULL); 2711fe899794SMatthias Ringwald } 2712fe899794SMatthias Ringwald 2713aa4dd815SMatthias Ringwald /** 2714aa4dd815SMatthias Ringwald * @brief number is stored. 2715aa4dd815SMatthias Ringwald */ 2716aa4dd815SMatthias Ringwald void hfp_ag_set_clip(uint8_t type, const char * number){ 2717f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_SET_CLIP, 0, type, number); 2718aa4dd815SMatthias Ringwald } 2719aa4dd815SMatthias Ringwald 2720aa4dd815SMatthias Ringwald void hfp_ag_call_dropped(void){ 2721aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL); 2722aa4dd815SMatthias Ringwald } 2723aa4dd815SMatthias Ringwald 2724aa4dd815SMatthias Ringwald // call from AG UI 2725aa4dd815SMatthias Ringwald void hfp_ag_answer_incoming_call(void){ 2726aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL); 2727aa4dd815SMatthias Ringwald } 2728aa4dd815SMatthias Ringwald 2729ce263fc8SMatthias Ringwald void hfp_ag_join_held_call(void){ 2730ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL); 2731ce263fc8SMatthias Ringwald } 2732ce263fc8SMatthias Ringwald 2733aa4dd815SMatthias Ringwald void hfp_ag_terminate_call(void){ 2734aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL); 2735aa4dd815SMatthias Ringwald } 2736aa4dd815SMatthias Ringwald 2737aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_ringing(void){ 2738aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL); 2739aa4dd815SMatthias Ringwald } 2740aa4dd815SMatthias Ringwald 2741aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_established(void){ 2742aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL); 2743aa4dd815SMatthias Ringwald } 2744aa4dd815SMatthias Ringwald 2745aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_rejected(void){ 2746aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL); 2747aa4dd815SMatthias Ringwald } 2748aa4dd815SMatthias Ringwald 2749aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_accepted(void){ 2750aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL); 2751aa4dd815SMatthias Ringwald } 2752ce263fc8SMatthias Ringwald 2753ce263fc8SMatthias Ringwald void hfp_ag_hold_incoming_call(void){ 2754ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL); 2755ce263fc8SMatthias Ringwald } 2756ce263fc8SMatthias Ringwald 2757ce263fc8SMatthias Ringwald void hfp_ag_accept_held_incoming_call(void) { 2758ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL); 2759ce263fc8SMatthias Ringwald } 2760ce263fc8SMatthias Ringwald 2761ce263fc8SMatthias Ringwald void hfp_ag_reject_held_incoming_call(void){ 2762ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL); 2763ce263fc8SMatthias Ringwald } 2764ce263fc8SMatthias Ringwald 2765aa4dd815SMatthias Ringwald static void hfp_ag_set_ag_indicator(const char * name, int value){ 2766aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name(name); 2767aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 2768aa4dd815SMatthias Ringwald hfp_ag_indicators[indicator_index].status = value; 2769aa4dd815SMatthias Ringwald 2770665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 2771665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2772665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2773a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 277466c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2775a0ffb263SMatthias Ringwald if (!hfp_connection->ag_indicators[indicator_index].enabled) { 2776245852b7SMilanka Ringwald log_info("Requested AG indicator '%s' update to %u, but it is not enabled", hfp_ag_indicators[indicator_index].name, value); 2777ce263fc8SMatthias Ringwald continue; 2778ce263fc8SMatthias Ringwald } 2779245852b7SMilanka Ringwald log_info("AG indicator '%s' changed to %u, request transfer status", hfp_ag_indicators[indicator_index].name, value); 2780a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 2781f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2782aa4dd815SMatthias Ringwald } 2783aa4dd815SMatthias Ringwald } 2784aa4dd815SMatthias Ringwald 2785c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_registration_status(int registration_status){ 2786c5fa3c94SMilanka Ringwald if ((registration_status < 0) || (registration_status > 1)){ 2787c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2788c5fa3c94SMilanka Ringwald } 2789c5fa3c94SMilanka Ringwald 2790c5fa3c94SMilanka Ringwald if ( (registration_status == 0) && (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 2791245852b7SMilanka Ringwald 2792245852b7SMilanka Ringwald // if network goes away wihle a call is active: 2793245852b7SMilanka Ringwald // - the call gets dropped 2794245852b7SMilanka Ringwald // - we send NO CARRIER 2795245852b7SMilanka Ringwald // NOTE: the CALL=0 has to be sent before NO CARRIER 2796245852b7SMilanka Ringwald 2797245852b7SMilanka Ringwald hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL); 2798245852b7SMilanka Ringwald 2799245852b7SMilanka Ringwald btstack_linked_list_iterator_t it; 2800245852b7SMilanka Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2801245852b7SMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2802245852b7SMilanka Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 2803245852b7SMilanka Ringwald hfp_connection->ag_send_no_carrier = true; 2804245852b7SMilanka Ringwald } 2805245852b7SMilanka Ringwald } 2806c5fa3c94SMilanka Ringwald hfp_ag_set_ag_indicator("service", registration_status); 2807c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2808aa4dd815SMatthias Ringwald } 2809aa4dd815SMatthias Ringwald 2810c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_signal_strength(int signal_strength){ 2811c5fa3c94SMilanka Ringwald if ((signal_strength < 0) || (signal_strength > 5)){ 2812c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2813aa4dd815SMatthias Ringwald } 2814aa4dd815SMatthias Ringwald 2815c5fa3c94SMilanka Ringwald hfp_ag_set_ag_indicator("signal", signal_strength); 2816c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2817aa4dd815SMatthias Ringwald } 2818aa4dd815SMatthias Ringwald 2819c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_roaming_status(int roaming_status){ 2820c5fa3c94SMilanka Ringwald if ((roaming_status < 0) || (roaming_status > 1)){ 2821c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2822aa4dd815SMatthias Ringwald } 2823aa4dd815SMatthias Ringwald 2824c5fa3c94SMilanka Ringwald hfp_ag_set_ag_indicator("roam", roaming_status); 2825c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2826c5fa3c94SMilanka Ringwald } 2827c5fa3c94SMilanka Ringwald 2828c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_battery_level(int battery_level){ 2829c5fa3c94SMilanka Ringwald if ((battery_level < 0) || (battery_level > 5)){ 2830c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2831c5fa3c94SMilanka Ringwald } 2832c5fa3c94SMilanka Ringwald hfp_ag_set_ag_indicator("battchg", battery_level); 2833c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2834c5fa3c94SMilanka Ringwald } 2835c5fa3c94SMilanka Ringwald 28366b8275b0SMilanka Ringwald uint8_t hfp_ag_activate_voice_recognition(hci_con_handle_t acl_handle){ 283714c148caSMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 283814c148caSMilanka Ringwald if (!hfp_connection){ 283914c148caSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 28406b8275b0SMilanka Ringwald } 2841f71d2aacSMilanka Ringwald 28429cd26862SMilanka Ringwald bool enhanced_vra_supported = hfp_ag_enhanced_vra_flag_supported(hfp_connection); 28439cd26862SMilanka Ringwald bool legacy_vra_supported = hfp_ag_vra_flag_supported(hfp_connection); 2844f71d2aacSMilanka Ringwald if (!enhanced_vra_supported && !legacy_vra_supported){ 284514c148caSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 28466b8275b0SMilanka Ringwald } 28476b8275b0SMilanka Ringwald 2848135d9718SMilanka Ringwald if (!hfp_ag_can_activate_voice_recognition(hfp_connection)){ 2849135d9718SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2850f71d2aacSMilanka Ringwald } 2851f71d2aacSMilanka Ringwald 2852f71d2aacSMilanka Ringwald hfp_connection->ag_activate_voice_recognition_value = 1; 2853f71d2aacSMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED; 2854f71d2aacSMilanka Ringwald hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION; 2855fcf4ede6SMilanka Ringwald hfp_connection->enhanced_voice_recognition_enabled = enhanced_vra_supported; 2856f71d2aacSMilanka Ringwald hfp_connection->ag_audio_connection_opened_before_vra = hfp_ag_is_audio_connection_active(hfp_connection); 2857f71d2aacSMilanka Ringwald hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY; 2858f71d2aacSMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 2859f71d2aacSMilanka Ringwald return ERROR_CODE_SUCCESS; 2860f71d2aacSMilanka Ringwald } 2861f71d2aacSMilanka Ringwald 2862d84719b1SMilanka Ringwald uint8_t hfp_ag_deactivate_voice_recognition(hci_con_handle_t acl_handle){ 286314c148caSMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 286414c148caSMilanka Ringwald if (!hfp_connection){ 286514c148caSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 286645796ff1SMilanka Ringwald } 2867f71d2aacSMilanka Ringwald 28689cd26862SMilanka Ringwald bool enhanced_vra_supported = hfp_ag_enhanced_vra_flag_supported(hfp_connection); 28699cd26862SMilanka Ringwald bool legacy_vra_supported = hfp_ag_vra_flag_supported(hfp_connection); 28709cd26862SMilanka Ringwald 2871f71d2aacSMilanka Ringwald if (!enhanced_vra_supported && !legacy_vra_supported){ 287214c148caSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 287345796ff1SMilanka Ringwald } 287445796ff1SMilanka Ringwald 2875135d9718SMilanka Ringwald if (!hfp_ag_voice_recognition_session_active(hfp_connection)){ 2876135d9718SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2877f71d2aacSMilanka Ringwald } 2878f71d2aacSMilanka Ringwald 2879f71d2aacSMilanka Ringwald hfp_connection->ag_activate_voice_recognition_value = 0; 2880f71d2aacSMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF; 2881f71d2aacSMilanka Ringwald hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION; 2882f71d2aacSMilanka Ringwald hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY; 2883f71d2aacSMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 2884f71d2aacSMilanka Ringwald return ERROR_CODE_SUCCESS; 2885f71d2aacSMilanka Ringwald } 2886f71d2aacSMilanka Ringwald 2887cf75be85SMilanka Ringwald static uint8_t hfp_ag_enhanced_voice_recognition_send_state(hci_con_handle_t acl_handle, hfp_voice_recognition_state_t state){ 288845796ff1SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 288945796ff1SMilanka Ringwald if (!hfp_connection){ 2890c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 289145796ff1SMilanka Ringwald } 2892c5fa3c94SMilanka Ringwald 28939cd26862SMilanka Ringwald bool enhanced_vra_supported = hfp_ag_enhanced_vra_flag_supported(hfp_connection); 2894f71d2aacSMilanka Ringwald if (!enhanced_vra_supported ){ 2895f71d2aacSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 2896f71d2aacSMilanka Ringwald } 2897f71d2aacSMilanka Ringwald 2898645bfa78SMilanka Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){ 2899645bfa78SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2900645bfa78SMilanka Ringwald } 2901645bfa78SMilanka Ringwald 2902645bfa78SMilanka Ringwald if (hfp_connection->vra_state != HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO){ 2903135d9718SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 29049cd26862SMilanka Ringwald } 29059cd26862SMilanka Ringwald 2906cf75be85SMilanka Ringwald hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION; 290745796ff1SMilanka Ringwald hfp_connection->ag_vra_state = state; 2908cf75be85SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_STATUS; 290945796ff1SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 29109cd26862SMilanka Ringwald return ERROR_CODE_SUCCESS; 291145796ff1SMilanka Ringwald } 291245796ff1SMilanka Ringwald 2913f0c27a95SMilanka Ringwald uint8_t hfp_ag_enhanced_voice_recognition_report_sending_audio(hci_con_handle_t acl_handle){ 2914cf75be85SMilanka Ringwald return hfp_ag_enhanced_voice_recognition_send_state(acl_handle, HFP_VOICE_RECOGNITION_STATE_AG_IS_STARTING_SOUND); 291545796ff1SMilanka Ringwald } 2916f0c27a95SMilanka Ringwald uint8_t hfp_ag_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle){ 2917cf75be85SMilanka Ringwald return hfp_ag_enhanced_voice_recognition_send_state(acl_handle, HFP_VOICE_RECOGNITION_STATE_AG_READY_TO_ACCEPT_AUDIO_INPUT); 291845796ff1SMilanka Ringwald } 2919cf75be85SMilanka Ringwald uint8_t hfp_ag_enhanced_voice_recognition_report_processing_input(hci_con_handle_t acl_handle){ 2920cf75be85SMilanka Ringwald return hfp_ag_enhanced_voice_recognition_send_state(acl_handle, HFP_VOICE_RECOGNITION_STATE_AG_IS_PROCESSING_AUDIO_INPUT); 292145796ff1SMilanka Ringwald } 292245796ff1SMilanka Ringwald 2923b95cac54SMilanka Ringwald 2924b95cac54SMilanka 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){ 2925b95cac54SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2926b95cac54SMilanka Ringwald if (!hfp_connection){ 2927b95cac54SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2928b95cac54SMilanka Ringwald } 2929b95cac54SMilanka Ringwald 29309cd26862SMilanka Ringwald bool enhanced_vra_supported = hfp_ag_enhanced_vra_flag_supported(hfp_connection); 2931f71d2aacSMilanka Ringwald if (!enhanced_vra_supported ){ 2932f71d2aacSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 2933f71d2aacSMilanka Ringwald } 2934f71d2aacSMilanka Ringwald 29359cd26862SMilanka Ringwald bool enhanced_vra_msg_supported = hfp_ag_can_send_enhanced_vra_message_flag_supported(hfp_connection); 2936f71d2aacSMilanka Ringwald if (!enhanced_vra_msg_supported ){ 2937f71d2aacSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 2938f71d2aacSMilanka Ringwald } 2939f71d2aacSMilanka Ringwald 2940645bfa78SMilanka Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){ 2941645bfa78SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2942645bfa78SMilanka Ringwald } 2943645bfa78SMilanka Ringwald 2944645bfa78SMilanka Ringwald if (hfp_connection->vra_state != HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO){ 2945135d9718SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2946f71d2aacSMilanka Ringwald } 2947f71d2aacSMilanka Ringwald 294851aa5d5aSMilanka Ringwald uint16_t message_len = strlen(msg.text); 294951aa5d5aSMilanka Ringwald 295089e7c136SMilanka Ringwald if (message_len > HFP_MAX_VR_TEXT_SIZE){ 29516504a03aSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 29526504a03aSMilanka Ringwald } 29536504a03aSMilanka Ringwald 29546504a03aSMilanka Ringwald if ((HFP_VR_TEXT_HEADER_SIZE + message_len) > hfp_connection->rfcomm_mtu){ 295551aa5d5aSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 295651aa5d5aSMilanka Ringwald } 295751aa5d5aSMilanka Ringwald 2958b95cac54SMilanka Ringwald hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION; 2959b95cac54SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_MSG; 2960b95cac54SMilanka Ringwald hfp_connection->ag_msg = msg; 2961b95cac54SMilanka Ringwald hfp_connection->ag_vra_state = state; 2962b95cac54SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 2963f71d2aacSMilanka Ringwald 2964f71d2aacSMilanka Ringwald return ERROR_CODE_SUCCESS; 2965b95cac54SMilanka Ringwald } 2966b95cac54SMilanka Ringwald 2967b95cac54SMilanka Ringwald 2968c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 29699c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2970a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2971c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2972a33eb0c4SMilanka Ringwald } 2973c5fa3c94SMilanka Ringwald 2974c5fa3c94SMilanka Ringwald if ((gain < 0) || (gain > 15)){ 2975c5fa3c94SMilanka Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 2976c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2977c5fa3c94SMilanka Ringwald } 2978c5fa3c94SMilanka Ringwald 2979a0ffb263SMatthias Ringwald if (hfp_connection->microphone_gain != gain){ 2980a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_SET_MICROPHONE_GAIN; 2981a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = gain; 2982a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 2983aa4dd815SMatthias Ringwald } 2984f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2985c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2986aa4dd815SMatthias Ringwald } 2987aa4dd815SMatthias Ringwald 2988c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 29899c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2990a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2991c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2992a33eb0c4SMilanka Ringwald } 2993c5fa3c94SMilanka Ringwald 2994c5fa3c94SMilanka Ringwald if ((gain < 0) || (gain > 15)){ 2995c5fa3c94SMilanka Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 2996c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2997c5fa3c94SMilanka Ringwald } 2998c5fa3c94SMilanka Ringwald 2999a0ffb263SMatthias Ringwald if (hfp_connection->speaker_gain != gain){ 3000a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = gain; 3001a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 3002aa4dd815SMatthias Ringwald } 3003f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 3004c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 3005aa4dd815SMatthias Ringwald } 3006aa4dd815SMatthias Ringwald 3007c5fa3c94SMilanka Ringwald uint8_t hfp_ag_send_phone_number_for_voice_tag(hci_con_handle_t acl_handle, const char * number){ 30089c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3009a33eb0c4SMilanka Ringwald if (!hfp_connection){ 3010c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3011a33eb0c4SMilanka Ringwald } 3012c5fa3c94SMilanka Ringwald 3013aa4dd815SMatthias Ringwald hfp_ag_set_clip(0, number); 3014a0ffb263SMatthias Ringwald hfp_connection->send_phone_number_for_voice_tag = 1; 3015c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 3016aa4dd815SMatthias Ringwald } 3017aa4dd815SMatthias Ringwald 3018c5fa3c94SMilanka Ringwald uint8_t hfp_ag_reject_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 30199c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3020a33eb0c4SMilanka Ringwald if (!hfp_connection){ 3021c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3022a33eb0c4SMilanka Ringwald } 3023c5fa3c94SMilanka Ringwald 3024a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 3025c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 3026aa4dd815SMatthias Ringwald } 3027aa4dd815SMatthias Ringwald 3028c5fa3c94SMilanka Ringwald uint8_t hfp_ag_send_dtmf_code_done(hci_con_handle_t acl_handle){ 30299c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3030a33eb0c4SMilanka Ringwald if (!hfp_connection){ 3031c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3032a33eb0c4SMilanka Ringwald } 3033c5fa3c94SMilanka Ringwald 3034a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 3035c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 3036c1797c7dSMatthias Ringwald } 3037aa4dd815SMatthias Ringwald 3038ce263fc8SMatthias Ringwald void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){ 3039aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers = numbers; 3040aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers_count = numbers_count; 3041ce263fc8SMatthias Ringwald } 3042ce263fc8SMatthias Ringwald 30439cae807eSMatthias Ringwald void hfp_ag_clear_last_dialed_number(void){ 30449cae807eSMatthias Ringwald hfp_gsm_clear_last_dialed_number(); 3045ce263fc8SMatthias Ringwald } 3046ce263fc8SMatthias Ringwald 30479de679b7SMilanka Ringwald void hfp_ag_set_last_dialed_number(const char * number){ 30489de679b7SMilanka Ringwald hfp_gsm_set_last_dialed_number(number); 30499de679b7SMilanka Ringwald } 30509de679b7SMilanka Ringwald 3051c5fa3c94SMilanka Ringwald uint8_t hfp_ag_notify_incoming_call_waiting(hci_con_handle_t acl_handle){ 30529c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3053a33eb0c4SMilanka Ringwald if (!hfp_connection){ 3054c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3055a33eb0c4SMilanka Ringwald } 3056c5fa3c94SMilanka Ringwald 3057c5fa3c94SMilanka Ringwald if (!hfp_connection->call_waiting_notification_enabled){ 3058c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3059c5fa3c94SMilanka Ringwald } 3060a0ffb263SMatthias Ringwald 3061a0ffb263SMatthias Ringwald hfp_connection->ag_notify_incoming_call_waiting = 1; 3062f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 3063c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 3064a0ffb263SMatthias Ringwald } 3065ce263fc8SMatthias Ringwald 306676cc1527SMatthias Ringwald void hfp_ag_create_sdp_record(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr, const char * name, uint8_t ability_to_reject_call, uint16_t supported_features, int wide_band_speech){ 306776cc1527SMatthias Ringwald if (!name){ 3068aeb0f0feSMatthias Ringwald name = hfp_ag_default_service_name; 306976cc1527SMatthias Ringwald } 307076cc1527SMatthias Ringwald hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, rfcomm_channel_nr, name); 307176cc1527SMatthias Ringwald 307276cc1527SMatthias Ringwald /* 307376cc1527SMatthias Ringwald * 0x01 – Ability to reject a call 307476cc1527SMatthias Ringwald * 0x00 – No ability to reject a call 307576cc1527SMatthias Ringwald */ 307676cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301); // Hands-Free Profile - Network 307776cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call); 307876cc1527SMatthias Ringwald 307976cc1527SMatthias Ringwald // Construct SupportedFeatures for SDP bitmap: 308076cc1527SMatthias Ringwald // 308176cc1527SMatthias Ringwald // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 308276cc1527SMatthias Ringwald // of the Bits 0 to 4 of the unsolicited result code +BRSF" 308376cc1527SMatthias Ringwald // 308476cc1527SMatthias Ringwald // Wide band speech (bit 5) requires Codec negotiation 308576cc1527SMatthias Ringwald // 308676cc1527SMatthias Ringwald uint16_t sdp_features = supported_features & 0x1f; 3087ef3ae4ebSMilanka Ringwald if ( (wide_band_speech == 1) && (supported_features & (1 << HFP_AGSF_CODEC_NEGOTIATION))){ 308876cc1527SMatthias Ringwald sdp_features |= 1 << 5; 308976cc1527SMatthias Ringwald } 3090ef3ae4ebSMilanka Ringwald 3091ef3ae4ebSMilanka Ringwald if (supported_features & (1 << HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS)){ 3092ef3ae4ebSMilanka Ringwald sdp_features |= 1 << 6; 3093ef3ae4ebSMilanka Ringwald } 3094ef3ae4ebSMilanka Ringwald 3095ef3ae4ebSMilanka Ringwald if (supported_features & (1 << HFP_AGSF_VOICE_RECOGNITION_TEXT)){ 3096ef3ae4ebSMilanka Ringwald sdp_features |= 1 << 7; 3097ef3ae4ebSMilanka Ringwald } 3098ef3ae4ebSMilanka Ringwald 309976cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 310076cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 310176cc1527SMatthias Ringwald } 310276cc1527SMatthias Ringwald 310376cc1527SMatthias Ringwald void hfp_ag_register_packet_handler(btstack_packet_handler_t callback){ 310468466199SMilanka Ringwald btstack_assert(callback != NULL); 310568466199SMilanka Ringwald 310676cc1527SMatthias Ringwald hfp_ag_callback = callback; 310776cc1527SMatthias Ringwald hfp_set_ag_callback(callback); 310876cc1527SMatthias Ringwald } 3109