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 <stdio.h> 453deb3ec6SMatthias Ringwald #include <stdlib.h> 463deb3ec6SMatthias Ringwald #include <string.h> 473deb3ec6SMatthias Ringwald #include <inttypes.h> 483deb3ec6SMatthias Ringwald 49235946f1SMatthias Ringwald #include "bluetooth_sdp.h" 50023f2764SMatthias Ringwald #include "btstack_debug.h" 5159c6af15SMatthias Ringwald #include "btstack_event.h" 5259c6af15SMatthias Ringwald #include "btstack_memory.h" 5359c6af15SMatthias Ringwald #include "btstack_run_loop.h" 5459c6af15SMatthias Ringwald #include "classic/core.h" 55efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h" 56746ccb7eSMatthias Ringwald #include "classic/sdp_server.h" 57023f2764SMatthias Ringwald #include "classic/sdp_util.h" 5859c6af15SMatthias Ringwald #include "hci.h" 5959c6af15SMatthias Ringwald #include "hci_cmd.h" 6059c6af15SMatthias Ringwald #include "hci_dump.h" 6159c6af15SMatthias Ringwald #include "l2cap.h" 623deb3ec6SMatthias Ringwald 633deb3ec6SMatthias Ringwald #define HFP_HF_FEATURES_SIZE 10 643deb3ec6SMatthias Ringwald #define HFP_AG_FEATURES_SIZE 12 653deb3ec6SMatthias Ringwald 663deb3ec6SMatthias Ringwald 673deb3ec6SMatthias Ringwald static const char * hfp_hf_features[] = { 683deb3ec6SMatthias Ringwald "EC and/or NR function", 693deb3ec6SMatthias Ringwald "Three-way calling", 703deb3ec6SMatthias Ringwald "CLI presentation capability", 713deb3ec6SMatthias Ringwald "Voice recognition activation", 723deb3ec6SMatthias Ringwald "Remote volume control", 733deb3ec6SMatthias Ringwald 743deb3ec6SMatthias Ringwald "Enhanced call status", 753deb3ec6SMatthias Ringwald "Enhanced call control", 763deb3ec6SMatthias Ringwald 773deb3ec6SMatthias Ringwald "Codec negotiation", 783deb3ec6SMatthias Ringwald 793deb3ec6SMatthias Ringwald "HF Indicators", 803deb3ec6SMatthias Ringwald "eSCO S4 (and T2) Settings Supported", 813deb3ec6SMatthias Ringwald "Reserved for future definition" 823deb3ec6SMatthias Ringwald }; 833deb3ec6SMatthias Ringwald 843deb3ec6SMatthias Ringwald static const char * hfp_ag_features[] = { 853deb3ec6SMatthias Ringwald "Three-way calling", 863deb3ec6SMatthias Ringwald "EC and/or NR function", 873deb3ec6SMatthias Ringwald "Voice recognition function", 883deb3ec6SMatthias Ringwald "In-band ring tone capability", 893deb3ec6SMatthias Ringwald "Attach a number to a voice tag", 903deb3ec6SMatthias Ringwald "Ability to reject a call", 913deb3ec6SMatthias Ringwald "Enhanced call status", 923deb3ec6SMatthias Ringwald "Enhanced call control", 933deb3ec6SMatthias Ringwald "Extended Error Result Codes", 943deb3ec6SMatthias Ringwald "Codec negotiation", 953deb3ec6SMatthias Ringwald "HF Indicators", 963deb3ec6SMatthias Ringwald "eSCO S4 (and T2) Settings Supported", 973deb3ec6SMatthias Ringwald "Reserved for future definition" 983deb3ec6SMatthias Ringwald }; 993deb3ec6SMatthias Ringwald 1000aee97efSMilanka Ringwald static const char * hfp_enhanced_call_dir[] = { 1010aee97efSMilanka Ringwald "outgoing", 1020aee97efSMilanka Ringwald "incoming" 1030aee97efSMilanka Ringwald }; 1040aee97efSMilanka Ringwald 1050aee97efSMilanka Ringwald static const char * hfp_enhanced_call_status[] = { 1060aee97efSMilanka Ringwald "active", 1070aee97efSMilanka Ringwald "held", 1080aee97efSMilanka Ringwald "outgoing dialing", 1090aee97efSMilanka Ringwald "outgoing alerting", 1100aee97efSMilanka Ringwald "incoming", 1110aee97efSMilanka Ringwald "incoming waiting", 1120aee97efSMilanka Ringwald "call held by response and hold" 1130aee97efSMilanka Ringwald }; 1140aee97efSMilanka Ringwald 1150aee97efSMilanka Ringwald static const char * hfp_enhanced_call_mode[] = { 1160aee97efSMilanka Ringwald "voice", 1170aee97efSMilanka Ringwald "data", 1180aee97efSMilanka Ringwald "fax" 1190aee97efSMilanka Ringwald }; 1200aee97efSMilanka Ringwald 1210aee97efSMilanka Ringwald static const char * hfp_enhanced_call_mpty[] = { 1220aee97efSMilanka Ringwald "not a conference call", 1230aee97efSMilanka Ringwald "conference call" 1240aee97efSMilanka Ringwald }; 1250aee97efSMilanka Ringwald 1260aee97efSMilanka Ringwald const char * hfp_enhanced_call_dir2str(uint16_t index){ 1270aee97efSMilanka Ringwald if (index <= HFP_ENHANCED_CALL_DIR_INCOMING) return hfp_enhanced_call_dir[index]; 1280aee97efSMilanka Ringwald return "not defined"; 1290aee97efSMilanka Ringwald } 1300aee97efSMilanka Ringwald 1310aee97efSMilanka Ringwald const char * hfp_enhanced_call_status2str(uint16_t index){ 1320aee97efSMilanka Ringwald if (index <= HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD) return hfp_enhanced_call_status[index]; 1330aee97efSMilanka Ringwald return "not defined"; 1340aee97efSMilanka Ringwald } 1350aee97efSMilanka Ringwald 1360aee97efSMilanka Ringwald const char * hfp_enhanced_call_mode2str(uint16_t index){ 1370aee97efSMilanka Ringwald if (index <= HFP_ENHANCED_CALL_MODE_FAX) return hfp_enhanced_call_mode[index]; 1380aee97efSMilanka Ringwald return "not defined"; 1390aee97efSMilanka Ringwald } 1400aee97efSMilanka Ringwald 1410aee97efSMilanka Ringwald const char * hfp_enhanced_call_mpty2str(uint16_t index){ 1420aee97efSMilanka Ringwald if (index <= HFP_ENHANCED_CALL_MPTY_CONFERENCE_CALL) return hfp_enhanced_call_mpty[index]; 1430aee97efSMilanka Ringwald return "not defined"; 1440aee97efSMilanka Ringwald } 1450aee97efSMilanka Ringwald 146ce263fc8SMatthias Ringwald static void parse_sequence(hfp_connection_t * context); 147ca59be51SMatthias Ringwald 1489c9c64c1SMatthias Ringwald static btstack_linked_list_t hfp_connections = NULL; 1499c9c64c1SMatthias Ringwald 150ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_hf_callback; 151ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_ag_callback; 152ca59be51SMatthias Ringwald 153ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_hf_rfcomm_packet_handler; 154ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_ag_rfcomm_packet_handler; 155f4000eebSMatthias Ringwald 1561b005648SMatthias Ringwald static void (*hfp_hf_run_for_context)(hfp_connection_t * hfp_connection); 1571b005648SMatthias Ringwald 158eddcd308SMatthias Ringwald static hfp_connection_t * sco_establishment_active; 159eddcd308SMatthias Ringwald 16025789943SMilanka Ringwald static uint16_t hfp_parse_indicator_index(hfp_connection_t * hfp_connection){ 16125789943SMilanka Ringwald uint16_t index = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 16225789943SMilanka Ringwald 16325789943SMilanka Ringwald if (index > HFP_MAX_NUM_INDICATORS){ 16425789943SMilanka Ringwald log_info("ignoring invalid indicator index bigger then HFP_MAX_NUM_INDICATORS"); 16525789943SMilanka Ringwald return HFP_MAX_NUM_INDICATORS - 1; 16625789943SMilanka Ringwald } 16725789943SMilanka Ringwald 16825789943SMilanka Ringwald // indicator index enumeration starts with 1, we substract 1 to store in array with starting index 0 16925789943SMilanka Ringwald if (index > 0){ 17025789943SMilanka Ringwald index -= 1; 17125789943SMilanka Ringwald } else { 17225789943SMilanka Ringwald log_info("ignoring invalid indicator index 0"); 17325789943SMilanka Ringwald return 0; 17425789943SMilanka Ringwald } 17525789943SMilanka Ringwald return index; 17625789943SMilanka Ringwald } 17725789943SMilanka Ringwald 17825789943SMilanka Ringwald static void hfp_next_indicators_index(hfp_connection_t * hfp_connection){ 17925789943SMilanka Ringwald if (hfp_connection->parser_item_index < HFP_MAX_NUM_INDICATORS - 1){ 18025789943SMilanka Ringwald hfp_connection->parser_item_index++; 18125789943SMilanka Ringwald } else { 18225789943SMilanka Ringwald log_info("Ignoring additional indicator"); 18325789943SMilanka Ringwald } 18425789943SMilanka Ringwald } 18525789943SMilanka Ringwald 18625789943SMilanka Ringwald static void hfp_next_codec_index(hfp_connection_t * hfp_connection){ 18725789943SMilanka Ringwald if (hfp_connection->parser_item_index < HFP_MAX_NUM_CODECS - 1){ 18825789943SMilanka Ringwald hfp_connection->parser_item_index++; 18925789943SMilanka Ringwald } else { 19025789943SMilanka Ringwald log_info("Ignoring additional codec index"); 19125789943SMilanka Ringwald } 19225789943SMilanka Ringwald } 19325789943SMilanka Ringwald 194*eed67a37SMilanka Ringwald static void hfp_next_remote_call_services_index(hfp_connection_t * hfp_connection){ 195*eed67a37SMilanka Ringwald if (hfp_connection->remote_call_services_index < HFP_MAX_NUM_CALL_SERVICES - 1){ 196*eed67a37SMilanka Ringwald hfp_connection->remote_call_services_index++; 197*eed67a37SMilanka Ringwald } else { 198*eed67a37SMilanka Ringwald log_info("Ignoring additional remote_call_services"); 199*eed67a37SMilanka Ringwald } 200*eed67a37SMilanka Ringwald } 20125789943SMilanka Ringwald 2023deb3ec6SMatthias Ringwald const char * hfp_hf_feature(int index){ 2033deb3ec6SMatthias Ringwald if (index > HFP_HF_FEATURES_SIZE){ 2043deb3ec6SMatthias Ringwald return hfp_hf_features[HFP_HF_FEATURES_SIZE]; 2053deb3ec6SMatthias Ringwald } 2063deb3ec6SMatthias Ringwald return hfp_hf_features[index]; 2073deb3ec6SMatthias Ringwald } 2083deb3ec6SMatthias Ringwald 2093deb3ec6SMatthias Ringwald const char * hfp_ag_feature(int index){ 2103deb3ec6SMatthias Ringwald if (index > HFP_AG_FEATURES_SIZE){ 2113deb3ec6SMatthias Ringwald return hfp_ag_features[HFP_AG_FEATURES_SIZE]; 2123deb3ec6SMatthias Ringwald } 2133deb3ec6SMatthias Ringwald return hfp_ag_features[index]; 2143deb3ec6SMatthias Ringwald } 2153deb3ec6SMatthias Ringwald 2163deb3ec6SMatthias Ringwald int send_str_over_rfcomm(uint16_t cid, char * command){ 2173deb3ec6SMatthias Ringwald if (!rfcomm_can_send_packet_now(cid)) return 1; 21874386ee0SMatthias Ringwald log_info("HFP_TX %s", command); 21928190c0bSMatthias Ringwald int err = rfcomm_send(cid, (uint8_t*) command, strlen(command)); 2203deb3ec6SMatthias Ringwald if (err){ 22128190c0bSMatthias Ringwald log_error("rfcomm_send -> error 0x%02x \n", err); 2223deb3ec6SMatthias Ringwald } 2233deb3ec6SMatthias Ringwald return 1; 2243deb3ec6SMatthias Ringwald } 2253deb3ec6SMatthias Ringwald 2266a7f44bdSMilanka Ringwald int hfp_supports_codec(uint8_t codec, int codecs_nr, uint8_t * codecs){ 227e8d1dc0dSMatthias Ringwald 228e8d1dc0dSMatthias Ringwald // mSBC requires support for eSCO connections 229c1ab6cc1SMatthias Ringwald if ((codec == HFP_CODEC_MSBC) && !hci_extended_sco_link_supported()) return 0; 230e8d1dc0dSMatthias Ringwald 231df327ff3SMilanka Ringwald int i; 232df327ff3SMilanka Ringwald for (i = 0; i < codecs_nr; i++){ 233e8d1dc0dSMatthias Ringwald if (codecs[i] != codec) continue; 234e8d1dc0dSMatthias Ringwald return 1; 235df327ff3SMilanka Ringwald } 236df327ff3SMilanka Ringwald return 0; 237df327ff3SMilanka Ringwald } 238df327ff3SMilanka Ringwald 239d715cf51SMatthias Ringwald void hfp_hf_drop_mSBC_if_eSCO_not_supported(uint8_t * codecs, uint8_t * codecs_nr){ 240d715cf51SMatthias Ringwald if (hci_extended_sco_link_supported()) return; 241d715cf51SMatthias Ringwald uint8_t tmp_codecs[HFP_MAX_NUM_CODECS]; 242d715cf51SMatthias Ringwald int i; 243d715cf51SMatthias Ringwald int tmp_codec_nr = 0; 244d715cf51SMatthias Ringwald for (i=0; i < *codecs_nr; i++){ 245d715cf51SMatthias Ringwald if (codecs[i] == HFP_CODEC_MSBC) continue; 246d715cf51SMatthias Ringwald tmp_codecs[tmp_codec_nr++] = codecs[i]; 247d715cf51SMatthias Ringwald } 248d715cf51SMatthias Ringwald *codecs_nr = tmp_codec_nr; 2496535961aSMatthias Ringwald (void)memcpy(codecs, tmp_codecs, tmp_codec_nr); 250d715cf51SMatthias Ringwald } 251d715cf51SMatthias Ringwald 2523deb3ec6SMatthias Ringwald // UTILS 2533deb3ec6SMatthias Ringwald int get_bit(uint16_t bitmap, int position){ 2543deb3ec6SMatthias Ringwald return (bitmap >> position) & 1; 2553deb3ec6SMatthias Ringwald } 2563deb3ec6SMatthias Ringwald 2573deb3ec6SMatthias Ringwald int store_bit(uint32_t bitmap, int position, uint8_t value){ 2583deb3ec6SMatthias Ringwald if (value){ 2593deb3ec6SMatthias Ringwald bitmap |= 1 << position; 2603deb3ec6SMatthias Ringwald } else { 2613deb3ec6SMatthias Ringwald bitmap &= ~ (1 << position); 2623deb3ec6SMatthias Ringwald } 2633deb3ec6SMatthias Ringwald return bitmap; 2643deb3ec6SMatthias Ringwald } 2653deb3ec6SMatthias Ringwald 2663deb3ec6SMatthias Ringwald int join(char * buffer, int buffer_size, uint8_t * values, int values_nr){ 267c1ab6cc1SMatthias Ringwald if (buffer_size < (values_nr * 3)) return 0; 2683deb3ec6SMatthias Ringwald int i; 2693deb3ec6SMatthias Ringwald int offset = 0; 270c1ab6cc1SMatthias Ringwald for (i = 0; i < (values_nr-1); i++) { 2713deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", values[i]); // puts string into buffer 2723deb3ec6SMatthias Ringwald } 2733deb3ec6SMatthias Ringwald if (i<values_nr){ 2743deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d", values[i]); 2753deb3ec6SMatthias Ringwald } 2763deb3ec6SMatthias Ringwald return offset; 2773deb3ec6SMatthias Ringwald } 2783deb3ec6SMatthias Ringwald 2793deb3ec6SMatthias Ringwald int join_bitmap(char * buffer, int buffer_size, uint32_t values, int values_nr){ 280c1ab6cc1SMatthias Ringwald if (buffer_size < (values_nr * 3)) return 0; 2813deb3ec6SMatthias Ringwald 2823deb3ec6SMatthias Ringwald int i; 2833deb3ec6SMatthias Ringwald int offset = 0; 284c1ab6cc1SMatthias Ringwald for (i = 0; i < (values_nr-1); i++) { 2853deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", get_bit(values,i)); // puts string into buffer 2863deb3ec6SMatthias Ringwald } 2873deb3ec6SMatthias Ringwald 2883deb3ec6SMatthias Ringwald if (i<values_nr){ 2893deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d", get_bit(values,i)); 2903deb3ec6SMatthias Ringwald } 2913deb3ec6SMatthias Ringwald return offset; 2923deb3ec6SMatthias Ringwald } 2933deb3ec6SMatthias Ringwald 294ca59be51SMatthias Ringwald static void hfp_emit_event_for_context(hfp_connection_t * hfp_connection, uint8_t * packet, uint16_t size){ 295ca59be51SMatthias Ringwald if (!hfp_connection) return; 296ca59be51SMatthias Ringwald btstack_packet_handler_t callback = NULL; 297ca59be51SMatthias Ringwald switch (hfp_connection->local_role){ 298ca59be51SMatthias Ringwald case HFP_ROLE_HF: 299ca59be51SMatthias Ringwald callback = hfp_hf_callback; 300671f1aa2SMatthias Ringwald break; 301ca59be51SMatthias Ringwald case HFP_ROLE_AG: 302ca59be51SMatthias Ringwald callback = hfp_ag_callback; 303671f1aa2SMatthias Ringwald break; 304ca59be51SMatthias Ringwald default: 305ca59be51SMatthias Ringwald return; 306ca59be51SMatthias Ringwald } 307ca59be51SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, packet, size); 308ca59be51SMatthias Ringwald } 309ca59be51SMatthias Ringwald 310ca59be51SMatthias Ringwald void hfp_emit_simple_event(hfp_connection_t * hfp_connection, uint8_t event_subtype){ 311a0ffb263SMatthias Ringwald uint8_t event[3]; 312a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 313a0ffb263SMatthias Ringwald event[1] = sizeof(event) - 2; 314a0ffb263SMatthias Ringwald event[2] = event_subtype; 315ca59be51SMatthias Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 316a0ffb263SMatthias Ringwald } 317a0ffb263SMatthias Ringwald 318ca59be51SMatthias Ringwald void hfp_emit_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, uint8_t value){ 3193deb3ec6SMatthias Ringwald uint8_t event[4]; 3203deb3ec6SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 3213deb3ec6SMatthias Ringwald event[1] = sizeof(event) - 2; 3223deb3ec6SMatthias Ringwald event[2] = event_subtype; 3233deb3ec6SMatthias Ringwald event[3] = value; // status 0 == OK 324ca59be51SMatthias Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 3253deb3ec6SMatthias Ringwald } 3263deb3ec6SMatthias Ringwald 327ca59be51SMatthias 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){ 3288901a7c4SMatthias Ringwald uint8_t event[12]; 3296a7f44bdSMilanka Ringwald int pos = 0; 3306a7f44bdSMilanka Ringwald event[pos++] = HCI_EVENT_HFP_META; 3316a7f44bdSMilanka Ringwald event[pos++] = sizeof(event) - 2; 332d0c4aea6SMilanka Ringwald event[pos++] = HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 3336a7f44bdSMilanka Ringwald event[pos++] = status; // status 0 == OK 3346a7f44bdSMilanka Ringwald little_endian_store_16(event, pos, con_handle); 3356a7f44bdSMilanka Ringwald pos += 2; 3366a7f44bdSMilanka Ringwald reverse_bd_addr(addr,&event[pos]); 3376a7f44bdSMilanka Ringwald pos += 6; 338ca59be51SMatthias Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 339a0653c3bSMilanka Ringwald } 340a0653c3bSMilanka Ringwald 341ca59be51SMatthias Ringwald static void hfp_emit_sco_event(hfp_connection_t * hfp_connection, uint8_t status, hci_con_handle_t con_handle, bd_addr_t addr, uint8_t negotiated_codec){ 342d0c4aea6SMilanka Ringwald uint8_t event[13]; 343d0c4aea6SMilanka Ringwald int pos = 0; 344d0c4aea6SMilanka Ringwald event[pos++] = HCI_EVENT_HFP_META; 345d0c4aea6SMilanka Ringwald event[pos++] = sizeof(event) - 2; 346d0c4aea6SMilanka Ringwald event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED; 347d0c4aea6SMilanka Ringwald event[pos++] = status; // status 0 == OK 348d0c4aea6SMilanka Ringwald little_endian_store_16(event, pos, con_handle); 349d0c4aea6SMilanka Ringwald pos += 2; 350d0c4aea6SMilanka Ringwald reverse_bd_addr(addr,&event[pos]); 351d0c4aea6SMilanka Ringwald pos += 6; 352d0c4aea6SMilanka Ringwald event[pos++] = negotiated_codec; 353ca59be51SMatthias Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 354d0c4aea6SMilanka Ringwald } 355d0c4aea6SMilanka Ringwald 356ca59be51SMatthias Ringwald void hfp_emit_string_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, const char * value){ 357c1797c7dSMatthias Ringwald uint8_t event[40]; 358aa4dd815SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 359aa4dd815SMatthias Ringwald event[1] = sizeof(event) - 2; 360aa4dd815SMatthias Ringwald event[2] = event_subtype; 36109ca35c4SMilanka Ringwald uint16_t size = btstack_min(strlen(value), sizeof(event) - 4); 362aa4dd815SMatthias Ringwald strncpy((char*)&event[3], value, size); 363aa4dd815SMatthias Ringwald event[3 + size] = 0; 364ca59be51SMatthias Ringwald hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 365aa4dd815SMatthias Ringwald } 366aa4dd815SMatthias Ringwald 3670cb5b971SMatthias Ringwald btstack_linked_list_t * hfp_get_connections(void){ 3688f2a52f4SMatthias Ringwald return (btstack_linked_list_t *) &hfp_connections; 3693deb3ec6SMatthias Ringwald } 3703deb3ec6SMatthias Ringwald 3713deb3ec6SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid){ 372665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 373665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 374665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 375a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 376a0ffb263SMatthias Ringwald if (hfp_connection->rfcomm_cid == cid){ 377a0ffb263SMatthias Ringwald return hfp_connection; 3783deb3ec6SMatthias Ringwald } 3793deb3ec6SMatthias Ringwald } 3803deb3ec6SMatthias Ringwald return NULL; 3813deb3ec6SMatthias Ringwald } 3823deb3ec6SMatthias Ringwald 383405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr, hfp_role_t hfp_role){ 384665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 385665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 386665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 387a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 3885df9dc78SMatthias Ringwald if ((memcmp(hfp_connection->remote_addr, bd_addr, 6) == 0) && (hfp_connection->local_role == hfp_role)) { 389a0ffb263SMatthias Ringwald return hfp_connection; 3903deb3ec6SMatthias Ringwald } 3913deb3ec6SMatthias Ringwald } 3923deb3ec6SMatthias Ringwald return NULL; 3933deb3ec6SMatthias Ringwald } 3943deb3ec6SMatthias Ringwald 395405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_sco_handle(uint16_t handle, hfp_role_t hfp_role){ 396665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 397665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 398665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 399a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 400c1ab6cc1SMatthias Ringwald if ((hfp_connection->sco_handle == handle) && (hfp_connection->local_role == hfp_role)){ 401a0ffb263SMatthias Ringwald return hfp_connection; 4023deb3ec6SMatthias Ringwald } 4033deb3ec6SMatthias Ringwald } 4043deb3ec6SMatthias Ringwald return NULL; 4053deb3ec6SMatthias Ringwald } 4063deb3ec6SMatthias Ringwald 407405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_acl_handle(uint16_t handle, hfp_role_t hfp_role){ 408d97d752dSMilanka Ringwald btstack_linked_list_iterator_t it; 409d97d752dSMilanka Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 410d97d752dSMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 411d97d752dSMilanka Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 412c1ab6cc1SMatthias Ringwald if ((hfp_connection->acl_handle == handle) && (hfp_connection->local_role == hfp_role)){ 413d97d752dSMilanka Ringwald return hfp_connection; 414d97d752dSMilanka Ringwald } 415d97d752dSMilanka Ringwald } 416d97d752dSMilanka Ringwald return NULL; 417d97d752dSMilanka Ringwald } 418d97d752dSMilanka Ringwald 419a0ffb263SMatthias Ringwald void hfp_reset_context_flags(hfp_connection_t * hfp_connection){ 420a0ffb263SMatthias Ringwald if (!hfp_connection) return; 421a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 422a0ffb263SMatthias Ringwald hfp_connection->send_error = 0; 4233deb3ec6SMatthias Ringwald 424dd533208SMatthias Ringwald hfp_connection->found_equal_sign = false; 4253deb3ec6SMatthias Ringwald 426a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 0; 427a0ffb263SMatthias Ringwald hfp_connection->operator_name_changed = 0; 4283deb3ec6SMatthias Ringwald 429a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = 0; 430a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 4313deb3ec6SMatthias Ringwald 432a0ffb263SMatthias Ringwald // establish codecs hfp_connection 433a0ffb263SMatthias Ringwald hfp_connection->suggested_codec = 0; 4347522e673SMatthias Ringwald hfp_connection->negotiated_codec = 0; 435a0ffb263SMatthias Ringwald hfp_connection->codec_confirmed = 0; 4363deb3ec6SMatthias Ringwald 437a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 438a0ffb263SMatthias Ringwald hfp_connection->call_waiting_notification_enabled = 0; 439a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 440a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 4413deb3ec6SMatthias Ringwald } 4423deb3ec6SMatthias Ringwald 443fffdd288SMatthias Ringwald static hfp_connection_t * create_hfp_connection_context(void){ 444a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = btstack_memory_hfp_connection_get(); 445a0ffb263SMatthias Ringwald if (!hfp_connection) return NULL; 4463deb3ec6SMatthias Ringwald 447a0ffb263SMatthias Ringwald hfp_connection->state = HFP_IDLE; 448a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 449a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_IDLE; 450aa4dd815SMatthias Ringwald 451a0ffb263SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 452a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 4533deb3ec6SMatthias Ringwald 454d751f069SMatthias Ringwald hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID; 455d751f069SMatthias Ringwald hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID; 456d751f069SMatthias Ringwald 457a0ffb263SMatthias Ringwald hfp_reset_context_flags(hfp_connection); 4583deb3ec6SMatthias Ringwald 459d63c37a1SMatthias Ringwald btstack_linked_list_add(&hfp_connections, (btstack_linked_item_t*)hfp_connection); 460a0ffb263SMatthias Ringwald return hfp_connection; 4613deb3ec6SMatthias Ringwald } 4623deb3ec6SMatthias Ringwald 463a0ffb263SMatthias Ringwald static void remove_hfp_connection_context(hfp_connection_t * hfp_connection){ 464a0ffb263SMatthias Ringwald btstack_linked_list_remove(&hfp_connections, (btstack_linked_item_t*) hfp_connection); 46509a233a1SMatthias Ringwald btstack_memory_hfp_connection_free(hfp_connection); 4663deb3ec6SMatthias Ringwald } 4673deb3ec6SMatthias Ringwald 468323d3000SMatthias Ringwald static hfp_connection_t * provide_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr, hfp_role_t local_role){ 469405014fbSMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr, local_role); 470a0ffb263SMatthias Ringwald if (hfp_connection) return hfp_connection; 471a0ffb263SMatthias Ringwald hfp_connection = create_hfp_connection_context(); 4726535961aSMatthias Ringwald (void)memcpy(hfp_connection->remote_addr, bd_addr, 6); 473323d3000SMatthias Ringwald hfp_connection->local_role = local_role; 474bdf572f4SMatthias Ringwald log_info("Create HFP context %p: role %u, addr %s", hfp_connection, local_role, bd_addr_to_str(bd_addr)); 475bdf572f4SMatthias Ringwald 476a0ffb263SMatthias Ringwald return hfp_connection; 4773deb3ec6SMatthias Ringwald } 4783deb3ec6SMatthias Ringwald 479aa4dd815SMatthias Ringwald /* @param network. 480aa4dd815SMatthias Ringwald * 0 == no ability to reject a call. 481aa4dd815SMatthias Ringwald * 1 == ability to reject a call. 482aa4dd815SMatthias Ringwald */ 4833deb3ec6SMatthias Ringwald 4843deb3ec6SMatthias Ringwald /* @param suported_features 4853deb3ec6SMatthias Ringwald * HF bit 0: EC and/or NR function (yes/no, 1 = yes, 0 = no) 4863deb3ec6SMatthias Ringwald * HF bit 1: Call waiting or three-way calling(yes/no, 1 = yes, 0 = no) 4873deb3ec6SMatthias Ringwald * HF bit 2: CLI presentation capability (yes/no, 1 = yes, 0 = no) 4883deb3ec6SMatthias Ringwald * HF bit 3: Voice recognition activation (yes/no, 1= yes, 0 = no) 4893deb3ec6SMatthias Ringwald * HF bit 4: Remote volume control (yes/no, 1 = yes, 0 = no) 4903deb3ec6SMatthias Ringwald * HF bit 5: Wide band speech (yes/no, 1 = yes, 0 = no) 4913deb3ec6SMatthias Ringwald */ 4923deb3ec6SMatthias Ringwald /* Bit position: 4933deb3ec6SMatthias Ringwald * AG bit 0: Three-way calling (yes/no, 1 = yes, 0 = no) 4943deb3ec6SMatthias Ringwald * AG bit 1: EC and/or NR function (yes/no, 1 = yes, 0 = no) 4953deb3ec6SMatthias Ringwald * AG bit 2: Voice recognition function (yes/no, 1 = yes, 0 = no) 4963deb3ec6SMatthias Ringwald * AG bit 3: In-band ring tone capability (yes/no, 1 = yes, 0 = no) 4973deb3ec6SMatthias Ringwald * AG bit 4: Attach a phone number to a voice tag (yes/no, 1 = yes, 0 = no) 4983deb3ec6SMatthias Ringwald * AG bit 5: Wide band speech (yes/no, 1 = yes, 0 = no) 4993deb3ec6SMatthias Ringwald */ 5003deb3ec6SMatthias Ringwald 5019b1c3b4dSMatthias 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){ 5023deb3ec6SMatthias Ringwald uint8_t* attribute; 5033deb3ec6SMatthias Ringwald de_create_sequence(service); 5043deb3ec6SMatthias Ringwald 5053deb3ec6SMatthias Ringwald // 0x0000 "Service Record Handle" 506235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE); 5079b1c3b4dSMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle); 5083deb3ec6SMatthias Ringwald 5093deb3ec6SMatthias Ringwald // 0x0001 "Service Class ID List" 510235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST); 5113deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 5123deb3ec6SMatthias Ringwald { 5133deb3ec6SMatthias Ringwald // "UUID for Service" 5143deb3ec6SMatthias Ringwald de_add_number(attribute, DE_UUID, DE_SIZE_16, service_uuid); 515235946f1SMatthias Ringwald de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_GENERIC_AUDIO); 5163deb3ec6SMatthias Ringwald } 5173deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 5183deb3ec6SMatthias Ringwald 5193deb3ec6SMatthias Ringwald // 0x0004 "Protocol Descriptor List" 520235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST); 5213deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 5223deb3ec6SMatthias Ringwald { 5233deb3ec6SMatthias Ringwald uint8_t* l2cpProtocol = de_push_sequence(attribute); 5243deb3ec6SMatthias Ringwald { 525235946f1SMatthias Ringwald de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP); 5263deb3ec6SMatthias Ringwald } 5273deb3ec6SMatthias Ringwald de_pop_sequence(attribute, l2cpProtocol); 5283deb3ec6SMatthias Ringwald 5293deb3ec6SMatthias Ringwald uint8_t* rfcomm = de_push_sequence(attribute); 5303deb3ec6SMatthias Ringwald { 531235946f1SMatthias Ringwald de_add_number(rfcomm, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_RFCOMM); // rfcomm_service 5323deb3ec6SMatthias Ringwald de_add_number(rfcomm, DE_UINT, DE_SIZE_8, rfcomm_channel_nr); // rfcomm channel 5333deb3ec6SMatthias Ringwald } 5343deb3ec6SMatthias Ringwald de_pop_sequence(attribute, rfcomm); 5353deb3ec6SMatthias Ringwald } 5363deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 5373deb3ec6SMatthias Ringwald 5383deb3ec6SMatthias Ringwald 5393deb3ec6SMatthias Ringwald // 0x0005 "Public Browse Group" 540235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group 5413deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 5423deb3ec6SMatthias Ringwald { 543235946f1SMatthias Ringwald de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT); 5443deb3ec6SMatthias Ringwald } 5453deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 5463deb3ec6SMatthias Ringwald 5473deb3ec6SMatthias Ringwald // 0x0009 "Bluetooth Profile Descriptor List" 548235946f1SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST); 5493deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 5503deb3ec6SMatthias Ringwald { 5513deb3ec6SMatthias Ringwald uint8_t *sppProfile = de_push_sequence(attribute); 5523deb3ec6SMatthias Ringwald { 553235946f1SMatthias Ringwald de_add_number(sppProfile, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_HANDSFREE); 5543deb3ec6SMatthias Ringwald de_add_number(sppProfile, DE_UINT, DE_SIZE_16, 0x0107); // Verision 1.7 5553deb3ec6SMatthias Ringwald } 5563deb3ec6SMatthias Ringwald de_pop_sequence(attribute, sppProfile); 5573deb3ec6SMatthias Ringwald } 5583deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 5593deb3ec6SMatthias Ringwald 5603deb3ec6SMatthias Ringwald // 0x0100 "Service Name" 5613deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); 5623deb3ec6SMatthias Ringwald de_add_data(service, DE_STRING, strlen(name), (uint8_t *) name); 5633deb3ec6SMatthias Ringwald } 5643deb3ec6SMatthias Ringwald 5653deb3ec6SMatthias Ringwald static hfp_connection_t * connection_doing_sdp_query = NULL; 566a8747467SMatthias Ringwald 5676c927b22SMatthias Ringwald static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 5688a46ec40SMatthias Ringwald UNUSED(packet_type); // ok: handling own sdp events 5698a46ec40SMatthias Ringwald UNUSED(channel); // ok: no channel 5708a46ec40SMatthias Ringwald UNUSED(size); // ok: handling own sdp events 571a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = connection_doing_sdp_query; 57284904e95SMilanka Ringwald if (!hfp_connection) { 57384904e95SMilanka Ringwald log_error("handle_query_rfcomm_event, no connection"); 57484904e95SMilanka Ringwald return; 57584904e95SMilanka Ringwald } 5763deb3ec6SMatthias Ringwald 5770e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 5785611a760SMatthias Ringwald case SDP_EVENT_QUERY_RFCOMM_SERVICE: 579a0ffb263SMatthias Ringwald hfp_connection->rfcomm_channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet); 5803deb3ec6SMatthias Ringwald break; 5815611a760SMatthias Ringwald case SDP_EVENT_QUERY_COMPLETE: 5823deb3ec6SMatthias Ringwald connection_doing_sdp_query = NULL; 583a0ffb263SMatthias Ringwald if (hfp_connection->rfcomm_channel_nr > 0){ 584a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_CONNECTED; 585a0ffb263SMatthias Ringwald log_info("HFP: SDP_EVENT_QUERY_COMPLETE context %p, addr %s, state %d", hfp_connection, bd_addr_to_str( hfp_connection->remote_addr), hfp_connection->state); 586520c92d5SMatthias Ringwald btstack_packet_handler_t packet_handler; 587520c92d5SMatthias Ringwald switch (hfp_connection->local_role){ 588520c92d5SMatthias Ringwald case HFP_ROLE_AG: 589520c92d5SMatthias Ringwald packet_handler = hfp_ag_rfcomm_packet_handler; 590520c92d5SMatthias Ringwald break; 591520c92d5SMatthias Ringwald case HFP_ROLE_HF: 592520c92d5SMatthias Ringwald packet_handler = hfp_hf_rfcomm_packet_handler; 593520c92d5SMatthias Ringwald break; 594520c92d5SMatthias Ringwald default: 595520c92d5SMatthias Ringwald log_error("Role %x", hfp_connection->local_role); 596520c92d5SMatthias Ringwald return; 597520c92d5SMatthias Ringwald } 598ca59be51SMatthias Ringwald rfcomm_create_channel(packet_handler, hfp_connection->remote_addr, hfp_connection->rfcomm_channel_nr, NULL); 5993deb3ec6SMatthias Ringwald break; 6003deb3ec6SMatthias Ringwald } 60184904e95SMilanka Ringwald hfp_connection->state = HFP_IDLE; 602ca59be51SMatthias Ringwald hfp_emit_slc_connection_event(hfp_connection, sdp_event_query_complete_get_status(packet), HCI_CON_HANDLE_INVALID, hfp_connection->remote_addr); 60372b50801SMatthias Ringwald log_info("rfcomm service not found, status %u.", sdp_event_query_complete_get_status(packet)); 6043deb3ec6SMatthias Ringwald break; 6053deb3ec6SMatthias Ringwald default: 6063deb3ec6SMatthias Ringwald break; 6073deb3ec6SMatthias Ringwald } 6083deb3ec6SMatthias Ringwald } 6093deb3ec6SMatthias Ringwald 61038200c1dSMilanka Ringwald // returns 0 if unexpected error or no other link options remained, otherwise 1 61138200c1dSMilanka Ringwald static int hfp_handle_failed_sco_connection(uint8_t status){ 612eddcd308SMatthias Ringwald 613eddcd308SMatthias Ringwald if (!sco_establishment_active){ 614eddcd308SMatthias Ringwald log_error("(e)SCO Connection failed but not started by us"); 61538200c1dSMilanka Ringwald return 0; 616eddcd308SMatthias Ringwald } 61738200c1dSMilanka Ringwald log_info("(e)SCO Connection failed status 0x%02x", status); 618eddcd308SMatthias Ringwald // invalid params / unspecified error 619505f1c30SMatthias Ringwald if ((status != 0x11) && (status != 0x1f) && (status != 0x0D)) return 0; 620eddcd308SMatthias Ringwald 621eddcd308SMatthias Ringwald switch (sco_establishment_active->link_setting){ 622eddcd308SMatthias Ringwald case HFP_LINK_SETTINGS_D0: 62338200c1dSMilanka Ringwald return 0; // no other option left 624eddcd308SMatthias Ringwald case HFP_LINK_SETTINGS_D1: 625eddcd308SMatthias Ringwald sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D0; 626eddcd308SMatthias Ringwald break; 627eddcd308SMatthias Ringwald case HFP_LINK_SETTINGS_S1: 628eddcd308SMatthias Ringwald sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D1; 629eddcd308SMatthias Ringwald break; 630eddcd308SMatthias Ringwald case HFP_LINK_SETTINGS_S2: 631eddcd308SMatthias Ringwald sco_establishment_active->link_setting = HFP_LINK_SETTINGS_S1; 632eddcd308SMatthias Ringwald break; 6337522e673SMatthias Ringwald case HFP_LINK_SETTINGS_S3: 6347522e673SMatthias Ringwald sco_establishment_active->link_setting = HFP_LINK_SETTINGS_S2; 6357522e673SMatthias Ringwald break; 6367522e673SMatthias Ringwald case HFP_LINK_SETTINGS_S4: 637eddcd308SMatthias Ringwald sco_establishment_active->link_setting = HFP_LINK_SETTINGS_S3; 638eddcd308SMatthias Ringwald break; 6397522e673SMatthias Ringwald case HFP_LINK_SETTINGS_T1: 6407522e673SMatthias Ringwald log_info("T1 failed, fallback to CVSD - D1"); 6417522e673SMatthias Ringwald sco_establishment_active->negotiated_codec = HFP_CODEC_CVSD; 642bc1b1537SMilanka Ringwald sco_establishment_active->sco_for_msbc_failed = 1; 643bc1b1537SMilanka Ringwald sco_establishment_active->command = HFP_CMD_AG_SEND_COMMON_CODEC; 6447522e673SMatthias Ringwald sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D1; 6457522e673SMatthias Ringwald break; 6467522e673SMatthias Ringwald case HFP_LINK_SETTINGS_T2: 6477522e673SMatthias Ringwald sco_establishment_active->link_setting = HFP_LINK_SETTINGS_T1; 6487522e673SMatthias Ringwald break; 649eddcd308SMatthias Ringwald } 65038200c1dSMilanka Ringwald log_info("e)SCO Connection: try new link_setting %d", sco_establishment_active->link_setting); 651eddcd308SMatthias Ringwald sco_establishment_active->establish_audio_connection = 1; 65238200c1dSMilanka Ringwald sco_establishment_active = NULL; 65338200c1dSMilanka Ringwald return 1; 654eddcd308SMatthias Ringwald } 655eddcd308SMatthias Ringwald 656eddcd308SMatthias Ringwald 657405014fbSMatthias Ringwald void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){ 6585575558eSMilanka Ringwald UNUSED(packet_type); 6598a46ec40SMatthias Ringwald UNUSED(channel); // ok: no channel 6605575558eSMilanka Ringwald UNUSED(size); 6619ec2630cSMatthias Ringwald 6623deb3ec6SMatthias Ringwald bd_addr_t event_addr; 66327950165SMatthias Ringwald hci_con_handle_t handle; 664a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = NULL; 665674515e8SMatthias Ringwald uint8_t status; 6663deb3ec6SMatthias Ringwald 66727950165SMatthias Ringwald log_debug("HFP HCI event handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size); 668aa4dd815SMatthias Ringwald 6690e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 6701b005648SMatthias Ringwald 671011577abSMilanka Ringwald case HCI_EVENT_CONNECTION_REQUEST: 6727522e673SMatthias Ringwald switch(hci_event_connection_request_get_link_type(packet)){ 6737522e673SMatthias Ringwald case 0: // SCO 6747522e673SMatthias Ringwald case 2: // eSCO 675011577abSMilanka Ringwald hci_event_connection_request_get_bd_addr(packet, event_addr); 676405014fbSMatthias Ringwald hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role); 677011577abSMilanka Ringwald if (!hfp_connection) break; 678c169df2fSMatthias Ringwald if (hci_event_connection_request_get_link_type(packet) == 2){ 679c169df2fSMatthias Ringwald hfp_connection->hf_accept_sco = 2; 680c169df2fSMatthias Ringwald } else { 681b72c4a9eSMatthias Ringwald hfp_connection->hf_accept_sco = 1; 682c169df2fSMatthias Ringwald } 683c169df2fSMatthias Ringwald log_info("hf accept sco %u\n", hfp_connection->hf_accept_sco); 6841b005648SMatthias Ringwald if (!hfp_hf_run_for_context) break; 6851b005648SMatthias Ringwald (*hfp_hf_run_for_context)(hfp_connection); 686202c8a4cSMatthias Ringwald break; 6877522e673SMatthias Ringwald default: 6887522e673SMatthias Ringwald break; 6897522e673SMatthias Ringwald } 690011577abSMilanka Ringwald break; 6913deb3ec6SMatthias Ringwald 692eddcd308SMatthias Ringwald case HCI_EVENT_COMMAND_STATUS: 693eddcd308SMatthias Ringwald if (hci_event_command_status_get_command_opcode(packet) == hci_setup_synchronous_connection.opcode) { 694be2a1a78SMatthias Ringwald status = hci_event_command_status_get_status(packet); 69538200c1dSMilanka Ringwald if (status == ERROR_CODE_SUCCESS) break; 69638200c1dSMilanka Ringwald 69738200c1dSMilanka Ringwald hfp_connection = sco_establishment_active; 69838200c1dSMilanka Ringwald if (hfp_handle_failed_sco_connection(status)) break; 69938200c1dSMilanka Ringwald hfp_connection->establish_audio_connection = 0; 70038200c1dSMilanka Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 70138200c1dSMilanka Ringwald hfp_emit_sco_event(hfp_connection, status, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec); 7026c5b2002SMatthias Ringwald } 703eddcd308SMatthias Ringwald break; 704eddcd308SMatthias Ringwald 7053deb3ec6SMatthias Ringwald case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{ 706011577abSMilanka Ringwald hci_event_synchronous_connection_complete_get_bd_addr(packet, event_addr); 707405014fbSMatthias Ringwald hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role); 708011577abSMilanka Ringwald if (!hfp_connection) { 709011577abSMilanka Ringwald log_error("HFP: connection does not exist for remote with addr %s.", bd_addr_to_str(event_addr)); 710011577abSMilanka Ringwald return; 711011577abSMilanka Ringwald } 712ce263fc8SMatthias Ringwald 713011577abSMilanka Ringwald status = hci_event_synchronous_connection_complete_get_status(packet); 71438200c1dSMilanka Ringwald if (status != ERROR_CODE_SUCCESS){ 715b72c4a9eSMatthias Ringwald hfp_connection->hf_accept_sco = 0; 71638200c1dSMilanka Ringwald if (hfp_handle_failed_sco_connection(status)) break; 71738200c1dSMilanka Ringwald 71838200c1dSMilanka Ringwald hfp_connection->establish_audio_connection = 0; 71938200c1dSMilanka Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 72038200c1dSMilanka Ringwald hfp_emit_sco_event(hfp_connection, status, 0, event_addr, hfp_connection->negotiated_codec); 721aa4dd815SMatthias Ringwald break; 722aa4dd815SMatthias Ringwald } 723aa4dd815SMatthias Ringwald 724011577abSMilanka Ringwald uint16_t sco_handle = hci_event_synchronous_connection_complete_get_handle(packet); 725011577abSMilanka Ringwald uint8_t link_type = hci_event_synchronous_connection_complete_get_link_type(packet); 726011577abSMilanka Ringwald uint8_t transmission_interval = hci_event_synchronous_connection_complete_get_transmission_interval(packet); // measured in slots 727011577abSMilanka Ringwald uint8_t retransmission_interval = hci_event_synchronous_connection_complete_get_retransmission_interval(packet);// measured in slots 728011577abSMilanka Ringwald uint16_t rx_packet_length = hci_event_synchronous_connection_complete_get_rx_packet_length(packet); // measured in bytes 729011577abSMilanka Ringwald uint16_t tx_packet_length = hci_event_synchronous_connection_complete_get_tx_packet_length(packet); // measured in bytes 730011577abSMilanka Ringwald uint8_t air_mode = hci_event_synchronous_connection_complete_get_air_mode(packet); 7313deb3ec6SMatthias Ringwald 7323deb3ec6SMatthias Ringwald switch (link_type){ 7333deb3ec6SMatthias Ringwald case 0x00: 734aa4dd815SMatthias Ringwald log_info("SCO Connection established."); 7353deb3ec6SMatthias Ringwald if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval); 7363deb3ec6SMatthias Ringwald if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval); 7373deb3ec6SMatthias Ringwald if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length); 7383deb3ec6SMatthias Ringwald if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length); 7393deb3ec6SMatthias Ringwald break; 7403deb3ec6SMatthias Ringwald case 0x02: 741aa4dd815SMatthias Ringwald log_info("eSCO Connection established. \n"); 7423deb3ec6SMatthias Ringwald break; 7433deb3ec6SMatthias Ringwald default: 7443deb3ec6SMatthias Ringwald log_error("(e)SCO reserved link_type 0x%2x", link_type); 7453deb3ec6SMatthias Ringwald break; 7463deb3ec6SMatthias Ringwald } 7473deb3ec6SMatthias Ringwald log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, " 748aa4dd815SMatthias Ringwald " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)\n", sco_handle, 749aa4dd815SMatthias Ringwald bd_addr_to_str(event_addr), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length, air_mode); 7503deb3ec6SMatthias Ringwald 75138200c1dSMilanka Ringwald // hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role); 7523deb3ec6SMatthias Ringwald 75338200c1dSMilanka Ringwald // if (!hfp_connection) { 75438200c1dSMilanka Ringwald // log_error("SCO link created, hfp_connection for address %s not found.", bd_addr_to_str(event_addr)); 75538200c1dSMilanka Ringwald // break; 75638200c1dSMilanka Ringwald // } 7573deb3ec6SMatthias Ringwald 758d63c37a1SMatthias Ringwald if (hfp_connection->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){ 759aa4dd815SMatthias Ringwald log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN"); 760d63c37a1SMatthias Ringwald hfp_connection->state = HFP_W2_DISCONNECT_SCO; 761aa4dd815SMatthias Ringwald break; 762aa4dd815SMatthias Ringwald } 763d63c37a1SMatthias Ringwald hfp_connection->sco_handle = sco_handle; 764d63c37a1SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 765d63c37a1SMatthias Ringwald hfp_connection->state = HFP_AUDIO_CONNECTION_ESTABLISHED; 76638200c1dSMilanka Ringwald hfp_emit_sco_event(hfp_connection, status, sco_handle, event_addr, hfp_connection->negotiated_codec); 7673deb3ec6SMatthias Ringwald break; 7683deb3ec6SMatthias Ringwald } 7693deb3ec6SMatthias Ringwald 7703deb3ec6SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 771f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet,3); 772405014fbSMatthias Ringwald hfp_connection = get_hfp_connection_context_for_sco_handle(handle, local_role); 773aa4dd815SMatthias Ringwald 774d63c37a1SMatthias Ringwald if (!hfp_connection) break; 775aa4dd815SMatthias Ringwald 776d751f069SMatthias Ringwald hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID; 777d63c37a1SMatthias Ringwald hfp_connection->release_audio_connection = 0; 778d63c37a1SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 779ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED, 0); 7800c3047fbSMatthias Ringwald 7815f053052SMilanka Ringwald if (hfp_connection->release_slc_connection){ 7825f053052SMilanka Ringwald hfp_connection->release_slc_connection = 0; 7830c3047fbSMatthias Ringwald log_info("SCO disconnected, w2 disconnect RFCOMM\n"); 7840c3047fbSMatthias Ringwald hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM; 7855f053052SMilanka Ringwald } 7863deb3ec6SMatthias Ringwald break; 7873deb3ec6SMatthias Ringwald 788fc64f94aSMatthias Ringwald default: 7893deb3ec6SMatthias Ringwald break; 7903deb3ec6SMatthias Ringwald } 7913deb3ec6SMatthias Ringwald } 7923deb3ec6SMatthias Ringwald 79327950165SMatthias Ringwald void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){ 7945575558eSMilanka Ringwald UNUSED(packet_type); 79527950165SMatthias Ringwald UNUSED(channel); // ok: no channel 7965575558eSMilanka Ringwald UNUSED(size); 79727950165SMatthias Ringwald 79827950165SMatthias Ringwald bd_addr_t event_addr; 79927950165SMatthias Ringwald uint16_t rfcomm_cid; 80027950165SMatthias Ringwald hfp_connection_t * hfp_connection = NULL; 80127950165SMatthias Ringwald uint8_t status; 80227950165SMatthias Ringwald 80327950165SMatthias Ringwald log_debug("HFP packet_handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size); 80427950165SMatthias Ringwald 80527950165SMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 80627950165SMatthias Ringwald 80727950165SMatthias Ringwald case RFCOMM_EVENT_INCOMING_CONNECTION: 80827950165SMatthias Ringwald // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 80927950165SMatthias Ringwald rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr); 81027950165SMatthias Ringwald hfp_connection = provide_hfp_connection_context_for_bd_addr(event_addr, local_role); 81127950165SMatthias Ringwald if (!hfp_connection){ 81227950165SMatthias Ringwald log_info("hfp: no memory to accept incoming connection - decline"); 81327950165SMatthias Ringwald rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet)); 81427950165SMatthias Ringwald return; 81527950165SMatthias Ringwald } 81627950165SMatthias Ringwald if (hfp_connection->state != HFP_IDLE) { 8174960f9e7SMatthias Ringwald log_error("hfp: incoming connection but not idle, reject"); 8184960f9e7SMatthias Ringwald rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet)); 81927950165SMatthias Ringwald return; 82027950165SMatthias Ringwald } 82127950165SMatthias Ringwald 82227950165SMatthias Ringwald hfp_connection->rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet); 82327950165SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_CONNECTED; 82427950165SMatthias Ringwald // printf("RFCOMM channel %u requested for %s\n", hfp_connection->rfcomm_cid, bd_addr_to_str(hfp_connection->remote_addr)); 82527950165SMatthias Ringwald rfcomm_accept_connection(hfp_connection->rfcomm_cid); 82627950165SMatthias Ringwald break; 82727950165SMatthias Ringwald 82827950165SMatthias Ringwald case RFCOMM_EVENT_CHANNEL_OPENED: 82927950165SMatthias Ringwald // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16) 83027950165SMatthias Ringwald 83127950165SMatthias Ringwald rfcomm_event_channel_opened_get_bd_addr(packet, event_addr); 83227950165SMatthias Ringwald status = rfcomm_event_channel_opened_get_status(packet); 83327950165SMatthias Ringwald 834405014fbSMatthias Ringwald hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role); 835505f1c30SMatthias Ringwald if (!hfp_connection || (hfp_connection->state != HFP_W4_RFCOMM_CONNECTED)) return; 83627950165SMatthias Ringwald 83727950165SMatthias Ringwald if (status) { 83827950165SMatthias Ringwald hfp_emit_slc_connection_event(hfp_connection, status, rfcomm_event_channel_opened_get_con_handle(packet), event_addr); 83927950165SMatthias Ringwald remove_hfp_connection_context(hfp_connection); 84027950165SMatthias Ringwald } else { 84127950165SMatthias Ringwald hfp_connection->acl_handle = rfcomm_event_channel_opened_get_con_handle(packet); 84227950165SMatthias Ringwald hfp_connection->rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet); 84327950165SMatthias Ringwald bd_addr_copy(hfp_connection->remote_addr, event_addr); 84427950165SMatthias Ringwald // uint16_t mtu = rfcomm_event_channel_opened_get_max_frame_size(packet); 84527950165SMatthias Ringwald // printf("RFCOMM channel open succeeded. hfp_connection %p, RFCOMM Channel ID 0x%02x, max frame size %u\n", hfp_connection, hfp_connection->rfcomm_cid, mtu); 84627950165SMatthias Ringwald 84727950165SMatthias Ringwald switch (hfp_connection->state){ 84827950165SMatthias Ringwald case HFP_W4_RFCOMM_CONNECTED: 84927950165SMatthias Ringwald hfp_connection->state = HFP_EXCHANGE_SUPPORTED_FEATURES; 85027950165SMatthias Ringwald break; 85127950165SMatthias Ringwald case HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN: 85227950165SMatthias Ringwald hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM; 85327950165SMatthias Ringwald // printf("Shutting down RFCOMM.\n"); 85427950165SMatthias Ringwald break; 85527950165SMatthias Ringwald default: 85627950165SMatthias Ringwald break; 85727950165SMatthias Ringwald } 85827950165SMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 85927950165SMatthias Ringwald } 86027950165SMatthias Ringwald break; 86127950165SMatthias Ringwald 86227950165SMatthias Ringwald case RFCOMM_EVENT_CHANNEL_CLOSED: 86327950165SMatthias Ringwald rfcomm_cid = little_endian_read_16(packet,2); 86427950165SMatthias Ringwald hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid); 86527950165SMatthias Ringwald if (!hfp_connection) break; 86627950165SMatthias Ringwald if (hfp_connection->state == HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART){ 86727950165SMatthias Ringwald hfp_connection->state = HFP_IDLE; 86827950165SMatthias Ringwald hfp_establish_service_level_connection(hfp_connection->remote_addr, hfp_connection->service_uuid, local_role); 86927950165SMatthias Ringwald break; 87027950165SMatthias Ringwald } 87127950165SMatthias Ringwald 87227950165SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0); 87327950165SMatthias Ringwald remove_hfp_connection_context(hfp_connection); 87427950165SMatthias Ringwald break; 87527950165SMatthias Ringwald 87627950165SMatthias Ringwald default: 87727950165SMatthias Ringwald break; 87827950165SMatthias Ringwald } 87927950165SMatthias Ringwald } 880aa4dd815SMatthias Ringwald // translates command string into hfp_command_t CMD 88194a27792SMatthias Ringwald 88294a27792SMatthias Ringwald typedef struct { 88394a27792SMatthias Ringwald const char * command; 88494a27792SMatthias Ringwald hfp_command_t command_id; 88594a27792SMatthias Ringwald } hfp_command_entry_t; 88694a27792SMatthias Ringwald 88794a27792SMatthias Ringwald static hfp_command_entry_t hfp_ag_commmand_table[] = { 888cb33905eSMatthias Ringwald { "AT+BAC=", HFP_CMD_AVAILABLE_CODECS }, 889cb33905eSMatthias Ringwald { "AT+BCC", HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP }, 890cb33905eSMatthias Ringwald { "AT+BCS=", HFP_CMD_HF_CONFIRMED_CODEC }, 891cb33905eSMatthias Ringwald { "AT+BIA=", HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE, }, // +BIA:<enabled>,,<enabled>,,,<enabled> 892cb33905eSMatthias Ringwald { "AT+BIEV=", HFP_CMD_HF_INDICATOR_STATUS }, 89394a27792SMatthias Ringwald { "AT+BIND=", HFP_CMD_LIST_GENERIC_STATUS_INDICATORS }, 89494a27792SMatthias Ringwald { "AT+BIND=?", HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS }, 89594a27792SMatthias Ringwald { "AT+BIND?", HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE }, 896cb33905eSMatthias Ringwald { "AT+BINP", HFP_CMD_HF_REQUEST_PHONE_NUMBER }, 897cb33905eSMatthias Ringwald { "AT+BLDN", HFP_CMD_REDIAL_LAST_NUMBER }, 898cb33905eSMatthias Ringwald { "AT+BRSF=", HFP_CMD_SUPPORTED_FEATURES }, 89994a27792SMatthias Ringwald { "AT+BTRH=", HFP_CMD_RESPONSE_AND_HOLD_COMMAND }, 90094a27792SMatthias Ringwald { "AT+BTRH?", HFP_CMD_RESPONSE_AND_HOLD_QUERY }, 901cb33905eSMatthias Ringwald { "AT+BVRA=", HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION }, 902cb33905eSMatthias Ringwald { "AT+CCWA=", HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION}, 90394a27792SMatthias Ringwald { "AT+CHLD=", HFP_CMD_CALL_HOLD }, 90494a27792SMatthias Ringwald { "AT+CHLD=?", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES }, 905cb33905eSMatthias Ringwald { "AT+CHUP", HFP_CMD_HANG_UP_CALL }, 90694a27792SMatthias Ringwald { "AT+CIND=?", HFP_CMD_RETRIEVE_AG_INDICATORS }, 90794a27792SMatthias Ringwald { "AT+CIND?", HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS }, 908cb33905eSMatthias Ringwald { "AT+CLCC", HFP_CMD_LIST_CURRENT_CALLS }, 909cb33905eSMatthias Ringwald { "AT+CLIP=", HFP_CMD_ENABLE_CLIP}, 910cb33905eSMatthias Ringwald { "AT+CMEE=", HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR}, 911cb33905eSMatthias Ringwald { "AT+CMER=", HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE }, 912cb33905eSMatthias Ringwald { "AT+CNUM", HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION }, 91394a27792SMatthias Ringwald { "AT+COPS=", HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT }, 91494a27792SMatthias Ringwald { "AT+COPS?", HFP_CMD_QUERY_OPERATOR_SELECTION_NAME }, 915cb33905eSMatthias Ringwald { "AT+NREC=", HFP_CMD_TURN_OFF_EC_AND_NR, }, 916cb33905eSMatthias Ringwald { "AT+VGM=", HFP_CMD_SET_MICROPHONE_GAIN }, 917cb33905eSMatthias Ringwald { "AT+VGS=", HFP_CMD_SET_SPEAKER_GAIN }, 918cb33905eSMatthias Ringwald { "AT+VTS:", HFP_CMD_TRANSMIT_DTMF_CODES }, 91994a27792SMatthias Ringwald { "ATA", HFP_CMD_CALL_ANSWERED }, 92094a27792SMatthias Ringwald }; 92194a27792SMatthias Ringwald 92294a27792SMatthias Ringwald static hfp_command_entry_t hfp_hf_commmand_table[] = { 923cb33905eSMatthias Ringwald { "+BCS:", HFP_CMD_AG_SUGGESTED_CODEC }, 92494a27792SMatthias Ringwald { "+BIND:", HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS }, 925cb33905eSMatthias Ringwald { "+BINP", HFP_CMD_AG_SENT_PHONE_NUMBER }, 926cb33905eSMatthias Ringwald { "+BRSF:", HFP_CMD_SUPPORTED_FEATURES }, 927cb33905eSMatthias Ringwald { "+BSIR:", HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING }, 92894a27792SMatthias Ringwald { "+BTRH:", HFP_CMD_RESPONSE_AND_HOLD_STATUS }, 929cb33905eSMatthias Ringwald { "+BVRA:", HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION }, 930cb33905eSMatthias Ringwald { "+CCWA:", HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE, }, 93194a27792SMatthias Ringwald { "+CHLD:", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES }, 932cb33905eSMatthias Ringwald { "+CIEV:", HFP_CMD_TRANSFER_AG_INDICATOR_STATUS}, 93384a0c24eSMatthias Ringwald { "+CIND:", HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC }, 934cb33905eSMatthias Ringwald { "+CLCC:", HFP_CMD_LIST_CURRENT_CALLS }, 935cb33905eSMatthias Ringwald { "+CLIP:", HFP_CMD_AG_SENT_CLIP_INFORMATION }, 936cb33905eSMatthias Ringwald { "+CME ERROR:", HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR }, 937cb33905eSMatthias Ringwald { "+CNUM:", HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION}, 938cb33905eSMatthias Ringwald { "+COPS:", HFP_CMD_QUERY_OPERATOR_SELECTION_NAME }, 939cb33905eSMatthias Ringwald { "+VGM:", HFP_CMD_SET_MICROPHONE_GAIN }, 940cb33905eSMatthias Ringwald { "+VGS:", HFP_CMD_SET_SPEAKER_GAIN}, 941cb33905eSMatthias Ringwald { "ERROR", HFP_CMD_ERROR}, 942cb33905eSMatthias Ringwald { "NOP", HFP_CMD_NONE}, // dummy command used by unit tests 943cb33905eSMatthias Ringwald { "OK", HFP_CMD_OK }, 944cb33905eSMatthias Ringwald { "RING", HFP_CMD_RING }, 94594a27792SMatthias Ringwald }; 94694a27792SMatthias Ringwald 947aa4dd815SMatthias Ringwald static hfp_command_t parse_command(const char * line_buffer, int isHandsFree){ 9483deb3ec6SMatthias Ringwald 949cb33905eSMatthias Ringwald // table lookup based on role 95094a27792SMatthias Ringwald uint16_t num_entries; 95194a27792SMatthias Ringwald hfp_command_entry_t * table; 95294a27792SMatthias Ringwald if (isHandsFree == 0){ 95394a27792SMatthias Ringwald table = hfp_ag_commmand_table; 95494a27792SMatthias Ringwald num_entries = sizeof(hfp_ag_commmand_table) / sizeof(hfp_command_entry_t); 9553deb3ec6SMatthias Ringwald } else { 95694a27792SMatthias Ringwald table = hfp_hf_commmand_table; 95794a27792SMatthias Ringwald num_entries = sizeof(hfp_hf_commmand_table) / sizeof(hfp_command_entry_t); 95894a27792SMatthias Ringwald } 959791f0d0aSMatthias Ringwald // binary search 960791f0d0aSMatthias Ringwald uint16_t left = 0; 961791f0d0aSMatthias Ringwald uint16_t right = num_entries - 1; 962791f0d0aSMatthias Ringwald while (left <= right){ 963791f0d0aSMatthias Ringwald uint16_t middle = left + (right - left) / 2; 964791f0d0aSMatthias Ringwald hfp_command_entry_t *entry = &table[middle]; 96594a27792SMatthias Ringwald int match = strcmp(line_buffer, entry->command); 966791f0d0aSMatthias Ringwald if (match < 0){ 967791f0d0aSMatthias Ringwald // search term is lower than middle element 96847005182SMilanka Ringwald if (right == 0) break; 969791f0d0aSMatthias Ringwald right = middle - 1; 970791f0d0aSMatthias Ringwald } else if (match == 0){ 97194a27792SMatthias Ringwald return entry->command_id; 972791f0d0aSMatthias Ringwald } else { 973791f0d0aSMatthias Ringwald // search term is higher than middle element 974791f0d0aSMatthias Ringwald left = middle + 1; 97594a27792SMatthias Ringwald } 97694a27792SMatthias Ringwald } 97794a27792SMatthias Ringwald 978cb33905eSMatthias Ringwald // note: if parser in CMD_HEADER state would treats digits and maybe '+' as separator, match on "ATD" would work. 9790222a807SMatthias Ringwald // note: phone number is currently expected in line_buffer[3..] 980cb33905eSMatthias Ringwald // prefix match on 'ATD', AG only 981cb33905eSMatthias Ringwald if ((isHandsFree == 0) && (strncmp(line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0)){ 982cb33905eSMatthias Ringwald return HFP_CMD_CALL_PHONE_NUMBER; 9833deb3ec6SMatthias Ringwald } 9843deb3ec6SMatthias Ringwald 985cb33905eSMatthias Ringwald // Valid looking, but unknown commands/responses 986cb33905eSMatthias Ringwald if ((isHandsFree == 0) && (strncmp(line_buffer, "AT+", 3) == 0)){ 987aa4dd815SMatthias Ringwald return HFP_CMD_UNKNOWN; 9883deb3ec6SMatthias Ringwald } 9893deb3ec6SMatthias Ringwald 990cb33905eSMatthias Ringwald if ((isHandsFree != 0) && (strncmp(line_buffer, "+", 1) == 0)){ 991aa4dd815SMatthias Ringwald return HFP_CMD_UNKNOWN; 992aa4dd815SMatthias Ringwald } 9933deb3ec6SMatthias Ringwald 994aa4dd815SMatthias Ringwald return HFP_CMD_NONE; 9953deb3ec6SMatthias Ringwald } 9963deb3ec6SMatthias Ringwald 997a0ffb263SMatthias Ringwald static void hfp_parser_store_byte(hfp_connection_t * hfp_connection, uint8_t byte){ 99862c7768eSMatthias Ringwald if ((hfp_connection->line_size + 1 ) >= HFP_MAX_INDICATOR_DESC_SIZE) return; 999a0ffb263SMatthias Ringwald hfp_connection->line_buffer[hfp_connection->line_size++] = byte; 1000a0ffb263SMatthias Ringwald hfp_connection->line_buffer[hfp_connection->line_size] = 0; 10013deb3ec6SMatthias Ringwald } 1002a0ffb263SMatthias Ringwald static int hfp_parser_is_buffer_empty(hfp_connection_t * hfp_connection){ 1003a0ffb263SMatthias Ringwald return hfp_connection->line_size == 0; 10043deb3ec6SMatthias Ringwald } 10053deb3ec6SMatthias Ringwald 10063deb3ec6SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){ 1007c1ab6cc1SMatthias Ringwald return (byte == '\n') || (byte == '\r'); 10083deb3ec6SMatthias Ringwald } 10093deb3ec6SMatthias Ringwald 101094a27792SMatthias Ringwald static void hfp_parser_reset_line_buffer(hfp_connection_t *hfp_connection) { 1011a0ffb263SMatthias Ringwald hfp_connection->line_size = 0; 10123deb3ec6SMatthias Ringwald } 10131d81c7feSMatthias Ringwald 10141d81c7feSMatthias Ringwald static void hfp_parser_store_if_token(hfp_connection_t * hfp_connection, uint8_t byte){ 10151d81c7feSMatthias Ringwald switch (byte){ 10161d81c7feSMatthias Ringwald case ',': 10171d81c7feSMatthias Ringwald case ';': 10181d81c7feSMatthias Ringwald case '(': 10191d81c7feSMatthias Ringwald case ')': 10201d81c7feSMatthias Ringwald case '\n': 10211d81c7feSMatthias Ringwald case '\r': 10223deb3ec6SMatthias Ringwald break; 10233deb3ec6SMatthias Ringwald default: 10241d81c7feSMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 10253deb3ec6SMatthias Ringwald break; 10263deb3ec6SMatthias Ringwald } 10273deb3ec6SMatthias Ringwald } 10281d81c7feSMatthias Ringwald 10291d81c7feSMatthias Ringwald static bool hfp_parser_is_separator( uint8_t byte){ 10301d81c7feSMatthias Ringwald switch (byte){ 10311d81c7feSMatthias Ringwald case ',': 10321d81c7feSMatthias Ringwald case ';': 10331d81c7feSMatthias Ringwald case '\n': 10341d81c7feSMatthias Ringwald case '\r': 10351d81c7feSMatthias Ringwald return true; 1036dd533208SMatthias Ringwald default: 10371d81c7feSMatthias Ringwald return false; 10383deb3ec6SMatthias Ringwald } 10393deb3ec6SMatthias Ringwald } 10403deb3ec6SMatthias Ringwald 1041d6b237acSMatthias Ringwald static bool hfp_parse_byte(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){ 1042aa4dd815SMatthias Ringwald 10431dddc4f4SMatthias Ringwald // handle doubles quotes 10441dddc4f4SMatthias Ringwald if (byte == '"'){ 10451dddc4f4SMatthias Ringwald hfp_connection->parser_quoted = !hfp_connection->parser_quoted; 1046d6b237acSMatthias Ringwald return true; 10471dddc4f4SMatthias Ringwald } 10481dddc4f4SMatthias Ringwald if (hfp_connection->parser_quoted) { 10491dddc4f4SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 1050d6b237acSMatthias Ringwald return true; 10511dddc4f4SMatthias Ringwald } 10523deb3ec6SMatthias Ringwald 10531dddc4f4SMatthias Ringwald // ignore spaces outside command or double quotes (required e.g. for '+CME ERROR:..") command 1054d6b237acSMatthias Ringwald if ((byte == ' ') && (hfp_connection->parser_state != HFP_PARSER_CMD_HEADER)) return true; 10551dddc4f4SMatthias Ringwald 1056bfeaf344SMatthias Ringwald bool processed = true; 1057bfeaf344SMatthias Ringwald 1058a0ffb263SMatthias Ringwald switch (hfp_connection->parser_state) { 1059dd533208SMatthias Ringwald case HFP_PARSER_CMD_HEADER: 1060d28b2d5dSMilanka Ringwald switch (byte) { 1061dd533208SMatthias Ringwald case '\n': 1062dd533208SMatthias Ringwald case '\r': 1063dd533208SMatthias Ringwald case ';': 1064bfeaf344SMatthias Ringwald // ignore separator 1065bfeaf344SMatthias Ringwald break; 1066bfeaf344SMatthias Ringwald case ':': 1067bfeaf344SMatthias Ringwald case '?': 1068bfeaf344SMatthias Ringwald // store separator 1069bfeaf344SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 1070dd533208SMatthias Ringwald break; 1071d28b2d5dSMilanka Ringwald case '=': 1072bfeaf344SMatthias Ringwald // equal sign: remember and wait for next char to decided between '=?' and '=\?' 1073dd533208SMatthias Ringwald hfp_connection->found_equal_sign = true; 1074a0ffb263SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 1075d6b237acSMatthias Ringwald return true; 1076d28b2d5dSMilanka Ringwald default: 1077bfeaf344SMatthias Ringwald // store if not lookahead 1078d7f16ff1SMatthias Ringwald if (!hfp_connection->found_equal_sign) { 1079dd533208SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 1080d6b237acSMatthias Ringwald return true; 1081dd533208SMatthias Ringwald } 1082bfeaf344SMatthias Ringwald // mark as lookahead 1083bfeaf344SMatthias Ringwald processed = false; 1084d7f16ff1SMatthias Ringwald break; 1085d7f16ff1SMatthias Ringwald } 1086ce263fc8SMatthias Ringwald 10871d81c7feSMatthias Ringwald // ignore empty tokens 1088d6b237acSMatthias Ringwald if (hfp_parser_is_buffer_empty(hfp_connection)) return true; 1089dd533208SMatthias Ringwald 1090bfeaf344SMatthias Ringwald // parse 1091dd533208SMatthias Ringwald hfp_connection->command = parse_command((char *)hfp_connection->line_buffer, isHandsFree); 1092aa4dd815SMatthias Ringwald 109384a0c24eSMatthias Ringwald // pick +CIND version based on connection state: descriptions during SLC vs. states later 109484a0c24eSMatthias Ringwald if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC){ 1095a0ffb263SMatthias Ringwald switch(hfp_connection->state){ 1096aa4dd815SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS_STATUS: 1097a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS; 1098aa4dd815SMatthias Ringwald break; 1099aa4dd815SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS: 1100a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS; 1101aa4dd815SMatthias Ringwald break; 1102aa4dd815SMatthias Ringwald default: 110384a0c24eSMatthias Ringwald hfp_connection->command = HFP_CMD_UNKNOWN; 1104aa4dd815SMatthias Ringwald break; 1105aa4dd815SMatthias Ringwald } 1106aa4dd815SMatthias Ringwald } 11073deb3ec6SMatthias Ringwald 1108c0c0437aSMatthias Ringwald log_info("command string '%s', handsfree %u -> cmd id %u", (char *)hfp_connection->line_buffer, isHandsFree, hfp_connection->command); 1109c0c0437aSMatthias Ringwald 1110bfeaf344SMatthias Ringwald // next state 1111bfeaf344SMatthias Ringwald hfp_connection->found_equal_sign = false; 11121d81c7feSMatthias Ringwald hfp_parser_reset_line_buffer(hfp_connection); 1113dd533208SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE; 1114dd533208SMatthias Ringwald 1115bfeaf344SMatthias Ringwald return processed; 1116dd533208SMatthias Ringwald 1117bfeaf344SMatthias Ringwald case HFP_PARSER_CMD_SEQUENCE: 11181d81c7feSMatthias Ringwald // handle empty fields 11191d81c7feSMatthias Ringwald if ((byte == ',' ) && (hfp_connection->line_size == 0)){ 1120dd533208SMatthias Ringwald hfp_connection->line_buffer[0] = 0; 1121dd533208SMatthias Ringwald hfp_connection->ignore_value = 1; 1122dd533208SMatthias Ringwald parse_sequence(hfp_connection); 11233d051a53SMatthias Ringwald return true; 11243d051a53SMatthias Ringwald } 11253d051a53SMatthias Ringwald 11261d81c7feSMatthias Ringwald hfp_parser_store_if_token(hfp_connection, byte); 11271d81c7feSMatthias Ringwald if (!hfp_parser_is_separator(byte)) return true; 11281d81c7feSMatthias Ringwald 11291d81c7feSMatthias Ringwald // ignore empty tokens 11303d051a53SMatthias Ringwald if (hfp_parser_is_buffer_empty(hfp_connection) && (hfp_connection->ignore_value == 0)) return true; 11313d051a53SMatthias Ringwald 11323d051a53SMatthias Ringwald parse_sequence(hfp_connection); 11331d81c7feSMatthias Ringwald 11341d81c7feSMatthias Ringwald hfp_parser_reset_line_buffer(hfp_connection); 11351d81c7feSMatthias Ringwald 11361d81c7feSMatthias Ringwald switch (hfp_connection->command){ 11371d81c7feSMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 11381d81c7feSMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 11391d81c7feSMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 11401d81c7feSMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 11411d81c7feSMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 11421d81c7feSMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 11431d81c7feSMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 11441d81c7feSMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 11451d81c7feSMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 11461d81c7feSMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_SECOND_ITEM; 11471d81c7feSMatthias Ringwald break; 11481d81c7feSMatthias Ringwald default: 11491d81c7feSMatthias Ringwald break; 11501d81c7feSMatthias Ringwald } 11513d051a53SMatthias Ringwald return true; 11523d051a53SMatthias Ringwald 1153ba0de059SMatthias Ringwald case HFP_PARSER_SECOND_ITEM: 11541d81c7feSMatthias Ringwald 11551d81c7feSMatthias Ringwald hfp_parser_store_if_token(hfp_connection, byte); 11561d81c7feSMatthias Ringwald if (!hfp_parser_is_separator(byte)) return true; 1157dd533208SMatthias Ringwald 1158a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1159ce263fc8SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1160a0ffb263SMatthias Ringwald log_info("format %s, ", hfp_connection->line_buffer); 11612308e108SMilanka Ringwald hfp_connection->network_operator.format = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1162ce263fc8SMatthias Ringwald break; 1163ce263fc8SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 1164a0ffb263SMatthias Ringwald log_info("format %s \n", hfp_connection->line_buffer); 11652308e108SMilanka Ringwald hfp_connection->network_operator.format = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1166ce263fc8SMatthias Ringwald break; 1167ce263fc8SMatthias Ringwald case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 1168ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 1169ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 11702308e108SMilanka Ringwald hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].state = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1171ce263fc8SMatthias Ringwald break; 1172ce263fc8SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 11732308e108SMilanka Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1174a0ffb263SMatthias Ringwald log_info("%d \n", hfp_connection->ag_indicators[hfp_connection->parser_item_index].status); 1175a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].status_changed = 1; 1176ce263fc8SMatthias Ringwald break; 1177ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 11782308e108SMilanka Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].min_range = btstack_atoi((char *)hfp_connection->line_buffer); 1179a0ffb263SMatthias Ringwald log_info("%s, ", hfp_connection->line_buffer); 1180ce263fc8SMatthias Ringwald break; 1181ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 1182a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1183a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 11842308e108SMilanka Ringwald hfp_connection->bnip_type = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1185ce263fc8SMatthias Ringwald break; 11860222a807SMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 11870222a807SMatthias Ringwald hfp_connection->parser_indicator_value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 11880222a807SMatthias Ringwald break; 1189ce263fc8SMatthias Ringwald default: 1190ce263fc8SMatthias Ringwald break; 1191ce263fc8SMatthias Ringwald } 11921d81c7feSMatthias Ringwald 11931d81c7feSMatthias Ringwald hfp_parser_reset_line_buffer(hfp_connection); 11941d81c7feSMatthias Ringwald 11951d81c7feSMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_THIRD_ITEM; 11961d81c7feSMatthias Ringwald 1197ba0de059SMatthias Ringwald return true; 1198ce263fc8SMatthias Ringwald 119974c7548aSMatthias Ringwald case HFP_PARSER_THIRD_ITEM: 12001d81c7feSMatthias Ringwald 12011d81c7feSMatthias Ringwald hfp_parser_store_if_token(hfp_connection, byte); 12021d81c7feSMatthias Ringwald if (!hfp_parser_is_separator(byte)) return true; 12031d81c7feSMatthias Ringwald 1204a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1205ce263fc8SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 120689425bfcSMilanka Ringwald strncpy(hfp_connection->network_operator.name, (char *)hfp_connection->line_buffer, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE); 120789425bfcSMilanka Ringwald hfp_connection->network_operator.name[HFP_MAX_NETWORK_OPERATOR_NAME_SIZE - 1] = 0; 1208a0ffb263SMatthias Ringwald log_info("name %s\n", hfp_connection->line_buffer); 1209ce263fc8SMatthias Ringwald break; 1210ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 12112308e108SMilanka Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].max_range = btstack_atoi((char *)hfp_connection->line_buffer); 121225789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 121325789943SMilanka Ringwald hfp_connection->ag_indicators_nr = hfp_connection->parser_item_index; 1214a0ffb263SMatthias Ringwald log_info("%s)\n", hfp_connection->line_buffer); 1215ce263fc8SMatthias Ringwald break; 1216ce263fc8SMatthias Ringwald default: 1217ce263fc8SMatthias Ringwald break; 1218ce263fc8SMatthias Ringwald } 12191d81c7feSMatthias Ringwald 12201d81c7feSMatthias Ringwald hfp_parser_reset_line_buffer(hfp_connection); 12211d81c7feSMatthias Ringwald 12221d81c7feSMatthias Ringwald if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS){ 12231d81c7feSMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE; 12241d81c7feSMatthias Ringwald } else { 12251d81c7feSMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 12261d81c7feSMatthias Ringwald } 1227d6b237acSMatthias Ringwald return true; 122874c7548aSMatthias Ringwald 122974c7548aSMatthias Ringwald default: 123074c7548aSMatthias Ringwald btstack_assert(false); 123174c7548aSMatthias Ringwald return true; 123274c7548aSMatthias Ringwald } 1233d6b237acSMatthias Ringwald } 1234d6b237acSMatthias Ringwald 1235d6b237acSMatthias Ringwald void hfp_parse(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){ 1236d6b237acSMatthias Ringwald bool processed = false; 1237d6b237acSMatthias Ringwald while (!processed){ 1238d6b237acSMatthias Ringwald processed = hfp_parse_byte(hfp_connection, byte, isHandsFree); 1239d6b237acSMatthias Ringwald } 12401d81c7feSMatthias Ringwald // reset parser state on end-of-line 12411d81c7feSMatthias Ringwald if (hfp_parser_is_end_of_line(byte)){ 12421d81c7feSMatthias Ringwald hfp_connection->parser_item_index = 0; 12431d81c7feSMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 12441d81c7feSMatthias Ringwald } 1245ce263fc8SMatthias Ringwald } 1246ce263fc8SMatthias Ringwald 1247a0ffb263SMatthias Ringwald static void parse_sequence(hfp_connection_t * hfp_connection){ 1248ce263fc8SMatthias Ringwald int value; 1249a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1250667ec068SMatthias Ringwald case HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS: 12512308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1252667ec068SMatthias Ringwald int i; 1253a0ffb263SMatthias Ringwald switch (hfp_connection->parser_item_index){ 1254667ec068SMatthias Ringwald case 0: 1255a0ffb263SMatthias Ringwald for (i=0;i<hfp_connection->generic_status_indicators_nr;i++){ 1256a0ffb263SMatthias Ringwald if (hfp_connection->generic_status_indicators[i].uuid == value){ 1257a0ffb263SMatthias Ringwald hfp_connection->parser_indicator_index = i; 1258667ec068SMatthias Ringwald break; 1259667ec068SMatthias Ringwald } 1260667ec068SMatthias Ringwald } 1261667ec068SMatthias Ringwald break; 1262667ec068SMatthias Ringwald case 1: 1263a0ffb263SMatthias Ringwald if (hfp_connection->parser_indicator_index <0) break; 1264a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[hfp_connection->parser_indicator_index].state = value; 1265667ec068SMatthias Ringwald log_info("HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS set indicator at index %u, to %u\n", 1266a0ffb263SMatthias Ringwald hfp_connection->parser_item_index, value); 1267667ec068SMatthias Ringwald break; 1268667ec068SMatthias Ringwald default: 1269667ec068SMatthias Ringwald break; 1270667ec068SMatthias Ringwald } 127125789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 1272667ec068SMatthias Ringwald break; 1273667ec068SMatthias Ringwald 1274667ec068SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1275a0ffb263SMatthias Ringwald switch(hfp_connection->parser_item_index){ 1276667ec068SMatthias Ringwald case 0: 1277a0ffb263SMatthias Ringwald strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number)); 1278a0ffb263SMatthias Ringwald hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0; 1279667ec068SMatthias Ringwald break; 1280667ec068SMatthias Ringwald case 1: 12812308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1282a0ffb263SMatthias Ringwald hfp_connection->bnip_type = value; 1283667ec068SMatthias Ringwald break; 1284667ec068SMatthias Ringwald default: 1285667ec068SMatthias Ringwald break; 1286667ec068SMatthias Ringwald } 1287*eed67a37SMilanka Ringwald // index > 2 are ignored in switch above 1288a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1289667ec068SMatthias Ringwald break; 1290667ec068SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 1291a0ffb263SMatthias Ringwald switch(hfp_connection->parser_item_index){ 1292667ec068SMatthias Ringwald case 0: 12932308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1294a0ffb263SMatthias Ringwald hfp_connection->clcc_idx = value; 1295667ec068SMatthias Ringwald break; 1296667ec068SMatthias Ringwald case 1: 12972308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1298a0ffb263SMatthias Ringwald hfp_connection->clcc_dir = value; 1299667ec068SMatthias Ringwald break; 1300667ec068SMatthias Ringwald case 2: 13012308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1302a0ffb263SMatthias Ringwald hfp_connection->clcc_status = value; 1303667ec068SMatthias Ringwald break; 1304667ec068SMatthias Ringwald case 3: 13052308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 13060aee97efSMilanka Ringwald hfp_connection->clcc_mode = value; 1307667ec068SMatthias Ringwald break; 1308667ec068SMatthias Ringwald case 4: 13090aee97efSMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 13100aee97efSMilanka Ringwald hfp_connection->clcc_mpty = value; 13110aee97efSMilanka Ringwald break; 13120aee97efSMilanka Ringwald case 5: 1313a0ffb263SMatthias Ringwald strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number)); 1314a0ffb263SMatthias Ringwald hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0; 1315667ec068SMatthias Ringwald break; 13160aee97efSMilanka Ringwald case 6: 13172308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1318a0ffb263SMatthias Ringwald hfp_connection->bnip_type = value; 1319667ec068SMatthias Ringwald break; 1320667ec068SMatthias Ringwald default: 1321667ec068SMatthias Ringwald break; 1322667ec068SMatthias Ringwald } 1323*eed67a37SMilanka Ringwald // index > 6 are ignored in switch above 1324a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1325667ec068SMatthias Ringwald break; 1326aa4dd815SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 13272308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1328a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = value; 1329aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CMD_SET_MICROPHONE_GAIN %d\n", value); 1330aa4dd815SMatthias Ringwald break; 1331aa4dd815SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 13322308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1333a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = value; 1334aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CMD_SET_SPEAKER_GAIN %d\n", value); 1335aa4dd815SMatthias Ringwald break; 1336aa4dd815SMatthias Ringwald case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION: 13372308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1338a0ffb263SMatthias Ringwald hfp_connection->ag_activate_voice_recognition = value; 1339aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION %d\n", value); 1340aa4dd815SMatthias Ringwald break; 1341aa4dd815SMatthias Ringwald case HFP_CMD_TURN_OFF_EC_AND_NR: 13422308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1343a0ffb263SMatthias Ringwald hfp_connection->ag_echo_and_noise_reduction = value; 1344aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CMD_TURN_OFF_EC_AND_NR %d\n", value); 1345aa4dd815SMatthias Ringwald break; 1346aa4dd815SMatthias Ringwald case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING: 13472308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1348a0ffb263SMatthias Ringwald hfp_connection->remote_supported_features = store_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE, value); 1349aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CHANGE_IN_BAND_RING_TONE_SETTING %d\n", value); 1350aa4dd815SMatthias Ringwald break; 13513deb3ec6SMatthias Ringwald case HFP_CMD_HF_CONFIRMED_CODEC: 13522308e108SMilanka Ringwald hfp_connection->codec_confirmed = btstack_atoi((char*)hfp_connection->line_buffer); 1353a0ffb263SMatthias Ringwald log_info("hfp parse HFP_CMD_HF_CONFIRMED_CODEC %d\n", hfp_connection->codec_confirmed); 13543deb3ec6SMatthias Ringwald break; 13553deb3ec6SMatthias Ringwald case HFP_CMD_AG_SUGGESTED_CODEC: 13562308e108SMilanka Ringwald hfp_connection->suggested_codec = btstack_atoi((char*)hfp_connection->line_buffer); 1357a0ffb263SMatthias Ringwald log_info("hfp parse HFP_CMD_AG_SUGGESTED_CODEC %d\n", hfp_connection->suggested_codec); 13583deb3ec6SMatthias Ringwald break; 13593deb3ec6SMatthias Ringwald case HFP_CMD_SUPPORTED_FEATURES: 13602308e108SMilanka Ringwald hfp_connection->remote_supported_features = btstack_atoi((char*)hfp_connection->line_buffer); 1361bace42efSMatthias Ringwald log_info("Parsed supported feature %d\n", (int) hfp_connection->remote_supported_features); 13623deb3ec6SMatthias Ringwald break; 13633deb3ec6SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 1364a0ffb263SMatthias Ringwald log_info("Parsed codec %s\n", hfp_connection->line_buffer); 13652308e108SMilanka Ringwald hfp_connection->remote_codecs[hfp_connection->parser_item_index] = (uint16_t)btstack_atoi((char*)hfp_connection->line_buffer); 136625789943SMilanka Ringwald hfp_next_codec_index(hfp_connection); 1367a0ffb263SMatthias Ringwald hfp_connection->remote_codecs_nr = hfp_connection->parser_item_index; 13683deb3ec6SMatthias Ringwald break; 1369aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 137089425bfcSMilanka Ringwald strncpy((char *)hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, (char *)hfp_connection->line_buffer, HFP_MAX_INDICATOR_DESC_SIZE); 137189425bfcSMilanka Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].name[HFP_MAX_INDICATOR_DESC_SIZE-1] = 0; 1372a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].index = hfp_connection->parser_item_index+1; 1373a0ffb263SMatthias Ringwald log_info("Indicator %d: %s (", hfp_connection->ag_indicators_nr+1, hfp_connection->line_buffer); 1374aa4dd815SMatthias Ringwald break; 1375aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 1376a0ffb263SMatthias Ringwald log_info("Parsed Indicator %d with status: %s\n", hfp_connection->parser_item_index+1, hfp_connection->line_buffer); 13772308e108SMilanka Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = btstack_atoi((char *) hfp_connection->line_buffer); 137825789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 13793deb3ec6SMatthias Ringwald break; 13803deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 138125789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 1382a0ffb263SMatthias Ringwald if (hfp_connection->parser_item_index != 4) break; 1383a0ffb263SMatthias Ringwald log_info("Parsed Enable indicators: %s\n", hfp_connection->line_buffer); 13842308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1385a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = (uint8_t) value; 13863deb3ec6SMatthias Ringwald break; 13873deb3ec6SMatthias Ringwald case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES: 1388a0ffb263SMatthias Ringwald log_info("Parsed Support call hold: %s\n", hfp_connection->line_buffer); 1389a0ffb263SMatthias Ringwald if (hfp_connection->line_size > 2 ) break; 139025789943SMilanka Ringwald strncpy((char *)hfp_connection->remote_call_services[hfp_connection->remote_call_services_index].name, (char *)hfp_connection->line_buffer, HFP_CALL_SERVICE_SIZE); 139125789943SMilanka Ringwald hfp_connection->remote_call_services[hfp_connection->remote_call_services_index].name[HFP_CALL_SERVICE_SIZE - 1] = 0; 1392*eed67a37SMilanka Ringwald hfp_next_remote_call_services_index(hfp_connection); 13933deb3ec6SMatthias Ringwald break; 1394aa4dd815SMatthias Ringwald case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 1395aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 1396a0ffb263SMatthias Ringwald log_info("Parsed Generic status indicator: %s\n", hfp_connection->line_buffer); 13972308e108SMilanka Ringwald hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].uuid = (uint16_t)btstack_atoi((char*)hfp_connection->line_buffer); 139825789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 1399a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators_nr = hfp_connection->parser_item_index; 14003deb3ec6SMatthias Ringwald break; 1401aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 14023deb3ec6SMatthias Ringwald // HF parses inital AG gen. ind. state 1403a0ffb263SMatthias Ringwald log_info("Parsed List generic status indicator %s state: ", hfp_connection->line_buffer); 140425789943SMilanka Ringwald hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection); 14053deb3ec6SMatthias Ringwald break; 1406ce263fc8SMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 140725789943SMilanka Ringwald hfp_connection->parser_indicator_index = hfp_parse_indicator_index(hfp_connection); 1408a0ffb263SMatthias Ringwald log_info("Parsed HF indicator index %u", hfp_connection->parser_indicator_index); 1409ce263fc8SMatthias Ringwald break; 14103deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE: 14113deb3ec6SMatthias Ringwald // AG parses new gen. ind. state 1412a0ffb263SMatthias Ringwald if (hfp_connection->ignore_value){ 1413a0ffb263SMatthias Ringwald hfp_connection->ignore_value = 0; 1414a0ffb263SMatthias Ringwald log_info("Parsed Enable AG indicator pos %u('%s') - unchanged (stays %u)\n", hfp_connection->parser_item_index, 1415a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled); 1416ce263fc8SMatthias Ringwald } 1417a0ffb263SMatthias Ringwald else if (hfp_connection->ag_indicators[hfp_connection->parser_item_index].mandatory){ 1418ce263fc8SMatthias Ringwald log_info("Parsed Enable AG indicator pos %u('%s') - ignore (mandatory)\n", 1419a0ffb263SMatthias Ringwald hfp_connection->parser_item_index, hfp_connection->ag_indicators[hfp_connection->parser_item_index].name); 1420ce263fc8SMatthias Ringwald } else { 14212308e108SMilanka Ringwald value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1422a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled = value; 1423a0ffb263SMatthias Ringwald log_info("Parsed Enable AG indicator pos %u('%s'): %u\n", hfp_connection->parser_item_index, 1424a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, value); 14253deb3ec6SMatthias Ringwald } 142625789943SMilanka Ringwald hfp_next_indicators_index(hfp_connection); 14273deb3ec6SMatthias Ringwald break; 14283deb3ec6SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 14293deb3ec6SMatthias Ringwald // indicators are indexed starting with 1 143025789943SMilanka Ringwald hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection); 1431a0ffb263SMatthias Ringwald log_info("Parsed status of the AG indicator %d, status ", hfp_connection->parser_item_index); 14323deb3ec6SMatthias Ringwald break; 1433aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 14342308e108SMilanka Ringwald hfp_connection->network_operator.mode = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1435a0ffb263SMatthias Ringwald log_info("Parsed network operator mode: %d, ", hfp_connection->network_operator.mode); 1436aa4dd815SMatthias Ringwald break; 1437aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 1438a0ffb263SMatthias Ringwald if (hfp_connection->line_buffer[0] == '3'){ 1439a0ffb263SMatthias Ringwald log_info("Parsed Set network operator format : %s, ", hfp_connection->line_buffer); 14403deb3ec6SMatthias Ringwald break; 14413deb3ec6SMatthias Ringwald } 14423deb3ec6SMatthias Ringwald // TODO emit ERROR, wrong format 1443a0ffb263SMatthias Ringwald log_info("ERROR Set network operator format: index %s not supported\n", hfp_connection->line_buffer); 14443deb3ec6SMatthias Ringwald break; 14453deb3ec6SMatthias Ringwald case HFP_CMD_ERROR: 14463deb3ec6SMatthias Ringwald break; 14473deb3ec6SMatthias Ringwald case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1448a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 1; 14492308e108SMilanka Ringwald hfp_connection->extended_audio_gateway_error_value = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 14503deb3ec6SMatthias Ringwald break; 14513deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR: 14522308e108SMilanka Ringwald hfp_connection->enable_extended_audio_gateway_error_report = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1453a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1454a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 14553deb3ec6SMatthias Ringwald break; 1456ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 1457a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1458a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1459a0ffb263SMatthias Ringwald strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number)); 1460a0ffb263SMatthias Ringwald hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0; 14613deb3ec6SMatthias Ringwald break; 14620222a807SMatthias Ringwald case HFP_CMD_CALL_HOLD: 14630222a807SMatthias Ringwald hfp_connection->ag_call_hold_action = hfp_connection->line_buffer[0] - '0'; 14640222a807SMatthias Ringwald if (hfp_connection->line_buffer[1] != '\0'){ 14650222a807SMatthias Ringwald hfp_connection->call_index = btstack_atoi((char *)&hfp_connection->line_buffer[1]); 14660222a807SMatthias Ringwald } 14670222a807SMatthias Ringwald break; 14680222a807SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_COMMAND: 14690222a807SMatthias Ringwald hfp_connection->ag_response_and_hold_action = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 14700222a807SMatthias Ringwald break; 14710222a807SMatthias Ringwald case HFP_CMD_TRANSMIT_DTMF_CODES: 14720222a807SMatthias Ringwald hfp_connection->ag_dtmf_code = hfp_connection->line_buffer[0]; 14730222a807SMatthias Ringwald break; 14740222a807SMatthias Ringwald case HFP_CMD_ENABLE_CLIP: 14750222a807SMatthias Ringwald hfp_connection->clip_enabled = hfp_connection->line_buffer[0] != '0'; 14760222a807SMatthias Ringwald break; 14770222a807SMatthias Ringwald case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION: 14780222a807SMatthias Ringwald hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[0] != '0'; 14790222a807SMatthias Ringwald break; 14803deb3ec6SMatthias Ringwald default: 14813deb3ec6SMatthias Ringwald break; 14823deb3ec6SMatthias Ringwald } 14833deb3ec6SMatthias Ringwald } 14843deb3ec6SMatthias Ringwald 1485323d3000SMatthias Ringwald void hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid, hfp_role_t local_role){ 1486323d3000SMatthias Ringwald hfp_connection_t * hfp_connection = provide_hfp_connection_context_for_bd_addr(bd_addr, local_role); 1487a0ffb263SMatthias Ringwald log_info("hfp_connect %s, hfp_connection %p", bd_addr_to_str(bd_addr), hfp_connection); 14883deb3ec6SMatthias Ringwald 1489a0ffb263SMatthias Ringwald if (!hfp_connection) { 14903deb3ec6SMatthias Ringwald log_error("hfp_establish_service_level_connection for addr %s failed", bd_addr_to_str(bd_addr)); 14913deb3ec6SMatthias Ringwald return; 14923deb3ec6SMatthias Ringwald } 1493a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 14943deb3ec6SMatthias Ringwald case HFP_W2_DISCONNECT_RFCOMM: 1495a0ffb263SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 14963deb3ec6SMatthias Ringwald return; 14973deb3ec6SMatthias Ringwald case HFP_W4_RFCOMM_DISCONNECTED: 1498a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART; 14993deb3ec6SMatthias Ringwald return; 15003deb3ec6SMatthias Ringwald case HFP_IDLE: 15016535961aSMatthias Ringwald (void)memcpy(hfp_connection->remote_addr, bd_addr, 6); 1502d63c37a1SMatthias Ringwald hfp_connection->state = HFP_W4_SDP_QUERY_COMPLETE; 1503a0ffb263SMatthias Ringwald connection_doing_sdp_query = hfp_connection; 1504a0ffb263SMatthias Ringwald hfp_connection->service_uuid = service_uuid; 15057fbb5f59SMatthias Ringwald sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, hfp_connection->remote_addr, service_uuid); 15063deb3ec6SMatthias Ringwald break; 15073deb3ec6SMatthias Ringwald default: 15083deb3ec6SMatthias Ringwald break; 15093deb3ec6SMatthias Ringwald } 15103deb3ec6SMatthias Ringwald } 15113deb3ec6SMatthias Ringwald 1512a0ffb263SMatthias Ringwald void hfp_release_service_level_connection(hfp_connection_t * hfp_connection){ 1513a0ffb263SMatthias Ringwald if (!hfp_connection) return; 1514a0ffb263SMatthias Ringwald hfp_release_audio_connection(hfp_connection); 15153deb3ec6SMatthias Ringwald 1516a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_W4_RFCOMM_CONNECTED){ 1517a0ffb263SMatthias Ringwald hfp_connection->state = HFP_IDLE; 15183deb3ec6SMatthias Ringwald return; 15193deb3ec6SMatthias Ringwald } 15203deb3ec6SMatthias Ringwald 1521a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_W4_RFCOMM_CONNECTED){ 1522a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN; 15233deb3ec6SMatthias Ringwald return; 15243deb3ec6SMatthias Ringwald } 15253deb3ec6SMatthias Ringwald 15260c3047fbSMatthias Ringwald if (hfp_connection->state < HFP_W4_SCO_CONNECTED){ 15270c3047fbSMatthias Ringwald hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM; 15280c3047fbSMatthias Ringwald return; 15290c3047fbSMatthias Ringwald } 15300c3047fbSMatthias Ringwald 15310c3047fbSMatthias Ringwald if (hfp_connection->state < HFP_W4_SCO_DISCONNECTED){ 15320c3047fbSMatthias Ringwald hfp_connection->state = HFP_W2_DISCONNECT_SCO; 15330c3047fbSMatthias Ringwald return; 15340c3047fbSMatthias Ringwald } 15350c3047fbSMatthias Ringwald 15360c3047fbSMatthias Ringwald // HFP_W4_SCO_DISCONNECTED or later 15375f053052SMilanka Ringwald hfp_connection->release_slc_connection = 1; 15383deb3ec6SMatthias Ringwald } 15393deb3ec6SMatthias Ringwald 1540a0ffb263SMatthias Ringwald void hfp_release_audio_connection(hfp_connection_t * hfp_connection){ 1541a0ffb263SMatthias Ringwald if (!hfp_connection) return; 1542a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return; 1543a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 1; 15443deb3ec6SMatthias Ringwald } 15453deb3ec6SMatthias Ringwald 1546ce263fc8SMatthias Ringwald static const struct link_settings { 1547ce263fc8SMatthias Ringwald const uint16_t max_latency; 1548ce263fc8SMatthias Ringwald const uint8_t retransmission_effort; 1549ce263fc8SMatthias Ringwald const uint16_t packet_types; 1550ce263fc8SMatthias Ringwald } hfp_link_settings [] = { 1551ce263fc8SMatthias Ringwald { 0xffff, 0xff, 0x03c1 }, // HFP_LINK_SETTINGS_D0, HV1 1552ce263fc8SMatthias Ringwald { 0xffff, 0xff, 0x03c4 }, // HFP_LINK_SETTINGS_D1, HV3 1553ce263fc8SMatthias Ringwald { 0x0007, 0x01, 0x03c8 }, // HFP_LINK_SETTINGS_S1, EV3 1554ce263fc8SMatthias Ringwald { 0x0007, 0x01, 0x0380 }, // HFP_LINK_SETTINGS_S2, 2-EV3 1555ce263fc8SMatthias Ringwald { 0x000a, 0x01, 0x0380 }, // HFP_LINK_SETTINGS_S3, 2-EV3 1556ce263fc8SMatthias Ringwald { 0x000c, 0x02, 0x0380 }, // HFP_LINK_SETTINGS_S4, 2-EV3 1557ce263fc8SMatthias Ringwald { 0x0008, 0x02, 0x03c8 }, // HFP_LINK_SETTINGS_T1, EV3 1558ce263fc8SMatthias Ringwald { 0x000d, 0x02, 0x0380 } // HFP_LINK_SETTINGS_T2, 2-EV3 1559ce263fc8SMatthias Ringwald }; 15603deb3ec6SMatthias Ringwald 1561eddcd308SMatthias Ringwald void hfp_setup_synchronous_connection(hfp_connection_t * hfp_connection){ 1562ce263fc8SMatthias Ringwald // all packet types, fixed bandwidth 1563eddcd308SMatthias Ringwald int setting = hfp_connection->link_setting; 1564ce263fc8SMatthias Ringwald log_info("hfp_setup_synchronous_connection using setting nr %u", setting); 1565eddcd308SMatthias Ringwald sco_establishment_active = hfp_connection; 1566d2c1d59aSMatthias Ringwald uint16_t sco_voice_setting = hci_get_sco_voice_setting(); 1567d2c1d59aSMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 15687522e673SMatthias Ringwald sco_voice_setting = 0x0043; // Transparent data 1569d2c1d59aSMatthias Ringwald } 1570eddcd308SMatthias Ringwald hci_send_cmd(&hci_setup_synchronous_connection, hfp_connection->acl_handle, 8000, 8000, hfp_link_settings[setting].max_latency, 1571d2c1d59aSMatthias Ringwald sco_voice_setting, hfp_link_settings[setting].retransmission_effort, hfp_link_settings[setting].packet_types); // all types 0x003f, only 2-ev3 0x380 1572ce263fc8SMatthias Ringwald } 1573d68dcce1SMatthias Ringwald 15741b005648SMatthias Ringwald void hfp_set_hf_callback(btstack_packet_handler_t callback){ 15751b005648SMatthias Ringwald hfp_hf_callback = callback; 15761b005648SMatthias Ringwald } 15771b005648SMatthias Ringwald 15781b005648SMatthias Ringwald void hfp_set_ag_callback(btstack_packet_handler_t callback){ 15791b005648SMatthias Ringwald hfp_ag_callback = callback; 15801b005648SMatthias Ringwald } 15811b005648SMatthias Ringwald 1582ca59be51SMatthias Ringwald void hfp_set_ag_rfcomm_packet_handler(btstack_packet_handler_t handler){ 1583ca59be51SMatthias Ringwald hfp_ag_rfcomm_packet_handler = handler; 1584ca59be51SMatthias Ringwald } 15851b005648SMatthias Ringwald 1586ca59be51SMatthias Ringwald void hfp_set_hf_rfcomm_packet_handler(btstack_packet_handler_t handler){ 1587ca59be51SMatthias Ringwald hfp_hf_rfcomm_packet_handler = handler; 1588d68dcce1SMatthias Ringwald } 1589d68dcce1SMatthias Ringwald 15901b005648SMatthias Ringwald void hfp_set_hf_run_for_context(void (*callback)(hfp_connection_t * hfp_connection)){ 15911b005648SMatthias Ringwald hfp_hf_run_for_context = callback; 15921b005648SMatthias Ringwald } 15931b005648SMatthias Ringwald 1594520c92d5SMatthias Ringwald void hfp_init(void){ 1595520c92d5SMatthias Ringwald } 1596186dd3d2SMatthias Ringwald 1597afac2a04SMilanka Ringwald void hfp_init_link_settings(hfp_connection_t * hfp_connection, uint8_t esco_s4_supported){ 1598afac2a04SMilanka Ringwald // determine highest possible link setting 1599afac2a04SMilanka Ringwald hfp_connection->link_setting = HFP_LINK_SETTINGS_D1; 1600afac2a04SMilanka Ringwald // anything else requires eSCO support on both sides 1601afac2a04SMilanka Ringwald if (hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){ 1602afac2a04SMilanka Ringwald switch (hfp_connection->negotiated_codec){ 1603afac2a04SMilanka Ringwald case HFP_CODEC_CVSD: 1604afac2a04SMilanka Ringwald hfp_connection->link_setting = HFP_LINK_SETTINGS_S3; 1605afac2a04SMilanka Ringwald if (esco_s4_supported){ 1606afac2a04SMilanka Ringwald hfp_connection->link_setting = HFP_LINK_SETTINGS_S4; 1607afac2a04SMilanka Ringwald } 1608afac2a04SMilanka Ringwald break; 1609afac2a04SMilanka Ringwald case HFP_CODEC_MSBC: 1610afac2a04SMilanka Ringwald hfp_connection->link_setting = HFP_LINK_SETTINGS_T2; 1611afac2a04SMilanka Ringwald break; 1612afac2a04SMilanka Ringwald default: 1613afac2a04SMilanka Ringwald break; 1614afac2a04SMilanka Ringwald } 1615afac2a04SMilanka Ringwald } 1616afac2a04SMilanka Ringwald log_info("hfp_init_link_settings: %u", hfp_connection->link_setting); 1617afac2a04SMilanka Ringwald } 1618afac2a04SMilanka Ringwald 1619186dd3d2SMatthias Ringwald #define HFP_HF_RX_DEBUG_PRINT_LINE 80 1620186dd3d2SMatthias Ringwald 1621186dd3d2SMatthias Ringwald void hfp_log_rfcomm_message(const char * tag, uint8_t * packet, uint16_t size){ 1622186dd3d2SMatthias Ringwald #ifdef ENABLE_LOG_INFO 1623186dd3d2SMatthias Ringwald // encode \n\r 1624186dd3d2SMatthias Ringwald char printable[HFP_HF_RX_DEBUG_PRINT_LINE+2]; 162515a27967SMatthias Ringwald int i = 0; 1626186dd3d2SMatthias Ringwald int pos; 162715a27967SMatthias Ringwald for (pos=0 ; (pos < size) && (i < (HFP_HF_RX_DEBUG_PRINT_LINE - 3)) ; pos++){ 1628186dd3d2SMatthias Ringwald switch (packet[pos]){ 1629186dd3d2SMatthias Ringwald case '\n': 1630186dd3d2SMatthias Ringwald printable[i++] = '\\'; 1631186dd3d2SMatthias Ringwald printable[i++] = 'n'; 1632186dd3d2SMatthias Ringwald break; 1633186dd3d2SMatthias Ringwald case '\r': 1634186dd3d2SMatthias Ringwald printable[i++] = '\\'; 1635186dd3d2SMatthias Ringwald printable[i++] = 'r'; 1636186dd3d2SMatthias Ringwald break; 1637186dd3d2SMatthias Ringwald default: 1638186dd3d2SMatthias Ringwald printable[i++] = packet[pos]; 1639186dd3d2SMatthias Ringwald break; 1640186dd3d2SMatthias Ringwald } 1641186dd3d2SMatthias Ringwald } 1642186dd3d2SMatthias Ringwald printable[i] = 0; 1643186dd3d2SMatthias Ringwald log_info("%s: '%s'", tag, printable); 1644186dd3d2SMatthias Ringwald #endif 1645186dd3d2SMatthias Ringwald } 1646