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 232fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 242fca4dadSMilanka Ringwald * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 253deb3ec6SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 263deb3ec6SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 273deb3ec6SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 283deb3ec6SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 293deb3ec6SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 303deb3ec6SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 313deb3ec6SMatthias Ringwald * SUCH DAMAGE. 323deb3ec6SMatthias Ringwald * 333deb3ec6SMatthias Ringwald * Please inquire about commercial licensing options at 343deb3ec6SMatthias Ringwald * [email protected] 353deb3ec6SMatthias Ringwald * 363deb3ec6SMatthias Ringwald */ 37ab2c6ae4SMatthias Ringwald 38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "hfp.c" 393deb3ec6SMatthias Ringwald 403deb3ec6SMatthias Ringwald 417907f069SMatthias Ringwald #include "btstack_config.h" 423deb3ec6SMatthias Ringwald 433deb3ec6SMatthias Ringwald #include <stdint.h> 443cfa4086SMatthias Ringwald #include <stdio.h> 453deb3ec6SMatthias Ringwald #include <string.h> 463deb3ec6SMatthias Ringwald #include <inttypes.h> 473deb3ec6SMatthias Ringwald 48235946f1SMatthias Ringwald #include "bluetooth_sdp.h" 49023f2764SMatthias Ringwald #include "btstack_debug.h" 5059c6af15SMatthias Ringwald #include "btstack_event.h" 5159c6af15SMatthias Ringwald #include "btstack_memory.h" 5259c6af15SMatthias Ringwald #include "btstack_run_loop.h" 53efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h" 54746ccb7eSMatthias Ringwald #include "classic/sdp_server.h" 55023f2764SMatthias Ringwald #include "classic/sdp_util.h" 561d9c9c90SMilanka Ringwald #include "classic/sdp_client.h" 5759c6af15SMatthias Ringwald #include "hci.h" 5859c6af15SMatthias Ringwald #include "hci_cmd.h" 5959c6af15SMatthias Ringwald #include "hci_dump.h" 603deb3ec6SMatthias Ringwald 613721a235SMatthias Ringwald #if defined(ENABLE_CC256X_ASSISTED_HFP) && !defined(ENABLE_SCO_OVER_PCM) 623721a235SMatthias Ringwald #error "Assisted HFP is only possible over PCM/I2S. Please add define: ENABLE_SCO_OVER_PCM" 633721a235SMatthias Ringwald #endif 643721a235SMatthias Ringwald 65689d4323SMatthias Ringwald #if defined(ENABLE_BCM_PCM_WBS) && !defined(ENABLE_SCO_OVER_PCM) 66689d4323SMatthias Ringwald #error "WBS for PCM is only possible over PCM/I2S. Please add define: ENABLE_SCO_OVER_PCM" 67689d4323SMatthias Ringwald #endif 68689d4323SMatthias Ringwald 693deb3ec6SMatthias Ringwald #define HFP_HF_FEATURES_SIZE 10 703deb3ec6SMatthias Ringwald #define HFP_AG_FEATURES_SIZE 12 713deb3ec6SMatthias Ringwald 7220b2edb6SMatthias Ringwald typedef struct { 7320b2edb6SMatthias Ringwald hfp_role_t local_role; 7420b2edb6SMatthias Ringwald bd_addr_t remote_address; 7520b2edb6SMatthias Ringwald } hfp_sdp_query_context_t; 7620b2edb6SMatthias Ringwald 7720b2edb6SMatthias Ringwald // globals 7820b2edb6SMatthias Ringwald 7920b2edb6SMatthias Ringwald static btstack_linked_list_t hfp_connections ; 8020b2edb6SMatthias Ringwald 8120b2edb6SMatthias Ringwald static btstack_packet_handler_t hfp_hf_callback; 8220b2edb6SMatthias Ringwald static btstack_packet_handler_t hfp_ag_callback; 8320b2edb6SMatthias Ringwald 8420b2edb6SMatthias Ringwald static btstack_packet_handler_t hfp_hf_rfcomm_packet_handler; 8520b2edb6SMatthias Ringwald static btstack_packet_handler_t hfp_ag_rfcomm_packet_handler; 8620b2edb6SMatthias Ringwald 8721df969bSMatthias Ringwald static uint8_t hfp_hf_indicators_nr; 8821df969bSMatthias Ringwald static const uint8_t * hfp_hf_indicators; 8921df969bSMatthias Ringwald 9020b2edb6SMatthias Ringwald static uint16_t hfp_allowed_sco_packet_types; 91aeb0f0feSMatthias Ringwald static hfp_connection_t * hfp_sco_establishment_active; 92aeb0f0feSMatthias Ringwald 93aeb0f0feSMatthias Ringwald static hfp_sdp_query_context_t hfp_sdp_query_context; 94aeb0f0feSMatthias Ringwald static btstack_context_callback_registration_t hfp_sdp_query_request; 95aeb0f0feSMatthias Ringwald 96aeb0f0feSMatthias Ringwald 97aeb0f0feSMatthias Ringwald 98aeb0f0feSMatthias Ringwald 99aeb0f0feSMatthias Ringwald 100471dea41SMatthias Ringwald // custom commands 101471dea41SMatthias Ringwald static btstack_linked_list_t hfp_custom_commands_ag; 1027404dce3SMatthias Ringwald static btstack_linked_list_t hfp_custom_commands_hf; 10320b2edb6SMatthias Ringwald 10420b2edb6SMatthias Ringwald // prototypes 10520b2edb6SMatthias Ringwald static hfp_link_settings_t hfp_next_link_setting_for_connection(hfp_link_settings_t current_setting, hfp_connection_t * hfp_connection, uint8_t eSCO_S4_supported); 10620b2edb6SMatthias Ringwald static void parse_sequence(hfp_connection_t * context); 10720b2edb6SMatthias Ringwald 1084e01e802SMatthias Ringwald 10964126f36SMatthias Ringwald #define CODEC_MASK_CVSD (1 << HFP_CODEC_CVSD) 11013ea5944SMatthias Ringwald #define CODEC_MASK_OTHER ((1 << HFP_CODEC_MSBC) | (1 << HFP_CODEC_LC3_SWB)) 11113ea5944SMatthias Ringwald 11213ea5944SMatthias Ringwald static const struct { 1134e01e802SMatthias Ringwald const uint16_t max_latency; 1144e01e802SMatthias Ringwald const uint8_t retransmission_effort; 1154e01e802SMatthias Ringwald const uint16_t packet_types; 11664126f36SMatthias Ringwald const uint8_t codec_mask; 1174e01e802SMatthias Ringwald } hfp_link_settings [] = { 118b4320e7fSMatthias Ringwald {0x0004, 0xff, SCO_PACKET_TYPES_HV1, CODEC_MASK_CVSD }, // HFP_LINK_SETTINGS_D0 119b4320e7fSMatthias Ringwald {0x0005, 0xff, SCO_PACKET_TYPES_HV3, CODEC_MASK_CVSD }, // HFP_LINK_SETTINGS_D1 120b4320e7fSMatthias Ringwald {0x0007, 0x01, SCO_PACKET_TYPES_EV3, CODEC_MASK_CVSD }, // HFP_LINK_SETTINGS_S1 121b4320e7fSMatthias Ringwald {0x0007, 0x01, SCO_PACKET_TYPES_2EV3, CODEC_MASK_CVSD }, // HFP_LINK_SETTINGS_S2 122b4320e7fSMatthias Ringwald {0x000a, 0x01, SCO_PACKET_TYPES_2EV3, CODEC_MASK_CVSD }, // HFP_LINK_SETTINGS_S3 123b4320e7fSMatthias Ringwald {0x000c, 0x02, SCO_PACKET_TYPES_2EV3, CODEC_MASK_CVSD }, // HFP_LINK_SETTINGS_S4 124b4320e7fSMatthias Ringwald {0x0008, 0x02, SCO_PACKET_TYPES_EV3, CODEC_MASK_OTHER}, // HFP_LINK_SETTINGS_T1 125b4320e7fSMatthias Ringwald {0x000d, 0x02, SCO_PACKET_TYPES_2EV3, CODEC_MASK_OTHER} // HFP_LINK_SETTINGS_T2 12613ea5944SMatthias Ringwald }; 1273ad75286SMatthias Ringwald 128c169b70dSMatthias Ringwald #ifdef ENABLE_HFP_HF_SAFE_SETTINGS 129c169b70dSMatthias Ringwald // HFP v1.9, table 6.10 'mandatory safe settings' for eSCO + similar entries for SCO 13032459254SMatthias Ringwald static const struct hfp_mandatory_safe_setting { 13132459254SMatthias Ringwald const uint8_t codec_mask; 13232459254SMatthias Ringwald const bool secure_connection_in_use; 13332459254SMatthias Ringwald hfp_link_settings_t link_setting; 13432459254SMatthias Ringwald } hfp_mandatory_safe_settings[] = { 13532459254SMatthias Ringwald { CODEC_MASK_CVSD, false, HFP_LINK_SETTINGS_D1}, 13632459254SMatthias Ringwald { CODEC_MASK_CVSD, true, HFP_LINK_SETTINGS_D1}, 13732459254SMatthias Ringwald { CODEC_MASK_CVSD, false, HFP_LINK_SETTINGS_S1}, 13832459254SMatthias Ringwald { CODEC_MASK_CVSD, true, HFP_LINK_SETTINGS_S4}, 13932459254SMatthias Ringwald { CODEC_MASK_OTHER, false, HFP_LINK_SETTINGS_T1}, 14032459254SMatthias Ringwald { CODEC_MASK_OTHER, true, HFP_LINK_SETTINGS_T2}, 14132459254SMatthias Ringwald }; 142c169b70dSMatthias Ringwald #endif 14332459254SMatthias Ringwald 1443deb3ec6SMatthias Ringwald static const char * hfp_hf_features[] = { 1453deb3ec6SMatthias Ringwald "EC and/or NR function", 1463deb3ec6SMatthias Ringwald "Three-way calling", 1473deb3ec6SMatthias Ringwald "CLI presentation capability", 1483deb3ec6SMatthias Ringwald "Voice recognition activation", 1493deb3ec6SMatthias Ringwald "Remote volume control", 1503deb3ec6SMatthias Ringwald 1513deb3ec6SMatthias Ringwald "Enhanced call status", 1523deb3ec6SMatthias Ringwald "Enhanced call control", 1533deb3ec6SMatthias Ringwald 1543deb3ec6SMatthias Ringwald "Codec negotiation", 1553deb3ec6SMatthias Ringwald 1563deb3ec6SMatthias Ringwald "HF Indicators", 1573deb3ec6SMatthias Ringwald "eSCO S4 (and T2) Settings Supported", 1583deb3ec6SMatthias Ringwald "Reserved for future definition" 1593deb3ec6SMatthias Ringwald }; 1603deb3ec6SMatthias Ringwald 1613deb3ec6SMatthias Ringwald static const char * hfp_ag_features[] = { 1623deb3ec6SMatthias Ringwald "Three-way calling", 1633deb3ec6SMatthias Ringwald "EC and/or NR function", 1643deb3ec6SMatthias Ringwald "Voice recognition function", 1653deb3ec6SMatthias Ringwald "In-band ring tone capability", 1663deb3ec6SMatthias Ringwald "Attach a number to a voice tag", 1673deb3ec6SMatthias Ringwald "Ability to reject a call", 1683deb3ec6SMatthias Ringwald "Enhanced call status", 1693deb3ec6SMatthias Ringwald "Enhanced call control", 1703deb3ec6SMatthias Ringwald "Extended Error Result Codes", 1713deb3ec6SMatthias Ringwald "Codec negotiation", 1723deb3ec6SMatthias Ringwald "HF Indicators", 1733deb3ec6SMatthias Ringwald "eSCO S4 (and T2) Settings Supported", 1743deb3ec6SMatthias Ringwald "Reserved for future definition" 1753deb3ec6SMatthias Ringwald }; 1763deb3ec6SMatthias Ringwald 1770aee97efSMilanka Ringwald static const char * hfp_enhanced_call_dir[] = { 1780aee97efSMilanka Ringwald "outgoing", 1790aee97efSMilanka Ringwald "incoming" 1800aee97efSMilanka Ringwald }; 1810aee97efSMilanka Ringwald 1820aee97efSMilanka Ringwald static const char * hfp_enhanced_call_status[] = { 1830aee97efSMilanka Ringwald "active", 1840aee97efSMilanka Ringwald "held", 1850aee97efSMilanka Ringwald "outgoing dialing", 1860aee97efSMilanka Ringwald "outgoing alerting", 1870aee97efSMilanka Ringwald "incoming", 1880aee97efSMilanka Ringwald "incoming waiting", 1890aee97efSMilanka Ringwald "call held by response and hold" 1900aee97efSMilanka Ringwald }; 1910aee97efSMilanka Ringwald 1920aee97efSMilanka Ringwald static const char * hfp_enhanced_call_mode[] = { 1930aee97efSMilanka Ringwald "voice", 1940aee97efSMilanka Ringwald "data", 1950aee97efSMilanka Ringwald "fax" 1960aee97efSMilanka Ringwald }; 1970aee97efSMilanka Ringwald 1980aee97efSMilanka Ringwald static const char * hfp_enhanced_call_mpty[] = { 1990aee97efSMilanka Ringwald "not a conference call", 2000aee97efSMilanka Ringwald "conference call" 2010aee97efSMilanka Ringwald }; 2020aee97efSMilanka Ringwald 2030aee97efSMilanka Ringwald const char * hfp_enhanced_call_dir2str(uint16_t index){ 2040aee97efSMilanka Ringwald if (index <= HFP_ENHANCED_CALL_DIR_INCOMING) return hfp_enhanced_call_dir[index]; 2050aee97efSMilanka Ringwald return "not defined"; 2060aee97efSMilanka Ringwald } 2070aee97efSMilanka Ringwald 2080aee97efSMilanka Ringwald const char * hfp_enhanced_call_status2str(uint16_t index){ 2090aee97efSMilanka Ringwald if (index <= HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD) return hfp_enhanced_call_status[index]; 2100aee97efSMilanka Ringwald return "not defined"; 2110aee97efSMilanka Ringwald } 2120aee97efSMilanka Ringwald 2130aee97efSMilanka Ringwald const char * hfp_enhanced_call_mode2str(uint16_t index){ 2140aee97efSMilanka Ringwald if (index <= HFP_ENHANCED_CALL_MODE_FAX) return hfp_enhanced_call_mode[index]; 2150aee97efSMilanka Ringwald return "not defined"; 2160aee97efSMilanka Ringwald } 2170aee97efSMilanka Ringwald 2180aee97efSMilanka Ringwald const char * hfp_enhanced_call_mpty2str(uint16_t index){ 2190aee97efSMilanka Ringwald if (index <= HFP_ENHANCED_CALL_MPTY_CONFERENCE_CALL) return hfp_enhanced_call_mpty[index]; 2200aee97efSMilanka Ringwald return "not defined"; 2210aee97efSMilanka Ringwald } 2220aee97efSMilanka Ringwald 22325789943SMilanka Ringwald static uint16_t hfp_parse_indicator_index(hfp_connection_t * hfp_connection){ 22425789943SMilanka Ringwald uint16_t index = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 22525789943SMilanka Ringwald 22625789943SMilanka Ringwald if (index > HFP_MAX_NUM_INDICATORS){ 22725789943SMilanka Ringwald log_info("ignoring invalid indicator index bigger then HFP_MAX_NUM_INDICATORS"); 22825789943SMilanka Ringwald return HFP_MAX_NUM_INDICATORS - 1; 22925789943SMilanka Ringwald } 23025789943SMilanka Ringwald 23125789943SMilanka Ringwald // indicator index enumeration starts with 1, we substract 1 to store in array with starting index 0 23225789943SMilanka Ringwald if (index > 0){ 23325789943SMilanka Ringwald index -= 1; 23425789943SMilanka Ringwald } else { 23525789943SMilanka Ringwald log_info("ignoring invalid indicator index 0"); 23625789943SMilanka Ringwald return 0; 23725789943SMilanka Ringwald } 23825789943SMilanka Ringwald return index; 23925789943SMilanka Ringwald } 24025789943SMilanka Ringwald 24125789943SMilanka Ringwald static void hfp_next_indicators_index(hfp_connection_t * hfp_connection){ 24225789943SMilanka Ringwald if (hfp_connection->parser_item_index < HFP_MAX_NUM_INDICATORS - 1){ 24325789943SMilanka Ringwald hfp_connection->parser_item_index++; 24425789943SMilanka Ringwald } else { 24525789943SMilanka Ringwald log_info("Ignoring additional indicator"); 24625789943SMilanka Ringwald } 24725789943SMilanka Ringwald } 24825789943SMilanka Ringwald 24925789943SMilanka Ringwald static void hfp_next_codec_index(hfp_connection_t * hfp_connection){ 25025789943SMilanka Ringwald if (hfp_connection->parser_item_index < HFP_MAX_NUM_CODECS - 1){ 25125789943SMilanka Ringwald hfp_connection->parser_item_index++; 25225789943SMilanka Ringwald } else { 25325789943SMilanka Ringwald log_info("Ignoring additional codec index"); 25425789943SMilanka Ringwald } 25525789943SMilanka Ringwald } 25625789943SMilanka Ringwald 257eed67a37SMilanka Ringwald static void hfp_next_remote_call_services_index(hfp_connection_t * hfp_connection){ 258eed67a37SMilanka Ringwald if (hfp_connection->remote_call_services_index < HFP_MAX_NUM_CALL_SERVICES - 1){ 259eed67a37SMilanka Ringwald hfp_connection->remote_call_services_index++; 260eed67a37SMilanka Ringwald } else { 261eed67a37SMilanka Ringwald log_info("Ignoring additional remote_call_services"); 262eed67a37SMilanka Ringwald } 263eed67a37SMilanka Ringwald } 26425789943SMilanka Ringwald 2653deb3ec6SMatthias Ringwald const char * hfp_hf_feature(int index){ 2663deb3ec6SMatthias Ringwald if (index > HFP_HF_FEATURES_SIZE){ 2673deb3ec6SMatthias Ringwald return hfp_hf_features[HFP_HF_FEATURES_SIZE]; 2683deb3ec6SMatthias Ringwald } 2693deb3ec6SMatthias Ringwald return hfp_hf_features[index]; 2703deb3ec6SMatthias Ringwald } 2713deb3ec6SMatthias Ringwald 2723deb3ec6SMatthias Ringwald const char * hfp_ag_feature(int index){ 2733deb3ec6SMatthias Ringwald if (index > HFP_AG_FEATURES_SIZE){ 2743deb3ec6SMatthias Ringwald return hfp_ag_features[HFP_AG_FEATURES_SIZE]; 2753deb3ec6SMatthias Ringwald } 2763deb3ec6SMatthias Ringwald return hfp_ag_features[index]; 2773deb3ec6SMatthias Ringwald } 2783deb3ec6SMatthias Ringwald 2795ebff802SMatthias Ringwald int send_str_over_rfcomm(uint16_t cid, const char * command){ 2803deb3ec6SMatthias Ringwald if (!rfcomm_can_send_packet_now(cid)) return 1; 28174386ee0SMatthias Ringwald log_info("HFP_TX %s", command); 282ab2445a0SMatthias Ringwald int err = rfcomm_send(cid, (uint8_t*) command, (uint16_t) strlen(command)); 2833deb3ec6SMatthias Ringwald if (err){ 28428190c0bSMatthias Ringwald log_error("rfcomm_send -> error 0x%02x \n", err); 2853deb3ec6SMatthias Ringwald } 286e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES 287e43d1938SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(cid); 288e43d1938SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_SENT, command); 289e43d1938SMatthias Ringwald #endif 2903deb3ec6SMatthias Ringwald return 1; 2913deb3ec6SMatthias Ringwald } 2923deb3ec6SMatthias Ringwald 2936a7f44bdSMilanka Ringwald int hfp_supports_codec(uint8_t codec, int codecs_nr, uint8_t * codecs){ 294e8d1dc0dSMatthias Ringwald 295e8d1dc0dSMatthias Ringwald // mSBC requires support for eSCO connections 296c1ab6cc1SMatthias Ringwald if ((codec == HFP_CODEC_MSBC) && !hci_extended_sco_link_supported()) return 0; 297e8d1dc0dSMatthias Ringwald 298df327ff3SMilanka Ringwald int i; 299df327ff3SMilanka Ringwald for (i = 0; i < codecs_nr; i++){ 300e8d1dc0dSMatthias Ringwald if (codecs[i] != codec) continue; 301e8d1dc0dSMatthias Ringwald return 1; 302df327ff3SMilanka Ringwald } 303df327ff3SMilanka Ringwald return 0; 304df327ff3SMilanka Ringwald } 305df327ff3SMilanka Ringwald 306d715cf51SMatthias Ringwald void hfp_hf_drop_mSBC_if_eSCO_not_supported(uint8_t * codecs, uint8_t * codecs_nr){ 307d715cf51SMatthias Ringwald if (hci_extended_sco_link_supported()) return; 308806089a7SMatthias Ringwald uint8_t i; 309806089a7SMatthias Ringwald int filtered_codec_count = 0; 310d715cf51SMatthias Ringwald for (i=0; i < *codecs_nr; i++){ 311806089a7SMatthias Ringwald if (codecs[i] != HFP_CODEC_MSBC) { 312806089a7SMatthias Ringwald codecs[filtered_codec_count++] = codecs[i]; 313d715cf51SMatthias Ringwald } 314806089a7SMatthias Ringwald } 315806089a7SMatthias Ringwald *codecs_nr = filtered_codec_count; 316d715cf51SMatthias Ringwald } 317d715cf51SMatthias Ringwald 3183deb3ec6SMatthias Ringwald // UTILS 3193deb3ec6SMatthias Ringwald int get_bit(uint16_t bitmap, int position){ 3203deb3ec6SMatthias Ringwald return (bitmap >> position) & 1; 3213deb3ec6SMatthias Ringwald } 3223deb3ec6SMatthias Ringwald 3233deb3ec6SMatthias Ringwald int store_bit(uint32_t bitmap, int position, uint8_t value){ 3243deb3ec6SMatthias Ringwald if (value){ 3253deb3ec6SMatthias Ringwald bitmap |= 1 << position; 3263deb3ec6SMatthias Ringwald } else { 3273deb3ec6SMatthias Ringwald bitmap &= ~ (1 << position); 3283deb3ec6SMatthias Ringwald } 3293deb3ec6SMatthias Ringwald return bitmap; 3303deb3ec6SMatthias Ringwald } 3313deb3ec6SMatthias Ringwald 3323deb3ec6SMatthias Ringwald int join(char * buffer, int buffer_size, uint8_t * values, int values_nr){ 333c1ab6cc1SMatthias Ringwald if (buffer_size < (values_nr * 3)) return 0; 3343deb3ec6SMatthias Ringwald int i; 3353deb3ec6SMatthias Ringwald int offset = 0; 336c1ab6cc1SMatthias Ringwald for (i = 0; i < (values_nr-1); i++) { 3373deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", values[i]); // puts string into buffer 3383deb3ec6SMatthias Ringwald } 3393deb3ec6SMatthias Ringwald if (i<values_nr){ 3403deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d", values[i]); 3413deb3ec6SMatthias Ringwald } 3423deb3ec6SMatthias Ringwald return offset; 3433deb3ec6SMatthias Ringwald } 3443deb3ec6SMatthias Ringwald 3453deb3ec6SMatthias Ringwald int join_bitmap(char * buffer, int buffer_size, uint32_t values, int values_nr){ 346c1ab6cc1SMatthias Ringwald if (buffer_size < (values_nr * 3)) return 0; 3473deb3ec6SMatthias Ringwald 3483deb3ec6SMatthias Ringwald int i; 3493deb3ec6SMatthias Ringwald int offset = 0; 350c1ab6cc1SMatthias Ringwald for (i = 0; i < (values_nr-1); i++) { 3513deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", get_bit(values,i)); // puts string into buffer 3523deb3ec6SMatthias Ringwald } 3533deb3ec6SMatthias Ringwald 3543deb3ec6SMatthias Ringwald if (i<values_nr){ 3553deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d", get_bit(values,i)); 3563deb3ec6SMatthias Ringwald } 3573deb3ec6SMatthias Ringwald return offset; 3583deb3ec6SMatthias Ringwald } 359293fae36SMatthias Ringwald static void hfp_emit_event_for_role(hfp_role_t local_role, uint8_t * packet, uint16_t size){ 360293fae36SMatthias Ringwald switch (local_role){ 361293fae36SMatthias Ringwald case HFP_ROLE_HF: 362293fae36SMatthias Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, packet, size); 363293fae36SMatthias Ringwald break; 364293fae36SMatthias Ringwald case HFP_ROLE_AG: 365293fae36SMatthias Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, packet, size); 366293fae36SMatthias Ringwald break; 367293fae36SMatthias Ringwald default: 368293fae36SMatthias Ringwald btstack_unreachable(); 369293fae36SMatthias Ringwald break; 370293fae36SMatthias Ringwald } 371293fae36SMatthias Ringwald } 3723deb3ec6SMatthias Ringwald 373ca59be51SMatthias Ringwald static void hfp_emit_event_for_context(hfp_connection_t * hfp_connection, uint8_t * packet, uint16_t size){ 374ca59be51SMatthias Ringwald if (!hfp_connection) return; 375293fae36SMatthias Ringwald hfp_emit_event_for_role(hfp_connection->local_role, packet, size); 376ca59be51SMatthias Ringwald } 377ca59be51SMatthias Ringwald 378ca59be51SMatthias Ringwald void hfp_emit_simple_event(hfp_connection_t * hfp_connection, uint8_t event_subtype){ 3797d81706fSMatthias Ringwald hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 380d703d377SMatthias Ringwald uint8_t event[5]; 381a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 382a0ffb263SMatthias Ringwald event[1] = sizeof(event) - 2; 383a0ffb263SMatthias Ringwald event[2] = event_subtype; 3847d81706fSMatthias Ringwald little_endian_store_16(event, 3, acl_handle); 385ca59be51SMatthias Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 386a0ffb263SMatthias Ringwald } 387a0ffb263SMatthias Ringwald 388ca59be51SMatthias Ringwald void hfp_emit_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, uint8_t value){ 3897d81706fSMatthias Ringwald hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 390d703d377SMatthias Ringwald uint8_t event[6]; 3913deb3ec6SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 3923deb3ec6SMatthias Ringwald event[1] = sizeof(event) - 2; 3933deb3ec6SMatthias Ringwald event[2] = event_subtype; 3947d81706fSMatthias Ringwald little_endian_store_16(event, 3, acl_handle); 395d703d377SMatthias Ringwald event[5] = value; // status 0 == OK 396ca59be51SMatthias Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 3973deb3ec6SMatthias Ringwald } 3983deb3ec6SMatthias Ringwald 399553a4a56SMilanka Ringwald void hfp_emit_voice_recognition_enabled(hfp_connection_t * hfp_connection, uint8_t status){ 400553a4a56SMilanka Ringwald btstack_assert(hfp_connection != NULL); 401553a4a56SMilanka Ringwald 402a95ec82fSMilanka Ringwald uint8_t event[7]; 403a95ec82fSMilanka Ringwald event[0] = HCI_EVENT_HFP_META; 404a95ec82fSMilanka Ringwald event[1] = sizeof(event) - 2; 405a9e632e9SMilanka Ringwald event[2] = HFP_SUBEVENT_VOICE_RECOGNITION_ACTIVATED; 406de9e0ea7SMilanka Ringwald 407553a4a56SMilanka Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 408a95ec82fSMilanka Ringwald event[5] = status; // 0:success 409553a4a56SMilanka Ringwald event[6] = hfp_connection->enhanced_voice_recognition_enabled ? 1 : 0; 410553a4a56SMilanka Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 4111a26de69SMilanka Ringwald } 412553a4a56SMilanka Ringwald 413553a4a56SMilanka Ringwald void hfp_emit_voice_recognition_disabled(hfp_connection_t * hfp_connection, uint8_t status){ 414553a4a56SMilanka Ringwald btstack_assert(hfp_connection != NULL); 415553a4a56SMilanka Ringwald 416553a4a56SMilanka Ringwald uint8_t event[6]; 417553a4a56SMilanka Ringwald event[0] = HCI_EVENT_HFP_META; 418553a4a56SMilanka Ringwald event[1] = sizeof(event) - 2; 419a9e632e9SMilanka Ringwald event[2] = HFP_SUBEVENT_VOICE_RECOGNITION_DEACTIVATED; 420553a4a56SMilanka Ringwald 421553a4a56SMilanka Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 422553a4a56SMilanka Ringwald event[5] = status; // 0:success 423013cc750SMilanka Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 424013cc750SMilanka Ringwald } 425013cc750SMilanka Ringwald 426de9e0ea7SMilanka Ringwald void hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection_t * hfp_connection, uint8_t status){ 427de9e0ea7SMilanka Ringwald hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 428de9e0ea7SMilanka Ringwald 429de9e0ea7SMilanka Ringwald uint8_t event[6]; 430de9e0ea7SMilanka Ringwald event[0] = HCI_EVENT_HFP_META; 431de9e0ea7SMilanka Ringwald event[1] = sizeof(event) - 2; 432de9e0ea7SMilanka Ringwald event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_HF_READY_FOR_AUDIO; 433de9e0ea7SMilanka Ringwald little_endian_store_16(event, 3, acl_handle); 434de9e0ea7SMilanka Ringwald event[5] = status; 435de9e0ea7SMilanka Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 436de9e0ea7SMilanka Ringwald } 437de9e0ea7SMilanka Ringwald 438cf75be85SMilanka Ringwald void hfp_emit_enhanced_voice_recognition_state_event(hfp_connection_t * hfp_connection, uint8_t status){ 439cf75be85SMilanka Ringwald hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 440cf75be85SMilanka Ringwald 441cf75be85SMilanka Ringwald uint8_t event[6]; 442cf75be85SMilanka Ringwald event[0] = HCI_EVENT_HFP_META; 443cf75be85SMilanka Ringwald event[1] = sizeof(event) - 2; 444cf75be85SMilanka Ringwald switch (hfp_connection->ag_vra_state){ 445cf75be85SMilanka Ringwald case HFP_VOICE_RECOGNITION_STATE_AG_READY_TO_ACCEPT_AUDIO_INPUT: 446cf75be85SMilanka Ringwald event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_READY_TO_ACCEPT_AUDIO_INPUT; 447cf75be85SMilanka Ringwald break; 448cf75be85SMilanka Ringwald case HFP_VOICE_RECOGNITION_STATE_AG_IS_STARTING_SOUND: 449cf75be85SMilanka Ringwald event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_STARTING_SOUND; 450cf75be85SMilanka Ringwald break; 451cf75be85SMilanka Ringwald case HFP_VOICE_RECOGNITION_STATE_AG_IS_PROCESSING_AUDIO_INPUT: 452cf75be85SMilanka Ringwald event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_PROCESSING_AUDIO_INPUT; 453cf75be85SMilanka Ringwald break; 454cf75be85SMilanka Ringwald default: 455cf75be85SMilanka Ringwald btstack_unreachable(); 456cf75be85SMilanka Ringwald break; 457cf75be85SMilanka Ringwald } 458cf75be85SMilanka Ringwald 459cf75be85SMilanka Ringwald little_endian_store_16(event, 3, acl_handle); 460cf75be85SMilanka Ringwald event[5] = status; 461cf75be85SMilanka Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 462cf75be85SMilanka Ringwald } 463cf75be85SMilanka Ringwald 4647095467fSMatthias Ringwald void hfp_emit_slc_connection_event(hfp_role_t local_role, uint8_t status, hci_con_handle_t con_handle, bd_addr_t addr){ 4658901a7c4SMatthias Ringwald uint8_t event[12]; 4666a7f44bdSMilanka Ringwald int pos = 0; 4676a7f44bdSMilanka Ringwald event[pos++] = HCI_EVENT_HFP_META; 4686a7f44bdSMilanka Ringwald event[pos++] = sizeof(event) - 2; 469d0c4aea6SMilanka Ringwald event[pos++] = HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 4706a7f44bdSMilanka Ringwald little_endian_store_16(event, pos, con_handle); 4716a7f44bdSMilanka Ringwald pos += 2; 47203870092SMilanka Ringwald event[pos++] = status; // status 0 == OK 4736a7f44bdSMilanka Ringwald reverse_bd_addr(addr,&event[pos]); 4746a7f44bdSMilanka Ringwald pos += 6; 4757095467fSMatthias Ringwald hfp_emit_event_for_role(local_role, event, sizeof(event)); 476a0653c3bSMilanka Ringwald } 477a0653c3bSMilanka Ringwald 478d703d377SMatthias Ringwald static void hfp_emit_audio_connection_released(hfp_connection_t * hfp_connection, hci_con_handle_t sco_handle){ 4797d81706fSMatthias Ringwald btstack_assert(hfp_connection != NULL); 480d703d377SMatthias Ringwald uint8_t event[7]; 4815441d52fSMatthias Ringwald int pos = 0; 4825441d52fSMatthias Ringwald event[pos++] = HCI_EVENT_HFP_META; 4835441d52fSMatthias Ringwald event[pos++] = sizeof(event) - 2; 4845441d52fSMatthias Ringwald event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED; 485d703d377SMatthias Ringwald little_endian_store_16(event, pos, hfp_connection->acl_handle); 486d703d377SMatthias Ringwald pos += 2; 487d703d377SMatthias Ringwald little_endian_store_16(event, pos, sco_handle); 488d703d377SMatthias Ringwald pos += 2; 4895441d52fSMatthias Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 4905441d52fSMatthias Ringwald } 4915441d52fSMatthias Ringwald 492f14c5dafSMatthias Ringwald void hfp_emit_sco_connection_established(hfp_connection_t *hfp_connection, uint8_t status, uint8_t negotiated_codec, 4935e8e3664SMatthias Ringwald uint16_t rx_packet_length, uint16_t tx_packet_length) { 4947d81706fSMatthias Ringwald btstack_assert(hfp_connection != NULL); 495c8149263SMatthias Ringwald uint8_t event[21]; 496d0c4aea6SMilanka Ringwald int pos = 0; 497d0c4aea6SMilanka Ringwald event[pos++] = HCI_EVENT_HFP_META; 498d0c4aea6SMilanka Ringwald event[pos++] = sizeof(event) - 2; 499d0c4aea6SMilanka Ringwald event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED; 500d703d377SMatthias Ringwald little_endian_store_16(event, pos, hfp_connection->acl_handle); 501d703d377SMatthias Ringwald pos += 2; 502d0c4aea6SMilanka Ringwald event[pos++] = status; // status 0 == OK 503f14c5dafSMatthias Ringwald little_endian_store_16(event, pos, hfp_connection->sco_handle); 504d0c4aea6SMilanka Ringwald pos += 2; 505f14c5dafSMatthias Ringwald reverse_bd_addr(hfp_connection->remote_addr,&event[pos]); 506d0c4aea6SMilanka Ringwald pos += 6; 507d0c4aea6SMilanka Ringwald event[pos++] = negotiated_codec; 5085e8e3664SMatthias Ringwald little_endian_store_16(event, pos, hfp_connection->packet_types); 5091a9e9d0bSMatthias Ringwald pos += 2; 510c8149263SMatthias Ringwald little_endian_store_16(event, pos, rx_packet_length); 511c8149263SMatthias Ringwald pos += 2; 512c8149263SMatthias Ringwald little_endian_store_16(event, pos, tx_packet_length); 513c8149263SMatthias Ringwald pos += 2; 514ca59be51SMatthias Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 515d0c4aea6SMilanka Ringwald } 516d0c4aea6SMilanka Ringwald 517ca59be51SMatthias Ringwald void hfp_emit_string_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, const char * value){ 5187d81706fSMatthias Ringwald btstack_assert(hfp_connection != NULL); 5192c50df93SBjoern Hartmann #ifdef ENABLE_HFP_AT_MESSAGES 5202c50df93SBjoern Hartmann uint8_t event[256]; 5212c50df93SBjoern Hartmann #else 522c1797c7dSMatthias Ringwald uint8_t event[40]; 5232c50df93SBjoern Hartmann #endif 524ab2445a0SMatthias Ringwald uint16_t string_len = btstack_min((uint16_t) strlen(value), sizeof(event) - 6); 525aa4dd815SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 526c10fde09SMatthias Ringwald event[1] = 4 + string_len; 527aa4dd815SMatthias Ringwald event[2] = event_subtype; 528d703d377SMatthias Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 529c10fde09SMatthias Ringwald memcpy((char*)&event[5], value, string_len); 530c10fde09SMatthias Ringwald event[5 + string_len] = 0; 531c10fde09SMatthias Ringwald hfp_emit_event_for_context(hfp_connection, event, 6 + string_len); 532aa4dd815SMatthias Ringwald } 533aa4dd815SMatthias Ringwald 5340cb5b971SMatthias Ringwald btstack_linked_list_t * hfp_get_connections(void){ 5358f2a52f4SMatthias Ringwald return (btstack_linked_list_t *) &hfp_connections; 5363deb3ec6SMatthias Ringwald } 5373deb3ec6SMatthias Ringwald 5383deb3ec6SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid){ 539665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 540665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 541665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 542a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 543a0ffb263SMatthias Ringwald if (hfp_connection->rfcomm_cid == cid){ 544a0ffb263SMatthias Ringwald return hfp_connection; 5453deb3ec6SMatthias Ringwald } 5463deb3ec6SMatthias Ringwald } 5473deb3ec6SMatthias Ringwald return NULL; 5483deb3ec6SMatthias Ringwald } 5493deb3ec6SMatthias Ringwald 550405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr, hfp_role_t hfp_role){ 551665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 552665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 553665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 554a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 5555df9dc78SMatthias Ringwald if ((memcmp(hfp_connection->remote_addr, bd_addr, 6) == 0) && (hfp_connection->local_role == hfp_role)) { 556a0ffb263SMatthias Ringwald return hfp_connection; 5573deb3ec6SMatthias Ringwald } 5583deb3ec6SMatthias Ringwald } 5593deb3ec6SMatthias Ringwald return NULL; 5603deb3ec6SMatthias Ringwald } 5613deb3ec6SMatthias Ringwald 562405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_sco_handle(uint16_t handle, hfp_role_t hfp_role){ 563665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 564665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 565665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 566a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 567c1ab6cc1SMatthias Ringwald if ((hfp_connection->sco_handle == handle) && (hfp_connection->local_role == hfp_role)){ 568a0ffb263SMatthias Ringwald return hfp_connection; 5693deb3ec6SMatthias Ringwald } 5703deb3ec6SMatthias Ringwald } 5713deb3ec6SMatthias Ringwald return NULL; 5723deb3ec6SMatthias Ringwald } 5733deb3ec6SMatthias Ringwald 574405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_acl_handle(uint16_t handle, hfp_role_t hfp_role){ 575d97d752dSMilanka Ringwald btstack_linked_list_iterator_t it; 576d97d752dSMilanka Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 577d97d752dSMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 578d97d752dSMilanka Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 579c1ab6cc1SMatthias Ringwald if ((hfp_connection->acl_handle == handle) && (hfp_connection->local_role == hfp_role)){ 580d97d752dSMilanka Ringwald return hfp_connection; 581d97d752dSMilanka Ringwald } 582d97d752dSMilanka Ringwald } 583d97d752dSMilanka Ringwald return NULL; 584d97d752dSMilanka Ringwald } 585d97d752dSMilanka Ringwald 586c95b5b3cSMilanka Ringwald 587c95b5b3cSMilanka Ringwald static void hfp_reset_voice_recognition(hfp_connection_t * hfp_connection){ 588c95b5b3cSMilanka Ringwald btstack_assert(hfp_connection != NULL); 589c95b5b3cSMilanka Ringwald hfp_voice_recognition_activation_status_t current_vra_state = hfp_connection->vra_state; 590c95b5b3cSMilanka Ringwald hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF; 591c95b5b3cSMilanka Ringwald 592c95b5b3cSMilanka Ringwald if (current_vra_state != HFP_VRA_VOICE_RECOGNITION_OFF){ 593553a4a56SMilanka Ringwald hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS); 594c95b5b3cSMilanka Ringwald } else if (hfp_connection->vra_state_requested != HFP_VRA_VOICE_RECOGNITION_OFF){ 595553a4a56SMilanka Ringwald hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS); 596c95b5b3cSMilanka Ringwald } 597c95b5b3cSMilanka Ringwald 598c95b5b3cSMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_VOICE_RECOGNITION_OFF; 599c95b5b3cSMilanka Ringwald hfp_connection->activate_voice_recognition = false; 600c95b5b3cSMilanka Ringwald hfp_connection->deactivate_voice_recognition = false; 601c95b5b3cSMilanka Ringwald hfp_connection->enhanced_voice_recognition_enabled = false; 602c95b5b3cSMilanka Ringwald hfp_connection->ag_vra_status = 0; 603c95b5b3cSMilanka Ringwald hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY; 604c95b5b3cSMilanka Ringwald } 605c95b5b3cSMilanka Ringwald 606a0ffb263SMatthias Ringwald void hfp_reset_context_flags(hfp_connection_t * hfp_connection){ 607a0ffb263SMatthias Ringwald if (!hfp_connection) return; 608a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 609a0ffb263SMatthias Ringwald hfp_connection->send_error = 0; 6103deb3ec6SMatthias Ringwald 611dd533208SMatthias Ringwald hfp_connection->found_equal_sign = false; 6123deb3ec6SMatthias Ringwald 613a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 0; 614a0ffb263SMatthias Ringwald hfp_connection->operator_name_changed = 0; 6153deb3ec6SMatthias Ringwald 616a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = 0; 617a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 6183deb3ec6SMatthias Ringwald 619a0ffb263SMatthias Ringwald // establish codecs hfp_connection 620a0ffb263SMatthias Ringwald hfp_connection->suggested_codec = 0; 6217522e673SMatthias Ringwald hfp_connection->negotiated_codec = 0; 622a0ffb263SMatthias Ringwald hfp_connection->codec_confirmed = 0; 6233deb3ec6SMatthias Ringwald 624a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 625a0ffb263SMatthias Ringwald hfp_connection->call_waiting_notification_enabled = 0; 626a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 627a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 628125560b8SMatthias Ringwald hfp_connection->clip_have_alpha = false; 629c95b5b3cSMilanka Ringwald hfp_reset_voice_recognition(hfp_connection); 6303deb3ec6SMatthias Ringwald } 6313deb3ec6SMatthias Ringwald 632fffdd288SMatthias Ringwald static hfp_connection_t * create_hfp_connection_context(void){ 633a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = btstack_memory_hfp_connection_get(); 634a0ffb263SMatthias Ringwald if (!hfp_connection) return NULL; 6353deb3ec6SMatthias Ringwald 636a0ffb263SMatthias Ringwald hfp_connection->state = HFP_IDLE; 637a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 638a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_IDLE; 639aa4dd815SMatthias Ringwald 640a0ffb263SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 6413deb3ec6SMatthias Ringwald 642d751f069SMatthias Ringwald hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID; 643d751f069SMatthias Ringwald hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID; 644d751f069SMatthias Ringwald 645a0ffb263SMatthias Ringwald hfp_reset_context_flags(hfp_connection); 6463deb3ec6SMatthias Ringwald 647d63c37a1SMatthias Ringwald btstack_linked_list_add(&hfp_connections, (btstack_linked_item_t*)hfp_connection); 648a0ffb263SMatthias Ringwald return hfp_connection; 6493deb3ec6SMatthias Ringwald } 6503deb3ec6SMatthias Ringwald 65148e6eeeeSMatthias Ringwald void hfp_finalize_connection_context(hfp_connection_t * hfp_connection){ 652a0ffb263SMatthias Ringwald btstack_linked_list_remove(&hfp_connections, (btstack_linked_item_t*) hfp_connection); 65309a233a1SMatthias Ringwald btstack_memory_hfp_connection_free(hfp_connection); 6543deb3ec6SMatthias Ringwald } 6553deb3ec6SMatthias Ringwald 6564eb3f1d8SMilanka Ringwald static hfp_connection_t * hfp_create_connection(bd_addr_t bd_addr, hfp_role_t local_role){ 657405014fbSMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr, local_role); 658a0ffb263SMatthias Ringwald if (hfp_connection) return hfp_connection; 659a0ffb263SMatthias Ringwald hfp_connection = create_hfp_connection_context(); 660ea7d4f28SPeter Harper if (!hfp_connection) { 661ea7d4f28SPeter Harper return NULL; 662ea7d4f28SPeter Harper } 6636535961aSMatthias Ringwald (void)memcpy(hfp_connection->remote_addr, bd_addr, 6); 664323d3000SMatthias Ringwald hfp_connection->local_role = local_role; 665bdf572f4SMatthias Ringwald log_info("Create HFP context %p: role %u, addr %s", hfp_connection, local_role, bd_addr_to_str(bd_addr)); 666bdf572f4SMatthias Ringwald 6675fd6f360SMatthias Ringwald #ifdef ENABLE_NXP_PCM_WBS 6685fd6f360SMatthias Ringwald hfp_connection->nxp_start_audio_handle = HCI_CON_HANDLE_INVALID; 6695fd6f360SMatthias Ringwald hfp_connection->nxp_stop_audio_handle = HCI_CON_HANDLE_INVALID; 6705fd6f360SMatthias Ringwald #endif 6715fd6f360SMatthias Ringwald 672a0ffb263SMatthias Ringwald return hfp_connection; 6733deb3ec6SMatthias Ringwald } 6743deb3ec6SMatthias Ringwald 675aa4dd815SMatthias Ringwald /* @param network. 676aa4dd815SMatthias Ringwald * 0 == no ability to reject a call. 677aa4dd815SMatthias Ringwald * 1 == ability to reject a call. 678aa4dd815SMatthias Ringwald */ 6793deb3ec6SMatthias Ringwald 6803deb3ec6SMatthias Ringwald /* @param suported_features 6813deb3ec6SMatthias Ringwald * HF bit 0: EC and/or NR function (yes/no, 1 = yes, 0 = no) 6823deb3ec6SMatthias Ringwald * HF bit 1: Call waiting or three-way calling(yes/no, 1 = yes, 0 = no) 6833deb3ec6SMatthias Ringwald * HF bit 2: CLI presentation capability (yes/no, 1 = yes, 0 = no) 6843deb3ec6SMatthias Ringwald * HF bit 3: Voice recognition activation (yes/no, 1= yes, 0 = no) 6853deb3ec6SMatthias Ringwald * HF bit 4: Remote volume control (yes/no, 1 = yes, 0 = no) 6863deb3ec6SMatthias Ringwald * HF bit 5: Wide band speech (yes/no, 1 = yes, 0 = no) 6873deb3ec6SMatthias Ringwald */ 6883deb3ec6SMatthias Ringwald /* Bit position: 6893deb3ec6SMatthias Ringwald * AG bit 0: Three-way calling (yes/no, 1 = yes, 0 = no) 6903deb3ec6SMatthias Ringwald * AG bit 1: EC and/or NR function (yes/no, 1 = yes, 0 = no) 6913deb3ec6SMatthias Ringwald * AG bit 2: Voice recognition function (yes/no, 1 = yes, 0 = no) 6923deb3ec6SMatthias Ringwald * AG bit 3: In-band ring tone capability (yes/no, 1 = yes, 0 = no) 6933deb3ec6SMatthias Ringwald * AG bit 4: Attach a phone number to a voice tag (yes/no, 1 = yes, 0 = no) 6943deb3ec6SMatthias Ringwald * AG bit 5: Wide band speech (yes/no, 1 = yes, 0 = no) 6953deb3ec6SMatthias Ringwald */ 6963deb3ec6SMatthias Ringwald 6979b1c3b4dSMatthias Ringwald void hfp_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint16_t service_uuid, int rfcomm_channel_nr, const char * name){ 6983deb3ec6SMatthias Ringwald uint8_t* attribute; 6993deb3ec6SMatthias Ringwald de_create_sequence(service); 7003deb3ec6SMatthias Ringwald 7013deb3ec6SMatthias Ringwald // 0x0000 "Service Record Handle" 702235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE); 7039b1c3b4dSMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle); 7043deb3ec6SMatthias Ringwald 7053deb3ec6SMatthias Ringwald // 0x0001 "Service Class ID List" 706235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST); 7073deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 7083deb3ec6SMatthias Ringwald { 7093deb3ec6SMatthias Ringwald // "UUID for Service" 7103deb3ec6SMatthias Ringwald de_add_number(attribute, DE_UUID, DE_SIZE_16, service_uuid); 711235946f1SMatthias Ringwald de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_GENERIC_AUDIO); 7123deb3ec6SMatthias Ringwald } 7133deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 7143deb3ec6SMatthias Ringwald 7153deb3ec6SMatthias Ringwald // 0x0004 "Protocol Descriptor List" 716235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST); 7173deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 7183deb3ec6SMatthias Ringwald { 7193deb3ec6SMatthias Ringwald uint8_t* l2cpProtocol = de_push_sequence(attribute); 7203deb3ec6SMatthias Ringwald { 721235946f1SMatthias Ringwald de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP); 7223deb3ec6SMatthias Ringwald } 7233deb3ec6SMatthias Ringwald de_pop_sequence(attribute, l2cpProtocol); 7243deb3ec6SMatthias Ringwald 7253deb3ec6SMatthias Ringwald uint8_t* rfcomm = de_push_sequence(attribute); 7263deb3ec6SMatthias Ringwald { 727235946f1SMatthias Ringwald de_add_number(rfcomm, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_RFCOMM); // rfcomm_service 7283deb3ec6SMatthias Ringwald de_add_number(rfcomm, DE_UINT, DE_SIZE_8, rfcomm_channel_nr); // rfcomm channel 7293deb3ec6SMatthias Ringwald } 7303deb3ec6SMatthias Ringwald de_pop_sequence(attribute, rfcomm); 7313deb3ec6SMatthias Ringwald } 7323deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 7333deb3ec6SMatthias Ringwald 7343deb3ec6SMatthias Ringwald 7353deb3ec6SMatthias Ringwald // 0x0005 "Public Browse Group" 736235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group 7373deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 7383deb3ec6SMatthias Ringwald { 739235946f1SMatthias Ringwald de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT); 7403deb3ec6SMatthias Ringwald } 7413deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 7423deb3ec6SMatthias Ringwald 7433deb3ec6SMatthias Ringwald // 0x0009 "Bluetooth Profile Descriptor List" 744235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST); 7453deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 7463deb3ec6SMatthias Ringwald { 7473deb3ec6SMatthias Ringwald uint8_t *sppProfile = de_push_sequence(attribute); 7483deb3ec6SMatthias Ringwald { 749235946f1SMatthias Ringwald de_add_number(sppProfile, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_HANDSFREE); 7502eb5c290SMatthias Ringwald de_add_number(sppProfile, DE_UINT, DE_SIZE_16, 0x0109); // Version 1.9 7513deb3ec6SMatthias Ringwald } 7523deb3ec6SMatthias Ringwald de_pop_sequence(attribute, sppProfile); 7533deb3ec6SMatthias Ringwald } 7543deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 7553deb3ec6SMatthias Ringwald 7563deb3ec6SMatthias Ringwald // 0x0100 "Service Name" 757ada240cdSMatthias Ringwald if (strlen(name) > 0){ 7583deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); 759ab2445a0SMatthias Ringwald de_add_data(service, DE_STRING, (uint16_t) strlen(name), (uint8_t *) name); 7603deb3ec6SMatthias Ringwald } 761ada240cdSMatthias Ringwald } 7623deb3ec6SMatthias Ringwald 7637dceaecaSMatthias Ringwald static void hfp_handle_slc_setup_error(hfp_connection_t * hfp_connection, uint8_t status){ 7647dceaecaSMatthias Ringwald // cache fields for event 7657dceaecaSMatthias Ringwald hfp_role_t local_role = hfp_connection->local_role; 7667dceaecaSMatthias Ringwald bd_addr_t remote_addr; 767d5529700SMatthias Ringwald // cppcheck-suppress uninitvar ; remote_addr is reported as uninitialized although it's the destination of the memcpy 768481c0cbcSMatthias Ringwald (void)memcpy(remote_addr, hfp_connection->remote_addr, 6); 7697dceaecaSMatthias Ringwald // finalize connection struct 7707dceaecaSMatthias Ringwald hfp_finalize_connection_context(hfp_connection); 7717dceaecaSMatthias Ringwald // emit event 7727dceaecaSMatthias Ringwald hfp_emit_slc_connection_event(local_role, status, HCI_CON_HANDLE_INVALID, remote_addr); 7737dceaecaSMatthias Ringwald } 7747dceaecaSMatthias Ringwald 7756c927b22SMatthias Ringwald static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 7768a46ec40SMatthias Ringwald UNUSED(packet_type); // ok: handling own sdp events 7778a46ec40SMatthias Ringwald UNUSED(channel); // ok: no channel 7788a46ec40SMatthias Ringwald UNUSED(size); // ok: handling own sdp events 7791d9c9c90SMilanka Ringwald 780aeb0f0feSMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(hfp_sdp_query_context.remote_address, hfp_sdp_query_context.local_role); 7811d9c9c90SMilanka Ringwald if (hfp_connection == NULL) { 782aeb0f0feSMatthias Ringwald log_info("connection with %s and local role %d not found", hfp_sdp_query_context.remote_address, hfp_sdp_query_context.local_role); 78384904e95SMilanka Ringwald return; 78484904e95SMilanka Ringwald } 7853deb3ec6SMatthias Ringwald 7860e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 7875611a760SMatthias Ringwald case SDP_EVENT_QUERY_RFCOMM_SERVICE: 788a0ffb263SMatthias Ringwald hfp_connection->rfcomm_channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet); 7893deb3ec6SMatthias Ringwald break; 7905611a760SMatthias Ringwald case SDP_EVENT_QUERY_COMPLETE: 791a0ffb263SMatthias Ringwald if (hfp_connection->rfcomm_channel_nr > 0){ 792a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_CONNECTED; 793520c92d5SMatthias Ringwald btstack_packet_handler_t packet_handler; 794520c92d5SMatthias Ringwald switch (hfp_connection->local_role){ 795520c92d5SMatthias Ringwald case HFP_ROLE_AG: 796520c92d5SMatthias Ringwald packet_handler = hfp_ag_rfcomm_packet_handler; 797520c92d5SMatthias Ringwald break; 798520c92d5SMatthias Ringwald case HFP_ROLE_HF: 799520c92d5SMatthias Ringwald packet_handler = hfp_hf_rfcomm_packet_handler; 800520c92d5SMatthias Ringwald break; 801520c92d5SMatthias Ringwald default: 8021d9c9c90SMilanka Ringwald btstack_assert(false); 803520c92d5SMatthias Ringwald return; 804520c92d5SMatthias Ringwald } 8051d9c9c90SMilanka Ringwald 806ca59be51SMatthias Ringwald rfcomm_create_channel(packet_handler, hfp_connection->remote_addr, hfp_connection->rfcomm_channel_nr, NULL); 8071d9c9c90SMilanka Ringwald 8081d9c9c90SMilanka Ringwald } else { 809cc92f22bSMatthias Ringwald uint8_t status = sdp_event_query_complete_get_status(packet); 810cc92f22bSMatthias Ringwald if (status == ERROR_CODE_SUCCESS){ 811cc92f22bSMatthias Ringwald // report service not found 812cc92f22bSMatthias Ringwald status = SDP_SERVICE_NOT_FOUND; 813cc92f22bSMatthias Ringwald } 8147dceaecaSMatthias Ringwald hfp_handle_slc_setup_error(hfp_connection, status); 815cc92f22bSMatthias Ringwald log_info("rfcomm service not found, status 0x%02x", status); 8161d9c9c90SMilanka Ringwald } 8171d9c9c90SMilanka Ringwald 8181d9c9c90SMilanka Ringwald // register the SDP Query request to check if there is another connection waiting for the query 8191d9c9c90SMilanka Ringwald // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback 820aeb0f0feSMatthias Ringwald (void) sdp_client_register_query_callback(&hfp_sdp_query_request); 8213deb3ec6SMatthias Ringwald break; 8223deb3ec6SMatthias Ringwald default: 8233deb3ec6SMatthias Ringwald break; 8243deb3ec6SMatthias Ringwald } 8253deb3ec6SMatthias Ringwald } 8263deb3ec6SMatthias Ringwald 82738200c1dSMilanka Ringwald // returns 0 if unexpected error or no other link options remained, otherwise 1 82838200c1dSMilanka Ringwald static int hfp_handle_failed_sco_connection(uint8_t status){ 829eddcd308SMatthias Ringwald 83030e5ec75SMatthias Ringwald if (hfp_sco_establishment_active->accept_sco != 0){ 8313427dd75SMatthias Ringwald log_info("(e)SCO Connection failed but not started by us"); 83238200c1dSMilanka Ringwald return 0; 833eddcd308SMatthias Ringwald } 834eddcd308SMatthias Ringwald 8353427dd75SMatthias Ringwald log_info("(e)SCO Connection failed 0x%02x", status); 8363427dd75SMatthias Ringwald switch (status){ 83733c74df1SMatthias Ringwald case ERROR_CODE_INVALID_LMP_PARAMETERS_INVALID_LL_PARAMETERS: 8381579b82fSMatthias Ringwald case ERROR_CODE_SCO_AIR_MODE_REJECTED: 8391579b82fSMatthias Ringwald case ERROR_CODE_SCO_INTERVAL_REJECTED: 8401579b82fSMatthias Ringwald case ERROR_CODE_SCO_OFFSET_REJECTED: 8413427dd75SMatthias Ringwald case ERROR_CODE_UNSPECIFIED_ERROR: 8421579b82fSMatthias Ringwald case ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE: 84369a0db57SMatthias Ringwald case ERROR_CODE_UNSUPPORTED_REMOTE_FEATURE_UNSUPPORTED_LMP_FEATURE: 844eddcd308SMatthias Ringwald break; 8453427dd75SMatthias Ringwald default: 8463427dd75SMatthias Ringwald return 0; 8473427dd75SMatthias Ringwald } 8483427dd75SMatthias Ringwald 8493427dd75SMatthias Ringwald // note: eSCO_S4 supported flag not available, but it's only relevant for highest CVSD link setting (and the current failed) 850aeb0f0feSMatthias Ringwald hfp_link_settings_t next_setting = hfp_next_link_setting_for_connection(hfp_sco_establishment_active->link_setting, hfp_sco_establishment_active, false); 8513427dd75SMatthias Ringwald 8523427dd75SMatthias Ringwald // handle no valid setting found 8533427dd75SMatthias Ringwald if (next_setting == HFP_LINK_SETTINGS_NONE) { 854aeb0f0feSMatthias Ringwald if (hfp_sco_establishment_active->negotiated_codec == HFP_CODEC_MSBC){ 8553427dd75SMatthias Ringwald log_info("T2/T1 failed, fallback to CVSD - D1"); 856aeb0f0feSMatthias Ringwald hfp_sco_establishment_active->negotiated_codec = HFP_CODEC_CVSD; 857aeb0f0feSMatthias Ringwald hfp_sco_establishment_active->sco_for_msbc_failed = 1; 858a814da6aSMatthias Ringwald hfp_sco_establishment_active->ag_send_common_codec = true; 859aeb0f0feSMatthias Ringwald hfp_sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D1; 8603427dd75SMatthias Ringwald } else { 8613427dd75SMatthias Ringwald // no other options 8623427dd75SMatthias Ringwald return 0; 863eddcd308SMatthias Ringwald } 8643427dd75SMatthias Ringwald } 8653427dd75SMatthias Ringwald 8663427dd75SMatthias Ringwald log_info("e)SCO Connection: try new link_setting %d", next_setting); 867aeb0f0feSMatthias Ringwald hfp_sco_establishment_active->establish_audio_connection = 1; 868aeb0f0feSMatthias Ringwald hfp_sco_establishment_active->link_setting = next_setting; 869677003cbSMatthias Ringwald hfp_sco_establishment_active->accept_sco = 0; 870aeb0f0feSMatthias Ringwald hfp_sco_establishment_active = NULL; 87138200c1dSMilanka Ringwald return 1; 872eddcd308SMatthias Ringwald } 873eddcd308SMatthias Ringwald 874405014fbSMatthias Ringwald void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){ 8755575558eSMilanka Ringwald UNUSED(packet_type); 8768a46ec40SMatthias Ringwald UNUSED(channel); // ok: no channel 8775575558eSMilanka Ringwald UNUSED(size); 8789ec2630cSMatthias Ringwald 8793deb3ec6SMatthias Ringwald bd_addr_t event_addr; 88027950165SMatthias Ringwald hci_con_handle_t handle; 881a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = NULL; 882674515e8SMatthias Ringwald uint8_t status; 8833deb3ec6SMatthias Ringwald 88427950165SMatthias Ringwald log_debug("HFP HCI event handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size); 885aa4dd815SMatthias Ringwald 8860e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 8871b005648SMatthias Ringwald 888011577abSMilanka Ringwald case HCI_EVENT_CONNECTION_REQUEST: 8897522e673SMatthias Ringwald switch(hci_event_connection_request_get_link_type(packet)){ 8907522e673SMatthias Ringwald case 0: // SCO 8917522e673SMatthias Ringwald case 2: // eSCO 892011577abSMilanka Ringwald hci_event_connection_request_get_bd_addr(packet, event_addr); 893405014fbSMatthias Ringwald hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role); 894011577abSMilanka Ringwald if (!hfp_connection) break; 895c169df2fSMatthias Ringwald if (hci_event_connection_request_get_link_type(packet) == 2){ 896cb81d35dSMatthias Ringwald hfp_connection->accept_sco = 2; 897c169df2fSMatthias Ringwald } else { 898cb81d35dSMatthias Ringwald hfp_connection->accept_sco = 1; 899c169df2fSMatthias Ringwald } 9000327df7dSMatthias Ringwald // configure SBC coded if needed 9010327df7dSMatthias Ringwald hfp_prepare_for_sco(hfp_connection); 902cb81d35dSMatthias Ringwald log_info("accept sco %u\n", hfp_connection->accept_sco); 903202c8a4cSMatthias Ringwald break; 9047522e673SMatthias Ringwald default: 9057522e673SMatthias Ringwald break; 9067522e673SMatthias Ringwald } 907011577abSMilanka Ringwald break; 9083deb3ec6SMatthias Ringwald 909eddcd308SMatthias Ringwald case HCI_EVENT_COMMAND_STATUS: 910eddcd308SMatthias Ringwald if (hci_event_command_status_get_command_opcode(packet) == hci_setup_synchronous_connection.opcode) { 911aeb0f0feSMatthias Ringwald if (hfp_sco_establishment_active == NULL) break; 912be2a1a78SMatthias Ringwald status = hci_event_command_status_get_status(packet); 91338200c1dSMilanka Ringwald if (status == ERROR_CODE_SUCCESS) break; 91438200c1dSMilanka Ringwald 915aeb0f0feSMatthias Ringwald hfp_connection = hfp_sco_establishment_active; 91638200c1dSMilanka Ringwald if (hfp_handle_failed_sco_connection(status)) break; 917677003cbSMatthias Ringwald 918677003cbSMatthias Ringwald hfp_connection->accept_sco = 0; 91938200c1dSMilanka Ringwald hfp_connection->establish_audio_connection = 0; 92038200c1dSMilanka Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 921447743f7SMatthias Ringwald hfp_sco_establishment_active = NULL; 922f14c5dafSMatthias Ringwald hfp_emit_sco_connection_established(hfp_connection, status, 9235e8e3664SMatthias Ringwald hfp_connection->negotiated_codec, 0, 0); 9246c5b2002SMatthias Ringwald } 925eddcd308SMatthias Ringwald break; 926eddcd308SMatthias Ringwald 9273deb3ec6SMatthias Ringwald case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{ 928aeb0f0feSMatthias Ringwald if (hfp_sco_establishment_active == NULL) break; 929447743f7SMatthias Ringwald 930447743f7SMatthias Ringwald hfp_connection = hfp_sco_establishment_active; 931ce263fc8SMatthias Ringwald 932011577abSMilanka Ringwald status = hci_event_synchronous_connection_complete_get_status(packet); 93338200c1dSMilanka Ringwald if (status != ERROR_CODE_SUCCESS){ 93438200c1dSMilanka Ringwald if (hfp_handle_failed_sco_connection(status)) break; 93538200c1dSMilanka Ringwald 936677003cbSMatthias Ringwald hfp_connection->accept_sco = 0; 93738200c1dSMilanka Ringwald hfp_connection->establish_audio_connection = 0; 93838200c1dSMilanka Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 939447743f7SMatthias Ringwald hfp_sco_establishment_active = NULL; 940f14c5dafSMatthias Ringwald hfp_emit_sco_connection_established(hfp_connection, status, 9415e8e3664SMatthias Ringwald hfp_connection->negotiated_codec, 0, 0); 942aa4dd815SMatthias Ringwald break; 943aa4dd815SMatthias Ringwald } 944aa4dd815SMatthias Ringwald 945011577abSMilanka Ringwald uint16_t sco_handle = hci_event_synchronous_connection_complete_get_handle(packet); 946011577abSMilanka Ringwald uint8_t link_type = hci_event_synchronous_connection_complete_get_link_type(packet); 947011577abSMilanka Ringwald uint8_t transmission_interval = hci_event_synchronous_connection_complete_get_transmission_interval(packet); // measured in slots 948011577abSMilanka Ringwald uint8_t retransmission_interval = hci_event_synchronous_connection_complete_get_retransmission_interval(packet);// measured in slots 949011577abSMilanka Ringwald uint16_t rx_packet_length = hci_event_synchronous_connection_complete_get_rx_packet_length(packet); // measured in bytes 950011577abSMilanka Ringwald uint16_t tx_packet_length = hci_event_synchronous_connection_complete_get_tx_packet_length(packet); // measured in bytes 9513deb3ec6SMatthias Ringwald 9523deb3ec6SMatthias Ringwald switch (link_type){ 9533deb3ec6SMatthias Ringwald case 0x00: 954aa4dd815SMatthias Ringwald log_info("SCO Connection established."); 9553deb3ec6SMatthias Ringwald if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval); 9563deb3ec6SMatthias Ringwald if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval); 9573deb3ec6SMatthias Ringwald if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length); 9583deb3ec6SMatthias Ringwald if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length); 9593deb3ec6SMatthias Ringwald break; 9603deb3ec6SMatthias Ringwald case 0x02: 961aa4dd815SMatthias Ringwald log_info("eSCO Connection established. \n"); 9623deb3ec6SMatthias Ringwald break; 9633deb3ec6SMatthias Ringwald default: 9643deb3ec6SMatthias Ringwald log_error("(e)SCO reserved link_type 0x%2x", link_type); 9653deb3ec6SMatthias Ringwald break; 9663deb3ec6SMatthias Ringwald } 967e0d13a19SMilanka Ringwald 9683deb3ec6SMatthias Ringwald log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, " 969aa4dd815SMatthias Ringwald " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)\n", sco_handle, 970e0d13a19SMilanka Ringwald bd_addr_to_str(event_addr), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length, 971e0d13a19SMilanka Ringwald hci_event_synchronous_connection_complete_get_air_mode(packet)); 9723deb3ec6SMatthias Ringwald 973d63c37a1SMatthias Ringwald if (hfp_connection->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){ 974aa4dd815SMatthias Ringwald log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN"); 975d63c37a1SMatthias Ringwald hfp_connection->state = HFP_W2_DISCONNECT_SCO; 976aa4dd815SMatthias Ringwald break; 977aa4dd815SMatthias Ringwald } 978d63c37a1SMatthias Ringwald hfp_connection->sco_handle = sco_handle; 979677003cbSMatthias Ringwald hfp_connection->accept_sco = 0; 980d63c37a1SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 981c95b5b3cSMilanka Ringwald 982d63c37a1SMatthias Ringwald hfp_connection->state = HFP_AUDIO_CONNECTION_ESTABLISHED; 9831a26de69SMilanka Ringwald 9840b4debbfSMilanka Ringwald switch (hfp_connection->vra_state){ 9850b4debbfSMilanka Ringwald case HFP_VRA_VOICE_RECOGNITION_ACTIVATED: 9861a26de69SMilanka Ringwald hfp_connection->ag_audio_connection_opened_before_vra = false; 9870b4debbfSMilanka Ringwald break; 9880b4debbfSMilanka Ringwald default: 9891a26de69SMilanka Ringwald hfp_connection->ag_audio_connection_opened_before_vra = true; 9900b4debbfSMilanka Ringwald break; 9910b4debbfSMilanka Ringwald } 992447743f7SMatthias Ringwald 993447743f7SMatthias Ringwald hfp_sco_establishment_active = NULL; 994447743f7SMatthias Ringwald 995f14c5dafSMatthias Ringwald hfp_emit_sco_connection_established(hfp_connection, status, 9965e8e3664SMatthias Ringwald hfp_connection->negotiated_codec, rx_packet_length, tx_packet_length); 9975fd6f360SMatthias Ringwald 9985fd6f360SMatthias Ringwald #ifdef ENABLE_NXP_PCM_WBS 9995fd6f360SMatthias Ringwald hfp_connection->nxp_start_audio_handle = hfp_connection->sco_handle; 10005fd6f360SMatthias Ringwald #endif 10013deb3ec6SMatthias Ringwald break; 10023deb3ec6SMatthias Ringwald } 10033deb3ec6SMatthias Ringwald 10043deb3ec6SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 1005f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet,3); 1006405014fbSMatthias Ringwald hfp_connection = get_hfp_connection_context_for_sco_handle(handle, local_role); 1007aa4dd815SMatthias Ringwald 1008d63c37a1SMatthias Ringwald if (!hfp_connection) break; 1009aa4dd815SMatthias Ringwald 10103721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP 10113721a235SMatthias Ringwald hfp_connection->cc256x_send_wbs_disassociate = true; 10123721a235SMatthias Ringwald #endif 1013689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 1014689d4323SMatthias Ringwald hfp_connection->bcm_send_disable_wbs = true; 1015689d4323SMatthias Ringwald #endif 10165fd6f360SMatthias Ringwald #ifdef ENABLE_NXP_PCM_WBS 10175fd6f360SMatthias Ringwald hfp_connection->nxp_stop_audio_handle = hfp_connection->sco_handle; 10185fd6f360SMatthias Ringwald #endif 10195fd6f360SMatthias Ringwald 102052cbdd6dSMilanka Ringwald if (hfp_connection->sco_handle == handle){ 1021d751f069SMatthias Ringwald hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID; 1022d63c37a1SMatthias Ringwald hfp_connection->release_audio_connection = 0; 102352cbdd6dSMilanka Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 102452cbdd6dSMilanka Ringwald hfp_emit_audio_connection_released(hfp_connection, handle); 102552cbdd6dSMilanka Ringwald 10261a26de69SMilanka Ringwald hfp_connection->ag_audio_connection_opened_before_vra = false; 102752cbdd6dSMilanka Ringwald 102852cbdd6dSMilanka Ringwald if (hfp_connection->acl_handle == HCI_CON_HANDLE_INVALID){ 102911b091bfSMilanka Ringwald hfp_reset_voice_recognition(hfp_connection); 103052cbdd6dSMilanka Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0); 103152cbdd6dSMilanka Ringwald hfp_finalize_connection_context(hfp_connection); 103219976d6bSMilanka Ringwald break; 103352cbdd6dSMilanka Ringwald } else if (hfp_connection->release_slc_connection == 1){ 103452cbdd6dSMilanka Ringwald hfp_connection->release_slc_connection = 0; 103552cbdd6dSMilanka Ringwald hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM; 103652cbdd6dSMilanka Ringwald rfcomm_disconnect(hfp_connection->acl_handle); 103752cbdd6dSMilanka Ringwald } 103852cbdd6dSMilanka Ringwald } 103948e6eeeeSMatthias Ringwald 104048e6eeeeSMatthias Ringwald if (hfp_connection->state == HFP_W4_SCO_DISCONNECTED_TO_SHUTDOWN){ 104148e6eeeeSMatthias Ringwald // RFCOMM already closed -> remote power off 104248e6eeeeSMatthias Ringwald #if defined(ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS) 104348e6eeeeSMatthias Ringwald hfp_connection->state = HFP_W4_WBS_SHUTDOWN; 104448e6eeeeSMatthias Ringwald #else 104548e6eeeeSMatthias Ringwald hfp_finalize_connection_context(hfp_connection); 104648e6eeeeSMatthias Ringwald #endif 104748e6eeeeSMatthias Ringwald break; 104848e6eeeeSMatthias Ringwald } 10493deb3ec6SMatthias Ringwald break; 10503deb3ec6SMatthias Ringwald 1051fc64f94aSMatthias Ringwald default: 10523deb3ec6SMatthias Ringwald break; 10533deb3ec6SMatthias Ringwald } 10543deb3ec6SMatthias Ringwald } 10553deb3ec6SMatthias Ringwald 10563721a235SMatthias Ringwald 105727950165SMatthias Ringwald void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){ 10585575558eSMilanka Ringwald UNUSED(packet_type); 105927950165SMatthias Ringwald UNUSED(channel); // ok: no channel 10605575558eSMilanka Ringwald UNUSED(size); 106127950165SMatthias Ringwald 106227950165SMatthias Ringwald bd_addr_t event_addr; 106327950165SMatthias Ringwald uint16_t rfcomm_cid; 106427950165SMatthias Ringwald hfp_connection_t * hfp_connection = NULL; 106527950165SMatthias Ringwald uint8_t status; 106627950165SMatthias Ringwald 106727950165SMatthias Ringwald log_debug("HFP packet_handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size); 106827950165SMatthias Ringwald 106927950165SMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 107027950165SMatthias Ringwald 107127950165SMatthias Ringwald case RFCOMM_EVENT_INCOMING_CONNECTION: 107227950165SMatthias Ringwald // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 107327950165SMatthias Ringwald rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr); 10744eb3f1d8SMilanka Ringwald hfp_connection = hfp_create_connection(event_addr, local_role); 107527950165SMatthias Ringwald if (!hfp_connection){ 107627950165SMatthias Ringwald log_info("hfp: no memory to accept incoming connection - decline"); 107727950165SMatthias Ringwald rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet)); 107827950165SMatthias Ringwald return; 107927950165SMatthias Ringwald } 108027950165SMatthias Ringwald if (hfp_connection->state != HFP_IDLE) { 10814960f9e7SMatthias Ringwald log_error("hfp: incoming connection but not idle, reject"); 10824960f9e7SMatthias Ringwald rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet)); 108327950165SMatthias Ringwald return; 108427950165SMatthias Ringwald } 108527950165SMatthias Ringwald 108627950165SMatthias Ringwald hfp_connection->rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet); 108727950165SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_CONNECTED; 108827950165SMatthias Ringwald rfcomm_accept_connection(hfp_connection->rfcomm_cid); 108927950165SMatthias Ringwald break; 109027950165SMatthias Ringwald 109127950165SMatthias Ringwald case RFCOMM_EVENT_CHANNEL_OPENED: 109227950165SMatthias Ringwald rfcomm_event_channel_opened_get_bd_addr(packet, event_addr); 109327950165SMatthias Ringwald 1094405014fbSMatthias Ringwald hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role); 10956504a03aSMilanka Ringwald btstack_assert(hfp_connection != NULL); 109627950165SMatthias Ringwald 10976504a03aSMilanka Ringwald if (hfp_connection->state != HFP_W4_RFCOMM_CONNECTED){ 10986504a03aSMilanka Ringwald break; 10996504a03aSMilanka Ringwald } 11006504a03aSMilanka Ringwald 11016504a03aSMilanka Ringwald status = rfcomm_event_channel_opened_get_status(packet); 11026504a03aSMilanka Ringwald if (status != ERROR_CODE_SUCCESS) { 11037dceaecaSMatthias Ringwald hfp_handle_slc_setup_error(hfp_connection, status); 11046504a03aSMilanka Ringwald break; 11056504a03aSMilanka Ringwald } 11066504a03aSMilanka Ringwald 110727950165SMatthias Ringwald hfp_connection->acl_handle = rfcomm_event_channel_opened_get_con_handle(packet); 110827950165SMatthias Ringwald hfp_connection->rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet); 11096504a03aSMilanka Ringwald hfp_connection->rfcomm_mtu = rfcomm_event_channel_opened_get_max_frame_size(packet); 111027950165SMatthias Ringwald bd_addr_copy(hfp_connection->remote_addr, event_addr); 111127950165SMatthias Ringwald hfp_connection->state = HFP_EXCHANGE_SUPPORTED_FEATURES; 11126504a03aSMilanka Ringwald 111327950165SMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 111427950165SMatthias Ringwald break; 111527950165SMatthias Ringwald 111627950165SMatthias Ringwald case RFCOMM_EVENT_CHANNEL_CLOSED: 111727950165SMatthias Ringwald rfcomm_cid = little_endian_read_16(packet,2); 111827950165SMatthias Ringwald hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid); 111927950165SMatthias Ringwald if (!hfp_connection) break; 112052cbdd6dSMilanka Ringwald switch (hfp_connection->state){ 112152cbdd6dSMilanka Ringwald case HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART: 112252cbdd6dSMilanka Ringwald hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID; 112327950165SMatthias Ringwald hfp_connection->state = HFP_IDLE; 112427950165SMatthias Ringwald hfp_establish_service_level_connection(hfp_connection->remote_addr, hfp_connection->service_uuid, local_role); 112527950165SMatthias Ringwald break; 112652cbdd6dSMilanka Ringwald 112752cbdd6dSMilanka Ringwald case HFP_AUDIO_CONNECTION_ESTABLISHED: 112848e6eeeeSMatthias Ringwald // service connection was released, this implicitly releases audio connection as well 112948e6eeeeSMatthias Ringwald hfp_connection->release_audio_connection = 0; 113052cbdd6dSMilanka Ringwald hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID; 113148e6eeeeSMatthias Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED_TO_SHUTDOWN; 113252cbdd6dSMilanka Ringwald gap_disconnect(hfp_connection->sco_handle); 113352cbdd6dSMilanka Ringwald break; 113452cbdd6dSMilanka Ringwald 113552cbdd6dSMilanka Ringwald default: 113648e6eeeeSMatthias Ringwald // regular case 113711b091bfSMilanka Ringwald hfp_reset_voice_recognition(hfp_connection); 113848e6eeeeSMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0); 113948e6eeeeSMatthias Ringwald hfp_finalize_connection_context(hfp_connection); 114052cbdd6dSMilanka Ringwald break; 114148e6eeeeSMatthias Ringwald } 114227950165SMatthias Ringwald break; 114327950165SMatthias Ringwald 114427950165SMatthias Ringwald default: 114527950165SMatthias Ringwald break; 114627950165SMatthias Ringwald } 114727950165SMatthias Ringwald } 1148aa4dd815SMatthias Ringwald // translates command string into hfp_command_t CMD 114994a27792SMatthias Ringwald 115094a27792SMatthias Ringwald typedef struct { 115194a27792SMatthias Ringwald const char * command; 115294a27792SMatthias Ringwald hfp_command_t command_id; 115394a27792SMatthias Ringwald } hfp_command_entry_t; 115494a27792SMatthias Ringwald 1155f703e170SMatthias Ringwald static hfp_command_entry_t hfp_ag_command_table[] = { 1156cb33905eSMatthias Ringwald { "AT+BAC=", HFP_CMD_AVAILABLE_CODECS }, 1157cb33905eSMatthias Ringwald { "AT+BCC", HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP }, 1158cb33905eSMatthias Ringwald { "AT+BCS=", HFP_CMD_HF_CONFIRMED_CODEC }, 1159cb33905eSMatthias Ringwald { "AT+BIA=", HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE, }, // +BIA:<enabled>,,<enabled>,,,<enabled> 1160cb33905eSMatthias Ringwald { "AT+BIEV=", HFP_CMD_HF_INDICATOR_STATUS }, 116194a27792SMatthias Ringwald { "AT+BIND=", HFP_CMD_LIST_GENERIC_STATUS_INDICATORS }, 116294a27792SMatthias Ringwald { "AT+BIND=?", HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS }, 116394a27792SMatthias Ringwald { "AT+BIND?", HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE }, 1164051f797dSMilanka Ringwald { "AT+BINP=", HFP_CMD_HF_REQUEST_PHONE_NUMBER }, 1165cb33905eSMatthias Ringwald { "AT+BLDN", HFP_CMD_REDIAL_LAST_NUMBER }, 1166cb33905eSMatthias Ringwald { "AT+BRSF=", HFP_CMD_SUPPORTED_FEATURES }, 116794a27792SMatthias Ringwald { "AT+BTRH=", HFP_CMD_RESPONSE_AND_HOLD_COMMAND }, 116894a27792SMatthias Ringwald { "AT+BTRH?", HFP_CMD_RESPONSE_AND_HOLD_QUERY }, 11699ed286f3SMilanka Ringwald { "AT+BVRA=", HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION }, 1170cb33905eSMatthias Ringwald { "AT+CCWA=", HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION}, 117194a27792SMatthias Ringwald { "AT+CHLD=", HFP_CMD_CALL_HOLD }, 117294a27792SMatthias Ringwald { "AT+CHLD=?", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES }, 1173cb33905eSMatthias Ringwald { "AT+CHUP", HFP_CMD_HANG_UP_CALL }, 117494a27792SMatthias Ringwald { "AT+CIND=?", HFP_CMD_RETRIEVE_AG_INDICATORS }, 117594a27792SMatthias Ringwald { "AT+CIND?", HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS }, 1176cb33905eSMatthias Ringwald { "AT+CLCC", HFP_CMD_LIST_CURRENT_CALLS }, 1177cb33905eSMatthias Ringwald { "AT+CLIP=", HFP_CMD_ENABLE_CLIP}, 1178cb33905eSMatthias Ringwald { "AT+CMEE=", HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR}, 1179cb33905eSMatthias Ringwald { "AT+CMER=", HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE }, 1180cb33905eSMatthias Ringwald { "AT+CNUM", HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION }, 118194a27792SMatthias Ringwald { "AT+COPS=", HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT }, 118294a27792SMatthias Ringwald { "AT+COPS?", HFP_CMD_QUERY_OPERATOR_SELECTION_NAME }, 1183cb33905eSMatthias Ringwald { "AT+NREC=", HFP_CMD_TURN_OFF_EC_AND_NR, }, 1184cb33905eSMatthias Ringwald { "AT+VGM=", HFP_CMD_SET_MICROPHONE_GAIN }, 1185cb33905eSMatthias Ringwald { "AT+VGS=", HFP_CMD_SET_SPEAKER_GAIN }, 1186d8656ee6SMilanka Ringwald { "AT+VTS=", HFP_CMD_TRANSMIT_DTMF_CODES }, 118794a27792SMatthias Ringwald { "ATA", HFP_CMD_CALL_ANSWERED }, 118894a27792SMatthias Ringwald }; 118994a27792SMatthias Ringwald 1190f703e170SMatthias Ringwald static hfp_command_entry_t hfp_hf_command_table[] = { 1191cb33905eSMatthias Ringwald { "+BCS:", HFP_CMD_AG_SUGGESTED_CODEC }, 119294a27792SMatthias Ringwald { "+BIND:", HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS }, 1193b3615b9aSMilanka Ringwald { "+BINP:", HFP_CMD_AG_SENT_PHONE_NUMBER }, 1194cb33905eSMatthias Ringwald { "+BRSF:", HFP_CMD_SUPPORTED_FEATURES }, 1195cb33905eSMatthias Ringwald { "+BSIR:", HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING }, 119694a27792SMatthias Ringwald { "+BTRH:", HFP_CMD_RESPONSE_AND_HOLD_STATUS }, 1197cb33905eSMatthias Ringwald { "+BVRA:", HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION }, 1198cb33905eSMatthias Ringwald { "+CCWA:", HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE, }, 119994a27792SMatthias Ringwald { "+CHLD:", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES }, 1200cb33905eSMatthias Ringwald { "+CIEV:", HFP_CMD_TRANSFER_AG_INDICATOR_STATUS}, 120184a0c24eSMatthias Ringwald { "+CIND:", HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC }, 1202cb33905eSMatthias Ringwald { "+CLCC:", HFP_CMD_LIST_CURRENT_CALLS }, 1203cb33905eSMatthias Ringwald { "+CLIP:", HFP_CMD_AG_SENT_CLIP_INFORMATION }, 1204cb33905eSMatthias Ringwald { "+CME ERROR:", HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR }, 1205cb33905eSMatthias Ringwald { "+CNUM:", HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION}, 1206cb33905eSMatthias Ringwald { "+COPS:", HFP_CMD_QUERY_OPERATOR_SELECTION_NAME }, 1207cb33905eSMatthias Ringwald { "+VGM:", HFP_CMD_SET_MICROPHONE_GAIN }, 1208ce284ffbSMatthias Ringwald { "+VGM=", HFP_CMD_SET_MICROPHONE_GAIN }, 12094c30d2d1SMatthias Ringwald { "+VGS:", HFP_CMD_SET_SPEAKER_GAIN}, 1210ce284ffbSMatthias Ringwald { "+VGS=", HFP_CMD_SET_SPEAKER_GAIN}, 1211cb33905eSMatthias Ringwald { "ERROR", HFP_CMD_ERROR}, 1212cb33905eSMatthias Ringwald { "NOP", HFP_CMD_NONE}, // dummy command used by unit tests 1213cb33905eSMatthias Ringwald { "OK", HFP_CMD_OK }, 1214cb33905eSMatthias Ringwald { "RING", HFP_CMD_RING }, 121594a27792SMatthias Ringwald }; 121694a27792SMatthias Ringwald 12176e64d9feSMatthias Ringwald static const hfp_custom_at_command_t * 12186e64d9feSMatthias Ringwald hfp_custom_command_lookup(bool isHandsFree, const char *text) { 12196e64d9feSMatthias Ringwald btstack_linked_list_t * custom_commands = isHandsFree ? &hfp_custom_commands_hf : &hfp_custom_commands_ag; 122004cf3dd4SMatthias Ringwald btstack_linked_list_iterator_t it; 12216e64d9feSMatthias Ringwald btstack_linked_list_iterator_init(&it, custom_commands); 1222471dea41SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)) { 1223471dea41SMatthias Ringwald hfp_custom_at_command_t *at_command = (hfp_custom_at_command_t *) btstack_linked_list_iterator_next(&it); 1224471dea41SMatthias Ringwald int match = strcmp(text, at_command->command); 1225471dea41SMatthias Ringwald if (match == 0) { 1226471dea41SMatthias Ringwald return at_command; 1227471dea41SMatthias Ringwald } 1228471dea41SMatthias Ringwald } 1229471dea41SMatthias Ringwald return NULL; 1230471dea41SMatthias Ringwald } 1231471dea41SMatthias Ringwald 1232aa4dd815SMatthias Ringwald static hfp_command_t parse_command(const char * line_buffer, int isHandsFree){ 12333deb3ec6SMatthias Ringwald 1234ff158b9bSMatthias Ringwald // check for custom commands, AG only 12356e64d9feSMatthias Ringwald const hfp_custom_at_command_t * custom_at_command = hfp_custom_command_lookup(isHandsFree, line_buffer); 1236ff158b9bSMatthias Ringwald if (custom_at_command != NULL){ 1237ff158b9bSMatthias Ringwald return HFP_CMD_CUSTOM_MESSAGE; 1238ff158b9bSMatthias Ringwald } 1239ff158b9bSMatthias Ringwald 1240cb33905eSMatthias Ringwald // table lookup based on role 124194a27792SMatthias Ringwald uint16_t num_entries; 124294a27792SMatthias Ringwald hfp_command_entry_t * table; 124394a27792SMatthias Ringwald if (isHandsFree == 0){ 1244f703e170SMatthias Ringwald table = hfp_ag_command_table; 1245f703e170SMatthias Ringwald num_entries = sizeof(hfp_ag_command_table) / sizeof(hfp_command_entry_t); 12463deb3ec6SMatthias Ringwald } else { 1247f703e170SMatthias Ringwald table = hfp_hf_command_table; 1248f703e170SMatthias Ringwald num_entries = sizeof(hfp_hf_command_table) / sizeof(hfp_command_entry_t); 124994a27792SMatthias Ringwald } 1250791f0d0aSMatthias Ringwald // binary search 1251791f0d0aSMatthias Ringwald uint16_t left = 0; 1252791f0d0aSMatthias Ringwald uint16_t right = num_entries - 1; 1253791f0d0aSMatthias Ringwald while (left <= right){ 1254791f0d0aSMatthias Ringwald uint16_t middle = left + (right - left) / 2; 1255791f0d0aSMatthias Ringwald hfp_command_entry_t *entry = &table[middle]; 125694a27792SMatthias Ringwald int match = strcmp(line_buffer, entry->command); 1257791f0d0aSMatthias Ringwald if (match < 0){ 1258791f0d0aSMatthias Ringwald // search term is lower than middle element 125947005182SMilanka Ringwald if (right == 0) break; 1260791f0d0aSMatthias Ringwald right = middle - 1; 1261791f0d0aSMatthias Ringwald } else if (match == 0){ 126294a27792SMatthias Ringwald return entry->command_id; 1263791f0d0aSMatthias Ringwald } else { 1264791f0d0aSMatthias Ringwald // search term is higher than middle element 1265791f0d0aSMatthias Ringwald left = middle + 1; 126694a27792SMatthias Ringwald } 126794a27792SMatthias Ringwald } 126894a27792SMatthias Ringwald 1269cb33905eSMatthias Ringwald // note: if parser in CMD_HEADER state would treats digits and maybe '+' as separator, match on "ATD" would work. 12700222a807SMatthias Ringwald // note: phone number is currently expected in line_buffer[3..] 1271cb33905eSMatthias Ringwald // prefix match on 'ATD', AG only 1272cb33905eSMatthias Ringwald if ((isHandsFree == 0) && (strncmp(line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0)){ 1273cb33905eSMatthias Ringwald return HFP_CMD_CALL_PHONE_NUMBER; 12743deb3ec6SMatthias Ringwald } 12753deb3ec6SMatthias Ringwald 1276cb33905eSMatthias Ringwald // Valid looking, but unknown commands/responses 1277cb33905eSMatthias Ringwald if ((isHandsFree == 0) && (strncmp(line_buffer, "AT+", 3) == 0)){ 1278aa4dd815SMatthias Ringwald return HFP_CMD_UNKNOWN; 12793deb3ec6SMatthias Ringwald } 12803deb3ec6SMatthias Ringwald 1281cb33905eSMatthias Ringwald if ((isHandsFree != 0) && (strncmp(line_buffer, "+", 1) == 0)){ 1282aa4dd815SMatthias Ringwald return HFP_CMD_UNKNOWN; 1283aa4dd815SMatthias Ringwald } 12843deb3ec6SMatthias Ringwald 1285aa4dd815SMatthias Ringwald return HFP_CMD_NONE; 12863deb3ec6SMatthias Ringwald } 12873deb3ec6SMatthias Ringwald 1288a0ffb263SMatthias Ringwald static void hfp_parser_store_byte(hfp_connection_t * hfp_connection, uint8_t byte){ 128951aa5d5aSMilanka Ringwald if ((hfp_connection->line_size + 1) >= HFP_MAX_VR_TEXT_SIZE) return; 1290a0ffb263SMatthias Ringwald hfp_connection->line_buffer[hfp_connection->line_size++] = byte; 1291a0ffb263SMatthias Ringwald hfp_connection->line_buffer[hfp_connection->line_size] = 0; 12923deb3ec6SMatthias Ringwald } 1293a0ffb263SMatthias Ringwald static int hfp_parser_is_buffer_empty(hfp_connection_t * hfp_connection){ 1294a0ffb263SMatthias Ringwald return hfp_connection->line_size == 0; 12953deb3ec6SMatthias Ringwald } 12963deb3ec6SMatthias Ringwald 12973deb3ec6SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){ 1298c1ab6cc1SMatthias Ringwald return (byte == '\n') || (byte == '\r'); 12993deb3ec6SMatthias Ringwald } 13003deb3ec6SMatthias Ringwald 1301471dea41SMatthias Ringwald void hfp_parser_reset_line_buffer(hfp_connection_t *hfp_connection) { 1302a0ffb263SMatthias Ringwald hfp_connection->line_size = 0; 1303125560b8SMatthias Ringwald // we don't set the first byte to '\0' to allow access to last argument e.g. in hfp_hf_handle_rfcommand 13043deb3ec6SMatthias Ringwald } 13051d81c7feSMatthias Ringwald 13061d81c7feSMatthias Ringwald static void hfp_parser_store_if_token(hfp_connection_t * hfp_connection, uint8_t byte){ 13071d81c7feSMatthias Ringwald switch (byte){ 13081d81c7feSMatthias Ringwald case ',': 1309f8301d46SMatthias Ringwald case '-': 13101d81c7feSMatthias Ringwald case ';': 13111d81c7feSMatthias Ringwald case '(': 13121d81c7feSMatthias Ringwald case ')': 13131d81c7feSMatthias Ringwald case '\n': 13141d81c7feSMatthias Ringwald case '\r': 13153deb3ec6SMatthias Ringwald break; 13163deb3ec6SMatthias Ringwald default: 13171d81c7feSMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 13183deb3ec6SMatthias Ringwald break; 13193deb3ec6SMatthias Ringwald } 13203deb3ec6SMatthias Ringwald } 13211d81c7feSMatthias Ringwald 13221d81c7feSMatthias Ringwald static bool hfp_parser_is_separator( uint8_t byte){ 13231d81c7feSMatthias Ringwald switch (byte){ 13241d81c7feSMatthias Ringwald case ',': 1325f8301d46SMatthias Ringwald case '-': 13261d81c7feSMatthias Ringwald case ';': 13271d81c7feSMatthias Ringwald case '\n': 13281d81c7feSMatthias Ringwald case '\r': 13291d81c7feSMatthias Ringwald return true; 1330dd533208SMatthias Ringwald default: 13311d81c7feSMatthias Ringwald return false; 13323deb3ec6SMatthias Ringwald } 13333deb3ec6SMatthias Ringwald } 13343deb3ec6SMatthias Ringwald 1335f79a7821SMatthias Ringwald // returns true if received bytes was processed. Otherwise, functions will be called with same byte again 1336f79a7821SMatthias Ringwald // this is used to for a one byte lookahead, where an unexpected byte is pushed back by returning false 1337d6b237acSMatthias Ringwald static bool hfp_parse_byte(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){ 1338aa4dd815SMatthias Ringwald 1339f79a7821SMatthias Ringwald #ifdef HFP_DEBUG 1340f79a7821SMatthias Ringwald if (byte >= ' '){ 1341f79a7821SMatthias Ringwald printf("Parse '%c' - state %u, buffer %s\n", byte, hfp_connection->parser_state, hfp_connection->line_buffer); 1342f79a7821SMatthias Ringwald } else { 1343f79a7821SMatthias Ringwald printf("Parse 0x%02x - state %u, buffer %s\n", byte, hfp_connection->parser_state, hfp_connection->line_buffer); 1344f79a7821SMatthias Ringwald } 1345f79a7821SMatthias Ringwald #endif 1346f79a7821SMatthias Ringwald 13471dddc4f4SMatthias Ringwald // handle doubles quotes 13481dddc4f4SMatthias Ringwald if (byte == '"'){ 13491dddc4f4SMatthias Ringwald hfp_connection->parser_quoted = !hfp_connection->parser_quoted; 1350d6b237acSMatthias Ringwald return true; 13511dddc4f4SMatthias Ringwald } 13521dddc4f4SMatthias Ringwald if (hfp_connection->parser_quoted) { 13531dddc4f4SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 1354d6b237acSMatthias Ringwald return true; 13551dddc4f4SMatthias Ringwald } 13563deb3ec6SMatthias Ringwald 13571dddc4f4SMatthias Ringwald // ignore spaces outside command or double quotes (required e.g. for '+CME ERROR:..") command 1358d6b237acSMatthias Ringwald if ((byte == ' ') && (hfp_connection->parser_state != HFP_PARSER_CMD_HEADER)) return true; 13591dddc4f4SMatthias Ringwald 1360bfeaf344SMatthias Ringwald bool processed = true; 1361bfeaf344SMatthias Ringwald 1362a0ffb263SMatthias Ringwald switch (hfp_connection->parser_state) { 1363dd533208SMatthias Ringwald case HFP_PARSER_CMD_HEADER: 1364d28b2d5dSMilanka Ringwald switch (byte) { 1365dd533208SMatthias Ringwald case '\n': 1366dd533208SMatthias Ringwald case '\r': 1367dd533208SMatthias Ringwald case ';': 1368bfeaf344SMatthias Ringwald // ignore separator 1369bfeaf344SMatthias Ringwald break; 1370bfeaf344SMatthias Ringwald case ':': 1371bfeaf344SMatthias Ringwald case '?': 1372bfeaf344SMatthias Ringwald // store separator 1373bfeaf344SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 1374dd533208SMatthias Ringwald break; 1375d28b2d5dSMilanka Ringwald case '=': 1376bfeaf344SMatthias Ringwald // equal sign: remember and wait for next char to decided between '=?' and '=\?' 1377dd533208SMatthias Ringwald hfp_connection->found_equal_sign = true; 1378a0ffb263SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 1379d6b237acSMatthias Ringwald return true; 1380d28b2d5dSMilanka Ringwald default: 1381bfeaf344SMatthias Ringwald // store if not lookahead 1382d7f16ff1SMatthias Ringwald if (!hfp_connection->found_equal_sign) { 1383dd533208SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 1384d6b237acSMatthias Ringwald return true; 1385dd533208SMatthias Ringwald } 1386bfeaf344SMatthias Ringwald // mark as lookahead 1387bfeaf344SMatthias Ringwald processed = false; 1388d7f16ff1SMatthias Ringwald break; 1389d7f16ff1SMatthias Ringwald } 1390ce263fc8SMatthias Ringwald 13911d81c7feSMatthias Ringwald // ignore empty tokens 1392d6b237acSMatthias Ringwald if (hfp_parser_is_buffer_empty(hfp_connection)) return true; 1393dd533208SMatthias Ringwald 1394bfeaf344SMatthias Ringwald // parse 1395dd533208SMatthias Ringwald hfp_connection->command = parse_command((char *)hfp_connection->line_buffer, isHandsFree); 1396aa4dd815SMatthias Ringwald 139784a0c24eSMatthias Ringwald // pick +CIND version based on connection state: descriptions during SLC vs. states later 139884a0c24eSMatthias Ringwald if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC){ 1399a0ffb263SMatthias Ringwald switch(hfp_connection->state){ 1400aa4dd815SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS_STATUS: 1401a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS; 1402aa4dd815SMatthias Ringwald break; 1403aa4dd815SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS: 1404a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS; 1405aa4dd815SMatthias Ringwald break; 1406aa4dd815SMatthias Ringwald default: 140784a0c24eSMatthias Ringwald hfp_connection->command = HFP_CMD_UNKNOWN; 1408aa4dd815SMatthias Ringwald break; 1409aa4dd815SMatthias Ringwald } 1410aa4dd815SMatthias Ringwald } 14113deb3ec6SMatthias Ringwald 1412c0c0437aSMatthias Ringwald log_info("command string '%s', handsfree %u -> cmd id %u", (char *)hfp_connection->line_buffer, isHandsFree, hfp_connection->command); 1413c0c0437aSMatthias Ringwald 1414471dea41SMatthias Ringwald // store command id for custom command and just store rest of line 1415471dea41SMatthias Ringwald if (hfp_connection->command == HFP_CMD_CUSTOM_MESSAGE){ 14166e64d9feSMatthias Ringwald const hfp_custom_at_command_t * at_command = hfp_custom_command_lookup(isHandsFree, (const char *) hfp_connection->line_buffer); 14176d9a41f9SMatthias Ringwald hfp_connection->custom_at_command_id = at_command->command_id; 1418471dea41SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CUSTOM_COMMAND; 1419f79a7821SMatthias Ringwald return processed; 1420471dea41SMatthias Ringwald } 1421471dea41SMatthias Ringwald 1422bfeaf344SMatthias Ringwald // next state 14231d81c7feSMatthias Ringwald hfp_parser_reset_line_buffer(hfp_connection); 1424dd533208SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE; 1425dd533208SMatthias Ringwald 1426bfeaf344SMatthias Ringwald return processed; 1427dd533208SMatthias Ringwald 1428bfeaf344SMatthias Ringwald case HFP_PARSER_CMD_SEQUENCE: 14291d81c7feSMatthias Ringwald // handle empty fields 14301d81c7feSMatthias Ringwald if ((byte == ',' ) && (hfp_connection->line_size == 0)){ 1431dd533208SMatthias Ringwald hfp_connection->line_buffer[0] = 0; 1432dd533208SMatthias Ringwald hfp_connection->ignore_value = 1; 1433dd533208SMatthias Ringwald parse_sequence(hfp_connection); 14343d051a53SMatthias Ringwald return true; 14353d051a53SMatthias Ringwald } 14363d051a53SMatthias Ringwald 14371d81c7feSMatthias Ringwald hfp_parser_store_if_token(hfp_connection, byte); 14381d81c7feSMatthias Ringwald if (!hfp_parser_is_separator(byte)) return true; 14391d81c7feSMatthias Ringwald 14401d81c7feSMatthias Ringwald // ignore empty tokens 144189e7c136SMilanka Ringwald switch (hfp_connection->command){ 144289e7c136SMilanka Ringwald case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION: 144389e7c136SMilanka Ringwald // don't ignore empty string 144489e7c136SMilanka Ringwald break; 144589e7c136SMilanka Ringwald default: 144689e7c136SMilanka Ringwald if (hfp_parser_is_buffer_empty(hfp_connection) && (hfp_connection->ignore_value == 0)) { 144789e7c136SMilanka Ringwald return true; 144889e7c136SMilanka Ringwald } 144989e7c136SMilanka Ringwald break; 145089e7c136SMilanka Ringwald } 14513d051a53SMatthias Ringwald 14523d051a53SMatthias Ringwald parse_sequence(hfp_connection); 14531d81c7feSMatthias Ringwald 14541d81c7feSMatthias Ringwald hfp_parser_reset_line_buffer(hfp_connection); 14551d81c7feSMatthias Ringwald 14561d81c7feSMatthias Ringwald switch (hfp_connection->command){ 14571d81c7feSMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 14581d81c7feSMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 14591d81c7feSMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 14601d81c7feSMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 14611d81c7feSMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 14621d81c7feSMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 14631d81c7feSMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 14641d81c7feSMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 14651d81c7feSMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 14661d81c7feSMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_SECOND_ITEM; 14671d81c7feSMatthias Ringwald break; 14681d81c7feSMatthias Ringwald default: 14691d81c7feSMatthias Ringwald break; 14701d81c7feSMatthias Ringwald } 14713d051a53SMatthias Ringwald return true; 14723d051a53SMatthias Ringwald 1473ba0de059SMatthias Ringwald case HFP_PARSER_SECOND_ITEM: 14741d81c7feSMatthias Ringwald 14751d81c7feSMatthias Ringwald hfp_parser_store_if_token(hfp_connection, byte); 14761d81c7feSMatthias Ringwald if (!hfp_parser_is_separator(byte)) return true; 1477dd533208SMatthias Ringwald 1478a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1479ce263fc8SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1480a0ffb263SMatthias Ringwald log_info("format %s, ", hfp_connection->line_buffer); 14812308e108SMilanka Ringwald hfp_connection->network_operator.format = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1482ce263fc8SMatthias Ringwald break; 1483ce263fc8SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 1484a0ffb263SMatthias Ringwald log_info("format %s \n", hfp_connection->line_buffer); 14852308e108SMilanka Ringwald hfp_connection->network_operator.format = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1486ce263fc8SMatthias Ringwald break; 1487ce263fc8SMatthias Ringwald case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 1488ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 1489ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 14902308e108SMilanka Ringwald hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].state = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1491ce263fc8SMatthias Ringwald break; 1492ce263fc8SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 14932308e108SMilanka Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1494a0ffb263SMatthias Ringwald log_info("%d \n", hfp_connection->ag_indicators[hfp_connection->parser_item_index].status); 1495a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].status_changed = 1; 1496ce263fc8SMatthias Ringwald break; 1497ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 14982308e108SMilanka Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].min_range = btstack_atoi((char *)hfp_connection->line_buffer); 1499a0ffb263SMatthias Ringwald log_info("%s, ", hfp_connection->line_buffer); 1500ce263fc8SMatthias Ringwald break; 1501ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 1502a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1503a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 15042308e108SMilanka Ringwald hfp_connection->bnip_type = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1505ce263fc8SMatthias Ringwald break; 15060222a807SMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 15070222a807SMatthias Ringwald hfp_connection->parser_indicator_value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 15080222a807SMatthias Ringwald break; 1509ce263fc8SMatthias Ringwald default: 1510ce263fc8SMatthias Ringwald break; 1511ce263fc8SMatthias Ringwald } 15121d81c7feSMatthias Ringwald 15131d81c7feSMatthias Ringwald hfp_parser_reset_line_buffer(hfp_connection); 15141d81c7feSMatthias Ringwald 15151d81c7feSMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_THIRD_ITEM; 15161d81c7feSMatthias Ringwald 1517ba0de059SMatthias Ringwald return true; 1518ce263fc8SMatthias Ringwald 151974c7548aSMatthias Ringwald case HFP_PARSER_THIRD_ITEM: 15201d81c7feSMatthias Ringwald 15211d81c7feSMatthias Ringwald hfp_parser_store_if_token(hfp_connection, byte); 15221d81c7feSMatthias Ringwald if (!hfp_parser_is_separator(byte)) return true; 15231d81c7feSMatthias Ringwald 1524a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1525ce263fc8SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1526c10fde09SMatthias Ringwald btstack_strcpy(hfp_connection->network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE, (char *)hfp_connection->line_buffer); 1527ce263fc8SMatthias Ringwald break; 1528ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 15292308e108SMilanka Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].max_range = btstack_atoi((char *)hfp_connection->line_buffer); 153025789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 153125789943SMilanka Ringwald hfp_connection->ag_indicators_nr = hfp_connection->parser_item_index; 1532ce263fc8SMatthias Ringwald break; 1533125560b8SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1534f6e6c52dSMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1535125560b8SMatthias Ringwald // track if last argument exists 1536125560b8SMatthias Ringwald hfp_connection->clip_have_alpha = hfp_connection->line_size != 0; 1537125560b8SMatthias Ringwald break; 1538ce263fc8SMatthias Ringwald default: 1539ce263fc8SMatthias Ringwald break; 1540ce263fc8SMatthias Ringwald } 15411d81c7feSMatthias Ringwald 15421d81c7feSMatthias Ringwald hfp_parser_reset_line_buffer(hfp_connection); 15431d81c7feSMatthias Ringwald 15441d81c7feSMatthias Ringwald if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS){ 15451d81c7feSMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE; 15461d81c7feSMatthias Ringwald } 1547d6b237acSMatthias Ringwald return true; 154874c7548aSMatthias Ringwald 1549471dea41SMatthias Ringwald case HFP_PARSER_CUSTOM_COMMAND: 15508deec660SMatthias Ringwald if (hfp_parser_is_end_of_line(byte) == false){ 1551471dea41SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 15528deec660SMatthias Ringwald } 1553471dea41SMatthias Ringwald return true; 1554471dea41SMatthias Ringwald 155574c7548aSMatthias Ringwald default: 155674c7548aSMatthias Ringwald btstack_assert(false); 155774c7548aSMatthias Ringwald return true; 155874c7548aSMatthias Ringwald } 1559d6b237acSMatthias Ringwald } 1560d6b237acSMatthias Ringwald 1561d6b237acSMatthias Ringwald void hfp_parse(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){ 1562d6b237acSMatthias Ringwald bool processed = false; 1563d6b237acSMatthias Ringwald while (!processed){ 1564d6b237acSMatthias Ringwald processed = hfp_parse_byte(hfp_connection, byte, isHandsFree); 1565d6b237acSMatthias Ringwald } 15661d81c7feSMatthias Ringwald // reset parser state on end-of-line 15671d81c7feSMatthias Ringwald if (hfp_parser_is_end_of_line(byte)){ 1568a34e380cSMatthias Ringwald hfp_connection->found_equal_sign = false; 15691d81c7feSMatthias Ringwald hfp_connection->parser_item_index = 0; 15701d81c7feSMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 15711d81c7feSMatthias Ringwald } 1572ce263fc8SMatthias Ringwald } 1573ce263fc8SMatthias Ringwald 1574a0ffb263SMatthias Ringwald static void parse_sequence(hfp_connection_t * hfp_connection){ 1575ce263fc8SMatthias Ringwald int value; 1576a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1577667ec068SMatthias Ringwald case HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS: 15782308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1579667ec068SMatthias Ringwald int i; 1580a0ffb263SMatthias Ringwald switch (hfp_connection->parser_item_index){ 1581667ec068SMatthias Ringwald case 0: 1582a0ffb263SMatthias Ringwald for (i=0;i<hfp_connection->generic_status_indicators_nr;i++){ 1583a0ffb263SMatthias Ringwald if (hfp_connection->generic_status_indicators[i].uuid == value){ 1584a0ffb263SMatthias Ringwald hfp_connection->parser_indicator_index = i; 1585667ec068SMatthias Ringwald break; 1586667ec068SMatthias Ringwald } 1587667ec068SMatthias Ringwald } 1588667ec068SMatthias Ringwald break; 1589667ec068SMatthias Ringwald case 1: 1590a0ffb263SMatthias Ringwald if (hfp_connection->parser_indicator_index <0) break; 1591a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[hfp_connection->parser_indicator_index].state = value; 1592667ec068SMatthias Ringwald log_info("HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS set indicator at index %u, to %u\n", 1593a0ffb263SMatthias Ringwald hfp_connection->parser_item_index, value); 1594667ec068SMatthias Ringwald break; 1595667ec068SMatthias Ringwald default: 1596667ec068SMatthias Ringwald break; 1597667ec068SMatthias Ringwald } 159825789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 1599667ec068SMatthias Ringwald break; 1600667ec068SMatthias Ringwald 1601667ec068SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1602a0ffb263SMatthias Ringwald switch(hfp_connection->parser_item_index){ 1603667ec068SMatthias Ringwald case 0: 160428a4aea3SMilanka Ringwald // <alpha>: This optional field is not supported, and shall be left blank. 160528a4aea3SMilanka Ringwald break; 160628a4aea3SMilanka Ringwald case 1: 160728a4aea3SMilanka Ringwald // <number>: Quoted string containing the phone number in the format specified by <type>. 1608c10fde09SMatthias Ringwald btstack_strcpy(hfp_connection->bnip_number, sizeof(hfp_connection->bnip_number), (char *)hfp_connection->line_buffer); 1609667ec068SMatthias Ringwald break; 161028a4aea3SMilanka Ringwald case 2: 161128a4aea3SMilanka Ringwald /* 161228a4aea3SMilanka Ringwald <type> field specifies the format of the phone number provided, and can be one of the following values: 161328a4aea3SMilanka Ringwald - values 128-143: The phone number format may be a national or international format, and may contain prefix and/or escape digits. No changes on the number presentation are required. 161428a4aea3SMilanka Ringwald - values 144-159: The phone number format is an international number, including the country code prefix. If the plus sign ("+") is not included as part of the number and shall be added by the AG as needed. 161528a4aea3SMilanka Ringwald - values 160-175: National number. No prefix nor escape digits included. 161628a4aea3SMilanka Ringwald */ 16172308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1618a0ffb263SMatthias Ringwald hfp_connection->bnip_type = value; 1619667ec068SMatthias Ringwald break; 162028a4aea3SMilanka Ringwald case 3: 162128a4aea3SMilanka Ringwald // <speed>: This optional field is not supported, and shall be left blank. 162228a4aea3SMilanka Ringwald break; 162328a4aea3SMilanka Ringwald case 4: 162428a4aea3SMilanka Ringwald // <service>: Indicates which service this phone number relates to. Shall be either 4 (voice) or 5 (fax). 1625667ec068SMatthias Ringwald default: 1626667ec068SMatthias Ringwald break; 1627667ec068SMatthias Ringwald } 1628eed67a37SMilanka Ringwald // index > 2 are ignored in switch above 1629a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1630667ec068SMatthias Ringwald break; 1631667ec068SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 1632a0ffb263SMatthias Ringwald switch(hfp_connection->parser_item_index){ 1633667ec068SMatthias Ringwald case 0: 16342308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1635a0ffb263SMatthias Ringwald hfp_connection->clcc_idx = value; 1636667ec068SMatthias Ringwald break; 1637667ec068SMatthias Ringwald case 1: 16382308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1639a0ffb263SMatthias Ringwald hfp_connection->clcc_dir = value; 1640667ec068SMatthias Ringwald break; 1641667ec068SMatthias Ringwald case 2: 16422308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1643a0ffb263SMatthias Ringwald hfp_connection->clcc_status = value; 1644667ec068SMatthias Ringwald break; 1645667ec068SMatthias Ringwald case 3: 16462308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 16470aee97efSMilanka Ringwald hfp_connection->clcc_mode = value; 1648667ec068SMatthias Ringwald break; 1649667ec068SMatthias Ringwald case 4: 16500aee97efSMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 16510aee97efSMilanka Ringwald hfp_connection->clcc_mpty = value; 16520aee97efSMilanka Ringwald break; 16530aee97efSMilanka Ringwald case 5: 1654c10fde09SMatthias Ringwald btstack_strcpy(hfp_connection->bnip_number, sizeof(hfp_connection->bnip_number), (char *)hfp_connection->line_buffer); 1655667ec068SMatthias Ringwald break; 16560aee97efSMilanka Ringwald case 6: 16572308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1658a0ffb263SMatthias Ringwald hfp_connection->bnip_type = value; 1659667ec068SMatthias Ringwald break; 1660667ec068SMatthias Ringwald default: 1661667ec068SMatthias Ringwald break; 1662667ec068SMatthias Ringwald } 1663eed67a37SMilanka Ringwald // index > 6 are ignored in switch above 1664a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1665667ec068SMatthias Ringwald break; 1666aa4dd815SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 16672308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1668a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = value; 1669aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CMD_SET_MICROPHONE_GAIN %d\n", value); 1670aa4dd815SMatthias Ringwald break; 1671aa4dd815SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 16722308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1673a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = value; 1674aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CMD_SET_SPEAKER_GAIN %d\n", value); 1675aa4dd815SMatthias Ringwald break; 1676aa4dd815SMatthias Ringwald case HFP_CMD_TURN_OFF_EC_AND_NR: 16772308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1678a0ffb263SMatthias Ringwald hfp_connection->ag_echo_and_noise_reduction = value; 1679aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CMD_TURN_OFF_EC_AND_NR %d\n", value); 1680aa4dd815SMatthias Ringwald break; 1681aa4dd815SMatthias Ringwald case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING: 16822308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1683a0ffb263SMatthias Ringwald hfp_connection->remote_supported_features = store_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE, value); 1684aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CHANGE_IN_BAND_RING_TONE_SETTING %d\n", value); 1685aa4dd815SMatthias Ringwald break; 16863deb3ec6SMatthias Ringwald case HFP_CMD_HF_CONFIRMED_CODEC: 16872308e108SMilanka Ringwald hfp_connection->codec_confirmed = btstack_atoi((char*)hfp_connection->line_buffer); 1688a0ffb263SMatthias Ringwald log_info("hfp parse HFP_CMD_HF_CONFIRMED_CODEC %d\n", hfp_connection->codec_confirmed); 16893deb3ec6SMatthias Ringwald break; 16903deb3ec6SMatthias Ringwald case HFP_CMD_AG_SUGGESTED_CODEC: 16912308e108SMilanka Ringwald hfp_connection->suggested_codec = btstack_atoi((char*)hfp_connection->line_buffer); 1692a0ffb263SMatthias Ringwald log_info("hfp parse HFP_CMD_AG_SUGGESTED_CODEC %d\n", hfp_connection->suggested_codec); 16933deb3ec6SMatthias Ringwald break; 16943deb3ec6SMatthias Ringwald case HFP_CMD_SUPPORTED_FEATURES: 16952308e108SMilanka Ringwald hfp_connection->remote_supported_features = btstack_atoi((char*)hfp_connection->line_buffer); 1696bace42efSMatthias Ringwald log_info("Parsed supported feature %d\n", (int) hfp_connection->remote_supported_features); 16973deb3ec6SMatthias Ringwald break; 16983deb3ec6SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 1699a0ffb263SMatthias Ringwald log_info("Parsed codec %s\n", hfp_connection->line_buffer); 1700ab2445a0SMatthias Ringwald hfp_connection->remote_codecs[hfp_connection->parser_item_index] = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 170125789943SMilanka Ringwald hfp_next_codec_index(hfp_connection); 1702a0ffb263SMatthias Ringwald hfp_connection->remote_codecs_nr = hfp_connection->parser_item_index; 17033deb3ec6SMatthias Ringwald break; 1704aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 1705c10fde09SMatthias Ringwald btstack_strcpy((char *)hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, HFP_MAX_INDICATOR_DESC_SIZE, (char *)hfp_connection->line_buffer); 1706a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].index = hfp_connection->parser_item_index+1; 1707a0ffb263SMatthias Ringwald log_info("Indicator %d: %s (", hfp_connection->ag_indicators_nr+1, hfp_connection->line_buffer); 1708aa4dd815SMatthias Ringwald break; 1709aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 1710a0ffb263SMatthias Ringwald log_info("Parsed Indicator %d with status: %s\n", hfp_connection->parser_item_index+1, hfp_connection->line_buffer); 17112308e108SMilanka Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = btstack_atoi((char *) hfp_connection->line_buffer); 171225789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 17133deb3ec6SMatthias Ringwald break; 17143deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 171525789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 1716a0ffb263SMatthias Ringwald if (hfp_connection->parser_item_index != 4) break; 1717a0ffb263SMatthias Ringwald log_info("Parsed Enable indicators: %s\n", hfp_connection->line_buffer); 17182308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1719a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = (uint8_t) value; 17203deb3ec6SMatthias Ringwald break; 17213deb3ec6SMatthias Ringwald case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES: 1722a0ffb263SMatthias Ringwald log_info("Parsed Support call hold: %s\n", hfp_connection->line_buffer); 1723a0ffb263SMatthias Ringwald if (hfp_connection->line_size > 2 ) break; 172497d2cfbcSMatthias Ringwald memcpy((char *)hfp_connection->remote_call_services[hfp_connection->remote_call_services_index].name, (char *)hfp_connection->line_buffer, HFP_CALL_SERVICE_SIZE-1); 172525789943SMilanka Ringwald hfp_connection->remote_call_services[hfp_connection->remote_call_services_index].name[HFP_CALL_SERVICE_SIZE - 1] = 0; 1726eed67a37SMilanka Ringwald hfp_next_remote_call_services_index(hfp_connection); 17273deb3ec6SMatthias Ringwald break; 1728aa4dd815SMatthias Ringwald case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 1729aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 1730a0ffb263SMatthias Ringwald log_info("Parsed Generic status indicator: %s\n", hfp_connection->line_buffer); 17312308e108SMilanka Ringwald hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].uuid = (uint16_t)btstack_atoi((char*)hfp_connection->line_buffer); 173225789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 1733a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators_nr = hfp_connection->parser_item_index; 17343deb3ec6SMatthias Ringwald break; 1735aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 17363deb3ec6SMatthias Ringwald // HF parses inital AG gen. ind. state 1737a0ffb263SMatthias Ringwald log_info("Parsed List generic status indicator %s state: ", hfp_connection->line_buffer); 173825789943SMilanka Ringwald hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection); 17393deb3ec6SMatthias Ringwald break; 1740ce263fc8SMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 174125789943SMilanka Ringwald hfp_connection->parser_indicator_index = hfp_parse_indicator_index(hfp_connection); 1742a0ffb263SMatthias Ringwald log_info("Parsed HF indicator index %u", hfp_connection->parser_indicator_index); 1743ce263fc8SMatthias Ringwald break; 17443deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE: 17453deb3ec6SMatthias Ringwald // AG parses new gen. ind. state 1746a0ffb263SMatthias Ringwald if (hfp_connection->ignore_value){ 1747a0ffb263SMatthias Ringwald hfp_connection->ignore_value = 0; 1748a0ffb263SMatthias Ringwald log_info("Parsed Enable AG indicator pos %u('%s') - unchanged (stays %u)\n", hfp_connection->parser_item_index, 1749a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled); 1750ce263fc8SMatthias Ringwald } 1751a0ffb263SMatthias Ringwald else if (hfp_connection->ag_indicators[hfp_connection->parser_item_index].mandatory){ 1752ce263fc8SMatthias Ringwald log_info("Parsed Enable AG indicator pos %u('%s') - ignore (mandatory)\n", 1753a0ffb263SMatthias Ringwald hfp_connection->parser_item_index, hfp_connection->ag_indicators[hfp_connection->parser_item_index].name); 1754ce263fc8SMatthias Ringwald } else { 17552308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1756a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled = value; 1757a0ffb263SMatthias Ringwald log_info("Parsed Enable AG indicator pos %u('%s'): %u\n", hfp_connection->parser_item_index, 1758a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, value); 17593deb3ec6SMatthias Ringwald } 176025789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 17613deb3ec6SMatthias Ringwald break; 17623deb3ec6SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 17633deb3ec6SMatthias Ringwald // indicators are indexed starting with 1 176425789943SMilanka Ringwald hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection); 1765a0ffb263SMatthias Ringwald log_info("Parsed status of the AG indicator %d, status ", hfp_connection->parser_item_index); 17663deb3ec6SMatthias Ringwald break; 1767aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 17682308e108SMilanka Ringwald hfp_connection->network_operator.mode = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1769a0ffb263SMatthias Ringwald log_info("Parsed network operator mode: %d, ", hfp_connection->network_operator.mode); 1770aa4dd815SMatthias Ringwald break; 1771aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 1772a0ffb263SMatthias Ringwald if (hfp_connection->line_buffer[0] == '3'){ 1773a0ffb263SMatthias Ringwald log_info("Parsed Set network operator format : %s, ", hfp_connection->line_buffer); 17743deb3ec6SMatthias Ringwald break; 17753deb3ec6SMatthias Ringwald } 17763deb3ec6SMatthias Ringwald // TODO emit ERROR, wrong format 1777a0ffb263SMatthias Ringwald log_info("ERROR Set network operator format: index %s not supported\n", hfp_connection->line_buffer); 17783deb3ec6SMatthias Ringwald break; 17793deb3ec6SMatthias Ringwald case HFP_CMD_ERROR: 17803deb3ec6SMatthias Ringwald break; 17813deb3ec6SMatthias Ringwald case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1782a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 1; 17832308e108SMilanka Ringwald hfp_connection->extended_audio_gateway_error_value = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 17843deb3ec6SMatthias Ringwald break; 17853deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR: 17862308e108SMilanka Ringwald hfp_connection->enable_extended_audio_gateway_error_report = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1787a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 17883deb3ec6SMatthias Ringwald break; 1789ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 1790a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1791a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1792c10fde09SMatthias Ringwald btstack_strcpy((char *)hfp_connection->bnip_number, sizeof(hfp_connection->bnip_number), (char *)hfp_connection->line_buffer); 17933deb3ec6SMatthias Ringwald break; 17940222a807SMatthias Ringwald case HFP_CMD_CALL_HOLD: 17950222a807SMatthias Ringwald hfp_connection->ag_call_hold_action = hfp_connection->line_buffer[0] - '0'; 17960222a807SMatthias Ringwald if (hfp_connection->line_buffer[1] != '\0'){ 17970222a807SMatthias Ringwald hfp_connection->call_index = btstack_atoi((char *)&hfp_connection->line_buffer[1]); 17980222a807SMatthias Ringwald } 17990222a807SMatthias Ringwald break; 18000222a807SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_COMMAND: 18010222a807SMatthias Ringwald hfp_connection->ag_response_and_hold_action = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 18020222a807SMatthias Ringwald break; 18030222a807SMatthias Ringwald case HFP_CMD_TRANSMIT_DTMF_CODES: 18040222a807SMatthias Ringwald hfp_connection->ag_dtmf_code = hfp_connection->line_buffer[0]; 18050222a807SMatthias Ringwald break; 18060222a807SMatthias Ringwald case HFP_CMD_ENABLE_CLIP: 18070222a807SMatthias Ringwald hfp_connection->clip_enabled = hfp_connection->line_buffer[0] != '0'; 18080222a807SMatthias Ringwald break; 18090222a807SMatthias Ringwald case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION: 18100222a807SMatthias Ringwald hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[0] != '0'; 18110222a807SMatthias Ringwald break; 18120b4debbfSMilanka Ringwald case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION: 18130b4debbfSMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 18140b4debbfSMilanka Ringwald hfp_connection->ag_activate_voice_recognition_value = value; 18150b4debbfSMilanka Ringwald break; 1816db3cdbd4SMilanka Ringwald case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION: 1817db3cdbd4SMilanka Ringwald switch(hfp_connection->parser_item_index){ 1818db3cdbd4SMilanka Ringwald case 0: 18190b4debbfSMilanka Ringwald hfp_connection->ag_vra_status = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1820db3cdbd4SMilanka Ringwald break; 1821db3cdbd4SMilanka Ringwald case 1: 1822c78e7b73SMatthias Ringwald hfp_connection->ag_vra_state = (hfp_voice_recognition_state_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1823db3cdbd4SMilanka Ringwald break; 1824db3cdbd4SMilanka Ringwald case 2: 182545796ff1SMilanka Ringwald hfp_connection->ag_msg.text_id = 0; 1826db3cdbd4SMilanka Ringwald for (i = 0 ; i < 4; i++){ 182745796ff1SMilanka Ringwald hfp_connection->ag_msg.text_id = (hfp_connection->ag_msg.text_id << 4) | nibble_for_char(hfp_connection->line_buffer[i]); 1828db3cdbd4SMilanka Ringwald } 1829db3cdbd4SMilanka Ringwald break; 1830db3cdbd4SMilanka Ringwald case 3: 18310d854c7cSMilanka Ringwald hfp_connection->ag_msg.text_type = (hfp_text_type_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1832db3cdbd4SMilanka Ringwald break; 1833db3cdbd4SMilanka Ringwald case 4: 18340d854c7cSMilanka Ringwald hfp_connection->ag_msg.text_operation = (hfp_text_operation_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1835db3cdbd4SMilanka Ringwald break; 1836db3cdbd4SMilanka Ringwald case 5: 183751aa5d5aSMilanka Ringwald hfp_connection->line_buffer[hfp_connection->line_size] = 0; 183851aa5d5aSMilanka Ringwald hfp_connection->ag_vra_msg_length = hfp_connection->line_size + 1; 1839db3cdbd4SMilanka Ringwald break; 1840db3cdbd4SMilanka Ringwald default: 1841db3cdbd4SMilanka Ringwald break; 1842db3cdbd4SMilanka Ringwald } 1843ddbf29d2SMilanka Ringwald hfp_connection->parser_item_index++; 1844db3cdbd4SMilanka Ringwald break; 18453deb3ec6SMatthias Ringwald default: 18463deb3ec6SMatthias Ringwald break; 18473deb3ec6SMatthias Ringwald } 18483deb3ec6SMatthias Ringwald } 18493deb3ec6SMatthias Ringwald 18501d9c9c90SMilanka Ringwald static void hfp_handle_start_sdp_client_query(void * context){ 18511d9c9c90SMilanka Ringwald UNUSED(context); 18521d9c9c90SMilanka Ringwald 18531d9c9c90SMilanka Ringwald btstack_linked_list_iterator_t it; 18541d9c9c90SMilanka Ringwald btstack_linked_list_iterator_init(&it, &hfp_connections); 18551d9c9c90SMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 18561d9c9c90SMilanka Ringwald hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 18571d9c9c90SMilanka Ringwald 18581d9c9c90SMilanka Ringwald if (connection->state != HFP_W2_SEND_SDP_QUERY) continue; 18591d9c9c90SMilanka Ringwald 18601d9c9c90SMilanka Ringwald connection->state = HFP_W4_SDP_QUERY_COMPLETE; 1861aeb0f0feSMatthias Ringwald hfp_sdp_query_context.local_role = connection->local_role; 1862aeb0f0feSMatthias Ringwald (void)memcpy(hfp_sdp_query_context.remote_address, connection->remote_addr, 6); 1863e55c05e3SMatthias Ringwald sdp_client_query_rfcomm_channel_and_name_for_service_class_uuid(&handle_query_rfcomm_event, connection->remote_addr, connection->service_uuid); 18641d9c9c90SMilanka Ringwald return; 18651d9c9c90SMilanka Ringwald } 18661d9c9c90SMilanka Ringwald } 18671d9c9c90SMilanka Ringwald 18684eb3f1d8SMilanka Ringwald uint8_t hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid, hfp_role_t local_role){ 18694eb3f1d8SMilanka Ringwald hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr, local_role); 1870bb7f748cSMatthias Ringwald if (connection != NULL){ 1871bb7f748cSMatthias Ringwald // allow to call hfp_establish_service_level_connection after SLC was triggered remotely 1872bb7f748cSMatthias Ringwald // @note this also allows to call hfp_establish_service_level_connection again before SLC is complete 1873bb7f748cSMatthias Ringwald if (connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){ 1874bb7f748cSMatthias Ringwald return ERROR_CODE_SUCCESS; 1875bb7f748cSMatthias Ringwald } else { 18764eb3f1d8SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 18773deb3ec6SMatthias Ringwald } 1878bb7f748cSMatthias Ringwald } 18794eb3f1d8SMilanka Ringwald 18804eb3f1d8SMilanka Ringwald connection = hfp_create_connection(bd_addr, local_role); 18814eb3f1d8SMilanka Ringwald if (!connection){ 18824eb3f1d8SMilanka Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 18834eb3f1d8SMilanka Ringwald } 18844eb3f1d8SMilanka Ringwald 18854eb3f1d8SMilanka Ringwald connection->state = HFP_W2_SEND_SDP_QUERY; 18864eb3f1d8SMilanka Ringwald 18874eb3f1d8SMilanka Ringwald bd_addr_copy(connection->remote_addr, bd_addr); 18884eb3f1d8SMilanka Ringwald connection->service_uuid = service_uuid; 18891d9c9c90SMilanka Ringwald 1890*94af8b0eSMatthias Ringwald if (local_role == HFP_ROLE_HF) { 1891*94af8b0eSMatthias Ringwald // setup HF Indicators 1892*94af8b0eSMatthias Ringwald uint8_t i; 1893*94af8b0eSMatthias Ringwald for (i=0; i < hfp_hf_indicators_nr; i++){ 1894*94af8b0eSMatthias Ringwald connection->generic_status_indicators[i].uuid = hfp_hf_indicators[i]; 1895*94af8b0eSMatthias Ringwald connection->generic_status_indicators[i].state = 0; 1896*94af8b0eSMatthias Ringwald } 1897*94af8b0eSMatthias Ringwald } 1898*94af8b0eSMatthias Ringwald 1899aeb0f0feSMatthias Ringwald hfp_sdp_query_request.callback = &hfp_handle_start_sdp_client_query; 19001d9c9c90SMilanka Ringwald // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback 1901aeb0f0feSMatthias Ringwald (void) sdp_client_register_query_callback(&hfp_sdp_query_request); 19024eb3f1d8SMilanka Ringwald return ERROR_CODE_SUCCESS; 19033deb3ec6SMatthias Ringwald } 19043deb3ec6SMatthias Ringwald 1905657bc59fSMilanka Ringwald void hfp_trigger_release_service_level_connection(hfp_connection_t * hfp_connection){ 19061ffa0dd9SMilanka Ringwald // called internally, NULL check already performed 1907657bc59fSMilanka Ringwald btstack_assert(hfp_connection != NULL); 19083deb3ec6SMatthias Ringwald 1909657bc59fSMilanka Ringwald hfp_trigger_release_audio_connection(hfp_connection); 1910657bc59fSMilanka Ringwald if (hfp_connection->state < HFP_W4_RFCOMM_CONNECTED){ 1911657bc59fSMilanka Ringwald hfp_connection->state = HFP_IDLE; 19123deb3ec6SMatthias Ringwald return; 19133deb3ec6SMatthias Ringwald } 19143deb3ec6SMatthias Ringwald 1915657bc59fSMilanka Ringwald if (hfp_connection->state == HFP_W4_RFCOMM_CONNECTED){ 1916657bc59fSMilanka Ringwald hfp_connection->state = HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN; 19173deb3ec6SMatthias Ringwald return; 19183deb3ec6SMatthias Ringwald } 191952cbdd6dSMilanka Ringwald hfp_connection->release_slc_connection = 1; 1920657bc59fSMilanka Ringwald if (hfp_connection->state < HFP_W4_SCO_CONNECTED){ 1921657bc59fSMilanka Ringwald hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM; 19220c3047fbSMatthias Ringwald return; 19230c3047fbSMatthias Ringwald } 19240c3047fbSMatthias Ringwald 1925657bc59fSMilanka Ringwald if (hfp_connection->state < HFP_W4_SCO_DISCONNECTED){ 1926657bc59fSMilanka Ringwald hfp_connection->state = HFP_W2_DISCONNECT_SCO; 192752cbdd6dSMilanka Ringwald hfp_connection->release_audio_connection = 1; 19280c3047fbSMatthias Ringwald return; 19290c3047fbSMatthias Ringwald } 19303deb3ec6SMatthias Ringwald } 19313deb3ec6SMatthias Ringwald 19320b4debbfSMilanka Ringwald uint8_t hfp_trigger_release_audio_connection(hfp_connection_t * hfp_connection){ 19331ffa0dd9SMilanka Ringwald // called internally, NULL check already performed 1934657bc59fSMilanka Ringwald btstack_assert(hfp_connection != NULL); 19350b4debbfSMilanka Ringwald if (hfp_connection->state <= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){ 19360b4debbfSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 19371ffa0dd9SMilanka Ringwald } 19380b4debbfSMilanka Ringwald switch (hfp_connection->state) { 19390b4debbfSMilanka Ringwald case HFP_W2_CONNECT_SCO: 19400b4debbfSMilanka Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 19410b4debbfSMilanka Ringwald break; 19420b4debbfSMilanka Ringwald case HFP_W4_SCO_CONNECTED: 19430b4debbfSMilanka Ringwald case HFP_AUDIO_CONNECTION_ESTABLISHED: 19440b4debbfSMilanka Ringwald hfp_connection->release_audio_connection = 1; 19450b4debbfSMilanka Ringwald break; 19460b4debbfSMilanka Ringwald default: 19470b4debbfSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 19480b4debbfSMilanka Ringwald } 19490b4debbfSMilanka Ringwald return ERROR_CODE_SUCCESS; 19503deb3ec6SMatthias Ringwald } 19513deb3ec6SMatthias Ringwald 19526b3901f2SMatthias Ringwald bool hfp_sco_setup_active(void){ 19536b3901f2SMatthias Ringwald return hfp_sco_establishment_active != NULL; 19546b3901f2SMatthias Ringwald } 19556b3901f2SMatthias Ringwald 1956eddcd308SMatthias Ringwald void hfp_setup_synchronous_connection(hfp_connection_t * hfp_connection){ 1957447743f7SMatthias Ringwald 1958447743f7SMatthias Ringwald hfp_sco_establishment_active = hfp_connection; 1959447743f7SMatthias Ringwald 1960ce263fc8SMatthias Ringwald // all packet types, fixed bandwidth 1961eddcd308SMatthias Ringwald int setting = hfp_connection->link_setting; 1962ce263fc8SMatthias Ringwald log_info("hfp_setup_synchronous_connection using setting nr %u", setting); 1963d2c1d59aSMatthias Ringwald uint16_t sco_voice_setting = hci_get_sco_voice_setting(); 1964d393ed5aSMatthias Ringwald if (hfp_connection->negotiated_codec != HFP_CODEC_CVSD){ 1965689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 1966689d4323SMatthias Ringwald sco_voice_setting = 0x0063; // Transparent data, 16-bit for BCM controllers 1967689d4323SMatthias Ringwald #else 1968689d4323SMatthias Ringwald sco_voice_setting = 0x0043; // Transparent data, 8-bit otherwise 1969689d4323SMatthias Ringwald #endif 1970d2c1d59aSMatthias Ringwald } 1971d9290e95SMatthias Ringwald uint16_t packet_types = hfp_link_settings[setting].packet_types; 1972d9290e95SMatthias Ringwald hfp_connection->packet_types = packet_types; 1973d9290e95SMatthias Ringwald 1974352a0504SMatthias Ringwald // get packet types - bits 6-9 are 'don't allow' 1975d9290e95SMatthias Ringwald uint16_t packet_types_flipped = packet_types ^ 0x03c0; 197677d63549SMatthias Ringwald #if defined(ENABLE_SCO_OVER_PCM) && defined(ENABLE_NXP_PCM_WBS) 197777d63549SMatthias Ringwald uint8_t radio_coding_format = 3; 197877d63549SMatthias Ringwald uint32_t host_bandwidth = 0; 197977d63549SMatthias Ringwald uint8_t coded_data_size = 0; 198077d63549SMatthias Ringwald switch (hfp_connection->negotiated_codec){ 198177d63549SMatthias Ringwald case HFP_CODEC_CVSD: 198277d63549SMatthias Ringwald radio_coding_format = 0x02; 198377d63549SMatthias Ringwald host_bandwidth = 16000; 198477d63549SMatthias Ringwald coded_data_size = 0x10; 198577d63549SMatthias Ringwald break; 198677d63549SMatthias Ringwald case HFP_CODEC_MSBC: 198777d63549SMatthias Ringwald radio_coding_format = 0x05; 198877d63549SMatthias Ringwald host_bandwidth = 32000; 198977d63549SMatthias Ringwald coded_data_size = 0x08; 199077d63549SMatthias Ringwald break; 199177d63549SMatthias Ringwald default: 199277d63549SMatthias Ringwald log_error("Coding format %u not supported by Controller", hfp_connection->negotiated_codec); 199377d63549SMatthias Ringwald btstack_assert(false); 199477d63549SMatthias Ringwald break; 199577d63549SMatthias Ringwald } 199677d63549SMatthias Ringwald hci_send_cmd(&hci_enhanced_setup_synchronous_connection, 199777d63549SMatthias Ringwald // ACL Handle 199877d63549SMatthias Ringwald hfp_connection->acl_handle, 199977d63549SMatthias Ringwald // Transmit_Bandwidth 200077d63549SMatthias Ringwald 8000, 200177d63549SMatthias Ringwald // Receive_Bandwidth 200277d63549SMatthias Ringwald 8000, 200377d63549SMatthias Ringwald // Transmit_Coding_Format: radio_config_format, company, codec 200477d63549SMatthias Ringwald radio_coding_format, 0x00, 0x00, 200577d63549SMatthias Ringwald // Receive_Coding_Format: radio_config_format, company, codec 200677d63549SMatthias Ringwald radio_coding_format, 0x00, 0x00, 200777d63549SMatthias Ringwald // Transmit_Codec_Frame_Size 200877d63549SMatthias Ringwald 0x3c, 200977d63549SMatthias Ringwald // Receive_Codec_Frame_Size 201077d63549SMatthias Ringwald 0x3c, 201177d63549SMatthias Ringwald // Input_Bandwidth 201277d63549SMatthias Ringwald host_bandwidth, 201377d63549SMatthias Ringwald // Output_Bandwidth 201477d63549SMatthias Ringwald host_bandwidth, 201577d63549SMatthias Ringwald // Input_Coding_Format, 0x04 = Linear PCM, company, codec 201677d63549SMatthias Ringwald 0x04, 0x00, 0x00, 201777d63549SMatthias Ringwald // Output_Coding_Format, 0x04 = Linear PCM, company, codec 201877d63549SMatthias Ringwald 0x04, 0x00, 0x00, 201977d63549SMatthias Ringwald // Input_Coded_Data_Size 202077d63549SMatthias Ringwald coded_data_size, 202177d63549SMatthias Ringwald // Output_Coded_Data_Size 202277d63549SMatthias Ringwald coded_data_size, 202377d63549SMatthias Ringwald // Input_PCM_Data_Format, 0x02 = 2’s complement 202477d63549SMatthias Ringwald 0x02, 202577d63549SMatthias Ringwald // Output_PCM_Data_Format, 0x02 = 2’s complement 202677d63549SMatthias Ringwald 0x02, 202777d63549SMatthias Ringwald // Input_PCM_Sample_Payload_MSB_Position 202877d63549SMatthias Ringwald 0x00, 202977d63549SMatthias Ringwald // Output_PCM_Sample_Payload_MSB_Position 203077d63549SMatthias Ringwald 0x00, 203177d63549SMatthias Ringwald // Input_Data_Path - vendor specific: NXP - I2S/PCM 203277d63549SMatthias Ringwald 0x01, 203377d63549SMatthias Ringwald // Output_Data_Path - vendor specific: NXP - I2S/PCM 203477d63549SMatthias Ringwald 0x01, 203577d63549SMatthias Ringwald // Input_Transport_Unit_Size 203677d63549SMatthias Ringwald 0x10, 203777d63549SMatthias Ringwald // Output_Transport_Unit_Size 203877d63549SMatthias Ringwald 0x10, 203977d63549SMatthias Ringwald // 204077d63549SMatthias Ringwald hfp_link_settings[setting].max_latency, 204177d63549SMatthias Ringwald packet_types_flipped, 204277d63549SMatthias Ringwald hfp_link_settings[setting].retransmission_effort); 204377d63549SMatthias Ringwald #else 2044eddcd308SMatthias Ringwald hci_send_cmd(&hci_setup_synchronous_connection, hfp_connection->acl_handle, 8000, 8000, hfp_link_settings[setting].max_latency, 2045d9290e95SMatthias Ringwald sco_voice_setting, hfp_link_settings[setting].retransmission_effort, packet_types_flipped); 204677d63549SMatthias Ringwald #endif 2047ce263fc8SMatthias Ringwald } 2048d68dcce1SMatthias Ringwald 2049c169b70dSMatthias Ringwald #ifdef ENABLE_HFP_HF_SAFE_SETTINGS 205032459254SMatthias Ringwald hfp_link_settings_t hfp_safe_settings_for_context(bool use_eSCO, uint8_t negotiated_codec, bool secure_connection_in_use){ 205132459254SMatthias Ringwald uint8_t i; 205232459254SMatthias Ringwald hfp_link_settings_t link_setting = HFP_LINK_SETTINGS_NONE; 205332459254SMatthias Ringwald for (i=0 ; i < (sizeof(hfp_mandatory_safe_settings) / sizeof(struct hfp_mandatory_safe_setting)) ; i++){ 205432459254SMatthias Ringwald uint16_t packet_types = hfp_link_settings[(uint8_t)(hfp_mandatory_safe_settings[i].link_setting)].packet_types; 205532459254SMatthias Ringwald bool is_eSCO_setting = (packet_types & SCO_PACKET_TYPES_ESCO) != 0; 205632459254SMatthias Ringwald if (is_eSCO_setting != use_eSCO) continue; 205732459254SMatthias Ringwald if ((hfp_mandatory_safe_settings[i].codec_mask & (1 << negotiated_codec)) == 0) continue; 205832459254SMatthias Ringwald if (hfp_mandatory_safe_settings[i].secure_connection_in_use != secure_connection_in_use) continue; 205932459254SMatthias Ringwald link_setting = hfp_mandatory_safe_settings[i].link_setting; 206032459254SMatthias Ringwald break; 206132459254SMatthias Ringwald } 206232459254SMatthias Ringwald return link_setting; 206332459254SMatthias Ringwald } 2064c169b70dSMatthias Ringwald #endif 206532459254SMatthias Ringwald 20663ad75286SMatthias Ringwald void hfp_accept_synchronous_connection(hfp_connection_t * hfp_connection, bool use_eSCO){ 20673ad75286SMatthias Ringwald 2068447743f7SMatthias Ringwald hfp_sco_establishment_active = hfp_connection; 2069447743f7SMatthias Ringwald 207032459254SMatthias Ringwald // lookup safe settings based on SCO type, SC use and Codec type 2071715d9d6aSMatthias Ringwald uint16_t max_latency; 2072715d9d6aSMatthias Ringwald uint16_t packet_types; 2073715d9d6aSMatthias Ringwald uint16_t retransmission_effort; 207432459254SMatthias Ringwald 2075c169b70dSMatthias Ringwald #ifdef ENABLE_HFP_HF_SAFE_SETTINGS 2076577ad333SMatthias Ringwald hfp_link_settings_t link_setting = HFP_LINK_SETTINGS_NONE; 2077715d9d6aSMatthias Ringwald // fallback for non-CVSD codec and SCO connection 2078715d9d6aSMatthias Ringwald if ((hfp_connection->negotiated_codec != HFP_CODEC_CVSD) && (use_eSCO == false)){ 2079715d9d6aSMatthias Ringwald max_latency = 0xffff; 2080715d9d6aSMatthias Ringwald retransmission_effort = 0xff; 2081715d9d6aSMatthias Ringwald packet_types = SCO_PACKET_TYPES_HV3 | SCO_PACKET_TYPES_HV1; 2082715d9d6aSMatthias Ringwald } else { 2083c169b70dSMatthias Ringwald // use safe settings from HFP v1.9, table 6.10 2084c169b70dSMatthias Ringwald bool secure_connection_in_use = gap_secure_connection(hfp_connection->acl_handle); 2085715d9d6aSMatthias Ringwald link_setting = hfp_safe_settings_for_context(use_eSCO, hfp_connection->negotiated_codec, secure_connection_in_use); 2086715d9d6aSMatthias Ringwald max_latency = hfp_link_settings[(uint8_t) link_setting].max_latency; 2087715d9d6aSMatthias Ringwald retransmission_effort = hfp_link_settings[(uint8_t) link_setting].retransmission_effort; 2088715d9d6aSMatthias Ringwald packet_types = hfp_link_settings[(uint8_t) link_setting].packet_types; 2089715d9d6aSMatthias Ringwald } 2090c169b70dSMatthias Ringwald #else 2091c169b70dSMatthias Ringwald max_latency = 0xffff; 2092c169b70dSMatthias Ringwald retransmission_effort = 0xff; 2093c169b70dSMatthias Ringwald if (use_eSCO) { 2094c169b70dSMatthias Ringwald packet_types = SCO_PACKET_TYPES_EV3 | SCO_PACKET_TYPES_2EV3; 2095c169b70dSMatthias Ringwald } else { 2096c169b70dSMatthias Ringwald packet_types = SCO_PACKET_TYPES_HV3 | SCO_PACKET_TYPES_HV1; 2097c169b70dSMatthias Ringwald } 2098c169b70dSMatthias Ringwald #endif 20993ad75286SMatthias Ringwald 21003ad75286SMatthias Ringwald // transparent data for non-CVSD connections or if codec provided by Controller 2101cb81d35dSMatthias Ringwald uint16_t sco_voice_setting = hci_get_sco_voice_setting(); 2102d393ed5aSMatthias Ringwald if (hfp_connection->negotiated_codec != HFP_CODEC_CVSD){ 2103cb81d35dSMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 2104cb81d35dSMatthias Ringwald sco_voice_setting = 0x0063; // Transparent data, 16-bit for BCM controllers 2105cb81d35dSMatthias Ringwald #else 2106cb81d35dSMatthias Ringwald sco_voice_setting = 0x0043; // Transparent data, 8-bit otherwise 2107cb81d35dSMatthias Ringwald #endif 2108cb81d35dSMatthias Ringwald } 2109cb81d35dSMatthias Ringwald 2110cb81d35dSMatthias Ringwald // filter packet types 211132459254SMatthias Ringwald packet_types &= hfp_get_sco_packet_types(); 211232459254SMatthias Ringwald 2113d9290e95SMatthias Ringwald hfp_connection->packet_types = packet_types; 2114d9290e95SMatthias Ringwald 2115cb81d35dSMatthias Ringwald // bits 6-9 are 'don't allow' 2116d9290e95SMatthias Ringwald uint16_t packet_types_flipped = packet_types ^ 0x3c0; 2117cb81d35dSMatthias Ringwald 2118577ad333SMatthias Ringwald log_info("Sending hci_accept_connection_request: packet types 0x%04x, sco_voice_setting 0x%02x", 2119577ad333SMatthias Ringwald packet_types, sco_voice_setting); 212032459254SMatthias Ringwald 212177d63549SMatthias Ringwald #if defined(ENABLE_SCO_OVER_PCM) && defined(ENABLE_NXP_PCM_WBS) 212277d63549SMatthias Ringwald uint8_t radio_coding_format = 3; 212377d63549SMatthias Ringwald uint32_t host_bandwidth = 0; 212477d63549SMatthias Ringwald uint8_t coded_data_size = 0; 212577d63549SMatthias Ringwald switch (hfp_connection->negotiated_codec){ 212677d63549SMatthias Ringwald case HFP_CODEC_CVSD: 212777d63549SMatthias Ringwald radio_coding_format = 0x02; 212877d63549SMatthias Ringwald host_bandwidth = 16000; 212977d63549SMatthias Ringwald coded_data_size = 0x10; 213077d63549SMatthias Ringwald break; 213177d63549SMatthias Ringwald case HFP_CODEC_MSBC: 213277d63549SMatthias Ringwald radio_coding_format = 0x05; 213377d63549SMatthias Ringwald host_bandwidth = 32000; 213477d63549SMatthias Ringwald coded_data_size = 0x08; 213577d63549SMatthias Ringwald break; 213677d63549SMatthias Ringwald default: 213777d63549SMatthias Ringwald log_error("Coding format %u not supported by Controller", hfp_connection->negotiated_codec); 213877d63549SMatthias Ringwald btstack_assert(false); 213977d63549SMatthias Ringwald break; 214077d63549SMatthias Ringwald } 214177d63549SMatthias Ringwald hci_send_cmd(&hci_enhanced_accept_synchronous_connection, 214277d63549SMatthias Ringwald // BD_ADDR 214377d63549SMatthias Ringwald hfp_connection->remote_addr, 214477d63549SMatthias Ringwald // Transmit_Bandwidth 214577d63549SMatthias Ringwald 8000, 214677d63549SMatthias Ringwald // Receive_Bandwidth 214777d63549SMatthias Ringwald 8000, 214877d63549SMatthias Ringwald // Transmit_Coding_Format: radio_config_format, company, codec 214977d63549SMatthias Ringwald radio_coding_format, 0x00, 0x00, 215077d63549SMatthias Ringwald // Receive_Coding_Format: radio_config_format, company, codec 215177d63549SMatthias Ringwald radio_coding_format, 0x00, 0x00, 215277d63549SMatthias Ringwald // Transmit_Codec_Frame_Size 215377d63549SMatthias Ringwald 0x3c, 215477d63549SMatthias Ringwald // Receive_Codec_Frame_Size 215577d63549SMatthias Ringwald 0x3c, 215677d63549SMatthias Ringwald // Input_Bandwidth 215777d63549SMatthias Ringwald host_bandwidth, 215877d63549SMatthias Ringwald // Output_Bandwidth 215977d63549SMatthias Ringwald host_bandwidth, 216077d63549SMatthias Ringwald // Input_Coding_Format, 0x04 = Linear PCM, company, codec 216177d63549SMatthias Ringwald 0x04, 0x00, 0x00, 216277d63549SMatthias Ringwald // Output_Coding_Format, 0x04 = Linear PCM, company, codec 216377d63549SMatthias Ringwald 0x04, 0x00, 0x00, 216477d63549SMatthias Ringwald // Input_Coded_Data_Size 216577d63549SMatthias Ringwald coded_data_size, 216677d63549SMatthias Ringwald // Output_Coded_Data_Size 216777d63549SMatthias Ringwald coded_data_size, 216877d63549SMatthias Ringwald // Input_PCM_Data_Format, 0x02 = 2’s complement 216977d63549SMatthias Ringwald 0x02, 217077d63549SMatthias Ringwald // Output_PCM_Data_Format, 0x02 = 2’s complement 217177d63549SMatthias Ringwald 0x02, 217277d63549SMatthias Ringwald // Input_PCM_Sample_Payload_MSB_Position 217377d63549SMatthias Ringwald 0x00, 217477d63549SMatthias Ringwald // Output_PCM_Sample_Payload_MSB_Position 217577d63549SMatthias Ringwald 0x00, 217677d63549SMatthias Ringwald // Input_Data_Path - vendor specific: NXP - I2S/PCM 217777d63549SMatthias Ringwald 0x01, 217877d63549SMatthias Ringwald // Output_Data_Path - vendor specific: NXP - I2S/PCM 217977d63549SMatthias Ringwald 0x01, 218077d63549SMatthias Ringwald // Input_Transport_Unit_Size 218177d63549SMatthias Ringwald 0x10, 218277d63549SMatthias Ringwald // Output_Transport_Unit_Size 218377d63549SMatthias Ringwald 0x10, 218477d63549SMatthias Ringwald // 218577d63549SMatthias Ringwald max_latency, 218677d63549SMatthias Ringwald packet_types_flipped, 218777d63549SMatthias Ringwald retransmission_effort); 218877d63549SMatthias Ringwald #else 2189cb81d35dSMatthias Ringwald hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency, 2190d9290e95SMatthias Ringwald sco_voice_setting, retransmission_effort, packet_types_flipped); 219177d63549SMatthias Ringwald #endif 2192cb81d35dSMatthias Ringwald } 2193cb81d35dSMatthias Ringwald 21943721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP 21953721a235SMatthias Ringwald void hfp_cc256x_write_codec_config(hfp_connection_t * hfp_connection){ 21963721a235SMatthias Ringwald uint32_t sample_rate_hz; 21973721a235SMatthias Ringwald uint16_t clock_rate_khz; 21983721a235SMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 21993721a235SMatthias Ringwald clock_rate_khz = 512; 22003721a235SMatthias Ringwald sample_rate_hz = 16000; 22013721a235SMatthias Ringwald } else { 22023721a235SMatthias Ringwald clock_rate_khz = 256; 22033721a235SMatthias Ringwald sample_rate_hz = 8000; 22043721a235SMatthias Ringwald } 22053721a235SMatthias Ringwald uint8_t clock_direction = 0; // master 22063721a235SMatthias Ringwald uint16_t frame_sync_duty_cycle = 0; // i2s with 50% 22073721a235SMatthias Ringwald uint8_t frame_sync_edge = 1; // rising edge 22083721a235SMatthias Ringwald uint8_t frame_sync_polarity = 0; // active high 22093721a235SMatthias Ringwald uint8_t reserved = 0; 22103721a235SMatthias Ringwald uint16_t size = 16; 22113721a235SMatthias Ringwald uint16_t chan_1_offset = 1; 22123721a235SMatthias Ringwald uint16_t chan_2_offset = chan_1_offset + size; 22133721a235SMatthias Ringwald uint8_t out_edge = 1; // rising 22143721a235SMatthias Ringwald uint8_t in_edge = 0; // falling 22153721a235SMatthias Ringwald hci_send_cmd(&hci_ti_write_codec_config, clock_rate_khz, clock_direction, sample_rate_hz, frame_sync_duty_cycle, 22163721a235SMatthias Ringwald frame_sync_edge, frame_sync_polarity, reserved, 22173721a235SMatthias Ringwald size, chan_1_offset, out_edge, size, chan_1_offset, in_edge, reserved, 22183721a235SMatthias Ringwald size, chan_2_offset, out_edge, size, chan_2_offset, in_edge, reserved); 22193721a235SMatthias Ringwald } 22203721a235SMatthias Ringwald #endif 22213721a235SMatthias Ringwald 2222689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 2223689d4323SMatthias Ringwald void hfp_bcm_write_i2spcm_interface_param(hfp_connection_t * hfp_connection){ 2224689d4323SMatthias Ringwald uint8_t sample_rate = (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? 1 : 0; 22251d2bbd54SMatthias Ringwald // i2s enable, master, 8/16 kHz, 512 kHz 2226d12046f0SMatthias Ringwald hci_send_cmd(&hci_bcm_write_i2spcm_interface_param, 1, 1, sample_rate, 2); 2227689d4323SMatthias Ringwald } 2228689d4323SMatthias Ringwald #endif 2229689d4323SMatthias Ringwald 22300327df7dSMatthias Ringwald void hfp_prepare_for_sco(hfp_connection_t * hfp_connection){ 22313548b7cbSDirk Helbig UNUSED(hfp_connection); 22320327df7dSMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP 22330327df7dSMatthias Ringwald hfp_connection->cc256x_send_write_codec_config = true; 22340327df7dSMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 22350327df7dSMatthias Ringwald hfp_connection->cc256x_send_wbs_associate = true; 22360327df7dSMatthias Ringwald } 22373a8950c4SMatthias Ringwald #endif 22383a8950c4SMatthias Ringwald 22390327df7dSMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 224008a78038SMatthias Ringwald #ifndef HAVE_BCM_PCM_NBS_16KHZ 22410327df7dSMatthias Ringwald hfp_connection->bcm_send_write_i2spcm_interface_param = true; 224208a78038SMatthias Ringwald #endif 22430327df7dSMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 22440327df7dSMatthias Ringwald hfp_connection->bcm_send_enable_wbs = true; 22450327df7dSMatthias Ringwald } 22460327df7dSMatthias Ringwald #endif 22470327df7dSMatthias Ringwald 22480327df7dSMatthias Ringwald #ifdef ENABLE_RTK_PCM_WBS 22490327df7dSMatthias Ringwald hfp_connection->rtk_send_sco_config = true; 22500327df7dSMatthias Ringwald #endif 22510327df7dSMatthias Ringwald } 22520327df7dSMatthias Ringwald 22531b005648SMatthias Ringwald void hfp_set_hf_callback(btstack_packet_handler_t callback){ 22541b005648SMatthias Ringwald hfp_hf_callback = callback; 22551b005648SMatthias Ringwald } 22561b005648SMatthias Ringwald 22571b005648SMatthias Ringwald void hfp_set_ag_callback(btstack_packet_handler_t callback){ 22581b005648SMatthias Ringwald hfp_ag_callback = callback; 22591b005648SMatthias Ringwald } 22601b005648SMatthias Ringwald 2261ca59be51SMatthias Ringwald void hfp_set_ag_rfcomm_packet_handler(btstack_packet_handler_t handler){ 2262ca59be51SMatthias Ringwald hfp_ag_rfcomm_packet_handler = handler; 2263ca59be51SMatthias Ringwald } 22641b005648SMatthias Ringwald 2265ca59be51SMatthias Ringwald void hfp_set_hf_rfcomm_packet_handler(btstack_packet_handler_t handler){ 2266ca59be51SMatthias Ringwald hfp_hf_rfcomm_packet_handler = handler; 2267d68dcce1SMatthias Ringwald } 2268d68dcce1SMatthias Ringwald 226921df969bSMatthias Ringwald void hfp_set_hf_indicators(uint8_t indicators_nr, const uint8_t * indicators) { 227021df969bSMatthias Ringwald hfp_hf_indicators_nr = indicators_nr; 227121df969bSMatthias Ringwald hfp_hf_indicators = indicators; 227221df969bSMatthias Ringwald } 227321df969bSMatthias Ringwald 2274520c92d5SMatthias Ringwald void hfp_init(void){ 2275352a0504SMatthias Ringwald hfp_allowed_sco_packet_types = SCO_PACKET_TYPES_ALL; 2276991c26beSMatthias Ringwald } 2277991c26beSMatthias Ringwald 227820b2edb6SMatthias Ringwald void hfp_deinit(void){ 2279aeb0f0feSMatthias Ringwald hfp_allowed_sco_packet_types = 0; 228020b2edb6SMatthias Ringwald hfp_connections = NULL; 228120b2edb6SMatthias Ringwald hfp_hf_callback = NULL; 228220b2edb6SMatthias Ringwald hfp_ag_callback = NULL; 228320b2edb6SMatthias Ringwald hfp_hf_rfcomm_packet_handler = NULL; 228420b2edb6SMatthias Ringwald hfp_ag_rfcomm_packet_handler = NULL; 2285aeb0f0feSMatthias Ringwald hfp_sco_establishment_active = NULL; 2286471dea41SMatthias Ringwald hfp_custom_commands_ag = NULL; 22877404dce3SMatthias Ringwald hfp_custom_commands_hf = NULL; 2288aeb0f0feSMatthias Ringwald (void) memset(&hfp_sdp_query_context, 0, sizeof(hfp_sdp_query_context_t)); 2289aeb0f0feSMatthias Ringwald (void) memset(&hfp_sdp_query_request, 0, sizeof(btstack_context_callback_registration_t)); 229020b2edb6SMatthias Ringwald } 229120b2edb6SMatthias Ringwald 2292991c26beSMatthias Ringwald void hfp_set_sco_packet_types(uint16_t packet_types){ 229301e5b727SMatthias Ringwald hfp_allowed_sco_packet_types = packet_types; 2294991c26beSMatthias Ringwald } 2295991c26beSMatthias Ringwald 2296991c26beSMatthias Ringwald uint16_t hfp_get_sco_packet_types(void){ 229701e5b727SMatthias Ringwald return hfp_allowed_sco_packet_types; 2298520c92d5SMatthias Ringwald } 2299186dd3d2SMatthias Ringwald 2300b4320e7fSMatthias Ringwald hfp_link_settings_t hfp_next_link_setting(hfp_link_settings_t current_setting, uint16_t local_sco_packet_types, 2301b4320e7fSMatthias Ringwald uint16_t remote_sco_packet_types, bool eSCO_S4_supported, 2302b4320e7fSMatthias Ringwald uint8_t negotiated_codec) { 2303e453c1d9SMatthias Ringwald int8_t setting = (int8_t) current_setting; 2304e453c1d9SMatthias Ringwald while (setting > 0){ 2305e453c1d9SMatthias Ringwald setting--; 2306b4320e7fSMatthias Ringwald // skip S4 if not supported 2307e453c1d9SMatthias Ringwald if ((setting == (int8_t) HFP_LINK_SETTINGS_S4) && !eSCO_S4_supported) continue; 2308e453c1d9SMatthias Ringwald // skip wrong codec 230964126f36SMatthias Ringwald if ((hfp_link_settings[setting].codec_mask & (1 << negotiated_codec)) == 0) continue; 23101e70f50aSMatthias Ringwald // skip disabled or not supported packet types 2311352a0504SMatthias Ringwald uint16_t required_packet_types = hfp_link_settings[setting].packet_types; 2312b4320e7fSMatthias Ringwald uint16_t allowed_packet_types = hfp_allowed_sco_packet_types & local_sco_packet_types & remote_sco_packet_types; 2313352a0504SMatthias Ringwald if ((required_packet_types & allowed_packet_types) == 0) continue; 231401e5b727SMatthias Ringwald 2315e453c1d9SMatthias Ringwald // found matching setting 2316e453c1d9SMatthias Ringwald return (hfp_link_settings_t) setting; 2317afac2a04SMilanka Ringwald } 2318e453c1d9SMatthias Ringwald return HFP_LINK_SETTINGS_NONE; 2319afac2a04SMilanka Ringwald } 2320e453c1d9SMatthias Ringwald 23212b7abbfbSMatthias Ringwald static hfp_link_settings_t hfp_next_link_setting_for_connection(hfp_link_settings_t current_setting, hfp_connection_t * hfp_connection, uint8_t eSCO_S4_supported){ 2322e453c1d9SMatthias Ringwald uint8_t negotiated_codec = hfp_connection->negotiated_codec; 2323b4320e7fSMatthias Ringwald uint16_t local_sco_packet_types = hci_usable_sco_packet_types(); 2324b4320e7fSMatthias Ringwald uint16_t remote_sco_packet_types = hci_remote_sco_packet_types(hfp_connection->acl_handle); 2325b4320e7fSMatthias Ringwald return hfp_next_link_setting(current_setting, local_sco_packet_types, remote_sco_packet_types, eSCO_S4_supported, 2326b4320e7fSMatthias Ringwald negotiated_codec); 23272b7abbfbSMatthias Ringwald } 23282b7abbfbSMatthias Ringwald 23292b7abbfbSMatthias Ringwald void hfp_init_link_settings(hfp_connection_t * hfp_connection, uint8_t eSCO_S4_supported){ 2330e453c1d9SMatthias Ringwald // get highest possible link setting 23312b7abbfbSMatthias Ringwald hfp_connection->link_setting = hfp_next_link_setting_for_connection(HFP_LINK_SETTINGS_NONE, hfp_connection, eSCO_S4_supported); 2332afac2a04SMilanka Ringwald log_info("hfp_init_link_settings: %u", hfp_connection->link_setting); 2333afac2a04SMilanka Ringwald } 2334afac2a04SMilanka Ringwald 2335186dd3d2SMatthias Ringwald #define HFP_HF_RX_DEBUG_PRINT_LINE 80 2336186dd3d2SMatthias Ringwald 2337186dd3d2SMatthias Ringwald void hfp_log_rfcomm_message(const char * tag, uint8_t * packet, uint16_t size){ 2338186dd3d2SMatthias Ringwald #ifdef ENABLE_LOG_INFO 2339186dd3d2SMatthias Ringwald // encode \n\r 2340186dd3d2SMatthias Ringwald char printable[HFP_HF_RX_DEBUG_PRINT_LINE+2]; 234115a27967SMatthias Ringwald int i = 0; 2342186dd3d2SMatthias Ringwald int pos; 234315a27967SMatthias Ringwald for (pos=0 ; (pos < size) && (i < (HFP_HF_RX_DEBUG_PRINT_LINE - 3)) ; pos++){ 2344186dd3d2SMatthias Ringwald switch (packet[pos]){ 2345186dd3d2SMatthias Ringwald case '\n': 2346186dd3d2SMatthias Ringwald printable[i++] = '\\'; 2347186dd3d2SMatthias Ringwald printable[i++] = 'n'; 2348186dd3d2SMatthias Ringwald break; 2349186dd3d2SMatthias Ringwald case '\r': 2350186dd3d2SMatthias Ringwald printable[i++] = '\\'; 2351186dd3d2SMatthias Ringwald printable[i++] = 'r'; 2352186dd3d2SMatthias Ringwald break; 2353186dd3d2SMatthias Ringwald default: 2354186dd3d2SMatthias Ringwald printable[i++] = packet[pos]; 2355186dd3d2SMatthias Ringwald break; 2356186dd3d2SMatthias Ringwald } 2357186dd3d2SMatthias Ringwald } 2358186dd3d2SMatthias Ringwald printable[i] = 0; 2359186dd3d2SMatthias Ringwald log_info("%s: '%s'", tag, printable); 2360186dd3d2SMatthias Ringwald #endif 2361186dd3d2SMatthias Ringwald } 2362471dea41SMatthias Ringwald 2363471dea41SMatthias Ringwald void hfp_register_custom_ag_command(hfp_custom_at_command_t * custom_at_command){ 2364471dea41SMatthias Ringwald btstack_linked_list_add(&hfp_custom_commands_ag, (btstack_linked_item_t *) custom_at_command); 2365471dea41SMatthias Ringwald } 236625ffee39SMatthias Ringwald 23677404dce3SMatthias Ringwald void hfp_register_custom_hf_command(hfp_custom_at_command_t * custom_at_command){ 23687404dce3SMatthias Ringwald btstack_linked_list_add(&hfp_custom_commands_hf, (btstack_linked_item_t *) custom_at_command); 23697404dce3SMatthias Ringwald } 23707404dce3SMatthias Ringwald 237125ffee39SMatthias Ringwald // HFP H2 Synchronization - might get moved into a hfp_h2.c 237225ffee39SMatthias Ringwald 237325ffee39SMatthias Ringwald // find position of h2 sync header, returns -1 if not found, or h2 sync position 237425ffee39SMatthias Ringwald static int16_t hfp_h2_sync_find(const uint8_t * frame_data, uint16_t frame_len){ 237525ffee39SMatthias Ringwald uint16_t i; 237625ffee39SMatthias Ringwald for (i=0;i<(frame_len - 1);i++){ 237725ffee39SMatthias Ringwald // check: first byte == 1 237825ffee39SMatthias Ringwald uint8_t h2_first_byte = frame_data[i]; 237925ffee39SMatthias Ringwald if (h2_first_byte == 0x01) { 238025ffee39SMatthias Ringwald uint8_t h2_second_byte = frame_data[i + 1]; 238125ffee39SMatthias Ringwald // check lower nibble of second byte == 0x08 238225ffee39SMatthias Ringwald if ((h2_second_byte & 0x0F) == 8) { 238325ffee39SMatthias Ringwald // check if bits 0+2 == bits 1+3 238425ffee39SMatthias Ringwald uint8_t hn = h2_second_byte >> 4; 238525ffee39SMatthias Ringwald if (((hn >> 1) & 0x05) == (hn & 0x05)) { 238625ffee39SMatthias Ringwald return (int16_t) i; 238725ffee39SMatthias Ringwald } 238825ffee39SMatthias Ringwald } 238925ffee39SMatthias Ringwald } 239025ffee39SMatthias Ringwald } 239125ffee39SMatthias Ringwald return -1; 239225ffee39SMatthias Ringwald } 239325ffee39SMatthias Ringwald 239425ffee39SMatthias Ringwald static void hfp_h2_sync_reset(hfp_h2_sync_t * hfp_h2_sync){ 239525ffee39SMatthias Ringwald hfp_h2_sync->frame_len = 0; 239625ffee39SMatthias Ringwald } 239725ffee39SMatthias Ringwald 239825ffee39SMatthias Ringwald void hfp_h2_sync_init(hfp_h2_sync_t * hfp_h2_sync, 239925ffee39SMatthias Ringwald bool (*callback)(bool bad_frame, const uint8_t * frame_data, uint16_t frame_len)){ 240025ffee39SMatthias Ringwald hfp_h2_sync->callback = callback; 240125ffee39SMatthias Ringwald hfp_h2_sync->dropped_bytes = 0; 240225ffee39SMatthias Ringwald hfp_h2_sync_reset(hfp_h2_sync); 240325ffee39SMatthias Ringwald } 240425ffee39SMatthias Ringwald 240525ffee39SMatthias Ringwald static void hfp_h2_report_bad_frames(hfp_h2_sync_t *hfp_h2_sync){ 240625ffee39SMatthias Ringwald // report bad frames 240725ffee39SMatthias Ringwald while (hfp_h2_sync->dropped_bytes >= HFP_H2_SYNC_FRAME_SIZE){ 240825ffee39SMatthias Ringwald hfp_h2_sync->dropped_bytes -= HFP_H2_SYNC_FRAME_SIZE; 240925ffee39SMatthias Ringwald (void)(*hfp_h2_sync->callback)(true,NULL, HFP_H2_SYNC_FRAME_SIZE); 241025ffee39SMatthias Ringwald } 241125ffee39SMatthias Ringwald } 241225ffee39SMatthias Ringwald 241325ffee39SMatthias Ringwald static void hfp_h2_sync_drop_bytes(hfp_h2_sync_t * hfp_h2_sync, uint16_t bytes_to_drop){ 241425ffee39SMatthias Ringwald btstack_assert(bytes_to_drop <= hfp_h2_sync->frame_len); 241525ffee39SMatthias Ringwald memmove(hfp_h2_sync->frame_data, &hfp_h2_sync->frame_data[bytes_to_drop], hfp_h2_sync->frame_len - bytes_to_drop); 241625ffee39SMatthias Ringwald hfp_h2_sync->dropped_bytes += bytes_to_drop; 241725ffee39SMatthias Ringwald hfp_h2_sync->frame_len -= bytes_to_drop; 241825ffee39SMatthias Ringwald hfp_h2_report_bad_frames(hfp_h2_sync); 241925ffee39SMatthias Ringwald } 242025ffee39SMatthias Ringwald 242125ffee39SMatthias Ringwald void hfp_h2_sync_process(hfp_h2_sync_t *hfp_h2_sync, bool bad_frame, const uint8_t *frame_data, uint16_t frame_len) { 242225ffee39SMatthias Ringwald 242325ffee39SMatthias Ringwald if (bad_frame){ 242425ffee39SMatthias Ringwald // drop all data 242525ffee39SMatthias Ringwald hfp_h2_sync->dropped_bytes += hfp_h2_sync->frame_len; 242625ffee39SMatthias Ringwald hfp_h2_sync->frame_len = 0; 242725ffee39SMatthias Ringwald // all new data is bad, too 242825ffee39SMatthias Ringwald hfp_h2_sync->dropped_bytes += frame_len; 242925ffee39SMatthias Ringwald // report frames 243025ffee39SMatthias Ringwald hfp_h2_report_bad_frames(hfp_h2_sync); 243125ffee39SMatthias Ringwald return; 243225ffee39SMatthias Ringwald } 243325ffee39SMatthias Ringwald 243425ffee39SMatthias Ringwald while (frame_len > 0){ 243525ffee39SMatthias Ringwald // Fill hfp_h2_sync->frame_buffer 243625ffee39SMatthias Ringwald uint16_t bytes_free_in_frame_buffer = HFP_H2_SYNC_FRAME_SIZE - hfp_h2_sync->frame_len; 243725ffee39SMatthias Ringwald uint16_t bytes_to_append = btstack_min(frame_len, bytes_free_in_frame_buffer); 243825ffee39SMatthias Ringwald memcpy(&hfp_h2_sync->frame_data[hfp_h2_sync->frame_len], frame_data, bytes_to_append); 243925ffee39SMatthias Ringwald frame_data += bytes_to_append; 244025ffee39SMatthias Ringwald frame_len -= bytes_to_append; 244125ffee39SMatthias Ringwald hfp_h2_sync->frame_len += bytes_to_append; 244225ffee39SMatthias Ringwald // check complete frame for h2 sync 244325ffee39SMatthias Ringwald if (hfp_h2_sync->frame_len == HFP_H2_SYNC_FRAME_SIZE){ 244425ffee39SMatthias Ringwald bool valid_frame = true; 244525ffee39SMatthias Ringwald int16_t h2_pos = hfp_h2_sync_find(hfp_h2_sync->frame_data, hfp_h2_sync->frame_len); 244625ffee39SMatthias Ringwald if (h2_pos < 0){ 244725ffee39SMatthias Ringwald // no h2 sync, no valid frame, keep last byte if it is 0x01 244825ffee39SMatthias Ringwald if (hfp_h2_sync->frame_data[HFP_H2_SYNC_FRAME_SIZE-1] == 0x01){ 244925ffee39SMatthias Ringwald hfp_h2_sync_drop_bytes(hfp_h2_sync, HFP_H2_SYNC_FRAME_SIZE - 1); 245025ffee39SMatthias Ringwald } else { 245125ffee39SMatthias Ringwald hfp_h2_sync_drop_bytes(hfp_h2_sync, HFP_H2_SYNC_FRAME_SIZE); 245225ffee39SMatthias Ringwald } 245325ffee39SMatthias Ringwald valid_frame = false; 245425ffee39SMatthias Ringwald } 245525ffee39SMatthias Ringwald else if (h2_pos > 0){ 245625ffee39SMatthias Ringwald // drop data before h2 sync 245725ffee39SMatthias Ringwald hfp_h2_sync_drop_bytes(hfp_h2_sync, h2_pos); 245825ffee39SMatthias Ringwald valid_frame = false; 245925ffee39SMatthias Ringwald } 246025ffee39SMatthias Ringwald if (valid_frame) { 246125ffee39SMatthias Ringwald // h2 sync at pos 0 and complete frame 246225ffee39SMatthias Ringwald bool codec_ok = (*hfp_h2_sync->callback)(false, hfp_h2_sync->frame_data, hfp_h2_sync->frame_len); 246325ffee39SMatthias Ringwald if (codec_ok){ 246425ffee39SMatthias Ringwald hfp_h2_sync_reset(hfp_h2_sync); 246525ffee39SMatthias Ringwald } else { 246625ffee39SMatthias Ringwald // drop first two bytes 246725ffee39SMatthias Ringwald hfp_h2_sync_drop_bytes(hfp_h2_sync, 2); 246825ffee39SMatthias Ringwald } 246925ffee39SMatthias Ringwald } 247025ffee39SMatthias Ringwald } 247125ffee39SMatthias Ringwald } 247225ffee39SMatthias Ringwald } 2473