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 1043deb3ec6SMatthias Ringwald static const char * hfp_hf_features[] = { 1053deb3ec6SMatthias Ringwald "EC and/or NR function", 1063deb3ec6SMatthias Ringwald "Three-way calling", 1073deb3ec6SMatthias Ringwald "CLI presentation capability", 1083deb3ec6SMatthias Ringwald "Voice recognition activation", 1093deb3ec6SMatthias Ringwald "Remote volume control", 1103deb3ec6SMatthias Ringwald 1113deb3ec6SMatthias Ringwald "Enhanced call status", 1123deb3ec6SMatthias Ringwald "Enhanced call control", 1133deb3ec6SMatthias Ringwald 1143deb3ec6SMatthias Ringwald "Codec negotiation", 1153deb3ec6SMatthias Ringwald 1163deb3ec6SMatthias Ringwald "HF Indicators", 1173deb3ec6SMatthias Ringwald "eSCO S4 (and T2) Settings Supported", 1183deb3ec6SMatthias Ringwald "Reserved for future definition" 1193deb3ec6SMatthias Ringwald }; 1203deb3ec6SMatthias Ringwald 1213deb3ec6SMatthias Ringwald static const char * hfp_ag_features[] = { 1223deb3ec6SMatthias Ringwald "Three-way calling", 1233deb3ec6SMatthias Ringwald "EC and/or NR function", 1243deb3ec6SMatthias Ringwald "Voice recognition function", 1253deb3ec6SMatthias Ringwald "In-band ring tone capability", 1263deb3ec6SMatthias Ringwald "Attach a number to a voice tag", 1273deb3ec6SMatthias Ringwald "Ability to reject a call", 1283deb3ec6SMatthias Ringwald "Enhanced call status", 1293deb3ec6SMatthias Ringwald "Enhanced call control", 1303deb3ec6SMatthias Ringwald "Extended Error Result Codes", 1313deb3ec6SMatthias Ringwald "Codec negotiation", 1323deb3ec6SMatthias Ringwald "HF Indicators", 1333deb3ec6SMatthias Ringwald "eSCO S4 (and T2) Settings Supported", 1343deb3ec6SMatthias Ringwald "Reserved for future definition" 1353deb3ec6SMatthias Ringwald }; 1363deb3ec6SMatthias Ringwald 1370aee97efSMilanka Ringwald static const char * hfp_enhanced_call_dir[] = { 1380aee97efSMilanka Ringwald "outgoing", 1390aee97efSMilanka Ringwald "incoming" 1400aee97efSMilanka Ringwald }; 1410aee97efSMilanka Ringwald 1420aee97efSMilanka Ringwald static const char * hfp_enhanced_call_status[] = { 1430aee97efSMilanka Ringwald "active", 1440aee97efSMilanka Ringwald "held", 1450aee97efSMilanka Ringwald "outgoing dialing", 1460aee97efSMilanka Ringwald "outgoing alerting", 1470aee97efSMilanka Ringwald "incoming", 1480aee97efSMilanka Ringwald "incoming waiting", 1490aee97efSMilanka Ringwald "call held by response and hold" 1500aee97efSMilanka Ringwald }; 1510aee97efSMilanka Ringwald 1520aee97efSMilanka Ringwald static const char * hfp_enhanced_call_mode[] = { 1530aee97efSMilanka Ringwald "voice", 1540aee97efSMilanka Ringwald "data", 1550aee97efSMilanka Ringwald "fax" 1560aee97efSMilanka Ringwald }; 1570aee97efSMilanka Ringwald 1580aee97efSMilanka Ringwald static const char * hfp_enhanced_call_mpty[] = { 1590aee97efSMilanka Ringwald "not a conference call", 1600aee97efSMilanka Ringwald "conference call" 1610aee97efSMilanka Ringwald }; 1620aee97efSMilanka Ringwald 1630aee97efSMilanka Ringwald const char * hfp_enhanced_call_dir2str(uint16_t index){ 1640aee97efSMilanka Ringwald if (index <= HFP_ENHANCED_CALL_DIR_INCOMING) return hfp_enhanced_call_dir[index]; 1650aee97efSMilanka Ringwald return "not defined"; 1660aee97efSMilanka Ringwald } 1670aee97efSMilanka Ringwald 1680aee97efSMilanka Ringwald const char * hfp_enhanced_call_status2str(uint16_t index){ 1690aee97efSMilanka Ringwald if (index <= HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD) return hfp_enhanced_call_status[index]; 1700aee97efSMilanka Ringwald return "not defined"; 1710aee97efSMilanka Ringwald } 1720aee97efSMilanka Ringwald 1730aee97efSMilanka Ringwald const char * hfp_enhanced_call_mode2str(uint16_t index){ 1740aee97efSMilanka Ringwald if (index <= HFP_ENHANCED_CALL_MODE_FAX) return hfp_enhanced_call_mode[index]; 1750aee97efSMilanka Ringwald return "not defined"; 1760aee97efSMilanka Ringwald } 1770aee97efSMilanka Ringwald 1780aee97efSMilanka Ringwald const char * hfp_enhanced_call_mpty2str(uint16_t index){ 1790aee97efSMilanka Ringwald if (index <= HFP_ENHANCED_CALL_MPTY_CONFERENCE_CALL) return hfp_enhanced_call_mpty[index]; 1800aee97efSMilanka Ringwald return "not defined"; 1810aee97efSMilanka Ringwald } 1820aee97efSMilanka Ringwald 18325789943SMilanka Ringwald static uint16_t hfp_parse_indicator_index(hfp_connection_t * hfp_connection){ 18425789943SMilanka Ringwald uint16_t index = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 18525789943SMilanka Ringwald 18625789943SMilanka Ringwald if (index > HFP_MAX_NUM_INDICATORS){ 18725789943SMilanka Ringwald log_info("ignoring invalid indicator index bigger then HFP_MAX_NUM_INDICATORS"); 18825789943SMilanka Ringwald return HFP_MAX_NUM_INDICATORS - 1; 18925789943SMilanka Ringwald } 19025789943SMilanka Ringwald 19125789943SMilanka Ringwald // indicator index enumeration starts with 1, we substract 1 to store in array with starting index 0 19225789943SMilanka Ringwald if (index > 0){ 19325789943SMilanka Ringwald index -= 1; 19425789943SMilanka Ringwald } else { 19525789943SMilanka Ringwald log_info("ignoring invalid indicator index 0"); 19625789943SMilanka Ringwald return 0; 19725789943SMilanka Ringwald } 19825789943SMilanka Ringwald return index; 19925789943SMilanka Ringwald } 20025789943SMilanka Ringwald 20125789943SMilanka Ringwald static void hfp_next_indicators_index(hfp_connection_t * hfp_connection){ 20225789943SMilanka Ringwald if (hfp_connection->parser_item_index < HFP_MAX_NUM_INDICATORS - 1){ 20325789943SMilanka Ringwald hfp_connection->parser_item_index++; 20425789943SMilanka Ringwald } else { 20525789943SMilanka Ringwald log_info("Ignoring additional indicator"); 20625789943SMilanka Ringwald } 20725789943SMilanka Ringwald } 20825789943SMilanka Ringwald 20925789943SMilanka Ringwald static void hfp_next_codec_index(hfp_connection_t * hfp_connection){ 21025789943SMilanka Ringwald if (hfp_connection->parser_item_index < HFP_MAX_NUM_CODECS - 1){ 21125789943SMilanka Ringwald hfp_connection->parser_item_index++; 21225789943SMilanka Ringwald } else { 21325789943SMilanka Ringwald log_info("Ignoring additional codec index"); 21425789943SMilanka Ringwald } 21525789943SMilanka Ringwald } 21625789943SMilanka Ringwald 217eed67a37SMilanka Ringwald static void hfp_next_remote_call_services_index(hfp_connection_t * hfp_connection){ 218eed67a37SMilanka Ringwald if (hfp_connection->remote_call_services_index < HFP_MAX_NUM_CALL_SERVICES - 1){ 219eed67a37SMilanka Ringwald hfp_connection->remote_call_services_index++; 220eed67a37SMilanka Ringwald } else { 221eed67a37SMilanka Ringwald log_info("Ignoring additional remote_call_services"); 222eed67a37SMilanka Ringwald } 223eed67a37SMilanka Ringwald } 22425789943SMilanka Ringwald 2253deb3ec6SMatthias Ringwald const char * hfp_hf_feature(int index){ 2263deb3ec6SMatthias Ringwald if (index > HFP_HF_FEATURES_SIZE){ 2273deb3ec6SMatthias Ringwald return hfp_hf_features[HFP_HF_FEATURES_SIZE]; 2283deb3ec6SMatthias Ringwald } 2293deb3ec6SMatthias Ringwald return hfp_hf_features[index]; 2303deb3ec6SMatthias Ringwald } 2313deb3ec6SMatthias Ringwald 2323deb3ec6SMatthias Ringwald const char * hfp_ag_feature(int index){ 2333deb3ec6SMatthias Ringwald if (index > HFP_AG_FEATURES_SIZE){ 2343deb3ec6SMatthias Ringwald return hfp_ag_features[HFP_AG_FEATURES_SIZE]; 2353deb3ec6SMatthias Ringwald } 2363deb3ec6SMatthias Ringwald return hfp_ag_features[index]; 2373deb3ec6SMatthias Ringwald } 2383deb3ec6SMatthias Ringwald 2395ebff802SMatthias Ringwald int send_str_over_rfcomm(uint16_t cid, const char * command){ 2403deb3ec6SMatthias Ringwald if (!rfcomm_can_send_packet_now(cid)) return 1; 24174386ee0SMatthias Ringwald log_info("HFP_TX %s", command); 242ab2445a0SMatthias Ringwald int err = rfcomm_send(cid, (uint8_t*) command, (uint16_t) strlen(command)); 2433deb3ec6SMatthias Ringwald if (err){ 24428190c0bSMatthias Ringwald log_error("rfcomm_send -> error 0x%02x \n", err); 2453deb3ec6SMatthias Ringwald } 246e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES 247e43d1938SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(cid); 248e43d1938SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_SENT, command); 249e43d1938SMatthias Ringwald #endif 2503deb3ec6SMatthias Ringwald return 1; 2513deb3ec6SMatthias Ringwald } 2523deb3ec6SMatthias Ringwald 2536a7f44bdSMilanka Ringwald int hfp_supports_codec(uint8_t codec, int codecs_nr, uint8_t * codecs){ 254e8d1dc0dSMatthias Ringwald 255e8d1dc0dSMatthias Ringwald // mSBC requires support for eSCO connections 256c1ab6cc1SMatthias Ringwald if ((codec == HFP_CODEC_MSBC) && !hci_extended_sco_link_supported()) return 0; 257e8d1dc0dSMatthias Ringwald 258df327ff3SMilanka Ringwald int i; 259df327ff3SMilanka Ringwald for (i = 0; i < codecs_nr; i++){ 260e8d1dc0dSMatthias Ringwald if (codecs[i] != codec) continue; 261e8d1dc0dSMatthias Ringwald return 1; 262df327ff3SMilanka Ringwald } 263df327ff3SMilanka Ringwald return 0; 264df327ff3SMilanka Ringwald } 265df327ff3SMilanka Ringwald 266d715cf51SMatthias Ringwald void hfp_hf_drop_mSBC_if_eSCO_not_supported(uint8_t * codecs, uint8_t * codecs_nr){ 267d715cf51SMatthias Ringwald if (hci_extended_sco_link_supported()) return; 268d715cf51SMatthias Ringwald uint8_t tmp_codecs[HFP_MAX_NUM_CODECS]; 269d715cf51SMatthias Ringwald int i; 270d715cf51SMatthias Ringwald int tmp_codec_nr = 0; 271d715cf51SMatthias Ringwald for (i=0; i < *codecs_nr; i++){ 272d715cf51SMatthias Ringwald if (codecs[i] == HFP_CODEC_MSBC) continue; 273d715cf51SMatthias Ringwald tmp_codecs[tmp_codec_nr++] = codecs[i]; 274d715cf51SMatthias Ringwald } 275d715cf51SMatthias Ringwald *codecs_nr = tmp_codec_nr; 2766535961aSMatthias Ringwald (void)memcpy(codecs, tmp_codecs, tmp_codec_nr); 277d715cf51SMatthias Ringwald } 278d715cf51SMatthias Ringwald 2793deb3ec6SMatthias Ringwald // UTILS 2803deb3ec6SMatthias Ringwald int get_bit(uint16_t bitmap, int position){ 2813deb3ec6SMatthias Ringwald return (bitmap >> position) & 1; 2823deb3ec6SMatthias Ringwald } 2833deb3ec6SMatthias Ringwald 2843deb3ec6SMatthias Ringwald int store_bit(uint32_t bitmap, int position, uint8_t value){ 2853deb3ec6SMatthias Ringwald if (value){ 2863deb3ec6SMatthias Ringwald bitmap |= 1 << position; 2873deb3ec6SMatthias Ringwald } else { 2883deb3ec6SMatthias Ringwald bitmap &= ~ (1 << position); 2893deb3ec6SMatthias Ringwald } 2903deb3ec6SMatthias Ringwald return bitmap; 2913deb3ec6SMatthias Ringwald } 2923deb3ec6SMatthias Ringwald 2933deb3ec6SMatthias Ringwald int join(char * buffer, int buffer_size, uint8_t * values, int values_nr){ 294c1ab6cc1SMatthias Ringwald if (buffer_size < (values_nr * 3)) return 0; 2953deb3ec6SMatthias Ringwald int i; 2963deb3ec6SMatthias Ringwald int offset = 0; 297c1ab6cc1SMatthias Ringwald for (i = 0; i < (values_nr-1); i++) { 2983deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", values[i]); // puts string into buffer 2993deb3ec6SMatthias Ringwald } 3003deb3ec6SMatthias Ringwald if (i<values_nr){ 3013deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d", values[i]); 3023deb3ec6SMatthias Ringwald } 3033deb3ec6SMatthias Ringwald return offset; 3043deb3ec6SMatthias Ringwald } 3053deb3ec6SMatthias Ringwald 3063deb3ec6SMatthias Ringwald int join_bitmap(char * buffer, int buffer_size, uint32_t values, int values_nr){ 307c1ab6cc1SMatthias Ringwald if (buffer_size < (values_nr * 3)) return 0; 3083deb3ec6SMatthias Ringwald 3093deb3ec6SMatthias Ringwald int i; 3103deb3ec6SMatthias Ringwald int offset = 0; 311c1ab6cc1SMatthias Ringwald for (i = 0; i < (values_nr-1); i++) { 3123deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", get_bit(values,i)); // puts string into buffer 3133deb3ec6SMatthias Ringwald } 3143deb3ec6SMatthias Ringwald 3153deb3ec6SMatthias Ringwald if (i<values_nr){ 3163deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d", get_bit(values,i)); 3173deb3ec6SMatthias Ringwald } 3183deb3ec6SMatthias Ringwald return offset; 3193deb3ec6SMatthias Ringwald } 320293fae36SMatthias Ringwald static void hfp_emit_event_for_role(hfp_role_t local_role, uint8_t * packet, uint16_t size){ 321293fae36SMatthias Ringwald switch (local_role){ 322293fae36SMatthias Ringwald case HFP_ROLE_HF: 323293fae36SMatthias Ringwald (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, packet, size); 324293fae36SMatthias Ringwald break; 325293fae36SMatthias Ringwald case HFP_ROLE_AG: 326293fae36SMatthias Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, packet, size); 327293fae36SMatthias Ringwald break; 328293fae36SMatthias Ringwald default: 329293fae36SMatthias Ringwald btstack_unreachable(); 330293fae36SMatthias Ringwald break; 331293fae36SMatthias Ringwald } 332293fae36SMatthias Ringwald } 3333deb3ec6SMatthias Ringwald 334ca59be51SMatthias Ringwald static void hfp_emit_event_for_context(hfp_connection_t * hfp_connection, uint8_t * packet, uint16_t size){ 335ca59be51SMatthias Ringwald if (!hfp_connection) return; 336293fae36SMatthias Ringwald hfp_emit_event_for_role(hfp_connection->local_role, packet, size); 337ca59be51SMatthias Ringwald } 338ca59be51SMatthias Ringwald 339ca59be51SMatthias Ringwald void hfp_emit_simple_event(hfp_connection_t * hfp_connection, uint8_t event_subtype){ 3407d81706fSMatthias Ringwald hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 341d703d377SMatthias Ringwald uint8_t event[5]; 342a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 343a0ffb263SMatthias Ringwald event[1] = sizeof(event) - 2; 344a0ffb263SMatthias Ringwald event[2] = event_subtype; 3457d81706fSMatthias Ringwald little_endian_store_16(event, 3, acl_handle); 346ca59be51SMatthias Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 347a0ffb263SMatthias Ringwald } 348a0ffb263SMatthias Ringwald 349ca59be51SMatthias Ringwald void hfp_emit_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, uint8_t value){ 3507d81706fSMatthias Ringwald hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 351d703d377SMatthias Ringwald uint8_t event[6]; 3523deb3ec6SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 3533deb3ec6SMatthias Ringwald event[1] = sizeof(event) - 2; 3543deb3ec6SMatthias Ringwald event[2] = event_subtype; 3557d81706fSMatthias Ringwald little_endian_store_16(event, 3, acl_handle); 356d703d377SMatthias Ringwald event[5] = value; // status 0 == OK 357ca59be51SMatthias Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 3583deb3ec6SMatthias Ringwald } 3593deb3ec6SMatthias Ringwald 360553a4a56SMilanka Ringwald void hfp_emit_voice_recognition_enabled(hfp_connection_t * hfp_connection, uint8_t status){ 361553a4a56SMilanka Ringwald btstack_assert(hfp_connection != NULL); 362553a4a56SMilanka Ringwald 363a95ec82fSMilanka Ringwald uint8_t event[7]; 364a95ec82fSMilanka Ringwald event[0] = HCI_EVENT_HFP_META; 365a95ec82fSMilanka Ringwald event[1] = sizeof(event) - 2; 366a9e632e9SMilanka Ringwald event[2] = HFP_SUBEVENT_VOICE_RECOGNITION_ACTIVATED; 367de9e0ea7SMilanka Ringwald 368553a4a56SMilanka Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 369a95ec82fSMilanka Ringwald event[5] = status; // 0:success 370553a4a56SMilanka Ringwald event[6] = hfp_connection->enhanced_voice_recognition_enabled ? 1 : 0; 371553a4a56SMilanka Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 3721a26de69SMilanka Ringwald } 373553a4a56SMilanka Ringwald 374553a4a56SMilanka Ringwald void hfp_emit_voice_recognition_disabled(hfp_connection_t * hfp_connection, uint8_t status){ 375553a4a56SMilanka Ringwald btstack_assert(hfp_connection != NULL); 376553a4a56SMilanka Ringwald 377553a4a56SMilanka Ringwald uint8_t event[6]; 378553a4a56SMilanka Ringwald event[0] = HCI_EVENT_HFP_META; 379553a4a56SMilanka Ringwald event[1] = sizeof(event) - 2; 380a9e632e9SMilanka Ringwald event[2] = HFP_SUBEVENT_VOICE_RECOGNITION_DEACTIVATED; 381553a4a56SMilanka Ringwald 382553a4a56SMilanka Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 383553a4a56SMilanka Ringwald event[5] = status; // 0:success 384013cc750SMilanka Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 385013cc750SMilanka Ringwald } 386013cc750SMilanka Ringwald 387de9e0ea7SMilanka Ringwald void hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection_t * hfp_connection, uint8_t status){ 388de9e0ea7SMilanka Ringwald hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 389de9e0ea7SMilanka Ringwald 390de9e0ea7SMilanka Ringwald uint8_t event[6]; 391de9e0ea7SMilanka Ringwald event[0] = HCI_EVENT_HFP_META; 392de9e0ea7SMilanka Ringwald event[1] = sizeof(event) - 2; 393de9e0ea7SMilanka Ringwald event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_HF_READY_FOR_AUDIO; 394de9e0ea7SMilanka Ringwald little_endian_store_16(event, 3, acl_handle); 395de9e0ea7SMilanka Ringwald event[5] = status; 396de9e0ea7SMilanka Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 397de9e0ea7SMilanka Ringwald } 398de9e0ea7SMilanka Ringwald 399cf75be85SMilanka Ringwald void hfp_emit_enhanced_voice_recognition_state_event(hfp_connection_t * hfp_connection, uint8_t status){ 400cf75be85SMilanka Ringwald hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 401cf75be85SMilanka Ringwald 402cf75be85SMilanka Ringwald uint8_t event[6]; 403cf75be85SMilanka Ringwald event[0] = HCI_EVENT_HFP_META; 404cf75be85SMilanka Ringwald event[1] = sizeof(event) - 2; 405cf75be85SMilanka Ringwald switch (hfp_connection->ag_vra_state){ 406cf75be85SMilanka Ringwald case HFP_VOICE_RECOGNITION_STATE_AG_READY_TO_ACCEPT_AUDIO_INPUT: 407cf75be85SMilanka Ringwald event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_READY_TO_ACCEPT_AUDIO_INPUT; 408cf75be85SMilanka Ringwald break; 409cf75be85SMilanka Ringwald case HFP_VOICE_RECOGNITION_STATE_AG_IS_STARTING_SOUND: 410cf75be85SMilanka Ringwald event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_STARTING_SOUND; 411cf75be85SMilanka Ringwald break; 412cf75be85SMilanka Ringwald case HFP_VOICE_RECOGNITION_STATE_AG_IS_PROCESSING_AUDIO_INPUT: 413cf75be85SMilanka Ringwald event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_PROCESSING_AUDIO_INPUT; 414cf75be85SMilanka Ringwald break; 415cf75be85SMilanka Ringwald default: 416cf75be85SMilanka Ringwald btstack_unreachable(); 417cf75be85SMilanka Ringwald break; 418cf75be85SMilanka Ringwald } 419cf75be85SMilanka Ringwald 420cf75be85SMilanka Ringwald little_endian_store_16(event, 3, acl_handle); 421cf75be85SMilanka Ringwald event[5] = status; 422cf75be85SMilanka Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 423cf75be85SMilanka Ringwald } 424cf75be85SMilanka Ringwald 4257095467fSMatthias 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){ 4268901a7c4SMatthias Ringwald uint8_t event[12]; 4276a7f44bdSMilanka Ringwald int pos = 0; 4286a7f44bdSMilanka Ringwald event[pos++] = HCI_EVENT_HFP_META; 4296a7f44bdSMilanka Ringwald event[pos++] = sizeof(event) - 2; 430d0c4aea6SMilanka Ringwald event[pos++] = HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 4316a7f44bdSMilanka Ringwald little_endian_store_16(event, pos, con_handle); 4326a7f44bdSMilanka Ringwald pos += 2; 43303870092SMilanka Ringwald event[pos++] = status; // status 0 == OK 4346a7f44bdSMilanka Ringwald reverse_bd_addr(addr,&event[pos]); 4356a7f44bdSMilanka Ringwald pos += 6; 4367095467fSMatthias Ringwald hfp_emit_event_for_role(local_role, event, sizeof(event)); 437a0653c3bSMilanka Ringwald } 438a0653c3bSMilanka Ringwald 439d703d377SMatthias Ringwald static void hfp_emit_audio_connection_released(hfp_connection_t * hfp_connection, hci_con_handle_t sco_handle){ 4407d81706fSMatthias Ringwald btstack_assert(hfp_connection != NULL); 441d703d377SMatthias Ringwald uint8_t event[7]; 4425441d52fSMatthias Ringwald int pos = 0; 4435441d52fSMatthias Ringwald event[pos++] = HCI_EVENT_HFP_META; 4445441d52fSMatthias Ringwald event[pos++] = sizeof(event) - 2; 4455441d52fSMatthias Ringwald event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED; 446d703d377SMatthias Ringwald little_endian_store_16(event, pos, hfp_connection->acl_handle); 447d703d377SMatthias Ringwald pos += 2; 448d703d377SMatthias Ringwald little_endian_store_16(event, pos, sco_handle); 449d703d377SMatthias Ringwald pos += 2; 4505441d52fSMatthias Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 4515441d52fSMatthias Ringwald } 4525441d52fSMatthias Ringwald 453d703d377SMatthias 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){ 4547d81706fSMatthias Ringwald btstack_assert(hfp_connection != NULL); 455d703d377SMatthias Ringwald uint8_t event[15]; 456d0c4aea6SMilanka Ringwald int pos = 0; 457d0c4aea6SMilanka Ringwald event[pos++] = HCI_EVENT_HFP_META; 458d0c4aea6SMilanka Ringwald event[pos++] = sizeof(event) - 2; 459d0c4aea6SMilanka Ringwald event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED; 460d703d377SMatthias Ringwald little_endian_store_16(event, pos, hfp_connection->acl_handle); 461d703d377SMatthias Ringwald pos += 2; 462d0c4aea6SMilanka Ringwald event[pos++] = status; // status 0 == OK 463d703d377SMatthias Ringwald little_endian_store_16(event, pos, sco_handle); 464d0c4aea6SMilanka Ringwald pos += 2; 465d0c4aea6SMilanka Ringwald reverse_bd_addr(addr,&event[pos]); 466d0c4aea6SMilanka Ringwald pos += 6; 467d0c4aea6SMilanka Ringwald event[pos++] = negotiated_codec; 468ca59be51SMatthias Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 469d0c4aea6SMilanka Ringwald } 470d0c4aea6SMilanka Ringwald 471ca59be51SMatthias Ringwald void hfp_emit_string_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, const char * value){ 4727d81706fSMatthias Ringwald btstack_assert(hfp_connection != NULL); 4732c50df93SBjoern Hartmann #ifdef ENABLE_HFP_AT_MESSAGES 4742c50df93SBjoern Hartmann uint8_t event[256]; 4752c50df93SBjoern Hartmann #else 476c1797c7dSMatthias Ringwald uint8_t event[40]; 4772c50df93SBjoern Hartmann #endif 478ab2445a0SMatthias Ringwald uint16_t string_len = btstack_min((uint16_t) strlen(value), sizeof(event) - 6); 479aa4dd815SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 480c10fde09SMatthias Ringwald event[1] = 4 + string_len; 481aa4dd815SMatthias Ringwald event[2] = event_subtype; 482d703d377SMatthias Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 483c10fde09SMatthias Ringwald memcpy((char*)&event[5], value, string_len); 484c10fde09SMatthias Ringwald event[5 + string_len] = 0; 485c10fde09SMatthias Ringwald hfp_emit_event_for_context(hfp_connection, event, 6 + string_len); 486aa4dd815SMatthias Ringwald } 487aa4dd815SMatthias Ringwald 4880cb5b971SMatthias Ringwald btstack_linked_list_t * hfp_get_connections(void){ 4898f2a52f4SMatthias Ringwald return (btstack_linked_list_t *) &hfp_connections; 4903deb3ec6SMatthias Ringwald } 4913deb3ec6SMatthias Ringwald 4923deb3ec6SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid){ 493665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 494665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 495665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 496a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 497a0ffb263SMatthias Ringwald if (hfp_connection->rfcomm_cid == cid){ 498a0ffb263SMatthias Ringwald return hfp_connection; 4993deb3ec6SMatthias Ringwald } 5003deb3ec6SMatthias Ringwald } 5013deb3ec6SMatthias Ringwald return NULL; 5023deb3ec6SMatthias Ringwald } 5033deb3ec6SMatthias Ringwald 504405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr, hfp_role_t hfp_role){ 505665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 506665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 507665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 508a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 5095df9dc78SMatthias Ringwald if ((memcmp(hfp_connection->remote_addr, bd_addr, 6) == 0) && (hfp_connection->local_role == hfp_role)) { 510a0ffb263SMatthias Ringwald return hfp_connection; 5113deb3ec6SMatthias Ringwald } 5123deb3ec6SMatthias Ringwald } 5133deb3ec6SMatthias Ringwald return NULL; 5143deb3ec6SMatthias Ringwald } 5153deb3ec6SMatthias Ringwald 516405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_sco_handle(uint16_t handle, hfp_role_t hfp_role){ 517665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 518665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 519665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 520a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 521c1ab6cc1SMatthias Ringwald if ((hfp_connection->sco_handle == handle) && (hfp_connection->local_role == hfp_role)){ 522a0ffb263SMatthias Ringwald return hfp_connection; 5233deb3ec6SMatthias Ringwald } 5243deb3ec6SMatthias Ringwald } 5253deb3ec6SMatthias Ringwald return NULL; 5263deb3ec6SMatthias Ringwald } 5273deb3ec6SMatthias Ringwald 528405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_acl_handle(uint16_t handle, hfp_role_t hfp_role){ 529d97d752dSMilanka Ringwald btstack_linked_list_iterator_t it; 530d97d752dSMilanka Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 531d97d752dSMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 532d97d752dSMilanka Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 533c1ab6cc1SMatthias Ringwald if ((hfp_connection->acl_handle == handle) && (hfp_connection->local_role == hfp_role)){ 534d97d752dSMilanka Ringwald return hfp_connection; 535d97d752dSMilanka Ringwald } 536d97d752dSMilanka Ringwald } 537d97d752dSMilanka Ringwald return NULL; 538d97d752dSMilanka Ringwald } 539d97d752dSMilanka Ringwald 540c95b5b3cSMilanka Ringwald 541c95b5b3cSMilanka Ringwald static void hfp_reset_voice_recognition(hfp_connection_t * hfp_connection){ 542c95b5b3cSMilanka Ringwald btstack_assert(hfp_connection != NULL); 543c95b5b3cSMilanka Ringwald hfp_voice_recognition_activation_status_t current_vra_state = hfp_connection->vra_state; 544c95b5b3cSMilanka Ringwald hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF; 545c95b5b3cSMilanka Ringwald 546c95b5b3cSMilanka Ringwald if (current_vra_state != HFP_VRA_VOICE_RECOGNITION_OFF){ 547553a4a56SMilanka Ringwald hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS); 548c95b5b3cSMilanka Ringwald } else if (hfp_connection->vra_state_requested != HFP_VRA_VOICE_RECOGNITION_OFF){ 549553a4a56SMilanka Ringwald hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS); 550c95b5b3cSMilanka Ringwald } 551c95b5b3cSMilanka Ringwald 552c95b5b3cSMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_VOICE_RECOGNITION_OFF; 553c95b5b3cSMilanka Ringwald hfp_connection->activate_voice_recognition = false; 554c95b5b3cSMilanka Ringwald hfp_connection->deactivate_voice_recognition = false; 555c95b5b3cSMilanka Ringwald hfp_connection->enhanced_voice_recognition_enabled = false; 556c95b5b3cSMilanka Ringwald hfp_connection->ag_vra_status = 0; 557c95b5b3cSMilanka Ringwald hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY; 558c95b5b3cSMilanka Ringwald } 559c95b5b3cSMilanka Ringwald 560a0ffb263SMatthias Ringwald void hfp_reset_context_flags(hfp_connection_t * hfp_connection){ 561a0ffb263SMatthias Ringwald if (!hfp_connection) return; 562a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 563a0ffb263SMatthias Ringwald hfp_connection->send_error = 0; 5643deb3ec6SMatthias Ringwald 565dd533208SMatthias Ringwald hfp_connection->found_equal_sign = false; 5663deb3ec6SMatthias Ringwald 567a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 0; 568a0ffb263SMatthias Ringwald hfp_connection->operator_name_changed = 0; 5693deb3ec6SMatthias Ringwald 570a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = 0; 571a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 5723deb3ec6SMatthias Ringwald 573a0ffb263SMatthias Ringwald // establish codecs hfp_connection 574a0ffb263SMatthias Ringwald hfp_connection->suggested_codec = 0; 5757522e673SMatthias Ringwald hfp_connection->negotiated_codec = 0; 576a0ffb263SMatthias Ringwald hfp_connection->codec_confirmed = 0; 5773deb3ec6SMatthias Ringwald 578a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 579a0ffb263SMatthias Ringwald hfp_connection->call_waiting_notification_enabled = 0; 580a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 581a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 582125560b8SMatthias Ringwald hfp_connection->clip_have_alpha = false; 583c95b5b3cSMilanka Ringwald hfp_reset_voice_recognition(hfp_connection); 5843deb3ec6SMatthias Ringwald } 5853deb3ec6SMatthias Ringwald 586fffdd288SMatthias Ringwald static hfp_connection_t * create_hfp_connection_context(void){ 587a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = btstack_memory_hfp_connection_get(); 588a0ffb263SMatthias Ringwald if (!hfp_connection) return NULL; 5893deb3ec6SMatthias Ringwald 590a0ffb263SMatthias Ringwald hfp_connection->state = HFP_IDLE; 591a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 592a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_IDLE; 593aa4dd815SMatthias Ringwald 594a0ffb263SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 5953deb3ec6SMatthias Ringwald 596d751f069SMatthias Ringwald hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID; 597d751f069SMatthias Ringwald hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID; 598d751f069SMatthias Ringwald 599a0ffb263SMatthias Ringwald hfp_reset_context_flags(hfp_connection); 6003deb3ec6SMatthias Ringwald 601d63c37a1SMatthias Ringwald btstack_linked_list_add(&hfp_connections, (btstack_linked_item_t*)hfp_connection); 602a0ffb263SMatthias Ringwald return hfp_connection; 6033deb3ec6SMatthias Ringwald } 6043deb3ec6SMatthias Ringwald 60548e6eeeeSMatthias Ringwald void hfp_finalize_connection_context(hfp_connection_t * hfp_connection){ 606a0ffb263SMatthias Ringwald btstack_linked_list_remove(&hfp_connections, (btstack_linked_item_t*) hfp_connection); 60709a233a1SMatthias Ringwald btstack_memory_hfp_connection_free(hfp_connection); 6083deb3ec6SMatthias Ringwald } 6093deb3ec6SMatthias Ringwald 6104eb3f1d8SMilanka Ringwald static hfp_connection_t * hfp_create_connection(bd_addr_t bd_addr, hfp_role_t local_role){ 611405014fbSMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr, local_role); 612a0ffb263SMatthias Ringwald if (hfp_connection) return hfp_connection; 613a0ffb263SMatthias Ringwald hfp_connection = create_hfp_connection_context(); 6146535961aSMatthias Ringwald (void)memcpy(hfp_connection->remote_addr, bd_addr, 6); 615323d3000SMatthias Ringwald hfp_connection->local_role = local_role; 616bdf572f4SMatthias Ringwald log_info("Create HFP context %p: role %u, addr %s", hfp_connection, local_role, bd_addr_to_str(bd_addr)); 617bdf572f4SMatthias Ringwald 618a0ffb263SMatthias Ringwald return hfp_connection; 6193deb3ec6SMatthias Ringwald } 6203deb3ec6SMatthias Ringwald 621aa4dd815SMatthias Ringwald /* @param network. 622aa4dd815SMatthias Ringwald * 0 == no ability to reject a call. 623aa4dd815SMatthias Ringwald * 1 == ability to reject a call. 624aa4dd815SMatthias Ringwald */ 6253deb3ec6SMatthias Ringwald 6263deb3ec6SMatthias Ringwald /* @param suported_features 6273deb3ec6SMatthias Ringwald * HF bit 0: EC and/or NR function (yes/no, 1 = yes, 0 = no) 6283deb3ec6SMatthias Ringwald * HF bit 1: Call waiting or three-way calling(yes/no, 1 = yes, 0 = no) 6293deb3ec6SMatthias Ringwald * HF bit 2: CLI presentation capability (yes/no, 1 = yes, 0 = no) 6303deb3ec6SMatthias Ringwald * HF bit 3: Voice recognition activation (yes/no, 1= yes, 0 = no) 6313deb3ec6SMatthias Ringwald * HF bit 4: Remote volume control (yes/no, 1 = yes, 0 = no) 6323deb3ec6SMatthias Ringwald * HF bit 5: Wide band speech (yes/no, 1 = yes, 0 = no) 6333deb3ec6SMatthias Ringwald */ 6343deb3ec6SMatthias Ringwald /* Bit position: 6353deb3ec6SMatthias Ringwald * AG bit 0: Three-way calling (yes/no, 1 = yes, 0 = no) 6363deb3ec6SMatthias Ringwald * AG bit 1: EC and/or NR function (yes/no, 1 = yes, 0 = no) 6373deb3ec6SMatthias Ringwald * AG bit 2: Voice recognition function (yes/no, 1 = yes, 0 = no) 6383deb3ec6SMatthias Ringwald * AG bit 3: In-band ring tone capability (yes/no, 1 = yes, 0 = no) 6393deb3ec6SMatthias Ringwald * AG bit 4: Attach a phone number to a voice tag (yes/no, 1 = yes, 0 = no) 6403deb3ec6SMatthias Ringwald * AG bit 5: Wide band speech (yes/no, 1 = yes, 0 = no) 6413deb3ec6SMatthias Ringwald */ 6423deb3ec6SMatthias Ringwald 6439b1c3b4dSMatthias 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){ 6443deb3ec6SMatthias Ringwald uint8_t* attribute; 6453deb3ec6SMatthias Ringwald de_create_sequence(service); 6463deb3ec6SMatthias Ringwald 6473deb3ec6SMatthias Ringwald // 0x0000 "Service Record Handle" 648235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE); 6499b1c3b4dSMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle); 6503deb3ec6SMatthias Ringwald 6513deb3ec6SMatthias Ringwald // 0x0001 "Service Class ID List" 652235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST); 6533deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 6543deb3ec6SMatthias Ringwald { 6553deb3ec6SMatthias Ringwald // "UUID for Service" 6563deb3ec6SMatthias Ringwald de_add_number(attribute, DE_UUID, DE_SIZE_16, service_uuid); 657235946f1SMatthias Ringwald de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_GENERIC_AUDIO); 6583deb3ec6SMatthias Ringwald } 6593deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 6603deb3ec6SMatthias Ringwald 6613deb3ec6SMatthias Ringwald // 0x0004 "Protocol Descriptor List" 662235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST); 6633deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 6643deb3ec6SMatthias Ringwald { 6653deb3ec6SMatthias Ringwald uint8_t* l2cpProtocol = de_push_sequence(attribute); 6663deb3ec6SMatthias Ringwald { 667235946f1SMatthias Ringwald de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP); 6683deb3ec6SMatthias Ringwald } 6693deb3ec6SMatthias Ringwald de_pop_sequence(attribute, l2cpProtocol); 6703deb3ec6SMatthias Ringwald 6713deb3ec6SMatthias Ringwald uint8_t* rfcomm = de_push_sequence(attribute); 6723deb3ec6SMatthias Ringwald { 673235946f1SMatthias Ringwald de_add_number(rfcomm, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_RFCOMM); // rfcomm_service 6743deb3ec6SMatthias Ringwald de_add_number(rfcomm, DE_UINT, DE_SIZE_8, rfcomm_channel_nr); // rfcomm channel 6753deb3ec6SMatthias Ringwald } 6763deb3ec6SMatthias Ringwald de_pop_sequence(attribute, rfcomm); 6773deb3ec6SMatthias Ringwald } 6783deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 6793deb3ec6SMatthias Ringwald 6803deb3ec6SMatthias Ringwald 6813deb3ec6SMatthias Ringwald // 0x0005 "Public Browse Group" 682235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group 6833deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 6843deb3ec6SMatthias Ringwald { 685235946f1SMatthias Ringwald de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT); 6863deb3ec6SMatthias Ringwald } 6873deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 6883deb3ec6SMatthias Ringwald 6893deb3ec6SMatthias Ringwald // 0x0009 "Bluetooth Profile Descriptor List" 690235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST); 6913deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 6923deb3ec6SMatthias Ringwald { 6933deb3ec6SMatthias Ringwald uint8_t *sppProfile = de_push_sequence(attribute); 6943deb3ec6SMatthias Ringwald { 695235946f1SMatthias Ringwald de_add_number(sppProfile, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_HANDSFREE); 6963cb2bba2SMilanka Ringwald de_add_number(sppProfile, DE_UINT, DE_SIZE_16, 0x0108); // Verision 1.8 6973deb3ec6SMatthias Ringwald } 6983deb3ec6SMatthias Ringwald de_pop_sequence(attribute, sppProfile); 6993deb3ec6SMatthias Ringwald } 7003deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 7013deb3ec6SMatthias Ringwald 7023deb3ec6SMatthias Ringwald // 0x0100 "Service Name" 7033deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); 704ab2445a0SMatthias Ringwald de_add_data(service, DE_STRING, (uint16_t) strlen(name), (uint8_t *) name); 7053deb3ec6SMatthias Ringwald } 7063deb3ec6SMatthias Ringwald 7077dceaecaSMatthias Ringwald static void hfp_handle_slc_setup_error(hfp_connection_t * hfp_connection, uint8_t status){ 7087dceaecaSMatthias Ringwald // cache fields for event 7097dceaecaSMatthias Ringwald hfp_role_t local_role = hfp_connection->local_role; 7107dceaecaSMatthias Ringwald bd_addr_t remote_addr; 711481c0cbcSMatthias Ringwald (void)memcpy(remote_addr, hfp_connection->remote_addr, 6); 7127dceaecaSMatthias Ringwald // finalize connection struct 7137dceaecaSMatthias Ringwald hfp_finalize_connection_context(hfp_connection); 7147dceaecaSMatthias Ringwald // emit event 7157dceaecaSMatthias Ringwald hfp_emit_slc_connection_event(local_role, status, HCI_CON_HANDLE_INVALID, remote_addr); 7167dceaecaSMatthias Ringwald } 7177dceaecaSMatthias Ringwald 7186c927b22SMatthias Ringwald static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 7198a46ec40SMatthias Ringwald UNUSED(packet_type); // ok: handling own sdp events 7208a46ec40SMatthias Ringwald UNUSED(channel); // ok: no channel 7218a46ec40SMatthias Ringwald UNUSED(size); // ok: handling own sdp events 7221d9c9c90SMilanka Ringwald 723aeb0f0feSMatthias 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); 7241d9c9c90SMilanka Ringwald if (hfp_connection == NULL) { 725aeb0f0feSMatthias Ringwald log_info("connection with %s and local role %d not found", hfp_sdp_query_context.remote_address, hfp_sdp_query_context.local_role); 72684904e95SMilanka Ringwald return; 72784904e95SMilanka Ringwald } 7283deb3ec6SMatthias Ringwald 7290e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 7305611a760SMatthias Ringwald case SDP_EVENT_QUERY_RFCOMM_SERVICE: 731a0ffb263SMatthias Ringwald hfp_connection->rfcomm_channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet); 7323deb3ec6SMatthias Ringwald break; 7335611a760SMatthias Ringwald case SDP_EVENT_QUERY_COMPLETE: 734a0ffb263SMatthias Ringwald if (hfp_connection->rfcomm_channel_nr > 0){ 735a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_CONNECTED; 736520c92d5SMatthias Ringwald btstack_packet_handler_t packet_handler; 737520c92d5SMatthias Ringwald switch (hfp_connection->local_role){ 738520c92d5SMatthias Ringwald case HFP_ROLE_AG: 739520c92d5SMatthias Ringwald packet_handler = hfp_ag_rfcomm_packet_handler; 740520c92d5SMatthias Ringwald break; 741520c92d5SMatthias Ringwald case HFP_ROLE_HF: 742520c92d5SMatthias Ringwald packet_handler = hfp_hf_rfcomm_packet_handler; 743520c92d5SMatthias Ringwald break; 744520c92d5SMatthias Ringwald default: 7451d9c9c90SMilanka Ringwald btstack_assert(false); 746520c92d5SMatthias Ringwald return; 747520c92d5SMatthias Ringwald } 7481d9c9c90SMilanka Ringwald 749ca59be51SMatthias Ringwald rfcomm_create_channel(packet_handler, hfp_connection->remote_addr, hfp_connection->rfcomm_channel_nr, NULL); 7501d9c9c90SMilanka Ringwald 7511d9c9c90SMilanka Ringwald } else { 752cc92f22bSMatthias Ringwald uint8_t status = sdp_event_query_complete_get_status(packet); 753cc92f22bSMatthias Ringwald if (status == ERROR_CODE_SUCCESS){ 754cc92f22bSMatthias Ringwald // report service not found 755cc92f22bSMatthias Ringwald status = SDP_SERVICE_NOT_FOUND; 756cc92f22bSMatthias Ringwald } 7577dceaecaSMatthias Ringwald hfp_handle_slc_setup_error(hfp_connection, status); 758cc92f22bSMatthias Ringwald log_info("rfcomm service not found, status 0x%02x", status); 7591d9c9c90SMilanka Ringwald } 7601d9c9c90SMilanka Ringwald 7611d9c9c90SMilanka Ringwald // register the SDP Query request to check if there is another connection waiting for the query 7621d9c9c90SMilanka Ringwald // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback 763aeb0f0feSMatthias Ringwald (void) sdp_client_register_query_callback(&hfp_sdp_query_request); 7643deb3ec6SMatthias Ringwald break; 7653deb3ec6SMatthias Ringwald default: 7663deb3ec6SMatthias Ringwald break; 7673deb3ec6SMatthias Ringwald } 7683deb3ec6SMatthias Ringwald } 7693deb3ec6SMatthias Ringwald 77038200c1dSMilanka Ringwald // returns 0 if unexpected error or no other link options remained, otherwise 1 77138200c1dSMilanka Ringwald static int hfp_handle_failed_sco_connection(uint8_t status){ 772eddcd308SMatthias Ringwald 773aeb0f0feSMatthias Ringwald if (!hfp_sco_establishment_active){ 7743427dd75SMatthias Ringwald log_info("(e)SCO Connection failed but not started by us"); 77538200c1dSMilanka Ringwald return 0; 776eddcd308SMatthias Ringwald } 777eddcd308SMatthias Ringwald 7783427dd75SMatthias Ringwald log_info("(e)SCO Connection failed 0x%02x", status); 7793427dd75SMatthias Ringwald switch (status){ 7801579b82fSMatthias Ringwald case ERROR_CODE_SCO_AIR_MODE_REJECTED: 7811579b82fSMatthias Ringwald case ERROR_CODE_SCO_INTERVAL_REJECTED: 7821579b82fSMatthias Ringwald case ERROR_CODE_SCO_OFFSET_REJECTED: 7833427dd75SMatthias Ringwald case ERROR_CODE_UNSPECIFIED_ERROR: 7841579b82fSMatthias Ringwald case ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE: 78569a0db57SMatthias Ringwald case ERROR_CODE_UNSUPPORTED_REMOTE_FEATURE_UNSUPPORTED_LMP_FEATURE: 786eddcd308SMatthias Ringwald break; 7873427dd75SMatthias Ringwald default: 7883427dd75SMatthias Ringwald return 0; 7893427dd75SMatthias Ringwald } 7903427dd75SMatthias Ringwald 7913427dd75SMatthias Ringwald // note: eSCO_S4 supported flag not available, but it's only relevant for highest CVSD link setting (and the current failed) 792aeb0f0feSMatthias Ringwald hfp_link_settings_t next_setting = hfp_next_link_setting_for_connection(hfp_sco_establishment_active->link_setting, hfp_sco_establishment_active, false); 7933427dd75SMatthias Ringwald 7943427dd75SMatthias Ringwald // handle no valid setting found 7953427dd75SMatthias Ringwald if (next_setting == HFP_LINK_SETTINGS_NONE) { 796aeb0f0feSMatthias Ringwald if (hfp_sco_establishment_active->negotiated_codec == HFP_CODEC_MSBC){ 7973427dd75SMatthias Ringwald log_info("T2/T1 failed, fallback to CVSD - D1"); 798aeb0f0feSMatthias Ringwald hfp_sco_establishment_active->negotiated_codec = HFP_CODEC_CVSD; 799aeb0f0feSMatthias Ringwald hfp_sco_establishment_active->sco_for_msbc_failed = 1; 800a814da6aSMatthias Ringwald hfp_sco_establishment_active->ag_send_common_codec = true; 801aeb0f0feSMatthias Ringwald hfp_sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D1; 8023427dd75SMatthias Ringwald } else { 8033427dd75SMatthias Ringwald // no other options 8043427dd75SMatthias Ringwald return 0; 805eddcd308SMatthias Ringwald } 8063427dd75SMatthias Ringwald } 8073427dd75SMatthias Ringwald 8083427dd75SMatthias Ringwald log_info("e)SCO Connection: try new link_setting %d", next_setting); 809aeb0f0feSMatthias Ringwald hfp_sco_establishment_active->establish_audio_connection = 1; 810aeb0f0feSMatthias Ringwald hfp_sco_establishment_active->link_setting = next_setting; 811aeb0f0feSMatthias Ringwald hfp_sco_establishment_active = NULL; 81238200c1dSMilanka Ringwald return 1; 813eddcd308SMatthias Ringwald } 814eddcd308SMatthias Ringwald 815405014fbSMatthias Ringwald void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){ 8165575558eSMilanka Ringwald UNUSED(packet_type); 8178a46ec40SMatthias Ringwald UNUSED(channel); // ok: no channel 8185575558eSMilanka Ringwald UNUSED(size); 8199ec2630cSMatthias Ringwald 8203deb3ec6SMatthias Ringwald bd_addr_t event_addr; 82127950165SMatthias Ringwald hci_con_handle_t handle; 822a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = NULL; 823674515e8SMatthias Ringwald uint8_t status; 8243deb3ec6SMatthias Ringwald 82527950165SMatthias Ringwald log_debug("HFP HCI event handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size); 826aa4dd815SMatthias Ringwald 8270e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 8281b005648SMatthias Ringwald 829011577abSMilanka Ringwald case HCI_EVENT_CONNECTION_REQUEST: 8307522e673SMatthias Ringwald switch(hci_event_connection_request_get_link_type(packet)){ 8317522e673SMatthias Ringwald case 0: // SCO 8327522e673SMatthias Ringwald case 2: // eSCO 833011577abSMilanka Ringwald hci_event_connection_request_get_bd_addr(packet, event_addr); 834405014fbSMatthias Ringwald hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role); 835011577abSMilanka Ringwald if (!hfp_connection) break; 836c169df2fSMatthias Ringwald if (hci_event_connection_request_get_link_type(packet) == 2){ 837cb81d35dSMatthias Ringwald hfp_connection->accept_sco = 2; 838c169df2fSMatthias Ringwald } else { 839cb81d35dSMatthias Ringwald hfp_connection->accept_sco = 1; 840c169df2fSMatthias Ringwald } 8413721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP 8423721a235SMatthias Ringwald hfp_cc256x_prepare_for_sco(hfp_connection); 8433721a235SMatthias Ringwald #endif 844689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 845689d4323SMatthias Ringwald hfp_bcm_prepare_for_sco(hfp_connection); 846689d4323SMatthias Ringwald #endif 8472b5f92fdSMatthias Ringwald #ifdef ENABLE_RTK_PCM_WBS 8482b5f92fdSMatthias Ringwald hfp_connection->rtk_send_sco_config = true; 8492b5f92fdSMatthias Ringwald #endif 850cb81d35dSMatthias Ringwald log_info("accept sco %u\n", hfp_connection->accept_sco); 851aeb0f0feSMatthias Ringwald hfp_sco_establishment_active = hfp_connection; 852202c8a4cSMatthias Ringwald break; 8537522e673SMatthias Ringwald default: 8547522e673SMatthias Ringwald break; 8557522e673SMatthias Ringwald } 856011577abSMilanka Ringwald break; 8573deb3ec6SMatthias Ringwald 858eddcd308SMatthias Ringwald case HCI_EVENT_COMMAND_STATUS: 859eddcd308SMatthias Ringwald if (hci_event_command_status_get_command_opcode(packet) == hci_setup_synchronous_connection.opcode) { 860aeb0f0feSMatthias Ringwald if (hfp_sco_establishment_active == NULL) break; 861be2a1a78SMatthias Ringwald status = hci_event_command_status_get_status(packet); 86238200c1dSMilanka Ringwald if (status == ERROR_CODE_SUCCESS) break; 86338200c1dSMilanka Ringwald 864aeb0f0feSMatthias Ringwald hfp_connection = hfp_sco_establishment_active; 86538200c1dSMilanka Ringwald if (hfp_handle_failed_sco_connection(status)) break; 86638200c1dSMilanka Ringwald hfp_connection->establish_audio_connection = 0; 86738200c1dSMilanka Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 86838200c1dSMilanka Ringwald hfp_emit_sco_event(hfp_connection, status, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec); 8696c5b2002SMatthias Ringwald } 870eddcd308SMatthias Ringwald break; 871eddcd308SMatthias Ringwald 8723deb3ec6SMatthias Ringwald case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{ 873aeb0f0feSMatthias Ringwald if (hfp_sco_establishment_active == NULL) break; 874011577abSMilanka Ringwald hci_event_synchronous_connection_complete_get_bd_addr(packet, event_addr); 875405014fbSMatthias Ringwald hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role); 876011577abSMilanka Ringwald if (!hfp_connection) { 877011577abSMilanka Ringwald log_error("HFP: connection does not exist for remote with addr %s.", bd_addr_to_str(event_addr)); 878011577abSMilanka Ringwald return; 879011577abSMilanka Ringwald } 880ce263fc8SMatthias Ringwald 881011577abSMilanka Ringwald status = hci_event_synchronous_connection_complete_get_status(packet); 88238200c1dSMilanka Ringwald if (status != ERROR_CODE_SUCCESS){ 883cb81d35dSMatthias Ringwald hfp_connection->accept_sco = 0; 88438200c1dSMilanka Ringwald if (hfp_handle_failed_sco_connection(status)) break; 88538200c1dSMilanka Ringwald 88638200c1dSMilanka Ringwald hfp_connection->establish_audio_connection = 0; 88738200c1dSMilanka Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 88838200c1dSMilanka Ringwald hfp_emit_sco_event(hfp_connection, status, 0, event_addr, hfp_connection->negotiated_codec); 889aa4dd815SMatthias Ringwald break; 890aa4dd815SMatthias Ringwald } 891aa4dd815SMatthias Ringwald 892011577abSMilanka Ringwald uint16_t sco_handle = hci_event_synchronous_connection_complete_get_handle(packet); 893011577abSMilanka Ringwald uint8_t link_type = hci_event_synchronous_connection_complete_get_link_type(packet); 894011577abSMilanka Ringwald uint8_t transmission_interval = hci_event_synchronous_connection_complete_get_transmission_interval(packet); // measured in slots 895011577abSMilanka Ringwald uint8_t retransmission_interval = hci_event_synchronous_connection_complete_get_retransmission_interval(packet);// measured in slots 896011577abSMilanka Ringwald uint16_t rx_packet_length = hci_event_synchronous_connection_complete_get_rx_packet_length(packet); // measured in bytes 897011577abSMilanka Ringwald uint16_t tx_packet_length = hci_event_synchronous_connection_complete_get_tx_packet_length(packet); // measured in bytes 8983deb3ec6SMatthias Ringwald 8993deb3ec6SMatthias Ringwald switch (link_type){ 9003deb3ec6SMatthias Ringwald case 0x00: 901aa4dd815SMatthias Ringwald log_info("SCO Connection established."); 9023deb3ec6SMatthias Ringwald if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval); 9033deb3ec6SMatthias Ringwald if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval); 9043deb3ec6SMatthias Ringwald if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length); 9053deb3ec6SMatthias Ringwald if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length); 9063deb3ec6SMatthias Ringwald break; 9073deb3ec6SMatthias Ringwald case 0x02: 908aa4dd815SMatthias Ringwald log_info("eSCO Connection established. \n"); 9093deb3ec6SMatthias Ringwald break; 9103deb3ec6SMatthias Ringwald default: 9113deb3ec6SMatthias Ringwald log_error("(e)SCO reserved link_type 0x%2x", link_type); 9123deb3ec6SMatthias Ringwald break; 9133deb3ec6SMatthias Ringwald } 914e0d13a19SMilanka Ringwald 9153deb3ec6SMatthias Ringwald log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, " 916aa4dd815SMatthias Ringwald " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)\n", sco_handle, 917e0d13a19SMilanka Ringwald bd_addr_to_str(event_addr), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length, 918e0d13a19SMilanka Ringwald hci_event_synchronous_connection_complete_get_air_mode(packet)); 9193deb3ec6SMatthias Ringwald 920d63c37a1SMatthias Ringwald if (hfp_connection->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){ 921aa4dd815SMatthias Ringwald log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN"); 922d63c37a1SMatthias Ringwald hfp_connection->state = HFP_W2_DISCONNECT_SCO; 923aa4dd815SMatthias Ringwald break; 924aa4dd815SMatthias Ringwald } 925d63c37a1SMatthias Ringwald hfp_connection->sco_handle = sco_handle; 926d63c37a1SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 927c95b5b3cSMilanka Ringwald 928d63c37a1SMatthias Ringwald hfp_connection->state = HFP_AUDIO_CONNECTION_ESTABLISHED; 9291a26de69SMilanka Ringwald 9300b4debbfSMilanka Ringwald switch (hfp_connection->vra_state){ 9310b4debbfSMilanka Ringwald case HFP_VRA_VOICE_RECOGNITION_ACTIVATED: 9321a26de69SMilanka Ringwald hfp_connection->ag_audio_connection_opened_before_vra = false; 9330b4debbfSMilanka Ringwald break; 9340b4debbfSMilanka Ringwald default: 9351a26de69SMilanka Ringwald hfp_connection->ag_audio_connection_opened_before_vra = true; 9360b4debbfSMilanka Ringwald break; 9370b4debbfSMilanka Ringwald } 93838200c1dSMilanka Ringwald hfp_emit_sco_event(hfp_connection, status, sco_handle, event_addr, hfp_connection->negotiated_codec); 9393deb3ec6SMatthias Ringwald break; 9403deb3ec6SMatthias Ringwald } 9413deb3ec6SMatthias Ringwald 9423deb3ec6SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 943f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet,3); 944405014fbSMatthias Ringwald hfp_connection = get_hfp_connection_context_for_sco_handle(handle, local_role); 945aa4dd815SMatthias Ringwald 946d63c37a1SMatthias Ringwald if (!hfp_connection) break; 947aa4dd815SMatthias Ringwald 9483721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP 9493721a235SMatthias Ringwald hfp_connection->cc256x_send_wbs_disassociate = true; 9503721a235SMatthias Ringwald #endif 951689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 952689d4323SMatthias Ringwald hfp_connection->bcm_send_disable_wbs = true; 953689d4323SMatthias Ringwald #endif 95452cbdd6dSMilanka Ringwald if (hfp_connection->sco_handle == handle){ 955d751f069SMatthias Ringwald hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID; 956d63c37a1SMatthias Ringwald hfp_connection->release_audio_connection = 0; 95752cbdd6dSMilanka Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 95852cbdd6dSMilanka Ringwald hfp_emit_audio_connection_released(hfp_connection, handle); 95952cbdd6dSMilanka Ringwald 9601a26de69SMilanka Ringwald hfp_connection->ag_audio_connection_opened_before_vra = false; 96152cbdd6dSMilanka Ringwald 96252cbdd6dSMilanka Ringwald if (hfp_connection->acl_handle == HCI_CON_HANDLE_INVALID){ 96311b091bfSMilanka Ringwald hfp_reset_voice_recognition(hfp_connection); 96452cbdd6dSMilanka Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0); 96552cbdd6dSMilanka Ringwald hfp_finalize_connection_context(hfp_connection); 96619976d6bSMilanka Ringwald break; 96752cbdd6dSMilanka Ringwald } else if (hfp_connection->release_slc_connection == 1){ 96852cbdd6dSMilanka Ringwald hfp_connection->release_slc_connection = 0; 96952cbdd6dSMilanka Ringwald hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM; 97052cbdd6dSMilanka Ringwald rfcomm_disconnect(hfp_connection->acl_handle); 97152cbdd6dSMilanka Ringwald } 97252cbdd6dSMilanka Ringwald } 97348e6eeeeSMatthias Ringwald 97448e6eeeeSMatthias Ringwald if (hfp_connection->state == HFP_W4_SCO_DISCONNECTED_TO_SHUTDOWN){ 97548e6eeeeSMatthias Ringwald // RFCOMM already closed -> remote power off 97648e6eeeeSMatthias Ringwald #if defined(ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS) 97748e6eeeeSMatthias Ringwald hfp_connection->state = HFP_W4_WBS_SHUTDOWN; 97848e6eeeeSMatthias Ringwald #else 97948e6eeeeSMatthias Ringwald hfp_finalize_connection_context(hfp_connection); 98048e6eeeeSMatthias Ringwald #endif 98148e6eeeeSMatthias Ringwald break; 98248e6eeeeSMatthias Ringwald } 9833deb3ec6SMatthias Ringwald break; 9843deb3ec6SMatthias Ringwald 985fc64f94aSMatthias Ringwald default: 9863deb3ec6SMatthias Ringwald break; 9873deb3ec6SMatthias Ringwald } 9883deb3ec6SMatthias Ringwald } 9893deb3ec6SMatthias Ringwald 9903721a235SMatthias Ringwald 99127950165SMatthias Ringwald void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){ 9925575558eSMilanka Ringwald UNUSED(packet_type); 99327950165SMatthias Ringwald UNUSED(channel); // ok: no channel 9945575558eSMilanka Ringwald UNUSED(size); 99527950165SMatthias Ringwald 99627950165SMatthias Ringwald bd_addr_t event_addr; 99727950165SMatthias Ringwald uint16_t rfcomm_cid; 99827950165SMatthias Ringwald hfp_connection_t * hfp_connection = NULL; 99927950165SMatthias Ringwald uint8_t status; 100027950165SMatthias Ringwald 100127950165SMatthias Ringwald log_debug("HFP packet_handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size); 100227950165SMatthias Ringwald 100327950165SMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 100427950165SMatthias Ringwald 100527950165SMatthias Ringwald case RFCOMM_EVENT_INCOMING_CONNECTION: 100627950165SMatthias Ringwald // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 100727950165SMatthias Ringwald rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr); 10084eb3f1d8SMilanka Ringwald hfp_connection = hfp_create_connection(event_addr, local_role); 100927950165SMatthias Ringwald if (!hfp_connection){ 101027950165SMatthias Ringwald log_info("hfp: no memory to accept incoming connection - decline"); 101127950165SMatthias Ringwald rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet)); 101227950165SMatthias Ringwald return; 101327950165SMatthias Ringwald } 101427950165SMatthias Ringwald if (hfp_connection->state != HFP_IDLE) { 10154960f9e7SMatthias Ringwald log_error("hfp: incoming connection but not idle, reject"); 10164960f9e7SMatthias Ringwald rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet)); 101727950165SMatthias Ringwald return; 101827950165SMatthias Ringwald } 101927950165SMatthias Ringwald 102027950165SMatthias Ringwald hfp_connection->rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet); 102127950165SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_CONNECTED; 102227950165SMatthias Ringwald rfcomm_accept_connection(hfp_connection->rfcomm_cid); 102327950165SMatthias Ringwald break; 102427950165SMatthias Ringwald 102527950165SMatthias Ringwald case RFCOMM_EVENT_CHANNEL_OPENED: 102627950165SMatthias Ringwald rfcomm_event_channel_opened_get_bd_addr(packet, event_addr); 102727950165SMatthias Ringwald 1028405014fbSMatthias Ringwald hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role); 10296504a03aSMilanka Ringwald btstack_assert(hfp_connection != NULL); 103027950165SMatthias Ringwald 10316504a03aSMilanka Ringwald if (hfp_connection->state != HFP_W4_RFCOMM_CONNECTED){ 10326504a03aSMilanka Ringwald break; 10336504a03aSMilanka Ringwald } 10346504a03aSMilanka Ringwald 10356504a03aSMilanka Ringwald status = rfcomm_event_channel_opened_get_status(packet); 10366504a03aSMilanka Ringwald if (status != ERROR_CODE_SUCCESS) { 10377dceaecaSMatthias Ringwald hfp_handle_slc_setup_error(hfp_connection, status); 10386504a03aSMilanka Ringwald break; 10396504a03aSMilanka Ringwald } 10406504a03aSMilanka Ringwald 104127950165SMatthias Ringwald hfp_connection->acl_handle = rfcomm_event_channel_opened_get_con_handle(packet); 104227950165SMatthias Ringwald hfp_connection->rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet); 10436504a03aSMilanka Ringwald hfp_connection->rfcomm_mtu = rfcomm_event_channel_opened_get_max_frame_size(packet); 104427950165SMatthias Ringwald bd_addr_copy(hfp_connection->remote_addr, event_addr); 104527950165SMatthias Ringwald hfp_connection->state = HFP_EXCHANGE_SUPPORTED_FEATURES; 10466504a03aSMilanka Ringwald 104727950165SMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 104827950165SMatthias Ringwald break; 104927950165SMatthias Ringwald 105027950165SMatthias Ringwald case RFCOMM_EVENT_CHANNEL_CLOSED: 105127950165SMatthias Ringwald rfcomm_cid = little_endian_read_16(packet,2); 105227950165SMatthias Ringwald hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid); 105327950165SMatthias Ringwald if (!hfp_connection) break; 105452cbdd6dSMilanka Ringwald switch (hfp_connection->state){ 105552cbdd6dSMilanka Ringwald case HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART: 105652cbdd6dSMilanka Ringwald hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID; 105727950165SMatthias Ringwald hfp_connection->state = HFP_IDLE; 105827950165SMatthias Ringwald hfp_establish_service_level_connection(hfp_connection->remote_addr, hfp_connection->service_uuid, local_role); 105927950165SMatthias Ringwald break; 106052cbdd6dSMilanka Ringwald 106152cbdd6dSMilanka Ringwald case HFP_AUDIO_CONNECTION_ESTABLISHED: 106248e6eeeeSMatthias Ringwald // service connection was released, this implicitly releases audio connection as well 106348e6eeeeSMatthias Ringwald hfp_connection->release_audio_connection = 0; 106452cbdd6dSMilanka Ringwald hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID; 106548e6eeeeSMatthias Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED_TO_SHUTDOWN; 106652cbdd6dSMilanka Ringwald gap_disconnect(hfp_connection->sco_handle); 106752cbdd6dSMilanka Ringwald break; 106852cbdd6dSMilanka Ringwald 106952cbdd6dSMilanka Ringwald default: 107048e6eeeeSMatthias Ringwald // regular case 107111b091bfSMilanka Ringwald hfp_reset_voice_recognition(hfp_connection); 107248e6eeeeSMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0); 107348e6eeeeSMatthias Ringwald hfp_finalize_connection_context(hfp_connection); 107452cbdd6dSMilanka Ringwald break; 107548e6eeeeSMatthias Ringwald } 107627950165SMatthias Ringwald break; 107727950165SMatthias Ringwald 107827950165SMatthias Ringwald default: 107927950165SMatthias Ringwald break; 108027950165SMatthias Ringwald } 108127950165SMatthias Ringwald } 1082aa4dd815SMatthias Ringwald // translates command string into hfp_command_t CMD 108394a27792SMatthias Ringwald 108494a27792SMatthias Ringwald typedef struct { 108594a27792SMatthias Ringwald const char * command; 108694a27792SMatthias Ringwald hfp_command_t command_id; 108794a27792SMatthias Ringwald } hfp_command_entry_t; 108894a27792SMatthias Ringwald 108994a27792SMatthias Ringwald static hfp_command_entry_t hfp_ag_commmand_table[] = { 1090cb33905eSMatthias Ringwald { "AT+BAC=", HFP_CMD_AVAILABLE_CODECS }, 1091cb33905eSMatthias Ringwald { "AT+BCC", HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP }, 1092cb33905eSMatthias Ringwald { "AT+BCS=", HFP_CMD_HF_CONFIRMED_CODEC }, 1093cb33905eSMatthias Ringwald { "AT+BIA=", HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE, }, // +BIA:<enabled>,,<enabled>,,,<enabled> 1094cb33905eSMatthias Ringwald { "AT+BIEV=", HFP_CMD_HF_INDICATOR_STATUS }, 109594a27792SMatthias Ringwald { "AT+BIND=", HFP_CMD_LIST_GENERIC_STATUS_INDICATORS }, 109694a27792SMatthias Ringwald { "AT+BIND=?", HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS }, 109794a27792SMatthias Ringwald { "AT+BIND?", HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE }, 1098051f797dSMilanka Ringwald { "AT+BINP=", HFP_CMD_HF_REQUEST_PHONE_NUMBER }, 1099cb33905eSMatthias Ringwald { "AT+BLDN", HFP_CMD_REDIAL_LAST_NUMBER }, 1100cb33905eSMatthias Ringwald { "AT+BRSF=", HFP_CMD_SUPPORTED_FEATURES }, 110194a27792SMatthias Ringwald { "AT+BTRH=", HFP_CMD_RESPONSE_AND_HOLD_COMMAND }, 110294a27792SMatthias Ringwald { "AT+BTRH?", HFP_CMD_RESPONSE_AND_HOLD_QUERY }, 11039ed286f3SMilanka Ringwald { "AT+BVRA=", HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION }, 1104cb33905eSMatthias Ringwald { "AT+CCWA=", HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION}, 110594a27792SMatthias Ringwald { "AT+CHLD=", HFP_CMD_CALL_HOLD }, 110694a27792SMatthias Ringwald { "AT+CHLD=?", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES }, 1107cb33905eSMatthias Ringwald { "AT+CHUP", HFP_CMD_HANG_UP_CALL }, 110894a27792SMatthias Ringwald { "AT+CIND=?", HFP_CMD_RETRIEVE_AG_INDICATORS }, 110994a27792SMatthias Ringwald { "AT+CIND?", HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS }, 1110cb33905eSMatthias Ringwald { "AT+CLCC", HFP_CMD_LIST_CURRENT_CALLS }, 1111cb33905eSMatthias Ringwald { "AT+CLIP=", HFP_CMD_ENABLE_CLIP}, 1112cb33905eSMatthias Ringwald { "AT+CMEE=", HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR}, 1113cb33905eSMatthias Ringwald { "AT+CMER=", HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE }, 1114cb33905eSMatthias Ringwald { "AT+CNUM", HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION }, 111594a27792SMatthias Ringwald { "AT+COPS=", HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT }, 111694a27792SMatthias Ringwald { "AT+COPS?", HFP_CMD_QUERY_OPERATOR_SELECTION_NAME }, 1117cb33905eSMatthias Ringwald { "AT+NREC=", HFP_CMD_TURN_OFF_EC_AND_NR, }, 1118cb33905eSMatthias Ringwald { "AT+VGM=", HFP_CMD_SET_MICROPHONE_GAIN }, 1119cb33905eSMatthias Ringwald { "AT+VGS=", HFP_CMD_SET_SPEAKER_GAIN }, 1120d8656ee6SMilanka Ringwald { "AT+VTS=", HFP_CMD_TRANSMIT_DTMF_CODES }, 112194a27792SMatthias Ringwald { "ATA", HFP_CMD_CALL_ANSWERED }, 112294a27792SMatthias Ringwald }; 112394a27792SMatthias Ringwald 112494a27792SMatthias Ringwald static hfp_command_entry_t hfp_hf_commmand_table[] = { 1125cb33905eSMatthias Ringwald { "+BCS:", HFP_CMD_AG_SUGGESTED_CODEC }, 112694a27792SMatthias Ringwald { "+BIND:", HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS }, 1127b3615b9aSMilanka Ringwald { "+BINP:", HFP_CMD_AG_SENT_PHONE_NUMBER }, 1128cb33905eSMatthias Ringwald { "+BRSF:", HFP_CMD_SUPPORTED_FEATURES }, 1129cb33905eSMatthias Ringwald { "+BSIR:", HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING }, 113094a27792SMatthias Ringwald { "+BTRH:", HFP_CMD_RESPONSE_AND_HOLD_STATUS }, 1131cb33905eSMatthias Ringwald { "+BVRA:", HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION }, 1132cb33905eSMatthias Ringwald { "+CCWA:", HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE, }, 113394a27792SMatthias Ringwald { "+CHLD:", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES }, 1134cb33905eSMatthias Ringwald { "+CIEV:", HFP_CMD_TRANSFER_AG_INDICATOR_STATUS}, 113584a0c24eSMatthias Ringwald { "+CIND:", HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC }, 1136cb33905eSMatthias Ringwald { "+CLCC:", HFP_CMD_LIST_CURRENT_CALLS }, 1137cb33905eSMatthias Ringwald { "+CLIP:", HFP_CMD_AG_SENT_CLIP_INFORMATION }, 1138cb33905eSMatthias Ringwald { "+CME ERROR:", HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR }, 1139cb33905eSMatthias Ringwald { "+CNUM:", HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION}, 1140cb33905eSMatthias Ringwald { "+COPS:", HFP_CMD_QUERY_OPERATOR_SELECTION_NAME }, 1141cb33905eSMatthias Ringwald { "+VGM:", HFP_CMD_SET_MICROPHONE_GAIN }, 1142ce284ffbSMatthias Ringwald { "+VGM=", HFP_CMD_SET_MICROPHONE_GAIN }, 11434c30d2d1SMatthias Ringwald { "+VGS:", HFP_CMD_SET_SPEAKER_GAIN}, 1144ce284ffbSMatthias Ringwald { "+VGS=", HFP_CMD_SET_SPEAKER_GAIN}, 1145cb33905eSMatthias Ringwald { "ERROR", HFP_CMD_ERROR}, 1146cb33905eSMatthias Ringwald { "NOP", HFP_CMD_NONE}, // dummy command used by unit tests 1147cb33905eSMatthias Ringwald { "OK", HFP_CMD_OK }, 1148cb33905eSMatthias Ringwald { "RING", HFP_CMD_RING }, 114994a27792SMatthias Ringwald }; 115094a27792SMatthias Ringwald 1151471dea41SMatthias Ringwald static const hfp_custom_at_command_t * hfp_custom_command_lookup(const char * text){ 1152471dea41SMatthias Ringwald btstack_linked_list_iterator_t it; 1153471dea41SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hfp_custom_commands_ag); 1154471dea41SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)) { 1155471dea41SMatthias Ringwald hfp_custom_at_command_t *at_command = (hfp_custom_at_command_t *) btstack_linked_list_iterator_next(&it); 1156471dea41SMatthias Ringwald int match = strcmp(text, at_command->command); 1157471dea41SMatthias Ringwald if (match == 0) { 1158471dea41SMatthias Ringwald return at_command; 1159471dea41SMatthias Ringwald } 1160471dea41SMatthias Ringwald } 1161471dea41SMatthias Ringwald return NULL; 1162471dea41SMatthias Ringwald } 1163471dea41SMatthias Ringwald 1164aa4dd815SMatthias Ringwald static hfp_command_t parse_command(const char * line_buffer, int isHandsFree){ 11653deb3ec6SMatthias Ringwald 1166*ff158b9bSMatthias Ringwald // check for custom commands, AG only 1167*ff158b9bSMatthias Ringwald if (isHandsFree == 0) { 1168*ff158b9bSMatthias Ringwald const hfp_custom_at_command_t * custom_at_command = hfp_custom_command_lookup(line_buffer); 1169*ff158b9bSMatthias Ringwald if (custom_at_command != NULL){ 1170*ff158b9bSMatthias Ringwald return HFP_CMD_CUSTOM_MESSAGE; 1171*ff158b9bSMatthias Ringwald } 1172*ff158b9bSMatthias Ringwald } 1173*ff158b9bSMatthias Ringwald 1174cb33905eSMatthias Ringwald // table lookup based on role 117594a27792SMatthias Ringwald uint16_t num_entries; 117694a27792SMatthias Ringwald hfp_command_entry_t * table; 117794a27792SMatthias Ringwald if (isHandsFree == 0){ 117894a27792SMatthias Ringwald table = hfp_ag_commmand_table; 117994a27792SMatthias Ringwald num_entries = sizeof(hfp_ag_commmand_table) / sizeof(hfp_command_entry_t); 11803deb3ec6SMatthias Ringwald } else { 118194a27792SMatthias Ringwald table = hfp_hf_commmand_table; 118294a27792SMatthias Ringwald num_entries = sizeof(hfp_hf_commmand_table) / sizeof(hfp_command_entry_t); 118394a27792SMatthias Ringwald } 1184791f0d0aSMatthias Ringwald // binary search 1185791f0d0aSMatthias Ringwald uint16_t left = 0; 1186791f0d0aSMatthias Ringwald uint16_t right = num_entries - 1; 1187791f0d0aSMatthias Ringwald while (left <= right){ 1188791f0d0aSMatthias Ringwald uint16_t middle = left + (right - left) / 2; 1189791f0d0aSMatthias Ringwald hfp_command_entry_t *entry = &table[middle]; 119094a27792SMatthias Ringwald int match = strcmp(line_buffer, entry->command); 1191791f0d0aSMatthias Ringwald if (match < 0){ 1192791f0d0aSMatthias Ringwald // search term is lower than middle element 119347005182SMilanka Ringwald if (right == 0) break; 1194791f0d0aSMatthias Ringwald right = middle - 1; 1195791f0d0aSMatthias Ringwald } else if (match == 0){ 119694a27792SMatthias Ringwald return entry->command_id; 1197791f0d0aSMatthias Ringwald } else { 1198791f0d0aSMatthias Ringwald // search term is higher than middle element 1199791f0d0aSMatthias Ringwald left = middle + 1; 120094a27792SMatthias Ringwald } 120194a27792SMatthias Ringwald } 120294a27792SMatthias Ringwald 1203cb33905eSMatthias Ringwald // note: if parser in CMD_HEADER state would treats digits and maybe '+' as separator, match on "ATD" would work. 12040222a807SMatthias Ringwald // note: phone number is currently expected in line_buffer[3..] 1205cb33905eSMatthias Ringwald // prefix match on 'ATD', AG only 1206cb33905eSMatthias Ringwald if ((isHandsFree == 0) && (strncmp(line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0)){ 1207cb33905eSMatthias Ringwald return HFP_CMD_CALL_PHONE_NUMBER; 12083deb3ec6SMatthias Ringwald } 12093deb3ec6SMatthias Ringwald 1210cb33905eSMatthias Ringwald // Valid looking, but unknown commands/responses 1211cb33905eSMatthias Ringwald if ((isHandsFree == 0) && (strncmp(line_buffer, "AT+", 3) == 0)){ 1212aa4dd815SMatthias Ringwald return HFP_CMD_UNKNOWN; 12133deb3ec6SMatthias Ringwald } 12143deb3ec6SMatthias Ringwald 1215cb33905eSMatthias Ringwald if ((isHandsFree != 0) && (strncmp(line_buffer, "+", 1) == 0)){ 1216aa4dd815SMatthias Ringwald return HFP_CMD_UNKNOWN; 1217aa4dd815SMatthias Ringwald } 12183deb3ec6SMatthias Ringwald 1219aa4dd815SMatthias Ringwald return HFP_CMD_NONE; 12203deb3ec6SMatthias Ringwald } 12213deb3ec6SMatthias Ringwald 1222a0ffb263SMatthias Ringwald static void hfp_parser_store_byte(hfp_connection_t * hfp_connection, uint8_t byte){ 122351aa5d5aSMilanka Ringwald if ((hfp_connection->line_size + 1) >= HFP_MAX_VR_TEXT_SIZE) return; 1224a0ffb263SMatthias Ringwald hfp_connection->line_buffer[hfp_connection->line_size++] = byte; 1225a0ffb263SMatthias Ringwald hfp_connection->line_buffer[hfp_connection->line_size] = 0; 12263deb3ec6SMatthias Ringwald } 1227a0ffb263SMatthias Ringwald static int hfp_parser_is_buffer_empty(hfp_connection_t * hfp_connection){ 1228a0ffb263SMatthias Ringwald return hfp_connection->line_size == 0; 12293deb3ec6SMatthias Ringwald } 12303deb3ec6SMatthias Ringwald 12313deb3ec6SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){ 1232c1ab6cc1SMatthias Ringwald return (byte == '\n') || (byte == '\r'); 12333deb3ec6SMatthias Ringwald } 12343deb3ec6SMatthias Ringwald 1235471dea41SMatthias Ringwald void hfp_parser_reset_line_buffer(hfp_connection_t *hfp_connection) { 1236a0ffb263SMatthias Ringwald hfp_connection->line_size = 0; 1237125560b8SMatthias Ringwald // we don't set the first byte to '\0' to allow access to last argument e.g. in hfp_hf_handle_rfcommand 12383deb3ec6SMatthias Ringwald } 12391d81c7feSMatthias Ringwald 12401d81c7feSMatthias Ringwald static void hfp_parser_store_if_token(hfp_connection_t * hfp_connection, uint8_t byte){ 12411d81c7feSMatthias Ringwald switch (byte){ 12421d81c7feSMatthias Ringwald case ',': 1243f8301d46SMatthias Ringwald case '-': 12441d81c7feSMatthias Ringwald case ';': 12451d81c7feSMatthias Ringwald case '(': 12461d81c7feSMatthias Ringwald case ')': 12471d81c7feSMatthias Ringwald case '\n': 12481d81c7feSMatthias Ringwald case '\r': 12493deb3ec6SMatthias Ringwald break; 12503deb3ec6SMatthias Ringwald default: 12511d81c7feSMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 12523deb3ec6SMatthias Ringwald break; 12533deb3ec6SMatthias Ringwald } 12543deb3ec6SMatthias Ringwald } 12551d81c7feSMatthias Ringwald 12561d81c7feSMatthias Ringwald static bool hfp_parser_is_separator( uint8_t byte){ 12571d81c7feSMatthias Ringwald switch (byte){ 12581d81c7feSMatthias Ringwald case ',': 1259f8301d46SMatthias Ringwald case '-': 12601d81c7feSMatthias Ringwald case ';': 12611d81c7feSMatthias Ringwald case '\n': 12621d81c7feSMatthias Ringwald case '\r': 12631d81c7feSMatthias Ringwald return true; 1264dd533208SMatthias Ringwald default: 12651d81c7feSMatthias Ringwald return false; 12663deb3ec6SMatthias Ringwald } 12673deb3ec6SMatthias Ringwald } 12683deb3ec6SMatthias Ringwald 1269d6b237acSMatthias Ringwald static bool hfp_parse_byte(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){ 1270aa4dd815SMatthias Ringwald 12711dddc4f4SMatthias Ringwald // handle doubles quotes 12721dddc4f4SMatthias Ringwald if (byte == '"'){ 12731dddc4f4SMatthias Ringwald hfp_connection->parser_quoted = !hfp_connection->parser_quoted; 1274d6b237acSMatthias Ringwald return true; 12751dddc4f4SMatthias Ringwald } 12761dddc4f4SMatthias Ringwald if (hfp_connection->parser_quoted) { 12771dddc4f4SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 1278d6b237acSMatthias Ringwald return true; 12791dddc4f4SMatthias Ringwald } 12803deb3ec6SMatthias Ringwald 12811dddc4f4SMatthias Ringwald // ignore spaces outside command or double quotes (required e.g. for '+CME ERROR:..") command 1282d6b237acSMatthias Ringwald if ((byte == ' ') && (hfp_connection->parser_state != HFP_PARSER_CMD_HEADER)) return true; 12831dddc4f4SMatthias Ringwald 1284bfeaf344SMatthias Ringwald bool processed = true; 1285bfeaf344SMatthias Ringwald 1286a0ffb263SMatthias Ringwald switch (hfp_connection->parser_state) { 1287dd533208SMatthias Ringwald case HFP_PARSER_CMD_HEADER: 1288d28b2d5dSMilanka Ringwald switch (byte) { 1289dd533208SMatthias Ringwald case '\n': 1290dd533208SMatthias Ringwald case '\r': 1291dd533208SMatthias Ringwald case ';': 1292bfeaf344SMatthias Ringwald // ignore separator 1293bfeaf344SMatthias Ringwald break; 1294bfeaf344SMatthias Ringwald case ':': 1295bfeaf344SMatthias Ringwald case '?': 1296bfeaf344SMatthias Ringwald // store separator 1297bfeaf344SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 1298dd533208SMatthias Ringwald break; 1299d28b2d5dSMilanka Ringwald case '=': 1300bfeaf344SMatthias Ringwald // equal sign: remember and wait for next char to decided between '=?' and '=\?' 1301dd533208SMatthias Ringwald hfp_connection->found_equal_sign = true; 1302a0ffb263SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 1303d6b237acSMatthias Ringwald return true; 1304d28b2d5dSMilanka Ringwald default: 1305bfeaf344SMatthias Ringwald // store if not lookahead 1306d7f16ff1SMatthias Ringwald if (!hfp_connection->found_equal_sign) { 1307dd533208SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 1308d6b237acSMatthias Ringwald return true; 1309dd533208SMatthias Ringwald } 1310bfeaf344SMatthias Ringwald // mark as lookahead 1311bfeaf344SMatthias Ringwald processed = false; 1312d7f16ff1SMatthias Ringwald break; 1313d7f16ff1SMatthias Ringwald } 1314ce263fc8SMatthias Ringwald 13151d81c7feSMatthias Ringwald // ignore empty tokens 1316d6b237acSMatthias Ringwald if (hfp_parser_is_buffer_empty(hfp_connection)) return true; 1317dd533208SMatthias Ringwald 1318bfeaf344SMatthias Ringwald // parse 1319dd533208SMatthias Ringwald hfp_connection->command = parse_command((char *)hfp_connection->line_buffer, isHandsFree); 1320aa4dd815SMatthias Ringwald 132184a0c24eSMatthias Ringwald // pick +CIND version based on connection state: descriptions during SLC vs. states later 132284a0c24eSMatthias Ringwald if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC){ 1323a0ffb263SMatthias Ringwald switch(hfp_connection->state){ 1324aa4dd815SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS_STATUS: 1325a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS; 1326aa4dd815SMatthias Ringwald break; 1327aa4dd815SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS: 1328a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS; 1329aa4dd815SMatthias Ringwald break; 1330aa4dd815SMatthias Ringwald default: 133184a0c24eSMatthias Ringwald hfp_connection->command = HFP_CMD_UNKNOWN; 1332aa4dd815SMatthias Ringwald break; 1333aa4dd815SMatthias Ringwald } 1334aa4dd815SMatthias Ringwald } 13353deb3ec6SMatthias Ringwald 1336c0c0437aSMatthias Ringwald log_info("command string '%s', handsfree %u -> cmd id %u", (char *)hfp_connection->line_buffer, isHandsFree, hfp_connection->command); 1337c0c0437aSMatthias Ringwald 1338471dea41SMatthias Ringwald // store command id for custom command and just store rest of line 1339471dea41SMatthias Ringwald if (hfp_connection->command == HFP_CMD_CUSTOM_MESSAGE){ 1340471dea41SMatthias Ringwald const hfp_custom_at_command_t * at_command = hfp_custom_command_lookup((const char *)hfp_connection->line_buffer); 1341471dea41SMatthias Ringwald hfp_connection->ag_custom_at_command_id = at_command->command_id; 1342471dea41SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CUSTOM_COMMAND; 1343471dea41SMatthias Ringwald return true; 1344471dea41SMatthias Ringwald } 1345471dea41SMatthias Ringwald 1346bfeaf344SMatthias Ringwald // next state 13471d81c7feSMatthias Ringwald hfp_parser_reset_line_buffer(hfp_connection); 1348dd533208SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE; 1349dd533208SMatthias Ringwald 1350bfeaf344SMatthias Ringwald return processed; 1351dd533208SMatthias Ringwald 1352bfeaf344SMatthias Ringwald case HFP_PARSER_CMD_SEQUENCE: 13531d81c7feSMatthias Ringwald // handle empty fields 13541d81c7feSMatthias Ringwald if ((byte == ',' ) && (hfp_connection->line_size == 0)){ 1355dd533208SMatthias Ringwald hfp_connection->line_buffer[0] = 0; 1356dd533208SMatthias Ringwald hfp_connection->ignore_value = 1; 1357dd533208SMatthias Ringwald parse_sequence(hfp_connection); 13583d051a53SMatthias Ringwald return true; 13593d051a53SMatthias Ringwald } 13603d051a53SMatthias Ringwald 13611d81c7feSMatthias Ringwald hfp_parser_store_if_token(hfp_connection, byte); 13621d81c7feSMatthias Ringwald if (!hfp_parser_is_separator(byte)) return true; 13631d81c7feSMatthias Ringwald 13641d81c7feSMatthias Ringwald // ignore empty tokens 136589e7c136SMilanka Ringwald switch (hfp_connection->command){ 136689e7c136SMilanka Ringwald case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION: 136789e7c136SMilanka Ringwald // don't ignore empty string 136889e7c136SMilanka Ringwald break; 136989e7c136SMilanka Ringwald default: 137089e7c136SMilanka Ringwald if (hfp_parser_is_buffer_empty(hfp_connection) && (hfp_connection->ignore_value == 0)) { 137189e7c136SMilanka Ringwald return true; 137289e7c136SMilanka Ringwald } 137389e7c136SMilanka Ringwald break; 137489e7c136SMilanka Ringwald } 13753d051a53SMatthias Ringwald 13763d051a53SMatthias Ringwald parse_sequence(hfp_connection); 13771d81c7feSMatthias Ringwald 13781d81c7feSMatthias Ringwald hfp_parser_reset_line_buffer(hfp_connection); 13791d81c7feSMatthias Ringwald 13801d81c7feSMatthias Ringwald switch (hfp_connection->command){ 13811d81c7feSMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 13821d81c7feSMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 13831d81c7feSMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 13841d81c7feSMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 13851d81c7feSMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 13861d81c7feSMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 13871d81c7feSMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 13881d81c7feSMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 13891d81c7feSMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 13901d81c7feSMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_SECOND_ITEM; 13911d81c7feSMatthias Ringwald break; 13921d81c7feSMatthias Ringwald default: 13931d81c7feSMatthias Ringwald break; 13941d81c7feSMatthias Ringwald } 13953d051a53SMatthias Ringwald return true; 13963d051a53SMatthias Ringwald 1397ba0de059SMatthias Ringwald case HFP_PARSER_SECOND_ITEM: 13981d81c7feSMatthias Ringwald 13991d81c7feSMatthias Ringwald hfp_parser_store_if_token(hfp_connection, byte); 14001d81c7feSMatthias Ringwald if (!hfp_parser_is_separator(byte)) return true; 1401dd533208SMatthias Ringwald 1402a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1403ce263fc8SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1404a0ffb263SMatthias Ringwald log_info("format %s, ", hfp_connection->line_buffer); 14052308e108SMilanka Ringwald hfp_connection->network_operator.format = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1406ce263fc8SMatthias Ringwald break; 1407ce263fc8SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 1408a0ffb263SMatthias Ringwald log_info("format %s \n", hfp_connection->line_buffer); 14092308e108SMilanka Ringwald hfp_connection->network_operator.format = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1410ce263fc8SMatthias Ringwald break; 1411ce263fc8SMatthias Ringwald case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 1412ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 1413ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 14142308e108SMilanka Ringwald hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].state = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1415ce263fc8SMatthias Ringwald break; 1416ce263fc8SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 14172308e108SMilanka Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1418a0ffb263SMatthias Ringwald log_info("%d \n", hfp_connection->ag_indicators[hfp_connection->parser_item_index].status); 1419a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].status_changed = 1; 1420ce263fc8SMatthias Ringwald break; 1421ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 14222308e108SMilanka Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].min_range = btstack_atoi((char *)hfp_connection->line_buffer); 1423a0ffb263SMatthias Ringwald log_info("%s, ", hfp_connection->line_buffer); 1424ce263fc8SMatthias Ringwald break; 1425ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 1426a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1427a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 14282308e108SMilanka Ringwald hfp_connection->bnip_type = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1429ce263fc8SMatthias Ringwald break; 14300222a807SMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 14310222a807SMatthias Ringwald hfp_connection->parser_indicator_value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 14320222a807SMatthias Ringwald break; 1433ce263fc8SMatthias Ringwald default: 1434ce263fc8SMatthias Ringwald break; 1435ce263fc8SMatthias Ringwald } 14361d81c7feSMatthias Ringwald 14371d81c7feSMatthias Ringwald hfp_parser_reset_line_buffer(hfp_connection); 14381d81c7feSMatthias Ringwald 14391d81c7feSMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_THIRD_ITEM; 14401d81c7feSMatthias Ringwald 1441ba0de059SMatthias Ringwald return true; 1442ce263fc8SMatthias Ringwald 144374c7548aSMatthias Ringwald case HFP_PARSER_THIRD_ITEM: 14441d81c7feSMatthias Ringwald 14451d81c7feSMatthias Ringwald hfp_parser_store_if_token(hfp_connection, byte); 14461d81c7feSMatthias Ringwald if (!hfp_parser_is_separator(byte)) return true; 14471d81c7feSMatthias Ringwald 1448a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1449ce263fc8SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1450c10fde09SMatthias Ringwald btstack_strcpy(hfp_connection->network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE, (char *)hfp_connection->line_buffer); 1451ce263fc8SMatthias Ringwald break; 1452ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 14532308e108SMilanka Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].max_range = btstack_atoi((char *)hfp_connection->line_buffer); 145425789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 145525789943SMilanka Ringwald hfp_connection->ag_indicators_nr = hfp_connection->parser_item_index; 1456ce263fc8SMatthias Ringwald break; 1457125560b8SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1458125560b8SMatthias Ringwald // track if last argument exists 1459125560b8SMatthias Ringwald hfp_connection->clip_have_alpha = hfp_connection->line_size != 0; 1460125560b8SMatthias Ringwald break; 1461ce263fc8SMatthias Ringwald default: 1462ce263fc8SMatthias Ringwald break; 1463ce263fc8SMatthias Ringwald } 14641d81c7feSMatthias Ringwald 14651d81c7feSMatthias Ringwald hfp_parser_reset_line_buffer(hfp_connection); 14661d81c7feSMatthias Ringwald 14671d81c7feSMatthias Ringwald if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS){ 14681d81c7feSMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE; 14691d81c7feSMatthias Ringwald } 1470d6b237acSMatthias Ringwald return true; 147174c7548aSMatthias Ringwald 1472471dea41SMatthias Ringwald case HFP_PARSER_CUSTOM_COMMAND: 1473471dea41SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 1474471dea41SMatthias Ringwald return true; 1475471dea41SMatthias Ringwald 147674c7548aSMatthias Ringwald default: 147774c7548aSMatthias Ringwald btstack_assert(false); 147874c7548aSMatthias Ringwald return true; 147974c7548aSMatthias Ringwald } 1480d6b237acSMatthias Ringwald } 1481d6b237acSMatthias Ringwald 1482d6b237acSMatthias Ringwald void hfp_parse(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){ 1483d6b237acSMatthias Ringwald bool processed = false; 1484d6b237acSMatthias Ringwald while (!processed){ 1485d6b237acSMatthias Ringwald processed = hfp_parse_byte(hfp_connection, byte, isHandsFree); 1486d6b237acSMatthias Ringwald } 14871d81c7feSMatthias Ringwald // reset parser state on end-of-line 14881d81c7feSMatthias Ringwald if (hfp_parser_is_end_of_line(byte)){ 1489a34e380cSMatthias Ringwald hfp_connection->found_equal_sign = false; 14901d81c7feSMatthias Ringwald hfp_connection->parser_item_index = 0; 14911d81c7feSMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 14921d81c7feSMatthias Ringwald } 1493ce263fc8SMatthias Ringwald } 1494ce263fc8SMatthias Ringwald 1495a0ffb263SMatthias Ringwald static void parse_sequence(hfp_connection_t * hfp_connection){ 1496ce263fc8SMatthias Ringwald int value; 1497a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1498667ec068SMatthias Ringwald case HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS: 14992308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1500667ec068SMatthias Ringwald int i; 1501a0ffb263SMatthias Ringwald switch (hfp_connection->parser_item_index){ 1502667ec068SMatthias Ringwald case 0: 1503a0ffb263SMatthias Ringwald for (i=0;i<hfp_connection->generic_status_indicators_nr;i++){ 1504a0ffb263SMatthias Ringwald if (hfp_connection->generic_status_indicators[i].uuid == value){ 1505a0ffb263SMatthias Ringwald hfp_connection->parser_indicator_index = i; 1506667ec068SMatthias Ringwald break; 1507667ec068SMatthias Ringwald } 1508667ec068SMatthias Ringwald } 1509667ec068SMatthias Ringwald break; 1510667ec068SMatthias Ringwald case 1: 1511a0ffb263SMatthias Ringwald if (hfp_connection->parser_indicator_index <0) break; 1512a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[hfp_connection->parser_indicator_index].state = value; 1513667ec068SMatthias Ringwald log_info("HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS set indicator at index %u, to %u\n", 1514a0ffb263SMatthias Ringwald hfp_connection->parser_item_index, value); 1515667ec068SMatthias Ringwald break; 1516667ec068SMatthias Ringwald default: 1517667ec068SMatthias Ringwald break; 1518667ec068SMatthias Ringwald } 151925789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 1520667ec068SMatthias Ringwald break; 1521667ec068SMatthias Ringwald 1522667ec068SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1523a0ffb263SMatthias Ringwald switch(hfp_connection->parser_item_index){ 1524667ec068SMatthias Ringwald case 0: 152528a4aea3SMilanka Ringwald // <alpha>: This optional field is not supported, and shall be left blank. 152628a4aea3SMilanka Ringwald break; 152728a4aea3SMilanka Ringwald case 1: 152828a4aea3SMilanka Ringwald // <number>: Quoted string containing the phone number in the format specified by <type>. 1529c10fde09SMatthias Ringwald btstack_strcpy(hfp_connection->bnip_number, sizeof(hfp_connection->bnip_number), (char *)hfp_connection->line_buffer); 1530667ec068SMatthias Ringwald break; 153128a4aea3SMilanka Ringwald case 2: 153228a4aea3SMilanka Ringwald /* 153328a4aea3SMilanka Ringwald <type> field specifies the format of the phone number provided, and can be one of the following values: 153428a4aea3SMilanka 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. 153528a4aea3SMilanka 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. 153628a4aea3SMilanka Ringwald - values 160-175: National number. No prefix nor escape digits included. 153728a4aea3SMilanka Ringwald */ 15382308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1539a0ffb263SMatthias Ringwald hfp_connection->bnip_type = value; 1540667ec068SMatthias Ringwald break; 154128a4aea3SMilanka Ringwald case 3: 154228a4aea3SMilanka Ringwald // <speed>: This optional field is not supported, and shall be left blank. 154328a4aea3SMilanka Ringwald break; 154428a4aea3SMilanka Ringwald case 4: 154528a4aea3SMilanka Ringwald // <service>: Indicates which service this phone number relates to. Shall be either 4 (voice) or 5 (fax). 1546667ec068SMatthias Ringwald default: 1547667ec068SMatthias Ringwald break; 1548667ec068SMatthias Ringwald } 1549eed67a37SMilanka Ringwald // index > 2 are ignored in switch above 1550a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1551667ec068SMatthias Ringwald break; 1552667ec068SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 1553a0ffb263SMatthias Ringwald switch(hfp_connection->parser_item_index){ 1554667ec068SMatthias Ringwald case 0: 15552308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1556a0ffb263SMatthias Ringwald hfp_connection->clcc_idx = value; 1557667ec068SMatthias Ringwald break; 1558667ec068SMatthias Ringwald case 1: 15592308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1560a0ffb263SMatthias Ringwald hfp_connection->clcc_dir = value; 1561667ec068SMatthias Ringwald break; 1562667ec068SMatthias Ringwald case 2: 15632308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1564a0ffb263SMatthias Ringwald hfp_connection->clcc_status = value; 1565667ec068SMatthias Ringwald break; 1566667ec068SMatthias Ringwald case 3: 15672308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 15680aee97efSMilanka Ringwald hfp_connection->clcc_mode = value; 1569667ec068SMatthias Ringwald break; 1570667ec068SMatthias Ringwald case 4: 15710aee97efSMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 15720aee97efSMilanka Ringwald hfp_connection->clcc_mpty = value; 15730aee97efSMilanka Ringwald break; 15740aee97efSMilanka Ringwald case 5: 1575c10fde09SMatthias Ringwald btstack_strcpy(hfp_connection->bnip_number, sizeof(hfp_connection->bnip_number), (char *)hfp_connection->line_buffer); 1576667ec068SMatthias Ringwald break; 15770aee97efSMilanka Ringwald case 6: 15782308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1579a0ffb263SMatthias Ringwald hfp_connection->bnip_type = value; 1580667ec068SMatthias Ringwald break; 1581667ec068SMatthias Ringwald default: 1582667ec068SMatthias Ringwald break; 1583667ec068SMatthias Ringwald } 1584eed67a37SMilanka Ringwald // index > 6 are ignored in switch above 1585a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1586667ec068SMatthias Ringwald break; 1587aa4dd815SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 15882308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1589a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = value; 1590aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CMD_SET_MICROPHONE_GAIN %d\n", value); 1591aa4dd815SMatthias Ringwald break; 1592aa4dd815SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 15932308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1594a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = value; 1595aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CMD_SET_SPEAKER_GAIN %d\n", value); 1596aa4dd815SMatthias Ringwald break; 1597aa4dd815SMatthias Ringwald case HFP_CMD_TURN_OFF_EC_AND_NR: 15982308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1599a0ffb263SMatthias Ringwald hfp_connection->ag_echo_and_noise_reduction = value; 1600aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CMD_TURN_OFF_EC_AND_NR %d\n", value); 1601aa4dd815SMatthias Ringwald break; 1602aa4dd815SMatthias Ringwald case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING: 16032308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1604a0ffb263SMatthias Ringwald hfp_connection->remote_supported_features = store_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE, value); 1605aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CHANGE_IN_BAND_RING_TONE_SETTING %d\n", value); 1606aa4dd815SMatthias Ringwald break; 16073deb3ec6SMatthias Ringwald case HFP_CMD_HF_CONFIRMED_CODEC: 16082308e108SMilanka Ringwald hfp_connection->codec_confirmed = btstack_atoi((char*)hfp_connection->line_buffer); 1609a0ffb263SMatthias Ringwald log_info("hfp parse HFP_CMD_HF_CONFIRMED_CODEC %d\n", hfp_connection->codec_confirmed); 16103deb3ec6SMatthias Ringwald break; 16113deb3ec6SMatthias Ringwald case HFP_CMD_AG_SUGGESTED_CODEC: 16122308e108SMilanka Ringwald hfp_connection->suggested_codec = btstack_atoi((char*)hfp_connection->line_buffer); 1613a0ffb263SMatthias Ringwald log_info("hfp parse HFP_CMD_AG_SUGGESTED_CODEC %d\n", hfp_connection->suggested_codec); 16143deb3ec6SMatthias Ringwald break; 16153deb3ec6SMatthias Ringwald case HFP_CMD_SUPPORTED_FEATURES: 16162308e108SMilanka Ringwald hfp_connection->remote_supported_features = btstack_atoi((char*)hfp_connection->line_buffer); 1617bace42efSMatthias Ringwald log_info("Parsed supported feature %d\n", (int) hfp_connection->remote_supported_features); 16183deb3ec6SMatthias Ringwald break; 16193deb3ec6SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 1620a0ffb263SMatthias Ringwald log_info("Parsed codec %s\n", hfp_connection->line_buffer); 1621ab2445a0SMatthias Ringwald hfp_connection->remote_codecs[hfp_connection->parser_item_index] = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 162225789943SMilanka Ringwald hfp_next_codec_index(hfp_connection); 1623a0ffb263SMatthias Ringwald hfp_connection->remote_codecs_nr = hfp_connection->parser_item_index; 16243deb3ec6SMatthias Ringwald break; 1625aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 1626c10fde09SMatthias Ringwald btstack_strcpy((char *)hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, HFP_MAX_INDICATOR_DESC_SIZE, (char *)hfp_connection->line_buffer); 1627a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].index = hfp_connection->parser_item_index+1; 1628a0ffb263SMatthias Ringwald log_info("Indicator %d: %s (", hfp_connection->ag_indicators_nr+1, hfp_connection->line_buffer); 1629aa4dd815SMatthias Ringwald break; 1630aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 1631a0ffb263SMatthias Ringwald log_info("Parsed Indicator %d with status: %s\n", hfp_connection->parser_item_index+1, hfp_connection->line_buffer); 16322308e108SMilanka Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = btstack_atoi((char *) hfp_connection->line_buffer); 163325789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 16343deb3ec6SMatthias Ringwald break; 16353deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 163625789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 1637a0ffb263SMatthias Ringwald if (hfp_connection->parser_item_index != 4) break; 1638a0ffb263SMatthias Ringwald log_info("Parsed Enable indicators: %s\n", hfp_connection->line_buffer); 16392308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1640a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = (uint8_t) value; 16413deb3ec6SMatthias Ringwald break; 16423deb3ec6SMatthias Ringwald case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES: 1643a0ffb263SMatthias Ringwald log_info("Parsed Support call hold: %s\n", hfp_connection->line_buffer); 1644a0ffb263SMatthias Ringwald if (hfp_connection->line_size > 2 ) break; 164597d2cfbcSMatthias 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); 164625789943SMilanka Ringwald hfp_connection->remote_call_services[hfp_connection->remote_call_services_index].name[HFP_CALL_SERVICE_SIZE - 1] = 0; 1647eed67a37SMilanka Ringwald hfp_next_remote_call_services_index(hfp_connection); 16483deb3ec6SMatthias Ringwald break; 1649aa4dd815SMatthias Ringwald case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 1650aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 1651a0ffb263SMatthias Ringwald log_info("Parsed Generic status indicator: %s\n", hfp_connection->line_buffer); 16522308e108SMilanka Ringwald hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].uuid = (uint16_t)btstack_atoi((char*)hfp_connection->line_buffer); 165325789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 1654a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators_nr = hfp_connection->parser_item_index; 16553deb3ec6SMatthias Ringwald break; 1656aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 16573deb3ec6SMatthias Ringwald // HF parses inital AG gen. ind. state 1658a0ffb263SMatthias Ringwald log_info("Parsed List generic status indicator %s state: ", hfp_connection->line_buffer); 165925789943SMilanka Ringwald hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection); 16603deb3ec6SMatthias Ringwald break; 1661ce263fc8SMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 166225789943SMilanka Ringwald hfp_connection->parser_indicator_index = hfp_parse_indicator_index(hfp_connection); 1663a0ffb263SMatthias Ringwald log_info("Parsed HF indicator index %u", hfp_connection->parser_indicator_index); 1664ce263fc8SMatthias Ringwald break; 16653deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE: 16663deb3ec6SMatthias Ringwald // AG parses new gen. ind. state 1667a0ffb263SMatthias Ringwald if (hfp_connection->ignore_value){ 1668a0ffb263SMatthias Ringwald hfp_connection->ignore_value = 0; 1669a0ffb263SMatthias Ringwald log_info("Parsed Enable AG indicator pos %u('%s') - unchanged (stays %u)\n", hfp_connection->parser_item_index, 1670a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled); 1671ce263fc8SMatthias Ringwald } 1672a0ffb263SMatthias Ringwald else if (hfp_connection->ag_indicators[hfp_connection->parser_item_index].mandatory){ 1673ce263fc8SMatthias Ringwald log_info("Parsed Enable AG indicator pos %u('%s') - ignore (mandatory)\n", 1674a0ffb263SMatthias Ringwald hfp_connection->parser_item_index, hfp_connection->ag_indicators[hfp_connection->parser_item_index].name); 1675ce263fc8SMatthias Ringwald } else { 16762308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1677a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled = value; 1678a0ffb263SMatthias Ringwald log_info("Parsed Enable AG indicator pos %u('%s'): %u\n", hfp_connection->parser_item_index, 1679a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, value); 16803deb3ec6SMatthias Ringwald } 168125789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 16823deb3ec6SMatthias Ringwald break; 16833deb3ec6SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 16843deb3ec6SMatthias Ringwald // indicators are indexed starting with 1 168525789943SMilanka Ringwald hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection); 1686a0ffb263SMatthias Ringwald log_info("Parsed status of the AG indicator %d, status ", hfp_connection->parser_item_index); 16873deb3ec6SMatthias Ringwald break; 1688aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 16892308e108SMilanka Ringwald hfp_connection->network_operator.mode = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1690a0ffb263SMatthias Ringwald log_info("Parsed network operator mode: %d, ", hfp_connection->network_operator.mode); 1691aa4dd815SMatthias Ringwald break; 1692aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 1693a0ffb263SMatthias Ringwald if (hfp_connection->line_buffer[0] == '3'){ 1694a0ffb263SMatthias Ringwald log_info("Parsed Set network operator format : %s, ", hfp_connection->line_buffer); 16953deb3ec6SMatthias Ringwald break; 16963deb3ec6SMatthias Ringwald } 16973deb3ec6SMatthias Ringwald // TODO emit ERROR, wrong format 1698a0ffb263SMatthias Ringwald log_info("ERROR Set network operator format: index %s not supported\n", hfp_connection->line_buffer); 16993deb3ec6SMatthias Ringwald break; 17003deb3ec6SMatthias Ringwald case HFP_CMD_ERROR: 17013deb3ec6SMatthias Ringwald break; 17023deb3ec6SMatthias Ringwald case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1703a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 1; 17042308e108SMilanka Ringwald hfp_connection->extended_audio_gateway_error_value = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 17053deb3ec6SMatthias Ringwald break; 17063deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR: 17072308e108SMilanka Ringwald hfp_connection->enable_extended_audio_gateway_error_report = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1708a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1709a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 17103deb3ec6SMatthias Ringwald break; 1711ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 1712a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1713a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1714c10fde09SMatthias Ringwald btstack_strcpy((char *)hfp_connection->bnip_number, sizeof(hfp_connection->bnip_number), (char *)hfp_connection->line_buffer); 17153deb3ec6SMatthias Ringwald break; 17160222a807SMatthias Ringwald case HFP_CMD_CALL_HOLD: 17170222a807SMatthias Ringwald hfp_connection->ag_call_hold_action = hfp_connection->line_buffer[0] - '0'; 17180222a807SMatthias Ringwald if (hfp_connection->line_buffer[1] != '\0'){ 17190222a807SMatthias Ringwald hfp_connection->call_index = btstack_atoi((char *)&hfp_connection->line_buffer[1]); 17200222a807SMatthias Ringwald } 17210222a807SMatthias Ringwald break; 17220222a807SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_COMMAND: 17230222a807SMatthias Ringwald hfp_connection->ag_response_and_hold_action = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 17240222a807SMatthias Ringwald break; 17250222a807SMatthias Ringwald case HFP_CMD_TRANSMIT_DTMF_CODES: 17260222a807SMatthias Ringwald hfp_connection->ag_dtmf_code = hfp_connection->line_buffer[0]; 17270222a807SMatthias Ringwald break; 17280222a807SMatthias Ringwald case HFP_CMD_ENABLE_CLIP: 17290222a807SMatthias Ringwald hfp_connection->clip_enabled = hfp_connection->line_buffer[0] != '0'; 17300222a807SMatthias Ringwald break; 17310222a807SMatthias Ringwald case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION: 17320222a807SMatthias Ringwald hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[0] != '0'; 17330222a807SMatthias Ringwald break; 17340b4debbfSMilanka Ringwald case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION: 17350b4debbfSMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 17360b4debbfSMilanka Ringwald hfp_connection->ag_activate_voice_recognition_value = value; 17370b4debbfSMilanka Ringwald break; 1738db3cdbd4SMilanka Ringwald case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION: 1739db3cdbd4SMilanka Ringwald switch(hfp_connection->parser_item_index){ 1740db3cdbd4SMilanka Ringwald case 0: 17410b4debbfSMilanka Ringwald hfp_connection->ag_vra_status = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1742db3cdbd4SMilanka Ringwald break; 1743db3cdbd4SMilanka Ringwald case 1: 1744c78e7b73SMatthias Ringwald hfp_connection->ag_vra_state = (hfp_voice_recognition_state_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1745db3cdbd4SMilanka Ringwald break; 1746db3cdbd4SMilanka Ringwald case 2: 174745796ff1SMilanka Ringwald hfp_connection->ag_msg.text_id = 0; 1748db3cdbd4SMilanka Ringwald for (i = 0 ; i < 4; i++){ 174945796ff1SMilanka Ringwald hfp_connection->ag_msg.text_id = (hfp_connection->ag_msg.text_id << 4) | nibble_for_char(hfp_connection->line_buffer[i]); 1750db3cdbd4SMilanka Ringwald } 1751db3cdbd4SMilanka Ringwald break; 1752db3cdbd4SMilanka Ringwald case 3: 17530d854c7cSMilanka Ringwald hfp_connection->ag_msg.text_type = (hfp_text_type_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1754db3cdbd4SMilanka Ringwald break; 1755db3cdbd4SMilanka Ringwald case 4: 17560d854c7cSMilanka Ringwald hfp_connection->ag_msg.text_operation = (hfp_text_operation_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1757db3cdbd4SMilanka Ringwald break; 1758db3cdbd4SMilanka Ringwald case 5: 175951aa5d5aSMilanka Ringwald hfp_connection->line_buffer[hfp_connection->line_size] = 0; 176051aa5d5aSMilanka Ringwald hfp_connection->ag_vra_msg_length = hfp_connection->line_size + 1; 1761db3cdbd4SMilanka Ringwald break; 1762db3cdbd4SMilanka Ringwald default: 1763db3cdbd4SMilanka Ringwald break; 1764db3cdbd4SMilanka Ringwald } 1765ddbf29d2SMilanka Ringwald hfp_connection->parser_item_index++; 1766db3cdbd4SMilanka Ringwald break; 17673deb3ec6SMatthias Ringwald default: 17683deb3ec6SMatthias Ringwald break; 17693deb3ec6SMatthias Ringwald } 17703deb3ec6SMatthias Ringwald } 17713deb3ec6SMatthias Ringwald 17721d9c9c90SMilanka Ringwald static void hfp_handle_start_sdp_client_query(void * context){ 17731d9c9c90SMilanka Ringwald UNUSED(context); 17741d9c9c90SMilanka Ringwald 17751d9c9c90SMilanka Ringwald btstack_linked_list_iterator_t it; 17761d9c9c90SMilanka Ringwald btstack_linked_list_iterator_init(&it, &hfp_connections); 17771d9c9c90SMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 17781d9c9c90SMilanka Ringwald hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 17791d9c9c90SMilanka Ringwald 17801d9c9c90SMilanka Ringwald if (connection->state != HFP_W2_SEND_SDP_QUERY) continue; 17811d9c9c90SMilanka Ringwald 17821d9c9c90SMilanka Ringwald connection->state = HFP_W4_SDP_QUERY_COMPLETE; 1783aeb0f0feSMatthias Ringwald hfp_sdp_query_context.local_role = connection->local_role; 1784aeb0f0feSMatthias Ringwald (void)memcpy(hfp_sdp_query_context.remote_address, connection->remote_addr, 6); 1785e55c05e3SMatthias Ringwald sdp_client_query_rfcomm_channel_and_name_for_service_class_uuid(&handle_query_rfcomm_event, connection->remote_addr, connection->service_uuid); 17861d9c9c90SMilanka Ringwald return; 17871d9c9c90SMilanka Ringwald } 17881d9c9c90SMilanka Ringwald } 17891d9c9c90SMilanka Ringwald 17904eb3f1d8SMilanka Ringwald uint8_t hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid, hfp_role_t local_role){ 17914eb3f1d8SMilanka Ringwald hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr, local_role); 17924eb3f1d8SMilanka Ringwald if (connection){ 17934eb3f1d8SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 17943deb3ec6SMatthias Ringwald } 17954eb3f1d8SMilanka Ringwald 17964eb3f1d8SMilanka Ringwald connection = hfp_create_connection(bd_addr, local_role); 17974eb3f1d8SMilanka Ringwald if (!connection){ 17984eb3f1d8SMilanka Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 17994eb3f1d8SMilanka Ringwald } 18004eb3f1d8SMilanka Ringwald 18014eb3f1d8SMilanka Ringwald connection->state = HFP_W2_SEND_SDP_QUERY; 18024eb3f1d8SMilanka Ringwald 18034eb3f1d8SMilanka Ringwald bd_addr_copy(connection->remote_addr, bd_addr); 18044eb3f1d8SMilanka Ringwald connection->service_uuid = service_uuid; 18051d9c9c90SMilanka Ringwald 1806aeb0f0feSMatthias Ringwald hfp_sdp_query_request.callback = &hfp_handle_start_sdp_client_query; 18071d9c9c90SMilanka Ringwald // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback 1808aeb0f0feSMatthias Ringwald (void) sdp_client_register_query_callback(&hfp_sdp_query_request); 18094eb3f1d8SMilanka Ringwald return ERROR_CODE_SUCCESS; 18103deb3ec6SMatthias Ringwald } 18113deb3ec6SMatthias Ringwald 1812657bc59fSMilanka Ringwald void hfp_trigger_release_service_level_connection(hfp_connection_t * hfp_connection){ 18131ffa0dd9SMilanka Ringwald // called internally, NULL check already performed 1814657bc59fSMilanka Ringwald btstack_assert(hfp_connection != NULL); 18153deb3ec6SMatthias Ringwald 1816657bc59fSMilanka Ringwald hfp_trigger_release_audio_connection(hfp_connection); 1817657bc59fSMilanka Ringwald if (hfp_connection->state < HFP_W4_RFCOMM_CONNECTED){ 1818657bc59fSMilanka Ringwald hfp_connection->state = HFP_IDLE; 18193deb3ec6SMatthias Ringwald return; 18203deb3ec6SMatthias Ringwald } 18213deb3ec6SMatthias Ringwald 1822657bc59fSMilanka Ringwald if (hfp_connection->state == HFP_W4_RFCOMM_CONNECTED){ 1823657bc59fSMilanka Ringwald hfp_connection->state = HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN; 18243deb3ec6SMatthias Ringwald return; 18253deb3ec6SMatthias Ringwald } 182652cbdd6dSMilanka Ringwald hfp_connection->release_slc_connection = 1; 1827657bc59fSMilanka Ringwald if (hfp_connection->state < HFP_W4_SCO_CONNECTED){ 1828657bc59fSMilanka Ringwald hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM; 18290c3047fbSMatthias Ringwald return; 18300c3047fbSMatthias Ringwald } 18310c3047fbSMatthias Ringwald 1832657bc59fSMilanka Ringwald if (hfp_connection->state < HFP_W4_SCO_DISCONNECTED){ 1833657bc59fSMilanka Ringwald hfp_connection->state = HFP_W2_DISCONNECT_SCO; 183452cbdd6dSMilanka Ringwald hfp_connection->release_audio_connection = 1; 18350c3047fbSMatthias Ringwald return; 18360c3047fbSMatthias Ringwald } 18373deb3ec6SMatthias Ringwald } 18383deb3ec6SMatthias Ringwald 18390b4debbfSMilanka Ringwald uint8_t hfp_trigger_release_audio_connection(hfp_connection_t * hfp_connection){ 18401ffa0dd9SMilanka Ringwald // called internally, NULL check already performed 1841657bc59fSMilanka Ringwald btstack_assert(hfp_connection != NULL); 18420b4debbfSMilanka Ringwald if (hfp_connection->state <= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){ 18430b4debbfSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 18441ffa0dd9SMilanka Ringwald } 18450b4debbfSMilanka Ringwald switch (hfp_connection->state) { 18460b4debbfSMilanka Ringwald case HFP_W2_CONNECT_SCO: 18470b4debbfSMilanka Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 18480b4debbfSMilanka Ringwald break; 18490b4debbfSMilanka Ringwald case HFP_W4_SCO_CONNECTED: 18500b4debbfSMilanka Ringwald case HFP_AUDIO_CONNECTION_ESTABLISHED: 18510b4debbfSMilanka Ringwald hfp_connection->release_audio_connection = 1; 18520b4debbfSMilanka Ringwald break; 18530b4debbfSMilanka Ringwald default: 18540b4debbfSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 18550b4debbfSMilanka Ringwald } 18560b4debbfSMilanka Ringwald return ERROR_CODE_SUCCESS; 18573deb3ec6SMatthias Ringwald } 18583deb3ec6SMatthias Ringwald 1859ce263fc8SMatthias Ringwald static const struct link_settings { 1860ce263fc8SMatthias Ringwald const uint16_t max_latency; 1861ce263fc8SMatthias Ringwald const uint8_t retransmission_effort; 1862ce263fc8SMatthias Ringwald const uint16_t packet_types; 1863ebc6f15bSMatthias Ringwald const bool eSCO; 1864ebc6f15bSMatthias Ringwald const uint8_t codec; 1865ce263fc8SMatthias Ringwald } hfp_link_settings [] = { 1866352a0504SMatthias Ringwald { 0xffff, 0xff, SCO_PACKET_TYPES_HV1, false, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_D0 1867352a0504SMatthias Ringwald { 0xffff, 0xff, SCO_PACKET_TYPES_HV3, false, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_D1 1868352a0504SMatthias Ringwald { 0x0007, 0x01, SCO_PACKET_TYPES_EV3, true, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S1 1869352a0504SMatthias Ringwald { 0x0007, 0x01, SCO_PACKET_TYPES_2EV3, true, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S2 1870352a0504SMatthias Ringwald { 0x000a, 0x01, SCO_PACKET_TYPES_2EV3, true, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S3 1871352a0504SMatthias Ringwald { 0x000c, 0x02, SCO_PACKET_TYPES_2EV3, true, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S4 1872352a0504SMatthias Ringwald { 0x0008, 0x02, SCO_PACKET_TYPES_EV3, true, HFP_CODEC_MSBC }, // HFP_LINK_SETTINGS_T1 1873352a0504SMatthias Ringwald { 0x000d, 0x02, SCO_PACKET_TYPES_2EV3, true, HFP_CODEC_MSBC } // HFP_LINK_SETTINGS_T2 1874ce263fc8SMatthias Ringwald }; 18753deb3ec6SMatthias Ringwald 1876eddcd308SMatthias Ringwald void hfp_setup_synchronous_connection(hfp_connection_t * hfp_connection){ 1877ce263fc8SMatthias Ringwald // all packet types, fixed bandwidth 1878eddcd308SMatthias Ringwald int setting = hfp_connection->link_setting; 1879ce263fc8SMatthias Ringwald log_info("hfp_setup_synchronous_connection using setting nr %u", setting); 1880aeb0f0feSMatthias Ringwald hfp_sco_establishment_active = hfp_connection; 1881d2c1d59aSMatthias Ringwald uint16_t sco_voice_setting = hci_get_sco_voice_setting(); 1882d2c1d59aSMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 1883689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 1884689d4323SMatthias Ringwald sco_voice_setting = 0x0063; // Transparent data, 16-bit for BCM controllers 1885689d4323SMatthias Ringwald #else 1886689d4323SMatthias Ringwald sco_voice_setting = 0x0043; // Transparent data, 8-bit otherwise 1887689d4323SMatthias Ringwald #endif 1888d2c1d59aSMatthias Ringwald } 1889352a0504SMatthias Ringwald // get packet types - bits 6-9 are 'don't allow' 1890352a0504SMatthias Ringwald uint16_t packet_types = hfp_link_settings[setting].packet_types ^ 0x03c0; 1891eddcd308SMatthias Ringwald hci_send_cmd(&hci_setup_synchronous_connection, hfp_connection->acl_handle, 8000, 8000, hfp_link_settings[setting].max_latency, 1892352a0504SMatthias Ringwald sco_voice_setting, hfp_link_settings[setting].retransmission_effort, packet_types); 1893ce263fc8SMatthias Ringwald } 1894d68dcce1SMatthias Ringwald 1895cb81d35dSMatthias Ringwald void hfp_accept_synchronous_connection(hfp_connection_t * hfp_connection, bool incoming_eSCO){ 1896cb81d35dSMatthias Ringwald 1897cb81d35dSMatthias Ringwald // remote supported feature eSCO is set if link type is eSCO 1898cb81d35dSMatthias Ringwald // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms, 1899cb81d35dSMatthias Ringwald uint16_t max_latency; 1900cb81d35dSMatthias Ringwald uint8_t retransmission_effort; 1901cb81d35dSMatthias Ringwald uint16_t packet_types; 1902cb81d35dSMatthias Ringwald 1903cb81d35dSMatthias Ringwald if (incoming_eSCO && hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){ 1904cb81d35dSMatthias Ringwald max_latency = 0x000c; 1905cb81d35dSMatthias Ringwald retransmission_effort = 0x02; 1906cb81d35dSMatthias Ringwald // eSCO: EV3 and 2-EV3 1907cb81d35dSMatthias Ringwald packet_types = 0x0048; 1908cb81d35dSMatthias Ringwald } else { 1909cb81d35dSMatthias Ringwald max_latency = 0xffff; 1910cb81d35dSMatthias Ringwald retransmission_effort = 0xff; 1911cb81d35dSMatthias Ringwald // sco: HV1 and HV3 1912cb81d35dSMatthias Ringwald packet_types = 0x005; 1913cb81d35dSMatthias Ringwald } 1914cb81d35dSMatthias Ringwald 1915cb81d35dSMatthias Ringwald // mSBC only allows for transparent data 1916cb81d35dSMatthias Ringwald uint16_t sco_voice_setting = hci_get_sco_voice_setting(); 1917cb81d35dSMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 1918cb81d35dSMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 1919cb81d35dSMatthias Ringwald sco_voice_setting = 0x0063; // Transparent data, 16-bit for BCM controllers 1920cb81d35dSMatthias Ringwald #else 1921cb81d35dSMatthias Ringwald sco_voice_setting = 0x0043; // Transparent data, 8-bit otherwise 1922cb81d35dSMatthias Ringwald #endif 1923cb81d35dSMatthias Ringwald } 1924cb81d35dSMatthias Ringwald 1925cb81d35dSMatthias Ringwald // filter packet types 1926cb81d35dSMatthias Ringwald packet_types &= hfp_get_sco_packet_types(); 1927cb81d35dSMatthias Ringwald 1928cb81d35dSMatthias Ringwald // bits 6-9 are 'don't allow' 1929cb81d35dSMatthias Ringwald packet_types ^= 0x3c0; 1930cb81d35dSMatthias Ringwald 1931cb81d35dSMatthias Ringwald log_info("HFP: sending hci_accept_connection_request, packet types 0x%04x, sco_voice_setting 0x%02x", packet_types, sco_voice_setting); 1932cb81d35dSMatthias Ringwald hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency, 1933cb81d35dSMatthias Ringwald sco_voice_setting, retransmission_effort, packet_types); 1934cb81d35dSMatthias Ringwald } 1935cb81d35dSMatthias Ringwald 19363721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP 19373721a235SMatthias Ringwald void hfp_cc256x_prepare_for_sco(hfp_connection_t * hfp_connection){ 19383721a235SMatthias Ringwald hfp_connection->cc256x_send_write_codec_config = true; 19393721a235SMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 19403721a235SMatthias Ringwald hfp_connection->cc256x_send_wbs_associate = true; 19413721a235SMatthias Ringwald } 19423721a235SMatthias Ringwald } 19433721a235SMatthias Ringwald 19443721a235SMatthias Ringwald void hfp_cc256x_write_codec_config(hfp_connection_t * hfp_connection){ 19453721a235SMatthias Ringwald uint32_t sample_rate_hz; 19463721a235SMatthias Ringwald uint16_t clock_rate_khz; 19473721a235SMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 19483721a235SMatthias Ringwald clock_rate_khz = 512; 19493721a235SMatthias Ringwald sample_rate_hz = 16000; 19503721a235SMatthias Ringwald } else { 19513721a235SMatthias Ringwald clock_rate_khz = 256; 19523721a235SMatthias Ringwald sample_rate_hz = 8000; 19533721a235SMatthias Ringwald } 19543721a235SMatthias Ringwald uint8_t clock_direction = 0; // master 19553721a235SMatthias Ringwald uint16_t frame_sync_duty_cycle = 0; // i2s with 50% 19563721a235SMatthias Ringwald uint8_t frame_sync_edge = 1; // rising edge 19573721a235SMatthias Ringwald uint8_t frame_sync_polarity = 0; // active high 19583721a235SMatthias Ringwald uint8_t reserved = 0; 19593721a235SMatthias Ringwald uint16_t size = 16; 19603721a235SMatthias Ringwald uint16_t chan_1_offset = 1; 19613721a235SMatthias Ringwald uint16_t chan_2_offset = chan_1_offset + size; 19623721a235SMatthias Ringwald uint8_t out_edge = 1; // rising 19633721a235SMatthias Ringwald uint8_t in_edge = 0; // falling 19643721a235SMatthias Ringwald hci_send_cmd(&hci_ti_write_codec_config, clock_rate_khz, clock_direction, sample_rate_hz, frame_sync_duty_cycle, 19653721a235SMatthias Ringwald frame_sync_edge, frame_sync_polarity, reserved, 19663721a235SMatthias Ringwald size, chan_1_offset, out_edge, size, chan_1_offset, in_edge, reserved, 19673721a235SMatthias Ringwald size, chan_2_offset, out_edge, size, chan_2_offset, in_edge, reserved); 19683721a235SMatthias Ringwald } 19693721a235SMatthias Ringwald #endif 19703721a235SMatthias Ringwald 1971689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 1972689d4323SMatthias Ringwald void hfp_bcm_prepare_for_sco(hfp_connection_t * hfp_connection){ 1973689d4323SMatthias Ringwald hfp_connection->bcm_send_write_i2spcm_interface_param = true; 1974689d4323SMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 1975689d4323SMatthias Ringwald hfp_connection->bcm_send_enable_wbs = true; 1976689d4323SMatthias Ringwald } 1977689d4323SMatthias Ringwald } 1978689d4323SMatthias Ringwald void hfp_bcm_write_i2spcm_interface_param(hfp_connection_t * hfp_connection){ 1979689d4323SMatthias Ringwald uint8_t sample_rate = (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? 1 : 0; 19801d2bbd54SMatthias Ringwald // i2s enable, master, 8/16 kHz, 512 kHz 19811d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_i2spcm_interface_paramhci_bcm_write_i2spcm_interface_param, 1, 1, sample_rate, 2); 1982689d4323SMatthias Ringwald } 1983689d4323SMatthias Ringwald #endif 1984689d4323SMatthias Ringwald 19851b005648SMatthias Ringwald void hfp_set_hf_callback(btstack_packet_handler_t callback){ 19861b005648SMatthias Ringwald hfp_hf_callback = callback; 19871b005648SMatthias Ringwald } 19881b005648SMatthias Ringwald 19891b005648SMatthias Ringwald void hfp_set_ag_callback(btstack_packet_handler_t callback){ 19901b005648SMatthias Ringwald hfp_ag_callback = callback; 19911b005648SMatthias Ringwald } 19921b005648SMatthias Ringwald 1993ca59be51SMatthias Ringwald void hfp_set_ag_rfcomm_packet_handler(btstack_packet_handler_t handler){ 1994ca59be51SMatthias Ringwald hfp_ag_rfcomm_packet_handler = handler; 1995ca59be51SMatthias Ringwald } 19961b005648SMatthias Ringwald 1997ca59be51SMatthias Ringwald void hfp_set_hf_rfcomm_packet_handler(btstack_packet_handler_t handler){ 1998ca59be51SMatthias Ringwald hfp_hf_rfcomm_packet_handler = handler; 1999d68dcce1SMatthias Ringwald } 2000d68dcce1SMatthias Ringwald 2001520c92d5SMatthias Ringwald void hfp_init(void){ 2002352a0504SMatthias Ringwald hfp_allowed_sco_packet_types = SCO_PACKET_TYPES_ALL; 2003991c26beSMatthias Ringwald } 2004991c26beSMatthias Ringwald 200520b2edb6SMatthias Ringwald void hfp_deinit(void){ 2006aeb0f0feSMatthias Ringwald hfp_allowed_sco_packet_types = 0; 200720b2edb6SMatthias Ringwald hfp_connections = NULL; 200820b2edb6SMatthias Ringwald hfp_hf_callback = NULL; 200920b2edb6SMatthias Ringwald hfp_ag_callback = NULL; 201020b2edb6SMatthias Ringwald hfp_hf_rfcomm_packet_handler = NULL; 201120b2edb6SMatthias Ringwald hfp_ag_rfcomm_packet_handler = NULL; 2012aeb0f0feSMatthias Ringwald hfp_sco_establishment_active = NULL; 2013471dea41SMatthias Ringwald hfp_custom_commands_ag = NULL; 2014aeb0f0feSMatthias Ringwald (void) memset(&hfp_sdp_query_context, 0, sizeof(hfp_sdp_query_context_t)); 2015aeb0f0feSMatthias Ringwald (void) memset(&hfp_sdp_query_request, 0, sizeof(btstack_context_callback_registration_t)); 201620b2edb6SMatthias Ringwald } 201720b2edb6SMatthias Ringwald 2018991c26beSMatthias Ringwald void hfp_set_sco_packet_types(uint16_t packet_types){ 201901e5b727SMatthias Ringwald hfp_allowed_sco_packet_types = packet_types; 2020991c26beSMatthias Ringwald } 2021991c26beSMatthias Ringwald 2022991c26beSMatthias Ringwald uint16_t hfp_get_sco_packet_types(void){ 202301e5b727SMatthias Ringwald return hfp_allowed_sco_packet_types; 2024520c92d5SMatthias Ringwald } 2025186dd3d2SMatthias Ringwald 2026e453c1d9SMatthias 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){ 2027e453c1d9SMatthias Ringwald int8_t setting = (int8_t) current_setting; 2028e453c1d9SMatthias Ringwald bool can_use_eSCO = local_eSCO_supported && remote_eSCO_supported; 2029e453c1d9SMatthias Ringwald while (setting > 0){ 2030e453c1d9SMatthias Ringwald setting--; 2031e453c1d9SMatthias Ringwald // skip if eSCO required but not available 2032e453c1d9SMatthias Ringwald if (hfp_link_settings[setting].eSCO && !can_use_eSCO) continue; 2033e453c1d9SMatthias Ringwald // skip if S4 but not supported 2034e453c1d9SMatthias Ringwald if ((setting == (int8_t) HFP_LINK_SETTINGS_S4) && !eSCO_S4_supported) continue; 2035e453c1d9SMatthias Ringwald // skip wrong codec 2036e453c1d9SMatthias Ringwald if ( hfp_link_settings[setting].codec != negotiated_codec) continue; 203701e5b727SMatthias Ringwald // skip disabled packet types 2038352a0504SMatthias Ringwald uint16_t required_packet_types = hfp_link_settings[setting].packet_types; 2039352a0504SMatthias Ringwald uint16_t allowed_packet_types = hfp_allowed_sco_packet_types; 2040352a0504SMatthias Ringwald if ((required_packet_types & allowed_packet_types) == 0) continue; 204101e5b727SMatthias Ringwald 2042e453c1d9SMatthias Ringwald // found matching setting 2043e453c1d9SMatthias Ringwald return (hfp_link_settings_t) setting; 2044afac2a04SMilanka Ringwald } 2045e453c1d9SMatthias Ringwald return HFP_LINK_SETTINGS_NONE; 2046afac2a04SMilanka Ringwald } 2047e453c1d9SMatthias Ringwald 20482b7abbfbSMatthias 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){ 2049e453c1d9SMatthias Ringwald bool local_eSCO_supported = hci_extended_sco_link_supported(); 2050e453c1d9SMatthias Ringwald bool remote_eSCO_supported = hci_remote_esco_supported(hfp_connection->acl_handle); 2051e453c1d9SMatthias Ringwald uint8_t negotiated_codec = hfp_connection->negotiated_codec; 20522b7abbfbSMatthias Ringwald return hfp_next_link_setting(current_setting, local_eSCO_supported, remote_eSCO_supported, eSCO_S4_supported, negotiated_codec); 20532b7abbfbSMatthias Ringwald } 20542b7abbfbSMatthias Ringwald 20552b7abbfbSMatthias Ringwald void hfp_init_link_settings(hfp_connection_t * hfp_connection, uint8_t eSCO_S4_supported){ 2056e453c1d9SMatthias Ringwald // get highest possible link setting 20572b7abbfbSMatthias Ringwald hfp_connection->link_setting = hfp_next_link_setting_for_connection(HFP_LINK_SETTINGS_NONE, hfp_connection, eSCO_S4_supported); 2058afac2a04SMilanka Ringwald log_info("hfp_init_link_settings: %u", hfp_connection->link_setting); 2059afac2a04SMilanka Ringwald } 2060afac2a04SMilanka Ringwald 2061186dd3d2SMatthias Ringwald #define HFP_HF_RX_DEBUG_PRINT_LINE 80 2062186dd3d2SMatthias Ringwald 2063186dd3d2SMatthias Ringwald void hfp_log_rfcomm_message(const char * tag, uint8_t * packet, uint16_t size){ 2064186dd3d2SMatthias Ringwald #ifdef ENABLE_LOG_INFO 2065186dd3d2SMatthias Ringwald // encode \n\r 2066186dd3d2SMatthias Ringwald char printable[HFP_HF_RX_DEBUG_PRINT_LINE+2]; 206715a27967SMatthias Ringwald int i = 0; 2068186dd3d2SMatthias Ringwald int pos; 206915a27967SMatthias Ringwald for (pos=0 ; (pos < size) && (i < (HFP_HF_RX_DEBUG_PRINT_LINE - 3)) ; pos++){ 2070186dd3d2SMatthias Ringwald switch (packet[pos]){ 2071186dd3d2SMatthias Ringwald case '\n': 2072186dd3d2SMatthias Ringwald printable[i++] = '\\'; 2073186dd3d2SMatthias Ringwald printable[i++] = 'n'; 2074186dd3d2SMatthias Ringwald break; 2075186dd3d2SMatthias Ringwald case '\r': 2076186dd3d2SMatthias Ringwald printable[i++] = '\\'; 2077186dd3d2SMatthias Ringwald printable[i++] = 'r'; 2078186dd3d2SMatthias Ringwald break; 2079186dd3d2SMatthias Ringwald default: 2080186dd3d2SMatthias Ringwald printable[i++] = packet[pos]; 2081186dd3d2SMatthias Ringwald break; 2082186dd3d2SMatthias Ringwald } 2083186dd3d2SMatthias Ringwald } 2084186dd3d2SMatthias Ringwald printable[i] = 0; 2085186dd3d2SMatthias Ringwald log_info("%s: '%s'", tag, printable); 2086186dd3d2SMatthias Ringwald #endif 2087186dd3d2SMatthias Ringwald } 2088471dea41SMatthias Ringwald 2089471dea41SMatthias Ringwald void hfp_register_custom_ag_command(hfp_custom_at_command_t * custom_at_command){ 2090471dea41SMatthias Ringwald btstack_linked_list_add(&hfp_custom_commands_ag, (btstack_linked_item_t *) custom_at_command); 2091471dea41SMatthias Ringwald } 2092