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 108739426febSMatthias Ringwald hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_REJECTED); 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()){ 1105aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1106d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_INCOMING_CALL); 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()){ 1117aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1118d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_INCOMING_CALL); 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()){ 1135aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1136d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG); 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"); 1150d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG); 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"); 1171d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_HELD_CALL_JOINED_BY_AG); 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()){ 1189aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1190d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF); 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()){ 1211ce263fc8SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1212d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG); 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()){ 1235ce263fc8SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1236d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF); 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; 1259d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG); 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; 1270d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG); 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()){ 1283aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1284d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF); 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: 1291aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1292d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF); 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; 1301aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1302d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF); 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()){ 1318aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1319d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG); 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; 1328aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1329d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG); 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 } 1355d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_CALL_DROPPED); 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) { 1362d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_CALL_DROPPED); 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 } 1366d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_CALL_DROPPED); 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 } 1384a0ffb263SMatthias Ringwald hfp_gsm_handle_event_with_call_number(HFP_AG_OUTGOING_CALL_INITIATED, (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 139974386ee0SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_OUTGOING_REDIAL_INITIATED); 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; 1430d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ACCEPTED); 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 1455d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_RINGING); 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; 1473d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ESTABLISHED); 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 1486d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_USER_BUSY: 1487d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_CALL_HOLD_USER_BUSY); 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){ 1500a0ffb263SMatthias Ringwald hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index); 1501d0c20769SMatthias Ringwald 1502d0c20769SMatthias Ringwald } 1503d0c20769SMatthias Ringwald 1504d210d9c4SMatthias Ringwald if (call_setup_in_progress){ 150560ebb071SMilanka Ringwald log_info("AG: Call Dropped, Accept new call"); 1506d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1507a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1508d210d9c4SMatthias Ringwald } else { 150960ebb071SMilanka Ringwald log_info("AG: Call Dropped, Resume held call"); 1510d210d9c4SMatthias Ringwald } 1511d210d9c4SMatthias Ringwald if (call_held){ 1512d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1513a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1514d210d9c4SMatthias Ringwald } 1515d0c20769SMatthias Ringwald 1516a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1517d210d9c4SMatthias Ringwald break; 1518d210d9c4SMatthias Ringwald } 1519d0c20769SMatthias Ringwald 1520d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{ 1521d210d9c4SMatthias Ringwald // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call. 1522d210d9c4SMatthias Ringwald // only update if callsetup changed 1523d0c20769SMatthias Ringwald int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1524a0ffb263SMatthias Ringwald hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index); 1525d0c20769SMatthias Ringwald 1526d210d9c4SMatthias Ringwald if (call_setup_in_progress){ 152760ebb071SMilanka Ringwald log_info("AG: Call on Hold, Accept new call"); 1528d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1529a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1530d210d9c4SMatthias Ringwald } else { 153160ebb071SMilanka Ringwald log_info("AG: Swap calls"); 1532d210d9c4SMatthias Ringwald } 1533d0c20769SMatthias Ringwald 1534d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1535d0c20769SMatthias Ringwald // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED); 1536a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1537a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1538d210d9c4SMatthias Ringwald break; 1539d210d9c4SMatthias Ringwald } 1540d0c20769SMatthias Ringwald 1541d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_ADD_HELD_CALL: 1542d210d9c4SMatthias Ringwald // Adds a held call to the conversation. 1543d0c20769SMatthias Ringwald if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){ 154460ebb071SMilanka Ringwald log_info("AG: Join 3-way-call"); 1545d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_CALL_HOLD_ADD_HELD_CALL); 1546d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1547a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1548ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL); 1549d210d9c4SMatthias Ringwald } 1550a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1551d210d9c4SMatthias Ringwald break; 1552d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS: 1553d210d9c4SMatthias Ringwald // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer) 1554d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS); 155560ebb071SMilanka Ringwald log_info("AG: Transfer call -> Connect two calls and disconnect"); 1556d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1557d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1558a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1559a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1560a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 1561d210d9c4SMatthias Ringwald break; 1562ce263fc8SMatthias Ringwald 15633deb3ec6SMatthias Ringwald default: 15643deb3ec6SMatthias Ringwald break; 15653deb3ec6SMatthias Ringwald } 1566d210d9c4SMatthias Ringwald 1567d0c20769SMatthias Ringwald 15683deb3ec6SMatthias Ringwald } 15693deb3ec6SMatthias Ringwald 15709cae807eSMatthias Ringwald 1571a0ffb263SMatthias Ringwald static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){ 15729cae807eSMatthias Ringwald hfp_gsm_call_t * active_call = hfp_gsm_call(call_index); 15739cae807eSMatthias Ringwald if (!active_call) return; 15749cae807eSMatthias Ringwald 15759cae807eSMatthias Ringwald int idx = active_call->index; 15769cae807eSMatthias Ringwald hfp_enhanced_call_dir_t dir = active_call->direction; 15779cae807eSMatthias Ringwald hfp_enhanced_call_status_t status = active_call->enhanced_status; 15789cae807eSMatthias Ringwald hfp_enhanced_call_mode_t mode = active_call->mode; 15799cae807eSMatthias Ringwald hfp_enhanced_call_mpty_t mpty = active_call->mpty; 15809cae807eSMatthias Ringwald uint8_t type = active_call->clip_type; 15819cae807eSMatthias Ringwald char * number = active_call->clip_number; 15829cae807eSMatthias Ringwald 15839cae807eSMatthias Ringwald char buffer[100]; 15849cae807eSMatthias Ringwald // TODO: check length of a buffer, to fit the MTU 15859cae807eSMatthias Ringwald int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty); 15869cae807eSMatthias Ringwald if (number){ 15871cc1d9e9SMilanka Ringwald offset += snprintf(buffer+offset, sizeof(buffer)-offset-3, ", \"%s\",%u", number, type); 15889cae807eSMatthias Ringwald } 15899cae807eSMatthias Ringwald snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 159060ebb071SMilanka 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, 15919cae807eSMatthias Ringwald mode, mpty, type, number); 1592a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 15939cae807eSMatthias Ringwald } 15949cae807eSMatthias Ringwald 1595473ac565SMatthias Ringwald 1596febc14f5SMatthias Ringwald // sends pending command, returns if command was sent 1597febc14f5SMatthias Ringwald static int hfp_ag_send_commands(hfp_connection_t *hfp_connection){ 159822387625SMatthias Ringwald 1599a0ffb263SMatthias Ringwald if (hfp_connection->send_status_of_current_calls){ 1600a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 1601a0ffb263SMatthias Ringwald if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){ 1602a0ffb263SMatthias Ringwald hfp_connection->next_call_index++; 1603a0ffb263SMatthias Ringwald hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index); 1604febc14f5SMatthias Ringwald return 1; 16059cae807eSMatthias Ringwald } else { 1606febc14f5SMatthias 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 1607a0ffb263SMatthias Ringwald hfp_connection->next_call_index = 0; 1608a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1609a0ffb263SMatthias Ringwald hfp_connection->send_status_of_current_calls = 0; 16109cae807eSMatthias Ringwald } 1611ce263fc8SMatthias Ringwald } 1612ce263fc8SMatthias Ringwald 1613a0ffb263SMatthias Ringwald if (hfp_connection->ag_notify_incoming_call_waiting){ 1614a0ffb263SMatthias Ringwald hfp_connection->ag_notify_incoming_call_waiting = 0; 1615a0ffb263SMatthias Ringwald hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid); 1616febc14f5SMatthias Ringwald return 1; 1617aa4dd815SMatthias Ringwald } 1618aa4dd815SMatthias Ringwald 1619a0ffb263SMatthias Ringwald if (hfp_connection->command == HFP_CMD_UNKNOWN){ 1620a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 1621a0ffb263SMatthias Ringwald hfp_connection->send_error = 0; 1622a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1623485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 1624febc14f5SMatthias Ringwald return 1; 1625a0ffb263SMatthias Ringwald } 1626a0ffb263SMatthias Ringwald 1627a0ffb263SMatthias Ringwald if (hfp_connection->send_error){ 1628a0ffb263SMatthias Ringwald hfp_connection->send_error = 0; 1629a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1630485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 1631febc14f5SMatthias Ringwald return 1; 1632aa4dd815SMatthias Ringwald } 1633aa4dd815SMatthias Ringwald 1634ce263fc8SMatthias Ringwald // note: before update AG indicators and ok_pending 1635a0ffb263SMatthias Ringwald if (hfp_connection->send_response_and_hold_status){ 1636a0ffb263SMatthias Ringwald int status = hfp_connection->send_response_and_hold_status - 1; 1637a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = 0; 1638485ac19eSMilanka Ringwald hfp_ag_send_set_response_and_hold(hfp_connection->rfcomm_cid, status); 1639febc14f5SMatthias Ringwald return 1; 1640ce263fc8SMatthias Ringwald } 1641ce263fc8SMatthias Ringwald 1642a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending){ 1643a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 1644a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1645485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1646febc14f5SMatthias Ringwald return 1; 1647ce263fc8SMatthias Ringwald } 1648ce263fc8SMatthias Ringwald 1649aa4dd815SMatthias Ringwald // update AG indicators 1650a0ffb263SMatthias Ringwald if (hfp_connection->ag_indicators_status_update_bitmap){ 1651aa4dd815SMatthias Ringwald int i; 1652a0ffb263SMatthias Ringwald for (i=0;i<hfp_connection->ag_indicators_nr;i++){ 1653a0ffb263SMatthias Ringwald if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){ 1654a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0); 1655a0ffb263SMatthias Ringwald if (!hfp_connection->enable_status_update_for_ag_indicators) { 1656ce263fc8SMatthias Ringwald log_info("+CMER:3,0,0,0 - not sending update for '%s'", hfp_ag_indicators[i].name); 1657ce263fc8SMatthias Ringwald break; 1658ce263fc8SMatthias Ringwald } 1659485ac19eSMilanka Ringwald hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]); 1660febc14f5SMatthias Ringwald return 1; 1661aa4dd815SMatthias Ringwald } 1662aa4dd815SMatthias Ringwald } 1663aa4dd815SMatthias Ringwald } 1664aa4dd815SMatthias Ringwald 1665a0ffb263SMatthias Ringwald if (hfp_connection->ag_ring){ 1666a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 0; 1667a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1668485ac19eSMilanka Ringwald hfp_ag_send_ring(hfp_connection->rfcomm_cid); 1669febc14f5SMatthias Ringwald return 1; 1670aa4dd815SMatthias Ringwald } 1671aa4dd815SMatthias Ringwald 1672a0ffb263SMatthias Ringwald if (hfp_connection->ag_send_clip){ 1673a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = 0; 1674a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1675a0ffb263SMatthias Ringwald hfp_ag_send_clip(hfp_connection->rfcomm_cid); 1676febc14f5SMatthias Ringwald return 1; 1677aa4dd815SMatthias Ringwald } 1678aa4dd815SMatthias Ringwald 1679a0ffb263SMatthias Ringwald if (hfp_connection->send_phone_number_for_voice_tag){ 1680a0ffb263SMatthias Ringwald hfp_connection->send_phone_number_for_voice_tag = 0; 1681a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1682a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1683a0ffb263SMatthias Ringwald hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid); 1684febc14f5SMatthias Ringwald return 1; 1685aa4dd815SMatthias Ringwald } 1686aa4dd815SMatthias Ringwald 1687a0ffb263SMatthias Ringwald if (hfp_connection->send_subscriber_number){ 1688a0ffb263SMatthias Ringwald if (hfp_connection->next_subscriber_number_to_send < subscriber_numbers_count){ 1689a0ffb263SMatthias Ringwald hfp_phone_number_t phone = subscriber_numbers[hfp_connection->next_subscriber_number_to_send++]; 1690a0ffb263SMatthias Ringwald hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number); 1691ce263fc8SMatthias Ringwald } else { 1692a0ffb263SMatthias Ringwald hfp_connection->send_subscriber_number = 0; 1693a0ffb263SMatthias Ringwald hfp_connection->next_subscriber_number_to_send = 0; 1694485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1695ce263fc8SMatthias Ringwald } 1696a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1697febc14f5SMatthias Ringwald return 1; 1698ce263fc8SMatthias Ringwald } 1699ce263fc8SMatthias Ringwald 1700a0ffb263SMatthias Ringwald if (hfp_connection->send_microphone_gain){ 1701a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 0; 1702a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1703485ac19eSMilanka Ringwald hfp_ag_send_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 1704febc14f5SMatthias Ringwald return 1; 1705aa4dd815SMatthias Ringwald } 1706aa4dd815SMatthias Ringwald 1707a0ffb263SMatthias Ringwald if (hfp_connection->send_speaker_gain){ 1708a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 0; 1709a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1710485ac19eSMilanka Ringwald hfp_ag_send_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 1711febc14f5SMatthias Ringwald return 1; 17123deb3ec6SMatthias Ringwald } 17133deb3ec6SMatthias Ringwald 1714a0ffb263SMatthias Ringwald if (hfp_connection->send_ag_status_indicators){ 1715a0ffb263SMatthias Ringwald hfp_connection->send_ag_status_indicators = 0; 1716485ac19eSMilanka Ringwald hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid); 1717febc14f5SMatthias Ringwald return 1; 1718febc14f5SMatthias Ringwald } 1719febc14f5SMatthias Ringwald 1720febc14f5SMatthias Ringwald return 0; 1721febc14f5SMatthias Ringwald } 1722febc14f5SMatthias Ringwald 1723febc14f5SMatthias Ringwald static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection){ 1724febc14f5SMatthias Ringwald 172576cc1527SMatthias Ringwald btstack_assert(hfp_connection != NULL); 172676cc1527SMatthias Ringwald btstack_assert(hfp_connection->local_role == HFP_ROLE_AG); 1727febc14f5SMatthias Ringwald 172876cc1527SMatthias Ringwald // during SDP query, RFCOMM CID is not set 172976cc1527SMatthias Ringwald if (hfp_connection->rfcomm_cid == 0) return; 1730ce263fc8SMatthias Ringwald 1731febc14f5SMatthias Ringwald if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) { 1732febc14f5SMatthias Ringwald log_info("hfp_ag_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid); 1733febc14f5SMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 1734febc14f5SMatthias Ringwald return; 1735febc14f5SMatthias Ringwald } 1736febc14f5SMatthias Ringwald 1737febc14f5SMatthias Ringwald int cmd_sent = hfp_ag_send_commands(hfp_connection); 1738febc14f5SMatthias Ringwald 17392ee45eb2SMilanka Ringwald // trigger codec exchange if requested by app 17402ee45eb2SMilanka Ringwald if (hfp_connection->trigger_codec_exchange){ 17412ee45eb2SMilanka Ringwald log_info("trigger codec, command %u, codec state %u", hfp_connection->command, hfp_connection->codecs_state); 17422ee45eb2SMilanka Ringwald } 1743febc14f5SMatthias Ringwald 1744c1ab6cc1SMatthias Ringwald if (hfp_connection->trigger_codec_exchange && (hfp_connection->command == HFP_CMD_NONE)){ 17452ee45eb2SMilanka Ringwald switch (hfp_connection->codecs_state){ 17462ee45eb2SMilanka Ringwald case HFP_CODECS_IDLE: 17472ee45eb2SMilanka Ringwald case HFP_CODECS_RECEIVED_LIST: 17482ee45eb2SMilanka Ringwald case HFP_CODECS_AG_RESEND_COMMON_CODEC: 17492ee45eb2SMilanka Ringwald case HFP_CODECS_ERROR: 17502ee45eb2SMilanka Ringwald hfp_connection->trigger_codec_exchange = 0; 17512ee45eb2SMilanka Ringwald hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC; 17522ee45eb2SMilanka Ringwald break; 17532ee45eb2SMilanka Ringwald default: 17542ee45eb2SMilanka Ringwald break; 17552ee45eb2SMilanka Ringwald } 17562ee45eb2SMilanka Ringwald } 17572ee45eb2SMilanka Ringwald 1758febc14f5SMatthias Ringwald if (!cmd_sent){ 1759febc14f5SMatthias Ringwald cmd_sent = hfp_ag_run_for_context_service_level_connection(hfp_connection); 1760febc14f5SMatthias Ringwald } 1761febc14f5SMatthias Ringwald 1762c04cf7ffSMilanka Ringwald if (!cmd_sent){ 1763c04cf7ffSMilanka Ringwald cmd_sent = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection); 17643deb3ec6SMatthias Ringwald } 17653deb3ec6SMatthias Ringwald 1766c04cf7ffSMilanka Ringwald if (!cmd_sent){ 1767c04cf7ffSMilanka Ringwald cmd_sent = call_setup_state_machine(hfp_connection); 1768aa4dd815SMatthias Ringwald } 1769aa4dd815SMatthias Ringwald 1770c04cf7ffSMilanka Ringwald if (!cmd_sent){ 1771c04cf7ffSMilanka Ringwald cmd_sent = hfp_ag_run_for_audio_connection(hfp_connection); 1772aa4dd815SMatthias Ringwald } 17733deb3ec6SMatthias Ringwald 1774c1ab6cc1SMatthias Ringwald if ((hfp_connection->command == HFP_CMD_NONE) && !cmd_sent){ 1775a0ffb263SMatthias Ringwald // log_info("hfp_connection->command == HFP_CMD_NONE"); 1776a0ffb263SMatthias Ringwald switch(hfp_connection->state){ 17773deb3ec6SMatthias Ringwald case HFP_W2_DISCONNECT_RFCOMM: 1778a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 1779a0ffb263SMatthias Ringwald rfcomm_disconnect(hfp_connection->rfcomm_cid); 17803deb3ec6SMatthias Ringwald break; 17813deb3ec6SMatthias Ringwald default: 17823deb3ec6SMatthias Ringwald break; 17833deb3ec6SMatthias Ringwald } 17843deb3ec6SMatthias Ringwald } 1785c04cf7ffSMilanka Ringwald 1786c04cf7ffSMilanka Ringwald if (cmd_sent){ 1787a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1788473ac565SMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 1789473ac565SMatthias Ringwald } 17903deb3ec6SMatthias Ringwald } 1791d68dcce1SMatthias Ringwald 1792ce263fc8SMatthias Ringwald static hfp_generic_status_indicator_t *get_hf_indicator_by_number(int number){ 1793ce263fc8SMatthias Ringwald int i; 1794a0ffb263SMatthias Ringwald for (i=0;i< hfp_generic_status_indicators_nr;i++){ 1795a0ffb263SMatthias Ringwald hfp_generic_status_indicator_t * indicator = &hfp_generic_status_indicators[i]; 1796ce263fc8SMatthias Ringwald if (indicator->uuid == number){ 1797ce263fc8SMatthias Ringwald return indicator; 1798ce263fc8SMatthias Ringwald } 1799ce263fc8SMatthias Ringwald } 1800ce263fc8SMatthias Ringwald return NULL; 1801ce263fc8SMatthias Ringwald } 18023deb3ec6SMatthias Ringwald 1803e0d09929SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){ 1804c1ab6cc1SMatthias Ringwald return (byte == '\n') || (byte == '\r'); 1805e0d09929SMatthias Ringwald } 1806e0d09929SMatthias Ringwald 1807e9c22d4eSMatthias Ringwald static void hfp_ag_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 18088a46ec40SMatthias Ringwald UNUSED(packet_type); // ok: only called with RFCOMM_DATA_PACKET 18098a46ec40SMatthias Ringwald 18108a46ec40SMatthias Ringwald // assertion: size >= 1 as rfcomm.c does not deliver empty packets 18118a46ec40SMatthias Ringwald if (size < 1) return; 18129ec2630cSMatthias Ringwald 1813a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 1814a0ffb263SMatthias Ringwald if (!hfp_connection) return; 18151e35c04dSMatthias Ringwald 1816186dd3d2SMatthias Ringwald hfp_log_rfcomm_message("HFP_AG_RX", packet, size); 18171e35c04dSMatthias Ringwald 18188a46ec40SMatthias Ringwald // process messages byte-wise 18193deb3ec6SMatthias Ringwald int pos; 18203deb3ec6SMatthias Ringwald for (pos = 0; pos < size ; pos++){ 1821a0ffb263SMatthias Ringwald hfp_parse(hfp_connection, packet[pos], 0); 1822186dd3d2SMatthias Ringwald 1823e0d09929SMatthias Ringwald // parse until end of line 1824e0d09929SMatthias Ringwald if (!hfp_parser_is_end_of_line(packet[pos])) continue; 1825e0d09929SMatthias Ringwald 1826186dd3d2SMatthias Ringwald hfp_generic_status_indicator_t * indicator; 1827a0ffb263SMatthias Ringwald switch(hfp_connection->command){ 1828ce263fc8SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_QUERY: 1829ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_active){ 1830a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1; 1831ce263fc8SMatthias Ringwald } 1832a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1833ce263fc8SMatthias Ringwald break; 1834ce263fc8SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_COMMAND: 18350222a807SMatthias Ringwald switch(hfp_connection->ag_response_and_hold_action){ 1836ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD: 1837a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection); 1838ce263fc8SMatthias Ringwald break; 1839ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED: 1840a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection); 1841ce263fc8SMatthias Ringwald break; 1842ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED: 1843a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection); 1844ce263fc8SMatthias Ringwald break; 1845ce263fc8SMatthias Ringwald default: 1846ce263fc8SMatthias Ringwald break; 1847ce263fc8SMatthias Ringwald } 1848a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1849ce263fc8SMatthias Ringwald break; 1850ce263fc8SMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 1851a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1852ce263fc8SMatthias Ringwald // find indicator by assigned number 1853a0ffb263SMatthias Ringwald indicator = get_hf_indicator_by_number(hfp_connection->parser_indicator_index); 1854ce263fc8SMatthias Ringwald if (!indicator){ 1855a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1856ce263fc8SMatthias Ringwald break; 1857ce263fc8SMatthias Ringwald } 1858ce263fc8SMatthias Ringwald switch (indicator->uuid){ 1859ce263fc8SMatthias Ringwald case 1: // enhanced security 18600222a807SMatthias Ringwald if (hfp_connection->parser_indicator_value > 1) { 1861a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1862ce263fc8SMatthias Ringwald return; 1863ce263fc8SMatthias Ringwald } 1864*e883851fSMatthias Ringwald log_info("HF Indicator 'enhanced security' set to %u", (unsigned int) hfp_connection->parser_indicator_value); 1865ce263fc8SMatthias Ringwald break; 1866ce263fc8SMatthias Ringwald case 2: // battery level 18670222a807SMatthias Ringwald if (hfp_connection->parser_indicator_value > 100){ 1868a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1869ce263fc8SMatthias Ringwald return; 1870ce263fc8SMatthias Ringwald } 1871*e883851fSMatthias Ringwald log_info("HF Indicator 'battery' set to %u", (unsigned int) hfp_connection->parser_indicator_value); 1872ce263fc8SMatthias Ringwald break; 1873ce263fc8SMatthias Ringwald default: 1874*e883851fSMatthias Ringwald log_info("HF Indicator unknown set to %u", (unsigned int) hfp_connection->parser_indicator_value); 1875ce263fc8SMatthias Ringwald break; 1876ce263fc8SMatthias Ringwald } 1877a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1878ce263fc8SMatthias Ringwald break; 1879ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 1880ce263fc8SMatthias Ringwald // expected by SLC state machine 1881a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break; 1882a0ffb263SMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0; 1883a0ffb263SMatthias Ringwald hfp_connection->send_ag_status_indicators = 1; 1884ce263fc8SMatthias Ringwald break; 1885ce263fc8SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 1886a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1887a0ffb263SMatthias Ringwald hfp_connection->next_call_index = 0; 1888a0ffb263SMatthias Ringwald hfp_connection->send_status_of_current_calls = 1; 1889ce263fc8SMatthias Ringwald break; 1890ce263fc8SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1891ce263fc8SMatthias Ringwald if (subscriber_numbers_count == 0){ 1892485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1893ce263fc8SMatthias Ringwald break; 1894ce263fc8SMatthias Ringwald } 1895a0ffb263SMatthias Ringwald hfp_connection->next_subscriber_number_to_send = 0; 1896a0ffb263SMatthias Ringwald hfp_connection->send_subscriber_number = 1; 1897ce263fc8SMatthias Ringwald break; 1898c1797c7dSMatthias Ringwald case HFP_CMD_TRANSMIT_DTMF_CODES: 18990222a807SMatthias Ringwald { 1900a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 19010222a807SMatthias Ringwald char buffer[2]; 19020222a807SMatthias Ringwald buffer[0] = (char) hfp_connection->ag_dtmf_code; 19030222a807SMatthias Ringwald buffer[1] = 0; 19040222a807SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, buffer); 1905c1797c7dSMatthias Ringwald break; 19060222a807SMatthias Ringwald } 1907aa4dd815SMatthias Ringwald case HFP_CMD_HF_REQUEST_PHONE_NUMBER: 1908a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1909ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG); 1910aa4dd815SMatthias Ringwald break; 1911aa4dd815SMatthias Ringwald case HFP_CMD_TURN_OFF_EC_AND_NR: 1912a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1913aa4dd815SMatthias Ringwald if (get_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION)){ 1914a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1915a0ffb263SMatthias Ringwald hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION, hfp_connection->ag_echo_and_noise_reduction); 191660ebb071SMilanka Ringwald log_info("AG: EC/NR = %u", hfp_connection->ag_echo_and_noise_reduction); 1917aa4dd815SMatthias Ringwald } else { 1918a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1919aa4dd815SMatthias Ringwald } 1920aa4dd815SMatthias Ringwald break; 1921aa4dd815SMatthias Ringwald case HFP_CMD_CALL_ANSWERED: 1922a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 192360ebb071SMilanka Ringwald log_info("HFP: ATA"); 1924a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection); 1925aa4dd815SMatthias Ringwald break; 1926aa4dd815SMatthias Ringwald case HFP_CMD_HANG_UP_CALL: 1927a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1928a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1929a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection); 1930aa4dd815SMatthias Ringwald break; 1931aa4dd815SMatthias Ringwald case HFP_CMD_CALL_HOLD: { 1932a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1933a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1934d0c20769SMatthias Ringwald 19350222a807SMatthias Ringwald switch (hfp_connection->ag_call_hold_action){ 19360222a807SMatthias Ringwald case 0: 1937d0c20769SMatthias Ringwald // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call. 1938a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection); 1939aa4dd815SMatthias Ringwald break; 19400222a807SMatthias Ringwald case 1: 1941d0c20769SMatthias Ringwald // Releases all active calls (if any exist) and accepts the other (held or waiting) call. 1942d0c20769SMatthias Ringwald // Where both a held and a waiting call exist, the above procedures shall apply to the 1943d0c20769SMatthias Ringwald // waiting call (i.e., not to the held call) in conflicting situation. 1944a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection); 1945aa4dd815SMatthias Ringwald break; 19460222a807SMatthias Ringwald case 2: 1947d0c20769SMatthias Ringwald // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call. 1948d0c20769SMatthias Ringwald // Where both a held and a waiting call exist, the above procedures shall apply to the 1949d0c20769SMatthias Ringwald // waiting call (i.e., not to the held call) in conflicting situation. 1950a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection); 1951aa4dd815SMatthias Ringwald break; 19520222a807SMatthias Ringwald case 3: 1953d0c20769SMatthias Ringwald // Adds a held call to the conversation. 1954a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection); 1955aa4dd815SMatthias Ringwald break; 19560222a807SMatthias Ringwald case 4: 1957d0c20769SMatthias Ringwald // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer). 1958a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection); 1959aa4dd815SMatthias Ringwald break; 1960aa4dd815SMatthias Ringwald default: 1961aa4dd815SMatthias Ringwald break; 1962aa4dd815SMatthias Ringwald } 19630222a807SMatthias Ringwald hfp_connection->call_index = 0; 196435e92150SMatthias Ringwald break; 1965aa4dd815SMatthias Ringwald } 1966aa4dd815SMatthias Ringwald case HFP_CMD_CALL_PHONE_NUMBER: 1967a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1968a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED, hfp_connection); 1969aa4dd815SMatthias Ringwald break; 1970aa4dd815SMatthias Ringwald case HFP_CMD_REDIAL_LAST_NUMBER: 1971a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1972a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection); 1973aa4dd815SMatthias Ringwald break; 1974aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_CLIP: 1975a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1976a0ffb263SMatthias Ringwald log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled); 1977a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1978aa4dd815SMatthias Ringwald break; 1979aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION: 1980a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1981a0ffb263SMatthias Ringwald log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled); 1982a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1983aa4dd815SMatthias Ringwald break; 1984aa4dd815SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 1985a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1986a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 198760ebb071SMilanka Ringwald log_info("HF speaker gain = %u", hfp_connection->speaker_gain); 1988aa4dd815SMatthias Ringwald break; 1989aa4dd815SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 1990a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1991a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 199260ebb071SMilanka Ringwald log_info("HF microphone gain = %u", hfp_connection->microphone_gain); 1993aa4dd815SMatthias Ringwald break; 1994aa4dd815SMatthias Ringwald default: 1995aa4dd815SMatthias Ringwald break; 19963deb3ec6SMatthias Ringwald } 19973deb3ec6SMatthias Ringwald } 19980cef86faSMatthias Ringwald } 19993deb3ec6SMatthias Ringwald 2000aa4dd815SMatthias Ringwald static void hfp_run(void){ 2001d63c37a1SMatthias Ringwald btstack_linked_list_iterator_t it; 2002d63c37a1SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2003d63c37a1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2004d63c37a1SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 200522387625SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2006f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 20073deb3ec6SMatthias Ringwald } 20083deb3ec6SMatthias Ringwald } 20093deb3ec6SMatthias Ringwald 2010e9c22d4eSMatthias Ringwald static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 20113deb3ec6SMatthias Ringwald switch (packet_type){ 20123deb3ec6SMatthias Ringwald case RFCOMM_DATA_PACKET: 2013e9c22d4eSMatthias Ringwald hfp_ag_handle_rfcomm_data(packet_type, channel, packet, size); 20143deb3ec6SMatthias Ringwald break; 20153deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 2016e30a6a47SMatthias Ringwald if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 2017e30a6a47SMatthias Ringwald uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 2018f0aeb307SMatthias Ringwald hfp_ag_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid)); 2019e30a6a47SMatthias Ringwald return; 2020e30a6a47SMatthias Ringwald } 202127950165SMatthias Ringwald hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_AG); 2022aa4dd815SMatthias Ringwald break; 20233deb3ec6SMatthias Ringwald default: 20243deb3ec6SMatthias Ringwald break; 20253deb3ec6SMatthias Ringwald } 20263deb3ec6SMatthias Ringwald 20273deb3ec6SMatthias Ringwald hfp_run(); 20283deb3ec6SMatthias Ringwald } 20293deb3ec6SMatthias Ringwald 2030405014fbSMatthias Ringwald static void hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2031405014fbSMatthias Ringwald hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_AG); 203205bed9f6SMatthias Ringwald 203305bed9f6SMatthias Ringwald // allow for sco established -> ring transition 203405bed9f6SMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 203505bed9f6SMatthias Ringwald if (hci_event_packet_get_type(packet) != HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE) return; 203605bed9f6SMatthias Ringwald hfp_run(); 2037405014fbSMatthias Ringwald } 2038405014fbSMatthias Ringwald 2039a0ffb263SMatthias Ringwald void hfp_ag_init_codecs(int codecs_nr, uint8_t * codecs){ 20403deb3ec6SMatthias Ringwald if (codecs_nr > HFP_MAX_NUM_CODECS){ 20413deb3ec6SMatthias Ringwald log_error("hfp_init: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS); 20423deb3ec6SMatthias Ringwald return; 20433deb3ec6SMatthias Ringwald } 20443deb3ec6SMatthias Ringwald int i; 2045a0ffb263SMatthias Ringwald hfp_codecs_nr = codecs_nr; 20463deb3ec6SMatthias Ringwald for (i=0; i < codecs_nr; i++){ 20473deb3ec6SMatthias Ringwald hfp_codecs[i] = codecs[i]; 20483deb3ec6SMatthias Ringwald } 2049a0ffb263SMatthias Ringwald } 20503deb3ec6SMatthias Ringwald 2051a0ffb263SMatthias Ringwald void hfp_ag_init_supported_features(uint32_t supported_features){ 2052a0ffb263SMatthias Ringwald hfp_supported_features = supported_features; 2053a0ffb263SMatthias Ringwald } 20543deb3ec6SMatthias Ringwald 2055a0ffb263SMatthias Ringwald void hfp_ag_init_ag_indicators(int ag_indicators_nr, hfp_ag_indicator_t * ag_indicators){ 2056a0ffb263SMatthias Ringwald hfp_ag_indicators_nr = ag_indicators_nr; 20576535961aSMatthias Ringwald (void)memcpy(hfp_ag_indicators, ag_indicators, 20586535961aSMatthias Ringwald ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 2059a0ffb263SMatthias Ringwald } 20603deb3ec6SMatthias Ringwald 2061a0ffb263SMatthias Ringwald void hfp_ag_init_hf_indicators(int hf_indicators_nr, hfp_generic_status_indicator_t * hf_indicators){ 206225789943SMilanka Ringwald if (hf_indicators_nr > HFP_MAX_NUM_INDICATORS) return; 2063a0ffb263SMatthias Ringwald hfp_generic_status_indicators_nr = hf_indicators_nr; 20646535961aSMatthias Ringwald (void)memcpy(hfp_generic_status_indicators, hf_indicators, 20656535961aSMatthias Ringwald hf_indicators_nr * sizeof(hfp_generic_status_indicator_t)); 2066a0ffb263SMatthias Ringwald } 2067a0ffb263SMatthias Ringwald 2068a0ffb263SMatthias Ringwald void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){ 20693deb3ec6SMatthias Ringwald hfp_ag_call_hold_services_nr = call_hold_services_nr; 20706535961aSMatthias Ringwald (void)memcpy(hfp_ag_call_hold_services, call_hold_services, 20716535961aSMatthias Ringwald call_hold_services_nr * sizeof(char *)); 2072a0ffb263SMatthias Ringwald } 2073a0ffb263SMatthias Ringwald 2074a0ffb263SMatthias Ringwald 2075a0ffb263SMatthias Ringwald void hfp_ag_init(uint16_t rfcomm_channel_nr){ 207627950165SMatthias Ringwald 2077520c92d5SMatthias Ringwald hfp_init(); 2078d63c37a1SMatthias Ringwald 2079405014fbSMatthias Ringwald hci_event_callback_registration.callback = &hci_event_packet_handler; 208027950165SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 208127950165SMatthias Ringwald 2082e9c22d4eSMatthias Ringwald rfcomm_register_service(&rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 208327950165SMatthias Ringwald 208427950165SMatthias Ringwald // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us 2085e9c22d4eSMatthias Ringwald hfp_set_ag_rfcomm_packet_handler(&rfcomm_packet_handler); 2086aa4dd815SMatthias Ringwald 2087d210d9c4SMatthias Ringwald hfp_ag_response_and_hold_active = 0; 2088d210d9c4SMatthias Ringwald subscriber_numbers = NULL; 2089d210d9c4SMatthias Ringwald subscriber_numbers_count = 0; 2090d210d9c4SMatthias Ringwald 2091d210d9c4SMatthias Ringwald hfp_gsm_init(); 20923deb3ec6SMatthias Ringwald } 20933deb3ec6SMatthias Ringwald 20943deb3ec6SMatthias Ringwald void hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){ 2095323d3000SMatthias Ringwald hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE, HFP_ROLE_AG); 20963deb3ec6SMatthias Ringwald } 20973deb3ec6SMatthias Ringwald 2098d97d752dSMilanka Ringwald void hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){ 20999c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2100a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2101a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2102a33eb0c4SMilanka Ringwald return; 2103a33eb0c4SMilanka Ringwald } 2104a0ffb263SMatthias Ringwald hfp_release_service_level_connection(hfp_connection); 2105f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 21063deb3ec6SMatthias Ringwald } 21073deb3ec6SMatthias Ringwald 2108d97d752dSMilanka Ringwald void hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, hfp_cme_error_t error){ 21099c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2110a0ffb263SMatthias Ringwald if (!hfp_connection){ 2111a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 21123deb3ec6SMatthias Ringwald return; 21133deb3ec6SMatthias Ringwald } 2114a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 2115a0ffb263SMatthias Ringwald if (!hfp_connection->enable_extended_audio_gateway_error_report){ 21163deb3ec6SMatthias Ringwald return; 21173deb3ec6SMatthias Ringwald } 2118a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = error; 2119f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 21203deb3ec6SMatthias Ringwald } 21213deb3ec6SMatthias Ringwald 2122a0ffb263SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){ 21232ee45eb2SMilanka Ringwald log_info("hfp_ag_setup_audio_connection state %u", hfp_connection->state); 2124a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return; 2125a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return; 21263deb3ec6SMatthias Ringwald 2127a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 1; 2128a0ffb263SMatthias Ringwald if (!has_codec_negotiation_feature(hfp_connection)){ 2129d6ff09e1SMatthias Ringwald log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using CVSD"); 2130d6ff09e1SMatthias Ringwald hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 2131a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 21327522e673SMatthias Ringwald // now, pick link settings 2133afac2a04SMilanka Ringwald hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection)); 21342ee45eb2SMilanka Ringwald return; 21353deb3ec6SMatthias Ringwald } 2136aa4dd815SMatthias Ringwald 21372ee45eb2SMilanka Ringwald hfp_connection->trigger_codec_exchange = 1; 2138aa4dd815SMatthias Ringwald } 2139aa4dd815SMatthias Ringwald 2140d97d752dSMilanka Ringwald void hfp_ag_establish_audio_connection(hci_con_handle_t acl_handle){ 21419c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2142a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2143a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2144a33eb0c4SMilanka Ringwald return; 2145a33eb0c4SMilanka Ringwald } 21462ee45eb2SMilanka Ringwald hfp_connection->trigger_codec_exchange = 0; 2147a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 2148a0ffb263SMatthias Ringwald hfp_ag_setup_audio_connection(hfp_connection); 2149f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 21503deb3ec6SMatthias Ringwald } 21513deb3ec6SMatthias Ringwald 2152d97d752dSMilanka Ringwald void hfp_ag_release_audio_connection(hci_con_handle_t acl_handle){ 21539c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2154a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2155a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2156a33eb0c4SMilanka Ringwald return; 2157a33eb0c4SMilanka Ringwald } 2158a0ffb263SMatthias Ringwald hfp_release_audio_connection(hfp_connection); 2159f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 21603deb3ec6SMatthias Ringwald } 2161aa4dd815SMatthias Ringwald 2162aa4dd815SMatthias Ringwald /** 2163aa4dd815SMatthias Ringwald * @brief Enable in-band ring tone 2164aa4dd815SMatthias Ringwald */ 2165aa4dd815SMatthias Ringwald void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){ 2166aa4dd815SMatthias Ringwald if (get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE) == use_in_band_ring_tone){ 2167aa4dd815SMatthias Ringwald return; 2168aa4dd815SMatthias Ringwald } 2169aa4dd815SMatthias Ringwald hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE, use_in_band_ring_tone); 2170aa4dd815SMatthias Ringwald 2171665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 2172665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2173665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2174a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 217566c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2176a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING; 2177f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2178aa4dd815SMatthias Ringwald } 2179aa4dd815SMatthias Ringwald } 2180aa4dd815SMatthias Ringwald 2181aa4dd815SMatthias Ringwald /** 2182aa4dd815SMatthias Ringwald * @brief Called from GSM 2183aa4dd815SMatthias Ringwald */ 2184aa4dd815SMatthias Ringwald void hfp_ag_incoming_call(void){ 2185aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL); 2186aa4dd815SMatthias Ringwald } 2187aa4dd815SMatthias Ringwald 2188aa4dd815SMatthias Ringwald /** 2189aa4dd815SMatthias Ringwald * @brief number is stored. 2190aa4dd815SMatthias Ringwald */ 2191aa4dd815SMatthias Ringwald void hfp_ag_set_clip(uint8_t type, const char * number){ 2192d0c20769SMatthias Ringwald hfp_gsm_handle_event_with_clip(HFP_AG_SET_CLIP, type, number); 2193aa4dd815SMatthias Ringwald } 2194aa4dd815SMatthias Ringwald 2195aa4dd815SMatthias Ringwald void hfp_ag_call_dropped(void){ 2196aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL); 2197aa4dd815SMatthias Ringwald } 2198aa4dd815SMatthias Ringwald 2199aa4dd815SMatthias Ringwald // call from AG UI 2200aa4dd815SMatthias Ringwald void hfp_ag_answer_incoming_call(void){ 2201aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL); 2202aa4dd815SMatthias Ringwald } 2203aa4dd815SMatthias Ringwald 2204ce263fc8SMatthias Ringwald void hfp_ag_join_held_call(void){ 2205ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL); 2206ce263fc8SMatthias Ringwald } 2207ce263fc8SMatthias Ringwald 2208aa4dd815SMatthias Ringwald void hfp_ag_terminate_call(void){ 2209aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL); 2210aa4dd815SMatthias Ringwald } 2211aa4dd815SMatthias Ringwald 2212aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_ringing(void){ 2213aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL); 2214aa4dd815SMatthias Ringwald } 2215aa4dd815SMatthias Ringwald 2216aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_established(void){ 2217aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL); 2218aa4dd815SMatthias Ringwald } 2219aa4dd815SMatthias Ringwald 2220aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_rejected(void){ 2221aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL); 2222aa4dd815SMatthias Ringwald } 2223aa4dd815SMatthias Ringwald 2224aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_accepted(void){ 2225aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL); 2226aa4dd815SMatthias Ringwald } 2227ce263fc8SMatthias Ringwald 2228ce263fc8SMatthias Ringwald void hfp_ag_hold_incoming_call(void){ 2229ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL); 2230ce263fc8SMatthias Ringwald } 2231ce263fc8SMatthias Ringwald 2232ce263fc8SMatthias Ringwald void hfp_ag_accept_held_incoming_call(void) { 2233ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL); 2234ce263fc8SMatthias Ringwald } 2235ce263fc8SMatthias Ringwald 2236ce263fc8SMatthias Ringwald void hfp_ag_reject_held_incoming_call(void){ 2237ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL); 2238ce263fc8SMatthias Ringwald } 2239ce263fc8SMatthias Ringwald 2240aa4dd815SMatthias Ringwald static void hfp_ag_set_ag_indicator(const char * name, int value){ 2241aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name(name); 2242aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 2243aa4dd815SMatthias Ringwald hfp_ag_indicators[indicator_index].status = value; 2244aa4dd815SMatthias Ringwald 2245ce263fc8SMatthias Ringwald 2246665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 2247665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2248665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2249a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 225066c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2251a0ffb263SMatthias Ringwald if (! hfp_connection->ag_indicators[indicator_index].enabled) { 2252ce263fc8SMatthias Ringwald log_info("AG indicator '%s' changed to %u but not enabled", hfp_ag_indicators[indicator_index].name, value); 2253ce263fc8SMatthias Ringwald continue; 2254ce263fc8SMatthias Ringwald } 2255ce263fc8SMatthias Ringwald log_info("AG indicator '%s' changed to %u, request transfer statur", hfp_ag_indicators[indicator_index].name, value); 2256a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 2257f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2258aa4dd815SMatthias Ringwald } 2259aa4dd815SMatthias Ringwald } 2260aa4dd815SMatthias Ringwald 2261aa4dd815SMatthias Ringwald void hfp_ag_set_registration_status(int status){ 2262aa4dd815SMatthias Ringwald hfp_ag_set_ag_indicator("service", status); 2263aa4dd815SMatthias Ringwald } 2264aa4dd815SMatthias Ringwald 2265aa4dd815SMatthias Ringwald void hfp_ag_set_signal_strength(int strength){ 2266aa4dd815SMatthias Ringwald hfp_ag_set_ag_indicator("signal", strength); 2267aa4dd815SMatthias Ringwald } 2268aa4dd815SMatthias Ringwald 2269aa4dd815SMatthias Ringwald void hfp_ag_set_roaming_status(int status){ 2270aa4dd815SMatthias Ringwald hfp_ag_set_ag_indicator("roam", status); 2271aa4dd815SMatthias Ringwald } 2272aa4dd815SMatthias Ringwald 2273aa4dd815SMatthias Ringwald void hfp_ag_set_battery_level(int level){ 2274aa4dd815SMatthias Ringwald hfp_ag_set_ag_indicator("battchg", level); 2275aa4dd815SMatthias Ringwald } 2276aa4dd815SMatthias Ringwald 2277d97d752dSMilanka Ringwald void hfp_ag_activate_voice_recognition(hci_con_handle_t acl_handle, int activate){ 2278aa4dd815SMatthias Ringwald if (!get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)) return; 22799c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2280a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2281a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2282a33eb0c4SMilanka Ringwald return; 2283a33eb0c4SMilanka Ringwald } 2284aa4dd815SMatthias Ringwald 2285a0ffb263SMatthias Ringwald if (!get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION)) { 228660ebb071SMilanka Ringwald log_info("AG cannot acivate voice recognition - not supported by HF"); 2287aa4dd815SMatthias Ringwald return; 2288aa4dd815SMatthias Ringwald } 2289aa4dd815SMatthias Ringwald 2290aa4dd815SMatthias Ringwald if (activate){ 2291d97d752dSMilanka Ringwald hfp_ag_establish_audio_connection(acl_handle); 2292aa4dd815SMatthias Ringwald } 2293aa4dd815SMatthias Ringwald 2294a0ffb263SMatthias Ringwald hfp_connection->ag_activate_voice_recognition = activate; 2295a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION; 2296f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2297aa4dd815SMatthias Ringwald } 2298aa4dd815SMatthias Ringwald 2299d97d752dSMilanka Ringwald void hfp_ag_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 23009c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2301a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2302a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2303a33eb0c4SMilanka Ringwald return; 2304a33eb0c4SMilanka Ringwald } 2305a0ffb263SMatthias Ringwald if (hfp_connection->microphone_gain != gain){ 2306a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_SET_MICROPHONE_GAIN; 2307a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = gain; 2308a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 2309aa4dd815SMatthias Ringwald } 2310f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2311aa4dd815SMatthias Ringwald } 2312aa4dd815SMatthias Ringwald 2313d97d752dSMilanka Ringwald void hfp_ag_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 23149c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2315a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2316a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2317a33eb0c4SMilanka Ringwald return; 2318a33eb0c4SMilanka Ringwald } 2319a0ffb263SMatthias Ringwald if (hfp_connection->speaker_gain != gain){ 2320a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = gain; 2321a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 2322aa4dd815SMatthias Ringwald } 2323f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2324aa4dd815SMatthias Ringwald } 2325aa4dd815SMatthias Ringwald 2326d97d752dSMilanka Ringwald void hfp_ag_send_phone_number_for_voice_tag(hci_con_handle_t acl_handle, const char * number){ 23279c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2328a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2329a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2330a33eb0c4SMilanka Ringwald return; 2331a33eb0c4SMilanka Ringwald } 2332aa4dd815SMatthias Ringwald hfp_ag_set_clip(0, number); 2333a0ffb263SMatthias Ringwald hfp_connection->send_phone_number_for_voice_tag = 1; 2334aa4dd815SMatthias Ringwald } 2335aa4dd815SMatthias Ringwald 2336d97d752dSMilanka Ringwald void hfp_ag_reject_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 23379c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2338a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2339a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2340a33eb0c4SMilanka Ringwald return; 2341a33eb0c4SMilanka Ringwald } 2342a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 2343aa4dd815SMatthias Ringwald } 2344aa4dd815SMatthias Ringwald 2345d97d752dSMilanka Ringwald void hfp_ag_send_dtmf_code_done(hci_con_handle_t acl_handle){ 23469c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2347a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2348a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2349a33eb0c4SMilanka Ringwald return; 2350a33eb0c4SMilanka Ringwald } 2351a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2352c1797c7dSMatthias Ringwald } 2353aa4dd815SMatthias Ringwald 2354ce263fc8SMatthias Ringwald void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){ 2355ce263fc8SMatthias Ringwald subscriber_numbers = numbers; 2356ce263fc8SMatthias Ringwald subscriber_numbers_count = numbers_count; 2357ce263fc8SMatthias Ringwald } 2358ce263fc8SMatthias Ringwald 23599cae807eSMatthias Ringwald void hfp_ag_clear_last_dialed_number(void){ 23609cae807eSMatthias Ringwald hfp_gsm_clear_last_dialed_number(); 2361ce263fc8SMatthias Ringwald } 2362ce263fc8SMatthias Ringwald 2363d97d752dSMilanka Ringwald void hfp_ag_notify_incoming_call_waiting(hci_con_handle_t acl_handle){ 23649c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2365a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2366a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2367a33eb0c4SMilanka Ringwald return; 2368a33eb0c4SMilanka Ringwald } 2369a0ffb263SMatthias Ringwald if (!hfp_connection->call_waiting_notification_enabled) return; 2370a0ffb263SMatthias Ringwald 2371a0ffb263SMatthias Ringwald hfp_connection->ag_notify_incoming_call_waiting = 1; 2372f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection); 2373a0ffb263SMatthias Ringwald } 2374ce263fc8SMatthias Ringwald 237576cc1527SMatthias 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){ 237676cc1527SMatthias Ringwald if (!name){ 237776cc1527SMatthias Ringwald name = default_hfp_ag_service_name; 237876cc1527SMatthias Ringwald } 237976cc1527SMatthias Ringwald hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, rfcomm_channel_nr, name); 238076cc1527SMatthias Ringwald 238176cc1527SMatthias Ringwald /* 238276cc1527SMatthias Ringwald * 0x01 – Ability to reject a call 238376cc1527SMatthias Ringwald * 0x00 – No ability to reject a call 238476cc1527SMatthias Ringwald */ 238576cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301); // Hands-Free Profile - Network 238676cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call); 238776cc1527SMatthias Ringwald 238876cc1527SMatthias Ringwald // Construct SupportedFeatures for SDP bitmap: 238976cc1527SMatthias Ringwald // 239076cc1527SMatthias Ringwald // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 239176cc1527SMatthias Ringwald // of the Bits 0 to 4 of the unsolicited result code +BRSF" 239276cc1527SMatthias Ringwald // 239376cc1527SMatthias Ringwald // Wide band speech (bit 5) requires Codec negotiation 239476cc1527SMatthias Ringwald // 239576cc1527SMatthias Ringwald uint16_t sdp_features = supported_features & 0x1f; 239676cc1527SMatthias Ringwald if (wide_band_speech && (supported_features & (1 << HFP_AGSF_CODEC_NEGOTIATION))){ 239776cc1527SMatthias Ringwald sdp_features |= 1 << 5; 239876cc1527SMatthias Ringwald } 239976cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 240076cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 240176cc1527SMatthias Ringwald } 240276cc1527SMatthias Ringwald 240376cc1527SMatthias Ringwald void hfp_ag_register_packet_handler(btstack_packet_handler_t callback){ 240476cc1527SMatthias Ringwald if (callback == NULL){ 240576cc1527SMatthias Ringwald log_error("hfp_ag_register_packet_handler called with NULL callback"); 240676cc1527SMatthias Ringwald return; 240776cc1527SMatthias Ringwald } 240876cc1527SMatthias Ringwald hfp_ag_callback = callback; 240976cc1527SMatthias Ringwald hfp_set_ag_callback(callback); 241076cc1527SMatthias Ringwald } 2411