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 38ab2c6ae4SMatthias 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 <stdio.h> 503deb3ec6SMatthias Ringwald #include <stdlib.h> 513deb3ec6SMatthias Ringwald #include <string.h> 523deb3ec6SMatthias Ringwald 5356042629SMatthias Ringwald #include "hci_cmd.h" 5482636622SMatthias Ringwald #include "btstack_run_loop.h" 553deb3ec6SMatthias Ringwald 56235946f1SMatthias Ringwald #include "bluetooth_sdp.h" 573deb3ec6SMatthias Ringwald #include "hci.h" 583deb3ec6SMatthias Ringwald #include "btstack_memory.h" 593deb3ec6SMatthias Ringwald #include "hci_dump.h" 603deb3ec6SMatthias Ringwald #include "l2cap.h" 6116ece135SMatthias Ringwald #include "btstack_debug.h" 62e30a6a47SMatthias Ringwald #include "btstack_event.h" 6359c6af15SMatthias Ringwald #include "classic/core.h" 643edc84c5SMatthias Ringwald #include "classic/hfp.h" 653edc84c5SMatthias Ringwald #include "classic/hfp_ag.h" 66023f2764SMatthias Ringwald #include "classic/hfp_gsm_model.h" 67efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h" 68023f2764SMatthias Ringwald #include "classic/sdp_server.h" 69023f2764SMatthias Ringwald #include "classic/sdp_util.h" 703deb3ec6SMatthias Ringwald 71c5b64319SMatthias Ringwald // private prototypes 72adaba9f3SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t *hfp_connection); 73adaba9f3SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection); 74adaba9f3SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection); 75c5b64319SMatthias Ringwald 76c5b64319SMatthias Ringwald // public prototypes 7735833313SMatthias Ringwald hfp_generic_status_indicator_t * get_hfp_generic_status_indicators(void); 7835833313SMatthias Ringwald int get_hfp_generic_status_indicators_nr(void); 79c5b64319SMatthias Ringwald void set_hfp_generic_status_indicators(hfp_generic_status_indicator_t * indicators, int indicator_nr); 80c5b64319SMatthias Ringwald void set_hfp_ag_indicators(hfp_ag_indicator_t * indicators, int indicator_nr); 81c5b64319SMatthias Ringwald int get_hfp_ag_indicators_nr(hfp_connection_t * context); 82c5b64319SMatthias Ringwald hfp_ag_indicator_t * get_hfp_ag_indicators(hfp_connection_t * context); 83c5b64319SMatthias Ringwald 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; 933deb3ec6SMatthias Ringwald static hfp_ag_indicator_t hfp_ag_indicators[HFP_MAX_NUM_AG_INDICATORS]; 943deb3ec6SMatthias Ringwald 95a0ffb263SMatthias Ringwald static int hfp_generic_status_indicators_nr = 0; 96a0ffb263SMatthias Ringwald static hfp_generic_status_indicator_t hfp_generic_status_indicators[HFP_MAX_NUM_HF_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 10999a10067SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection); 1103deb3ec6SMatthias Ringwald 1113deb3ec6SMatthias Ringwald 112a0ffb263SMatthias Ringwald static int hfp_ag_get_ag_indicators_nr(hfp_connection_t * hfp_connection){ 113a0ffb263SMatthias Ringwald if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){ 114a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr; 115a0ffb263SMatthias Ringwald memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 1163deb3ec6SMatthias Ringwald } 117a0ffb263SMatthias Ringwald return hfp_connection->ag_indicators_nr; 118a0ffb263SMatthias Ringwald } 119a0ffb263SMatthias Ringwald 120a0ffb263SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection){ 121a0ffb263SMatthias Ringwald // TODO: save only value, and value changed in the hfp_connection? 122a0ffb263SMatthias Ringwald if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){ 123a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr; 124a0ffb263SMatthias Ringwald memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 125a0ffb263SMatthias Ringwald } 126a0ffb263SMatthias Ringwald return (hfp_ag_indicator_t *)&(hfp_connection->ag_indicators); 1273deb3ec6SMatthias Ringwald } 1283deb3ec6SMatthias Ringwald 129aa4dd815SMatthias Ringwald static hfp_ag_indicator_t * get_ag_indicator_for_name(const char * name){ 130aa4dd815SMatthias Ringwald int i; 131aa4dd815SMatthias Ringwald for (i = 0; i < hfp_ag_indicators_nr; i++){ 132aa4dd815SMatthias Ringwald if (strcmp(hfp_ag_indicators[i].name, name) == 0){ 133aa4dd815SMatthias Ringwald return &hfp_ag_indicators[i]; 134aa4dd815SMatthias Ringwald } 135aa4dd815SMatthias Ringwald } 136aa4dd815SMatthias Ringwald return NULL; 137aa4dd815SMatthias Ringwald } 138aa4dd815SMatthias Ringwald 139aa4dd815SMatthias Ringwald static int get_ag_indicator_index_for_name(const char * name){ 140aa4dd815SMatthias Ringwald int i; 141aa4dd815SMatthias Ringwald for (i = 0; i < hfp_ag_indicators_nr; i++){ 142aa4dd815SMatthias Ringwald if (strcmp(hfp_ag_indicators[i].name, name) == 0){ 143aa4dd815SMatthias Ringwald return i; 144aa4dd815SMatthias Ringwald } 145aa4dd815SMatthias Ringwald } 146aa4dd815SMatthias Ringwald return -1; 147aa4dd815SMatthias Ringwald } 148aa4dd815SMatthias Ringwald 149*9c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_ag_connection_context_for_acl_handle(uint16_t handle){ 150*9c9c64c1SMatthias Ringwald btstack_linked_list_iterator_t it; 151*9c9c64c1SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 152*9c9c64c1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 153*9c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 154*9c9c64c1SMatthias Ringwald if (hfp_connection->acl_handle != handle) continue; 155*9c9c64c1SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 156*9c9c64c1SMatthias Ringwald return hfp_connection; 157*9c9c64c1SMatthias Ringwald } 158*9c9c64c1SMatthias Ringwald return NULL; 159*9c9c64c1SMatthias Ringwald } 1603deb3ec6SMatthias Ringwald 16113839019SMatthias Ringwald void hfp_ag_register_packet_handler(btstack_packet_handler_t callback){ 1623deb3ec6SMatthias Ringwald if (callback == NULL){ 1633deb3ec6SMatthias Ringwald log_error("hfp_ag_register_packet_handler called with NULL callback"); 1643deb3ec6SMatthias Ringwald return; 1653deb3ec6SMatthias Ringwald } 166ca59be51SMatthias Ringwald hfp_ag_callback = callback; 167ca59be51SMatthias Ringwald hfp_set_ag_callback(callback); 1683deb3ec6SMatthias 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 1924f84bf36SMatthias 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){ 1933deb3ec6SMatthias Ringwald if (!name){ 1943deb3ec6SMatthias Ringwald name = default_hfp_ag_service_name; 1953deb3ec6SMatthias Ringwald } 196235946f1SMatthias Ringwald hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, rfcomm_channel_nr, name); 1973deb3ec6SMatthias Ringwald 1983deb3ec6SMatthias Ringwald /* 1993deb3ec6SMatthias Ringwald * 0x01 – Ability to reject a call 2003deb3ec6SMatthias Ringwald * 0x00 – No ability to reject a call 2013deb3ec6SMatthias Ringwald */ 202aa4dd815SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301); // Hands-Free Profile - Network 203aa4dd815SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call); 204aa4dd815SMatthias Ringwald 2054f84bf36SMatthias Ringwald // Construct SupportedFeatures for SDP bitmap: 2064f84bf36SMatthias Ringwald // 2074f84bf36SMatthias Ringwald // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 2084f84bf36SMatthias Ringwald // of the Bits 0 to 4 of the unsolicited result code +BRSF" 2094f84bf36SMatthias Ringwald // 2104f84bf36SMatthias Ringwald uint16_t sdp_features = supported_features &0x1f; 2114f84bf36SMatthias Ringwald if (supported_features & wide_band_speech){ 2124f84bf36SMatthias Ringwald sdp_features |= 1 << 5; // Wide band speech bit 2134f84bf36SMatthias Ringwald } 214aa4dd815SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 2154f84bf36SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 216aa4dd815SMatthias Ringwald } 217aa4dd815SMatthias Ringwald 218485ac19eSMilanka Ringwald static int hfp_ag_send_change_in_band_ring_tone_setting_cmd(uint16_t cid){ 219aa4dd815SMatthias Ringwald char buffer[20]; 220aa4dd815SMatthias Ringwald sprintf(buffer, "\r\n%s:%d\r\n", HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone()); 221aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2223deb3ec6SMatthias Ringwald } 2233deb3ec6SMatthias Ringwald 2243deb3ec6SMatthias Ringwald static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){ 2253deb3ec6SMatthias Ringwald char buffer[40]; 2263deb3ec6SMatthias Ringwald sprintf(buffer, "\r\n%s:%d\r\n\r\nOK\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features); 2273deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2283deb3ec6SMatthias Ringwald } 2293deb3ec6SMatthias Ringwald 230485ac19eSMilanka Ringwald static int hfp_ag_send_ok(uint16_t cid){ 2313deb3ec6SMatthias Ringwald char buffer[10]; 2323deb3ec6SMatthias Ringwald sprintf(buffer, "\r\nOK\r\n"); 2333deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2343deb3ec6SMatthias Ringwald } 2353deb3ec6SMatthias Ringwald 236485ac19eSMilanka Ringwald static int hfp_ag_send_ring(uint16_t cid){ 237aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, (char *) "\r\nRING\r\n"); 238aa4dd815SMatthias Ringwald } 239aa4dd815SMatthias Ringwald 240aa4dd815SMatthias Ringwald static int hfp_ag_send_clip(uint16_t cid){ 241aa4dd815SMatthias Ringwald char buffer[50]; 242d0c20769SMatthias Ringwald sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP, hfp_gsm_clip_number(), hfp_gsm_clip_type()); 243aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 244aa4dd815SMatthias Ringwald } 245aa4dd815SMatthias Ringwald 246ce263fc8SMatthias Ringwald static int hfp_send_subscriber_number_cmd(uint16_t cid, uint8_t type, const char * number){ 247ce263fc8SMatthias Ringwald char buffer[50]; 248ce263fc8SMatthias Ringwald sprintf(buffer, "\r\n%s: ,\"%s\",%u, , \r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type); 249ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 250ce263fc8SMatthias Ringwald } 251ce263fc8SMatthias Ringwald 252c1797c7dSMatthias Ringwald static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){ 253aa4dd815SMatthias Ringwald char buffer[50]; 254d0c20769SMatthias Ringwald sprintf(buffer, "\r\n%s: %s\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number()); 255aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 256aa4dd815SMatthias Ringwald } 257aa4dd815SMatthias Ringwald 258aa4dd815SMatthias Ringwald static int hfp_ag_send_call_waiting_notification(uint16_t cid){ 259aa4dd815SMatthias Ringwald char buffer[50]; 260a0ffb263SMatthias Ringwald sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(), hfp_gsm_clip_type()); 261aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 262aa4dd815SMatthias Ringwald } 263aa4dd815SMatthias Ringwald 264485ac19eSMilanka Ringwald static int hfp_ag_send_error(uint16_t cid){ 2653deb3ec6SMatthias Ringwald char buffer[10]; 2663deb3ec6SMatthias Ringwald sprintf(buffer, "\r\nERROR\r\n"); 2673deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2683deb3ec6SMatthias Ringwald } 2693deb3ec6SMatthias Ringwald 270485ac19eSMilanka Ringwald static int hfp_ag_send_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){ 2713deb3ec6SMatthias Ringwald char buffer[20]; 2723deb3ec6SMatthias Ringwald sprintf(buffer, "\r\n%s=%d\r\n", HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error); 2733deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2743deb3ec6SMatthias Ringwald } 2753deb3ec6SMatthias Ringwald 276ad902e3dSMatthias Ringwald // get size for indicator string 277a0ffb263SMatthias Ringwald static int hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection, int i){ 278ad902e3dSMatthias Ringwald // template: ("$NAME",($MIN,$MAX)) 27945718b6fSMatthias Ringwald return 8 + (int) strlen(hfp_ag_get_ag_indicators(hfp_connection)[i].name) 280a0ffb263SMatthias Ringwald + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range) 281a0ffb263SMatthias Ringwald + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range); 282ad902e3dSMatthias Ringwald } 283ad902e3dSMatthias Ringwald 284ad902e3dSMatthias Ringwald // store indicator 285a0ffb263SMatthias Ringwald static void hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection, int i, uint8_t * buffer){ 286ad902e3dSMatthias Ringwald sprintf((char *) buffer, "(\"%s\",(%d,%d)),", 287a0ffb263SMatthias Ringwald hfp_ag_get_ag_indicators(hfp_connection)[i].name, 288a0ffb263SMatthias Ringwald hfp_ag_get_ag_indicators(hfp_connection)[i].min_range, 289a0ffb263SMatthias Ringwald hfp_ag_get_ag_indicators(hfp_connection)[i].max_range); 2903deb3ec6SMatthias Ringwald } 291ad902e3dSMatthias Ringwald 292ad902e3dSMatthias Ringwald // structure: header [indicator [comma indicator]] footer 293a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_num_segments(hfp_connection_t * hfp_connection){ 294a0ffb263SMatthias Ringwald int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 295ad902e3dSMatthias Ringwald if (!num_indicators) return 2; 296ad902e3dSMatthias Ringwald return 3 + (num_indicators-1) * 2; 2973deb3ec6SMatthias Ringwald } 298ad902e3dSMatthias Ringwald 299ad902e3dSMatthias Ringwald // get size of individual segment for hfp_ag_retrieve_indicators_cmd 300a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_get_segment_len(hfp_connection_t * hfp_connection, int index){ 301ad902e3dSMatthias Ringwald if (index == 0) { 302ad902e3dSMatthias Ringwald return strlen(HFP_INDICATOR) + 3; // "\n\r%s:"" 303ad902e3dSMatthias Ringwald } 304ad902e3dSMatthias Ringwald index--; 305a0ffb263SMatthias Ringwald int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 306ad902e3dSMatthias Ringwald int indicator_index = index >> 1; 307ad902e3dSMatthias Ringwald if ((index & 1) == 0){ 308a0ffb263SMatthias Ringwald return hfp_ag_indicators_string_size(hfp_connection, indicator_index); 309ad902e3dSMatthias Ringwald } 310ad902e3dSMatthias Ringwald if (indicator_index == num_indicators - 1){ 311ad902e3dSMatthias Ringwald return 8; // "\r\n\r\nOK\r\n" 312ad902e3dSMatthias Ringwald } 313ad902e3dSMatthias Ringwald return 1; // comma 314ad902e3dSMatthias Ringwald } 315ad902e3dSMatthias Ringwald 316a0ffb263SMatthias Ringwald static void hgp_ag_indicators_cmd_generator_store_segment(hfp_connection_t * hfp_connection, int index, uint8_t * buffer){ 317ad902e3dSMatthias Ringwald if (index == 0){ 318ad902e3dSMatthias Ringwald *buffer++ = '\r'; 31974386ee0SMatthias Ringwald *buffer++ = '\n'; 320ad902e3dSMatthias Ringwald int len = strlen(HFP_INDICATOR); 321ad902e3dSMatthias Ringwald memcpy(buffer, HFP_INDICATOR, len); 322ad902e3dSMatthias Ringwald buffer += len; 323ad902e3dSMatthias Ringwald *buffer++ = ':'; 324ad902e3dSMatthias Ringwald return; 325ad902e3dSMatthias Ringwald } 326ad902e3dSMatthias Ringwald index--; 327a0ffb263SMatthias Ringwald int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 328ad902e3dSMatthias Ringwald int indicator_index = index >> 1; 329ad902e3dSMatthias Ringwald if ((index & 1) == 0){ 330a0ffb263SMatthias Ringwald hfp_ag_indicators_string_store(hfp_connection, indicator_index, buffer); 331ad902e3dSMatthias Ringwald return; 332ad902e3dSMatthias Ringwald } 333ad902e3dSMatthias Ringwald if (indicator_index == num_indicators-1){ 334ad902e3dSMatthias Ringwald memcpy(buffer, "\r\n\r\nOK\r\n", 8); 335ad902e3dSMatthias Ringwald return; 336ad902e3dSMatthias Ringwald } 337ad902e3dSMatthias Ringwald *buffer = ','; 3383deb3ec6SMatthias Ringwald } 3393deb3ec6SMatthias Ringwald 3403deb3ec6SMatthias Ringwald static int hfp_hf_indicators_join(char * buffer, int buffer_size){ 3413deb3ec6SMatthias Ringwald if (buffer_size < hfp_ag_indicators_nr * 3) return 0; 3423deb3ec6SMatthias Ringwald int i; 3433deb3ec6SMatthias Ringwald int offset = 0; 344a0ffb263SMatthias Ringwald for (i = 0; i < hfp_generic_status_indicators_nr-1; i++) { 345a0ffb263SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid); 3463deb3ec6SMatthias Ringwald } 347a0ffb263SMatthias Ringwald if (i < hfp_generic_status_indicators_nr){ 348a0ffb263SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid); 3493deb3ec6SMatthias Ringwald } 3503deb3ec6SMatthias Ringwald return offset; 3513deb3ec6SMatthias Ringwald } 3523deb3ec6SMatthias Ringwald 3533deb3ec6SMatthias Ringwald static int hfp_hf_indicators_initial_status_join(char * buffer, int buffer_size){ 354a0ffb263SMatthias Ringwald if (buffer_size < hfp_generic_status_indicators_nr * 3) return 0; 3553deb3ec6SMatthias Ringwald int i; 3563deb3ec6SMatthias Ringwald int offset = 0; 357a0ffb263SMatthias Ringwald for (i = 0; i < hfp_generic_status_indicators_nr; i++) { 358a0ffb263SMatthias 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); 3593deb3ec6SMatthias Ringwald } 3603deb3ec6SMatthias Ringwald return offset; 3613deb3ec6SMatthias Ringwald } 3623deb3ec6SMatthias Ringwald 3633deb3ec6SMatthias Ringwald static int hfp_ag_indicators_status_join(char * buffer, int buffer_size){ 3643deb3ec6SMatthias Ringwald if (buffer_size < hfp_ag_indicators_nr * 3) return 0; 3653deb3ec6SMatthias Ringwald int i; 3663deb3ec6SMatthias Ringwald int offset = 0; 3673deb3ec6SMatthias Ringwald for (i = 0; i < hfp_ag_indicators_nr-1; i++) { 3683deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_indicators[i].status); 3693deb3ec6SMatthias Ringwald } 3703deb3ec6SMatthias Ringwald if (i<hfp_ag_indicators_nr){ 3713deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_indicators[i].status); 3723deb3ec6SMatthias Ringwald } 3733deb3ec6SMatthias Ringwald return offset; 3743deb3ec6SMatthias Ringwald } 3753deb3ec6SMatthias Ringwald 3763deb3ec6SMatthias Ringwald static int hfp_ag_call_services_join(char * buffer, int buffer_size){ 3773deb3ec6SMatthias Ringwald if (buffer_size < hfp_ag_call_hold_services_nr * 3) return 0; 3783deb3ec6SMatthias Ringwald int i; 3793deb3ec6SMatthias Ringwald int offset = snprintf(buffer, buffer_size, "("); 3803deb3ec6SMatthias Ringwald for (i = 0; i < hfp_ag_call_hold_services_nr-1; i++) { 3813deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%s,", hfp_ag_call_hold_services[i]); 3823deb3ec6SMatthias Ringwald } 3833deb3ec6SMatthias Ringwald if (i<hfp_ag_call_hold_services_nr){ 3843deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%s)", hfp_ag_call_hold_services[i]); 3853deb3ec6SMatthias Ringwald } 3863deb3ec6SMatthias Ringwald return offset; 3873deb3ec6SMatthias Ringwald } 3883deb3ec6SMatthias Ringwald 389485ac19eSMilanka Ringwald static int hfp_ag_send_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, 390ad902e3dSMatthias Ringwald int start_segment, int num_segments, 391a0ffb263SMatthias Ringwald int (*get_segment_len)(hfp_connection_t * hfp_connection, int segment), 392a0ffb263SMatthias Ringwald void (*store_segment) (hfp_connection_t * hfp_connection, int segment, uint8_t * buffer)){ 3933deb3ec6SMatthias Ringwald 394ad902e3dSMatthias Ringwald // assumes: can send now == true 395ad902e3dSMatthias Ringwald // assumes: num segments > 0 396aba39421SMatthias Ringwald // assumes: individual segments are smaller than MTU 397ad902e3dSMatthias Ringwald rfcomm_reserve_packet_buffer(); 398ad902e3dSMatthias Ringwald int mtu = rfcomm_get_max_frame_size(cid); 399ad902e3dSMatthias Ringwald uint8_t * data = rfcomm_get_outgoing_buffer(); 400ad902e3dSMatthias Ringwald int offset = 0; 401ad902e3dSMatthias Ringwald int segment = start_segment; 402ad902e3dSMatthias Ringwald while (segment < num_segments){ 403a0ffb263SMatthias Ringwald int segment_len = get_segment_len(hfp_connection, segment); 404aba39421SMatthias Ringwald if (offset + segment_len > mtu) break; 405aba39421SMatthias Ringwald // append segement 406a0ffb263SMatthias Ringwald store_segment(hfp_connection, segment, data+offset); 407ad902e3dSMatthias Ringwald offset += segment_len; 408ad902e3dSMatthias Ringwald segment++; 409ad902e3dSMatthias Ringwald } 410ad902e3dSMatthias Ringwald rfcomm_send_prepared(cid, offset); 411ad902e3dSMatthias Ringwald return segment; 412ad902e3dSMatthias Ringwald } 4133deb3ec6SMatthias Ringwald 414ad902e3dSMatthias Ringwald // returns next segment to store 415485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, int start_segment){ 416a0ffb263SMatthias Ringwald int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection); 417485ac19eSMilanka Ringwald return hfp_ag_send_cmd_via_generator(cid, hfp_connection, start_segment, num_segments, 418ad902e3dSMatthias Ringwald hfp_ag_indicators_cmd_generator_get_segment_len, hgp_ag_indicators_cmd_generator_store_segment); 4193deb3ec6SMatthias Ringwald } 4203deb3ec6SMatthias Ringwald 421485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_status_cmd(uint16_t cid){ 4223deb3ec6SMatthias Ringwald char buffer[40]; 42389425bfcSMilanka Ringwald const int size = sizeof(buffer); 42489425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "\r\n%s:", HFP_INDICATOR); 42589425bfcSMilanka Ringwald offset += hfp_ag_indicators_status_join(buffer+offset, size-offset-9); 42689425bfcSMilanka Ringwald offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n"); 4273deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4283deb3ec6SMatthias Ringwald } 4293deb3ec6SMatthias Ringwald 430485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_can_hold_call_cmd(uint16_t cid){ 431ad902e3dSMatthias Ringwald char buffer[40]; 43289425bfcSMilanka Ringwald const int size = sizeof(buffer); 43389425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "\r\n%s:", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES); 43489425bfcSMilanka Ringwald offset += hfp_ag_call_services_join(buffer+offset, size-offset-9); 43589425bfcSMilanka Ringwald offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n"); 4363deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4373deb3ec6SMatthias Ringwald } 4383deb3ec6SMatthias Ringwald 4393deb3ec6SMatthias Ringwald 440485ac19eSMilanka Ringwald static int hfp_ag_send_list_supported_generic_status_indicators_cmd(uint16_t cid){ 441485ac19eSMilanka Ringwald return hfp_ag_send_ok(cid); 4423deb3ec6SMatthias Ringwald } 4433deb3ec6SMatthias Ringwald 444485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(uint16_t cid){ 4453deb3ec6SMatthias Ringwald char buffer[40]; 44689425bfcSMilanka Ringwald const int size = sizeof(buffer); 44789425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "\r\n%s:(", HFP_GENERIC_STATUS_INDICATOR); 44889425bfcSMilanka Ringwald offset += hfp_hf_indicators_join(buffer+offset, size-offset-10); 44989425bfcSMilanka Ringwald offset += snprintf(buffer+offset, size-offset, ")\r\n\r\nOK\r\n"); 4503deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4513deb3ec6SMatthias Ringwald } 4523deb3ec6SMatthias Ringwald 453485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid){ 4543deb3ec6SMatthias Ringwald char buffer[40]; 4551cc1d9e9SMilanka Ringwald int offset = hfp_hf_indicators_initial_status_join(buffer, sizeof(buffer) - 7); 4561cc1d9e9SMilanka Ringwald snprintf(buffer+offset, sizeof(buffer)-offset, "\r\nOK\r\n"); 4573deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4583deb3ec6SMatthias Ringwald } 4593deb3ec6SMatthias Ringwald 460485ac19eSMilanka Ringwald static int hfp_ag_send_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){ 4613deb3ec6SMatthias Ringwald char buffer[20]; 462aa4dd815SMatthias Ringwald sprintf(buffer, "\r\n%s:%d,%d\r\n", HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index, indicator->status); 4633deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4643deb3ec6SMatthias Ringwald } 4653deb3ec6SMatthias Ringwald 466485ac19eSMilanka Ringwald static int hfp_ag_send_report_network_operator_name_cmd(uint16_t cid, hfp_network_opearator_t op){ 4675e71456cSMilanka Ringwald char buffer[41]; 4683deb3ec6SMatthias Ringwald if (strlen(op.name) == 0){ 46989425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s:%d,,\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode); 4703deb3ec6SMatthias Ringwald } else { 47189425bfcSMilanka Ringwald int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:%d,%d,", HFP_QUERY_OPERATOR_SELECTION, op.mode, op.format); 4725e71456cSMilanka Ringwald offset += snprintf(buffer+offset, 16, "%s", op.name); 47389425bfcSMilanka Ringwald snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n"); 4743deb3ec6SMatthias Ringwald } 4753deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4763deb3ec6SMatthias Ringwald } 4773deb3ec6SMatthias Ringwald 4781cc1d9e9SMilanka Ringwald static inline int hfp_ag_send_cmd_with_int(uint16_t cid, const char * cmd, uint8_t value){ 4791cc1d9e9SMilanka Ringwald char buffer[30]; 48089425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n", cmd, value); 4811cc1d9e9SMilanka Ringwald return send_str_over_rfcomm(cid, buffer); 4821cc1d9e9SMilanka Ringwald } 4833deb3ec6SMatthias Ringwald 484485ac19eSMilanka Ringwald static int hfp_ag_send_suggest_codec_cmd(uint16_t cid, uint8_t codec){ 4851cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec); 4863deb3ec6SMatthias Ringwald } 4873deb3ec6SMatthias Ringwald 488485ac19eSMilanka Ringwald static int hfp_ag_send_activate_voice_recognition_cmd(uint16_t cid, uint8_t activate_voice_recognition){ 4891cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition); 490aa4dd815SMatthias Ringwald } 491aa4dd815SMatthias Ringwald 492485ac19eSMilanka Ringwald static int hfp_ag_send_set_speaker_gain_cmd(uint16_t cid, uint8_t gain){ 4931cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain); 494aa4dd815SMatthias Ringwald } 495aa4dd815SMatthias Ringwald 496485ac19eSMilanka Ringwald static int hfp_ag_send_set_microphone_gain_cmd(uint16_t cid, uint8_t gain){ 4971cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain); 498aa4dd815SMatthias Ringwald } 499aa4dd815SMatthias Ringwald 500485ac19eSMilanka Ringwald static int hfp_ag_send_set_response_and_hold(uint16_t cid, int state){ 5011cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_RESPONSE_AND_HOLD, state); 502ce263fc8SMatthias Ringwald } 503ce263fc8SMatthias Ringwald 504a0ffb263SMatthias Ringwald static uint8_t hfp_ag_suggest_codec(hfp_connection_t *hfp_connection){ 505bc1b1537SMilanka Ringwald if (hfp_connection->sco_for_msbc_failed) return HFP_CODEC_CVSD; 506bc1b1537SMilanka Ringwald 5076a7f44bdSMilanka Ringwald if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_codecs_nr, hfp_codecs)){ 5086a7f44bdSMilanka Ringwald if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){ 509df327ff3SMilanka Ringwald return HFP_CODEC_MSBC; 5103deb3ec6SMatthias Ringwald } 5113deb3ec6SMatthias Ringwald } 512df327ff3SMilanka Ringwald return HFP_CODEC_CVSD; 5133deb3ec6SMatthias Ringwald } 5143deb3ec6SMatthias Ringwald 5157522e673SMatthias Ringwald static void hfp_init_link_settings(hfp_connection_t * hfp_connection){ 5167522e673SMatthias Ringwald // determine highest possible link setting 5177522e673SMatthias Ringwald hfp_connection->link_setting = HFP_LINK_SETTINGS_D1; 518d9f77559SMatthias Ringwald // anything else requires eSCO support on both sides 519d9f77559SMatthias Ringwald if (hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){ 5207522e673SMatthias Ringwald switch (hfp_connection->negotiated_codec){ 5217522e673SMatthias Ringwald case HFP_CODEC_CVSD: 5227522e673SMatthias Ringwald hfp_connection->link_setting = HFP_LINK_SETTINGS_S3; 5237522e673SMatthias Ringwald if ((hfp_connection->remote_supported_features & (1<<HFP_HFSF_ESCO_S4)) 5247522e673SMatthias Ringwald && (hfp_supported_features & (1<<HFP_AGSF_ESCO_S4))){ 5257522e673SMatthias Ringwald hfp_connection->link_setting = HFP_LINK_SETTINGS_S4; 5267522e673SMatthias Ringwald } 5277522e673SMatthias Ringwald break; 5287522e673SMatthias Ringwald case HFP_CODEC_MSBC: 5297522e673SMatthias Ringwald hfp_connection->link_setting = HFP_LINK_SETTINGS_T2; 5307522e673SMatthias Ringwald break; 531d9f77559SMatthias Ringwald default: 532d9f77559SMatthias Ringwald break; 533d9f77559SMatthias Ringwald } 5347522e673SMatthias Ringwald } 5357522e673SMatthias Ringwald log_info("hfp_init_link_settings: %u", hfp_connection->link_setting); 5367522e673SMatthias Ringwald } 5377522e673SMatthias Ringwald 538a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){ 539aa4dd815SMatthias Ringwald /* events ( == commands): 540aa4dd815SMatthias Ringwald HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs 541aa4dd815SMatthias Ringwald HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 542aa4dd815SMatthias Ringwald hf_trigger_codec_connection_setup == received BCC 543aa4dd815SMatthias Ringwald ag_trigger_codec_connection_setup == received from AG to send BCS 544aa4dd815SMatthias Ringwald HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS 545aa4dd815SMatthias Ringwald */ 546aa4dd815SMatthias Ringwald 547a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 548aa4dd815SMatthias Ringwald case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 549a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC; 550aa4dd815SMatthias Ringwald break; 551aa4dd815SMatthias Ringwald case HFP_CODECS_AG_RESEND_COMMON_CODEC: 552a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC; 553aa4dd815SMatthias Ringwald break; 554aa4dd815SMatthias Ringwald default: 555aa4dd815SMatthias Ringwald break; 556aa4dd815SMatthias Ringwald } 557aa4dd815SMatthias Ringwald 558aa4dd815SMatthias Ringwald // printf(" -> State machine: CC\n"); 559aa4dd815SMatthias Ringwald 560a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 561aa4dd815SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 562a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){ 563a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_LIST; 564485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 565aa4dd815SMatthias Ringwald return 1; 566aa4dd815SMatthias Ringwald } 567aa4dd815SMatthias Ringwald 568a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 569aa4dd815SMatthias Ringwald case HFP_CODECS_AG_SENT_COMMON_CODEC: 570aa4dd815SMatthias Ringwald case HFP_CODECS_EXCHANGED: 571a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_AG_RESEND_COMMON_CODEC; 572aa4dd815SMatthias Ringwald break; 573aa4dd815SMatthias Ringwald default: 574aa4dd815SMatthias Ringwald break; 575aa4dd815SMatthias Ringwald } 576485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 577aa4dd815SMatthias Ringwald return 1; 578aa4dd815SMatthias Ringwald 579aa4dd815SMatthias Ringwald case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 580a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 581485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 582aa4dd815SMatthias Ringwald return 1; 583aa4dd815SMatthias Ringwald 584aa4dd815SMatthias Ringwald case HFP_CMD_AG_SEND_COMMON_CODEC: 585a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_AG_SENT_COMMON_CODEC; 586a0ffb263SMatthias Ringwald hfp_connection->suggested_codec = hfp_ag_suggest_codec(hfp_connection); 587485ac19eSMilanka Ringwald hfp_ag_send_suggest_codec_cmd(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec); 588aa4dd815SMatthias Ringwald return 1; 589aa4dd815SMatthias Ringwald 590aa4dd815SMatthias Ringwald case HFP_CMD_HF_CONFIRMED_CODEC: 591a0ffb263SMatthias Ringwald if (hfp_connection->codec_confirmed != hfp_connection->suggested_codec){ 592a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_ERROR; 593485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 594aa4dd815SMatthias Ringwald return 1; 595aa4dd815SMatthias Ringwald } 596a0ffb263SMatthias Ringwald hfp_connection->negotiated_codec = hfp_connection->codec_confirmed; 597a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 59803645b44SMatthias Ringwald log_info("hfp: codec confirmed: %s", hfp_connection->negotiated_codec == HFP_CODEC_MSBC ? "mSBC" : "CVSD"); 599485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 6007522e673SMatthias Ringwald // now, pick link settings 6017522e673SMatthias Ringwald hfp_init_link_settings(hfp_connection); 602aa4dd815SMatthias Ringwald return 1; 603aa4dd815SMatthias Ringwald default: 604aa4dd815SMatthias Ringwald break; 605aa4dd815SMatthias Ringwald } 606aa4dd815SMatthias Ringwald return 0; 607aa4dd815SMatthias Ringwald } 608aa4dd815SMatthias Ringwald 609a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){ 610a0ffb263SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 611ca59be51SMatthias Ringwald hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr); 612aa4dd815SMatthias Ringwald 613a0ffb263SMatthias Ringwald // if active call exist, set per-hfp_connection state active, too (when audio is on) 614d0c20769SMatthias Ringwald if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 615a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE; 616aa4dd815SMatthias Ringwald } 617ce263fc8SMatthias Ringwald // if AG is ringing, also start ringing on the HF 618d0c20769SMatthias Ringwald if (hfp_gsm_call_status() == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS && 619d0c20769SMatthias Ringwald hfp_gsm_callsetup_status() == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 620a0ffb263SMatthias Ringwald hfp_ag_hf_start_ringing(hfp_connection); 621ce263fc8SMatthias Ringwald } 622aa4dd815SMatthias Ringwald } 6233deb3ec6SMatthias Ringwald 624a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){ 625c79b4cabSMatthias Ringwald // log_info("hfp_ag_run_for_context_service_level_connection state %u, command %u", hfp_connection->state, hfp_connection->command); 626a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 627c04cf7ffSMilanka Ringwald int sent = 0; 628a0ffb263SMatthias Ringwald switch(hfp_connection->command){ 6293deb3ec6SMatthias Ringwald case HFP_CMD_SUPPORTED_FEATURES: 630a0ffb263SMatthias Ringwald switch(hfp_connection->state){ 6313deb3ec6SMatthias Ringwald case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 632aa4dd815SMatthias Ringwald case HFP_EXCHANGE_SUPPORTED_FEATURES: 633d715cf51SMatthias Ringwald hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr); 634a0ffb263SMatthias Ringwald if (has_codec_negotiation_feature(hfp_connection)){ 635a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS; 636aa4dd815SMatthias Ringwald } else { 637a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 638aa4dd815SMatthias Ringwald } 639a0ffb263SMatthias Ringwald hfp_ag_exchange_supported_features_cmd(hfp_connection->rfcomm_cid); 640aa4dd815SMatthias Ringwald return 1; 6413deb3ec6SMatthias Ringwald default: 6423deb3ec6SMatthias Ringwald break; 6433deb3ec6SMatthias Ringwald } 6443deb3ec6SMatthias Ringwald break; 6453deb3ec6SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 646c04cf7ffSMilanka Ringwald sent = codecs_exchange_state_machine(hfp_connection); 647a0ffb263SMatthias Ringwald if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){ 648a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 6493deb3ec6SMatthias Ringwald } 650c04cf7ffSMilanka Ringwald return sent; 651aa4dd815SMatthias Ringwald 652aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 653a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_W4_RETRIEVE_INDICATORS) { 654473ac565SMatthias Ringwald // HF requested AG Indicators and we did expect it 655473ac565SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 656473ac565SMatthias Ringwald // continue below in state switch 657ad902e3dSMatthias Ringwald } 658ad902e3dSMatthias Ringwald break; 659aa4dd815SMatthias Ringwald 660aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 661a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break; 662a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE; 663485ac19eSMilanka Ringwald hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid); 664aa4dd815SMatthias Ringwald return 1; 665aa4dd815SMatthias Ringwald 6663deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 667a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break; 668a0ffb263SMatthias Ringwald if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 669a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL; 670a0ffb263SMatthias Ringwald } else if (has_hf_indicators_feature(hfp_connection)){ 671a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 672aa4dd815SMatthias Ringwald } else { 673a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 6743deb3ec6SMatthias Ringwald } 6758a46ec40SMatthias Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 676aa4dd815SMatthias Ringwald return 1; 6773deb3ec6SMatthias Ringwald 678aa4dd815SMatthias Ringwald case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES: 679a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break; 680a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 681a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 682aa4dd815SMatthias Ringwald } 683485ac19eSMilanka Ringwald hfp_ag_send_retrieve_can_hold_call_cmd(hfp_connection->rfcomm_cid); 684a0ffb263SMatthias Ringwald if (!has_hf_indicators_feature(hfp_connection)){ 685a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 686ce263fc8SMatthias Ringwald } 687aa4dd815SMatthias Ringwald return 1; 688aa4dd815SMatthias Ringwald 689aa4dd815SMatthias Ringwald case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 690a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break; 691a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS; 692485ac19eSMilanka Ringwald hfp_ag_send_list_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 693aa4dd815SMatthias Ringwald return 1; 694aa4dd815SMatthias Ringwald 695aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 696a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break; 697a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 698485ac19eSMilanka Ringwald hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 699aa4dd815SMatthias Ringwald return 1; 700aa4dd815SMatthias Ringwald 701aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 702a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break; 703a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 704485ac19eSMilanka Ringwald hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 705aa4dd815SMatthias Ringwald return 1; 7063deb3ec6SMatthias Ringwald default: 7073deb3ec6SMatthias Ringwald break; 7083deb3ec6SMatthias Ringwald } 709473ac565SMatthias Ringwald 710473ac565SMatthias Ringwald switch (hfp_connection->state){ 711473ac565SMatthias Ringwald case HFP_RETRIEVE_INDICATORS: { 712485ac19eSMilanka Ringwald int next_segment = hfp_ag_send_retrieve_indicators_cmd_via_generator(hfp_connection->rfcomm_cid, hfp_connection, hfp_connection->send_ag_indicators_segment); 713473ac565SMatthias Ringwald int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection); 714473ac565SMatthias Ringwald log_info("HFP_CMD_RETRIEVE_AG_INDICATORS next segment %u, num_segments %u", next_segment, num_segments); 715473ac565SMatthias Ringwald if (next_segment < num_segments){ 716473ac565SMatthias Ringwald // prepare sending of next segment 717473ac565SMatthias Ringwald hfp_connection->send_ag_indicators_segment = next_segment; 718473ac565SMatthias Ringwald log_info("HFP_CMD_RETRIEVE_AG_INDICATORS more. command %u, next seg %u", hfp_connection->command, next_segment); 719473ac565SMatthias Ringwald } else { 720473ac565SMatthias Ringwald // done, go to next state 721473ac565SMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0; 722473ac565SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS; 723473ac565SMatthias Ringwald } 724473ac565SMatthias Ringwald return 1; 725473ac565SMatthias Ringwald } 726473ac565SMatthias Ringwald default: 727473ac565SMatthias Ringwald break; 728473ac565SMatthias Ringwald } 729c04cf7ffSMilanka Ringwald return 0; 7303deb3ec6SMatthias Ringwald } 7313deb3ec6SMatthias Ringwald 732a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){ 733c04cf7ffSMilanka Ringwald int sent = codecs_exchange_state_machine(hfp_connection); 734c04cf7ffSMilanka Ringwald if (sent) return 1; 735aa4dd815SMatthias Ringwald 736a0ffb263SMatthias Ringwald switch(hfp_connection->command){ 737aa4dd815SMatthias Ringwald case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION: 738a0ffb263SMatthias Ringwald hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition); 739485ac19eSMilanka Ringwald hfp_ag_send_activate_voice_recognition_cmd(hfp_connection->rfcomm_cid, hfp_connection->ag_activate_voice_recognition); 740aa4dd815SMatthias Ringwald return 1; 741aa4dd815SMatthias Ringwald case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION: 742aa4dd815SMatthias Ringwald if (get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)){ 743a0ffb263SMatthias Ringwald hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition); 744485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 745a0ffb263SMatthias Ringwald hfp_ag_setup_audio_connection(hfp_connection); 746aa4dd815SMatthias Ringwald } else { 747485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 748aa4dd815SMatthias Ringwald } 749aa4dd815SMatthias Ringwald return 1; 750aa4dd815SMatthias Ringwald case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING: 751485ac19eSMilanka Ringwald hfp_ag_send_change_in_band_ring_tone_setting_cmd(hfp_connection->rfcomm_cid); 752aa4dd815SMatthias Ringwald return 1; 753aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 754485ac19eSMilanka Ringwald hfp_ag_send_report_network_operator_name_cmd(hfp_connection->rfcomm_cid, hfp_connection->network_operator); 755aa4dd815SMatthias Ringwald return 1; 756aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 757a0ffb263SMatthias Ringwald if (hfp_connection->network_operator.format != 0){ 758485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 759aa4dd815SMatthias Ringwald } else { 760485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 7613deb3ec6SMatthias Ringwald } 762aa4dd815SMatthias Ringwald return 1; 763aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE: 764485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 765aa4dd815SMatthias Ringwald return 1; 7663deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR: 767a0ffb263SMatthias Ringwald if (hfp_connection->extended_audio_gateway_error){ 768a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 769485ac19eSMilanka Ringwald hfp_ag_send_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, hfp_connection->extended_audio_gateway_error_value); 770aa4dd815SMatthias Ringwald return 1; 7713deb3ec6SMatthias Ringwald } 772202c8a4cSMatthias Ringwald break; 7733deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 774485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 775ce263fc8SMatthias Ringwald return 1; 7763deb3ec6SMatthias Ringwald default: 7773deb3ec6SMatthias Ringwald break; 7783deb3ec6SMatthias Ringwald } 779aa4dd815SMatthias Ringwald return 0; 7803deb3ec6SMatthias Ringwald } 7813deb3ec6SMatthias Ringwald 782a0ffb263SMatthias Ringwald static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){ 783a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || 784a0ffb263SMatthias Ringwald hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0; 7853deb3ec6SMatthias Ringwald 786aa4dd815SMatthias Ringwald 787a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){ 788a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 789a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 0; 790a0ffb263SMatthias Ringwald gap_disconnect(hfp_connection->sco_handle); 791aa4dd815SMatthias Ringwald return 1; 792aa4dd815SMatthias Ringwald } 793aa4dd815SMatthias Ringwald 794a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 795aa4dd815SMatthias Ringwald 796aa4dd815SMatthias Ringwald // run codecs exchange 797c04cf7ffSMilanka Ringwald int sent = codecs_exchange_state_machine(hfp_connection); 798c04cf7ffSMilanka Ringwald if (sent) return 1; 799aa4dd815SMatthias Ringwald 800c04cf7ffSMilanka Ringwald if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0; 801a0ffb263SMatthias Ringwald if (hfp_connection->establish_audio_connection){ 802a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 803a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 804eddcd308SMatthias Ringwald hfp_setup_synchronous_connection(hfp_connection); 805aa4dd815SMatthias Ringwald return 1; 806aa4dd815SMatthias Ringwald } 807aa4dd815SMatthias Ringwald return 0; 808aa4dd815SMatthias Ringwald } 809aa4dd815SMatthias Ringwald 810ec820d77SMatthias Ringwald static hfp_connection_t * hfp_ag_context_for_timer(btstack_timer_source_t * ts){ 811665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 812665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 813aa4dd815SMatthias Ringwald 814665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 815a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 816a0ffb263SMatthias Ringwald if ( & hfp_connection->hfp_timeout == ts) { 817a0ffb263SMatthias Ringwald return hfp_connection; 818aa4dd815SMatthias Ringwald } 819aa4dd815SMatthias Ringwald } 820aa4dd815SMatthias Ringwald return NULL; 821aa4dd815SMatthias Ringwald } 822aa4dd815SMatthias Ringwald 823ec820d77SMatthias Ringwald static void hfp_timeout_handler(btstack_timer_source_t * timer){ 824d63c37a1SMatthias Ringwald hfp_connection_t * hfp_connection = hfp_ag_context_for_timer(timer); 825d63c37a1SMatthias Ringwald if (!hfp_connection) return; 826aa4dd815SMatthias Ringwald 827d63c37a1SMatthias Ringwald log_info("HFP start ring timeout, con handle 0x%02x", hfp_connection->acl_handle); 828a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 1; 829a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled; 830aa4dd815SMatthias Ringwald 831a0ffb263SMatthias Ringwald btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout 832a0ffb263SMatthias Ringwald btstack_run_loop_add_timer(& hfp_connection->hfp_timeout); 833aa4dd815SMatthias Ringwald 834a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 835aa4dd815SMatthias Ringwald } 836aa4dd815SMatthias Ringwald 837a0ffb263SMatthias Ringwald static void hfp_timeout_start(hfp_connection_t * hfp_connection){ 838a0ffb263SMatthias Ringwald btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout); 839a0ffb263SMatthias Ringwald btstack_run_loop_set_timer_handler(& hfp_connection->hfp_timeout, hfp_timeout_handler); 840a0ffb263SMatthias Ringwald btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout 841a0ffb263SMatthias Ringwald btstack_run_loop_add_timer(& hfp_connection->hfp_timeout); 842aa4dd815SMatthias Ringwald } 843aa4dd815SMatthias Ringwald 844a0ffb263SMatthias Ringwald static void hfp_timeout_stop(hfp_connection_t * hfp_connection){ 845a0ffb263SMatthias Ringwald log_info("HFP stop ring timeout, con handle 0x%02x", hfp_connection->acl_handle); 846a0ffb263SMatthias Ringwald btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout); 847aa4dd815SMatthias Ringwald } 848aa4dd815SMatthias Ringwald 849aa4dd815SMatthias Ringwald // 850aa4dd815SMatthias Ringwald // transitition implementations for hfp_ag_call_state_machine 851aa4dd815SMatthias Ringwald // 852aa4dd815SMatthias Ringwald 853a0ffb263SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection){ 854aa4dd815SMatthias Ringwald if (use_in_band_tone()){ 855a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING; 856d97d752dSMilanka Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 857aa4dd815SMatthias Ringwald } else { 858a0ffb263SMatthias Ringwald hfp_timeout_start(hfp_connection); 859a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 1; 860a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled; 861a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_RINGING; 862ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGINIG); 863aa4dd815SMatthias Ringwald } 864aa4dd815SMatthias Ringwald } 865aa4dd815SMatthias Ringwald 866a0ffb263SMatthias Ringwald static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){ 867a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 0; 868a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = 0; 869a0ffb263SMatthias Ringwald hfp_timeout_stop(hfp_connection); 870ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGINIG); 871aa4dd815SMatthias Ringwald } 872aa4dd815SMatthias Ringwald 873aa4dd815SMatthias Ringwald static void hfp_ag_trigger_incoming_call(void){ 874aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name("callsetup"); 875aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 876aa4dd815SMatthias Ringwald 877665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 878665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 879665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 880a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 881a0ffb263SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 882a0ffb263SMatthias Ringwald if (hfp_connection->call_state == HFP_CALL_IDLE){ 883a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 884d63c37a1SMatthias Ringwald hfp_ag_hf_start_ringing(hfp_connection); 885aa4dd815SMatthias Ringwald } 886a0ffb263SMatthias Ringwald if (hfp_connection->call_state == HFP_CALL_ACTIVE){ 887a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING; 888aa4dd815SMatthias Ringwald } 889a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 890aa4dd815SMatthias Ringwald } 891aa4dd815SMatthias Ringwald } 892aa4dd815SMatthias Ringwald 893aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callsetup_state(void){ 894aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name("callsetup"); 895aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 896aa4dd815SMatthias Ringwald 897665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 898665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 899665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 900a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 901a0ffb263SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 902a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 903a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 904aa4dd815SMatthias Ringwald } 905aa4dd815SMatthias Ringwald } 906aa4dd815SMatthias Ringwald 907aa4dd815SMatthias Ringwald static void hfp_ag_transfer_call_state(void){ 908aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name("call"); 909aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 910aa4dd815SMatthias Ringwald 911665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 912665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 913665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 914a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 915a0ffb263SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 916a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 917a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 918aa4dd815SMatthias Ringwald } 919aa4dd815SMatthias Ringwald } 920aa4dd815SMatthias Ringwald 921aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callheld_state(void){ 922aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name("callheld"); 923aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 924aa4dd815SMatthias Ringwald 925665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 926665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 927665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 928a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 929a0ffb263SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 930a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 931a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 932aa4dd815SMatthias Ringwald } 933aa4dd815SMatthias Ringwald } 934aa4dd815SMatthias Ringwald 935aa4dd815SMatthias Ringwald static void hfp_ag_hf_accept_call(hfp_connection_t * source){ 936aa4dd815SMatthias Ringwald 937aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 938aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 939aa4dd815SMatthias Ringwald 940665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 941665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 942665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 943a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 944a0ffb263SMatthias Ringwald if (hfp_connection->call_state != HFP_CALL_RINGING && 945a0ffb263SMatthias Ringwald hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue; 946aa4dd815SMatthias Ringwald 947a0ffb263SMatthias Ringwald hfp_ag_hf_stop_ringing(hfp_connection); 948a0ffb263SMatthias Ringwald if (hfp_connection == source){ 949a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 950aa4dd815SMatthias Ringwald 951aa4dd815SMatthias Ringwald if (use_in_band_tone()){ 952a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 953aa4dd815SMatthias Ringwald } else { 954a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE; 955d97d752dSMilanka Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 956aa4dd815SMatthias Ringwald } 957aa4dd815SMatthias Ringwald 958a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 959a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 960aa4dd815SMatthias Ringwald 961aa4dd815SMatthias Ringwald } else { 962a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 963aa4dd815SMatthias Ringwald } 964a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 965aa4dd815SMatthias Ringwald } 966aa4dd815SMatthias Ringwald } 967aa4dd815SMatthias Ringwald 968aa4dd815SMatthias Ringwald static void hfp_ag_ag_accept_call(void){ 969aa4dd815SMatthias Ringwald 970aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 971aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 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); 977a0ffb263SMatthias Ringwald if (hfp_connection->call_state != HFP_CALL_RINGING) continue; 978aa4dd815SMatthias Ringwald 979a0ffb263SMatthias Ringwald hfp_ag_hf_stop_ringing(hfp_connection); 980a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION; 981aa4dd815SMatthias Ringwald 982a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 983a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 984aa4dd815SMatthias Ringwald 985a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 986aa4dd815SMatthias Ringwald break; // only single 987aa4dd815SMatthias Ringwald } 988aa4dd815SMatthias Ringwald } 989aa4dd815SMatthias Ringwald 990aa4dd815SMatthias Ringwald static void hfp_ag_trigger_reject_call(void){ 991aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 992665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 993665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 994665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 995665d90f2SMatthias Ringwald hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 996aa4dd815SMatthias Ringwald if (connection->call_state != HFP_CALL_RINGING && 997aa4dd815SMatthias Ringwald connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue; 998aa4dd815SMatthias Ringwald hfp_ag_hf_stop_ringing(connection); 999aa4dd815SMatthias Ringwald connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1000aa4dd815SMatthias Ringwald connection->call_state = HFP_CALL_IDLE; 1001aa4dd815SMatthias Ringwald hfp_run_for_context(connection); 1002aa4dd815SMatthias Ringwald } 1003aa4dd815SMatthias Ringwald } 1004aa4dd815SMatthias Ringwald 1005ca59be51SMatthias Ringwald static void hfp_ag_emit_simple_event(uint8_t event_subtype){ 1006ca59be51SMatthias Ringwald uint8_t event[3]; 1007ca59be51SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 1008ca59be51SMatthias Ringwald event[1] = sizeof(event) - 2; 1009ca59be51SMatthias Ringwald event[2] = event_subtype; 1010ca59be51SMatthias Ringwald if (!hfp_ag_callback) return; 1011ca59be51SMatthias Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 1012ca59be51SMatthias Ringwald } 1013ca59be51SMatthias Ringwald 1014aa4dd815SMatthias Ringwald static void hfp_ag_trigger_terminate_call(void){ 1015aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 1016aa4dd815SMatthias Ringwald 1017665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1018665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1019665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1020a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1021d63c37a1SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 1022a0ffb263SMatthias Ringwald if (hfp_connection->call_state == HFP_CALL_IDLE) continue; 1023a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 1024a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1025a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 1; 1026a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1027aa4dd815SMatthias Ringwald } 1028ca59be51SMatthias Ringwald hfp_ag_emit_simple_event(HFP_SUBEVENT_CALL_TERMINATED); 1029aa4dd815SMatthias Ringwald } 1030aa4dd815SMatthias Ringwald 10310cb5b971SMatthias Ringwald static void hfp_ag_set_callsetup_indicator(void){ 1032aa4dd815SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup"); 1033aa4dd815SMatthias Ringwald if (!indicator){ 1034d0c20769SMatthias Ringwald log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing"); 103545718b6fSMatthias Ringwald return; 1036aa4dd815SMatthias Ringwald }; 1037d0c20769SMatthias Ringwald indicator->status = hfp_gsm_callsetup_status(); 1038aa4dd815SMatthias Ringwald } 1039aa4dd815SMatthias Ringwald 10400cb5b971SMatthias Ringwald static void hfp_ag_set_callheld_indicator(void){ 1041d210d9c4SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld"); 1042d210d9c4SMatthias Ringwald if (!indicator){ 1043d210d9c4SMatthias Ringwald log_error("hfp_ag_set_callheld_state: callheld indicator is missing"); 104445718b6fSMatthias Ringwald return; 1045d210d9c4SMatthias Ringwald }; 1046d210d9c4SMatthias Ringwald indicator->status = hfp_gsm_callheld_status(); 1047d210d9c4SMatthias Ringwald } 1048d210d9c4SMatthias Ringwald 10490cb5b971SMatthias Ringwald static void hfp_ag_set_call_indicator(void){ 1050aa4dd815SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call"); 1051aa4dd815SMatthias Ringwald if (!indicator){ 1052aa4dd815SMatthias Ringwald log_error("hfp_ag_set_call_state: call indicator is missing"); 105345718b6fSMatthias Ringwald return; 1054aa4dd815SMatthias Ringwald }; 1055d210d9c4SMatthias Ringwald indicator->status = hfp_gsm_call_status(); 1056aa4dd815SMatthias Ringwald } 1057aa4dd815SMatthias Ringwald 1058aa4dd815SMatthias Ringwald static void hfp_ag_stop_ringing(void){ 1059665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1060665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1061665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1062a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1063a0ffb263SMatthias Ringwald if (hfp_connection->call_state != HFP_CALL_RINGING && 1064a0ffb263SMatthias Ringwald hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue; 1065a0ffb263SMatthias Ringwald hfp_ag_hf_stop_ringing(hfp_connection); 1066aa4dd815SMatthias Ringwald } 1067aa4dd815SMatthias Ringwald } 1068aa4dd815SMatthias Ringwald 1069aa4dd815SMatthias Ringwald static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){ 1070665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1071665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1072665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1073a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1074a0ffb263SMatthias Ringwald if (hfp_connection->call_state == call_state) return hfp_connection; 1075aa4dd815SMatthias Ringwald } 1076aa4dd815SMatthias Ringwald return NULL; 1077aa4dd815SMatthias Ringwald } 1078aa4dd815SMatthias Ringwald 1079a5bdcda8SMatthias Ringwald static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){ 1080665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1081665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1082665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1083a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1084a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = state + 1; 1085ce263fc8SMatthias Ringwald } 1086ce263fc8SMatthias Ringwald } 1087ce263fc8SMatthias Ringwald 1088a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 1089aa4dd815SMatthias Ringwald int indicator_index; 1090a0ffb263SMatthias Ringwald switch (hfp_connection->call_state){ 1091aa4dd815SMatthias Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING: 1092a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 1093a0ffb263SMatthias Ringwald // we got event: audio hfp_connection established 1094a0ffb263SMatthias Ringwald hfp_timeout_start(hfp_connection); 1095a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 1; 1096a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled; 1097a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_RINGING; 1098a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_RINGING; 1099ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGINIG); 1100aa4dd815SMatthias Ringwald break; 1101aa4dd815SMatthias Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE: 1102a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 1103a0ffb263SMatthias Ringwald // we got event: audio hfp_connection established 1104a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1105aa4dd815SMatthias Ringwald break; 1106aa4dd815SMatthias Ringwald case HFP_CALL_W2_SEND_CALL_WAITING: 1107a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_CHLD; 1108a0ffb263SMatthias Ringwald hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid); 1109aa4dd815SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callsetup"); 1110a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1111aa4dd815SMatthias Ringwald break; 1112aa4dd815SMatthias Ringwald default: 1113aa4dd815SMatthias Ringwald break; 1114aa4dd815SMatthias Ringwald } 1115aa4dd815SMatthias Ringwald return 0; 1116aa4dd815SMatthias Ringwald } 1117a0ffb263SMatthias Ringwald // hfp_connection is used to identify originating HF 1118a0ffb263SMatthias Ringwald static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){ 1119aa4dd815SMatthias Ringwald int indicator_index; 1120d210d9c4SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1121d210d9c4SMatthias Ringwald int callheld_indicator_index = get_ag_indicator_index_for_name("callheld"); 1122d210d9c4SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 112374386ee0SMatthias Ringwald 1124d210d9c4SMatthias Ringwald //printf("hfp_ag_call_sm event %d \n", event); 1125aa4dd815SMatthias Ringwald switch (event){ 1126aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL: 1127d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1128aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1129d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1130aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1131d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_INCOMING_CALL); 1132d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1133aa4dd815SMatthias Ringwald hfp_ag_trigger_incoming_call(); 113460ebb071SMilanka Ringwald log_info("AG rings"); 1135aa4dd815SMatthias Ringwald break; 1136aa4dd815SMatthias Ringwald default: 11373deb3ec6SMatthias Ringwald break; 11383deb3ec6SMatthias Ringwald } 11393deb3ec6SMatthias Ringwald break; 1140aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1141d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1142aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1143d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_INCOMING_CALL); 1144d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1145aa4dd815SMatthias Ringwald hfp_ag_trigger_incoming_call(); 114660ebb071SMilanka Ringwald log_info("AG call waiting"); 1147aa4dd815SMatthias Ringwald break; 1148aa4dd815SMatthias Ringwald default: 11493deb3ec6SMatthias Ringwald break; 11503deb3ec6SMatthias Ringwald } 11513deb3ec6SMatthias Ringwald break; 1152aa4dd815SMatthias Ringwald } 1153aa4dd815SMatthias Ringwald break; 1154aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG: 1155d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1156aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1157d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1158aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1159d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG); 1160d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1161d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1162aa4dd815SMatthias Ringwald hfp_ag_ag_accept_call(); 116360ebb071SMilanka Ringwald log_info("AG answers call, accept call by GSM"); 1164aa4dd815SMatthias Ringwald break; 1165aa4dd815SMatthias Ringwald default: 11663deb3ec6SMatthias Ringwald break; 11673deb3ec6SMatthias Ringwald } 1168aa4dd815SMatthias Ringwald break; 1169aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1170d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1171aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 117260ebb071SMilanka Ringwald log_info("AG: current call is placed on hold, incoming call gets active"); 1173d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG); 1174d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1175d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1176ce263fc8SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1177ce263fc8SMatthias Ringwald hfp_ag_transfer_callheld_state(); 1178aa4dd815SMatthias Ringwald break; 1179aa4dd815SMatthias Ringwald default: 1180aa4dd815SMatthias Ringwald break; 1181aa4dd815SMatthias Ringwald } 1182aa4dd815SMatthias Ringwald break; 1183aa4dd815SMatthias Ringwald } 1184aa4dd815SMatthias Ringwald break; 1185ce263fc8SMatthias Ringwald 1186ce263fc8SMatthias Ringwald case HFP_AG_HELD_CALL_JOINED_BY_AG: 1187d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1188ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1189d0c20769SMatthias Ringwald switch (hfp_gsm_callheld_status()){ 1190ce263fc8SMatthias Ringwald case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED: 119160ebb071SMilanka Ringwald log_info("AG: joining held call with active call"); 1192d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_HELD_CALL_JOINED_BY_AG); 1193d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1194ce263fc8SMatthias Ringwald hfp_ag_transfer_callheld_state(); 1195ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL); 1196ce263fc8SMatthias Ringwald break; 1197ce263fc8SMatthias Ringwald default: 1198ce263fc8SMatthias Ringwald break; 1199ce263fc8SMatthias Ringwald } 1200ce263fc8SMatthias Ringwald break; 1201ce263fc8SMatthias Ringwald default: 1202ce263fc8SMatthias Ringwald break; 1203ce263fc8SMatthias Ringwald } 1204ce263fc8SMatthias Ringwald break; 1205ce263fc8SMatthias Ringwald 1206aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF: 1207d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1208aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1209d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1210aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1211d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF); 1212d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1213d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1214a0ffb263SMatthias Ringwald hfp_ag_hf_accept_call(hfp_connection); 121560ebb071SMilanka Ringwald log_info("HF answers call, accept call by GSM"); 1216ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED); 1217aa4dd815SMatthias Ringwald break; 1218aa4dd815SMatthias Ringwald default: 1219aa4dd815SMatthias Ringwald break; 1220aa4dd815SMatthias Ringwald } 12213deb3ec6SMatthias Ringwald break; 12223deb3ec6SMatthias Ringwald default: 12233deb3ec6SMatthias Ringwald break; 12243deb3ec6SMatthias Ringwald } 12253deb3ec6SMatthias Ringwald break; 12263deb3ec6SMatthias Ringwald 1227ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG: 1228d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1229ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1230d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1231ce263fc8SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1232d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG); 1233ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_active = 1; 1234ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1235a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1236ce263fc8SMatthias Ringwald // as with regualr call 1237d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1238d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1239ce263fc8SMatthias Ringwald hfp_ag_ag_accept_call(); 124060ebb071SMilanka Ringwald log_info("AG response and hold - hold by AG"); 1241ce263fc8SMatthias Ringwald break; 1242ce263fc8SMatthias Ringwald default: 1243ce263fc8SMatthias Ringwald break; 1244ce263fc8SMatthias Ringwald } 1245ce263fc8SMatthias Ringwald break; 1246ce263fc8SMatthias Ringwald default: 1247ce263fc8SMatthias Ringwald break; 1248ce263fc8SMatthias Ringwald } 1249ce263fc8SMatthias Ringwald break; 1250ce263fc8SMatthias Ringwald 1251ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF: 1252d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1253ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1254d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1255ce263fc8SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1256d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF); 1257ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_active = 1; 1258ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1259a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1260ce263fc8SMatthias Ringwald // as with regualr call 1261d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1262d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1263a0ffb263SMatthias Ringwald hfp_ag_hf_accept_call(hfp_connection); 126460ebb071SMilanka Ringwald log_info("AG response and hold - hold by HF"); 1265ce263fc8SMatthias Ringwald break; 1266ce263fc8SMatthias Ringwald default: 1267ce263fc8SMatthias Ringwald break; 1268ce263fc8SMatthias Ringwald } 1269ce263fc8SMatthias Ringwald break; 1270ce263fc8SMatthias Ringwald default: 1271ce263fc8SMatthias Ringwald break; 1272ce263fc8SMatthias Ringwald } 1273ce263fc8SMatthias Ringwald break; 1274ce263fc8SMatthias Ringwald 1275ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG: 1276ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF: 1277ce263fc8SMatthias Ringwald if (!hfp_ag_response_and_hold_active) break; 1278ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1279d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG); 1280a5bdcda8SMatthias Ringwald hfp_ag_response_and_hold_active = 0; 1281ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED; 1282a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 128360ebb071SMilanka Ringwald log_info("Held Call accepted and active"); 1284ce263fc8SMatthias Ringwald break; 1285ce263fc8SMatthias Ringwald 1286ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG: 1287ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF: 1288ce263fc8SMatthias Ringwald if (!hfp_ag_response_and_hold_active) break; 1289ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1290d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG); 1291a5bdcda8SMatthias Ringwald hfp_ag_response_and_hold_active = 0; 1292ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1293a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1294ce263fc8SMatthias Ringwald // from terminate by ag 1295d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1296ce263fc8SMatthias Ringwald hfp_ag_trigger_terminate_call(); 1297ce263fc8SMatthias Ringwald break; 1298ce263fc8SMatthias Ringwald 1299aa4dd815SMatthias Ringwald case HFP_AG_TERMINATE_CALL_BY_HF: 1300d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1301aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1302d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1303aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1304d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF); 1305d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1306aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1307aa4dd815SMatthias Ringwald hfp_ag_trigger_reject_call(); 130860ebb071SMilanka Ringwald log_info("HF Rejected Incoming call, AG terminate call"); 1309aa4dd815SMatthias Ringwald break; 1310aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 1311aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1312d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF); 1313d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1314aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 131560ebb071SMilanka Ringwald log_info("AG terminate outgoing call process"); 1316202c8a4cSMatthias Ringwald break; 1317aa4dd815SMatthias Ringwald default: 1318aa4dd815SMatthias Ringwald break; 1319aa4dd815SMatthias Ringwald } 1320aa4dd815SMatthias Ringwald break; 1321aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1322d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF); 1323d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1324aa4dd815SMatthias Ringwald hfp_ag_transfer_call_state(); 1325a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 132660ebb071SMilanka Ringwald log_info("AG terminate call"); 1327aa4dd815SMatthias Ringwald break; 1328aa4dd815SMatthias Ringwald } 1329aa4dd815SMatthias Ringwald break; 13303deb3ec6SMatthias Ringwald 1331aa4dd815SMatthias Ringwald case HFP_AG_TERMINATE_CALL_BY_AG: 1332d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1333aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1334d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1335aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1336d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG); 1337d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1338aa4dd815SMatthias Ringwald hfp_ag_trigger_reject_call(); 133960ebb071SMilanka Ringwald log_info("AG Rejected Incoming call, AG terminate call"); 1340aa4dd815SMatthias Ringwald break; 1341aa4dd815SMatthias Ringwald default: 1342aa4dd815SMatthias Ringwald break; 13433deb3ec6SMatthias Ringwald } 1344202c8a4cSMatthias Ringwald break; 1345aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1346d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG); 1347d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1348d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1349aa4dd815SMatthias Ringwald hfp_ag_trigger_terminate_call(); 135060ebb071SMilanka Ringwald log_info("AG terminate call"); 1351aa4dd815SMatthias Ringwald break; 1352aa4dd815SMatthias Ringwald default: 13533deb3ec6SMatthias Ringwald break; 13543deb3ec6SMatthias Ringwald } 13553deb3ec6SMatthias Ringwald break; 1356aa4dd815SMatthias Ringwald case HFP_AG_CALL_DROPPED: 1357d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1358aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1359d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1360aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1361aa4dd815SMatthias Ringwald hfp_ag_stop_ringing(); 136260ebb071SMilanka Ringwald log_info("Incoming call interrupted"); 1363aa4dd815SMatthias Ringwald break; 1364aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 1365aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 136660ebb071SMilanka Ringwald log_info("Outgoing call interrupted\n"); 136760ebb071SMilanka Ringwald log_info("AG notify call dropped\n"); 1368aa4dd815SMatthias Ringwald break; 1369aa4dd815SMatthias Ringwald default: 13703deb3ec6SMatthias Ringwald break; 13713deb3ec6SMatthias Ringwald } 1372d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_CALL_DROPPED); 1373d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1374aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1375aa4dd815SMatthias Ringwald break; 1376aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1377ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_active) { 1378d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_CALL_DROPPED); 1379ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1380a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1381ce263fc8SMatthias Ringwald } 1382d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_CALL_DROPPED); 1383d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1384d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1385aa4dd815SMatthias Ringwald hfp_ag_trigger_terminate_call(); 138660ebb071SMilanka Ringwald log_info("AG notify call dropped"); 1387aa4dd815SMatthias Ringwald break; 1388aa4dd815SMatthias Ringwald default: 1389aa4dd815SMatthias Ringwald break; 1390aa4dd815SMatthias Ringwald } 1391aa4dd815SMatthias Ringwald break; 1392aa4dd815SMatthias Ringwald 1393aa4dd815SMatthias Ringwald case HFP_AG_OUTGOING_CALL_INITIATED: 1394d210d9c4SMatthias Ringwald // directly reject call if number of free slots is exceeded 1395d210d9c4SMatthias Ringwald if (!hfp_gsm_call_possible()){ 1396a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1397a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1398d210d9c4SMatthias Ringwald break; 1399d210d9c4SMatthias Ringwald } 1400a0ffb263SMatthias Ringwald hfp_gsm_handle_event_with_call_number(HFP_AG_OUTGOING_CALL_INITIATED, (const char *) &hfp_connection->line_buffer[3]); 14019cae807eSMatthias Ringwald 1402a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 140374386ee0SMatthias Ringwald 1404ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]); 1405aa4dd815SMatthias Ringwald break; 1406aa4dd815SMatthias Ringwald 14079cae807eSMatthias Ringwald case HFP_AG_OUTGOING_REDIAL_INITIATED:{ 1408d210d9c4SMatthias Ringwald // directly reject call if number of free slots is exceeded 1409d210d9c4SMatthias Ringwald if (!hfp_gsm_call_possible()){ 1410a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1411a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1412d210d9c4SMatthias Ringwald break; 1413d210d9c4SMatthias Ringwald } 1414d210d9c4SMatthias Ringwald 141574386ee0SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_OUTGOING_REDIAL_INITIATED); 1416a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 141774386ee0SMatthias Ringwald 141860ebb071SMilanka Ringwald log_info("Redial last number"); 14199cae807eSMatthias Ringwald char * last_dialed_number = hfp_gsm_last_dialed_number(); 1420aa4dd815SMatthias Ringwald 14219cae807eSMatthias Ringwald if (strlen(last_dialed_number) > 0){ 142260ebb071SMilanka Ringwald log_info("Last number exists: accept call"); 1423ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number); 14249cae807eSMatthias Ringwald } else { 142560ebb071SMilanka Ringwald log_info("log_infoLast number missing: reject call"); 14269cae807eSMatthias Ringwald hfp_ag_outgoing_call_rejected(); 14279cae807eSMatthias Ringwald } 14289cae807eSMatthias Ringwald break; 14299cae807eSMatthias Ringwald } 1430aa4dd815SMatthias Ringwald case HFP_AG_OUTGOING_CALL_REJECTED: 1431a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 1432a0ffb263SMatthias Ringwald if (!hfp_connection){ 1433a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state"); 1434aa4dd815SMatthias Ringwald break; 1435aa4dd815SMatthias Ringwald } 1436d210d9c4SMatthias Ringwald 1437d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_REJECTED); 1438a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 1439a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1440a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1441aa4dd815SMatthias Ringwald break; 1442aa4dd815SMatthias Ringwald 1443d210d9c4SMatthias Ringwald case HFP_AG_OUTGOING_CALL_ACCEPTED:{ 1444a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 1445a0ffb263SMatthias Ringwald if (!hfp_connection){ 1446a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state"); 1447aa4dd815SMatthias Ringwald break; 1448aa4dd815SMatthias Ringwald } 1449aa4dd815SMatthias Ringwald 1450a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1451a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING; 1452aa4dd815SMatthias Ringwald 1453aa4dd815SMatthias Ringwald // trigger callsetup to be 1454d0c20769SMatthias Ringwald int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT; 1455d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ACCEPTED); 1456d210d9c4SMatthias Ringwald 1457d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1458aa4dd815SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callsetup"); 1459a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1460aa4dd815SMatthias Ringwald 1461aa4dd815SMatthias Ringwald // put current call on hold if active 1462d210d9c4SMatthias Ringwald if (put_call_on_hold){ 146360ebb071SMilanka Ringwald log_info("AG putting current call on hold for new outgoing calllog_info"); 1464d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1465aa4dd815SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callheld"); 1466485ac19eSMilanka Ringwald hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]); 1467aa4dd815SMatthias Ringwald } 1468aa4dd815SMatthias Ringwald 1469aa4dd815SMatthias Ringwald // start audio if needed 1470d97d752dSMilanka Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 1471aa4dd815SMatthias Ringwald break; 1472d210d9c4SMatthias Ringwald } 1473aa4dd815SMatthias Ringwald case HFP_AG_OUTGOING_CALL_RINGING: 1474a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1475a0ffb263SMatthias Ringwald if (!hfp_connection){ 1476a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state"); 1477aa4dd815SMatthias Ringwald break; 1478aa4dd815SMatthias Ringwald } 1479d210d9c4SMatthias Ringwald 1480d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_RINGING); 1481a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING; 1482d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1483aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1484aa4dd815SMatthias Ringwald break; 1485aa4dd815SMatthias Ringwald 1486d210d9c4SMatthias Ringwald case HFP_AG_OUTGOING_CALL_ESTABLISHED:{ 1487aa4dd815SMatthias Ringwald // get outgoing call 1488a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING); 1489a0ffb263SMatthias Ringwald if (!hfp_connection){ 1490a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1491aa4dd815SMatthias Ringwald } 1492a0ffb263SMatthias Ringwald if (!hfp_connection){ 1493a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection"); 1494aa4dd815SMatthias Ringwald break; 1495aa4dd815SMatthias Ringwald } 1496d210d9c4SMatthias Ringwald 1497d0c20769SMatthias 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; 1498d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ESTABLISHED); 1499a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1500d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1501d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1502aa4dd815SMatthias Ringwald hfp_ag_transfer_call_state(); 1503aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1504d210d9c4SMatthias Ringwald if (CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS){ 1505d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1506aa4dd815SMatthias Ringwald hfp_ag_transfer_callheld_state(); 15073deb3ec6SMatthias Ringwald } 15083deb3ec6SMatthias Ringwald break; 1509d210d9c4SMatthias Ringwald } 1510d210d9c4SMatthias Ringwald 1511d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_USER_BUSY: 1512d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_CALL_HOLD_USER_BUSY); 1513d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1514a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1515a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 151660ebb071SMilanka Ringwald log_info("AG: Call Waiting, User Busy"); 1517d210d9c4SMatthias Ringwald break; 1518d210d9c4SMatthias Ringwald 1519d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{ 1520d0c20769SMatthias Ringwald int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1521d0c20769SMatthias Ringwald int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD; 1522d210d9c4SMatthias Ringwald 1523d210d9c4SMatthias Ringwald // Releases all active calls (if any exist) and accepts the other (held or waiting) call. 1524d0c20769SMatthias Ringwald if (call_held || call_setup_in_progress){ 1525a0ffb263SMatthias Ringwald hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index); 1526d0c20769SMatthias Ringwald 1527d0c20769SMatthias Ringwald } 1528d0c20769SMatthias Ringwald 1529d210d9c4SMatthias Ringwald if (call_setup_in_progress){ 153060ebb071SMilanka Ringwald log_info("AG: Call Dropped, Accept new call"); 1531d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1532a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1533d210d9c4SMatthias Ringwald } else { 153460ebb071SMilanka Ringwald log_info("AG: Call Dropped, Resume held call"); 1535d210d9c4SMatthias Ringwald } 1536d210d9c4SMatthias Ringwald if (call_held){ 1537d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1538a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1539d210d9c4SMatthias Ringwald } 1540d0c20769SMatthias Ringwald 1541a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1542d210d9c4SMatthias Ringwald break; 1543d210d9c4SMatthias Ringwald } 1544d0c20769SMatthias Ringwald 1545d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{ 1546d210d9c4SMatthias Ringwald // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call. 1547d210d9c4SMatthias Ringwald // only update if callsetup changed 1548d0c20769SMatthias Ringwald int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1549a0ffb263SMatthias Ringwald hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index); 1550d0c20769SMatthias Ringwald 1551d210d9c4SMatthias Ringwald if (call_setup_in_progress){ 155260ebb071SMilanka Ringwald log_info("AG: Call on Hold, Accept new call"); 1553d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1554a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1555d210d9c4SMatthias Ringwald } else { 155660ebb071SMilanka Ringwald log_info("AG: Swap calls"); 1557d210d9c4SMatthias Ringwald } 1558d0c20769SMatthias Ringwald 1559d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1560d0c20769SMatthias Ringwald // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED); 1561a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1562a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1563d210d9c4SMatthias Ringwald break; 1564d210d9c4SMatthias Ringwald } 1565d0c20769SMatthias Ringwald 1566d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_ADD_HELD_CALL: 1567d210d9c4SMatthias Ringwald // Adds a held call to the conversation. 1568d0c20769SMatthias Ringwald if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){ 156960ebb071SMilanka Ringwald log_info("AG: Join 3-way-call"); 1570d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_CALL_HOLD_ADD_HELD_CALL); 1571d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1572a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1573ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL); 1574d210d9c4SMatthias Ringwald } 1575a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1576d210d9c4SMatthias Ringwald break; 1577d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS: 1578d210d9c4SMatthias Ringwald // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer) 1579d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS); 158060ebb071SMilanka Ringwald log_info("AG: Transfer call -> Connect two calls and disconnect"); 1581d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1582d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1583a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1584a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1585a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 1586d210d9c4SMatthias Ringwald break; 1587ce263fc8SMatthias Ringwald 15883deb3ec6SMatthias Ringwald default: 15893deb3ec6SMatthias Ringwald break; 15903deb3ec6SMatthias Ringwald } 1591d210d9c4SMatthias Ringwald 1592d0c20769SMatthias Ringwald 15933deb3ec6SMatthias Ringwald } 15943deb3ec6SMatthias Ringwald 15959cae807eSMatthias Ringwald 1596a0ffb263SMatthias Ringwald static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){ 15979cae807eSMatthias Ringwald hfp_gsm_call_t * active_call = hfp_gsm_call(call_index); 15989cae807eSMatthias Ringwald if (!active_call) return; 15999cae807eSMatthias Ringwald 16009cae807eSMatthias Ringwald int idx = active_call->index; 16019cae807eSMatthias Ringwald hfp_enhanced_call_dir_t dir = active_call->direction; 16029cae807eSMatthias Ringwald hfp_enhanced_call_status_t status = active_call->enhanced_status; 16039cae807eSMatthias Ringwald hfp_enhanced_call_mode_t mode = active_call->mode; 16049cae807eSMatthias Ringwald hfp_enhanced_call_mpty_t mpty = active_call->mpty; 16059cae807eSMatthias Ringwald uint8_t type = active_call->clip_type; 16069cae807eSMatthias Ringwald char * number = active_call->clip_number; 16079cae807eSMatthias Ringwald 16089cae807eSMatthias Ringwald char buffer[100]; 16099cae807eSMatthias Ringwald // TODO: check length of a buffer, to fit the MTU 16109cae807eSMatthias Ringwald int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty); 16119cae807eSMatthias Ringwald if (number){ 16121cc1d9e9SMilanka Ringwald offset += snprintf(buffer+offset, sizeof(buffer)-offset-3, ", \"%s\",%u", number, type); 16139cae807eSMatthias Ringwald } 16149cae807eSMatthias Ringwald snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 161560ebb071SMilanka 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, 16169cae807eSMatthias Ringwald mode, mpty, type, number); 1617a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 16189cae807eSMatthias Ringwald } 16199cae807eSMatthias Ringwald 1620a0ffb263SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t *hfp_connection){ 1621a0ffb263SMatthias Ringwald if (!hfp_connection) return; 1622724f400dSMatthias Ringwald if (!hfp_connection->rfcomm_cid) return; 1623473ac565SMatthias Ringwald 162422387625SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) { 162522387625SMatthias Ringwald log_info("HFP AG%p, wrong role %u", hfp_connection, hfp_connection->local_role); 162622387625SMatthias Ringwald return; 162722387625SMatthias Ringwald } 162822387625SMatthias Ringwald 1629e30a6a47SMatthias Ringwald if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) { 1630473ac565SMatthias Ringwald log_info("hfp_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid); 1631e30a6a47SMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 1632e30a6a47SMatthias Ringwald return; 1633e30a6a47SMatthias Ringwald } 1634a0ffb263SMatthias Ringwald if (hfp_connection->send_status_of_current_calls){ 1635a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 1636a0ffb263SMatthias Ringwald if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){ 1637a0ffb263SMatthias Ringwald hfp_connection->next_call_index++; 1638a0ffb263SMatthias Ringwald hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index); 16399cae807eSMatthias Ringwald } else { 1640a0ffb263SMatthias Ringwald hfp_connection->next_call_index = 0; 1641a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1642a0ffb263SMatthias Ringwald hfp_connection->send_status_of_current_calls = 0; 16439cae807eSMatthias Ringwald } 1644ce263fc8SMatthias Ringwald return; 1645ce263fc8SMatthias Ringwald } 1646ce263fc8SMatthias Ringwald 1647a0ffb263SMatthias Ringwald if (hfp_connection->ag_notify_incoming_call_waiting){ 1648a0ffb263SMatthias Ringwald hfp_connection->ag_notify_incoming_call_waiting = 0; 1649a0ffb263SMatthias Ringwald hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid); 1650aa4dd815SMatthias Ringwald return; 1651aa4dd815SMatthias Ringwald } 1652aa4dd815SMatthias Ringwald 1653a0ffb263SMatthias Ringwald if (hfp_connection->command == HFP_CMD_UNKNOWN){ 1654a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 1655a0ffb263SMatthias Ringwald hfp_connection->send_error = 0; 1656a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1657485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 1658a0ffb263SMatthias Ringwald return; 1659a0ffb263SMatthias Ringwald } 1660a0ffb263SMatthias Ringwald 1661a0ffb263SMatthias Ringwald if (hfp_connection->send_error){ 1662a0ffb263SMatthias Ringwald hfp_connection->send_error = 0; 1663a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1664485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 1665aa4dd815SMatthias Ringwald return; 1666aa4dd815SMatthias Ringwald } 1667aa4dd815SMatthias Ringwald 1668ce263fc8SMatthias Ringwald // note: before update AG indicators and ok_pending 1669a0ffb263SMatthias Ringwald if (hfp_connection->send_response_and_hold_status){ 1670a0ffb263SMatthias Ringwald int status = hfp_connection->send_response_and_hold_status - 1; 1671a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = 0; 1672485ac19eSMilanka Ringwald hfp_ag_send_set_response_and_hold(hfp_connection->rfcomm_cid, status); 1673ce263fc8SMatthias Ringwald return; 1674ce263fc8SMatthias Ringwald } 1675ce263fc8SMatthias Ringwald 1676a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending){ 1677a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 1678a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1679485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1680ce263fc8SMatthias Ringwald return; 1681ce263fc8SMatthias Ringwald } 1682ce263fc8SMatthias Ringwald 1683aa4dd815SMatthias Ringwald // update AG indicators 1684a0ffb263SMatthias Ringwald if (hfp_connection->ag_indicators_status_update_bitmap){ 1685aa4dd815SMatthias Ringwald int i; 1686a0ffb263SMatthias Ringwald for (i=0;i<hfp_connection->ag_indicators_nr;i++){ 1687a0ffb263SMatthias Ringwald if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){ 1688a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0); 1689a0ffb263SMatthias Ringwald if (!hfp_connection->enable_status_update_for_ag_indicators) { 1690ce263fc8SMatthias Ringwald log_info("+CMER:3,0,0,0 - not sending update for '%s'", hfp_ag_indicators[i].name); 1691ce263fc8SMatthias Ringwald break; 1692ce263fc8SMatthias Ringwald } 1693485ac19eSMilanka Ringwald hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]); 1694aa4dd815SMatthias Ringwald return; 1695aa4dd815SMatthias Ringwald } 1696aa4dd815SMatthias Ringwald } 1697aa4dd815SMatthias Ringwald } 1698aa4dd815SMatthias Ringwald 1699a0ffb263SMatthias Ringwald if (hfp_connection->ag_ring){ 1700a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 0; 1701a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1702485ac19eSMilanka Ringwald hfp_ag_send_ring(hfp_connection->rfcomm_cid); 1703aa4dd815SMatthias Ringwald return; 1704aa4dd815SMatthias Ringwald } 1705aa4dd815SMatthias Ringwald 1706a0ffb263SMatthias Ringwald if (hfp_connection->ag_send_clip){ 1707a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = 0; 1708a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1709a0ffb263SMatthias Ringwald hfp_ag_send_clip(hfp_connection->rfcomm_cid); 1710aa4dd815SMatthias Ringwald return; 1711aa4dd815SMatthias Ringwald } 1712aa4dd815SMatthias Ringwald 1713a0ffb263SMatthias Ringwald if (hfp_connection->send_phone_number_for_voice_tag){ 1714a0ffb263SMatthias Ringwald hfp_connection->send_phone_number_for_voice_tag = 0; 1715a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1716a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1717a0ffb263SMatthias Ringwald hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid); 1718aa4dd815SMatthias Ringwald return; 1719aa4dd815SMatthias Ringwald } 1720aa4dd815SMatthias Ringwald 1721a0ffb263SMatthias Ringwald if (hfp_connection->send_subscriber_number){ 1722a0ffb263SMatthias Ringwald if (hfp_connection->next_subscriber_number_to_send < subscriber_numbers_count){ 1723a0ffb263SMatthias Ringwald hfp_phone_number_t phone = subscriber_numbers[hfp_connection->next_subscriber_number_to_send++]; 1724a0ffb263SMatthias Ringwald hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number); 1725ce263fc8SMatthias Ringwald } else { 1726a0ffb263SMatthias Ringwald hfp_connection->send_subscriber_number = 0; 1727a0ffb263SMatthias Ringwald hfp_connection->next_subscriber_number_to_send = 0; 1728485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1729ce263fc8SMatthias Ringwald } 1730a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1731485ac19eSMilanka Ringwald 1732ce263fc8SMatthias Ringwald } 1733ce263fc8SMatthias Ringwald 1734a0ffb263SMatthias Ringwald if (hfp_connection->send_microphone_gain){ 1735a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 0; 1736a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1737485ac19eSMilanka Ringwald hfp_ag_send_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 1738aa4dd815SMatthias Ringwald return; 1739aa4dd815SMatthias Ringwald } 1740aa4dd815SMatthias Ringwald 1741a0ffb263SMatthias Ringwald if (hfp_connection->send_speaker_gain){ 1742a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 0; 1743a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1744485ac19eSMilanka Ringwald hfp_ag_send_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 17453deb3ec6SMatthias Ringwald return; 17463deb3ec6SMatthias Ringwald } 17473deb3ec6SMatthias Ringwald 1748a0ffb263SMatthias Ringwald if (hfp_connection->send_ag_status_indicators){ 1749a0ffb263SMatthias Ringwald hfp_connection->send_ag_status_indicators = 0; 1750485ac19eSMilanka Ringwald hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid); 1751ce263fc8SMatthias Ringwald return; 1752ce263fc8SMatthias Ringwald } 1753ce263fc8SMatthias Ringwald 17542ee45eb2SMilanka Ringwald // trigger codec exchange if requested by app 17552ee45eb2SMilanka Ringwald if (hfp_connection->trigger_codec_exchange){ 17562ee45eb2SMilanka Ringwald log_info("trigger codec, command %u, codec state %u", hfp_connection->command, hfp_connection->codecs_state); 17572ee45eb2SMilanka Ringwald } 17582ee45eb2SMilanka Ringwald if (hfp_connection->trigger_codec_exchange && hfp_connection->command == HFP_CMD_NONE){ 17592ee45eb2SMilanka Ringwald switch (hfp_connection->codecs_state){ 17602ee45eb2SMilanka Ringwald case HFP_CODECS_IDLE: 17612ee45eb2SMilanka Ringwald case HFP_CODECS_RECEIVED_LIST: 17622ee45eb2SMilanka Ringwald case HFP_CODECS_AG_RESEND_COMMON_CODEC: 17632ee45eb2SMilanka Ringwald case HFP_CODECS_ERROR: 17642ee45eb2SMilanka Ringwald hfp_connection->trigger_codec_exchange = 0; 17652ee45eb2SMilanka Ringwald hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC; 17662ee45eb2SMilanka Ringwald break; 17672ee45eb2SMilanka Ringwald default: 17682ee45eb2SMilanka Ringwald break; 17692ee45eb2SMilanka Ringwald } 17702ee45eb2SMilanka Ringwald } 17712ee45eb2SMilanka Ringwald 1772c04cf7ffSMilanka Ringwald int cmd_sent = hfp_ag_run_for_context_service_level_connection(hfp_connection); 1773c04cf7ffSMilanka Ringwald if (!cmd_sent){ 1774c04cf7ffSMilanka Ringwald cmd_sent = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection); 17753deb3ec6SMatthias Ringwald } 17763deb3ec6SMatthias Ringwald 1777c04cf7ffSMilanka Ringwald if (!cmd_sent){ 1778c04cf7ffSMilanka Ringwald cmd_sent = call_setup_state_machine(hfp_connection); 1779aa4dd815SMatthias Ringwald } 1780aa4dd815SMatthias Ringwald 1781c04cf7ffSMilanka Ringwald if (!cmd_sent){ 1782c04cf7ffSMilanka Ringwald cmd_sent = hfp_ag_run_for_audio_connection(hfp_connection); 1783aa4dd815SMatthias Ringwald } 17843deb3ec6SMatthias Ringwald 1785c04cf7ffSMilanka Ringwald if (hfp_connection->command == HFP_CMD_NONE && !cmd_sent){ 1786a0ffb263SMatthias Ringwald // log_info("hfp_connection->command == HFP_CMD_NONE"); 1787a0ffb263SMatthias Ringwald switch(hfp_connection->state){ 17883deb3ec6SMatthias Ringwald case HFP_W2_DISCONNECT_RFCOMM: 1789a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 1790a0ffb263SMatthias Ringwald rfcomm_disconnect(hfp_connection->rfcomm_cid); 17913deb3ec6SMatthias Ringwald break; 17923deb3ec6SMatthias Ringwald default: 17933deb3ec6SMatthias Ringwald break; 17943deb3ec6SMatthias Ringwald } 17953deb3ec6SMatthias Ringwald } 1796c04cf7ffSMilanka Ringwald 1797c04cf7ffSMilanka Ringwald if (cmd_sent){ 1798a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1799473ac565SMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 1800473ac565SMatthias Ringwald } 18013deb3ec6SMatthias Ringwald } 1802d68dcce1SMatthias Ringwald 1803ce263fc8SMatthias Ringwald static hfp_generic_status_indicator_t *get_hf_indicator_by_number(int number){ 1804ce263fc8SMatthias Ringwald int i; 1805a0ffb263SMatthias Ringwald for (i=0;i< hfp_generic_status_indicators_nr;i++){ 1806a0ffb263SMatthias Ringwald hfp_generic_status_indicator_t * indicator = &hfp_generic_status_indicators[i]; 1807ce263fc8SMatthias Ringwald if (indicator->uuid == number){ 1808ce263fc8SMatthias Ringwald return indicator; 1809ce263fc8SMatthias Ringwald } 1810ce263fc8SMatthias Ringwald } 1811ce263fc8SMatthias Ringwald return NULL; 1812ce263fc8SMatthias Ringwald } 18133deb3ec6SMatthias Ringwald 1814e9c22d4eSMatthias Ringwald static void hfp_ag_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 18158a46ec40SMatthias Ringwald UNUSED(packet_type); // ok: only called with RFCOMM_DATA_PACKET 18168a46ec40SMatthias Ringwald 18178a46ec40SMatthias Ringwald // assertion: size >= 1 as rfcomm.c does not deliver empty packets 18188a46ec40SMatthias Ringwald if (size < 1) return; 18199ec2630cSMatthias Ringwald 1820a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 1821a0ffb263SMatthias Ringwald if (!hfp_connection) return; 18221e35c04dSMatthias Ringwald 18238a46ec40SMatthias Ringwald // temp overwrite last byte (most likely \n for log_info) 18241e35c04dSMatthias Ringwald char last_char = packet[size-1]; 18251e35c04dSMatthias Ringwald packet[size-1] = 0; 18261e35c04dSMatthias Ringwald log_info("HFP_RX %s", packet); 18271e35c04dSMatthias Ringwald packet[size-1] = last_char; 18281e35c04dSMatthias Ringwald 18298a46ec40SMatthias Ringwald // process messages byte-wise 18303deb3ec6SMatthias Ringwald int pos; 18313deb3ec6SMatthias Ringwald for (pos = 0; pos < size ; pos++){ 1832a0ffb263SMatthias Ringwald hfp_parse(hfp_connection, packet[pos], 0); 1833aa4dd815SMatthias Ringwald } 1834ce263fc8SMatthias Ringwald hfp_generic_status_indicator_t * indicator; 1835ce263fc8SMatthias Ringwald int value; 1836a0ffb263SMatthias Ringwald switch(hfp_connection->command){ 1837ce263fc8SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_QUERY: 1838ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_active){ 1839a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1; 1840ce263fc8SMatthias Ringwald } 1841a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1842ce263fc8SMatthias Ringwald break; 1843ce263fc8SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_COMMAND: 18442308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 184560ebb071SMilanka Ringwald log_info("HF Response and Hold: %u", value); 1846ce263fc8SMatthias Ringwald switch(value){ 1847ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD: 1848a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection); 1849ce263fc8SMatthias Ringwald break; 1850ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED: 1851a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection); 1852ce263fc8SMatthias Ringwald break; 1853ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED: 1854a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection); 1855ce263fc8SMatthias Ringwald break; 1856ce263fc8SMatthias Ringwald default: 1857ce263fc8SMatthias Ringwald break; 1858ce263fc8SMatthias Ringwald } 1859a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1860ce263fc8SMatthias Ringwald break; 1861ce263fc8SMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 1862a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1863ce263fc8SMatthias Ringwald // find indicator by assigned number 1864a0ffb263SMatthias Ringwald indicator = get_hf_indicator_by_number(hfp_connection->parser_indicator_index); 1865ce263fc8SMatthias Ringwald if (!indicator){ 1866a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1867ce263fc8SMatthias Ringwald break; 1868ce263fc8SMatthias Ringwald } 18692308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1870ce263fc8SMatthias Ringwald switch (indicator->uuid){ 1871ce263fc8SMatthias Ringwald case 1: // enhanced security 1872ce263fc8SMatthias Ringwald if (value > 1) { 1873a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1874ce263fc8SMatthias Ringwald return; 1875ce263fc8SMatthias Ringwald } 187660ebb071SMilanka Ringwald log_info("HF Indicator 'enhanced security' set to %u", value); 1877ce263fc8SMatthias Ringwald break; 1878ce263fc8SMatthias Ringwald case 2: // battery level 1879ce263fc8SMatthias Ringwald if (value > 100){ 1880a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1881ce263fc8SMatthias Ringwald return; 1882ce263fc8SMatthias Ringwald } 188360ebb071SMilanka Ringwald log_info("HF Indicator 'battery' set to %u", value); 1884ce263fc8SMatthias Ringwald break; 1885ce263fc8SMatthias Ringwald default: 188660ebb071SMilanka Ringwald log_info("HF Indicator unknown set to %u", value); 1887ce263fc8SMatthias Ringwald break; 1888ce263fc8SMatthias Ringwald } 1889a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1890ce263fc8SMatthias Ringwald break; 1891ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 1892ce263fc8SMatthias Ringwald // expected by SLC state machine 1893a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break; 1894a0ffb263SMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0; 1895a0ffb263SMatthias Ringwald hfp_connection->send_ag_status_indicators = 1; 1896ce263fc8SMatthias Ringwald break; 1897ce263fc8SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 1898a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1899a0ffb263SMatthias Ringwald hfp_connection->next_call_index = 0; 1900a0ffb263SMatthias Ringwald hfp_connection->send_status_of_current_calls = 1; 1901ce263fc8SMatthias Ringwald break; 1902ce263fc8SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1903ce263fc8SMatthias Ringwald if (subscriber_numbers_count == 0){ 1904485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1905ce263fc8SMatthias Ringwald break; 1906ce263fc8SMatthias Ringwald } 1907a0ffb263SMatthias Ringwald hfp_connection->next_subscriber_number_to_send = 0; 1908a0ffb263SMatthias Ringwald hfp_connection->send_subscriber_number = 1; 1909ce263fc8SMatthias Ringwald break; 1910c1797c7dSMatthias Ringwald case HFP_CMD_TRANSMIT_DTMF_CODES: 1911a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1912ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, (const char *) &hfp_connection->line_buffer[0]); 1913c1797c7dSMatthias Ringwald break; 1914aa4dd815SMatthias Ringwald case HFP_CMD_HF_REQUEST_PHONE_NUMBER: 1915a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1916ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG); 1917aa4dd815SMatthias Ringwald break; 1918aa4dd815SMatthias Ringwald case HFP_CMD_TURN_OFF_EC_AND_NR: 1919a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1920aa4dd815SMatthias Ringwald if (get_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION)){ 1921a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1922a0ffb263SMatthias Ringwald hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION, hfp_connection->ag_echo_and_noise_reduction); 192360ebb071SMilanka Ringwald log_info("AG: EC/NR = %u", hfp_connection->ag_echo_and_noise_reduction); 1924aa4dd815SMatthias Ringwald } else { 1925a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1926aa4dd815SMatthias Ringwald } 1927aa4dd815SMatthias Ringwald break; 1928aa4dd815SMatthias Ringwald case HFP_CMD_CALL_ANSWERED: 1929a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 193060ebb071SMilanka Ringwald log_info("HFP: ATA"); 1931a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection); 1932aa4dd815SMatthias Ringwald break; 1933aa4dd815SMatthias Ringwald case HFP_CMD_HANG_UP_CALL: 1934a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1935a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1936a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection); 1937aa4dd815SMatthias Ringwald break; 1938aa4dd815SMatthias Ringwald case HFP_CMD_CALL_HOLD: { 1939aa4dd815SMatthias Ringwald // TODO: fully implement this 1940a0ffb263SMatthias Ringwald log_error("HFP: unhandled call hold type %c", hfp_connection->line_buffer[0]); 1941a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1942a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1943a0ffb263SMatthias Ringwald hfp_connection->call_index = 0; 1944d0c20769SMatthias Ringwald 1945a0ffb263SMatthias Ringwald if (hfp_connection->line_buffer[1] != '\0'){ 19462308e108SMilanka Ringwald hfp_connection->call_index = btstack_atoi((char *)&hfp_connection->line_buffer[1]); 1947d0c20769SMatthias Ringwald } 1948d210d9c4SMatthias Ringwald 1949a0ffb263SMatthias Ringwald switch (hfp_connection->line_buffer[0]){ 1950aa4dd815SMatthias Ringwald case '0': 1951d0c20769SMatthias Ringwald // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call. 1952a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection); 1953aa4dd815SMatthias Ringwald break; 1954aa4dd815SMatthias Ringwald case '1': 1955d0c20769SMatthias Ringwald // Releases all active calls (if any exist) and accepts the other (held or waiting) call. 1956d0c20769SMatthias Ringwald // Where both a held and a waiting call exist, the above procedures shall apply to the 1957d0c20769SMatthias Ringwald // waiting call (i.e., not to the held call) in conflicting situation. 1958a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection); 1959aa4dd815SMatthias Ringwald break; 1960aa4dd815SMatthias Ringwald case '2': 1961d0c20769SMatthias Ringwald // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call. 1962d0c20769SMatthias Ringwald // Where both a held and a waiting call exist, the above procedures shall apply to the 1963d0c20769SMatthias Ringwald // waiting call (i.e., not to the held call) in conflicting situation. 1964a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection); 1965aa4dd815SMatthias Ringwald break; 1966aa4dd815SMatthias Ringwald case '3': 1967d0c20769SMatthias Ringwald // Adds a held call to the conversation. 1968a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection); 1969aa4dd815SMatthias Ringwald break; 1970aa4dd815SMatthias Ringwald case '4': 1971d0c20769SMatthias Ringwald // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer). 1972a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection); 1973aa4dd815SMatthias Ringwald break; 1974aa4dd815SMatthias Ringwald default: 1975aa4dd815SMatthias Ringwald break; 1976aa4dd815SMatthias Ringwald } 1977aa4dd815SMatthias Ringwald break; 1978aa4dd815SMatthias Ringwald } 1979aa4dd815SMatthias Ringwald case HFP_CMD_CALL_PHONE_NUMBER: 1980a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1981a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED, hfp_connection); 1982aa4dd815SMatthias Ringwald break; 1983aa4dd815SMatthias Ringwald case HFP_CMD_REDIAL_LAST_NUMBER: 1984a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1985a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection); 1986aa4dd815SMatthias Ringwald break; 1987aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_CLIP: 1988a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1989a0ffb263SMatthias Ringwald hfp_connection->clip_enabled = hfp_connection->line_buffer[8] != '0'; 1990a0ffb263SMatthias Ringwald log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled); 1991a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1992aa4dd815SMatthias Ringwald break; 1993aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION: 1994a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1995a0ffb263SMatthias Ringwald hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[8] != '0'; 1996a0ffb263SMatthias Ringwald log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled); 1997a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1998aa4dd815SMatthias Ringwald break; 1999aa4dd815SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 2000a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2001a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 200260ebb071SMilanka Ringwald log_info("HF speaker gain = %u", hfp_connection->speaker_gain); 2003aa4dd815SMatthias Ringwald break; 2004aa4dd815SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 2005a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2006a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 200760ebb071SMilanka Ringwald log_info("HF microphone gain = %u", hfp_connection->microphone_gain); 2008aa4dd815SMatthias Ringwald break; 2009aa4dd815SMatthias Ringwald default: 2010aa4dd815SMatthias Ringwald break; 20113deb3ec6SMatthias Ringwald } 20123deb3ec6SMatthias Ringwald } 20133deb3ec6SMatthias Ringwald 2014aa4dd815SMatthias Ringwald static void hfp_run(void){ 2015d63c37a1SMatthias Ringwald btstack_linked_list_iterator_t it; 2016d63c37a1SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2017d63c37a1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2018d63c37a1SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 201922387625SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2020a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 20213deb3ec6SMatthias Ringwald } 20223deb3ec6SMatthias Ringwald } 20233deb3ec6SMatthias Ringwald 2024e9c22d4eSMatthias Ringwald static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 20253deb3ec6SMatthias Ringwald switch (packet_type){ 20263deb3ec6SMatthias Ringwald case RFCOMM_DATA_PACKET: 2027e9c22d4eSMatthias Ringwald hfp_ag_handle_rfcomm_data(packet_type, channel, packet, size); 20283deb3ec6SMatthias Ringwald break; 20293deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 2030e30a6a47SMatthias Ringwald if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 2031e30a6a47SMatthias Ringwald uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 2032e30a6a47SMatthias Ringwald hfp_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid)); 2033e30a6a47SMatthias Ringwald return; 2034e30a6a47SMatthias Ringwald } 2035323d3000SMatthias Ringwald hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_AG); 2036aa4dd815SMatthias Ringwald break; 20373deb3ec6SMatthias Ringwald default: 20383deb3ec6SMatthias Ringwald break; 20393deb3ec6SMatthias Ringwald } 20403deb3ec6SMatthias Ringwald 20413deb3ec6SMatthias Ringwald hfp_run(); 20423deb3ec6SMatthias Ringwald } 20433deb3ec6SMatthias Ringwald 2044a0ffb263SMatthias Ringwald void hfp_ag_init_codecs(int codecs_nr, uint8_t * codecs){ 20453deb3ec6SMatthias Ringwald if (codecs_nr > HFP_MAX_NUM_CODECS){ 20463deb3ec6SMatthias Ringwald log_error("hfp_init: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS); 20473deb3ec6SMatthias Ringwald return; 20483deb3ec6SMatthias Ringwald } 20493deb3ec6SMatthias Ringwald int i; 2050a0ffb263SMatthias Ringwald hfp_codecs_nr = codecs_nr; 20513deb3ec6SMatthias Ringwald for (i=0; i < codecs_nr; i++){ 20523deb3ec6SMatthias Ringwald hfp_codecs[i] = codecs[i]; 20533deb3ec6SMatthias Ringwald } 2054a0ffb263SMatthias Ringwald } 20553deb3ec6SMatthias Ringwald 2056a0ffb263SMatthias Ringwald void hfp_ag_init_supported_features(uint32_t supported_features){ 2057a0ffb263SMatthias Ringwald hfp_supported_features = supported_features; 2058a0ffb263SMatthias Ringwald } 20593deb3ec6SMatthias Ringwald 2060a0ffb263SMatthias Ringwald void hfp_ag_init_ag_indicators(int ag_indicators_nr, hfp_ag_indicator_t * ag_indicators){ 2061a0ffb263SMatthias Ringwald hfp_ag_indicators_nr = ag_indicators_nr; 2062a0ffb263SMatthias Ringwald memcpy(hfp_ag_indicators, ag_indicators, ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 2063a0ffb263SMatthias Ringwald } 20643deb3ec6SMatthias Ringwald 2065a0ffb263SMatthias Ringwald void hfp_ag_init_hf_indicators(int hf_indicators_nr, hfp_generic_status_indicator_t * hf_indicators){ 2066a0ffb263SMatthias Ringwald if (hf_indicators_nr > HFP_MAX_NUM_HF_INDICATORS) return; 2067a0ffb263SMatthias Ringwald hfp_generic_status_indicators_nr = hf_indicators_nr; 2068a0ffb263SMatthias Ringwald memcpy(hfp_generic_status_indicators, hf_indicators, hf_indicators_nr * sizeof(hfp_generic_status_indicator_t)); 2069a0ffb263SMatthias Ringwald } 2070a0ffb263SMatthias Ringwald 2071a0ffb263SMatthias Ringwald void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){ 20723deb3ec6SMatthias Ringwald hfp_ag_call_hold_services_nr = call_hold_services_nr; 20733deb3ec6SMatthias Ringwald memcpy(hfp_ag_call_hold_services, call_hold_services, call_hold_services_nr * sizeof(char *)); 2074a0ffb263SMatthias Ringwald } 2075a0ffb263SMatthias Ringwald 2076a0ffb263SMatthias Ringwald 2077a0ffb263SMatthias Ringwald void hfp_ag_init(uint16_t rfcomm_channel_nr){ 2078520c92d5SMatthias Ringwald hfp_init(); 2079d63c37a1SMatthias Ringwald 2080e9c22d4eSMatthias Ringwald rfcomm_register_service(&rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 2081e9c22d4eSMatthias Ringwald hfp_set_ag_rfcomm_packet_handler(&rfcomm_packet_handler); 2082aa4dd815SMatthias Ringwald 2083d210d9c4SMatthias Ringwald hfp_ag_response_and_hold_active = 0; 2084d210d9c4SMatthias Ringwald subscriber_numbers = NULL; 2085d210d9c4SMatthias Ringwald subscriber_numbers_count = 0; 2086d210d9c4SMatthias Ringwald 2087d210d9c4SMatthias Ringwald hfp_gsm_init(); 20883deb3ec6SMatthias Ringwald } 20893deb3ec6SMatthias Ringwald 20903deb3ec6SMatthias Ringwald void hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){ 2091323d3000SMatthias Ringwald hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE, HFP_ROLE_AG); 20923deb3ec6SMatthias Ringwald } 20933deb3ec6SMatthias Ringwald 2094d97d752dSMilanka Ringwald void hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){ 2095*9c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2096a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2097a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2098a33eb0c4SMilanka Ringwald return; 2099a33eb0c4SMilanka Ringwald } 2100a0ffb263SMatthias Ringwald hfp_release_service_level_connection(hfp_connection); 2101a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 21023deb3ec6SMatthias Ringwald } 21033deb3ec6SMatthias Ringwald 2104d97d752dSMilanka Ringwald void hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, hfp_cme_error_t error){ 2105*9c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2106a0ffb263SMatthias Ringwald if (!hfp_connection){ 2107a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 21083deb3ec6SMatthias Ringwald return; 21093deb3ec6SMatthias Ringwald } 2110a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 2111a0ffb263SMatthias Ringwald if (!hfp_connection->enable_extended_audio_gateway_error_report){ 21123deb3ec6SMatthias Ringwald return; 21133deb3ec6SMatthias Ringwald } 2114a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = error; 2115a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 21163deb3ec6SMatthias Ringwald } 21173deb3ec6SMatthias Ringwald 2118a0ffb263SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){ 21192ee45eb2SMilanka Ringwald log_info("hfp_ag_setup_audio_connection state %u", hfp_connection->state); 2120a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return; 2121a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return; 21223deb3ec6SMatthias Ringwald 2123a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 1; 2124a0ffb263SMatthias Ringwald if (!has_codec_negotiation_feature(hfp_connection)){ 2125d6ff09e1SMatthias Ringwald log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using CVSD"); 2126d6ff09e1SMatthias Ringwald hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 2127a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 21287522e673SMatthias Ringwald // now, pick link settings 21297522e673SMatthias Ringwald hfp_init_link_settings(hfp_connection); 21302ee45eb2SMilanka Ringwald return; 21313deb3ec6SMatthias Ringwald } 2132aa4dd815SMatthias Ringwald 21332ee45eb2SMilanka Ringwald hfp_connection->trigger_codec_exchange = 1; 2134aa4dd815SMatthias Ringwald } 2135aa4dd815SMatthias Ringwald 2136d97d752dSMilanka Ringwald void hfp_ag_establish_audio_connection(hci_con_handle_t acl_handle){ 2137*9c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2138a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2139a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2140a33eb0c4SMilanka Ringwald return; 2141a33eb0c4SMilanka Ringwald } 21422ee45eb2SMilanka Ringwald hfp_connection->trigger_codec_exchange = 0; 2143a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 2144a0ffb263SMatthias Ringwald hfp_ag_setup_audio_connection(hfp_connection); 2145a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 21463deb3ec6SMatthias Ringwald } 21473deb3ec6SMatthias Ringwald 2148d97d752dSMilanka Ringwald void hfp_ag_release_audio_connection(hci_con_handle_t acl_handle){ 2149*9c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2150a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2151a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2152a33eb0c4SMilanka Ringwald return; 2153a33eb0c4SMilanka Ringwald } 2154a0ffb263SMatthias Ringwald hfp_release_audio_connection(hfp_connection); 2155a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 21563deb3ec6SMatthias Ringwald } 2157aa4dd815SMatthias Ringwald 2158aa4dd815SMatthias Ringwald /** 2159aa4dd815SMatthias Ringwald * @brief Enable in-band ring tone 2160aa4dd815SMatthias Ringwald */ 2161aa4dd815SMatthias Ringwald void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){ 2162aa4dd815SMatthias Ringwald if (get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE) == use_in_band_ring_tone){ 2163aa4dd815SMatthias Ringwald return; 2164aa4dd815SMatthias Ringwald } 2165aa4dd815SMatthias Ringwald hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE, use_in_band_ring_tone); 2166aa4dd815SMatthias Ringwald 2167665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 2168665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2169665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2170a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 2171a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING; 2172a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 2173aa4dd815SMatthias Ringwald } 2174aa4dd815SMatthias Ringwald } 2175aa4dd815SMatthias Ringwald 2176aa4dd815SMatthias Ringwald /** 2177aa4dd815SMatthias Ringwald * @brief Called from GSM 2178aa4dd815SMatthias Ringwald */ 2179aa4dd815SMatthias Ringwald void hfp_ag_incoming_call(void){ 2180aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL); 2181aa4dd815SMatthias Ringwald } 2182aa4dd815SMatthias Ringwald 2183aa4dd815SMatthias Ringwald /** 2184aa4dd815SMatthias Ringwald * @brief number is stored. 2185aa4dd815SMatthias Ringwald */ 2186aa4dd815SMatthias Ringwald void hfp_ag_set_clip(uint8_t type, const char * number){ 2187d0c20769SMatthias Ringwald hfp_gsm_handle_event_with_clip(HFP_AG_SET_CLIP, type, number); 2188aa4dd815SMatthias Ringwald } 2189aa4dd815SMatthias Ringwald 2190aa4dd815SMatthias Ringwald void hfp_ag_call_dropped(void){ 2191aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL); 2192aa4dd815SMatthias Ringwald } 2193aa4dd815SMatthias Ringwald 2194aa4dd815SMatthias Ringwald // call from AG UI 2195aa4dd815SMatthias Ringwald void hfp_ag_answer_incoming_call(void){ 2196aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL); 2197aa4dd815SMatthias Ringwald } 2198aa4dd815SMatthias Ringwald 2199ce263fc8SMatthias Ringwald void hfp_ag_join_held_call(void){ 2200ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL); 2201ce263fc8SMatthias Ringwald } 2202ce263fc8SMatthias Ringwald 2203aa4dd815SMatthias Ringwald void hfp_ag_terminate_call(void){ 2204aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL); 2205aa4dd815SMatthias Ringwald } 2206aa4dd815SMatthias Ringwald 2207aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_ringing(void){ 2208aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL); 2209aa4dd815SMatthias Ringwald } 2210aa4dd815SMatthias Ringwald 2211aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_established(void){ 2212aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL); 2213aa4dd815SMatthias Ringwald } 2214aa4dd815SMatthias Ringwald 2215aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_rejected(void){ 2216aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL); 2217aa4dd815SMatthias Ringwald } 2218aa4dd815SMatthias Ringwald 2219aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_accepted(void){ 2220aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL); 2221aa4dd815SMatthias Ringwald } 2222ce263fc8SMatthias Ringwald 2223ce263fc8SMatthias Ringwald void hfp_ag_hold_incoming_call(void){ 2224ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL); 2225ce263fc8SMatthias Ringwald } 2226ce263fc8SMatthias Ringwald 2227ce263fc8SMatthias Ringwald void hfp_ag_accept_held_incoming_call(void) { 2228ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL); 2229ce263fc8SMatthias Ringwald } 2230ce263fc8SMatthias Ringwald 2231ce263fc8SMatthias Ringwald void hfp_ag_reject_held_incoming_call(void){ 2232ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL); 2233ce263fc8SMatthias Ringwald } 2234ce263fc8SMatthias Ringwald 2235aa4dd815SMatthias Ringwald static void hfp_ag_set_ag_indicator(const char * name, int value){ 2236aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name(name); 2237aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 2238aa4dd815SMatthias Ringwald hfp_ag_indicators[indicator_index].status = value; 2239aa4dd815SMatthias Ringwald 2240ce263fc8SMatthias Ringwald 2241665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 2242665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2243665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2244a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 2245a0ffb263SMatthias Ringwald if (! hfp_connection->ag_indicators[indicator_index].enabled) { 2246ce263fc8SMatthias Ringwald log_info("AG indicator '%s' changed to %u but not enabled", hfp_ag_indicators[indicator_index].name, value); 2247ce263fc8SMatthias Ringwald continue; 2248ce263fc8SMatthias Ringwald } 2249ce263fc8SMatthias Ringwald log_info("AG indicator '%s' changed to %u, request transfer statur", hfp_ag_indicators[indicator_index].name, value); 2250a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 2251a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 2252aa4dd815SMatthias Ringwald } 2253aa4dd815SMatthias Ringwald } 2254aa4dd815SMatthias Ringwald 2255aa4dd815SMatthias Ringwald void hfp_ag_set_registration_status(int status){ 2256aa4dd815SMatthias Ringwald hfp_ag_set_ag_indicator("service", status); 2257aa4dd815SMatthias Ringwald } 2258aa4dd815SMatthias Ringwald 2259aa4dd815SMatthias Ringwald void hfp_ag_set_signal_strength(int strength){ 2260aa4dd815SMatthias Ringwald hfp_ag_set_ag_indicator("signal", strength); 2261aa4dd815SMatthias Ringwald } 2262aa4dd815SMatthias Ringwald 2263aa4dd815SMatthias Ringwald void hfp_ag_set_roaming_status(int status){ 2264aa4dd815SMatthias Ringwald hfp_ag_set_ag_indicator("roam", status); 2265aa4dd815SMatthias Ringwald } 2266aa4dd815SMatthias Ringwald 2267aa4dd815SMatthias Ringwald void hfp_ag_set_battery_level(int level){ 2268aa4dd815SMatthias Ringwald hfp_ag_set_ag_indicator("battchg", level); 2269aa4dd815SMatthias Ringwald } 2270aa4dd815SMatthias Ringwald 2271d97d752dSMilanka Ringwald void hfp_ag_activate_voice_recognition(hci_con_handle_t acl_handle, int activate){ 2272aa4dd815SMatthias Ringwald if (!get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)) return; 2273*9c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2274a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2275a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2276a33eb0c4SMilanka Ringwald return; 2277a33eb0c4SMilanka Ringwald } 2278aa4dd815SMatthias Ringwald 2279a0ffb263SMatthias Ringwald if (!get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION)) { 228060ebb071SMilanka Ringwald log_info("AG cannot acivate voice recognition - not supported by HF"); 2281aa4dd815SMatthias Ringwald return; 2282aa4dd815SMatthias Ringwald } 2283aa4dd815SMatthias Ringwald 2284aa4dd815SMatthias Ringwald if (activate){ 2285d97d752dSMilanka Ringwald hfp_ag_establish_audio_connection(acl_handle); 2286aa4dd815SMatthias Ringwald } 2287aa4dd815SMatthias Ringwald 2288a0ffb263SMatthias Ringwald hfp_connection->ag_activate_voice_recognition = activate; 2289a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION; 2290a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 2291aa4dd815SMatthias Ringwald } 2292aa4dd815SMatthias Ringwald 2293d97d752dSMilanka Ringwald void hfp_ag_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 2294*9c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2295a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2296a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2297a33eb0c4SMilanka Ringwald return; 2298a33eb0c4SMilanka Ringwald } 2299a0ffb263SMatthias Ringwald if (hfp_connection->microphone_gain != gain){ 2300a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_SET_MICROPHONE_GAIN; 2301a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = gain; 2302a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 2303aa4dd815SMatthias Ringwald } 2304a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 2305aa4dd815SMatthias Ringwald } 2306aa4dd815SMatthias Ringwald 2307d97d752dSMilanka Ringwald void hfp_ag_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 2308*9c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2309a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2310a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2311a33eb0c4SMilanka Ringwald return; 2312a33eb0c4SMilanka Ringwald } 2313a0ffb263SMatthias Ringwald if (hfp_connection->speaker_gain != gain){ 2314a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = gain; 2315a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 2316aa4dd815SMatthias Ringwald } 2317a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 2318aa4dd815SMatthias Ringwald } 2319aa4dd815SMatthias Ringwald 2320d97d752dSMilanka Ringwald void hfp_ag_send_phone_number_for_voice_tag(hci_con_handle_t acl_handle, const char * number){ 2321*9c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2322a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2323a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2324a33eb0c4SMilanka Ringwald return; 2325a33eb0c4SMilanka Ringwald } 2326aa4dd815SMatthias Ringwald hfp_ag_set_clip(0, number); 2327a0ffb263SMatthias Ringwald hfp_connection->send_phone_number_for_voice_tag = 1; 2328aa4dd815SMatthias Ringwald } 2329aa4dd815SMatthias Ringwald 2330d97d752dSMilanka Ringwald void hfp_ag_reject_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 2331*9c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2332a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2333a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2334a33eb0c4SMilanka Ringwald return; 2335a33eb0c4SMilanka Ringwald } 2336a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 2337aa4dd815SMatthias Ringwald } 2338aa4dd815SMatthias Ringwald 2339d97d752dSMilanka Ringwald void hfp_ag_send_dtmf_code_done(hci_con_handle_t acl_handle){ 2340*9c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2341a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2342a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2343a33eb0c4SMilanka Ringwald return; 2344a33eb0c4SMilanka Ringwald } 2345a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2346c1797c7dSMatthias Ringwald } 2347aa4dd815SMatthias Ringwald 2348ce263fc8SMatthias Ringwald void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){ 2349ce263fc8SMatthias Ringwald subscriber_numbers = numbers; 2350ce263fc8SMatthias Ringwald subscriber_numbers_count = numbers_count; 2351ce263fc8SMatthias Ringwald } 2352ce263fc8SMatthias Ringwald 23539cae807eSMatthias Ringwald void hfp_ag_clear_last_dialed_number(void){ 23549cae807eSMatthias Ringwald hfp_gsm_clear_last_dialed_number(); 2355ce263fc8SMatthias Ringwald } 2356ce263fc8SMatthias Ringwald 2357d97d752dSMilanka Ringwald void hfp_ag_notify_incoming_call_waiting(hci_con_handle_t acl_handle){ 2358*9c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2359a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2360a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2361a33eb0c4SMilanka Ringwald return; 2362a33eb0c4SMilanka Ringwald } 2363a0ffb263SMatthias Ringwald if (!hfp_connection->call_waiting_notification_enabled) return; 2364a0ffb263SMatthias Ringwald 2365a0ffb263SMatthias Ringwald hfp_connection->ag_notify_incoming_call_waiting = 1; 2366a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 2367a0ffb263SMatthias Ringwald } 2368ce263fc8SMatthias Ringwald 2369