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 84*50b9afa4SMilanka Ringwald static void hfp_ag_trigger_ring_and_clip(); 85*50b9afa4SMilanka Ringwald 8699af1e28SMilanka Ringwald #define HFP_SUBEVENT_INVALID 0xFFFF 8727950165SMatthias Ringwald 8820b2edb6SMatthias Ringwald // const 89aeb0f0feSMatthias Ringwald static const char hfp_ag_default_service_name[] = "Voice gateway"; 90aa4dd815SMatthias Ringwald 9120b2edb6SMatthias Ringwald // globals 92aeb0f0feSMatthias Ringwald 93aeb0f0feSMatthias Ringwald // higher layer callbacks 94aeb0f0feSMatthias Ringwald static btstack_packet_handler_t hfp_ag_callback; 95aeb0f0feSMatthias Ringwald 9620b2edb6SMatthias Ringwald static btstack_packet_callback_registration_t hfp_ag_hci_event_callback_registration; 97aa4dd815SMatthias Ringwald 98aeb0f0feSMatthias Ringwald static uint16_t hfp_ag_supported_features; 9920b2edb6SMatthias Ringwald 100aeb0f0feSMatthias Ringwald // codecs 101aeb0f0feSMatthias Ringwald static uint8_t hfp_ag_codecs_nr; 102aeb0f0feSMatthias Ringwald static uint8_t hfp_ag_codecs[HFP_MAX_NUM_CODECS]; 1033deb3ec6SMatthias Ringwald 104aeb0f0feSMatthias Ringwald // AG indicators 10520b2edb6SMatthias Ringwald static int hfp_ag_indicators_nr; 10625789943SMilanka Ringwald static hfp_ag_indicator_t hfp_ag_indicators[HFP_MAX_NUM_INDICATORS]; 1073deb3ec6SMatthias Ringwald 108aeb0f0feSMatthias Ringwald // generic status indicators 109aeb0f0feSMatthias Ringwald static int hfp_ag_generic_status_indicators_nr; 110aeb0f0feSMatthias Ringwald static hfp_generic_status_indicator_t hfp_ag_generic_status_indicators[HFP_MAX_NUM_INDICATORS]; 111a0ffb263SMatthias Ringwald 11220b2edb6SMatthias Ringwald static int hfp_ag_call_hold_services_nr; 1133deb3ec6SMatthias Ringwald static char * hfp_ag_call_hold_services[6]; 1143deb3ec6SMatthias Ringwald 115ce263fc8SMatthias Ringwald static hfp_response_and_hold_state_t hfp_ag_response_and_hold_state; 11620b2edb6SMatthias Ringwald static int hfp_ag_response_and_hold_active; 117aa4dd815SMatthias Ringwald 11820b2edb6SMatthias Ringwald // Subscriber information entries 119aeb0f0feSMatthias Ringwald static hfp_phone_number_t * hfp_ag_subscriber_numbers; 120aeb0f0feSMatthias Ringwald static int hfp_ag_subscriber_numbers_count; 121ce263fc8SMatthias Ringwald 122a4f85bd2SMilanka Ringwald // call state 123a4f85bd2SMilanka Ringwald static btstack_timer_source_t hfp_ag_ring_timeout; 124a4f85bd2SMilanka Ringwald 12520b2edb6SMatthias Ringwald // code 126a0ffb263SMatthias Ringwald static int hfp_ag_get_ag_indicators_nr(hfp_connection_t * hfp_connection){ 127a0ffb263SMatthias Ringwald if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){ 128a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr; 1296535961aSMatthias Ringwald (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, 1306535961aSMatthias Ringwald hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 1313deb3ec6SMatthias Ringwald } 132a0ffb263SMatthias Ringwald return hfp_connection->ag_indicators_nr; 133a0ffb263SMatthias Ringwald } 134a0ffb263SMatthias Ringwald 135a0ffb263SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection){ 136a0ffb263SMatthias Ringwald // TODO: save only value, and value changed in the hfp_connection? 137a0ffb263SMatthias Ringwald if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){ 138a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr; 1396535961aSMatthias Ringwald (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, 1406535961aSMatthias Ringwald hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 141a0ffb263SMatthias Ringwald } 142a0ffb263SMatthias Ringwald return (hfp_ag_indicator_t *)&(hfp_connection->ag_indicators); 1433deb3ec6SMatthias Ringwald } 1443deb3ec6SMatthias Ringwald 145aa4dd815SMatthias Ringwald static hfp_ag_indicator_t * get_ag_indicator_for_name(const char * name){ 146aa4dd815SMatthias Ringwald int i; 147aa4dd815SMatthias Ringwald for (i = 0; i < hfp_ag_indicators_nr; i++){ 148aa4dd815SMatthias Ringwald if (strcmp(hfp_ag_indicators[i].name, name) == 0){ 149aa4dd815SMatthias Ringwald return &hfp_ag_indicators[i]; 150aa4dd815SMatthias Ringwald } 151aa4dd815SMatthias Ringwald } 152aa4dd815SMatthias Ringwald return NULL; 153aa4dd815SMatthias Ringwald } 154aa4dd815SMatthias Ringwald 155aa4dd815SMatthias Ringwald static int get_ag_indicator_index_for_name(const char * name){ 156aa4dd815SMatthias Ringwald int i; 157aa4dd815SMatthias Ringwald for (i = 0; i < hfp_ag_indicators_nr; i++){ 158aa4dd815SMatthias Ringwald if (strcmp(hfp_ag_indicators[i].name, name) == 0){ 159aa4dd815SMatthias Ringwald return i; 160aa4dd815SMatthias Ringwald } 161aa4dd815SMatthias Ringwald } 162aa4dd815SMatthias Ringwald return -1; 163aa4dd815SMatthias Ringwald } 164aa4dd815SMatthias Ringwald 1659c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_ag_connection_context_for_acl_handle(uint16_t handle){ 1669c9c64c1SMatthias Ringwald btstack_linked_list_iterator_t it; 1679c9c64c1SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1689c9c64c1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1699c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1709c9c64c1SMatthias Ringwald if (hfp_connection->acl_handle != handle) continue; 1719c9c64c1SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1729c9c64c1SMatthias Ringwald return hfp_connection; 1739c9c64c1SMatthias Ringwald } 1749c9c64c1SMatthias Ringwald return NULL; 1759c9c64c1SMatthias Ringwald } 1763deb3ec6SMatthias Ringwald 1770cb5b971SMatthias Ringwald static int use_in_band_tone(void){ 178aeb0f0feSMatthias Ringwald return get_bit(hfp_ag_supported_features, HFP_AGSF_IN_BAND_RING_TONE); 1793deb3ec6SMatthias Ringwald } 1803deb3ec6SMatthias Ringwald 181a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){ 182a0ffb263SMatthias Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_CODEC_NEGOTIATION); 183aeb0f0feSMatthias Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_CODEC_NEGOTIATION); 1843deb3ec6SMatthias Ringwald return hf && ag; 1853deb3ec6SMatthias Ringwald } 1863deb3ec6SMatthias Ringwald 187a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){ 188a0ffb263SMatthias Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_THREE_WAY_CALLING); 189aeb0f0feSMatthias Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_THREE_WAY_CALLING); 1903deb3ec6SMatthias Ringwald return hf && ag; 1913deb3ec6SMatthias Ringwald } 1923deb3ec6SMatthias Ringwald 193a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){ 194a0ffb263SMatthias Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_HF_INDICATORS); 195aeb0f0feSMatthias Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_HF_INDICATORS); 1963deb3ec6SMatthias Ringwald return hf && ag; 1973deb3ec6SMatthias Ringwald } 1983deb3ec6SMatthias Ringwald 19976cc1527SMatthias Ringwald /* unsolicited responses */ 200aa4dd815SMatthias Ringwald 201485ac19eSMilanka Ringwald static int hfp_ag_send_change_in_band_ring_tone_setting_cmd(uint16_t cid){ 202aa4dd815SMatthias Ringwald char buffer[20]; 203ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: %d\r\n", 204ff7d6aeaSMatthias Ringwald HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone()); 205ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 206aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2073deb3ec6SMatthias Ringwald } 2083deb3ec6SMatthias Ringwald 2093deb3ec6SMatthias Ringwald static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){ 2103deb3ec6SMatthias Ringwald char buffer[40]; 211ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n\r\nOK\r\n", 212aeb0f0feSMatthias Ringwald HFP_SUPPORTED_FEATURES, hfp_ag_supported_features); 213ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 2143deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2153deb3ec6SMatthias Ringwald } 2163deb3ec6SMatthias Ringwald 217485ac19eSMilanka Ringwald static int hfp_ag_send_ok(uint16_t cid){ 2183deb3ec6SMatthias Ringwald char buffer[10]; 219ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\nOK\r\n"); 220ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 2213deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2223deb3ec6SMatthias Ringwald } 2233deb3ec6SMatthias Ringwald 224485ac19eSMilanka Ringwald static int hfp_ag_send_ring(uint16_t cid){ 225aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, (char *) "\r\nRING\r\n"); 226aa4dd815SMatthias Ringwald } 227aa4dd815SMatthias Ringwald 228245852b7SMilanka Ringwald static int hfp_ag_send_no_carrier(uint16_t cid){ 229245852b7SMilanka Ringwald char buffer[15]; 230245852b7SMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\nNO CARRIER\r\n"); 231245852b7SMilanka Ringwald buffer[sizeof(buffer) - 1] = 0; 232245852b7SMilanka Ringwald return send_str_over_rfcomm(cid, buffer); 233245852b7SMilanka Ringwald } 234245852b7SMilanka Ringwald 235aa4dd815SMatthias Ringwald static int hfp_ag_send_clip(uint16_t cid){ 236aa4dd815SMatthias Ringwald char buffer[50]; 237ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP, 238ff7d6aeaSMatthias Ringwald hfp_gsm_clip_number(), hfp_gsm_clip_type()); 239ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 240aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 241aa4dd815SMatthias Ringwald } 242aa4dd815SMatthias Ringwald 243ce263fc8SMatthias Ringwald static int hfp_send_subscriber_number_cmd(uint16_t cid, uint8_t type, const char * number){ 244ce263fc8SMatthias Ringwald char buffer[50]; 245ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: ,\"%s\",%u, , \r\n", 246ff7d6aeaSMatthias Ringwald HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type); 247ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 248ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 249ce263fc8SMatthias Ringwald } 250ce263fc8SMatthias Ringwald 251c1797c7dSMatthias Ringwald static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){ 252aa4dd815SMatthias Ringwald char buffer[50]; 253ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: %s\r\n", 254ff7d6aeaSMatthias Ringwald HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number()); 255ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 256aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 257aa4dd815SMatthias Ringwald } 258aa4dd815SMatthias Ringwald 259aa4dd815SMatthias Ringwald static int hfp_ag_send_call_waiting_notification(uint16_t cid){ 260aa4dd815SMatthias Ringwald char buffer[50]; 261ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: \"%s\",%u\r\n", 262ff7d6aeaSMatthias Ringwald HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(), 263ff7d6aeaSMatthias Ringwald hfp_gsm_clip_type()); 264ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 265aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 266aa4dd815SMatthias Ringwald } 267aa4dd815SMatthias Ringwald 268485ac19eSMilanka Ringwald static int hfp_ag_send_error(uint16_t cid){ 2693deb3ec6SMatthias Ringwald char buffer[10]; 270ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\nERROR\r\n"); 271ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 2723deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2733deb3ec6SMatthias Ringwald } 2743deb3ec6SMatthias Ringwald 275485ac19eSMilanka Ringwald static int hfp_ag_send_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){ 2763deb3ec6SMatthias Ringwald char buffer[20]; 277ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s=%d\r\n", 278ff7d6aeaSMatthias Ringwald HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error); 279ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 2803deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2813deb3ec6SMatthias Ringwald } 2823deb3ec6SMatthias Ringwald 283ad902e3dSMatthias Ringwald // get size for indicator string 284a0ffb263SMatthias Ringwald static int hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection, int i){ 285ad902e3dSMatthias Ringwald // template: ("$NAME",($MIN,$MAX)) 28645718b6fSMatthias Ringwald return 8 + (int) strlen(hfp_ag_get_ag_indicators(hfp_connection)[i].name) 287a0ffb263SMatthias Ringwald + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range) 288a0ffb263SMatthias Ringwald + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range); 289ad902e3dSMatthias Ringwald } 290ad902e3dSMatthias Ringwald 291ad902e3dSMatthias Ringwald // store indicator 2921167ff83SMatthias Ringwald static void hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection, int i, uint8_t * buffer, uint16_t buffer_size){ 2931167ff83SMatthias Ringwald snprintf((char *)buffer, buffer_size, "(\"%s\",(%d,%d)),", 294a0ffb263SMatthias Ringwald hfp_ag_get_ag_indicators(hfp_connection)[i].name, 295a0ffb263SMatthias Ringwald hfp_ag_get_ag_indicators(hfp_connection)[i].min_range, 296a0ffb263SMatthias Ringwald hfp_ag_get_ag_indicators(hfp_connection)[i].max_range); 2971167ff83SMatthias Ringwald ((char *)buffer)[buffer_size - 1] = 0; 2983deb3ec6SMatthias Ringwald } 299ad902e3dSMatthias Ringwald 300ad902e3dSMatthias Ringwald // structure: header [indicator [comma indicator]] footer 301a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_num_segments(hfp_connection_t * hfp_connection){ 302a0ffb263SMatthias Ringwald int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 303ad902e3dSMatthias Ringwald if (!num_indicators) return 2; 304c1ab6cc1SMatthias Ringwald return 3 + ((num_indicators-1) * 2); 3053deb3ec6SMatthias Ringwald } 306ad902e3dSMatthias Ringwald 307ad902e3dSMatthias Ringwald // get size of individual segment for hfp_ag_retrieve_indicators_cmd 308a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_get_segment_len(hfp_connection_t * hfp_connection, int index){ 309ad902e3dSMatthias Ringwald if (index == 0) { 310ad902e3dSMatthias Ringwald return strlen(HFP_INDICATOR) + 3; // "\n\r%s:"" 311ad902e3dSMatthias Ringwald } 312ad902e3dSMatthias Ringwald index--; 313a0ffb263SMatthias Ringwald int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 314ad902e3dSMatthias Ringwald int indicator_index = index >> 1; 315ad902e3dSMatthias Ringwald if ((index & 1) == 0){ 316a0ffb263SMatthias Ringwald return hfp_ag_indicators_string_size(hfp_connection, indicator_index); 317ad902e3dSMatthias Ringwald } 318c1ab6cc1SMatthias Ringwald if (indicator_index == (num_indicators - 1)){ 319ad902e3dSMatthias Ringwald return 8; // "\r\n\r\nOK\r\n" 320ad902e3dSMatthias Ringwald } 321ad902e3dSMatthias Ringwald return 1; // comma 322ad902e3dSMatthias Ringwald } 323ad902e3dSMatthias Ringwald 3241167ff83SMatthias Ringwald static void hfp_ag_indicators_cmd_generator_store_segment(hfp_connection_t * hfp_connection, int index, uint8_t * buffer, uint16_t buffer_size){ 325ad902e3dSMatthias Ringwald if (index == 0){ 326ad902e3dSMatthias Ringwald *buffer++ = '\r'; 32774386ee0SMatthias Ringwald *buffer++ = '\n'; 328ad902e3dSMatthias Ringwald int len = strlen(HFP_INDICATOR); 3296535961aSMatthias Ringwald (void)memcpy(buffer, HFP_INDICATOR, len); 330ad902e3dSMatthias Ringwald buffer += len; 331ad902e3dSMatthias Ringwald *buffer++ = ':'; 332ad902e3dSMatthias Ringwald return; 333ad902e3dSMatthias Ringwald } 334ad902e3dSMatthias Ringwald index--; 335a0ffb263SMatthias Ringwald int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 336ad902e3dSMatthias Ringwald int indicator_index = index >> 1; 337ad902e3dSMatthias Ringwald if ((index & 1) == 0){ 3381167ff83SMatthias Ringwald hfp_ag_indicators_string_store(hfp_connection, indicator_index, buffer, buffer_size); 339ad902e3dSMatthias Ringwald return; 340ad902e3dSMatthias Ringwald } 341c1ab6cc1SMatthias Ringwald if (indicator_index == (num_indicators-1)){ 3426535961aSMatthias Ringwald (void)memcpy(buffer, "\r\n\r\nOK\r\n", 8); 343ad902e3dSMatthias Ringwald return; 344ad902e3dSMatthias Ringwald } 345ad902e3dSMatthias Ringwald *buffer = ','; 3463deb3ec6SMatthias Ringwald } 3473deb3ec6SMatthias Ringwald 348aeb0f0feSMatthias Ringwald static int hfp_ag_generic_indicators_join(char * buffer, int buffer_size){ 349c1ab6cc1SMatthias Ringwald if (buffer_size < (hfp_ag_indicators_nr * 3)) return 0; 3503deb3ec6SMatthias Ringwald int i; 3513deb3ec6SMatthias Ringwald int offset = 0; 352aeb0f0feSMatthias Ringwald for (i = 0; i < (hfp_ag_generic_status_indicators_nr - 1); i++) { 353aeb0f0feSMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_generic_status_indicators[i].uuid); 3543deb3ec6SMatthias Ringwald } 355aeb0f0feSMatthias Ringwald if (i < hfp_ag_generic_status_indicators_nr){ 356aeb0f0feSMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_generic_status_indicators[i].uuid); 3573deb3ec6SMatthias Ringwald } 3583deb3ec6SMatthias Ringwald return offset; 3593deb3ec6SMatthias Ringwald } 3603deb3ec6SMatthias Ringwald 361aeb0f0feSMatthias Ringwald static int hfp_ag_generic_indicators_initial_status_join(char * buffer, int buffer_size){ 362aeb0f0feSMatthias Ringwald if (buffer_size < (hfp_ag_generic_status_indicators_nr * 3)) return 0; 3633deb3ec6SMatthias Ringwald int i; 3643deb3ec6SMatthias Ringwald int offset = 0; 365aeb0f0feSMatthias Ringwald for (i = 0; i < hfp_ag_generic_status_indicators_nr; i++) { 366aeb0f0feSMatthias 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); 3673deb3ec6SMatthias Ringwald } 3683deb3ec6SMatthias Ringwald return offset; 3693deb3ec6SMatthias Ringwald } 3703deb3ec6SMatthias Ringwald 3713deb3ec6SMatthias Ringwald static int hfp_ag_indicators_status_join(char * buffer, int buffer_size){ 372c1ab6cc1SMatthias Ringwald if (buffer_size < (hfp_ag_indicators_nr * 3)) return 0; 3733deb3ec6SMatthias Ringwald int i; 3743deb3ec6SMatthias Ringwald int offset = 0; 375c1ab6cc1SMatthias Ringwald for (i = 0; i < (hfp_ag_indicators_nr-1); i++) { 3763deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_indicators[i].status); 3773deb3ec6SMatthias Ringwald } 3783deb3ec6SMatthias Ringwald if (i<hfp_ag_indicators_nr){ 3793deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_indicators[i].status); 3803deb3ec6SMatthias Ringwald } 3813deb3ec6SMatthias Ringwald return offset; 3823deb3ec6SMatthias Ringwald } 3833deb3ec6SMatthias Ringwald 3843deb3ec6SMatthias Ringwald static int hfp_ag_call_services_join(char * buffer, int buffer_size){ 385c1ab6cc1SMatthias Ringwald if (buffer_size < (hfp_ag_call_hold_services_nr * 3)) return 0; 3863deb3ec6SMatthias Ringwald int i; 3873deb3ec6SMatthias Ringwald int offset = snprintf(buffer, buffer_size, "("); 388c1ab6cc1SMatthias Ringwald for (i = 0; i < (hfp_ag_call_hold_services_nr-1); i++) { 3893deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%s,", hfp_ag_call_hold_services[i]); 3903deb3ec6SMatthias Ringwald } 3913deb3ec6SMatthias Ringwald if (i<hfp_ag_call_hold_services_nr){ 3923deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%s)", hfp_ag_call_hold_services[i]); 3933deb3ec6SMatthias Ringwald } 3943deb3ec6SMatthias Ringwald return offset; 3953deb3ec6SMatthias Ringwald } 3963deb3ec6SMatthias Ringwald 397485ac19eSMilanka Ringwald static int hfp_ag_send_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, 398ad902e3dSMatthias Ringwald int start_segment, int num_segments, 399a0ffb263SMatthias Ringwald int (*get_segment_len)(hfp_connection_t * hfp_connection, int segment), 4001167ff83SMatthias Ringwald void (*store_segment) (hfp_connection_t * hfp_connection, int segment, uint8_t * buffer, uint16_t buffer_size)){ 4013deb3ec6SMatthias Ringwald 402ad902e3dSMatthias Ringwald // assumes: can send now == true 403ad902e3dSMatthias Ringwald // assumes: num segments > 0 404aba39421SMatthias Ringwald // assumes: individual segments are smaller than MTU 405ad902e3dSMatthias Ringwald rfcomm_reserve_packet_buffer(); 406ad902e3dSMatthias Ringwald int mtu = rfcomm_get_max_frame_size(cid); 407ad902e3dSMatthias Ringwald uint8_t * data = rfcomm_get_outgoing_buffer(); 408ad902e3dSMatthias Ringwald int offset = 0; 409ad902e3dSMatthias Ringwald int segment = start_segment; 410ad902e3dSMatthias Ringwald while (segment < num_segments){ 411a0ffb263SMatthias Ringwald int segment_len = get_segment_len(hfp_connection, segment); 4121167ff83SMatthias Ringwald if ((offset + segment_len + 1) > mtu) break; 4131167ff83SMatthias Ringwald // append segment. As it appends a '\0', we provide a buffer one byte larger 4141167ff83SMatthias Ringwald store_segment(hfp_connection, segment, data+offset, segment_len + 1); 415ad902e3dSMatthias Ringwald offset += segment_len; 416ad902e3dSMatthias Ringwald segment++; 417ad902e3dSMatthias Ringwald } 418ad902e3dSMatthias Ringwald rfcomm_send_prepared(cid, offset); 419f20dd2aeSBjoern Hartmann #ifdef ENABLE_HFP_AT_MESSAGES 420f20dd2aeSBjoern Hartmann hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_SENT, (char *) data); 421f20dd2aeSBjoern Hartmann #endif 422ad902e3dSMatthias Ringwald return segment; 423ad902e3dSMatthias Ringwald } 4243deb3ec6SMatthias Ringwald 425ad902e3dSMatthias Ringwald // returns next segment to store 426485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, int start_segment){ 427a0ffb263SMatthias Ringwald int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection); 428485ac19eSMilanka Ringwald return hfp_ag_send_cmd_via_generator(cid, hfp_connection, start_segment, num_segments, 429894d499cSMatthias Ringwald hfp_ag_indicators_cmd_generator_get_segment_len, 430894d499cSMatthias Ringwald hfp_ag_indicators_cmd_generator_store_segment); 4313deb3ec6SMatthias Ringwald } 4323deb3ec6SMatthias Ringwald 433485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_status_cmd(uint16_t cid){ 4343deb3ec6SMatthias Ringwald char buffer[40]; 43589425bfcSMilanka Ringwald const int size = sizeof(buffer); 43689425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "\r\n%s:", HFP_INDICATOR); 43789425bfcSMilanka Ringwald offset += hfp_ag_indicators_status_join(buffer+offset, size-offset-9); 43889425bfcSMilanka Ringwald offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n"); 4393deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4403deb3ec6SMatthias Ringwald } 4413deb3ec6SMatthias Ringwald 442485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_can_hold_call_cmd(uint16_t cid){ 443ad902e3dSMatthias Ringwald char buffer[40]; 44489425bfcSMilanka Ringwald const int size = sizeof(buffer); 44589425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "\r\n%s:", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES); 44689425bfcSMilanka Ringwald offset += hfp_ag_call_services_join(buffer+offset, size-offset-9); 44789425bfcSMilanka Ringwald offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n"); 4483deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4493deb3ec6SMatthias Ringwald } 4503deb3ec6SMatthias Ringwald 4513deb3ec6SMatthias Ringwald 452485ac19eSMilanka Ringwald static int hfp_ag_send_list_supported_generic_status_indicators_cmd(uint16_t cid){ 453485ac19eSMilanka Ringwald return hfp_ag_send_ok(cid); 4543deb3ec6SMatthias Ringwald } 4553deb3ec6SMatthias Ringwald 456485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(uint16_t cid){ 4573deb3ec6SMatthias Ringwald char buffer[40]; 45889425bfcSMilanka Ringwald const int size = sizeof(buffer); 45989425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "\r\n%s:(", HFP_GENERIC_STATUS_INDICATOR); 460aeb0f0feSMatthias Ringwald offset += hfp_ag_generic_indicators_join(buffer + offset, size - offset - 10); 46189425bfcSMilanka Ringwald offset += snprintf(buffer+offset, size-offset, ")\r\n\r\nOK\r\n"); 4623deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4633deb3ec6SMatthias Ringwald } 4643deb3ec6SMatthias Ringwald 465485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid){ 4663deb3ec6SMatthias Ringwald char buffer[40]; 467aeb0f0feSMatthias Ringwald int offset = hfp_ag_generic_indicators_initial_status_join(buffer, sizeof(buffer) - 7); 4681cc1d9e9SMilanka Ringwald snprintf(buffer+offset, sizeof(buffer)-offset, "\r\nOK\r\n"); 4693deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4703deb3ec6SMatthias Ringwald } 4713deb3ec6SMatthias Ringwald 472485ac19eSMilanka Ringwald static int hfp_ag_send_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){ 4733deb3ec6SMatthias Ringwald char buffer[20]; 474ff7d6aeaSMatthias Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s:%d,%d\r\n", 475ff7d6aeaSMatthias Ringwald HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index, 476ff7d6aeaSMatthias Ringwald indicator->status); 477ff7d6aeaSMatthias Ringwald buffer[sizeof(buffer) - 1] = 0; 4783deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4793deb3ec6SMatthias Ringwald } 4803deb3ec6SMatthias Ringwald 481485ac19eSMilanka Ringwald static int hfp_ag_send_report_network_operator_name_cmd(uint16_t cid, hfp_network_opearator_t op){ 4825e71456cSMilanka Ringwald char buffer[41]; 4833deb3ec6SMatthias Ringwald if (strlen(op.name) == 0){ 48489425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s:%d,,\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode); 4853deb3ec6SMatthias Ringwald } else { 48689425bfcSMilanka Ringwald int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:%d,%d,", HFP_QUERY_OPERATOR_SELECTION, op.mode, op.format); 4875e71456cSMilanka Ringwald offset += snprintf(buffer+offset, 16, "%s", op.name); 48889425bfcSMilanka Ringwald snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n"); 4893deb3ec6SMatthias Ringwald } 4903deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4913deb3ec6SMatthias Ringwald } 4923deb3ec6SMatthias Ringwald 493a04aecb1SMilanka Ringwald static inline int hfp_ag_send_cmd_with_space_and_int(uint16_t cid, const char * cmd, uint8_t value){ 494a04aecb1SMilanka Ringwald char buffer[30]; 495a04aecb1SMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: %d\r\n", cmd, value); 496a04aecb1SMilanka Ringwald return send_str_over_rfcomm(cid, buffer); 497a04aecb1SMilanka Ringwald } 498a04aecb1SMilanka Ringwald 499a04aecb1SMilanka Ringwald 5001cc1d9e9SMilanka Ringwald static inline int hfp_ag_send_cmd_with_int(uint16_t cid, const char * cmd, uint8_t value){ 5011cc1d9e9SMilanka Ringwald char buffer[30]; 50289425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n", cmd, value); 5031cc1d9e9SMilanka Ringwald return send_str_over_rfcomm(cid, buffer); 5041cc1d9e9SMilanka Ringwald } 5053deb3ec6SMatthias Ringwald 506485ac19eSMilanka Ringwald static int hfp_ag_send_suggest_codec_cmd(uint16_t cid, uint8_t codec){ 5071cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec); 5083deb3ec6SMatthias Ringwald } 5093deb3ec6SMatthias Ringwald 510485ac19eSMilanka Ringwald static int hfp_ag_send_set_speaker_gain_cmd(uint16_t cid, uint8_t gain){ 5111cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain); 512aa4dd815SMatthias Ringwald } 513aa4dd815SMatthias Ringwald 514485ac19eSMilanka Ringwald static int hfp_ag_send_set_microphone_gain_cmd(uint16_t cid, uint8_t gain){ 5151cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain); 516aa4dd815SMatthias Ringwald } 517aa4dd815SMatthias Ringwald 518485ac19eSMilanka Ringwald static int hfp_ag_send_set_response_and_hold(uint16_t cid, int state){ 51966191aa0SMilanka Ringwald return hfp_ag_send_cmd_with_space_and_int(cid, HFP_RESPONSE_AND_HOLD, state); 520ce263fc8SMatthias Ringwald } 521ce263fc8SMatthias Ringwald 522cf75be85SMilanka Ringwald static int hfp_ag_send_enhanced_voice_recognition_state_cmd(hfp_connection_t * hfp_connection){ 523cf75be85SMilanka Ringwald char buffer[30]; 524cf75be85SMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, hfp_connection->enhanced_voice_recognition_enabled, hfp_connection->ag_vra_state); 525cf75be85SMilanka Ringwald return send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 526cf75be85SMilanka Ringwald } 527cf75be85SMilanka Ringwald 528d1b50446SMilanka Ringwald static int hfp_ag_send_voice_recognition_cmd(hfp_connection_t * hfp_connection, uint8_t activate_voice_recognition){ 52945796ff1SMilanka Ringwald char buffer[30]; 530d1b50446SMilanka Ringwald if (hfp_connection->enhanced_voice_recognition_enabled){ 531d1b50446SMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition, hfp_connection->ag_vra_state); 532d1b50446SMilanka Ringwald } else { 533d1b50446SMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: %d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition); 534d1b50446SMilanka Ringwald } 535d1b50446SMilanka Ringwald return send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 53645796ff1SMilanka Ringwald } 53745796ff1SMilanka Ringwald 53845796ff1SMilanka Ringwald static int hfp_ag_send_enhanced_voice_recognition_msg_cmd(hfp_connection_t * hfp_connection){ 53945796ff1SMilanka Ringwald char buffer[100]; 54045796ff1SMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s: 1,%d,%X,%d,%d,\"%s\"\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, 54145796ff1SMilanka Ringwald hfp_connection->ag_vra_state, 54245796ff1SMilanka Ringwald hfp_connection->ag_msg.text_id, 54345796ff1SMilanka Ringwald hfp_connection->ag_msg.text_type, 54445796ff1SMilanka Ringwald hfp_connection->ag_msg.text_operation, 54545796ff1SMilanka Ringwald hfp_connection->ag_msg.text); 54645796ff1SMilanka Ringwald return send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 54745796ff1SMilanka Ringwald } 54845796ff1SMilanka Ringwald 549a0ffb263SMatthias Ringwald static uint8_t hfp_ag_suggest_codec(hfp_connection_t *hfp_connection){ 550bc1b1537SMilanka Ringwald if (hfp_connection->sco_for_msbc_failed) return HFP_CODEC_CVSD; 551bc1b1537SMilanka Ringwald 552aeb0f0feSMatthias Ringwald if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_ag_codecs_nr, hfp_ag_codecs)){ 5536a7f44bdSMilanka Ringwald if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){ 554df327ff3SMilanka Ringwald return HFP_CODEC_MSBC; 5553deb3ec6SMatthias Ringwald } 5563deb3ec6SMatthias Ringwald } 557df327ff3SMilanka Ringwald return HFP_CODEC_CVSD; 5583deb3ec6SMatthias Ringwald } 5593deb3ec6SMatthias Ringwald 56076cc1527SMatthias Ringwald /* state machines */ 56176cc1527SMatthias Ringwald 562afac2a04SMilanka Ringwald static uint8_t hfp_ag_esco_s4_supported(hfp_connection_t * hfp_connection){ 563aeb0f0feSMatthias Ringwald return (hfp_connection->remote_supported_features & (1<<HFP_HFSF_ESCO_S4)) && (hfp_ag_supported_features & (1 << HFP_AGSF_ESCO_S4)); 5647522e673SMatthias Ringwald } 5657522e673SMatthias Ringwald 566a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){ 567aa4dd815SMatthias Ringwald /* events ( == commands): 568aa4dd815SMatthias Ringwald HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs 569aa4dd815SMatthias Ringwald HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 570aa4dd815SMatthias Ringwald hf_trigger_codec_connection_setup == received BCC 571aa4dd815SMatthias Ringwald ag_trigger_codec_connection_setup == received from AG to send BCS 572aa4dd815SMatthias Ringwald HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS 573aa4dd815SMatthias Ringwald */ 574a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 5753e3b9207SMilanka Ringwald case HFP_CODECS_EXCHANGED: 5763e3b9207SMilanka Ringwald if (hfp_connection->command == HFP_CMD_AVAILABLE_CODECS){ 5773e3b9207SMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 5783e3b9207SMilanka Ringwald return 1; 5793e3b9207SMilanka Ringwald } 5803e3b9207SMilanka Ringwald break; 5813e3b9207SMilanka Ringwald 582aa4dd815SMatthias Ringwald case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 583a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC; 584aa4dd815SMatthias Ringwald break; 585aa4dd815SMatthias Ringwald case HFP_CODECS_AG_RESEND_COMMON_CODEC: 586a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC; 587aa4dd815SMatthias Ringwald break; 588aa4dd815SMatthias Ringwald default: 589aa4dd815SMatthias Ringwald break; 590aa4dd815SMatthias Ringwald } 591aa4dd815SMatthias Ringwald 592a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 593aa4dd815SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 594a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){ 595a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_LIST; 596485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 597aa4dd815SMatthias Ringwald return 1; 598aa4dd815SMatthias Ringwald } 599aa4dd815SMatthias Ringwald 600a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 601aa4dd815SMatthias Ringwald case HFP_CODECS_AG_SENT_COMMON_CODEC: 602a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_AG_RESEND_COMMON_CODEC; 603aa4dd815SMatthias Ringwald break; 604aa4dd815SMatthias Ringwald default: 605aa4dd815SMatthias Ringwald break; 606aa4dd815SMatthias Ringwald } 607485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 608aa4dd815SMatthias Ringwald return 1; 609aa4dd815SMatthias Ringwald 610aa4dd815SMatthias Ringwald case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 611a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 61240e8d6c5SMatthias Ringwald hfp_connection->establish_audio_connection = 1; 613485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 614aa4dd815SMatthias Ringwald return 1; 615aa4dd815SMatthias Ringwald 616aa4dd815SMatthias Ringwald case HFP_CMD_AG_SEND_COMMON_CODEC: 617a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_AG_SENT_COMMON_CODEC; 618a0ffb263SMatthias Ringwald hfp_connection->suggested_codec = hfp_ag_suggest_codec(hfp_connection); 619485ac19eSMilanka Ringwald hfp_ag_send_suggest_codec_cmd(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec); 620aa4dd815SMatthias Ringwald return 1; 621aa4dd815SMatthias Ringwald 622aa4dd815SMatthias Ringwald case HFP_CMD_HF_CONFIRMED_CODEC: 623a0ffb263SMatthias Ringwald if (hfp_connection->codec_confirmed != hfp_connection->suggested_codec){ 624a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_ERROR; 625485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 626aa4dd815SMatthias Ringwald return 1; 627aa4dd815SMatthias Ringwald } 628a0ffb263SMatthias Ringwald hfp_connection->negotiated_codec = hfp_connection->codec_confirmed; 629a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 630c1ab6cc1SMatthias Ringwald log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD"); 631485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 6327522e673SMatthias Ringwald // now, pick link settings 633afac2a04SMilanka Ringwald hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection)); 6343721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP 6353721a235SMatthias Ringwald hfp_cc256x_prepare_for_sco(hfp_connection); 6363721a235SMatthias Ringwald #endif 637aa4dd815SMatthias Ringwald return 1; 638aa4dd815SMatthias Ringwald default: 639aa4dd815SMatthias Ringwald break; 640aa4dd815SMatthias Ringwald } 641aa4dd815SMatthias Ringwald return 0; 642aa4dd815SMatthias Ringwald } 643aa4dd815SMatthias Ringwald 644a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){ 645a0ffb263SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 646ca59be51SMatthias Ringwald hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr); 647aa4dd815SMatthias Ringwald 648a0ffb263SMatthias Ringwald // if active call exist, set per-hfp_connection state active, too (when audio is on) 649d0c20769SMatthias Ringwald if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 650a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE; 651aa4dd815SMatthias Ringwald } 652ce263fc8SMatthias Ringwald // if AG is ringing, also start ringing on the HF 653c4f8fc1cSMilanka Ringwald if (hfp_gsm_call_status() == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS){ 654c4f8fc1cSMilanka Ringwald switch (hfp_gsm_callsetup_status()){ 655c4f8fc1cSMilanka Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 6562ff1d7d7SMilanka Ringwald hfp_ag_hf_start_ringing_incoming(hfp_connection); 657c4f8fc1cSMilanka Ringwald break; 658fcd59412SMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 659fcd59412SMilanka Ringwald hfp_ag_hf_start_ringing_outgoing(hfp_connection); 660fcd59412SMilanka Ringwald break; 661c4f8fc1cSMilanka Ringwald default: 662c4f8fc1cSMilanka Ringwald break; 663c4f8fc1cSMilanka Ringwald } 664ce263fc8SMatthias Ringwald } 665aa4dd815SMatthias Ringwald } 6663deb3ec6SMatthias Ringwald 667a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){ 668c79b4cabSMatthias Ringwald // log_info("hfp_ag_run_for_context_service_level_connection state %u, command %u", hfp_connection->state, hfp_connection->command); 669a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 670c04cf7ffSMilanka Ringwald int sent = 0; 671a0ffb263SMatthias Ringwald switch(hfp_connection->command){ 6723deb3ec6SMatthias Ringwald case HFP_CMD_SUPPORTED_FEATURES: 673a0ffb263SMatthias Ringwald switch(hfp_connection->state){ 6743deb3ec6SMatthias Ringwald case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 675aa4dd815SMatthias Ringwald case HFP_EXCHANGE_SUPPORTED_FEATURES: 676aeb0f0feSMatthias Ringwald hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_ag_codecs, &hfp_ag_codecs_nr); 677a0ffb263SMatthias Ringwald if (has_codec_negotiation_feature(hfp_connection)){ 678a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS; 679aa4dd815SMatthias Ringwald } else { 680a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 681aa4dd815SMatthias Ringwald } 682a0ffb263SMatthias Ringwald hfp_ag_exchange_supported_features_cmd(hfp_connection->rfcomm_cid); 683aa4dd815SMatthias Ringwald return 1; 6843deb3ec6SMatthias Ringwald default: 6853deb3ec6SMatthias Ringwald break; 6863deb3ec6SMatthias Ringwald } 6873deb3ec6SMatthias Ringwald break; 6883deb3ec6SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 689c04cf7ffSMilanka Ringwald sent = codecs_exchange_state_machine(hfp_connection); 690a0ffb263SMatthias Ringwald if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){ 691a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 6923deb3ec6SMatthias Ringwald } 693c04cf7ffSMilanka Ringwald return sent; 694aa4dd815SMatthias Ringwald 695aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 696a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_W4_RETRIEVE_INDICATORS) { 697473ac565SMatthias Ringwald // HF requested AG Indicators and we did expect it 69884a0c24eSMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0; 699473ac565SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 700473ac565SMatthias Ringwald // continue below in state switch 701ad902e3dSMatthias Ringwald } 702ad902e3dSMatthias Ringwald break; 703aa4dd815SMatthias Ringwald 704aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 705a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break; 706a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE; 707485ac19eSMilanka Ringwald hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid); 708aa4dd815SMatthias Ringwald return 1; 709aa4dd815SMatthias Ringwald 7103deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 711a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break; 712a0ffb263SMatthias Ringwald if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 713a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL; 714a0ffb263SMatthias Ringwald } else if (has_hf_indicators_feature(hfp_connection)){ 715a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 716aa4dd815SMatthias Ringwald } else { 717a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 7183deb3ec6SMatthias Ringwald } 7198a46ec40SMatthias Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 720aa4dd815SMatthias Ringwald return 1; 7213deb3ec6SMatthias Ringwald 722aa4dd815SMatthias Ringwald case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES: 723a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break; 724a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 725a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 726aa4dd815SMatthias Ringwald } 727485ac19eSMilanka Ringwald hfp_ag_send_retrieve_can_hold_call_cmd(hfp_connection->rfcomm_cid); 728a0ffb263SMatthias Ringwald if (!has_hf_indicators_feature(hfp_connection)){ 729a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 730ce263fc8SMatthias Ringwald } 731aa4dd815SMatthias Ringwald return 1; 732aa4dd815SMatthias Ringwald 733aa4dd815SMatthias Ringwald case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 734a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break; 735a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS; 736485ac19eSMilanka Ringwald hfp_ag_send_list_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 737aa4dd815SMatthias Ringwald return 1; 738aa4dd815SMatthias Ringwald 739aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 740a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break; 741a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 742485ac19eSMilanka Ringwald hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 743aa4dd815SMatthias Ringwald return 1; 744aa4dd815SMatthias Ringwald 745aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 746a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break; 747a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 748485ac19eSMilanka Ringwald hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 749aa4dd815SMatthias Ringwald return 1; 7503deb3ec6SMatthias Ringwald default: 7513deb3ec6SMatthias Ringwald break; 7523deb3ec6SMatthias Ringwald } 753473ac565SMatthias Ringwald 754473ac565SMatthias Ringwald switch (hfp_connection->state){ 755473ac565SMatthias Ringwald case HFP_RETRIEVE_INDICATORS: { 756485ac19eSMilanka Ringwald int next_segment = hfp_ag_send_retrieve_indicators_cmd_via_generator(hfp_connection->rfcomm_cid, hfp_connection, hfp_connection->send_ag_indicators_segment); 757473ac565SMatthias Ringwald int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection); 758473ac565SMatthias Ringwald log_info("HFP_CMD_RETRIEVE_AG_INDICATORS next segment %u, num_segments %u", next_segment, num_segments); 759473ac565SMatthias Ringwald if (next_segment < num_segments){ 760473ac565SMatthias Ringwald // prepare sending of next segment 761473ac565SMatthias Ringwald hfp_connection->send_ag_indicators_segment = next_segment; 762473ac565SMatthias Ringwald log_info("HFP_CMD_RETRIEVE_AG_INDICATORS more. command %u, next seg %u", hfp_connection->command, next_segment); 763473ac565SMatthias Ringwald } else { 764473ac565SMatthias Ringwald // done, go to next state 765473ac565SMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0; 766473ac565SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS; 767473ac565SMatthias Ringwald } 768473ac565SMatthias Ringwald return 1; 769473ac565SMatthias Ringwald } 770473ac565SMatthias Ringwald default: 771473ac565SMatthias Ringwald break; 772473ac565SMatthias Ringwald } 773c04cf7ffSMilanka Ringwald return 0; 7743deb3ec6SMatthias Ringwald } 7753deb3ec6SMatthias Ringwald 776013cc750SMilanka Ringwald static bool hfp_ag_voice_recognition_supported(hfp_connection_t * hfp_connection, bool enhanced){ 777013cc750SMilanka Ringwald uint8_t ag_vra_flag = HFP_AGSF_VOICE_RECOGNITION_FUNCTION; 778013cc750SMilanka Ringwald uint8_t hf_vra_flag = HFP_HFSF_VOICE_RECOGNITION_FUNCTION; 779013cc750SMilanka Ringwald 780013cc750SMilanka Ringwald if (enhanced){ 781013cc750SMilanka Ringwald ag_vra_flag = HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS; 782013cc750SMilanka Ringwald hf_vra_flag = HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS; 783013cc750SMilanka Ringwald } 784013cc750SMilanka Ringwald 785aeb0f0feSMatthias Ringwald int ag = get_bit(hfp_ag_supported_features, ag_vra_flag); 786013cc750SMilanka Ringwald int hf = get_bit(hfp_connection->remote_supported_features, hf_vra_flag); 787d84719b1SMilanka Ringwald return hf && ag; 788d84719b1SMilanka Ringwald } 789d84719b1SMilanka Ringwald 790a0c831ebSMilanka Ringwald static uint8_t hfp_ag_can_activate_voice_recognition(hfp_connection_t * hfp_connection, bool enhanced){ 79108a0b01cSMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){ 79208a0b01cSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 79308a0b01cSMilanka Ringwald } 794013cc750SMilanka Ringwald if (!hfp_ag_voice_recognition_supported(hfp_connection, enhanced)){ 795d84719b1SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 796d84719b1SMilanka Ringwald } 7970c738fd4SMilanka Ringwald if (hfp_connection->vra_state != HFP_VRA_VOICE_RECOGNITION_OFF){ 798d84719b1SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 799d84719b1SMilanka Ringwald } 800d84719b1SMilanka Ringwald return ERROR_CODE_SUCCESS; 801d84719b1SMilanka Ringwald } 802d84719b1SMilanka Ringwald 803a0c831ebSMilanka Ringwald static uint8_t hfp_ag_voice_recognition_session_active(hfp_connection_t * hfp_connection, bool enhanced){ 80408a0b01cSMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){ 80508a0b01cSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 80608a0b01cSMilanka Ringwald } 807013cc750SMilanka Ringwald if (!hfp_ag_voice_recognition_supported(hfp_connection, enhanced)){ 808d84719b1SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 809d84719b1SMilanka 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){ 812de9e0ea7SMilanka Ringwald return ERROR_CODE_SUCCESS; 813de9e0ea7SMilanka Ringwald } 814d84719b1SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 815d84719b1SMilanka Ringwald } 816d84719b1SMilanka Ringwald return ERROR_CODE_SUCCESS; 817d84719b1SMilanka Ringwald } 818754910caSMilanka Ringwald 819f48848deSMilanka Ringwald static bool hfp_ag_can_send_enahnced_voice_recognition_message(hfp_connection_t * hfp_connection, bool enhanced){ 820f48848deSMilanka Ringwald if (hfp_ag_voice_recognition_session_active(hfp_connection, enhanced) == ERROR_CODE_SUCCESS){ 821aeb0f0feSMatthias Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_VOICE_RECOGNITION_TEXT); 822f48848deSMilanka Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_TEXT); 823f48848deSMilanka Ringwald return hf && ag; 824f48848deSMilanka Ringwald } 825f48848deSMilanka Ringwald return false; 826f48848deSMilanka Ringwald } 827adb00feeSMilanka Ringwald 8280b4debbfSMilanka Ringwald static bool hfp_ag_is_audio_connection_active(hfp_connection_t * hfp_connection){ 8290b4debbfSMilanka Ringwald switch (hfp_connection->state){ 8300b4debbfSMilanka Ringwald case HFP_W2_CONNECT_SCO: 8310b4debbfSMilanka Ringwald case HFP_W4_SCO_CONNECTED: 8320b4debbfSMilanka Ringwald case HFP_AUDIO_CONNECTION_ESTABLISHED: 8330b4debbfSMilanka Ringwald return true; 834754910caSMilanka Ringwald default: 8350b4debbfSMilanka Ringwald return false; 836754910caSMilanka Ringwald } 837754910caSMilanka Ringwald } 838754910caSMilanka Ringwald 839b95cac54SMilanka Ringwald static void hfp_ag_emit_enhanced_voice_recognition_msg_sent_event(hfp_connection_t * hfp_connection, uint8_t status){ 840b95cac54SMilanka Ringwald hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 841b95cac54SMilanka Ringwald 842b95cac54SMilanka Ringwald uint8_t event[6]; 843b95cac54SMilanka Ringwald event[0] = HCI_EVENT_HFP_META; 844b95cac54SMilanka Ringwald event[1] = sizeof(event) - 2; 845c95b5b3cSMilanka Ringwald event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_MESSAGE_SENT; 846b95cac54SMilanka Ringwald little_endian_store_16(event, 3, acl_handle); 847b95cac54SMilanka Ringwald event[5] = status; 848b95cac54SMilanka Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 849b95cac54SMilanka Ringwald } 850b95cac54SMilanka Ringwald 851347d46c8SMilanka Ringwald 852585db78dSMilanka Ringwald static void hfp_ag_emit_hf_indicator_value(hfp_connection_t * hfp_connection, uint16_t uuid, uint8_t value){ 853347d46c8SMilanka Ringwald hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 854347d46c8SMilanka Ringwald 855347d46c8SMilanka Ringwald uint8_t event[8]; 856347d46c8SMilanka Ringwald event[0] = HCI_EVENT_HFP_META; 857347d46c8SMilanka Ringwald event[1] = sizeof(event) - 2; 858347d46c8SMilanka Ringwald event[2] = HFP_SUBEVENT_HF_INDICATOR; 859347d46c8SMilanka Ringwald little_endian_store_16(event, 3, acl_handle); 860347d46c8SMilanka Ringwald little_endian_store_16(event, 5, uuid); 861347d46c8SMilanka Ringwald event[7] = value; 862347d46c8SMilanka Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 863347d46c8SMilanka Ringwald } 864347d46c8SMilanka Ringwald 865a4f85bd2SMilanka Ringwald static void hfp_ag_emit_general_simple_event(uint8_t event_subtype){ 866a4f85bd2SMilanka Ringwald if (!hfp_ag_callback) return; 867a4f85bd2SMilanka Ringwald 868a4f85bd2SMilanka Ringwald uint8_t event[5]; 869a4f85bd2SMilanka Ringwald event[0] = HCI_EVENT_HFP_META; 870a4f85bd2SMilanka Ringwald event[1] = sizeof(event) - 2; 871a4f85bd2SMilanka Ringwald event[2] = event_subtype; 872a4f85bd2SMilanka Ringwald little_endian_store_16(event, 3, HCI_CON_HANDLE_INVALID); 873a4f85bd2SMilanka Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 874a4f85bd2SMilanka Ringwald } 875a4f85bd2SMilanka Ringwald 876754910caSMilanka Ringwald static int hfp_ag_voice_recognition_state_machine(hfp_connection_t * hfp_connection){ 877754910caSMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) { 878754910caSMilanka Ringwald return 0; 879754910caSMilanka Ringwald } 880754910caSMilanka Ringwald int done = 0; 881754910caSMilanka Ringwald uint8_t status; 882754910caSMilanka Ringwald 883fd4151d1SMilanka Ringwald switch (hfp_connection->command){ 884fd4151d1SMilanka Ringwald case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION: 885cf75be85SMilanka Ringwald switch (hfp_connection->vra_state_requested){ 886b95cac54SMilanka Ringwald case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_MSG: 887b95cac54SMilanka Ringwald done = hfp_ag_send_enhanced_voice_recognition_msg_cmd(hfp_connection); 888b95cac54SMilanka Ringwald if (done == 0){ 889b95cac54SMilanka Ringwald hfp_ag_emit_enhanced_voice_recognition_msg_sent_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 890b95cac54SMilanka Ringwald } else { 891b95cac54SMilanka Ringwald hfp_ag_emit_enhanced_voice_recognition_msg_sent_event(hfp_connection, ERROR_CODE_SUCCESS); 892b95cac54SMilanka Ringwald } 893b95cac54SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 894b95cac54SMilanka Ringwald return done; 895b95cac54SMilanka Ringwald 896cf75be85SMilanka Ringwald case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_STATUS: 897cf75be85SMilanka Ringwald done = hfp_ag_send_enhanced_voice_recognition_state_cmd(hfp_connection); 898cf75be85SMilanka Ringwald if (done == 0){ 899cf75be85SMilanka Ringwald hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 900cf75be85SMilanka Ringwald } else { 901cf75be85SMilanka Ringwald hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS); 902cf75be85SMilanka Ringwald } 903cf75be85SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 904b95cac54SMilanka Ringwald return done; 905b95cac54SMilanka Ringwald 906cf75be85SMilanka Ringwald default: 907d1b50446SMilanka Ringwald done = hfp_ag_send_voice_recognition_cmd(hfp_connection, hfp_connection->ag_activate_voice_recognition_value); 9080c738fd4SMilanka Ringwald if (done == 0){ 9090c738fd4SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 9101a26de69SMilanka Ringwald hfp_emit_voice_recognition_state_event(hfp_connection, ERROR_CODE_COMMAND_DISALLOWED); 911fd4151d1SMilanka Ringwald return 0; 912fd4151d1SMilanka Ringwald } 913fd4151d1SMilanka Ringwald break; 914cf75be85SMilanka Ringwald } 915971388feSMatthias Ringwald break; 916fd4151d1SMilanka Ringwald 917fd4151d1SMilanka Ringwald case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION: 918734ef2bdSMilanka Ringwald // HF initiatied voice recognition, parser extracted activation value 919de9e0ea7SMilanka Ringwald switch (hfp_connection->ag_activate_voice_recognition_value){ 920de9e0ea7SMilanka Ringwald case 0: 921a0c831ebSMilanka Ringwald if (hfp_ag_voice_recognition_session_active(hfp_connection, hfp_connection->enhanced_voice_recognition_enabled) == ERROR_CODE_SUCCESS){ 922fd4151d1SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF; 923fd4151d1SMilanka Ringwald done = hfp_ag_send_ok(hfp_connection->rfcomm_cid); 924fd4151d1SMilanka Ringwald } 925de9e0ea7SMilanka Ringwald break; 926de9e0ea7SMilanka Ringwald 927de9e0ea7SMilanka Ringwald case 1: 928a0c831ebSMilanka Ringwald if (hfp_ag_can_activate_voice_recognition(hfp_connection, hfp_connection->enhanced_voice_recognition_enabled) == ERROR_CODE_SUCCESS){ 929fd4151d1SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED; 930fd4151d1SMilanka Ringwald done = hfp_ag_send_ok(hfp_connection->rfcomm_cid); 931fd4151d1SMilanka Ringwald } 932de9e0ea7SMilanka Ringwald break; 933de9e0ea7SMilanka Ringwald 934de9e0ea7SMilanka Ringwald case 2: 935de9e0ea7SMilanka Ringwald if (hfp_ag_voice_recognition_session_active(hfp_connection, hfp_connection->enhanced_voice_recognition_enabled) == ERROR_CODE_SUCCESS){ 936de9e0ea7SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 937de9e0ea7SMilanka Ringwald done = hfp_ag_send_ok(hfp_connection->rfcomm_cid); 938de9e0ea7SMilanka Ringwald } 939de9e0ea7SMilanka Ringwald break; 940de9e0ea7SMilanka Ringwald default: 941de9e0ea7SMilanka Ringwald break; 942fd4151d1SMilanka Ringwald } 943fd4151d1SMilanka Ringwald if (done == 0){ 944fd4151d1SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 945fd4151d1SMilanka Ringwald done = hfp_ag_send_error(hfp_connection->rfcomm_cid); 946fd4151d1SMilanka Ringwald return 1; 947fd4151d1SMilanka Ringwald } 948fd4151d1SMilanka Ringwald break; 949fd4151d1SMilanka Ringwald 950fd4151d1SMilanka Ringwald default: 951754910caSMilanka Ringwald return 0; 952754910caSMilanka Ringwald } 953754910caSMilanka Ringwald 954fd4151d1SMilanka Ringwald switch (hfp_connection->vra_state_requested){ 955de9e0ea7SMilanka Ringwald case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 956de9e0ea7SMilanka Ringwald if (!hfp_ag_is_audio_connection_active(hfp_connection)){ 957de9e0ea7SMilanka Ringwald status = hfp_ag_setup_audio_connection(hfp_connection); 958de9e0ea7SMilanka Ringwald if (status != ERROR_CODE_SUCCESS){ 959de9e0ea7SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 960de9e0ea7SMilanka Ringwald hfp_emit_voice_recognition_state_event(hfp_connection, status); 961de9e0ea7SMilanka Ringwald return 0; 962de9e0ea7SMilanka Ringwald } 963de9e0ea7SMilanka Ringwald } 964de9e0ea7SMilanka Ringwald hfp_connection->enhanced_voice_recognition_enabled = true; 965de9e0ea7SMilanka Ringwald hfp_connection->vra_state = HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 966de9e0ea7SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 967de9e0ea7SMilanka Ringwald hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_SUCCESS); 968de9e0ea7SMilanka Ringwald break; 969de9e0ea7SMilanka Ringwald 970fd4151d1SMilanka Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 9710c738fd4SMilanka Ringwald hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF; 9720c738fd4SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 9730c738fd4SMilanka Ringwald 9741a26de69SMilanka Ringwald // release audio connection only if it was opened after audio VR activated 9751a26de69SMilanka Ringwald if (hfp_ag_is_audio_connection_active(hfp_connection) && !hfp_connection->ag_audio_connection_opened_before_vra){ 976754910caSMilanka Ringwald hfp_trigger_release_audio_connection(hfp_connection); 9770b4debbfSMilanka Ringwald } 9780b4debbfSMilanka Ringwald 9791a26de69SMilanka Ringwald hfp_emit_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS); 980754910caSMilanka Ringwald break; 981754910caSMilanka Ringwald 982fd4151d1SMilanka Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED: 9830b4debbfSMilanka Ringwald // open only if audio connection does not already exist 9840b4debbfSMilanka Ringwald if (!hfp_ag_is_audio_connection_active(hfp_connection)){ 985754910caSMilanka Ringwald status = hfp_ag_setup_audio_connection(hfp_connection); 986754910caSMilanka Ringwald if (status != ERROR_CODE_SUCCESS){ 9870c738fd4SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 9881a26de69SMilanka Ringwald hfp_emit_voice_recognition_state_event(hfp_connection, status); 9890c738fd4SMilanka Ringwald return 0; 990754910caSMilanka Ringwald } 9910b4debbfSMilanka Ringwald } 9920c738fd4SMilanka Ringwald 9930c738fd4SMilanka Ringwald hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_ACTIVATED; 9940c738fd4SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state; 9951a26de69SMilanka Ringwald hfp_emit_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS); 996754910caSMilanka Ringwald break; 997754910caSMilanka Ringwald 998754910caSMilanka Ringwald default: 999754910caSMilanka Ringwald break; 1000754910caSMilanka Ringwald } 1001754910caSMilanka Ringwald return done; 1002754910caSMilanka Ringwald } 1003754910caSMilanka Ringwald 1004a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){ 1005c04cf7ffSMilanka Ringwald int sent = codecs_exchange_state_machine(hfp_connection); 1006c04cf7ffSMilanka Ringwald if (sent) return 1; 1007aa4dd815SMatthias Ringwald 1008a0ffb263SMatthias Ringwald switch(hfp_connection->command){ 100945796ff1SMilanka Ringwald 1010aa4dd815SMatthias Ringwald case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING: 1011485ac19eSMilanka Ringwald hfp_ag_send_change_in_band_ring_tone_setting_cmd(hfp_connection->rfcomm_cid); 1012aa4dd815SMatthias Ringwald return 1; 1013aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1014485ac19eSMilanka Ringwald hfp_ag_send_report_network_operator_name_cmd(hfp_connection->rfcomm_cid, hfp_connection->network_operator); 1015aa4dd815SMatthias Ringwald return 1; 1016aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 1017a0ffb263SMatthias Ringwald if (hfp_connection->network_operator.format != 0){ 1018485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 1019aa4dd815SMatthias Ringwald } else { 1020485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 10213deb3ec6SMatthias Ringwald } 1022aa4dd815SMatthias Ringwald return 1; 1023aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE: 1024485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1025aa4dd815SMatthias Ringwald return 1; 10263deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR: 1027a0ffb263SMatthias Ringwald if (hfp_connection->extended_audio_gateway_error){ 1028a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 1029485ac19eSMilanka Ringwald hfp_ag_send_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, hfp_connection->extended_audio_gateway_error_value); 1030aa4dd815SMatthias Ringwald return 1; 10313deb3ec6SMatthias Ringwald } 1032202c8a4cSMatthias Ringwald break; 10333deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 1034485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1035ce263fc8SMatthias Ringwald return 1; 10363deb3ec6SMatthias Ringwald default: 10373deb3ec6SMatthias Ringwald break; 10383deb3ec6SMatthias Ringwald } 1039aa4dd815SMatthias Ringwald return 0; 10403deb3ec6SMatthias Ringwald } 10413deb3ec6SMatthias Ringwald 1042a0ffb263SMatthias Ringwald static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){ 1043505f1c30SMatthias Ringwald if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) || 1044505f1c30SMatthias Ringwald (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0; 10453deb3ec6SMatthias Ringwald 1046a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 104752cbdd6dSMilanka Ringwald 104852cbdd6dSMilanka Ringwald if (hfp_connection->release_audio_connection){ 104952cbdd6dSMilanka Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 105052cbdd6dSMilanka Ringwald hfp_connection->release_audio_connection = 0; 105152cbdd6dSMilanka Ringwald gap_disconnect(hfp_connection->sco_handle); 105252cbdd6dSMilanka Ringwald return 1; 105352cbdd6dSMilanka Ringwald } 1054aa4dd815SMatthias Ringwald 105529cddf58SMatthias Ringwald // accept incoming audio connection (codec negotiation is not used) 105629cddf58SMatthias Ringwald if (hfp_connection->accept_sco){ 105729cddf58SMatthias Ringwald // notify about codec selection if not done already 105829cddf58SMatthias Ringwald if (hfp_connection->negotiated_codec == 0){ 105929cddf58SMatthias Ringwald hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 106029cddf58SMatthias Ringwald } 106129cddf58SMatthias Ringwald // 106229cddf58SMatthias Ringwald bool incoming_eSCO = hfp_connection->accept_sco == 2; 106329cddf58SMatthias Ringwald hfp_connection->accept_sco = 0; 106429cddf58SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 106529cddf58SMatthias Ringwald hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO); 106629cddf58SMatthias Ringwald return 1; 106729cddf58SMatthias Ringwald } 106829cddf58SMatthias Ringwald 1069aa4dd815SMatthias Ringwald // run codecs exchange 1070c04cf7ffSMilanka Ringwald int sent = codecs_exchange_state_machine(hfp_connection); 1071c04cf7ffSMilanka Ringwald if (sent) return 1; 1072aa4dd815SMatthias Ringwald 1073c04cf7ffSMilanka Ringwald if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0; 1074a0ffb263SMatthias Ringwald if (hfp_connection->establish_audio_connection){ 1075a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 1076a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 1077eddcd308SMatthias Ringwald hfp_setup_synchronous_connection(hfp_connection); 1078aa4dd815SMatthias Ringwald return 1; 1079aa4dd815SMatthias Ringwald } 1080aa4dd815SMatthias Ringwald return 0; 1081aa4dd815SMatthias Ringwald } 1082aa4dd815SMatthias Ringwald 1083056efd45SMatthias Ringwald static void hfp_ag_set_callsetup_indicator(void){ 1084056efd45SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup"); 1085056efd45SMatthias Ringwald if (!indicator){ 1086056efd45SMatthias Ringwald log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing"); 1087056efd45SMatthias Ringwald return; 1088056efd45SMatthias Ringwald }; 1089056efd45SMatthias Ringwald indicator->status = hfp_gsm_callsetup_status(); 1090056efd45SMatthias Ringwald } 1091056efd45SMatthias Ringwald 1092056efd45SMatthias Ringwald static void hfp_ag_set_callheld_indicator(void){ 1093056efd45SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld"); 1094056efd45SMatthias Ringwald if (!indicator){ 1095056efd45SMatthias Ringwald log_error("hfp_ag_set_callheld_state: callheld indicator is missing"); 1096056efd45SMatthias Ringwald return; 1097056efd45SMatthias Ringwald }; 1098056efd45SMatthias Ringwald indicator->status = hfp_gsm_callheld_status(); 1099056efd45SMatthias Ringwald } 1100056efd45SMatthias Ringwald 1101aa4dd815SMatthias Ringwald // 1102fcd59412SMilanka Ringwald // transition implementations for hfp_ag_call_state_machine 1103aa4dd815SMatthias Ringwald // 1104aa4dd815SMatthias Ringwald 11052ff1d7d7SMilanka Ringwald static void hfp_ag_hf_start_ringing_incoming(hfp_connection_t * hfp_connection){ 1106aa4dd815SMatthias Ringwald if (use_in_band_tone()){ 1107a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING; 1108d97d752dSMilanka Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 1109aa4dd815SMatthias Ringwald } else { 11109493406bSMilanka Ringwald hfp_connection->call_state = HFP_CALL_INCOMING_RINGING; 1111aa4dd815SMatthias Ringwald } 1112aa4dd815SMatthias Ringwald } 1113aa4dd815SMatthias Ringwald 1114fcd59412SMilanka Ringwald static void hfp_ag_hf_start_ringing_outgoing(hfp_connection_t * hfp_connection){ 1115fcd59412SMilanka Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING; 1116fcd59412SMilanka Ringwald } 1117fcd59412SMilanka Ringwald 1118a0ffb263SMatthias Ringwald static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){ 1119a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 0; 1120a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = 0; 1121aa4dd815SMatthias Ringwald } 1122aa4dd815SMatthias Ringwald 1123a4f85bd2SMilanka Ringwald 112431f55b91SMilanka Ringwald static void hfp_ag_trigger_incoming_call_during_active_one(void){ 112531f55b91SMilanka Ringwald btstack_linked_list_iterator_t it; 112631f55b91SMilanka Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 112731f55b91SMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 112831f55b91SMilanka Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 112931f55b91SMilanka Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 113031f55b91SMilanka Ringwald if (hfp_connection->call_state != HFP_CALL_ACTIVE) continue; 113131f55b91SMilanka Ringwald 113231f55b91SMilanka Ringwald hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING; 113331f55b91SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 113431f55b91SMilanka Ringwald } 113531f55b91SMilanka Ringwald } 113631f55b91SMilanka Ringwald 1137*50b9afa4SMilanka Ringwald static void hfp_ag_trigger_ring_and_clip() { 1138a4f85bd2SMilanka Ringwald btstack_linked_list_iterator_t it; 1139a4f85bd2SMilanka Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1140a4f85bd2SMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1141a4f85bd2SMilanka Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1142a4f85bd2SMilanka Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1143b7e27351SMilanka Ringwald switch (hfp_connection->call_state){ 1144b7e27351SMilanka Ringwald case HFP_CALL_INCOMING_RINGING: 1145b7e27351SMilanka Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING: 1146b7e27351SMilanka Ringwald case HFP_CALL_OUTGOING_RINGING: 11473561686eSMatthias Ringwald // Queue RING on this connection 1148a4f85bd2SMilanka Ringwald hfp_connection->ag_ring = 1; 11493561686eSMatthias Ringwald 11503561686eSMatthias Ringwald // HFP v1.8, 4.23 Calling Line Identification (CLI) Notification 11513561686eSMatthias Ringwald // "If the calling subscriber number information is available from the network, the AG shall issue the 11523561686eSMatthias Ringwald // +CLIP unsolicited result code just after every RING indication when the HF is alerted in an incoming call." 11533561686eSMatthias Ringwald hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled; 11543561686eSMatthias Ringwald 11553561686eSMatthias Ringwald // trigger next message 1156a4f85bd2SMilanka Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 1157b7e27351SMilanka Ringwald break; 1158b7e27351SMilanka Ringwald default: 1159b7e27351SMilanka Ringwald break; 1160b7e27351SMilanka Ringwald } 1161a4f85bd2SMilanka Ringwald } 1162a4f85bd2SMilanka Ringwald } 1163a4f85bd2SMilanka Ringwald 1164*50b9afa4SMilanka Ringwald // trigger RING and CLIP messages on connections that are ringing 1165*50b9afa4SMilanka Ringwald static void hfp_ag_ring_timeout_handler(btstack_timer_source_t * timer){ 1166*50b9afa4SMilanka Ringwald hfp_ag_trigger_ring_and_clip(); 1167*50b9afa4SMilanka Ringwald 1168*50b9afa4SMilanka Ringwald btstack_run_loop_set_timer(timer, RING_PERIOD_MS); // 2 seconds timeout 1169*50b9afa4SMilanka Ringwald btstack_run_loop_add_timer(timer); 1170a4f85bd2SMilanka Ringwald } 1171a4f85bd2SMilanka Ringwald 1172a4f85bd2SMilanka Ringwald static void hfp_ag_ring_timeout_stop(void){ 1173a4f85bd2SMilanka Ringwald btstack_run_loop_remove_timer(&hfp_ag_ring_timeout); 1174a4f85bd2SMilanka Ringwald } 1175a4f85bd2SMilanka Ringwald 1176a4f85bd2SMilanka Ringwald static void hfp_ag_start_ringing(void){ 1177*50b9afa4SMilanka Ringwald // setup ring timer 1178*50b9afa4SMilanka Ringwald btstack_run_loop_remove_timer(&hfp_ag_ring_timeout); 1179*50b9afa4SMilanka Ringwald btstack_run_loop_set_timer_handler(&hfp_ag_ring_timeout, hfp_ag_ring_timeout_handler); 1180*50b9afa4SMilanka Ringwald btstack_run_loop_set_timer(&hfp_ag_ring_timeout, RING_PERIOD_MS); // 2 seconds timeout 1181*50b9afa4SMilanka Ringwald btstack_run_loop_add_timer(&hfp_ag_ring_timeout); 1182*50b9afa4SMilanka Ringwald 1183*50b9afa4SMilanka Ringwald // emit start ringing 1184a4f85bd2SMilanka Ringwald hfp_ag_emit_general_simple_event(HFP_SUBEVENT_START_RINGING); 1185*50b9afa4SMilanka Ringwald 1186*50b9afa4SMilanka Ringwald // send initial RING + CLIP 1187*50b9afa4SMilanka Ringwald hfp_ag_trigger_ring_and_clip(); 1188a4f85bd2SMilanka Ringwald } 1189a4f85bd2SMilanka Ringwald 1190a4f85bd2SMilanka Ringwald static void hfp_ag_stop_ringing(void){ 1191a4f85bd2SMilanka Ringwald hfp_ag_ring_timeout_stop(); 1192a4f85bd2SMilanka Ringwald hfp_ag_emit_general_simple_event(HFP_SUBEVENT_STOP_RINGING); 1193a4f85bd2SMilanka Ringwald 1194a4f85bd2SMilanka Ringwald btstack_linked_list_iterator_t it; 1195a4f85bd2SMilanka Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1196a4f85bd2SMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1197a4f85bd2SMilanka Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1198a4f85bd2SMilanka Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1199a4f85bd2SMilanka Ringwald if ((hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) && 1200a4f85bd2SMilanka Ringwald (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue; 1201a4f85bd2SMilanka Ringwald hfp_ag_hf_stop_ringing(hfp_connection); 1202a4f85bd2SMilanka Ringwald } 1203a4f85bd2SMilanka Ringwald } 1204a4f85bd2SMilanka Ringwald 120531f55b91SMilanka Ringwald static void hfp_ag_trigger_incoming_call_idle(void){ 1206aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name("callsetup"); 1207aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 1208aa4dd815SMatthias Ringwald 12093561686eSMatthias Ringwald // update callsetup state in connections 1210665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1211665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1212665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1213a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 121466c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 121531f55b91SMilanka Ringwald if (hfp_connection->call_state != HFP_CALL_IDLE) continue; 121631f55b91SMilanka Ringwald 1217a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1218a4f85bd2SMilanka Ringwald 1219a4f85bd2SMilanka Ringwald hfp_ag_hf_start_ringing_incoming(hfp_connection); 1220a4f85bd2SMilanka Ringwald 1221f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1222aa4dd815SMatthias Ringwald } 12233561686eSMatthias Ringwald 12243561686eSMatthias Ringwald // start local ringing - started after connection state has been updated 12253561686eSMatthias Ringwald hfp_ag_start_ringing(); 1226aa4dd815SMatthias Ringwald } 1227aa4dd815SMatthias Ringwald 1228fe899794SMatthias Ringwald static void hfp_ag_trigger_outgoing_call(void){ 1229fe899794SMatthias Ringwald btstack_linked_list_iterator_t it; 1230fe899794SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1231fe899794SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1232fe899794SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1233fe899794SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1234fe899794SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 1235fe899794SMatthias Ringwald if (hfp_connection->call_state == HFP_CALL_IDLE){ 1236fe899794SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 1237fe899794SMatthias Ringwald } 1238fe899794SMatthias Ringwald } 1239fe899794SMatthias Ringwald } 12405a7033e2SMatthias Ringwald 12415a7033e2SMatthias Ringwald static void hfp_ag_handle_outgoing_call_accepted(hfp_connection_t * hfp_connection){ 12425a7033e2SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING; 12435a7033e2SMatthias Ringwald 12445a7033e2SMatthias Ringwald // trigger callsetup to be 12455a7033e2SMatthias Ringwald int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT; 12465a7033e2SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_ACCEPTED, 0, 0, NULL); 12475a7033e2SMatthias Ringwald 12485a7033e2SMatthias Ringwald int indicator_index ; 12495a7033e2SMatthias Ringwald 12505a7033e2SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 12515a7033e2SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callsetup"); 12525a7033e2SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 12535a7033e2SMatthias Ringwald 12545a7033e2SMatthias Ringwald // put current call on hold if active 12555a7033e2SMatthias Ringwald if (put_call_on_hold){ 12565a7033e2SMatthias Ringwald log_info("AG putting current call on hold for new outgoing call"); 12575a7033e2SMatthias Ringwald hfp_ag_set_callheld_indicator(); 12585a7033e2SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callheld"); 12595a7033e2SMatthias Ringwald hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]); 12605a7033e2SMatthias Ringwald } 12615a7033e2SMatthias Ringwald 12625a7033e2SMatthias Ringwald // start audio if needed 12635a7033e2SMatthias Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 12645a7033e2SMatthias Ringwald } 1265fe899794SMatthias Ringwald 12664a2e058fSMatthias Ringwald static void hfp_ag_transfer_indicator(const char * name){ 12674a2e058fSMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name(name); 1268aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 1269aa4dd815SMatthias Ringwald 1270665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1271665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1272665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1273a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 127466c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1275a0ffb263SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 1276a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1277f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1278aa4dd815SMatthias Ringwald } 1279aa4dd815SMatthias Ringwald } 1280aa4dd815SMatthias Ringwald 12814a2e058fSMatthias Ringwald static void hfp_ag_transfer_callsetup_state(void){ 12824a2e058fSMatthias Ringwald hfp_ag_transfer_indicator("callsetup"); 12834a2e058fSMatthias Ringwald } 12844a2e058fSMatthias Ringwald 1285aa4dd815SMatthias Ringwald static void hfp_ag_transfer_call_state(void){ 12864a2e058fSMatthias Ringwald hfp_ag_transfer_indicator("call"); 1287aa4dd815SMatthias Ringwald } 1288aa4dd815SMatthias Ringwald 1289aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callheld_state(void){ 12904a2e058fSMatthias Ringwald hfp_ag_transfer_indicator("callheld"); 1291aa4dd815SMatthias Ringwald } 1292aa4dd815SMatthias Ringwald 1293aa4dd815SMatthias Ringwald static void hfp_ag_hf_accept_call(hfp_connection_t * source){ 1294aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 1295aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1296aa4dd815SMatthias Ringwald 1297a4f85bd2SMilanka Ringwald hfp_ag_stop_ringing(); 1298a4f85bd2SMilanka Ringwald 1299665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1300665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1301665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1302a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 130366c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 13049493406bSMilanka Ringwald if ((hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) && 1305505f1c30SMatthias Ringwald (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue; 1306aa4dd815SMatthias Ringwald 1307a0ffb263SMatthias Ringwald if (hfp_connection == source){ 1308aa4dd815SMatthias Ringwald 1309aa4dd815SMatthias Ringwald if (use_in_band_tone()){ 1310a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1311aa4dd815SMatthias Ringwald } else { 1312a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE; 1313d97d752dSMilanka Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 1314aa4dd815SMatthias Ringwald } 1315aa4dd815SMatthias Ringwald 1316a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1317a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1318aa4dd815SMatthias Ringwald 1319aa4dd815SMatthias Ringwald } else { 1320a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 1321aa4dd815SMatthias Ringwald } 1322f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1323aa4dd815SMatthias Ringwald } 1324aa4dd815SMatthias Ringwald } 1325aa4dd815SMatthias Ringwald 1326aa4dd815SMatthias Ringwald static void hfp_ag_ag_accept_call(void){ 1327aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 1328aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1329aa4dd815SMatthias Ringwald 1330a4f85bd2SMilanka Ringwald hfp_ag_stop_ringing(); 1331a4f85bd2SMilanka Ringwald 1332665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1333665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1334665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1335a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 133666c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 13379493406bSMilanka Ringwald if (hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) continue; 1338aa4dd815SMatthias Ringwald 1339a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION; 1340aa4dd815SMatthias Ringwald 1341a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1342a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1343aa4dd815SMatthias Ringwald 1344f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1345aa4dd815SMatthias Ringwald break; // only single 1346aa4dd815SMatthias Ringwald } 1347aa4dd815SMatthias Ringwald } 1348aa4dd815SMatthias Ringwald 1349aa4dd815SMatthias Ringwald static void hfp_ag_trigger_reject_call(void){ 1350aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1351a4f85bd2SMilanka Ringwald 1352a4f85bd2SMilanka Ringwald hfp_ag_stop_ringing(); 1353a4f85bd2SMilanka Ringwald 1354665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1355665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1356665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1357665d90f2SMatthias Ringwald hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 135866c5995fSMatthias Ringwald if (connection->local_role != HFP_ROLE_AG) continue; 13599493406bSMilanka Ringwald if ((connection->call_state != HFP_CALL_INCOMING_RINGING) && 1360505f1c30SMatthias Ringwald (connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue; 1361095a9f2bSMilanka Ringwald 1362aa4dd815SMatthias Ringwald connection->call_state = HFP_CALL_IDLE; 1363095a9f2bSMilanka Ringwald connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1364095a9f2bSMilanka Ringwald 1365095a9f2bSMilanka Ringwald if (connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 1366095a9f2bSMilanka Ringwald connection->release_audio_connection = 1; 1367095a9f2bSMilanka Ringwald } 1368095a9f2bSMilanka Ringwald hfp_emit_simple_event(connection, HFP_SUBEVENT_CALL_TERMINATED); 1369f0aeb307SMatthias Ringwald hfp_ag_run_for_context(connection); 1370095a9f2bSMilanka Ringwald rfcomm_request_can_send_now_event(connection->rfcomm_cid); 1371aa4dd815SMatthias Ringwald } 1372aa4dd815SMatthias Ringwald } 1373aa4dd815SMatthias Ringwald 1374aa4dd815SMatthias Ringwald static void hfp_ag_trigger_terminate_call(void){ 1375aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 1376aa4dd815SMatthias Ringwald 1377a4f85bd2SMilanka Ringwald // no ringing during call 1378a4f85bd2SMilanka Ringwald 1379665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1380665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1381665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1382a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 138366c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1384a0ffb263SMatthias Ringwald if (hfp_connection->call_state == HFP_CALL_IDLE) continue; 1385fb75b483SMilanka Ringwald 1386a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 1387a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1388095a9f2bSMilanka Ringwald 13896d78145cSMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 1390a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 1; 13916d78145cSMatthias Ringwald } 1392095a9f2bSMilanka Ringwald 1393fb75b483SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED); 1394f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1395a13ff7d1SMilanka Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 1396aa4dd815SMatthias Ringwald } 1397aa4dd815SMatthias Ringwald } 1398aa4dd815SMatthias Ringwald 13990cb5b971SMatthias Ringwald static void hfp_ag_set_call_indicator(void){ 1400aa4dd815SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call"); 1401aa4dd815SMatthias Ringwald if (!indicator){ 1402aa4dd815SMatthias Ringwald log_error("hfp_ag_set_call_state: call indicator is missing"); 140345718b6fSMatthias Ringwald return; 1404aa4dd815SMatthias Ringwald }; 1405d210d9c4SMatthias Ringwald indicator->status = hfp_gsm_call_status(); 1406aa4dd815SMatthias Ringwald } 1407aa4dd815SMatthias Ringwald 1408aa4dd815SMatthias Ringwald static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){ 1409665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1410665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1411665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1412a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 141366c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1414a0ffb263SMatthias Ringwald if (hfp_connection->call_state == call_state) return hfp_connection; 1415aa4dd815SMatthias Ringwald } 1416aa4dd815SMatthias Ringwald return NULL; 1417aa4dd815SMatthias Ringwald } 1418aa4dd815SMatthias Ringwald 1419a5bdcda8SMatthias Ringwald static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){ 1420665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1421665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1422665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1423a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 142466c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1425a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = state + 1; 1426ce263fc8SMatthias Ringwald } 1427ce263fc8SMatthias Ringwald } 1428ce263fc8SMatthias Ringwald 1429a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 1430aa4dd815SMatthias Ringwald int indicator_index; 1431a0ffb263SMatthias Ringwald switch (hfp_connection->call_state){ 1432aa4dd815SMatthias Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING: 1433a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 1434a4f85bd2SMilanka Ringwald 1435a0ffb263SMatthias Ringwald // we got event: audio hfp_connection established 14369493406bSMilanka Ringwald hfp_connection->call_state = HFP_CALL_INCOMING_RINGING; 1437aa4dd815SMatthias Ringwald break; 1438aa4dd815SMatthias Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE: 1439a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 1440a0ffb263SMatthias Ringwald // we got event: audio hfp_connection established 1441a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1442aa4dd815SMatthias Ringwald break; 1443aa4dd815SMatthias Ringwald case HFP_CALL_W2_SEND_CALL_WAITING: 1444a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_CHLD; 1445a0ffb263SMatthias Ringwald hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid); 1446aa4dd815SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callsetup"); 1447a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1448aa4dd815SMatthias Ringwald break; 1449aa4dd815SMatthias Ringwald default: 1450aa4dd815SMatthias Ringwald break; 1451aa4dd815SMatthias Ringwald } 1452aa4dd815SMatthias Ringwald return 0; 1453aa4dd815SMatthias Ringwald } 145439426febSMatthias Ringwald 145539426febSMatthias Ringwald // functions extracted from hfp_ag_call_sm below 14569f9621cfSMatthias Ringwald static void hfp_ag_handle_reject_outgoing_call(void){ 145739426febSMatthias Ringwald hfp_connection_t * hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 145839426febSMatthias Ringwald if (!hfp_connection){ 145939426febSMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state"); 146039426febSMatthias Ringwald return; 146139426febSMatthias Ringwald } 146239426febSMatthias Ringwald 1463f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_REJECTED, 0, 0, NULL); 146439426febSMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 146539426febSMatthias Ringwald hfp_connection->send_error = 1; 146639426febSMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 146739426febSMatthias Ringwald } 146839426febSMatthias Ringwald 1469a0ffb263SMatthias Ringwald // hfp_connection is used to identify originating HF 1470a0ffb263SMatthias Ringwald static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){ 1471d210d9c4SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1472d210d9c4SMatthias Ringwald int callheld_indicator_index = get_ag_indicator_index_for_name("callheld"); 1473d210d9c4SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 147474386ee0SMatthias Ringwald 1475aa4dd815SMatthias Ringwald switch (event){ 1476aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL: 1477d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1478aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1479d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 148012cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1481f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL, 0, 0, NULL); 1482d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 148331f55b91SMilanka Ringwald hfp_ag_trigger_incoming_call_idle(); 148460ebb071SMilanka Ringwald log_info("AG rings"); 1485aa4dd815SMatthias Ringwald break; 1486aa4dd815SMatthias Ringwald default: 14873deb3ec6SMatthias Ringwald break; 14883deb3ec6SMatthias Ringwald } 14893deb3ec6SMatthias Ringwald break; 1490aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1491d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 149212cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1493f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL, 0, 0, NULL); 1494d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 149531f55b91SMilanka Ringwald hfp_ag_trigger_incoming_call_during_active_one(); 149660ebb071SMilanka Ringwald log_info("AG call waiting"); 1497aa4dd815SMatthias Ringwald break; 1498aa4dd815SMatthias Ringwald default: 14993deb3ec6SMatthias Ringwald break; 15003deb3ec6SMatthias Ringwald } 15013deb3ec6SMatthias Ringwald break; 15027bbeb3adSMilanka Ringwald default: 15037bbeb3adSMilanka Ringwald break; 1504aa4dd815SMatthias Ringwald } 1505aa4dd815SMatthias Ringwald break; 1506aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG: 1507d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1508aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1509d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 151012cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1511f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, 0, 0, NULL); 1512d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1513d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1514aa4dd815SMatthias Ringwald hfp_ag_ag_accept_call(); 151560ebb071SMilanka Ringwald log_info("AG answers call, accept call by GSM"); 1516aa4dd815SMatthias Ringwald break; 1517aa4dd815SMatthias Ringwald default: 15183deb3ec6SMatthias Ringwald break; 15193deb3ec6SMatthias Ringwald } 1520aa4dd815SMatthias Ringwald break; 1521aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1522d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1523aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 152460ebb071SMilanka Ringwald log_info("AG: current call is placed on hold, incoming call gets active"); 1525f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, 0, 0, NULL); 1526d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1527d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1528ce263fc8SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1529ce263fc8SMatthias Ringwald hfp_ag_transfer_callheld_state(); 1530aa4dd815SMatthias Ringwald break; 1531aa4dd815SMatthias Ringwald default: 1532aa4dd815SMatthias Ringwald break; 1533aa4dd815SMatthias Ringwald } 1534aa4dd815SMatthias Ringwald break; 15357bbeb3adSMilanka Ringwald default: 15367bbeb3adSMilanka Ringwald break; 1537aa4dd815SMatthias Ringwald } 1538aa4dd815SMatthias Ringwald break; 1539ce263fc8SMatthias Ringwald 1540ce263fc8SMatthias Ringwald case HFP_AG_HELD_CALL_JOINED_BY_AG: 1541d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1542ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1543d0c20769SMatthias Ringwald switch (hfp_gsm_callheld_status()){ 1544ce263fc8SMatthias Ringwald case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED: 154560ebb071SMilanka Ringwald log_info("AG: joining held call with active call"); 1546f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_HELD_CALL_JOINED_BY_AG, 0, 0, NULL); 1547d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1548ce263fc8SMatthias Ringwald hfp_ag_transfer_callheld_state(); 1549ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL); 1550ce263fc8SMatthias Ringwald break; 1551ce263fc8SMatthias Ringwald default: 1552ce263fc8SMatthias Ringwald break; 1553ce263fc8SMatthias Ringwald } 1554ce263fc8SMatthias Ringwald break; 1555ce263fc8SMatthias Ringwald default: 1556ce263fc8SMatthias Ringwald break; 1557ce263fc8SMatthias Ringwald } 1558ce263fc8SMatthias Ringwald break; 1559ce263fc8SMatthias Ringwald 1560aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF: 1561d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1562aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1563d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 156412cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1565f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, 0, 0, NULL); 1566d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1567d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1568a0ffb263SMatthias Ringwald hfp_ag_hf_accept_call(hfp_connection); 15696323f788SMatthias Ringwald hfp_connection->ok_pending = 1; 157060ebb071SMilanka Ringwald log_info("HF answers call, accept call by GSM"); 1571ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED); 1572aa4dd815SMatthias Ringwald break; 1573aa4dd815SMatthias Ringwald default: 1574aa4dd815SMatthias Ringwald break; 1575aa4dd815SMatthias Ringwald } 15763deb3ec6SMatthias Ringwald break; 15773deb3ec6SMatthias Ringwald default: 15783deb3ec6SMatthias Ringwald break; 15793deb3ec6SMatthias Ringwald } 15803deb3ec6SMatthias Ringwald break; 15813deb3ec6SMatthias Ringwald 1582ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG: 1583d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1584ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1585d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 158612cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1587f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, 0, 0, NULL); 1588ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_active = 1; 1589ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1590a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1591ac6f828eSMilanka Ringwald // as with regular call 1592d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1593d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1594ce263fc8SMatthias Ringwald hfp_ag_ag_accept_call(); 159560ebb071SMilanka Ringwald log_info("AG response and hold - hold by AG"); 1596ce263fc8SMatthias Ringwald break; 1597ce263fc8SMatthias Ringwald default: 1598ce263fc8SMatthias Ringwald break; 1599ce263fc8SMatthias Ringwald } 1600ce263fc8SMatthias Ringwald break; 1601ce263fc8SMatthias Ringwald default: 1602ce263fc8SMatthias Ringwald break; 1603ce263fc8SMatthias Ringwald } 1604ce263fc8SMatthias Ringwald break; 1605ce263fc8SMatthias Ringwald 1606ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF: 1607d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1608ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1609d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 161012cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1611f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, 0, 0, NULL); 1612ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_active = 1; 1613ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1614a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1615ac6f828eSMilanka Ringwald // as with regular call 1616d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1617d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1618a0ffb263SMatthias Ringwald hfp_ag_hf_accept_call(hfp_connection); 161960ebb071SMilanka Ringwald log_info("AG response and hold - hold by HF"); 1620ce263fc8SMatthias Ringwald break; 1621ce263fc8SMatthias Ringwald default: 1622ce263fc8SMatthias Ringwald break; 1623ce263fc8SMatthias Ringwald } 1624ce263fc8SMatthias Ringwald break; 1625ce263fc8SMatthias Ringwald default: 1626ce263fc8SMatthias Ringwald break; 1627ce263fc8SMatthias Ringwald } 1628ce263fc8SMatthias Ringwald break; 1629ce263fc8SMatthias Ringwald 1630ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG: 1631ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF: 1632ce263fc8SMatthias Ringwald if (!hfp_ag_response_and_hold_active) break; 1633ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1634f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, 0, 0, NULL); 1635a5bdcda8SMatthias Ringwald hfp_ag_response_and_hold_active = 0; 1636ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED; 1637a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 163860ebb071SMilanka Ringwald log_info("Held Call accepted and active"); 1639ce263fc8SMatthias Ringwald break; 1640ce263fc8SMatthias Ringwald 1641ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG: 1642ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF: 1643ce263fc8SMatthias Ringwald if (!hfp_ag_response_and_hold_active) break; 1644ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1645f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, 0, 0, NULL); 1646a5bdcda8SMatthias Ringwald hfp_ag_response_and_hold_active = 0; 1647ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1648a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1649ce263fc8SMatthias Ringwald // from terminate by ag 1650d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1651ce263fc8SMatthias Ringwald hfp_ag_trigger_terminate_call(); 1652ce263fc8SMatthias Ringwald break; 1653ce263fc8SMatthias Ringwald 1654aa4dd815SMatthias Ringwald case HFP_AG_TERMINATE_CALL_BY_HF: 1655d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1656aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1657d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 165812cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1659f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL); 1660d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1661aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1662aa4dd815SMatthias Ringwald hfp_ag_trigger_reject_call(); 166360ebb071SMilanka Ringwald log_info("HF Rejected Incoming call, AG terminate call"); 1664aa4dd815SMatthias Ringwald break; 1665aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 166612cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1667f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL); 1668d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1669aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 167060ebb071SMilanka Ringwald log_info("AG terminate outgoing call process"); 1671202c8a4cSMatthias Ringwald break; 1672aa4dd815SMatthias Ringwald default: 1673aa4dd815SMatthias Ringwald break; 1674aa4dd815SMatthias Ringwald } 1675aa4dd815SMatthias Ringwald break; 167612cbbeeeSMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1677f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL); 16781ec112deSMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1679d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 16801ec112deSMatthias Ringwald hfp_ag_trigger_terminate_call(); 168160ebb071SMilanka Ringwald log_info("AG terminate call"); 1682aa4dd815SMatthias Ringwald break; 16837bbeb3adSMilanka Ringwald default: 16847bbeb3adSMilanka Ringwald break; 1685aa4dd815SMatthias Ringwald } 1686aa4dd815SMatthias Ringwald break; 16873deb3ec6SMatthias Ringwald 1688aa4dd815SMatthias Ringwald case HFP_AG_TERMINATE_CALL_BY_AG: 1689d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1690aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1691d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 169212cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1693f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_AG, 0, 0, NULL); 1694d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1695aa4dd815SMatthias Ringwald hfp_ag_trigger_reject_call(); 169660ebb071SMilanka Ringwald log_info("AG Rejected Incoming call, AG terminate call"); 1697aa4dd815SMatthias Ringwald break; 1698aa4dd815SMatthias Ringwald default: 1699aa4dd815SMatthias Ringwald break; 17003deb3ec6SMatthias Ringwald } 1701202c8a4cSMatthias Ringwald break; 170212cbbeeeSMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1703f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_AG, 0, 0, NULL); 1704d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1705d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1706aa4dd815SMatthias Ringwald hfp_ag_trigger_terminate_call(); 170760ebb071SMilanka Ringwald log_info("AG terminate call"); 1708aa4dd815SMatthias Ringwald break; 1709aa4dd815SMatthias Ringwald default: 17103deb3ec6SMatthias Ringwald break; 17113deb3ec6SMatthias Ringwald } 17123deb3ec6SMatthias Ringwald break; 1713aa4dd815SMatthias Ringwald case HFP_AG_CALL_DROPPED: 1714d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1715aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1716d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1717aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1718aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1719fb75b483SMilanka Ringwald hfp_ag_stop_ringing(); 1720fb75b483SMilanka Ringwald break; 1721fb75b483SMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 1722fb75b483SMilanka Ringwald log_info("Outgoing call interrupted\n"); 1723aa4dd815SMatthias Ringwald break; 1724aa4dd815SMatthias Ringwald default: 17253deb3ec6SMatthias Ringwald break; 17263deb3ec6SMatthias Ringwald } 1727f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL); 1728d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1729aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 173010c236bfSMatthias Ringwald hfp_ag_trigger_terminate_call(); 1731aa4dd815SMatthias Ringwald break; 1732aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1733ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_active) { 1734f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL); 1735ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1736a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1737ce263fc8SMatthias Ringwald } 1738f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL); 1739d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1740d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1741aa4dd815SMatthias Ringwald hfp_ag_trigger_terminate_call(); 174260ebb071SMilanka Ringwald log_info("AG notify call dropped"); 1743aa4dd815SMatthias Ringwald break; 1744aa4dd815SMatthias Ringwald default: 1745aa4dd815SMatthias Ringwald break; 1746aa4dd815SMatthias Ringwald } 1747aa4dd815SMatthias Ringwald break; 1748aa4dd815SMatthias Ringwald 17499ff73f41SMatthias Ringwald case HFP_AG_OUTGOING_CALL_INITIATED_BY_HF: 1750d210d9c4SMatthias Ringwald // directly reject call if number of free slots is exceeded 1751d210d9c4SMatthias Ringwald if (!hfp_gsm_call_possible()){ 1752a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1753f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1754d210d9c4SMatthias Ringwald break; 1755d210d9c4SMatthias Ringwald } 1756fe899794SMatthias Ringwald 1757fe899794SMatthias Ringwald // note: number not used currently 17589ff73f41SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_INITIATED_BY_HF, 0, 0, (const char *) &hfp_connection->line_buffer[3]); 17599cae807eSMatthias Ringwald 1760a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 176174386ee0SMatthias Ringwald 1762ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]); 1763aa4dd815SMatthias Ringwald break; 1764aa4dd815SMatthias Ringwald 1765fe899794SMatthias Ringwald case HFP_AG_OUTGOING_CALL_INITIATED_BY_AG: 1766fe899794SMatthias Ringwald // directly reject call if number of free slots is exceeded 1767fe899794SMatthias Ringwald if (!hfp_gsm_call_possible()) { 1768fe899794SMatthias Ringwald // TODO: no error for user 1769fe899794SMatthias Ringwald break; 1770fe899794SMatthias Ringwald } 1771fe899794SMatthias Ringwald switch (hfp_gsm_call_status()) { 1772fe899794SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1773fe899794SMatthias Ringwald switch (hfp_gsm_callsetup_status()) { 1774fe899794SMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1775fe899794SMatthias Ringwald // note: number not used currently 1776fe899794SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_INITIATED_BY_AG, 0, 0, "1234567"); 1777fe899794SMatthias Ringwald // init call state for all connections 1778fe899794SMatthias Ringwald hfp_ag_trigger_outgoing_call(); 1779fe899794SMatthias Ringwald // get first one and accept call 1780fe899794SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 1781fe899794SMatthias Ringwald if (hfp_connection) { 1782fe899794SMatthias Ringwald hfp_ag_handle_outgoing_call_accepted(hfp_connection); 1783fe899794SMatthias Ringwald } 1784fe899794SMatthias Ringwald break; 1785fe899794SMatthias Ringwald default: 1786fe899794SMatthias Ringwald // TODO: no error for user 1787fe899794SMatthias Ringwald break; 1788fe899794SMatthias Ringwald } 1789fe899794SMatthias Ringwald break; 1790fe899794SMatthias Ringwald default: 1791fe899794SMatthias Ringwald // TODO: no error for user 1792fe899794SMatthias Ringwald break; 1793fe899794SMatthias Ringwald } 1794fe899794SMatthias Ringwald break; 17959cae807eSMatthias Ringwald case HFP_AG_OUTGOING_REDIAL_INITIATED:{ 1796d210d9c4SMatthias Ringwald // directly reject call if number of free slots is exceeded 1797d210d9c4SMatthias Ringwald if (!hfp_gsm_call_possible()){ 1798a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1799f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1800d210d9c4SMatthias Ringwald break; 1801d210d9c4SMatthias Ringwald } 1802d210d9c4SMatthias Ringwald 1803f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_REDIAL_INITIATED, 0, 0, NULL); 1804a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 180574386ee0SMatthias Ringwald 180660ebb071SMilanka Ringwald log_info("Redial last number"); 18079cae807eSMatthias Ringwald char * last_dialed_number = hfp_gsm_last_dialed_number(); 1808aa4dd815SMatthias Ringwald 18099cae807eSMatthias Ringwald if (strlen(last_dialed_number) > 0){ 181060ebb071SMilanka Ringwald log_info("Last number exists: accept call"); 1811ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number); 18129cae807eSMatthias Ringwald } else { 181360ebb071SMilanka Ringwald log_info("log_infoLast number missing: reject call"); 18149f9621cfSMatthias Ringwald hfp_ag_handle_reject_outgoing_call(); 18159cae807eSMatthias Ringwald } 18169cae807eSMatthias Ringwald break; 18179cae807eSMatthias Ringwald } 1818aa4dd815SMatthias Ringwald case HFP_AG_OUTGOING_CALL_REJECTED: 18199f9621cfSMatthias Ringwald hfp_ag_handle_reject_outgoing_call(); 1820aa4dd815SMatthias Ringwald break; 1821aa4dd815SMatthias Ringwald 1822d210d9c4SMatthias Ringwald case HFP_AG_OUTGOING_CALL_ACCEPTED:{ 1823a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 1824a0ffb263SMatthias Ringwald if (!hfp_connection){ 1825a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state"); 1826aa4dd815SMatthias Ringwald break; 1827aa4dd815SMatthias Ringwald } 1828aa4dd815SMatthias Ringwald 1829a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 18305a7033e2SMatthias Ringwald hfp_ag_handle_outgoing_call_accepted(hfp_connection); 1831aa4dd815SMatthias Ringwald break; 1832d210d9c4SMatthias Ringwald } 1833aa4dd815SMatthias Ringwald case HFP_AG_OUTGOING_CALL_RINGING: 1834a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1835a0ffb263SMatthias Ringwald if (!hfp_connection){ 1836a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state"); 1837aa4dd815SMatthias Ringwald break; 1838aa4dd815SMatthias Ringwald } 1839d210d9c4SMatthias Ringwald 1840f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_RINGING, 0, 0, NULL); 1841a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING; 1842d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1843aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1844fcd59412SMilanka Ringwald hfp_ag_hf_start_ringing_outgoing(hfp_connection); 1845aa4dd815SMatthias Ringwald break; 1846aa4dd815SMatthias Ringwald 1847d210d9c4SMatthias Ringwald case HFP_AG_OUTGOING_CALL_ESTABLISHED:{ 1848aa4dd815SMatthias Ringwald // get outgoing call 1849a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING); 1850a0ffb263SMatthias Ringwald if (!hfp_connection){ 1851a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1852aa4dd815SMatthias Ringwald } 1853a0ffb263SMatthias Ringwald if (!hfp_connection){ 1854a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection"); 1855aa4dd815SMatthias Ringwald break; 1856aa4dd815SMatthias Ringwald } 1857d210d9c4SMatthias Ringwald 1858addc49c4SMilanka 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; 1859f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_ESTABLISHED, 0, 0, NULL); 1860a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1861d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1862d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1863aa4dd815SMatthias Ringwald hfp_ag_transfer_call_state(); 1864aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1865addc49c4SMilanka Ringwald if (callheld_status_call_on_hold_and_no_active_calls){ 1866d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1867aa4dd815SMatthias Ringwald hfp_ag_transfer_callheld_state(); 18683deb3ec6SMatthias Ringwald } 1869addc49c4SMilanka Ringwald hfp_ag_hf_stop_ringing(hfp_connection); 1870674ebed5SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED); 18713deb3ec6SMatthias Ringwald break; 1872d210d9c4SMatthias Ringwald } 1873d210d9c4SMatthias Ringwald 187412cbbeeeSMatthias Ringwald case HFP_AG_CALL_HOLD_USER_BUSY: 1875f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_USER_BUSY, 0, 0, NULL); 1876d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1877a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1878a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 187960ebb071SMilanka Ringwald log_info("AG: Call Waiting, User Busy"); 1880d210d9c4SMatthias Ringwald break; 1881d210d9c4SMatthias Ringwald 1882d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{ 1883d0c20769SMatthias Ringwald int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1884d0c20769SMatthias Ringwald int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD; 1885d210d9c4SMatthias Ringwald 1886d210d9c4SMatthias Ringwald // Releases all active calls (if any exist) and accepts the other (held or waiting) call. 1887d0c20769SMatthias Ringwald if (call_held || call_setup_in_progress){ 1888f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index, 1889f8737b81SMatthias Ringwald 0, NULL); 1890d0c20769SMatthias Ringwald 1891d0c20769SMatthias Ringwald } 1892d0c20769SMatthias Ringwald 1893d210d9c4SMatthias Ringwald if (call_setup_in_progress){ 189460ebb071SMilanka Ringwald log_info("AG: Call Dropped, Accept new call"); 1895d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1896a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1897d210d9c4SMatthias Ringwald } else { 189860ebb071SMilanka Ringwald log_info("AG: Call Dropped, Resume held call"); 1899d210d9c4SMatthias Ringwald } 1900d210d9c4SMatthias Ringwald if (call_held){ 1901d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1902a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1903d210d9c4SMatthias Ringwald } 1904d0c20769SMatthias Ringwald 1905a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1906d210d9c4SMatthias Ringwald break; 1907d210d9c4SMatthias Ringwald } 1908d0c20769SMatthias Ringwald 1909d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{ 1910d210d9c4SMatthias Ringwald // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call. 1911d210d9c4SMatthias Ringwald // only update if callsetup changed 1912d0c20769SMatthias Ringwald int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1913f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index, 0, 1914f8737b81SMatthias Ringwald NULL); 1915d0c20769SMatthias Ringwald 1916d210d9c4SMatthias Ringwald if (call_setup_in_progress){ 191760ebb071SMilanka Ringwald log_info("AG: Call on Hold, Accept new call"); 1918d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1919a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1920d210d9c4SMatthias Ringwald } else { 192160ebb071SMilanka Ringwald log_info("AG: Swap calls"); 1922d210d9c4SMatthias Ringwald } 1923d0c20769SMatthias Ringwald 1924d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1925d0c20769SMatthias Ringwald // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED); 1926a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1927a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1928d210d9c4SMatthias Ringwald break; 1929d210d9c4SMatthias Ringwald } 1930d0c20769SMatthias Ringwald 1931d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_ADD_HELD_CALL: 1932d210d9c4SMatthias Ringwald // Adds a held call to the conversation. 1933d0c20769SMatthias Ringwald if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){ 193460ebb071SMilanka Ringwald log_info("AG: Join 3-way-call"); 1935f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_ADD_HELD_CALL, 0, 0, NULL); 1936d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1937a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1938ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL); 1939d210d9c4SMatthias Ringwald } 1940a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1941d210d9c4SMatthias Ringwald break; 1942d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS: 1943d210d9c4SMatthias Ringwald // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer) 1944f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, 0, 0, NULL); 194560ebb071SMilanka Ringwald log_info("AG: Transfer call -> Connect two calls and disconnect"); 1946d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1947d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1948a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1949a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1950a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 1951d210d9c4SMatthias Ringwald break; 1952ce263fc8SMatthias Ringwald 19533deb3ec6SMatthias Ringwald default: 19543deb3ec6SMatthias Ringwald break; 19553deb3ec6SMatthias Ringwald } 1956d210d9c4SMatthias Ringwald 1957d0c20769SMatthias Ringwald 19583deb3ec6SMatthias Ringwald } 19593deb3ec6SMatthias Ringwald 19609cae807eSMatthias Ringwald 1961a0ffb263SMatthias Ringwald static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){ 19629cae807eSMatthias Ringwald hfp_gsm_call_t * active_call = hfp_gsm_call(call_index); 19639cae807eSMatthias Ringwald if (!active_call) return; 19649cae807eSMatthias Ringwald 19659cae807eSMatthias Ringwald int idx = active_call->index; 19669cae807eSMatthias Ringwald hfp_enhanced_call_dir_t dir = active_call->direction; 19679cae807eSMatthias Ringwald hfp_enhanced_call_status_t status = active_call->enhanced_status; 19689cae807eSMatthias Ringwald hfp_enhanced_call_mode_t mode = active_call->mode; 19699cae807eSMatthias Ringwald hfp_enhanced_call_mpty_t mpty = active_call->mpty; 19709cae807eSMatthias Ringwald uint8_t type = active_call->clip_type; 19719cae807eSMatthias Ringwald char * number = active_call->clip_number; 19729cae807eSMatthias Ringwald 19739cae807eSMatthias Ringwald char buffer[100]; 19749cae807eSMatthias Ringwald // TODO: check length of a buffer, to fit the MTU 19759cae807eSMatthias Ringwald int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty); 19769cae807eSMatthias Ringwald if (number){ 19771cc1d9e9SMilanka Ringwald offset += snprintf(buffer+offset, sizeof(buffer)-offset-3, ", \"%s\",%u", number, type); 19789cae807eSMatthias Ringwald } 19799cae807eSMatthias Ringwald snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 198060ebb071SMilanka 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, 19819cae807eSMatthias Ringwald mode, mpty, type, number); 1982a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 19839cae807eSMatthias Ringwald } 19849cae807eSMatthias Ringwald 1985febc14f5SMatthias Ringwald // sends pending command, returns if command was sent 1986febc14f5SMatthias Ringwald static int hfp_ag_send_commands(hfp_connection_t *hfp_connection){ 1987a0ffb263SMatthias Ringwald if (hfp_connection->send_status_of_current_calls){ 1988a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 1989a0ffb263SMatthias Ringwald if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){ 1990a0ffb263SMatthias Ringwald hfp_connection->next_call_index++; 1991a0ffb263SMatthias Ringwald hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index); 1992febc14f5SMatthias Ringwald return 1; 19939cae807eSMatthias Ringwald } else { 1994febc14f5SMatthias 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 1995a0ffb263SMatthias Ringwald hfp_connection->next_call_index = 0; 1996a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1997a0ffb263SMatthias Ringwald hfp_connection->send_status_of_current_calls = 0; 19989cae807eSMatthias Ringwald } 1999ce263fc8SMatthias Ringwald } 2000ce263fc8SMatthias Ringwald 2001a0ffb263SMatthias Ringwald if (hfp_connection->ag_notify_incoming_call_waiting){ 2002a0ffb263SMatthias Ringwald hfp_connection->ag_notify_incoming_call_waiting = 0; 2003a0ffb263SMatthias Ringwald hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid); 2004febc14f5SMatthias Ringwald return 1; 2005aa4dd815SMatthias Ringwald } 2006aa4dd815SMatthias Ringwald 2007a0ffb263SMatthias Ringwald if (hfp_connection->command == HFP_CMD_UNKNOWN){ 2008a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 2009a0ffb263SMatthias Ringwald hfp_connection->send_error = 0; 2010a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2011485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 2012febc14f5SMatthias Ringwald return 1; 2013a0ffb263SMatthias Ringwald } 2014a0ffb263SMatthias Ringwald 2015a0ffb263SMatthias Ringwald if (hfp_connection->send_error){ 2016a0ffb263SMatthias Ringwald hfp_connection->send_error = 0; 2017a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2018485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 2019febc14f5SMatthias Ringwald return 1; 2020aa4dd815SMatthias Ringwald } 2021aa4dd815SMatthias Ringwald 2022ce263fc8SMatthias Ringwald // note: before update AG indicators and ok_pending 2023a0ffb263SMatthias Ringwald if (hfp_connection->send_response_and_hold_status){ 2024a0ffb263SMatthias Ringwald int status = hfp_connection->send_response_and_hold_status - 1; 2025a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = 0; 2026485ac19eSMilanka Ringwald hfp_ag_send_set_response_and_hold(hfp_connection->rfcomm_cid, status); 2027febc14f5SMatthias Ringwald return 1; 2028ce263fc8SMatthias Ringwald } 2029ce263fc8SMatthias Ringwald 2030a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending){ 2031a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 2032a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2033485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 2034febc14f5SMatthias Ringwald return 1; 2035ce263fc8SMatthias Ringwald } 2036ce263fc8SMatthias Ringwald 2037aa4dd815SMatthias Ringwald // update AG indicators 2038a0ffb263SMatthias Ringwald if (hfp_connection->ag_indicators_status_update_bitmap){ 2039aa4dd815SMatthias Ringwald int i; 2040a0ffb263SMatthias Ringwald for (i=0;i<hfp_connection->ag_indicators_nr;i++){ 2041a0ffb263SMatthias Ringwald if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){ 2042a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0); 2043a0ffb263SMatthias Ringwald if (!hfp_connection->enable_status_update_for_ag_indicators) { 2044ce263fc8SMatthias Ringwald log_info("+CMER:3,0,0,0 - not sending update for '%s'", hfp_ag_indicators[i].name); 2045ce263fc8SMatthias Ringwald break; 2046ce263fc8SMatthias Ringwald } 2047485ac19eSMilanka Ringwald hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]); 2048febc14f5SMatthias Ringwald return 1; 2049aa4dd815SMatthias Ringwald } 2050aa4dd815SMatthias Ringwald } 2051aa4dd815SMatthias Ringwald } 2052aa4dd815SMatthias Ringwald 2053245852b7SMilanka Ringwald if (hfp_connection->ag_send_no_carrier){ 2054245852b7SMilanka Ringwald hfp_connection->ag_send_no_carrier = false; 2055245852b7SMilanka Ringwald hfp_ag_send_no_carrier(hfp_connection->rfcomm_cid); 2056245852b7SMilanka Ringwald return 1; 2057245852b7SMilanka Ringwald } 2058245852b7SMilanka Ringwald 2059a0ffb263SMatthias Ringwald if (hfp_connection->ag_ring){ 2060a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 0; 2061a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 20620d38e7e1SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING); 2063485ac19eSMilanka Ringwald hfp_ag_send_ring(hfp_connection->rfcomm_cid); 2064febc14f5SMatthias Ringwald return 1; 2065aa4dd815SMatthias Ringwald } 2066aa4dd815SMatthias Ringwald 2067a0ffb263SMatthias Ringwald if (hfp_connection->ag_send_clip){ 2068a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = 0; 2069a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2070a0ffb263SMatthias Ringwald hfp_ag_send_clip(hfp_connection->rfcomm_cid); 2071febc14f5SMatthias Ringwald return 1; 2072aa4dd815SMatthias Ringwald } 2073aa4dd815SMatthias Ringwald 2074a0ffb263SMatthias Ringwald if (hfp_connection->send_phone_number_for_voice_tag){ 2075a0ffb263SMatthias Ringwald hfp_connection->send_phone_number_for_voice_tag = 0; 2076a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2077a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2078a0ffb263SMatthias Ringwald hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid); 2079febc14f5SMatthias Ringwald return 1; 2080aa4dd815SMatthias Ringwald } 2081aa4dd815SMatthias Ringwald 2082a0ffb263SMatthias Ringwald if (hfp_connection->send_subscriber_number){ 2083aeb0f0feSMatthias Ringwald if (hfp_connection->next_subscriber_number_to_send < hfp_ag_subscriber_numbers_count){ 2084aeb0f0feSMatthias Ringwald hfp_phone_number_t phone = hfp_ag_subscriber_numbers[hfp_connection->next_subscriber_number_to_send++]; 2085a0ffb263SMatthias Ringwald hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number); 2086ce263fc8SMatthias Ringwald } else { 2087a0ffb263SMatthias Ringwald hfp_connection->send_subscriber_number = 0; 2088a0ffb263SMatthias Ringwald hfp_connection->next_subscriber_number_to_send = 0; 2089485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 2090ce263fc8SMatthias Ringwald } 2091a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2092febc14f5SMatthias Ringwald return 1; 2093ce263fc8SMatthias Ringwald } 2094ce263fc8SMatthias Ringwald 2095a0ffb263SMatthias Ringwald if (hfp_connection->send_microphone_gain){ 2096a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 0; 2097a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2098485ac19eSMilanka Ringwald hfp_ag_send_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 2099febc14f5SMatthias Ringwald return 1; 2100aa4dd815SMatthias Ringwald } 2101aa4dd815SMatthias Ringwald 2102a0ffb263SMatthias Ringwald if (hfp_connection->send_speaker_gain){ 2103a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 0; 2104a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2105485ac19eSMilanka Ringwald hfp_ag_send_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 2106febc14f5SMatthias Ringwald return 1; 21073deb3ec6SMatthias Ringwald } 21083deb3ec6SMatthias Ringwald 2109a0ffb263SMatthias Ringwald if (hfp_connection->send_ag_status_indicators){ 2110a0ffb263SMatthias Ringwald hfp_connection->send_ag_status_indicators = 0; 2111485ac19eSMilanka Ringwald hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid); 2112febc14f5SMatthias Ringwald return 1; 2113febc14f5SMatthias Ringwald } 2114febc14f5SMatthias Ringwald 2115febc14f5SMatthias Ringwald return 0; 2116febc14f5SMatthias Ringwald } 2117febc14f5SMatthias Ringwald 2118febc14f5SMatthias Ringwald static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection){ 2119febc14f5SMatthias Ringwald 212076cc1527SMatthias Ringwald btstack_assert(hfp_connection != NULL); 212176cc1527SMatthias Ringwald btstack_assert(hfp_connection->local_role == HFP_ROLE_AG); 2122febc14f5SMatthias Ringwald 212376cc1527SMatthias Ringwald // during SDP query, RFCOMM CID is not set 212476cc1527SMatthias Ringwald if (hfp_connection->rfcomm_cid == 0) return; 2125ce263fc8SMatthias Ringwald 21263721a235SMatthias Ringwald // assert command could be sent 21273721a235SMatthias Ringwald if (hci_can_send_command_packet_now() == 0) return; 21283721a235SMatthias Ringwald 2129f9f3ba28SMilanka Ringwald if ((hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) && hfp_connection->release_audio_connection){ 2130f9f3ba28SMilanka Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 2131f9f3ba28SMilanka Ringwald hfp_connection->release_audio_connection = 0; 2132f9f3ba28SMilanka Ringwald gap_disconnect(hfp_connection->sco_handle); 2133f9f3ba28SMilanka Ringwald return; 2134f9f3ba28SMilanka Ringwald } 2135f9f3ba28SMilanka Ringwald 21363721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP 21373721a235SMatthias Ringwald // WBS Disassociate 21383721a235SMatthias Ringwald if (hfp_connection->cc256x_send_wbs_disassociate){ 21393721a235SMatthias Ringwald hfp_connection->cc256x_send_wbs_disassociate = false; 21403721a235SMatthias Ringwald hci_send_cmd(&hci_ti_wbs_disassociate); 21413721a235SMatthias Ringwald return; 21423721a235SMatthias Ringwald } 21433721a235SMatthias Ringwald // Write Codec Config 21443721a235SMatthias Ringwald if (hfp_connection->cc256x_send_write_codec_config){ 21453721a235SMatthias Ringwald hfp_connection->cc256x_send_write_codec_config = false; 21463721a235SMatthias Ringwald hfp_cc256x_write_codec_config(hfp_connection); 21473721a235SMatthias Ringwald return; 21483721a235SMatthias Ringwald } 21493721a235SMatthias Ringwald // WBS Associate 21503721a235SMatthias Ringwald if (hfp_connection->cc256x_send_wbs_associate){ 21513721a235SMatthias Ringwald hfp_connection->cc256x_send_wbs_associate = false; 21523721a235SMatthias Ringwald hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle); 21533721a235SMatthias Ringwald return; 21543721a235SMatthias Ringwald } 21553721a235SMatthias Ringwald #endif 2156689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 2157689d4323SMatthias Ringwald // Enable WBS 2158689d4323SMatthias Ringwald if (hfp_connection->bcm_send_enable_wbs){ 2159689d4323SMatthias Ringwald hfp_connection->bcm_send_enable_wbs = false; 2160689d4323SMatthias Ringwald hci_send_cmd(&hci_bcm_enable_wbs, 1, 2); 2161689d4323SMatthias Ringwald return; 2162689d4323SMatthias Ringwald } 2163689d4323SMatthias Ringwald // Write I2S/PCM params 2164689d4323SMatthias Ringwald if (hfp_connection->bcm_send_write_i2spcm_interface_param){ 2165689d4323SMatthias Ringwald hfp_connection->bcm_send_write_i2spcm_interface_param = false; 2166689d4323SMatthias Ringwald hfp_bcm_write_i2spcm_interface_param(hfp_connection); 2167689d4323SMatthias Ringwald return; 2168689d4323SMatthias Ringwald } 2169689d4323SMatthias Ringwald // Disable WBS 2170689d4323SMatthias Ringwald if (hfp_connection->bcm_send_disable_wbs){ 2171689d4323SMatthias Ringwald hfp_connection->bcm_send_disable_wbs = false; 2172689d4323SMatthias Ringwald hci_send_cmd(&hci_bcm_enable_wbs, 0, 2); 2173689d4323SMatthias Ringwald return; 2174689d4323SMatthias Ringwald } 2175689d4323SMatthias Ringwald #endif 217648e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS) 217748e6eeeeSMatthias Ringwald if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){ 217848e6eeeeSMatthias Ringwald hfp_finalize_connection_context(hfp_connection); 217948e6eeeeSMatthias Ringwald return; 218048e6eeeeSMatthias Ringwald } 218148e6eeeeSMatthias Ringwald #endif 21823721a235SMatthias Ringwald 2183febc14f5SMatthias Ringwald if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) { 2184febc14f5SMatthias Ringwald log_info("hfp_ag_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid); 2185febc14f5SMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 2186febc14f5SMatthias Ringwald return; 2187febc14f5SMatthias Ringwald } 2188febc14f5SMatthias Ringwald 2189febc14f5SMatthias Ringwald int cmd_sent = hfp_ag_send_commands(hfp_connection); 2190febc14f5SMatthias Ringwald 2191febc14f5SMatthias Ringwald if (!cmd_sent){ 2192febc14f5SMatthias Ringwald cmd_sent = hfp_ag_run_for_context_service_level_connection(hfp_connection); 2193febc14f5SMatthias Ringwald } 2194febc14f5SMatthias Ringwald 2195c04cf7ffSMilanka Ringwald if (!cmd_sent){ 2196c04cf7ffSMilanka Ringwald cmd_sent = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection); 21973deb3ec6SMatthias Ringwald } 21983deb3ec6SMatthias Ringwald 2199c04cf7ffSMilanka Ringwald if (!cmd_sent){ 2200c04cf7ffSMilanka Ringwald cmd_sent = call_setup_state_machine(hfp_connection); 2201aa4dd815SMatthias Ringwald } 2202aa4dd815SMatthias Ringwald 2203b956fff3SMatthias Ringwald // trigger codec exchange (must be before hfp_ag_run_for_audio_connection) 2204d0a0eceeSMatthias Ringwald if (!cmd_sent && (hfp_connection->command == HFP_CMD_NONE) && hfp_connection->trigger_codec_exchange){ 2205d0a0eceeSMatthias Ringwald switch (hfp_connection->codecs_state){ 2206d0a0eceeSMatthias Ringwald case HFP_CODECS_IDLE: 2207d0a0eceeSMatthias Ringwald case HFP_CODECS_RECEIVED_LIST: 2208d0a0eceeSMatthias Ringwald case HFP_CODECS_AG_RESEND_COMMON_CODEC: 2209d0a0eceeSMatthias Ringwald case HFP_CODECS_ERROR: 2210d0a0eceeSMatthias Ringwald hfp_connection->trigger_codec_exchange = 0; 2211d0a0eceeSMatthias Ringwald hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC; 22123deb3ec6SMatthias Ringwald break; 22133deb3ec6SMatthias Ringwald default: 22143deb3ec6SMatthias Ringwald break; 22153deb3ec6SMatthias Ringwald } 22163deb3ec6SMatthias Ringwald } 2217c04cf7ffSMilanka Ringwald 2218b956fff3SMatthias Ringwald if (!cmd_sent){ 2219b956fff3SMatthias Ringwald cmd_sent = hfp_ag_run_for_audio_connection(hfp_connection); 2220b956fff3SMatthias Ringwald } 2221b956fff3SMatthias Ringwald 2222c95b5b3cSMilanka Ringwald if (!cmd_sent){ 2223c95b5b3cSMilanka Ringwald cmd_sent = hfp_ag_voice_recognition_state_machine(hfp_connection); 2224c95b5b3cSMilanka Ringwald } 2225b956fff3SMatthias Ringwald 2226d0a0eceeSMatthias Ringwald // disconnect 2227d0a0eceeSMatthias Ringwald if (!cmd_sent && (hfp_connection->command == HFP_CMD_NONE) && (hfp_connection->state == HFP_W2_DISCONNECT_RFCOMM)){ 2228d0a0eceeSMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 2229d0a0eceeSMatthias Ringwald rfcomm_disconnect(hfp_connection->rfcomm_cid); 2230d0a0eceeSMatthias Ringwald } 2231d0a0eceeSMatthias Ringwald 2232c04cf7ffSMilanka Ringwald if (cmd_sent){ 2233a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2234473ac565SMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 2235473ac565SMatthias Ringwald } 22363deb3ec6SMatthias Ringwald } 2237d68dcce1SMatthias Ringwald 2238e0d09929SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){ 2239c1ab6cc1SMatthias Ringwald return (byte == '\n') || (byte == '\r'); 2240e0d09929SMatthias Ringwald } 2241e0d09929SMatthias Ringwald 2242fdf18f86SMilanka Ringwald static void hfp_ag_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){ 22438a46ec40SMatthias Ringwald // assertion: size >= 1 as rfcomm.c does not deliver empty packets 2244fdf18f86SMilanka Ringwald if (size < 1) return; 2245347d46c8SMilanka Ringwald uint8_t status = ERROR_CODE_SUCCESS; 22461e35c04dSMatthias Ringwald 2247186dd3d2SMatthias Ringwald hfp_log_rfcomm_message("HFP_AG_RX", packet, size); 2248e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES 2249e43d1938SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet); 2250e43d1938SMatthias Ringwald #endif 22511e35c04dSMatthias Ringwald 22528a46ec40SMatthias Ringwald // process messages byte-wise 22533deb3ec6SMatthias Ringwald int pos; 22543deb3ec6SMatthias Ringwald for (pos = 0; pos < size ; pos++){ 2255a0ffb263SMatthias Ringwald hfp_parse(hfp_connection, packet[pos], 0); 2256186dd3d2SMatthias Ringwald 2257e0d09929SMatthias Ringwald // parse until end of line 2258e0d09929SMatthias Ringwald if (!hfp_parser_is_end_of_line(packet[pos])) continue; 2259e0d09929SMatthias Ringwald 2260186dd3d2SMatthias Ringwald hfp_generic_status_indicator_t * indicator; 2261a0ffb263SMatthias Ringwald switch(hfp_connection->command){ 226245796ff1SMilanka Ringwald case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION: 2263fd4151d1SMilanka Ringwald // dealt with in hfp_ag_voice_recognition_state_machine 226445796ff1SMilanka Ringwald break; 2265ce263fc8SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_QUERY: 2266ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_active){ 2267a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1; 2268ce263fc8SMatthias Ringwald } 2269a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2270ce263fc8SMatthias Ringwald break; 2271ce263fc8SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_COMMAND: 22720222a807SMatthias Ringwald switch(hfp_connection->ag_response_and_hold_action){ 2273ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD: 2274a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection); 2275ce263fc8SMatthias Ringwald break; 2276ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED: 2277a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection); 2278ce263fc8SMatthias Ringwald break; 2279ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED: 2280a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection); 2281ce263fc8SMatthias Ringwald break; 2282ce263fc8SMatthias Ringwald default: 2283ce263fc8SMatthias Ringwald break; 2284ce263fc8SMatthias Ringwald } 2285a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2286ce263fc8SMatthias Ringwald break; 2287ce263fc8SMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 2288a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2289347d46c8SMilanka Ringwald 2290aeb0f0feSMatthias Ringwald if (hfp_connection->parser_indicator_index < hfp_ag_generic_status_indicators_nr){ 2291aeb0f0feSMatthias Ringwald indicator = &hfp_ag_generic_status_indicators[hfp_connection->parser_indicator_index]; 2292ce263fc8SMatthias Ringwald switch (indicator->uuid){ 2293347d46c8SMilanka Ringwald case HFP_HF_INDICATOR_UUID_ENHANCED_SAFETY: 22940222a807SMatthias Ringwald if (hfp_connection->parser_indicator_value > 1) { 2295a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 2296ce263fc8SMatthias Ringwald break; 2297ce263fc8SMatthias Ringwald } 2298a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2299585db78dSMilanka Ringwald hfp_ag_emit_hf_indicator_value(hfp_connection, indicator->uuid, hfp_connection->parser_indicator_value); 2300347d46c8SMilanka Ringwald break; 2301347d46c8SMilanka Ringwald case HFP_HF_INDICATOR_UUID_BATTERY_LEVEL: 2302347d46c8SMilanka Ringwald if (hfp_connection->parser_indicator_value > 100){ 2303347d46c8SMilanka Ringwald hfp_connection->send_error = 1; 2304347d46c8SMilanka Ringwald break; 2305347d46c8SMilanka Ringwald } 2306347d46c8SMilanka Ringwald hfp_connection->ok_pending = 1; 2307585db78dSMilanka Ringwald hfp_ag_emit_hf_indicator_value(hfp_connection, indicator->uuid, hfp_connection->parser_indicator_value); 2308347d46c8SMilanka Ringwald break; 2309347d46c8SMilanka Ringwald default: 2310585db78dSMilanka Ringwald hfp_connection->send_error = 1; 2311347d46c8SMilanka Ringwald break; 2312347d46c8SMilanka Ringwald } 2313347d46c8SMilanka Ringwald } else { 2314347d46c8SMilanka Ringwald hfp_connection->send_error = 1; 2315347d46c8SMilanka Ringwald } 2316ce263fc8SMatthias Ringwald break; 2317ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 2318ce263fc8SMatthias Ringwald // expected by SLC state machine 2319a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break; 2320a0ffb263SMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0; 2321a0ffb263SMatthias Ringwald hfp_connection->send_ag_status_indicators = 1; 2322ce263fc8SMatthias Ringwald break; 2323ce263fc8SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 2324a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2325a0ffb263SMatthias Ringwald hfp_connection->next_call_index = 0; 2326a0ffb263SMatthias Ringwald hfp_connection->send_status_of_current_calls = 1; 2327ce263fc8SMatthias Ringwald break; 2328ce263fc8SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 2329aeb0f0feSMatthias Ringwald if (hfp_ag_subscriber_numbers_count == 0){ 2330485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 2331ce263fc8SMatthias Ringwald break; 2332ce263fc8SMatthias Ringwald } 2333a0ffb263SMatthias Ringwald hfp_connection->next_subscriber_number_to_send = 0; 2334a0ffb263SMatthias Ringwald hfp_connection->send_subscriber_number = 1; 2335ce263fc8SMatthias Ringwald break; 2336c1797c7dSMatthias Ringwald case HFP_CMD_TRANSMIT_DTMF_CODES: 23370222a807SMatthias Ringwald { 2338a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 23390222a807SMatthias Ringwald char buffer[2]; 23400222a807SMatthias Ringwald buffer[0] = (char) hfp_connection->ag_dtmf_code; 23410222a807SMatthias Ringwald buffer[1] = 0; 23420222a807SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, buffer); 2343c1797c7dSMatthias Ringwald break; 23440222a807SMatthias Ringwald } 2345aa4dd815SMatthias Ringwald case HFP_CMD_HF_REQUEST_PHONE_NUMBER: 2346a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2347ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG); 2348aa4dd815SMatthias Ringwald break; 2349aa4dd815SMatthias Ringwald case HFP_CMD_TURN_OFF_EC_AND_NR: 2350a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2351347d46c8SMilanka Ringwald 2352aeb0f0feSMatthias Ringwald if (get_bit(hfp_ag_supported_features, HFP_AGSF_EC_NR_FUNCTION)){ 2353a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2354aeb0f0feSMatthias Ringwald hfp_ag_supported_features = store_bit(hfp_ag_supported_features, HFP_AGSF_EC_NR_FUNCTION, hfp_connection->ag_echo_and_noise_reduction); 2355347d46c8SMilanka Ringwald status = ERROR_CODE_SUCCESS; 2356aa4dd815SMatthias Ringwald } else { 2357a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 2358347d46c8SMilanka Ringwald status = ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 2359aa4dd815SMatthias Ringwald } 2360347d46c8SMilanka Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE, status); 2361aa4dd815SMatthias Ringwald break; 2362aa4dd815SMatthias Ringwald case HFP_CMD_CALL_ANSWERED: 2363a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 236460ebb071SMilanka Ringwald log_info("HFP: ATA"); 2365a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection); 2366aa4dd815SMatthias Ringwald break; 2367aa4dd815SMatthias Ringwald case HFP_CMD_HANG_UP_CALL: 2368a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2369a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2370a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection); 2371aa4dd815SMatthias Ringwald break; 2372aa4dd815SMatthias Ringwald case HFP_CMD_CALL_HOLD: { 2373a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2374a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2375d0c20769SMatthias Ringwald 23760222a807SMatthias Ringwald switch (hfp_connection->ag_call_hold_action){ 23770222a807SMatthias Ringwald case 0: 2378d0c20769SMatthias Ringwald // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call. 2379a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection); 2380aa4dd815SMatthias Ringwald break; 23810222a807SMatthias Ringwald case 1: 2382d0c20769SMatthias Ringwald // Releases all active calls (if any exist) and accepts the other (held or waiting) call. 2383d0c20769SMatthias Ringwald // Where both a held and a waiting call exist, the above procedures shall apply to the 2384d0c20769SMatthias Ringwald // waiting call (i.e., not to the held call) in conflicting situation. 2385a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection); 2386aa4dd815SMatthias Ringwald break; 23870222a807SMatthias Ringwald case 2: 2388d0c20769SMatthias Ringwald // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call. 2389d0c20769SMatthias Ringwald // Where both a held and a waiting call exist, the above procedures shall apply to the 2390d0c20769SMatthias Ringwald // waiting call (i.e., not to the held call) in conflicting situation. 2391a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection); 2392aa4dd815SMatthias Ringwald break; 23930222a807SMatthias Ringwald case 3: 2394d0c20769SMatthias Ringwald // Adds a held call to the conversation. 2395a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection); 2396aa4dd815SMatthias Ringwald break; 23970222a807SMatthias Ringwald case 4: 2398d0c20769SMatthias Ringwald // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer). 2399a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection); 2400aa4dd815SMatthias Ringwald break; 2401aa4dd815SMatthias Ringwald default: 2402aa4dd815SMatthias Ringwald break; 2403aa4dd815SMatthias Ringwald } 24040222a807SMatthias Ringwald hfp_connection->call_index = 0; 240535e92150SMatthias Ringwald break; 2406aa4dd815SMatthias Ringwald } 2407aa4dd815SMatthias Ringwald case HFP_CMD_CALL_PHONE_NUMBER: 2408a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 24099ff73f41SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED_BY_HF, hfp_connection); 2410aa4dd815SMatthias Ringwald break; 2411aa4dd815SMatthias Ringwald case HFP_CMD_REDIAL_LAST_NUMBER: 2412a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2413a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection); 2414aa4dd815SMatthias Ringwald break; 2415aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_CLIP: 2416a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2417a0ffb263SMatthias Ringwald log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled); 2418a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2419aa4dd815SMatthias Ringwald break; 2420aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION: 2421a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2422a0ffb263SMatthias Ringwald log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled); 2423a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2424aa4dd815SMatthias Ringwald break; 2425aa4dd815SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 2426a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2427a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 242860ebb071SMilanka Ringwald log_info("HF speaker gain = %u", hfp_connection->speaker_gain); 24293db60f78SBjoern Hartmann hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_connection->speaker_gain); 2430aa4dd815SMatthias Ringwald break; 2431aa4dd815SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 2432a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2433a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 243460ebb071SMilanka Ringwald log_info("HF microphone gain = %u", hfp_connection->microphone_gain); 24352abbd98dSMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_connection->microphone_gain); 2436aa4dd815SMatthias Ringwald break; 2437aa4dd815SMatthias Ringwald default: 2438aa4dd815SMatthias Ringwald break; 24393deb3ec6SMatthias Ringwald } 24403deb3ec6SMatthias Ringwald } 24410cef86faSMatthias Ringwald } 24423deb3ec6SMatthias Ringwald 24431c6a0fc0SMatthias Ringwald static void hfp_ag_run(void){ 2444d63c37a1SMatthias Ringwald btstack_linked_list_iterator_t it; 2445d63c37a1SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2446d63c37a1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2447d63c37a1SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 244822387625SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2449f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 24503deb3ec6SMatthias Ringwald } 24513deb3ec6SMatthias Ringwald } 24523deb3ec6SMatthias Ringwald 24531c6a0fc0SMatthias Ringwald static void hfp_ag_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2454fdf18f86SMilanka Ringwald hfp_connection_t * hfp_connection = NULL; 24553deb3ec6SMatthias Ringwald switch (packet_type){ 24563deb3ec6SMatthias Ringwald case RFCOMM_DATA_PACKET: 2457fdf18f86SMilanka Ringwald hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 2458fd075f20SMilanka Ringwald btstack_assert(hfp_connection != NULL); 2459fdf18f86SMilanka Ringwald 2460fdf18f86SMilanka Ringwald hfp_ag_handle_rfcomm_data(hfp_connection, packet, size); 2461fdf18f86SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 2462fdf18f86SMilanka Ringwald return; 24633deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 2464e30a6a47SMatthias Ringwald if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 2465e30a6a47SMatthias Ringwald uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 2466fd075f20SMilanka Ringwald hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid); 2467fd075f20SMilanka Ringwald btstack_assert(hfp_connection != NULL); 2468fd075f20SMilanka Ringwald 2469fd075f20SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 2470e30a6a47SMatthias Ringwald return; 2471e30a6a47SMatthias Ringwald } 247227950165SMatthias Ringwald hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_AG); 2473aa4dd815SMatthias Ringwald break; 24743deb3ec6SMatthias Ringwald default: 24753deb3ec6SMatthias Ringwald break; 24763deb3ec6SMatthias Ringwald } 24773deb3ec6SMatthias Ringwald 24781c6a0fc0SMatthias Ringwald hfp_ag_run(); 24793deb3ec6SMatthias Ringwald } 24803deb3ec6SMatthias Ringwald 24811c6a0fc0SMatthias Ringwald static void hfp_ag_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2482405014fbSMatthias Ringwald hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_AG); 24831c6a0fc0SMatthias Ringwald hfp_ag_run(); 2484405014fbSMatthias Ringwald } 2485405014fbSMatthias Ringwald 24867ca89cabSMatthias Ringwald void hfp_ag_init_codecs(int codecs_nr, const uint8_t * codecs){ 24873deb3ec6SMatthias Ringwald if (codecs_nr > HFP_MAX_NUM_CODECS){ 24883deb3ec6SMatthias Ringwald log_error("hfp_init: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS); 24893deb3ec6SMatthias Ringwald return; 24903deb3ec6SMatthias Ringwald } 24913deb3ec6SMatthias Ringwald int i; 2492aeb0f0feSMatthias Ringwald hfp_ag_codecs_nr = codecs_nr; 24933deb3ec6SMatthias Ringwald for (i=0; i < codecs_nr; i++){ 2494aeb0f0feSMatthias Ringwald hfp_ag_codecs[i] = codecs[i]; 24953deb3ec6SMatthias Ringwald } 2496a0ffb263SMatthias Ringwald } 24973deb3ec6SMatthias Ringwald 2498a0ffb263SMatthias Ringwald void hfp_ag_init_supported_features(uint32_t supported_features){ 2499aeb0f0feSMatthias Ringwald hfp_ag_supported_features = supported_features; 2500a0ffb263SMatthias Ringwald } 25013deb3ec6SMatthias Ringwald 25027ca89cabSMatthias Ringwald void hfp_ag_init_ag_indicators(int ag_indicators_nr, const hfp_ag_indicator_t * ag_indicators){ 2503a0ffb263SMatthias Ringwald hfp_ag_indicators_nr = ag_indicators_nr; 25046535961aSMatthias Ringwald (void)memcpy(hfp_ag_indicators, ag_indicators, 25056535961aSMatthias Ringwald ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 2506a0ffb263SMatthias Ringwald } 25073deb3ec6SMatthias Ringwald 25087ca89cabSMatthias Ringwald void hfp_ag_init_hf_indicators(int hf_indicators_nr, const hfp_generic_status_indicator_t * hf_indicators){ 250925789943SMilanka Ringwald if (hf_indicators_nr > HFP_MAX_NUM_INDICATORS) return; 2510aeb0f0feSMatthias Ringwald hfp_ag_generic_status_indicators_nr = hf_indicators_nr; 2511aeb0f0feSMatthias Ringwald (void)memcpy(hfp_ag_generic_status_indicators, hf_indicators, 25126535961aSMatthias Ringwald hf_indicators_nr * sizeof(hfp_generic_status_indicator_t)); 2513a0ffb263SMatthias Ringwald } 2514a0ffb263SMatthias Ringwald 2515a0ffb263SMatthias Ringwald void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){ 25163deb3ec6SMatthias Ringwald hfp_ag_call_hold_services_nr = call_hold_services_nr; 25176535961aSMatthias Ringwald (void)memcpy(hfp_ag_call_hold_services, call_hold_services, 25186535961aSMatthias Ringwald call_hold_services_nr * sizeof(char *)); 2519a0ffb263SMatthias Ringwald } 2520a0ffb263SMatthias Ringwald 2521a0ffb263SMatthias Ringwald 2522a0ffb263SMatthias Ringwald void hfp_ag_init(uint16_t rfcomm_channel_nr){ 252327950165SMatthias Ringwald 2524520c92d5SMatthias Ringwald hfp_init(); 252520b2edb6SMatthias Ringwald hfp_ag_call_hold_services_nr = 0; 252620b2edb6SMatthias Ringwald hfp_ag_response_and_hold_active = 0; 252720b2edb6SMatthias Ringwald hfp_ag_indicators_nr = 0; 2528aeb0f0feSMatthias Ringwald hfp_ag_codecs_nr = 0; 2529aeb0f0feSMatthias Ringwald hfp_ag_supported_features = HFP_DEFAULT_AG_SUPPORTED_FEATURES; 2530aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers = NULL; 2531aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers_count = 0; 2532d63c37a1SMatthias Ringwald 25331c6a0fc0SMatthias Ringwald hfp_ag_hci_event_callback_registration.callback = &hfp_ag_hci_event_packet_handler; 25341c6a0fc0SMatthias Ringwald hci_add_event_handler(&hfp_ag_hci_event_callback_registration); 253527950165SMatthias Ringwald 25361c6a0fc0SMatthias Ringwald rfcomm_register_service(&hfp_ag_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 253727950165SMatthias Ringwald 253827950165SMatthias Ringwald // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us 25391c6a0fc0SMatthias Ringwald hfp_set_ag_rfcomm_packet_handler(&hfp_ag_rfcomm_packet_handler); 2540aa4dd815SMatthias Ringwald 2541d210d9c4SMatthias Ringwald hfp_gsm_init(); 25423deb3ec6SMatthias Ringwald } 25433deb3ec6SMatthias Ringwald 254420b2edb6SMatthias Ringwald void hfp_ag_deinit(void){ 254520b2edb6SMatthias Ringwald hfp_deinit(); 254620b2edb6SMatthias Ringwald hfp_gsm_deinit(); 2547aeb0f0feSMatthias Ringwald 2548aeb0f0feSMatthias Ringwald hfp_ag_callback = NULL; 2549aeb0f0feSMatthias Ringwald hfp_ag_supported_features = 0; 2550aeb0f0feSMatthias Ringwald hfp_ag_codecs_nr = 0; 2551aeb0f0feSMatthias Ringwald hfp_ag_indicators_nr = 0; 2552aeb0f0feSMatthias Ringwald hfp_ag_call_hold_services_nr = 0; 255320b2edb6SMatthias Ringwald (void) memset(&hfp_ag_call_hold_services, 0, sizeof(hfp_ag_call_hold_services)); 2554aeb0f0feSMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 255520b2edb6SMatthias Ringwald (void) memset(&hfp_ag_response_and_hold_state, 0, sizeof(hfp_response_and_hold_state_t)); 2556aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers = NULL; 2557aeb0f0feSMatthias Ringwald (void) memset(&hfp_ag_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t)); 255820b2edb6SMatthias Ringwald } 255920b2edb6SMatthias Ringwald 25604eb3f1d8SMilanka Ringwald uint8_t hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){ 25614eb3f1d8SMilanka Ringwald return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE, HFP_ROLE_AG); 25623deb3ec6SMatthias Ringwald } 25633deb3ec6SMatthias Ringwald 2564657bc59fSMilanka Ringwald uint8_t hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){ 25659c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2566a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2567657bc59fSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2568a33eb0c4SMilanka Ringwald } 2569c5fa3c94SMilanka Ringwald 25701ffa0dd9SMilanka Ringwald hfp_trigger_release_service_level_connection(hfp_connection); 2571f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2572657bc59fSMilanka Ringwald return ERROR_CODE_SUCCESS; 25733deb3ec6SMatthias Ringwald } 25743deb3ec6SMatthias Ringwald 2575c5fa3c94SMilanka Ringwald uint8_t hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, hfp_cme_error_t error){ 25769c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2577a0ffb263SMatthias Ringwald if (!hfp_connection){ 2578c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 25793deb3ec6SMatthias Ringwald } 25803deb3ec6SMatthias Ringwald 2581c5fa3c94SMilanka Ringwald hfp_connection->extended_audio_gateway_error = 0; 2582c5fa3c94SMilanka Ringwald if (!hfp_connection->enable_extended_audio_gateway_error_report){ 2583c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2584c5fa3c94SMilanka Ringwald } 2585c5fa3c94SMilanka Ringwald 2586c5fa3c94SMilanka Ringwald hfp_connection->extended_audio_gateway_error = error; 2587c5fa3c94SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 2588c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2589c5fa3c94SMilanka Ringwald } 2590c5fa3c94SMilanka Ringwald 2591c5fa3c94SMilanka Ringwald static uint8_t hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){ 2592c5fa3c94SMilanka Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 2593c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2594c5fa3c94SMilanka Ringwald } 2595c5fa3c94SMilanka Ringwald if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){ 2596c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2597c5fa3c94SMilanka Ringwald } 25983deb3ec6SMatthias Ringwald 2599a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 1; 2600a0ffb263SMatthias Ringwald if (!has_codec_negotiation_feature(hfp_connection)){ 2601d6ff09e1SMatthias Ringwald log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using CVSD"); 2602d6ff09e1SMatthias Ringwald hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 2603a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 26047522e673SMatthias Ringwald // now, pick link settings 2605afac2a04SMilanka Ringwald hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection)); 26063721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP 26073721a235SMatthias Ringwald hfp_cc256x_prepare_for_sco(hfp_connection); 26083721a235SMatthias Ringwald #endif 2609c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 26103deb3ec6SMatthias Ringwald } 2611aa4dd815SMatthias Ringwald 2612411a9776SMilanka Ringwald uint8_t i; 2613411a9776SMilanka Ringwald bool codec_was_in_use = false; 2614411a9776SMilanka Ringwald bool better_codec_can_be_used = false; 2615411a9776SMilanka Ringwald 2616411a9776SMilanka Ringwald for (i = 0; i<hfp_connection->remote_codecs_nr; i++){ 2617411a9776SMilanka Ringwald if (hfp_connection->negotiated_codec == hfp_connection->remote_codecs[i]){ 2618411a9776SMilanka Ringwald codec_was_in_use = true; 2619411a9776SMilanka Ringwald } else if (hfp_connection->negotiated_codec < hfp_connection->remote_codecs[i]){ 2620411a9776SMilanka Ringwald better_codec_can_be_used = true; 2621411a9776SMilanka Ringwald } 2622411a9776SMilanka Ringwald } 2623411a9776SMilanka Ringwald 2624411a9776SMilanka Ringwald if (!codec_was_in_use || better_codec_can_be_used){ 26252ee45eb2SMilanka Ringwald hfp_connection->trigger_codec_exchange = 1; 2626411a9776SMilanka Ringwald hfp_connection->codecs_state = HFP_CODECS_IDLE; 2627411a9776SMilanka Ringwald } 2628c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2629aa4dd815SMatthias Ringwald } 2630aa4dd815SMatthias Ringwald 2631c5fa3c94SMilanka Ringwald uint8_t hfp_ag_establish_audio_connection(hci_con_handle_t acl_handle){ 26329c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2633a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2634c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2635a33eb0c4SMilanka Ringwald } 2636c5fa3c94SMilanka Ringwald uint8_t status = hfp_ag_setup_audio_connection(hfp_connection); 2637c5fa3c94SMilanka Ringwald if (status != ERROR_CODE_SUCCESS){ 2638c5fa3c94SMilanka Ringwald return status; 2639c5fa3c94SMilanka Ringwald } 2640f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2641c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 26423deb3ec6SMatthias Ringwald } 26433deb3ec6SMatthias Ringwald 2644c5fa3c94SMilanka Ringwald uint8_t hfp_ag_release_audio_connection(hci_con_handle_t acl_handle){ 26459c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2646a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2647c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2648a33eb0c4SMilanka Ringwald } 2649c5fa3c94SMilanka Ringwald 26500b4debbfSMilanka Ringwald if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){ 26510b4debbfSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 26520b4debbfSMilanka Ringwald } 26531a26de69SMilanka Ringwald 26540b4debbfSMilanka Ringwald uint8_t status = hfp_trigger_release_audio_connection(hfp_connection); 26550b4debbfSMilanka Ringwald if (status == ERROR_CODE_SUCCESS){ 2656f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 26570b4debbfSMilanka Ringwald } 26580b4debbfSMilanka Ringwald return status; 26593deb3ec6SMatthias Ringwald } 2660aa4dd815SMatthias Ringwald 2661aa4dd815SMatthias Ringwald /** 2662aa4dd815SMatthias Ringwald * @brief Enable in-band ring tone 2663aa4dd815SMatthias Ringwald */ 2664aa4dd815SMatthias Ringwald void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){ 2665aeb0f0feSMatthias Ringwald if (get_bit(hfp_ag_supported_features, HFP_AGSF_IN_BAND_RING_TONE) == use_in_band_ring_tone){ 2666aa4dd815SMatthias Ringwald return; 2667aa4dd815SMatthias Ringwald } 2668c5fa3c94SMilanka Ringwald 2669aeb0f0feSMatthias Ringwald hfp_ag_supported_features = store_bit(hfp_ag_supported_features, HFP_AGSF_IN_BAND_RING_TONE, use_in_band_ring_tone); 2670aa4dd815SMatthias Ringwald 2671665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 2672665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2673665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2674a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 267566c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2676a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING; 2677f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2678aa4dd815SMatthias Ringwald } 2679aa4dd815SMatthias Ringwald } 2680aa4dd815SMatthias Ringwald 2681aa4dd815SMatthias Ringwald /** 2682aa4dd815SMatthias Ringwald * @brief Called from GSM 2683aa4dd815SMatthias Ringwald */ 2684aa4dd815SMatthias Ringwald void hfp_ag_incoming_call(void){ 2685aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL); 2686aa4dd815SMatthias Ringwald } 2687aa4dd815SMatthias Ringwald 2688fe899794SMatthias Ringwald void hfp_ag_outgoing_call_initiated(const char * number) { 2689c5753b03SMatthias Ringwald UNUSED(number); 2690fe899794SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED_BY_AG, NULL); 2691fe899794SMatthias Ringwald } 2692fe899794SMatthias Ringwald 2693aa4dd815SMatthias Ringwald /** 2694aa4dd815SMatthias Ringwald * @brief number is stored. 2695aa4dd815SMatthias Ringwald */ 2696aa4dd815SMatthias Ringwald void hfp_ag_set_clip(uint8_t type, const char * number){ 2697f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_SET_CLIP, 0, type, number); 2698aa4dd815SMatthias Ringwald } 2699aa4dd815SMatthias Ringwald 2700aa4dd815SMatthias Ringwald void hfp_ag_call_dropped(void){ 2701aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL); 2702aa4dd815SMatthias Ringwald } 2703aa4dd815SMatthias Ringwald 2704aa4dd815SMatthias Ringwald // call from AG UI 2705aa4dd815SMatthias Ringwald void hfp_ag_answer_incoming_call(void){ 2706aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL); 2707aa4dd815SMatthias Ringwald } 2708aa4dd815SMatthias Ringwald 2709ce263fc8SMatthias Ringwald void hfp_ag_join_held_call(void){ 2710ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL); 2711ce263fc8SMatthias Ringwald } 2712ce263fc8SMatthias Ringwald 2713aa4dd815SMatthias Ringwald void hfp_ag_terminate_call(void){ 2714aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL); 2715aa4dd815SMatthias Ringwald } 2716aa4dd815SMatthias Ringwald 2717aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_ringing(void){ 2718aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL); 2719aa4dd815SMatthias Ringwald } 2720aa4dd815SMatthias Ringwald 2721aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_established(void){ 2722aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL); 2723aa4dd815SMatthias Ringwald } 2724aa4dd815SMatthias Ringwald 2725aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_rejected(void){ 2726aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL); 2727aa4dd815SMatthias Ringwald } 2728aa4dd815SMatthias Ringwald 2729aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_accepted(void){ 2730aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL); 2731aa4dd815SMatthias Ringwald } 2732ce263fc8SMatthias Ringwald 2733ce263fc8SMatthias Ringwald void hfp_ag_hold_incoming_call(void){ 2734ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL); 2735ce263fc8SMatthias Ringwald } 2736ce263fc8SMatthias Ringwald 2737ce263fc8SMatthias Ringwald void hfp_ag_accept_held_incoming_call(void) { 2738ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL); 2739ce263fc8SMatthias Ringwald } 2740ce263fc8SMatthias Ringwald 2741ce263fc8SMatthias Ringwald void hfp_ag_reject_held_incoming_call(void){ 2742ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL); 2743ce263fc8SMatthias Ringwald } 2744ce263fc8SMatthias Ringwald 2745aa4dd815SMatthias Ringwald static void hfp_ag_set_ag_indicator(const char * name, int value){ 2746aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name(name); 2747aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 2748aa4dd815SMatthias Ringwald hfp_ag_indicators[indicator_index].status = value; 2749aa4dd815SMatthias Ringwald 2750665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 2751665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2752665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2753a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 275466c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2755a0ffb263SMatthias Ringwald if (!hfp_connection->ag_indicators[indicator_index].enabled) { 2756245852b7SMilanka Ringwald log_info("Requested AG indicator '%s' update to %u, but it is not enabled", hfp_ag_indicators[indicator_index].name, value); 2757ce263fc8SMatthias Ringwald continue; 2758ce263fc8SMatthias Ringwald } 2759245852b7SMilanka Ringwald log_info("AG indicator '%s' changed to %u, request transfer status", hfp_ag_indicators[indicator_index].name, value); 2760a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 2761f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2762aa4dd815SMatthias Ringwald } 2763aa4dd815SMatthias Ringwald } 2764aa4dd815SMatthias Ringwald 2765c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_registration_status(int registration_status){ 2766c5fa3c94SMilanka Ringwald if ((registration_status < 0) || (registration_status > 1)){ 2767c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2768c5fa3c94SMilanka Ringwald } 2769c5fa3c94SMilanka Ringwald 2770c5fa3c94SMilanka Ringwald if ( (registration_status == 0) && (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 2771245852b7SMilanka Ringwald 2772245852b7SMilanka Ringwald // if network goes away wihle a call is active: 2773245852b7SMilanka Ringwald // - the call gets dropped 2774245852b7SMilanka Ringwald // - we send NO CARRIER 2775245852b7SMilanka Ringwald // NOTE: the CALL=0 has to be sent before NO CARRIER 2776245852b7SMilanka Ringwald 2777245852b7SMilanka Ringwald hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL); 2778245852b7SMilanka Ringwald 2779245852b7SMilanka Ringwald btstack_linked_list_iterator_t it; 2780245852b7SMilanka Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2781245852b7SMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2782245852b7SMilanka Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 2783245852b7SMilanka Ringwald hfp_connection->ag_send_no_carrier = true; 2784245852b7SMilanka Ringwald } 2785245852b7SMilanka Ringwald } 2786c5fa3c94SMilanka Ringwald hfp_ag_set_ag_indicator("service", registration_status); 2787c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2788aa4dd815SMatthias Ringwald } 2789aa4dd815SMatthias Ringwald 2790c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_signal_strength(int signal_strength){ 2791c5fa3c94SMilanka Ringwald if ((signal_strength < 0) || (signal_strength > 5)){ 2792c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2793aa4dd815SMatthias Ringwald } 2794aa4dd815SMatthias Ringwald 2795c5fa3c94SMilanka Ringwald hfp_ag_set_ag_indicator("signal", signal_strength); 2796c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2797aa4dd815SMatthias Ringwald } 2798aa4dd815SMatthias Ringwald 2799c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_roaming_status(int roaming_status){ 2800c5fa3c94SMilanka Ringwald if ((roaming_status < 0) || (roaming_status > 1)){ 2801c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2802aa4dd815SMatthias Ringwald } 2803aa4dd815SMatthias Ringwald 2804c5fa3c94SMilanka Ringwald hfp_ag_set_ag_indicator("roam", roaming_status); 2805c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2806c5fa3c94SMilanka Ringwald } 2807c5fa3c94SMilanka Ringwald 2808c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_battery_level(int battery_level){ 2809c5fa3c94SMilanka Ringwald if ((battery_level < 0) || (battery_level > 5)){ 2810c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2811c5fa3c94SMilanka Ringwald } 2812c5fa3c94SMilanka Ringwald hfp_ag_set_ag_indicator("battchg", battery_level); 2813c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2814c5fa3c94SMilanka Ringwald } 2815c5fa3c94SMilanka Ringwald 28166b8275b0SMilanka Ringwald static uint8_t activate_voice_recognition(hci_con_handle_t acl_handle, bool enhanced){ 28179c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2818a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2819c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2820a33eb0c4SMilanka Ringwald } 28216b8275b0SMilanka Ringwald 28226b8275b0SMilanka Ringwald uint8_t status = hfp_ag_can_activate_voice_recognition(hfp_connection, enhanced); 2823d84719b1SMilanka Ringwald if (status == ERROR_CODE_SUCCESS){ 2824734ef2bdSMilanka Ringwald hfp_connection->ag_activate_voice_recognition_value = 1; 2825fd4151d1SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED; 2826fd4151d1SMilanka Ringwald hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION; 28276b8275b0SMilanka Ringwald hfp_connection->enhanced_voice_recognition_enabled = enhanced; 28281a26de69SMilanka Ringwald hfp_connection->ag_audio_connection_opened_before_vra = hfp_ag_is_audio_connection_active(hfp_connection); 28296b8275b0SMilanka Ringwald hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY; 2830f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2831aa4dd815SMatthias Ringwald } 2832d84719b1SMilanka Ringwald return status; 2833d84719b1SMilanka Ringwald } 2834d84719b1SMilanka Ringwald 28356b8275b0SMilanka Ringwald static uint8_t deactivate_voice_recognition(hci_con_handle_t acl_handle, bool enhanced){ 28366b8275b0SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 28376b8275b0SMilanka Ringwald if (!hfp_connection){ 28386b8275b0SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 28396b8275b0SMilanka Ringwald } 28406b8275b0SMilanka Ringwald uint8_t status = hfp_ag_voice_recognition_session_active(hfp_connection, enhanced); 28416b8275b0SMilanka Ringwald if (status == ERROR_CODE_SUCCESS){ 28426b8275b0SMilanka Ringwald hfp_connection->ag_activate_voice_recognition_value = 0; 28436b8275b0SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF; 28446b8275b0SMilanka Ringwald hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION; 28456b8275b0SMilanka Ringwald hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY; 28466b8275b0SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 28476b8275b0SMilanka Ringwald } 28486b8275b0SMilanka Ringwald return status; 28496b8275b0SMilanka Ringwald } 28506b8275b0SMilanka Ringwald 28516b8275b0SMilanka Ringwald uint8_t hfp_ag_activate_voice_recognition(hci_con_handle_t acl_handle){ 28526b8275b0SMilanka Ringwald return activate_voice_recognition(acl_handle, false); 28536b8275b0SMilanka Ringwald } 28546b8275b0SMilanka Ringwald 28556b8275b0SMilanka Ringwald uint8_t hfp_ag_activate_enhanced_voice_recognition(hci_con_handle_t acl_handle){ 28566b8275b0SMilanka Ringwald return activate_voice_recognition(acl_handle, true); 28576b8275b0SMilanka Ringwald } 28586b8275b0SMilanka Ringwald 2859d84719b1SMilanka Ringwald uint8_t hfp_ag_deactivate_voice_recognition(hci_con_handle_t acl_handle){ 28606b8275b0SMilanka Ringwald return deactivate_voice_recognition(acl_handle, false); 286145796ff1SMilanka Ringwald } 286245796ff1SMilanka Ringwald 2863e83c2025SMilanka Ringwald uint8_t hfp_ag_deactivate_enhanced_voice_recognition(hci_con_handle_t acl_handle){ 28646b8275b0SMilanka Ringwald return deactivate_voice_recognition(acl_handle, true); 286545796ff1SMilanka Ringwald } 286645796ff1SMilanka Ringwald 2867cf75be85SMilanka Ringwald static uint8_t hfp_ag_enhanced_voice_recognition_send_state(hci_con_handle_t acl_handle, hfp_voice_recognition_state_t state){ 286845796ff1SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 286945796ff1SMilanka Ringwald if (!hfp_connection){ 2870c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 287145796ff1SMilanka Ringwald } 2872c5fa3c94SMilanka Ringwald 28736a61d7dcSMilanka Ringwald uint8_t status = hfp_ag_voice_recognition_session_active(hfp_connection, true); 28746a61d7dcSMilanka Ringwald if (status == ERROR_CODE_SUCCESS){ 2875cf75be85SMilanka Ringwald hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION; 287645796ff1SMilanka Ringwald hfp_connection->ag_vra_state = state; 2877cf75be85SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_STATUS; 287845796ff1SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 28796a61d7dcSMilanka Ringwald } 28806a61d7dcSMilanka Ringwald return status; 288145796ff1SMilanka Ringwald } 288245796ff1SMilanka Ringwald 2883f0c27a95SMilanka Ringwald uint8_t hfp_ag_enhanced_voice_recognition_report_sending_audio(hci_con_handle_t acl_handle){ 2884cf75be85SMilanka Ringwald return hfp_ag_enhanced_voice_recognition_send_state(acl_handle, HFP_VOICE_RECOGNITION_STATE_AG_IS_STARTING_SOUND); 288545796ff1SMilanka Ringwald } 2886f0c27a95SMilanka Ringwald uint8_t hfp_ag_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle){ 2887cf75be85SMilanka Ringwald return hfp_ag_enhanced_voice_recognition_send_state(acl_handle, HFP_VOICE_RECOGNITION_STATE_AG_READY_TO_ACCEPT_AUDIO_INPUT); 288845796ff1SMilanka Ringwald } 2889cf75be85SMilanka Ringwald uint8_t hfp_ag_enhanced_voice_recognition_report_processing_input(hci_con_handle_t acl_handle){ 2890cf75be85SMilanka Ringwald return hfp_ag_enhanced_voice_recognition_send_state(acl_handle, HFP_VOICE_RECOGNITION_STATE_AG_IS_PROCESSING_AUDIO_INPUT); 289145796ff1SMilanka Ringwald } 289245796ff1SMilanka Ringwald 2893b95cac54SMilanka Ringwald 2894b95cac54SMilanka 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){ 2895b95cac54SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2896b95cac54SMilanka Ringwald if (!hfp_connection){ 2897b95cac54SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2898b95cac54SMilanka Ringwald } 2899b95cac54SMilanka Ringwald 2900b95cac54SMilanka Ringwald uint8_t status = hfp_ag_can_send_enahnced_voice_recognition_message(hfp_connection, true); 2901b95cac54SMilanka Ringwald if (status == ERROR_CODE_SUCCESS){ 2902b95cac54SMilanka Ringwald hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION; 2903b95cac54SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_MSG; 2904b95cac54SMilanka Ringwald hfp_connection->ag_msg = msg; 2905b95cac54SMilanka Ringwald hfp_connection->ag_vra_state = state; 2906b95cac54SMilanka Ringwald hfp_ag_run_for_context(hfp_connection); 2907b95cac54SMilanka Ringwald } 2908b95cac54SMilanka Ringwald return status; 2909b95cac54SMilanka Ringwald } 2910b95cac54SMilanka Ringwald 2911b95cac54SMilanka Ringwald 2912c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 29139c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2914a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2915c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2916a33eb0c4SMilanka Ringwald } 2917c5fa3c94SMilanka Ringwald 2918c5fa3c94SMilanka Ringwald if ((gain < 0) || (gain > 15)){ 2919c5fa3c94SMilanka Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 2920c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2921c5fa3c94SMilanka Ringwald } 2922c5fa3c94SMilanka Ringwald 2923a0ffb263SMatthias Ringwald if (hfp_connection->microphone_gain != gain){ 2924a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_SET_MICROPHONE_GAIN; 2925a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = gain; 2926a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 2927aa4dd815SMatthias Ringwald } 2928f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2929c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2930aa4dd815SMatthias Ringwald } 2931aa4dd815SMatthias Ringwald 2932c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 29339c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2934a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2935c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2936a33eb0c4SMilanka Ringwald } 2937c5fa3c94SMilanka Ringwald 2938c5fa3c94SMilanka Ringwald if ((gain < 0) || (gain > 15)){ 2939c5fa3c94SMilanka Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 2940c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2941c5fa3c94SMilanka Ringwald } 2942c5fa3c94SMilanka Ringwald 2943a0ffb263SMatthias Ringwald if (hfp_connection->speaker_gain != gain){ 2944a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = gain; 2945a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 2946aa4dd815SMatthias Ringwald } 2947f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2948c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2949aa4dd815SMatthias Ringwald } 2950aa4dd815SMatthias Ringwald 2951c5fa3c94SMilanka Ringwald uint8_t hfp_ag_send_phone_number_for_voice_tag(hci_con_handle_t acl_handle, const char * number){ 29529c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2953a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2954c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2955a33eb0c4SMilanka Ringwald } 2956c5fa3c94SMilanka Ringwald 2957aa4dd815SMatthias Ringwald hfp_ag_set_clip(0, number); 2958a0ffb263SMatthias Ringwald hfp_connection->send_phone_number_for_voice_tag = 1; 2959c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2960aa4dd815SMatthias Ringwald } 2961aa4dd815SMatthias Ringwald 2962c5fa3c94SMilanka Ringwald uint8_t hfp_ag_reject_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 29639c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2964a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2965c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2966a33eb0c4SMilanka Ringwald } 2967c5fa3c94SMilanka Ringwald 2968a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 2969c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2970aa4dd815SMatthias Ringwald } 2971aa4dd815SMatthias Ringwald 2972c5fa3c94SMilanka Ringwald uint8_t hfp_ag_send_dtmf_code_done(hci_con_handle_t acl_handle){ 29739c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2974a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2975c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2976a33eb0c4SMilanka Ringwald } 2977c5fa3c94SMilanka Ringwald 2978a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2979c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 2980c1797c7dSMatthias Ringwald } 2981aa4dd815SMatthias Ringwald 2982ce263fc8SMatthias Ringwald void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){ 2983aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers = numbers; 2984aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers_count = numbers_count; 2985ce263fc8SMatthias Ringwald } 2986ce263fc8SMatthias Ringwald 29879cae807eSMatthias Ringwald void hfp_ag_clear_last_dialed_number(void){ 29889cae807eSMatthias Ringwald hfp_gsm_clear_last_dialed_number(); 2989ce263fc8SMatthias Ringwald } 2990ce263fc8SMatthias Ringwald 29919de679b7SMilanka Ringwald void hfp_ag_set_last_dialed_number(const char * number){ 29929de679b7SMilanka Ringwald hfp_gsm_set_last_dialed_number(number); 29939de679b7SMilanka Ringwald } 29949de679b7SMilanka Ringwald 2995c5fa3c94SMilanka Ringwald uint8_t hfp_ag_notify_incoming_call_waiting(hci_con_handle_t acl_handle){ 29969c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2997a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2998c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2999a33eb0c4SMilanka Ringwald } 3000c5fa3c94SMilanka Ringwald 3001c5fa3c94SMilanka Ringwald if (!hfp_connection->call_waiting_notification_enabled){ 3002c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3003c5fa3c94SMilanka Ringwald } 3004a0ffb263SMatthias Ringwald 3005a0ffb263SMatthias Ringwald hfp_connection->ag_notify_incoming_call_waiting = 1; 3006f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 3007c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS; 3008a0ffb263SMatthias Ringwald } 3009ce263fc8SMatthias Ringwald 301076cc1527SMatthias 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){ 301176cc1527SMatthias Ringwald if (!name){ 3012aeb0f0feSMatthias Ringwald name = hfp_ag_default_service_name; 301376cc1527SMatthias Ringwald } 301476cc1527SMatthias Ringwald hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, rfcomm_channel_nr, name); 301576cc1527SMatthias Ringwald 301676cc1527SMatthias Ringwald /* 301776cc1527SMatthias Ringwald * 0x01 – Ability to reject a call 301876cc1527SMatthias Ringwald * 0x00 – No ability to reject a call 301976cc1527SMatthias Ringwald */ 302076cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301); // Hands-Free Profile - Network 302176cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call); 302276cc1527SMatthias Ringwald 302376cc1527SMatthias Ringwald // Construct SupportedFeatures for SDP bitmap: 302476cc1527SMatthias Ringwald // 302576cc1527SMatthias Ringwald // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 302676cc1527SMatthias Ringwald // of the Bits 0 to 4 of the unsolicited result code +BRSF" 302776cc1527SMatthias Ringwald // 302876cc1527SMatthias Ringwald // Wide band speech (bit 5) requires Codec negotiation 302976cc1527SMatthias Ringwald // 303076cc1527SMatthias Ringwald uint16_t sdp_features = supported_features & 0x1f; 3031ef3ae4ebSMilanka Ringwald if ( (wide_band_speech == 1) && (supported_features & (1 << HFP_AGSF_CODEC_NEGOTIATION))){ 303276cc1527SMatthias Ringwald sdp_features |= 1 << 5; 303376cc1527SMatthias Ringwald } 3034ef3ae4ebSMilanka Ringwald 3035ef3ae4ebSMilanka Ringwald if (supported_features & (1 << HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS)){ 3036ef3ae4ebSMilanka Ringwald sdp_features |= 1 << 6; 3037ef3ae4ebSMilanka Ringwald } 3038ef3ae4ebSMilanka Ringwald 3039ef3ae4ebSMilanka Ringwald if (supported_features & (1 << HFP_AGSF_VOICE_RECOGNITION_TEXT)){ 3040ef3ae4ebSMilanka Ringwald sdp_features |= 1 << 7; 3041ef3ae4ebSMilanka Ringwald } 3042ef3ae4ebSMilanka Ringwald 304376cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 304476cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 304576cc1527SMatthias Ringwald } 304676cc1527SMatthias Ringwald 304776cc1527SMatthias Ringwald void hfp_ag_register_packet_handler(btstack_packet_handler_t callback){ 304868466199SMilanka Ringwald btstack_assert(callback != NULL); 304968466199SMilanka Ringwald 305076cc1527SMatthias Ringwald hfp_ag_callback = callback; 305176cc1527SMatthias Ringwald hfp_set_ag_callback(callback); 305276cc1527SMatthias Ringwald } 3053