13deb3ec6SMatthias Ringwald /* 23deb3ec6SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 33deb3ec6SMatthias Ringwald * 43deb3ec6SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 53deb3ec6SMatthias Ringwald * modification, are permitted provided that the following conditions 63deb3ec6SMatthias Ringwald * are met: 73deb3ec6SMatthias Ringwald * 83deb3ec6SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 93deb3ec6SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 103deb3ec6SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 113deb3ec6SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 123deb3ec6SMatthias Ringwald * documentation and/or other materials provided with the distribution. 133deb3ec6SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 143deb3ec6SMatthias Ringwald * contributors may be used to endorse or promote products derived 153deb3ec6SMatthias Ringwald * from this software without specific prior written permission. 163deb3ec6SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 173deb3ec6SMatthias Ringwald * personal benefit and not for any commercial purpose or for 183deb3ec6SMatthias Ringwald * monetary gain. 193deb3ec6SMatthias Ringwald * 203deb3ec6SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 213deb3ec6SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 223deb3ec6SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 233deb3ec6SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 243deb3ec6SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 253deb3ec6SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 263deb3ec6SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 273deb3ec6SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 283deb3ec6SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 293deb3ec6SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 303deb3ec6SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 313deb3ec6SMatthias Ringwald * SUCH DAMAGE. 323deb3ec6SMatthias Ringwald * 333deb3ec6SMatthias Ringwald * Please inquire about commercial licensing options at 343deb3ec6SMatthias Ringwald * [email protected] 353deb3ec6SMatthias Ringwald * 363deb3ec6SMatthias Ringwald */ 37ab2c6ae4SMatthias Ringwald 38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "hfp_ag.c" 393deb3ec6SMatthias Ringwald 403deb3ec6SMatthias Ringwald // ***************************************************************************** 413deb3ec6SMatthias Ringwald // 4266a048abSMatthias Ringwald // HFP Audio Gateway (AG) unit 433deb3ec6SMatthias Ringwald // 443deb3ec6SMatthias Ringwald // ***************************************************************************** 453deb3ec6SMatthias Ringwald 467907f069SMatthias Ringwald #include "btstack_config.h" 473deb3ec6SMatthias Ringwald 483deb3ec6SMatthias Ringwald #include <stdint.h> 493deb3ec6SMatthias Ringwald #include <stdio.h> 503deb3ec6SMatthias Ringwald #include <stdlib.h> 513deb3ec6SMatthias Ringwald #include <string.h> 523deb3ec6SMatthias Ringwald 5356042629SMatthias Ringwald #include "hci_cmd.h" 5482636622SMatthias Ringwald #include "btstack_run_loop.h" 553deb3ec6SMatthias Ringwald 56235946f1SMatthias Ringwald #include "bluetooth_sdp.h" 573deb3ec6SMatthias Ringwald #include "hci.h" 583deb3ec6SMatthias Ringwald #include "btstack_memory.h" 593deb3ec6SMatthias Ringwald #include "hci_dump.h" 603deb3ec6SMatthias Ringwald #include "l2cap.h" 6116ece135SMatthias Ringwald #include "btstack_debug.h" 62e30a6a47SMatthias Ringwald #include "btstack_event.h" 6359c6af15SMatthias Ringwald #include "classic/core.h" 643edc84c5SMatthias Ringwald #include "classic/hfp.h" 653edc84c5SMatthias Ringwald #include "classic/hfp_ag.h" 66023f2764SMatthias Ringwald #include "classic/hfp_gsm_model.h" 67efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h" 68023f2764SMatthias Ringwald #include "classic/sdp_server.h" 69023f2764SMatthias Ringwald #include "classic/sdp_util.h" 703deb3ec6SMatthias Ringwald 71c5b64319SMatthias Ringwald // private prototypes 72adaba9f3SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t *hfp_connection); 73adaba9f3SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection); 74adaba9f3SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection); 75c5b64319SMatthias Ringwald 76c5b64319SMatthias Ringwald // public prototypes 7735833313SMatthias Ringwald hfp_generic_status_indicator_t * get_hfp_generic_status_indicators(void); 7835833313SMatthias Ringwald int get_hfp_generic_status_indicators_nr(void); 79c5b64319SMatthias Ringwald void set_hfp_generic_status_indicators(hfp_generic_status_indicator_t * indicators, int indicator_nr); 80c5b64319SMatthias Ringwald void set_hfp_ag_indicators(hfp_ag_indicator_t * indicators, int indicator_nr); 81c5b64319SMatthias Ringwald int get_hfp_ag_indicators_nr(hfp_connection_t * context); 82c5b64319SMatthias Ringwald hfp_ag_indicator_t * get_hfp_ag_indicators(hfp_connection_t * context); 83c5b64319SMatthias Ringwald 84*27950165SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 85*27950165SMatthias Ringwald 86c5b64319SMatthias Ringwald // gobals 873deb3ec6SMatthias Ringwald static const char default_hfp_ag_service_name[] = "Voice gateway"; 88aa4dd815SMatthias Ringwald 893deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_AG_SUPPORTED_FEATURES; 90aa4dd815SMatthias Ringwald 913deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0; 923deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS]; 933deb3ec6SMatthias Ringwald 943deb3ec6SMatthias Ringwald static int hfp_ag_indicators_nr = 0; 953deb3ec6SMatthias Ringwald static hfp_ag_indicator_t hfp_ag_indicators[HFP_MAX_NUM_AG_INDICATORS]; 963deb3ec6SMatthias Ringwald 97a0ffb263SMatthias Ringwald static int hfp_generic_status_indicators_nr = 0; 98a0ffb263SMatthias Ringwald static hfp_generic_status_indicator_t hfp_generic_status_indicators[HFP_MAX_NUM_HF_INDICATORS]; 99a0ffb263SMatthias Ringwald 1003deb3ec6SMatthias Ringwald static int hfp_ag_call_hold_services_nr = 0; 1013deb3ec6SMatthias Ringwald static char *hfp_ag_call_hold_services[6]; 102ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_ag_callback; 1033deb3ec6SMatthias Ringwald 104ce263fc8SMatthias Ringwald static hfp_response_and_hold_state_t hfp_ag_response_and_hold_state; 105ce263fc8SMatthias Ringwald static int hfp_ag_response_and_hold_active = 0; 106aa4dd815SMatthias Ringwald 107ce263fc8SMatthias Ringwald // Subcriber information entries 108ce263fc8SMatthias Ringwald static hfp_phone_number_t * subscriber_numbers = NULL; 109ce263fc8SMatthias Ringwald static int subscriber_numbers_count = 0; 110ce263fc8SMatthias Ringwald 11199a10067SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection); 1123deb3ec6SMatthias Ringwald 1133deb3ec6SMatthias Ringwald 114a0ffb263SMatthias Ringwald static int hfp_ag_get_ag_indicators_nr(hfp_connection_t * hfp_connection){ 115a0ffb263SMatthias Ringwald if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){ 116a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr; 117a0ffb263SMatthias Ringwald memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 1183deb3ec6SMatthias Ringwald } 119a0ffb263SMatthias Ringwald return hfp_connection->ag_indicators_nr; 120a0ffb263SMatthias Ringwald } 121a0ffb263SMatthias Ringwald 122a0ffb263SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection){ 123a0ffb263SMatthias Ringwald // TODO: save only value, and value changed in the hfp_connection? 124a0ffb263SMatthias Ringwald if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){ 125a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr; 126a0ffb263SMatthias Ringwald memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 127a0ffb263SMatthias Ringwald } 128a0ffb263SMatthias Ringwald return (hfp_ag_indicator_t *)&(hfp_connection->ag_indicators); 1293deb3ec6SMatthias Ringwald } 1303deb3ec6SMatthias Ringwald 131aa4dd815SMatthias Ringwald static hfp_ag_indicator_t * get_ag_indicator_for_name(const char * name){ 132aa4dd815SMatthias Ringwald int i; 133aa4dd815SMatthias Ringwald for (i = 0; i < hfp_ag_indicators_nr; i++){ 134aa4dd815SMatthias Ringwald if (strcmp(hfp_ag_indicators[i].name, name) == 0){ 135aa4dd815SMatthias Ringwald return &hfp_ag_indicators[i]; 136aa4dd815SMatthias Ringwald } 137aa4dd815SMatthias Ringwald } 138aa4dd815SMatthias Ringwald return NULL; 139aa4dd815SMatthias Ringwald } 140aa4dd815SMatthias Ringwald 141aa4dd815SMatthias Ringwald static int get_ag_indicator_index_for_name(const char * name){ 142aa4dd815SMatthias Ringwald int i; 143aa4dd815SMatthias Ringwald for (i = 0; i < hfp_ag_indicators_nr; i++){ 144aa4dd815SMatthias Ringwald if (strcmp(hfp_ag_indicators[i].name, name) == 0){ 145aa4dd815SMatthias Ringwald return i; 146aa4dd815SMatthias Ringwald } 147aa4dd815SMatthias Ringwald } 148aa4dd815SMatthias Ringwald return -1; 149aa4dd815SMatthias Ringwald } 150aa4dd815SMatthias Ringwald 1519c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_ag_connection_context_for_acl_handle(uint16_t handle){ 1529c9c64c1SMatthias Ringwald btstack_linked_list_iterator_t it; 1539c9c64c1SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1549c9c64c1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1559c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1569c9c64c1SMatthias Ringwald if (hfp_connection->acl_handle != handle) continue; 1579c9c64c1SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1589c9c64c1SMatthias Ringwald return hfp_connection; 1599c9c64c1SMatthias Ringwald } 1609c9c64c1SMatthias Ringwald return NULL; 1619c9c64c1SMatthias Ringwald } 1623deb3ec6SMatthias Ringwald 16313839019SMatthias Ringwald void hfp_ag_register_packet_handler(btstack_packet_handler_t callback){ 1643deb3ec6SMatthias Ringwald if (callback == NULL){ 1653deb3ec6SMatthias Ringwald log_error("hfp_ag_register_packet_handler called with NULL callback"); 1663deb3ec6SMatthias Ringwald return; 1673deb3ec6SMatthias Ringwald } 168ca59be51SMatthias Ringwald hfp_ag_callback = callback; 169ca59be51SMatthias Ringwald hfp_set_ag_callback(callback); 1703deb3ec6SMatthias Ringwald } 1713deb3ec6SMatthias Ringwald 1720cb5b971SMatthias Ringwald static int use_in_band_tone(void){ 173aa4dd815SMatthias Ringwald return get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE); 1743deb3ec6SMatthias Ringwald } 1753deb3ec6SMatthias Ringwald 176a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){ 177a0ffb263SMatthias Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_CODEC_NEGOTIATION); 1783deb3ec6SMatthias Ringwald int ag = get_bit(hfp_supported_features, HFP_AGSF_CODEC_NEGOTIATION); 1793deb3ec6SMatthias Ringwald return hf && ag; 1803deb3ec6SMatthias Ringwald } 1813deb3ec6SMatthias Ringwald 182a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){ 183a0ffb263SMatthias Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_THREE_WAY_CALLING); 1843deb3ec6SMatthias Ringwald int ag = get_bit(hfp_supported_features, HFP_AGSF_THREE_WAY_CALLING); 1853deb3ec6SMatthias Ringwald return hf && ag; 1863deb3ec6SMatthias Ringwald } 1873deb3ec6SMatthias Ringwald 188a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){ 189a0ffb263SMatthias Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_HF_INDICATORS); 1903deb3ec6SMatthias Ringwald int ag = get_bit(hfp_supported_features, HFP_AGSF_HF_INDICATORS); 1913deb3ec6SMatthias Ringwald return hf && ag; 1923deb3ec6SMatthias Ringwald } 1933deb3ec6SMatthias Ringwald 1944f84bf36SMatthias Ringwald void hfp_ag_create_sdp_record(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr, const char * name, uint8_t ability_to_reject_call, uint16_t supported_features, int wide_band_speech){ 1953deb3ec6SMatthias Ringwald if (!name){ 1963deb3ec6SMatthias Ringwald name = default_hfp_ag_service_name; 1973deb3ec6SMatthias Ringwald } 198235946f1SMatthias Ringwald hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, rfcomm_channel_nr, name); 1993deb3ec6SMatthias Ringwald 2003deb3ec6SMatthias Ringwald /* 2013deb3ec6SMatthias Ringwald * 0x01 – Ability to reject a call 2023deb3ec6SMatthias Ringwald * 0x00 – No ability to reject a call 2033deb3ec6SMatthias Ringwald */ 204aa4dd815SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301); // Hands-Free Profile - Network 205aa4dd815SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call); 206aa4dd815SMatthias Ringwald 2074f84bf36SMatthias Ringwald // Construct SupportedFeatures for SDP bitmap: 2084f84bf36SMatthias Ringwald // 2094f84bf36SMatthias Ringwald // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 2104f84bf36SMatthias Ringwald // of the Bits 0 to 4 of the unsolicited result code +BRSF" 2114f84bf36SMatthias Ringwald // 2124f84bf36SMatthias Ringwald uint16_t sdp_features = supported_features &0x1f; 2134f84bf36SMatthias Ringwald if (supported_features & wide_band_speech){ 2144f84bf36SMatthias Ringwald sdp_features |= 1 << 5; // Wide band speech bit 2154f84bf36SMatthias Ringwald } 216aa4dd815SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 2174f84bf36SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 218aa4dd815SMatthias Ringwald } 219aa4dd815SMatthias Ringwald 220485ac19eSMilanka Ringwald static int hfp_ag_send_change_in_band_ring_tone_setting_cmd(uint16_t cid){ 221aa4dd815SMatthias Ringwald char buffer[20]; 222aa4dd815SMatthias Ringwald sprintf(buffer, "\r\n%s:%d\r\n", HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone()); 223aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2243deb3ec6SMatthias Ringwald } 2253deb3ec6SMatthias Ringwald 2263deb3ec6SMatthias Ringwald static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){ 2273deb3ec6SMatthias Ringwald char buffer[40]; 2283deb3ec6SMatthias Ringwald sprintf(buffer, "\r\n%s:%d\r\n\r\nOK\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features); 2293deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2303deb3ec6SMatthias Ringwald } 2313deb3ec6SMatthias Ringwald 232485ac19eSMilanka Ringwald static int hfp_ag_send_ok(uint16_t cid){ 2333deb3ec6SMatthias Ringwald char buffer[10]; 2343deb3ec6SMatthias Ringwald sprintf(buffer, "\r\nOK\r\n"); 2353deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2363deb3ec6SMatthias Ringwald } 2373deb3ec6SMatthias Ringwald 238485ac19eSMilanka Ringwald static int hfp_ag_send_ring(uint16_t cid){ 239aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, (char *) "\r\nRING\r\n"); 240aa4dd815SMatthias Ringwald } 241aa4dd815SMatthias Ringwald 242aa4dd815SMatthias Ringwald static int hfp_ag_send_clip(uint16_t cid){ 243aa4dd815SMatthias Ringwald char buffer[50]; 244d0c20769SMatthias Ringwald sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP, hfp_gsm_clip_number(), hfp_gsm_clip_type()); 245aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 246aa4dd815SMatthias Ringwald } 247aa4dd815SMatthias Ringwald 248ce263fc8SMatthias Ringwald static int hfp_send_subscriber_number_cmd(uint16_t cid, uint8_t type, const char * number){ 249ce263fc8SMatthias Ringwald char buffer[50]; 250ce263fc8SMatthias Ringwald sprintf(buffer, "\r\n%s: ,\"%s\",%u, , \r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type); 251ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 252ce263fc8SMatthias Ringwald } 253ce263fc8SMatthias Ringwald 254c1797c7dSMatthias Ringwald static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){ 255aa4dd815SMatthias Ringwald char buffer[50]; 256d0c20769SMatthias Ringwald sprintf(buffer, "\r\n%s: %s\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number()); 257aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 258aa4dd815SMatthias Ringwald } 259aa4dd815SMatthias Ringwald 260aa4dd815SMatthias Ringwald static int hfp_ag_send_call_waiting_notification(uint16_t cid){ 261aa4dd815SMatthias Ringwald char buffer[50]; 262a0ffb263SMatthias Ringwald sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(), hfp_gsm_clip_type()); 263aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 264aa4dd815SMatthias Ringwald } 265aa4dd815SMatthias Ringwald 266485ac19eSMilanka Ringwald static int hfp_ag_send_error(uint16_t cid){ 2673deb3ec6SMatthias Ringwald char buffer[10]; 2683deb3ec6SMatthias Ringwald sprintf(buffer, "\r\nERROR\r\n"); 2693deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2703deb3ec6SMatthias Ringwald } 2713deb3ec6SMatthias Ringwald 272485ac19eSMilanka Ringwald static int hfp_ag_send_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){ 2733deb3ec6SMatthias Ringwald char buffer[20]; 2743deb3ec6SMatthias Ringwald sprintf(buffer, "\r\n%s=%d\r\n", HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error); 2753deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2763deb3ec6SMatthias Ringwald } 2773deb3ec6SMatthias Ringwald 278ad902e3dSMatthias Ringwald // get size for indicator string 279a0ffb263SMatthias Ringwald static int hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection, int i){ 280ad902e3dSMatthias Ringwald // template: ("$NAME",($MIN,$MAX)) 28145718b6fSMatthias Ringwald return 8 + (int) strlen(hfp_ag_get_ag_indicators(hfp_connection)[i].name) 282a0ffb263SMatthias Ringwald + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range) 283a0ffb263SMatthias Ringwald + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range); 284ad902e3dSMatthias Ringwald } 285ad902e3dSMatthias Ringwald 286ad902e3dSMatthias Ringwald // store indicator 287a0ffb263SMatthias Ringwald static void hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection, int i, uint8_t * buffer){ 288ad902e3dSMatthias Ringwald sprintf((char *) buffer, "(\"%s\",(%d,%d)),", 289a0ffb263SMatthias Ringwald hfp_ag_get_ag_indicators(hfp_connection)[i].name, 290a0ffb263SMatthias Ringwald hfp_ag_get_ag_indicators(hfp_connection)[i].min_range, 291a0ffb263SMatthias Ringwald hfp_ag_get_ag_indicators(hfp_connection)[i].max_range); 2923deb3ec6SMatthias Ringwald } 293ad902e3dSMatthias Ringwald 294ad902e3dSMatthias Ringwald // structure: header [indicator [comma indicator]] footer 295a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_num_segments(hfp_connection_t * hfp_connection){ 296a0ffb263SMatthias Ringwald int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 297ad902e3dSMatthias Ringwald if (!num_indicators) return 2; 298ad902e3dSMatthias Ringwald return 3 + (num_indicators-1) * 2; 2993deb3ec6SMatthias Ringwald } 300ad902e3dSMatthias Ringwald 301ad902e3dSMatthias Ringwald // get size of individual segment for hfp_ag_retrieve_indicators_cmd 302a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_get_segment_len(hfp_connection_t * hfp_connection, int index){ 303ad902e3dSMatthias Ringwald if (index == 0) { 304ad902e3dSMatthias Ringwald return strlen(HFP_INDICATOR) + 3; // "\n\r%s:"" 305ad902e3dSMatthias Ringwald } 306ad902e3dSMatthias Ringwald index--; 307a0ffb263SMatthias Ringwald int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 308ad902e3dSMatthias Ringwald int indicator_index = index >> 1; 309ad902e3dSMatthias Ringwald if ((index & 1) == 0){ 310a0ffb263SMatthias Ringwald return hfp_ag_indicators_string_size(hfp_connection, indicator_index); 311ad902e3dSMatthias Ringwald } 312ad902e3dSMatthias Ringwald if (indicator_index == num_indicators - 1){ 313ad902e3dSMatthias Ringwald return 8; // "\r\n\r\nOK\r\n" 314ad902e3dSMatthias Ringwald } 315ad902e3dSMatthias Ringwald return 1; // comma 316ad902e3dSMatthias Ringwald } 317ad902e3dSMatthias Ringwald 318a0ffb263SMatthias Ringwald static void hgp_ag_indicators_cmd_generator_store_segment(hfp_connection_t * hfp_connection, int index, uint8_t * buffer){ 319ad902e3dSMatthias Ringwald if (index == 0){ 320ad902e3dSMatthias Ringwald *buffer++ = '\r'; 32174386ee0SMatthias Ringwald *buffer++ = '\n'; 322ad902e3dSMatthias Ringwald int len = strlen(HFP_INDICATOR); 323ad902e3dSMatthias Ringwald memcpy(buffer, HFP_INDICATOR, len); 324ad902e3dSMatthias Ringwald buffer += len; 325ad902e3dSMatthias Ringwald *buffer++ = ':'; 326ad902e3dSMatthias Ringwald return; 327ad902e3dSMatthias Ringwald } 328ad902e3dSMatthias Ringwald index--; 329a0ffb263SMatthias Ringwald int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 330ad902e3dSMatthias Ringwald int indicator_index = index >> 1; 331ad902e3dSMatthias Ringwald if ((index & 1) == 0){ 332a0ffb263SMatthias Ringwald hfp_ag_indicators_string_store(hfp_connection, indicator_index, buffer); 333ad902e3dSMatthias Ringwald return; 334ad902e3dSMatthias Ringwald } 335ad902e3dSMatthias Ringwald if (indicator_index == num_indicators-1){ 336ad902e3dSMatthias Ringwald memcpy(buffer, "\r\n\r\nOK\r\n", 8); 337ad902e3dSMatthias Ringwald return; 338ad902e3dSMatthias Ringwald } 339ad902e3dSMatthias Ringwald *buffer = ','; 3403deb3ec6SMatthias Ringwald } 3413deb3ec6SMatthias Ringwald 3423deb3ec6SMatthias Ringwald static int hfp_hf_indicators_join(char * buffer, int buffer_size){ 3433deb3ec6SMatthias Ringwald if (buffer_size < hfp_ag_indicators_nr * 3) return 0; 3443deb3ec6SMatthias Ringwald int i; 3453deb3ec6SMatthias Ringwald int offset = 0; 346a0ffb263SMatthias Ringwald for (i = 0; i < hfp_generic_status_indicators_nr-1; i++) { 347a0ffb263SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid); 3483deb3ec6SMatthias Ringwald } 349a0ffb263SMatthias Ringwald if (i < hfp_generic_status_indicators_nr){ 350a0ffb263SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid); 3513deb3ec6SMatthias Ringwald } 3523deb3ec6SMatthias Ringwald return offset; 3533deb3ec6SMatthias Ringwald } 3543deb3ec6SMatthias Ringwald 3553deb3ec6SMatthias Ringwald static int hfp_hf_indicators_initial_status_join(char * buffer, int buffer_size){ 356a0ffb263SMatthias Ringwald if (buffer_size < hfp_generic_status_indicators_nr * 3) return 0; 3573deb3ec6SMatthias Ringwald int i; 3583deb3ec6SMatthias Ringwald int offset = 0; 359a0ffb263SMatthias Ringwald for (i = 0; i < hfp_generic_status_indicators_nr; i++) { 360a0ffb263SMatthias 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); 3613deb3ec6SMatthias Ringwald } 3623deb3ec6SMatthias Ringwald return offset; 3633deb3ec6SMatthias Ringwald } 3643deb3ec6SMatthias Ringwald 3653deb3ec6SMatthias Ringwald static int hfp_ag_indicators_status_join(char * buffer, int buffer_size){ 3663deb3ec6SMatthias Ringwald if (buffer_size < hfp_ag_indicators_nr * 3) return 0; 3673deb3ec6SMatthias Ringwald int i; 3683deb3ec6SMatthias Ringwald int offset = 0; 3693deb3ec6SMatthias Ringwald for (i = 0; i < hfp_ag_indicators_nr-1; i++) { 3703deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_indicators[i].status); 3713deb3ec6SMatthias Ringwald } 3723deb3ec6SMatthias Ringwald if (i<hfp_ag_indicators_nr){ 3733deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_indicators[i].status); 3743deb3ec6SMatthias Ringwald } 3753deb3ec6SMatthias Ringwald return offset; 3763deb3ec6SMatthias Ringwald } 3773deb3ec6SMatthias Ringwald 3783deb3ec6SMatthias Ringwald static int hfp_ag_call_services_join(char * buffer, int buffer_size){ 3793deb3ec6SMatthias Ringwald if (buffer_size < hfp_ag_call_hold_services_nr * 3) return 0; 3803deb3ec6SMatthias Ringwald int i; 3813deb3ec6SMatthias Ringwald int offset = snprintf(buffer, buffer_size, "("); 3823deb3ec6SMatthias Ringwald for (i = 0; i < hfp_ag_call_hold_services_nr-1; i++) { 3833deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%s,", hfp_ag_call_hold_services[i]); 3843deb3ec6SMatthias Ringwald } 3853deb3ec6SMatthias Ringwald if (i<hfp_ag_call_hold_services_nr){ 3863deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%s)", hfp_ag_call_hold_services[i]); 3873deb3ec6SMatthias Ringwald } 3883deb3ec6SMatthias Ringwald return offset; 3893deb3ec6SMatthias Ringwald } 3903deb3ec6SMatthias Ringwald 391485ac19eSMilanka Ringwald static int hfp_ag_send_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, 392ad902e3dSMatthias Ringwald int start_segment, int num_segments, 393a0ffb263SMatthias Ringwald int (*get_segment_len)(hfp_connection_t * hfp_connection, int segment), 394a0ffb263SMatthias Ringwald void (*store_segment) (hfp_connection_t * hfp_connection, int segment, uint8_t * buffer)){ 3953deb3ec6SMatthias Ringwald 396ad902e3dSMatthias Ringwald // assumes: can send now == true 397ad902e3dSMatthias Ringwald // assumes: num segments > 0 398aba39421SMatthias Ringwald // assumes: individual segments are smaller than MTU 399ad902e3dSMatthias Ringwald rfcomm_reserve_packet_buffer(); 400ad902e3dSMatthias Ringwald int mtu = rfcomm_get_max_frame_size(cid); 401ad902e3dSMatthias Ringwald uint8_t * data = rfcomm_get_outgoing_buffer(); 402ad902e3dSMatthias Ringwald int offset = 0; 403ad902e3dSMatthias Ringwald int segment = start_segment; 404ad902e3dSMatthias Ringwald while (segment < num_segments){ 405a0ffb263SMatthias Ringwald int segment_len = get_segment_len(hfp_connection, segment); 406aba39421SMatthias Ringwald if (offset + segment_len > mtu) break; 407aba39421SMatthias Ringwald // append segement 408a0ffb263SMatthias Ringwald store_segment(hfp_connection, segment, data+offset); 409ad902e3dSMatthias Ringwald offset += segment_len; 410ad902e3dSMatthias Ringwald segment++; 411ad902e3dSMatthias Ringwald } 412ad902e3dSMatthias Ringwald rfcomm_send_prepared(cid, offset); 413ad902e3dSMatthias Ringwald return segment; 414ad902e3dSMatthias Ringwald } 4153deb3ec6SMatthias Ringwald 416ad902e3dSMatthias Ringwald // returns next segment to store 417485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, int start_segment){ 418a0ffb263SMatthias Ringwald int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection); 419485ac19eSMilanka Ringwald return hfp_ag_send_cmd_via_generator(cid, hfp_connection, start_segment, num_segments, 420ad902e3dSMatthias Ringwald hfp_ag_indicators_cmd_generator_get_segment_len, hgp_ag_indicators_cmd_generator_store_segment); 4213deb3ec6SMatthias Ringwald } 4223deb3ec6SMatthias Ringwald 423485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_status_cmd(uint16_t cid){ 4243deb3ec6SMatthias Ringwald char buffer[40]; 42589425bfcSMilanka Ringwald const int size = sizeof(buffer); 42689425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "\r\n%s:", HFP_INDICATOR); 42789425bfcSMilanka Ringwald offset += hfp_ag_indicators_status_join(buffer+offset, size-offset-9); 42889425bfcSMilanka Ringwald offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n"); 4293deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4303deb3ec6SMatthias Ringwald } 4313deb3ec6SMatthias Ringwald 432485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_can_hold_call_cmd(uint16_t cid){ 433ad902e3dSMatthias Ringwald char buffer[40]; 43489425bfcSMilanka Ringwald const int size = sizeof(buffer); 43589425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "\r\n%s:", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES); 43689425bfcSMilanka Ringwald offset += hfp_ag_call_services_join(buffer+offset, size-offset-9); 43789425bfcSMilanka Ringwald offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n"); 4383deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4393deb3ec6SMatthias Ringwald } 4403deb3ec6SMatthias Ringwald 4413deb3ec6SMatthias Ringwald 442485ac19eSMilanka Ringwald static int hfp_ag_send_list_supported_generic_status_indicators_cmd(uint16_t cid){ 443485ac19eSMilanka Ringwald return hfp_ag_send_ok(cid); 4443deb3ec6SMatthias Ringwald } 4453deb3ec6SMatthias Ringwald 446485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(uint16_t cid){ 4473deb3ec6SMatthias Ringwald char buffer[40]; 44889425bfcSMilanka Ringwald const int size = sizeof(buffer); 44989425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "\r\n%s:(", HFP_GENERIC_STATUS_INDICATOR); 45089425bfcSMilanka Ringwald offset += hfp_hf_indicators_join(buffer+offset, size-offset-10); 45189425bfcSMilanka Ringwald offset += snprintf(buffer+offset, size-offset, ")\r\n\r\nOK\r\n"); 4523deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4533deb3ec6SMatthias Ringwald } 4543deb3ec6SMatthias Ringwald 455485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid){ 4563deb3ec6SMatthias Ringwald char buffer[40]; 4571cc1d9e9SMilanka Ringwald int offset = hfp_hf_indicators_initial_status_join(buffer, sizeof(buffer) - 7); 4581cc1d9e9SMilanka Ringwald snprintf(buffer+offset, sizeof(buffer)-offset, "\r\nOK\r\n"); 4593deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4603deb3ec6SMatthias Ringwald } 4613deb3ec6SMatthias Ringwald 462485ac19eSMilanka Ringwald static int hfp_ag_send_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){ 4633deb3ec6SMatthias Ringwald char buffer[20]; 464aa4dd815SMatthias Ringwald sprintf(buffer, "\r\n%s:%d,%d\r\n", HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index, indicator->status); 4653deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4663deb3ec6SMatthias Ringwald } 4673deb3ec6SMatthias Ringwald 468485ac19eSMilanka Ringwald static int hfp_ag_send_report_network_operator_name_cmd(uint16_t cid, hfp_network_opearator_t op){ 4695e71456cSMilanka Ringwald char buffer[41]; 4703deb3ec6SMatthias Ringwald if (strlen(op.name) == 0){ 47189425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s:%d,,\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode); 4723deb3ec6SMatthias Ringwald } else { 47389425bfcSMilanka Ringwald int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:%d,%d,", HFP_QUERY_OPERATOR_SELECTION, op.mode, op.format); 4745e71456cSMilanka Ringwald offset += snprintf(buffer+offset, 16, "%s", op.name); 47589425bfcSMilanka Ringwald snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n"); 4763deb3ec6SMatthias Ringwald } 4773deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 4783deb3ec6SMatthias Ringwald } 4793deb3ec6SMatthias Ringwald 4801cc1d9e9SMilanka Ringwald static inline int hfp_ag_send_cmd_with_int(uint16_t cid, const char * cmd, uint8_t value){ 4811cc1d9e9SMilanka Ringwald char buffer[30]; 48289425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n", cmd, value); 4831cc1d9e9SMilanka Ringwald return send_str_over_rfcomm(cid, buffer); 4841cc1d9e9SMilanka Ringwald } 4853deb3ec6SMatthias Ringwald 486485ac19eSMilanka Ringwald static int hfp_ag_send_suggest_codec_cmd(uint16_t cid, uint8_t codec){ 4871cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec); 4883deb3ec6SMatthias Ringwald } 4893deb3ec6SMatthias Ringwald 490485ac19eSMilanka Ringwald static int hfp_ag_send_activate_voice_recognition_cmd(uint16_t cid, uint8_t activate_voice_recognition){ 4911cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition); 492aa4dd815SMatthias Ringwald } 493aa4dd815SMatthias Ringwald 494485ac19eSMilanka Ringwald static int hfp_ag_send_set_speaker_gain_cmd(uint16_t cid, uint8_t gain){ 4951cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain); 496aa4dd815SMatthias Ringwald } 497aa4dd815SMatthias Ringwald 498485ac19eSMilanka Ringwald static int hfp_ag_send_set_microphone_gain_cmd(uint16_t cid, uint8_t gain){ 4991cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain); 500aa4dd815SMatthias Ringwald } 501aa4dd815SMatthias Ringwald 502485ac19eSMilanka Ringwald static int hfp_ag_send_set_response_and_hold(uint16_t cid, int state){ 5031cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_RESPONSE_AND_HOLD, state); 504ce263fc8SMatthias Ringwald } 505ce263fc8SMatthias Ringwald 506a0ffb263SMatthias Ringwald static uint8_t hfp_ag_suggest_codec(hfp_connection_t *hfp_connection){ 507bc1b1537SMilanka Ringwald if (hfp_connection->sco_for_msbc_failed) return HFP_CODEC_CVSD; 508bc1b1537SMilanka Ringwald 5096a7f44bdSMilanka Ringwald if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_codecs_nr, hfp_codecs)){ 5106a7f44bdSMilanka Ringwald if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){ 511df327ff3SMilanka Ringwald return HFP_CODEC_MSBC; 5123deb3ec6SMatthias Ringwald } 5133deb3ec6SMatthias Ringwald } 514df327ff3SMilanka Ringwald return HFP_CODEC_CVSD; 5153deb3ec6SMatthias Ringwald } 5163deb3ec6SMatthias Ringwald 5177522e673SMatthias Ringwald static void hfp_init_link_settings(hfp_connection_t * hfp_connection){ 5187522e673SMatthias Ringwald // determine highest possible link setting 5197522e673SMatthias Ringwald hfp_connection->link_setting = HFP_LINK_SETTINGS_D1; 520d9f77559SMatthias Ringwald // anything else requires eSCO support on both sides 521d9f77559SMatthias Ringwald if (hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){ 5227522e673SMatthias Ringwald switch (hfp_connection->negotiated_codec){ 5237522e673SMatthias Ringwald case HFP_CODEC_CVSD: 5247522e673SMatthias Ringwald hfp_connection->link_setting = HFP_LINK_SETTINGS_S3; 5257522e673SMatthias Ringwald if ((hfp_connection->remote_supported_features & (1<<HFP_HFSF_ESCO_S4)) 5267522e673SMatthias Ringwald && (hfp_supported_features & (1<<HFP_AGSF_ESCO_S4))){ 5277522e673SMatthias Ringwald hfp_connection->link_setting = HFP_LINK_SETTINGS_S4; 5287522e673SMatthias Ringwald } 5297522e673SMatthias Ringwald break; 5307522e673SMatthias Ringwald case HFP_CODEC_MSBC: 5317522e673SMatthias Ringwald hfp_connection->link_setting = HFP_LINK_SETTINGS_T2; 5327522e673SMatthias Ringwald break; 533d9f77559SMatthias Ringwald default: 534d9f77559SMatthias Ringwald break; 535d9f77559SMatthias Ringwald } 5367522e673SMatthias Ringwald } 5377522e673SMatthias Ringwald log_info("hfp_init_link_settings: %u", hfp_connection->link_setting); 5387522e673SMatthias Ringwald } 5397522e673SMatthias Ringwald 540a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){ 541aa4dd815SMatthias Ringwald /* events ( == commands): 542aa4dd815SMatthias Ringwald HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs 543aa4dd815SMatthias Ringwald HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 544aa4dd815SMatthias Ringwald hf_trigger_codec_connection_setup == received BCC 545aa4dd815SMatthias Ringwald ag_trigger_codec_connection_setup == received from AG to send BCS 546aa4dd815SMatthias Ringwald HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS 547aa4dd815SMatthias Ringwald */ 548aa4dd815SMatthias Ringwald 549a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 550aa4dd815SMatthias Ringwald case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 551a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC; 552aa4dd815SMatthias Ringwald break; 553aa4dd815SMatthias Ringwald case HFP_CODECS_AG_RESEND_COMMON_CODEC: 554a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC; 555aa4dd815SMatthias Ringwald break; 556aa4dd815SMatthias Ringwald default: 557aa4dd815SMatthias Ringwald break; 558aa4dd815SMatthias Ringwald } 559aa4dd815SMatthias Ringwald 560aa4dd815SMatthias Ringwald // printf(" -> State machine: CC\n"); 561aa4dd815SMatthias Ringwald 562a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 563aa4dd815SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 564a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){ 565a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_LIST; 566485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 567aa4dd815SMatthias Ringwald return 1; 568aa4dd815SMatthias Ringwald } 569aa4dd815SMatthias Ringwald 570a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 571aa4dd815SMatthias Ringwald case HFP_CODECS_AG_SENT_COMMON_CODEC: 572aa4dd815SMatthias Ringwald case HFP_CODECS_EXCHANGED: 573a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_AG_RESEND_COMMON_CODEC; 574aa4dd815SMatthias Ringwald break; 575aa4dd815SMatthias Ringwald default: 576aa4dd815SMatthias Ringwald break; 577aa4dd815SMatthias Ringwald } 578485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 579aa4dd815SMatthias Ringwald return 1; 580aa4dd815SMatthias Ringwald 581aa4dd815SMatthias Ringwald case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 582a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 583485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 584aa4dd815SMatthias Ringwald return 1; 585aa4dd815SMatthias Ringwald 586aa4dd815SMatthias Ringwald case HFP_CMD_AG_SEND_COMMON_CODEC: 587a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_AG_SENT_COMMON_CODEC; 588a0ffb263SMatthias Ringwald hfp_connection->suggested_codec = hfp_ag_suggest_codec(hfp_connection); 589485ac19eSMilanka Ringwald hfp_ag_send_suggest_codec_cmd(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec); 590aa4dd815SMatthias Ringwald return 1; 591aa4dd815SMatthias Ringwald 592aa4dd815SMatthias Ringwald case HFP_CMD_HF_CONFIRMED_CODEC: 593a0ffb263SMatthias Ringwald if (hfp_connection->codec_confirmed != hfp_connection->suggested_codec){ 594a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_ERROR; 595485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 596aa4dd815SMatthias Ringwald return 1; 597aa4dd815SMatthias Ringwald } 598a0ffb263SMatthias Ringwald hfp_connection->negotiated_codec = hfp_connection->codec_confirmed; 599a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 60003645b44SMatthias Ringwald log_info("hfp: codec confirmed: %s", hfp_connection->negotiated_codec == HFP_CODEC_MSBC ? "mSBC" : "CVSD"); 601485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 6027522e673SMatthias Ringwald // now, pick link settings 6037522e673SMatthias Ringwald hfp_init_link_settings(hfp_connection); 604aa4dd815SMatthias Ringwald return 1; 605aa4dd815SMatthias Ringwald default: 606aa4dd815SMatthias Ringwald break; 607aa4dd815SMatthias Ringwald } 608aa4dd815SMatthias Ringwald return 0; 609aa4dd815SMatthias Ringwald } 610aa4dd815SMatthias Ringwald 611a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){ 612a0ffb263SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 613ca59be51SMatthias Ringwald hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr); 614aa4dd815SMatthias Ringwald 615a0ffb263SMatthias Ringwald // if active call exist, set per-hfp_connection state active, too (when audio is on) 616d0c20769SMatthias Ringwald if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 617a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE; 618aa4dd815SMatthias Ringwald } 619ce263fc8SMatthias Ringwald // if AG is ringing, also start ringing on the HF 620d0c20769SMatthias Ringwald if (hfp_gsm_call_status() == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS && 621d0c20769SMatthias Ringwald hfp_gsm_callsetup_status() == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 622a0ffb263SMatthias Ringwald hfp_ag_hf_start_ringing(hfp_connection); 623ce263fc8SMatthias Ringwald } 624aa4dd815SMatthias Ringwald } 6253deb3ec6SMatthias Ringwald 626a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){ 627c79b4cabSMatthias Ringwald // log_info("hfp_ag_run_for_context_service_level_connection state %u, command %u", hfp_connection->state, hfp_connection->command); 628a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 629c04cf7ffSMilanka Ringwald int sent = 0; 630a0ffb263SMatthias Ringwald switch(hfp_connection->command){ 6313deb3ec6SMatthias Ringwald case HFP_CMD_SUPPORTED_FEATURES: 632a0ffb263SMatthias Ringwald switch(hfp_connection->state){ 6333deb3ec6SMatthias Ringwald case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 634aa4dd815SMatthias Ringwald case HFP_EXCHANGE_SUPPORTED_FEATURES: 635d715cf51SMatthias Ringwald hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr); 636a0ffb263SMatthias Ringwald if (has_codec_negotiation_feature(hfp_connection)){ 637a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS; 638aa4dd815SMatthias Ringwald } else { 639a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 640aa4dd815SMatthias Ringwald } 641a0ffb263SMatthias Ringwald hfp_ag_exchange_supported_features_cmd(hfp_connection->rfcomm_cid); 642aa4dd815SMatthias Ringwald return 1; 6433deb3ec6SMatthias Ringwald default: 6443deb3ec6SMatthias Ringwald break; 6453deb3ec6SMatthias Ringwald } 6463deb3ec6SMatthias Ringwald break; 6473deb3ec6SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 648c04cf7ffSMilanka Ringwald sent = codecs_exchange_state_machine(hfp_connection); 649a0ffb263SMatthias Ringwald if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){ 650a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 6513deb3ec6SMatthias Ringwald } 652c04cf7ffSMilanka Ringwald return sent; 653aa4dd815SMatthias Ringwald 654aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 655a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_W4_RETRIEVE_INDICATORS) { 656473ac565SMatthias Ringwald // HF requested AG Indicators and we did expect it 657473ac565SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 658473ac565SMatthias Ringwald // continue below in state switch 659ad902e3dSMatthias Ringwald } 660ad902e3dSMatthias Ringwald break; 661aa4dd815SMatthias Ringwald 662aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 663a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break; 664a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE; 665485ac19eSMilanka Ringwald hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid); 666aa4dd815SMatthias Ringwald return 1; 667aa4dd815SMatthias Ringwald 6683deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 669a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break; 670a0ffb263SMatthias Ringwald if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 671a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL; 672a0ffb263SMatthias Ringwald } else if (has_hf_indicators_feature(hfp_connection)){ 673a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 674aa4dd815SMatthias Ringwald } else { 675a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 6763deb3ec6SMatthias Ringwald } 6778a46ec40SMatthias Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 678aa4dd815SMatthias Ringwald return 1; 6793deb3ec6SMatthias Ringwald 680aa4dd815SMatthias Ringwald case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES: 681a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break; 682a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 683a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 684aa4dd815SMatthias Ringwald } 685485ac19eSMilanka Ringwald hfp_ag_send_retrieve_can_hold_call_cmd(hfp_connection->rfcomm_cid); 686a0ffb263SMatthias Ringwald if (!has_hf_indicators_feature(hfp_connection)){ 687a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 688ce263fc8SMatthias Ringwald } 689aa4dd815SMatthias Ringwald return 1; 690aa4dd815SMatthias Ringwald 691aa4dd815SMatthias Ringwald case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 692a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break; 693a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS; 694485ac19eSMilanka Ringwald hfp_ag_send_list_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 695aa4dd815SMatthias Ringwald return 1; 696aa4dd815SMatthias Ringwald 697aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 698a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break; 699a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 700485ac19eSMilanka Ringwald hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 701aa4dd815SMatthias Ringwald return 1; 702aa4dd815SMatthias Ringwald 703aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 704a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break; 705a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 706485ac19eSMilanka Ringwald hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 707aa4dd815SMatthias Ringwald return 1; 7083deb3ec6SMatthias Ringwald default: 7093deb3ec6SMatthias Ringwald break; 7103deb3ec6SMatthias Ringwald } 711473ac565SMatthias Ringwald 712473ac565SMatthias Ringwald switch (hfp_connection->state){ 713473ac565SMatthias Ringwald case HFP_RETRIEVE_INDICATORS: { 714485ac19eSMilanka Ringwald int next_segment = hfp_ag_send_retrieve_indicators_cmd_via_generator(hfp_connection->rfcomm_cid, hfp_connection, hfp_connection->send_ag_indicators_segment); 715473ac565SMatthias Ringwald int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection); 716473ac565SMatthias Ringwald log_info("HFP_CMD_RETRIEVE_AG_INDICATORS next segment %u, num_segments %u", next_segment, num_segments); 717473ac565SMatthias Ringwald if (next_segment < num_segments){ 718473ac565SMatthias Ringwald // prepare sending of next segment 719473ac565SMatthias Ringwald hfp_connection->send_ag_indicators_segment = next_segment; 720473ac565SMatthias Ringwald log_info("HFP_CMD_RETRIEVE_AG_INDICATORS more. command %u, next seg %u", hfp_connection->command, next_segment); 721473ac565SMatthias Ringwald } else { 722473ac565SMatthias Ringwald // done, go to next state 723473ac565SMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0; 724473ac565SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS; 725473ac565SMatthias Ringwald } 726473ac565SMatthias Ringwald return 1; 727473ac565SMatthias Ringwald } 728473ac565SMatthias Ringwald default: 729473ac565SMatthias Ringwald break; 730473ac565SMatthias Ringwald } 731c04cf7ffSMilanka Ringwald return 0; 7323deb3ec6SMatthias Ringwald } 7333deb3ec6SMatthias Ringwald 734a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){ 735c04cf7ffSMilanka Ringwald int sent = codecs_exchange_state_machine(hfp_connection); 736c04cf7ffSMilanka Ringwald if (sent) return 1; 737aa4dd815SMatthias Ringwald 738a0ffb263SMatthias Ringwald switch(hfp_connection->command){ 739aa4dd815SMatthias Ringwald case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION: 740a0ffb263SMatthias Ringwald hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition); 741485ac19eSMilanka Ringwald hfp_ag_send_activate_voice_recognition_cmd(hfp_connection->rfcomm_cid, hfp_connection->ag_activate_voice_recognition); 742aa4dd815SMatthias Ringwald return 1; 743aa4dd815SMatthias Ringwald case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION: 744aa4dd815SMatthias Ringwald if (get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)){ 745a0ffb263SMatthias Ringwald hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition); 746485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 747a0ffb263SMatthias Ringwald hfp_ag_setup_audio_connection(hfp_connection); 748aa4dd815SMatthias Ringwald } else { 749485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 750aa4dd815SMatthias Ringwald } 751aa4dd815SMatthias Ringwald return 1; 752aa4dd815SMatthias Ringwald case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING: 753485ac19eSMilanka Ringwald hfp_ag_send_change_in_band_ring_tone_setting_cmd(hfp_connection->rfcomm_cid); 754aa4dd815SMatthias Ringwald return 1; 755aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 756485ac19eSMilanka Ringwald hfp_ag_send_report_network_operator_name_cmd(hfp_connection->rfcomm_cid, hfp_connection->network_operator); 757aa4dd815SMatthias Ringwald return 1; 758aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 759a0ffb263SMatthias Ringwald if (hfp_connection->network_operator.format != 0){ 760485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 761aa4dd815SMatthias Ringwald } else { 762485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 7633deb3ec6SMatthias Ringwald } 764aa4dd815SMatthias Ringwald return 1; 765aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE: 766485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 767aa4dd815SMatthias Ringwald return 1; 7683deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR: 769a0ffb263SMatthias Ringwald if (hfp_connection->extended_audio_gateway_error){ 770a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 771485ac19eSMilanka Ringwald hfp_ag_send_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, hfp_connection->extended_audio_gateway_error_value); 772aa4dd815SMatthias Ringwald return 1; 7733deb3ec6SMatthias Ringwald } 774202c8a4cSMatthias Ringwald break; 7753deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 776485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 777ce263fc8SMatthias Ringwald return 1; 7783deb3ec6SMatthias Ringwald default: 7793deb3ec6SMatthias Ringwald break; 7803deb3ec6SMatthias Ringwald } 781aa4dd815SMatthias Ringwald return 0; 7823deb3ec6SMatthias Ringwald } 7833deb3ec6SMatthias Ringwald 784a0ffb263SMatthias Ringwald static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){ 785a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || 786a0ffb263SMatthias Ringwald hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0; 7873deb3ec6SMatthias Ringwald 788aa4dd815SMatthias Ringwald 789a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){ 790a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 791a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 0; 792a0ffb263SMatthias Ringwald gap_disconnect(hfp_connection->sco_handle); 793aa4dd815SMatthias Ringwald return 1; 794aa4dd815SMatthias Ringwald } 795aa4dd815SMatthias Ringwald 796a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 797aa4dd815SMatthias Ringwald 798aa4dd815SMatthias Ringwald // run codecs exchange 799c04cf7ffSMilanka Ringwald int sent = codecs_exchange_state_machine(hfp_connection); 800c04cf7ffSMilanka Ringwald if (sent) return 1; 801aa4dd815SMatthias Ringwald 802c04cf7ffSMilanka Ringwald if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0; 803a0ffb263SMatthias Ringwald if (hfp_connection->establish_audio_connection){ 804a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 805a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 806eddcd308SMatthias Ringwald hfp_setup_synchronous_connection(hfp_connection); 807aa4dd815SMatthias Ringwald return 1; 808aa4dd815SMatthias Ringwald } 809aa4dd815SMatthias Ringwald return 0; 810aa4dd815SMatthias Ringwald } 811aa4dd815SMatthias Ringwald 812ec820d77SMatthias Ringwald static hfp_connection_t * hfp_ag_context_for_timer(btstack_timer_source_t * ts){ 813665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 814665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 815aa4dd815SMatthias Ringwald 816665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 817a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 818a0ffb263SMatthias Ringwald if ( & hfp_connection->hfp_timeout == ts) { 819a0ffb263SMatthias Ringwald return hfp_connection; 820aa4dd815SMatthias Ringwald } 821aa4dd815SMatthias Ringwald } 822aa4dd815SMatthias Ringwald return NULL; 823aa4dd815SMatthias Ringwald } 824aa4dd815SMatthias Ringwald 825ec820d77SMatthias Ringwald static void hfp_timeout_handler(btstack_timer_source_t * timer){ 826d63c37a1SMatthias Ringwald hfp_connection_t * hfp_connection = hfp_ag_context_for_timer(timer); 827d63c37a1SMatthias Ringwald if (!hfp_connection) return; 828aa4dd815SMatthias Ringwald 829d63c37a1SMatthias Ringwald log_info("HFP start ring timeout, con handle 0x%02x", hfp_connection->acl_handle); 830a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 1; 831a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled; 832aa4dd815SMatthias Ringwald 833a0ffb263SMatthias Ringwald btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout 834a0ffb263SMatthias Ringwald btstack_run_loop_add_timer(& hfp_connection->hfp_timeout); 835aa4dd815SMatthias Ringwald 836a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 837aa4dd815SMatthias Ringwald } 838aa4dd815SMatthias Ringwald 839a0ffb263SMatthias Ringwald static void hfp_timeout_start(hfp_connection_t * hfp_connection){ 840a0ffb263SMatthias Ringwald btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout); 841a0ffb263SMatthias Ringwald btstack_run_loop_set_timer_handler(& hfp_connection->hfp_timeout, hfp_timeout_handler); 842a0ffb263SMatthias Ringwald btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout 843a0ffb263SMatthias Ringwald btstack_run_loop_add_timer(& hfp_connection->hfp_timeout); 844aa4dd815SMatthias Ringwald } 845aa4dd815SMatthias Ringwald 846a0ffb263SMatthias Ringwald static void hfp_timeout_stop(hfp_connection_t * hfp_connection){ 847a0ffb263SMatthias Ringwald log_info("HFP stop ring timeout, con handle 0x%02x", hfp_connection->acl_handle); 848a0ffb263SMatthias Ringwald btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout); 849aa4dd815SMatthias Ringwald } 850aa4dd815SMatthias Ringwald 851aa4dd815SMatthias Ringwald // 852aa4dd815SMatthias Ringwald // transitition implementations for hfp_ag_call_state_machine 853aa4dd815SMatthias Ringwald // 854aa4dd815SMatthias Ringwald 855a0ffb263SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection){ 856aa4dd815SMatthias Ringwald if (use_in_band_tone()){ 857a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING; 858d97d752dSMilanka Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 859aa4dd815SMatthias Ringwald } else { 860a0ffb263SMatthias Ringwald hfp_timeout_start(hfp_connection); 861a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 1; 862a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled; 863a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_RINGING; 864ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGINIG); 865aa4dd815SMatthias Ringwald } 866aa4dd815SMatthias Ringwald } 867aa4dd815SMatthias Ringwald 868a0ffb263SMatthias Ringwald static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){ 869a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 0; 870a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = 0; 871a0ffb263SMatthias Ringwald hfp_timeout_stop(hfp_connection); 872ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGINIG); 873aa4dd815SMatthias Ringwald } 874aa4dd815SMatthias Ringwald 875aa4dd815SMatthias Ringwald static void hfp_ag_trigger_incoming_call(void){ 876aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name("callsetup"); 877aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 878aa4dd815SMatthias Ringwald 879665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 880665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 881665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 882a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 883a0ffb263SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 884a0ffb263SMatthias Ringwald if (hfp_connection->call_state == HFP_CALL_IDLE){ 885a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 886d63c37a1SMatthias Ringwald hfp_ag_hf_start_ringing(hfp_connection); 887aa4dd815SMatthias Ringwald } 888a0ffb263SMatthias Ringwald if (hfp_connection->call_state == HFP_CALL_ACTIVE){ 889a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING; 890aa4dd815SMatthias Ringwald } 891a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 892aa4dd815SMatthias Ringwald } 893aa4dd815SMatthias Ringwald } 894aa4dd815SMatthias Ringwald 895aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callsetup_state(void){ 896aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name("callsetup"); 897aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 898aa4dd815SMatthias Ringwald 899665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 900665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 901665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 902a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 903a0ffb263SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 904a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 905a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 906aa4dd815SMatthias Ringwald } 907aa4dd815SMatthias Ringwald } 908aa4dd815SMatthias Ringwald 909aa4dd815SMatthias Ringwald static void hfp_ag_transfer_call_state(void){ 910aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name("call"); 911aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 912aa4dd815SMatthias Ringwald 913665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 914665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 915665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 916a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 917a0ffb263SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 918a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 919a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 920aa4dd815SMatthias Ringwald } 921aa4dd815SMatthias Ringwald } 922aa4dd815SMatthias Ringwald 923aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callheld_state(void){ 924aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name("callheld"); 925aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 926aa4dd815SMatthias Ringwald 927665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 928665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 929665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 930a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 931a0ffb263SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 932a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 933a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 934aa4dd815SMatthias Ringwald } 935aa4dd815SMatthias Ringwald } 936aa4dd815SMatthias Ringwald 937aa4dd815SMatthias Ringwald static void hfp_ag_hf_accept_call(hfp_connection_t * source){ 938aa4dd815SMatthias Ringwald 939aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 940aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 941aa4dd815SMatthias Ringwald 942665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 943665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 944665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 945a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 946a0ffb263SMatthias Ringwald if (hfp_connection->call_state != HFP_CALL_RINGING && 947a0ffb263SMatthias Ringwald hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue; 948aa4dd815SMatthias Ringwald 949a0ffb263SMatthias Ringwald hfp_ag_hf_stop_ringing(hfp_connection); 950a0ffb263SMatthias Ringwald if (hfp_connection == source){ 951a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 952aa4dd815SMatthias Ringwald 953aa4dd815SMatthias Ringwald if (use_in_band_tone()){ 954a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 955aa4dd815SMatthias Ringwald } else { 956a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE; 957d97d752dSMilanka Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 958aa4dd815SMatthias Ringwald } 959aa4dd815SMatthias Ringwald 960a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 961a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 962aa4dd815SMatthias Ringwald 963aa4dd815SMatthias Ringwald } else { 964a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 965aa4dd815SMatthias Ringwald } 966a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 967aa4dd815SMatthias Ringwald } 968aa4dd815SMatthias Ringwald } 969aa4dd815SMatthias Ringwald 970aa4dd815SMatthias Ringwald static void hfp_ag_ag_accept_call(void){ 971aa4dd815SMatthias Ringwald 972aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 973aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 974aa4dd815SMatthias Ringwald 975665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 976665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 977665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 978a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 979a0ffb263SMatthias Ringwald if (hfp_connection->call_state != HFP_CALL_RINGING) continue; 980aa4dd815SMatthias Ringwald 981a0ffb263SMatthias Ringwald hfp_ag_hf_stop_ringing(hfp_connection); 982a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION; 983aa4dd815SMatthias Ringwald 984a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 985a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 986aa4dd815SMatthias Ringwald 987a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 988aa4dd815SMatthias Ringwald break; // only single 989aa4dd815SMatthias Ringwald } 990aa4dd815SMatthias Ringwald } 991aa4dd815SMatthias Ringwald 992aa4dd815SMatthias Ringwald static void hfp_ag_trigger_reject_call(void){ 993aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 994665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 995665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 996665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 997665d90f2SMatthias Ringwald hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 998aa4dd815SMatthias Ringwald if (connection->call_state != HFP_CALL_RINGING && 999aa4dd815SMatthias Ringwald connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue; 1000aa4dd815SMatthias Ringwald hfp_ag_hf_stop_ringing(connection); 1001aa4dd815SMatthias Ringwald connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1002aa4dd815SMatthias Ringwald connection->call_state = HFP_CALL_IDLE; 1003aa4dd815SMatthias Ringwald hfp_run_for_context(connection); 1004aa4dd815SMatthias Ringwald } 1005aa4dd815SMatthias Ringwald } 1006aa4dd815SMatthias Ringwald 1007ca59be51SMatthias Ringwald static void hfp_ag_emit_simple_event(uint8_t event_subtype){ 1008ca59be51SMatthias Ringwald uint8_t event[3]; 1009ca59be51SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 1010ca59be51SMatthias Ringwald event[1] = sizeof(event) - 2; 1011ca59be51SMatthias Ringwald event[2] = event_subtype; 1012ca59be51SMatthias Ringwald if (!hfp_ag_callback) return; 1013ca59be51SMatthias Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 1014ca59be51SMatthias Ringwald } 1015ca59be51SMatthias Ringwald 1016aa4dd815SMatthias Ringwald static void hfp_ag_trigger_terminate_call(void){ 1017aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 1018aa4dd815SMatthias Ringwald 1019665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1020665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1021665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1022a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1023d63c37a1SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 1024a0ffb263SMatthias Ringwald if (hfp_connection->call_state == HFP_CALL_IDLE) continue; 1025a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 1026a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1027a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 1; 1028a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1029aa4dd815SMatthias Ringwald } 1030ca59be51SMatthias Ringwald hfp_ag_emit_simple_event(HFP_SUBEVENT_CALL_TERMINATED); 1031aa4dd815SMatthias Ringwald } 1032aa4dd815SMatthias Ringwald 10330cb5b971SMatthias Ringwald static void hfp_ag_set_callsetup_indicator(void){ 1034aa4dd815SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup"); 1035aa4dd815SMatthias Ringwald if (!indicator){ 1036d0c20769SMatthias Ringwald log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing"); 103745718b6fSMatthias Ringwald return; 1038aa4dd815SMatthias Ringwald }; 1039d0c20769SMatthias Ringwald indicator->status = hfp_gsm_callsetup_status(); 1040aa4dd815SMatthias Ringwald } 1041aa4dd815SMatthias Ringwald 10420cb5b971SMatthias Ringwald static void hfp_ag_set_callheld_indicator(void){ 1043d210d9c4SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld"); 1044d210d9c4SMatthias Ringwald if (!indicator){ 1045d210d9c4SMatthias Ringwald log_error("hfp_ag_set_callheld_state: callheld indicator is missing"); 104645718b6fSMatthias Ringwald return; 1047d210d9c4SMatthias Ringwald }; 1048d210d9c4SMatthias Ringwald indicator->status = hfp_gsm_callheld_status(); 1049d210d9c4SMatthias Ringwald } 1050d210d9c4SMatthias Ringwald 10510cb5b971SMatthias Ringwald static void hfp_ag_set_call_indicator(void){ 1052aa4dd815SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call"); 1053aa4dd815SMatthias Ringwald if (!indicator){ 1054aa4dd815SMatthias Ringwald log_error("hfp_ag_set_call_state: call indicator is missing"); 105545718b6fSMatthias Ringwald return; 1056aa4dd815SMatthias Ringwald }; 1057d210d9c4SMatthias Ringwald indicator->status = hfp_gsm_call_status(); 1058aa4dd815SMatthias Ringwald } 1059aa4dd815SMatthias Ringwald 1060aa4dd815SMatthias Ringwald static void hfp_ag_stop_ringing(void){ 1061665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1062665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1063665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1064a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1065a0ffb263SMatthias Ringwald if (hfp_connection->call_state != HFP_CALL_RINGING && 1066a0ffb263SMatthias Ringwald hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue; 1067a0ffb263SMatthias Ringwald hfp_ag_hf_stop_ringing(hfp_connection); 1068aa4dd815SMatthias Ringwald } 1069aa4dd815SMatthias Ringwald } 1070aa4dd815SMatthias Ringwald 1071aa4dd815SMatthias Ringwald static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){ 1072665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1073665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1074665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1075a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1076a0ffb263SMatthias Ringwald if (hfp_connection->call_state == call_state) return hfp_connection; 1077aa4dd815SMatthias Ringwald } 1078aa4dd815SMatthias Ringwald return NULL; 1079aa4dd815SMatthias Ringwald } 1080aa4dd815SMatthias Ringwald 1081a5bdcda8SMatthias Ringwald static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){ 1082665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1083665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1084665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1085a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1086a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = state + 1; 1087ce263fc8SMatthias Ringwald } 1088ce263fc8SMatthias Ringwald } 1089ce263fc8SMatthias Ringwald 1090a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 1091aa4dd815SMatthias Ringwald int indicator_index; 1092a0ffb263SMatthias Ringwald switch (hfp_connection->call_state){ 1093aa4dd815SMatthias Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING: 1094a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 1095a0ffb263SMatthias Ringwald // we got event: audio hfp_connection established 1096a0ffb263SMatthias Ringwald hfp_timeout_start(hfp_connection); 1097a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 1; 1098a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled; 1099a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_RINGING; 1100a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_RINGING; 1101ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGINIG); 1102aa4dd815SMatthias Ringwald break; 1103aa4dd815SMatthias Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE: 1104a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 1105a0ffb263SMatthias Ringwald // we got event: audio hfp_connection established 1106a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1107aa4dd815SMatthias Ringwald break; 1108aa4dd815SMatthias Ringwald case HFP_CALL_W2_SEND_CALL_WAITING: 1109a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_CHLD; 1110a0ffb263SMatthias Ringwald hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid); 1111aa4dd815SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callsetup"); 1112a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1113aa4dd815SMatthias Ringwald break; 1114aa4dd815SMatthias Ringwald default: 1115aa4dd815SMatthias Ringwald break; 1116aa4dd815SMatthias Ringwald } 1117aa4dd815SMatthias Ringwald return 0; 1118aa4dd815SMatthias Ringwald } 1119a0ffb263SMatthias Ringwald // hfp_connection is used to identify originating HF 1120a0ffb263SMatthias Ringwald static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){ 1121aa4dd815SMatthias Ringwald int indicator_index; 1122d210d9c4SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1123d210d9c4SMatthias Ringwald int callheld_indicator_index = get_ag_indicator_index_for_name("callheld"); 1124d210d9c4SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call"); 112574386ee0SMatthias Ringwald 1126d210d9c4SMatthias Ringwald //printf("hfp_ag_call_sm event %d \n", event); 1127aa4dd815SMatthias Ringwald switch (event){ 1128aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL: 1129d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1130aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1131d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1132aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1133d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_INCOMING_CALL); 1134d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1135aa4dd815SMatthias Ringwald hfp_ag_trigger_incoming_call(); 113660ebb071SMilanka Ringwald log_info("AG rings"); 1137aa4dd815SMatthias Ringwald break; 1138aa4dd815SMatthias Ringwald default: 11393deb3ec6SMatthias Ringwald break; 11403deb3ec6SMatthias Ringwald } 11413deb3ec6SMatthias Ringwald break; 1142aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1143d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1144aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1145d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_INCOMING_CALL); 1146d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1147aa4dd815SMatthias Ringwald hfp_ag_trigger_incoming_call(); 114860ebb071SMilanka Ringwald log_info("AG call waiting"); 1149aa4dd815SMatthias Ringwald break; 1150aa4dd815SMatthias Ringwald default: 11513deb3ec6SMatthias Ringwald break; 11523deb3ec6SMatthias Ringwald } 11533deb3ec6SMatthias Ringwald break; 1154aa4dd815SMatthias Ringwald } 1155aa4dd815SMatthias Ringwald break; 1156aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG: 1157d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1158aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1159d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1160aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1161d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG); 1162d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1163d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1164aa4dd815SMatthias Ringwald hfp_ag_ag_accept_call(); 116560ebb071SMilanka Ringwald log_info("AG answers call, accept call by GSM"); 1166aa4dd815SMatthias Ringwald break; 1167aa4dd815SMatthias Ringwald default: 11683deb3ec6SMatthias Ringwald break; 11693deb3ec6SMatthias Ringwald } 1170aa4dd815SMatthias Ringwald break; 1171aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1172d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1173aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 117460ebb071SMilanka Ringwald log_info("AG: current call is placed on hold, incoming call gets active"); 1175d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG); 1176d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1177d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1178ce263fc8SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1179ce263fc8SMatthias Ringwald hfp_ag_transfer_callheld_state(); 1180aa4dd815SMatthias Ringwald break; 1181aa4dd815SMatthias Ringwald default: 1182aa4dd815SMatthias Ringwald break; 1183aa4dd815SMatthias Ringwald } 1184aa4dd815SMatthias Ringwald break; 1185aa4dd815SMatthias Ringwald } 1186aa4dd815SMatthias Ringwald break; 1187ce263fc8SMatthias Ringwald 1188ce263fc8SMatthias Ringwald case HFP_AG_HELD_CALL_JOINED_BY_AG: 1189d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1190ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1191d0c20769SMatthias Ringwald switch (hfp_gsm_callheld_status()){ 1192ce263fc8SMatthias Ringwald case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED: 119360ebb071SMilanka Ringwald log_info("AG: joining held call with active call"); 1194d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_HELD_CALL_JOINED_BY_AG); 1195d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1196ce263fc8SMatthias Ringwald hfp_ag_transfer_callheld_state(); 1197ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL); 1198ce263fc8SMatthias Ringwald break; 1199ce263fc8SMatthias Ringwald default: 1200ce263fc8SMatthias Ringwald break; 1201ce263fc8SMatthias Ringwald } 1202ce263fc8SMatthias Ringwald break; 1203ce263fc8SMatthias Ringwald default: 1204ce263fc8SMatthias Ringwald break; 1205ce263fc8SMatthias Ringwald } 1206ce263fc8SMatthias Ringwald break; 1207ce263fc8SMatthias Ringwald 1208aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF: 1209d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1210aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1211d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1212aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1213d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF); 1214d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1215d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1216a0ffb263SMatthias Ringwald hfp_ag_hf_accept_call(hfp_connection); 121760ebb071SMilanka Ringwald log_info("HF answers call, accept call by GSM"); 1218ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED); 1219aa4dd815SMatthias Ringwald break; 1220aa4dd815SMatthias Ringwald default: 1221aa4dd815SMatthias Ringwald break; 1222aa4dd815SMatthias Ringwald } 12233deb3ec6SMatthias Ringwald break; 12243deb3ec6SMatthias Ringwald default: 12253deb3ec6SMatthias Ringwald break; 12263deb3ec6SMatthias Ringwald } 12273deb3ec6SMatthias Ringwald break; 12283deb3ec6SMatthias Ringwald 1229ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG: 1230d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1231ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1232d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1233ce263fc8SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1234d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG); 1235ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_active = 1; 1236ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1237a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1238ce263fc8SMatthias Ringwald // as with regualr call 1239d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1240d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1241ce263fc8SMatthias Ringwald hfp_ag_ag_accept_call(); 124260ebb071SMilanka Ringwald log_info("AG response and hold - hold by AG"); 1243ce263fc8SMatthias Ringwald break; 1244ce263fc8SMatthias Ringwald default: 1245ce263fc8SMatthias Ringwald break; 1246ce263fc8SMatthias Ringwald } 1247ce263fc8SMatthias Ringwald break; 1248ce263fc8SMatthias Ringwald default: 1249ce263fc8SMatthias Ringwald break; 1250ce263fc8SMatthias Ringwald } 1251ce263fc8SMatthias Ringwald break; 1252ce263fc8SMatthias Ringwald 1253ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF: 1254d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1255ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1256d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1257ce263fc8SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1258d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF); 1259ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_active = 1; 1260ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1261a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1262ce263fc8SMatthias Ringwald // as with regualr call 1263d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1264d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1265a0ffb263SMatthias Ringwald hfp_ag_hf_accept_call(hfp_connection); 126660ebb071SMilanka Ringwald log_info("AG response and hold - hold by HF"); 1267ce263fc8SMatthias Ringwald break; 1268ce263fc8SMatthias Ringwald default: 1269ce263fc8SMatthias Ringwald break; 1270ce263fc8SMatthias Ringwald } 1271ce263fc8SMatthias Ringwald break; 1272ce263fc8SMatthias Ringwald default: 1273ce263fc8SMatthias Ringwald break; 1274ce263fc8SMatthias Ringwald } 1275ce263fc8SMatthias Ringwald break; 1276ce263fc8SMatthias Ringwald 1277ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG: 1278ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF: 1279ce263fc8SMatthias Ringwald if (!hfp_ag_response_and_hold_active) break; 1280ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1281d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG); 1282a5bdcda8SMatthias Ringwald hfp_ag_response_and_hold_active = 0; 1283ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED; 1284a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 128560ebb071SMilanka Ringwald log_info("Held Call accepted and active"); 1286ce263fc8SMatthias Ringwald break; 1287ce263fc8SMatthias Ringwald 1288ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG: 1289ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF: 1290ce263fc8SMatthias Ringwald if (!hfp_ag_response_and_hold_active) break; 1291ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1292d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG); 1293a5bdcda8SMatthias Ringwald hfp_ag_response_and_hold_active = 0; 1294ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1295a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1296ce263fc8SMatthias Ringwald // from terminate by ag 1297d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1298ce263fc8SMatthias Ringwald hfp_ag_trigger_terminate_call(); 1299ce263fc8SMatthias Ringwald break; 1300ce263fc8SMatthias Ringwald 1301aa4dd815SMatthias Ringwald case HFP_AG_TERMINATE_CALL_BY_HF: 1302d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1303aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1304d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1305aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1306d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF); 1307d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1308aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1309aa4dd815SMatthias Ringwald hfp_ag_trigger_reject_call(); 131060ebb071SMilanka Ringwald log_info("HF Rejected Incoming call, AG terminate call"); 1311aa4dd815SMatthias Ringwald break; 1312aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 1313aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1314d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF); 1315d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1316aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 131760ebb071SMilanka Ringwald log_info("AG terminate outgoing call process"); 1318202c8a4cSMatthias Ringwald break; 1319aa4dd815SMatthias Ringwald default: 1320aa4dd815SMatthias Ringwald break; 1321aa4dd815SMatthias Ringwald } 1322aa4dd815SMatthias Ringwald break; 1323aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1324d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF); 1325d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1326aa4dd815SMatthias Ringwald hfp_ag_transfer_call_state(); 1327a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 132860ebb071SMilanka Ringwald log_info("AG terminate call"); 1329aa4dd815SMatthias Ringwald break; 1330aa4dd815SMatthias Ringwald } 1331aa4dd815SMatthias Ringwald break; 13323deb3ec6SMatthias Ringwald 1333aa4dd815SMatthias Ringwald case HFP_AG_TERMINATE_CALL_BY_AG: 1334d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1335aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1336d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1337aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1338d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG); 1339d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1340aa4dd815SMatthias Ringwald hfp_ag_trigger_reject_call(); 134160ebb071SMilanka Ringwald log_info("AG Rejected Incoming call, AG terminate call"); 1342aa4dd815SMatthias Ringwald break; 1343aa4dd815SMatthias Ringwald default: 1344aa4dd815SMatthias Ringwald break; 13453deb3ec6SMatthias Ringwald } 1346202c8a4cSMatthias Ringwald break; 1347aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1348d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG); 1349d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1350d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1351aa4dd815SMatthias Ringwald hfp_ag_trigger_terminate_call(); 135260ebb071SMilanka Ringwald log_info("AG terminate call"); 1353aa4dd815SMatthias Ringwald break; 1354aa4dd815SMatthias Ringwald default: 13553deb3ec6SMatthias Ringwald break; 13563deb3ec6SMatthias Ringwald } 13573deb3ec6SMatthias Ringwald break; 1358aa4dd815SMatthias Ringwald case HFP_AG_CALL_DROPPED: 1359d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){ 1360aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1361d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){ 1362aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1363aa4dd815SMatthias Ringwald hfp_ag_stop_ringing(); 136460ebb071SMilanka Ringwald log_info("Incoming call interrupted"); 1365aa4dd815SMatthias Ringwald break; 1366aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 1367aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 136860ebb071SMilanka Ringwald log_info("Outgoing call interrupted\n"); 136960ebb071SMilanka Ringwald log_info("AG notify call dropped\n"); 1370aa4dd815SMatthias Ringwald break; 1371aa4dd815SMatthias Ringwald default: 13723deb3ec6SMatthias Ringwald break; 13733deb3ec6SMatthias Ringwald } 1374d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_CALL_DROPPED); 1375d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1376aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1377aa4dd815SMatthias Ringwald break; 1378aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1379ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_active) { 1380d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_CALL_DROPPED); 1381ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1382a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1383ce263fc8SMatthias Ringwald } 1384d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_CALL_DROPPED); 1385d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1386d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1387aa4dd815SMatthias Ringwald hfp_ag_trigger_terminate_call(); 138860ebb071SMilanka Ringwald log_info("AG notify call dropped"); 1389aa4dd815SMatthias Ringwald break; 1390aa4dd815SMatthias Ringwald default: 1391aa4dd815SMatthias Ringwald break; 1392aa4dd815SMatthias Ringwald } 1393aa4dd815SMatthias Ringwald break; 1394aa4dd815SMatthias Ringwald 1395aa4dd815SMatthias Ringwald case HFP_AG_OUTGOING_CALL_INITIATED: 1396d210d9c4SMatthias Ringwald // directly reject call if number of free slots is exceeded 1397d210d9c4SMatthias Ringwald if (!hfp_gsm_call_possible()){ 1398a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1399a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1400d210d9c4SMatthias Ringwald break; 1401d210d9c4SMatthias Ringwald } 1402a0ffb263SMatthias Ringwald hfp_gsm_handle_event_with_call_number(HFP_AG_OUTGOING_CALL_INITIATED, (const char *) &hfp_connection->line_buffer[3]); 14039cae807eSMatthias Ringwald 1404a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 140574386ee0SMatthias Ringwald 1406ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]); 1407aa4dd815SMatthias Ringwald break; 1408aa4dd815SMatthias Ringwald 14099cae807eSMatthias Ringwald case HFP_AG_OUTGOING_REDIAL_INITIATED:{ 1410d210d9c4SMatthias Ringwald // directly reject call if number of free slots is exceeded 1411d210d9c4SMatthias Ringwald if (!hfp_gsm_call_possible()){ 1412a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1413a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1414d210d9c4SMatthias Ringwald break; 1415d210d9c4SMatthias Ringwald } 1416d210d9c4SMatthias Ringwald 141774386ee0SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_OUTGOING_REDIAL_INITIATED); 1418a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 141974386ee0SMatthias Ringwald 142060ebb071SMilanka Ringwald log_info("Redial last number"); 14219cae807eSMatthias Ringwald char * last_dialed_number = hfp_gsm_last_dialed_number(); 1422aa4dd815SMatthias Ringwald 14239cae807eSMatthias Ringwald if (strlen(last_dialed_number) > 0){ 142460ebb071SMilanka Ringwald log_info("Last number exists: accept call"); 1425ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number); 14269cae807eSMatthias Ringwald } else { 142760ebb071SMilanka Ringwald log_info("log_infoLast number missing: reject call"); 14289cae807eSMatthias Ringwald hfp_ag_outgoing_call_rejected(); 14299cae807eSMatthias Ringwald } 14309cae807eSMatthias Ringwald break; 14319cae807eSMatthias Ringwald } 1432aa4dd815SMatthias Ringwald case HFP_AG_OUTGOING_CALL_REJECTED: 1433a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 1434a0ffb263SMatthias Ringwald if (!hfp_connection){ 1435a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state"); 1436aa4dd815SMatthias Ringwald break; 1437aa4dd815SMatthias Ringwald } 1438d210d9c4SMatthias Ringwald 1439d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_REJECTED); 1440a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 1441a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1442a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1443aa4dd815SMatthias Ringwald break; 1444aa4dd815SMatthias Ringwald 1445d210d9c4SMatthias Ringwald case HFP_AG_OUTGOING_CALL_ACCEPTED:{ 1446a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 1447a0ffb263SMatthias Ringwald if (!hfp_connection){ 1448a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state"); 1449aa4dd815SMatthias Ringwald break; 1450aa4dd815SMatthias Ringwald } 1451aa4dd815SMatthias Ringwald 1452a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1453a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING; 1454aa4dd815SMatthias Ringwald 1455aa4dd815SMatthias Ringwald // trigger callsetup to be 1456d0c20769SMatthias Ringwald int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT; 1457d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ACCEPTED); 1458d210d9c4SMatthias Ringwald 1459d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1460aa4dd815SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callsetup"); 1461a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1462aa4dd815SMatthias Ringwald 1463aa4dd815SMatthias Ringwald // put current call on hold if active 1464d210d9c4SMatthias Ringwald if (put_call_on_hold){ 146560ebb071SMilanka Ringwald log_info("AG putting current call on hold for new outgoing calllog_info"); 1466d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1467aa4dd815SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callheld"); 1468485ac19eSMilanka Ringwald hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]); 1469aa4dd815SMatthias Ringwald } 1470aa4dd815SMatthias Ringwald 1471aa4dd815SMatthias Ringwald // start audio if needed 1472d97d752dSMilanka Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 1473aa4dd815SMatthias Ringwald break; 1474d210d9c4SMatthias Ringwald } 1475aa4dd815SMatthias Ringwald case HFP_AG_OUTGOING_CALL_RINGING: 1476a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1477a0ffb263SMatthias Ringwald if (!hfp_connection){ 1478a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state"); 1479aa4dd815SMatthias Ringwald break; 1480aa4dd815SMatthias Ringwald } 1481d210d9c4SMatthias Ringwald 1482d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_RINGING); 1483a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING; 1484d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1485aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1486aa4dd815SMatthias Ringwald break; 1487aa4dd815SMatthias Ringwald 1488d210d9c4SMatthias Ringwald case HFP_AG_OUTGOING_CALL_ESTABLISHED:{ 1489aa4dd815SMatthias Ringwald // get outgoing call 1490a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING); 1491a0ffb263SMatthias Ringwald if (!hfp_connection){ 1492a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1493aa4dd815SMatthias Ringwald } 1494a0ffb263SMatthias Ringwald if (!hfp_connection){ 1495a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection"); 1496aa4dd815SMatthias Ringwald break; 1497aa4dd815SMatthias Ringwald } 1498d210d9c4SMatthias Ringwald 1499d0c20769SMatthias 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; 1500d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ESTABLISHED); 1501a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1502d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1503d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1504aa4dd815SMatthias Ringwald hfp_ag_transfer_call_state(); 1505aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state(); 1506d210d9c4SMatthias Ringwald if (CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS){ 1507d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1508aa4dd815SMatthias Ringwald hfp_ag_transfer_callheld_state(); 15093deb3ec6SMatthias Ringwald } 15103deb3ec6SMatthias Ringwald break; 1511d210d9c4SMatthias Ringwald } 1512d210d9c4SMatthias Ringwald 1513d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_USER_BUSY: 1514d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_CALL_HOLD_USER_BUSY); 1515d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1516a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1517a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 151860ebb071SMilanka Ringwald log_info("AG: Call Waiting, User Busy"); 1519d210d9c4SMatthias Ringwald break; 1520d210d9c4SMatthias Ringwald 1521d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{ 1522d0c20769SMatthias Ringwald int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1523d0c20769SMatthias Ringwald int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD; 1524d210d9c4SMatthias Ringwald 1525d210d9c4SMatthias Ringwald // Releases all active calls (if any exist) and accepts the other (held or waiting) call. 1526d0c20769SMatthias Ringwald if (call_held || call_setup_in_progress){ 1527a0ffb263SMatthias Ringwald hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index); 1528d0c20769SMatthias Ringwald 1529d0c20769SMatthias Ringwald } 1530d0c20769SMatthias Ringwald 1531d210d9c4SMatthias Ringwald if (call_setup_in_progress){ 153260ebb071SMilanka Ringwald log_info("AG: Call Dropped, Accept new call"); 1533d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1534a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1535d210d9c4SMatthias Ringwald } else { 153660ebb071SMilanka Ringwald log_info("AG: Call Dropped, Resume held call"); 1537d210d9c4SMatthias Ringwald } 1538d210d9c4SMatthias Ringwald if (call_held){ 1539d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1540a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1541d210d9c4SMatthias Ringwald } 1542d0c20769SMatthias Ringwald 1543a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1544d210d9c4SMatthias Ringwald break; 1545d210d9c4SMatthias Ringwald } 1546d0c20769SMatthias Ringwald 1547d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{ 1548d210d9c4SMatthias Ringwald // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call. 1549d210d9c4SMatthias Ringwald // only update if callsetup changed 1550d0c20769SMatthias Ringwald int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1551a0ffb263SMatthias Ringwald hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index); 1552d0c20769SMatthias Ringwald 1553d210d9c4SMatthias Ringwald if (call_setup_in_progress){ 155460ebb071SMilanka Ringwald log_info("AG: Call on Hold, Accept new call"); 1555d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator(); 1556a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1557d210d9c4SMatthias Ringwald } else { 155860ebb071SMilanka Ringwald log_info("AG: Swap calls"); 1559d210d9c4SMatthias Ringwald } 1560d0c20769SMatthias Ringwald 1561d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1562d0c20769SMatthias Ringwald // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED); 1563a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1564a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1565d210d9c4SMatthias Ringwald break; 1566d210d9c4SMatthias Ringwald } 1567d0c20769SMatthias Ringwald 1568d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_ADD_HELD_CALL: 1569d210d9c4SMatthias Ringwald // Adds a held call to the conversation. 1570d0c20769SMatthias Ringwald if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){ 157160ebb071SMilanka Ringwald log_info("AG: Join 3-way-call"); 1572d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_CALL_HOLD_ADD_HELD_CALL); 1573d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1574a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1575ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL); 1576d210d9c4SMatthias Ringwald } 1577a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE; 1578d210d9c4SMatthias Ringwald break; 1579d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS: 1580d210d9c4SMatthias Ringwald // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer) 1581d210d9c4SMatthias Ringwald hfp_gsm_handle_event(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS); 158260ebb071SMilanka Ringwald log_info("AG: Transfer call -> Connect two calls and disconnect"); 1583d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator(); 1584d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator(); 1585a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1586a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1587a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 1588d210d9c4SMatthias Ringwald break; 1589ce263fc8SMatthias Ringwald 15903deb3ec6SMatthias Ringwald default: 15913deb3ec6SMatthias Ringwald break; 15923deb3ec6SMatthias Ringwald } 1593d210d9c4SMatthias Ringwald 1594d0c20769SMatthias Ringwald 15953deb3ec6SMatthias Ringwald } 15963deb3ec6SMatthias Ringwald 15979cae807eSMatthias Ringwald 1598a0ffb263SMatthias Ringwald static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){ 15999cae807eSMatthias Ringwald hfp_gsm_call_t * active_call = hfp_gsm_call(call_index); 16009cae807eSMatthias Ringwald if (!active_call) return; 16019cae807eSMatthias Ringwald 16029cae807eSMatthias Ringwald int idx = active_call->index; 16039cae807eSMatthias Ringwald hfp_enhanced_call_dir_t dir = active_call->direction; 16049cae807eSMatthias Ringwald hfp_enhanced_call_status_t status = active_call->enhanced_status; 16059cae807eSMatthias Ringwald hfp_enhanced_call_mode_t mode = active_call->mode; 16069cae807eSMatthias Ringwald hfp_enhanced_call_mpty_t mpty = active_call->mpty; 16079cae807eSMatthias Ringwald uint8_t type = active_call->clip_type; 16089cae807eSMatthias Ringwald char * number = active_call->clip_number; 16099cae807eSMatthias Ringwald 16109cae807eSMatthias Ringwald char buffer[100]; 16119cae807eSMatthias Ringwald // TODO: check length of a buffer, to fit the MTU 16129cae807eSMatthias Ringwald int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty); 16139cae807eSMatthias Ringwald if (number){ 16141cc1d9e9SMilanka Ringwald offset += snprintf(buffer+offset, sizeof(buffer)-offset-3, ", \"%s\",%u", number, type); 16159cae807eSMatthias Ringwald } 16169cae807eSMatthias Ringwald snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 161760ebb071SMilanka Ringwald log_info("hfp_ag_send_current_call_status 000 index %d, dir %d, status %d, mode %d, mpty %d, type %d, number %s", idx, dir, status, 16189cae807eSMatthias Ringwald mode, mpty, type, number); 1619a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 16209cae807eSMatthias Ringwald } 16219cae807eSMatthias Ringwald 1622a0ffb263SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t *hfp_connection){ 1623a0ffb263SMatthias Ringwald if (!hfp_connection) return; 1624724f400dSMatthias Ringwald if (!hfp_connection->rfcomm_cid) return; 1625473ac565SMatthias Ringwald 162622387625SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) { 162722387625SMatthias Ringwald log_info("HFP AG%p, wrong role %u", hfp_connection, hfp_connection->local_role); 162822387625SMatthias Ringwald return; 162922387625SMatthias Ringwald } 163022387625SMatthias Ringwald 1631e30a6a47SMatthias Ringwald if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) { 1632473ac565SMatthias Ringwald log_info("hfp_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid); 1633e30a6a47SMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 1634e30a6a47SMatthias Ringwald return; 1635e30a6a47SMatthias Ringwald } 1636a0ffb263SMatthias Ringwald if (hfp_connection->send_status_of_current_calls){ 1637a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 1638a0ffb263SMatthias Ringwald if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){ 1639a0ffb263SMatthias Ringwald hfp_connection->next_call_index++; 1640a0ffb263SMatthias Ringwald hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index); 16419cae807eSMatthias Ringwald } else { 1642a0ffb263SMatthias Ringwald hfp_connection->next_call_index = 0; 1643a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1644a0ffb263SMatthias Ringwald hfp_connection->send_status_of_current_calls = 0; 16459cae807eSMatthias Ringwald } 1646ce263fc8SMatthias Ringwald return; 1647ce263fc8SMatthias Ringwald } 1648ce263fc8SMatthias Ringwald 1649a0ffb263SMatthias Ringwald if (hfp_connection->ag_notify_incoming_call_waiting){ 1650a0ffb263SMatthias Ringwald hfp_connection->ag_notify_incoming_call_waiting = 0; 1651a0ffb263SMatthias Ringwald hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid); 1652aa4dd815SMatthias Ringwald return; 1653aa4dd815SMatthias Ringwald } 1654aa4dd815SMatthias Ringwald 1655a0ffb263SMatthias Ringwald if (hfp_connection->command == HFP_CMD_UNKNOWN){ 1656a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 1657a0ffb263SMatthias Ringwald hfp_connection->send_error = 0; 1658a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1659485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 1660a0ffb263SMatthias Ringwald return; 1661a0ffb263SMatthias Ringwald } 1662a0ffb263SMatthias Ringwald 1663a0ffb263SMatthias Ringwald if (hfp_connection->send_error){ 1664a0ffb263SMatthias Ringwald hfp_connection->send_error = 0; 1665a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1666485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid); 1667aa4dd815SMatthias Ringwald return; 1668aa4dd815SMatthias Ringwald } 1669aa4dd815SMatthias Ringwald 1670ce263fc8SMatthias Ringwald // note: before update AG indicators and ok_pending 1671a0ffb263SMatthias Ringwald if (hfp_connection->send_response_and_hold_status){ 1672a0ffb263SMatthias Ringwald int status = hfp_connection->send_response_and_hold_status - 1; 1673a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = 0; 1674485ac19eSMilanka Ringwald hfp_ag_send_set_response_and_hold(hfp_connection->rfcomm_cid, status); 1675ce263fc8SMatthias Ringwald return; 1676ce263fc8SMatthias Ringwald } 1677ce263fc8SMatthias Ringwald 1678a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending){ 1679a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 1680a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1681485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1682ce263fc8SMatthias Ringwald return; 1683ce263fc8SMatthias Ringwald } 1684ce263fc8SMatthias Ringwald 1685aa4dd815SMatthias Ringwald // update AG indicators 1686a0ffb263SMatthias Ringwald if (hfp_connection->ag_indicators_status_update_bitmap){ 1687aa4dd815SMatthias Ringwald int i; 1688a0ffb263SMatthias Ringwald for (i=0;i<hfp_connection->ag_indicators_nr;i++){ 1689a0ffb263SMatthias Ringwald if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){ 1690a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0); 1691a0ffb263SMatthias Ringwald if (!hfp_connection->enable_status_update_for_ag_indicators) { 1692ce263fc8SMatthias Ringwald log_info("+CMER:3,0,0,0 - not sending update for '%s'", hfp_ag_indicators[i].name); 1693ce263fc8SMatthias Ringwald break; 1694ce263fc8SMatthias Ringwald } 1695485ac19eSMilanka Ringwald hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]); 1696aa4dd815SMatthias Ringwald return; 1697aa4dd815SMatthias Ringwald } 1698aa4dd815SMatthias Ringwald } 1699aa4dd815SMatthias Ringwald } 1700aa4dd815SMatthias Ringwald 1701a0ffb263SMatthias Ringwald if (hfp_connection->ag_ring){ 1702a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 0; 1703a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1704485ac19eSMilanka Ringwald hfp_ag_send_ring(hfp_connection->rfcomm_cid); 1705aa4dd815SMatthias Ringwald return; 1706aa4dd815SMatthias Ringwald } 1707aa4dd815SMatthias Ringwald 1708a0ffb263SMatthias Ringwald if (hfp_connection->ag_send_clip){ 1709a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = 0; 1710a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1711a0ffb263SMatthias Ringwald hfp_ag_send_clip(hfp_connection->rfcomm_cid); 1712aa4dd815SMatthias Ringwald return; 1713aa4dd815SMatthias Ringwald } 1714aa4dd815SMatthias Ringwald 1715a0ffb263SMatthias Ringwald if (hfp_connection->send_phone_number_for_voice_tag){ 1716a0ffb263SMatthias Ringwald hfp_connection->send_phone_number_for_voice_tag = 0; 1717a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1718a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1719a0ffb263SMatthias Ringwald hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid); 1720aa4dd815SMatthias Ringwald return; 1721aa4dd815SMatthias Ringwald } 1722aa4dd815SMatthias Ringwald 1723a0ffb263SMatthias Ringwald if (hfp_connection->send_subscriber_number){ 1724a0ffb263SMatthias Ringwald if (hfp_connection->next_subscriber_number_to_send < subscriber_numbers_count){ 1725a0ffb263SMatthias Ringwald hfp_phone_number_t phone = subscriber_numbers[hfp_connection->next_subscriber_number_to_send++]; 1726a0ffb263SMatthias Ringwald hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number); 1727ce263fc8SMatthias Ringwald } else { 1728a0ffb263SMatthias Ringwald hfp_connection->send_subscriber_number = 0; 1729a0ffb263SMatthias Ringwald hfp_connection->next_subscriber_number_to_send = 0; 1730485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1731ce263fc8SMatthias Ringwald } 1732a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1733485ac19eSMilanka Ringwald 1734ce263fc8SMatthias Ringwald } 1735ce263fc8SMatthias Ringwald 1736a0ffb263SMatthias Ringwald if (hfp_connection->send_microphone_gain){ 1737a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 0; 1738a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1739485ac19eSMilanka Ringwald hfp_ag_send_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 1740aa4dd815SMatthias Ringwald return; 1741aa4dd815SMatthias Ringwald } 1742aa4dd815SMatthias Ringwald 1743a0ffb263SMatthias Ringwald if (hfp_connection->send_speaker_gain){ 1744a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 0; 1745a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1746485ac19eSMilanka Ringwald hfp_ag_send_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 17473deb3ec6SMatthias Ringwald return; 17483deb3ec6SMatthias Ringwald } 17493deb3ec6SMatthias Ringwald 1750a0ffb263SMatthias Ringwald if (hfp_connection->send_ag_status_indicators){ 1751a0ffb263SMatthias Ringwald hfp_connection->send_ag_status_indicators = 0; 1752485ac19eSMilanka Ringwald hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid); 1753ce263fc8SMatthias Ringwald return; 1754ce263fc8SMatthias Ringwald } 1755ce263fc8SMatthias Ringwald 17562ee45eb2SMilanka Ringwald // trigger codec exchange if requested by app 17572ee45eb2SMilanka Ringwald if (hfp_connection->trigger_codec_exchange){ 17582ee45eb2SMilanka Ringwald log_info("trigger codec, command %u, codec state %u", hfp_connection->command, hfp_connection->codecs_state); 17592ee45eb2SMilanka Ringwald } 17602ee45eb2SMilanka Ringwald if (hfp_connection->trigger_codec_exchange && hfp_connection->command == HFP_CMD_NONE){ 17612ee45eb2SMilanka Ringwald switch (hfp_connection->codecs_state){ 17622ee45eb2SMilanka Ringwald case HFP_CODECS_IDLE: 17632ee45eb2SMilanka Ringwald case HFP_CODECS_RECEIVED_LIST: 17642ee45eb2SMilanka Ringwald case HFP_CODECS_AG_RESEND_COMMON_CODEC: 17652ee45eb2SMilanka Ringwald case HFP_CODECS_ERROR: 17662ee45eb2SMilanka Ringwald hfp_connection->trigger_codec_exchange = 0; 17672ee45eb2SMilanka Ringwald hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC; 17682ee45eb2SMilanka Ringwald break; 17692ee45eb2SMilanka Ringwald default: 17702ee45eb2SMilanka Ringwald break; 17712ee45eb2SMilanka Ringwald } 17722ee45eb2SMilanka Ringwald } 17732ee45eb2SMilanka Ringwald 1774c04cf7ffSMilanka Ringwald int cmd_sent = hfp_ag_run_for_context_service_level_connection(hfp_connection); 1775c04cf7ffSMilanka Ringwald if (!cmd_sent){ 1776c04cf7ffSMilanka Ringwald cmd_sent = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection); 17773deb3ec6SMatthias Ringwald } 17783deb3ec6SMatthias Ringwald 1779c04cf7ffSMilanka Ringwald if (!cmd_sent){ 1780c04cf7ffSMilanka Ringwald cmd_sent = call_setup_state_machine(hfp_connection); 1781aa4dd815SMatthias Ringwald } 1782aa4dd815SMatthias Ringwald 1783c04cf7ffSMilanka Ringwald if (!cmd_sent){ 1784c04cf7ffSMilanka Ringwald cmd_sent = hfp_ag_run_for_audio_connection(hfp_connection); 1785aa4dd815SMatthias Ringwald } 17863deb3ec6SMatthias Ringwald 1787c04cf7ffSMilanka Ringwald if (hfp_connection->command == HFP_CMD_NONE && !cmd_sent){ 1788a0ffb263SMatthias Ringwald // log_info("hfp_connection->command == HFP_CMD_NONE"); 1789a0ffb263SMatthias Ringwald switch(hfp_connection->state){ 17903deb3ec6SMatthias Ringwald case HFP_W2_DISCONNECT_RFCOMM: 1791a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 1792a0ffb263SMatthias Ringwald rfcomm_disconnect(hfp_connection->rfcomm_cid); 17933deb3ec6SMatthias Ringwald break; 17943deb3ec6SMatthias Ringwald default: 17953deb3ec6SMatthias Ringwald break; 17963deb3ec6SMatthias Ringwald } 17973deb3ec6SMatthias Ringwald } 1798c04cf7ffSMilanka Ringwald 1799c04cf7ffSMilanka Ringwald if (cmd_sent){ 1800a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1801473ac565SMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 1802473ac565SMatthias Ringwald } 18033deb3ec6SMatthias Ringwald } 1804d68dcce1SMatthias Ringwald 1805ce263fc8SMatthias Ringwald static hfp_generic_status_indicator_t *get_hf_indicator_by_number(int number){ 1806ce263fc8SMatthias Ringwald int i; 1807a0ffb263SMatthias Ringwald for (i=0;i< hfp_generic_status_indicators_nr;i++){ 1808a0ffb263SMatthias Ringwald hfp_generic_status_indicator_t * indicator = &hfp_generic_status_indicators[i]; 1809ce263fc8SMatthias Ringwald if (indicator->uuid == number){ 1810ce263fc8SMatthias Ringwald return indicator; 1811ce263fc8SMatthias Ringwald } 1812ce263fc8SMatthias Ringwald } 1813ce263fc8SMatthias Ringwald return NULL; 1814ce263fc8SMatthias Ringwald } 18153deb3ec6SMatthias Ringwald 1816e9c22d4eSMatthias Ringwald static void hfp_ag_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 18178a46ec40SMatthias Ringwald UNUSED(packet_type); // ok: only called with RFCOMM_DATA_PACKET 18188a46ec40SMatthias Ringwald 18198a46ec40SMatthias Ringwald // assertion: size >= 1 as rfcomm.c does not deliver empty packets 18208a46ec40SMatthias Ringwald if (size < 1) return; 18219ec2630cSMatthias Ringwald 1822a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 1823a0ffb263SMatthias Ringwald if (!hfp_connection) return; 18241e35c04dSMatthias Ringwald 18258a46ec40SMatthias Ringwald // temp overwrite last byte (most likely \n for log_info) 18261e35c04dSMatthias Ringwald char last_char = packet[size-1]; 18271e35c04dSMatthias Ringwald packet[size-1] = 0; 18281e35c04dSMatthias Ringwald log_info("HFP_RX %s", packet); 18291e35c04dSMatthias Ringwald packet[size-1] = last_char; 18301e35c04dSMatthias Ringwald 18318a46ec40SMatthias Ringwald // process messages byte-wise 18323deb3ec6SMatthias Ringwald int pos; 18333deb3ec6SMatthias Ringwald for (pos = 0; pos < size ; pos++){ 1834a0ffb263SMatthias Ringwald hfp_parse(hfp_connection, packet[pos], 0); 1835aa4dd815SMatthias Ringwald } 1836ce263fc8SMatthias Ringwald hfp_generic_status_indicator_t * indicator; 1837ce263fc8SMatthias Ringwald int value; 1838a0ffb263SMatthias Ringwald switch(hfp_connection->command){ 1839ce263fc8SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_QUERY: 1840ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_active){ 1841a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1; 1842ce263fc8SMatthias Ringwald } 1843a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1844ce263fc8SMatthias Ringwald break; 1845ce263fc8SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_COMMAND: 18462308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 184760ebb071SMilanka Ringwald log_info("HF Response and Hold: %u", value); 1848ce263fc8SMatthias Ringwald switch(value){ 1849ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD: 1850a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection); 1851ce263fc8SMatthias Ringwald break; 1852ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED: 1853a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection); 1854ce263fc8SMatthias Ringwald break; 1855ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED: 1856a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection); 1857ce263fc8SMatthias Ringwald break; 1858ce263fc8SMatthias Ringwald default: 1859ce263fc8SMatthias Ringwald break; 1860ce263fc8SMatthias Ringwald } 1861a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1862ce263fc8SMatthias Ringwald break; 1863ce263fc8SMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 1864a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1865ce263fc8SMatthias Ringwald // find indicator by assigned number 1866a0ffb263SMatthias Ringwald indicator = get_hf_indicator_by_number(hfp_connection->parser_indicator_index); 1867ce263fc8SMatthias Ringwald if (!indicator){ 1868a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1869ce263fc8SMatthias Ringwald break; 1870ce263fc8SMatthias Ringwald } 18712308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1872ce263fc8SMatthias Ringwald switch (indicator->uuid){ 1873ce263fc8SMatthias Ringwald case 1: // enhanced security 1874ce263fc8SMatthias Ringwald if (value > 1) { 1875a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1876ce263fc8SMatthias Ringwald return; 1877ce263fc8SMatthias Ringwald } 187860ebb071SMilanka Ringwald log_info("HF Indicator 'enhanced security' set to %u", value); 1879ce263fc8SMatthias Ringwald break; 1880ce263fc8SMatthias Ringwald case 2: // battery level 1881ce263fc8SMatthias Ringwald if (value > 100){ 1882a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1883ce263fc8SMatthias Ringwald return; 1884ce263fc8SMatthias Ringwald } 188560ebb071SMilanka Ringwald log_info("HF Indicator 'battery' set to %u", value); 1886ce263fc8SMatthias Ringwald break; 1887ce263fc8SMatthias Ringwald default: 188860ebb071SMilanka Ringwald log_info("HF Indicator unknown set to %u", value); 1889ce263fc8SMatthias Ringwald break; 1890ce263fc8SMatthias Ringwald } 1891a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1892ce263fc8SMatthias Ringwald break; 1893ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 1894ce263fc8SMatthias Ringwald // expected by SLC state machine 1895a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break; 1896a0ffb263SMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0; 1897a0ffb263SMatthias Ringwald hfp_connection->send_ag_status_indicators = 1; 1898ce263fc8SMatthias Ringwald break; 1899ce263fc8SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 1900a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1901a0ffb263SMatthias Ringwald hfp_connection->next_call_index = 0; 1902a0ffb263SMatthias Ringwald hfp_connection->send_status_of_current_calls = 1; 1903ce263fc8SMatthias Ringwald break; 1904ce263fc8SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1905ce263fc8SMatthias Ringwald if (subscriber_numbers_count == 0){ 1906485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1907ce263fc8SMatthias Ringwald break; 1908ce263fc8SMatthias Ringwald } 1909a0ffb263SMatthias Ringwald hfp_connection->next_subscriber_number_to_send = 0; 1910a0ffb263SMatthias Ringwald hfp_connection->send_subscriber_number = 1; 1911ce263fc8SMatthias Ringwald break; 1912c1797c7dSMatthias Ringwald case HFP_CMD_TRANSMIT_DTMF_CODES: 1913a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1914ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, (const char *) &hfp_connection->line_buffer[0]); 1915c1797c7dSMatthias Ringwald break; 1916aa4dd815SMatthias Ringwald case HFP_CMD_HF_REQUEST_PHONE_NUMBER: 1917a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1918ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG); 1919aa4dd815SMatthias Ringwald break; 1920aa4dd815SMatthias Ringwald case HFP_CMD_TURN_OFF_EC_AND_NR: 1921a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1922aa4dd815SMatthias Ringwald if (get_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION)){ 1923a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1924a0ffb263SMatthias Ringwald hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION, hfp_connection->ag_echo_and_noise_reduction); 192560ebb071SMilanka Ringwald log_info("AG: EC/NR = %u", hfp_connection->ag_echo_and_noise_reduction); 1926aa4dd815SMatthias Ringwald } else { 1927a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 1928aa4dd815SMatthias Ringwald } 1929aa4dd815SMatthias Ringwald break; 1930aa4dd815SMatthias Ringwald case HFP_CMD_CALL_ANSWERED: 1931a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 193260ebb071SMilanka Ringwald log_info("HFP: ATA"); 1933a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection); 1934aa4dd815SMatthias Ringwald break; 1935aa4dd815SMatthias Ringwald case HFP_CMD_HANG_UP_CALL: 1936a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1937a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1938a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection); 1939aa4dd815SMatthias Ringwald break; 1940aa4dd815SMatthias Ringwald case HFP_CMD_CALL_HOLD: { 1941aa4dd815SMatthias Ringwald // TODO: fully implement this 1942a0ffb263SMatthias Ringwald log_error("HFP: unhandled call hold type %c", hfp_connection->line_buffer[0]); 1943a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1944a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1945a0ffb263SMatthias Ringwald hfp_connection->call_index = 0; 1946d0c20769SMatthias Ringwald 1947a0ffb263SMatthias Ringwald if (hfp_connection->line_buffer[1] != '\0'){ 19482308e108SMilanka Ringwald hfp_connection->call_index = btstack_atoi((char *)&hfp_connection->line_buffer[1]); 1949d0c20769SMatthias Ringwald } 1950d210d9c4SMatthias Ringwald 1951a0ffb263SMatthias Ringwald switch (hfp_connection->line_buffer[0]){ 1952aa4dd815SMatthias Ringwald case '0': 1953d0c20769SMatthias Ringwald // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call. 1954a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection); 1955aa4dd815SMatthias Ringwald break; 1956aa4dd815SMatthias Ringwald case '1': 1957d0c20769SMatthias Ringwald // Releases all active calls (if any exist) and accepts the other (held or waiting) call. 1958d0c20769SMatthias Ringwald // Where both a held and a waiting call exist, the above procedures shall apply to the 1959d0c20769SMatthias Ringwald // waiting call (i.e., not to the held call) in conflicting situation. 1960a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection); 1961aa4dd815SMatthias Ringwald break; 1962aa4dd815SMatthias Ringwald case '2': 1963d0c20769SMatthias Ringwald // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call. 1964d0c20769SMatthias Ringwald // Where both a held and a waiting call exist, the above procedures shall apply to the 1965d0c20769SMatthias Ringwald // waiting call (i.e., not to the held call) in conflicting situation. 1966a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection); 1967aa4dd815SMatthias Ringwald break; 1968aa4dd815SMatthias Ringwald case '3': 1969d0c20769SMatthias Ringwald // Adds a held call to the conversation. 1970a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection); 1971aa4dd815SMatthias Ringwald break; 1972aa4dd815SMatthias Ringwald case '4': 1973d0c20769SMatthias Ringwald // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer). 1974a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection); 1975aa4dd815SMatthias Ringwald break; 1976aa4dd815SMatthias Ringwald default: 1977aa4dd815SMatthias Ringwald break; 1978aa4dd815SMatthias Ringwald } 1979aa4dd815SMatthias Ringwald break; 1980aa4dd815SMatthias Ringwald } 1981aa4dd815SMatthias Ringwald case HFP_CMD_CALL_PHONE_NUMBER: 1982a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1983a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED, hfp_connection); 1984aa4dd815SMatthias Ringwald break; 1985aa4dd815SMatthias Ringwald case HFP_CMD_REDIAL_LAST_NUMBER: 1986a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1987a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection); 1988aa4dd815SMatthias Ringwald break; 1989aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_CLIP: 1990a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1991a0ffb263SMatthias Ringwald hfp_connection->clip_enabled = hfp_connection->line_buffer[8] != '0'; 1992a0ffb263SMatthias Ringwald log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled); 1993a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1994aa4dd815SMatthias Ringwald break; 1995aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION: 1996a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1997a0ffb263SMatthias Ringwald hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[8] != '0'; 1998a0ffb263SMatthias Ringwald log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled); 1999a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2000aa4dd815SMatthias Ringwald break; 2001aa4dd815SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 2002a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2003a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 200460ebb071SMilanka Ringwald log_info("HF speaker gain = %u", hfp_connection->speaker_gain); 2005aa4dd815SMatthias Ringwald break; 2006aa4dd815SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 2007a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 2008a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 200960ebb071SMilanka Ringwald log_info("HF microphone gain = %u", hfp_connection->microphone_gain); 2010aa4dd815SMatthias Ringwald break; 2011aa4dd815SMatthias Ringwald default: 2012aa4dd815SMatthias Ringwald break; 20133deb3ec6SMatthias Ringwald } 20143deb3ec6SMatthias Ringwald } 20153deb3ec6SMatthias Ringwald 2016aa4dd815SMatthias Ringwald static void hfp_run(void){ 2017d63c37a1SMatthias Ringwald btstack_linked_list_iterator_t it; 2018d63c37a1SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2019d63c37a1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2020d63c37a1SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 202122387625SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2022a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 20233deb3ec6SMatthias Ringwald } 20243deb3ec6SMatthias Ringwald } 20253deb3ec6SMatthias Ringwald 2026e9c22d4eSMatthias Ringwald static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 20273deb3ec6SMatthias Ringwald switch (packet_type){ 20283deb3ec6SMatthias Ringwald case RFCOMM_DATA_PACKET: 2029e9c22d4eSMatthias Ringwald hfp_ag_handle_rfcomm_data(packet_type, channel, packet, size); 20303deb3ec6SMatthias Ringwald break; 20313deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 2032e30a6a47SMatthias Ringwald if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 2033e30a6a47SMatthias Ringwald uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 2034e30a6a47SMatthias Ringwald hfp_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid)); 2035e30a6a47SMatthias Ringwald return; 2036e30a6a47SMatthias Ringwald } 2037*27950165SMatthias Ringwald hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_AG); 2038aa4dd815SMatthias Ringwald break; 20393deb3ec6SMatthias Ringwald default: 20403deb3ec6SMatthias Ringwald break; 20413deb3ec6SMatthias Ringwald } 20423deb3ec6SMatthias Ringwald 20433deb3ec6SMatthias Ringwald hfp_run(); 20443deb3ec6SMatthias Ringwald } 20453deb3ec6SMatthias Ringwald 2046a0ffb263SMatthias Ringwald void hfp_ag_init_codecs(int codecs_nr, uint8_t * codecs){ 20473deb3ec6SMatthias Ringwald if (codecs_nr > HFP_MAX_NUM_CODECS){ 20483deb3ec6SMatthias Ringwald log_error("hfp_init: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS); 20493deb3ec6SMatthias Ringwald return; 20503deb3ec6SMatthias Ringwald } 20513deb3ec6SMatthias Ringwald int i; 2052a0ffb263SMatthias Ringwald hfp_codecs_nr = codecs_nr; 20533deb3ec6SMatthias Ringwald for (i=0; i < codecs_nr; i++){ 20543deb3ec6SMatthias Ringwald hfp_codecs[i] = codecs[i]; 20553deb3ec6SMatthias Ringwald } 2056a0ffb263SMatthias Ringwald } 20573deb3ec6SMatthias Ringwald 2058a0ffb263SMatthias Ringwald void hfp_ag_init_supported_features(uint32_t supported_features){ 2059a0ffb263SMatthias Ringwald hfp_supported_features = supported_features; 2060a0ffb263SMatthias Ringwald } 20613deb3ec6SMatthias Ringwald 2062a0ffb263SMatthias Ringwald void hfp_ag_init_ag_indicators(int ag_indicators_nr, hfp_ag_indicator_t * ag_indicators){ 2063a0ffb263SMatthias Ringwald hfp_ag_indicators_nr = ag_indicators_nr; 2064a0ffb263SMatthias Ringwald memcpy(hfp_ag_indicators, ag_indicators, ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 2065a0ffb263SMatthias Ringwald } 20663deb3ec6SMatthias Ringwald 2067a0ffb263SMatthias Ringwald void hfp_ag_init_hf_indicators(int hf_indicators_nr, hfp_generic_status_indicator_t * hf_indicators){ 2068a0ffb263SMatthias Ringwald if (hf_indicators_nr > HFP_MAX_NUM_HF_INDICATORS) return; 2069a0ffb263SMatthias Ringwald hfp_generic_status_indicators_nr = hf_indicators_nr; 2070a0ffb263SMatthias Ringwald memcpy(hfp_generic_status_indicators, hf_indicators, hf_indicators_nr * sizeof(hfp_generic_status_indicator_t)); 2071a0ffb263SMatthias Ringwald } 2072a0ffb263SMatthias Ringwald 2073a0ffb263SMatthias Ringwald void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){ 20743deb3ec6SMatthias Ringwald hfp_ag_call_hold_services_nr = call_hold_services_nr; 20753deb3ec6SMatthias Ringwald memcpy(hfp_ag_call_hold_services, call_hold_services, call_hold_services_nr * sizeof(char *)); 2076a0ffb263SMatthias Ringwald } 2077a0ffb263SMatthias Ringwald 2078a0ffb263SMatthias Ringwald 2079a0ffb263SMatthias Ringwald void hfp_ag_init(uint16_t rfcomm_channel_nr){ 2080*27950165SMatthias Ringwald 2081520c92d5SMatthias Ringwald hfp_init(); 2082d63c37a1SMatthias Ringwald 2083*27950165SMatthias Ringwald hci_event_callback_registration.callback = &hfp_handle_hci_event; 2084*27950165SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 2085*27950165SMatthias Ringwald 2086e9c22d4eSMatthias Ringwald rfcomm_register_service(&rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 2087*27950165SMatthias Ringwald 2088*27950165SMatthias Ringwald // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us 2089e9c22d4eSMatthias Ringwald hfp_set_ag_rfcomm_packet_handler(&rfcomm_packet_handler); 2090aa4dd815SMatthias Ringwald 2091d210d9c4SMatthias Ringwald hfp_ag_response_and_hold_active = 0; 2092d210d9c4SMatthias Ringwald subscriber_numbers = NULL; 2093d210d9c4SMatthias Ringwald subscriber_numbers_count = 0; 2094d210d9c4SMatthias Ringwald 2095d210d9c4SMatthias Ringwald hfp_gsm_init(); 20963deb3ec6SMatthias Ringwald } 20973deb3ec6SMatthias Ringwald 20983deb3ec6SMatthias Ringwald void hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){ 2099323d3000SMatthias Ringwald hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE, HFP_ROLE_AG); 21003deb3ec6SMatthias Ringwald } 21013deb3ec6SMatthias Ringwald 2102d97d752dSMilanka Ringwald void hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){ 21039c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2104a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2105a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2106a33eb0c4SMilanka Ringwald return; 2107a33eb0c4SMilanka Ringwald } 2108a0ffb263SMatthias Ringwald hfp_release_service_level_connection(hfp_connection); 2109a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 21103deb3ec6SMatthias Ringwald } 21113deb3ec6SMatthias Ringwald 2112d97d752dSMilanka Ringwald void hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, hfp_cme_error_t error){ 21139c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2114a0ffb263SMatthias Ringwald if (!hfp_connection){ 2115a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 21163deb3ec6SMatthias Ringwald return; 21173deb3ec6SMatthias Ringwald } 2118a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 2119a0ffb263SMatthias Ringwald if (!hfp_connection->enable_extended_audio_gateway_error_report){ 21203deb3ec6SMatthias Ringwald return; 21213deb3ec6SMatthias Ringwald } 2122a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = error; 2123a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 21243deb3ec6SMatthias Ringwald } 21253deb3ec6SMatthias Ringwald 2126a0ffb263SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){ 21272ee45eb2SMilanka Ringwald log_info("hfp_ag_setup_audio_connection state %u", hfp_connection->state); 2128a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return; 2129a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return; 21303deb3ec6SMatthias Ringwald 2131a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 1; 2132a0ffb263SMatthias Ringwald if (!has_codec_negotiation_feature(hfp_connection)){ 2133d6ff09e1SMatthias Ringwald log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using CVSD"); 2134d6ff09e1SMatthias Ringwald hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 2135a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 21367522e673SMatthias Ringwald // now, pick link settings 21377522e673SMatthias Ringwald hfp_init_link_settings(hfp_connection); 21382ee45eb2SMilanka Ringwald return; 21393deb3ec6SMatthias Ringwald } 2140aa4dd815SMatthias Ringwald 21412ee45eb2SMilanka Ringwald hfp_connection->trigger_codec_exchange = 1; 2142aa4dd815SMatthias Ringwald } 2143aa4dd815SMatthias Ringwald 2144d97d752dSMilanka Ringwald void hfp_ag_establish_audio_connection(hci_con_handle_t acl_handle){ 21459c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2146a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2147a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2148a33eb0c4SMilanka Ringwald return; 2149a33eb0c4SMilanka Ringwald } 21502ee45eb2SMilanka Ringwald hfp_connection->trigger_codec_exchange = 0; 2151a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 2152a0ffb263SMatthias Ringwald hfp_ag_setup_audio_connection(hfp_connection); 2153a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 21543deb3ec6SMatthias Ringwald } 21553deb3ec6SMatthias Ringwald 2156d97d752dSMilanka Ringwald void hfp_ag_release_audio_connection(hci_con_handle_t acl_handle){ 21579c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2158a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2159a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2160a33eb0c4SMilanka Ringwald return; 2161a33eb0c4SMilanka Ringwald } 2162a0ffb263SMatthias Ringwald hfp_release_audio_connection(hfp_connection); 2163a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 21643deb3ec6SMatthias Ringwald } 2165aa4dd815SMatthias Ringwald 2166aa4dd815SMatthias Ringwald /** 2167aa4dd815SMatthias Ringwald * @brief Enable in-band ring tone 2168aa4dd815SMatthias Ringwald */ 2169aa4dd815SMatthias Ringwald void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){ 2170aa4dd815SMatthias Ringwald if (get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE) == use_in_band_ring_tone){ 2171aa4dd815SMatthias Ringwald return; 2172aa4dd815SMatthias Ringwald } 2173aa4dd815SMatthias Ringwald hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE, use_in_band_ring_tone); 2174aa4dd815SMatthias Ringwald 2175665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 2176665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2177665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2178a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 2179a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING; 2180a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 2181aa4dd815SMatthias Ringwald } 2182aa4dd815SMatthias Ringwald } 2183aa4dd815SMatthias Ringwald 2184aa4dd815SMatthias Ringwald /** 2185aa4dd815SMatthias Ringwald * @brief Called from GSM 2186aa4dd815SMatthias Ringwald */ 2187aa4dd815SMatthias Ringwald void hfp_ag_incoming_call(void){ 2188aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL); 2189aa4dd815SMatthias Ringwald } 2190aa4dd815SMatthias Ringwald 2191aa4dd815SMatthias Ringwald /** 2192aa4dd815SMatthias Ringwald * @brief number is stored. 2193aa4dd815SMatthias Ringwald */ 2194aa4dd815SMatthias Ringwald void hfp_ag_set_clip(uint8_t type, const char * number){ 2195d0c20769SMatthias Ringwald hfp_gsm_handle_event_with_clip(HFP_AG_SET_CLIP, type, number); 2196aa4dd815SMatthias Ringwald } 2197aa4dd815SMatthias Ringwald 2198aa4dd815SMatthias Ringwald void hfp_ag_call_dropped(void){ 2199aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL); 2200aa4dd815SMatthias Ringwald } 2201aa4dd815SMatthias Ringwald 2202aa4dd815SMatthias Ringwald // call from AG UI 2203aa4dd815SMatthias Ringwald void hfp_ag_answer_incoming_call(void){ 2204aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL); 2205aa4dd815SMatthias Ringwald } 2206aa4dd815SMatthias Ringwald 2207ce263fc8SMatthias Ringwald void hfp_ag_join_held_call(void){ 2208ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL); 2209ce263fc8SMatthias Ringwald } 2210ce263fc8SMatthias Ringwald 2211aa4dd815SMatthias Ringwald void hfp_ag_terminate_call(void){ 2212aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL); 2213aa4dd815SMatthias Ringwald } 2214aa4dd815SMatthias Ringwald 2215aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_ringing(void){ 2216aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL); 2217aa4dd815SMatthias Ringwald } 2218aa4dd815SMatthias Ringwald 2219aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_established(void){ 2220aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL); 2221aa4dd815SMatthias Ringwald } 2222aa4dd815SMatthias Ringwald 2223aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_rejected(void){ 2224aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL); 2225aa4dd815SMatthias Ringwald } 2226aa4dd815SMatthias Ringwald 2227aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_accepted(void){ 2228aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL); 2229aa4dd815SMatthias Ringwald } 2230ce263fc8SMatthias Ringwald 2231ce263fc8SMatthias Ringwald void hfp_ag_hold_incoming_call(void){ 2232ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL); 2233ce263fc8SMatthias Ringwald } 2234ce263fc8SMatthias Ringwald 2235ce263fc8SMatthias Ringwald void hfp_ag_accept_held_incoming_call(void) { 2236ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL); 2237ce263fc8SMatthias Ringwald } 2238ce263fc8SMatthias Ringwald 2239ce263fc8SMatthias Ringwald void hfp_ag_reject_held_incoming_call(void){ 2240ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL); 2241ce263fc8SMatthias Ringwald } 2242ce263fc8SMatthias Ringwald 2243aa4dd815SMatthias Ringwald static void hfp_ag_set_ag_indicator(const char * name, int value){ 2244aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name(name); 2245aa4dd815SMatthias Ringwald if (indicator_index < 0) return; 2246aa4dd815SMatthias Ringwald hfp_ag_indicators[indicator_index].status = value; 2247aa4dd815SMatthias Ringwald 2248ce263fc8SMatthias Ringwald 2249665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 2250665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2251665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2252a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 2253a0ffb263SMatthias Ringwald if (! hfp_connection->ag_indicators[indicator_index].enabled) { 2254ce263fc8SMatthias Ringwald log_info("AG indicator '%s' changed to %u but not enabled", hfp_ag_indicators[indicator_index].name, value); 2255ce263fc8SMatthias Ringwald continue; 2256ce263fc8SMatthias Ringwald } 2257ce263fc8SMatthias Ringwald log_info("AG indicator '%s' changed to %u, request transfer statur", hfp_ag_indicators[indicator_index].name, value); 2258a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 2259a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 2260aa4dd815SMatthias Ringwald } 2261aa4dd815SMatthias Ringwald } 2262aa4dd815SMatthias Ringwald 2263aa4dd815SMatthias Ringwald void hfp_ag_set_registration_status(int status){ 2264aa4dd815SMatthias Ringwald hfp_ag_set_ag_indicator("service", status); 2265aa4dd815SMatthias Ringwald } 2266aa4dd815SMatthias Ringwald 2267aa4dd815SMatthias Ringwald void hfp_ag_set_signal_strength(int strength){ 2268aa4dd815SMatthias Ringwald hfp_ag_set_ag_indicator("signal", strength); 2269aa4dd815SMatthias Ringwald } 2270aa4dd815SMatthias Ringwald 2271aa4dd815SMatthias Ringwald void hfp_ag_set_roaming_status(int status){ 2272aa4dd815SMatthias Ringwald hfp_ag_set_ag_indicator("roam", status); 2273aa4dd815SMatthias Ringwald } 2274aa4dd815SMatthias Ringwald 2275aa4dd815SMatthias Ringwald void hfp_ag_set_battery_level(int level){ 2276aa4dd815SMatthias Ringwald hfp_ag_set_ag_indicator("battchg", level); 2277aa4dd815SMatthias Ringwald } 2278aa4dd815SMatthias Ringwald 2279d97d752dSMilanka Ringwald void hfp_ag_activate_voice_recognition(hci_con_handle_t acl_handle, int activate){ 2280aa4dd815SMatthias Ringwald if (!get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)) return; 22819c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2282a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2283a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2284a33eb0c4SMilanka Ringwald return; 2285a33eb0c4SMilanka Ringwald } 2286aa4dd815SMatthias Ringwald 2287a0ffb263SMatthias Ringwald if (!get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION)) { 228860ebb071SMilanka Ringwald log_info("AG cannot acivate voice recognition - not supported by HF"); 2289aa4dd815SMatthias Ringwald return; 2290aa4dd815SMatthias Ringwald } 2291aa4dd815SMatthias Ringwald 2292aa4dd815SMatthias Ringwald if (activate){ 2293d97d752dSMilanka Ringwald hfp_ag_establish_audio_connection(acl_handle); 2294aa4dd815SMatthias Ringwald } 2295aa4dd815SMatthias Ringwald 2296a0ffb263SMatthias Ringwald hfp_connection->ag_activate_voice_recognition = activate; 2297a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION; 2298a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 2299aa4dd815SMatthias Ringwald } 2300aa4dd815SMatthias Ringwald 2301d97d752dSMilanka Ringwald void hfp_ag_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 23029c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2303a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2304a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2305a33eb0c4SMilanka Ringwald return; 2306a33eb0c4SMilanka Ringwald } 2307a0ffb263SMatthias Ringwald if (hfp_connection->microphone_gain != gain){ 2308a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_SET_MICROPHONE_GAIN; 2309a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = gain; 2310a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 2311aa4dd815SMatthias Ringwald } 2312a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 2313aa4dd815SMatthias Ringwald } 2314aa4dd815SMatthias Ringwald 2315d97d752dSMilanka Ringwald void hfp_ag_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 23169c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2317a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2318a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2319a33eb0c4SMilanka Ringwald return; 2320a33eb0c4SMilanka Ringwald } 2321a0ffb263SMatthias Ringwald if (hfp_connection->speaker_gain != gain){ 2322a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = gain; 2323a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 2324aa4dd815SMatthias Ringwald } 2325a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 2326aa4dd815SMatthias Ringwald } 2327aa4dd815SMatthias Ringwald 2328d97d752dSMilanka Ringwald void hfp_ag_send_phone_number_for_voice_tag(hci_con_handle_t acl_handle, const char * number){ 23299c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2330a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2331a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2332a33eb0c4SMilanka Ringwald return; 2333a33eb0c4SMilanka Ringwald } 2334aa4dd815SMatthias Ringwald hfp_ag_set_clip(0, number); 2335a0ffb263SMatthias Ringwald hfp_connection->send_phone_number_for_voice_tag = 1; 2336aa4dd815SMatthias Ringwald } 2337aa4dd815SMatthias Ringwald 2338d97d752dSMilanka Ringwald void hfp_ag_reject_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 23399c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2340a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2341a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2342a33eb0c4SMilanka Ringwald return; 2343a33eb0c4SMilanka Ringwald } 2344a0ffb263SMatthias Ringwald hfp_connection->send_error = 1; 2345aa4dd815SMatthias Ringwald } 2346aa4dd815SMatthias Ringwald 2347d97d752dSMilanka Ringwald void hfp_ag_send_dtmf_code_done(hci_con_handle_t acl_handle){ 23489c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2349a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2350a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2351a33eb0c4SMilanka Ringwald return; 2352a33eb0c4SMilanka Ringwald } 2353a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 2354c1797c7dSMatthias Ringwald } 2355aa4dd815SMatthias Ringwald 2356ce263fc8SMatthias Ringwald void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){ 2357ce263fc8SMatthias Ringwald subscriber_numbers = numbers; 2358ce263fc8SMatthias Ringwald subscriber_numbers_count = numbers_count; 2359ce263fc8SMatthias Ringwald } 2360ce263fc8SMatthias Ringwald 23619cae807eSMatthias Ringwald void hfp_ag_clear_last_dialed_number(void){ 23629cae807eSMatthias Ringwald hfp_gsm_clear_last_dialed_number(); 2363ce263fc8SMatthias Ringwald } 2364ce263fc8SMatthias Ringwald 2365d97d752dSMilanka Ringwald void hfp_ag_notify_incoming_call_waiting(hci_con_handle_t acl_handle){ 23669c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2367a33eb0c4SMilanka Ringwald if (!hfp_connection){ 2368a33eb0c4SMilanka Ringwald log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2369a33eb0c4SMilanka Ringwald return; 2370a33eb0c4SMilanka Ringwald } 2371a0ffb263SMatthias Ringwald if (!hfp_connection->call_waiting_notification_enabled) return; 2372a0ffb263SMatthias Ringwald 2373a0ffb263SMatthias Ringwald hfp_connection->ag_notify_incoming_call_waiting = 1; 2374a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 2375a0ffb263SMatthias Ringwald } 2376ce263fc8SMatthias Ringwald 2377