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 */ 373deb3ec6SMatthias Ringwald 383deb3ec6SMatthias Ringwald // ***************************************************************************** 393deb3ec6SMatthias Ringwald // 4066a048abSMatthias Ringwald // HFP Audio Gateway (AG) unit 413deb3ec6SMatthias Ringwald // 423deb3ec6SMatthias Ringwald // ***************************************************************************** 433deb3ec6SMatthias Ringwald 447907f069SMatthias Ringwald #include "btstack_config.h" 453deb3ec6SMatthias Ringwald 463deb3ec6SMatthias Ringwald #include <stdint.h> 473deb3ec6SMatthias Ringwald #include <stdio.h> 483deb3ec6SMatthias Ringwald #include <stdlib.h> 493deb3ec6SMatthias Ringwald #include <string.h> 503deb3ec6SMatthias Ringwald 5156042629SMatthias Ringwald #include "hci_cmd.h" 5282636622SMatthias Ringwald #include "btstack_run_loop.h" 533deb3ec6SMatthias Ringwald 543deb3ec6SMatthias Ringwald #include "hci.h" 553deb3ec6SMatthias Ringwald #include "btstack_memory.h" 563deb3ec6SMatthias Ringwald #include "hci_dump.h" 573deb3ec6SMatthias Ringwald #include "l2cap.h" 5816ece135SMatthias Ringwald #include "btstack_debug.h" 593edc84c5SMatthias Ringwald #include "classic/hfp.h" 603edc84c5SMatthias Ringwald #include "classic/hfp_ag.h" 61023f2764SMatthias Ringwald #include "classic/hfp_gsm_model.h" 62023f2764SMatthias Ringwald #include "classic/sdp_query_rfcomm.h" 63023f2764SMatthias Ringwald #include "classic/sdp_server.h" 64023f2764SMatthias Ringwald #include "classic/sdp_util.h" 653deb3ec6SMatthias Ringwald 66c5b64319SMatthias Ringwald // private prototypes 67c5b64319SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 68c5b64319SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t *context); 69c5b64319SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * connection); 70c5b64319SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * context); 71c5b64319SMatthias Ringwald 72c5b64319SMatthias Ringwald // public prototypes 73c5b64319SMatthias Ringwald hfp_generic_status_indicator_t * get_hfp_generic_status_indicators(); 74c5b64319SMatthias Ringwald int get_hfp_generic_status_indicators_nr(); 75c5b64319SMatthias Ringwald void set_hfp_generic_status_indicators(hfp_generic_status_indicator_t * indicators, int indicator_nr); 76c5b64319SMatthias Ringwald void set_hfp_ag_indicators(hfp_ag_indicator_t * indicators, int indicator_nr); 77c5b64319SMatthias Ringwald int get_hfp_ag_indicators_nr(hfp_connection_t * context); 78c5b64319SMatthias Ringwald hfp_ag_indicator_t * get_hfp_ag_indicators(hfp_connection_t * context); 79c5b64319SMatthias Ringwald 80c5b64319SMatthias Ringwald // gobals 813deb3ec6SMatthias Ringwald static const char default_hfp_ag_service_name[] = "Voice gateway"; 82aa4dd815SMatthias Ringwald 833deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_AG_SUPPORTED_FEATURES; 84aa4dd815SMatthias Ringwald 853deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0; 863deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS]; 873deb3ec6SMatthias Ringwald 883deb3ec6SMatthias Ringwald static int hfp_ag_indicators_nr = 0; 893deb3ec6SMatthias Ringwald static hfp_ag_indicator_t hfp_ag_indicators[HFP_MAX_NUM_AG_INDICATORS]; 903deb3ec6SMatthias Ringwald 91*a0ffb263SMatthias Ringwald static int hfp_generic_status_indicators_nr = 0; 92*a0ffb263SMatthias Ringwald static hfp_generic_status_indicator_t hfp_generic_status_indicators[HFP_MAX_NUM_HF_INDICATORS]; 93*a0ffb263SMatthias Ringwald 943deb3ec6SMatthias Ringwald static int hfp_ag_call_hold_services_nr = 0; 953deb3ec6SMatthias Ringwald static char *hfp_ag_call_hold_services[6]; 963deb3ec6SMatthias Ringwald static hfp_callback_t hfp_callback; 973deb3ec6SMatthias Ringwald 98ce263fc8SMatthias Ringwald static hfp_response_and_hold_state_t hfp_ag_response_and_hold_state; 99ce263fc8SMatthias Ringwald static int hfp_ag_response_and_hold_active = 0; 100aa4dd815SMatthias Ringwald 101ce263fc8SMatthias Ringwald // Subcriber information entries 102ce263fc8SMatthias Ringwald static hfp_phone_number_t * subscriber_numbers = NULL; 103ce263fc8SMatthias Ringwald static int subscriber_numbers_count = 0; 104ce263fc8SMatthias Ringwald 105c5b64319SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 1063deb3ec6SMatthias Ringwald 1073deb3ec6SMatthias Ringwald 108*a0ffb263SMatthias Ringwald static int hfp_ag_get_ag_indicators_nr(hfp_connection_t * hfp_connection){ 109*a0ffb263SMatthias Ringwald if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){ 110*a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr; 111*a0ffb263SMatthias Ringwald memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 1123deb3ec6SMatthias Ringwald } 113*a0ffb263SMatthias Ringwald return hfp_connection->ag_indicators_nr; 114*a0ffb263SMatthias Ringwald } 115*a0ffb263SMatthias Ringwald 116*a0ffb263SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection){ 117*a0ffb263SMatthias Ringwald // TODO: save only value, and value changed in the hfp_connection? 118*a0ffb263SMatthias Ringwald if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){ 119*a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr; 120*a0ffb263SMatthias Ringwald memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 121*a0ffb263SMatthias Ringwald } 122*a0ffb263SMatthias Ringwald return (hfp_ag_indicator_t *)&(hfp_connection->ag_indicators); 1233deb3ec6SMatthias Ringwald } 1243deb3ec6SMatthias Ringwald 125aa4dd815SMatthias Ringwald static hfp_ag_indicator_t * get_ag_indicator_for_name(const char * name){ 126aa4dd815SMatthias Ringwald int i; 127aa4dd815SMatthias Ringwald for (i = 0; i < hfp_ag_indicators_nr; i++){ 128aa4dd815SMatthias Ringwald if (strcmp(hfp_ag_indicators[i].name, name) == 0){ 129aa4dd815SMatthias Ringwald return &hfp_ag_indicators[i]; 130aa4dd815SMatthias Ringwald } 131aa4dd815SMatthias Ringwald } 132aa4dd815SMatthias Ringwald return NULL; 133aa4dd815SMatthias Ringwald } 134aa4dd815SMatthias Ringwald 135aa4dd815SMatthias Ringwald static int get_ag_indicator_index_for_name(const char * name){ 136aa4dd815SMatthias Ringwald int i; 137aa4dd815SMatthias Ringwald for (i = 0; i < hfp_ag_indicators_nr; i++){ 138aa4dd815SMatthias Ringwald if (strcmp(hfp_ag_indicators[i].name, name) == 0){ 139aa4dd815SMatthias Ringwald return i; 140aa4dd815SMatthias Ringwald } 141aa4dd815SMatthias Ringwald } 142aa4dd815SMatthias Ringwald return -1; 143aa4dd815SMatthias Ringwald } 144aa4dd815SMatthias Ringwald 1453deb3ec6SMatthias Ringwald 1463deb3ec6SMatthias Ringwald void hfp_ag_register_packet_handler(hfp_callback_t callback){ 1473deb3ec6SMatthias Ringwald if (callback == NULL){ 1483deb3ec6SMatthias Ringwald log_error("hfp_ag_register_packet_handler called with NULL callback"); 1493deb3ec6SMatthias Ringwald return; 1503deb3ec6SMatthias Ringwald } 1513deb3ec6SMatthias Ringwald hfp_callback = callback; 152f4000eebSMatthias Ringwald hfp_set_callback(callback); 1533deb3ec6SMatthias Ringwald } 1543deb3ec6SMatthias Ringwald 155aa4dd815SMatthias Ringwald static int use_in_band_tone(){ 156aa4dd815SMatthias Ringwald return get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE); 1573deb3ec6SMatthias Ringwald } 1583deb3ec6SMatthias Ringwald 159*a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){ 160*a0ffb263SMatthias Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_CODEC_NEGOTIATION); 1613deb3ec6SMatthias Ringwald int ag = get_bit(hfp_supported_features, HFP_AGSF_CODEC_NEGOTIATION); 1623deb3ec6SMatthias Ringwald return hf && ag; 1633deb3ec6SMatthias Ringwald } 1643deb3ec6SMatthias Ringwald 165*a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){ 166*a0ffb263SMatthias Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_THREE_WAY_CALLING); 1673deb3ec6SMatthias Ringwald int ag = get_bit(hfp_supported_features, HFP_AGSF_THREE_WAY_CALLING); 1683deb3ec6SMatthias Ringwald return hf && ag; 1693deb3ec6SMatthias Ringwald } 1703deb3ec6SMatthias Ringwald 171*a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){ 172*a0ffb263SMatthias Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_HF_INDICATORS); 1733deb3ec6SMatthias Ringwald int ag = get_bit(hfp_supported_features, HFP_AGSF_HF_INDICATORS); 1743deb3ec6SMatthias Ringwald return hf && ag; 1753deb3ec6SMatthias Ringwald } 1763deb3ec6SMatthias Ringwald 1779b1c3b4dSMatthias 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){ 1783deb3ec6SMatthias Ringwald if (!name){ 1793deb3ec6SMatthias Ringwald name = default_hfp_ag_service_name; 1803deb3ec6SMatthias Ringwald } 1812ef54b27SMatthias Ringwald hfp_create_sdp_record(service, service_record_handle, SDP_HandsfreeAudioGateway, rfcomm_channel_nr, name); 1823deb3ec6SMatthias Ringwald 1833deb3ec6SMatthias Ringwald /* 1843deb3ec6SMatthias Ringwald * 0x01 – Ability to reject a call 1853deb3ec6SMatthias Ringwald * 0x00 – No ability to reject a call 1863deb3ec6SMatthias Ringwald */ 187aa4dd815SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301); // Hands-Free Profile - Network 188aa4dd815SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call); 189aa4dd815SMatthias Ringwald 190aa4dd815SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 191aa4dd815SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, supported_features); 192aa4dd815SMatthias Ringwald } 193aa4dd815SMatthias Ringwald 194aa4dd815SMatthias Ringwald static int hfp_ag_change_in_band_ring_tone_setting_cmd(uint16_t cid){ 195aa4dd815SMatthias Ringwald char buffer[20]; 196aa4dd815SMatthias Ringwald sprintf(buffer, "\r\n%s:%d\r\n", HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone()); 197aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 1983deb3ec6SMatthias Ringwald } 1993deb3ec6SMatthias Ringwald 2003deb3ec6SMatthias Ringwald static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){ 2013deb3ec6SMatthias Ringwald char buffer[40]; 2023deb3ec6SMatthias Ringwald sprintf(buffer, "\r\n%s:%d\r\n\r\nOK\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features); 2033deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2043deb3ec6SMatthias Ringwald } 2053deb3ec6SMatthias Ringwald 2063deb3ec6SMatthias Ringwald static int hfp_ag_ok(uint16_t cid){ 2073deb3ec6SMatthias Ringwald char buffer[10]; 2083deb3ec6SMatthias Ringwald sprintf(buffer, "\r\nOK\r\n"); 2093deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2103deb3ec6SMatthias Ringwald } 2113deb3ec6SMatthias Ringwald 212aa4dd815SMatthias Ringwald static int hfp_ag_ring(uint16_t cid){ 213aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, (char *) "\r\nRING\r\n"); 214aa4dd815SMatthias Ringwald } 215aa4dd815SMatthias Ringwald 216aa4dd815SMatthias Ringwald static int hfp_ag_send_clip(uint16_t cid){ 217aa4dd815SMatthias Ringwald char buffer[50]; 218d0c20769SMatthias Ringwald sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP, hfp_gsm_clip_number(), hfp_gsm_clip_type()); 219aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 220aa4dd815SMatthias Ringwald } 221aa4dd815SMatthias Ringwald 222ce263fc8SMatthias Ringwald static int hfp_send_subscriber_number_cmd(uint16_t cid, uint8_t type, const char * number){ 223ce263fc8SMatthias Ringwald char buffer[50]; 224ce263fc8SMatthias Ringwald sprintf(buffer, "\r\n%s: ,\"%s\",%u, , \r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type); 225ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 226ce263fc8SMatthias Ringwald } 227ce263fc8SMatthias Ringwald 228c1797c7dSMatthias Ringwald static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){ 229aa4dd815SMatthias Ringwald char buffer[50]; 230d0c20769SMatthias Ringwald sprintf(buffer, "\r\n%s: %s\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number()); 231aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 232aa4dd815SMatthias Ringwald } 233aa4dd815SMatthias Ringwald 234aa4dd815SMatthias Ringwald static int hfp_ag_send_call_waiting_notification(uint16_t cid){ 235aa4dd815SMatthias Ringwald char buffer[50]; 236*a0ffb263SMatthias Ringwald sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(), hfp_gsm_clip_type()); 237aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 238aa4dd815SMatthias Ringwald } 239aa4dd815SMatthias Ringwald 2403deb3ec6SMatthias Ringwald static int hfp_ag_error(uint16_t cid){ 2413deb3ec6SMatthias Ringwald char buffer[10]; 2423deb3ec6SMatthias Ringwald sprintf(buffer, "\r\nERROR\r\n"); 2433deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2443deb3ec6SMatthias Ringwald } 2453deb3ec6SMatthias Ringwald 2463deb3ec6SMatthias Ringwald static int hfp_ag_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){ 2473deb3ec6SMatthias Ringwald char buffer[20]; 2483deb3ec6SMatthias Ringwald sprintf(buffer, "\r\n%s=%d\r\n", HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error); 2493deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2503deb3ec6SMatthias Ringwald } 2513deb3ec6SMatthias Ringwald 252ad902e3dSMatthias Ringwald // fast & small implementation for fixed int size 253ad902e3dSMatthias Ringwald static int string_len_for_uint32(uint32_t i){ 254ad902e3dSMatthias Ringwald if (i < 10) return 1; 255ad902e3dSMatthias Ringwald if (i < 100) return 2; 256ad902e3dSMatthias Ringwald if (i < 1000) return 3; 257ad902e3dSMatthias Ringwald if (i < 10000) return 4; 25874386ee0SMatthias Ringwald if (i < 100000) return 5; 259ad902e3dSMatthias Ringwald if (i < 1000000) return 6; 260ad902e3dSMatthias Ringwald if (i < 10000000) return 7; 261ad902e3dSMatthias Ringwald if (i < 100000000) return 8; 262ad902e3dSMatthias Ringwald if (i < 1000000000) return 9; 263ad902e3dSMatthias Ringwald return 10; 264ad902e3dSMatthias Ringwald } 265ad902e3dSMatthias Ringwald 266ad902e3dSMatthias Ringwald // get size for indicator string 267*a0ffb263SMatthias Ringwald static int hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection, int i){ 268ad902e3dSMatthias Ringwald // template: ("$NAME",($MIN,$MAX)) 269*a0ffb263SMatthias Ringwald return 8 + strlen(hfp_ag_get_ag_indicators(hfp_connection)[i].name) 270*a0ffb263SMatthias Ringwald + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range) 271*a0ffb263SMatthias Ringwald + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range); 272ad902e3dSMatthias Ringwald } 273ad902e3dSMatthias Ringwald 274ad902e3dSMatthias Ringwald // store indicator 275*a0ffb263SMatthias Ringwald static void hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection, int i, uint8_t * buffer){ 276ad902e3dSMatthias Ringwald sprintf((char *) buffer, "(\"%s\",(%d,%d)),", 277*a0ffb263SMatthias Ringwald hfp_ag_get_ag_indicators(hfp_connection)[i].name, 278*a0ffb263SMatthias Ringwald hfp_ag_get_ag_indicators(hfp_connection)[i].min_range, 279*a0ffb263SMatthias Ringwald hfp_ag_get_ag_indicators(hfp_connection)[i].max_range); 2803deb3ec6SMatthias Ringwald } 281ad902e3dSMatthias Ringwald 282ad902e3dSMatthias Ringwald // structure: header [indicator [comma indicator]] footer 283*a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_num_segments(hfp_connection_t * hfp_connection){ 284*a0ffb263SMatthias Ringwald int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 285ad902e3dSMatthias Ringwald if (!num_indicators) return 2; 286ad902e3dSMatthias Ringwald return 3 + (num_indicators-1) * 2; 2873deb3ec6SMatthias Ringwald } 288ad902e3dSMatthias Ringwald 289ad902e3dSMatthias Ringwald // get size of individual segment for hfp_ag_retrieve_indicators_cmd 290*a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_get_segment_len(hfp_connection_t * hfp_connection, int index){ 291ad902e3dSMatthias Ringwald if (index == 0) { 292ad902e3dSMatthias Ringwald return strlen(HFP_INDICATOR) + 3; // "\n\r%s:"" 293ad902e3dSMatthias Ringwald } 294ad902e3dSMatthias Ringwald index--; 295*a0ffb263SMatthias Ringwald int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 296ad902e3dSMatthias Ringwald int indicator_index = index >> 1; 297ad902e3dSMatthias Ringwald if ((index & 1) == 0){ 298*a0ffb263SMatthias Ringwald return hfp_ag_indicators_string_size(hfp_connection, indicator_index); 299ad902e3dSMatthias Ringwald } 300ad902e3dSMatthias Ringwald if (indicator_index == num_indicators - 1){ 301ad902e3dSMatthias Ringwald return 8; // "\r\n\r\nOK\r\n" 302ad902e3dSMatthias Ringwald } 303ad902e3dSMatthias Ringwald return 1; // comma 304ad902e3dSMatthias Ringwald } 305ad902e3dSMatthias Ringwald 306*a0ffb263SMatthias Ringwald static void hgp_ag_indicators_cmd_generator_store_segment(hfp_connection_t * hfp_connection, int index, uint8_t * buffer){ 307ad902e3dSMatthias Ringwald if (index == 0){ 308ad902e3dSMatthias Ringwald *buffer++ = '\r'; 30974386ee0SMatthias Ringwald *buffer++ = '\n'; 310ad902e3dSMatthias Ringwald int len = strlen(HFP_INDICATOR); 311ad902e3dSMatthias Ringwald memcpy(buffer, HFP_INDICATOR, len); 312ad902e3dSMatthias Ringwald buffer += len; 313ad902e3dSMatthias Ringwald *buffer++ = ':'; 314ad902e3dSMatthias Ringwald return; 315ad902e3dSMatthias Ringwald } 316ad902e3dSMatthias Ringwald index--; 317*a0ffb263SMatthias Ringwald int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 318ad902e3dSMatthias Ringwald int indicator_index = index >> 1; 319ad902e3dSMatthias Ringwald if ((index & 1) == 0){ 320*a0ffb263SMatthias Ringwald hfp_ag_indicators_string_store(hfp_connection, indicator_index, buffer); 321ad902e3dSMatthias Ringwald return; 322ad902e3dSMatthias Ringwald } 323ad902e3dSMatthias Ringwald if (indicator_index == num_indicators-1){ 324ad902e3dSMatthias Ringwald memcpy(buffer, "\r\n\r\nOK\r\n", 8); 325ad902e3dSMatthias Ringwald return; 326ad902e3dSMatthias Ringwald } 327ad902e3dSMatthias Ringwald *buffer = ','; 3283deb3ec6SMatthias Ringwald } 3293deb3ec6SMatthias Ringwald 3303deb3ec6SMatthias Ringwald static int hfp_hf_indicators_join(char * buffer, int buffer_size){ 3313deb3ec6SMatthias Ringwald if (buffer_size < hfp_ag_indicators_nr * 3) return 0; 3323deb3ec6SMatthias Ringwald int i; 3333deb3ec6SMatthias Ringwald int offset = 0; 334*a0ffb263SMatthias Ringwald for (i = 0; i < hfp_generic_status_indicators_nr-1; i++) { 335*a0ffb263SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid); 3363deb3ec6SMatthias Ringwald } 337*a0ffb263SMatthias Ringwald if (i < hfp_generic_status_indicators_nr){ 338*a0ffb263SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid); 3393deb3ec6SMatthias Ringwald } 3403deb3ec6SMatthias Ringwald return offset; 3413deb3ec6SMatthias Ringwald } 3423deb3ec6SMatthias Ringwald 3433deb3ec6SMatthias Ringwald static int hfp_hf_indicators_initial_status_join(char * buffer, int buffer_size){ 344*a0ffb263SMatthias Ringwald if (buffer_size < hfp_generic_status_indicators_nr * 3) return 0; 3453deb3ec6SMatthias Ringwald int i; 3463deb3ec6SMatthias Ringwald int offset = 0; 347*a0ffb263SMatthias Ringwald for (i = 0; i < hfp_generic_status_indicators_nr; i++) { 348*a0ffb263SMatthias 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); 3493deb3ec6SMatthias Ringwald } 3503deb3ec6SMatthias Ringwald return offset; 3513deb3ec6SMatthias Ringwald } 3523deb3ec6SMatthias Ringwald 3533deb3ec6SMatthias Ringwald static int hfp_ag_indicators_status_join(char * buffer, int buffer_size){ 3543deb3ec6SMatthias Ringwald if (buffer_size < hfp_ag_indicators_nr * 3) return 0; 3553deb3ec6SMatthias Ringwald int i; 3563deb3ec6SMatthias Ringwald int offset = 0; 3573deb3ec6SMatthias Ringwald for (i = 0; i < hfp_ag_indicators_nr-1; i++) { 3583deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_indicators[i].status); 3593deb3ec6SMatthias Ringwald } 3603deb3ec6SMatthias Ringwald if (i<hfp_ag_indicators_nr){ 3613deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_indicators[i].status); 3623deb3ec6SMatthias Ringwald } 3633deb3ec6SMatthias Ringwald return offset; 3643deb3ec6SMatthias Ringwald } 3653deb3ec6SMatthias Ringwald 3663deb3ec6SMatthias Ringwald static int hfp_ag_call_services_join(char * buffer, int buffer_size){ 3673deb3ec6SMatthias Ringwald if (buffer_size < hfp_ag_call_hold_services_nr * 3) return 0; 3683deb3ec6SMatthias Ringwald int i; 3693deb3ec6SMatthias Ringwald int offset = snprintf(buffer, buffer_size, "("); 3703deb3ec6SMatthias Ringwald for (i = 0; i < hfp_ag_call_hold_services_nr-1; i++) { 3713deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%s,", hfp_ag_call_hold_services[i]); 3723deb3ec6SMatthias Ringwald } 3733deb3ec6SMatthias Ringwald if (i<hfp_ag_call_hold_services_nr){ 3743deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%s)", hfp_ag_call_hold_services[i]); 3753deb3ec6SMatthias Ringwald } 3763deb3ec6SMatthias Ringwald return offset; 3773deb3ec6SMatthias Ringwald } 3783deb3ec6SMatthias Ringwald 379*a0ffb263SMatthias Ringwald static int hfp_ag_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, 380ad902e3dSMatthias Ringwald int start_segment, int num_segments, 381*a0ffb263SMatthias Ringwald int (*get_segment_len)(hfp_connection_t * hfp_connection, int segment), 382*a0ffb263SMatthias Ringwald void (*store_segment) (hfp_connection_t * hfp_connection, int segment, uint8_t * buffer)){ 3833deb3ec6SMatthias Ringwald 384ad902e3dSMatthias Ringwald // assumes: can send now == true 385ad902e3dSMatthias Ringwald // assumes: num segments > 0 386ad902e3dSMatthias Ringwald rfcomm_reserve_packet_buffer(); 387ad902e3dSMatthias Ringwald int mtu = rfcomm_get_max_frame_size(cid); 388ad902e3dSMatthias Ringwald uint8_t * data = rfcomm_get_outgoing_buffer(); 389ad902e3dSMatthias Ringwald int offset = 0; 390ad902e3dSMatthias Ringwald int segment = start_segment; 391ad902e3dSMatthias Ringwald while (segment < num_segments){ 392*a0ffb263SMatthias Ringwald int segment_len = get_segment_len(hfp_connection, segment); 393ad902e3dSMatthias Ringwald if (offset + segment_len <= mtu){ 394*a0ffb263SMatthias Ringwald store_segment(hfp_connection, segment, data+offset); 395ad902e3dSMatthias Ringwald offset += segment_len; 396ad902e3dSMatthias Ringwald segment++; 397ad902e3dSMatthias Ringwald } 398ad902e3dSMatthias Ringwald } 399ad902e3dSMatthias Ringwald rfcomm_send_prepared(cid, offset); 400ad902e3dSMatthias Ringwald return segment; 401ad902e3dSMatthias Ringwald } 4023deb3ec6SMatthias Ringwald 403ad902e3dSMatthias Ringwald // returns next segment to store 404*a0ffb263SMatthias Ringwald static int hfp_ag_retrieve_indicators_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, int start_segment){ 405*a0ffb263SMatthias Ringwald int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection); 406*a0ffb263SMatthias Ringwald return hfp_ag_cmd_via_generator(cid, hfp_connection, start_segment, num_segments, 407ad902e3dSMatthias Ringwald hfp_ag_indicators_cmd_generator_get_segment_len, hgp_ag_indicators_cmd_generator_store_segment); 4083deb3ec6SMatthias Ringwald } 4093deb3ec6SMatthias Ringwald 4103deb3ec6SMatthias Ringwald static int hfp_ag_retrieve_indicators_status_cmd(uint16_t cid){ 4113deb3ec6SMatthias Ringwald char buffer[40]; 4123deb3ec6SMatthias Ringwald int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:", HFP_INDICATOR); 4133deb3ec6SMatthias Ringwald offset += hfp_ag_indicators_status_join(buffer+offset, sizeof(buffer)-offset); 4143deb3ec6SMatthias Ringwald 4153deb3ec6SMatthias Ringwald buffer[offset] = 0; 4163deb3ec6SMatthias Ringwald 4173deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n"); 4183deb3ec6SMatthias Ringwald buffer[offset] = 0; 4193deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4203deb3ec6SMatthias Ringwald } 4213deb3ec6SMatthias Ringwald 4223deb3ec6SMatthias Ringwald static int hfp_ag_set_indicator_status_update_cmd(uint16_t cid, uint8_t activate){ 4233deb3ec6SMatthias Ringwald // AT\r\n%s:3,0,0,%d\r\n 4243deb3ec6SMatthias Ringwald return hfp_ag_ok(cid); 4253deb3ec6SMatthias Ringwald } 4263deb3ec6SMatthias Ringwald 4273deb3ec6SMatthias Ringwald 4283deb3ec6SMatthias Ringwald static int hfp_ag_retrieve_can_hold_call_cmd(uint16_t cid){ 429ad902e3dSMatthias Ringwald char buffer[40]; 4303deb3ec6SMatthias Ringwald int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES); 4313deb3ec6SMatthias Ringwald offset += hfp_ag_call_services_join(buffer+offset, sizeof(buffer)-offset); 4323deb3ec6SMatthias Ringwald 4333deb3ec6SMatthias Ringwald buffer[offset] = 0; 4343deb3ec6SMatthias Ringwald 4353deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n"); 4363deb3ec6SMatthias Ringwald buffer[offset] = 0; 4373deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4383deb3ec6SMatthias Ringwald } 4393deb3ec6SMatthias Ringwald 4403deb3ec6SMatthias Ringwald 4413deb3ec6SMatthias Ringwald static int hfp_ag_list_supported_generic_status_indicators_cmd(uint16_t cid){ 4423deb3ec6SMatthias Ringwald return hfp_ag_ok(cid); 4433deb3ec6SMatthias Ringwald } 4443deb3ec6SMatthias Ringwald 4453deb3ec6SMatthias Ringwald static int hfp_ag_retrieve_supported_generic_status_indicators_cmd(uint16_t cid){ 4463deb3ec6SMatthias Ringwald char buffer[40]; 447aa4dd815SMatthias Ringwald int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:(", HFP_GENERIC_STATUS_INDICATOR); 4483deb3ec6SMatthias Ringwald offset += hfp_hf_indicators_join(buffer+offset, sizeof(buffer)-offset); 4493deb3ec6SMatthias Ringwald 4503deb3ec6SMatthias Ringwald buffer[offset] = 0; 4513deb3ec6SMatthias Ringwald 452aa4dd815SMatthias Ringwald offset += snprintf(buffer+offset, sizeof(buffer)-offset, ")\r\n\r\nOK\r\n"); 4533deb3ec6SMatthias Ringwald buffer[offset] = 0; 4543deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4553deb3ec6SMatthias Ringwald } 4563deb3ec6SMatthias Ringwald 4573deb3ec6SMatthias Ringwald static int hfp_ag_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid){ 4583deb3ec6SMatthias Ringwald char buffer[40]; 4593deb3ec6SMatthias Ringwald int offset = hfp_hf_indicators_initial_status_join(buffer, sizeof(buffer)); 4603deb3ec6SMatthias Ringwald 4613deb3ec6SMatthias Ringwald buffer[offset] = 0; 4623deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\nOK\r\n"); 4633deb3ec6SMatthias Ringwald buffer[offset] = 0; 4643deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4653deb3ec6SMatthias Ringwald } 4663deb3ec6SMatthias Ringwald 467aa4dd815SMatthias Ringwald static int hfp_ag_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){ 4683deb3ec6SMatthias Ringwald char buffer[20]; 469aa4dd815SMatthias Ringwald sprintf(buffer, "\r\n%s:%d,%d\r\n", HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index, indicator->status); 4703deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4713deb3ec6SMatthias Ringwald } 4723deb3ec6SMatthias Ringwald 4733deb3ec6SMatthias Ringwald static int hfp_ag_report_network_operator_name_cmd(uint16_t cid, hfp_network_opearator_t op){ 4743deb3ec6SMatthias Ringwald char buffer[40]; 4753deb3ec6SMatthias Ringwald if (strlen(op.name) == 0){ 4763deb3ec6SMatthias Ringwald sprintf(buffer, "\r\n%s:%d,,\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode); 4773deb3ec6SMatthias Ringwald } else { 4783deb3ec6SMatthias Ringwald sprintf(buffer, "\r\n%s:%d,%d,%s\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode, op.format, op.name); 4793deb3ec6SMatthias Ringwald } 4803deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4813deb3ec6SMatthias Ringwald } 4823deb3ec6SMatthias Ringwald 4833deb3ec6SMatthias Ringwald 4843deb3ec6SMatthias Ringwald static int hfp_ag_cmd_suggest_codec(uint16_t cid, uint8_t codec){ 4853deb3ec6SMatthias Ringwald char buffer[30]; 4863deb3ec6SMatthias Ringwald sprintf(buffer, "\r\n%s:%d\r\n", HFP_CONFIRM_COMMON_CODEC, codec); 4873deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4883deb3ec6SMatthias Ringwald } 4893deb3ec6SMatthias Ringwald 490aa4dd815SMatthias Ringwald static int hfp_ag_activate_voice_recognition_cmd(uint16_t cid, uint8_t activate_voice_recognition){ 491aa4dd815SMatthias Ringwald char buffer[30]; 492aa4dd815SMatthias Ringwald sprintf(buffer, "\r\n%s: %d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition); 493aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 494aa4dd815SMatthias Ringwald } 495aa4dd815SMatthias Ringwald 496aa4dd815SMatthias Ringwald static int hfp_ag_set_speaker_gain_cmd(uint16_t cid, uint8_t gain){ 497aa4dd815SMatthias Ringwald char buffer[30]; 498aa4dd815SMatthias Ringwald sprintf(buffer, "\r\n%s:%d\r\n", HFP_SET_SPEAKER_GAIN, gain); 499aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 500aa4dd815SMatthias Ringwald } 501aa4dd815SMatthias Ringwald 502aa4dd815SMatthias Ringwald static int hfp_ag_set_microphone_gain_cmd(uint16_t cid, uint8_t gain){ 503aa4dd815SMatthias Ringwald char buffer[30]; 504aa4dd815SMatthias Ringwald sprintf(buffer, "\r\n%s:%d\r\n", HFP_SET_MICROPHONE_GAIN, gain); 505aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 506aa4dd815SMatthias Ringwald } 507aa4dd815SMatthias Ringwald 508ce263fc8SMatthias Ringwald static int hfp_ag_set_response_and_hold(uint16_t cid, int state){ 509ce263fc8SMatthias Ringwald char buffer[30]; 510ce263fc8SMatthias Ringwald sprintf(buffer, "\r\n%s: %d\r\n", HFP_RESPONSE_AND_HOLD, state); 511ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 512ce263fc8SMatthias Ringwald } 513ce263fc8SMatthias Ringwald 514aa4dd815SMatthias Ringwald 515*a0ffb263SMatthias Ringwald static uint8_t hfp_ag_suggest_codec(hfp_connection_t *hfp_connection){ 5163deb3ec6SMatthias Ringwald int i,j; 517aa4dd815SMatthias Ringwald uint8_t codec = HFP_CODEC_CVSD; 5183deb3ec6SMatthias Ringwald for (i = 0; i < hfp_codecs_nr; i++){ 519*a0ffb263SMatthias Ringwald for (j = 0; j < hfp_connection->remote_codecs_nr; j++){ 520*a0ffb263SMatthias Ringwald if (hfp_connection->remote_codecs[j] == hfp_codecs[i]){ 521*a0ffb263SMatthias Ringwald codec = hfp_connection->remote_codecs[j]; 5223deb3ec6SMatthias Ringwald continue; 5233deb3ec6SMatthias Ringwald } 5243deb3ec6SMatthias Ringwald } 5253deb3ec6SMatthias Ringwald } 5263deb3ec6SMatthias Ringwald return codec; 5273deb3ec6SMatthias Ringwald } 5283deb3ec6SMatthias Ringwald 529*a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){ 530aa4dd815SMatthias Ringwald /* events ( == commands): 531aa4dd815SMatthias Ringwald HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs 532aa4dd815SMatthias Ringwald HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 533aa4dd815SMatthias Ringwald hf_trigger_codec_connection_setup == received BCC 534aa4dd815SMatthias Ringwald ag_trigger_codec_connection_setup == received from AG to send BCS 535aa4dd815SMatthias Ringwald HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS 536aa4dd815SMatthias Ringwald */ 537aa4dd815SMatthias Ringwald 538*a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 539aa4dd815SMatthias Ringwald case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 540*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC; 541aa4dd815SMatthias Ringwald break; 542aa4dd815SMatthias Ringwald case HFP_CODECS_AG_RESEND_COMMON_CODEC: 543*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC; 544aa4dd815SMatthias Ringwald break; 545aa4dd815SMatthias Ringwald default: 546aa4dd815SMatthias Ringwald break; 547aa4dd815SMatthias Ringwald } 548aa4dd815SMatthias Ringwald 549aa4dd815SMatthias Ringwald // printf(" -> State machine: CC\n"); 550aa4dd815SMatthias Ringwald 551*a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 552aa4dd815SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 553aa4dd815SMatthias Ringwald //printf("HFP_CODECS_RECEIVED_LIST \n"); 554*a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){ 555*a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_LIST; 556*a0ffb263SMatthias Ringwald hfp_ag_ok(hfp_connection->rfcomm_cid); 557aa4dd815SMatthias Ringwald return 1; 558aa4dd815SMatthias Ringwald } 559aa4dd815SMatthias Ringwald 560*a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 561aa4dd815SMatthias Ringwald case HFP_CODECS_AG_SENT_COMMON_CODEC: 562aa4dd815SMatthias Ringwald case HFP_CODECS_EXCHANGED: 563*a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_AG_RESEND_COMMON_CODEC; 564aa4dd815SMatthias Ringwald break; 565aa4dd815SMatthias Ringwald default: 566aa4dd815SMatthias Ringwald break; 567aa4dd815SMatthias Ringwald } 568*a0ffb263SMatthias Ringwald hfp_ag_ok(hfp_connection->rfcomm_cid); 569aa4dd815SMatthias Ringwald return 1; 570aa4dd815SMatthias Ringwald 571aa4dd815SMatthias Ringwald case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 572aa4dd815SMatthias Ringwald //printf(" HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP \n"); 573*a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 574*a0ffb263SMatthias Ringwald hfp_ag_ok(hfp_connection->rfcomm_cid); 575aa4dd815SMatthias Ringwald return 1; 576aa4dd815SMatthias Ringwald 577aa4dd815SMatthias Ringwald case HFP_CMD_AG_SEND_COMMON_CODEC: 578aa4dd815SMatthias Ringwald //printf(" HFP_CMD_AG_SEND_COMMON_CODEC \n"); 579*a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_AG_SENT_COMMON_CODEC; 580*a0ffb263SMatthias Ringwald hfp_connection->suggested_codec = hfp_ag_suggest_codec(hfp_connection); 581*a0ffb263SMatthias Ringwald hfp_ag_cmd_suggest_codec(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec); 582aa4dd815SMatthias Ringwald return 1; 583aa4dd815SMatthias Ringwald 584aa4dd815SMatthias Ringwald case HFP_CMD_HF_CONFIRMED_CODEC: 585aa4dd815SMatthias Ringwald //printf("HFP_CMD_HF_CONFIRMED_CODEC \n"); 586*a0ffb263SMatthias Ringwald if (hfp_connection->codec_confirmed != hfp_connection->suggested_codec){ 587*a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_ERROR; 588*a0ffb263SMatthias Ringwald hfp_ag_error(hfp_connection->rfcomm_cid); 589aa4dd815SMatthias Ringwald return 1; 590aa4dd815SMatthias Ringwald } 591*a0ffb263SMatthias Ringwald hfp_connection->negotiated_codec = hfp_connection->codec_confirmed; 592*a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 593aa4dd815SMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_CODECS_CONNECTION_COMPLETE, 0); 594*a0ffb263SMatthias Ringwald hfp_ag_ok(hfp_connection->rfcomm_cid); 595aa4dd815SMatthias Ringwald return 1; 596aa4dd815SMatthias Ringwald default: 597aa4dd815SMatthias Ringwald break; 598aa4dd815SMatthias Ringwald } 599aa4dd815SMatthias Ringwald return 0; 600aa4dd815SMatthias Ringwald } 601aa4dd815SMatthias Ringwald 602*a0ffb263SMatthias Ringwald static void hfp_init_link_settings(hfp_connection_t * hfp_connection){ 603ce263fc8SMatthias Ringwald // determine highest possible link setting 604*a0ffb263SMatthias Ringwald hfp_connection->link_setting = HFP_LINK_SETTINGS_D1; 605*a0ffb263SMatthias Ringwald if (hci_remote_esco_supported(hfp_connection->acl_handle)){ 606*a0ffb263SMatthias Ringwald hfp_connection->link_setting = HFP_LINK_SETTINGS_S3; 607*a0ffb263SMatthias Ringwald if ((hfp_connection->remote_supported_features & (1<<HFP_HFSF_ESCO_S4)) 608ce263fc8SMatthias Ringwald && (hfp_supported_features & (1<<HFP_AGSF_ESCO_S4))){ 609*a0ffb263SMatthias Ringwald hfp_connection->link_setting = HFP_LINK_SETTINGS_S4; 610ce263fc8SMatthias Ringwald } 611ce263fc8SMatthias Ringwald } 612ce263fc8SMatthias Ringwald } 613ce263fc8SMatthias Ringwald 614*a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){ 615*a0ffb263SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 616aa4dd815SMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED, 0); 617aa4dd815SMatthias Ringwald 618*a0ffb263SMatthias Ringwald hfp_init_link_settings(hfp_connection); 619ce263fc8SMatthias Ringwald 620*a0ffb263SMatthias Ringwald // if active call exist, set per-hfp_connection state active, too (when audio is on) 621d0c20769SMatthias Ringwald if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 622*a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE; 623aa4dd815SMatthias Ringwald } 624ce263fc8SMatthias Ringwald // if AG is ringing, also start ringing on the HF 625d0c20769SMatthias Ringwald if (hfp_gsm_call_status() == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS && 626d0c20769SMatthias Ringwald hfp_gsm_callsetup_status() == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 627*a0ffb263SMatthias Ringwald hfp_ag_hf_start_ringing(hfp_connection); 628ce263fc8SMatthias Ringwald } 629aa4dd815SMatthias Ringwald } 6303deb3ec6SMatthias Ringwald 631*a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){ 632*a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 6333deb3ec6SMatthias Ringwald int done = 0; 634aa4dd815SMatthias Ringwald // printf(" -> State machine: SLC\n"); 635*a0ffb263SMatthias Ringwald switch(hfp_connection->command){ 6363deb3ec6SMatthias Ringwald case HFP_CMD_SUPPORTED_FEATURES: 637*a0ffb263SMatthias Ringwald switch(hfp_connection->state){ 6383deb3ec6SMatthias Ringwald case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 639aa4dd815SMatthias Ringwald case HFP_EXCHANGE_SUPPORTED_FEATURES: 640*a0ffb263SMatthias Ringwald if (has_codec_negotiation_feature(hfp_connection)){ 641*a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS; 642aa4dd815SMatthias Ringwald } else { 643*a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 644aa4dd815SMatthias Ringwald } 645*a0ffb263SMatthias Ringwald hfp_ag_exchange_supported_features_cmd(hfp_connection->rfcomm_cid); 646aa4dd815SMatthias Ringwald return 1; 6473deb3ec6SMatthias Ringwald default: 6483deb3ec6SMatthias Ringwald break; 6493deb3ec6SMatthias Ringwald } 6503deb3ec6SMatthias Ringwald break; 6513deb3ec6SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 652*a0ffb263SMatthias Ringwald done = codecs_exchange_state_machine(hfp_connection); 6533deb3ec6SMatthias Ringwald 654*a0ffb263SMatthias Ringwald if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){ 655*a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 6563deb3ec6SMatthias Ringwald } 657aa4dd815SMatthias Ringwald return done; 658aa4dd815SMatthias Ringwald 659aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 660*a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_W4_RETRIEVE_INDICATORS) { 661*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; // prevent reentrance 662*a0ffb263SMatthias Ringwald int next_segment = hfp_ag_retrieve_indicators_cmd_via_generator(hfp_connection->rfcomm_cid, hfp_connection, hfp_connection->send_ag_indicators_segment); 663*a0ffb263SMatthias Ringwald if (next_segment < hfp_ag_indicators_cmd_generator_num_segments(hfp_connection)){ 664ad902e3dSMatthias Ringwald // prepare sending of next segment 665*a0ffb263SMatthias Ringwald hfp_connection->send_ag_indicators_segment = next_segment; 666*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS; 667ad902e3dSMatthias Ringwald } else { 668ad902e3dSMatthias Ringwald // done, go to next state 669*a0ffb263SMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0; 670*a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS; 671ad902e3dSMatthias Ringwald } 672aa4dd815SMatthias Ringwald return 1; 673ad902e3dSMatthias Ringwald } 674ad902e3dSMatthias Ringwald break; 675aa4dd815SMatthias Ringwald 676aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 677*a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break; 678*a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE; 679*a0ffb263SMatthias Ringwald hfp_ag_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid); 680aa4dd815SMatthias Ringwald return 1; 681aa4dd815SMatthias Ringwald 6823deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 683*a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break; 684*a0ffb263SMatthias Ringwald if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 685*a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL; 686*a0ffb263SMatthias Ringwald } else if (has_hf_indicators_feature(hfp_connection)){ 687*a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 688aa4dd815SMatthias Ringwald } else { 689*a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 6903deb3ec6SMatthias Ringwald } 691*a0ffb263SMatthias Ringwald hfp_ag_set_indicator_status_update_cmd(hfp_connection->rfcomm_cid, 1); 692aa4dd815SMatthias Ringwald return 1; 6933deb3ec6SMatthias Ringwald 694aa4dd815SMatthias Ringwald case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES: 695*a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break; 696*a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 697*a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 698aa4dd815SMatthias Ringwald } 699*a0ffb263SMatthias Ringwald hfp_ag_retrieve_can_hold_call_cmd(hfp_connection->rfcomm_cid); 700*a0ffb263SMatthias Ringwald if (!has_hf_indicators_feature(hfp_connection)){ 701*a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 702ce263fc8SMatthias Ringwald } 703aa4dd815SMatthias Ringwald return 1; 704aa4dd815SMatthias Ringwald 705aa4dd815SMatthias Ringwald case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 706*a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break; 707*a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS; 708*a0ffb263SMatthias Ringwald hfp_ag_list_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 709aa4dd815SMatthias Ringwald return 1; 710aa4dd815SMatthias Ringwald 711aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 712*a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break; 713*a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 714*a0ffb263SMatthias Ringwald hfp_ag_retrieve_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 715aa4dd815SMatthias Ringwald return 1; 716aa4dd815SMatthias Ringwald 717aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 718*a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break; 719*a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 720*a0ffb263SMatthias Ringwald hfp_ag_retrieve_initital_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 721aa4dd815SMatthias Ringwald return 1; 7223deb3ec6SMatthias Ringwald default: 7233deb3ec6SMatthias Ringwald break; 7243deb3ec6SMatthias Ringwald } 7253deb3ec6SMatthias Ringwald return done; 7263deb3ec6SMatthias Ringwald } 7273deb3ec6SMatthias Ringwald 728*a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){ 729*a0ffb263SMatthias Ringwald // if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 7303deb3ec6SMatthias Ringwald 731*a0ffb263SMatthias Ringwald int done = codecs_exchange_state_machine(hfp_connection); 732aa4dd815SMatthias Ringwald if (done) return done; 733aa4dd815SMatthias Ringwald 734aa4dd815SMatthias Ringwald // printf(" -> State machine: SLC Queries\n"); 735*a0ffb263SMatthias Ringwald switch(hfp_connection->command){ 736aa4dd815SMatthias Ringwald case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION: 737*a0ffb263SMatthias Ringwald hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition); 738*a0ffb263SMatthias Ringwald hfp_ag_activate_voice_recognition_cmd(hfp_connection->rfcomm_cid, hfp_connection->ag_activate_voice_recognition); 739aa4dd815SMatthias Ringwald return 1; 740aa4dd815SMatthias Ringwald case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION: 741aa4dd815SMatthias Ringwald if (get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)){ 742*a0ffb263SMatthias Ringwald hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition); 743*a0ffb263SMatthias Ringwald hfp_ag_ok(hfp_connection->rfcomm_cid); 744*a0ffb263SMatthias Ringwald hfp_ag_setup_audio_connection(hfp_connection); 745aa4dd815SMatthias Ringwald } else { 746*a0ffb263SMatthias Ringwald hfp_ag_error(hfp_connection->rfcomm_cid); 747aa4dd815SMatthias Ringwald } 748aa4dd815SMatthias Ringwald return 1; 749aa4dd815SMatthias Ringwald case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING: 750*a0ffb263SMatthias Ringwald hfp_ag_change_in_band_ring_tone_setting_cmd(hfp_connection->rfcomm_cid); 751aa4dd815SMatthias Ringwald return 1; 752aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 753*a0ffb263SMatthias Ringwald hfp_ag_report_network_operator_name_cmd(hfp_connection->rfcomm_cid, hfp_connection->network_operator); 754aa4dd815SMatthias Ringwald return 1; 755aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 756*a0ffb263SMatthias Ringwald if (hfp_connection->network_operator.format != 0){ 757*a0ffb263SMatthias Ringwald hfp_ag_error(hfp_connection->rfcomm_cid); 758aa4dd815SMatthias Ringwald } else { 759*a0ffb263SMatthias Ringwald hfp_ag_ok(hfp_connection->rfcomm_cid); 7603deb3ec6SMatthias Ringwald } 761aa4dd815SMatthias Ringwald return 1; 762aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE: 763*a0ffb263SMatthias Ringwald hfp_ag_ok(hfp_connection->rfcomm_cid); 764aa4dd815SMatthias Ringwald return 1; 7653deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR: 766*a0ffb263SMatthias Ringwald if (hfp_connection->extended_audio_gateway_error){ 767*a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 768*a0ffb263SMatthias Ringwald hfp_ag_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, hfp_connection->extended_audio_gateway_error_value); 769aa4dd815SMatthias Ringwald return 1; 7703deb3ec6SMatthias Ringwald } 7713deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 772*a0ffb263SMatthias Ringwald hfp_ag_ok(hfp_connection->rfcomm_cid); 773ce263fc8SMatthias Ringwald return 1; 7743deb3ec6SMatthias Ringwald default: 7753deb3ec6SMatthias Ringwald break; 7763deb3ec6SMatthias Ringwald } 777aa4dd815SMatthias Ringwald return 0; 7783deb3ec6SMatthias Ringwald } 7793deb3ec6SMatthias Ringwald 780*a0ffb263SMatthias Ringwald static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){ 781*a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || 782*a0ffb263SMatthias Ringwald hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0; 7833deb3ec6SMatthias Ringwald 784aa4dd815SMatthias Ringwald 785*a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){ 786*a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 787*a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 0; 788*a0ffb263SMatthias Ringwald gap_disconnect(hfp_connection->sco_handle); 789aa4dd815SMatthias Ringwald return 1; 790aa4dd815SMatthias Ringwald } 791aa4dd815SMatthias Ringwald 792*a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 793aa4dd815SMatthias Ringwald 794aa4dd815SMatthias Ringwald // run codecs exchange 795*a0ffb263SMatthias Ringwald int done = codecs_exchange_state_machine(hfp_connection); 796aa4dd815SMatthias Ringwald if (done) return done; 797*a0ffb263SMatthias Ringwald // printf(" -> State machine: Audio hfp_connection\n"); 798aa4dd815SMatthias Ringwald 799*a0ffb263SMatthias Ringwald if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return done; 800*a0ffb263SMatthias Ringwald if (hfp_connection->establish_audio_connection){ 801*a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 802*a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 803*a0ffb263SMatthias Ringwald hfp_setup_synchronous_connection(hfp_connection->acl_handle, context->link_setting); 804aa4dd815SMatthias Ringwald return 1; 805aa4dd815SMatthias Ringwald } 806aa4dd815SMatthias Ringwald return 0; 807aa4dd815SMatthias Ringwald } 808aa4dd815SMatthias Ringwald 809ec820d77SMatthias Ringwald static hfp_connection_t * hfp_ag_context_for_timer(btstack_timer_source_t * ts){ 810665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 811665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 812aa4dd815SMatthias Ringwald 813665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 814*a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 815*a0ffb263SMatthias Ringwald if ( & hfp_connection->hfp_timeout == ts) { 816*a0ffb263SMatthias Ringwald return hfp_connection; 817aa4dd815SMatthias Ringwald } 818aa4dd815SMatthias Ringwald } 819aa4dd815SMatthias Ringwald return NULL; 820aa4dd815SMatthias Ringwald } 821aa4dd815SMatthias Ringwald 822ec820d77SMatthias Ringwald static void hfp_timeout_handler(btstack_timer_source_t * timer){ 823aa4dd815SMatthias Ringwald hfp_connection_t * context = hfp_ag_context_for_timer(timer); 824aa4dd815SMatthias Ringwald if (!context) return; 825aa4dd815SMatthias Ringwald 826*a0ffb263SMatthias Ringwald log_info("HFP start ring timeout, con handle 0x%02x", hfp_connection->con_handle); 827*a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 1; 828*a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled; 829aa4dd815SMatthias Ringwald 830*a0ffb263SMatthias Ringwald btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout 831*a0ffb263SMatthias Ringwald btstack_run_loop_add_timer(& hfp_connection->hfp_timeout); 832aa4dd815SMatthias Ringwald 833*a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 834aa4dd815SMatthias Ringwald } 835aa4dd815SMatthias Ringwald 836*a0ffb263SMatthias Ringwald static void hfp_timeout_start(hfp_connection_t * hfp_connection){ 837*a0ffb263SMatthias Ringwald btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout); 838*a0ffb263SMatthias Ringwald btstack_run_loop_set_timer_handler(& hfp_connection->hfp_timeout, hfp_timeout_handler); 839*a0ffb263SMatthias Ringwald btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout 840*a0ffb263SMatthias Ringwald btstack_run_loop_add_timer(& hfp_connection->hfp_timeout); 841aa4dd815SMatthias Ringwald } 842aa4dd815SMatthias Ringwald 843*a0ffb263SMatthias Ringwald static void hfp_timeout_stop(hfp_connection_t * hfp_connection){ 844*a0ffb263SMatthias Ringwald log_info("HFP stop ring timeout, con handle 0x%02x", hfp_connection->acl_handle); 845*a0ffb263SMatthias Ringwald btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout); 846aa4dd815SMatthias Ringwald } 847aa4dd815SMatthias Ringwald 848aa4dd815SMatthias Ringwald // 849aa4dd815SMatthias Ringwald // transitition implementations for hfp_ag_call_state_machine 850aa4dd815SMatthias Ringwald // 851aa4dd815SMatthias Ringwald 852*a0ffb263SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection){ 853aa4dd815SMatthias Ringwald if (use_in_band_tone()){ 854*a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING; 855*a0ffb263SMatthias Ringwald hfp_ag_establish_audio_connection(hfp_connection->remote_addr); 856aa4dd815SMatthias Ringwald } else { 857*a0ffb263SMatthias Ringwald hfp_timeout_start(hfp_connection); 858*a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 1; 859*a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled; 860*a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_RINGING; 861*a0ffb263SMatthias Ringwald hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_START_RINGINIG); 862aa4dd815SMatthias Ringwald } 863aa4dd815SMatthias Ringwald } 864aa4dd815SMatthias Ringwald 865*a0ffb263SMatthias Ringwald static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){ 866*a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 0; 867*a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = 0; 868*a0ffb263SMatthias Ringwald hfp_timeout_stop(hfp_connection); 869*a0ffb263SMatthias Ringwald hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_STOP_RINGINIG); 870aa4dd815SMatthias Ringwald } 871aa4dd815SMatthias Ringwald 872aa4dd815SMatthias Ringwald static void hfp_ag_trigger_incoming_call(void){ 873aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name("callsetup"); 874aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 875aa4dd815SMatthias Ringwald 876665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 877665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 878665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 879*a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 880*a0ffb263SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 881*a0ffb263SMatthias Ringwald if (hfp_connection->call_state == HFP_CALL_IDLE){ 882*a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 883aa4dd815SMatthias Ringwald hfp_ag_hf_start_ringing(connection); 884aa4dd815SMatthias Ringwald } 885*a0ffb263SMatthias Ringwald if (hfp_connection->call_state == HFP_CALL_ACTIVE){ 886*a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING; 887aa4dd815SMatthias Ringwald } 888*a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 889aa4dd815SMatthias Ringwald } 890aa4dd815SMatthias Ringwald } 891aa4dd815SMatthias Ringwald 892aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callsetup_state(void){ 893aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name("callsetup"); 894aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 895aa4dd815SMatthias Ringwald 896665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 897665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 898665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 899*a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 900*a0ffb263SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 901*a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 902*a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 903aa4dd815SMatthias Ringwald } 904aa4dd815SMatthias Ringwald } 905aa4dd815SMatthias Ringwald 906aa4dd815SMatthias Ringwald static void hfp_ag_transfer_call_state(void){ 907aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name("call"); 908aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 909aa4dd815SMatthias Ringwald 910665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 911665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 912665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 913*a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 914*a0ffb263SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 915*a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 916*a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 917aa4dd815SMatthias Ringwald } 918aa4dd815SMatthias Ringwald } 919aa4dd815SMatthias Ringwald 920aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callheld_state(void){ 921aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name("callheld"); 922aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 923aa4dd815SMatthias Ringwald 924665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 925665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 926665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 927*a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 928*a0ffb263SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 929*a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 930*a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 931aa4dd815SMatthias Ringwald } 932aa4dd815SMatthias Ringwald } 933aa4dd815SMatthias Ringwald 934aa4dd815SMatthias Ringwald static void hfp_ag_hf_accept_call(hfp_connection_t * source){ 935aa4dd815SMatthias Ringwald 936aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 937aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 938aa4dd815SMatthias Ringwald 939665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 940665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 941665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 942*a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 943*a0ffb263SMatthias Ringwald if (hfp_connection->call_state != HFP_CALL_RINGING && 944*a0ffb263SMatthias Ringwald hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue; 945aa4dd815SMatthias Ringwald 946*a0ffb263SMatthias Ringwald hfp_ag_hf_stop_ringing(hfp_connection); 947*a0ffb263SMatthias Ringwald if (hfp_connection == source){ 948*a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 949aa4dd815SMatthias Ringwald 950aa4dd815SMatthias Ringwald if (use_in_band_tone()){ 951*a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 952aa4dd815SMatthias Ringwald } else { 953*a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE; 954*a0ffb263SMatthias Ringwald hfp_ag_establish_audio_connection(hfp_connection->remote_addr); 955aa4dd815SMatthias Ringwald } 956aa4dd815SMatthias Ringwald 957*a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 958*a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 959aa4dd815SMatthias Ringwald 960aa4dd815SMatthias Ringwald } else { 961*a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 962aa4dd815SMatthias Ringwald } 963*a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 964aa4dd815SMatthias Ringwald } 965aa4dd815SMatthias Ringwald } 966aa4dd815SMatthias Ringwald 967aa4dd815SMatthias Ringwald static void hfp_ag_ag_accept_call(void){ 968aa4dd815SMatthias Ringwald 969aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 970aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 971aa4dd815SMatthias Ringwald 972665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 973665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 974665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 975*a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 976*a0ffb263SMatthias Ringwald if (hfp_connection->call_state != HFP_CALL_RINGING) continue; 977aa4dd815SMatthias Ringwald 978*a0ffb263SMatthias Ringwald hfp_ag_hf_stop_ringing(hfp_connection); 979*a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION; 980aa4dd815SMatthias Ringwald 981*a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 982*a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 983aa4dd815SMatthias Ringwald 984*a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 985aa4dd815SMatthias Ringwald break; // only single 986aa4dd815SMatthias Ringwald } 987aa4dd815SMatthias Ringwald } 988aa4dd815SMatthias Ringwald 989aa4dd815SMatthias Ringwald static void hfp_ag_trigger_reject_call(void){ 990aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 991665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 992665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 993665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 994665d90f2SMatthias Ringwald hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 995aa4dd815SMatthias Ringwald if (connection->call_state != HFP_CALL_RINGING && 996aa4dd815SMatthias Ringwald connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue; 997aa4dd815SMatthias Ringwald hfp_ag_hf_stop_ringing(connection); 998aa4dd815SMatthias Ringwald connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 999aa4dd815SMatthias Ringwald connection->call_state = HFP_CALL_IDLE; 1000aa4dd815SMatthias Ringwald hfp_run_for_context(connection); 1001aa4dd815SMatthias Ringwald } 1002aa4dd815SMatthias Ringwald } 1003aa4dd815SMatthias Ringwald 1004aa4dd815SMatthias Ringwald static void hfp_ag_trigger_terminate_call(void){ 1005aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 1006aa4dd815SMatthias Ringwald 1007665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1008665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1009665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1010*a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1011aa4dd815SMatthias Ringwald hfp_ag_establish_service_level_connection(connection->remote_addr); 1012*a0ffb263SMatthias Ringwald if (hfp_connection->call_state == HFP_CALL_IDLE) continue; 1013*a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 1014*a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1015*a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 1; 1016*a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1017aa4dd815SMatthias Ringwald } 1018*a0ffb263SMatthias Ringwald hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_CALL_TERMINATED); 1019aa4dd815SMatthias Ringwald } 1020aa4dd815SMatthias Ringwald 1021d0c20769SMatthias Ringwald static void hfp_ag_set_callsetup_indicator(){ 1022aa4dd815SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup"); 1023aa4dd815SMatthias Ringwald if (!indicator){ 1024d0c20769SMatthias Ringwald log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing"); 1025aa4dd815SMatthias Ringwald }; 1026d0c20769SMatthias Ringwald indicator->status = hfp_gsm_callsetup_status(); 1027aa4dd815SMatthias Ringwald } 1028aa4dd815SMatthias Ringwald 1029d210d9c4SMatthias Ringwald static void hfp_ag_set_callheld_indicator(){ 1030d210d9c4SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld"); 1031d210d9c4SMatthias Ringwald if (!indicator){ 1032d210d9c4SMatthias Ringwald log_error("hfp_ag_set_callheld_state: callheld indicator is missing"); 1033d210d9c4SMatthias Ringwald }; 1034d210d9c4SMatthias Ringwald indicator->status = hfp_gsm_callheld_status(); 1035d210d9c4SMatthias Ringwald } 1036d210d9c4SMatthias Ringwald 1037d210d9c4SMatthias Ringwald static void hfp_ag_set_call_indicator(){ 1038aa4dd815SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call"); 1039aa4dd815SMatthias Ringwald if (!indicator){ 1040aa4dd815SMatthias Ringwald log_error("hfp_ag_set_call_state: call indicator is missing"); 1041aa4dd815SMatthias Ringwald }; 1042d210d9c4SMatthias Ringwald indicator->status = hfp_gsm_call_status(); 1043aa4dd815SMatthias Ringwald } 1044aa4dd815SMatthias Ringwald 1045aa4dd815SMatthias Ringwald static void hfp_ag_stop_ringing(void){ 1046665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1047665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1048665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1049*a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1050*a0ffb263SMatthias Ringwald if (hfp_connection->call_state != HFP_CALL_RINGING && 1051*a0ffb263SMatthias Ringwald hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue; 1052*a0ffb263SMatthias Ringwald hfp_ag_hf_stop_ringing(hfp_connection); 1053aa4dd815SMatthias Ringwald } 1054aa4dd815SMatthias Ringwald } 1055aa4dd815SMatthias Ringwald 1056aa4dd815SMatthias Ringwald static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){ 1057665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1058665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1059665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1060*a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1061*a0ffb263SMatthias Ringwald if (hfp_connection->call_state == call_state) return hfp_connection; 1062aa4dd815SMatthias Ringwald } 1063aa4dd815SMatthias Ringwald return NULL; 1064aa4dd815SMatthias Ringwald } 1065aa4dd815SMatthias Ringwald 1066a5bdcda8SMatthias Ringwald static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){ 1067665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1068665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1069665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1070*a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1071*a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = state + 1; 1072ce263fc8SMatthias Ringwald } 1073ce263fc8SMatthias Ringwald } 1074ce263fc8SMatthias Ringwald 1075*a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 1076aa4dd815SMatthias Ringwald int indicator_index; 1077*a0ffb263SMatthias Ringwald switch (hfp_connection->call_state){ 1078aa4dd815SMatthias Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING: 1079*a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 1080*a0ffb263SMatthias Ringwald // we got event: audio hfp_connection established 1081*a0ffb263SMatthias Ringwald hfp_timeout_start(hfp_connection); 1082*a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 1; 1083*a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled; 1084*a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_RINGING; 1085*a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_RINGING; 1086*a0ffb263SMatthias Ringwald hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_START_RINGINIG); 1087aa4dd815SMatthias Ringwald break; 1088aa4dd815SMatthias Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE: 1089*a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 1090*a0ffb263SMatthias Ringwald // we got event: audio hfp_connection established 1091*a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1092aa4dd815SMatthias Ringwald break; 1093aa4dd815SMatthias Ringwald case HFP_CALL_W2_SEND_CALL_WAITING: 1094*a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_CHLD; 1095*a0ffb263SMatthias Ringwald hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid); 1096aa4dd815SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callsetup"); 1097*a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1098aa4dd815SMatthias Ringwald break; 1099aa4dd815SMatthias Ringwald default: 1100aa4dd815SMatthias Ringwald break; 1101aa4dd815SMatthias Ringwald } 1102aa4dd815SMatthias Ringwald return 0; 1103aa4dd815SMatthias Ringwald } 1104*a0ffb263SMatthias Ringwald // hfp_connection is used to identify originating HF 1105*a0ffb263SMatthias Ringwald static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){ 1106aa4dd815SMatthias Ringwald int indicator_index; 1107d210d9c4SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1108d210d9c4SMatthias Ringwald int callheld_indicator_index = get_ag_indicator_index_for_name("callheld"); 1109d210d9c4SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 111074386ee0SMatthias Ringwald 1111d210d9c4SMatthias Ringwald //printf("hfp_ag_call_sm event %d \n", event); 1112aa4dd815SMatthias Ringwald switch (event){ 1113aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL: 1114d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1115aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1116d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1117aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1118d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_INCOMING_CALL); 1119d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1120aa4dd815SMatthias Ringwald hfp_ag_trigger_incoming_call(); 1121aa4dd815SMatthias Ringwald printf("AG rings\n"); 1122aa4dd815SMatthias Ringwald break; 1123aa4dd815SMatthias Ringwald default: 11243deb3ec6SMatthias Ringwald break; 11253deb3ec6SMatthias Ringwald } 11263deb3ec6SMatthias Ringwald break; 1127aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1128d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1129aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1130d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_INCOMING_CALL); 1131d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1132aa4dd815SMatthias Ringwald hfp_ag_trigger_incoming_call(); 1133aa4dd815SMatthias Ringwald printf("AG call waiting\n"); 1134aa4dd815SMatthias Ringwald break; 1135aa4dd815SMatthias Ringwald default: 11363deb3ec6SMatthias Ringwald break; 11373deb3ec6SMatthias Ringwald } 11383deb3ec6SMatthias Ringwald break; 1139aa4dd815SMatthias Ringwald } 1140aa4dd815SMatthias Ringwald break; 1141aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG: 1142d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1143aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1144d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1145aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1146d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG); 1147d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1148d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1149aa4dd815SMatthias Ringwald hfp_ag_ag_accept_call(); 1150aa4dd815SMatthias Ringwald printf("AG answers call, accept call by GSM\n"); 1151aa4dd815SMatthias Ringwald break; 1152aa4dd815SMatthias Ringwald default: 11533deb3ec6SMatthias Ringwald break; 11543deb3ec6SMatthias Ringwald } 1155aa4dd815SMatthias Ringwald break; 1156aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1157d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1158aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1159aa4dd815SMatthias Ringwald printf("AG: current call is placed on hold, incoming call gets active\n"); 1160d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG); 1161d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1162d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1163ce263fc8SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1164ce263fc8SMatthias Ringwald hfp_ag_transfer_callheld_state(); 1165aa4dd815SMatthias Ringwald break; 1166aa4dd815SMatthias Ringwald default: 1167aa4dd815SMatthias Ringwald break; 1168aa4dd815SMatthias Ringwald } 1169aa4dd815SMatthias Ringwald break; 1170aa4dd815SMatthias Ringwald } 1171aa4dd815SMatthias Ringwald break; 1172ce263fc8SMatthias Ringwald 1173ce263fc8SMatthias Ringwald case HFP_AG_HELD_CALL_JOINED_BY_AG: 1174d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1175ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1176d0c20769SMatthias Ringwald switch (hfp_gsm_callheld_status()){ 1177ce263fc8SMatthias Ringwald case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED: 1178ce263fc8SMatthias Ringwald printf("AG: joining held call with active call\n"); 1179d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_HELD_CALL_JOINED_BY_AG); 1180d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1181ce263fc8SMatthias Ringwald hfp_ag_transfer_callheld_state(); 1182*a0ffb263SMatthias Ringwald hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_CONFERENCE_CALL); 1183ce263fc8SMatthias Ringwald break; 1184ce263fc8SMatthias Ringwald default: 1185ce263fc8SMatthias Ringwald break; 1186ce263fc8SMatthias Ringwald } 1187ce263fc8SMatthias Ringwald break; 1188ce263fc8SMatthias Ringwald default: 1189ce263fc8SMatthias Ringwald break; 1190ce263fc8SMatthias Ringwald } 1191ce263fc8SMatthias Ringwald break; 1192ce263fc8SMatthias Ringwald 1193aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF: 1194d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1195aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1196d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1197aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1198d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF); 1199d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1200d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1201*a0ffb263SMatthias Ringwald hfp_ag_hf_accept_call(hfp_connection); 1202aa4dd815SMatthias Ringwald printf("HF answers call, accept call by GSM\n"); 1203*a0ffb263SMatthias Ringwald hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_CALL_ANSWERED); 1204aa4dd815SMatthias Ringwald break; 1205aa4dd815SMatthias Ringwald default: 1206aa4dd815SMatthias Ringwald break; 1207aa4dd815SMatthias Ringwald } 12083deb3ec6SMatthias Ringwald break; 12093deb3ec6SMatthias Ringwald default: 12103deb3ec6SMatthias Ringwald break; 12113deb3ec6SMatthias Ringwald } 12123deb3ec6SMatthias Ringwald break; 12133deb3ec6SMatthias Ringwald 1214ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG: 1215d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1216ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1217d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1218ce263fc8SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1219d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG); 1220ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_active = 1; 1221ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1222a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1223ce263fc8SMatthias Ringwald // as with regualr call 1224d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1225d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1226ce263fc8SMatthias Ringwald hfp_ag_ag_accept_call(); 1227ce263fc8SMatthias Ringwald printf("AG response and hold - hold by AG\n"); 1228ce263fc8SMatthias Ringwald break; 1229ce263fc8SMatthias Ringwald default: 1230ce263fc8SMatthias Ringwald break; 1231ce263fc8SMatthias Ringwald } 1232ce263fc8SMatthias Ringwald break; 1233ce263fc8SMatthias Ringwald default: 1234ce263fc8SMatthias Ringwald break; 1235ce263fc8SMatthias Ringwald } 1236ce263fc8SMatthias Ringwald break; 1237ce263fc8SMatthias Ringwald 1238ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF: 1239d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1240ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1241d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1242ce263fc8SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1243d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF); 1244ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_active = 1; 1245ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1246a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1247ce263fc8SMatthias Ringwald // as with regualr call 1248d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1249d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1250*a0ffb263SMatthias Ringwald hfp_ag_hf_accept_call(hfp_connection); 1251ce263fc8SMatthias Ringwald printf("AG response and hold - hold by HF\n"); 1252ce263fc8SMatthias Ringwald break; 1253ce263fc8SMatthias Ringwald default: 1254ce263fc8SMatthias Ringwald break; 1255ce263fc8SMatthias Ringwald } 1256ce263fc8SMatthias Ringwald break; 1257ce263fc8SMatthias Ringwald default: 1258ce263fc8SMatthias Ringwald break; 1259ce263fc8SMatthias Ringwald } 1260ce263fc8SMatthias Ringwald break; 1261ce263fc8SMatthias Ringwald 1262ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG: 1263ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF: 1264ce263fc8SMatthias Ringwald if (!hfp_ag_response_and_hold_active) break; 1265ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1266d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG); 1267a5bdcda8SMatthias Ringwald hfp_ag_response_and_hold_active = 0; 1268ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED; 1269a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1270ce263fc8SMatthias Ringwald printf("Held Call accepted and active\n"); 1271ce263fc8SMatthias Ringwald break; 1272ce263fc8SMatthias Ringwald 1273ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG: 1274ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF: 1275ce263fc8SMatthias Ringwald if (!hfp_ag_response_and_hold_active) break; 1276ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1277d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG); 1278a5bdcda8SMatthias Ringwald hfp_ag_response_and_hold_active = 0; 1279ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1280a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1281ce263fc8SMatthias Ringwald // from terminate by ag 1282d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1283ce263fc8SMatthias Ringwald hfp_ag_trigger_terminate_call(); 1284ce263fc8SMatthias Ringwald break; 1285ce263fc8SMatthias Ringwald 1286aa4dd815SMatthias Ringwald case HFP_AG_TERMINATE_CALL_BY_HF: 1287d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1288aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1289d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1290aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1291d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF); 1292d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1293aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1294aa4dd815SMatthias Ringwald hfp_ag_trigger_reject_call(); 1295aa4dd815SMatthias Ringwald printf("HF Rejected Incoming call, AG terminate call\n"); 1296aa4dd815SMatthias Ringwald break; 1297aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 1298aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1299d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF); 1300d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1301aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1302aa4dd815SMatthias Ringwald printf("AG terminate outgoing call process\n"); 1303aa4dd815SMatthias Ringwald default: 1304aa4dd815SMatthias Ringwald break; 1305aa4dd815SMatthias Ringwald } 1306aa4dd815SMatthias Ringwald break; 1307aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1308d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF); 1309d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1310aa4dd815SMatthias Ringwald hfp_ag_transfer_call_state(); 1311*a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 1312aa4dd815SMatthias Ringwald printf("AG terminate call\n"); 1313aa4dd815SMatthias Ringwald break; 1314aa4dd815SMatthias Ringwald } 1315aa4dd815SMatthias Ringwald break; 13163deb3ec6SMatthias Ringwald 1317aa4dd815SMatthias Ringwald case HFP_AG_TERMINATE_CALL_BY_AG: 1318d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1319aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1320d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1321aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1322d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG); 1323d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1324aa4dd815SMatthias Ringwald hfp_ag_trigger_reject_call(); 1325aa4dd815SMatthias Ringwald printf("AG Rejected Incoming call, AG terminate call\n"); 1326aa4dd815SMatthias Ringwald break; 1327aa4dd815SMatthias Ringwald default: 1328aa4dd815SMatthias Ringwald break; 13293deb3ec6SMatthias Ringwald } 1330aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1331d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG); 1332d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1333d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1334aa4dd815SMatthias Ringwald hfp_ag_trigger_terminate_call(); 1335aa4dd815SMatthias Ringwald printf("AG terminate call\n"); 1336aa4dd815SMatthias Ringwald break; 1337aa4dd815SMatthias Ringwald default: 13383deb3ec6SMatthias Ringwald break; 13393deb3ec6SMatthias Ringwald } 13403deb3ec6SMatthias Ringwald break; 1341aa4dd815SMatthias Ringwald case HFP_AG_CALL_DROPPED: 1342d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1343aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1344d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1345aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1346aa4dd815SMatthias Ringwald hfp_ag_stop_ringing(); 1347aa4dd815SMatthias Ringwald printf("Incoming call interrupted\n"); 1348aa4dd815SMatthias Ringwald break; 1349aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 1350aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1351aa4dd815SMatthias Ringwald printf("Outgoing call interrupted\n"); 1352aa4dd815SMatthias Ringwald printf("AG notify call dropped\n"); 1353aa4dd815SMatthias Ringwald break; 1354aa4dd815SMatthias Ringwald default: 13553deb3ec6SMatthias Ringwald break; 13563deb3ec6SMatthias Ringwald } 1357d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_CALL_DROPPED); 1358d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1359aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1360aa4dd815SMatthias Ringwald break; 1361aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1362ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_active) { 1363d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_CALL_DROPPED); 1364ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1365a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1366ce263fc8SMatthias Ringwald } 1367d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_CALL_DROPPED); 1368d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1369d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1370aa4dd815SMatthias Ringwald hfp_ag_trigger_terminate_call(); 1371aa4dd815SMatthias Ringwald printf("AG notify call dropped\n"); 1372aa4dd815SMatthias Ringwald break; 1373aa4dd815SMatthias Ringwald default: 1374aa4dd815SMatthias Ringwald break; 1375aa4dd815SMatthias Ringwald } 1376aa4dd815SMatthias Ringwald break; 1377aa4dd815SMatthias Ringwald 1378aa4dd815SMatthias Ringwald case HFP_AG_OUTGOING_CALL_INITIATED: 1379d210d9c4SMatthias Ringwald // directly reject call if number of free slots is exceeded 1380d210d9c4SMatthias Ringwald if (!hfp_gsm_call_possible()){ 1381*a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1382*a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1383d210d9c4SMatthias Ringwald break; 1384d210d9c4SMatthias Ringwald } 1385*a0ffb263SMatthias Ringwald hfp_gsm_handle_event_with_call_number(HFP_AG_OUTGOING_CALL_INITIATED, (const char *) &hfp_connection->line_buffer[3]); 13869cae807eSMatthias Ringwald 1387*a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 138874386ee0SMatthias Ringwald 1389*a0ffb263SMatthias Ringwald hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]); 1390aa4dd815SMatthias Ringwald break; 1391aa4dd815SMatthias Ringwald 13929cae807eSMatthias Ringwald case HFP_AG_OUTGOING_REDIAL_INITIATED:{ 1393d210d9c4SMatthias Ringwald // directly reject call if number of free slots is exceeded 1394d210d9c4SMatthias Ringwald if (!hfp_gsm_call_possible()){ 1395*a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1396*a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1397d210d9c4SMatthias Ringwald break; 1398d210d9c4SMatthias Ringwald } 1399d210d9c4SMatthias Ringwald 140074386ee0SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_OUTGOING_REDIAL_INITIATED); 1401*a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 140274386ee0SMatthias Ringwald 14039cae807eSMatthias Ringwald printf("\nRedial last number"); 14049cae807eSMatthias Ringwald char * last_dialed_number = hfp_gsm_last_dialed_number(); 1405aa4dd815SMatthias Ringwald 14069cae807eSMatthias Ringwald if (strlen(last_dialed_number) > 0){ 14079cae807eSMatthias Ringwald printf("\nLast number exists: accept call"); 14089cae807eSMatthias Ringwald hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number); 14099cae807eSMatthias Ringwald } else { 14109cae807eSMatthias Ringwald printf("\nLast number missing: reject call"); 14119cae807eSMatthias Ringwald hfp_ag_outgoing_call_rejected(); 14129cae807eSMatthias Ringwald } 14139cae807eSMatthias Ringwald break; 14149cae807eSMatthias Ringwald } 1415aa4dd815SMatthias Ringwald case HFP_AG_OUTGOING_CALL_REJECTED: 1416*a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 1417*a0ffb263SMatthias Ringwald if (!hfp_connection){ 1418*a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state"); 1419aa4dd815SMatthias Ringwald break; 1420aa4dd815SMatthias Ringwald } 1421d210d9c4SMatthias Ringwald 1422d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_REJECTED); 1423*a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 1424*a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1425*a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1426aa4dd815SMatthias Ringwald break; 1427aa4dd815SMatthias Ringwald 1428d210d9c4SMatthias Ringwald case HFP_AG_OUTGOING_CALL_ACCEPTED:{ 1429*a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 1430*a0ffb263SMatthias Ringwald if (!hfp_connection){ 1431*a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state"); 1432aa4dd815SMatthias Ringwald break; 1433aa4dd815SMatthias Ringwald } 1434aa4dd815SMatthias Ringwald 1435*a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1436*a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING; 1437aa4dd815SMatthias Ringwald 1438aa4dd815SMatthias Ringwald // trigger callsetup to be 1439d0c20769SMatthias Ringwald int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT; 1440d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ACCEPTED); 1441d210d9c4SMatthias Ringwald 1442d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1443aa4dd815SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callsetup"); 1444*a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1445aa4dd815SMatthias Ringwald 1446aa4dd815SMatthias Ringwald // put current call on hold if active 1447d210d9c4SMatthias Ringwald if (put_call_on_hold){ 1448aa4dd815SMatthias Ringwald printf("AG putting current call on hold for new outgoing call\n"); 1449d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1450aa4dd815SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callheld"); 1451*a0ffb263SMatthias Ringwald hfp_ag_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]); 1452aa4dd815SMatthias Ringwald } 1453aa4dd815SMatthias Ringwald 1454aa4dd815SMatthias Ringwald // start audio if needed 1455*a0ffb263SMatthias Ringwald hfp_ag_establish_audio_connection(hfp_connection->remote_addr); 1456aa4dd815SMatthias Ringwald break; 1457d210d9c4SMatthias Ringwald } 1458aa4dd815SMatthias Ringwald case HFP_AG_OUTGOING_CALL_RINGING: 1459*a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1460*a0ffb263SMatthias Ringwald if (!hfp_connection){ 1461*a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state"); 1462aa4dd815SMatthias Ringwald break; 1463aa4dd815SMatthias Ringwald } 1464d210d9c4SMatthias Ringwald 1465d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_RINGING); 1466*a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING; 1467d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1468aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1469aa4dd815SMatthias Ringwald break; 1470aa4dd815SMatthias Ringwald 1471d210d9c4SMatthias Ringwald case HFP_AG_OUTGOING_CALL_ESTABLISHED:{ 1472aa4dd815SMatthias Ringwald // get outgoing call 1473*a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING); 1474*a0ffb263SMatthias Ringwald if (!hfp_connection){ 1475*a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1476aa4dd815SMatthias Ringwald } 1477*a0ffb263SMatthias Ringwald if (!hfp_connection){ 1478*a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection"); 1479aa4dd815SMatthias Ringwald break; 1480aa4dd815SMatthias Ringwald } 1481d210d9c4SMatthias Ringwald 1482d0c20769SMatthias 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; 1483d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ESTABLISHED); 1484*a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1485d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1486d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1487aa4dd815SMatthias Ringwald hfp_ag_transfer_call_state(); 1488aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1489d210d9c4SMatthias Ringwald if (CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS){ 1490d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1491aa4dd815SMatthias Ringwald hfp_ag_transfer_callheld_state(); 14923deb3ec6SMatthias Ringwald } 14933deb3ec6SMatthias Ringwald break; 1494d210d9c4SMatthias Ringwald } 1495d210d9c4SMatthias Ringwald 1496d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_USER_BUSY: 1497d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_CALL_HOLD_USER_BUSY); 1498d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1499*a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1500*a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1501d210d9c4SMatthias Ringwald printf("AG: Call Waiting, User Busy\n"); 1502d210d9c4SMatthias Ringwald break; 1503d210d9c4SMatthias Ringwald 1504d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{ 1505d0c20769SMatthias Ringwald int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1506d0c20769SMatthias Ringwald int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD; 1507d210d9c4SMatthias Ringwald 1508d210d9c4SMatthias Ringwald // Releases all active calls (if any exist) and accepts the other (held or waiting) call. 1509d0c20769SMatthias Ringwald if (call_held || call_setup_in_progress){ 1510*a0ffb263SMatthias Ringwald hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index); 1511d0c20769SMatthias Ringwald 1512d0c20769SMatthias Ringwald } 1513d0c20769SMatthias Ringwald 1514d210d9c4SMatthias Ringwald if (call_setup_in_progress){ 1515d210d9c4SMatthias Ringwald printf("AG: Call Dropped, Accept new call\n"); 1516d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1517*a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1518d210d9c4SMatthias Ringwald } else { 1519d210d9c4SMatthias Ringwald printf("AG: Call Dropped, Resume held call\n"); 1520d210d9c4SMatthias Ringwald } 1521d210d9c4SMatthias Ringwald if (call_held){ 1522d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1523*a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1524d210d9c4SMatthias Ringwald } 1525d0c20769SMatthias Ringwald 1526*a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1527d210d9c4SMatthias Ringwald break; 1528d210d9c4SMatthias Ringwald } 1529d0c20769SMatthias Ringwald 1530d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{ 1531d210d9c4SMatthias Ringwald // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call. 1532d210d9c4SMatthias Ringwald // only update if callsetup changed 1533d0c20769SMatthias Ringwald int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1534*a0ffb263SMatthias Ringwald hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index); 1535d0c20769SMatthias Ringwald 1536d210d9c4SMatthias Ringwald if (call_setup_in_progress){ 1537d210d9c4SMatthias Ringwald printf("AG: Call on Hold, Accept new call\n"); 1538d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1539*a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1540d210d9c4SMatthias Ringwald } else { 1541d210d9c4SMatthias Ringwald printf("AG: Swap calls\n"); 1542d210d9c4SMatthias Ringwald } 1543d0c20769SMatthias Ringwald 1544d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1545d0c20769SMatthias Ringwald // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED); 1546*a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1547*a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1548d210d9c4SMatthias Ringwald break; 1549d210d9c4SMatthias Ringwald } 1550d0c20769SMatthias Ringwald 1551d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_ADD_HELD_CALL: 1552d210d9c4SMatthias Ringwald // Adds a held call to the conversation. 1553d0c20769SMatthias Ringwald if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){ 1554d210d9c4SMatthias Ringwald printf("AG: Join 3-way-call\n"); 1555d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_CALL_HOLD_ADD_HELD_CALL); 1556d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1557*a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1558*a0ffb263SMatthias Ringwald hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_CONFERENCE_CALL); 1559d210d9c4SMatthias Ringwald } 1560*a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1561d210d9c4SMatthias Ringwald break; 1562d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS: 1563d210d9c4SMatthias Ringwald // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer) 1564d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS); 1565d210d9c4SMatthias Ringwald printf("AG: Transfer call -> Connect two calls and disconnect\n"); 1566d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1567d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1568*a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1569*a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1570*a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 1571d210d9c4SMatthias Ringwald break; 1572ce263fc8SMatthias Ringwald 15733deb3ec6SMatthias Ringwald default: 15743deb3ec6SMatthias Ringwald break; 15753deb3ec6SMatthias Ringwald } 1576d210d9c4SMatthias Ringwald 1577d0c20769SMatthias Ringwald 15783deb3ec6SMatthias Ringwald } 15793deb3ec6SMatthias Ringwald 15809cae807eSMatthias Ringwald 1581*a0ffb263SMatthias Ringwald static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){ 15829cae807eSMatthias Ringwald hfp_gsm_call_t * active_call = hfp_gsm_call(call_index); 15839cae807eSMatthias Ringwald if (!active_call) return; 15849cae807eSMatthias Ringwald 15859cae807eSMatthias Ringwald int idx = active_call->index; 15869cae807eSMatthias Ringwald hfp_enhanced_call_dir_t dir = active_call->direction; 15879cae807eSMatthias Ringwald hfp_enhanced_call_status_t status = active_call->enhanced_status; 15889cae807eSMatthias Ringwald hfp_enhanced_call_mode_t mode = active_call->mode; 15899cae807eSMatthias Ringwald hfp_enhanced_call_mpty_t mpty = active_call->mpty; 15909cae807eSMatthias Ringwald uint8_t type = active_call->clip_type; 15919cae807eSMatthias Ringwald char * number = active_call->clip_number; 15929cae807eSMatthias Ringwald 15939cae807eSMatthias Ringwald char buffer[100]; 15949cae807eSMatthias Ringwald // TODO: check length of a buffer, to fit the MTU 15959cae807eSMatthias Ringwald int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty); 15969cae807eSMatthias Ringwald if (number){ 15979cae807eSMatthias Ringwald offset += snprintf(buffer+offset, sizeof(buffer)-offset, ", \"%s\",%u", number, type); 15989cae807eSMatthias Ringwald } 15999cae807eSMatthias Ringwald snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 16009cae807eSMatthias Ringwald printf("hfp_ag_send_current_call_status 000 index %d, dir %d, status %d, mode %d, mpty %d, type %d, number %s\n", idx, dir, status, 16019cae807eSMatthias Ringwald mode, mpty, type, number); 1602*a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 16039cae807eSMatthias Ringwald } 16049cae807eSMatthias Ringwald 1605*a0ffb263SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t *hfp_connection){ 1606*a0ffb263SMatthias Ringwald if (!hfp_connection) return; 1607*a0ffb263SMatthias Ringwald if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) return; 16083deb3ec6SMatthias Ringwald 1609*a0ffb263SMatthias Ringwald if (hfp_connection->send_status_of_current_calls){ 1610*a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 1611*a0ffb263SMatthias Ringwald if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){ 1612*a0ffb263SMatthias Ringwald hfp_connection->next_call_index++; 1613*a0ffb263SMatthias Ringwald hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index); 16149cae807eSMatthias Ringwald } else { 1615*a0ffb263SMatthias Ringwald hfp_connection->next_call_index = 0; 1616*a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1617*a0ffb263SMatthias Ringwald hfp_connection->send_status_of_current_calls = 0; 16189cae807eSMatthias Ringwald } 1619ce263fc8SMatthias Ringwald return; 1620ce263fc8SMatthias Ringwald } 1621ce263fc8SMatthias Ringwald 1622*a0ffb263SMatthias Ringwald if (hfp_connection->ag_notify_incoming_call_waiting){ 1623*a0ffb263SMatthias Ringwald hfp_connection->ag_notify_incoming_call_waiting = 0; 1624*a0ffb263SMatthias Ringwald hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid); 1625aa4dd815SMatthias Ringwald return; 1626aa4dd815SMatthias Ringwald } 1627aa4dd815SMatthias Ringwald 1628*a0ffb263SMatthias Ringwald if (hfp_connection->command == HFP_CMD_UNKNOWN){ 1629*a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 1630*a0ffb263SMatthias Ringwald hfp_connection->send_error = 0; 1631*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1632*a0ffb263SMatthias Ringwald hfp_ag_error(hfp_connection->rfcomm_cid); 1633*a0ffb263SMatthias Ringwald return; 1634*a0ffb263SMatthias Ringwald } 1635*a0ffb263SMatthias Ringwald 1636*a0ffb263SMatthias Ringwald if (hfp_connection->send_error){ 1637*a0ffb263SMatthias Ringwald hfp_connection->send_error = 0; 1638*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1639*a0ffb263SMatthias Ringwald hfp_ag_error(hfp_connection->rfcomm_cid); 1640aa4dd815SMatthias Ringwald return; 1641aa4dd815SMatthias Ringwald } 1642aa4dd815SMatthias Ringwald 1643ce263fc8SMatthias Ringwald // note: before update AG indicators and ok_pending 1644*a0ffb263SMatthias Ringwald if (hfp_connection->send_response_and_hold_status){ 1645*a0ffb263SMatthias Ringwald int status = hfp_connection->send_response_and_hold_status - 1; 1646*a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = 0; 1647*a0ffb263SMatthias Ringwald hfp_ag_set_response_and_hold(hfp_connection->rfcomm_cid, status); 1648ce263fc8SMatthias Ringwald return; 1649ce263fc8SMatthias Ringwald } 1650ce263fc8SMatthias Ringwald 1651*a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending){ 1652*a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 1653*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1654*a0ffb263SMatthias Ringwald hfp_ag_ok(hfp_connection->rfcomm_cid); 1655ce263fc8SMatthias Ringwald return; 1656ce263fc8SMatthias Ringwald } 1657ce263fc8SMatthias Ringwald 1658aa4dd815SMatthias Ringwald // update AG indicators 1659*a0ffb263SMatthias Ringwald if (hfp_connection->ag_indicators_status_update_bitmap){ 1660aa4dd815SMatthias Ringwald int i; 1661*a0ffb263SMatthias Ringwald for (i=0;i<hfp_connection->ag_indicators_nr;i++){ 1662*a0ffb263SMatthias Ringwald if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){ 1663*a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0); 1664*a0ffb263SMatthias Ringwald if (!hfp_connection->enable_status_update_for_ag_indicators) { 1665ce263fc8SMatthias Ringwald log_info("+CMER:3,0,0,0 - not sending update for '%s'", hfp_ag_indicators[i].name); 1666ce263fc8SMatthias Ringwald break; 1667ce263fc8SMatthias Ringwald } 1668*a0ffb263SMatthias Ringwald hfp_ag_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]); 1669aa4dd815SMatthias Ringwald return; 1670aa4dd815SMatthias Ringwald } 1671aa4dd815SMatthias Ringwald } 1672aa4dd815SMatthias Ringwald } 1673aa4dd815SMatthias Ringwald 1674*a0ffb263SMatthias Ringwald if (hfp_connection->ag_ring){ 1675*a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 0; 1676*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1677*a0ffb263SMatthias Ringwald hfp_ag_ring(hfp_connection->rfcomm_cid); 1678aa4dd815SMatthias Ringwald return; 1679aa4dd815SMatthias Ringwald } 1680aa4dd815SMatthias Ringwald 1681*a0ffb263SMatthias Ringwald if (hfp_connection->ag_send_clip){ 1682*a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = 0; 1683*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1684*a0ffb263SMatthias Ringwald hfp_ag_send_clip(hfp_connection->rfcomm_cid); 1685aa4dd815SMatthias Ringwald return; 1686aa4dd815SMatthias Ringwald } 1687aa4dd815SMatthias Ringwald 1688*a0ffb263SMatthias Ringwald if (hfp_connection->send_phone_number_for_voice_tag){ 1689*a0ffb263SMatthias Ringwald hfp_connection->send_phone_number_for_voice_tag = 0; 1690*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1691*a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1692*a0ffb263SMatthias Ringwald hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid); 1693aa4dd815SMatthias Ringwald return; 1694aa4dd815SMatthias Ringwald } 1695aa4dd815SMatthias Ringwald 1696*a0ffb263SMatthias Ringwald if (hfp_connection->send_subscriber_number){ 1697*a0ffb263SMatthias Ringwald if (hfp_connection->next_subscriber_number_to_send < subscriber_numbers_count){ 1698*a0ffb263SMatthias Ringwald hfp_phone_number_t phone = subscriber_numbers[hfp_connection->next_subscriber_number_to_send++]; 1699*a0ffb263SMatthias Ringwald hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number); 1700ce263fc8SMatthias Ringwald } else { 1701*a0ffb263SMatthias Ringwald hfp_connection->send_subscriber_number = 0; 1702*a0ffb263SMatthias Ringwald hfp_connection->next_subscriber_number_to_send = 0; 1703*a0ffb263SMatthias Ringwald hfp_ag_ok(hfp_connection->rfcomm_cid); 1704ce263fc8SMatthias Ringwald } 1705*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1706ce263fc8SMatthias Ringwald } 1707ce263fc8SMatthias Ringwald 1708*a0ffb263SMatthias Ringwald if (hfp_connection->send_microphone_gain){ 1709*a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 0; 1710*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1711*a0ffb263SMatthias Ringwald hfp_ag_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 1712aa4dd815SMatthias Ringwald return; 1713aa4dd815SMatthias Ringwald } 1714aa4dd815SMatthias Ringwald 1715*a0ffb263SMatthias Ringwald if (hfp_connection->send_speaker_gain){ 1716*a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 0; 1717*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1718*a0ffb263SMatthias Ringwald hfp_ag_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 17193deb3ec6SMatthias Ringwald return; 17203deb3ec6SMatthias Ringwald } 17213deb3ec6SMatthias Ringwald 1722*a0ffb263SMatthias Ringwald if (hfp_connection->send_ag_status_indicators){ 1723*a0ffb263SMatthias Ringwald hfp_connection->send_ag_status_indicators = 0; 1724*a0ffb263SMatthias Ringwald hfp_ag_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid); 1725ce263fc8SMatthias Ringwald return; 1726ce263fc8SMatthias Ringwald } 1727ce263fc8SMatthias Ringwald 1728*a0ffb263SMatthias Ringwald int done = hfp_ag_run_for_context_service_level_connection(hfp_connection); 1729aa4dd815SMatthias Ringwald if (!done){ 1730*a0ffb263SMatthias Ringwald done = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection); 17313deb3ec6SMatthias Ringwald } 17323deb3ec6SMatthias Ringwald 1733aa4dd815SMatthias Ringwald if (!done){ 1734*a0ffb263SMatthias Ringwald done = call_setup_state_machine(hfp_connection); 1735aa4dd815SMatthias Ringwald } 1736aa4dd815SMatthias Ringwald 1737aa4dd815SMatthias Ringwald if (!done){ 1738*a0ffb263SMatthias Ringwald done = hfp_ag_run_for_audio_connection(hfp_connection); 1739aa4dd815SMatthias Ringwald } 17403deb3ec6SMatthias Ringwald 1741*a0ffb263SMatthias Ringwald if (hfp_connection->command == HFP_CMD_NONE && !done){ 1742*a0ffb263SMatthias Ringwald // log_info("hfp_connection->command == HFP_CMD_NONE"); 1743*a0ffb263SMatthias Ringwald switch(hfp_connection->state){ 17443deb3ec6SMatthias Ringwald case HFP_W2_DISCONNECT_RFCOMM: 1745*a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 1746*a0ffb263SMatthias Ringwald rfcomm_disconnect(hfp_connection->rfcomm_cid); 17473deb3ec6SMatthias Ringwald break; 17483deb3ec6SMatthias Ringwald default: 17493deb3ec6SMatthias Ringwald break; 17503deb3ec6SMatthias Ringwald } 17513deb3ec6SMatthias Ringwald } 17523deb3ec6SMatthias Ringwald if (done){ 1753*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 17543deb3ec6SMatthias Ringwald } 17553deb3ec6SMatthias Ringwald } 1756ce263fc8SMatthias Ringwald static hfp_generic_status_indicator_t *get_hf_indicator_by_number(int number){ 1757ce263fc8SMatthias Ringwald int i; 1758*a0ffb263SMatthias Ringwald for (i=0;i< hfp_generic_status_indicators_nr;i++){ 1759*a0ffb263SMatthias Ringwald hfp_generic_status_indicator_t * indicator = &hfp_generic_status_indicators[i]; 1760ce263fc8SMatthias Ringwald if (indicator->uuid == number){ 1761ce263fc8SMatthias Ringwald return indicator; 1762ce263fc8SMatthias Ringwald } 1763ce263fc8SMatthias Ringwald } 1764ce263fc8SMatthias Ringwald return NULL; 1765ce263fc8SMatthias Ringwald } 17663deb3ec6SMatthias Ringwald 1767aa4dd815SMatthias Ringwald static void hfp_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1768*a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 1769*a0ffb263SMatthias Ringwald if (!hfp_connection) return; 17701e35c04dSMatthias Ringwald 17711e35c04dSMatthias Ringwald char last_char = packet[size-1]; 17721e35c04dSMatthias Ringwald packet[size-1] = 0; 17731e35c04dSMatthias Ringwald log_info("HFP_RX %s", packet); 17741e35c04dSMatthias Ringwald packet[size-1] = last_char; 17751e35c04dSMatthias Ringwald 17763deb3ec6SMatthias Ringwald int pos; 17773deb3ec6SMatthias Ringwald for (pos = 0; pos < size ; pos++){ 1778*a0ffb263SMatthias Ringwald hfp_parse(hfp_connection, packet[pos], 0); 1779aa4dd815SMatthias Ringwald } 1780ce263fc8SMatthias Ringwald hfp_generic_status_indicator_t * indicator; 1781ce263fc8SMatthias Ringwald int value; 1782*a0ffb263SMatthias Ringwald switch(hfp_connection->command){ 1783ce263fc8SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_QUERY: 1784ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_active){ 1785*a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1; 1786ce263fc8SMatthias Ringwald } 1787*a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1788ce263fc8SMatthias Ringwald break; 1789ce263fc8SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_COMMAND: 1790*a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1791ce263fc8SMatthias Ringwald printf("HF Response and Hold: %u\n", value); 1792ce263fc8SMatthias Ringwald switch(value){ 1793ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD: 1794*a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection); 1795ce263fc8SMatthias Ringwald break; 1796ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED: 1797*a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection); 1798ce263fc8SMatthias Ringwald break; 1799ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED: 1800*a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection); 1801ce263fc8SMatthias Ringwald break; 1802ce263fc8SMatthias Ringwald default: 1803ce263fc8SMatthias Ringwald break; 1804ce263fc8SMatthias Ringwald } 1805*a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1806ce263fc8SMatthias Ringwald break; 1807ce263fc8SMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 1808*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1809ce263fc8SMatthias Ringwald // find indicator by assigned number 1810*a0ffb263SMatthias Ringwald indicator = get_hf_indicator_by_number(hfp_connection->parser_indicator_index); 1811ce263fc8SMatthias Ringwald if (!indicator){ 1812*a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1813ce263fc8SMatthias Ringwald break; 1814ce263fc8SMatthias Ringwald } 1815*a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1816ce263fc8SMatthias Ringwald switch (indicator->uuid){ 1817ce263fc8SMatthias Ringwald case 1: // enhanced security 1818ce263fc8SMatthias Ringwald if (value > 1) { 1819*a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1820ce263fc8SMatthias Ringwald return; 1821ce263fc8SMatthias Ringwald } 1822ce263fc8SMatthias Ringwald printf("HF Indicator 'enhanced security' set to %u\n", value); 1823ce263fc8SMatthias Ringwald break; 1824ce263fc8SMatthias Ringwald case 2: // battery level 1825ce263fc8SMatthias Ringwald if (value > 100){ 1826*a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1827ce263fc8SMatthias Ringwald return; 1828ce263fc8SMatthias Ringwald } 1829ce263fc8SMatthias Ringwald printf("HF Indicator 'battery' set to %u\n", value); 1830ce263fc8SMatthias Ringwald break; 1831ce263fc8SMatthias Ringwald default: 1832ce263fc8SMatthias Ringwald printf("HF Indicator unknown set to %u\n", value); 1833ce263fc8SMatthias Ringwald break; 1834ce263fc8SMatthias Ringwald } 1835*a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1836ce263fc8SMatthias Ringwald break; 1837ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 1838ce263fc8SMatthias Ringwald // expected by SLC state machine 1839*a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break; 1840*a0ffb263SMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0; 1841*a0ffb263SMatthias Ringwald hfp_connection->send_ag_status_indicators = 1; 1842ce263fc8SMatthias Ringwald break; 1843ce263fc8SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 1844*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1845*a0ffb263SMatthias Ringwald hfp_connection->next_call_index = 0; 1846*a0ffb263SMatthias Ringwald hfp_connection->send_status_of_current_calls = 1; 1847ce263fc8SMatthias Ringwald break; 1848ce263fc8SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1849ce263fc8SMatthias Ringwald if (subscriber_numbers_count == 0){ 1850*a0ffb263SMatthias Ringwald hfp_ag_ok(hfp_connection->rfcomm_cid); 1851ce263fc8SMatthias Ringwald break; 1852ce263fc8SMatthias Ringwald } 1853*a0ffb263SMatthias Ringwald hfp_connection->next_subscriber_number_to_send = 0; 1854*a0ffb263SMatthias Ringwald hfp_connection->send_subscriber_number = 1; 1855ce263fc8SMatthias Ringwald break; 1856c1797c7dSMatthias Ringwald case HFP_CMD_TRANSMIT_DTMF_CODES: 1857*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1858*a0ffb263SMatthias Ringwald hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, (const char *) &hfp_connection->line_buffer[0]); 1859c1797c7dSMatthias Ringwald break; 1860aa4dd815SMatthias Ringwald case HFP_CMD_HF_REQUEST_PHONE_NUMBER: 1861*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1862*a0ffb263SMatthias Ringwald hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG); 1863aa4dd815SMatthias Ringwald break; 1864aa4dd815SMatthias Ringwald case HFP_CMD_TURN_OFF_EC_AND_NR: 1865*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1866aa4dd815SMatthias Ringwald if (get_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION)){ 1867*a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1868*a0ffb263SMatthias Ringwald hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION, hfp_connection->ag_echo_and_noise_reduction); 1869*a0ffb263SMatthias Ringwald printf("AG: EC/NR = %u\n", hfp_connection->ag_echo_and_noise_reduction); 1870aa4dd815SMatthias Ringwald } else { 1871*a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1872aa4dd815SMatthias Ringwald } 1873aa4dd815SMatthias Ringwald break; 1874aa4dd815SMatthias Ringwald case HFP_CMD_CALL_ANSWERED: 1875*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1876aa4dd815SMatthias Ringwald printf("HFP: ATA\n"); 1877*a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection); 1878aa4dd815SMatthias Ringwald break; 1879aa4dd815SMatthias Ringwald case HFP_CMD_HANG_UP_CALL: 1880*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1881*a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1882*a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection); 1883aa4dd815SMatthias Ringwald break; 1884aa4dd815SMatthias Ringwald case HFP_CMD_CALL_HOLD: { 1885aa4dd815SMatthias Ringwald // TODO: fully implement this 1886*a0ffb263SMatthias Ringwald log_error("HFP: unhandled call hold type %c", hfp_connection->line_buffer[0]); 1887*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1888*a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1889*a0ffb263SMatthias Ringwald hfp_connection->call_index = 0; 1890d0c20769SMatthias Ringwald 1891*a0ffb263SMatthias Ringwald if (hfp_connection->line_buffer[1] != '\0'){ 1892*a0ffb263SMatthias Ringwald hfp_connection->call_index = atoi((char *)&hfp_connection->line_buffer[1]); 1893d0c20769SMatthias Ringwald } 1894d210d9c4SMatthias Ringwald 1895*a0ffb263SMatthias Ringwald switch (hfp_connection->line_buffer[0]){ 1896aa4dd815SMatthias Ringwald case '0': 1897d0c20769SMatthias Ringwald // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call. 1898*a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection); 1899aa4dd815SMatthias Ringwald break; 1900aa4dd815SMatthias Ringwald case '1': 1901d0c20769SMatthias Ringwald // Releases all active calls (if any exist) and accepts the other (held or waiting) call. 1902d0c20769SMatthias Ringwald // Where both a held and a waiting call exist, the above procedures shall apply to the 1903d0c20769SMatthias Ringwald // waiting call (i.e., not to the held call) in conflicting situation. 1904*a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection); 1905aa4dd815SMatthias Ringwald break; 1906aa4dd815SMatthias Ringwald case '2': 1907d0c20769SMatthias Ringwald // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call. 1908d0c20769SMatthias Ringwald // Where both a held and a waiting call exist, the above procedures shall apply to the 1909d0c20769SMatthias Ringwald // waiting call (i.e., not to the held call) in conflicting situation. 1910*a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection); 1911aa4dd815SMatthias Ringwald break; 1912aa4dd815SMatthias Ringwald case '3': 1913d0c20769SMatthias Ringwald // Adds a held call to the conversation. 1914*a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection); 1915aa4dd815SMatthias Ringwald break; 1916aa4dd815SMatthias Ringwald case '4': 1917d0c20769SMatthias Ringwald // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer). 1918*a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection); 1919aa4dd815SMatthias Ringwald break; 1920aa4dd815SMatthias Ringwald default: 1921aa4dd815SMatthias Ringwald break; 1922aa4dd815SMatthias Ringwald } 1923aa4dd815SMatthias Ringwald break; 1924aa4dd815SMatthias Ringwald } 1925aa4dd815SMatthias Ringwald case HFP_CMD_CALL_PHONE_NUMBER: 1926*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1927*a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED, hfp_connection); 1928aa4dd815SMatthias Ringwald break; 1929aa4dd815SMatthias Ringwald case HFP_CMD_REDIAL_LAST_NUMBER: 1930*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1931*a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection); 1932aa4dd815SMatthias Ringwald break; 1933aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_CLIP: 1934*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1935*a0ffb263SMatthias Ringwald hfp_connection->clip_enabled = hfp_connection->line_buffer[8] != '0'; 1936*a0ffb263SMatthias Ringwald log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled); 1937*a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1938aa4dd815SMatthias Ringwald break; 1939aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION: 1940*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1941*a0ffb263SMatthias Ringwald hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[8] != '0'; 1942*a0ffb263SMatthias Ringwald log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled); 1943*a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1944aa4dd815SMatthias Ringwald break; 1945aa4dd815SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 1946*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1947*a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1948*a0ffb263SMatthias Ringwald printf("HF speaker gain = %u\n", hfp_connection->speaker_gain); 1949aa4dd815SMatthias Ringwald break; 1950aa4dd815SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 1951*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1952*a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1953*a0ffb263SMatthias Ringwald printf("HF microphone gain = %u\n", hfp_connection->microphone_gain); 1954aa4dd815SMatthias Ringwald break; 1955aa4dd815SMatthias Ringwald default: 1956aa4dd815SMatthias Ringwald break; 19573deb3ec6SMatthias Ringwald } 19583deb3ec6SMatthias Ringwald } 19593deb3ec6SMatthias Ringwald 1960aa4dd815SMatthias Ringwald static void hfp_run(void){ 1961*a0ffb263SMatthias Ringwald linked_list_iterator_t it; 1962*a0ffb263SMatthias Ringwald linked_list_iterator_init(&it, hfp_get_connections()); 1963*a0ffb263SMatthias Ringwald while (linked_list_iterator_has_next(&it)){ 1964*a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)linked_list_iterator_next(&it); 1965*a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 19663deb3ec6SMatthias Ringwald } 19673deb3ec6SMatthias Ringwald } 19683deb3ec6SMatthias Ringwald 1969ffbf8201SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 19703deb3ec6SMatthias Ringwald switch (packet_type){ 19713deb3ec6SMatthias Ringwald case RFCOMM_DATA_PACKET: 1972aa4dd815SMatthias Ringwald hfp_handle_rfcomm_data(packet_type, channel, packet, size); 19733deb3ec6SMatthias Ringwald break; 19743deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 1975f4000eebSMatthias Ringwald hfp_handle_hci_event(packet_type, packet, size); 1976aa4dd815SMatthias Ringwald break; 19773deb3ec6SMatthias Ringwald default: 19783deb3ec6SMatthias Ringwald break; 19793deb3ec6SMatthias Ringwald } 19803deb3ec6SMatthias Ringwald 19813deb3ec6SMatthias Ringwald hfp_run(); 19823deb3ec6SMatthias Ringwald } 19833deb3ec6SMatthias Ringwald 19841e35c04dSMatthias Ringwald 1985*a0ffb263SMatthias Ringwald void hfp_ag_init_codecs(int codecs_nr, uint8_t * codecs){ 19863deb3ec6SMatthias Ringwald if (codecs_nr > HFP_MAX_NUM_CODECS){ 19873deb3ec6SMatthias Ringwald log_error("hfp_init: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS); 19883deb3ec6SMatthias Ringwald return; 19893deb3ec6SMatthias Ringwald } 19903deb3ec6SMatthias Ringwald int i; 1991*a0ffb263SMatthias Ringwald hfp_codecs_nr = codecs_nr; 19923deb3ec6SMatthias Ringwald for (i=0; i < codecs_nr; i++){ 19933deb3ec6SMatthias Ringwald hfp_codecs[i] = codecs[i]; 19943deb3ec6SMatthias Ringwald } 1995*a0ffb263SMatthias Ringwald } 19963deb3ec6SMatthias Ringwald 1997*a0ffb263SMatthias Ringwald void hfp_ag_init_supported_features(uint32_t supported_features){ 1998*a0ffb263SMatthias Ringwald hfp_supported_features = supported_features; 1999*a0ffb263SMatthias Ringwald } 20003deb3ec6SMatthias Ringwald 2001*a0ffb263SMatthias Ringwald void hfp_ag_init_ag_indicators(int ag_indicators_nr, hfp_ag_indicator_t * ag_indicators){ 2002*a0ffb263SMatthias Ringwald hfp_ag_indicators_nr = ag_indicators_nr; 2003*a0ffb263SMatthias Ringwald memcpy(hfp_ag_indicators, ag_indicators, ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 2004*a0ffb263SMatthias Ringwald } 20053deb3ec6SMatthias Ringwald 2006*a0ffb263SMatthias Ringwald void hfp_ag_init_hf_indicators(int hf_indicators_nr, hfp_generic_status_indicator_t * hf_indicators){ 2007*a0ffb263SMatthias Ringwald if (hf_indicators_nr > HFP_MAX_NUM_HF_INDICATORS) return; 2008*a0ffb263SMatthias Ringwald hfp_generic_status_indicators_nr = hf_indicators_nr; 2009*a0ffb263SMatthias Ringwald memcpy(hfp_generic_status_indicators, hf_indicators, hf_indicators_nr * sizeof(hfp_generic_status_indicator_t)); 2010*a0ffb263SMatthias Ringwald } 2011*a0ffb263SMatthias Ringwald 2012*a0ffb263SMatthias Ringwald void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){ 20133deb3ec6SMatthias Ringwald hfp_ag_call_hold_services_nr = call_hold_services_nr; 20143deb3ec6SMatthias Ringwald memcpy(hfp_ag_call_hold_services, call_hold_services, call_hold_services_nr * sizeof(char *)); 2015*a0ffb263SMatthias Ringwald } 2016*a0ffb263SMatthias Ringwald 2017*a0ffb263SMatthias Ringwald 2018*a0ffb263SMatthias Ringwald void hfp_ag_init(uint16_t rfcomm_channel_nr){ 2019*a0ffb263SMatthias Ringwald l2cap_init(); 2020*a0ffb263SMatthias Ringwald l2cap_register_packet_handler(packet_handler); 2021*a0ffb263SMatthias Ringwald rfcomm_register_packet_handler(packet_handler); 2022*a0ffb263SMatthias Ringwald hfp_init(rfcomm_channel_nr); 2023aa4dd815SMatthias Ringwald 2024d210d9c4SMatthias Ringwald hfp_ag_response_and_hold_active = 0; 2025d210d9c4SMatthias Ringwald subscriber_numbers = NULL; 2026d210d9c4SMatthias Ringwald subscriber_numbers_count = 0; 2027d210d9c4SMatthias Ringwald 2028d210d9c4SMatthias Ringwald hfp_gsm_init(); 20293deb3ec6SMatthias Ringwald } 20303deb3ec6SMatthias Ringwald 20313deb3ec6SMatthias Ringwald void hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){ 20323deb3ec6SMatthias Ringwald hfp_establish_service_level_connection(bd_addr, SDP_Handsfree); 20333deb3ec6SMatthias Ringwald } 20343deb3ec6SMatthias Ringwald 20353deb3ec6SMatthias Ringwald void hfp_ag_release_service_level_connection(bd_addr_t bd_addr){ 2036*a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 2037*a0ffb263SMatthias Ringwald hfp_release_service_level_connection(hfp_connection); 2038*a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 20393deb3ec6SMatthias Ringwald } 20403deb3ec6SMatthias Ringwald 20413deb3ec6SMatthias Ringwald void hfp_ag_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr, hfp_cme_error_t error){ 2042*a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 2043*a0ffb263SMatthias Ringwald if (!hfp_connection){ 2044*a0ffb263SMatthias Ringwald log_error("HFP HF: hfp_connection doesn't exist."); 20453deb3ec6SMatthias Ringwald return; 20463deb3ec6SMatthias Ringwald } 2047*a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 2048*a0ffb263SMatthias Ringwald if (!hfp_connection->enable_extended_audio_gateway_error_report){ 20493deb3ec6SMatthias Ringwald return; 20503deb3ec6SMatthias Ringwald } 2051*a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = error; 2052*a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 20533deb3ec6SMatthias Ringwald } 20543deb3ec6SMatthias Ringwald 2055*a0ffb263SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){ 2056*a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return; 2057*a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return; 20583deb3ec6SMatthias Ringwald 2059*a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 1; 2060aa4dd815SMatthias Ringwald 2061*a0ffb263SMatthias Ringwald if (!has_codec_negotiation_feature(hfp_connection)){ 2062aa4dd815SMatthias Ringwald log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults"); 2063*a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 20643deb3ec6SMatthias Ringwald } 2065aa4dd815SMatthias Ringwald 2066*a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 2067aa4dd815SMatthias Ringwald case HFP_CODECS_IDLE: 2068aa4dd815SMatthias Ringwald case HFP_CODECS_RECEIVED_LIST: 2069aa4dd815SMatthias Ringwald case HFP_CODECS_AG_RESEND_COMMON_CODEC: 2070aa4dd815SMatthias Ringwald case HFP_CODECS_ERROR: 2071*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC; 2072aa4dd815SMatthias Ringwald break; 2073aa4dd815SMatthias Ringwald default: 2074aa4dd815SMatthias Ringwald break; 2075aa4dd815SMatthias Ringwald } 2076aa4dd815SMatthias Ringwald } 2077aa4dd815SMatthias Ringwald 2078aa4dd815SMatthias Ringwald void hfp_ag_establish_audio_connection(bd_addr_t bd_addr){ 2079aa4dd815SMatthias Ringwald hfp_ag_establish_service_level_connection(bd_addr); 2080*a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 2081aa4dd815SMatthias Ringwald 2082*a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 2083*a0ffb263SMatthias Ringwald hfp_ag_setup_audio_connection(hfp_connection); 2084*a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 20853deb3ec6SMatthias Ringwald } 20863deb3ec6SMatthias Ringwald 20873deb3ec6SMatthias Ringwald void hfp_ag_release_audio_connection(bd_addr_t bd_addr){ 2088*a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 2089*a0ffb263SMatthias Ringwald hfp_release_audio_connection(hfp_connection); 2090*a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 20913deb3ec6SMatthias Ringwald } 2092aa4dd815SMatthias Ringwald 2093aa4dd815SMatthias Ringwald /** 2094aa4dd815SMatthias Ringwald * @brief Enable in-band ring tone 2095aa4dd815SMatthias Ringwald */ 2096aa4dd815SMatthias Ringwald void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){ 2097aa4dd815SMatthias Ringwald if (get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE) == use_in_band_ring_tone){ 2098aa4dd815SMatthias Ringwald return; 2099aa4dd815SMatthias Ringwald } 2100aa4dd815SMatthias Ringwald hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE, use_in_band_ring_tone); 2101aa4dd815SMatthias Ringwald 2102665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 2103665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2104665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2105*a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 2106*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING; 2107*a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 2108aa4dd815SMatthias Ringwald } 2109aa4dd815SMatthias Ringwald } 2110aa4dd815SMatthias Ringwald 2111aa4dd815SMatthias Ringwald /** 2112aa4dd815SMatthias Ringwald * @brief Called from GSM 2113aa4dd815SMatthias Ringwald */ 2114aa4dd815SMatthias Ringwald void hfp_ag_incoming_call(void){ 2115aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL); 2116aa4dd815SMatthias Ringwald } 2117aa4dd815SMatthias Ringwald 2118aa4dd815SMatthias Ringwald /** 2119aa4dd815SMatthias Ringwald * @brief number is stored. 2120aa4dd815SMatthias Ringwald */ 2121aa4dd815SMatthias Ringwald void hfp_ag_set_clip(uint8_t type, const char * number){ 2122d0c20769SMatthias Ringwald hfp_gsm_handle_event_with_clip(HFP_AG_SET_CLIP, type, number); 2123aa4dd815SMatthias Ringwald } 2124aa4dd815SMatthias Ringwald 2125aa4dd815SMatthias Ringwald void hfp_ag_call_dropped(void){ 2126aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL); 2127aa4dd815SMatthias Ringwald } 2128aa4dd815SMatthias Ringwald 2129aa4dd815SMatthias Ringwald // call from AG UI 2130aa4dd815SMatthias Ringwald void hfp_ag_answer_incoming_call(void){ 2131aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL); 2132aa4dd815SMatthias Ringwald } 2133aa4dd815SMatthias Ringwald 2134ce263fc8SMatthias Ringwald void hfp_ag_join_held_call(void){ 2135ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL); 2136ce263fc8SMatthias Ringwald } 2137ce263fc8SMatthias Ringwald 2138aa4dd815SMatthias Ringwald void hfp_ag_terminate_call(void){ 2139aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL); 2140aa4dd815SMatthias Ringwald } 2141aa4dd815SMatthias Ringwald 2142aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_ringing(void){ 2143aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL); 2144aa4dd815SMatthias Ringwald } 2145aa4dd815SMatthias Ringwald 2146aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_established(void){ 2147aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL); 2148aa4dd815SMatthias Ringwald } 2149aa4dd815SMatthias Ringwald 2150aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_rejected(void){ 2151aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL); 2152aa4dd815SMatthias Ringwald } 2153aa4dd815SMatthias Ringwald 2154aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_accepted(void){ 2155aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL); 2156aa4dd815SMatthias Ringwald } 2157ce263fc8SMatthias Ringwald 2158ce263fc8SMatthias Ringwald void hfp_ag_hold_incoming_call(void){ 2159ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL); 2160ce263fc8SMatthias Ringwald } 2161ce263fc8SMatthias Ringwald 2162ce263fc8SMatthias Ringwald void hfp_ag_accept_held_incoming_call(void) { 2163ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL); 2164ce263fc8SMatthias Ringwald } 2165ce263fc8SMatthias Ringwald 2166ce263fc8SMatthias Ringwald void hfp_ag_reject_held_incoming_call(void){ 2167ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL); 2168ce263fc8SMatthias Ringwald } 2169ce263fc8SMatthias Ringwald 2170aa4dd815SMatthias Ringwald static void hfp_ag_set_ag_indicator(const char * name, int value){ 2171aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name(name); 2172aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 2173aa4dd815SMatthias Ringwald hfp_ag_indicators[indicator_index].status = value; 2174aa4dd815SMatthias Ringwald 2175ce263fc8SMatthias Ringwald 2176665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 2177665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2178665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2179*a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 2180*a0ffb263SMatthias Ringwald if (! hfp_connection->ag_indicators[indicator_index].enabled) { 2181ce263fc8SMatthias Ringwald log_info("AG indicator '%s' changed to %u but not enabled", hfp_ag_indicators[indicator_index].name, value); 2182ce263fc8SMatthias Ringwald continue; 2183ce263fc8SMatthias Ringwald } 2184ce263fc8SMatthias Ringwald log_info("AG indicator '%s' changed to %u, request transfer statur", hfp_ag_indicators[indicator_index].name, value); 2185*a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 2186*a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 2187aa4dd815SMatthias Ringwald } 2188aa4dd815SMatthias Ringwald } 2189aa4dd815SMatthias Ringwald 2190aa4dd815SMatthias Ringwald void hfp_ag_set_registration_status(int status){ 2191aa4dd815SMatthias Ringwald hfp_ag_set_ag_indicator("service", status); 2192aa4dd815SMatthias Ringwald } 2193aa4dd815SMatthias Ringwald 2194aa4dd815SMatthias Ringwald void hfp_ag_set_signal_strength(int strength){ 2195aa4dd815SMatthias Ringwald hfp_ag_set_ag_indicator("signal", strength); 2196aa4dd815SMatthias Ringwald } 2197aa4dd815SMatthias Ringwald 2198aa4dd815SMatthias Ringwald void hfp_ag_set_roaming_status(int status){ 2199aa4dd815SMatthias Ringwald hfp_ag_set_ag_indicator("roam", status); 2200aa4dd815SMatthias Ringwald } 2201aa4dd815SMatthias Ringwald 2202aa4dd815SMatthias Ringwald void hfp_ag_set_battery_level(int level){ 2203aa4dd815SMatthias Ringwald hfp_ag_set_ag_indicator("battchg", level); 2204aa4dd815SMatthias Ringwald } 2205aa4dd815SMatthias Ringwald 2206aa4dd815SMatthias Ringwald void hfp_ag_activate_voice_recognition(bd_addr_t bd_addr, int activate){ 2207aa4dd815SMatthias Ringwald if (!get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)) return; 2208*a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 2209aa4dd815SMatthias Ringwald 2210*a0ffb263SMatthias Ringwald if (!get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION)) { 2211aa4dd815SMatthias Ringwald printf("AG cannot acivate voice recognition - not supported by HF\n"); 2212aa4dd815SMatthias Ringwald return; 2213aa4dd815SMatthias Ringwald } 2214aa4dd815SMatthias Ringwald 2215aa4dd815SMatthias Ringwald if (activate){ 2216aa4dd815SMatthias Ringwald hfp_ag_establish_audio_connection(bd_addr); 2217aa4dd815SMatthias Ringwald } 2218aa4dd815SMatthias Ringwald 2219*a0ffb263SMatthias Ringwald hfp_connection->ag_activate_voice_recognition = activate; 2220*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION; 2221*a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 2222aa4dd815SMatthias Ringwald } 2223aa4dd815SMatthias Ringwald 2224aa4dd815SMatthias Ringwald void hfp_ag_set_microphone_gain(bd_addr_t bd_addr, int gain){ 2225*a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 2226*a0ffb263SMatthias Ringwald if (hfp_connection->microphone_gain != gain){ 2227*a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_SET_MICROPHONE_GAIN; 2228*a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = gain; 2229*a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 2230aa4dd815SMatthias Ringwald } 2231*a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 2232aa4dd815SMatthias Ringwald } 2233aa4dd815SMatthias Ringwald 2234aa4dd815SMatthias Ringwald void hfp_ag_set_speaker_gain(bd_addr_t bd_addr, int gain){ 2235*a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 2236*a0ffb263SMatthias Ringwald if (hfp_connection->speaker_gain != gain){ 2237*a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = gain; 2238*a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 2239aa4dd815SMatthias Ringwald } 2240*a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 2241aa4dd815SMatthias Ringwald } 2242aa4dd815SMatthias Ringwald 2243aa4dd815SMatthias Ringwald void hfp_ag_send_phone_number_for_voice_tag(bd_addr_t bd_addr, const char * number){ 2244*a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 2245aa4dd815SMatthias Ringwald hfp_ag_set_clip(0, number); 2246*a0ffb263SMatthias Ringwald hfp_connection->send_phone_number_for_voice_tag = 1; 2247aa4dd815SMatthias Ringwald } 2248aa4dd815SMatthias Ringwald 2249aa4dd815SMatthias Ringwald void hfp_ag_reject_phone_number_for_voice_tag(bd_addr_t bd_addr){ 2250*a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 2251*a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 2252aa4dd815SMatthias Ringwald } 2253aa4dd815SMatthias Ringwald 2254c1797c7dSMatthias Ringwald void hfp_ag_send_dtmf_code_done(bd_addr_t bd_addr){ 2255*a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 2256*a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2257c1797c7dSMatthias Ringwald } 2258aa4dd815SMatthias Ringwald 2259ce263fc8SMatthias Ringwald void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){ 2260ce263fc8SMatthias Ringwald subscriber_numbers = numbers; 2261ce263fc8SMatthias Ringwald subscriber_numbers_count = numbers_count; 2262ce263fc8SMatthias Ringwald } 2263ce263fc8SMatthias Ringwald 22649cae807eSMatthias Ringwald void hfp_ag_clear_last_dialed_number(void){ 22659cae807eSMatthias Ringwald hfp_gsm_clear_last_dialed_number(); 2266ce263fc8SMatthias Ringwald } 2267ce263fc8SMatthias Ringwald 2268*a0ffb263SMatthias Ringwald void hfp_ag_notify_incoming_call_waiting(bd_addr_t bd_addr){ 2269*a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 2270*a0ffb263SMatthias Ringwald if (!hfp_connection->call_waiting_notification_enabled) return; 2271*a0ffb263SMatthias Ringwald 2272*a0ffb263SMatthias Ringwald hfp_connection->ag_notify_incoming_call_waiting = 1; 2273*a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 2274*a0ffb263SMatthias Ringwald } 2275ce263fc8SMatthias Ringwald 2276