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> 493deb3ec6SMatthias Ringwald #include <string.h> 503deb3ec6SMatthias Ringwald 5156042629SMatthias Ringwald #include "hci_cmd.h" 5282636622SMatthias Ringwald #include "btstack_run_loop.h" 533deb3ec6SMatthias Ringwald 54235946f1SMatthias Ringwald #include "bluetooth_sdp.h" 553deb3ec6SMatthias Ringwald #include "hci.h" 563deb3ec6SMatthias Ringwald #include "btstack_memory.h" 573deb3ec6SMatthias Ringwald #include "hci_dump.h" 583deb3ec6SMatthias Ringwald #include "l2cap.h" 5916ece135SMatthias Ringwald #include "btstack_debug.h" 60e30a6a47SMatthias Ringwald #include "btstack_event.h" 6159c6af15SMatthias Ringwald #include "classic/core.h" 623edc84c5SMatthias Ringwald #include "classic/hfp.h" 633edc84c5SMatthias Ringwald #include "classic/hfp_ag.h" 64023f2764SMatthias Ringwald #include "classic/hfp_gsm_model.h" 65efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h" 66023f2764SMatthias Ringwald #include "classic/sdp_server.h" 67023f2764SMatthias Ringwald #include "classic/sdp_util.h" 683deb3ec6SMatthias Ringwald 69c5b64319SMatthias Ringwald // private prototypes 70f0aeb307SMatthias Ringwald static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection); 71adaba9f3SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection); 72adaba9f3SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection); 73c5b64319SMatthias Ringwald 74c5b64319SMatthias Ringwald // public prototypes 7535833313SMatthias Ringwald hfp_generic_status_indicator_t * get_hfp_generic_status_indicators(void); 7635833313SMatthias Ringwald int get_hfp_generic_status_indicators_nr(void); 77c5b64319SMatthias Ringwald void set_hfp_generic_status_indicators(hfp_generic_status_indicator_t * indicators, int indicator_nr); 78c5b64319SMatthias Ringwald void set_hfp_ag_indicators(hfp_ag_indicator_t * indicators, int indicator_nr); 79c5b64319SMatthias Ringwald int get_hfp_ag_indicators_nr(hfp_connection_t * context); 80c5b64319SMatthias Ringwald hfp_ag_indicator_t * get_hfp_ag_indicators(hfp_connection_t * context); 81c5b64319SMatthias Ringwald 8227950165SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 8327950165SMatthias Ringwald 84c5b64319SMatthias Ringwald // gobals 853deb3ec6SMatthias Ringwald static const char default_hfp_ag_service_name[] = "Voice gateway"; 86aa4dd815SMatthias Ringwald 873deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_AG_SUPPORTED_FEATURES; 88aa4dd815SMatthias Ringwald 893deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0; 903deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS]; 913deb3ec6SMatthias Ringwald 923deb3ec6SMatthias Ringwald static int hfp_ag_indicators_nr = 0; 9325789943SMilanka Ringwald static hfp_ag_indicator_t hfp_ag_indicators[HFP_MAX_NUM_INDICATORS]; 943deb3ec6SMatthias Ringwald 95a0ffb263SMatthias Ringwald static int hfp_generic_status_indicators_nr = 0; 9625789943SMilanka Ringwald static hfp_generic_status_indicator_t hfp_generic_status_indicators[HFP_MAX_NUM_INDICATORS]; 97a0ffb263SMatthias Ringwald 983deb3ec6SMatthias Ringwald static int hfp_ag_call_hold_services_nr = 0; 993deb3ec6SMatthias Ringwald static char *hfp_ag_call_hold_services[6]; 100ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_ag_callback; 1013deb3ec6SMatthias Ringwald 102ce263fc8SMatthias Ringwald static hfp_response_and_hold_state_t hfp_ag_response_and_hold_state; 103ce263fc8SMatthias Ringwald static int hfp_ag_response_and_hold_active = 0; 104aa4dd815SMatthias Ringwald 105ce263fc8SMatthias Ringwald // Subcriber information entries 106ce263fc8SMatthias Ringwald static hfp_phone_number_t * subscriber_numbers = NULL; 107ce263fc8SMatthias Ringwald static int subscriber_numbers_count = 0; 108ce263fc8SMatthias Ringwald 1093deb3ec6SMatthias Ringwald 11076cc1527SMatthias Ringwald static void hfp_ag_emit_simple_event(uint8_t event_subtype){ 11176cc1527SMatthias Ringwald uint8_t event[3]; 11276cc1527SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 11376cc1527SMatthias Ringwald event[1] = sizeof(event) - 2; 11476cc1527SMatthias Ringwald event[2] = event_subtype; 11576cc1527SMatthias Ringwald if (!hfp_ag_callback) return; 11676cc1527SMatthias Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 11776cc1527SMatthias Ringwald } 11876cc1527SMatthias Ringwald 119a0ffb263SMatthias Ringwald static int hfp_ag_get_ag_indicators_nr(hfp_connection_t * hfp_connection){ 120a0ffb263SMatthias Ringwald if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){ 121a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr; 1226535961aSMatthias Ringwald (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, 1236535961aSMatthias Ringwald hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 1243deb3ec6SMatthias Ringwald } 125a0ffb263SMatthias Ringwald return hfp_connection->ag_indicators_nr; 126a0ffb263SMatthias Ringwald } 127a0ffb263SMatthias Ringwald 128a0ffb263SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection){ 129a0ffb263SMatthias Ringwald // TODO: save only value, and value changed in the hfp_connection? 130a0ffb263SMatthias Ringwald if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){ 131a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr; 1326535961aSMatthias Ringwald (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, 1336535961aSMatthias Ringwald hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 134a0ffb263SMatthias Ringwald } 135a0ffb263SMatthias Ringwald return (hfp_ag_indicator_t *)&(hfp_connection->ag_indicators); 1363deb3ec6SMatthias Ringwald } 1373deb3ec6SMatthias Ringwald 138aa4dd815SMatthias Ringwald static hfp_ag_indicator_t * get_ag_indicator_for_name(const char * name){ 139aa4dd815SMatthias Ringwald int i; 140aa4dd815SMatthias Ringwald for (i = 0; i < hfp_ag_indicators_nr; i++){ 141aa4dd815SMatthias Ringwald if (strcmp(hfp_ag_indicators[i].name, name) == 0){ 142aa4dd815SMatthias Ringwald return &hfp_ag_indicators[i]; 143aa4dd815SMatthias Ringwald } 144aa4dd815SMatthias Ringwald } 145aa4dd815SMatthias Ringwald return NULL; 146aa4dd815SMatthias Ringwald } 147aa4dd815SMatthias Ringwald 148aa4dd815SMatthias Ringwald static int get_ag_indicator_index_for_name(const char * name){ 149aa4dd815SMatthias Ringwald int i; 150aa4dd815SMatthias Ringwald for (i = 0; i < hfp_ag_indicators_nr; i++){ 151aa4dd815SMatthias Ringwald if (strcmp(hfp_ag_indicators[i].name, name) == 0){ 152aa4dd815SMatthias Ringwald return i; 153aa4dd815SMatthias Ringwald } 154aa4dd815SMatthias Ringwald } 155aa4dd815SMatthias Ringwald return -1; 156aa4dd815SMatthias Ringwald } 157aa4dd815SMatthias Ringwald 1589c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_ag_connection_context_for_acl_handle(uint16_t handle){ 1599c9c64c1SMatthias Ringwald btstack_linked_list_iterator_t it; 1609c9c64c1SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1619c9c64c1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1629c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1639c9c64c1SMatthias Ringwald if (hfp_connection->acl_handle != handle) continue; 1649c9c64c1SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1659c9c64c1SMatthias Ringwald return hfp_connection; 1669c9c64c1SMatthias Ringwald } 1679c9c64c1SMatthias Ringwald return NULL; 1689c9c64c1SMatthias Ringwald } 1693deb3ec6SMatthias Ringwald 1700cb5b971SMatthias Ringwald static int use_in_band_tone(void){ 171aa4dd815SMatthias Ringwald return get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE); 1723deb3ec6SMatthias Ringwald } 1733deb3ec6SMatthias Ringwald 174a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){ 175a0ffb263SMatthias Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_CODEC_NEGOTIATION); 1763deb3ec6SMatthias Ringwald int ag = get_bit(hfp_supported_features, HFP_AGSF_CODEC_NEGOTIATION); 1773deb3ec6SMatthias Ringwald return hf && ag; 1783deb3ec6SMatthias Ringwald } 1793deb3ec6SMatthias Ringwald 180a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){ 181a0ffb263SMatthias Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_THREE_WAY_CALLING); 1823deb3ec6SMatthias Ringwald int ag = get_bit(hfp_supported_features, HFP_AGSF_THREE_WAY_CALLING); 1833deb3ec6SMatthias Ringwald return hf && ag; 1843deb3ec6SMatthias Ringwald } 1853deb3ec6SMatthias Ringwald 186a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){ 187a0ffb263SMatthias Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_HF_INDICATORS); 1883deb3ec6SMatthias Ringwald int ag = get_bit(hfp_supported_features, HFP_AGSF_HF_INDICATORS); 1893deb3ec6SMatthias Ringwald return hf && ag; 1903deb3ec6SMatthias Ringwald } 1913deb3ec6SMatthias Ringwald 19276cc1527SMatthias Ringwald /* unsolicited responses */ 193aa4dd815SMatthias Ringwald 194485ac19eSMilanka Ringwald static int hfp_ag_send_change_in_band_ring_tone_setting_cmd(uint16_t cid){ 195aa4dd815SMatthias Ringwald char buffer[20]; 196aa4dd815SMatthias Ringwald sprintf(buffer, "\r\n%s:%d\r\n", HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone()); 197aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 1983deb3ec6SMatthias Ringwald } 1993deb3ec6SMatthias Ringwald 2003deb3ec6SMatthias Ringwald static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){ 2013deb3ec6SMatthias Ringwald char buffer[40]; 2023deb3ec6SMatthias Ringwald sprintf(buffer, "\r\n%s:%d\r\n\r\nOK\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features); 2033deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2043deb3ec6SMatthias Ringwald } 2053deb3ec6SMatthias Ringwald 206485ac19eSMilanka Ringwald static int hfp_ag_send_ok(uint16_t cid){ 2073deb3ec6SMatthias Ringwald char buffer[10]; 2083deb3ec6SMatthias Ringwald sprintf(buffer, "\r\nOK\r\n"); 2093deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2103deb3ec6SMatthias Ringwald } 2113deb3ec6SMatthias Ringwald 212485ac19eSMilanka Ringwald static int hfp_ag_send_ring(uint16_t cid){ 213aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, (char *) "\r\nRING\r\n"); 214aa4dd815SMatthias Ringwald } 215aa4dd815SMatthias Ringwald 216aa4dd815SMatthias Ringwald static int hfp_ag_send_clip(uint16_t cid){ 217aa4dd815SMatthias Ringwald char buffer[50]; 218d0c20769SMatthias Ringwald sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP, hfp_gsm_clip_number(), hfp_gsm_clip_type()); 219aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 220aa4dd815SMatthias Ringwald } 221aa4dd815SMatthias Ringwald 222ce263fc8SMatthias Ringwald static int hfp_send_subscriber_number_cmd(uint16_t cid, uint8_t type, const char * number){ 223ce263fc8SMatthias Ringwald char buffer[50]; 224ce263fc8SMatthias Ringwald sprintf(buffer, "\r\n%s: ,\"%s\",%u, , \r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type); 225ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 226ce263fc8SMatthias Ringwald } 227ce263fc8SMatthias Ringwald 228c1797c7dSMatthias Ringwald static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){ 229aa4dd815SMatthias Ringwald char buffer[50]; 230d0c20769SMatthias Ringwald sprintf(buffer, "\r\n%s: %s\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number()); 231aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 232aa4dd815SMatthias Ringwald } 233aa4dd815SMatthias Ringwald 234aa4dd815SMatthias Ringwald static int hfp_ag_send_call_waiting_notification(uint16_t cid){ 235aa4dd815SMatthias Ringwald char buffer[50]; 236a0ffb263SMatthias Ringwald sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(), hfp_gsm_clip_type()); 237aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 238aa4dd815SMatthias Ringwald } 239aa4dd815SMatthias Ringwald 240485ac19eSMilanka Ringwald static int hfp_ag_send_error(uint16_t cid){ 2413deb3ec6SMatthias Ringwald char buffer[10]; 2423deb3ec6SMatthias Ringwald sprintf(buffer, "\r\nERROR\r\n"); 2433deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2443deb3ec6SMatthias Ringwald } 2453deb3ec6SMatthias Ringwald 246485ac19eSMilanka Ringwald static int hfp_ag_send_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){ 2473deb3ec6SMatthias Ringwald char buffer[20]; 2483deb3ec6SMatthias Ringwald sprintf(buffer, "\r\n%s=%d\r\n", HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error); 2493deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2503deb3ec6SMatthias Ringwald } 2513deb3ec6SMatthias Ringwald 252ad902e3dSMatthias Ringwald // get size for indicator string 253a0ffb263SMatthias Ringwald static int hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection, int i){ 254ad902e3dSMatthias Ringwald // template: ("$NAME",($MIN,$MAX)) 25545718b6fSMatthias Ringwald return 8 + (int) strlen(hfp_ag_get_ag_indicators(hfp_connection)[i].name) 256a0ffb263SMatthias Ringwald + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range) 257a0ffb263SMatthias Ringwald + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range); 258ad902e3dSMatthias Ringwald } 259ad902e3dSMatthias Ringwald 260ad902e3dSMatthias Ringwald // store indicator 261a0ffb263SMatthias Ringwald static void hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection, int i, uint8_t * buffer){ 262ad902e3dSMatthias Ringwald sprintf((char *) buffer, "(\"%s\",(%d,%d)),", 263a0ffb263SMatthias Ringwald hfp_ag_get_ag_indicators(hfp_connection)[i].name, 264a0ffb263SMatthias Ringwald hfp_ag_get_ag_indicators(hfp_connection)[i].min_range, 265a0ffb263SMatthias Ringwald hfp_ag_get_ag_indicators(hfp_connection)[i].max_range); 2663deb3ec6SMatthias Ringwald } 267ad902e3dSMatthias Ringwald 268ad902e3dSMatthias Ringwald // structure: header [indicator [comma indicator]] footer 269a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_num_segments(hfp_connection_t * hfp_connection){ 270a0ffb263SMatthias Ringwald int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 271ad902e3dSMatthias Ringwald if (!num_indicators) return 2; 272c1ab6cc1SMatthias Ringwald return 3 + ((num_indicators-1) * 2); 2733deb3ec6SMatthias Ringwald } 274ad902e3dSMatthias Ringwald 275ad902e3dSMatthias Ringwald // get size of individual segment for hfp_ag_retrieve_indicators_cmd 276a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_get_segment_len(hfp_connection_t * hfp_connection, int index){ 277ad902e3dSMatthias Ringwald if (index == 0) { 278ad902e3dSMatthias Ringwald return strlen(HFP_INDICATOR) + 3; // "\n\r%s:"" 279ad902e3dSMatthias Ringwald } 280ad902e3dSMatthias Ringwald index--; 281a0ffb263SMatthias Ringwald int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 282ad902e3dSMatthias Ringwald int indicator_index = index >> 1; 283ad902e3dSMatthias Ringwald if ((index & 1) == 0){ 284a0ffb263SMatthias Ringwald return hfp_ag_indicators_string_size(hfp_connection, indicator_index); 285ad902e3dSMatthias Ringwald } 286c1ab6cc1SMatthias Ringwald if (indicator_index == (num_indicators - 1)){ 287ad902e3dSMatthias Ringwald return 8; // "\r\n\r\nOK\r\n" 288ad902e3dSMatthias Ringwald } 289ad902e3dSMatthias Ringwald return 1; // comma 290ad902e3dSMatthias Ringwald } 291ad902e3dSMatthias Ringwald 292a0ffb263SMatthias Ringwald static void hgp_ag_indicators_cmd_generator_store_segment(hfp_connection_t * hfp_connection, int index, uint8_t * buffer){ 293ad902e3dSMatthias Ringwald if (index == 0){ 294ad902e3dSMatthias Ringwald *buffer++ = '\r'; 29574386ee0SMatthias Ringwald *buffer++ = '\n'; 296ad902e3dSMatthias Ringwald int len = strlen(HFP_INDICATOR); 2976535961aSMatthias Ringwald (void)memcpy(buffer, HFP_INDICATOR, len); 298ad902e3dSMatthias Ringwald buffer += len; 299ad902e3dSMatthias Ringwald *buffer++ = ':'; 300ad902e3dSMatthias Ringwald return; 301ad902e3dSMatthias Ringwald } 302ad902e3dSMatthias Ringwald index--; 303a0ffb263SMatthias Ringwald int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 304ad902e3dSMatthias Ringwald int indicator_index = index >> 1; 305ad902e3dSMatthias Ringwald if ((index & 1) == 0){ 306a0ffb263SMatthias Ringwald hfp_ag_indicators_string_store(hfp_connection, indicator_index, buffer); 307ad902e3dSMatthias Ringwald return; 308ad902e3dSMatthias Ringwald } 309c1ab6cc1SMatthias Ringwald if (indicator_index == (num_indicators-1)){ 3106535961aSMatthias Ringwald (void)memcpy(buffer, "\r\n\r\nOK\r\n", 8); 311ad902e3dSMatthias Ringwald return; 312ad902e3dSMatthias Ringwald } 313ad902e3dSMatthias Ringwald *buffer = ','; 3143deb3ec6SMatthias Ringwald } 3153deb3ec6SMatthias Ringwald 3163deb3ec6SMatthias Ringwald static int hfp_hf_indicators_join(char * buffer, int buffer_size){ 317c1ab6cc1SMatthias Ringwald if (buffer_size < (hfp_ag_indicators_nr * 3)) return 0; 3183deb3ec6SMatthias Ringwald int i; 3193deb3ec6SMatthias Ringwald int offset = 0; 320c1ab6cc1SMatthias Ringwald for (i = 0; i < (hfp_generic_status_indicators_nr-1); i++) { 321a0ffb263SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid); 3223deb3ec6SMatthias Ringwald } 323a0ffb263SMatthias Ringwald if (i < hfp_generic_status_indicators_nr){ 324a0ffb263SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid); 3253deb3ec6SMatthias Ringwald } 3263deb3ec6SMatthias Ringwald return offset; 3273deb3ec6SMatthias Ringwald } 3283deb3ec6SMatthias Ringwald 3293deb3ec6SMatthias Ringwald static int hfp_hf_indicators_initial_status_join(char * buffer, int buffer_size){ 330c1ab6cc1SMatthias Ringwald if (buffer_size < (hfp_generic_status_indicators_nr * 3)) return 0; 3313deb3ec6SMatthias Ringwald int i; 3323deb3ec6SMatthias Ringwald int offset = 0; 333a0ffb263SMatthias Ringwald for (i = 0; i < hfp_generic_status_indicators_nr; i++) { 334a0ffb263SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "\r\n%s:%d,%d\r\n", HFP_GENERIC_STATUS_INDICATOR, hfp_generic_status_indicators[i].uuid, hfp_generic_status_indicators[i].state); 3353deb3ec6SMatthias Ringwald } 3363deb3ec6SMatthias Ringwald return offset; 3373deb3ec6SMatthias Ringwald } 3383deb3ec6SMatthias Ringwald 3393deb3ec6SMatthias Ringwald static int hfp_ag_indicators_status_join(char * buffer, int buffer_size){ 340c1ab6cc1SMatthias Ringwald if (buffer_size < (hfp_ag_indicators_nr * 3)) return 0; 3413deb3ec6SMatthias Ringwald int i; 3423deb3ec6SMatthias Ringwald int offset = 0; 343c1ab6cc1SMatthias Ringwald for (i = 0; i < (hfp_ag_indicators_nr-1); i++) { 3443deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_indicators[i].status); 3453deb3ec6SMatthias Ringwald } 3463deb3ec6SMatthias Ringwald if (i<hfp_ag_indicators_nr){ 3473deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_indicators[i].status); 3483deb3ec6SMatthias Ringwald } 3493deb3ec6SMatthias Ringwald return offset; 3503deb3ec6SMatthias Ringwald } 3513deb3ec6SMatthias Ringwald 3523deb3ec6SMatthias Ringwald static int hfp_ag_call_services_join(char * buffer, int buffer_size){ 353c1ab6cc1SMatthias Ringwald if (buffer_size < (hfp_ag_call_hold_services_nr * 3)) return 0; 3543deb3ec6SMatthias Ringwald int i; 3553deb3ec6SMatthias Ringwald int offset = snprintf(buffer, buffer_size, "("); 356c1ab6cc1SMatthias Ringwald for (i = 0; i < (hfp_ag_call_hold_services_nr-1); i++) { 3573deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%s,", hfp_ag_call_hold_services[i]); 3583deb3ec6SMatthias Ringwald } 3593deb3ec6SMatthias Ringwald if (i<hfp_ag_call_hold_services_nr){ 3603deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%s)", hfp_ag_call_hold_services[i]); 3613deb3ec6SMatthias Ringwald } 3623deb3ec6SMatthias Ringwald return offset; 3633deb3ec6SMatthias Ringwald } 3643deb3ec6SMatthias Ringwald 365485ac19eSMilanka Ringwald static int hfp_ag_send_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, 366ad902e3dSMatthias Ringwald int start_segment, int num_segments, 367a0ffb263SMatthias Ringwald int (*get_segment_len)(hfp_connection_t * hfp_connection, int segment), 368a0ffb263SMatthias Ringwald void (*store_segment) (hfp_connection_t * hfp_connection, int segment, uint8_t * buffer)){ 3693deb3ec6SMatthias Ringwald 370ad902e3dSMatthias Ringwald // assumes: can send now == true 371ad902e3dSMatthias Ringwald // assumes: num segments > 0 372aba39421SMatthias Ringwald // assumes: individual segments are smaller than MTU 373ad902e3dSMatthias Ringwald rfcomm_reserve_packet_buffer(); 374ad902e3dSMatthias Ringwald int mtu = rfcomm_get_max_frame_size(cid); 375ad902e3dSMatthias Ringwald uint8_t * data = rfcomm_get_outgoing_buffer(); 376ad902e3dSMatthias Ringwald int offset = 0; 377ad902e3dSMatthias Ringwald int segment = start_segment; 378ad902e3dSMatthias Ringwald while (segment < num_segments){ 379a0ffb263SMatthias Ringwald int segment_len = get_segment_len(hfp_connection, segment); 380c1ab6cc1SMatthias Ringwald if ((offset + segment_len) > mtu) break; 381aba39421SMatthias Ringwald // append segement 382a0ffb263SMatthias Ringwald store_segment(hfp_connection, segment, data+offset); 383ad902e3dSMatthias Ringwald offset += segment_len; 384ad902e3dSMatthias Ringwald segment++; 385ad902e3dSMatthias Ringwald } 386ad902e3dSMatthias Ringwald rfcomm_send_prepared(cid, offset); 387ad902e3dSMatthias Ringwald return segment; 388ad902e3dSMatthias Ringwald } 3893deb3ec6SMatthias Ringwald 390ad902e3dSMatthias Ringwald // returns next segment to store 391485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, int start_segment){ 392a0ffb263SMatthias Ringwald int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection); 393485ac19eSMilanka Ringwald return hfp_ag_send_cmd_via_generator(cid, hfp_connection, start_segment, num_segments, 394ad902e3dSMatthias Ringwald hfp_ag_indicators_cmd_generator_get_segment_len, hgp_ag_indicators_cmd_generator_store_segment); 3953deb3ec6SMatthias Ringwald } 3963deb3ec6SMatthias Ringwald 397485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_status_cmd(uint16_t cid){ 3983deb3ec6SMatthias Ringwald char buffer[40]; 39989425bfcSMilanka Ringwald const int size = sizeof(buffer); 40089425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "\r\n%s:", HFP_INDICATOR); 40189425bfcSMilanka Ringwald offset += hfp_ag_indicators_status_join(buffer+offset, size-offset-9); 40289425bfcSMilanka Ringwald offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n"); 4033deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4043deb3ec6SMatthias Ringwald } 4053deb3ec6SMatthias Ringwald 406485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_can_hold_call_cmd(uint16_t cid){ 407ad902e3dSMatthias Ringwald char buffer[40]; 40889425bfcSMilanka Ringwald const int size = sizeof(buffer); 40989425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "\r\n%s:", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES); 41089425bfcSMilanka Ringwald offset += hfp_ag_call_services_join(buffer+offset, size-offset-9); 41189425bfcSMilanka Ringwald offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n"); 4123deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4133deb3ec6SMatthias Ringwald } 4143deb3ec6SMatthias Ringwald 4153deb3ec6SMatthias Ringwald 416485ac19eSMilanka Ringwald static int hfp_ag_send_list_supported_generic_status_indicators_cmd(uint16_t cid){ 417485ac19eSMilanka Ringwald return hfp_ag_send_ok(cid); 4183deb3ec6SMatthias Ringwald } 4193deb3ec6SMatthias Ringwald 420485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(uint16_t cid){ 4213deb3ec6SMatthias Ringwald char buffer[40]; 42289425bfcSMilanka Ringwald const int size = sizeof(buffer); 42389425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "\r\n%s:(", HFP_GENERIC_STATUS_INDICATOR); 42489425bfcSMilanka Ringwald offset += hfp_hf_indicators_join(buffer+offset, size-offset-10); 42589425bfcSMilanka Ringwald offset += snprintf(buffer+offset, size-offset, ")\r\n\r\nOK\r\n"); 4263deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4273deb3ec6SMatthias Ringwald } 4283deb3ec6SMatthias Ringwald 429485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid){ 4303deb3ec6SMatthias Ringwald char buffer[40]; 4311cc1d9e9SMilanka Ringwald int offset = hfp_hf_indicators_initial_status_join(buffer, sizeof(buffer) - 7); 4321cc1d9e9SMilanka Ringwald snprintf(buffer+offset, sizeof(buffer)-offset, "\r\nOK\r\n"); 4333deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4343deb3ec6SMatthias Ringwald } 4353deb3ec6SMatthias Ringwald 436485ac19eSMilanka Ringwald static int hfp_ag_send_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){ 4373deb3ec6SMatthias Ringwald char buffer[20]; 438aa4dd815SMatthias Ringwald sprintf(buffer, "\r\n%s:%d,%d\r\n", HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index, indicator->status); 4393deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4403deb3ec6SMatthias Ringwald } 4413deb3ec6SMatthias Ringwald 442485ac19eSMilanka Ringwald static int hfp_ag_send_report_network_operator_name_cmd(uint16_t cid, hfp_network_opearator_t op){ 4435e71456cSMilanka Ringwald char buffer[41]; 4443deb3ec6SMatthias Ringwald if (strlen(op.name) == 0){ 44589425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s:%d,,\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode); 4463deb3ec6SMatthias Ringwald } else { 44789425bfcSMilanka Ringwald int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:%d,%d,", HFP_QUERY_OPERATOR_SELECTION, op.mode, op.format); 4485e71456cSMilanka Ringwald offset += snprintf(buffer+offset, 16, "%s", op.name); 44989425bfcSMilanka Ringwald snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n"); 4503deb3ec6SMatthias Ringwald } 4513deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4523deb3ec6SMatthias Ringwald } 4533deb3ec6SMatthias Ringwald 4541cc1d9e9SMilanka Ringwald static inline int hfp_ag_send_cmd_with_int(uint16_t cid, const char * cmd, uint8_t value){ 4551cc1d9e9SMilanka Ringwald char buffer[30]; 45689425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n", cmd, value); 4571cc1d9e9SMilanka Ringwald return send_str_over_rfcomm(cid, buffer); 4581cc1d9e9SMilanka Ringwald } 4593deb3ec6SMatthias Ringwald 460485ac19eSMilanka Ringwald static int hfp_ag_send_suggest_codec_cmd(uint16_t cid, uint8_t codec){ 4611cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec); 4623deb3ec6SMatthias Ringwald } 4633deb3ec6SMatthias Ringwald 464485ac19eSMilanka Ringwald static int hfp_ag_send_activate_voice_recognition_cmd(uint16_t cid, uint8_t activate_voice_recognition){ 4651cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition); 466aa4dd815SMatthias Ringwald } 467aa4dd815SMatthias Ringwald 468485ac19eSMilanka Ringwald static int hfp_ag_send_set_speaker_gain_cmd(uint16_t cid, uint8_t gain){ 4691cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain); 470aa4dd815SMatthias Ringwald } 471aa4dd815SMatthias Ringwald 472485ac19eSMilanka Ringwald static int hfp_ag_send_set_microphone_gain_cmd(uint16_t cid, uint8_t gain){ 4731cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain); 474aa4dd815SMatthias Ringwald } 475aa4dd815SMatthias Ringwald 476485ac19eSMilanka Ringwald static int hfp_ag_send_set_response_and_hold(uint16_t cid, int state){ 4771cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_RESPONSE_AND_HOLD, state); 478ce263fc8SMatthias Ringwald } 479ce263fc8SMatthias Ringwald 480a0ffb263SMatthias Ringwald static uint8_t hfp_ag_suggest_codec(hfp_connection_t *hfp_connection){ 481bc1b1537SMilanka Ringwald if (hfp_connection->sco_for_msbc_failed) return HFP_CODEC_CVSD; 482bc1b1537SMilanka Ringwald 4836a7f44bdSMilanka Ringwald if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_codecs_nr, hfp_codecs)){ 4846a7f44bdSMilanka Ringwald if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){ 485df327ff3SMilanka Ringwald return HFP_CODEC_MSBC; 4863deb3ec6SMatthias Ringwald } 4873deb3ec6SMatthias Ringwald } 488df327ff3SMilanka Ringwald return HFP_CODEC_CVSD; 4893deb3ec6SMatthias Ringwald } 4903deb3ec6SMatthias Ringwald 49176cc1527SMatthias Ringwald /* state machines */ 49276cc1527SMatthias Ringwald 493afac2a04SMilanka Ringwald static uint8_t hfp_ag_esco_s4_supported(hfp_connection_t * hfp_connection){ 494afac2a04SMilanka Ringwald return (hfp_connection->remote_supported_features & (1<<HFP_HFSF_ESCO_S4)) && (hfp_supported_features & (1<<HFP_AGSF_ESCO_S4)); 4957522e673SMatthias Ringwald } 4967522e673SMatthias Ringwald 497a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){ 498aa4dd815SMatthias Ringwald /* events ( == commands): 499aa4dd815SMatthias Ringwald HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs 500aa4dd815SMatthias Ringwald HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 501aa4dd815SMatthias Ringwald hf_trigger_codec_connection_setup == received BCC 502aa4dd815SMatthias Ringwald ag_trigger_codec_connection_setup == received from AG to send BCS 503aa4dd815SMatthias Ringwald HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS 504aa4dd815SMatthias Ringwald */ 505aa4dd815SMatthias Ringwald 506a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 507aa4dd815SMatthias Ringwald case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 508a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC; 509aa4dd815SMatthias Ringwald break; 510aa4dd815SMatthias Ringwald case HFP_CODECS_AG_RESEND_COMMON_CODEC: 511a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC; 512aa4dd815SMatthias Ringwald break; 513aa4dd815SMatthias Ringwald default: 514aa4dd815SMatthias Ringwald break; 515aa4dd815SMatthias Ringwald } 516aa4dd815SMatthias Ringwald 517a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 518aa4dd815SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 519a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){ 520a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_LIST; 521485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 522aa4dd815SMatthias Ringwald return 1; 523aa4dd815SMatthias Ringwald } 524aa4dd815SMatthias Ringwald 525a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 526aa4dd815SMatthias Ringwald case HFP_CODECS_AG_SENT_COMMON_CODEC: 527aa4dd815SMatthias Ringwald case HFP_CODECS_EXCHANGED: 528a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_AG_RESEND_COMMON_CODEC; 529aa4dd815SMatthias Ringwald break; 530aa4dd815SMatthias Ringwald default: 531aa4dd815SMatthias Ringwald break; 532aa4dd815SMatthias Ringwald } 533485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 534aa4dd815SMatthias Ringwald return 1; 535aa4dd815SMatthias Ringwald 536aa4dd815SMatthias Ringwald case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 537a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 538485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 539aa4dd815SMatthias Ringwald return 1; 540aa4dd815SMatthias Ringwald 541aa4dd815SMatthias Ringwald case HFP_CMD_AG_SEND_COMMON_CODEC: 542a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_AG_SENT_COMMON_CODEC; 543a0ffb263SMatthias Ringwald hfp_connection->suggested_codec = hfp_ag_suggest_codec(hfp_connection); 544485ac19eSMilanka Ringwald hfp_ag_send_suggest_codec_cmd(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec); 545aa4dd815SMatthias Ringwald return 1; 546aa4dd815SMatthias Ringwald 547aa4dd815SMatthias Ringwald case HFP_CMD_HF_CONFIRMED_CODEC: 548a0ffb263SMatthias Ringwald if (hfp_connection->codec_confirmed != hfp_connection->suggested_codec){ 549a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_ERROR; 550485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 551aa4dd815SMatthias Ringwald return 1; 552aa4dd815SMatthias Ringwald } 553a0ffb263SMatthias Ringwald hfp_connection->negotiated_codec = hfp_connection->codec_confirmed; 554a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 555c1ab6cc1SMatthias Ringwald log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD"); 556485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 5577522e673SMatthias Ringwald // now, pick link settings 558afac2a04SMilanka Ringwald 559afac2a04SMilanka Ringwald hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection)); 560aa4dd815SMatthias Ringwald return 1; 561aa4dd815SMatthias Ringwald default: 562aa4dd815SMatthias Ringwald break; 563aa4dd815SMatthias Ringwald } 564aa4dd815SMatthias Ringwald return 0; 565aa4dd815SMatthias Ringwald } 566aa4dd815SMatthias Ringwald 567a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){ 568a0ffb263SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 569ca59be51SMatthias Ringwald hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr); 570aa4dd815SMatthias Ringwald 571a0ffb263SMatthias Ringwald // if active call exist, set per-hfp_connection state active, too (when audio is on) 572d0c20769SMatthias Ringwald if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 573a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE; 574aa4dd815SMatthias Ringwald } 575ce263fc8SMatthias Ringwald // if AG is ringing, also start ringing on the HF 576c1ab6cc1SMatthias Ringwald if ((hfp_gsm_call_status() == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS) && 577505f1c30SMatthias Ringwald (hfp_gsm_callsetup_status() == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS)){ 578a0ffb263SMatthias Ringwald hfp_ag_hf_start_ringing(hfp_connection); 579ce263fc8SMatthias Ringwald } 580aa4dd815SMatthias Ringwald } 5813deb3ec6SMatthias Ringwald 582a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){ 583c79b4cabSMatthias Ringwald // log_info("hfp_ag_run_for_context_service_level_connection state %u, command %u", hfp_connection->state, hfp_connection->command); 584a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 585c04cf7ffSMilanka Ringwald int sent = 0; 586a0ffb263SMatthias Ringwald switch(hfp_connection->command){ 5873deb3ec6SMatthias Ringwald case HFP_CMD_SUPPORTED_FEATURES: 588a0ffb263SMatthias Ringwald switch(hfp_connection->state){ 5893deb3ec6SMatthias Ringwald case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 590aa4dd815SMatthias Ringwald case HFP_EXCHANGE_SUPPORTED_FEATURES: 591d715cf51SMatthias Ringwald hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr); 592a0ffb263SMatthias Ringwald if (has_codec_negotiation_feature(hfp_connection)){ 593a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS; 594aa4dd815SMatthias Ringwald } else { 595a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 596aa4dd815SMatthias Ringwald } 597a0ffb263SMatthias Ringwald hfp_ag_exchange_supported_features_cmd(hfp_connection->rfcomm_cid); 598aa4dd815SMatthias Ringwald return 1; 5993deb3ec6SMatthias Ringwald default: 6003deb3ec6SMatthias Ringwald break; 6013deb3ec6SMatthias Ringwald } 6023deb3ec6SMatthias Ringwald break; 6033deb3ec6SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 604c04cf7ffSMilanka Ringwald sent = codecs_exchange_state_machine(hfp_connection); 605a0ffb263SMatthias Ringwald if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){ 606a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 6073deb3ec6SMatthias Ringwald } 608c04cf7ffSMilanka Ringwald return sent; 609aa4dd815SMatthias Ringwald 610aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 611a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_W4_RETRIEVE_INDICATORS) { 612473ac565SMatthias Ringwald // HF requested AG Indicators and we did expect it 61384a0c24eSMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0; 614473ac565SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 615473ac565SMatthias Ringwald // continue below in state switch 616ad902e3dSMatthias Ringwald } 617ad902e3dSMatthias Ringwald break; 618aa4dd815SMatthias Ringwald 619aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 620a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break; 621a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE; 622485ac19eSMilanka Ringwald hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid); 623aa4dd815SMatthias Ringwald return 1; 624aa4dd815SMatthias Ringwald 6253deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 626a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break; 627a0ffb263SMatthias Ringwald if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 628a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL; 629a0ffb263SMatthias Ringwald } else if (has_hf_indicators_feature(hfp_connection)){ 630a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 631aa4dd815SMatthias Ringwald } else { 632a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 6333deb3ec6SMatthias Ringwald } 6348a46ec40SMatthias Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 635aa4dd815SMatthias Ringwald return 1; 6363deb3ec6SMatthias Ringwald 637aa4dd815SMatthias Ringwald case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES: 638a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break; 639a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 640a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 641aa4dd815SMatthias Ringwald } 642485ac19eSMilanka Ringwald hfp_ag_send_retrieve_can_hold_call_cmd(hfp_connection->rfcomm_cid); 643a0ffb263SMatthias Ringwald if (!has_hf_indicators_feature(hfp_connection)){ 644a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 645ce263fc8SMatthias Ringwald } 646aa4dd815SMatthias Ringwald return 1; 647aa4dd815SMatthias Ringwald 648aa4dd815SMatthias Ringwald case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 649a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break; 650a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS; 651485ac19eSMilanka Ringwald hfp_ag_send_list_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 652aa4dd815SMatthias Ringwald return 1; 653aa4dd815SMatthias Ringwald 654aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 655a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break; 656a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 657485ac19eSMilanka Ringwald hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 658aa4dd815SMatthias Ringwald return 1; 659aa4dd815SMatthias Ringwald 660aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 661a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break; 662a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 663485ac19eSMilanka Ringwald hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 664aa4dd815SMatthias Ringwald return 1; 6653deb3ec6SMatthias Ringwald default: 6663deb3ec6SMatthias Ringwald break; 6673deb3ec6SMatthias Ringwald } 668473ac565SMatthias Ringwald 669473ac565SMatthias Ringwald switch (hfp_connection->state){ 670473ac565SMatthias Ringwald case HFP_RETRIEVE_INDICATORS: { 671485ac19eSMilanka Ringwald int next_segment = hfp_ag_send_retrieve_indicators_cmd_via_generator(hfp_connection->rfcomm_cid, hfp_connection, hfp_connection->send_ag_indicators_segment); 672473ac565SMatthias Ringwald int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection); 673473ac565SMatthias Ringwald log_info("HFP_CMD_RETRIEVE_AG_INDICATORS next segment %u, num_segments %u", next_segment, num_segments); 674473ac565SMatthias Ringwald if (next_segment < num_segments){ 675473ac565SMatthias Ringwald // prepare sending of next segment 676473ac565SMatthias Ringwald hfp_connection->send_ag_indicators_segment = next_segment; 677473ac565SMatthias Ringwald log_info("HFP_CMD_RETRIEVE_AG_INDICATORS more. command %u, next seg %u", hfp_connection->command, next_segment); 678473ac565SMatthias Ringwald } else { 679473ac565SMatthias Ringwald // done, go to next state 680473ac565SMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0; 681473ac565SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS; 682473ac565SMatthias Ringwald } 683473ac565SMatthias Ringwald return 1; 684473ac565SMatthias Ringwald } 685473ac565SMatthias Ringwald default: 686473ac565SMatthias Ringwald break; 687473ac565SMatthias Ringwald } 688c04cf7ffSMilanka Ringwald return 0; 6893deb3ec6SMatthias Ringwald } 6903deb3ec6SMatthias Ringwald 691a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){ 692c04cf7ffSMilanka Ringwald int sent = codecs_exchange_state_machine(hfp_connection); 693c04cf7ffSMilanka Ringwald if (sent) return 1; 694aa4dd815SMatthias Ringwald 695a0ffb263SMatthias Ringwald switch(hfp_connection->command){ 696aa4dd815SMatthias Ringwald case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION: 697a0ffb263SMatthias Ringwald hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition); 698485ac19eSMilanka Ringwald hfp_ag_send_activate_voice_recognition_cmd(hfp_connection->rfcomm_cid, hfp_connection->ag_activate_voice_recognition); 699aa4dd815SMatthias Ringwald return 1; 700aa4dd815SMatthias Ringwald case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION: 701aa4dd815SMatthias Ringwald if (get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)){ 702a0ffb263SMatthias Ringwald hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition); 703485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 704a0ffb263SMatthias Ringwald hfp_ag_setup_audio_connection(hfp_connection); 705aa4dd815SMatthias Ringwald } else { 706485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 707aa4dd815SMatthias Ringwald } 708aa4dd815SMatthias Ringwald return 1; 709aa4dd815SMatthias Ringwald case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING: 710485ac19eSMilanka Ringwald hfp_ag_send_change_in_band_ring_tone_setting_cmd(hfp_connection->rfcomm_cid); 711aa4dd815SMatthias Ringwald return 1; 712aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 713485ac19eSMilanka Ringwald hfp_ag_send_report_network_operator_name_cmd(hfp_connection->rfcomm_cid, hfp_connection->network_operator); 714aa4dd815SMatthias Ringwald return 1; 715aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 716a0ffb263SMatthias Ringwald if (hfp_connection->network_operator.format != 0){ 717485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 718aa4dd815SMatthias Ringwald } else { 719485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 7203deb3ec6SMatthias Ringwald } 721aa4dd815SMatthias Ringwald return 1; 722aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE: 723485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 724aa4dd815SMatthias Ringwald return 1; 7253deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR: 726a0ffb263SMatthias Ringwald if (hfp_connection->extended_audio_gateway_error){ 727a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 728485ac19eSMilanka Ringwald hfp_ag_send_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, hfp_connection->extended_audio_gateway_error_value); 729aa4dd815SMatthias Ringwald return 1; 7303deb3ec6SMatthias Ringwald } 731202c8a4cSMatthias Ringwald break; 7323deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 733485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 734ce263fc8SMatthias Ringwald return 1; 7353deb3ec6SMatthias Ringwald default: 7363deb3ec6SMatthias Ringwald break; 7373deb3ec6SMatthias Ringwald } 738aa4dd815SMatthias Ringwald return 0; 7393deb3ec6SMatthias Ringwald } 7403deb3ec6SMatthias Ringwald 741a0ffb263SMatthias Ringwald static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){ 742505f1c30SMatthias Ringwald if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) || 743505f1c30SMatthias Ringwald (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0; 7443deb3ec6SMatthias Ringwald 745aa4dd815SMatthias Ringwald 746505f1c30SMatthias Ringwald if ((hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) && hfp_connection->release_audio_connection){ 747a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 748a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 0; 749a0ffb263SMatthias Ringwald gap_disconnect(hfp_connection->sco_handle); 750aa4dd815SMatthias Ringwald return 1; 751aa4dd815SMatthias Ringwald } 752aa4dd815SMatthias Ringwald 753a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 754aa4dd815SMatthias Ringwald 755aa4dd815SMatthias Ringwald // run codecs exchange 756c04cf7ffSMilanka Ringwald int sent = codecs_exchange_state_machine(hfp_connection); 757c04cf7ffSMilanka Ringwald if (sent) return 1; 758aa4dd815SMatthias Ringwald 759c04cf7ffSMilanka Ringwald if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0; 760a0ffb263SMatthias Ringwald if (hfp_connection->establish_audio_connection){ 761a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 762a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 763eddcd308SMatthias Ringwald hfp_setup_synchronous_connection(hfp_connection); 764aa4dd815SMatthias Ringwald return 1; 765aa4dd815SMatthias Ringwald } 766aa4dd815SMatthias Ringwald return 0; 767aa4dd815SMatthias Ringwald } 768aa4dd815SMatthias Ringwald 769ec820d77SMatthias Ringwald static hfp_connection_t * hfp_ag_context_for_timer(btstack_timer_source_t * ts){ 770665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 771665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 772aa4dd815SMatthias Ringwald 773665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 774a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 775a0ffb263SMatthias Ringwald if ( & hfp_connection->hfp_timeout == ts) { 776a0ffb263SMatthias Ringwald return hfp_connection; 777aa4dd815SMatthias Ringwald } 778aa4dd815SMatthias Ringwald } 779aa4dd815SMatthias Ringwald return NULL; 780aa4dd815SMatthias Ringwald } 781aa4dd815SMatthias Ringwald 782ec820d77SMatthias Ringwald static void hfp_timeout_handler(btstack_timer_source_t * timer){ 783d63c37a1SMatthias Ringwald hfp_connection_t * hfp_connection = hfp_ag_context_for_timer(timer); 784d63c37a1SMatthias Ringwald if (!hfp_connection) return; 785aa4dd815SMatthias Ringwald 786d63c37a1SMatthias Ringwald log_info("HFP start ring timeout, con handle 0x%02x", hfp_connection->acl_handle); 787a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 1; 788a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled; 789aa4dd815SMatthias Ringwald 790a0ffb263SMatthias Ringwald btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout 791a0ffb263SMatthias Ringwald btstack_run_loop_add_timer(& hfp_connection->hfp_timeout); 792aa4dd815SMatthias Ringwald 793f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 794aa4dd815SMatthias Ringwald } 795aa4dd815SMatthias Ringwald 796a0ffb263SMatthias Ringwald static void hfp_timeout_start(hfp_connection_t * hfp_connection){ 797a0ffb263SMatthias Ringwald btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout); 798a0ffb263SMatthias Ringwald btstack_run_loop_set_timer_handler(& hfp_connection->hfp_timeout, hfp_timeout_handler); 799a0ffb263SMatthias Ringwald btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout 800a0ffb263SMatthias Ringwald btstack_run_loop_add_timer(& hfp_connection->hfp_timeout); 801aa4dd815SMatthias Ringwald } 802aa4dd815SMatthias Ringwald 803a0ffb263SMatthias Ringwald static void hfp_timeout_stop(hfp_connection_t * hfp_connection){ 804a0ffb263SMatthias Ringwald log_info("HFP stop ring timeout, con handle 0x%02x", hfp_connection->acl_handle); 805a0ffb263SMatthias Ringwald btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout); 806aa4dd815SMatthias Ringwald } 807aa4dd815SMatthias Ringwald 808aa4dd815SMatthias Ringwald // 809aa4dd815SMatthias Ringwald // transitition implementations for hfp_ag_call_state_machine 810aa4dd815SMatthias Ringwald // 811aa4dd815SMatthias Ringwald 812a0ffb263SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection){ 813aa4dd815SMatthias Ringwald if (use_in_band_tone()){ 814a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING; 815d97d752dSMilanka Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 816aa4dd815SMatthias Ringwald } else { 817a0ffb263SMatthias Ringwald hfp_timeout_start(hfp_connection); 818a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 1; 819a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled; 820a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_RINGING; 821ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGINIG); 822aa4dd815SMatthias Ringwald } 823aa4dd815SMatthias Ringwald } 824aa4dd815SMatthias Ringwald 825a0ffb263SMatthias Ringwald static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){ 826a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 0; 827a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = 0; 828a0ffb263SMatthias Ringwald hfp_timeout_stop(hfp_connection); 829ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGINIG); 830aa4dd815SMatthias Ringwald } 831aa4dd815SMatthias Ringwald 832aa4dd815SMatthias Ringwald static void hfp_ag_trigger_incoming_call(void){ 833aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name("callsetup"); 834aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 835aa4dd815SMatthias Ringwald 836665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 837665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 838665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 839a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 84066c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 841a0ffb263SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 842a0ffb263SMatthias Ringwald if (hfp_connection->call_state == HFP_CALL_IDLE){ 843a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 844d63c37a1SMatthias Ringwald hfp_ag_hf_start_ringing(hfp_connection); 845aa4dd815SMatthias Ringwald } 846a0ffb263SMatthias Ringwald if (hfp_connection->call_state == HFP_CALL_ACTIVE){ 847a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING; 848aa4dd815SMatthias Ringwald } 849f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 850aa4dd815SMatthias Ringwald } 851aa4dd815SMatthias Ringwald } 852aa4dd815SMatthias Ringwald 853aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callsetup_state(void){ 854aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name("callsetup"); 855aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 856aa4dd815SMatthias Ringwald 857665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 858665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 859665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 860a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 86166c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 862a0ffb263SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 863a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 864f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 865aa4dd815SMatthias Ringwald } 866aa4dd815SMatthias Ringwald } 867aa4dd815SMatthias Ringwald 868aa4dd815SMatthias Ringwald static void hfp_ag_transfer_call_state(void){ 869aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name("call"); 870aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 871aa4dd815SMatthias Ringwald 872665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 873665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 874665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 875a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 87666c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 877a0ffb263SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 878a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 879f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 880aa4dd815SMatthias Ringwald } 881aa4dd815SMatthias Ringwald } 882aa4dd815SMatthias Ringwald 883aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callheld_state(void){ 884aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name("callheld"); 885aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 886aa4dd815SMatthias Ringwald 887665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 888665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 889665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 890a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 89166c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 892a0ffb263SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 893a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 894f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 895aa4dd815SMatthias Ringwald } 896aa4dd815SMatthias Ringwald } 897aa4dd815SMatthias Ringwald 898aa4dd815SMatthias Ringwald static void hfp_ag_hf_accept_call(hfp_connection_t * source){ 899aa4dd815SMatthias Ringwald 900aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 901aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 902aa4dd815SMatthias Ringwald 903665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 904665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 905665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 906a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 90766c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 908505f1c30SMatthias Ringwald if ((hfp_connection->call_state != HFP_CALL_RINGING) && 909505f1c30SMatthias Ringwald (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue; 910aa4dd815SMatthias Ringwald 911a0ffb263SMatthias Ringwald hfp_ag_hf_stop_ringing(hfp_connection); 912a0ffb263SMatthias Ringwald if (hfp_connection == source){ 913aa4dd815SMatthias Ringwald 914aa4dd815SMatthias Ringwald if (use_in_band_tone()){ 915a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 916aa4dd815SMatthias Ringwald } else { 917a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE; 918d97d752dSMilanka Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 919aa4dd815SMatthias Ringwald } 920aa4dd815SMatthias Ringwald 921a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 922a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 923aa4dd815SMatthias Ringwald 924aa4dd815SMatthias Ringwald } else { 925a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 926aa4dd815SMatthias Ringwald } 927f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 928aa4dd815SMatthias Ringwald } 929aa4dd815SMatthias Ringwald } 930aa4dd815SMatthias Ringwald 931aa4dd815SMatthias Ringwald static void hfp_ag_ag_accept_call(void){ 932aa4dd815SMatthias Ringwald 933aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 934aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 935aa4dd815SMatthias Ringwald 936665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 937665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 938665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 939a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 94066c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 941a0ffb263SMatthias Ringwald if (hfp_connection->call_state != HFP_CALL_RINGING) continue; 942aa4dd815SMatthias Ringwald 943a0ffb263SMatthias Ringwald hfp_ag_hf_stop_ringing(hfp_connection); 944a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION; 945aa4dd815SMatthias Ringwald 946a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 947a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 948aa4dd815SMatthias Ringwald 949f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 950aa4dd815SMatthias Ringwald break; // only single 951aa4dd815SMatthias Ringwald } 952aa4dd815SMatthias Ringwald } 953aa4dd815SMatthias Ringwald 954aa4dd815SMatthias Ringwald static void hfp_ag_trigger_reject_call(void){ 955aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 956665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 957665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 958665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 959665d90f2SMatthias Ringwald hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 96066c5995fSMatthias Ringwald if (connection->local_role != HFP_ROLE_AG) continue; 961505f1c30SMatthias Ringwald if ((connection->call_state != HFP_CALL_RINGING) && 962505f1c30SMatthias Ringwald (connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue; 963aa4dd815SMatthias Ringwald hfp_ag_hf_stop_ringing(connection); 964aa4dd815SMatthias Ringwald connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 965aa4dd815SMatthias Ringwald connection->call_state = HFP_CALL_IDLE; 966f0aeb307SMatthias Ringwald hfp_ag_run_for_context(connection); 967aa4dd815SMatthias Ringwald } 968aa4dd815SMatthias Ringwald } 969aa4dd815SMatthias Ringwald 970aa4dd815SMatthias Ringwald static void hfp_ag_trigger_terminate_call(void){ 971aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 972aa4dd815SMatthias Ringwald 973665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 974665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 975665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 976a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 97766c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 978d63c37a1SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 979a0ffb263SMatthias Ringwald if (hfp_connection->call_state == HFP_CALL_IDLE) continue; 980a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 981a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 9826d78145cSMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 983a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 1; 9846d78145cSMatthias Ringwald } 985f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 986aa4dd815SMatthias Ringwald } 987ca59be51SMatthias Ringwald hfp_ag_emit_simple_event(HFP_SUBEVENT_CALL_TERMINATED); 988aa4dd815SMatthias Ringwald } 989aa4dd815SMatthias Ringwald 9900cb5b971SMatthias Ringwald static void hfp_ag_set_callsetup_indicator(void){ 991aa4dd815SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup"); 992aa4dd815SMatthias Ringwald if (!indicator){ 993d0c20769SMatthias Ringwald log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing"); 99445718b6fSMatthias Ringwald return; 995aa4dd815SMatthias Ringwald }; 996d0c20769SMatthias Ringwald indicator->status = hfp_gsm_callsetup_status(); 997aa4dd815SMatthias Ringwald } 998aa4dd815SMatthias Ringwald 9990cb5b971SMatthias Ringwald static void hfp_ag_set_callheld_indicator(void){ 1000d210d9c4SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld"); 1001d210d9c4SMatthias Ringwald if (!indicator){ 1002d210d9c4SMatthias Ringwald log_error("hfp_ag_set_callheld_state: callheld indicator is missing"); 100345718b6fSMatthias Ringwald return; 1004d210d9c4SMatthias Ringwald }; 1005d210d9c4SMatthias Ringwald indicator->status = hfp_gsm_callheld_status(); 1006d210d9c4SMatthias Ringwald } 1007d210d9c4SMatthias Ringwald 10080cb5b971SMatthias Ringwald static void hfp_ag_set_call_indicator(void){ 1009aa4dd815SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call"); 1010aa4dd815SMatthias Ringwald if (!indicator){ 1011aa4dd815SMatthias Ringwald log_error("hfp_ag_set_call_state: call indicator is missing"); 101245718b6fSMatthias Ringwald return; 1013aa4dd815SMatthias Ringwald }; 1014d210d9c4SMatthias Ringwald indicator->status = hfp_gsm_call_status(); 1015aa4dd815SMatthias Ringwald } 1016aa4dd815SMatthias Ringwald 1017aa4dd815SMatthias Ringwald static void hfp_ag_stop_ringing(void){ 1018665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1019665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1020665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1021a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 102266c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1023505f1c30SMatthias Ringwald if ((hfp_connection->call_state != HFP_CALL_RINGING) && 1024505f1c30SMatthias Ringwald (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue; 1025a0ffb263SMatthias Ringwald hfp_ag_hf_stop_ringing(hfp_connection); 1026aa4dd815SMatthias Ringwald } 1027aa4dd815SMatthias Ringwald } 1028aa4dd815SMatthias Ringwald 1029aa4dd815SMatthias Ringwald static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){ 1030665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1031665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1032665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1033a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 103466c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1035a0ffb263SMatthias Ringwald if (hfp_connection->call_state == call_state) return hfp_connection; 1036aa4dd815SMatthias Ringwald } 1037aa4dd815SMatthias Ringwald return NULL; 1038aa4dd815SMatthias Ringwald } 1039aa4dd815SMatthias Ringwald 1040a5bdcda8SMatthias Ringwald static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){ 1041665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1042665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1043665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1044a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 104566c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1046a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = state + 1; 1047ce263fc8SMatthias Ringwald } 1048ce263fc8SMatthias Ringwald } 1049ce263fc8SMatthias Ringwald 1050a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 1051aa4dd815SMatthias Ringwald int indicator_index; 1052a0ffb263SMatthias Ringwald switch (hfp_connection->call_state){ 1053aa4dd815SMatthias Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING: 1054a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 1055a0ffb263SMatthias Ringwald // we got event: audio hfp_connection established 1056a0ffb263SMatthias Ringwald hfp_timeout_start(hfp_connection); 1057a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 1; 1058a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled; 1059a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_RINGING; 1060ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGINIG); 1061aa4dd815SMatthias Ringwald break; 1062aa4dd815SMatthias Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE: 1063a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 1064a0ffb263SMatthias Ringwald // we got event: audio hfp_connection established 1065a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1066aa4dd815SMatthias Ringwald break; 1067aa4dd815SMatthias Ringwald case HFP_CALL_W2_SEND_CALL_WAITING: 1068a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_CHLD; 1069a0ffb263SMatthias Ringwald hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid); 1070aa4dd815SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callsetup"); 1071a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1072aa4dd815SMatthias Ringwald break; 1073aa4dd815SMatthias Ringwald default: 1074aa4dd815SMatthias Ringwald break; 1075aa4dd815SMatthias Ringwald } 1076aa4dd815SMatthias Ringwald return 0; 1077aa4dd815SMatthias Ringwald } 107839426febSMatthias Ringwald 107939426febSMatthias Ringwald // functions extracted from hfp_ag_call_sm below 108039426febSMatthias Ringwald static void hfp_ag_handle_reject_incoming_call(void){ 108139426febSMatthias Ringwald hfp_connection_t * hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 108239426febSMatthias Ringwald if (!hfp_connection){ 108339426febSMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state"); 108439426febSMatthias Ringwald return; 108539426febSMatthias Ringwald } 108639426febSMatthias Ringwald 1087f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_REJECTED, 0, 0, NULL); 108839426febSMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 108939426febSMatthias Ringwald hfp_connection->send_error = 1; 109039426febSMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 109139426febSMatthias Ringwald } 109239426febSMatthias Ringwald 1093a0ffb263SMatthias Ringwald // hfp_connection is used to identify originating HF 1094a0ffb263SMatthias Ringwald static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){ 1095aa4dd815SMatthias Ringwald int indicator_index; 1096d210d9c4SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1097d210d9c4SMatthias Ringwald int callheld_indicator_index = get_ag_indicator_index_for_name("callheld"); 1098d210d9c4SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 109974386ee0SMatthias Ringwald 1100aa4dd815SMatthias Ringwald switch (event){ 1101aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL: 1102d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1103aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1104d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1105*12cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1106f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL, 0, 0, NULL); 1107d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1108aa4dd815SMatthias Ringwald hfp_ag_trigger_incoming_call(); 110960ebb071SMilanka Ringwald log_info("AG rings"); 1110aa4dd815SMatthias Ringwald break; 1111aa4dd815SMatthias Ringwald default: 11123deb3ec6SMatthias Ringwald break; 11133deb3ec6SMatthias Ringwald } 11143deb3ec6SMatthias Ringwald break; 1115aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1116d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1117*12cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1118f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL, 0, 0, NULL); 1119d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1120aa4dd815SMatthias Ringwald hfp_ag_trigger_incoming_call(); 112160ebb071SMilanka Ringwald log_info("AG call waiting"); 1122aa4dd815SMatthias Ringwald break; 1123aa4dd815SMatthias Ringwald default: 11243deb3ec6SMatthias Ringwald break; 11253deb3ec6SMatthias Ringwald } 11263deb3ec6SMatthias Ringwald break; 11277bbeb3adSMilanka Ringwald default: 11287bbeb3adSMilanka Ringwald break; 1129aa4dd815SMatthias Ringwald } 1130aa4dd815SMatthias Ringwald break; 1131aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG: 1132d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1133aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1134d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1135*12cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1136f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, 0, 0, NULL); 1137d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1138d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1139aa4dd815SMatthias Ringwald hfp_ag_ag_accept_call(); 114060ebb071SMilanka Ringwald log_info("AG answers call, accept call by GSM"); 1141aa4dd815SMatthias Ringwald break; 1142aa4dd815SMatthias Ringwald default: 11433deb3ec6SMatthias Ringwald break; 11443deb3ec6SMatthias Ringwald } 1145aa4dd815SMatthias Ringwald break; 1146aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1147d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1148aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 114960ebb071SMilanka Ringwald log_info("AG: current call is placed on hold, incoming call gets active"); 1150f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, 0, 0, NULL); 1151d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1152d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1153ce263fc8SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1154ce263fc8SMatthias Ringwald hfp_ag_transfer_callheld_state(); 1155aa4dd815SMatthias Ringwald break; 1156aa4dd815SMatthias Ringwald default: 1157aa4dd815SMatthias Ringwald break; 1158aa4dd815SMatthias Ringwald } 1159aa4dd815SMatthias Ringwald break; 11607bbeb3adSMilanka Ringwald default: 11617bbeb3adSMilanka Ringwald break; 1162aa4dd815SMatthias Ringwald } 1163aa4dd815SMatthias Ringwald break; 1164ce263fc8SMatthias Ringwald 1165ce263fc8SMatthias Ringwald case HFP_AG_HELD_CALL_JOINED_BY_AG: 1166d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1167ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1168d0c20769SMatthias Ringwald switch (hfp_gsm_callheld_status()){ 1169ce263fc8SMatthias Ringwald case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED: 117060ebb071SMilanka Ringwald log_info("AG: joining held call with active call"); 1171f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_HELD_CALL_JOINED_BY_AG, 0, 0, NULL); 1172d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1173ce263fc8SMatthias Ringwald hfp_ag_transfer_callheld_state(); 1174ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL); 1175ce263fc8SMatthias Ringwald break; 1176ce263fc8SMatthias Ringwald default: 1177ce263fc8SMatthias Ringwald break; 1178ce263fc8SMatthias Ringwald } 1179ce263fc8SMatthias Ringwald break; 1180ce263fc8SMatthias Ringwald default: 1181ce263fc8SMatthias Ringwald break; 1182ce263fc8SMatthias Ringwald } 1183ce263fc8SMatthias Ringwald break; 1184ce263fc8SMatthias Ringwald 1185aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF: 1186d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1187aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1188d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1189*12cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1190f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, 0, 0, NULL); 1191d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1192d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1193a0ffb263SMatthias Ringwald hfp_ag_hf_accept_call(hfp_connection); 11946323f788SMatthias Ringwald hfp_connection->ok_pending = 1; 119560ebb071SMilanka Ringwald log_info("HF answers call, accept call by GSM"); 1196ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED); 1197aa4dd815SMatthias Ringwald break; 1198aa4dd815SMatthias Ringwald default: 1199aa4dd815SMatthias Ringwald break; 1200aa4dd815SMatthias Ringwald } 12013deb3ec6SMatthias Ringwald break; 12023deb3ec6SMatthias Ringwald default: 12033deb3ec6SMatthias Ringwald break; 12043deb3ec6SMatthias Ringwald } 12053deb3ec6SMatthias Ringwald break; 12063deb3ec6SMatthias Ringwald 1207ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG: 1208d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1209ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1210d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1211*12cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1212f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, 0, 0, NULL); 1213ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_active = 1; 1214ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1215a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1216ce263fc8SMatthias Ringwald // as with regualr call 1217d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1218d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1219ce263fc8SMatthias Ringwald hfp_ag_ag_accept_call(); 122060ebb071SMilanka Ringwald log_info("AG response and hold - hold by AG"); 1221ce263fc8SMatthias Ringwald break; 1222ce263fc8SMatthias Ringwald default: 1223ce263fc8SMatthias Ringwald break; 1224ce263fc8SMatthias Ringwald } 1225ce263fc8SMatthias Ringwald break; 1226ce263fc8SMatthias Ringwald default: 1227ce263fc8SMatthias Ringwald break; 1228ce263fc8SMatthias Ringwald } 1229ce263fc8SMatthias Ringwald break; 1230ce263fc8SMatthias Ringwald 1231ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF: 1232d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1233ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1234d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1235*12cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1236f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, 0, 0, NULL); 1237ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_active = 1; 1238ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1239a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1240ce263fc8SMatthias Ringwald // as with regualr call 1241d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1242d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1243a0ffb263SMatthias Ringwald hfp_ag_hf_accept_call(hfp_connection); 124460ebb071SMilanka Ringwald log_info("AG response and hold - hold by HF"); 1245ce263fc8SMatthias Ringwald break; 1246ce263fc8SMatthias Ringwald default: 1247ce263fc8SMatthias Ringwald break; 1248ce263fc8SMatthias Ringwald } 1249ce263fc8SMatthias Ringwald break; 1250ce263fc8SMatthias Ringwald default: 1251ce263fc8SMatthias Ringwald break; 1252ce263fc8SMatthias Ringwald } 1253ce263fc8SMatthias Ringwald break; 1254ce263fc8SMatthias Ringwald 1255ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG: 1256ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF: 1257ce263fc8SMatthias Ringwald if (!hfp_ag_response_and_hold_active) break; 1258ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1259f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, 0, 0, NULL); 1260a5bdcda8SMatthias Ringwald hfp_ag_response_and_hold_active = 0; 1261ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED; 1262a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 126360ebb071SMilanka Ringwald log_info("Held Call accepted and active"); 1264ce263fc8SMatthias Ringwald break; 1265ce263fc8SMatthias Ringwald 1266ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG: 1267ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF: 1268ce263fc8SMatthias Ringwald if (!hfp_ag_response_and_hold_active) break; 1269ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1270f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, 0, 0, NULL); 1271a5bdcda8SMatthias Ringwald hfp_ag_response_and_hold_active = 0; 1272ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1273a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1274ce263fc8SMatthias Ringwald // from terminate by ag 1275d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1276ce263fc8SMatthias Ringwald hfp_ag_trigger_terminate_call(); 1277ce263fc8SMatthias Ringwald break; 1278ce263fc8SMatthias Ringwald 1279aa4dd815SMatthias Ringwald case HFP_AG_TERMINATE_CALL_BY_HF: 1280d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1281aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1282d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1283*12cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1284f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL); 1285d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1286aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1287aa4dd815SMatthias Ringwald hfp_ag_trigger_reject_call(); 128860ebb071SMilanka Ringwald log_info("HF Rejected Incoming call, AG terminate call"); 1289aa4dd815SMatthias Ringwald break; 1290aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 1291*12cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1292f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL); 1293d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1294aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 129560ebb071SMilanka Ringwald log_info("AG terminate outgoing call process"); 1296202c8a4cSMatthias Ringwald break; 1297aa4dd815SMatthias Ringwald default: 1298aa4dd815SMatthias Ringwald break; 1299aa4dd815SMatthias Ringwald } 1300aa4dd815SMatthias Ringwald break; 1301*12cbbeeeSMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1302f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL); 1303d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1304aa4dd815SMatthias Ringwald hfp_ag_transfer_call_state(); 1305a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 130660ebb071SMilanka Ringwald log_info("AG terminate call"); 1307aa4dd815SMatthias Ringwald break; 13087bbeb3adSMilanka Ringwald default: 13097bbeb3adSMilanka Ringwald break; 1310aa4dd815SMatthias Ringwald } 13110c1b8529SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED); 1312aa4dd815SMatthias Ringwald break; 13133deb3ec6SMatthias Ringwald 1314aa4dd815SMatthias Ringwald case HFP_AG_TERMINATE_CALL_BY_AG: 1315d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1316aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1317d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1318*12cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1319f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_AG, 0, 0, NULL); 1320d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1321aa4dd815SMatthias Ringwald hfp_ag_trigger_reject_call(); 132260ebb071SMilanka Ringwald log_info("AG Rejected Incoming call, AG terminate call"); 1323aa4dd815SMatthias Ringwald break; 1324aa4dd815SMatthias Ringwald default: 1325aa4dd815SMatthias Ringwald break; 13263deb3ec6SMatthias Ringwald } 1327202c8a4cSMatthias Ringwald break; 1328*12cbbeeeSMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1329f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_AG, 0, 0, NULL); 1330d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1331d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1332aa4dd815SMatthias Ringwald hfp_ag_trigger_terminate_call(); 133360ebb071SMilanka Ringwald log_info("AG terminate call"); 1334aa4dd815SMatthias Ringwald break; 1335aa4dd815SMatthias Ringwald default: 13363deb3ec6SMatthias Ringwald break; 13373deb3ec6SMatthias Ringwald } 13383deb3ec6SMatthias Ringwald break; 1339aa4dd815SMatthias Ringwald case HFP_AG_CALL_DROPPED: 1340d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1341aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1342d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1343aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1344aa4dd815SMatthias Ringwald hfp_ag_stop_ringing(); 134560ebb071SMilanka Ringwald log_info("Incoming call interrupted"); 1346aa4dd815SMatthias Ringwald break; 1347aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 1348aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 134960ebb071SMilanka Ringwald log_info("Outgoing call interrupted\n"); 135060ebb071SMilanka Ringwald log_info("AG notify call dropped\n"); 1351aa4dd815SMatthias Ringwald break; 1352aa4dd815SMatthias Ringwald default: 13533deb3ec6SMatthias Ringwald break; 13543deb3ec6SMatthias Ringwald } 1355f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL); 1356d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1357aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 135810c236bfSMatthias Ringwald hfp_ag_trigger_terminate_call(); 1359aa4dd815SMatthias Ringwald break; 1360aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1361ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_active) { 1362f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL); 1363ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1364a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1365ce263fc8SMatthias Ringwald } 1366f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL); 1367d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1368d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1369aa4dd815SMatthias Ringwald hfp_ag_trigger_terminate_call(); 137060ebb071SMilanka Ringwald log_info("AG notify call dropped"); 1371aa4dd815SMatthias Ringwald break; 1372aa4dd815SMatthias Ringwald default: 1373aa4dd815SMatthias Ringwald break; 1374aa4dd815SMatthias Ringwald } 1375aa4dd815SMatthias Ringwald break; 1376aa4dd815SMatthias Ringwald 1377aa4dd815SMatthias Ringwald case HFP_AG_OUTGOING_CALL_INITIATED: 1378d210d9c4SMatthias Ringwald // directly reject call if number of free slots is exceeded 1379d210d9c4SMatthias Ringwald if (!hfp_gsm_call_possible()){ 1380a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1381f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1382d210d9c4SMatthias Ringwald break; 1383d210d9c4SMatthias Ringwald } 1384f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_INITIATED, 0, 0, (const char *) &hfp_connection->line_buffer[3]); 13859cae807eSMatthias Ringwald 1386a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 138774386ee0SMatthias Ringwald 1388ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]); 1389aa4dd815SMatthias Ringwald break; 1390aa4dd815SMatthias Ringwald 13919cae807eSMatthias Ringwald case HFP_AG_OUTGOING_REDIAL_INITIATED:{ 1392d210d9c4SMatthias Ringwald // directly reject call if number of free slots is exceeded 1393d210d9c4SMatthias Ringwald if (!hfp_gsm_call_possible()){ 1394a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1395f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 1396d210d9c4SMatthias Ringwald break; 1397d210d9c4SMatthias Ringwald } 1398d210d9c4SMatthias Ringwald 1399f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_REDIAL_INITIATED, 0, 0, NULL); 1400a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 140174386ee0SMatthias Ringwald 140260ebb071SMilanka Ringwald log_info("Redial last number"); 14039cae807eSMatthias Ringwald char * last_dialed_number = hfp_gsm_last_dialed_number(); 1404aa4dd815SMatthias Ringwald 14059cae807eSMatthias Ringwald if (strlen(last_dialed_number) > 0){ 140660ebb071SMilanka Ringwald log_info("Last number exists: accept call"); 1407ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number); 14089cae807eSMatthias Ringwald } else { 140960ebb071SMilanka Ringwald log_info("log_infoLast number missing: reject call"); 141039426febSMatthias Ringwald hfp_ag_handle_reject_incoming_call(); 14119cae807eSMatthias Ringwald } 14129cae807eSMatthias Ringwald break; 14139cae807eSMatthias Ringwald } 1414aa4dd815SMatthias Ringwald case HFP_AG_OUTGOING_CALL_REJECTED: 141539426febSMatthias Ringwald hfp_ag_handle_reject_incoming_call(); 1416aa4dd815SMatthias Ringwald break; 1417aa4dd815SMatthias Ringwald 1418d210d9c4SMatthias Ringwald case HFP_AG_OUTGOING_CALL_ACCEPTED:{ 1419a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 1420a0ffb263SMatthias Ringwald if (!hfp_connection){ 1421a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state"); 1422aa4dd815SMatthias Ringwald break; 1423aa4dd815SMatthias Ringwald } 1424aa4dd815SMatthias Ringwald 1425a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1426a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING; 1427aa4dd815SMatthias Ringwald 1428aa4dd815SMatthias Ringwald // trigger callsetup to be 1429d0c20769SMatthias Ringwald int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT; 1430f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_ACCEPTED, 0, 0, NULL); 1431d210d9c4SMatthias Ringwald 1432d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1433aa4dd815SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callsetup"); 1434a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1435aa4dd815SMatthias Ringwald 1436aa4dd815SMatthias Ringwald // put current call on hold if active 1437d210d9c4SMatthias Ringwald if (put_call_on_hold){ 143860ebb071SMilanka Ringwald log_info("AG putting current call on hold for new outgoing calllog_info"); 1439d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1440aa4dd815SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callheld"); 1441485ac19eSMilanka Ringwald hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]); 1442aa4dd815SMatthias Ringwald } 1443aa4dd815SMatthias Ringwald 1444aa4dd815SMatthias Ringwald // start audio if needed 1445d97d752dSMilanka Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 1446aa4dd815SMatthias Ringwald break; 1447d210d9c4SMatthias Ringwald } 1448aa4dd815SMatthias Ringwald case HFP_AG_OUTGOING_CALL_RINGING: 1449a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1450a0ffb263SMatthias Ringwald if (!hfp_connection){ 1451a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state"); 1452aa4dd815SMatthias Ringwald break; 1453aa4dd815SMatthias Ringwald } 1454d210d9c4SMatthias Ringwald 1455f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_RINGING, 0, 0, NULL); 1456a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING; 1457d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1458aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1459aa4dd815SMatthias Ringwald break; 1460aa4dd815SMatthias Ringwald 1461d210d9c4SMatthias Ringwald case HFP_AG_OUTGOING_CALL_ESTABLISHED:{ 1462aa4dd815SMatthias Ringwald // get outgoing call 1463a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING); 1464a0ffb263SMatthias Ringwald if (!hfp_connection){ 1465a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1466aa4dd815SMatthias Ringwald } 1467a0ffb263SMatthias Ringwald if (!hfp_connection){ 1468a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection"); 1469aa4dd815SMatthias Ringwald break; 1470aa4dd815SMatthias Ringwald } 1471d210d9c4SMatthias Ringwald 1472d0c20769SMatthias Ringwald int CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS = hfp_gsm_callheld_status() == HFP_CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS; 1473f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_ESTABLISHED, 0, 0, NULL); 1474a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1475d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1476d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1477aa4dd815SMatthias Ringwald hfp_ag_transfer_call_state(); 1478aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1479d210d9c4SMatthias Ringwald if (CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS){ 1480d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1481aa4dd815SMatthias Ringwald hfp_ag_transfer_callheld_state(); 14823deb3ec6SMatthias Ringwald } 14833deb3ec6SMatthias Ringwald break; 1484d210d9c4SMatthias Ringwald } 1485d210d9c4SMatthias Ringwald 1486*12cbbeeeSMatthias Ringwald case HFP_AG_CALL_HOLD_USER_BUSY: 1487f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_USER_BUSY, 0, 0, NULL); 1488d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1489a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1490a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 149160ebb071SMilanka Ringwald log_info("AG: Call Waiting, User Busy"); 1492d210d9c4SMatthias Ringwald break; 1493d210d9c4SMatthias Ringwald 1494d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{ 1495d0c20769SMatthias Ringwald int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1496d0c20769SMatthias Ringwald int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD; 1497d210d9c4SMatthias Ringwald 1498d210d9c4SMatthias Ringwald // Releases all active calls (if any exist) and accepts the other (held or waiting) call. 1499d0c20769SMatthias Ringwald if (call_held || call_setup_in_progress){ 1500f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index, 1501f8737b81SMatthias Ringwald 0, NULL); 1502d0c20769SMatthias Ringwald 1503d0c20769SMatthias Ringwald } 1504d0c20769SMatthias Ringwald 1505d210d9c4SMatthias Ringwald if (call_setup_in_progress){ 150660ebb071SMilanka Ringwald log_info("AG: Call Dropped, Accept new call"); 1507d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1508a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1509d210d9c4SMatthias Ringwald } else { 151060ebb071SMilanka Ringwald log_info("AG: Call Dropped, Resume held call"); 1511d210d9c4SMatthias Ringwald } 1512d210d9c4SMatthias Ringwald if (call_held){ 1513d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1514a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1515d210d9c4SMatthias Ringwald } 1516d0c20769SMatthias Ringwald 1517a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1518d210d9c4SMatthias Ringwald break; 1519d210d9c4SMatthias Ringwald } 1520d0c20769SMatthias Ringwald 1521d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{ 1522d210d9c4SMatthias Ringwald // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call. 1523d210d9c4SMatthias Ringwald // only update if callsetup changed 1524d0c20769SMatthias Ringwald int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1525f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index, 0, 1526f8737b81SMatthias Ringwald NULL); 1527d0c20769SMatthias Ringwald 1528d210d9c4SMatthias Ringwald if (call_setup_in_progress){ 152960ebb071SMilanka Ringwald log_info("AG: Call on Hold, Accept new call"); 1530d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1531a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1532d210d9c4SMatthias Ringwald } else { 153360ebb071SMilanka Ringwald log_info("AG: Swap calls"); 1534d210d9c4SMatthias Ringwald } 1535d0c20769SMatthias Ringwald 1536d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1537d0c20769SMatthias Ringwald // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED); 1538a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1539a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1540d210d9c4SMatthias Ringwald break; 1541d210d9c4SMatthias Ringwald } 1542d0c20769SMatthias Ringwald 1543d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_ADD_HELD_CALL: 1544d210d9c4SMatthias Ringwald // Adds a held call to the conversation. 1545d0c20769SMatthias Ringwald if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){ 154660ebb071SMilanka Ringwald log_info("AG: Join 3-way-call"); 1547f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_ADD_HELD_CALL, 0, 0, NULL); 1548d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1549a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1550ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL); 1551d210d9c4SMatthias Ringwald } 1552a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1553d210d9c4SMatthias Ringwald break; 1554d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS: 1555d210d9c4SMatthias Ringwald // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer) 1556f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, 0, 0, NULL); 155760ebb071SMilanka Ringwald log_info("AG: Transfer call -> Connect two calls and disconnect"); 1558d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1559d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1560a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1561a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1562a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 1563d210d9c4SMatthias Ringwald break; 1564ce263fc8SMatthias Ringwald 15653deb3ec6SMatthias Ringwald default: 15663deb3ec6SMatthias Ringwald break; 15673deb3ec6SMatthias Ringwald } 1568d210d9c4SMatthias Ringwald 1569d0c20769SMatthias Ringwald 15703deb3ec6SMatthias Ringwald } 15713deb3ec6SMatthias Ringwald 15729cae807eSMatthias Ringwald 1573a0ffb263SMatthias Ringwald static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){ 15749cae807eSMatthias Ringwald hfp_gsm_call_t * active_call = hfp_gsm_call(call_index); 15759cae807eSMatthias Ringwald if (!active_call) return; 15769cae807eSMatthias Ringwald 15779cae807eSMatthias Ringwald int idx = active_call->index; 15789cae807eSMatthias Ringwald hfp_enhanced_call_dir_t dir = active_call->direction; 15799cae807eSMatthias Ringwald hfp_enhanced_call_status_t status = active_call->enhanced_status; 15809cae807eSMatthias Ringwald hfp_enhanced_call_mode_t mode = active_call->mode; 15819cae807eSMatthias Ringwald hfp_enhanced_call_mpty_t mpty = active_call->mpty; 15829cae807eSMatthias Ringwald uint8_t type = active_call->clip_type; 15839cae807eSMatthias Ringwald char * number = active_call->clip_number; 15849cae807eSMatthias Ringwald 15859cae807eSMatthias Ringwald char buffer[100]; 15869cae807eSMatthias Ringwald // TODO: check length of a buffer, to fit the MTU 15879cae807eSMatthias Ringwald int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty); 15889cae807eSMatthias Ringwald if (number){ 15891cc1d9e9SMilanka Ringwald offset += snprintf(buffer+offset, sizeof(buffer)-offset-3, ", \"%s\",%u", number, type); 15909cae807eSMatthias Ringwald } 15919cae807eSMatthias Ringwald snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 159260ebb071SMilanka 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, 15939cae807eSMatthias Ringwald mode, mpty, type, number); 1594a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 15959cae807eSMatthias Ringwald } 15969cae807eSMatthias Ringwald 1597473ac565SMatthias Ringwald 1598febc14f5SMatthias Ringwald // sends pending command, returns if command was sent 1599febc14f5SMatthias Ringwald static int hfp_ag_send_commands(hfp_connection_t *hfp_connection){ 160022387625SMatthias Ringwald 1601a0ffb263SMatthias Ringwald if (hfp_connection->send_status_of_current_calls){ 1602a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 1603a0ffb263SMatthias Ringwald if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){ 1604a0ffb263SMatthias Ringwald hfp_connection->next_call_index++; 1605a0ffb263SMatthias Ringwald hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index); 1606febc14f5SMatthias Ringwald return 1; 16079cae807eSMatthias Ringwald } else { 1608febc14f5SMatthias 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 1609a0ffb263SMatthias Ringwald hfp_connection->next_call_index = 0; 1610a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1611a0ffb263SMatthias Ringwald hfp_connection->send_status_of_current_calls = 0; 16129cae807eSMatthias Ringwald } 1613ce263fc8SMatthias Ringwald } 1614ce263fc8SMatthias Ringwald 1615a0ffb263SMatthias Ringwald if (hfp_connection->ag_notify_incoming_call_waiting){ 1616a0ffb263SMatthias Ringwald hfp_connection->ag_notify_incoming_call_waiting = 0; 1617a0ffb263SMatthias Ringwald hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid); 1618febc14f5SMatthias Ringwald return 1; 1619aa4dd815SMatthias Ringwald } 1620aa4dd815SMatthias Ringwald 1621a0ffb263SMatthias Ringwald if (hfp_connection->command == HFP_CMD_UNKNOWN){ 1622a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 1623a0ffb263SMatthias Ringwald hfp_connection->send_error = 0; 1624a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1625485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 1626febc14f5SMatthias Ringwald return 1; 1627a0ffb263SMatthias Ringwald } 1628a0ffb263SMatthias Ringwald 1629a0ffb263SMatthias Ringwald if (hfp_connection->send_error){ 1630a0ffb263SMatthias Ringwald hfp_connection->send_error = 0; 1631a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1632485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 1633febc14f5SMatthias Ringwald return 1; 1634aa4dd815SMatthias Ringwald } 1635aa4dd815SMatthias Ringwald 1636ce263fc8SMatthias Ringwald // note: before update AG indicators and ok_pending 1637a0ffb263SMatthias Ringwald if (hfp_connection->send_response_and_hold_status){ 1638a0ffb263SMatthias Ringwald int status = hfp_connection->send_response_and_hold_status - 1; 1639a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = 0; 1640485ac19eSMilanka Ringwald hfp_ag_send_set_response_and_hold(hfp_connection->rfcomm_cid, status); 1641febc14f5SMatthias Ringwald return 1; 1642ce263fc8SMatthias Ringwald } 1643ce263fc8SMatthias Ringwald 1644a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending){ 1645a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 1646a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1647485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1648febc14f5SMatthias Ringwald return 1; 1649ce263fc8SMatthias Ringwald } 1650ce263fc8SMatthias Ringwald 1651aa4dd815SMatthias Ringwald // update AG indicators 1652a0ffb263SMatthias Ringwald if (hfp_connection->ag_indicators_status_update_bitmap){ 1653aa4dd815SMatthias Ringwald int i; 1654a0ffb263SMatthias Ringwald for (i=0;i<hfp_connection->ag_indicators_nr;i++){ 1655a0ffb263SMatthias Ringwald if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){ 1656a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0); 1657a0ffb263SMatthias Ringwald if (!hfp_connection->enable_status_update_for_ag_indicators) { 1658ce263fc8SMatthias Ringwald log_info("+CMER:3,0,0,0 - not sending update for '%s'", hfp_ag_indicators[i].name); 1659ce263fc8SMatthias Ringwald break; 1660ce263fc8SMatthias Ringwald } 1661485ac19eSMilanka Ringwald hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]); 1662febc14f5SMatthias Ringwald return 1; 1663aa4dd815SMatthias Ringwald } 1664aa4dd815SMatthias Ringwald } 1665aa4dd815SMatthias Ringwald } 1666aa4dd815SMatthias Ringwald 1667a0ffb263SMatthias Ringwald if (hfp_connection->ag_ring){ 1668a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 0; 1669a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1670485ac19eSMilanka Ringwald hfp_ag_send_ring(hfp_connection->rfcomm_cid); 1671febc14f5SMatthias Ringwald return 1; 1672aa4dd815SMatthias Ringwald } 1673aa4dd815SMatthias Ringwald 1674a0ffb263SMatthias Ringwald if (hfp_connection->ag_send_clip){ 1675a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = 0; 1676a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1677a0ffb263SMatthias Ringwald hfp_ag_send_clip(hfp_connection->rfcomm_cid); 1678febc14f5SMatthias Ringwald return 1; 1679aa4dd815SMatthias Ringwald } 1680aa4dd815SMatthias Ringwald 1681a0ffb263SMatthias Ringwald if (hfp_connection->send_phone_number_for_voice_tag){ 1682a0ffb263SMatthias Ringwald hfp_connection->send_phone_number_for_voice_tag = 0; 1683a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1684a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1685a0ffb263SMatthias Ringwald hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid); 1686febc14f5SMatthias Ringwald return 1; 1687aa4dd815SMatthias Ringwald } 1688aa4dd815SMatthias Ringwald 1689a0ffb263SMatthias Ringwald if (hfp_connection->send_subscriber_number){ 1690a0ffb263SMatthias Ringwald if (hfp_connection->next_subscriber_number_to_send < subscriber_numbers_count){ 1691a0ffb263SMatthias Ringwald hfp_phone_number_t phone = subscriber_numbers[hfp_connection->next_subscriber_number_to_send++]; 1692a0ffb263SMatthias Ringwald hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number); 1693ce263fc8SMatthias Ringwald } else { 1694a0ffb263SMatthias Ringwald hfp_connection->send_subscriber_number = 0; 1695a0ffb263SMatthias Ringwald hfp_connection->next_subscriber_number_to_send = 0; 1696485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1697ce263fc8SMatthias Ringwald } 1698a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1699febc14f5SMatthias Ringwald return 1; 1700ce263fc8SMatthias Ringwald } 1701ce263fc8SMatthias Ringwald 1702a0ffb263SMatthias Ringwald if (hfp_connection->send_microphone_gain){ 1703a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 0; 1704a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1705485ac19eSMilanka Ringwald hfp_ag_send_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 1706febc14f5SMatthias Ringwald return 1; 1707aa4dd815SMatthias Ringwald } 1708aa4dd815SMatthias Ringwald 1709a0ffb263SMatthias Ringwald if (hfp_connection->send_speaker_gain){ 1710a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 0; 1711a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1712485ac19eSMilanka Ringwald hfp_ag_send_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 1713febc14f5SMatthias Ringwald return 1; 17143deb3ec6SMatthias Ringwald } 17153deb3ec6SMatthias Ringwald 1716a0ffb263SMatthias Ringwald if (hfp_connection->send_ag_status_indicators){ 1717a0ffb263SMatthias Ringwald hfp_connection->send_ag_status_indicators = 0; 1718485ac19eSMilanka Ringwald hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid); 1719febc14f5SMatthias Ringwald return 1; 1720febc14f5SMatthias Ringwald } 1721febc14f5SMatthias Ringwald 1722febc14f5SMatthias Ringwald return 0; 1723febc14f5SMatthias Ringwald } 1724febc14f5SMatthias Ringwald 1725febc14f5SMatthias Ringwald static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection){ 1726febc14f5SMatthias Ringwald 172776cc1527SMatthias Ringwald btstack_assert(hfp_connection != NULL); 172876cc1527SMatthias Ringwald btstack_assert(hfp_connection->local_role == HFP_ROLE_AG); 1729febc14f5SMatthias Ringwald 173076cc1527SMatthias Ringwald // during SDP query, RFCOMM CID is not set 173176cc1527SMatthias Ringwald if (hfp_connection->rfcomm_cid == 0) return; 1732ce263fc8SMatthias Ringwald 1733febc14f5SMatthias Ringwald if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) { 1734febc14f5SMatthias Ringwald log_info("hfp_ag_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid); 1735febc14f5SMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 1736febc14f5SMatthias Ringwald return; 1737febc14f5SMatthias Ringwald } 1738febc14f5SMatthias Ringwald 1739febc14f5SMatthias Ringwald int cmd_sent = hfp_ag_send_commands(hfp_connection); 1740febc14f5SMatthias Ringwald 17412ee45eb2SMilanka Ringwald // trigger codec exchange if requested by app 17422ee45eb2SMilanka Ringwald if (hfp_connection->trigger_codec_exchange){ 17432ee45eb2SMilanka Ringwald log_info("trigger codec, command %u, codec state %u", hfp_connection->command, hfp_connection->codecs_state); 17442ee45eb2SMilanka Ringwald } 1745febc14f5SMatthias Ringwald 1746c1ab6cc1SMatthias Ringwald if (hfp_connection->trigger_codec_exchange && (hfp_connection->command == HFP_CMD_NONE)){ 17472ee45eb2SMilanka Ringwald switch (hfp_connection->codecs_state){ 17482ee45eb2SMilanka Ringwald case HFP_CODECS_IDLE: 17492ee45eb2SMilanka Ringwald case HFP_CODECS_RECEIVED_LIST: 17502ee45eb2SMilanka Ringwald case HFP_CODECS_AG_RESEND_COMMON_CODEC: 17512ee45eb2SMilanka Ringwald case HFP_CODECS_ERROR: 17522ee45eb2SMilanka Ringwald hfp_connection->trigger_codec_exchange = 0; 17532ee45eb2SMilanka Ringwald hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC; 17542ee45eb2SMilanka Ringwald break; 17552ee45eb2SMilanka Ringwald default: 17562ee45eb2SMilanka Ringwald break; 17572ee45eb2SMilanka Ringwald } 17582ee45eb2SMilanka Ringwald } 17592ee45eb2SMilanka Ringwald 1760febc14f5SMatthias Ringwald if (!cmd_sent){ 1761febc14f5SMatthias Ringwald cmd_sent = hfp_ag_run_for_context_service_level_connection(hfp_connection); 1762febc14f5SMatthias Ringwald } 1763febc14f5SMatthias Ringwald 1764c04cf7ffSMilanka Ringwald if (!cmd_sent){ 1765c04cf7ffSMilanka Ringwald cmd_sent = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection); 17663deb3ec6SMatthias Ringwald } 17673deb3ec6SMatthias Ringwald 1768c04cf7ffSMilanka Ringwald if (!cmd_sent){ 1769c04cf7ffSMilanka Ringwald cmd_sent = call_setup_state_machine(hfp_connection); 1770aa4dd815SMatthias Ringwald } 1771aa4dd815SMatthias Ringwald 1772c04cf7ffSMilanka Ringwald if (!cmd_sent){ 1773c04cf7ffSMilanka Ringwald cmd_sent = hfp_ag_run_for_audio_connection(hfp_connection); 1774aa4dd815SMatthias Ringwald } 17753deb3ec6SMatthias Ringwald 1776c1ab6cc1SMatthias Ringwald if ((hfp_connection->command == HFP_CMD_NONE) && !cmd_sent){ 1777a0ffb263SMatthias Ringwald // log_info("hfp_connection->command == HFP_CMD_NONE"); 1778a0ffb263SMatthias Ringwald switch(hfp_connection->state){ 17793deb3ec6SMatthias Ringwald case HFP_W2_DISCONNECT_RFCOMM: 1780a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 1781a0ffb263SMatthias Ringwald rfcomm_disconnect(hfp_connection->rfcomm_cid); 17823deb3ec6SMatthias Ringwald break; 17833deb3ec6SMatthias Ringwald default: 17843deb3ec6SMatthias Ringwald break; 17853deb3ec6SMatthias Ringwald } 17863deb3ec6SMatthias Ringwald } 1787c04cf7ffSMilanka Ringwald 1788c04cf7ffSMilanka Ringwald if (cmd_sent){ 1789a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1790473ac565SMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 1791473ac565SMatthias Ringwald } 17923deb3ec6SMatthias Ringwald } 1793d68dcce1SMatthias Ringwald 1794ce263fc8SMatthias Ringwald static hfp_generic_status_indicator_t *get_hf_indicator_by_number(int number){ 1795ce263fc8SMatthias Ringwald int i; 1796a0ffb263SMatthias Ringwald for (i=0;i< hfp_generic_status_indicators_nr;i++){ 1797a0ffb263SMatthias Ringwald hfp_generic_status_indicator_t * indicator = &hfp_generic_status_indicators[i]; 1798ce263fc8SMatthias Ringwald if (indicator->uuid == number){ 1799ce263fc8SMatthias Ringwald return indicator; 1800ce263fc8SMatthias Ringwald } 1801ce263fc8SMatthias Ringwald } 1802ce263fc8SMatthias Ringwald return NULL; 1803ce263fc8SMatthias Ringwald } 18043deb3ec6SMatthias Ringwald 1805e0d09929SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){ 1806c1ab6cc1SMatthias Ringwald return (byte == '\n') || (byte == '\r'); 1807e0d09929SMatthias Ringwald } 1808e0d09929SMatthias Ringwald 1809e9c22d4eSMatthias Ringwald static void hfp_ag_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 18108a46ec40SMatthias Ringwald UNUSED(packet_type); // ok: only called with RFCOMM_DATA_PACKET 18118a46ec40SMatthias Ringwald 18128a46ec40SMatthias Ringwald // assertion: size >= 1 as rfcomm.c does not deliver empty packets 18138a46ec40SMatthias Ringwald if (size < 1) return; 18149ec2630cSMatthias Ringwald 1815a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 1816a0ffb263SMatthias Ringwald if (!hfp_connection) return; 18171e35c04dSMatthias Ringwald 1818186dd3d2SMatthias Ringwald hfp_log_rfcomm_message("HFP_AG_RX", packet, size); 18191e35c04dSMatthias Ringwald 18208a46ec40SMatthias Ringwald // process messages byte-wise 18213deb3ec6SMatthias Ringwald int pos; 18223deb3ec6SMatthias Ringwald for (pos = 0; pos < size ; pos++){ 1823a0ffb263SMatthias Ringwald hfp_parse(hfp_connection, packet[pos], 0); 1824186dd3d2SMatthias Ringwald 1825e0d09929SMatthias Ringwald // parse until end of line 1826e0d09929SMatthias Ringwald if (!hfp_parser_is_end_of_line(packet[pos])) continue; 1827e0d09929SMatthias Ringwald 1828186dd3d2SMatthias Ringwald hfp_generic_status_indicator_t * indicator; 1829a0ffb263SMatthias Ringwald switch(hfp_connection->command){ 1830ce263fc8SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_QUERY: 1831ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_active){ 1832a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1; 1833ce263fc8SMatthias Ringwald } 1834a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1835ce263fc8SMatthias Ringwald break; 1836ce263fc8SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_COMMAND: 18370222a807SMatthias Ringwald switch(hfp_connection->ag_response_and_hold_action){ 1838ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD: 1839a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection); 1840ce263fc8SMatthias Ringwald break; 1841ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED: 1842a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection); 1843ce263fc8SMatthias Ringwald break; 1844ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED: 1845a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection); 1846ce263fc8SMatthias Ringwald break; 1847ce263fc8SMatthias Ringwald default: 1848ce263fc8SMatthias Ringwald break; 1849ce263fc8SMatthias Ringwald } 1850a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1851ce263fc8SMatthias Ringwald break; 1852ce263fc8SMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 1853a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1854ce263fc8SMatthias Ringwald // find indicator by assigned number 1855a0ffb263SMatthias Ringwald indicator = get_hf_indicator_by_number(hfp_connection->parser_indicator_index); 1856ce263fc8SMatthias Ringwald if (!indicator){ 1857a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1858ce263fc8SMatthias Ringwald break; 1859ce263fc8SMatthias Ringwald } 1860ce263fc8SMatthias Ringwald switch (indicator->uuid){ 1861ce263fc8SMatthias Ringwald case 1: // enhanced security 18620222a807SMatthias Ringwald if (hfp_connection->parser_indicator_value > 1) { 1863a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1864ce263fc8SMatthias Ringwald return; 1865ce263fc8SMatthias Ringwald } 1866e883851fSMatthias Ringwald log_info("HF Indicator 'enhanced security' set to %u", (unsigned int) hfp_connection->parser_indicator_value); 1867ce263fc8SMatthias Ringwald break; 1868ce263fc8SMatthias Ringwald case 2: // battery level 18690222a807SMatthias Ringwald if (hfp_connection->parser_indicator_value > 100){ 1870a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1871ce263fc8SMatthias Ringwald return; 1872ce263fc8SMatthias Ringwald } 1873e883851fSMatthias Ringwald log_info("HF Indicator 'battery' set to %u", (unsigned int) hfp_connection->parser_indicator_value); 1874ce263fc8SMatthias Ringwald break; 1875ce263fc8SMatthias Ringwald default: 1876e883851fSMatthias Ringwald log_info("HF Indicator unknown set to %u", (unsigned int) hfp_connection->parser_indicator_value); 1877ce263fc8SMatthias Ringwald break; 1878ce263fc8SMatthias Ringwald } 1879a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1880ce263fc8SMatthias Ringwald break; 1881ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 1882ce263fc8SMatthias Ringwald // expected by SLC state machine 1883a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break; 1884a0ffb263SMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0; 1885a0ffb263SMatthias Ringwald hfp_connection->send_ag_status_indicators = 1; 1886ce263fc8SMatthias Ringwald break; 1887ce263fc8SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 1888a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1889a0ffb263SMatthias Ringwald hfp_connection->next_call_index = 0; 1890a0ffb263SMatthias Ringwald hfp_connection->send_status_of_current_calls = 1; 1891ce263fc8SMatthias Ringwald break; 1892ce263fc8SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1893ce263fc8SMatthias Ringwald if (subscriber_numbers_count == 0){ 1894485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1895ce263fc8SMatthias Ringwald break; 1896ce263fc8SMatthias Ringwald } 1897a0ffb263SMatthias Ringwald hfp_connection->next_subscriber_number_to_send = 0; 1898a0ffb263SMatthias Ringwald hfp_connection->send_subscriber_number = 1; 1899ce263fc8SMatthias Ringwald break; 1900c1797c7dSMatthias Ringwald case HFP_CMD_TRANSMIT_DTMF_CODES: 19010222a807SMatthias Ringwald { 1902a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 19030222a807SMatthias Ringwald char buffer[2]; 19040222a807SMatthias Ringwald buffer[0] = (char) hfp_connection->ag_dtmf_code; 19050222a807SMatthias Ringwald buffer[1] = 0; 19060222a807SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, buffer); 1907c1797c7dSMatthias Ringwald break; 19080222a807SMatthias Ringwald } 1909aa4dd815SMatthias Ringwald case HFP_CMD_HF_REQUEST_PHONE_NUMBER: 1910a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1911ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG); 1912aa4dd815SMatthias Ringwald break; 1913aa4dd815SMatthias Ringwald case HFP_CMD_TURN_OFF_EC_AND_NR: 1914a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1915aa4dd815SMatthias Ringwald if (get_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION)){ 1916a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1917a0ffb263SMatthias Ringwald hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION, hfp_connection->ag_echo_and_noise_reduction); 191860ebb071SMilanka Ringwald log_info("AG: EC/NR = %u", hfp_connection->ag_echo_and_noise_reduction); 1919aa4dd815SMatthias Ringwald } else { 1920a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1921aa4dd815SMatthias Ringwald } 1922aa4dd815SMatthias Ringwald break; 1923aa4dd815SMatthias Ringwald case HFP_CMD_CALL_ANSWERED: 1924a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 192560ebb071SMilanka Ringwald log_info("HFP: ATA"); 1926a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection); 1927aa4dd815SMatthias Ringwald break; 1928aa4dd815SMatthias Ringwald case HFP_CMD_HANG_UP_CALL: 1929a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1930a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1931a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection); 1932aa4dd815SMatthias Ringwald break; 1933aa4dd815SMatthias Ringwald case HFP_CMD_CALL_HOLD: { 1934a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1935a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1936d0c20769SMatthias Ringwald 19370222a807SMatthias Ringwald switch (hfp_connection->ag_call_hold_action){ 19380222a807SMatthias Ringwald case 0: 1939d0c20769SMatthias Ringwald // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call. 1940a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection); 1941aa4dd815SMatthias Ringwald break; 19420222a807SMatthias Ringwald case 1: 1943d0c20769SMatthias Ringwald // Releases all active calls (if any exist) and accepts the other (held or waiting) call. 1944d0c20769SMatthias Ringwald // Where both a held and a waiting call exist, the above procedures shall apply to the 1945d0c20769SMatthias Ringwald // waiting call (i.e., not to the held call) in conflicting situation. 1946a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection); 1947aa4dd815SMatthias Ringwald break; 19480222a807SMatthias Ringwald case 2: 1949d0c20769SMatthias Ringwald // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call. 1950d0c20769SMatthias Ringwald // Where both a held and a waiting call exist, the above procedures shall apply to the 1951d0c20769SMatthias Ringwald // waiting call (i.e., not to the held call) in conflicting situation. 1952a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection); 1953aa4dd815SMatthias Ringwald break; 19540222a807SMatthias Ringwald case 3: 1955d0c20769SMatthias Ringwald // Adds a held call to the conversation. 1956a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection); 1957aa4dd815SMatthias Ringwald break; 19580222a807SMatthias Ringwald case 4: 1959d0c20769SMatthias Ringwald // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer). 1960a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection); 1961aa4dd815SMatthias Ringwald break; 1962aa4dd815SMatthias Ringwald default: 1963aa4dd815SMatthias Ringwald break; 1964aa4dd815SMatthias Ringwald } 19650222a807SMatthias Ringwald hfp_connection->call_index = 0; 196635e92150SMatthias Ringwald break; 1967aa4dd815SMatthias Ringwald } 1968aa4dd815SMatthias Ringwald case HFP_CMD_CALL_PHONE_NUMBER: 1969a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1970a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED, hfp_connection); 1971aa4dd815SMatthias Ringwald break; 1972aa4dd815SMatthias Ringwald case HFP_CMD_REDIAL_LAST_NUMBER: 1973a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1974a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection); 1975aa4dd815SMatthias Ringwald break; 1976aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_CLIP: 1977a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1978a0ffb263SMatthias Ringwald log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled); 1979a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1980aa4dd815SMatthias Ringwald break; 1981aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION: 1982a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1983a0ffb263SMatthias Ringwald log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled); 1984a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1985aa4dd815SMatthias Ringwald break; 1986aa4dd815SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 1987a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1988a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 198960ebb071SMilanka Ringwald log_info("HF speaker gain = %u", hfp_connection->speaker_gain); 1990aa4dd815SMatthias Ringwald break; 1991aa4dd815SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 1992a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1993a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 199460ebb071SMilanka Ringwald log_info("HF microphone gain = %u", hfp_connection->microphone_gain); 1995aa4dd815SMatthias Ringwald break; 1996aa4dd815SMatthias Ringwald default: 1997aa4dd815SMatthias Ringwald break; 19983deb3ec6SMatthias Ringwald } 19993deb3ec6SMatthias Ringwald } 20000cef86faSMatthias Ringwald } 20013deb3ec6SMatthias Ringwald 2002aa4dd815SMatthias Ringwald static void hfp_run(void){ 2003d63c37a1SMatthias Ringwald btstack_linked_list_iterator_t it; 2004d63c37a1SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2005d63c37a1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2006d63c37a1SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 200722387625SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2008f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 20093deb3ec6SMatthias Ringwald } 20103deb3ec6SMatthias Ringwald } 20113deb3ec6SMatthias Ringwald 2012e9c22d4eSMatthias Ringwald static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 20133deb3ec6SMatthias Ringwald switch (packet_type){ 20143deb3ec6SMatthias Ringwald case RFCOMM_DATA_PACKET: 2015e9c22d4eSMatthias Ringwald hfp_ag_handle_rfcomm_data(packet_type, channel, packet, size); 20163deb3ec6SMatthias Ringwald break; 20173deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 2018e30a6a47SMatthias Ringwald if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 2019e30a6a47SMatthias Ringwald uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 2020f0aeb307SMatthias Ringwald hfp_ag_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid)); 2021e30a6a47SMatthias Ringwald return; 2022e30a6a47SMatthias Ringwald } 202327950165SMatthias Ringwald hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_AG); 2024aa4dd815SMatthias Ringwald break; 20253deb3ec6SMatthias Ringwald default: 20263deb3ec6SMatthias Ringwald break; 20273deb3ec6SMatthias Ringwald } 20283deb3ec6SMatthias Ringwald 20293deb3ec6SMatthias Ringwald hfp_run(); 20303deb3ec6SMatthias Ringwald } 20313deb3ec6SMatthias Ringwald 2032405014fbSMatthias Ringwald static void hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2033405014fbSMatthias Ringwald hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_AG); 203405bed9f6SMatthias Ringwald 203505bed9f6SMatthias Ringwald // allow for sco established -> ring transition 203605bed9f6SMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 203705bed9f6SMatthias Ringwald if (hci_event_packet_get_type(packet) != HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE) return; 203805bed9f6SMatthias Ringwald hfp_run(); 2039405014fbSMatthias Ringwald } 2040405014fbSMatthias Ringwald 2041a0ffb263SMatthias Ringwald void hfp_ag_init_codecs(int codecs_nr, uint8_t * codecs){ 20423deb3ec6SMatthias Ringwald if (codecs_nr > HFP_MAX_NUM_CODECS){ 20433deb3ec6SMatthias Ringwald log_error("hfp_init: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS); 20443deb3ec6SMatthias Ringwald return; 20453deb3ec6SMatthias Ringwald } 20463deb3ec6SMatthias Ringwald int i; 2047a0ffb263SMatthias Ringwald hfp_codecs_nr = codecs_nr; 20483deb3ec6SMatthias Ringwald for (i=0; i < codecs_nr; i++){ 20493deb3ec6SMatthias Ringwald hfp_codecs[i] = codecs[i]; 20503deb3ec6SMatthias Ringwald } 2051a0ffb263SMatthias Ringwald } 20523deb3ec6SMatthias Ringwald 2053a0ffb263SMatthias Ringwald void hfp_ag_init_supported_features(uint32_t supported_features){ 2054a0ffb263SMatthias Ringwald hfp_supported_features = supported_features; 2055a0ffb263SMatthias Ringwald } 20563deb3ec6SMatthias Ringwald 2057a0ffb263SMatthias Ringwald void hfp_ag_init_ag_indicators(int ag_indicators_nr, hfp_ag_indicator_t * ag_indicators){ 2058a0ffb263SMatthias Ringwald hfp_ag_indicators_nr = ag_indicators_nr; 20596535961aSMatthias Ringwald (void)memcpy(hfp_ag_indicators, ag_indicators, 20606535961aSMatthias Ringwald ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 2061a0ffb263SMatthias Ringwald } 20623deb3ec6SMatthias Ringwald 2063a0ffb263SMatthias Ringwald void hfp_ag_init_hf_indicators(int hf_indicators_nr, hfp_generic_status_indicator_t * hf_indicators){ 206425789943SMilanka Ringwald if (hf_indicators_nr > HFP_MAX_NUM_INDICATORS) return; 2065a0ffb263SMatthias Ringwald hfp_generic_status_indicators_nr = hf_indicators_nr; 20666535961aSMatthias Ringwald (void)memcpy(hfp_generic_status_indicators, hf_indicators, 20676535961aSMatthias Ringwald hf_indicators_nr * sizeof(hfp_generic_status_indicator_t)); 2068a0ffb263SMatthias Ringwald } 2069a0ffb263SMatthias Ringwald 2070a0ffb263SMatthias Ringwald void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){ 20713deb3ec6SMatthias Ringwald hfp_ag_call_hold_services_nr = call_hold_services_nr; 20726535961aSMatthias Ringwald (void)memcpy(hfp_ag_call_hold_services, call_hold_services, 20736535961aSMatthias Ringwald call_hold_services_nr * sizeof(char *)); 2074a0ffb263SMatthias Ringwald } 2075a0ffb263SMatthias Ringwald 2076a0ffb263SMatthias Ringwald 2077a0ffb263SMatthias Ringwald void hfp_ag_init(uint16_t rfcomm_channel_nr){ 207827950165SMatthias Ringwald 2079520c92d5SMatthias Ringwald hfp_init(); 2080d63c37a1SMatthias Ringwald 2081405014fbSMatthias Ringwald hci_event_callback_registration.callback = &hci_event_packet_handler; 208227950165SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 208327950165SMatthias Ringwald 2084e9c22d4eSMatthias Ringwald rfcomm_register_service(&rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 208527950165SMatthias Ringwald 208627950165SMatthias Ringwald // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us 2087e9c22d4eSMatthias Ringwald hfp_set_ag_rfcomm_packet_handler(&rfcomm_packet_handler); 2088aa4dd815SMatthias Ringwald 2089d210d9c4SMatthias Ringwald hfp_ag_response_and_hold_active = 0; 2090d210d9c4SMatthias Ringwald subscriber_numbers = NULL; 2091d210d9c4SMatthias Ringwald subscriber_numbers_count = 0; 2092d210d9c4SMatthias Ringwald 2093d210d9c4SMatthias Ringwald hfp_gsm_init(); 20943deb3ec6SMatthias Ringwald } 20953deb3ec6SMatthias Ringwald 20963deb3ec6SMatthias Ringwald void hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){ 2097323d3000SMatthias Ringwald hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE, HFP_ROLE_AG); 20983deb3ec6SMatthias Ringwald } 20993deb3ec6SMatthias Ringwald 2100d97d752dSMilanka Ringwald void hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){ 21019c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2102a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2103a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2104a33eb0c4SMilanka Ringwald return; 2105a33eb0c4SMilanka Ringwald } 2106a0ffb263SMatthias Ringwald hfp_release_service_level_connection(hfp_connection); 2107f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 21083deb3ec6SMatthias Ringwald } 21093deb3ec6SMatthias Ringwald 2110d97d752dSMilanka Ringwald void hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, hfp_cme_error_t error){ 21119c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2112a0ffb263SMatthias Ringwald if (!hfp_connection){ 2113a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 21143deb3ec6SMatthias Ringwald return; 21153deb3ec6SMatthias Ringwald } 2116a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 2117a0ffb263SMatthias Ringwald if (!hfp_connection->enable_extended_audio_gateway_error_report){ 21183deb3ec6SMatthias Ringwald return; 21193deb3ec6SMatthias Ringwald } 2120a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = error; 2121f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 21223deb3ec6SMatthias Ringwald } 21233deb3ec6SMatthias Ringwald 2124a0ffb263SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){ 21252ee45eb2SMilanka Ringwald log_info("hfp_ag_setup_audio_connection state %u", hfp_connection->state); 2126a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return; 2127a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return; 21283deb3ec6SMatthias Ringwald 2129a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 1; 2130a0ffb263SMatthias Ringwald if (!has_codec_negotiation_feature(hfp_connection)){ 2131d6ff09e1SMatthias Ringwald log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using CVSD"); 2132d6ff09e1SMatthias Ringwald hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 2133a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 21347522e673SMatthias Ringwald // now, pick link settings 2135afac2a04SMilanka Ringwald hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection)); 21362ee45eb2SMilanka Ringwald return; 21373deb3ec6SMatthias Ringwald } 2138aa4dd815SMatthias Ringwald 21392ee45eb2SMilanka Ringwald hfp_connection->trigger_codec_exchange = 1; 2140aa4dd815SMatthias Ringwald } 2141aa4dd815SMatthias Ringwald 2142d97d752dSMilanka Ringwald void hfp_ag_establish_audio_connection(hci_con_handle_t acl_handle){ 21439c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2144a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2145a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2146a33eb0c4SMilanka Ringwald return; 2147a33eb0c4SMilanka Ringwald } 21482ee45eb2SMilanka Ringwald hfp_connection->trigger_codec_exchange = 0; 2149a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 2150a0ffb263SMatthias Ringwald hfp_ag_setup_audio_connection(hfp_connection); 2151f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 21523deb3ec6SMatthias Ringwald } 21533deb3ec6SMatthias Ringwald 2154d97d752dSMilanka Ringwald void hfp_ag_release_audio_connection(hci_con_handle_t acl_handle){ 21559c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2156a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2157a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2158a33eb0c4SMilanka Ringwald return; 2159a33eb0c4SMilanka Ringwald } 2160a0ffb263SMatthias Ringwald hfp_release_audio_connection(hfp_connection); 2161f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 21623deb3ec6SMatthias Ringwald } 2163aa4dd815SMatthias Ringwald 2164aa4dd815SMatthias Ringwald /** 2165aa4dd815SMatthias Ringwald * @brief Enable in-band ring tone 2166aa4dd815SMatthias Ringwald */ 2167aa4dd815SMatthias Ringwald void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){ 2168aa4dd815SMatthias Ringwald if (get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE) == use_in_band_ring_tone){ 2169aa4dd815SMatthias Ringwald return; 2170aa4dd815SMatthias Ringwald } 2171aa4dd815SMatthias Ringwald hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE, use_in_band_ring_tone); 2172aa4dd815SMatthias Ringwald 2173665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 2174665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2175665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2176a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 217766c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2178a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING; 2179f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2180aa4dd815SMatthias Ringwald } 2181aa4dd815SMatthias Ringwald } 2182aa4dd815SMatthias Ringwald 2183aa4dd815SMatthias Ringwald /** 2184aa4dd815SMatthias Ringwald * @brief Called from GSM 2185aa4dd815SMatthias Ringwald */ 2186aa4dd815SMatthias Ringwald void hfp_ag_incoming_call(void){ 2187aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL); 2188aa4dd815SMatthias Ringwald } 2189aa4dd815SMatthias Ringwald 2190aa4dd815SMatthias Ringwald /** 2191aa4dd815SMatthias Ringwald * @brief number is stored. 2192aa4dd815SMatthias Ringwald */ 2193aa4dd815SMatthias Ringwald void hfp_ag_set_clip(uint8_t type, const char * number){ 2194f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_SET_CLIP, 0, type, number); 2195aa4dd815SMatthias Ringwald } 2196aa4dd815SMatthias Ringwald 2197aa4dd815SMatthias Ringwald void hfp_ag_call_dropped(void){ 2198aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL); 2199aa4dd815SMatthias Ringwald } 2200aa4dd815SMatthias Ringwald 2201aa4dd815SMatthias Ringwald // call from AG UI 2202aa4dd815SMatthias Ringwald void hfp_ag_answer_incoming_call(void){ 2203aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL); 2204aa4dd815SMatthias Ringwald } 2205aa4dd815SMatthias Ringwald 2206ce263fc8SMatthias Ringwald void hfp_ag_join_held_call(void){ 2207ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL); 2208ce263fc8SMatthias Ringwald } 2209ce263fc8SMatthias Ringwald 2210aa4dd815SMatthias Ringwald void hfp_ag_terminate_call(void){ 2211aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL); 2212aa4dd815SMatthias Ringwald } 2213aa4dd815SMatthias Ringwald 2214aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_ringing(void){ 2215aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL); 2216aa4dd815SMatthias Ringwald } 2217aa4dd815SMatthias Ringwald 2218aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_established(void){ 2219aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL); 2220aa4dd815SMatthias Ringwald } 2221aa4dd815SMatthias Ringwald 2222aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_rejected(void){ 2223aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL); 2224aa4dd815SMatthias Ringwald } 2225aa4dd815SMatthias Ringwald 2226aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_accepted(void){ 2227aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL); 2228aa4dd815SMatthias Ringwald } 2229ce263fc8SMatthias Ringwald 2230ce263fc8SMatthias Ringwald void hfp_ag_hold_incoming_call(void){ 2231ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL); 2232ce263fc8SMatthias Ringwald } 2233ce263fc8SMatthias Ringwald 2234ce263fc8SMatthias Ringwald void hfp_ag_accept_held_incoming_call(void) { 2235ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL); 2236ce263fc8SMatthias Ringwald } 2237ce263fc8SMatthias Ringwald 2238ce263fc8SMatthias Ringwald void hfp_ag_reject_held_incoming_call(void){ 2239ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL); 2240ce263fc8SMatthias Ringwald } 2241ce263fc8SMatthias Ringwald 2242aa4dd815SMatthias Ringwald static void hfp_ag_set_ag_indicator(const char * name, int value){ 2243aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name(name); 2244aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 2245aa4dd815SMatthias Ringwald hfp_ag_indicators[indicator_index].status = value; 2246aa4dd815SMatthias Ringwald 2247ce263fc8SMatthias Ringwald 2248665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 2249665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2250665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2251a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 225266c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2253a0ffb263SMatthias Ringwald if (! hfp_connection->ag_indicators[indicator_index].enabled) { 2254ce263fc8SMatthias Ringwald log_info("AG indicator '%s' changed to %u but not enabled", hfp_ag_indicators[indicator_index].name, value); 2255ce263fc8SMatthias Ringwald continue; 2256ce263fc8SMatthias Ringwald } 2257ce263fc8SMatthias Ringwald log_info("AG indicator '%s' changed to %u, request transfer statur", hfp_ag_indicators[indicator_index].name, value); 2258a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 2259f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2260aa4dd815SMatthias Ringwald } 2261aa4dd815SMatthias Ringwald } 2262aa4dd815SMatthias Ringwald 2263aa4dd815SMatthias Ringwald void hfp_ag_set_registration_status(int status){ 2264aa4dd815SMatthias Ringwald hfp_ag_set_ag_indicator("service", status); 2265aa4dd815SMatthias Ringwald } 2266aa4dd815SMatthias Ringwald 2267aa4dd815SMatthias Ringwald void hfp_ag_set_signal_strength(int strength){ 2268aa4dd815SMatthias Ringwald hfp_ag_set_ag_indicator("signal", strength); 2269aa4dd815SMatthias Ringwald } 2270aa4dd815SMatthias Ringwald 2271aa4dd815SMatthias Ringwald void hfp_ag_set_roaming_status(int status){ 2272aa4dd815SMatthias Ringwald hfp_ag_set_ag_indicator("roam", status); 2273aa4dd815SMatthias Ringwald } 2274aa4dd815SMatthias Ringwald 2275aa4dd815SMatthias Ringwald void hfp_ag_set_battery_level(int level){ 2276aa4dd815SMatthias Ringwald hfp_ag_set_ag_indicator("battchg", level); 2277aa4dd815SMatthias Ringwald } 2278aa4dd815SMatthias Ringwald 2279d97d752dSMilanka Ringwald void hfp_ag_activate_voice_recognition(hci_con_handle_t acl_handle, int activate){ 2280aa4dd815SMatthias Ringwald if (!get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)) return; 22819c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2282a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2283a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2284a33eb0c4SMilanka Ringwald return; 2285a33eb0c4SMilanka Ringwald } 2286aa4dd815SMatthias Ringwald 2287a0ffb263SMatthias Ringwald if (!get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION)) { 228860ebb071SMilanka Ringwald log_info("AG cannot acivate voice recognition - not supported by HF"); 2289aa4dd815SMatthias Ringwald return; 2290aa4dd815SMatthias Ringwald } 2291aa4dd815SMatthias Ringwald 2292aa4dd815SMatthias Ringwald if (activate){ 2293d97d752dSMilanka Ringwald hfp_ag_establish_audio_connection(acl_handle); 2294aa4dd815SMatthias Ringwald } 2295aa4dd815SMatthias Ringwald 2296a0ffb263SMatthias Ringwald hfp_connection->ag_activate_voice_recognition = activate; 2297a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION; 2298f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2299aa4dd815SMatthias Ringwald } 2300aa4dd815SMatthias Ringwald 2301d97d752dSMilanka Ringwald void hfp_ag_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 23029c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2303a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2304a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2305a33eb0c4SMilanka Ringwald return; 2306a33eb0c4SMilanka Ringwald } 2307a0ffb263SMatthias Ringwald if (hfp_connection->microphone_gain != gain){ 2308a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_SET_MICROPHONE_GAIN; 2309a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = gain; 2310a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 2311aa4dd815SMatthias Ringwald } 2312f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2313aa4dd815SMatthias Ringwald } 2314aa4dd815SMatthias Ringwald 2315d97d752dSMilanka Ringwald void hfp_ag_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 23169c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2317a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2318a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2319a33eb0c4SMilanka Ringwald return; 2320a33eb0c4SMilanka Ringwald } 2321a0ffb263SMatthias Ringwald if (hfp_connection->speaker_gain != gain){ 2322a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = gain; 2323a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 2324aa4dd815SMatthias Ringwald } 2325f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2326aa4dd815SMatthias Ringwald } 2327aa4dd815SMatthias Ringwald 2328d97d752dSMilanka Ringwald void hfp_ag_send_phone_number_for_voice_tag(hci_con_handle_t acl_handle, const char * number){ 23299c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2330a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2331a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2332a33eb0c4SMilanka Ringwald return; 2333a33eb0c4SMilanka Ringwald } 2334aa4dd815SMatthias Ringwald hfp_ag_set_clip(0, number); 2335a0ffb263SMatthias Ringwald hfp_connection->send_phone_number_for_voice_tag = 1; 2336aa4dd815SMatthias Ringwald } 2337aa4dd815SMatthias Ringwald 2338d97d752dSMilanka Ringwald void hfp_ag_reject_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 23399c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2340a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2341a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2342a33eb0c4SMilanka Ringwald return; 2343a33eb0c4SMilanka Ringwald } 2344a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 2345aa4dd815SMatthias Ringwald } 2346aa4dd815SMatthias Ringwald 2347d97d752dSMilanka Ringwald void hfp_ag_send_dtmf_code_done(hci_con_handle_t acl_handle){ 23489c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2349a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2350a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2351a33eb0c4SMilanka Ringwald return; 2352a33eb0c4SMilanka Ringwald } 2353a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2354c1797c7dSMatthias Ringwald } 2355aa4dd815SMatthias Ringwald 2356ce263fc8SMatthias Ringwald void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){ 2357ce263fc8SMatthias Ringwald subscriber_numbers = numbers; 2358ce263fc8SMatthias Ringwald subscriber_numbers_count = numbers_count; 2359ce263fc8SMatthias Ringwald } 2360ce263fc8SMatthias Ringwald 23619cae807eSMatthias Ringwald void hfp_ag_clear_last_dialed_number(void){ 23629cae807eSMatthias Ringwald hfp_gsm_clear_last_dialed_number(); 2363ce263fc8SMatthias Ringwald } 2364ce263fc8SMatthias Ringwald 2365d97d752dSMilanka Ringwald void hfp_ag_notify_incoming_call_waiting(hci_con_handle_t acl_handle){ 23669c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2367a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2368a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2369a33eb0c4SMilanka Ringwald return; 2370a33eb0c4SMilanka Ringwald } 2371a0ffb263SMatthias Ringwald if (!hfp_connection->call_waiting_notification_enabled) return; 2372a0ffb263SMatthias Ringwald 2373a0ffb263SMatthias Ringwald hfp_connection->ag_notify_incoming_call_waiting = 1; 2374f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2375a0ffb263SMatthias Ringwald } 2376ce263fc8SMatthias Ringwald 237776cc1527SMatthias 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){ 237876cc1527SMatthias Ringwald if (!name){ 237976cc1527SMatthias Ringwald name = default_hfp_ag_service_name; 238076cc1527SMatthias Ringwald } 238176cc1527SMatthias Ringwald hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, rfcomm_channel_nr, name); 238276cc1527SMatthias Ringwald 238376cc1527SMatthias Ringwald /* 238476cc1527SMatthias Ringwald * 0x01 – Ability to reject a call 238576cc1527SMatthias Ringwald * 0x00 – No ability to reject a call 238676cc1527SMatthias Ringwald */ 238776cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301); // Hands-Free Profile - Network 238876cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call); 238976cc1527SMatthias Ringwald 239076cc1527SMatthias Ringwald // Construct SupportedFeatures for SDP bitmap: 239176cc1527SMatthias Ringwald // 239276cc1527SMatthias Ringwald // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 239376cc1527SMatthias Ringwald // of the Bits 0 to 4 of the unsolicited result code +BRSF" 239476cc1527SMatthias Ringwald // 239576cc1527SMatthias Ringwald // Wide band speech (bit 5) requires Codec negotiation 239676cc1527SMatthias Ringwald // 239776cc1527SMatthias Ringwald uint16_t sdp_features = supported_features & 0x1f; 239876cc1527SMatthias Ringwald if (wide_band_speech && (supported_features & (1 << HFP_AGSF_CODEC_NEGOTIATION))){ 239976cc1527SMatthias Ringwald sdp_features |= 1 << 5; 240076cc1527SMatthias Ringwald } 240176cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 240276cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 240376cc1527SMatthias Ringwald } 240476cc1527SMatthias Ringwald 240576cc1527SMatthias Ringwald void hfp_ag_register_packet_handler(btstack_packet_handler_t callback){ 240676cc1527SMatthias Ringwald if (callback == NULL){ 240776cc1527SMatthias Ringwald log_error("hfp_ag_register_packet_handler called with NULL callback"); 240876cc1527SMatthias Ringwald return; 240976cc1527SMatthias Ringwald } 241076cc1527SMatthias Ringwald hfp_ag_callback = callback; 241176cc1527SMatthias Ringwald hfp_set_ag_callback(callback); 241276cc1527SMatthias Ringwald } 2413