13deb3ec6SMatthias Ringwald /* 23deb3ec6SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 33deb3ec6SMatthias Ringwald * 43deb3ec6SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 53deb3ec6SMatthias Ringwald * modification, are permitted provided that the following conditions 63deb3ec6SMatthias Ringwald * are met: 73deb3ec6SMatthias Ringwald * 83deb3ec6SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 93deb3ec6SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 103deb3ec6SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 113deb3ec6SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 123deb3ec6SMatthias Ringwald * documentation and/or other materials provided with the distribution. 133deb3ec6SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 143deb3ec6SMatthias Ringwald * contributors may be used to endorse or promote products derived 153deb3ec6SMatthias Ringwald * from this software without specific prior written permission. 163deb3ec6SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 173deb3ec6SMatthias Ringwald * personal benefit and not for any commercial purpose or for 183deb3ec6SMatthias Ringwald * monetary gain. 193deb3ec6SMatthias Ringwald * 203deb3ec6SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 213deb3ec6SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 223deb3ec6SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 233deb3ec6SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 243deb3ec6SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 253deb3ec6SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 263deb3ec6SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 273deb3ec6SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 283deb3ec6SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 293deb3ec6SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 303deb3ec6SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 313deb3ec6SMatthias Ringwald * SUCH DAMAGE. 323deb3ec6SMatthias Ringwald * 333deb3ec6SMatthias Ringwald * Please inquire about commercial licensing options at 343deb3ec6SMatthias Ringwald * [email protected] 353deb3ec6SMatthias Ringwald * 363deb3ec6SMatthias Ringwald */ 37ab2c6ae4SMatthias Ringwald 38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "hfp.c" 393deb3ec6SMatthias Ringwald 403deb3ec6SMatthias Ringwald 417907f069SMatthias Ringwald #include "btstack_config.h" 423deb3ec6SMatthias Ringwald 433deb3ec6SMatthias Ringwald #include <stdint.h> 443deb3ec6SMatthias Ringwald #include <string.h> 453deb3ec6SMatthias Ringwald #include <inttypes.h> 463deb3ec6SMatthias Ringwald 47235946f1SMatthias Ringwald #include "bluetooth_sdp.h" 48023f2764SMatthias Ringwald #include "btstack_debug.h" 4959c6af15SMatthias Ringwald #include "btstack_event.h" 5059c6af15SMatthias Ringwald #include "btstack_memory.h" 5159c6af15SMatthias Ringwald #include "btstack_run_loop.h" 52efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h" 53746ccb7eSMatthias Ringwald #include "classic/sdp_server.h" 54023f2764SMatthias Ringwald #include "classic/sdp_util.h" 551d9c9c90SMilanka Ringwald #include "classic/sdp_client.h" 5659c6af15SMatthias Ringwald #include "hci.h" 5759c6af15SMatthias Ringwald #include "hci_cmd.h" 5859c6af15SMatthias Ringwald #include "hci_dump.h" 593deb3ec6SMatthias Ringwald 603721a235SMatthias Ringwald #if defined(ENABLE_CC256X_ASSISTED_HFP) && !defined(ENABLE_SCO_OVER_PCM) 613721a235SMatthias Ringwald #error "Assisted HFP is only possible over PCM/I2S. Please add define: ENABLE_SCO_OVER_PCM" 623721a235SMatthias Ringwald #endif 633721a235SMatthias Ringwald 64689d4323SMatthias Ringwald #if defined(ENABLE_BCM_PCM_WBS) && !defined(ENABLE_SCO_OVER_PCM) 65689d4323SMatthias Ringwald #error "WBS for PCM is only possible over PCM/I2S. Please add define: ENABLE_SCO_OVER_PCM" 66689d4323SMatthias Ringwald #endif 67689d4323SMatthias Ringwald 683deb3ec6SMatthias Ringwald #define HFP_HF_FEATURES_SIZE 10 693deb3ec6SMatthias Ringwald #define HFP_AG_FEATURES_SIZE 12 703deb3ec6SMatthias Ringwald 7120b2edb6SMatthias Ringwald typedef struct { 7220b2edb6SMatthias Ringwald hfp_role_t local_role; 7320b2edb6SMatthias Ringwald bd_addr_t remote_address; 7420b2edb6SMatthias Ringwald } hfp_sdp_query_context_t; 7520b2edb6SMatthias Ringwald 7620b2edb6SMatthias Ringwald // globals 7720b2edb6SMatthias Ringwald 7820b2edb6SMatthias Ringwald static hfp_sdp_query_context_t sdp_query_context; 7920b2edb6SMatthias Ringwald static btstack_context_callback_registration_t hfp_handle_sdp_client_query_request; 8020b2edb6SMatthias Ringwald 8120b2edb6SMatthias Ringwald static btstack_linked_list_t hfp_connections ; 8220b2edb6SMatthias Ringwald 8320b2edb6SMatthias Ringwald static btstack_packet_handler_t hfp_hf_callback; 8420b2edb6SMatthias Ringwald static btstack_packet_handler_t hfp_ag_callback; 8520b2edb6SMatthias Ringwald 8620b2edb6SMatthias Ringwald static btstack_packet_handler_t hfp_hf_rfcomm_packet_handler; 8720b2edb6SMatthias Ringwald static btstack_packet_handler_t hfp_ag_rfcomm_packet_handler; 8820b2edb6SMatthias Ringwald 8920b2edb6SMatthias Ringwald static hfp_connection_t * sco_establishment_active; 9020b2edb6SMatthias Ringwald 9120b2edb6SMatthias Ringwald static uint16_t hfp_allowed_sco_packet_types; 9220b2edb6SMatthias Ringwald 9320b2edb6SMatthias Ringwald // prototypes 9420b2edb6SMatthias 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); 9520b2edb6SMatthias Ringwald static void parse_sequence(hfp_connection_t * context); 9620b2edb6SMatthias Ringwald 973deb3ec6SMatthias Ringwald 983deb3ec6SMatthias Ringwald static const char * hfp_hf_features[] = { 993deb3ec6SMatthias Ringwald "EC and/or NR function", 1003deb3ec6SMatthias Ringwald "Three-way calling", 1013deb3ec6SMatthias Ringwald "CLI presentation capability", 1023deb3ec6SMatthias Ringwald "Voice recognition activation", 1033deb3ec6SMatthias Ringwald "Remote volume control", 1043deb3ec6SMatthias Ringwald 1053deb3ec6SMatthias Ringwald "Enhanced call status", 1063deb3ec6SMatthias Ringwald "Enhanced call control", 1073deb3ec6SMatthias Ringwald 1083deb3ec6SMatthias Ringwald "Codec negotiation", 1093deb3ec6SMatthias Ringwald 1103deb3ec6SMatthias Ringwald "HF Indicators", 1113deb3ec6SMatthias Ringwald "eSCO S4 (and T2) Settings Supported", 1123deb3ec6SMatthias Ringwald "Reserved for future definition" 1133deb3ec6SMatthias Ringwald }; 1143deb3ec6SMatthias Ringwald 1153deb3ec6SMatthias Ringwald static const char * hfp_ag_features[] = { 1163deb3ec6SMatthias Ringwald "Three-way calling", 1173deb3ec6SMatthias Ringwald "EC and/or NR function", 1183deb3ec6SMatthias Ringwald "Voice recognition function", 1193deb3ec6SMatthias Ringwald "In-band ring tone capability", 1203deb3ec6SMatthias Ringwald "Attach a number to a voice tag", 1213deb3ec6SMatthias Ringwald "Ability to reject a call", 1223deb3ec6SMatthias Ringwald "Enhanced call status", 1233deb3ec6SMatthias Ringwald "Enhanced call control", 1243deb3ec6SMatthias Ringwald "Extended Error Result Codes", 1253deb3ec6SMatthias Ringwald "Codec negotiation", 1263deb3ec6SMatthias Ringwald "HF Indicators", 1273deb3ec6SMatthias Ringwald "eSCO S4 (and T2) Settings Supported", 1283deb3ec6SMatthias Ringwald "Reserved for future definition" 1293deb3ec6SMatthias Ringwald }; 1303deb3ec6SMatthias Ringwald 1310aee97efSMilanka Ringwald static const char * hfp_enhanced_call_dir[] = { 1320aee97efSMilanka Ringwald "outgoing", 1330aee97efSMilanka Ringwald "incoming" 1340aee97efSMilanka Ringwald }; 1350aee97efSMilanka Ringwald 1360aee97efSMilanka Ringwald static const char * hfp_enhanced_call_status[] = { 1370aee97efSMilanka Ringwald "active", 1380aee97efSMilanka Ringwald "held", 1390aee97efSMilanka Ringwald "outgoing dialing", 1400aee97efSMilanka Ringwald "outgoing alerting", 1410aee97efSMilanka Ringwald "incoming", 1420aee97efSMilanka Ringwald "incoming waiting", 1430aee97efSMilanka Ringwald "call held by response and hold" 1440aee97efSMilanka Ringwald }; 1450aee97efSMilanka Ringwald 1460aee97efSMilanka Ringwald static const char * hfp_enhanced_call_mode[] = { 1470aee97efSMilanka Ringwald "voice", 1480aee97efSMilanka Ringwald "data", 1490aee97efSMilanka Ringwald "fax" 1500aee97efSMilanka Ringwald }; 1510aee97efSMilanka Ringwald 1520aee97efSMilanka Ringwald static const char * hfp_enhanced_call_mpty[] = { 1530aee97efSMilanka Ringwald "not a conference call", 1540aee97efSMilanka Ringwald "conference call" 1550aee97efSMilanka Ringwald }; 1560aee97efSMilanka Ringwald 1570aee97efSMilanka Ringwald const char * hfp_enhanced_call_dir2str(uint16_t index){ 1580aee97efSMilanka Ringwald if (index <= HFP_ENHANCED_CALL_DIR_INCOMING) return hfp_enhanced_call_dir[index]; 1590aee97efSMilanka Ringwald return "not defined"; 1600aee97efSMilanka Ringwald } 1610aee97efSMilanka Ringwald 1620aee97efSMilanka Ringwald const char * hfp_enhanced_call_status2str(uint16_t index){ 1630aee97efSMilanka Ringwald if (index <= HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD) return hfp_enhanced_call_status[index]; 1640aee97efSMilanka Ringwald return "not defined"; 1650aee97efSMilanka Ringwald } 1660aee97efSMilanka Ringwald 1670aee97efSMilanka Ringwald const char * hfp_enhanced_call_mode2str(uint16_t index){ 1680aee97efSMilanka Ringwald if (index <= HFP_ENHANCED_CALL_MODE_FAX) return hfp_enhanced_call_mode[index]; 1690aee97efSMilanka Ringwald return "not defined"; 1700aee97efSMilanka Ringwald } 1710aee97efSMilanka Ringwald 1720aee97efSMilanka Ringwald const char * hfp_enhanced_call_mpty2str(uint16_t index){ 1730aee97efSMilanka Ringwald if (index <= HFP_ENHANCED_CALL_MPTY_CONFERENCE_CALL) return hfp_enhanced_call_mpty[index]; 1740aee97efSMilanka Ringwald return "not defined"; 1750aee97efSMilanka Ringwald } 1760aee97efSMilanka Ringwald 17725789943SMilanka Ringwald static uint16_t hfp_parse_indicator_index(hfp_connection_t * hfp_connection){ 17825789943SMilanka Ringwald uint16_t index = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 17925789943SMilanka Ringwald 18025789943SMilanka Ringwald if (index > HFP_MAX_NUM_INDICATORS){ 18125789943SMilanka Ringwald log_info("ignoring invalid indicator index bigger then HFP_MAX_NUM_INDICATORS"); 18225789943SMilanka Ringwald return HFP_MAX_NUM_INDICATORS - 1; 18325789943SMilanka Ringwald } 18425789943SMilanka Ringwald 18525789943SMilanka Ringwald // indicator index enumeration starts with 1, we substract 1 to store in array with starting index 0 18625789943SMilanka Ringwald if (index > 0){ 18725789943SMilanka Ringwald index -= 1; 18825789943SMilanka Ringwald } else { 18925789943SMilanka Ringwald log_info("ignoring invalid indicator index 0"); 19025789943SMilanka Ringwald return 0; 19125789943SMilanka Ringwald } 19225789943SMilanka Ringwald return index; 19325789943SMilanka Ringwald } 19425789943SMilanka Ringwald 19525789943SMilanka Ringwald static void hfp_next_indicators_index(hfp_connection_t * hfp_connection){ 19625789943SMilanka Ringwald if (hfp_connection->parser_item_index < HFP_MAX_NUM_INDICATORS - 1){ 19725789943SMilanka Ringwald hfp_connection->parser_item_index++; 19825789943SMilanka Ringwald } else { 19925789943SMilanka Ringwald log_info("Ignoring additional indicator"); 20025789943SMilanka Ringwald } 20125789943SMilanka Ringwald } 20225789943SMilanka Ringwald 20325789943SMilanka Ringwald static void hfp_next_codec_index(hfp_connection_t * hfp_connection){ 20425789943SMilanka Ringwald if (hfp_connection->parser_item_index < HFP_MAX_NUM_CODECS - 1){ 20525789943SMilanka Ringwald hfp_connection->parser_item_index++; 20625789943SMilanka Ringwald } else { 20725789943SMilanka Ringwald log_info("Ignoring additional codec index"); 20825789943SMilanka Ringwald } 20925789943SMilanka Ringwald } 21025789943SMilanka Ringwald 211eed67a37SMilanka Ringwald static void hfp_next_remote_call_services_index(hfp_connection_t * hfp_connection){ 212eed67a37SMilanka Ringwald if (hfp_connection->remote_call_services_index < HFP_MAX_NUM_CALL_SERVICES - 1){ 213eed67a37SMilanka Ringwald hfp_connection->remote_call_services_index++; 214eed67a37SMilanka Ringwald } else { 215eed67a37SMilanka Ringwald log_info("Ignoring additional remote_call_services"); 216eed67a37SMilanka Ringwald } 217eed67a37SMilanka Ringwald } 21825789943SMilanka Ringwald 2193deb3ec6SMatthias Ringwald const char * hfp_hf_feature(int index){ 2203deb3ec6SMatthias Ringwald if (index > HFP_HF_FEATURES_SIZE){ 2213deb3ec6SMatthias Ringwald return hfp_hf_features[HFP_HF_FEATURES_SIZE]; 2223deb3ec6SMatthias Ringwald } 2233deb3ec6SMatthias Ringwald return hfp_hf_features[index]; 2243deb3ec6SMatthias Ringwald } 2253deb3ec6SMatthias Ringwald 2263deb3ec6SMatthias Ringwald const char * hfp_ag_feature(int index){ 2273deb3ec6SMatthias Ringwald if (index > HFP_AG_FEATURES_SIZE){ 2283deb3ec6SMatthias Ringwald return hfp_ag_features[HFP_AG_FEATURES_SIZE]; 2293deb3ec6SMatthias Ringwald } 2303deb3ec6SMatthias Ringwald return hfp_ag_features[index]; 2313deb3ec6SMatthias Ringwald } 2323deb3ec6SMatthias Ringwald 2333deb3ec6SMatthias Ringwald int send_str_over_rfcomm(uint16_t cid, char * command){ 2343deb3ec6SMatthias Ringwald if (!rfcomm_can_send_packet_now(cid)) return 1; 23574386ee0SMatthias Ringwald log_info("HFP_TX %s", command); 23628190c0bSMatthias Ringwald int err = rfcomm_send(cid, (uint8_t*) command, strlen(command)); 2373deb3ec6SMatthias Ringwald if (err){ 23828190c0bSMatthias Ringwald log_error("rfcomm_send -> error 0x%02x \n", err); 2393deb3ec6SMatthias Ringwald } 240e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES 241e43d1938SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(cid); 242e43d1938SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_SENT, command); 243e43d1938SMatthias Ringwald #endif 2443deb3ec6SMatthias Ringwald return 1; 2453deb3ec6SMatthias Ringwald } 2463deb3ec6SMatthias Ringwald 2476a7f44bdSMilanka Ringwald int hfp_supports_codec(uint8_t codec, int codecs_nr, uint8_t * codecs){ 248e8d1dc0dSMatthias Ringwald 249e8d1dc0dSMatthias Ringwald // mSBC requires support for eSCO connections 250c1ab6cc1SMatthias Ringwald if ((codec == HFP_CODEC_MSBC) && !hci_extended_sco_link_supported()) return 0; 251e8d1dc0dSMatthias Ringwald 252df327ff3SMilanka Ringwald int i; 253df327ff3SMilanka Ringwald for (i = 0; i < codecs_nr; i++){ 254e8d1dc0dSMatthias Ringwald if (codecs[i] != codec) continue; 255e8d1dc0dSMatthias Ringwald return 1; 256df327ff3SMilanka Ringwald } 257df327ff3SMilanka Ringwald return 0; 258df327ff3SMilanka Ringwald } 259df327ff3SMilanka Ringwald 260d715cf51SMatthias Ringwald void hfp_hf_drop_mSBC_if_eSCO_not_supported(uint8_t * codecs, uint8_t * codecs_nr){ 261d715cf51SMatthias Ringwald if (hci_extended_sco_link_supported()) return; 262d715cf51SMatthias Ringwald uint8_t tmp_codecs[HFP_MAX_NUM_CODECS]; 263d715cf51SMatthias Ringwald int i; 264d715cf51SMatthias Ringwald int tmp_codec_nr = 0; 265d715cf51SMatthias Ringwald for (i=0; i < *codecs_nr; i++){ 266d715cf51SMatthias Ringwald if (codecs[i] == HFP_CODEC_MSBC) continue; 267d715cf51SMatthias Ringwald tmp_codecs[tmp_codec_nr++] = codecs[i]; 268d715cf51SMatthias Ringwald } 269d715cf51SMatthias Ringwald *codecs_nr = tmp_codec_nr; 2706535961aSMatthias Ringwald (void)memcpy(codecs, tmp_codecs, tmp_codec_nr); 271d715cf51SMatthias Ringwald } 272d715cf51SMatthias Ringwald 2733deb3ec6SMatthias Ringwald // UTILS 2743deb3ec6SMatthias Ringwald int get_bit(uint16_t bitmap, int position){ 2753deb3ec6SMatthias Ringwald return (bitmap >> position) & 1; 2763deb3ec6SMatthias Ringwald } 2773deb3ec6SMatthias Ringwald 2783deb3ec6SMatthias Ringwald int store_bit(uint32_t bitmap, int position, uint8_t value){ 2793deb3ec6SMatthias Ringwald if (value){ 2803deb3ec6SMatthias Ringwald bitmap |= 1 << position; 2813deb3ec6SMatthias Ringwald } else { 2823deb3ec6SMatthias Ringwald bitmap &= ~ (1 << position); 2833deb3ec6SMatthias Ringwald } 2843deb3ec6SMatthias Ringwald return bitmap; 2853deb3ec6SMatthias Ringwald } 2863deb3ec6SMatthias Ringwald 2873deb3ec6SMatthias Ringwald int join(char * buffer, int buffer_size, uint8_t * values, int values_nr){ 288c1ab6cc1SMatthias Ringwald if (buffer_size < (values_nr * 3)) return 0; 2893deb3ec6SMatthias Ringwald int i; 2903deb3ec6SMatthias Ringwald int offset = 0; 291c1ab6cc1SMatthias Ringwald for (i = 0; i < (values_nr-1); i++) { 2923deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", values[i]); // puts string into buffer 2933deb3ec6SMatthias Ringwald } 2943deb3ec6SMatthias Ringwald if (i<values_nr){ 2953deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d", values[i]); 2963deb3ec6SMatthias Ringwald } 2973deb3ec6SMatthias Ringwald return offset; 2983deb3ec6SMatthias Ringwald } 2993deb3ec6SMatthias Ringwald 3003deb3ec6SMatthias Ringwald int join_bitmap(char * buffer, int buffer_size, uint32_t values, int values_nr){ 301c1ab6cc1SMatthias Ringwald if (buffer_size < (values_nr * 3)) return 0; 3023deb3ec6SMatthias Ringwald 3033deb3ec6SMatthias Ringwald int i; 3043deb3ec6SMatthias Ringwald int offset = 0; 305c1ab6cc1SMatthias Ringwald for (i = 0; i < (values_nr-1); i++) { 3063deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", get_bit(values,i)); // puts string into buffer 3073deb3ec6SMatthias Ringwald } 3083deb3ec6SMatthias Ringwald 3093deb3ec6SMatthias Ringwald if (i<values_nr){ 3103deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d", get_bit(values,i)); 3113deb3ec6SMatthias Ringwald } 3123deb3ec6SMatthias Ringwald return offset; 3133deb3ec6SMatthias Ringwald } 3143deb3ec6SMatthias Ringwald 315ca59be51SMatthias Ringwald static void hfp_emit_event_for_context(hfp_connection_t * hfp_connection, uint8_t * packet, uint16_t size){ 316ca59be51SMatthias Ringwald if (!hfp_connection) return; 317ca59be51SMatthias Ringwald btstack_packet_handler_t callback = NULL; 318ca59be51SMatthias Ringwald switch (hfp_connection->local_role){ 319ca59be51SMatthias Ringwald case HFP_ROLE_HF: 320ca59be51SMatthias Ringwald callback = hfp_hf_callback; 321671f1aa2SMatthias Ringwald break; 322ca59be51SMatthias Ringwald case HFP_ROLE_AG: 323ca59be51SMatthias Ringwald callback = hfp_ag_callback; 324671f1aa2SMatthias Ringwald break; 325ca59be51SMatthias Ringwald default: 326ca59be51SMatthias Ringwald return; 327ca59be51SMatthias Ringwald } 328ca59be51SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, packet, size); 329ca59be51SMatthias Ringwald } 330ca59be51SMatthias Ringwald 331ca59be51SMatthias Ringwald void hfp_emit_simple_event(hfp_connection_t * hfp_connection, uint8_t event_subtype){ 3327d81706fSMatthias Ringwald hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 333d703d377SMatthias Ringwald uint8_t event[5]; 334a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 335a0ffb263SMatthias Ringwald event[1] = sizeof(event) - 2; 336a0ffb263SMatthias Ringwald event[2] = event_subtype; 3377d81706fSMatthias Ringwald little_endian_store_16(event, 3, acl_handle); 338ca59be51SMatthias Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 339a0ffb263SMatthias Ringwald } 340a0ffb263SMatthias Ringwald 341ca59be51SMatthias Ringwald void hfp_emit_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, uint8_t value){ 3427d81706fSMatthias Ringwald hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 343d703d377SMatthias Ringwald uint8_t event[6]; 3443deb3ec6SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 3453deb3ec6SMatthias Ringwald event[1] = sizeof(event) - 2; 3463deb3ec6SMatthias Ringwald event[2] = event_subtype; 3477d81706fSMatthias Ringwald little_endian_store_16(event, 3, acl_handle); 348d703d377SMatthias Ringwald event[5] = value; // status 0 == OK 349ca59be51SMatthias Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 3503deb3ec6SMatthias Ringwald } 3513deb3ec6SMatthias Ringwald 352ca59be51SMatthias Ringwald void hfp_emit_slc_connection_event(hfp_connection_t * hfp_connection, uint8_t status, hci_con_handle_t con_handle, bd_addr_t addr){ 3537d81706fSMatthias Ringwald btstack_assert(hfp_connection != NULL); 3548901a7c4SMatthias Ringwald uint8_t event[12]; 3556a7f44bdSMilanka Ringwald int pos = 0; 3566a7f44bdSMilanka Ringwald event[pos++] = HCI_EVENT_HFP_META; 3576a7f44bdSMilanka Ringwald event[pos++] = sizeof(event) - 2; 358d0c4aea6SMilanka Ringwald event[pos++] = HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 3596a7f44bdSMilanka Ringwald event[pos++] = status; // status 0 == OK 3606a7f44bdSMilanka Ringwald little_endian_store_16(event, pos, con_handle); 3616a7f44bdSMilanka Ringwald pos += 2; 3626a7f44bdSMilanka Ringwald reverse_bd_addr(addr,&event[pos]); 3636a7f44bdSMilanka Ringwald pos += 6; 364ca59be51SMatthias Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 365a0653c3bSMilanka Ringwald } 366a0653c3bSMilanka Ringwald 367d703d377SMatthias Ringwald static void hfp_emit_audio_connection_released(hfp_connection_t * hfp_connection, hci_con_handle_t sco_handle){ 3687d81706fSMatthias Ringwald btstack_assert(hfp_connection != NULL); 369d703d377SMatthias Ringwald uint8_t event[7]; 3705441d52fSMatthias Ringwald int pos = 0; 3715441d52fSMatthias Ringwald event[pos++] = HCI_EVENT_HFP_META; 3725441d52fSMatthias Ringwald event[pos++] = sizeof(event) - 2; 3735441d52fSMatthias Ringwald event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED; 374d703d377SMatthias Ringwald little_endian_store_16(event, pos, hfp_connection->acl_handle); 375d703d377SMatthias Ringwald pos += 2; 376d703d377SMatthias Ringwald little_endian_store_16(event, pos, sco_handle); 377d703d377SMatthias Ringwald pos += 2; 3785441d52fSMatthias Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 3795441d52fSMatthias Ringwald } 3805441d52fSMatthias Ringwald 381d703d377SMatthias 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){ 3827d81706fSMatthias Ringwald btstack_assert(hfp_connection != NULL); 383d703d377SMatthias Ringwald uint8_t event[15]; 384d0c4aea6SMilanka Ringwald int pos = 0; 385d0c4aea6SMilanka Ringwald event[pos++] = HCI_EVENT_HFP_META; 386d0c4aea6SMilanka Ringwald event[pos++] = sizeof(event) - 2; 387d0c4aea6SMilanka Ringwald event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED; 388d703d377SMatthias Ringwald little_endian_store_16(event, pos, hfp_connection->acl_handle); 389d703d377SMatthias Ringwald pos += 2; 390d0c4aea6SMilanka Ringwald event[pos++] = status; // status 0 == OK 391d703d377SMatthias Ringwald little_endian_store_16(event, pos, sco_handle); 392d0c4aea6SMilanka Ringwald pos += 2; 393d0c4aea6SMilanka Ringwald reverse_bd_addr(addr,&event[pos]); 394d0c4aea6SMilanka Ringwald pos += 6; 395d0c4aea6SMilanka Ringwald event[pos++] = negotiated_codec; 396ca59be51SMatthias Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 397d0c4aea6SMilanka Ringwald } 398d0c4aea6SMilanka Ringwald 399ca59be51SMatthias Ringwald void hfp_emit_string_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, const char * value){ 4007d81706fSMatthias Ringwald btstack_assert(hfp_connection != NULL); 4012c50df93SBjoern Hartmann #ifdef ENABLE_HFP_AT_MESSAGES 4022c50df93SBjoern Hartmann uint8_t event[256]; 4032c50df93SBjoern Hartmann #else 404c1797c7dSMatthias Ringwald uint8_t event[40]; 4052c50df93SBjoern Hartmann #endif 406aa4dd815SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 407aa4dd815SMatthias Ringwald event[1] = sizeof(event) - 2; 408aa4dd815SMatthias Ringwald event[2] = event_subtype; 409d703d377SMatthias Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle); 410d703d377SMatthias Ringwald uint16_t size = btstack_min(strlen(value), sizeof(event) - 6); 41113ee9fd7SMatthias Ringwald strncpy((char*)&event[5], value, size); 412d703d377SMatthias Ringwald event[5 + size] = 0; 413ca59be51SMatthias Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 414aa4dd815SMatthias Ringwald } 415aa4dd815SMatthias Ringwald 416*db3cdbd4SMilanka Ringwald static void hfp_emit_enhanced_voice_recognition_state(hfp_connection_t * hfp_connection){ 417*db3cdbd4SMilanka Ringwald btstack_assert(hfp_connection != NULL); 418*db3cdbd4SMilanka Ringwald uint8_t event[7]; 419*db3cdbd4SMilanka Ringwald int pos = 0; 420*db3cdbd4SMilanka Ringwald event[pos++] = HCI_EVENT_HFP_META; 421*db3cdbd4SMilanka Ringwald event[pos++] = sizeof(event) - 2; 422*db3cdbd4SMilanka Ringwald event[pos++] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_STATUS; 423*db3cdbd4SMilanka Ringwald little_endian_store_16(event, pos, hfp_connection->acl_handle); 424*db3cdbd4SMilanka Ringwald pos += 2; 425*db3cdbd4SMilanka Ringwald event[pos++] = hfp_connection->ag_vra_status; 426*db3cdbd4SMilanka Ringwald event[pos++] = hfp_connection->ag_vra_state; 427*db3cdbd4SMilanka Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 428*db3cdbd4SMilanka Ringwald } 429*db3cdbd4SMilanka Ringwald 430*db3cdbd4SMilanka Ringwald static void hfp_emit_enhanced_voice_recognition_text(hfp_connection_t * hfp_connection, uint16_t value_length, uint8_t * value){ 431*db3cdbd4SMilanka Ringwald btstack_assert(hfp_connection != NULL); 432*db3cdbd4SMilanka Ringwald uint8_t event[HFP_MAX_VR_TEXT_SIZE]; 433*db3cdbd4SMilanka Ringwald int pos = 0; 434*db3cdbd4SMilanka Ringwald event[pos++] = HCI_EVENT_HFP_META; 435*db3cdbd4SMilanka Ringwald event[pos++] = sizeof(event) - 2; 436*db3cdbd4SMilanka Ringwald event[pos++] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_TEXT; 437*db3cdbd4SMilanka Ringwald little_endian_store_16(event, pos, hfp_connection->acl_handle); 438*db3cdbd4SMilanka Ringwald pos += 2; 439*db3cdbd4SMilanka Ringwald little_endian_store_16(event, pos, hfp_connection->ag_text_id); 440*db3cdbd4SMilanka Ringwald pos += 2; 441*db3cdbd4SMilanka Ringwald event[pos++] = hfp_connection->ag_text_operation; 442*db3cdbd4SMilanka Ringwald event[pos++] = hfp_connection->ag_text_type; 443*db3cdbd4SMilanka Ringwald 444*db3cdbd4SMilanka Ringwald // length, zero ending 445*db3cdbd4SMilanka Ringwald uint16_t size = btstack_min(value_length, sizeof(event) - pos - 2 - 1); 446*db3cdbd4SMilanka Ringwald little_endian_store_16(event, pos, size+1); 447*db3cdbd4SMilanka Ringwald pos += 2; 448*db3cdbd4SMilanka Ringwald memcpy(&event[pos], value, size); 449*db3cdbd4SMilanka Ringwald event[pos + size] = 0; 450*db3cdbd4SMilanka Ringwald pos += size + 1; 451*db3cdbd4SMilanka Ringwald hfp_emit_event_for_context(hfp_connection, event, pos); 452*db3cdbd4SMilanka Ringwald } 453*db3cdbd4SMilanka Ringwald 4540cb5b971SMatthias Ringwald btstack_linked_list_t * hfp_get_connections(void){ 4558f2a52f4SMatthias Ringwald return (btstack_linked_list_t *) &hfp_connections; 4563deb3ec6SMatthias Ringwald } 4573deb3ec6SMatthias Ringwald 4583deb3ec6SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid){ 459665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 460665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 461665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 462a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 463a0ffb263SMatthias Ringwald if (hfp_connection->rfcomm_cid == cid){ 464a0ffb263SMatthias Ringwald return hfp_connection; 4653deb3ec6SMatthias Ringwald } 4663deb3ec6SMatthias Ringwald } 4673deb3ec6SMatthias Ringwald return NULL; 4683deb3ec6SMatthias Ringwald } 4693deb3ec6SMatthias Ringwald 470405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr, hfp_role_t hfp_role){ 471665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 472665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 473665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 474a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 4755df9dc78SMatthias Ringwald if ((memcmp(hfp_connection->remote_addr, bd_addr, 6) == 0) && (hfp_connection->local_role == hfp_role)) { 476a0ffb263SMatthias Ringwald return hfp_connection; 4773deb3ec6SMatthias Ringwald } 4783deb3ec6SMatthias Ringwald } 4793deb3ec6SMatthias Ringwald return NULL; 4803deb3ec6SMatthias Ringwald } 4813deb3ec6SMatthias Ringwald 482405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_sco_handle(uint16_t handle, hfp_role_t hfp_role){ 483665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 484665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 485665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 486a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 487c1ab6cc1SMatthias Ringwald if ((hfp_connection->sco_handle == handle) && (hfp_connection->local_role == hfp_role)){ 488a0ffb263SMatthias Ringwald return hfp_connection; 4893deb3ec6SMatthias Ringwald } 4903deb3ec6SMatthias Ringwald } 4913deb3ec6SMatthias Ringwald return NULL; 4923deb3ec6SMatthias Ringwald } 4933deb3ec6SMatthias Ringwald 494405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_acl_handle(uint16_t handle, hfp_role_t hfp_role){ 495d97d752dSMilanka Ringwald btstack_linked_list_iterator_t it; 496d97d752dSMilanka Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 497d97d752dSMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 498d97d752dSMilanka Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 499c1ab6cc1SMatthias Ringwald if ((hfp_connection->acl_handle == handle) && (hfp_connection->local_role == hfp_role)){ 500d97d752dSMilanka Ringwald return hfp_connection; 501d97d752dSMilanka Ringwald } 502d97d752dSMilanka Ringwald } 503d97d752dSMilanka Ringwald return NULL; 504d97d752dSMilanka Ringwald } 505d97d752dSMilanka Ringwald 506a0ffb263SMatthias Ringwald void hfp_reset_context_flags(hfp_connection_t * hfp_connection){ 507a0ffb263SMatthias Ringwald if (!hfp_connection) return; 508a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 509a0ffb263SMatthias Ringwald hfp_connection->send_error = 0; 5103deb3ec6SMatthias Ringwald 511dd533208SMatthias Ringwald hfp_connection->found_equal_sign = false; 5123deb3ec6SMatthias Ringwald 513a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 0; 514a0ffb263SMatthias Ringwald hfp_connection->operator_name_changed = 0; 5153deb3ec6SMatthias Ringwald 516a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = 0; 517a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 5183deb3ec6SMatthias Ringwald 519a0ffb263SMatthias Ringwald // establish codecs hfp_connection 520a0ffb263SMatthias Ringwald hfp_connection->suggested_codec = 0; 5217522e673SMatthias Ringwald hfp_connection->negotiated_codec = 0; 522a0ffb263SMatthias Ringwald hfp_connection->codec_confirmed = 0; 5233deb3ec6SMatthias Ringwald 524a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 525a0ffb263SMatthias Ringwald hfp_connection->call_waiting_notification_enabled = 0; 526a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 527a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 5283deb3ec6SMatthias Ringwald } 5293deb3ec6SMatthias Ringwald 530fffdd288SMatthias Ringwald static hfp_connection_t * create_hfp_connection_context(void){ 531a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = btstack_memory_hfp_connection_get(); 532a0ffb263SMatthias Ringwald if (!hfp_connection) return NULL; 5333deb3ec6SMatthias Ringwald 534a0ffb263SMatthias Ringwald hfp_connection->state = HFP_IDLE; 535a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 536a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_IDLE; 537aa4dd815SMatthias Ringwald 538a0ffb263SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 539a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 5403deb3ec6SMatthias Ringwald 541d751f069SMatthias Ringwald hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID; 542d751f069SMatthias Ringwald hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID; 543d751f069SMatthias Ringwald 544a0ffb263SMatthias Ringwald hfp_reset_context_flags(hfp_connection); 5453deb3ec6SMatthias Ringwald 546d63c37a1SMatthias Ringwald btstack_linked_list_add(&hfp_connections, (btstack_linked_item_t*)hfp_connection); 547a0ffb263SMatthias Ringwald return hfp_connection; 5483deb3ec6SMatthias Ringwald } 5493deb3ec6SMatthias Ringwald 55048e6eeeeSMatthias Ringwald void hfp_finalize_connection_context(hfp_connection_t * hfp_connection){ 55178a72d5eSMilanka Ringwald btstack_run_loop_remove_timer(&hfp_connection->hfp_timeout); 552a0ffb263SMatthias Ringwald btstack_linked_list_remove(&hfp_connections, (btstack_linked_item_t*) hfp_connection); 55309a233a1SMatthias Ringwald btstack_memory_hfp_connection_free(hfp_connection); 5543deb3ec6SMatthias Ringwald } 5553deb3ec6SMatthias Ringwald 556323d3000SMatthias Ringwald static hfp_connection_t * provide_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr, hfp_role_t local_role){ 557405014fbSMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr, local_role); 558a0ffb263SMatthias Ringwald if (hfp_connection) return hfp_connection; 559a0ffb263SMatthias Ringwald hfp_connection = create_hfp_connection_context(); 5606535961aSMatthias Ringwald (void)memcpy(hfp_connection->remote_addr, bd_addr, 6); 561323d3000SMatthias Ringwald hfp_connection->local_role = local_role; 562bdf572f4SMatthias Ringwald log_info("Create HFP context %p: role %u, addr %s", hfp_connection, local_role, bd_addr_to_str(bd_addr)); 563bdf572f4SMatthias Ringwald 564a0ffb263SMatthias Ringwald return hfp_connection; 5653deb3ec6SMatthias Ringwald } 5663deb3ec6SMatthias Ringwald 567aa4dd815SMatthias Ringwald /* @param network. 568aa4dd815SMatthias Ringwald * 0 == no ability to reject a call. 569aa4dd815SMatthias Ringwald * 1 == ability to reject a call. 570aa4dd815SMatthias Ringwald */ 5713deb3ec6SMatthias Ringwald 5723deb3ec6SMatthias Ringwald /* @param suported_features 5733deb3ec6SMatthias Ringwald * HF bit 0: EC and/or NR function (yes/no, 1 = yes, 0 = no) 5743deb3ec6SMatthias Ringwald * HF bit 1: Call waiting or three-way calling(yes/no, 1 = yes, 0 = no) 5753deb3ec6SMatthias Ringwald * HF bit 2: CLI presentation capability (yes/no, 1 = yes, 0 = no) 5763deb3ec6SMatthias Ringwald * HF bit 3: Voice recognition activation (yes/no, 1= yes, 0 = no) 5773deb3ec6SMatthias Ringwald * HF bit 4: Remote volume control (yes/no, 1 = yes, 0 = no) 5783deb3ec6SMatthias Ringwald * HF bit 5: Wide band speech (yes/no, 1 = yes, 0 = no) 5793deb3ec6SMatthias Ringwald */ 5803deb3ec6SMatthias Ringwald /* Bit position: 5813deb3ec6SMatthias Ringwald * AG bit 0: Three-way calling (yes/no, 1 = yes, 0 = no) 5823deb3ec6SMatthias Ringwald * AG bit 1: EC and/or NR function (yes/no, 1 = yes, 0 = no) 5833deb3ec6SMatthias Ringwald * AG bit 2: Voice recognition function (yes/no, 1 = yes, 0 = no) 5843deb3ec6SMatthias Ringwald * AG bit 3: In-band ring tone capability (yes/no, 1 = yes, 0 = no) 5853deb3ec6SMatthias Ringwald * AG bit 4: Attach a phone number to a voice tag (yes/no, 1 = yes, 0 = no) 5863deb3ec6SMatthias Ringwald * AG bit 5: Wide band speech (yes/no, 1 = yes, 0 = no) 5873deb3ec6SMatthias Ringwald */ 5883deb3ec6SMatthias Ringwald 5899b1c3b4dSMatthias 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){ 5903deb3ec6SMatthias Ringwald uint8_t* attribute; 5913deb3ec6SMatthias Ringwald de_create_sequence(service); 5923deb3ec6SMatthias Ringwald 5933deb3ec6SMatthias Ringwald // 0x0000 "Service Record Handle" 594235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE); 5959b1c3b4dSMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle); 5963deb3ec6SMatthias Ringwald 5973deb3ec6SMatthias Ringwald // 0x0001 "Service Class ID List" 598235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST); 5993deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 6003deb3ec6SMatthias Ringwald { 6013deb3ec6SMatthias Ringwald // "UUID for Service" 6023deb3ec6SMatthias Ringwald de_add_number(attribute, DE_UUID, DE_SIZE_16, service_uuid); 603235946f1SMatthias Ringwald de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_GENERIC_AUDIO); 6043deb3ec6SMatthias Ringwald } 6053deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 6063deb3ec6SMatthias Ringwald 6073deb3ec6SMatthias Ringwald // 0x0004 "Protocol Descriptor List" 608235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST); 6093deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 6103deb3ec6SMatthias Ringwald { 6113deb3ec6SMatthias Ringwald uint8_t* l2cpProtocol = de_push_sequence(attribute); 6123deb3ec6SMatthias Ringwald { 613235946f1SMatthias Ringwald de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP); 6143deb3ec6SMatthias Ringwald } 6153deb3ec6SMatthias Ringwald de_pop_sequence(attribute, l2cpProtocol); 6163deb3ec6SMatthias Ringwald 6173deb3ec6SMatthias Ringwald uint8_t* rfcomm = de_push_sequence(attribute); 6183deb3ec6SMatthias Ringwald { 619235946f1SMatthias Ringwald de_add_number(rfcomm, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_RFCOMM); // rfcomm_service 6203deb3ec6SMatthias Ringwald de_add_number(rfcomm, DE_UINT, DE_SIZE_8, rfcomm_channel_nr); // rfcomm channel 6213deb3ec6SMatthias Ringwald } 6223deb3ec6SMatthias Ringwald de_pop_sequence(attribute, rfcomm); 6233deb3ec6SMatthias Ringwald } 6243deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 6253deb3ec6SMatthias Ringwald 6263deb3ec6SMatthias Ringwald 6273deb3ec6SMatthias Ringwald // 0x0005 "Public Browse Group" 628235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group 6293deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 6303deb3ec6SMatthias Ringwald { 631235946f1SMatthias Ringwald de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT); 6323deb3ec6SMatthias Ringwald } 6333deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 6343deb3ec6SMatthias Ringwald 6353deb3ec6SMatthias Ringwald // 0x0009 "Bluetooth Profile Descriptor List" 636235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST); 6373deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 6383deb3ec6SMatthias Ringwald { 6393deb3ec6SMatthias Ringwald uint8_t *sppProfile = de_push_sequence(attribute); 6403deb3ec6SMatthias Ringwald { 641235946f1SMatthias Ringwald de_add_number(sppProfile, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_HANDSFREE); 6423cb2bba2SMilanka Ringwald de_add_number(sppProfile, DE_UINT, DE_SIZE_16, 0x0108); // Verision 1.8 6433deb3ec6SMatthias Ringwald } 6443deb3ec6SMatthias Ringwald de_pop_sequence(attribute, sppProfile); 6453deb3ec6SMatthias Ringwald } 6463deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 6473deb3ec6SMatthias Ringwald 6483deb3ec6SMatthias Ringwald // 0x0100 "Service Name" 6493deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); 6503deb3ec6SMatthias Ringwald de_add_data(service, DE_STRING, strlen(name), (uint8_t *) name); 6513deb3ec6SMatthias Ringwald } 6523deb3ec6SMatthias Ringwald 6536c927b22SMatthias Ringwald static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 6548a46ec40SMatthias Ringwald UNUSED(packet_type); // ok: handling own sdp events 6558a46ec40SMatthias Ringwald UNUSED(channel); // ok: no channel 6568a46ec40SMatthias Ringwald UNUSED(size); // ok: handling own sdp events 6571d9c9c90SMilanka Ringwald 6581d9c9c90SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(sdp_query_context.remote_address, sdp_query_context.local_role); 6591d9c9c90SMilanka Ringwald if (hfp_connection == NULL) { 6601d9c9c90SMilanka Ringwald log_info("connection with %s and local role %d not found", sdp_query_context.remote_address, sdp_query_context.local_role); 66184904e95SMilanka Ringwald return; 66284904e95SMilanka Ringwald } 6633deb3ec6SMatthias Ringwald 6640e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 6655611a760SMatthias Ringwald case SDP_EVENT_QUERY_RFCOMM_SERVICE: 666a0ffb263SMatthias Ringwald hfp_connection->rfcomm_channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet); 6673deb3ec6SMatthias Ringwald break; 6685611a760SMatthias Ringwald case SDP_EVENT_QUERY_COMPLETE: 669a0ffb263SMatthias Ringwald if (hfp_connection->rfcomm_channel_nr > 0){ 670a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_CONNECTED; 671520c92d5SMatthias Ringwald btstack_packet_handler_t packet_handler; 672520c92d5SMatthias Ringwald switch (hfp_connection->local_role){ 673520c92d5SMatthias Ringwald case HFP_ROLE_AG: 674520c92d5SMatthias Ringwald packet_handler = hfp_ag_rfcomm_packet_handler; 675520c92d5SMatthias Ringwald break; 676520c92d5SMatthias Ringwald case HFP_ROLE_HF: 677520c92d5SMatthias Ringwald packet_handler = hfp_hf_rfcomm_packet_handler; 678520c92d5SMatthias Ringwald break; 679520c92d5SMatthias Ringwald default: 6801d9c9c90SMilanka Ringwald btstack_assert(false); 681520c92d5SMatthias Ringwald return; 682520c92d5SMatthias Ringwald } 6831d9c9c90SMilanka Ringwald 684ca59be51SMatthias Ringwald rfcomm_create_channel(packet_handler, hfp_connection->remote_addr, hfp_connection->rfcomm_channel_nr, NULL); 6851d9c9c90SMilanka Ringwald 6861d9c9c90SMilanka Ringwald } else { 68784904e95SMilanka Ringwald hfp_connection->state = HFP_IDLE; 688cc92f22bSMatthias Ringwald uint8_t status = sdp_event_query_complete_get_status(packet); 689cc92f22bSMatthias Ringwald if (status == ERROR_CODE_SUCCESS){ 690cc92f22bSMatthias Ringwald // report service not found 691cc92f22bSMatthias Ringwald status = SDP_SERVICE_NOT_FOUND; 692cc92f22bSMatthias Ringwald } 693cc92f22bSMatthias Ringwald hfp_emit_slc_connection_event(hfp_connection, status, HCI_CON_HANDLE_INVALID, hfp_connection->remote_addr); 694cc92f22bSMatthias Ringwald log_info("rfcomm service not found, status 0x%02x", status); 6951d9c9c90SMilanka Ringwald } 6961d9c9c90SMilanka Ringwald 6971d9c9c90SMilanka Ringwald // register the SDP Query request to check if there is another connection waiting for the query 6981d9c9c90SMilanka Ringwald // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback 6991d9c9c90SMilanka Ringwald (void) sdp_client_register_query_callback(&hfp_handle_sdp_client_query_request); 7003deb3ec6SMatthias Ringwald break; 7013deb3ec6SMatthias Ringwald default: 7023deb3ec6SMatthias Ringwald break; 7033deb3ec6SMatthias Ringwald } 7043deb3ec6SMatthias Ringwald } 7053deb3ec6SMatthias Ringwald 70638200c1dSMilanka Ringwald // returns 0 if unexpected error or no other link options remained, otherwise 1 70738200c1dSMilanka Ringwald static int hfp_handle_failed_sco_connection(uint8_t status){ 708eddcd308SMatthias Ringwald 709eddcd308SMatthias Ringwald if (!sco_establishment_active){ 7103427dd75SMatthias Ringwald log_info("(e)SCO Connection failed but not started by us"); 71138200c1dSMilanka Ringwald return 0; 712eddcd308SMatthias Ringwald } 713eddcd308SMatthias Ringwald 7143427dd75SMatthias Ringwald log_info("(e)SCO Connection failed 0x%02x", status); 7153427dd75SMatthias Ringwald switch (status){ 7163427dd75SMatthias Ringwald case ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE: 7173427dd75SMatthias Ringwald case ERROR_CODE_UNSPECIFIED_ERROR: 7183427dd75SMatthias Ringwald case ERROR_CODE_CONNECTION_REJECTED_DUE_TO_LIMITED_RESOURCES: 719eddcd308SMatthias Ringwald break; 7203427dd75SMatthias Ringwald default: 7213427dd75SMatthias Ringwald return 0; 7223427dd75SMatthias Ringwald } 7233427dd75SMatthias Ringwald 7243427dd75SMatthias Ringwald // note: eSCO_S4 supported flag not available, but it's only relevant for highest CVSD link setting (and the current failed) 7253427dd75SMatthias Ringwald hfp_link_settings_t next_setting = hfp_next_link_setting_for_connection(sco_establishment_active->link_setting, sco_establishment_active, false); 7263427dd75SMatthias Ringwald 7273427dd75SMatthias Ringwald // handle no valid setting found 7283427dd75SMatthias Ringwald if (next_setting == HFP_LINK_SETTINGS_NONE) { 7293427dd75SMatthias Ringwald if (sco_establishment_active->negotiated_codec == HFP_CODEC_MSBC){ 7303427dd75SMatthias Ringwald log_info("T2/T1 failed, fallback to CVSD - D1"); 7317522e673SMatthias Ringwald sco_establishment_active->negotiated_codec = HFP_CODEC_CVSD; 732bc1b1537SMilanka Ringwald sco_establishment_active->sco_for_msbc_failed = 1; 733bc1b1537SMilanka Ringwald sco_establishment_active->command = HFP_CMD_AG_SEND_COMMON_CODEC; 7347522e673SMatthias Ringwald sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D1; 7353427dd75SMatthias Ringwald } else { 7363427dd75SMatthias Ringwald // no other options 7373427dd75SMatthias Ringwald return 0; 738eddcd308SMatthias Ringwald } 7393427dd75SMatthias Ringwald } 7403427dd75SMatthias Ringwald 7413427dd75SMatthias Ringwald log_info("e)SCO Connection: try new link_setting %d", next_setting); 742eddcd308SMatthias Ringwald sco_establishment_active->establish_audio_connection = 1; 7433427dd75SMatthias Ringwald sco_establishment_active->link_setting = next_setting; 74438200c1dSMilanka Ringwald sco_establishment_active = NULL; 74538200c1dSMilanka Ringwald return 1; 746eddcd308SMatthias Ringwald } 747eddcd308SMatthias Ringwald 748eddcd308SMatthias Ringwald 749405014fbSMatthias Ringwald void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){ 7505575558eSMilanka Ringwald UNUSED(packet_type); 7518a46ec40SMatthias Ringwald UNUSED(channel); // ok: no channel 7525575558eSMilanka Ringwald UNUSED(size); 7539ec2630cSMatthias Ringwald 7543deb3ec6SMatthias Ringwald bd_addr_t event_addr; 75527950165SMatthias Ringwald hci_con_handle_t handle; 756a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = NULL; 757674515e8SMatthias Ringwald uint8_t status; 7583deb3ec6SMatthias Ringwald 75927950165SMatthias Ringwald log_debug("HFP HCI event handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size); 760aa4dd815SMatthias Ringwald 7610e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 7621b005648SMatthias Ringwald 763011577abSMilanka Ringwald case HCI_EVENT_CONNECTION_REQUEST: 7647522e673SMatthias Ringwald switch(hci_event_connection_request_get_link_type(packet)){ 7657522e673SMatthias Ringwald case 0: // SCO 7667522e673SMatthias Ringwald case 2: // eSCO 767011577abSMilanka Ringwald hci_event_connection_request_get_bd_addr(packet, event_addr); 768405014fbSMatthias Ringwald hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role); 769011577abSMilanka Ringwald if (!hfp_connection) break; 770c169df2fSMatthias Ringwald if (hci_event_connection_request_get_link_type(packet) == 2){ 771cb81d35dSMatthias Ringwald hfp_connection->accept_sco = 2; 772c169df2fSMatthias Ringwald } else { 773cb81d35dSMatthias Ringwald hfp_connection->accept_sco = 1; 774c169df2fSMatthias Ringwald } 7753721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP 7763721a235SMatthias Ringwald hfp_cc256x_prepare_for_sco(hfp_connection); 7773721a235SMatthias Ringwald #endif 778689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 779689d4323SMatthias Ringwald hfp_bcm_prepare_for_sco(hfp_connection); 780689d4323SMatthias Ringwald #endif 781cb81d35dSMatthias Ringwald log_info("accept sco %u\n", hfp_connection->accept_sco); 782719aedb8SMatthias Ringwald sco_establishment_active = hfp_connection; 783202c8a4cSMatthias Ringwald break; 7847522e673SMatthias Ringwald default: 7857522e673SMatthias Ringwald break; 7867522e673SMatthias Ringwald } 787011577abSMilanka Ringwald break; 7883deb3ec6SMatthias Ringwald 789eddcd308SMatthias Ringwald case HCI_EVENT_COMMAND_STATUS: 790eddcd308SMatthias Ringwald if (hci_event_command_status_get_command_opcode(packet) == hci_setup_synchronous_connection.opcode) { 791b87963a5SMatthias Ringwald if (sco_establishment_active == NULL) break; 792be2a1a78SMatthias Ringwald status = hci_event_command_status_get_status(packet); 79338200c1dSMilanka Ringwald if (status == ERROR_CODE_SUCCESS) break; 79438200c1dSMilanka Ringwald 79538200c1dSMilanka Ringwald hfp_connection = sco_establishment_active; 79638200c1dSMilanka Ringwald if (hfp_handle_failed_sco_connection(status)) break; 79738200c1dSMilanka Ringwald hfp_connection->establish_audio_connection = 0; 79838200c1dSMilanka Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 79938200c1dSMilanka Ringwald hfp_emit_sco_event(hfp_connection, status, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec); 8006c5b2002SMatthias Ringwald } 801eddcd308SMatthias Ringwald break; 802eddcd308SMatthias Ringwald 8033deb3ec6SMatthias Ringwald case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{ 804b87963a5SMatthias Ringwald if (sco_establishment_active == NULL) break; 805011577abSMilanka Ringwald hci_event_synchronous_connection_complete_get_bd_addr(packet, event_addr); 806405014fbSMatthias Ringwald hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role); 807011577abSMilanka Ringwald if (!hfp_connection) { 808011577abSMilanka Ringwald log_error("HFP: connection does not exist for remote with addr %s.", bd_addr_to_str(event_addr)); 809011577abSMilanka Ringwald return; 810011577abSMilanka Ringwald } 811ce263fc8SMatthias Ringwald 812011577abSMilanka Ringwald status = hci_event_synchronous_connection_complete_get_status(packet); 81338200c1dSMilanka Ringwald if (status != ERROR_CODE_SUCCESS){ 814cb81d35dSMatthias Ringwald hfp_connection->accept_sco = 0; 81538200c1dSMilanka Ringwald if (hfp_handle_failed_sco_connection(status)) break; 81638200c1dSMilanka Ringwald 81738200c1dSMilanka Ringwald hfp_connection->establish_audio_connection = 0; 81838200c1dSMilanka Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 81938200c1dSMilanka Ringwald hfp_emit_sco_event(hfp_connection, status, 0, event_addr, hfp_connection->negotiated_codec); 820aa4dd815SMatthias Ringwald break; 821aa4dd815SMatthias Ringwald } 822aa4dd815SMatthias Ringwald 823011577abSMilanka Ringwald uint16_t sco_handle = hci_event_synchronous_connection_complete_get_handle(packet); 824011577abSMilanka Ringwald uint8_t link_type = hci_event_synchronous_connection_complete_get_link_type(packet); 825011577abSMilanka Ringwald uint8_t transmission_interval = hci_event_synchronous_connection_complete_get_transmission_interval(packet); // measured in slots 826011577abSMilanka Ringwald uint8_t retransmission_interval = hci_event_synchronous_connection_complete_get_retransmission_interval(packet);// measured in slots 827011577abSMilanka Ringwald uint16_t rx_packet_length = hci_event_synchronous_connection_complete_get_rx_packet_length(packet); // measured in bytes 828011577abSMilanka Ringwald uint16_t tx_packet_length = hci_event_synchronous_connection_complete_get_tx_packet_length(packet); // measured in bytes 8293deb3ec6SMatthias Ringwald 8303deb3ec6SMatthias Ringwald switch (link_type){ 8313deb3ec6SMatthias Ringwald case 0x00: 832aa4dd815SMatthias Ringwald log_info("SCO Connection established."); 8333deb3ec6SMatthias Ringwald if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval); 8343deb3ec6SMatthias Ringwald if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval); 8353deb3ec6SMatthias Ringwald if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length); 8363deb3ec6SMatthias Ringwald if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length); 8373deb3ec6SMatthias Ringwald break; 8383deb3ec6SMatthias Ringwald case 0x02: 839aa4dd815SMatthias Ringwald log_info("eSCO Connection established. \n"); 8403deb3ec6SMatthias Ringwald break; 8413deb3ec6SMatthias Ringwald default: 8423deb3ec6SMatthias Ringwald log_error("(e)SCO reserved link_type 0x%2x", link_type); 8433deb3ec6SMatthias Ringwald break; 8443deb3ec6SMatthias Ringwald } 845e0d13a19SMilanka Ringwald 8463deb3ec6SMatthias Ringwald log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, " 847aa4dd815SMatthias Ringwald " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)\n", sco_handle, 848e0d13a19SMilanka Ringwald bd_addr_to_str(event_addr), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length, 849e0d13a19SMilanka Ringwald hci_event_synchronous_connection_complete_get_air_mode(packet)); 8503deb3ec6SMatthias Ringwald 851d63c37a1SMatthias Ringwald if (hfp_connection->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){ 852aa4dd815SMatthias Ringwald log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN"); 853d63c37a1SMatthias Ringwald hfp_connection->state = HFP_W2_DISCONNECT_SCO; 854aa4dd815SMatthias Ringwald break; 855aa4dd815SMatthias Ringwald } 856d63c37a1SMatthias Ringwald hfp_connection->sco_handle = sco_handle; 857d63c37a1SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 858d63c37a1SMatthias Ringwald hfp_connection->state = HFP_AUDIO_CONNECTION_ESTABLISHED; 85938200c1dSMilanka Ringwald hfp_emit_sco_event(hfp_connection, status, sco_handle, event_addr, hfp_connection->negotiated_codec); 8603deb3ec6SMatthias Ringwald break; 8613deb3ec6SMatthias Ringwald } 8623deb3ec6SMatthias Ringwald 8633deb3ec6SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 864f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet,3); 865405014fbSMatthias Ringwald hfp_connection = get_hfp_connection_context_for_sco_handle(handle, local_role); 866aa4dd815SMatthias Ringwald 867d63c37a1SMatthias Ringwald if (!hfp_connection) break; 868aa4dd815SMatthias Ringwald 8693721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP 8703721a235SMatthias Ringwald hfp_connection->cc256x_send_wbs_disassociate = true; 8713721a235SMatthias Ringwald #endif 872689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 873689d4323SMatthias Ringwald hfp_connection->bcm_send_disable_wbs = true; 874689d4323SMatthias Ringwald #endif 875d751f069SMatthias Ringwald hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID; 876d63c37a1SMatthias Ringwald hfp_connection->release_audio_connection = 0; 87748e6eeeeSMatthias Ringwald 87848e6eeeeSMatthias Ringwald if (hfp_connection->state == HFP_W4_SCO_DISCONNECTED_TO_SHUTDOWN){ 87948e6eeeeSMatthias Ringwald // RFCOMM already closed -> remote power off 88048e6eeeeSMatthias Ringwald #if defined(ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS) 88148e6eeeeSMatthias Ringwald hfp_connection->state = HFP_W4_WBS_SHUTDOWN; 88248e6eeeeSMatthias Ringwald #else 88348e6eeeeSMatthias Ringwald hfp_finalize_connection_context(hfp_connection); 88448e6eeeeSMatthias Ringwald #endif 88548e6eeeeSMatthias Ringwald break; 88648e6eeeeSMatthias Ringwald } 88748e6eeeeSMatthias Ringwald 888d63c37a1SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 889d703d377SMatthias Ringwald hfp_emit_audio_connection_released(hfp_connection, handle); 8900c3047fbSMatthias Ringwald 8915f053052SMilanka Ringwald if (hfp_connection->release_slc_connection){ 8925f053052SMilanka Ringwald hfp_connection->release_slc_connection = 0; 8930c3047fbSMatthias Ringwald log_info("SCO disconnected, w2 disconnect RFCOMM\n"); 8940c3047fbSMatthias Ringwald hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM; 8955f053052SMilanka Ringwald } 8963deb3ec6SMatthias Ringwald break; 8973deb3ec6SMatthias Ringwald 898fc64f94aSMatthias Ringwald default: 8993deb3ec6SMatthias Ringwald break; 9003deb3ec6SMatthias Ringwald } 9013deb3ec6SMatthias Ringwald } 9023deb3ec6SMatthias Ringwald 9033721a235SMatthias Ringwald 90427950165SMatthias Ringwald void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){ 9055575558eSMilanka Ringwald UNUSED(packet_type); 90627950165SMatthias Ringwald UNUSED(channel); // ok: no channel 9075575558eSMilanka Ringwald UNUSED(size); 90827950165SMatthias Ringwald 90927950165SMatthias Ringwald bd_addr_t event_addr; 91027950165SMatthias Ringwald uint16_t rfcomm_cid; 91127950165SMatthias Ringwald hfp_connection_t * hfp_connection = NULL; 91227950165SMatthias Ringwald uint8_t status; 91327950165SMatthias Ringwald 91427950165SMatthias Ringwald log_debug("HFP packet_handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size); 91527950165SMatthias Ringwald 91627950165SMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 91727950165SMatthias Ringwald 91827950165SMatthias Ringwald case RFCOMM_EVENT_INCOMING_CONNECTION: 91927950165SMatthias Ringwald // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 92027950165SMatthias Ringwald rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr); 92127950165SMatthias Ringwald hfp_connection = provide_hfp_connection_context_for_bd_addr(event_addr, local_role); 92227950165SMatthias Ringwald if (!hfp_connection){ 92327950165SMatthias Ringwald log_info("hfp: no memory to accept incoming connection - decline"); 92427950165SMatthias Ringwald rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet)); 92527950165SMatthias Ringwald return; 92627950165SMatthias Ringwald } 92727950165SMatthias Ringwald if (hfp_connection->state != HFP_IDLE) { 9284960f9e7SMatthias Ringwald log_error("hfp: incoming connection but not idle, reject"); 9294960f9e7SMatthias Ringwald rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet)); 93027950165SMatthias Ringwald return; 93127950165SMatthias Ringwald } 93227950165SMatthias Ringwald 93327950165SMatthias Ringwald hfp_connection->rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet); 93427950165SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_CONNECTED; 93527950165SMatthias Ringwald rfcomm_accept_connection(hfp_connection->rfcomm_cid); 93627950165SMatthias Ringwald break; 93727950165SMatthias Ringwald 93827950165SMatthias Ringwald case RFCOMM_EVENT_CHANNEL_OPENED: 93927950165SMatthias Ringwald // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16) 94027950165SMatthias Ringwald 94127950165SMatthias Ringwald rfcomm_event_channel_opened_get_bd_addr(packet, event_addr); 94227950165SMatthias Ringwald status = rfcomm_event_channel_opened_get_status(packet); 94327950165SMatthias Ringwald 944405014fbSMatthias Ringwald hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role); 945505f1c30SMatthias Ringwald if (!hfp_connection || (hfp_connection->state != HFP_W4_RFCOMM_CONNECTED)) return; 94627950165SMatthias Ringwald 94727950165SMatthias Ringwald if (status) { 94827950165SMatthias Ringwald hfp_emit_slc_connection_event(hfp_connection, status, rfcomm_event_channel_opened_get_con_handle(packet), event_addr); 94948e6eeeeSMatthias Ringwald hfp_finalize_connection_context(hfp_connection); 95027950165SMatthias Ringwald } else { 95127950165SMatthias Ringwald hfp_connection->acl_handle = rfcomm_event_channel_opened_get_con_handle(packet); 95227950165SMatthias Ringwald hfp_connection->rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet); 95327950165SMatthias Ringwald bd_addr_copy(hfp_connection->remote_addr, event_addr); 95427950165SMatthias Ringwald 95527950165SMatthias Ringwald switch (hfp_connection->state){ 95627950165SMatthias Ringwald case HFP_W4_RFCOMM_CONNECTED: 95727950165SMatthias Ringwald hfp_connection->state = HFP_EXCHANGE_SUPPORTED_FEATURES; 95827950165SMatthias Ringwald break; 95927950165SMatthias Ringwald case HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN: 96027950165SMatthias Ringwald hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM; 96127950165SMatthias Ringwald break; 96227950165SMatthias Ringwald default: 96327950165SMatthias Ringwald break; 96427950165SMatthias Ringwald } 96527950165SMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 96627950165SMatthias Ringwald } 96727950165SMatthias Ringwald break; 96827950165SMatthias Ringwald 96927950165SMatthias Ringwald case RFCOMM_EVENT_CHANNEL_CLOSED: 97027950165SMatthias Ringwald rfcomm_cid = little_endian_read_16(packet,2); 97127950165SMatthias Ringwald hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid); 97227950165SMatthias Ringwald if (!hfp_connection) break; 97327950165SMatthias Ringwald if (hfp_connection->state == HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART){ 97427950165SMatthias Ringwald hfp_connection->state = HFP_IDLE; 97527950165SMatthias Ringwald hfp_establish_service_level_connection(hfp_connection->remote_addr, hfp_connection->service_uuid, local_role); 97627950165SMatthias Ringwald break; 97727950165SMatthias Ringwald } 97848e6eeeeSMatthias Ringwald if ( hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 97948e6eeeeSMatthias Ringwald // service connection was released, this implicitly releases audio connection as well 98048e6eeeeSMatthias Ringwald hfp_connection->release_audio_connection = 0; 981d703d377SMatthias Ringwald hci_con_handle_t sco_handle = hfp_connection->sco_handle; 98248e6eeeeSMatthias Ringwald gap_disconnect(hfp_connection->sco_handle); 98348e6eeeeSMatthias Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED_TO_SHUTDOWN; 984d703d377SMatthias Ringwald hfp_emit_audio_connection_released(hfp_connection, sco_handle); 98527950165SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0); 98648e6eeeeSMatthias Ringwald } else { 98748e6eeeeSMatthias Ringwald // regular case 98848e6eeeeSMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0); 98948e6eeeeSMatthias Ringwald hfp_finalize_connection_context(hfp_connection); 99048e6eeeeSMatthias Ringwald } 99127950165SMatthias Ringwald break; 99227950165SMatthias Ringwald 99327950165SMatthias Ringwald default: 99427950165SMatthias Ringwald break; 99527950165SMatthias Ringwald } 99627950165SMatthias Ringwald } 997aa4dd815SMatthias Ringwald // translates command string into hfp_command_t CMD 99894a27792SMatthias Ringwald 99994a27792SMatthias Ringwald typedef struct { 100094a27792SMatthias Ringwald const char * command; 100194a27792SMatthias Ringwald hfp_command_t command_id; 100294a27792SMatthias Ringwald } hfp_command_entry_t; 100394a27792SMatthias Ringwald 100494a27792SMatthias Ringwald static hfp_command_entry_t hfp_ag_commmand_table[] = { 1005cb33905eSMatthias Ringwald { "AT+BAC=", HFP_CMD_AVAILABLE_CODECS }, 1006cb33905eSMatthias Ringwald { "AT+BCC", HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP }, 1007cb33905eSMatthias Ringwald { "AT+BCS=", HFP_CMD_HF_CONFIRMED_CODEC }, 1008cb33905eSMatthias Ringwald { "AT+BIA=", HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE, }, // +BIA:<enabled>,,<enabled>,,,<enabled> 1009cb33905eSMatthias Ringwald { "AT+BIEV=", HFP_CMD_HF_INDICATOR_STATUS }, 101094a27792SMatthias Ringwald { "AT+BIND=", HFP_CMD_LIST_GENERIC_STATUS_INDICATORS }, 101194a27792SMatthias Ringwald { "AT+BIND=?", HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS }, 101294a27792SMatthias Ringwald { "AT+BIND?", HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE }, 1013051f797dSMilanka Ringwald { "AT+BINP=", HFP_CMD_HF_REQUEST_PHONE_NUMBER }, 1014cb33905eSMatthias Ringwald { "AT+BLDN", HFP_CMD_REDIAL_LAST_NUMBER }, 1015cb33905eSMatthias Ringwald { "AT+BRSF=", HFP_CMD_SUPPORTED_FEATURES }, 101694a27792SMatthias Ringwald { "AT+BTRH=", HFP_CMD_RESPONSE_AND_HOLD_COMMAND }, 101794a27792SMatthias Ringwald { "AT+BTRH?", HFP_CMD_RESPONSE_AND_HOLD_QUERY }, 10189ed286f3SMilanka Ringwald { "AT+BVRA=", HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION }, 1019cb33905eSMatthias Ringwald { "AT+CCWA=", HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION}, 102094a27792SMatthias Ringwald { "AT+CHLD=", HFP_CMD_CALL_HOLD }, 102194a27792SMatthias Ringwald { "AT+CHLD=?", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES }, 1022cb33905eSMatthias Ringwald { "AT+CHUP", HFP_CMD_HANG_UP_CALL }, 102394a27792SMatthias Ringwald { "AT+CIND=?", HFP_CMD_RETRIEVE_AG_INDICATORS }, 102494a27792SMatthias Ringwald { "AT+CIND?", HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS }, 1025cb33905eSMatthias Ringwald { "AT+CLCC", HFP_CMD_LIST_CURRENT_CALLS }, 1026cb33905eSMatthias Ringwald { "AT+CLIP=", HFP_CMD_ENABLE_CLIP}, 1027cb33905eSMatthias Ringwald { "AT+CMEE=", HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR}, 1028cb33905eSMatthias Ringwald { "AT+CMER=", HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE }, 1029cb33905eSMatthias Ringwald { "AT+CNUM", HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION }, 103094a27792SMatthias Ringwald { "AT+COPS=", HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT }, 103194a27792SMatthias Ringwald { "AT+COPS?", HFP_CMD_QUERY_OPERATOR_SELECTION_NAME }, 1032cb33905eSMatthias Ringwald { "AT+NREC=", HFP_CMD_TURN_OFF_EC_AND_NR, }, 1033cb33905eSMatthias Ringwald { "AT+VGM=", HFP_CMD_SET_MICROPHONE_GAIN }, 1034cb33905eSMatthias Ringwald { "AT+VGS=", HFP_CMD_SET_SPEAKER_GAIN }, 1035d8656ee6SMilanka Ringwald { "AT+VTS=", HFP_CMD_TRANSMIT_DTMF_CODES }, 103694a27792SMatthias Ringwald { "ATA", HFP_CMD_CALL_ANSWERED }, 103794a27792SMatthias Ringwald }; 103894a27792SMatthias Ringwald 103994a27792SMatthias Ringwald static hfp_command_entry_t hfp_hf_commmand_table[] = { 1040cb33905eSMatthias Ringwald { "+BCS:", HFP_CMD_AG_SUGGESTED_CODEC }, 104194a27792SMatthias Ringwald { "+BIND:", HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS }, 1042cb33905eSMatthias Ringwald { "+BINP", HFP_CMD_AG_SENT_PHONE_NUMBER }, 1043cb33905eSMatthias Ringwald { "+BRSF:", HFP_CMD_SUPPORTED_FEATURES }, 1044cb33905eSMatthias Ringwald { "+BSIR:", HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING }, 104594a27792SMatthias Ringwald { "+BTRH:", HFP_CMD_RESPONSE_AND_HOLD_STATUS }, 1046cb33905eSMatthias Ringwald { "+BVRA:", HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION }, 1047cb33905eSMatthias Ringwald { "+CCWA:", HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE, }, 104894a27792SMatthias Ringwald { "+CHLD:", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES }, 1049cb33905eSMatthias Ringwald { "+CIEV:", HFP_CMD_TRANSFER_AG_INDICATOR_STATUS}, 105084a0c24eSMatthias Ringwald { "+CIND:", HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC }, 1051cb33905eSMatthias Ringwald { "+CLCC:", HFP_CMD_LIST_CURRENT_CALLS }, 1052cb33905eSMatthias Ringwald { "+CLIP:", HFP_CMD_AG_SENT_CLIP_INFORMATION }, 1053cb33905eSMatthias Ringwald { "+CME ERROR:", HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR }, 1054cb33905eSMatthias Ringwald { "+CNUM:", HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION}, 1055cb33905eSMatthias Ringwald { "+COPS:", HFP_CMD_QUERY_OPERATOR_SELECTION_NAME }, 1056cb33905eSMatthias Ringwald { "+VGM:", HFP_CMD_SET_MICROPHONE_GAIN }, 1057cb33905eSMatthias Ringwald { "+VGS:", HFP_CMD_SET_SPEAKER_GAIN}, 1058cb33905eSMatthias Ringwald { "ERROR", HFP_CMD_ERROR}, 1059cb33905eSMatthias Ringwald { "NOP", HFP_CMD_NONE}, // dummy command used by unit tests 1060cb33905eSMatthias Ringwald { "OK", HFP_CMD_OK }, 1061cb33905eSMatthias Ringwald { "RING", HFP_CMD_RING }, 106294a27792SMatthias Ringwald }; 106394a27792SMatthias Ringwald 1064aa4dd815SMatthias Ringwald static hfp_command_t parse_command(const char * line_buffer, int isHandsFree){ 10653deb3ec6SMatthias Ringwald 1066cb33905eSMatthias Ringwald // table lookup based on role 106794a27792SMatthias Ringwald uint16_t num_entries; 106894a27792SMatthias Ringwald hfp_command_entry_t * table; 106994a27792SMatthias Ringwald if (isHandsFree == 0){ 107094a27792SMatthias Ringwald table = hfp_ag_commmand_table; 107194a27792SMatthias Ringwald num_entries = sizeof(hfp_ag_commmand_table) / sizeof(hfp_command_entry_t); 10723deb3ec6SMatthias Ringwald } else { 107394a27792SMatthias Ringwald table = hfp_hf_commmand_table; 107494a27792SMatthias Ringwald num_entries = sizeof(hfp_hf_commmand_table) / sizeof(hfp_command_entry_t); 107594a27792SMatthias Ringwald } 1076791f0d0aSMatthias Ringwald // binary search 1077791f0d0aSMatthias Ringwald uint16_t left = 0; 1078791f0d0aSMatthias Ringwald uint16_t right = num_entries - 1; 1079791f0d0aSMatthias Ringwald while (left <= right){ 1080791f0d0aSMatthias Ringwald uint16_t middle = left + (right - left) / 2; 1081791f0d0aSMatthias Ringwald hfp_command_entry_t *entry = &table[middle]; 108294a27792SMatthias Ringwald int match = strcmp(line_buffer, entry->command); 1083791f0d0aSMatthias Ringwald if (match < 0){ 1084791f0d0aSMatthias Ringwald // search term is lower than middle element 108547005182SMilanka Ringwald if (right == 0) break; 1086791f0d0aSMatthias Ringwald right = middle - 1; 1087791f0d0aSMatthias Ringwald } else if (match == 0){ 108894a27792SMatthias Ringwald return entry->command_id; 1089791f0d0aSMatthias Ringwald } else { 1090791f0d0aSMatthias Ringwald // search term is higher than middle element 1091791f0d0aSMatthias Ringwald left = middle + 1; 109294a27792SMatthias Ringwald } 109394a27792SMatthias Ringwald } 109494a27792SMatthias Ringwald 1095cb33905eSMatthias Ringwald // note: if parser in CMD_HEADER state would treats digits and maybe '+' as separator, match on "ATD" would work. 10960222a807SMatthias Ringwald // note: phone number is currently expected in line_buffer[3..] 1097cb33905eSMatthias Ringwald // prefix match on 'ATD', AG only 1098cb33905eSMatthias Ringwald if ((isHandsFree == 0) && (strncmp(line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0)){ 1099cb33905eSMatthias Ringwald return HFP_CMD_CALL_PHONE_NUMBER; 11003deb3ec6SMatthias Ringwald } 11013deb3ec6SMatthias Ringwald 1102cb33905eSMatthias Ringwald // Valid looking, but unknown commands/responses 1103cb33905eSMatthias Ringwald if ((isHandsFree == 0) && (strncmp(line_buffer, "AT+", 3) == 0)){ 1104aa4dd815SMatthias Ringwald return HFP_CMD_UNKNOWN; 11053deb3ec6SMatthias Ringwald } 11063deb3ec6SMatthias Ringwald 1107cb33905eSMatthias Ringwald if ((isHandsFree != 0) && (strncmp(line_buffer, "+", 1) == 0)){ 1108aa4dd815SMatthias Ringwald return HFP_CMD_UNKNOWN; 1109aa4dd815SMatthias Ringwald } 11103deb3ec6SMatthias Ringwald 1111aa4dd815SMatthias Ringwald return HFP_CMD_NONE; 11123deb3ec6SMatthias Ringwald } 11133deb3ec6SMatthias Ringwald 1114a0ffb263SMatthias Ringwald static void hfp_parser_store_byte(hfp_connection_t * hfp_connection, uint8_t byte){ 111562c7768eSMatthias Ringwald if ((hfp_connection->line_size + 1 ) >= HFP_MAX_INDICATOR_DESC_SIZE) return; 1116a0ffb263SMatthias Ringwald hfp_connection->line_buffer[hfp_connection->line_size++] = byte; 1117a0ffb263SMatthias Ringwald hfp_connection->line_buffer[hfp_connection->line_size] = 0; 11183deb3ec6SMatthias Ringwald } 1119a0ffb263SMatthias Ringwald static int hfp_parser_is_buffer_empty(hfp_connection_t * hfp_connection){ 1120a0ffb263SMatthias Ringwald return hfp_connection->line_size == 0; 11213deb3ec6SMatthias Ringwald } 11223deb3ec6SMatthias Ringwald 11233deb3ec6SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){ 1124c1ab6cc1SMatthias Ringwald return (byte == '\n') || (byte == '\r'); 11253deb3ec6SMatthias Ringwald } 11263deb3ec6SMatthias Ringwald 112794a27792SMatthias Ringwald static void hfp_parser_reset_line_buffer(hfp_connection_t *hfp_connection) { 1128a0ffb263SMatthias Ringwald hfp_connection->line_size = 0; 11293deb3ec6SMatthias Ringwald } 11301d81c7feSMatthias Ringwald 11311d81c7feSMatthias Ringwald static void hfp_parser_store_if_token(hfp_connection_t * hfp_connection, uint8_t byte){ 11321d81c7feSMatthias Ringwald switch (byte){ 11331d81c7feSMatthias Ringwald case ',': 1134f8301d46SMatthias Ringwald case '-': 11351d81c7feSMatthias Ringwald case ';': 11361d81c7feSMatthias Ringwald case '(': 11371d81c7feSMatthias Ringwald case ')': 11381d81c7feSMatthias Ringwald case '\n': 11391d81c7feSMatthias Ringwald case '\r': 11403deb3ec6SMatthias Ringwald break; 11413deb3ec6SMatthias Ringwald default: 11421d81c7feSMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 11433deb3ec6SMatthias Ringwald break; 11443deb3ec6SMatthias Ringwald } 11453deb3ec6SMatthias Ringwald } 11461d81c7feSMatthias Ringwald 11471d81c7feSMatthias Ringwald static bool hfp_parser_is_separator( uint8_t byte){ 11481d81c7feSMatthias Ringwald switch (byte){ 11491d81c7feSMatthias Ringwald case ',': 1150f8301d46SMatthias Ringwald case '-': 11511d81c7feSMatthias Ringwald case ';': 11521d81c7feSMatthias Ringwald case '\n': 11531d81c7feSMatthias Ringwald case '\r': 11541d81c7feSMatthias Ringwald return true; 1155dd533208SMatthias Ringwald default: 11561d81c7feSMatthias Ringwald return false; 11573deb3ec6SMatthias Ringwald } 11583deb3ec6SMatthias Ringwald } 11593deb3ec6SMatthias Ringwald 1160d6b237acSMatthias Ringwald static bool hfp_parse_byte(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){ 1161aa4dd815SMatthias Ringwald 11621dddc4f4SMatthias Ringwald // handle doubles quotes 11631dddc4f4SMatthias Ringwald if (byte == '"'){ 11641dddc4f4SMatthias Ringwald hfp_connection->parser_quoted = !hfp_connection->parser_quoted; 1165d6b237acSMatthias Ringwald return true; 11661dddc4f4SMatthias Ringwald } 11671dddc4f4SMatthias Ringwald if (hfp_connection->parser_quoted) { 11681dddc4f4SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 1169d6b237acSMatthias Ringwald return true; 11701dddc4f4SMatthias Ringwald } 11713deb3ec6SMatthias Ringwald 11721dddc4f4SMatthias Ringwald // ignore spaces outside command or double quotes (required e.g. for '+CME ERROR:..") command 1173d6b237acSMatthias Ringwald if ((byte == ' ') && (hfp_connection->parser_state != HFP_PARSER_CMD_HEADER)) return true; 11741dddc4f4SMatthias Ringwald 1175bfeaf344SMatthias Ringwald bool processed = true; 1176bfeaf344SMatthias Ringwald 1177a0ffb263SMatthias Ringwald switch (hfp_connection->parser_state) { 1178dd533208SMatthias Ringwald case HFP_PARSER_CMD_HEADER: 1179d28b2d5dSMilanka Ringwald switch (byte) { 1180dd533208SMatthias Ringwald case '\n': 1181dd533208SMatthias Ringwald case '\r': 1182dd533208SMatthias Ringwald case ';': 1183bfeaf344SMatthias Ringwald // ignore separator 1184bfeaf344SMatthias Ringwald break; 1185bfeaf344SMatthias Ringwald case ':': 1186bfeaf344SMatthias Ringwald case '?': 1187bfeaf344SMatthias Ringwald // store separator 1188bfeaf344SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 1189dd533208SMatthias Ringwald break; 1190d28b2d5dSMilanka Ringwald case '=': 1191bfeaf344SMatthias Ringwald // equal sign: remember and wait for next char to decided between '=?' and '=\?' 1192dd533208SMatthias Ringwald hfp_connection->found_equal_sign = true; 1193a0ffb263SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 1194d6b237acSMatthias Ringwald return true; 1195d28b2d5dSMilanka Ringwald default: 1196bfeaf344SMatthias Ringwald // store if not lookahead 1197d7f16ff1SMatthias Ringwald if (!hfp_connection->found_equal_sign) { 1198dd533208SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 1199d6b237acSMatthias Ringwald return true; 1200dd533208SMatthias Ringwald } 1201bfeaf344SMatthias Ringwald // mark as lookahead 1202bfeaf344SMatthias Ringwald processed = false; 1203d7f16ff1SMatthias Ringwald break; 1204d7f16ff1SMatthias Ringwald } 1205ce263fc8SMatthias Ringwald 12061d81c7feSMatthias Ringwald // ignore empty tokens 1207d6b237acSMatthias Ringwald if (hfp_parser_is_buffer_empty(hfp_connection)) return true; 1208dd533208SMatthias Ringwald 1209bfeaf344SMatthias Ringwald // parse 1210dd533208SMatthias Ringwald hfp_connection->command = parse_command((char *)hfp_connection->line_buffer, isHandsFree); 1211aa4dd815SMatthias Ringwald 121284a0c24eSMatthias Ringwald // pick +CIND version based on connection state: descriptions during SLC vs. states later 121384a0c24eSMatthias Ringwald if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC){ 1214a0ffb263SMatthias Ringwald switch(hfp_connection->state){ 1215aa4dd815SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS_STATUS: 1216a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS; 1217aa4dd815SMatthias Ringwald break; 1218aa4dd815SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS: 1219a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS; 1220aa4dd815SMatthias Ringwald break; 1221aa4dd815SMatthias Ringwald default: 122284a0c24eSMatthias Ringwald hfp_connection->command = HFP_CMD_UNKNOWN; 1223aa4dd815SMatthias Ringwald break; 1224aa4dd815SMatthias Ringwald } 1225aa4dd815SMatthias Ringwald } 12263deb3ec6SMatthias Ringwald 1227c0c0437aSMatthias Ringwald log_info("command string '%s', handsfree %u -> cmd id %u", (char *)hfp_connection->line_buffer, isHandsFree, hfp_connection->command); 1228c0c0437aSMatthias Ringwald 1229bfeaf344SMatthias Ringwald // next state 1230bfeaf344SMatthias Ringwald hfp_connection->found_equal_sign = false; 12311d81c7feSMatthias Ringwald hfp_parser_reset_line_buffer(hfp_connection); 1232dd533208SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE; 1233dd533208SMatthias Ringwald 1234bfeaf344SMatthias Ringwald return processed; 1235dd533208SMatthias Ringwald 1236bfeaf344SMatthias Ringwald case HFP_PARSER_CMD_SEQUENCE: 12371d81c7feSMatthias Ringwald // handle empty fields 12381d81c7feSMatthias Ringwald if ((byte == ',' ) && (hfp_connection->line_size == 0)){ 1239dd533208SMatthias Ringwald hfp_connection->line_buffer[0] = 0; 1240dd533208SMatthias Ringwald hfp_connection->ignore_value = 1; 1241dd533208SMatthias Ringwald parse_sequence(hfp_connection); 12423d051a53SMatthias Ringwald return true; 12433d051a53SMatthias Ringwald } 12443d051a53SMatthias Ringwald 12451d81c7feSMatthias Ringwald hfp_parser_store_if_token(hfp_connection, byte); 12461d81c7feSMatthias Ringwald if (!hfp_parser_is_separator(byte)) return true; 12471d81c7feSMatthias Ringwald 12481d81c7feSMatthias Ringwald // ignore empty tokens 12493d051a53SMatthias Ringwald if (hfp_parser_is_buffer_empty(hfp_connection) && (hfp_connection->ignore_value == 0)) return true; 12503d051a53SMatthias Ringwald 12513d051a53SMatthias Ringwald parse_sequence(hfp_connection); 12521d81c7feSMatthias Ringwald 12531d81c7feSMatthias Ringwald hfp_parser_reset_line_buffer(hfp_connection); 12541d81c7feSMatthias Ringwald 12551d81c7feSMatthias Ringwald switch (hfp_connection->command){ 12561d81c7feSMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 12571d81c7feSMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 12581d81c7feSMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 12591d81c7feSMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 12601d81c7feSMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 12611d81c7feSMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 12621d81c7feSMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 12631d81c7feSMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 12641d81c7feSMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 12651d81c7feSMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_SECOND_ITEM; 12661d81c7feSMatthias Ringwald break; 12671d81c7feSMatthias Ringwald default: 12681d81c7feSMatthias Ringwald break; 12691d81c7feSMatthias Ringwald } 12703d051a53SMatthias Ringwald return true; 12713d051a53SMatthias Ringwald 1272ba0de059SMatthias Ringwald case HFP_PARSER_SECOND_ITEM: 12731d81c7feSMatthias Ringwald 12741d81c7feSMatthias Ringwald hfp_parser_store_if_token(hfp_connection, byte); 12751d81c7feSMatthias Ringwald if (!hfp_parser_is_separator(byte)) return true; 1276dd533208SMatthias Ringwald 1277a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1278ce263fc8SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1279a0ffb263SMatthias Ringwald log_info("format %s, ", hfp_connection->line_buffer); 12802308e108SMilanka Ringwald hfp_connection->network_operator.format = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1281ce263fc8SMatthias Ringwald break; 1282ce263fc8SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 1283a0ffb263SMatthias Ringwald log_info("format %s \n", hfp_connection->line_buffer); 12842308e108SMilanka Ringwald hfp_connection->network_operator.format = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1285ce263fc8SMatthias Ringwald break; 1286ce263fc8SMatthias Ringwald case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 1287ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 1288ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 12892308e108SMilanka Ringwald hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].state = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1290ce263fc8SMatthias Ringwald break; 1291ce263fc8SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 12922308e108SMilanka Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1293a0ffb263SMatthias Ringwald log_info("%d \n", hfp_connection->ag_indicators[hfp_connection->parser_item_index].status); 1294a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].status_changed = 1; 1295ce263fc8SMatthias Ringwald break; 1296ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 12972308e108SMilanka Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].min_range = btstack_atoi((char *)hfp_connection->line_buffer); 1298a0ffb263SMatthias Ringwald log_info("%s, ", hfp_connection->line_buffer); 1299ce263fc8SMatthias Ringwald break; 1300ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 1301a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1302a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 13032308e108SMilanka Ringwald hfp_connection->bnip_type = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1304ce263fc8SMatthias Ringwald break; 13050222a807SMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 13060222a807SMatthias Ringwald hfp_connection->parser_indicator_value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 13070222a807SMatthias Ringwald break; 1308ce263fc8SMatthias Ringwald default: 1309ce263fc8SMatthias Ringwald break; 1310ce263fc8SMatthias Ringwald } 13111d81c7feSMatthias Ringwald 13121d81c7feSMatthias Ringwald hfp_parser_reset_line_buffer(hfp_connection); 13131d81c7feSMatthias Ringwald 13141d81c7feSMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_THIRD_ITEM; 13151d81c7feSMatthias Ringwald 1316ba0de059SMatthias Ringwald return true; 1317ce263fc8SMatthias Ringwald 131874c7548aSMatthias Ringwald case HFP_PARSER_THIRD_ITEM: 13191d81c7feSMatthias Ringwald 13201d81c7feSMatthias Ringwald hfp_parser_store_if_token(hfp_connection, byte); 13211d81c7feSMatthias Ringwald if (!hfp_parser_is_separator(byte)) return true; 13221d81c7feSMatthias Ringwald 1323a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1324ce263fc8SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 132589425bfcSMilanka Ringwald strncpy(hfp_connection->network_operator.name, (char *)hfp_connection->line_buffer, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE); 132689425bfcSMilanka Ringwald hfp_connection->network_operator.name[HFP_MAX_NETWORK_OPERATOR_NAME_SIZE - 1] = 0; 1327a0ffb263SMatthias Ringwald log_info("name %s\n", hfp_connection->line_buffer); 1328ce263fc8SMatthias Ringwald break; 1329ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 13302308e108SMilanka Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].max_range = btstack_atoi((char *)hfp_connection->line_buffer); 133125789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 133225789943SMilanka Ringwald hfp_connection->ag_indicators_nr = hfp_connection->parser_item_index; 1333a0ffb263SMatthias Ringwald log_info("%s)\n", hfp_connection->line_buffer); 1334ce263fc8SMatthias Ringwald break; 1335ce263fc8SMatthias Ringwald default: 1336ce263fc8SMatthias Ringwald break; 1337ce263fc8SMatthias Ringwald } 13381d81c7feSMatthias Ringwald 13391d81c7feSMatthias Ringwald hfp_parser_reset_line_buffer(hfp_connection); 13401d81c7feSMatthias Ringwald 13411d81c7feSMatthias Ringwald if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS){ 13421d81c7feSMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE; 13431d81c7feSMatthias Ringwald } else { 13441d81c7feSMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 13451d81c7feSMatthias Ringwald } 1346d6b237acSMatthias Ringwald return true; 134774c7548aSMatthias Ringwald 134874c7548aSMatthias Ringwald default: 134974c7548aSMatthias Ringwald btstack_assert(false); 135074c7548aSMatthias Ringwald return true; 135174c7548aSMatthias Ringwald } 1352d6b237acSMatthias Ringwald } 1353d6b237acSMatthias Ringwald 1354d6b237acSMatthias Ringwald void hfp_parse(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){ 1355d6b237acSMatthias Ringwald bool processed = false; 1356d6b237acSMatthias Ringwald while (!processed){ 1357d6b237acSMatthias Ringwald processed = hfp_parse_byte(hfp_connection, byte, isHandsFree); 1358d6b237acSMatthias Ringwald } 13591d81c7feSMatthias Ringwald // reset parser state on end-of-line 13601d81c7feSMatthias Ringwald if (hfp_parser_is_end_of_line(byte)){ 13611d81c7feSMatthias Ringwald hfp_connection->parser_item_index = 0; 13621d81c7feSMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 13631d81c7feSMatthias Ringwald } 1364ce263fc8SMatthias Ringwald } 1365ce263fc8SMatthias Ringwald 1366a0ffb263SMatthias Ringwald static void parse_sequence(hfp_connection_t * hfp_connection){ 1367ce263fc8SMatthias Ringwald int value; 1368a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1369667ec068SMatthias Ringwald case HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS: 13702308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1371667ec068SMatthias Ringwald int i; 1372a0ffb263SMatthias Ringwald switch (hfp_connection->parser_item_index){ 1373667ec068SMatthias Ringwald case 0: 1374a0ffb263SMatthias Ringwald for (i=0;i<hfp_connection->generic_status_indicators_nr;i++){ 1375a0ffb263SMatthias Ringwald if (hfp_connection->generic_status_indicators[i].uuid == value){ 1376a0ffb263SMatthias Ringwald hfp_connection->parser_indicator_index = i; 1377667ec068SMatthias Ringwald break; 1378667ec068SMatthias Ringwald } 1379667ec068SMatthias Ringwald } 1380667ec068SMatthias Ringwald break; 1381667ec068SMatthias Ringwald case 1: 1382a0ffb263SMatthias Ringwald if (hfp_connection->parser_indicator_index <0) break; 1383a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[hfp_connection->parser_indicator_index].state = value; 1384667ec068SMatthias Ringwald log_info("HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS set indicator at index %u, to %u\n", 1385a0ffb263SMatthias Ringwald hfp_connection->parser_item_index, value); 1386667ec068SMatthias Ringwald break; 1387667ec068SMatthias Ringwald default: 1388667ec068SMatthias Ringwald break; 1389667ec068SMatthias Ringwald } 139025789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 1391667ec068SMatthias Ringwald break; 1392667ec068SMatthias Ringwald 1393667ec068SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1394a0ffb263SMatthias Ringwald switch(hfp_connection->parser_item_index){ 1395667ec068SMatthias Ringwald case 0: 139628a4aea3SMilanka Ringwald // <alpha>: This optional field is not supported, and shall be left blank. 139728a4aea3SMilanka Ringwald break; 139828a4aea3SMilanka Ringwald case 1: 139928a4aea3SMilanka Ringwald // <number>: Quoted string containing the phone number in the format specified by <type>. 1400a0ffb263SMatthias Ringwald strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number)); 1401a0ffb263SMatthias Ringwald hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0; 1402667ec068SMatthias Ringwald break; 140328a4aea3SMilanka Ringwald case 2: 140428a4aea3SMilanka Ringwald /* 140528a4aea3SMilanka Ringwald <type> field specifies the format of the phone number provided, and can be one of the following values: 140628a4aea3SMilanka 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. 140728a4aea3SMilanka 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. 140828a4aea3SMilanka Ringwald - values 160-175: National number. No prefix nor escape digits included. 140928a4aea3SMilanka Ringwald */ 14102308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1411a0ffb263SMatthias Ringwald hfp_connection->bnip_type = value; 1412667ec068SMatthias Ringwald break; 141328a4aea3SMilanka Ringwald case 3: 141428a4aea3SMilanka Ringwald // <speed>: This optional field is not supported, and shall be left blank. 141528a4aea3SMilanka Ringwald break; 141628a4aea3SMilanka Ringwald case 4: 141728a4aea3SMilanka Ringwald // <service>: Indicates which service this phone number relates to. Shall be either 4 (voice) or 5 (fax). 1418667ec068SMatthias Ringwald default: 1419667ec068SMatthias Ringwald break; 1420667ec068SMatthias Ringwald } 1421eed67a37SMilanka Ringwald // index > 2 are ignored in switch above 1422a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1423667ec068SMatthias Ringwald break; 1424667ec068SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 1425a0ffb263SMatthias Ringwald switch(hfp_connection->parser_item_index){ 1426667ec068SMatthias Ringwald case 0: 14272308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1428a0ffb263SMatthias Ringwald hfp_connection->clcc_idx = value; 1429667ec068SMatthias Ringwald break; 1430667ec068SMatthias Ringwald case 1: 14312308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1432a0ffb263SMatthias Ringwald hfp_connection->clcc_dir = value; 1433667ec068SMatthias Ringwald break; 1434667ec068SMatthias Ringwald case 2: 14352308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1436a0ffb263SMatthias Ringwald hfp_connection->clcc_status = value; 1437667ec068SMatthias Ringwald break; 1438667ec068SMatthias Ringwald case 3: 14392308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 14400aee97efSMilanka Ringwald hfp_connection->clcc_mode = value; 1441667ec068SMatthias Ringwald break; 1442667ec068SMatthias Ringwald case 4: 14430aee97efSMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 14440aee97efSMilanka Ringwald hfp_connection->clcc_mpty = value; 14450aee97efSMilanka Ringwald break; 14460aee97efSMilanka Ringwald case 5: 1447a0ffb263SMatthias Ringwald strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number)); 1448a0ffb263SMatthias Ringwald hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0; 1449667ec068SMatthias Ringwald break; 14500aee97efSMilanka Ringwald case 6: 14512308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1452a0ffb263SMatthias Ringwald hfp_connection->bnip_type = value; 1453667ec068SMatthias Ringwald break; 1454667ec068SMatthias Ringwald default: 1455667ec068SMatthias Ringwald break; 1456667ec068SMatthias Ringwald } 1457eed67a37SMilanka Ringwald // index > 6 are ignored in switch above 1458a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1459667ec068SMatthias Ringwald break; 1460aa4dd815SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 14612308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1462a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = value; 1463aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CMD_SET_MICROPHONE_GAIN %d\n", value); 1464aa4dd815SMatthias Ringwald break; 1465aa4dd815SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 14662308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1467a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = value; 1468aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CMD_SET_SPEAKER_GAIN %d\n", value); 1469aa4dd815SMatthias Ringwald break; 1470aa4dd815SMatthias Ringwald case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION: 14712308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1472a0ffb263SMatthias Ringwald hfp_connection->ag_activate_voice_recognition = value; 1473aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION %d\n", value); 1474aa4dd815SMatthias Ringwald break; 1475aa4dd815SMatthias Ringwald case HFP_CMD_TURN_OFF_EC_AND_NR: 14762308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1477a0ffb263SMatthias Ringwald hfp_connection->ag_echo_and_noise_reduction = value; 1478aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CMD_TURN_OFF_EC_AND_NR %d\n", value); 1479aa4dd815SMatthias Ringwald break; 1480aa4dd815SMatthias Ringwald case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING: 14812308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1482a0ffb263SMatthias Ringwald hfp_connection->remote_supported_features = store_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE, value); 1483aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CHANGE_IN_BAND_RING_TONE_SETTING %d\n", value); 1484aa4dd815SMatthias Ringwald break; 14853deb3ec6SMatthias Ringwald case HFP_CMD_HF_CONFIRMED_CODEC: 14862308e108SMilanka Ringwald hfp_connection->codec_confirmed = btstack_atoi((char*)hfp_connection->line_buffer); 1487a0ffb263SMatthias Ringwald log_info("hfp parse HFP_CMD_HF_CONFIRMED_CODEC %d\n", hfp_connection->codec_confirmed); 14883deb3ec6SMatthias Ringwald break; 14893deb3ec6SMatthias Ringwald case HFP_CMD_AG_SUGGESTED_CODEC: 14902308e108SMilanka Ringwald hfp_connection->suggested_codec = btstack_atoi((char*)hfp_connection->line_buffer); 1491a0ffb263SMatthias Ringwald log_info("hfp parse HFP_CMD_AG_SUGGESTED_CODEC %d\n", hfp_connection->suggested_codec); 14923deb3ec6SMatthias Ringwald break; 14933deb3ec6SMatthias Ringwald case HFP_CMD_SUPPORTED_FEATURES: 14942308e108SMilanka Ringwald hfp_connection->remote_supported_features = btstack_atoi((char*)hfp_connection->line_buffer); 1495bace42efSMatthias Ringwald log_info("Parsed supported feature %d\n", (int) hfp_connection->remote_supported_features); 14963deb3ec6SMatthias Ringwald break; 14973deb3ec6SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 1498a0ffb263SMatthias Ringwald log_info("Parsed codec %s\n", hfp_connection->line_buffer); 14992308e108SMilanka Ringwald hfp_connection->remote_codecs[hfp_connection->parser_item_index] = (uint16_t)btstack_atoi((char*)hfp_connection->line_buffer); 150025789943SMilanka Ringwald hfp_next_codec_index(hfp_connection); 1501a0ffb263SMatthias Ringwald hfp_connection->remote_codecs_nr = hfp_connection->parser_item_index; 15023deb3ec6SMatthias Ringwald break; 1503aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 150489425bfcSMilanka Ringwald strncpy((char *)hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, (char *)hfp_connection->line_buffer, HFP_MAX_INDICATOR_DESC_SIZE); 150589425bfcSMilanka Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].name[HFP_MAX_INDICATOR_DESC_SIZE-1] = 0; 1506a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].index = hfp_connection->parser_item_index+1; 1507a0ffb263SMatthias Ringwald log_info("Indicator %d: %s (", hfp_connection->ag_indicators_nr+1, hfp_connection->line_buffer); 1508aa4dd815SMatthias Ringwald break; 1509aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 1510a0ffb263SMatthias Ringwald log_info("Parsed Indicator %d with status: %s\n", hfp_connection->parser_item_index+1, hfp_connection->line_buffer); 15112308e108SMilanka Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = btstack_atoi((char *) hfp_connection->line_buffer); 151225789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 15133deb3ec6SMatthias Ringwald break; 15143deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 151525789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 1516a0ffb263SMatthias Ringwald if (hfp_connection->parser_item_index != 4) break; 1517a0ffb263SMatthias Ringwald log_info("Parsed Enable indicators: %s\n", hfp_connection->line_buffer); 15182308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1519a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = (uint8_t) value; 15203deb3ec6SMatthias Ringwald break; 15213deb3ec6SMatthias Ringwald case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES: 1522a0ffb263SMatthias Ringwald log_info("Parsed Support call hold: %s\n", hfp_connection->line_buffer); 1523a0ffb263SMatthias Ringwald if (hfp_connection->line_size > 2 ) break; 152497d2cfbcSMatthias 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); 152525789943SMilanka Ringwald hfp_connection->remote_call_services[hfp_connection->remote_call_services_index].name[HFP_CALL_SERVICE_SIZE - 1] = 0; 1526eed67a37SMilanka Ringwald hfp_next_remote_call_services_index(hfp_connection); 15273deb3ec6SMatthias Ringwald break; 1528aa4dd815SMatthias Ringwald case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 1529aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 1530a0ffb263SMatthias Ringwald log_info("Parsed Generic status indicator: %s\n", hfp_connection->line_buffer); 15312308e108SMilanka Ringwald hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].uuid = (uint16_t)btstack_atoi((char*)hfp_connection->line_buffer); 153225789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 1533a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators_nr = hfp_connection->parser_item_index; 15343deb3ec6SMatthias Ringwald break; 1535aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 15363deb3ec6SMatthias Ringwald // HF parses inital AG gen. ind. state 1537a0ffb263SMatthias Ringwald log_info("Parsed List generic status indicator %s state: ", hfp_connection->line_buffer); 153825789943SMilanka Ringwald hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection); 15393deb3ec6SMatthias Ringwald break; 1540ce263fc8SMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 154125789943SMilanka Ringwald hfp_connection->parser_indicator_index = hfp_parse_indicator_index(hfp_connection); 1542a0ffb263SMatthias Ringwald log_info("Parsed HF indicator index %u", hfp_connection->parser_indicator_index); 1543ce263fc8SMatthias Ringwald break; 15443deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE: 15453deb3ec6SMatthias Ringwald // AG parses new gen. ind. state 1546a0ffb263SMatthias Ringwald if (hfp_connection->ignore_value){ 1547a0ffb263SMatthias Ringwald hfp_connection->ignore_value = 0; 1548a0ffb263SMatthias Ringwald log_info("Parsed Enable AG indicator pos %u('%s') - unchanged (stays %u)\n", hfp_connection->parser_item_index, 1549a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled); 1550ce263fc8SMatthias Ringwald } 1551a0ffb263SMatthias Ringwald else if (hfp_connection->ag_indicators[hfp_connection->parser_item_index].mandatory){ 1552ce263fc8SMatthias Ringwald log_info("Parsed Enable AG indicator pos %u('%s') - ignore (mandatory)\n", 1553a0ffb263SMatthias Ringwald hfp_connection->parser_item_index, hfp_connection->ag_indicators[hfp_connection->parser_item_index].name); 1554ce263fc8SMatthias Ringwald } else { 15552308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1556a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled = value; 1557a0ffb263SMatthias Ringwald log_info("Parsed Enable AG indicator pos %u('%s'): %u\n", hfp_connection->parser_item_index, 1558a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, value); 15593deb3ec6SMatthias Ringwald } 156025789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 15613deb3ec6SMatthias Ringwald break; 15623deb3ec6SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 15633deb3ec6SMatthias Ringwald // indicators are indexed starting with 1 156425789943SMilanka Ringwald hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection); 1565a0ffb263SMatthias Ringwald log_info("Parsed status of the AG indicator %d, status ", hfp_connection->parser_item_index); 15663deb3ec6SMatthias Ringwald break; 1567aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 15682308e108SMilanka Ringwald hfp_connection->network_operator.mode = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1569a0ffb263SMatthias Ringwald log_info("Parsed network operator mode: %d, ", hfp_connection->network_operator.mode); 1570aa4dd815SMatthias Ringwald break; 1571aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 1572a0ffb263SMatthias Ringwald if (hfp_connection->line_buffer[0] == '3'){ 1573a0ffb263SMatthias Ringwald log_info("Parsed Set network operator format : %s, ", hfp_connection->line_buffer); 15743deb3ec6SMatthias Ringwald break; 15753deb3ec6SMatthias Ringwald } 15763deb3ec6SMatthias Ringwald // TODO emit ERROR, wrong format 1577a0ffb263SMatthias Ringwald log_info("ERROR Set network operator format: index %s not supported\n", hfp_connection->line_buffer); 15783deb3ec6SMatthias Ringwald break; 15793deb3ec6SMatthias Ringwald case HFP_CMD_ERROR: 15803deb3ec6SMatthias Ringwald break; 15813deb3ec6SMatthias Ringwald case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1582a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 1; 15832308e108SMilanka Ringwald hfp_connection->extended_audio_gateway_error_value = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 15843deb3ec6SMatthias Ringwald break; 15853deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR: 15862308e108SMilanka Ringwald hfp_connection->enable_extended_audio_gateway_error_report = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1587a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1588a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 15893deb3ec6SMatthias Ringwald break; 1590ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 1591a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1592a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1593a0ffb263SMatthias Ringwald strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number)); 1594a0ffb263SMatthias Ringwald hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0; 15953deb3ec6SMatthias Ringwald break; 15960222a807SMatthias Ringwald case HFP_CMD_CALL_HOLD: 15970222a807SMatthias Ringwald hfp_connection->ag_call_hold_action = hfp_connection->line_buffer[0] - '0'; 15980222a807SMatthias Ringwald if (hfp_connection->line_buffer[1] != '\0'){ 15990222a807SMatthias Ringwald hfp_connection->call_index = btstack_atoi((char *)&hfp_connection->line_buffer[1]); 16000222a807SMatthias Ringwald } 16010222a807SMatthias Ringwald break; 16020222a807SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_COMMAND: 16030222a807SMatthias Ringwald hfp_connection->ag_response_and_hold_action = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 16040222a807SMatthias Ringwald break; 16050222a807SMatthias Ringwald case HFP_CMD_TRANSMIT_DTMF_CODES: 16060222a807SMatthias Ringwald hfp_connection->ag_dtmf_code = hfp_connection->line_buffer[0]; 16070222a807SMatthias Ringwald break; 16080222a807SMatthias Ringwald case HFP_CMD_ENABLE_CLIP: 16090222a807SMatthias Ringwald hfp_connection->clip_enabled = hfp_connection->line_buffer[0] != '0'; 16100222a807SMatthias Ringwald break; 16110222a807SMatthias Ringwald case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION: 16120222a807SMatthias Ringwald hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[0] != '0'; 16130222a807SMatthias Ringwald break; 1614*db3cdbd4SMilanka Ringwald case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION: 1615*db3cdbd4SMilanka Ringwald switch(hfp_connection->parser_item_index){ 1616*db3cdbd4SMilanka Ringwald case 0: 1617*db3cdbd4SMilanka Ringwald hfp_connection->ag_vra_status = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1618*db3cdbd4SMilanka Ringwald break; 1619*db3cdbd4SMilanka Ringwald case 1: 1620*db3cdbd4SMilanka Ringwald hfp_connection->ag_vra_state = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1621*db3cdbd4SMilanka Ringwald hfp_emit_enhanced_voice_recognition_state(hfp_connection); 1622*db3cdbd4SMilanka Ringwald break; 1623*db3cdbd4SMilanka Ringwald case 2: 1624*db3cdbd4SMilanka Ringwald hfp_connection->ag_text_id = 0; 1625*db3cdbd4SMilanka Ringwald for (i = 0 ; i < 4; i++){ 1626*db3cdbd4SMilanka Ringwald hfp_connection->ag_text_id = (hfp_connection->ag_text_id << 4) | nibble_for_char(hfp_connection->line_buffer[i]); 1627*db3cdbd4SMilanka Ringwald } 1628*db3cdbd4SMilanka Ringwald printf("text ID 0x%04X\n", hfp_connection->ag_text_id); 1629*db3cdbd4SMilanka Ringwald break; 1630*db3cdbd4SMilanka Ringwald case 3: 1631*db3cdbd4SMilanka Ringwald hfp_connection->ag_text_operation = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1632*db3cdbd4SMilanka Ringwald break; 1633*db3cdbd4SMilanka Ringwald case 4: 1634*db3cdbd4SMilanka Ringwald hfp_connection->ag_text_type = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1635*db3cdbd4SMilanka Ringwald break; 1636*db3cdbd4SMilanka Ringwald case 5: 1637*db3cdbd4SMilanka Ringwald printf("text%s\n", hfp_connection->line_buffer); 1638*db3cdbd4SMilanka Ringwald hfp_emit_enhanced_voice_recognition_text(hfp_connection, hfp_connection->line_size, &hfp_connection->line_buffer[0]); 1639*db3cdbd4SMilanka Ringwald break; 1640*db3cdbd4SMilanka Ringwald default: 1641*db3cdbd4SMilanka Ringwald break; 1642*db3cdbd4SMilanka Ringwald } 1643*db3cdbd4SMilanka Ringwald break; 16443deb3ec6SMatthias Ringwald default: 16453deb3ec6SMatthias Ringwald break; 16463deb3ec6SMatthias Ringwald } 16473deb3ec6SMatthias Ringwald } 16483deb3ec6SMatthias Ringwald 16491d9c9c90SMilanka Ringwald static void hfp_handle_start_sdp_client_query(void * context){ 16501d9c9c90SMilanka Ringwald UNUSED(context); 16511d9c9c90SMilanka Ringwald 16521d9c9c90SMilanka Ringwald btstack_linked_list_iterator_t it; 16531d9c9c90SMilanka Ringwald btstack_linked_list_iterator_init(&it, &hfp_connections); 16541d9c9c90SMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 16551d9c9c90SMilanka Ringwald hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 16561d9c9c90SMilanka Ringwald 16571d9c9c90SMilanka Ringwald if (connection->state != HFP_W2_SEND_SDP_QUERY) continue; 16581d9c9c90SMilanka Ringwald 16591d9c9c90SMilanka Ringwald connection->state = HFP_W4_SDP_QUERY_COMPLETE; 16601d9c9c90SMilanka Ringwald sdp_query_context.local_role = connection->local_role; 16611d9c9c90SMilanka Ringwald (void)memcpy(sdp_query_context.remote_address, connection->remote_addr, 6); 1662e55c05e3SMatthias Ringwald sdp_client_query_rfcomm_channel_and_name_for_service_class_uuid(&handle_query_rfcomm_event, connection->remote_addr, connection->service_uuid); 16631d9c9c90SMilanka Ringwald return; 16641d9c9c90SMilanka Ringwald } 16651d9c9c90SMilanka Ringwald } 16661d9c9c90SMilanka Ringwald 1667323d3000SMatthias Ringwald void hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid, hfp_role_t local_role){ 1668323d3000SMatthias Ringwald hfp_connection_t * hfp_connection = provide_hfp_connection_context_for_bd_addr(bd_addr, local_role); 1669a0ffb263SMatthias Ringwald log_info("hfp_connect %s, hfp_connection %p", bd_addr_to_str(bd_addr), hfp_connection); 16703deb3ec6SMatthias Ringwald 1671a0ffb263SMatthias Ringwald if (!hfp_connection) { 16723deb3ec6SMatthias Ringwald log_error("hfp_establish_service_level_connection for addr %s failed", bd_addr_to_str(bd_addr)); 16733deb3ec6SMatthias Ringwald return; 16743deb3ec6SMatthias Ringwald } 1675a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 16763deb3ec6SMatthias Ringwald case HFP_W2_DISCONNECT_RFCOMM: 1677a0ffb263SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 16783deb3ec6SMatthias Ringwald return; 16793deb3ec6SMatthias Ringwald case HFP_W4_RFCOMM_DISCONNECTED: 1680a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART; 16813deb3ec6SMatthias Ringwald return; 16823deb3ec6SMatthias Ringwald case HFP_IDLE: 16836535961aSMatthias Ringwald (void)memcpy(hfp_connection->remote_addr, bd_addr, 6); 16841d9c9c90SMilanka Ringwald hfp_connection->state = HFP_W2_SEND_SDP_QUERY; 1685a0ffb263SMatthias Ringwald hfp_connection->service_uuid = service_uuid; 16861d9c9c90SMilanka Ringwald 16871d9c9c90SMilanka Ringwald hfp_handle_sdp_client_query_request.callback = &hfp_handle_start_sdp_client_query; 16881d9c9c90SMilanka Ringwald // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback 16891d9c9c90SMilanka Ringwald (void) sdp_client_register_query_callback(&hfp_handle_sdp_client_query_request); 16903deb3ec6SMatthias Ringwald break; 16913deb3ec6SMatthias Ringwald default: 16923deb3ec6SMatthias Ringwald break; 16933deb3ec6SMatthias Ringwald } 16943deb3ec6SMatthias Ringwald } 16953deb3ec6SMatthias Ringwald 1696a0ffb263SMatthias Ringwald void hfp_release_service_level_connection(hfp_connection_t * hfp_connection){ 1697a0ffb263SMatthias Ringwald if (!hfp_connection) return; 1698a0ffb263SMatthias Ringwald hfp_release_audio_connection(hfp_connection); 16993deb3ec6SMatthias Ringwald 1700a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_W4_RFCOMM_CONNECTED){ 1701a0ffb263SMatthias Ringwald hfp_connection->state = HFP_IDLE; 17023deb3ec6SMatthias Ringwald return; 17033deb3ec6SMatthias Ringwald } 17043deb3ec6SMatthias Ringwald 1705a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_W4_RFCOMM_CONNECTED){ 1706a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN; 17073deb3ec6SMatthias Ringwald return; 17083deb3ec6SMatthias Ringwald } 17093deb3ec6SMatthias Ringwald 17100c3047fbSMatthias Ringwald if (hfp_connection->state < HFP_W4_SCO_CONNECTED){ 17110c3047fbSMatthias Ringwald hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM; 17120c3047fbSMatthias Ringwald return; 17130c3047fbSMatthias Ringwald } 17140c3047fbSMatthias Ringwald 17150c3047fbSMatthias Ringwald if (hfp_connection->state < HFP_W4_SCO_DISCONNECTED){ 17160c3047fbSMatthias Ringwald hfp_connection->state = HFP_W2_DISCONNECT_SCO; 17170c3047fbSMatthias Ringwald return; 17180c3047fbSMatthias Ringwald } 17190c3047fbSMatthias Ringwald 17200c3047fbSMatthias Ringwald // HFP_W4_SCO_DISCONNECTED or later 17215f053052SMilanka Ringwald hfp_connection->release_slc_connection = 1; 17223deb3ec6SMatthias Ringwald } 17233deb3ec6SMatthias Ringwald 1724a0ffb263SMatthias Ringwald void hfp_release_audio_connection(hfp_connection_t * hfp_connection){ 1725a0ffb263SMatthias Ringwald if (!hfp_connection) return; 1726a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return; 1727a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 1; 17283deb3ec6SMatthias Ringwald } 17293deb3ec6SMatthias Ringwald 1730ce263fc8SMatthias Ringwald static const struct link_settings { 1731ce263fc8SMatthias Ringwald const uint16_t max_latency; 1732ce263fc8SMatthias Ringwald const uint8_t retransmission_effort; 1733ce263fc8SMatthias Ringwald const uint16_t packet_types; 1734ebc6f15bSMatthias Ringwald const bool eSCO; 1735ebc6f15bSMatthias Ringwald const uint8_t codec; 1736ce263fc8SMatthias Ringwald } hfp_link_settings [] = { 1737352a0504SMatthias Ringwald { 0xffff, 0xff, SCO_PACKET_TYPES_HV1, false, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_D0 1738352a0504SMatthias Ringwald { 0xffff, 0xff, SCO_PACKET_TYPES_HV3, false, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_D1 1739352a0504SMatthias Ringwald { 0x0007, 0x01, SCO_PACKET_TYPES_EV3, true, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S1 1740352a0504SMatthias Ringwald { 0x0007, 0x01, SCO_PACKET_TYPES_2EV3, true, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S2 1741352a0504SMatthias Ringwald { 0x000a, 0x01, SCO_PACKET_TYPES_2EV3, true, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S3 1742352a0504SMatthias Ringwald { 0x000c, 0x02, SCO_PACKET_TYPES_2EV3, true, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S4 1743352a0504SMatthias Ringwald { 0x0008, 0x02, SCO_PACKET_TYPES_EV3, true, HFP_CODEC_MSBC }, // HFP_LINK_SETTINGS_T1 1744352a0504SMatthias Ringwald { 0x000d, 0x02, SCO_PACKET_TYPES_2EV3, true, HFP_CODEC_MSBC } // HFP_LINK_SETTINGS_T2 1745ce263fc8SMatthias Ringwald }; 17463deb3ec6SMatthias Ringwald 1747eddcd308SMatthias Ringwald void hfp_setup_synchronous_connection(hfp_connection_t * hfp_connection){ 1748ce263fc8SMatthias Ringwald // all packet types, fixed bandwidth 1749eddcd308SMatthias Ringwald int setting = hfp_connection->link_setting; 1750ce263fc8SMatthias Ringwald log_info("hfp_setup_synchronous_connection using setting nr %u", setting); 1751eddcd308SMatthias Ringwald sco_establishment_active = hfp_connection; 1752d2c1d59aSMatthias Ringwald uint16_t sco_voice_setting = hci_get_sco_voice_setting(); 1753d2c1d59aSMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 1754689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 1755689d4323SMatthias Ringwald sco_voice_setting = 0x0063; // Transparent data, 16-bit for BCM controllers 1756689d4323SMatthias Ringwald #else 1757689d4323SMatthias Ringwald sco_voice_setting = 0x0043; // Transparent data, 8-bit otherwise 1758689d4323SMatthias Ringwald #endif 1759d2c1d59aSMatthias Ringwald } 1760352a0504SMatthias Ringwald // get packet types - bits 6-9 are 'don't allow' 1761352a0504SMatthias Ringwald uint16_t packet_types = hfp_link_settings[setting].packet_types ^ 0x03c0; 1762eddcd308SMatthias Ringwald hci_send_cmd(&hci_setup_synchronous_connection, hfp_connection->acl_handle, 8000, 8000, hfp_link_settings[setting].max_latency, 1763352a0504SMatthias Ringwald sco_voice_setting, hfp_link_settings[setting].retransmission_effort, packet_types); 1764ce263fc8SMatthias Ringwald } 1765d68dcce1SMatthias Ringwald 1766cb81d35dSMatthias Ringwald void hfp_accept_synchronous_connection(hfp_connection_t * hfp_connection, bool incoming_eSCO){ 1767cb81d35dSMatthias Ringwald 1768cb81d35dSMatthias Ringwald // remote supported feature eSCO is set if link type is eSCO 1769cb81d35dSMatthias Ringwald // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms, 1770cb81d35dSMatthias Ringwald uint16_t max_latency; 1771cb81d35dSMatthias Ringwald uint8_t retransmission_effort; 1772cb81d35dSMatthias Ringwald uint16_t packet_types; 1773cb81d35dSMatthias Ringwald 1774cb81d35dSMatthias Ringwald if (incoming_eSCO && hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){ 1775cb81d35dSMatthias Ringwald max_latency = 0x000c; 1776cb81d35dSMatthias Ringwald retransmission_effort = 0x02; 1777cb81d35dSMatthias Ringwald // eSCO: EV3 and 2-EV3 1778cb81d35dSMatthias Ringwald packet_types = 0x0048; 1779cb81d35dSMatthias Ringwald } else { 1780cb81d35dSMatthias Ringwald max_latency = 0xffff; 1781cb81d35dSMatthias Ringwald retransmission_effort = 0xff; 1782cb81d35dSMatthias Ringwald // sco: HV1 and HV3 1783cb81d35dSMatthias Ringwald packet_types = 0x005; 1784cb81d35dSMatthias Ringwald } 1785cb81d35dSMatthias Ringwald 1786cb81d35dSMatthias Ringwald // mSBC only allows for transparent data 1787cb81d35dSMatthias Ringwald uint16_t sco_voice_setting = hci_get_sco_voice_setting(); 1788cb81d35dSMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 1789cb81d35dSMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 1790cb81d35dSMatthias Ringwald sco_voice_setting = 0x0063; // Transparent data, 16-bit for BCM controllers 1791cb81d35dSMatthias Ringwald #else 1792cb81d35dSMatthias Ringwald sco_voice_setting = 0x0043; // Transparent data, 8-bit otherwise 1793cb81d35dSMatthias Ringwald #endif 1794cb81d35dSMatthias Ringwald } 1795cb81d35dSMatthias Ringwald 1796cb81d35dSMatthias Ringwald // filter packet types 1797cb81d35dSMatthias Ringwald packet_types &= hfp_get_sco_packet_types(); 1798cb81d35dSMatthias Ringwald 1799cb81d35dSMatthias Ringwald // bits 6-9 are 'don't allow' 1800cb81d35dSMatthias Ringwald packet_types ^= 0x3c0; 1801cb81d35dSMatthias Ringwald 1802cb81d35dSMatthias Ringwald log_info("HFP: sending hci_accept_connection_request, packet types 0x%04x, sco_voice_setting 0x%02x", packet_types, sco_voice_setting); 1803cb81d35dSMatthias Ringwald hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency, 1804cb81d35dSMatthias Ringwald sco_voice_setting, retransmission_effort, packet_types); 1805cb81d35dSMatthias Ringwald } 1806cb81d35dSMatthias Ringwald 18073721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP 18083721a235SMatthias Ringwald void hfp_cc256x_prepare_for_sco(hfp_connection_t * hfp_connection){ 18093721a235SMatthias Ringwald hfp_connection->cc256x_send_write_codec_config = true; 18103721a235SMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 18113721a235SMatthias Ringwald hfp_connection->cc256x_send_wbs_associate = true; 18123721a235SMatthias Ringwald } 18133721a235SMatthias Ringwald } 18143721a235SMatthias Ringwald 18153721a235SMatthias Ringwald void hfp_cc256x_write_codec_config(hfp_connection_t * hfp_connection){ 18163721a235SMatthias Ringwald uint32_t sample_rate_hz; 18173721a235SMatthias Ringwald uint16_t clock_rate_khz; 18183721a235SMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 18193721a235SMatthias Ringwald clock_rate_khz = 512; 18203721a235SMatthias Ringwald sample_rate_hz = 16000; 18213721a235SMatthias Ringwald } else { 18223721a235SMatthias Ringwald clock_rate_khz = 256; 18233721a235SMatthias Ringwald sample_rate_hz = 8000; 18243721a235SMatthias Ringwald } 18253721a235SMatthias Ringwald uint8_t clock_direction = 0; // master 18263721a235SMatthias Ringwald uint16_t frame_sync_duty_cycle = 0; // i2s with 50% 18273721a235SMatthias Ringwald uint8_t frame_sync_edge = 1; // rising edge 18283721a235SMatthias Ringwald uint8_t frame_sync_polarity = 0; // active high 18293721a235SMatthias Ringwald uint8_t reserved = 0; 18303721a235SMatthias Ringwald uint16_t size = 16; 18313721a235SMatthias Ringwald uint16_t chan_1_offset = 1; 18323721a235SMatthias Ringwald uint16_t chan_2_offset = chan_1_offset + size; 18333721a235SMatthias Ringwald uint8_t out_edge = 1; // rising 18343721a235SMatthias Ringwald uint8_t in_edge = 0; // falling 18353721a235SMatthias Ringwald hci_send_cmd(&hci_ti_write_codec_config, clock_rate_khz, clock_direction, sample_rate_hz, frame_sync_duty_cycle, 18363721a235SMatthias Ringwald frame_sync_edge, frame_sync_polarity, reserved, 18373721a235SMatthias Ringwald size, chan_1_offset, out_edge, size, chan_1_offset, in_edge, reserved, 18383721a235SMatthias Ringwald size, chan_2_offset, out_edge, size, chan_2_offset, in_edge, reserved); 18393721a235SMatthias Ringwald } 18403721a235SMatthias Ringwald #endif 18413721a235SMatthias Ringwald 1842689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 1843689d4323SMatthias Ringwald void hfp_bcm_prepare_for_sco(hfp_connection_t * hfp_connection){ 1844689d4323SMatthias Ringwald hfp_connection->bcm_send_write_i2spcm_interface_param = true; 1845689d4323SMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 1846689d4323SMatthias Ringwald hfp_connection->bcm_send_enable_wbs = true; 1847689d4323SMatthias Ringwald } 1848689d4323SMatthias Ringwald } 1849689d4323SMatthias Ringwald void hfp_bcm_write_i2spcm_interface_param(hfp_connection_t * hfp_connection){ 1850689d4323SMatthias Ringwald uint8_t sample_rate = (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? 1 : 0; 18511d2bbd54SMatthias Ringwald // i2s enable, master, 8/16 kHz, 512 kHz 18521d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_i2spcm_interface_paramhci_bcm_write_i2spcm_interface_param, 1, 1, sample_rate, 2); 1853689d4323SMatthias Ringwald } 1854689d4323SMatthias Ringwald #endif 1855689d4323SMatthias Ringwald 18561b005648SMatthias Ringwald void hfp_set_hf_callback(btstack_packet_handler_t callback){ 18571b005648SMatthias Ringwald hfp_hf_callback = callback; 18581b005648SMatthias Ringwald } 18591b005648SMatthias Ringwald 18601b005648SMatthias Ringwald void hfp_set_ag_callback(btstack_packet_handler_t callback){ 18611b005648SMatthias Ringwald hfp_ag_callback = callback; 18621b005648SMatthias Ringwald } 18631b005648SMatthias Ringwald 1864ca59be51SMatthias Ringwald void hfp_set_ag_rfcomm_packet_handler(btstack_packet_handler_t handler){ 1865ca59be51SMatthias Ringwald hfp_ag_rfcomm_packet_handler = handler; 1866ca59be51SMatthias Ringwald } 18671b005648SMatthias Ringwald 1868ca59be51SMatthias Ringwald void hfp_set_hf_rfcomm_packet_handler(btstack_packet_handler_t handler){ 1869ca59be51SMatthias Ringwald hfp_hf_rfcomm_packet_handler = handler; 1870d68dcce1SMatthias Ringwald } 1871d68dcce1SMatthias Ringwald 1872520c92d5SMatthias Ringwald void hfp_init(void){ 1873352a0504SMatthias Ringwald hfp_allowed_sco_packet_types = SCO_PACKET_TYPES_ALL; 1874991c26beSMatthias Ringwald } 1875991c26beSMatthias Ringwald 187620b2edb6SMatthias Ringwald void hfp_deinit(void){ 187720b2edb6SMatthias Ringwald hfp_connections = NULL; 187820b2edb6SMatthias Ringwald hfp_hf_callback = NULL; 187920b2edb6SMatthias Ringwald hfp_ag_callback = NULL; 188020b2edb6SMatthias Ringwald hfp_hf_rfcomm_packet_handler = NULL; 188120b2edb6SMatthias Ringwald hfp_ag_rfcomm_packet_handler = NULL; 188220b2edb6SMatthias Ringwald sco_establishment_active = NULL; 188320b2edb6SMatthias Ringwald (void) memset(&sdp_query_context, 0, sizeof(hfp_sdp_query_context_t)); 188420b2edb6SMatthias Ringwald (void) memset(&hfp_handle_sdp_client_query_request, 0, sizeof(btstack_context_callback_registration_t)); 188520b2edb6SMatthias Ringwald } 188620b2edb6SMatthias Ringwald 1887991c26beSMatthias Ringwald void hfp_set_sco_packet_types(uint16_t packet_types){ 188801e5b727SMatthias Ringwald hfp_allowed_sco_packet_types = packet_types; 1889991c26beSMatthias Ringwald } 1890991c26beSMatthias Ringwald 1891991c26beSMatthias Ringwald uint16_t hfp_get_sco_packet_types(void){ 189201e5b727SMatthias Ringwald return hfp_allowed_sco_packet_types; 1893520c92d5SMatthias Ringwald } 1894186dd3d2SMatthias Ringwald 1895e453c1d9SMatthias 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){ 1896e453c1d9SMatthias Ringwald int8_t setting = (int8_t) current_setting; 1897e453c1d9SMatthias Ringwald bool can_use_eSCO = local_eSCO_supported && remote_eSCO_supported; 1898e453c1d9SMatthias Ringwald while (setting > 0){ 1899e453c1d9SMatthias Ringwald setting--; 1900e453c1d9SMatthias Ringwald // skip if eSCO required but not available 1901e453c1d9SMatthias Ringwald if (hfp_link_settings[setting].eSCO && !can_use_eSCO) continue; 1902e453c1d9SMatthias Ringwald // skip if S4 but not supported 1903e453c1d9SMatthias Ringwald if ((setting == (int8_t) HFP_LINK_SETTINGS_S4) && !eSCO_S4_supported) continue; 1904e453c1d9SMatthias Ringwald // skip wrong codec 1905e453c1d9SMatthias Ringwald if ( hfp_link_settings[setting].codec != negotiated_codec) continue; 190601e5b727SMatthias Ringwald // skip disabled packet types 1907352a0504SMatthias Ringwald uint16_t required_packet_types = hfp_link_settings[setting].packet_types; 1908352a0504SMatthias Ringwald uint16_t allowed_packet_types = hfp_allowed_sco_packet_types; 1909352a0504SMatthias Ringwald if ((required_packet_types & allowed_packet_types) == 0) continue; 191001e5b727SMatthias Ringwald 1911e453c1d9SMatthias Ringwald // found matching setting 1912e453c1d9SMatthias Ringwald return (hfp_link_settings_t) setting; 1913afac2a04SMilanka Ringwald } 1914e453c1d9SMatthias Ringwald return HFP_LINK_SETTINGS_NONE; 1915afac2a04SMilanka Ringwald } 1916e453c1d9SMatthias Ringwald 19172b7abbfbSMatthias 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){ 1918e453c1d9SMatthias Ringwald bool local_eSCO_supported = hci_extended_sco_link_supported(); 1919e453c1d9SMatthias Ringwald bool remote_eSCO_supported = hci_remote_esco_supported(hfp_connection->acl_handle); 1920e453c1d9SMatthias Ringwald uint8_t negotiated_codec = hfp_connection->negotiated_codec; 19212b7abbfbSMatthias Ringwald return hfp_next_link_setting(current_setting, local_eSCO_supported, remote_eSCO_supported, eSCO_S4_supported, negotiated_codec); 19222b7abbfbSMatthias Ringwald } 19232b7abbfbSMatthias Ringwald 19242b7abbfbSMatthias Ringwald void hfp_init_link_settings(hfp_connection_t * hfp_connection, uint8_t eSCO_S4_supported){ 1925e453c1d9SMatthias Ringwald // get highest possible link setting 19262b7abbfbSMatthias Ringwald hfp_connection->link_setting = hfp_next_link_setting_for_connection(HFP_LINK_SETTINGS_NONE, hfp_connection, eSCO_S4_supported); 1927afac2a04SMilanka Ringwald log_info("hfp_init_link_settings: %u", hfp_connection->link_setting); 1928afac2a04SMilanka Ringwald } 1929afac2a04SMilanka Ringwald 1930186dd3d2SMatthias Ringwald #define HFP_HF_RX_DEBUG_PRINT_LINE 80 1931186dd3d2SMatthias Ringwald 1932186dd3d2SMatthias Ringwald void hfp_log_rfcomm_message(const char * tag, uint8_t * packet, uint16_t size){ 1933186dd3d2SMatthias Ringwald #ifdef ENABLE_LOG_INFO 1934186dd3d2SMatthias Ringwald // encode \n\r 1935186dd3d2SMatthias Ringwald char printable[HFP_HF_RX_DEBUG_PRINT_LINE+2]; 193615a27967SMatthias Ringwald int i = 0; 1937186dd3d2SMatthias Ringwald int pos; 193815a27967SMatthias Ringwald for (pos=0 ; (pos < size) && (i < (HFP_HF_RX_DEBUG_PRINT_LINE - 3)) ; pos++){ 1939186dd3d2SMatthias Ringwald switch (packet[pos]){ 1940186dd3d2SMatthias Ringwald case '\n': 1941186dd3d2SMatthias Ringwald printable[i++] = '\\'; 1942186dd3d2SMatthias Ringwald printable[i++] = 'n'; 1943186dd3d2SMatthias Ringwald break; 1944186dd3d2SMatthias Ringwald case '\r': 1945186dd3d2SMatthias Ringwald printable[i++] = '\\'; 1946186dd3d2SMatthias Ringwald printable[i++] = 'r'; 1947186dd3d2SMatthias Ringwald break; 1948186dd3d2SMatthias Ringwald default: 1949186dd3d2SMatthias Ringwald printable[i++] = packet[pos]; 1950186dd3d2SMatthias Ringwald break; 1951186dd3d2SMatthias Ringwald } 1952186dd3d2SMatthias Ringwald } 1953186dd3d2SMatthias Ringwald printable[i] = 0; 1954186dd3d2SMatthias Ringwald log_info("%s: '%s'", tag, printable); 1955186dd3d2SMatthias Ringwald #endif 1956186dd3d2SMatthias Ringwald } 1957