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 8720b2edb6SMatthias Ringwald static uint16_t hfp_allowed_sco_packet_types; 88aeb0f0feSMatthias Ringwald static hfp_connection_t * hfp_sco_establishment_active; 89aeb0f0feSMatthias Ringwald 90aeb0f0feSMatthias Ringwald static hfp_sdp_query_context_t hfp_sdp_query_context; 91aeb0f0feSMatthias Ringwald static btstack_context_callback_registration_t hfp_sdp_query_request; 92aeb0f0feSMatthias Ringwald 93aeb0f0feSMatthias Ringwald 94aeb0f0feSMatthias Ringwald 95aeb0f0feSMatthias Ringwald 96aeb0f0feSMatthias Ringwald 97471dea41SMatthias Ringwald // custom commands 98471dea41SMatthias Ringwald static btstack_linked_list_t hfp_custom_commands_ag; 9920b2edb6SMatthias Ringwald 10020b2edb6SMatthias Ringwald // prototypes 10120b2edb6SMatthias 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); 10220b2edb6SMatthias Ringwald static void parse_sequence(hfp_connection_t * context); 10320b2edb6SMatthias Ringwald 104*4e01e802SMatthias Ringwald 105*4e01e802SMatthias Ringwald static const struct link_settings { 106*4e01e802SMatthias Ringwald const uint16_t max_latency; 107*4e01e802SMatthias Ringwald const uint8_t retransmission_effort; 108*4e01e802SMatthias Ringwald const uint16_t packet_types; 109*4e01e802SMatthias Ringwald const bool eSCO; 110*4e01e802SMatthias Ringwald const uint8_t codec; 111*4e01e802SMatthias Ringwald } hfp_link_settings [] = { 112*4e01e802SMatthias Ringwald { 0xffff, 0xff, SCO_PACKET_TYPES_HV1, false, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_D0 113*4e01e802SMatthias Ringwald { 0xffff, 0xff, SCO_PACKET_TYPES_HV3, false, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_D1 114*4e01e802SMatthias Ringwald { 0x0007, 0x01, SCO_PACKET_TYPES_EV3, true, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S1 115*4e01e802SMatthias Ringwald { 0x0007, 0x01, SCO_PACKET_TYPES_2EV3, true, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S2 116*4e01e802SMatthias Ringwald { 0x000a, 0x01, SCO_PACKET_TYPES_2EV3, true, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S3 117*4e01e802SMatthias Ringwald { 0x000c, 0x02, SCO_PACKET_TYPES_2EV3, true, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S4 118*4e01e802SMatthias Ringwald { 0x0008, 0x02, SCO_PACKET_TYPES_EV3, true, HFP_CODEC_MSBC }, // HFP_LINK_SETTINGS_T1 119*4e01e802SMatthias Ringwald { 0x000d, 0x02, SCO_PACKET_TYPES_2EV3, true, HFP_CODEC_MSBC } // HFP_LINK_SETTINGS_T2 120*4e01e802SMatthias Ringwald }; 121*4e01e802SMatthias Ringwald 1223deb3ec6SMatthias Ringwald static const char * hfp_hf_features[] = { 1233deb3ec6SMatthias Ringwald "EC and/or NR function", 1243deb3ec6SMatthias Ringwald "Three-way calling", 1253deb3ec6SMatthias Ringwald "CLI presentation capability", 1263deb3ec6SMatthias Ringwald "Voice recognition activation", 1273deb3ec6SMatthias Ringwald "Remote volume control", 1283deb3ec6SMatthias Ringwald 1293deb3ec6SMatthias Ringwald "Enhanced call status", 1303deb3ec6SMatthias Ringwald "Enhanced call control", 1313deb3ec6SMatthias Ringwald 1323deb3ec6SMatthias Ringwald "Codec negotiation", 1333deb3ec6SMatthias Ringwald 1343deb3ec6SMatthias Ringwald "HF Indicators", 1353deb3ec6SMatthias Ringwald "eSCO S4 (and T2) Settings Supported", 1363deb3ec6SMatthias Ringwald "Reserved for future definition" 1373deb3ec6SMatthias Ringwald }; 1383deb3ec6SMatthias Ringwald 1393deb3ec6SMatthias Ringwald static const char * hfp_ag_features[] = { 1403deb3ec6SMatthias Ringwald "Three-way calling", 1413deb3ec6SMatthias Ringwald "EC and/or NR function", 1423deb3ec6SMatthias Ringwald "Voice recognition function", 1433deb3ec6SMatthias Ringwald "In-band ring tone capability", 1443deb3ec6SMatthias Ringwald "Attach a number to a voice tag", 1453deb3ec6SMatthias Ringwald "Ability to reject a call", 1463deb3ec6SMatthias Ringwald "Enhanced call status", 1473deb3ec6SMatthias Ringwald "Enhanced call control", 1483deb3ec6SMatthias Ringwald "Extended Error Result Codes", 1493deb3ec6SMatthias Ringwald "Codec negotiation", 1503deb3ec6SMatthias Ringwald "HF Indicators", 1513deb3ec6SMatthias Ringwald "eSCO S4 (and T2) Settings Supported", 1523deb3ec6SMatthias Ringwald "Reserved for future definition" 1533deb3ec6SMatthias Ringwald }; 1543deb3ec6SMatthias Ringwald 1550aee97efSMilanka Ringwald static const char * hfp_enhanced_call_dir[] = { 1560aee97efSMilanka Ringwald "outgoing", 1570aee97efSMilanka Ringwald "incoming" 1580aee97efSMilanka Ringwald }; 1590aee97efSMilanka Ringwald 1600aee97efSMilanka Ringwald static const char * hfp_enhanced_call_status[] = { 1610aee97efSMilanka Ringwald "active", 1620aee97efSMilanka Ringwald "held", 1630aee97efSMilanka Ringwald "outgoing dialing", 1640aee97efSMilanka Ringwald "outgoing alerting", 1650aee97efSMilanka Ringwald "incoming", 1660aee97efSMilanka Ringwald "incoming waiting", 1670aee97efSMilanka Ringwald "call held by response and hold" 1680aee97efSMilanka Ringwald }; 1690aee97efSMilanka Ringwald 1700aee97efSMilanka Ringwald static const char * hfp_enhanced_call_mode[] = { 1710aee97efSMilanka Ringwald "voice", 1720aee97efSMilanka Ringwald "data", 1730aee97efSMilanka Ringwald "fax" 1740aee97efSMilanka Ringwald }; 1750aee97efSMilanka Ringwald 1760aee97efSMilanka Ringwald static const char * hfp_enhanced_call_mpty[] = { 1770aee97efSMilanka Ringwald "not a conference call", 1780aee97efSMilanka Ringwald "conference call" 1790aee97efSMilanka Ringwald }; 1800aee97efSMilanka Ringwald 1810aee97efSMilanka Ringwald const char * hfp_enhanced_call_dir2str(uint16_t index){ 1820aee97efSMilanka Ringwald if (index <= HFP_ENHANCED_CALL_DIR_INCOMING) return hfp_enhanced_call_dir[index]; 1830aee97efSMilanka Ringwald return "not defined"; 1840aee97efSMilanka Ringwald } 1850aee97efSMilanka Ringwald 1860aee97efSMilanka Ringwald const char * hfp_enhanced_call_status2str(uint16_t index){ 1870aee97efSMilanka Ringwald if (index <= HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD) return hfp_enhanced_call_status[index]; 1880aee97efSMilanka Ringwald return "not defined"; 1890aee97efSMilanka Ringwald } 1900aee97efSMilanka Ringwald 1910aee97efSMilanka Ringwald const char * hfp_enhanced_call_mode2str(uint16_t index){ 1920aee97efSMilanka Ringwald if (index <= HFP_ENHANCED_CALL_MODE_FAX) return hfp_enhanced_call_mode[index]; 1930aee97efSMilanka Ringwald return "not defined"; 1940aee97efSMilanka Ringwald } 1950aee97efSMilanka Ringwald 1960aee97efSMilanka Ringwald const char * hfp_enhanced_call_mpty2str(uint16_t index){ 1970aee97efSMilanka Ringwald if (index <= HFP_ENHANCED_CALL_MPTY_CONFERENCE_CALL) return hfp_enhanced_call_mpty[index]; 1980aee97efSMilanka Ringwald return "not defined"; 1990aee97efSMilanka Ringwald } 2000aee97efSMilanka Ringwald 20125789943SMilanka Ringwald static uint16_t hfp_parse_indicator_index(hfp_connection_t * hfp_connection){ 20225789943SMilanka Ringwald uint16_t index = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 20325789943SMilanka Ringwald 20425789943SMilanka Ringwald if (index > HFP_MAX_NUM_INDICATORS){ 20525789943SMilanka Ringwald log_info("ignoring invalid indicator index bigger then HFP_MAX_NUM_INDICATORS"); 20625789943SMilanka Ringwald return HFP_MAX_NUM_INDICATORS - 1; 20725789943SMilanka Ringwald } 20825789943SMilanka Ringwald 20925789943SMilanka Ringwald // indicator index enumeration starts with 1, we substract 1 to store in array with starting index 0 21025789943SMilanka Ringwald if (index > 0){ 21125789943SMilanka Ringwald index -= 1; 21225789943SMilanka Ringwald } else { 21325789943SMilanka Ringwald log_info("ignoring invalid indicator index 0"); 21425789943SMilanka Ringwald return 0; 21525789943SMilanka Ringwald } 21625789943SMilanka Ringwald return index; 21725789943SMilanka Ringwald } 21825789943SMilanka Ringwald 21925789943SMilanka Ringwald static void hfp_next_indicators_index(hfp_connection_t * hfp_connection){ 22025789943SMilanka Ringwald if (hfp_connection->parser_item_index < HFP_MAX_NUM_INDICATORS - 1){ 22125789943SMilanka Ringwald hfp_connection->parser_item_index++; 22225789943SMilanka Ringwald } else { 22325789943SMilanka Ringwald log_info("Ignoring additional indicator"); 22425789943SMilanka Ringwald } 22525789943SMilanka Ringwald } 22625789943SMilanka Ringwald 22725789943SMilanka Ringwald static void hfp_next_codec_index(hfp_connection_t * hfp_connection){ 22825789943SMilanka Ringwald if (hfp_connection->parser_item_index < HFP_MAX_NUM_CODECS - 1){ 22925789943SMilanka Ringwald hfp_connection->parser_item_index++; 23025789943SMilanka Ringwald } else { 23125789943SMilanka Ringwald log_info("Ignoring additional codec index"); 23225789943SMilanka Ringwald } 23325789943SMilanka Ringwald } 23425789943SMilanka Ringwald 235eed67a37SMilanka Ringwald static void hfp_next_remote_call_services_index(hfp_connection_t * hfp_connection){ 236eed67a37SMilanka Ringwald if (hfp_connection->remote_call_services_index < HFP_MAX_NUM_CALL_SERVICES - 1){ 237eed67a37SMilanka Ringwald hfp_connection->remote_call_services_index++; 238eed67a37SMilanka Ringwald } else { 239eed67a37SMilanka Ringwald log_info("Ignoring additional remote_call_services"); 240eed67a37SMilanka Ringwald } 241eed67a37SMilanka Ringwald } 24225789943SMilanka Ringwald 2433deb3ec6SMatthias Ringwald const char * hfp_hf_feature(int index){ 2443deb3ec6SMatthias Ringwald if (index > HFP_HF_FEATURES_SIZE){ 2453deb3ec6SMatthias Ringwald return hfp_hf_features[HFP_HF_FEATURES_SIZE]; 2463deb3ec6SMatthias Ringwald } 2473deb3ec6SMatthias Ringwald return hfp_hf_features[index]; 2483deb3ec6SMatthias Ringwald } 2493deb3ec6SMatthias Ringwald 2503deb3ec6SMatthias Ringwald const char * hfp_ag_feature(int index){ 2513deb3ec6SMatthias Ringwald if (index > HFP_AG_FEATURES_SIZE){ 2523deb3ec6SMatthias Ringwald return hfp_ag_features[HFP_AG_FEATURES_SIZE]; 2533deb3ec6SMatthias Ringwald } 2543deb3ec6SMatthias Ringwald return hfp_ag_features[index]; 2553deb3ec6SMatthias Ringwald } 2563deb3ec6SMatthias Ringwald 2575ebff802SMatthias Ringwald int send_str_over_rfcomm(uint16_t cid, const char * command){ 2583deb3ec6SMatthias Ringwald if (!rfcomm_can_send_packet_now(cid)) return 1; 25974386ee0SMatthias Ringwald log_info("HFP_TX %s", command); 260ab2445a0SMatthias Ringwald int err = rfcomm_send(cid, (uint8_t*) command, (uint16_t) strlen(command)); 2613deb3ec6SMatthias Ringwald if (err){ 26228190c0bSMatthias Ringwald log_error("rfcomm_send -> error 0x%02x \n", err); 2633deb3ec6SMatthias Ringwald } 264e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES 265e43d1938SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(cid); 266e43d1938SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_SENT, command); 267e43d1938SMatthias Ringwald #endif 2683deb3ec6SMatthias Ringwald return 1; 2693deb3ec6SMatthias Ringwald } 2703deb3ec6SMatthias Ringwald 2716a7f44bdSMilanka Ringwald int hfp_supports_codec(uint8_t codec, int codecs_nr, uint8_t * codecs){ 272e8d1dc0dSMatthias Ringwald 273e8d1dc0dSMatthias Ringwald // mSBC requires support for eSCO connections 274c1ab6cc1SMatthias Ringwald if ((codec == HFP_CODEC_MSBC) && !hci_extended_sco_link_supported()) return 0; 275e8d1dc0dSMatthias Ringwald 276df327ff3SMilanka Ringwald int i; 277df327ff3SMilanka Ringwald for (i = 0; i < codecs_nr; i++){ 278e8d1dc0dSMatthias Ringwald if (codecs[i] != codec) continue; 279e8d1dc0dSMatthias Ringwald return 1; 280df327ff3SMilanka Ringwald } 281df327ff3SMilanka Ringwald return 0; 282df327ff3SMilanka Ringwald } 283df327ff3SMilanka Ringwald 284d715cf51SMatthias Ringwald void hfp_hf_drop_mSBC_if_eSCO_not_supported(uint8_t * codecs, uint8_t * codecs_nr){ 285d715cf51SMatthias Ringwald if (hci_extended_sco_link_supported()) return; 286d715cf51SMatthias Ringwald uint8_t tmp_codecs[HFP_MAX_NUM_CODECS]; 287d715cf51SMatthias Ringwald int i; 288d715cf51SMatthias Ringwald int tmp_codec_nr = 0; 289d715cf51SMatthias Ringwald for (i=0; i < *codecs_nr; i++){ 290d715cf51SMatthias Ringwald if (codecs[i] == HFP_CODEC_MSBC) continue; 291d715cf51SMatthias Ringwald tmp_codecs[tmp_codec_nr++] = codecs[i]; 292d715cf51SMatthias Ringwald } 293d715cf51SMatthias Ringwald *codecs_nr = tmp_codec_nr; 2946535961aSMatthias Ringwald (void)memcpy(codecs, tmp_codecs, tmp_codec_nr); 295d715cf51SMatthias Ringwald } 296d715cf51SMatthias Ringwald 2973deb3ec6SMatthias Ringwald // UTILS 2983deb3ec6SMatthias Ringwald int get_bit(uint16_t bitmap, int position){ 2993deb3ec6SMatthias Ringwald return (bitmap >> position) & 1; 3003deb3ec6SMatthias Ringwald } 3013deb3ec6SMatthias Ringwald 3023deb3ec6SMatthias Ringwald int store_bit(uint32_t bitmap, int position, uint8_t value){ 3033deb3ec6SMatthias Ringwald if (value){ 3043deb3ec6SMatthias Ringwald bitmap |= 1 << position; 3053deb3ec6SMatthias Ringwald } else { 3063deb3ec6SMatthias Ringwald bitmap &= ~ (1 << position); 3073deb3ec6SMatthias Ringwald } 3083deb3ec6SMatthias Ringwald return bitmap; 3093deb3ec6SMatthias Ringwald } 3103deb3ec6SMatthias Ringwald 3113deb3ec6SMatthias Ringwald int join(char * buffer, int buffer_size, uint8_t * values, int values_nr){ 312c1ab6cc1SMatthias Ringwald if (buffer_size < (values_nr * 3)) return 0; 3133deb3ec6SMatthias Ringwald int i; 3143deb3ec6SMatthias Ringwald int offset = 0; 315c1ab6cc1SMatthias Ringwald for (i = 0; i < (values_nr-1); i++) { 3163deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", values[i]); // puts string into buffer 3173deb3ec6SMatthias Ringwald } 3183deb3ec6SMatthias Ringwald if (i<values_nr){ 3193deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d", values[i]); 3203deb3ec6SMatthias Ringwald } 3213deb3ec6SMatthias Ringwald return offset; 3223deb3ec6SMatthias Ringwald } 3233deb3ec6SMatthias Ringwald 3243deb3ec6SMatthias Ringwald int join_bitmap(char * buffer, int buffer_size, uint32_t values, int values_nr){ 325c1ab6cc1SMatthias Ringwald if (buffer_size < (values_nr * 3)) return 0; 3263deb3ec6SMatthias Ringwald 3273deb3ec6SMatthias Ringwald int i; 3283deb3ec6SMatthias Ringwald int offset = 0; 329c1ab6cc1SMatthias Ringwald for (i = 0; i < (values_nr-1); i++) { 3303deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", get_bit(values,i)); // puts string into buffer 3313deb3ec6SMatthias Ringwald } 3323deb3ec6SMatthias Ringwald 3333deb3ec6SMatthias Ringwald if (i<values_nr){ 3343deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d", get_bit(values,i)); 3353deb3ec6SMatthias Ringwald } 3363deb3ec6SMatthias Ringwald return offset; 3373deb3ec6SMatthias Ringwald } 338293fae36SMatthias Ringwald static void hfp_emit_event_for_role(hfp_role_t local_role, uint8_t * packet, uint16_t size){ 339293fae36SMatthias Ringwald switch (local_role){ 340293fae36SMatthias Ringwald case HFP_ROLE_HF: 341293fae36SMatthias Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, packet, size); 342293fae36SMatthias Ringwald break; 343293fae36SMatthias Ringwald case HFP_ROLE_AG: 344293fae36SMatthias Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, packet, size); 345293fae36SMatthias Ringwald break; 346293fae36SMatthias Ringwald default: 347293fae36SMatthias Ringwald btstack_unreachable(); 348293fae36SMatthias Ringwald break; 349293fae36SMatthias Ringwald } 350293fae36SMatthias Ringwald } 3513deb3ec6SMatthias Ringwald 352ca59be51SMatthias Ringwald static void hfp_emit_event_for_context(hfp_connection_t * hfp_connection, uint8_t * packet, uint16_t size){ 353ca59be51SMatthias Ringwald if (!hfp_connection) return; 354293fae36SMatthias Ringwald hfp_emit_event_for_role(hfp_connection->local_role, packet, size); 355ca59be51SMatthias Ringwald } 356ca59be51SMatthias Ringwald 357ca59be51SMatthias Ringwald void hfp_emit_simple_event(hfp_connection_t * hfp_connection, uint8_t event_subtype){ 3587d81706fSMatthias Ringwald hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 359d703d377SMatthias Ringwald uint8_t event[5]; 360a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 361a0ffb263SMatthias Ringwald event[1] = sizeof(event) - 2; 362a0ffb263SMatthias Ringwald event[2] = event_subtype; 3637d81706fSMatthias Ringwald little_endian_store_16(event, 3, acl_handle); 364ca59be51SMatthias Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 365a0ffb263SMatthias Ringwald } 366a0ffb263SMatthias Ringwald 367ca59be51SMatthias Ringwald void hfp_emit_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, uint8_t value){ 3687d81706fSMatthias Ringwald hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 369d703d377SMatthias Ringwald uint8_t event[6]; 3703deb3ec6SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 3713deb3ec6SMatthias Ringwald event[1] = sizeof(event) - 2; 3723deb3ec6SMatthias Ringwald event[2] = event_subtype; 3737d81706fSMatthias Ringwald little_endian_store_16(event, 3, acl_handle); 374d703d377SMatthias Ringwald event[5] = value; // status 0 == OK 375ca59be51SMatthias Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 3763deb3ec6SMatthias Ringwald } 3773deb3ec6SMatthias Ringwald 378553a4a56SMilanka Ringwald void hfp_emit_voice_recognition_enabled(hfp_connection_t * hfp_connection, uint8_t status){ 379553a4a56SMilanka Ringwald btstack_assert(hfp_connection != NULL); 380553a4a56SMilanka Ringwald 381a95ec82fSMilanka Ringwald uint8_t event[7]; 382a95ec82fSMilanka Ringwald event[0] = HCI_EVENT_HFP_META; 383a95ec82fSMilanka Ringwald event[1] = sizeof(event) - 2; 384a9e632e9SMilanka Ringwald event[2] = HFP_SUBEVENT_VOICE_RECOGNITION_ACTIVATED; 385de9e0ea7SMilanka Ringwald 386553a4a56SMilanka Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 387a95ec82fSMilanka Ringwald event[5] = status; // 0:success 388553a4a56SMilanka Ringwald event[6] = hfp_connection->enhanced_voice_recognition_enabled ? 1 : 0; 389553a4a56SMilanka Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 3901a26de69SMilanka Ringwald } 391553a4a56SMilanka Ringwald 392553a4a56SMilanka Ringwald void hfp_emit_voice_recognition_disabled(hfp_connection_t * hfp_connection, uint8_t status){ 393553a4a56SMilanka Ringwald btstack_assert(hfp_connection != NULL); 394553a4a56SMilanka Ringwald 395553a4a56SMilanka Ringwald uint8_t event[6]; 396553a4a56SMilanka Ringwald event[0] = HCI_EVENT_HFP_META; 397553a4a56SMilanka Ringwald event[1] = sizeof(event) - 2; 398a9e632e9SMilanka Ringwald event[2] = HFP_SUBEVENT_VOICE_RECOGNITION_DEACTIVATED; 399553a4a56SMilanka Ringwald 400553a4a56SMilanka Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 401553a4a56SMilanka Ringwald event[5] = status; // 0:success 402013cc750SMilanka Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 403013cc750SMilanka Ringwald } 404013cc750SMilanka Ringwald 405de9e0ea7SMilanka Ringwald void hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection_t * hfp_connection, uint8_t status){ 406de9e0ea7SMilanka Ringwald hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 407de9e0ea7SMilanka Ringwald 408de9e0ea7SMilanka Ringwald uint8_t event[6]; 409de9e0ea7SMilanka Ringwald event[0] = HCI_EVENT_HFP_META; 410de9e0ea7SMilanka Ringwald event[1] = sizeof(event) - 2; 411de9e0ea7SMilanka Ringwald event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_HF_READY_FOR_AUDIO; 412de9e0ea7SMilanka Ringwald little_endian_store_16(event, 3, acl_handle); 413de9e0ea7SMilanka Ringwald event[5] = status; 414de9e0ea7SMilanka Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 415de9e0ea7SMilanka Ringwald } 416de9e0ea7SMilanka Ringwald 417cf75be85SMilanka Ringwald void hfp_emit_enhanced_voice_recognition_state_event(hfp_connection_t * hfp_connection, uint8_t status){ 418cf75be85SMilanka Ringwald hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 419cf75be85SMilanka Ringwald 420cf75be85SMilanka Ringwald uint8_t event[6]; 421cf75be85SMilanka Ringwald event[0] = HCI_EVENT_HFP_META; 422cf75be85SMilanka Ringwald event[1] = sizeof(event) - 2; 423cf75be85SMilanka Ringwald switch (hfp_connection->ag_vra_state){ 424cf75be85SMilanka Ringwald case HFP_VOICE_RECOGNITION_STATE_AG_READY_TO_ACCEPT_AUDIO_INPUT: 425cf75be85SMilanka Ringwald event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_READY_TO_ACCEPT_AUDIO_INPUT; 426cf75be85SMilanka Ringwald break; 427cf75be85SMilanka Ringwald case HFP_VOICE_RECOGNITION_STATE_AG_IS_STARTING_SOUND: 428cf75be85SMilanka Ringwald event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_STARTING_SOUND; 429cf75be85SMilanka Ringwald break; 430cf75be85SMilanka Ringwald case HFP_VOICE_RECOGNITION_STATE_AG_IS_PROCESSING_AUDIO_INPUT: 431cf75be85SMilanka Ringwald event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_PROCESSING_AUDIO_INPUT; 432cf75be85SMilanka Ringwald break; 433cf75be85SMilanka Ringwald default: 434cf75be85SMilanka Ringwald btstack_unreachable(); 435cf75be85SMilanka Ringwald break; 436cf75be85SMilanka Ringwald } 437cf75be85SMilanka Ringwald 438cf75be85SMilanka Ringwald little_endian_store_16(event, 3, acl_handle); 439cf75be85SMilanka Ringwald event[5] = status; 440cf75be85SMilanka Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 441cf75be85SMilanka Ringwald } 442cf75be85SMilanka Ringwald 4437095467fSMatthias 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){ 4448901a7c4SMatthias Ringwald uint8_t event[12]; 4456a7f44bdSMilanka Ringwald int pos = 0; 4466a7f44bdSMilanka Ringwald event[pos++] = HCI_EVENT_HFP_META; 4476a7f44bdSMilanka Ringwald event[pos++] = sizeof(event) - 2; 448d0c4aea6SMilanka Ringwald event[pos++] = HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 4496a7f44bdSMilanka Ringwald little_endian_store_16(event, pos, con_handle); 4506a7f44bdSMilanka Ringwald pos += 2; 45103870092SMilanka Ringwald event[pos++] = status; // status 0 == OK 4526a7f44bdSMilanka Ringwald reverse_bd_addr(addr,&event[pos]); 4536a7f44bdSMilanka Ringwald pos += 6; 4547095467fSMatthias Ringwald hfp_emit_event_for_role(local_role, event, sizeof(event)); 455a0653c3bSMilanka Ringwald } 456a0653c3bSMilanka Ringwald 457d703d377SMatthias Ringwald static void hfp_emit_audio_connection_released(hfp_connection_t * hfp_connection, hci_con_handle_t sco_handle){ 4587d81706fSMatthias Ringwald btstack_assert(hfp_connection != NULL); 459d703d377SMatthias Ringwald uint8_t event[7]; 4605441d52fSMatthias Ringwald int pos = 0; 4615441d52fSMatthias Ringwald event[pos++] = HCI_EVENT_HFP_META; 4625441d52fSMatthias Ringwald event[pos++] = sizeof(event) - 2; 4635441d52fSMatthias Ringwald event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED; 464d703d377SMatthias Ringwald little_endian_store_16(event, pos, hfp_connection->acl_handle); 465d703d377SMatthias Ringwald pos += 2; 466d703d377SMatthias Ringwald little_endian_store_16(event, pos, sco_handle); 467d703d377SMatthias Ringwald pos += 2; 4685441d52fSMatthias Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 4695441d52fSMatthias Ringwald } 4705441d52fSMatthias Ringwald 471d703d377SMatthias Ringwald void hfp_emit_sco_event(hfp_connection_t * hfp_connection, uint8_t status, hci_con_handle_t sco_handle, bd_addr_t addr, uint8_t negotiated_codec){ 4727d81706fSMatthias Ringwald btstack_assert(hfp_connection != NULL); 473d703d377SMatthias Ringwald uint8_t event[15]; 474d0c4aea6SMilanka Ringwald int pos = 0; 475d0c4aea6SMilanka Ringwald event[pos++] = HCI_EVENT_HFP_META; 476d0c4aea6SMilanka Ringwald event[pos++] = sizeof(event) - 2; 477d0c4aea6SMilanka Ringwald event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED; 478d703d377SMatthias Ringwald little_endian_store_16(event, pos, hfp_connection->acl_handle); 479d703d377SMatthias Ringwald pos += 2; 480d0c4aea6SMilanka Ringwald event[pos++] = status; // status 0 == OK 481d703d377SMatthias Ringwald little_endian_store_16(event, pos, sco_handle); 482d0c4aea6SMilanka Ringwald pos += 2; 483d0c4aea6SMilanka Ringwald reverse_bd_addr(addr,&event[pos]); 484d0c4aea6SMilanka Ringwald pos += 6; 485d0c4aea6SMilanka Ringwald event[pos++] = negotiated_codec; 486ca59be51SMatthias Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 487d0c4aea6SMilanka Ringwald } 488d0c4aea6SMilanka Ringwald 489ca59be51SMatthias Ringwald void hfp_emit_string_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, const char * value){ 4907d81706fSMatthias Ringwald btstack_assert(hfp_connection != NULL); 4912c50df93SBjoern Hartmann #ifdef ENABLE_HFP_AT_MESSAGES 4922c50df93SBjoern Hartmann uint8_t event[256]; 4932c50df93SBjoern Hartmann #else 494c1797c7dSMatthias Ringwald uint8_t event[40]; 4952c50df93SBjoern Hartmann #endif 496ab2445a0SMatthias Ringwald uint16_t string_len = btstack_min((uint16_t) strlen(value), sizeof(event) - 6); 497aa4dd815SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 498c10fde09SMatthias Ringwald event[1] = 4 + string_len; 499aa4dd815SMatthias Ringwald event[2] = event_subtype; 500d703d377SMatthias Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 501c10fde09SMatthias Ringwald memcpy((char*)&event[5], value, string_len); 502c10fde09SMatthias Ringwald event[5 + string_len] = 0; 503c10fde09SMatthias Ringwald hfp_emit_event_for_context(hfp_connection, event, 6 + string_len); 504aa4dd815SMatthias Ringwald } 505aa4dd815SMatthias Ringwald 5060cb5b971SMatthias Ringwald btstack_linked_list_t * hfp_get_connections(void){ 5078f2a52f4SMatthias Ringwald return (btstack_linked_list_t *) &hfp_connections; 5083deb3ec6SMatthias Ringwald } 5093deb3ec6SMatthias Ringwald 5103deb3ec6SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid){ 511665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 512665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 513665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 514a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 515a0ffb263SMatthias Ringwald if (hfp_connection->rfcomm_cid == cid){ 516a0ffb263SMatthias Ringwald return hfp_connection; 5173deb3ec6SMatthias Ringwald } 5183deb3ec6SMatthias Ringwald } 5193deb3ec6SMatthias Ringwald return NULL; 5203deb3ec6SMatthias Ringwald } 5213deb3ec6SMatthias Ringwald 522405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr, hfp_role_t hfp_role){ 523665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 524665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 525665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 526a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 5275df9dc78SMatthias Ringwald if ((memcmp(hfp_connection->remote_addr, bd_addr, 6) == 0) && (hfp_connection->local_role == hfp_role)) { 528a0ffb263SMatthias Ringwald return hfp_connection; 5293deb3ec6SMatthias Ringwald } 5303deb3ec6SMatthias Ringwald } 5313deb3ec6SMatthias Ringwald return NULL; 5323deb3ec6SMatthias Ringwald } 5333deb3ec6SMatthias Ringwald 534405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_sco_handle(uint16_t handle, hfp_role_t hfp_role){ 535665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 536665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 537665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 538a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 539c1ab6cc1SMatthias Ringwald if ((hfp_connection->sco_handle == handle) && (hfp_connection->local_role == hfp_role)){ 540a0ffb263SMatthias Ringwald return hfp_connection; 5413deb3ec6SMatthias Ringwald } 5423deb3ec6SMatthias Ringwald } 5433deb3ec6SMatthias Ringwald return NULL; 5443deb3ec6SMatthias Ringwald } 5453deb3ec6SMatthias Ringwald 546405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_acl_handle(uint16_t handle, hfp_role_t hfp_role){ 547d97d752dSMilanka Ringwald btstack_linked_list_iterator_t it; 548d97d752dSMilanka Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 549d97d752dSMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 550d97d752dSMilanka Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 551c1ab6cc1SMatthias Ringwald if ((hfp_connection->acl_handle == handle) && (hfp_connection->local_role == hfp_role)){ 552d97d752dSMilanka Ringwald return hfp_connection; 553d97d752dSMilanka Ringwald } 554d97d752dSMilanka Ringwald } 555d97d752dSMilanka Ringwald return NULL; 556d97d752dSMilanka Ringwald } 557d97d752dSMilanka Ringwald 558c95b5b3cSMilanka Ringwald 559c95b5b3cSMilanka Ringwald static void hfp_reset_voice_recognition(hfp_connection_t * hfp_connection){ 560c95b5b3cSMilanka Ringwald btstack_assert(hfp_connection != NULL); 561c95b5b3cSMilanka Ringwald hfp_voice_recognition_activation_status_t current_vra_state = hfp_connection->vra_state; 562c95b5b3cSMilanka Ringwald hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF; 563c95b5b3cSMilanka Ringwald 564c95b5b3cSMilanka Ringwald if (current_vra_state != HFP_VRA_VOICE_RECOGNITION_OFF){ 565553a4a56SMilanka Ringwald hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS); 566c95b5b3cSMilanka Ringwald } else if (hfp_connection->vra_state_requested != HFP_VRA_VOICE_RECOGNITION_OFF){ 567553a4a56SMilanka Ringwald hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS); 568c95b5b3cSMilanka Ringwald } 569c95b5b3cSMilanka Ringwald 570c95b5b3cSMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_VOICE_RECOGNITION_OFF; 571c95b5b3cSMilanka Ringwald hfp_connection->activate_voice_recognition = false; 572c95b5b3cSMilanka Ringwald hfp_connection->deactivate_voice_recognition = false; 573c95b5b3cSMilanka Ringwald hfp_connection->enhanced_voice_recognition_enabled = false; 574c95b5b3cSMilanka Ringwald hfp_connection->ag_vra_status = 0; 575c95b5b3cSMilanka Ringwald hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY; 576c95b5b3cSMilanka Ringwald } 577c95b5b3cSMilanka Ringwald 578a0ffb263SMatthias Ringwald void hfp_reset_context_flags(hfp_connection_t * hfp_connection){ 579a0ffb263SMatthias Ringwald if (!hfp_connection) return; 580a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 581a0ffb263SMatthias Ringwald hfp_connection->send_error = 0; 5823deb3ec6SMatthias Ringwald 583dd533208SMatthias Ringwald hfp_connection->found_equal_sign = false; 5843deb3ec6SMatthias Ringwald 585a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 0; 586a0ffb263SMatthias Ringwald hfp_connection->operator_name_changed = 0; 5873deb3ec6SMatthias Ringwald 588a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = 0; 589a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 5903deb3ec6SMatthias Ringwald 591a0ffb263SMatthias Ringwald // establish codecs hfp_connection 592a0ffb263SMatthias Ringwald hfp_connection->suggested_codec = 0; 5937522e673SMatthias Ringwald hfp_connection->negotiated_codec = 0; 594a0ffb263SMatthias Ringwald hfp_connection->codec_confirmed = 0; 5953deb3ec6SMatthias Ringwald 596a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 597a0ffb263SMatthias Ringwald hfp_connection->call_waiting_notification_enabled = 0; 598a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 599a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 600125560b8SMatthias Ringwald hfp_connection->clip_have_alpha = false; 601c95b5b3cSMilanka Ringwald hfp_reset_voice_recognition(hfp_connection); 6023deb3ec6SMatthias Ringwald } 6033deb3ec6SMatthias Ringwald 604fffdd288SMatthias Ringwald static hfp_connection_t * create_hfp_connection_context(void){ 605a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = btstack_memory_hfp_connection_get(); 606a0ffb263SMatthias Ringwald if (!hfp_connection) return NULL; 6073deb3ec6SMatthias Ringwald 608a0ffb263SMatthias Ringwald hfp_connection->state = HFP_IDLE; 609a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 610a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_IDLE; 611aa4dd815SMatthias Ringwald 612a0ffb263SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 6133deb3ec6SMatthias Ringwald 614d751f069SMatthias Ringwald hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID; 615d751f069SMatthias Ringwald hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID; 616d751f069SMatthias Ringwald 617a0ffb263SMatthias Ringwald hfp_reset_context_flags(hfp_connection); 6183deb3ec6SMatthias Ringwald 619d63c37a1SMatthias Ringwald btstack_linked_list_add(&hfp_connections, (btstack_linked_item_t*)hfp_connection); 620a0ffb263SMatthias Ringwald return hfp_connection; 6213deb3ec6SMatthias Ringwald } 6223deb3ec6SMatthias Ringwald 62348e6eeeeSMatthias Ringwald void hfp_finalize_connection_context(hfp_connection_t * hfp_connection){ 624a0ffb263SMatthias Ringwald btstack_linked_list_remove(&hfp_connections, (btstack_linked_item_t*) hfp_connection); 62509a233a1SMatthias Ringwald btstack_memory_hfp_connection_free(hfp_connection); 6263deb3ec6SMatthias Ringwald } 6273deb3ec6SMatthias Ringwald 6284eb3f1d8SMilanka Ringwald static hfp_connection_t * hfp_create_connection(bd_addr_t bd_addr, hfp_role_t local_role){ 629405014fbSMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr, local_role); 630a0ffb263SMatthias Ringwald if (hfp_connection) return hfp_connection; 631a0ffb263SMatthias Ringwald hfp_connection = create_hfp_connection_context(); 6326535961aSMatthias Ringwald (void)memcpy(hfp_connection->remote_addr, bd_addr, 6); 633323d3000SMatthias Ringwald hfp_connection->local_role = local_role; 634bdf572f4SMatthias Ringwald log_info("Create HFP context %p: role %u, addr %s", hfp_connection, local_role, bd_addr_to_str(bd_addr)); 635bdf572f4SMatthias Ringwald 636a0ffb263SMatthias Ringwald return hfp_connection; 6373deb3ec6SMatthias Ringwald } 6383deb3ec6SMatthias Ringwald 639aa4dd815SMatthias Ringwald /* @param network. 640aa4dd815SMatthias Ringwald * 0 == no ability to reject a call. 641aa4dd815SMatthias Ringwald * 1 == ability to reject a call. 642aa4dd815SMatthias Ringwald */ 6433deb3ec6SMatthias Ringwald 6443deb3ec6SMatthias Ringwald /* @param suported_features 6453deb3ec6SMatthias Ringwald * HF bit 0: EC and/or NR function (yes/no, 1 = yes, 0 = no) 6463deb3ec6SMatthias Ringwald * HF bit 1: Call waiting or three-way calling(yes/no, 1 = yes, 0 = no) 6473deb3ec6SMatthias Ringwald * HF bit 2: CLI presentation capability (yes/no, 1 = yes, 0 = no) 6483deb3ec6SMatthias Ringwald * HF bit 3: Voice recognition activation (yes/no, 1= yes, 0 = no) 6493deb3ec6SMatthias Ringwald * HF bit 4: Remote volume control (yes/no, 1 = yes, 0 = no) 6503deb3ec6SMatthias Ringwald * HF bit 5: Wide band speech (yes/no, 1 = yes, 0 = no) 6513deb3ec6SMatthias Ringwald */ 6523deb3ec6SMatthias Ringwald /* Bit position: 6533deb3ec6SMatthias Ringwald * AG bit 0: Three-way calling (yes/no, 1 = yes, 0 = no) 6543deb3ec6SMatthias Ringwald * AG bit 1: EC and/or NR function (yes/no, 1 = yes, 0 = no) 6553deb3ec6SMatthias Ringwald * AG bit 2: Voice recognition function (yes/no, 1 = yes, 0 = no) 6563deb3ec6SMatthias Ringwald * AG bit 3: In-band ring tone capability (yes/no, 1 = yes, 0 = no) 6573deb3ec6SMatthias Ringwald * AG bit 4: Attach a phone number to a voice tag (yes/no, 1 = yes, 0 = no) 6583deb3ec6SMatthias Ringwald * AG bit 5: Wide band speech (yes/no, 1 = yes, 0 = no) 6593deb3ec6SMatthias Ringwald */ 6603deb3ec6SMatthias Ringwald 6619b1c3b4dSMatthias 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){ 6623deb3ec6SMatthias Ringwald uint8_t* attribute; 6633deb3ec6SMatthias Ringwald de_create_sequence(service); 6643deb3ec6SMatthias Ringwald 6653deb3ec6SMatthias Ringwald // 0x0000 "Service Record Handle" 666235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE); 6679b1c3b4dSMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle); 6683deb3ec6SMatthias Ringwald 6693deb3ec6SMatthias Ringwald // 0x0001 "Service Class ID List" 670235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST); 6713deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 6723deb3ec6SMatthias Ringwald { 6733deb3ec6SMatthias Ringwald // "UUID for Service" 6743deb3ec6SMatthias Ringwald de_add_number(attribute, DE_UUID, DE_SIZE_16, service_uuid); 675235946f1SMatthias Ringwald de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_GENERIC_AUDIO); 6763deb3ec6SMatthias Ringwald } 6773deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 6783deb3ec6SMatthias Ringwald 6793deb3ec6SMatthias Ringwald // 0x0004 "Protocol Descriptor List" 680235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST); 6813deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 6823deb3ec6SMatthias Ringwald { 6833deb3ec6SMatthias Ringwald uint8_t* l2cpProtocol = de_push_sequence(attribute); 6843deb3ec6SMatthias Ringwald { 685235946f1SMatthias Ringwald de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP); 6863deb3ec6SMatthias Ringwald } 6873deb3ec6SMatthias Ringwald de_pop_sequence(attribute, l2cpProtocol); 6883deb3ec6SMatthias Ringwald 6893deb3ec6SMatthias Ringwald uint8_t* rfcomm = de_push_sequence(attribute); 6903deb3ec6SMatthias Ringwald { 691235946f1SMatthias Ringwald de_add_number(rfcomm, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_RFCOMM); // rfcomm_service 6923deb3ec6SMatthias Ringwald de_add_number(rfcomm, DE_UINT, DE_SIZE_8, rfcomm_channel_nr); // rfcomm channel 6933deb3ec6SMatthias Ringwald } 6943deb3ec6SMatthias Ringwald de_pop_sequence(attribute, rfcomm); 6953deb3ec6SMatthias Ringwald } 6963deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 6973deb3ec6SMatthias Ringwald 6983deb3ec6SMatthias Ringwald 6993deb3ec6SMatthias Ringwald // 0x0005 "Public Browse Group" 700235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group 7013deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 7023deb3ec6SMatthias Ringwald { 703235946f1SMatthias Ringwald de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT); 7043deb3ec6SMatthias Ringwald } 7053deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 7063deb3ec6SMatthias Ringwald 7073deb3ec6SMatthias Ringwald // 0x0009 "Bluetooth Profile Descriptor List" 708235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST); 7093deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 7103deb3ec6SMatthias Ringwald { 7113deb3ec6SMatthias Ringwald uint8_t *sppProfile = de_push_sequence(attribute); 7123deb3ec6SMatthias Ringwald { 713235946f1SMatthias Ringwald de_add_number(sppProfile, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_HANDSFREE); 7143cb2bba2SMilanka Ringwald de_add_number(sppProfile, DE_UINT, DE_SIZE_16, 0x0108); // Verision 1.8 7153deb3ec6SMatthias Ringwald } 7163deb3ec6SMatthias Ringwald de_pop_sequence(attribute, sppProfile); 7173deb3ec6SMatthias Ringwald } 7183deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 7193deb3ec6SMatthias Ringwald 7203deb3ec6SMatthias Ringwald // 0x0100 "Service Name" 7213deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); 722ab2445a0SMatthias Ringwald de_add_data(service, DE_STRING, (uint16_t) strlen(name), (uint8_t *) name); 7233deb3ec6SMatthias Ringwald } 7243deb3ec6SMatthias Ringwald 7257dceaecaSMatthias Ringwald static void hfp_handle_slc_setup_error(hfp_connection_t * hfp_connection, uint8_t status){ 7267dceaecaSMatthias Ringwald // cache fields for event 7277dceaecaSMatthias Ringwald hfp_role_t local_role = hfp_connection->local_role; 7287dceaecaSMatthias Ringwald bd_addr_t remote_addr; 729481c0cbcSMatthias Ringwald (void)memcpy(remote_addr, hfp_connection->remote_addr, 6); 7307dceaecaSMatthias Ringwald // finalize connection struct 7317dceaecaSMatthias Ringwald hfp_finalize_connection_context(hfp_connection); 7327dceaecaSMatthias Ringwald // emit event 7337dceaecaSMatthias Ringwald hfp_emit_slc_connection_event(local_role, status, HCI_CON_HANDLE_INVALID, remote_addr); 7347dceaecaSMatthias Ringwald } 7357dceaecaSMatthias Ringwald 7366c927b22SMatthias Ringwald static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 7378a46ec40SMatthias Ringwald UNUSED(packet_type); // ok: handling own sdp events 7388a46ec40SMatthias Ringwald UNUSED(channel); // ok: no channel 7398a46ec40SMatthias Ringwald UNUSED(size); // ok: handling own sdp events 7401d9c9c90SMilanka Ringwald 741aeb0f0feSMatthias 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); 7421d9c9c90SMilanka Ringwald if (hfp_connection == NULL) { 743aeb0f0feSMatthias Ringwald log_info("connection with %s and local role %d not found", hfp_sdp_query_context.remote_address, hfp_sdp_query_context.local_role); 74484904e95SMilanka Ringwald return; 74584904e95SMilanka Ringwald } 7463deb3ec6SMatthias Ringwald 7470e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 7485611a760SMatthias Ringwald case SDP_EVENT_QUERY_RFCOMM_SERVICE: 749a0ffb263SMatthias Ringwald hfp_connection->rfcomm_channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet); 7503deb3ec6SMatthias Ringwald break; 7515611a760SMatthias Ringwald case SDP_EVENT_QUERY_COMPLETE: 752a0ffb263SMatthias Ringwald if (hfp_connection->rfcomm_channel_nr > 0){ 753a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_CONNECTED; 754520c92d5SMatthias Ringwald btstack_packet_handler_t packet_handler; 755520c92d5SMatthias Ringwald switch (hfp_connection->local_role){ 756520c92d5SMatthias Ringwald case HFP_ROLE_AG: 757520c92d5SMatthias Ringwald packet_handler = hfp_ag_rfcomm_packet_handler; 758520c92d5SMatthias Ringwald break; 759520c92d5SMatthias Ringwald case HFP_ROLE_HF: 760520c92d5SMatthias Ringwald packet_handler = hfp_hf_rfcomm_packet_handler; 761520c92d5SMatthias Ringwald break; 762520c92d5SMatthias Ringwald default: 7631d9c9c90SMilanka Ringwald btstack_assert(false); 764520c92d5SMatthias Ringwald return; 765520c92d5SMatthias Ringwald } 7661d9c9c90SMilanka Ringwald 767ca59be51SMatthias Ringwald rfcomm_create_channel(packet_handler, hfp_connection->remote_addr, hfp_connection->rfcomm_channel_nr, NULL); 7681d9c9c90SMilanka Ringwald 7691d9c9c90SMilanka Ringwald } else { 770cc92f22bSMatthias Ringwald uint8_t status = sdp_event_query_complete_get_status(packet); 771cc92f22bSMatthias Ringwald if (status == ERROR_CODE_SUCCESS){ 772cc92f22bSMatthias Ringwald // report service not found 773cc92f22bSMatthias Ringwald status = SDP_SERVICE_NOT_FOUND; 774cc92f22bSMatthias Ringwald } 7757dceaecaSMatthias Ringwald hfp_handle_slc_setup_error(hfp_connection, status); 776cc92f22bSMatthias Ringwald log_info("rfcomm service not found, status 0x%02x", status); 7771d9c9c90SMilanka Ringwald } 7781d9c9c90SMilanka Ringwald 7791d9c9c90SMilanka Ringwald // register the SDP Query request to check if there is another connection waiting for the query 7801d9c9c90SMilanka Ringwald // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback 781aeb0f0feSMatthias Ringwald (void) sdp_client_register_query_callback(&hfp_sdp_query_request); 7823deb3ec6SMatthias Ringwald break; 7833deb3ec6SMatthias Ringwald default: 7843deb3ec6SMatthias Ringwald break; 7853deb3ec6SMatthias Ringwald } 7863deb3ec6SMatthias Ringwald } 7873deb3ec6SMatthias Ringwald 78838200c1dSMilanka Ringwald // returns 0 if unexpected error or no other link options remained, otherwise 1 78938200c1dSMilanka Ringwald static int hfp_handle_failed_sco_connection(uint8_t status){ 790eddcd308SMatthias Ringwald 791aeb0f0feSMatthias Ringwald if (!hfp_sco_establishment_active){ 7923427dd75SMatthias Ringwald log_info("(e)SCO Connection failed but not started by us"); 79338200c1dSMilanka Ringwald return 0; 794eddcd308SMatthias Ringwald } 795eddcd308SMatthias Ringwald 7963427dd75SMatthias Ringwald log_info("(e)SCO Connection failed 0x%02x", status); 7973427dd75SMatthias Ringwald switch (status){ 7981579b82fSMatthias Ringwald case ERROR_CODE_SCO_AIR_MODE_REJECTED: 7991579b82fSMatthias Ringwald case ERROR_CODE_SCO_INTERVAL_REJECTED: 8001579b82fSMatthias Ringwald case ERROR_CODE_SCO_OFFSET_REJECTED: 8013427dd75SMatthias Ringwald case ERROR_CODE_UNSPECIFIED_ERROR: 8021579b82fSMatthias Ringwald case ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE: 80369a0db57SMatthias Ringwald case ERROR_CODE_UNSUPPORTED_REMOTE_FEATURE_UNSUPPORTED_LMP_FEATURE: 804eddcd308SMatthias Ringwald break; 8053427dd75SMatthias Ringwald default: 8063427dd75SMatthias Ringwald return 0; 8073427dd75SMatthias Ringwald } 8083427dd75SMatthias Ringwald 8093427dd75SMatthias Ringwald // note: eSCO_S4 supported flag not available, but it's only relevant for highest CVSD link setting (and the current failed) 810aeb0f0feSMatthias Ringwald hfp_link_settings_t next_setting = hfp_next_link_setting_for_connection(hfp_sco_establishment_active->link_setting, hfp_sco_establishment_active, false); 8113427dd75SMatthias Ringwald 8123427dd75SMatthias Ringwald // handle no valid setting found 8133427dd75SMatthias Ringwald if (next_setting == HFP_LINK_SETTINGS_NONE) { 814aeb0f0feSMatthias Ringwald if (hfp_sco_establishment_active->negotiated_codec == HFP_CODEC_MSBC){ 8153427dd75SMatthias Ringwald log_info("T2/T1 failed, fallback to CVSD - D1"); 816aeb0f0feSMatthias Ringwald hfp_sco_establishment_active->negotiated_codec = HFP_CODEC_CVSD; 817aeb0f0feSMatthias Ringwald hfp_sco_establishment_active->sco_for_msbc_failed = 1; 818a814da6aSMatthias Ringwald hfp_sco_establishment_active->ag_send_common_codec = true; 819aeb0f0feSMatthias Ringwald hfp_sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D1; 8203427dd75SMatthias Ringwald } else { 8213427dd75SMatthias Ringwald // no other options 8223427dd75SMatthias Ringwald return 0; 823eddcd308SMatthias Ringwald } 8243427dd75SMatthias Ringwald } 8253427dd75SMatthias Ringwald 8263427dd75SMatthias Ringwald log_info("e)SCO Connection: try new link_setting %d", next_setting); 827aeb0f0feSMatthias Ringwald hfp_sco_establishment_active->establish_audio_connection = 1; 828aeb0f0feSMatthias Ringwald hfp_sco_establishment_active->link_setting = next_setting; 829aeb0f0feSMatthias Ringwald hfp_sco_establishment_active = NULL; 83038200c1dSMilanka Ringwald return 1; 831eddcd308SMatthias Ringwald } 832eddcd308SMatthias Ringwald 833405014fbSMatthias Ringwald void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){ 8345575558eSMilanka Ringwald UNUSED(packet_type); 8358a46ec40SMatthias Ringwald UNUSED(channel); // ok: no channel 8365575558eSMilanka Ringwald UNUSED(size); 8379ec2630cSMatthias Ringwald 8383deb3ec6SMatthias Ringwald bd_addr_t event_addr; 83927950165SMatthias Ringwald hci_con_handle_t handle; 840a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = NULL; 841674515e8SMatthias Ringwald uint8_t status; 8423deb3ec6SMatthias Ringwald 84327950165SMatthias Ringwald log_debug("HFP HCI event handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size); 844aa4dd815SMatthias Ringwald 8450e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 8461b005648SMatthias Ringwald 847011577abSMilanka Ringwald case HCI_EVENT_CONNECTION_REQUEST: 8487522e673SMatthias Ringwald switch(hci_event_connection_request_get_link_type(packet)){ 8497522e673SMatthias Ringwald case 0: // SCO 8507522e673SMatthias Ringwald case 2: // eSCO 851011577abSMilanka Ringwald hci_event_connection_request_get_bd_addr(packet, event_addr); 852405014fbSMatthias Ringwald hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role); 853011577abSMilanka Ringwald if (!hfp_connection) break; 854c169df2fSMatthias Ringwald if (hci_event_connection_request_get_link_type(packet) == 2){ 855cb81d35dSMatthias Ringwald hfp_connection->accept_sco = 2; 856c169df2fSMatthias Ringwald } else { 857cb81d35dSMatthias Ringwald hfp_connection->accept_sco = 1; 858c169df2fSMatthias Ringwald } 8593721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP 8603721a235SMatthias Ringwald hfp_cc256x_prepare_for_sco(hfp_connection); 8613721a235SMatthias Ringwald #endif 862689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 863689d4323SMatthias Ringwald hfp_bcm_prepare_for_sco(hfp_connection); 864689d4323SMatthias Ringwald #endif 8652b5f92fdSMatthias Ringwald #ifdef ENABLE_RTK_PCM_WBS 8662b5f92fdSMatthias Ringwald hfp_connection->rtk_send_sco_config = true; 8672b5f92fdSMatthias Ringwald #endif 868cb81d35dSMatthias Ringwald log_info("accept sco %u\n", hfp_connection->accept_sco); 869aeb0f0feSMatthias Ringwald hfp_sco_establishment_active = hfp_connection; 870202c8a4cSMatthias Ringwald break; 8717522e673SMatthias Ringwald default: 8727522e673SMatthias Ringwald break; 8737522e673SMatthias Ringwald } 874011577abSMilanka Ringwald break; 8753deb3ec6SMatthias Ringwald 876eddcd308SMatthias Ringwald case HCI_EVENT_COMMAND_STATUS: 877eddcd308SMatthias Ringwald if (hci_event_command_status_get_command_opcode(packet) == hci_setup_synchronous_connection.opcode) { 878aeb0f0feSMatthias Ringwald if (hfp_sco_establishment_active == NULL) break; 879be2a1a78SMatthias Ringwald status = hci_event_command_status_get_status(packet); 88038200c1dSMilanka Ringwald if (status == ERROR_CODE_SUCCESS) break; 88138200c1dSMilanka Ringwald 882aeb0f0feSMatthias Ringwald hfp_connection = hfp_sco_establishment_active; 88338200c1dSMilanka Ringwald if (hfp_handle_failed_sco_connection(status)) break; 88438200c1dSMilanka Ringwald hfp_connection->establish_audio_connection = 0; 88538200c1dSMilanka Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 88638200c1dSMilanka Ringwald hfp_emit_sco_event(hfp_connection, status, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec); 8876c5b2002SMatthias Ringwald } 888eddcd308SMatthias Ringwald break; 889eddcd308SMatthias Ringwald 8903deb3ec6SMatthias Ringwald case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{ 891aeb0f0feSMatthias Ringwald if (hfp_sco_establishment_active == NULL) break; 892011577abSMilanka Ringwald hci_event_synchronous_connection_complete_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) { 895011577abSMilanka Ringwald log_error("HFP: connection does not exist for remote with addr %s.", bd_addr_to_str(event_addr)); 896011577abSMilanka Ringwald return; 897011577abSMilanka Ringwald } 898ce263fc8SMatthias Ringwald 899011577abSMilanka Ringwald status = hci_event_synchronous_connection_complete_get_status(packet); 90038200c1dSMilanka Ringwald if (status != ERROR_CODE_SUCCESS){ 901cb81d35dSMatthias Ringwald hfp_connection->accept_sco = 0; 90238200c1dSMilanka Ringwald if (hfp_handle_failed_sco_connection(status)) break; 90338200c1dSMilanka Ringwald 90438200c1dSMilanka Ringwald hfp_connection->establish_audio_connection = 0; 90538200c1dSMilanka Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 90638200c1dSMilanka Ringwald hfp_emit_sco_event(hfp_connection, status, 0, event_addr, hfp_connection->negotiated_codec); 907aa4dd815SMatthias Ringwald break; 908aa4dd815SMatthias Ringwald } 909aa4dd815SMatthias Ringwald 910011577abSMilanka Ringwald uint16_t sco_handle = hci_event_synchronous_connection_complete_get_handle(packet); 911011577abSMilanka Ringwald uint8_t link_type = hci_event_synchronous_connection_complete_get_link_type(packet); 912011577abSMilanka Ringwald uint8_t transmission_interval = hci_event_synchronous_connection_complete_get_transmission_interval(packet); // measured in slots 913011577abSMilanka Ringwald uint8_t retransmission_interval = hci_event_synchronous_connection_complete_get_retransmission_interval(packet);// measured in slots 914011577abSMilanka Ringwald uint16_t rx_packet_length = hci_event_synchronous_connection_complete_get_rx_packet_length(packet); // measured in bytes 915011577abSMilanka Ringwald uint16_t tx_packet_length = hci_event_synchronous_connection_complete_get_tx_packet_length(packet); // measured in bytes 9163deb3ec6SMatthias Ringwald 9173deb3ec6SMatthias Ringwald switch (link_type){ 9183deb3ec6SMatthias Ringwald case 0x00: 919aa4dd815SMatthias Ringwald log_info("SCO Connection established."); 9203deb3ec6SMatthias Ringwald if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval); 9213deb3ec6SMatthias Ringwald if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval); 9223deb3ec6SMatthias Ringwald if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length); 9233deb3ec6SMatthias Ringwald if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length); 9243deb3ec6SMatthias Ringwald break; 9253deb3ec6SMatthias Ringwald case 0x02: 926aa4dd815SMatthias Ringwald log_info("eSCO Connection established. \n"); 9273deb3ec6SMatthias Ringwald break; 9283deb3ec6SMatthias Ringwald default: 9293deb3ec6SMatthias Ringwald log_error("(e)SCO reserved link_type 0x%2x", link_type); 9303deb3ec6SMatthias Ringwald break; 9313deb3ec6SMatthias Ringwald } 932e0d13a19SMilanka Ringwald 9333deb3ec6SMatthias Ringwald log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, " 934aa4dd815SMatthias Ringwald " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)\n", sco_handle, 935e0d13a19SMilanka Ringwald bd_addr_to_str(event_addr), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length, 936e0d13a19SMilanka Ringwald hci_event_synchronous_connection_complete_get_air_mode(packet)); 9373deb3ec6SMatthias Ringwald 938d63c37a1SMatthias Ringwald if (hfp_connection->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){ 939aa4dd815SMatthias Ringwald log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN"); 940d63c37a1SMatthias Ringwald hfp_connection->state = HFP_W2_DISCONNECT_SCO; 941aa4dd815SMatthias Ringwald break; 942aa4dd815SMatthias Ringwald } 943d63c37a1SMatthias Ringwald hfp_connection->sco_handle = sco_handle; 944d63c37a1SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 945c95b5b3cSMilanka Ringwald 946d63c37a1SMatthias Ringwald hfp_connection->state = HFP_AUDIO_CONNECTION_ESTABLISHED; 9471a26de69SMilanka Ringwald 9480b4debbfSMilanka Ringwald switch (hfp_connection->vra_state){ 9490b4debbfSMilanka Ringwald case HFP_VRA_VOICE_RECOGNITION_ACTIVATED: 9501a26de69SMilanka Ringwald hfp_connection->ag_audio_connection_opened_before_vra = false; 9510b4debbfSMilanka Ringwald break; 9520b4debbfSMilanka Ringwald default: 9531a26de69SMilanka Ringwald hfp_connection->ag_audio_connection_opened_before_vra = true; 9540b4debbfSMilanka Ringwald break; 9550b4debbfSMilanka Ringwald } 95638200c1dSMilanka Ringwald hfp_emit_sco_event(hfp_connection, status, sco_handle, event_addr, hfp_connection->negotiated_codec); 9573deb3ec6SMatthias Ringwald break; 9583deb3ec6SMatthias Ringwald } 9593deb3ec6SMatthias Ringwald 9603deb3ec6SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 961f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet,3); 962405014fbSMatthias Ringwald hfp_connection = get_hfp_connection_context_for_sco_handle(handle, local_role); 963aa4dd815SMatthias Ringwald 964d63c37a1SMatthias Ringwald if (!hfp_connection) break; 965aa4dd815SMatthias Ringwald 9663721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP 9673721a235SMatthias Ringwald hfp_connection->cc256x_send_wbs_disassociate = true; 9683721a235SMatthias Ringwald #endif 969689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 970689d4323SMatthias Ringwald hfp_connection->bcm_send_disable_wbs = true; 971689d4323SMatthias Ringwald #endif 97252cbdd6dSMilanka Ringwald if (hfp_connection->sco_handle == handle){ 973d751f069SMatthias Ringwald hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID; 974d63c37a1SMatthias Ringwald hfp_connection->release_audio_connection = 0; 97552cbdd6dSMilanka Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 97652cbdd6dSMilanka Ringwald hfp_emit_audio_connection_released(hfp_connection, handle); 97752cbdd6dSMilanka Ringwald 9781a26de69SMilanka Ringwald hfp_connection->ag_audio_connection_opened_before_vra = false; 97952cbdd6dSMilanka Ringwald 98052cbdd6dSMilanka Ringwald if (hfp_connection->acl_handle == HCI_CON_HANDLE_INVALID){ 98111b091bfSMilanka Ringwald hfp_reset_voice_recognition(hfp_connection); 98252cbdd6dSMilanka Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0); 98352cbdd6dSMilanka Ringwald hfp_finalize_connection_context(hfp_connection); 98419976d6bSMilanka Ringwald break; 98552cbdd6dSMilanka Ringwald } else if (hfp_connection->release_slc_connection == 1){ 98652cbdd6dSMilanka Ringwald hfp_connection->release_slc_connection = 0; 98752cbdd6dSMilanka Ringwald hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM; 98852cbdd6dSMilanka Ringwald rfcomm_disconnect(hfp_connection->acl_handle); 98952cbdd6dSMilanka Ringwald } 99052cbdd6dSMilanka Ringwald } 99148e6eeeeSMatthias Ringwald 99248e6eeeeSMatthias Ringwald if (hfp_connection->state == HFP_W4_SCO_DISCONNECTED_TO_SHUTDOWN){ 99348e6eeeeSMatthias Ringwald // RFCOMM already closed -> remote power off 99448e6eeeeSMatthias Ringwald #if defined(ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS) 99548e6eeeeSMatthias Ringwald hfp_connection->state = HFP_W4_WBS_SHUTDOWN; 99648e6eeeeSMatthias Ringwald #else 99748e6eeeeSMatthias Ringwald hfp_finalize_connection_context(hfp_connection); 99848e6eeeeSMatthias Ringwald #endif 99948e6eeeeSMatthias Ringwald break; 100048e6eeeeSMatthias Ringwald } 10013deb3ec6SMatthias Ringwald break; 10023deb3ec6SMatthias Ringwald 1003fc64f94aSMatthias Ringwald default: 10043deb3ec6SMatthias Ringwald break; 10053deb3ec6SMatthias Ringwald } 10063deb3ec6SMatthias Ringwald } 10073deb3ec6SMatthias Ringwald 10083721a235SMatthias Ringwald 100927950165SMatthias Ringwald void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){ 10105575558eSMilanka Ringwald UNUSED(packet_type); 101127950165SMatthias Ringwald UNUSED(channel); // ok: no channel 10125575558eSMilanka Ringwald UNUSED(size); 101327950165SMatthias Ringwald 101427950165SMatthias Ringwald bd_addr_t event_addr; 101527950165SMatthias Ringwald uint16_t rfcomm_cid; 101627950165SMatthias Ringwald hfp_connection_t * hfp_connection = NULL; 101727950165SMatthias Ringwald uint8_t status; 101827950165SMatthias Ringwald 101927950165SMatthias Ringwald log_debug("HFP packet_handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size); 102027950165SMatthias Ringwald 102127950165SMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 102227950165SMatthias Ringwald 102327950165SMatthias Ringwald case RFCOMM_EVENT_INCOMING_CONNECTION: 102427950165SMatthias Ringwald // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 102527950165SMatthias Ringwald rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr); 10264eb3f1d8SMilanka Ringwald hfp_connection = hfp_create_connection(event_addr, local_role); 102727950165SMatthias Ringwald if (!hfp_connection){ 102827950165SMatthias Ringwald log_info("hfp: no memory to accept incoming connection - decline"); 102927950165SMatthias Ringwald rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet)); 103027950165SMatthias Ringwald return; 103127950165SMatthias Ringwald } 103227950165SMatthias Ringwald if (hfp_connection->state != HFP_IDLE) { 10334960f9e7SMatthias Ringwald log_error("hfp: incoming connection but not idle, reject"); 10344960f9e7SMatthias Ringwald rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet)); 103527950165SMatthias Ringwald return; 103627950165SMatthias Ringwald } 103727950165SMatthias Ringwald 103827950165SMatthias Ringwald hfp_connection->rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet); 103927950165SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_CONNECTED; 104027950165SMatthias Ringwald rfcomm_accept_connection(hfp_connection->rfcomm_cid); 104127950165SMatthias Ringwald break; 104227950165SMatthias Ringwald 104327950165SMatthias Ringwald case RFCOMM_EVENT_CHANNEL_OPENED: 104427950165SMatthias Ringwald rfcomm_event_channel_opened_get_bd_addr(packet, event_addr); 104527950165SMatthias Ringwald 1046405014fbSMatthias Ringwald hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role); 10476504a03aSMilanka Ringwald btstack_assert(hfp_connection != NULL); 104827950165SMatthias Ringwald 10496504a03aSMilanka Ringwald if (hfp_connection->state != HFP_W4_RFCOMM_CONNECTED){ 10506504a03aSMilanka Ringwald break; 10516504a03aSMilanka Ringwald } 10526504a03aSMilanka Ringwald 10536504a03aSMilanka Ringwald status = rfcomm_event_channel_opened_get_status(packet); 10546504a03aSMilanka Ringwald if (status != ERROR_CODE_SUCCESS) { 10557dceaecaSMatthias Ringwald hfp_handle_slc_setup_error(hfp_connection, status); 10566504a03aSMilanka Ringwald break; 10576504a03aSMilanka Ringwald } 10586504a03aSMilanka Ringwald 105927950165SMatthias Ringwald hfp_connection->acl_handle = rfcomm_event_channel_opened_get_con_handle(packet); 106027950165SMatthias Ringwald hfp_connection->rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet); 10616504a03aSMilanka Ringwald hfp_connection->rfcomm_mtu = rfcomm_event_channel_opened_get_max_frame_size(packet); 106227950165SMatthias Ringwald bd_addr_copy(hfp_connection->remote_addr, event_addr); 106327950165SMatthias Ringwald hfp_connection->state = HFP_EXCHANGE_SUPPORTED_FEATURES; 10646504a03aSMilanka Ringwald 106527950165SMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 106627950165SMatthias Ringwald break; 106727950165SMatthias Ringwald 106827950165SMatthias Ringwald case RFCOMM_EVENT_CHANNEL_CLOSED: 106927950165SMatthias Ringwald rfcomm_cid = little_endian_read_16(packet,2); 107027950165SMatthias Ringwald hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid); 107127950165SMatthias Ringwald if (!hfp_connection) break; 107252cbdd6dSMilanka Ringwald switch (hfp_connection->state){ 107352cbdd6dSMilanka Ringwald case HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART: 107452cbdd6dSMilanka Ringwald hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID; 107527950165SMatthias Ringwald hfp_connection->state = HFP_IDLE; 107627950165SMatthias Ringwald hfp_establish_service_level_connection(hfp_connection->remote_addr, hfp_connection->service_uuid, local_role); 107727950165SMatthias Ringwald break; 107852cbdd6dSMilanka Ringwald 107952cbdd6dSMilanka Ringwald case HFP_AUDIO_CONNECTION_ESTABLISHED: 108048e6eeeeSMatthias Ringwald // service connection was released, this implicitly releases audio connection as well 108148e6eeeeSMatthias Ringwald hfp_connection->release_audio_connection = 0; 108252cbdd6dSMilanka Ringwald hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID; 108348e6eeeeSMatthias Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED_TO_SHUTDOWN; 108452cbdd6dSMilanka Ringwald gap_disconnect(hfp_connection->sco_handle); 108552cbdd6dSMilanka Ringwald break; 108652cbdd6dSMilanka Ringwald 108752cbdd6dSMilanka Ringwald default: 108848e6eeeeSMatthias Ringwald // regular case 108911b091bfSMilanka Ringwald hfp_reset_voice_recognition(hfp_connection); 109048e6eeeeSMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0); 109148e6eeeeSMatthias Ringwald hfp_finalize_connection_context(hfp_connection); 109252cbdd6dSMilanka Ringwald break; 109348e6eeeeSMatthias Ringwald } 109427950165SMatthias Ringwald break; 109527950165SMatthias Ringwald 109627950165SMatthias Ringwald default: 109727950165SMatthias Ringwald break; 109827950165SMatthias Ringwald } 109927950165SMatthias Ringwald } 1100aa4dd815SMatthias Ringwald // translates command string into hfp_command_t CMD 110194a27792SMatthias Ringwald 110294a27792SMatthias Ringwald typedef struct { 110394a27792SMatthias Ringwald const char * command; 110494a27792SMatthias Ringwald hfp_command_t command_id; 110594a27792SMatthias Ringwald } hfp_command_entry_t; 110694a27792SMatthias Ringwald 110794a27792SMatthias Ringwald static hfp_command_entry_t hfp_ag_commmand_table[] = { 1108cb33905eSMatthias Ringwald { "AT+BAC=", HFP_CMD_AVAILABLE_CODECS }, 1109cb33905eSMatthias Ringwald { "AT+BCC", HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP }, 1110cb33905eSMatthias Ringwald { "AT+BCS=", HFP_CMD_HF_CONFIRMED_CODEC }, 1111cb33905eSMatthias Ringwald { "AT+BIA=", HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE, }, // +BIA:<enabled>,,<enabled>,,,<enabled> 1112cb33905eSMatthias Ringwald { "AT+BIEV=", HFP_CMD_HF_INDICATOR_STATUS }, 111394a27792SMatthias Ringwald { "AT+BIND=", HFP_CMD_LIST_GENERIC_STATUS_INDICATORS }, 111494a27792SMatthias Ringwald { "AT+BIND=?", HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS }, 111594a27792SMatthias Ringwald { "AT+BIND?", HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE }, 1116051f797dSMilanka Ringwald { "AT+BINP=", HFP_CMD_HF_REQUEST_PHONE_NUMBER }, 1117cb33905eSMatthias Ringwald { "AT+BLDN", HFP_CMD_REDIAL_LAST_NUMBER }, 1118cb33905eSMatthias Ringwald { "AT+BRSF=", HFP_CMD_SUPPORTED_FEATURES }, 111994a27792SMatthias Ringwald { "AT+BTRH=", HFP_CMD_RESPONSE_AND_HOLD_COMMAND }, 112094a27792SMatthias Ringwald { "AT+BTRH?", HFP_CMD_RESPONSE_AND_HOLD_QUERY }, 11219ed286f3SMilanka Ringwald { "AT+BVRA=", HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION }, 1122cb33905eSMatthias Ringwald { "AT+CCWA=", HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION}, 112394a27792SMatthias Ringwald { "AT+CHLD=", HFP_CMD_CALL_HOLD }, 112494a27792SMatthias Ringwald { "AT+CHLD=?", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES }, 1125cb33905eSMatthias Ringwald { "AT+CHUP", HFP_CMD_HANG_UP_CALL }, 112694a27792SMatthias Ringwald { "AT+CIND=?", HFP_CMD_RETRIEVE_AG_INDICATORS }, 112794a27792SMatthias Ringwald { "AT+CIND?", HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS }, 1128cb33905eSMatthias Ringwald { "AT+CLCC", HFP_CMD_LIST_CURRENT_CALLS }, 1129cb33905eSMatthias Ringwald { "AT+CLIP=", HFP_CMD_ENABLE_CLIP}, 1130cb33905eSMatthias Ringwald { "AT+CMEE=", HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR}, 1131cb33905eSMatthias Ringwald { "AT+CMER=", HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE }, 1132cb33905eSMatthias Ringwald { "AT+CNUM", HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION }, 113394a27792SMatthias Ringwald { "AT+COPS=", HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT }, 113494a27792SMatthias Ringwald { "AT+COPS?", HFP_CMD_QUERY_OPERATOR_SELECTION_NAME }, 1135cb33905eSMatthias Ringwald { "AT+NREC=", HFP_CMD_TURN_OFF_EC_AND_NR, }, 1136cb33905eSMatthias Ringwald { "AT+VGM=", HFP_CMD_SET_MICROPHONE_GAIN }, 1137cb33905eSMatthias Ringwald { "AT+VGS=", HFP_CMD_SET_SPEAKER_GAIN }, 1138d8656ee6SMilanka Ringwald { "AT+VTS=", HFP_CMD_TRANSMIT_DTMF_CODES }, 113994a27792SMatthias Ringwald { "ATA", HFP_CMD_CALL_ANSWERED }, 114094a27792SMatthias Ringwald }; 114194a27792SMatthias Ringwald 114294a27792SMatthias Ringwald static hfp_command_entry_t hfp_hf_commmand_table[] = { 1143cb33905eSMatthias Ringwald { "+BCS:", HFP_CMD_AG_SUGGESTED_CODEC }, 114494a27792SMatthias Ringwald { "+BIND:", HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS }, 1145b3615b9aSMilanka Ringwald { "+BINP:", HFP_CMD_AG_SENT_PHONE_NUMBER }, 1146cb33905eSMatthias Ringwald { "+BRSF:", HFP_CMD_SUPPORTED_FEATURES }, 1147cb33905eSMatthias Ringwald { "+BSIR:", HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING }, 114894a27792SMatthias Ringwald { "+BTRH:", HFP_CMD_RESPONSE_AND_HOLD_STATUS }, 1149cb33905eSMatthias Ringwald { "+BVRA:", HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION }, 1150cb33905eSMatthias Ringwald { "+CCWA:", HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE, }, 115194a27792SMatthias Ringwald { "+CHLD:", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES }, 1152cb33905eSMatthias Ringwald { "+CIEV:", HFP_CMD_TRANSFER_AG_INDICATOR_STATUS}, 115384a0c24eSMatthias Ringwald { "+CIND:", HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC }, 1154cb33905eSMatthias Ringwald { "+CLCC:", HFP_CMD_LIST_CURRENT_CALLS }, 1155cb33905eSMatthias Ringwald { "+CLIP:", HFP_CMD_AG_SENT_CLIP_INFORMATION }, 1156cb33905eSMatthias Ringwald { "+CME ERROR:", HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR }, 1157cb33905eSMatthias Ringwald { "+CNUM:", HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION}, 1158cb33905eSMatthias Ringwald { "+COPS:", HFP_CMD_QUERY_OPERATOR_SELECTION_NAME }, 1159cb33905eSMatthias Ringwald { "+VGM:", HFP_CMD_SET_MICROPHONE_GAIN }, 1160ce284ffbSMatthias Ringwald { "+VGM=", HFP_CMD_SET_MICROPHONE_GAIN }, 11614c30d2d1SMatthias Ringwald { "+VGS:", HFP_CMD_SET_SPEAKER_GAIN}, 1162ce284ffbSMatthias Ringwald { "+VGS=", HFP_CMD_SET_SPEAKER_GAIN}, 1163cb33905eSMatthias Ringwald { "ERROR", HFP_CMD_ERROR}, 1164cb33905eSMatthias Ringwald { "NOP", HFP_CMD_NONE}, // dummy command used by unit tests 1165cb33905eSMatthias Ringwald { "OK", HFP_CMD_OK }, 1166cb33905eSMatthias Ringwald { "RING", HFP_CMD_RING }, 116794a27792SMatthias Ringwald }; 116894a27792SMatthias Ringwald 1169471dea41SMatthias Ringwald static const hfp_custom_at_command_t * hfp_custom_command_lookup(const char * text){ 1170471dea41SMatthias Ringwald btstack_linked_list_iterator_t it; 1171471dea41SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hfp_custom_commands_ag); 1172471dea41SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)) { 1173471dea41SMatthias Ringwald hfp_custom_at_command_t *at_command = (hfp_custom_at_command_t *) btstack_linked_list_iterator_next(&it); 1174471dea41SMatthias Ringwald int match = strcmp(text, at_command->command); 1175471dea41SMatthias Ringwald if (match == 0) { 1176471dea41SMatthias Ringwald return at_command; 1177471dea41SMatthias Ringwald } 1178471dea41SMatthias Ringwald } 1179471dea41SMatthias Ringwald return NULL; 1180471dea41SMatthias Ringwald } 1181471dea41SMatthias Ringwald 1182aa4dd815SMatthias Ringwald static hfp_command_t parse_command(const char * line_buffer, int isHandsFree){ 11833deb3ec6SMatthias Ringwald 1184ff158b9bSMatthias Ringwald // check for custom commands, AG only 1185ff158b9bSMatthias Ringwald if (isHandsFree == 0) { 1186ff158b9bSMatthias Ringwald const hfp_custom_at_command_t * custom_at_command = hfp_custom_command_lookup(line_buffer); 1187ff158b9bSMatthias Ringwald if (custom_at_command != NULL){ 1188ff158b9bSMatthias Ringwald return HFP_CMD_CUSTOM_MESSAGE; 1189ff158b9bSMatthias Ringwald } 1190ff158b9bSMatthias Ringwald } 1191ff158b9bSMatthias Ringwald 1192cb33905eSMatthias Ringwald // table lookup based on role 119394a27792SMatthias Ringwald uint16_t num_entries; 119494a27792SMatthias Ringwald hfp_command_entry_t * table; 119594a27792SMatthias Ringwald if (isHandsFree == 0){ 119694a27792SMatthias Ringwald table = hfp_ag_commmand_table; 119794a27792SMatthias Ringwald num_entries = sizeof(hfp_ag_commmand_table) / sizeof(hfp_command_entry_t); 11983deb3ec6SMatthias Ringwald } else { 119994a27792SMatthias Ringwald table = hfp_hf_commmand_table; 120094a27792SMatthias Ringwald num_entries = sizeof(hfp_hf_commmand_table) / sizeof(hfp_command_entry_t); 120194a27792SMatthias Ringwald } 1202791f0d0aSMatthias Ringwald // binary search 1203791f0d0aSMatthias Ringwald uint16_t left = 0; 1204791f0d0aSMatthias Ringwald uint16_t right = num_entries - 1; 1205791f0d0aSMatthias Ringwald while (left <= right){ 1206791f0d0aSMatthias Ringwald uint16_t middle = left + (right - left) / 2; 1207791f0d0aSMatthias Ringwald hfp_command_entry_t *entry = &table[middle]; 120894a27792SMatthias Ringwald int match = strcmp(line_buffer, entry->command); 1209791f0d0aSMatthias Ringwald if (match < 0){ 1210791f0d0aSMatthias Ringwald // search term is lower than middle element 121147005182SMilanka Ringwald if (right == 0) break; 1212791f0d0aSMatthias Ringwald right = middle - 1; 1213791f0d0aSMatthias Ringwald } else if (match == 0){ 121494a27792SMatthias Ringwald return entry->command_id; 1215791f0d0aSMatthias Ringwald } else { 1216791f0d0aSMatthias Ringwald // search term is higher than middle element 1217791f0d0aSMatthias Ringwald left = middle + 1; 121894a27792SMatthias Ringwald } 121994a27792SMatthias Ringwald } 122094a27792SMatthias Ringwald 1221cb33905eSMatthias Ringwald // note: if parser in CMD_HEADER state would treats digits and maybe '+' as separator, match on "ATD" would work. 12220222a807SMatthias Ringwald // note: phone number is currently expected in line_buffer[3..] 1223cb33905eSMatthias Ringwald // prefix match on 'ATD', AG only 1224cb33905eSMatthias Ringwald if ((isHandsFree == 0) && (strncmp(line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0)){ 1225cb33905eSMatthias Ringwald return HFP_CMD_CALL_PHONE_NUMBER; 12263deb3ec6SMatthias Ringwald } 12273deb3ec6SMatthias Ringwald 1228cb33905eSMatthias Ringwald // Valid looking, but unknown commands/responses 1229cb33905eSMatthias Ringwald if ((isHandsFree == 0) && (strncmp(line_buffer, "AT+", 3) == 0)){ 1230aa4dd815SMatthias Ringwald return HFP_CMD_UNKNOWN; 12313deb3ec6SMatthias Ringwald } 12323deb3ec6SMatthias Ringwald 1233cb33905eSMatthias Ringwald if ((isHandsFree != 0) && (strncmp(line_buffer, "+", 1) == 0)){ 1234aa4dd815SMatthias Ringwald return HFP_CMD_UNKNOWN; 1235aa4dd815SMatthias Ringwald } 12363deb3ec6SMatthias Ringwald 1237aa4dd815SMatthias Ringwald return HFP_CMD_NONE; 12383deb3ec6SMatthias Ringwald } 12393deb3ec6SMatthias Ringwald 1240a0ffb263SMatthias Ringwald static void hfp_parser_store_byte(hfp_connection_t * hfp_connection, uint8_t byte){ 124151aa5d5aSMilanka Ringwald if ((hfp_connection->line_size + 1) >= HFP_MAX_VR_TEXT_SIZE) return; 1242a0ffb263SMatthias Ringwald hfp_connection->line_buffer[hfp_connection->line_size++] = byte; 1243a0ffb263SMatthias Ringwald hfp_connection->line_buffer[hfp_connection->line_size] = 0; 12443deb3ec6SMatthias Ringwald } 1245a0ffb263SMatthias Ringwald static int hfp_parser_is_buffer_empty(hfp_connection_t * hfp_connection){ 1246a0ffb263SMatthias Ringwald return hfp_connection->line_size == 0; 12473deb3ec6SMatthias Ringwald } 12483deb3ec6SMatthias Ringwald 12493deb3ec6SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){ 1250c1ab6cc1SMatthias Ringwald return (byte == '\n') || (byte == '\r'); 12513deb3ec6SMatthias Ringwald } 12523deb3ec6SMatthias Ringwald 1253471dea41SMatthias Ringwald void hfp_parser_reset_line_buffer(hfp_connection_t *hfp_connection) { 1254a0ffb263SMatthias Ringwald hfp_connection->line_size = 0; 1255125560b8SMatthias Ringwald // we don't set the first byte to '\0' to allow access to last argument e.g. in hfp_hf_handle_rfcommand 12563deb3ec6SMatthias Ringwald } 12571d81c7feSMatthias Ringwald 12581d81c7feSMatthias Ringwald static void hfp_parser_store_if_token(hfp_connection_t * hfp_connection, uint8_t byte){ 12591d81c7feSMatthias Ringwald switch (byte){ 12601d81c7feSMatthias Ringwald case ',': 1261f8301d46SMatthias Ringwald case '-': 12621d81c7feSMatthias Ringwald case ';': 12631d81c7feSMatthias Ringwald case '(': 12641d81c7feSMatthias Ringwald case ')': 12651d81c7feSMatthias Ringwald case '\n': 12661d81c7feSMatthias Ringwald case '\r': 12673deb3ec6SMatthias Ringwald break; 12683deb3ec6SMatthias Ringwald default: 12691d81c7feSMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 12703deb3ec6SMatthias Ringwald break; 12713deb3ec6SMatthias Ringwald } 12723deb3ec6SMatthias Ringwald } 12731d81c7feSMatthias Ringwald 12741d81c7feSMatthias Ringwald static bool hfp_parser_is_separator( uint8_t byte){ 12751d81c7feSMatthias Ringwald switch (byte){ 12761d81c7feSMatthias Ringwald case ',': 1277f8301d46SMatthias Ringwald case '-': 12781d81c7feSMatthias Ringwald case ';': 12791d81c7feSMatthias Ringwald case '\n': 12801d81c7feSMatthias Ringwald case '\r': 12811d81c7feSMatthias Ringwald return true; 1282dd533208SMatthias Ringwald default: 12831d81c7feSMatthias Ringwald return false; 12843deb3ec6SMatthias Ringwald } 12853deb3ec6SMatthias Ringwald } 12863deb3ec6SMatthias Ringwald 1287d6b237acSMatthias Ringwald static bool hfp_parse_byte(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){ 1288aa4dd815SMatthias Ringwald 12891dddc4f4SMatthias Ringwald // handle doubles quotes 12901dddc4f4SMatthias Ringwald if (byte == '"'){ 12911dddc4f4SMatthias Ringwald hfp_connection->parser_quoted = !hfp_connection->parser_quoted; 1292d6b237acSMatthias Ringwald return true; 12931dddc4f4SMatthias Ringwald } 12941dddc4f4SMatthias Ringwald if (hfp_connection->parser_quoted) { 12951dddc4f4SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 1296d6b237acSMatthias Ringwald return true; 12971dddc4f4SMatthias Ringwald } 12983deb3ec6SMatthias Ringwald 12991dddc4f4SMatthias Ringwald // ignore spaces outside command or double quotes (required e.g. for '+CME ERROR:..") command 1300d6b237acSMatthias Ringwald if ((byte == ' ') && (hfp_connection->parser_state != HFP_PARSER_CMD_HEADER)) return true; 13011dddc4f4SMatthias Ringwald 1302bfeaf344SMatthias Ringwald bool processed = true; 1303bfeaf344SMatthias Ringwald 1304a0ffb263SMatthias Ringwald switch (hfp_connection->parser_state) { 1305dd533208SMatthias Ringwald case HFP_PARSER_CMD_HEADER: 1306d28b2d5dSMilanka Ringwald switch (byte) { 1307dd533208SMatthias Ringwald case '\n': 1308dd533208SMatthias Ringwald case '\r': 1309dd533208SMatthias Ringwald case ';': 1310bfeaf344SMatthias Ringwald // ignore separator 1311bfeaf344SMatthias Ringwald break; 1312bfeaf344SMatthias Ringwald case ':': 1313bfeaf344SMatthias Ringwald case '?': 1314bfeaf344SMatthias Ringwald // store separator 1315bfeaf344SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 1316dd533208SMatthias Ringwald break; 1317d28b2d5dSMilanka Ringwald case '=': 1318bfeaf344SMatthias Ringwald // equal sign: remember and wait for next char to decided between '=?' and '=\?' 1319dd533208SMatthias Ringwald hfp_connection->found_equal_sign = true; 1320a0ffb263SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 1321d6b237acSMatthias Ringwald return true; 1322d28b2d5dSMilanka Ringwald default: 1323bfeaf344SMatthias Ringwald // store if not lookahead 1324d7f16ff1SMatthias Ringwald if (!hfp_connection->found_equal_sign) { 1325dd533208SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 1326d6b237acSMatthias Ringwald return true; 1327dd533208SMatthias Ringwald } 1328bfeaf344SMatthias Ringwald // mark as lookahead 1329bfeaf344SMatthias Ringwald processed = false; 1330d7f16ff1SMatthias Ringwald break; 1331d7f16ff1SMatthias Ringwald } 1332ce263fc8SMatthias Ringwald 13331d81c7feSMatthias Ringwald // ignore empty tokens 1334d6b237acSMatthias Ringwald if (hfp_parser_is_buffer_empty(hfp_connection)) return true; 1335dd533208SMatthias Ringwald 1336bfeaf344SMatthias Ringwald // parse 1337dd533208SMatthias Ringwald hfp_connection->command = parse_command((char *)hfp_connection->line_buffer, isHandsFree); 1338aa4dd815SMatthias Ringwald 133984a0c24eSMatthias Ringwald // pick +CIND version based on connection state: descriptions during SLC vs. states later 134084a0c24eSMatthias Ringwald if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC){ 1341a0ffb263SMatthias Ringwald switch(hfp_connection->state){ 1342aa4dd815SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS_STATUS: 1343a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS; 1344aa4dd815SMatthias Ringwald break; 1345aa4dd815SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS: 1346a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS; 1347aa4dd815SMatthias Ringwald break; 1348aa4dd815SMatthias Ringwald default: 134984a0c24eSMatthias Ringwald hfp_connection->command = HFP_CMD_UNKNOWN; 1350aa4dd815SMatthias Ringwald break; 1351aa4dd815SMatthias Ringwald } 1352aa4dd815SMatthias Ringwald } 13533deb3ec6SMatthias Ringwald 1354c0c0437aSMatthias Ringwald log_info("command string '%s', handsfree %u -> cmd id %u", (char *)hfp_connection->line_buffer, isHandsFree, hfp_connection->command); 1355c0c0437aSMatthias Ringwald 1356471dea41SMatthias Ringwald // store command id for custom command and just store rest of line 1357471dea41SMatthias Ringwald if (hfp_connection->command == HFP_CMD_CUSTOM_MESSAGE){ 1358471dea41SMatthias Ringwald const hfp_custom_at_command_t * at_command = hfp_custom_command_lookup((const char *)hfp_connection->line_buffer); 1359471dea41SMatthias Ringwald hfp_connection->ag_custom_at_command_id = at_command->command_id; 1360471dea41SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CUSTOM_COMMAND; 1361471dea41SMatthias Ringwald return true; 1362471dea41SMatthias Ringwald } 1363471dea41SMatthias Ringwald 1364bfeaf344SMatthias Ringwald // next state 13651d81c7feSMatthias Ringwald hfp_parser_reset_line_buffer(hfp_connection); 1366dd533208SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE; 1367dd533208SMatthias Ringwald 1368bfeaf344SMatthias Ringwald return processed; 1369dd533208SMatthias Ringwald 1370bfeaf344SMatthias Ringwald case HFP_PARSER_CMD_SEQUENCE: 13711d81c7feSMatthias Ringwald // handle empty fields 13721d81c7feSMatthias Ringwald if ((byte == ',' ) && (hfp_connection->line_size == 0)){ 1373dd533208SMatthias Ringwald hfp_connection->line_buffer[0] = 0; 1374dd533208SMatthias Ringwald hfp_connection->ignore_value = 1; 1375dd533208SMatthias Ringwald parse_sequence(hfp_connection); 13763d051a53SMatthias Ringwald return true; 13773d051a53SMatthias Ringwald } 13783d051a53SMatthias Ringwald 13791d81c7feSMatthias Ringwald hfp_parser_store_if_token(hfp_connection, byte); 13801d81c7feSMatthias Ringwald if (!hfp_parser_is_separator(byte)) return true; 13811d81c7feSMatthias Ringwald 13821d81c7feSMatthias Ringwald // ignore empty tokens 138389e7c136SMilanka Ringwald switch (hfp_connection->command){ 138489e7c136SMilanka Ringwald case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION: 138589e7c136SMilanka Ringwald // don't ignore empty string 138689e7c136SMilanka Ringwald break; 138789e7c136SMilanka Ringwald default: 138889e7c136SMilanka Ringwald if (hfp_parser_is_buffer_empty(hfp_connection) && (hfp_connection->ignore_value == 0)) { 138989e7c136SMilanka Ringwald return true; 139089e7c136SMilanka Ringwald } 139189e7c136SMilanka Ringwald break; 139289e7c136SMilanka Ringwald } 13933d051a53SMatthias Ringwald 13943d051a53SMatthias Ringwald parse_sequence(hfp_connection); 13951d81c7feSMatthias Ringwald 13961d81c7feSMatthias Ringwald hfp_parser_reset_line_buffer(hfp_connection); 13971d81c7feSMatthias Ringwald 13981d81c7feSMatthias Ringwald switch (hfp_connection->command){ 13991d81c7feSMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 14001d81c7feSMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 14011d81c7feSMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 14021d81c7feSMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 14031d81c7feSMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 14041d81c7feSMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 14051d81c7feSMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 14061d81c7feSMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 14071d81c7feSMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 14081d81c7feSMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_SECOND_ITEM; 14091d81c7feSMatthias Ringwald break; 14101d81c7feSMatthias Ringwald default: 14111d81c7feSMatthias Ringwald break; 14121d81c7feSMatthias Ringwald } 14133d051a53SMatthias Ringwald return true; 14143d051a53SMatthias Ringwald 1415ba0de059SMatthias Ringwald case HFP_PARSER_SECOND_ITEM: 14161d81c7feSMatthias Ringwald 14171d81c7feSMatthias Ringwald hfp_parser_store_if_token(hfp_connection, byte); 14181d81c7feSMatthias Ringwald if (!hfp_parser_is_separator(byte)) return true; 1419dd533208SMatthias Ringwald 1420a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1421ce263fc8SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1422a0ffb263SMatthias Ringwald log_info("format %s, ", hfp_connection->line_buffer); 14232308e108SMilanka Ringwald hfp_connection->network_operator.format = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1424ce263fc8SMatthias Ringwald break; 1425ce263fc8SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 1426a0ffb263SMatthias Ringwald log_info("format %s \n", hfp_connection->line_buffer); 14272308e108SMilanka Ringwald hfp_connection->network_operator.format = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1428ce263fc8SMatthias Ringwald break; 1429ce263fc8SMatthias Ringwald case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 1430ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 1431ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 14322308e108SMilanka Ringwald hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].state = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1433ce263fc8SMatthias Ringwald break; 1434ce263fc8SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 14352308e108SMilanka Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1436a0ffb263SMatthias Ringwald log_info("%d \n", hfp_connection->ag_indicators[hfp_connection->parser_item_index].status); 1437a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].status_changed = 1; 1438ce263fc8SMatthias Ringwald break; 1439ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 14402308e108SMilanka Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].min_range = btstack_atoi((char *)hfp_connection->line_buffer); 1441a0ffb263SMatthias Ringwald log_info("%s, ", hfp_connection->line_buffer); 1442ce263fc8SMatthias Ringwald break; 1443ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 1444a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1445a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 14462308e108SMilanka Ringwald hfp_connection->bnip_type = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1447ce263fc8SMatthias Ringwald break; 14480222a807SMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 14490222a807SMatthias Ringwald hfp_connection->parser_indicator_value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 14500222a807SMatthias Ringwald break; 1451ce263fc8SMatthias Ringwald default: 1452ce263fc8SMatthias Ringwald break; 1453ce263fc8SMatthias Ringwald } 14541d81c7feSMatthias Ringwald 14551d81c7feSMatthias Ringwald hfp_parser_reset_line_buffer(hfp_connection); 14561d81c7feSMatthias Ringwald 14571d81c7feSMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_THIRD_ITEM; 14581d81c7feSMatthias Ringwald 1459ba0de059SMatthias Ringwald return true; 1460ce263fc8SMatthias Ringwald 146174c7548aSMatthias Ringwald case HFP_PARSER_THIRD_ITEM: 14621d81c7feSMatthias Ringwald 14631d81c7feSMatthias Ringwald hfp_parser_store_if_token(hfp_connection, byte); 14641d81c7feSMatthias Ringwald if (!hfp_parser_is_separator(byte)) return true; 14651d81c7feSMatthias Ringwald 1466a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1467ce263fc8SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1468c10fde09SMatthias Ringwald btstack_strcpy(hfp_connection->network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE, (char *)hfp_connection->line_buffer); 1469ce263fc8SMatthias Ringwald break; 1470ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 14712308e108SMilanka Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].max_range = btstack_atoi((char *)hfp_connection->line_buffer); 147225789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 147325789943SMilanka Ringwald hfp_connection->ag_indicators_nr = hfp_connection->parser_item_index; 1474ce263fc8SMatthias Ringwald break; 1475125560b8SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1476f6e6c52dSMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1477125560b8SMatthias Ringwald // track if last argument exists 1478125560b8SMatthias Ringwald hfp_connection->clip_have_alpha = hfp_connection->line_size != 0; 1479125560b8SMatthias Ringwald break; 1480ce263fc8SMatthias Ringwald default: 1481ce263fc8SMatthias Ringwald break; 1482ce263fc8SMatthias Ringwald } 14831d81c7feSMatthias Ringwald 14841d81c7feSMatthias Ringwald hfp_parser_reset_line_buffer(hfp_connection); 14851d81c7feSMatthias Ringwald 14861d81c7feSMatthias Ringwald if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS){ 14871d81c7feSMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE; 14881d81c7feSMatthias Ringwald } 1489d6b237acSMatthias Ringwald return true; 149074c7548aSMatthias Ringwald 1491471dea41SMatthias Ringwald case HFP_PARSER_CUSTOM_COMMAND: 1492471dea41SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 1493471dea41SMatthias Ringwald return true; 1494471dea41SMatthias Ringwald 149574c7548aSMatthias Ringwald default: 149674c7548aSMatthias Ringwald btstack_assert(false); 149774c7548aSMatthias Ringwald return true; 149874c7548aSMatthias Ringwald } 1499d6b237acSMatthias Ringwald } 1500d6b237acSMatthias Ringwald 1501d6b237acSMatthias Ringwald void hfp_parse(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){ 1502d6b237acSMatthias Ringwald bool processed = false; 1503d6b237acSMatthias Ringwald while (!processed){ 1504d6b237acSMatthias Ringwald processed = hfp_parse_byte(hfp_connection, byte, isHandsFree); 1505d6b237acSMatthias Ringwald } 15061d81c7feSMatthias Ringwald // reset parser state on end-of-line 15071d81c7feSMatthias Ringwald if (hfp_parser_is_end_of_line(byte)){ 1508a34e380cSMatthias Ringwald hfp_connection->found_equal_sign = false; 15091d81c7feSMatthias Ringwald hfp_connection->parser_item_index = 0; 15101d81c7feSMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 15111d81c7feSMatthias Ringwald } 1512ce263fc8SMatthias Ringwald } 1513ce263fc8SMatthias Ringwald 1514a0ffb263SMatthias Ringwald static void parse_sequence(hfp_connection_t * hfp_connection){ 1515ce263fc8SMatthias Ringwald int value; 1516a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1517667ec068SMatthias Ringwald case HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS: 15182308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1519667ec068SMatthias Ringwald int i; 1520a0ffb263SMatthias Ringwald switch (hfp_connection->parser_item_index){ 1521667ec068SMatthias Ringwald case 0: 1522a0ffb263SMatthias Ringwald for (i=0;i<hfp_connection->generic_status_indicators_nr;i++){ 1523a0ffb263SMatthias Ringwald if (hfp_connection->generic_status_indicators[i].uuid == value){ 1524a0ffb263SMatthias Ringwald hfp_connection->parser_indicator_index = i; 1525667ec068SMatthias Ringwald break; 1526667ec068SMatthias Ringwald } 1527667ec068SMatthias Ringwald } 1528667ec068SMatthias Ringwald break; 1529667ec068SMatthias Ringwald case 1: 1530a0ffb263SMatthias Ringwald if (hfp_connection->parser_indicator_index <0) break; 1531a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[hfp_connection->parser_indicator_index].state = value; 1532667ec068SMatthias Ringwald log_info("HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS set indicator at index %u, to %u\n", 1533a0ffb263SMatthias Ringwald hfp_connection->parser_item_index, value); 1534667ec068SMatthias Ringwald break; 1535667ec068SMatthias Ringwald default: 1536667ec068SMatthias Ringwald break; 1537667ec068SMatthias Ringwald } 153825789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 1539667ec068SMatthias Ringwald break; 1540667ec068SMatthias Ringwald 1541667ec068SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1542a0ffb263SMatthias Ringwald switch(hfp_connection->parser_item_index){ 1543667ec068SMatthias Ringwald case 0: 154428a4aea3SMilanka Ringwald // <alpha>: This optional field is not supported, and shall be left blank. 154528a4aea3SMilanka Ringwald break; 154628a4aea3SMilanka Ringwald case 1: 154728a4aea3SMilanka Ringwald // <number>: Quoted string containing the phone number in the format specified by <type>. 1548c10fde09SMatthias Ringwald btstack_strcpy(hfp_connection->bnip_number, sizeof(hfp_connection->bnip_number), (char *)hfp_connection->line_buffer); 1549667ec068SMatthias Ringwald break; 155028a4aea3SMilanka Ringwald case 2: 155128a4aea3SMilanka Ringwald /* 155228a4aea3SMilanka Ringwald <type> field specifies the format of the phone number provided, and can be one of the following values: 155328a4aea3SMilanka 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. 155428a4aea3SMilanka 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. 155528a4aea3SMilanka Ringwald - values 160-175: National number. No prefix nor escape digits included. 155628a4aea3SMilanka Ringwald */ 15572308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1558a0ffb263SMatthias Ringwald hfp_connection->bnip_type = value; 1559667ec068SMatthias Ringwald break; 156028a4aea3SMilanka Ringwald case 3: 156128a4aea3SMilanka Ringwald // <speed>: This optional field is not supported, and shall be left blank. 156228a4aea3SMilanka Ringwald break; 156328a4aea3SMilanka Ringwald case 4: 156428a4aea3SMilanka Ringwald // <service>: Indicates which service this phone number relates to. Shall be either 4 (voice) or 5 (fax). 1565667ec068SMatthias Ringwald default: 1566667ec068SMatthias Ringwald break; 1567667ec068SMatthias Ringwald } 1568eed67a37SMilanka Ringwald // index > 2 are ignored in switch above 1569a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1570667ec068SMatthias Ringwald break; 1571667ec068SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 1572a0ffb263SMatthias Ringwald switch(hfp_connection->parser_item_index){ 1573667ec068SMatthias Ringwald case 0: 15742308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1575a0ffb263SMatthias Ringwald hfp_connection->clcc_idx = value; 1576667ec068SMatthias Ringwald break; 1577667ec068SMatthias Ringwald case 1: 15782308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1579a0ffb263SMatthias Ringwald hfp_connection->clcc_dir = value; 1580667ec068SMatthias Ringwald break; 1581667ec068SMatthias Ringwald case 2: 15822308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1583a0ffb263SMatthias Ringwald hfp_connection->clcc_status = value; 1584667ec068SMatthias Ringwald break; 1585667ec068SMatthias Ringwald case 3: 15862308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 15870aee97efSMilanka Ringwald hfp_connection->clcc_mode = value; 1588667ec068SMatthias Ringwald break; 1589667ec068SMatthias Ringwald case 4: 15900aee97efSMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 15910aee97efSMilanka Ringwald hfp_connection->clcc_mpty = value; 15920aee97efSMilanka Ringwald break; 15930aee97efSMilanka Ringwald case 5: 1594c10fde09SMatthias Ringwald btstack_strcpy(hfp_connection->bnip_number, sizeof(hfp_connection->bnip_number), (char *)hfp_connection->line_buffer); 1595667ec068SMatthias Ringwald break; 15960aee97efSMilanka Ringwald case 6: 15972308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1598a0ffb263SMatthias Ringwald hfp_connection->bnip_type = value; 1599667ec068SMatthias Ringwald break; 1600667ec068SMatthias Ringwald default: 1601667ec068SMatthias Ringwald break; 1602667ec068SMatthias Ringwald } 1603eed67a37SMilanka Ringwald // index > 6 are ignored in switch above 1604a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1605667ec068SMatthias Ringwald break; 1606aa4dd815SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 16072308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1608a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = value; 1609aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CMD_SET_MICROPHONE_GAIN %d\n", value); 1610aa4dd815SMatthias Ringwald break; 1611aa4dd815SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 16122308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1613a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = value; 1614aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CMD_SET_SPEAKER_GAIN %d\n", value); 1615aa4dd815SMatthias Ringwald break; 1616aa4dd815SMatthias Ringwald case HFP_CMD_TURN_OFF_EC_AND_NR: 16172308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1618a0ffb263SMatthias Ringwald hfp_connection->ag_echo_and_noise_reduction = value; 1619aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CMD_TURN_OFF_EC_AND_NR %d\n", value); 1620aa4dd815SMatthias Ringwald break; 1621aa4dd815SMatthias Ringwald case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING: 16222308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1623a0ffb263SMatthias Ringwald hfp_connection->remote_supported_features = store_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE, value); 1624aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CHANGE_IN_BAND_RING_TONE_SETTING %d\n", value); 1625aa4dd815SMatthias Ringwald break; 16263deb3ec6SMatthias Ringwald case HFP_CMD_HF_CONFIRMED_CODEC: 16272308e108SMilanka Ringwald hfp_connection->codec_confirmed = btstack_atoi((char*)hfp_connection->line_buffer); 1628a0ffb263SMatthias Ringwald log_info("hfp parse HFP_CMD_HF_CONFIRMED_CODEC %d\n", hfp_connection->codec_confirmed); 16293deb3ec6SMatthias Ringwald break; 16303deb3ec6SMatthias Ringwald case HFP_CMD_AG_SUGGESTED_CODEC: 16312308e108SMilanka Ringwald hfp_connection->suggested_codec = btstack_atoi((char*)hfp_connection->line_buffer); 1632a0ffb263SMatthias Ringwald log_info("hfp parse HFP_CMD_AG_SUGGESTED_CODEC %d\n", hfp_connection->suggested_codec); 16333deb3ec6SMatthias Ringwald break; 16343deb3ec6SMatthias Ringwald case HFP_CMD_SUPPORTED_FEATURES: 16352308e108SMilanka Ringwald hfp_connection->remote_supported_features = btstack_atoi((char*)hfp_connection->line_buffer); 1636bace42efSMatthias Ringwald log_info("Parsed supported feature %d\n", (int) hfp_connection->remote_supported_features); 16373deb3ec6SMatthias Ringwald break; 16383deb3ec6SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 1639a0ffb263SMatthias Ringwald log_info("Parsed codec %s\n", hfp_connection->line_buffer); 1640ab2445a0SMatthias Ringwald hfp_connection->remote_codecs[hfp_connection->parser_item_index] = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 164125789943SMilanka Ringwald hfp_next_codec_index(hfp_connection); 1642a0ffb263SMatthias Ringwald hfp_connection->remote_codecs_nr = hfp_connection->parser_item_index; 16433deb3ec6SMatthias Ringwald break; 1644aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 1645c10fde09SMatthias Ringwald btstack_strcpy((char *)hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, HFP_MAX_INDICATOR_DESC_SIZE, (char *)hfp_connection->line_buffer); 1646a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].index = hfp_connection->parser_item_index+1; 1647a0ffb263SMatthias Ringwald log_info("Indicator %d: %s (", hfp_connection->ag_indicators_nr+1, hfp_connection->line_buffer); 1648aa4dd815SMatthias Ringwald break; 1649aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 1650a0ffb263SMatthias Ringwald log_info("Parsed Indicator %d with status: %s\n", hfp_connection->parser_item_index+1, hfp_connection->line_buffer); 16512308e108SMilanka Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = btstack_atoi((char *) hfp_connection->line_buffer); 165225789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 16533deb3ec6SMatthias Ringwald break; 16543deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 165525789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 1656a0ffb263SMatthias Ringwald if (hfp_connection->parser_item_index != 4) break; 1657a0ffb263SMatthias Ringwald log_info("Parsed Enable indicators: %s\n", hfp_connection->line_buffer); 16582308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1659a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = (uint8_t) value; 16603deb3ec6SMatthias Ringwald break; 16613deb3ec6SMatthias Ringwald case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES: 1662a0ffb263SMatthias Ringwald log_info("Parsed Support call hold: %s\n", hfp_connection->line_buffer); 1663a0ffb263SMatthias Ringwald if (hfp_connection->line_size > 2 ) break; 166497d2cfbcSMatthias 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); 166525789943SMilanka Ringwald hfp_connection->remote_call_services[hfp_connection->remote_call_services_index].name[HFP_CALL_SERVICE_SIZE - 1] = 0; 1666eed67a37SMilanka Ringwald hfp_next_remote_call_services_index(hfp_connection); 16673deb3ec6SMatthias Ringwald break; 1668aa4dd815SMatthias Ringwald case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 1669aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 1670a0ffb263SMatthias Ringwald log_info("Parsed Generic status indicator: %s\n", hfp_connection->line_buffer); 16712308e108SMilanka Ringwald hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].uuid = (uint16_t)btstack_atoi((char*)hfp_connection->line_buffer); 167225789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 1673a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators_nr = hfp_connection->parser_item_index; 16743deb3ec6SMatthias Ringwald break; 1675aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 16763deb3ec6SMatthias Ringwald // HF parses inital AG gen. ind. state 1677a0ffb263SMatthias Ringwald log_info("Parsed List generic status indicator %s state: ", hfp_connection->line_buffer); 167825789943SMilanka Ringwald hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection); 16793deb3ec6SMatthias Ringwald break; 1680ce263fc8SMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 168125789943SMilanka Ringwald hfp_connection->parser_indicator_index = hfp_parse_indicator_index(hfp_connection); 1682a0ffb263SMatthias Ringwald log_info("Parsed HF indicator index %u", hfp_connection->parser_indicator_index); 1683ce263fc8SMatthias Ringwald break; 16843deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE: 16853deb3ec6SMatthias Ringwald // AG parses new gen. ind. state 1686a0ffb263SMatthias Ringwald if (hfp_connection->ignore_value){ 1687a0ffb263SMatthias Ringwald hfp_connection->ignore_value = 0; 1688a0ffb263SMatthias Ringwald log_info("Parsed Enable AG indicator pos %u('%s') - unchanged (stays %u)\n", hfp_connection->parser_item_index, 1689a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled); 1690ce263fc8SMatthias Ringwald } 1691a0ffb263SMatthias Ringwald else if (hfp_connection->ag_indicators[hfp_connection->parser_item_index].mandatory){ 1692ce263fc8SMatthias Ringwald log_info("Parsed Enable AG indicator pos %u('%s') - ignore (mandatory)\n", 1693a0ffb263SMatthias Ringwald hfp_connection->parser_item_index, hfp_connection->ag_indicators[hfp_connection->parser_item_index].name); 1694ce263fc8SMatthias Ringwald } else { 16952308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1696a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled = value; 1697a0ffb263SMatthias Ringwald log_info("Parsed Enable AG indicator pos %u('%s'): %u\n", hfp_connection->parser_item_index, 1698a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, value); 16993deb3ec6SMatthias Ringwald } 170025789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 17013deb3ec6SMatthias Ringwald break; 17023deb3ec6SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 17033deb3ec6SMatthias Ringwald // indicators are indexed starting with 1 170425789943SMilanka Ringwald hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection); 1705a0ffb263SMatthias Ringwald log_info("Parsed status of the AG indicator %d, status ", hfp_connection->parser_item_index); 17063deb3ec6SMatthias Ringwald break; 1707aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 17082308e108SMilanka Ringwald hfp_connection->network_operator.mode = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1709a0ffb263SMatthias Ringwald log_info("Parsed network operator mode: %d, ", hfp_connection->network_operator.mode); 1710aa4dd815SMatthias Ringwald break; 1711aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 1712a0ffb263SMatthias Ringwald if (hfp_connection->line_buffer[0] == '3'){ 1713a0ffb263SMatthias Ringwald log_info("Parsed Set network operator format : %s, ", hfp_connection->line_buffer); 17143deb3ec6SMatthias Ringwald break; 17153deb3ec6SMatthias Ringwald } 17163deb3ec6SMatthias Ringwald // TODO emit ERROR, wrong format 1717a0ffb263SMatthias Ringwald log_info("ERROR Set network operator format: index %s not supported\n", hfp_connection->line_buffer); 17183deb3ec6SMatthias Ringwald break; 17193deb3ec6SMatthias Ringwald case HFP_CMD_ERROR: 17203deb3ec6SMatthias Ringwald break; 17213deb3ec6SMatthias Ringwald case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1722a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 1; 17232308e108SMilanka Ringwald hfp_connection->extended_audio_gateway_error_value = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 17243deb3ec6SMatthias Ringwald break; 17253deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR: 17262308e108SMilanka Ringwald hfp_connection->enable_extended_audio_gateway_error_report = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1727a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1728a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 17293deb3ec6SMatthias Ringwald break; 1730ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 1731a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1732a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1733c10fde09SMatthias Ringwald btstack_strcpy((char *)hfp_connection->bnip_number, sizeof(hfp_connection->bnip_number), (char *)hfp_connection->line_buffer); 17343deb3ec6SMatthias Ringwald break; 17350222a807SMatthias Ringwald case HFP_CMD_CALL_HOLD: 17360222a807SMatthias Ringwald hfp_connection->ag_call_hold_action = hfp_connection->line_buffer[0] - '0'; 17370222a807SMatthias Ringwald if (hfp_connection->line_buffer[1] != '\0'){ 17380222a807SMatthias Ringwald hfp_connection->call_index = btstack_atoi((char *)&hfp_connection->line_buffer[1]); 17390222a807SMatthias Ringwald } 17400222a807SMatthias Ringwald break; 17410222a807SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_COMMAND: 17420222a807SMatthias Ringwald hfp_connection->ag_response_and_hold_action = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 17430222a807SMatthias Ringwald break; 17440222a807SMatthias Ringwald case HFP_CMD_TRANSMIT_DTMF_CODES: 17450222a807SMatthias Ringwald hfp_connection->ag_dtmf_code = hfp_connection->line_buffer[0]; 17460222a807SMatthias Ringwald break; 17470222a807SMatthias Ringwald case HFP_CMD_ENABLE_CLIP: 17480222a807SMatthias Ringwald hfp_connection->clip_enabled = hfp_connection->line_buffer[0] != '0'; 17490222a807SMatthias Ringwald break; 17500222a807SMatthias Ringwald case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION: 17510222a807SMatthias Ringwald hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[0] != '0'; 17520222a807SMatthias Ringwald break; 17530b4debbfSMilanka Ringwald case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION: 17540b4debbfSMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 17550b4debbfSMilanka Ringwald hfp_connection->ag_activate_voice_recognition_value = value; 17560b4debbfSMilanka Ringwald break; 1757db3cdbd4SMilanka Ringwald case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION: 1758db3cdbd4SMilanka Ringwald switch(hfp_connection->parser_item_index){ 1759db3cdbd4SMilanka Ringwald case 0: 17600b4debbfSMilanka Ringwald hfp_connection->ag_vra_status = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1761db3cdbd4SMilanka Ringwald break; 1762db3cdbd4SMilanka Ringwald case 1: 1763c78e7b73SMatthias Ringwald hfp_connection->ag_vra_state = (hfp_voice_recognition_state_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1764db3cdbd4SMilanka Ringwald break; 1765db3cdbd4SMilanka Ringwald case 2: 176645796ff1SMilanka Ringwald hfp_connection->ag_msg.text_id = 0; 1767db3cdbd4SMilanka Ringwald for (i = 0 ; i < 4; i++){ 176845796ff1SMilanka Ringwald hfp_connection->ag_msg.text_id = (hfp_connection->ag_msg.text_id << 4) | nibble_for_char(hfp_connection->line_buffer[i]); 1769db3cdbd4SMilanka Ringwald } 1770db3cdbd4SMilanka Ringwald break; 1771db3cdbd4SMilanka Ringwald case 3: 17720d854c7cSMilanka Ringwald hfp_connection->ag_msg.text_type = (hfp_text_type_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1773db3cdbd4SMilanka Ringwald break; 1774db3cdbd4SMilanka Ringwald case 4: 17750d854c7cSMilanka Ringwald hfp_connection->ag_msg.text_operation = (hfp_text_operation_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1776db3cdbd4SMilanka Ringwald break; 1777db3cdbd4SMilanka Ringwald case 5: 177851aa5d5aSMilanka Ringwald hfp_connection->line_buffer[hfp_connection->line_size] = 0; 177951aa5d5aSMilanka Ringwald hfp_connection->ag_vra_msg_length = hfp_connection->line_size + 1; 1780db3cdbd4SMilanka Ringwald break; 1781db3cdbd4SMilanka Ringwald default: 1782db3cdbd4SMilanka Ringwald break; 1783db3cdbd4SMilanka Ringwald } 1784ddbf29d2SMilanka Ringwald hfp_connection->parser_item_index++; 1785db3cdbd4SMilanka Ringwald break; 17863deb3ec6SMatthias Ringwald default: 17873deb3ec6SMatthias Ringwald break; 17883deb3ec6SMatthias Ringwald } 17893deb3ec6SMatthias Ringwald } 17903deb3ec6SMatthias Ringwald 17911d9c9c90SMilanka Ringwald static void hfp_handle_start_sdp_client_query(void * context){ 17921d9c9c90SMilanka Ringwald UNUSED(context); 17931d9c9c90SMilanka Ringwald 17941d9c9c90SMilanka Ringwald btstack_linked_list_iterator_t it; 17951d9c9c90SMilanka Ringwald btstack_linked_list_iterator_init(&it, &hfp_connections); 17961d9c9c90SMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 17971d9c9c90SMilanka Ringwald hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 17981d9c9c90SMilanka Ringwald 17991d9c9c90SMilanka Ringwald if (connection->state != HFP_W2_SEND_SDP_QUERY) continue; 18001d9c9c90SMilanka Ringwald 18011d9c9c90SMilanka Ringwald connection->state = HFP_W4_SDP_QUERY_COMPLETE; 1802aeb0f0feSMatthias Ringwald hfp_sdp_query_context.local_role = connection->local_role; 1803aeb0f0feSMatthias Ringwald (void)memcpy(hfp_sdp_query_context.remote_address, connection->remote_addr, 6); 1804e55c05e3SMatthias Ringwald sdp_client_query_rfcomm_channel_and_name_for_service_class_uuid(&handle_query_rfcomm_event, connection->remote_addr, connection->service_uuid); 18051d9c9c90SMilanka Ringwald return; 18061d9c9c90SMilanka Ringwald } 18071d9c9c90SMilanka Ringwald } 18081d9c9c90SMilanka Ringwald 18094eb3f1d8SMilanka Ringwald uint8_t hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid, hfp_role_t local_role){ 18104eb3f1d8SMilanka Ringwald hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr, local_role); 18114eb3f1d8SMilanka Ringwald if (connection){ 18124eb3f1d8SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 18133deb3ec6SMatthias Ringwald } 18144eb3f1d8SMilanka Ringwald 18154eb3f1d8SMilanka Ringwald connection = hfp_create_connection(bd_addr, local_role); 18164eb3f1d8SMilanka Ringwald if (!connection){ 18174eb3f1d8SMilanka Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 18184eb3f1d8SMilanka Ringwald } 18194eb3f1d8SMilanka Ringwald 18204eb3f1d8SMilanka Ringwald connection->state = HFP_W2_SEND_SDP_QUERY; 18214eb3f1d8SMilanka Ringwald 18224eb3f1d8SMilanka Ringwald bd_addr_copy(connection->remote_addr, bd_addr); 18234eb3f1d8SMilanka Ringwald connection->service_uuid = service_uuid; 18241d9c9c90SMilanka Ringwald 1825aeb0f0feSMatthias Ringwald hfp_sdp_query_request.callback = &hfp_handle_start_sdp_client_query; 18261d9c9c90SMilanka Ringwald // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback 1827aeb0f0feSMatthias Ringwald (void) sdp_client_register_query_callback(&hfp_sdp_query_request); 18284eb3f1d8SMilanka Ringwald return ERROR_CODE_SUCCESS; 18293deb3ec6SMatthias Ringwald } 18303deb3ec6SMatthias Ringwald 1831657bc59fSMilanka Ringwald void hfp_trigger_release_service_level_connection(hfp_connection_t * hfp_connection){ 18321ffa0dd9SMilanka Ringwald // called internally, NULL check already performed 1833657bc59fSMilanka Ringwald btstack_assert(hfp_connection != NULL); 18343deb3ec6SMatthias Ringwald 1835657bc59fSMilanka Ringwald hfp_trigger_release_audio_connection(hfp_connection); 1836657bc59fSMilanka Ringwald if (hfp_connection->state < HFP_W4_RFCOMM_CONNECTED){ 1837657bc59fSMilanka Ringwald hfp_connection->state = HFP_IDLE; 18383deb3ec6SMatthias Ringwald return; 18393deb3ec6SMatthias Ringwald } 18403deb3ec6SMatthias Ringwald 1841657bc59fSMilanka Ringwald if (hfp_connection->state == HFP_W4_RFCOMM_CONNECTED){ 1842657bc59fSMilanka Ringwald hfp_connection->state = HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN; 18433deb3ec6SMatthias Ringwald return; 18443deb3ec6SMatthias Ringwald } 184552cbdd6dSMilanka Ringwald hfp_connection->release_slc_connection = 1; 1846657bc59fSMilanka Ringwald if (hfp_connection->state < HFP_W4_SCO_CONNECTED){ 1847657bc59fSMilanka Ringwald hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM; 18480c3047fbSMatthias Ringwald return; 18490c3047fbSMatthias Ringwald } 18500c3047fbSMatthias Ringwald 1851657bc59fSMilanka Ringwald if (hfp_connection->state < HFP_W4_SCO_DISCONNECTED){ 1852657bc59fSMilanka Ringwald hfp_connection->state = HFP_W2_DISCONNECT_SCO; 185352cbdd6dSMilanka Ringwald hfp_connection->release_audio_connection = 1; 18540c3047fbSMatthias Ringwald return; 18550c3047fbSMatthias Ringwald } 18563deb3ec6SMatthias Ringwald } 18573deb3ec6SMatthias Ringwald 18580b4debbfSMilanka Ringwald uint8_t hfp_trigger_release_audio_connection(hfp_connection_t * hfp_connection){ 18591ffa0dd9SMilanka Ringwald // called internally, NULL check already performed 1860657bc59fSMilanka Ringwald btstack_assert(hfp_connection != NULL); 18610b4debbfSMilanka Ringwald if (hfp_connection->state <= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){ 18620b4debbfSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 18631ffa0dd9SMilanka Ringwald } 18640b4debbfSMilanka Ringwald switch (hfp_connection->state) { 18650b4debbfSMilanka Ringwald case HFP_W2_CONNECT_SCO: 18660b4debbfSMilanka Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 18670b4debbfSMilanka Ringwald break; 18680b4debbfSMilanka Ringwald case HFP_W4_SCO_CONNECTED: 18690b4debbfSMilanka Ringwald case HFP_AUDIO_CONNECTION_ESTABLISHED: 18700b4debbfSMilanka Ringwald hfp_connection->release_audio_connection = 1; 18710b4debbfSMilanka Ringwald break; 18720b4debbfSMilanka Ringwald default: 18730b4debbfSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 18740b4debbfSMilanka Ringwald } 18750b4debbfSMilanka Ringwald return ERROR_CODE_SUCCESS; 18763deb3ec6SMatthias Ringwald } 18773deb3ec6SMatthias Ringwald 1878eddcd308SMatthias Ringwald void hfp_setup_synchronous_connection(hfp_connection_t * hfp_connection){ 1879ce263fc8SMatthias Ringwald // all packet types, fixed bandwidth 1880eddcd308SMatthias Ringwald int setting = hfp_connection->link_setting; 1881ce263fc8SMatthias Ringwald log_info("hfp_setup_synchronous_connection using setting nr %u", setting); 1882aeb0f0feSMatthias Ringwald hfp_sco_establishment_active = hfp_connection; 1883d2c1d59aSMatthias Ringwald uint16_t sco_voice_setting = hci_get_sco_voice_setting(); 1884d2c1d59aSMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 1885689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 1886689d4323SMatthias Ringwald sco_voice_setting = 0x0063; // Transparent data, 16-bit for BCM controllers 1887689d4323SMatthias Ringwald #else 1888689d4323SMatthias Ringwald sco_voice_setting = 0x0043; // Transparent data, 8-bit otherwise 1889689d4323SMatthias Ringwald #endif 1890d2c1d59aSMatthias Ringwald } 1891352a0504SMatthias Ringwald // get packet types - bits 6-9 are 'don't allow' 1892352a0504SMatthias Ringwald uint16_t packet_types = hfp_link_settings[setting].packet_types ^ 0x03c0; 1893eddcd308SMatthias Ringwald hci_send_cmd(&hci_setup_synchronous_connection, hfp_connection->acl_handle, 8000, 8000, hfp_link_settings[setting].max_latency, 1894352a0504SMatthias Ringwald sco_voice_setting, hfp_link_settings[setting].retransmission_effort, packet_types); 1895ce263fc8SMatthias Ringwald } 1896d68dcce1SMatthias Ringwald 1897cb81d35dSMatthias Ringwald void hfp_accept_synchronous_connection(hfp_connection_t * hfp_connection, bool incoming_eSCO){ 1898cb81d35dSMatthias Ringwald 1899cb81d35dSMatthias Ringwald // remote supported feature eSCO is set if link type is eSCO 1900cb81d35dSMatthias Ringwald // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms, 1901cb81d35dSMatthias Ringwald uint16_t max_latency; 1902cb81d35dSMatthias Ringwald uint8_t retransmission_effort; 1903cb81d35dSMatthias Ringwald uint16_t packet_types; 1904cb81d35dSMatthias Ringwald 1905cb81d35dSMatthias Ringwald if (incoming_eSCO && hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){ 1906cb81d35dSMatthias Ringwald max_latency = 0x000c; 1907cb81d35dSMatthias Ringwald retransmission_effort = 0x02; 1908cb81d35dSMatthias Ringwald // eSCO: EV3 and 2-EV3 1909cb81d35dSMatthias Ringwald packet_types = 0x0048; 1910cb81d35dSMatthias Ringwald } else { 1911cb81d35dSMatthias Ringwald max_latency = 0xffff; 1912cb81d35dSMatthias Ringwald retransmission_effort = 0xff; 1913cb81d35dSMatthias Ringwald // sco: HV1 and HV3 1914cb81d35dSMatthias Ringwald packet_types = 0x005; 1915cb81d35dSMatthias Ringwald } 1916cb81d35dSMatthias Ringwald 1917cb81d35dSMatthias Ringwald // mSBC only allows for transparent data 1918cb81d35dSMatthias Ringwald uint16_t sco_voice_setting = hci_get_sco_voice_setting(); 1919cb81d35dSMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 1920cb81d35dSMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 1921cb81d35dSMatthias Ringwald sco_voice_setting = 0x0063; // Transparent data, 16-bit for BCM controllers 1922cb81d35dSMatthias Ringwald #else 1923cb81d35dSMatthias Ringwald sco_voice_setting = 0x0043; // Transparent data, 8-bit otherwise 1924cb81d35dSMatthias Ringwald #endif 1925cb81d35dSMatthias Ringwald } 1926cb81d35dSMatthias Ringwald 1927cb81d35dSMatthias Ringwald // filter packet types 1928cb81d35dSMatthias Ringwald packet_types &= hfp_get_sco_packet_types(); 1929cb81d35dSMatthias Ringwald 1930cb81d35dSMatthias Ringwald // bits 6-9 are 'don't allow' 1931cb81d35dSMatthias Ringwald packet_types ^= 0x3c0; 1932cb81d35dSMatthias Ringwald 1933cb81d35dSMatthias Ringwald log_info("HFP: sending hci_accept_connection_request, packet types 0x%04x, sco_voice_setting 0x%02x", packet_types, sco_voice_setting); 1934cb81d35dSMatthias Ringwald hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency, 1935cb81d35dSMatthias Ringwald sco_voice_setting, retransmission_effort, packet_types); 1936cb81d35dSMatthias Ringwald } 1937cb81d35dSMatthias Ringwald 19383721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP 19393721a235SMatthias Ringwald void hfp_cc256x_prepare_for_sco(hfp_connection_t * hfp_connection){ 19403721a235SMatthias Ringwald hfp_connection->cc256x_send_write_codec_config = true; 19413721a235SMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 19423721a235SMatthias Ringwald hfp_connection->cc256x_send_wbs_associate = true; 19433721a235SMatthias Ringwald } 19443721a235SMatthias Ringwald } 19453721a235SMatthias Ringwald 19463721a235SMatthias Ringwald void hfp_cc256x_write_codec_config(hfp_connection_t * hfp_connection){ 19473721a235SMatthias Ringwald uint32_t sample_rate_hz; 19483721a235SMatthias Ringwald uint16_t clock_rate_khz; 19493721a235SMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 19503721a235SMatthias Ringwald clock_rate_khz = 512; 19513721a235SMatthias Ringwald sample_rate_hz = 16000; 19523721a235SMatthias Ringwald } else { 19533721a235SMatthias Ringwald clock_rate_khz = 256; 19543721a235SMatthias Ringwald sample_rate_hz = 8000; 19553721a235SMatthias Ringwald } 19563721a235SMatthias Ringwald uint8_t clock_direction = 0; // master 19573721a235SMatthias Ringwald uint16_t frame_sync_duty_cycle = 0; // i2s with 50% 19583721a235SMatthias Ringwald uint8_t frame_sync_edge = 1; // rising edge 19593721a235SMatthias Ringwald uint8_t frame_sync_polarity = 0; // active high 19603721a235SMatthias Ringwald uint8_t reserved = 0; 19613721a235SMatthias Ringwald uint16_t size = 16; 19623721a235SMatthias Ringwald uint16_t chan_1_offset = 1; 19633721a235SMatthias Ringwald uint16_t chan_2_offset = chan_1_offset + size; 19643721a235SMatthias Ringwald uint8_t out_edge = 1; // rising 19653721a235SMatthias Ringwald uint8_t in_edge = 0; // falling 19663721a235SMatthias Ringwald hci_send_cmd(&hci_ti_write_codec_config, clock_rate_khz, clock_direction, sample_rate_hz, frame_sync_duty_cycle, 19673721a235SMatthias Ringwald frame_sync_edge, frame_sync_polarity, reserved, 19683721a235SMatthias Ringwald size, chan_1_offset, out_edge, size, chan_1_offset, in_edge, reserved, 19693721a235SMatthias Ringwald size, chan_2_offset, out_edge, size, chan_2_offset, in_edge, reserved); 19703721a235SMatthias Ringwald } 19713721a235SMatthias Ringwald #endif 19723721a235SMatthias Ringwald 1973689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 1974689d4323SMatthias Ringwald void hfp_bcm_prepare_for_sco(hfp_connection_t * hfp_connection){ 1975689d4323SMatthias Ringwald hfp_connection->bcm_send_write_i2spcm_interface_param = true; 1976689d4323SMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 1977689d4323SMatthias Ringwald hfp_connection->bcm_send_enable_wbs = true; 1978689d4323SMatthias Ringwald } 1979689d4323SMatthias Ringwald } 1980689d4323SMatthias Ringwald void hfp_bcm_write_i2spcm_interface_param(hfp_connection_t * hfp_connection){ 1981689d4323SMatthias Ringwald uint8_t sample_rate = (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? 1 : 0; 19821d2bbd54SMatthias Ringwald // i2s enable, master, 8/16 kHz, 512 kHz 19831d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_i2spcm_interface_paramhci_bcm_write_i2spcm_interface_param, 1, 1, sample_rate, 2); 1984689d4323SMatthias Ringwald } 1985689d4323SMatthias Ringwald #endif 1986689d4323SMatthias Ringwald 19871b005648SMatthias Ringwald void hfp_set_hf_callback(btstack_packet_handler_t callback){ 19881b005648SMatthias Ringwald hfp_hf_callback = callback; 19891b005648SMatthias Ringwald } 19901b005648SMatthias Ringwald 19911b005648SMatthias Ringwald void hfp_set_ag_callback(btstack_packet_handler_t callback){ 19921b005648SMatthias Ringwald hfp_ag_callback = callback; 19931b005648SMatthias Ringwald } 19941b005648SMatthias Ringwald 1995ca59be51SMatthias Ringwald void hfp_set_ag_rfcomm_packet_handler(btstack_packet_handler_t handler){ 1996ca59be51SMatthias Ringwald hfp_ag_rfcomm_packet_handler = handler; 1997ca59be51SMatthias Ringwald } 19981b005648SMatthias Ringwald 1999ca59be51SMatthias Ringwald void hfp_set_hf_rfcomm_packet_handler(btstack_packet_handler_t handler){ 2000ca59be51SMatthias Ringwald hfp_hf_rfcomm_packet_handler = handler; 2001d68dcce1SMatthias Ringwald } 2002d68dcce1SMatthias Ringwald 2003520c92d5SMatthias Ringwald void hfp_init(void){ 2004352a0504SMatthias Ringwald hfp_allowed_sco_packet_types = SCO_PACKET_TYPES_ALL; 2005991c26beSMatthias Ringwald } 2006991c26beSMatthias Ringwald 200720b2edb6SMatthias Ringwald void hfp_deinit(void){ 2008aeb0f0feSMatthias Ringwald hfp_allowed_sco_packet_types = 0; 200920b2edb6SMatthias Ringwald hfp_connections = NULL; 201020b2edb6SMatthias Ringwald hfp_hf_callback = NULL; 201120b2edb6SMatthias Ringwald hfp_ag_callback = NULL; 201220b2edb6SMatthias Ringwald hfp_hf_rfcomm_packet_handler = NULL; 201320b2edb6SMatthias Ringwald hfp_ag_rfcomm_packet_handler = NULL; 2014aeb0f0feSMatthias Ringwald hfp_sco_establishment_active = NULL; 2015471dea41SMatthias Ringwald hfp_custom_commands_ag = NULL; 2016aeb0f0feSMatthias Ringwald (void) memset(&hfp_sdp_query_context, 0, sizeof(hfp_sdp_query_context_t)); 2017aeb0f0feSMatthias Ringwald (void) memset(&hfp_sdp_query_request, 0, sizeof(btstack_context_callback_registration_t)); 201820b2edb6SMatthias Ringwald } 201920b2edb6SMatthias Ringwald 2020991c26beSMatthias Ringwald void hfp_set_sco_packet_types(uint16_t packet_types){ 202101e5b727SMatthias Ringwald hfp_allowed_sco_packet_types = packet_types; 2022991c26beSMatthias Ringwald } 2023991c26beSMatthias Ringwald 2024991c26beSMatthias Ringwald uint16_t hfp_get_sco_packet_types(void){ 202501e5b727SMatthias Ringwald return hfp_allowed_sco_packet_types; 2026520c92d5SMatthias Ringwald } 2027186dd3d2SMatthias Ringwald 2028e453c1d9SMatthias Ringwald hfp_link_settings_t hfp_next_link_setting(hfp_link_settings_t current_setting, bool local_eSCO_supported, bool remote_eSCO_supported, bool eSCO_S4_supported, uint8_t negotiated_codec){ 2029e453c1d9SMatthias Ringwald int8_t setting = (int8_t) current_setting; 2030e453c1d9SMatthias Ringwald bool can_use_eSCO = local_eSCO_supported && remote_eSCO_supported; 2031e453c1d9SMatthias Ringwald while (setting > 0){ 2032e453c1d9SMatthias Ringwald setting--; 2033e453c1d9SMatthias Ringwald // skip if eSCO required but not available 2034e453c1d9SMatthias Ringwald if (hfp_link_settings[setting].eSCO && !can_use_eSCO) continue; 2035e453c1d9SMatthias Ringwald // skip if S4 but not supported 2036e453c1d9SMatthias Ringwald if ((setting == (int8_t) HFP_LINK_SETTINGS_S4) && !eSCO_S4_supported) continue; 2037e453c1d9SMatthias Ringwald // skip wrong codec 2038e453c1d9SMatthias Ringwald if ( hfp_link_settings[setting].codec != negotiated_codec) continue; 203901e5b727SMatthias Ringwald // skip disabled packet types 2040352a0504SMatthias Ringwald uint16_t required_packet_types = hfp_link_settings[setting].packet_types; 2041352a0504SMatthias Ringwald uint16_t allowed_packet_types = hfp_allowed_sco_packet_types; 2042352a0504SMatthias Ringwald if ((required_packet_types & allowed_packet_types) == 0) continue; 204301e5b727SMatthias Ringwald 2044e453c1d9SMatthias Ringwald // found matching setting 2045e453c1d9SMatthias Ringwald return (hfp_link_settings_t) setting; 2046afac2a04SMilanka Ringwald } 2047e453c1d9SMatthias Ringwald return HFP_LINK_SETTINGS_NONE; 2048afac2a04SMilanka Ringwald } 2049e453c1d9SMatthias Ringwald 20502b7abbfbSMatthias 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){ 2051e453c1d9SMatthias Ringwald bool local_eSCO_supported = hci_extended_sco_link_supported(); 2052e453c1d9SMatthias Ringwald bool remote_eSCO_supported = hci_remote_esco_supported(hfp_connection->acl_handle); 2053e453c1d9SMatthias Ringwald uint8_t negotiated_codec = hfp_connection->negotiated_codec; 20542b7abbfbSMatthias Ringwald return hfp_next_link_setting(current_setting, local_eSCO_supported, remote_eSCO_supported, eSCO_S4_supported, negotiated_codec); 20552b7abbfbSMatthias Ringwald } 20562b7abbfbSMatthias Ringwald 20572b7abbfbSMatthias Ringwald void hfp_init_link_settings(hfp_connection_t * hfp_connection, uint8_t eSCO_S4_supported){ 2058e453c1d9SMatthias Ringwald // get highest possible link setting 20592b7abbfbSMatthias Ringwald hfp_connection->link_setting = hfp_next_link_setting_for_connection(HFP_LINK_SETTINGS_NONE, hfp_connection, eSCO_S4_supported); 2060afac2a04SMilanka Ringwald log_info("hfp_init_link_settings: %u", hfp_connection->link_setting); 2061afac2a04SMilanka Ringwald } 2062afac2a04SMilanka Ringwald 2063186dd3d2SMatthias Ringwald #define HFP_HF_RX_DEBUG_PRINT_LINE 80 2064186dd3d2SMatthias Ringwald 2065186dd3d2SMatthias Ringwald void hfp_log_rfcomm_message(const char * tag, uint8_t * packet, uint16_t size){ 2066186dd3d2SMatthias Ringwald #ifdef ENABLE_LOG_INFO 2067186dd3d2SMatthias Ringwald // encode \n\r 2068186dd3d2SMatthias Ringwald char printable[HFP_HF_RX_DEBUG_PRINT_LINE+2]; 206915a27967SMatthias Ringwald int i = 0; 2070186dd3d2SMatthias Ringwald int pos; 207115a27967SMatthias Ringwald for (pos=0 ; (pos < size) && (i < (HFP_HF_RX_DEBUG_PRINT_LINE - 3)) ; pos++){ 2072186dd3d2SMatthias Ringwald switch (packet[pos]){ 2073186dd3d2SMatthias Ringwald case '\n': 2074186dd3d2SMatthias Ringwald printable[i++] = '\\'; 2075186dd3d2SMatthias Ringwald printable[i++] = 'n'; 2076186dd3d2SMatthias Ringwald break; 2077186dd3d2SMatthias Ringwald case '\r': 2078186dd3d2SMatthias Ringwald printable[i++] = '\\'; 2079186dd3d2SMatthias Ringwald printable[i++] = 'r'; 2080186dd3d2SMatthias Ringwald break; 2081186dd3d2SMatthias Ringwald default: 2082186dd3d2SMatthias Ringwald printable[i++] = packet[pos]; 2083186dd3d2SMatthias Ringwald break; 2084186dd3d2SMatthias Ringwald } 2085186dd3d2SMatthias Ringwald } 2086186dd3d2SMatthias Ringwald printable[i] = 0; 2087186dd3d2SMatthias Ringwald log_info("%s: '%s'", tag, printable); 2088186dd3d2SMatthias Ringwald #endif 2089186dd3d2SMatthias Ringwald } 2090471dea41SMatthias Ringwald 2091471dea41SMatthias Ringwald void hfp_register_custom_ag_command(hfp_custom_at_command_t * custom_at_command){ 2092471dea41SMatthias Ringwald btstack_linked_list_add(&hfp_custom_commands_ag, (btstack_linked_item_t *) custom_at_command); 2093471dea41SMatthias Ringwald } 2094