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 */ 373deb3ec6SMatthias Ringwald 383deb3ec6SMatthias Ringwald 397907f069SMatthias Ringwald #include "btstack_config.h" 403deb3ec6SMatthias Ringwald 413deb3ec6SMatthias Ringwald #include <stdint.h> 423deb3ec6SMatthias Ringwald #include <stdio.h> 433deb3ec6SMatthias Ringwald #include <stdlib.h> 443deb3ec6SMatthias Ringwald #include <string.h> 453deb3ec6SMatthias Ringwald #include <inttypes.h> 463deb3ec6SMatthias Ringwald 47023f2764SMatthias Ringwald #include "btstack_debug.h" 4859c6af15SMatthias Ringwald #include "btstack_event.h" 4959c6af15SMatthias Ringwald #include "btstack_memory.h" 5059c6af15SMatthias Ringwald #include "btstack_run_loop.h" 5159c6af15SMatthias Ringwald #include "classic/core.h" 52efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h" 53746ccb7eSMatthias Ringwald #include "classic/sdp_server.h" 54023f2764SMatthias Ringwald #include "classic/sdp_util.h" 5559c6af15SMatthias Ringwald #include "hci.h" 5659c6af15SMatthias Ringwald #include "hci_cmd.h" 5759c6af15SMatthias Ringwald #include "hci_dump.h" 5859c6af15SMatthias Ringwald #include "l2cap.h" 593deb3ec6SMatthias Ringwald 603deb3ec6SMatthias Ringwald #define HFP_HF_FEATURES_SIZE 10 613deb3ec6SMatthias Ringwald #define HFP_AG_FEATURES_SIZE 12 623deb3ec6SMatthias Ringwald 633deb3ec6SMatthias Ringwald 643deb3ec6SMatthias Ringwald static const char * hfp_hf_features[] = { 653deb3ec6SMatthias Ringwald "EC and/or NR function", 663deb3ec6SMatthias Ringwald "Three-way calling", 673deb3ec6SMatthias Ringwald "CLI presentation capability", 683deb3ec6SMatthias Ringwald "Voice recognition activation", 693deb3ec6SMatthias Ringwald "Remote volume control", 703deb3ec6SMatthias Ringwald 713deb3ec6SMatthias Ringwald "Enhanced call status", 723deb3ec6SMatthias Ringwald "Enhanced call control", 733deb3ec6SMatthias Ringwald 743deb3ec6SMatthias Ringwald "Codec negotiation", 753deb3ec6SMatthias Ringwald 763deb3ec6SMatthias Ringwald "HF Indicators", 773deb3ec6SMatthias Ringwald "eSCO S4 (and T2) Settings Supported", 783deb3ec6SMatthias Ringwald "Reserved for future definition" 793deb3ec6SMatthias Ringwald }; 803deb3ec6SMatthias Ringwald 813deb3ec6SMatthias Ringwald static const char * hfp_ag_features[] = { 823deb3ec6SMatthias Ringwald "Three-way calling", 833deb3ec6SMatthias Ringwald "EC and/or NR function", 843deb3ec6SMatthias Ringwald "Voice recognition function", 853deb3ec6SMatthias Ringwald "In-band ring tone capability", 863deb3ec6SMatthias Ringwald "Attach a number to a voice tag", 873deb3ec6SMatthias Ringwald "Ability to reject a call", 883deb3ec6SMatthias Ringwald "Enhanced call status", 893deb3ec6SMatthias Ringwald "Enhanced call control", 903deb3ec6SMatthias Ringwald "Extended Error Result Codes", 913deb3ec6SMatthias Ringwald "Codec negotiation", 923deb3ec6SMatthias Ringwald "HF Indicators", 933deb3ec6SMatthias Ringwald "eSCO S4 (and T2) Settings Supported", 943deb3ec6SMatthias Ringwald "Reserved for future definition" 953deb3ec6SMatthias Ringwald }; 963deb3ec6SMatthias Ringwald 978f2a52f4SMatthias Ringwald static btstack_linked_list_t hfp_connections = NULL; 98ce263fc8SMatthias Ringwald static void parse_sequence(hfp_connection_t * context); 9913839019SMatthias Ringwald static btstack_packet_handler_t hfp_callback; 100d68dcce1SMatthias Ringwald static btstack_packet_handler_t rfcomm_packet_handler; 101f4000eebSMatthias Ringwald 102eddcd308SMatthias Ringwald static hfp_connection_t * sco_establishment_active; 103eddcd308SMatthias Ringwald 10413839019SMatthias Ringwald void hfp_set_callback(btstack_packet_handler_t callback){ 105f4000eebSMatthias Ringwald hfp_callback = callback; 106f4000eebSMatthias Ringwald } 1073deb3ec6SMatthias Ringwald 1083deb3ec6SMatthias Ringwald const char * hfp_hf_feature(int index){ 1093deb3ec6SMatthias Ringwald if (index > HFP_HF_FEATURES_SIZE){ 1103deb3ec6SMatthias Ringwald return hfp_hf_features[HFP_HF_FEATURES_SIZE]; 1113deb3ec6SMatthias Ringwald } 1123deb3ec6SMatthias Ringwald return hfp_hf_features[index]; 1133deb3ec6SMatthias Ringwald } 1143deb3ec6SMatthias Ringwald 1153deb3ec6SMatthias Ringwald const char * hfp_ag_feature(int index){ 1163deb3ec6SMatthias Ringwald if (index > HFP_AG_FEATURES_SIZE){ 1173deb3ec6SMatthias Ringwald return hfp_ag_features[HFP_AG_FEATURES_SIZE]; 1183deb3ec6SMatthias Ringwald } 1193deb3ec6SMatthias Ringwald return hfp_ag_features[index]; 1203deb3ec6SMatthias Ringwald } 1213deb3ec6SMatthias Ringwald 1223deb3ec6SMatthias Ringwald int send_str_over_rfcomm(uint16_t cid, char * command){ 1233deb3ec6SMatthias Ringwald if (!rfcomm_can_send_packet_now(cid)) return 1; 12474386ee0SMatthias Ringwald log_info("HFP_TX %s", command); 12528190c0bSMatthias Ringwald int err = rfcomm_send(cid, (uint8_t*) command, strlen(command)); 1263deb3ec6SMatthias Ringwald if (err){ 12728190c0bSMatthias Ringwald log_error("rfcomm_send -> error 0x%02x \n", err); 1283deb3ec6SMatthias Ringwald } 1293deb3ec6SMatthias Ringwald return 1; 1303deb3ec6SMatthias Ringwald } 1313deb3ec6SMatthias Ringwald 1326a7f44bdSMilanka Ringwald int hfp_supports_codec(uint8_t codec, int codecs_nr, uint8_t * codecs){ 133e8d1dc0dSMatthias Ringwald 134e8d1dc0dSMatthias Ringwald // mSBC requires support for eSCO connections 135e8d1dc0dSMatthias Ringwald if (codec == HFP_CODEC_MSBC && !hci_extended_sco_link_supported()) return 0; 136e8d1dc0dSMatthias Ringwald 137df327ff3SMilanka Ringwald int i; 138df327ff3SMilanka Ringwald for (i = 0; i < codecs_nr; i++){ 139e8d1dc0dSMatthias Ringwald if (codecs[i] != codec) continue; 140e8d1dc0dSMatthias Ringwald return 1; 141df327ff3SMilanka Ringwald } 142df327ff3SMilanka Ringwald return 0; 143df327ff3SMilanka Ringwald } 144df327ff3SMilanka Ringwald 1453deb3ec6SMatthias Ringwald #if 0 146a0ffb263SMatthias Ringwald void hfp_set_codec(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){ 1473deb3ec6SMatthias Ringwald // parse available codecs 1483deb3ec6SMatthias Ringwald int pos = 0; 1493deb3ec6SMatthias Ringwald int i; 1503deb3ec6SMatthias Ringwald for (i=0; i<size; i++){ 1513deb3ec6SMatthias Ringwald pos+=8; 152a0ffb263SMatthias Ringwald if (packet[pos] > hfp_connection->negotiated_codec){ 153a0ffb263SMatthias Ringwald hfp_connection->negotiated_codec = packet[pos]; 1543deb3ec6SMatthias Ringwald } 1553deb3ec6SMatthias Ringwald } 156a0ffb263SMatthias Ringwald printf("Negotiated Codec 0x%02x\n", hfp_connection->negotiated_codec); 1573deb3ec6SMatthias Ringwald } 1583deb3ec6SMatthias Ringwald #endif 1593deb3ec6SMatthias Ringwald 1603deb3ec6SMatthias Ringwald // UTILS 1613deb3ec6SMatthias Ringwald int get_bit(uint16_t bitmap, int position){ 1623deb3ec6SMatthias Ringwald return (bitmap >> position) & 1; 1633deb3ec6SMatthias Ringwald } 1643deb3ec6SMatthias Ringwald 1653deb3ec6SMatthias Ringwald int store_bit(uint32_t bitmap, int position, uint8_t value){ 1663deb3ec6SMatthias Ringwald if (value){ 1673deb3ec6SMatthias Ringwald bitmap |= 1 << position; 1683deb3ec6SMatthias Ringwald } else { 1693deb3ec6SMatthias Ringwald bitmap &= ~ (1 << position); 1703deb3ec6SMatthias Ringwald } 1713deb3ec6SMatthias Ringwald return bitmap; 1723deb3ec6SMatthias Ringwald } 1733deb3ec6SMatthias Ringwald 1743deb3ec6SMatthias Ringwald int join(char * buffer, int buffer_size, uint8_t * values, int values_nr){ 1753deb3ec6SMatthias Ringwald if (buffer_size < values_nr * 3) return 0; 1763deb3ec6SMatthias Ringwald int i; 1773deb3ec6SMatthias Ringwald int offset = 0; 1783deb3ec6SMatthias Ringwald for (i = 0; i < values_nr-1; i++) { 1793deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", values[i]); // puts string into buffer 1803deb3ec6SMatthias Ringwald } 1813deb3ec6SMatthias Ringwald if (i<values_nr){ 1823deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d", values[i]); 1833deb3ec6SMatthias Ringwald } 1843deb3ec6SMatthias Ringwald return offset; 1853deb3ec6SMatthias Ringwald } 1863deb3ec6SMatthias Ringwald 1873deb3ec6SMatthias Ringwald int join_bitmap(char * buffer, int buffer_size, uint32_t values, int values_nr){ 1883deb3ec6SMatthias Ringwald if (buffer_size < values_nr * 3) return 0; 1893deb3ec6SMatthias Ringwald 1903deb3ec6SMatthias Ringwald int i; 1913deb3ec6SMatthias Ringwald int offset = 0; 1923deb3ec6SMatthias Ringwald for (i = 0; i < values_nr-1; i++) { 1933deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", get_bit(values,i)); // puts string into buffer 1943deb3ec6SMatthias Ringwald } 1953deb3ec6SMatthias Ringwald 1963deb3ec6SMatthias Ringwald if (i<values_nr){ 1973deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d", get_bit(values,i)); 1983deb3ec6SMatthias Ringwald } 1993deb3ec6SMatthias Ringwald return offset; 2003deb3ec6SMatthias Ringwald } 2013deb3ec6SMatthias Ringwald 20213839019SMatthias Ringwald void hfp_emit_simple_event(btstack_packet_handler_t callback, uint8_t event_subtype){ 203a0ffb263SMatthias Ringwald if (!callback) return; 204a0ffb263SMatthias Ringwald uint8_t event[3]; 205a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 206a0ffb263SMatthias Ringwald event[1] = sizeof(event) - 2; 207a0ffb263SMatthias Ringwald event[2] = event_subtype; 20813839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 209a0ffb263SMatthias Ringwald } 210a0ffb263SMatthias Ringwald 21113839019SMatthias Ringwald void hfp_emit_event(btstack_packet_handler_t callback, uint8_t event_subtype, uint8_t value){ 2123deb3ec6SMatthias Ringwald if (!callback) return; 2133deb3ec6SMatthias Ringwald uint8_t event[4]; 2143deb3ec6SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 2153deb3ec6SMatthias Ringwald event[1] = sizeof(event) - 2; 2163deb3ec6SMatthias Ringwald event[2] = event_subtype; 2173deb3ec6SMatthias Ringwald event[3] = value; // status 0 == OK 21813839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 2193deb3ec6SMatthias Ringwald } 2203deb3ec6SMatthias Ringwald 221d0c4aea6SMilanka Ringwald void hfp_emit_slc_connection_event(btstack_packet_handler_t callback, uint8_t status, hci_con_handle_t con_handle, bd_addr_t addr){ 222a0653c3bSMilanka Ringwald if (!callback) return; 2238901a7c4SMatthias Ringwald uint8_t event[12]; 2246a7f44bdSMilanka Ringwald int pos = 0; 2256a7f44bdSMilanka Ringwald event[pos++] = HCI_EVENT_HFP_META; 2266a7f44bdSMilanka Ringwald event[pos++] = sizeof(event) - 2; 227d0c4aea6SMilanka Ringwald event[pos++] = HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 2286a7f44bdSMilanka Ringwald event[pos++] = status; // status 0 == OK 2296a7f44bdSMilanka Ringwald little_endian_store_16(event, pos, con_handle); 2306a7f44bdSMilanka Ringwald pos += 2; 2316a7f44bdSMilanka Ringwald reverse_bd_addr(addr,&event[pos]); 2326a7f44bdSMilanka Ringwald pos += 6; 23313839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 234a0653c3bSMilanka Ringwald } 235a0653c3bSMilanka Ringwald 236d0c4aea6SMilanka Ringwald static void hfp_emit_sco_event(btstack_packet_handler_t callback, uint8_t status, hci_con_handle_t con_handle, bd_addr_t addr, uint8_t negotiated_codec){ 237d0c4aea6SMilanka Ringwald if (!callback) return; 238d0c4aea6SMilanka Ringwald uint8_t event[13]; 239d0c4aea6SMilanka Ringwald int pos = 0; 240d0c4aea6SMilanka Ringwald event[pos++] = HCI_EVENT_HFP_META; 241d0c4aea6SMilanka Ringwald event[pos++] = sizeof(event) - 2; 242d0c4aea6SMilanka Ringwald event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED; 243d0c4aea6SMilanka Ringwald event[pos++] = status; // status 0 == OK 244d0c4aea6SMilanka Ringwald little_endian_store_16(event, pos, con_handle); 245d0c4aea6SMilanka Ringwald pos += 2; 246d0c4aea6SMilanka Ringwald reverse_bd_addr(addr,&event[pos]); 247d0c4aea6SMilanka Ringwald pos += 6; 248d0c4aea6SMilanka Ringwald event[pos++] = negotiated_codec; 249d0c4aea6SMilanka Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 250d0c4aea6SMilanka Ringwald } 251d0c4aea6SMilanka Ringwald 25213839019SMatthias Ringwald void hfp_emit_string_event(btstack_packet_handler_t callback, uint8_t event_subtype, const char * value){ 253aa4dd815SMatthias Ringwald if (!callback) return; 254c1797c7dSMatthias Ringwald uint8_t event[40]; 255aa4dd815SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 256aa4dd815SMatthias Ringwald event[1] = sizeof(event) - 2; 257aa4dd815SMatthias Ringwald event[2] = event_subtype; 2587c959318SMatthias Ringwald int size = (strlen(value) < sizeof(event) - 4) ? (int) strlen(value) : sizeof(event) - 4; 259aa4dd815SMatthias Ringwald strncpy((char*)&event[3], value, size); 260aa4dd815SMatthias Ringwald event[3 + size] = 0; 26113839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 262aa4dd815SMatthias Ringwald } 263aa4dd815SMatthias Ringwald 2640cb5b971SMatthias Ringwald btstack_linked_list_t * hfp_get_connections(void){ 2658f2a52f4SMatthias Ringwald return (btstack_linked_list_t *) &hfp_connections; 2663deb3ec6SMatthias Ringwald } 2673deb3ec6SMatthias Ringwald 2683deb3ec6SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid){ 269665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 270665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 271665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 272a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 273a0ffb263SMatthias Ringwald if (hfp_connection->rfcomm_cid == cid){ 274a0ffb263SMatthias Ringwald return hfp_connection; 2753deb3ec6SMatthias Ringwald } 2763deb3ec6SMatthias Ringwald } 2773deb3ec6SMatthias Ringwald return NULL; 2783deb3ec6SMatthias Ringwald } 2793deb3ec6SMatthias Ringwald 2803deb3ec6SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr){ 281665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 282665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 283665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 284a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 285a0ffb263SMatthias Ringwald if (memcmp(hfp_connection->remote_addr, bd_addr, 6) == 0) { 286a0ffb263SMatthias Ringwald return hfp_connection; 2873deb3ec6SMatthias Ringwald } 2883deb3ec6SMatthias Ringwald } 2893deb3ec6SMatthias Ringwald return NULL; 2903deb3ec6SMatthias Ringwald } 2913deb3ec6SMatthias Ringwald 292aa4dd815SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_sco_handle(uint16_t handle){ 293665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 294665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 295665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 296a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 297a0ffb263SMatthias Ringwald if (hfp_connection->sco_handle == handle){ 298a0ffb263SMatthias Ringwald return hfp_connection; 2993deb3ec6SMatthias Ringwald } 3003deb3ec6SMatthias Ringwald } 3013deb3ec6SMatthias Ringwald return NULL; 3023deb3ec6SMatthias Ringwald } 3033deb3ec6SMatthias Ringwald 304d97d752dSMilanka Ringwald hfp_connection_t * get_hfp_connection_context_for_acl_handle(uint16_t handle){ 305d97d752dSMilanka Ringwald btstack_linked_list_iterator_t it; 306d97d752dSMilanka Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 307d97d752dSMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 308d97d752dSMilanka Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 309d97d752dSMilanka Ringwald if (hfp_connection->acl_handle == handle){ 310d97d752dSMilanka Ringwald return hfp_connection; 311d97d752dSMilanka Ringwald } 312d97d752dSMilanka Ringwald } 313d97d752dSMilanka Ringwald return NULL; 314d97d752dSMilanka Ringwald } 315d97d752dSMilanka Ringwald 316a0ffb263SMatthias Ringwald void hfp_reset_context_flags(hfp_connection_t * hfp_connection){ 317a0ffb263SMatthias Ringwald if (!hfp_connection) return; 318a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 319a0ffb263SMatthias Ringwald hfp_connection->send_error = 0; 3203deb3ec6SMatthias Ringwald 321a0ffb263SMatthias Ringwald hfp_connection->keep_byte = 0; 3223deb3ec6SMatthias Ringwald 323a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 0; 324a0ffb263SMatthias Ringwald hfp_connection->operator_name_changed = 0; 3253deb3ec6SMatthias Ringwald 326a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = 0; 327a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 3283deb3ec6SMatthias Ringwald 329a0ffb263SMatthias Ringwald // establish codecs hfp_connection 330a0ffb263SMatthias Ringwald hfp_connection->suggested_codec = 0; 3317522e673SMatthias Ringwald hfp_connection->negotiated_codec = 0; 332a0ffb263SMatthias Ringwald hfp_connection->codec_confirmed = 0; 3333deb3ec6SMatthias Ringwald 334a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 335a0ffb263SMatthias Ringwald hfp_connection->call_waiting_notification_enabled = 0; 336a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 337a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 3383deb3ec6SMatthias Ringwald } 3393deb3ec6SMatthias Ringwald 340fffdd288SMatthias Ringwald static hfp_connection_t * create_hfp_connection_context(void){ 341a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = btstack_memory_hfp_connection_get(); 342a0ffb263SMatthias Ringwald if (!hfp_connection) return NULL; 3433deb3ec6SMatthias Ringwald // init state 344a0ffb263SMatthias Ringwald memset(hfp_connection,0, sizeof(hfp_connection_t)); 3453deb3ec6SMatthias Ringwald 346a0ffb263SMatthias Ringwald hfp_connection->state = HFP_IDLE; 347a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 348a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_IDLE; 349aa4dd815SMatthias Ringwald 350a0ffb263SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 351a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 3523deb3ec6SMatthias Ringwald 353a0ffb263SMatthias Ringwald hfp_reset_context_flags(hfp_connection); 3543deb3ec6SMatthias Ringwald 355d63c37a1SMatthias Ringwald btstack_linked_list_add(&hfp_connections, (btstack_linked_item_t*)hfp_connection); 356a0ffb263SMatthias Ringwald return hfp_connection; 3573deb3ec6SMatthias Ringwald } 3583deb3ec6SMatthias Ringwald 359a0ffb263SMatthias Ringwald static void remove_hfp_connection_context(hfp_connection_t * hfp_connection){ 360a0ffb263SMatthias Ringwald btstack_linked_list_remove(&hfp_connections, (btstack_linked_item_t*) hfp_connection); 3613deb3ec6SMatthias Ringwald } 3623deb3ec6SMatthias Ringwald 3633deb3ec6SMatthias Ringwald static hfp_connection_t * provide_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr){ 364a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 365a0ffb263SMatthias Ringwald if (hfp_connection) return hfp_connection; 366a0ffb263SMatthias Ringwald hfp_connection = create_hfp_connection_context(); 367a0ffb263SMatthias Ringwald memcpy(hfp_connection->remote_addr, bd_addr, 6); 368a0ffb263SMatthias Ringwald return hfp_connection; 3693deb3ec6SMatthias Ringwald } 3703deb3ec6SMatthias Ringwald 371aa4dd815SMatthias Ringwald /* @param network. 372aa4dd815SMatthias Ringwald * 0 == no ability to reject a call. 373aa4dd815SMatthias Ringwald * 1 == ability to reject a call. 374aa4dd815SMatthias Ringwald */ 3753deb3ec6SMatthias Ringwald 3763deb3ec6SMatthias Ringwald /* @param suported_features 3773deb3ec6SMatthias Ringwald * HF bit 0: EC and/or NR function (yes/no, 1 = yes, 0 = no) 3783deb3ec6SMatthias Ringwald * HF bit 1: Call waiting or three-way calling(yes/no, 1 = yes, 0 = no) 3793deb3ec6SMatthias Ringwald * HF bit 2: CLI presentation capability (yes/no, 1 = yes, 0 = no) 3803deb3ec6SMatthias Ringwald * HF bit 3: Voice recognition activation (yes/no, 1= yes, 0 = no) 3813deb3ec6SMatthias Ringwald * HF bit 4: Remote volume control (yes/no, 1 = yes, 0 = no) 3823deb3ec6SMatthias Ringwald * HF bit 5: Wide band speech (yes/no, 1 = yes, 0 = no) 3833deb3ec6SMatthias Ringwald */ 3843deb3ec6SMatthias Ringwald /* Bit position: 3853deb3ec6SMatthias Ringwald * AG bit 0: Three-way calling (yes/no, 1 = yes, 0 = no) 3863deb3ec6SMatthias Ringwald * AG bit 1: EC and/or NR function (yes/no, 1 = yes, 0 = no) 3873deb3ec6SMatthias Ringwald * AG bit 2: Voice recognition function (yes/no, 1 = yes, 0 = no) 3883deb3ec6SMatthias Ringwald * AG bit 3: In-band ring tone capability (yes/no, 1 = yes, 0 = no) 3893deb3ec6SMatthias Ringwald * AG bit 4: Attach a phone number to a voice tag (yes/no, 1 = yes, 0 = no) 3903deb3ec6SMatthias Ringwald * AG bit 5: Wide band speech (yes/no, 1 = yes, 0 = no) 3913deb3ec6SMatthias Ringwald */ 3923deb3ec6SMatthias Ringwald 3939b1c3b4dSMatthias 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){ 3943deb3ec6SMatthias Ringwald uint8_t* attribute; 3953deb3ec6SMatthias Ringwald de_create_sequence(service); 3963deb3ec6SMatthias Ringwald 3973deb3ec6SMatthias Ringwald // 0x0000 "Service Record Handle" 3983deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ServiceRecordHandle); 3999b1c3b4dSMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle); 4003deb3ec6SMatthias Ringwald 4013deb3ec6SMatthias Ringwald // 0x0001 "Service Class ID List" 4023deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ServiceClassIDList); 4033deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 4043deb3ec6SMatthias Ringwald { 4053deb3ec6SMatthias Ringwald // "UUID for Service" 4063deb3ec6SMatthias Ringwald de_add_number(attribute, DE_UUID, DE_SIZE_16, service_uuid); 4073deb3ec6SMatthias Ringwald de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_GenericAudio); 4083deb3ec6SMatthias Ringwald } 4093deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 4103deb3ec6SMatthias Ringwald 4113deb3ec6SMatthias Ringwald // 0x0004 "Protocol Descriptor List" 4123deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ProtocolDescriptorList); 4133deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 4143deb3ec6SMatthias Ringwald { 4153deb3ec6SMatthias Ringwald uint8_t* l2cpProtocol = de_push_sequence(attribute); 4163deb3ec6SMatthias Ringwald { 4173deb3ec6SMatthias Ringwald de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, SDP_L2CAPProtocol); 4183deb3ec6SMatthias Ringwald } 4193deb3ec6SMatthias Ringwald de_pop_sequence(attribute, l2cpProtocol); 4203deb3ec6SMatthias Ringwald 4213deb3ec6SMatthias Ringwald uint8_t* rfcomm = de_push_sequence(attribute); 4223deb3ec6SMatthias Ringwald { 4233deb3ec6SMatthias Ringwald de_add_number(rfcomm, DE_UUID, DE_SIZE_16, SDP_RFCOMMProtocol); // rfcomm_service 4243deb3ec6SMatthias Ringwald de_add_number(rfcomm, DE_UINT, DE_SIZE_8, rfcomm_channel_nr); // rfcomm channel 4253deb3ec6SMatthias Ringwald } 4263deb3ec6SMatthias Ringwald de_pop_sequence(attribute, rfcomm); 4273deb3ec6SMatthias Ringwald } 4283deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 4293deb3ec6SMatthias Ringwald 4303deb3ec6SMatthias Ringwald 4313deb3ec6SMatthias Ringwald // 0x0005 "Public Browse Group" 4323deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, SDP_BrowseGroupList); // public browse group 4333deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 4343deb3ec6SMatthias Ringwald { 4353deb3ec6SMatthias Ringwald de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_PublicBrowseGroup); 4363deb3ec6SMatthias Ringwald } 4373deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 4383deb3ec6SMatthias Ringwald 4393deb3ec6SMatthias Ringwald // 0x0009 "Bluetooth Profile Descriptor List" 4403deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, SDP_BluetoothProfileDescriptorList); 4413deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 4423deb3ec6SMatthias Ringwald { 4433deb3ec6SMatthias Ringwald uint8_t *sppProfile = de_push_sequence(attribute); 4443deb3ec6SMatthias Ringwald { 4453deb3ec6SMatthias Ringwald de_add_number(sppProfile, DE_UUID, DE_SIZE_16, SDP_Handsfree); 4463deb3ec6SMatthias Ringwald de_add_number(sppProfile, DE_UINT, DE_SIZE_16, 0x0107); // Verision 1.7 4473deb3ec6SMatthias Ringwald } 4483deb3ec6SMatthias Ringwald de_pop_sequence(attribute, sppProfile); 4493deb3ec6SMatthias Ringwald } 4503deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 4513deb3ec6SMatthias Ringwald 4523deb3ec6SMatthias Ringwald // 0x0100 "Service Name" 4533deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); 4543deb3ec6SMatthias Ringwald de_add_data(service, DE_STRING, strlen(name), (uint8_t *) name); 4553deb3ec6SMatthias Ringwald } 4563deb3ec6SMatthias Ringwald 4573deb3ec6SMatthias Ringwald static hfp_connection_t * connection_doing_sdp_query = NULL; 458a8747467SMatthias Ringwald 4596c927b22SMatthias Ringwald static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 460a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = connection_doing_sdp_query; 4613deb3ec6SMatthias Ringwald 462d63c37a1SMatthias Ringwald if ( hfp_connection->state != HFP_W4_SDP_QUERY_COMPLETE) return; 4633deb3ec6SMatthias Ringwald 4640e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 4655611a760SMatthias Ringwald case SDP_EVENT_QUERY_RFCOMM_SERVICE: 466a0ffb263SMatthias Ringwald if (!hfp_connection) { 46772b50801SMatthias Ringwald log_error("handle_query_rfcomm_event alloc connection for RFCOMM port %u failed", sdp_event_query_rfcomm_service_get_rfcomm_channel(packet)); 4683deb3ec6SMatthias Ringwald return; 4693deb3ec6SMatthias Ringwald } 470a0ffb263SMatthias Ringwald hfp_connection->rfcomm_channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet); 4713deb3ec6SMatthias Ringwald break; 4725611a760SMatthias Ringwald case SDP_EVENT_QUERY_COMPLETE: 4733deb3ec6SMatthias Ringwald connection_doing_sdp_query = NULL; 474a0ffb263SMatthias Ringwald if (hfp_connection->rfcomm_channel_nr > 0){ 475a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_CONNECTED; 476a0ffb263SMatthias 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); 477d68dcce1SMatthias Ringwald rfcomm_create_channel(rfcomm_packet_handler, hfp_connection->remote_addr, hfp_connection->rfcomm_channel_nr, NULL); 4783deb3ec6SMatthias Ringwald break; 4793deb3ec6SMatthias Ringwald } 48072b50801SMatthias Ringwald log_info("rfcomm service not found, status %u.", sdp_event_query_complete_get_status(packet)); 4813deb3ec6SMatthias Ringwald break; 4823deb3ec6SMatthias Ringwald default: 4833deb3ec6SMatthias Ringwald break; 4843deb3ec6SMatthias Ringwald } 4853deb3ec6SMatthias Ringwald } 4863deb3ec6SMatthias Ringwald 487eddcd308SMatthias Ringwald static void hfp_handle_failed_sco_connection(uint8_t status){ 488eddcd308SMatthias Ringwald 489eddcd308SMatthias Ringwald if (!sco_establishment_active){ 490eddcd308SMatthias Ringwald log_error("(e)SCO Connection failed but not started by us"); 491eddcd308SMatthias Ringwald return; 492eddcd308SMatthias Ringwald } 4936c5b2002SMatthias Ringwald log_error("(e)SCO Connection failed status 0x%02x", status); 494eddcd308SMatthias Ringwald 495eddcd308SMatthias Ringwald // invalid params / unspecified error 496eddcd308SMatthias Ringwald if (status != 0x11 && status != 0x1f) return; 497eddcd308SMatthias Ringwald 498eddcd308SMatthias Ringwald switch (sco_establishment_active->link_setting){ 499eddcd308SMatthias Ringwald case HFP_LINK_SETTINGS_D0: 500eddcd308SMatthias Ringwald return; // no other option left 501eddcd308SMatthias Ringwald case HFP_LINK_SETTINGS_D1: 502eddcd308SMatthias Ringwald sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D0; 503eddcd308SMatthias Ringwald break; 504eddcd308SMatthias Ringwald case HFP_LINK_SETTINGS_S1: 505eddcd308SMatthias Ringwald sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D1; 506eddcd308SMatthias Ringwald break; 507eddcd308SMatthias Ringwald case HFP_LINK_SETTINGS_S2: 508eddcd308SMatthias Ringwald sco_establishment_active->link_setting = HFP_LINK_SETTINGS_S1; 509eddcd308SMatthias Ringwald break; 5107522e673SMatthias Ringwald case HFP_LINK_SETTINGS_S3: 5117522e673SMatthias Ringwald sco_establishment_active->link_setting = HFP_LINK_SETTINGS_S2; 5127522e673SMatthias Ringwald break; 5137522e673SMatthias Ringwald case HFP_LINK_SETTINGS_S4: 514eddcd308SMatthias Ringwald sco_establishment_active->link_setting = HFP_LINK_SETTINGS_S3; 515eddcd308SMatthias Ringwald break; 5167522e673SMatthias Ringwald case HFP_LINK_SETTINGS_T1: 5177522e673SMatthias Ringwald log_info("T1 failed, fallback to CVSD - D1"); 5187522e673SMatthias Ringwald sco_establishment_active->negotiated_codec = HFP_CODEC_CVSD; 519*bc1b1537SMilanka Ringwald sco_establishment_active->sco_for_msbc_failed = 1; 520*bc1b1537SMilanka Ringwald sco_establishment_active->command = HFP_CMD_AG_SEND_COMMON_CODEC; 5217522e673SMatthias Ringwald sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D1; 5227522e673SMatthias Ringwald break; 5237522e673SMatthias Ringwald case HFP_LINK_SETTINGS_T2: 5247522e673SMatthias Ringwald sco_establishment_active->link_setting = HFP_LINK_SETTINGS_T1; 5257522e673SMatthias Ringwald break; 526eddcd308SMatthias Ringwald } 527eddcd308SMatthias Ringwald sco_establishment_active->establish_audio_connection = 1; 528eddcd308SMatthias Ringwald sco_establishment_active = 0; 529eddcd308SMatthias Ringwald } 530eddcd308SMatthias Ringwald 531eddcd308SMatthias Ringwald 532e30a6a47SMatthias Ringwald void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 5333deb3ec6SMatthias Ringwald bd_addr_t event_addr; 5343deb3ec6SMatthias Ringwald uint16_t rfcomm_cid, handle; 535a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = NULL; 536674515e8SMatthias Ringwald uint8_t status; 5373deb3ec6SMatthias Ringwald 538eddcd308SMatthias Ringwald log_info("AG packet_handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size); 539aa4dd815SMatthias Ringwald 5400e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 541011577abSMilanka Ringwald case HCI_EVENT_CONNECTION_REQUEST: 542011577abSMilanka Ringwald // printf("hfp HCI_EVENT_CONNECTION_REQUEST\n"); 5437522e673SMatthias Ringwald switch(hci_event_connection_request_get_link_type(packet)){ 5447522e673SMatthias Ringwald case 0: // SCO 5457522e673SMatthias Ringwald case 2: // eSCO 546011577abSMilanka Ringwald hci_event_connection_request_get_bd_addr(packet, event_addr); 5477522e673SMatthias Ringwald hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr); 548011577abSMilanka Ringwald if (!hfp_connection) break; 549b72c4a9eSMatthias Ringwald hfp_connection->hf_accept_sco = 1; 5507522e673SMatthias Ringwald default: 5517522e673SMatthias Ringwald break; 5527522e673SMatthias Ringwald } 553011577abSMilanka Ringwald break; 5543deb3ec6SMatthias Ringwald 5553deb3ec6SMatthias Ringwald case RFCOMM_EVENT_INCOMING_CONNECTION: 5563deb3ec6SMatthias Ringwald // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 557caa82391SMilanka Ringwald rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr); 558d63c37a1SMatthias Ringwald hfp_connection = provide_hfp_connection_context_for_bd_addr(event_addr); 559d63c37a1SMatthias Ringwald if (!hfp_connection || hfp_connection->state != HFP_IDLE) return; 5603deb3ec6SMatthias Ringwald 561caa82391SMilanka Ringwald hfp_connection->rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet); 562d63c37a1SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_CONNECTED; 563674515e8SMatthias Ringwald // printf("RFCOMM channel %u requested for %s\n", hfp_connection->rfcomm_cid, bd_addr_to_str(hfp_connection->remote_addr)); 564d63c37a1SMatthias Ringwald rfcomm_accept_connection(hfp_connection->rfcomm_cid); 5653deb3ec6SMatthias Ringwald break; 5663deb3ec6SMatthias Ringwald 567f8f6a918SMatthias Ringwald case RFCOMM_EVENT_CHANNEL_OPENED: 5683deb3ec6SMatthias Ringwald // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16) 569aa4dd815SMatthias Ringwald 570674515e8SMatthias Ringwald rfcomm_event_channel_opened_get_bd_addr(packet, event_addr); 571674515e8SMatthias Ringwald status = rfcomm_event_channel_opened_get_status(packet); 572674515e8SMatthias Ringwald 573d63c37a1SMatthias Ringwald hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr); 574d63c37a1SMatthias Ringwald if (!hfp_connection || hfp_connection->state != HFP_W4_RFCOMM_CONNECTED) return; 5753deb3ec6SMatthias Ringwald 576674515e8SMatthias Ringwald if (status) { 577d0c4aea6SMilanka Ringwald hfp_emit_slc_connection_event(hfp_callback, status, rfcomm_event_channel_opened_get_con_handle(packet), event_addr); 578d63c37a1SMatthias Ringwald remove_hfp_connection_context(hfp_connection); 5793deb3ec6SMatthias Ringwald } else { 580caa82391SMilanka Ringwald hfp_connection->acl_handle = rfcomm_event_channel_opened_get_con_handle(packet); 581caa82391SMilanka Ringwald hfp_connection->rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet); 5826a7f44bdSMilanka Ringwald bd_addr_copy(hfp_connection->remote_addr, event_addr); 583674515e8SMatthias Ringwald // uint16_t mtu = rfcomm_event_channel_opened_get_max_frame_size(packet); 584674515e8SMatthias 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); 5853deb3ec6SMatthias Ringwald 586d63c37a1SMatthias Ringwald switch (hfp_connection->state){ 5873deb3ec6SMatthias Ringwald case HFP_W4_RFCOMM_CONNECTED: 588d63c37a1SMatthias Ringwald hfp_connection->state = HFP_EXCHANGE_SUPPORTED_FEATURES; 5893deb3ec6SMatthias Ringwald break; 5903deb3ec6SMatthias Ringwald case HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN: 591d63c37a1SMatthias Ringwald hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM; 592674515e8SMatthias Ringwald // printf("Shutting down RFCOMM.\n"); 5933deb3ec6SMatthias Ringwald break; 5943deb3ec6SMatthias Ringwald default: 5953deb3ec6SMatthias Ringwald break; 5963deb3ec6SMatthias Ringwald } 597bb6944afSMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 5983deb3ec6SMatthias Ringwald } 5993deb3ec6SMatthias Ringwald break; 6003deb3ec6SMatthias Ringwald 601eddcd308SMatthias Ringwald case HCI_EVENT_COMMAND_STATUS: 602eddcd308SMatthias Ringwald if (hci_event_command_status_get_command_opcode(packet) == hci_setup_synchronous_connection.opcode) { 603be2a1a78SMatthias Ringwald status = hci_event_command_status_get_status(packet); 6046c5b2002SMatthias Ringwald if (status) { 605eddcd308SMatthias Ringwald hfp_handle_failed_sco_connection(hci_event_command_status_get_status(packet)); 606eddcd308SMatthias Ringwald } 6076c5b2002SMatthias Ringwald } 608eddcd308SMatthias Ringwald break; 609eddcd308SMatthias Ringwald 6103deb3ec6SMatthias Ringwald case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{ 611011577abSMilanka Ringwald hci_event_synchronous_connection_complete_get_bd_addr(packet, event_addr); 612011577abSMilanka Ringwald hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr); 613011577abSMilanka Ringwald if (!hfp_connection) { 614011577abSMilanka Ringwald log_error("HFP: connection does not exist for remote with addr %s.", bd_addr_to_str(event_addr)); 615011577abSMilanka Ringwald return; 616011577abSMilanka Ringwald } 617ce263fc8SMatthias Ringwald 618011577abSMilanka Ringwald status = hci_event_synchronous_connection_complete_get_status(packet); 619aa4dd815SMatthias Ringwald if (status != 0){ 620b72c4a9eSMatthias Ringwald hfp_connection->hf_accept_sco = 0; 621eddcd308SMatthias Ringwald hfp_handle_failed_sco_connection(status); 622aa4dd815SMatthias Ringwald break; 623aa4dd815SMatthias Ringwald } 624aa4dd815SMatthias Ringwald 625011577abSMilanka Ringwald uint16_t sco_handle = hci_event_synchronous_connection_complete_get_handle(packet); 626011577abSMilanka Ringwald uint8_t link_type = hci_event_synchronous_connection_complete_get_link_type(packet); 627011577abSMilanka Ringwald uint8_t transmission_interval = hci_event_synchronous_connection_complete_get_transmission_interval(packet); // measured in slots 628011577abSMilanka Ringwald uint8_t retransmission_interval = hci_event_synchronous_connection_complete_get_retransmission_interval(packet);// measured in slots 629011577abSMilanka Ringwald uint16_t rx_packet_length = hci_event_synchronous_connection_complete_get_rx_packet_length(packet); // measured in bytes 630011577abSMilanka Ringwald uint16_t tx_packet_length = hci_event_synchronous_connection_complete_get_tx_packet_length(packet); // measured in bytes 631011577abSMilanka Ringwald uint8_t air_mode = hci_event_synchronous_connection_complete_get_air_mode(packet); 6323deb3ec6SMatthias Ringwald 6333deb3ec6SMatthias Ringwald switch (link_type){ 6343deb3ec6SMatthias Ringwald case 0x00: 635aa4dd815SMatthias Ringwald log_info("SCO Connection established."); 6363deb3ec6SMatthias Ringwald if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval); 6373deb3ec6SMatthias Ringwald if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval); 6383deb3ec6SMatthias Ringwald if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length); 6393deb3ec6SMatthias Ringwald if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length); 6403deb3ec6SMatthias Ringwald break; 6413deb3ec6SMatthias Ringwald case 0x02: 642aa4dd815SMatthias Ringwald log_info("eSCO Connection established. \n"); 6433deb3ec6SMatthias Ringwald break; 6443deb3ec6SMatthias Ringwald default: 6453deb3ec6SMatthias Ringwald log_error("(e)SCO reserved link_type 0x%2x", link_type); 6463deb3ec6SMatthias Ringwald break; 6473deb3ec6SMatthias Ringwald } 6483deb3ec6SMatthias Ringwald log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, " 649aa4dd815SMatthias Ringwald " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)\n", sco_handle, 650aa4dd815SMatthias Ringwald bd_addr_to_str(event_addr), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length, air_mode); 6513deb3ec6SMatthias Ringwald 652d63c37a1SMatthias Ringwald hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr); 6533deb3ec6SMatthias Ringwald 654d63c37a1SMatthias Ringwald if (!hfp_connection) { 655d63c37a1SMatthias Ringwald log_error("SCO link created, hfp_connection for address %s not found.", bd_addr_to_str(event_addr)); 6563deb3ec6SMatthias Ringwald break; 6573deb3ec6SMatthias Ringwald } 6583deb3ec6SMatthias Ringwald 659d63c37a1SMatthias Ringwald if (hfp_connection->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){ 660aa4dd815SMatthias Ringwald log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN"); 661d63c37a1SMatthias Ringwald hfp_connection->state = HFP_W2_DISCONNECT_SCO; 662aa4dd815SMatthias Ringwald break; 663aa4dd815SMatthias Ringwald } 664d63c37a1SMatthias Ringwald hfp_connection->sco_handle = sco_handle; 665d63c37a1SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 666d63c37a1SMatthias Ringwald hfp_connection->state = HFP_AUDIO_CONNECTION_ESTABLISHED; 667d0c4aea6SMilanka Ringwald hfp_emit_sco_event(hfp_callback, packet[2], sco_handle, event_addr, hfp_connection->negotiated_codec); 6683deb3ec6SMatthias Ringwald break; 6693deb3ec6SMatthias Ringwald } 6703deb3ec6SMatthias Ringwald 6713deb3ec6SMatthias Ringwald case RFCOMM_EVENT_CHANNEL_CLOSED: 672f8fbdce0SMatthias Ringwald rfcomm_cid = little_endian_read_16(packet,2); 673d63c37a1SMatthias Ringwald hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid); 674d63c37a1SMatthias Ringwald if (!hfp_connection) break; 675d63c37a1SMatthias Ringwald if (hfp_connection->state == HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART){ 676d63c37a1SMatthias Ringwald hfp_connection->state = HFP_IDLE; 677d63c37a1SMatthias Ringwald hfp_establish_service_level_connection(hfp_connection->remote_addr, hfp_connection->service_uuid); 6783deb3ec6SMatthias Ringwald break; 6793deb3ec6SMatthias Ringwald } 6803deb3ec6SMatthias Ringwald 681f4000eebSMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0); 682d63c37a1SMatthias Ringwald remove_hfp_connection_context(hfp_connection); 6833deb3ec6SMatthias Ringwald break; 6843deb3ec6SMatthias Ringwald 6853deb3ec6SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 686f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet,3); 687d63c37a1SMatthias Ringwald hfp_connection = get_hfp_connection_context_for_sco_handle(handle); 688aa4dd815SMatthias Ringwald 689d63c37a1SMatthias Ringwald if (!hfp_connection) break; 690aa4dd815SMatthias Ringwald 691d63c37a1SMatthias Ringwald if (hfp_connection->state != HFP_W4_SCO_DISCONNECTED){ 692aa4dd815SMatthias Ringwald log_info("Received gap disconnect in wrong hfp state"); 693aa4dd815SMatthias Ringwald } 694d63c37a1SMatthias Ringwald log_info("Check SCO handle: incoming 0x%02x, hfp_connection 0x%02x\n", handle, hfp_connection->sco_handle); 695aa4dd815SMatthias Ringwald 696d63c37a1SMatthias Ringwald if (handle == hfp_connection->sco_handle){ 697aa4dd815SMatthias Ringwald log_info("SCO disconnected, w2 disconnect RFCOMM\n"); 698d63c37a1SMatthias Ringwald hfp_connection->sco_handle = 0; 699d63c37a1SMatthias Ringwald hfp_connection->release_audio_connection = 0; 700d63c37a1SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 701f4000eebSMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED, 0); 7023deb3ec6SMatthias Ringwald break; 7033deb3ec6SMatthias Ringwald } 7043deb3ec6SMatthias Ringwald break; 7053deb3ec6SMatthias Ringwald 706fc64f94aSMatthias Ringwald default: 7073deb3ec6SMatthias Ringwald break; 7083deb3ec6SMatthias Ringwald } 7093deb3ec6SMatthias Ringwald } 7103deb3ec6SMatthias Ringwald 711aa4dd815SMatthias Ringwald // translates command string into hfp_command_t CMD 712aa4dd815SMatthias Ringwald static hfp_command_t parse_command(const char * line_buffer, int isHandsFree){ 713aa4dd815SMatthias Ringwald int offset = isHandsFree ? 0 : 2; 7143deb3ec6SMatthias Ringwald 715ce263fc8SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_LIST_CURRENT_CALLS, strlen(HFP_LIST_CURRENT_CALLS)) == 0){ 716ce263fc8SMatthias Ringwald return HFP_CMD_LIST_CURRENT_CALLS; 717ce263fc8SMatthias Ringwald } 718ce263fc8SMatthias Ringwald 719ce263fc8SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_SUBSCRIBER_NUMBER_INFORMATION, strlen(HFP_SUBSCRIBER_NUMBER_INFORMATION)) == 0){ 720ce263fc8SMatthias Ringwald return HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION; 721ce263fc8SMatthias Ringwald } 722ce263fc8SMatthias Ringwald 723aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_PHONE_NUMBER_FOR_VOICE_TAG, strlen(HFP_PHONE_NUMBER_FOR_VOICE_TAG)) == 0){ 724ce263fc8SMatthias Ringwald if (isHandsFree) return HFP_CMD_AG_SENT_PHONE_NUMBER; 725aa4dd815SMatthias Ringwald return HFP_CMD_HF_REQUEST_PHONE_NUMBER; 7263deb3ec6SMatthias Ringwald } 7273deb3ec6SMatthias Ringwald 728aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_TRANSMIT_DTMF_CODES, strlen(HFP_TRANSMIT_DTMF_CODES)) == 0){ 729aa4dd815SMatthias Ringwald return HFP_CMD_TRANSMIT_DTMF_CODES; 7303deb3ec6SMatthias Ringwald } 7313deb3ec6SMatthias Ringwald 732aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_SET_MICROPHONE_GAIN, strlen(HFP_SET_MICROPHONE_GAIN)) == 0){ 733aa4dd815SMatthias Ringwald return HFP_CMD_SET_MICROPHONE_GAIN; 7343deb3ec6SMatthias Ringwald } 7353deb3ec6SMatthias Ringwald 736aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_SET_SPEAKER_GAIN, strlen(HFP_SET_SPEAKER_GAIN)) == 0){ 737aa4dd815SMatthias Ringwald return HFP_CMD_SET_SPEAKER_GAIN; 7383deb3ec6SMatthias Ringwald } 7393deb3ec6SMatthias Ringwald 740aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_ACTIVATE_VOICE_RECOGNITION, strlen(HFP_ACTIVATE_VOICE_RECOGNITION)) == 0){ 741aa4dd815SMatthias Ringwald if (isHandsFree) return HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION; 742aa4dd815SMatthias Ringwald return HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION; 7433deb3ec6SMatthias Ringwald } 7443deb3ec6SMatthias Ringwald 745aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_TURN_OFF_EC_AND_NR, strlen(HFP_TURN_OFF_EC_AND_NR)) == 0){ 746aa4dd815SMatthias Ringwald return HFP_CMD_TURN_OFF_EC_AND_NR; 7473deb3ec6SMatthias Ringwald } 7483deb3ec6SMatthias Ringwald 749aa4dd815SMatthias Ringwald if (strncmp(line_buffer, HFP_CALL_ANSWERED, strlen(HFP_CALL_ANSWERED)) == 0){ 750aa4dd815SMatthias Ringwald return HFP_CMD_CALL_ANSWERED; 7513deb3ec6SMatthias Ringwald } 7523deb3ec6SMatthias Ringwald 753aa4dd815SMatthias Ringwald if (strncmp(line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0){ 754aa4dd815SMatthias Ringwald return HFP_CMD_CALL_PHONE_NUMBER; 7553deb3ec6SMatthias Ringwald } 7563deb3ec6SMatthias Ringwald 757ce263fc8SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_REDIAL_LAST_NUMBER, strlen(HFP_REDIAL_LAST_NUMBER)) == 0){ 758aa4dd815SMatthias Ringwald return HFP_CMD_REDIAL_LAST_NUMBER; 7593deb3ec6SMatthias Ringwald } 7603deb3ec6SMatthias Ringwald 761aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_CHANGE_IN_BAND_RING_TONE_SETTING, strlen(HFP_CHANGE_IN_BAND_RING_TONE_SETTING)) == 0){ 762aa4dd815SMatthias Ringwald return HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING; 763aa4dd815SMatthias Ringwald } 764aa4dd815SMatthias Ringwald 765aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_HANG_UP_CALL, strlen(HFP_HANG_UP_CALL)) == 0){ 766aa4dd815SMatthias Ringwald return HFP_CMD_HANG_UP_CALL; 767aa4dd815SMatthias Ringwald } 768aa4dd815SMatthias Ringwald 769aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_ERROR, strlen(HFP_ERROR)) == 0){ 770aa4dd815SMatthias Ringwald return HFP_CMD_ERROR; 771aa4dd815SMatthias Ringwald } 772aa4dd815SMatthias Ringwald 773ce263fc8SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_RING, strlen(HFP_RING)) == 0){ 774ce263fc8SMatthias Ringwald return HFP_CMD_RING; 775ce263fc8SMatthias Ringwald } 776ce263fc8SMatthias Ringwald 777aa4dd815SMatthias Ringwald if (isHandsFree && strncmp(line_buffer+offset, HFP_OK, strlen(HFP_OK)) == 0){ 778aa4dd815SMatthias Ringwald return HFP_CMD_OK; 779aa4dd815SMatthias Ringwald } 780aa4dd815SMatthias Ringwald 781aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_SUPPORTED_FEATURES, strlen(HFP_SUPPORTED_FEATURES)) == 0){ 782aa4dd815SMatthias Ringwald return HFP_CMD_SUPPORTED_FEATURES; 783aa4dd815SMatthias Ringwald } 784aa4dd815SMatthias Ringwald 785ce263fc8SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_TRANSFER_HF_INDICATOR_STATUS, strlen(HFP_TRANSFER_HF_INDICATOR_STATUS)) == 0){ 786ce263fc8SMatthias Ringwald return HFP_CMD_HF_INDICATOR_STATUS; 787ce263fc8SMatthias Ringwald } 788ce263fc8SMatthias Ringwald 789ce263fc8SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_RESPONSE_AND_HOLD, strlen(HFP_RESPONSE_AND_HOLD)) == 0){ 790ce263fc8SMatthias Ringwald if (strncmp(line_buffer+strlen(HFP_RESPONSE_AND_HOLD)+offset, "?", 1) == 0){ 791ce263fc8SMatthias Ringwald return HFP_CMD_RESPONSE_AND_HOLD_QUERY; 792ce263fc8SMatthias Ringwald } 793ce263fc8SMatthias Ringwald if (strncmp(line_buffer+strlen(HFP_RESPONSE_AND_HOLD)+offset, "=", 1) == 0){ 794ce263fc8SMatthias Ringwald return HFP_CMD_RESPONSE_AND_HOLD_COMMAND; 795ce263fc8SMatthias Ringwald } 796667ec068SMatthias Ringwald return HFP_CMD_RESPONSE_AND_HOLD_STATUS; 797ce263fc8SMatthias Ringwald } 798ce263fc8SMatthias Ringwald 799aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_INDICATOR, strlen(HFP_INDICATOR)) == 0){ 800aa4dd815SMatthias Ringwald if (strncmp(line_buffer+strlen(HFP_INDICATOR)+offset, "?", 1) == 0){ 801aa4dd815SMatthias Ringwald return HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS; 802aa4dd815SMatthias Ringwald } 803aa4dd815SMatthias Ringwald 804aa4dd815SMatthias Ringwald if (strncmp(line_buffer+strlen(HFP_INDICATOR)+offset, "=?", 2) == 0){ 805aa4dd815SMatthias Ringwald return HFP_CMD_RETRIEVE_AG_INDICATORS; 806aa4dd815SMatthias Ringwald } 807aa4dd815SMatthias Ringwald } 808aa4dd815SMatthias Ringwald 809aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_AVAILABLE_CODECS, strlen(HFP_AVAILABLE_CODECS)) == 0){ 810aa4dd815SMatthias Ringwald return HFP_CMD_AVAILABLE_CODECS; 811aa4dd815SMatthias Ringwald } 812aa4dd815SMatthias Ringwald 813aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, strlen(HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS)) == 0){ 814aa4dd815SMatthias Ringwald return HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE; 815aa4dd815SMatthias Ringwald } 816aa4dd815SMatthias Ringwald 817aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_ENABLE_CLIP, strlen(HFP_ENABLE_CLIP)) == 0){ 818a0ffb263SMatthias Ringwald if (isHandsFree) return HFP_CMD_AG_SENT_CLIP_INFORMATION; 819aa4dd815SMatthias Ringwald return HFP_CMD_ENABLE_CLIP; 820aa4dd815SMatthias Ringwald } 821aa4dd815SMatthias Ringwald 822aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_ENABLE_CALL_WAITING_NOTIFICATION, strlen(HFP_ENABLE_CALL_WAITING_NOTIFICATION)) == 0){ 823a0ffb263SMatthias Ringwald if (isHandsFree) return HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE; 824aa4dd815SMatthias Ringwald return HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION; 825aa4dd815SMatthias Ringwald } 826aa4dd815SMatthias Ringwald 827aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, strlen(HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES)) == 0){ 828aa4dd815SMatthias Ringwald 829aa4dd815SMatthias Ringwald if (isHandsFree) return HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES; 830aa4dd815SMatthias Ringwald 831aa4dd815SMatthias Ringwald if (strncmp(line_buffer+strlen(HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES)+offset, "=?", 2) == 0){ 832aa4dd815SMatthias Ringwald return HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES; 833aa4dd815SMatthias Ringwald } 834667ec068SMatthias Ringwald if (strncmp(line_buffer+strlen(HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES)+offset, "=", 1) == 0){ 835aa4dd815SMatthias Ringwald return HFP_CMD_CALL_HOLD; 836aa4dd815SMatthias Ringwald } 837aa4dd815SMatthias Ringwald 838aa4dd815SMatthias Ringwald return HFP_CMD_UNKNOWN; 839aa4dd815SMatthias Ringwald } 840aa4dd815SMatthias Ringwald 841aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_GENERIC_STATUS_INDICATOR, strlen(HFP_GENERIC_STATUS_INDICATOR)) == 0){ 842667ec068SMatthias Ringwald if (isHandsFree) { 843667ec068SMatthias Ringwald return HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS; 844667ec068SMatthias Ringwald } 845aa4dd815SMatthias Ringwald if (strncmp(line_buffer+strlen(HFP_GENERIC_STATUS_INDICATOR)+offset, "=?", 2) == 0){ 846aa4dd815SMatthias Ringwald return HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS; 847aa4dd815SMatthias Ringwald } 848aa4dd815SMatthias Ringwald if (strncmp(line_buffer+strlen(HFP_GENERIC_STATUS_INDICATOR)+offset, "=", 1) == 0){ 849aa4dd815SMatthias Ringwald return HFP_CMD_LIST_GENERIC_STATUS_INDICATORS; 850aa4dd815SMatthias Ringwald } 851aa4dd815SMatthias Ringwald return HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE; 852aa4dd815SMatthias Ringwald } 853aa4dd815SMatthias Ringwald 854aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS, strlen(HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS)) == 0){ 855aa4dd815SMatthias Ringwald return HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE; 8563deb3ec6SMatthias Ringwald } 8573deb3ec6SMatthias Ringwald 8583deb3ec6SMatthias Ringwald 859aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_QUERY_OPERATOR_SELECTION, strlen(HFP_QUERY_OPERATOR_SELECTION)) == 0){ 860aa4dd815SMatthias Ringwald if (strncmp(line_buffer+strlen(HFP_QUERY_OPERATOR_SELECTION)+offset, "=", 1) == 0){ 861aa4dd815SMatthias Ringwald return HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT; 8623deb3ec6SMatthias Ringwald } 863aa4dd815SMatthias Ringwald return HFP_CMD_QUERY_OPERATOR_SELECTION_NAME; 8643deb3ec6SMatthias Ringwald } 8653deb3ec6SMatthias Ringwald 866aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_TRANSFER_AG_INDICATOR_STATUS, strlen(HFP_TRANSFER_AG_INDICATOR_STATUS)) == 0){ 867aa4dd815SMatthias Ringwald return HFP_CMD_TRANSFER_AG_INDICATOR_STATUS; 8683deb3ec6SMatthias Ringwald } 8693deb3ec6SMatthias Ringwald 870aa4dd815SMatthias Ringwald if (isHandsFree && strncmp(line_buffer+offset, HFP_EXTENDED_AUDIO_GATEWAY_ERROR, strlen(HFP_EXTENDED_AUDIO_GATEWAY_ERROR)) == 0){ 871aa4dd815SMatthias Ringwald return HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR; 8723deb3ec6SMatthias Ringwald } 8733deb3ec6SMatthias Ringwald 874aa4dd815SMatthias Ringwald if (!isHandsFree && strncmp(line_buffer+offset, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, strlen(HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR)) == 0){ 875aa4dd815SMatthias Ringwald return HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR; 8763deb3ec6SMatthias Ringwald } 8773deb3ec6SMatthias Ringwald 878aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_TRIGGER_CODEC_CONNECTION_SETUP, strlen(HFP_TRIGGER_CODEC_CONNECTION_SETUP)) == 0){ 879aa4dd815SMatthias Ringwald return HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP; 880aa4dd815SMatthias Ringwald } 881aa4dd815SMatthias Ringwald 882aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_CONFIRM_COMMON_CODEC, strlen(HFP_CONFIRM_COMMON_CODEC)) == 0){ 8833deb3ec6SMatthias Ringwald if (isHandsFree){ 884aa4dd815SMatthias Ringwald return HFP_CMD_AG_SUGGESTED_CODEC; 8853deb3ec6SMatthias Ringwald } else { 886aa4dd815SMatthias Ringwald return HFP_CMD_HF_CONFIRMED_CODEC; 8873deb3ec6SMatthias Ringwald } 8883deb3ec6SMatthias Ringwald } 8893deb3ec6SMatthias Ringwald 890aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, "AT+", 3) == 0){ 891667ec068SMatthias Ringwald log_info("process unknown HF command %s \n", line_buffer); 892aa4dd815SMatthias Ringwald return HFP_CMD_UNKNOWN; 8933deb3ec6SMatthias Ringwald } 8943deb3ec6SMatthias Ringwald 895aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, "+", 1) == 0){ 896667ec068SMatthias Ringwald log_info(" process unknown AG command %s \n", line_buffer); 897aa4dd815SMatthias Ringwald return HFP_CMD_UNKNOWN; 898aa4dd815SMatthias Ringwald } 8993deb3ec6SMatthias Ringwald 900aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, "NOP", 3) == 0){ 901aa4dd815SMatthias Ringwald return HFP_CMD_NONE; 902aa4dd815SMatthias Ringwald } 903aa4dd815SMatthias Ringwald 904aa4dd815SMatthias Ringwald return HFP_CMD_NONE; 9053deb3ec6SMatthias Ringwald } 9063deb3ec6SMatthias Ringwald 907a0ffb263SMatthias Ringwald static void hfp_parser_store_byte(hfp_connection_t * hfp_connection, uint8_t byte){ 908ce263fc8SMatthias Ringwald // printf("hfp_parser_store_byte %c at pos %u\n", (char) byte, context->line_size); 9093deb3ec6SMatthias Ringwald // TODO: add limit 910a0ffb263SMatthias Ringwald hfp_connection->line_buffer[hfp_connection->line_size++] = byte; 911a0ffb263SMatthias Ringwald hfp_connection->line_buffer[hfp_connection->line_size] = 0; 9123deb3ec6SMatthias Ringwald } 913a0ffb263SMatthias Ringwald static int hfp_parser_is_buffer_empty(hfp_connection_t * hfp_connection){ 914a0ffb263SMatthias Ringwald return hfp_connection->line_size == 0; 9153deb3ec6SMatthias Ringwald } 9163deb3ec6SMatthias Ringwald 9173deb3ec6SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){ 9183deb3ec6SMatthias Ringwald return byte == '\n' || byte == '\r'; 9193deb3ec6SMatthias Ringwald } 9203deb3ec6SMatthias Ringwald 9213deb3ec6SMatthias Ringwald static int hfp_parser_is_end_of_header(uint8_t byte){ 9223deb3ec6SMatthias Ringwald return hfp_parser_is_end_of_line(byte) || byte == ':' || byte == '?'; 9233deb3ec6SMatthias Ringwald } 9243deb3ec6SMatthias Ringwald 925a0ffb263SMatthias Ringwald static int hfp_parser_found_separator(hfp_connection_t * hfp_connection, uint8_t byte){ 926a0ffb263SMatthias Ringwald if (hfp_connection->keep_byte == 1) return 1; 9273deb3ec6SMatthias Ringwald 9283deb3ec6SMatthias Ringwald int found_separator = byte == ',' || byte == '\n'|| byte == '\r'|| 9293deb3ec6SMatthias Ringwald byte == ')' || byte == '(' || byte == ':' || 9303deb3ec6SMatthias Ringwald byte == '-' || byte == '"' || byte == '?'|| byte == '='; 9313deb3ec6SMatthias Ringwald return found_separator; 9323deb3ec6SMatthias Ringwald } 9333deb3ec6SMatthias Ringwald 934a0ffb263SMatthias Ringwald static void hfp_parser_next_state(hfp_connection_t * hfp_connection, uint8_t byte){ 935a0ffb263SMatthias Ringwald hfp_connection->line_size = 0; 9363deb3ec6SMatthias Ringwald if (hfp_parser_is_end_of_line(byte)){ 937a0ffb263SMatthias Ringwald hfp_connection->parser_item_index = 0; 938a0ffb263SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 9393deb3ec6SMatthias Ringwald return; 9403deb3ec6SMatthias Ringwald } 941a0ffb263SMatthias Ringwald switch (hfp_connection->parser_state){ 9423deb3ec6SMatthias Ringwald case HFP_PARSER_CMD_HEADER: 943a0ffb263SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE; 944a0ffb263SMatthias Ringwald if (hfp_connection->keep_byte == 1){ 945a0ffb263SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 946a0ffb263SMatthias Ringwald hfp_connection->keep_byte = 0; 9473deb3ec6SMatthias Ringwald } 9483deb3ec6SMatthias Ringwald break; 9493deb3ec6SMatthias Ringwald case HFP_PARSER_CMD_SEQUENCE: 950a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 951ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 952a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 953a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 9543deb3ec6SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 955aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 956aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 957aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 958aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 959ce263fc8SMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 960a0ffb263SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_SECOND_ITEM; 9613deb3ec6SMatthias Ringwald break; 9623deb3ec6SMatthias Ringwald default: 9633deb3ec6SMatthias Ringwald break; 9643deb3ec6SMatthias Ringwald } 9653deb3ec6SMatthias Ringwald break; 9663deb3ec6SMatthias Ringwald case HFP_PARSER_SECOND_ITEM: 967a0ffb263SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_THIRD_ITEM; 9683deb3ec6SMatthias Ringwald break; 9693deb3ec6SMatthias Ringwald case HFP_PARSER_THIRD_ITEM: 970a0ffb263SMatthias Ringwald if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS){ 971a0ffb263SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE; 9723deb3ec6SMatthias Ringwald break; 9733deb3ec6SMatthias Ringwald } 974a0ffb263SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 9753deb3ec6SMatthias Ringwald break; 9763deb3ec6SMatthias Ringwald } 9773deb3ec6SMatthias Ringwald } 9783deb3ec6SMatthias Ringwald 979a0ffb263SMatthias Ringwald void hfp_parse(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){ 980aa4dd815SMatthias Ringwald // handle ATD<dial_string>; 981a0ffb263SMatthias Ringwald if (strncmp((const char*)hfp_connection->line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0){ 982aa4dd815SMatthias Ringwald // check for end-of-line or ';' 983aa4dd815SMatthias Ringwald if (byte == ';' || hfp_parser_is_end_of_line(byte)){ 984a0ffb263SMatthias Ringwald hfp_connection->line_buffer[hfp_connection->line_size] = 0; 985a0ffb263SMatthias Ringwald hfp_connection->line_size = 0; 986a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_CALL_PHONE_NUMBER; 987aa4dd815SMatthias Ringwald } else { 988a0ffb263SMatthias Ringwald hfp_connection->line_buffer[hfp_connection->line_size++] = byte; 989aa4dd815SMatthias Ringwald } 990aa4dd815SMatthias Ringwald return; 991aa4dd815SMatthias Ringwald } 992aa4dd815SMatthias Ringwald 9933deb3ec6SMatthias Ringwald // TODO: handle space inside word 994a0ffb263SMatthias Ringwald if (byte == ' ' && hfp_connection->parser_state > HFP_PARSER_CMD_HEADER) return; 9953deb3ec6SMatthias Ringwald 996a0ffb263SMatthias Ringwald if (byte == ',' && hfp_connection->command == HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE){ 997a0ffb263SMatthias Ringwald if (hfp_connection->line_size == 0){ 998a0ffb263SMatthias Ringwald hfp_connection->line_buffer[0] = 0; 999a0ffb263SMatthias Ringwald hfp_connection->ignore_value = 1; 1000a0ffb263SMatthias Ringwald parse_sequence(hfp_connection); 1001ce263fc8SMatthias Ringwald return; 1002ce263fc8SMatthias Ringwald } 1003ce263fc8SMatthias Ringwald } 1004ce263fc8SMatthias Ringwald 1005a0ffb263SMatthias Ringwald if (!hfp_parser_found_separator(hfp_connection, byte)){ 1006a0ffb263SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 10073deb3ec6SMatthias Ringwald return; 10083deb3ec6SMatthias Ringwald } 1009ce263fc8SMatthias Ringwald 10103deb3ec6SMatthias Ringwald if (hfp_parser_is_end_of_line(byte)) { 1011a0ffb263SMatthias Ringwald if (hfp_parser_is_buffer_empty(hfp_connection)){ 1012a0ffb263SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 10133deb3ec6SMatthias Ringwald } 10143deb3ec6SMatthias Ringwald } 1015a0ffb263SMatthias Ringwald if (hfp_parser_is_buffer_empty(hfp_connection)) return; 10163deb3ec6SMatthias Ringwald 1017a0ffb263SMatthias Ringwald switch (hfp_connection->parser_state){ 10183deb3ec6SMatthias Ringwald case HFP_PARSER_CMD_HEADER: // header 10193deb3ec6SMatthias Ringwald if (byte == '='){ 1020a0ffb263SMatthias Ringwald hfp_connection->keep_byte = 1; 1021a0ffb263SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 10223deb3ec6SMatthias Ringwald return; 10233deb3ec6SMatthias Ringwald } 10243deb3ec6SMatthias Ringwald 10253deb3ec6SMatthias Ringwald if (byte == '?'){ 1026a0ffb263SMatthias Ringwald hfp_connection->keep_byte = 0; 1027a0ffb263SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 10283deb3ec6SMatthias Ringwald return; 10293deb3ec6SMatthias Ringwald } 1030ce263fc8SMatthias Ringwald 1031ce263fc8SMatthias Ringwald if (byte == ','){ 1032a0ffb263SMatthias Ringwald hfp_connection->resolve_byte = 1; 1033ce263fc8SMatthias Ringwald } 1034ce263fc8SMatthias Ringwald 1035a0ffb263SMatthias Ringwald // printf(" parse header 2 %s, keep separator $ %d\n", hfp_connection->line_buffer, hfp_connection->keep_byte); 1036a0ffb263SMatthias Ringwald if (hfp_parser_is_end_of_header(byte) || hfp_connection->keep_byte == 1){ 1037a0ffb263SMatthias Ringwald // printf(" parse header 3 %s, keep separator $ %d\n", hfp_connection->line_buffer, hfp_connection->keep_byte); 1038a0ffb263SMatthias Ringwald char * line_buffer = (char *)hfp_connection->line_buffer; 1039a0ffb263SMatthias Ringwald hfp_connection->command = parse_command(line_buffer, isHandsFree); 1040aa4dd815SMatthias Ringwald 1041a0ffb263SMatthias Ringwald /* resolve command name according to hfp_connection */ 1042a0ffb263SMatthias Ringwald if (hfp_connection->command == HFP_CMD_UNKNOWN){ 1043a0ffb263SMatthias Ringwald switch(hfp_connection->state){ 1044aa4dd815SMatthias Ringwald case HFP_W4_LIST_GENERIC_STATUS_INDICATORS: 1045a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_LIST_GENERIC_STATUS_INDICATORS; 1046aa4dd815SMatthias Ringwald break; 1047aa4dd815SMatthias Ringwald case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS: 1048a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS; 1049aa4dd815SMatthias Ringwald break; 1050aa4dd815SMatthias Ringwald case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 1051a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE; 1052aa4dd815SMatthias Ringwald break; 1053aa4dd815SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS_STATUS: 1054a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS; 1055aa4dd815SMatthias Ringwald break; 1056aa4dd815SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS: 1057a0ffb263SMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0; 1058a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS; 1059aa4dd815SMatthias Ringwald break; 1060aa4dd815SMatthias Ringwald default: 1061aa4dd815SMatthias Ringwald break; 1062aa4dd815SMatthias Ringwald } 1063aa4dd815SMatthias Ringwald } 10643deb3ec6SMatthias Ringwald } 10653deb3ec6SMatthias Ringwald break; 10663deb3ec6SMatthias Ringwald 1067ce263fc8SMatthias Ringwald case HFP_PARSER_CMD_SEQUENCE: 1068a0ffb263SMatthias Ringwald parse_sequence(hfp_connection); 1069ce263fc8SMatthias Ringwald break; 1070ce263fc8SMatthias Ringwald case HFP_PARSER_SECOND_ITEM: 1071a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1072ce263fc8SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1073a0ffb263SMatthias Ringwald log_info("format %s, ", hfp_connection->line_buffer); 1074a0ffb263SMatthias Ringwald hfp_connection->network_operator.format = atoi((char *)&hfp_connection->line_buffer[0]); 1075ce263fc8SMatthias Ringwald break; 1076ce263fc8SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 1077a0ffb263SMatthias Ringwald log_info("format %s \n", hfp_connection->line_buffer); 1078a0ffb263SMatthias Ringwald hfp_connection->network_operator.format = atoi((char *)&hfp_connection->line_buffer[0]); 1079ce263fc8SMatthias Ringwald break; 1080ce263fc8SMatthias Ringwald case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 1081ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 1082ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 1083a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].state = (uint8_t)atoi((char*)hfp_connection->line_buffer); 1084ce263fc8SMatthias Ringwald break; 1085ce263fc8SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 1086a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = (uint8_t)atoi((char*)hfp_connection->line_buffer); 1087a0ffb263SMatthias Ringwald log_info("%d \n", hfp_connection->ag_indicators[hfp_connection->parser_item_index].status); 1088a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].status_changed = 1; 1089ce263fc8SMatthias Ringwald break; 1090ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 1091a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].min_range = atoi((char *)hfp_connection->line_buffer); 1092a0ffb263SMatthias Ringwald log_info("%s, ", hfp_connection->line_buffer); 1093ce263fc8SMatthias Ringwald break; 1094ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 1095a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1096a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1097a0ffb263SMatthias Ringwald hfp_connection->bnip_type = (uint8_t)atoi((char*)hfp_connection->line_buffer); 1098ce263fc8SMatthias Ringwald break; 1099ce263fc8SMatthias Ringwald default: 1100ce263fc8SMatthias Ringwald break; 1101ce263fc8SMatthias Ringwald } 1102ce263fc8SMatthias Ringwald break; 1103ce263fc8SMatthias Ringwald 1104ce263fc8SMatthias Ringwald case HFP_PARSER_THIRD_ITEM: 1105a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1106ce263fc8SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1107a0ffb263SMatthias Ringwald strcpy(hfp_connection->network_operator.name, (char *)hfp_connection->line_buffer); 1108a0ffb263SMatthias Ringwald log_info("name %s\n", hfp_connection->line_buffer); 1109ce263fc8SMatthias Ringwald break; 1110ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 1111a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].max_range = atoi((char *)hfp_connection->line_buffer); 1112a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1113a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr = hfp_connection->parser_item_index; 1114a0ffb263SMatthias Ringwald log_info("%s)\n", hfp_connection->line_buffer); 1115ce263fc8SMatthias Ringwald break; 1116ce263fc8SMatthias Ringwald default: 1117ce263fc8SMatthias Ringwald break; 1118ce263fc8SMatthias Ringwald } 1119ce263fc8SMatthias Ringwald break; 1120ce263fc8SMatthias Ringwald } 1121a0ffb263SMatthias Ringwald hfp_parser_next_state(hfp_connection, byte); 1122ce263fc8SMatthias Ringwald 1123a0ffb263SMatthias Ringwald if (hfp_connection->resolve_byte && hfp_connection->command == HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE){ 1124a0ffb263SMatthias Ringwald hfp_connection->resolve_byte = 0; 1125a0ffb263SMatthias Ringwald hfp_connection->ignore_value = 1; 1126a0ffb263SMatthias Ringwald parse_sequence(hfp_connection); 1127a0ffb263SMatthias Ringwald hfp_connection->line_buffer[0] = 0; 1128a0ffb263SMatthias Ringwald hfp_connection->line_size = 0; 1129ce263fc8SMatthias Ringwald } 1130ce263fc8SMatthias Ringwald } 1131ce263fc8SMatthias Ringwald 1132a0ffb263SMatthias Ringwald static void parse_sequence(hfp_connection_t * hfp_connection){ 1133ce263fc8SMatthias Ringwald int value; 1134a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1135667ec068SMatthias Ringwald case HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS: 1136a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1137667ec068SMatthias Ringwald int i; 1138a0ffb263SMatthias Ringwald switch (hfp_connection->parser_item_index){ 1139667ec068SMatthias Ringwald case 0: 1140a0ffb263SMatthias Ringwald for (i=0;i<hfp_connection->generic_status_indicators_nr;i++){ 1141a0ffb263SMatthias Ringwald if (hfp_connection->generic_status_indicators[i].uuid == value){ 1142a0ffb263SMatthias Ringwald hfp_connection->parser_indicator_index = i; 1143667ec068SMatthias Ringwald break; 1144667ec068SMatthias Ringwald } 1145667ec068SMatthias Ringwald } 1146667ec068SMatthias Ringwald break; 1147667ec068SMatthias Ringwald case 1: 1148a0ffb263SMatthias Ringwald if (hfp_connection->parser_indicator_index <0) break; 1149a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[hfp_connection->parser_indicator_index].state = value; 1150667ec068SMatthias Ringwald log_info("HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS set indicator at index %u, to %u\n", 1151a0ffb263SMatthias Ringwald hfp_connection->parser_item_index, value); 1152667ec068SMatthias Ringwald break; 1153667ec068SMatthias Ringwald default: 1154667ec068SMatthias Ringwald break; 1155667ec068SMatthias Ringwald } 1156a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1157667ec068SMatthias Ringwald break; 1158667ec068SMatthias Ringwald 1159667ec068SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1160a0ffb263SMatthias Ringwald switch(hfp_connection->parser_item_index){ 1161667ec068SMatthias Ringwald case 0: 1162a0ffb263SMatthias Ringwald strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number)); 1163a0ffb263SMatthias Ringwald hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0; 1164667ec068SMatthias Ringwald break; 1165667ec068SMatthias Ringwald case 1: 1166a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1167a0ffb263SMatthias Ringwald hfp_connection->bnip_type = value; 1168667ec068SMatthias Ringwald break; 1169667ec068SMatthias Ringwald default: 1170667ec068SMatthias Ringwald break; 1171667ec068SMatthias Ringwald } 1172a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1173667ec068SMatthias Ringwald break; 1174667ec068SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 1175a0ffb263SMatthias Ringwald switch(hfp_connection->parser_item_index){ 1176667ec068SMatthias Ringwald case 0: 1177a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1178a0ffb263SMatthias Ringwald hfp_connection->clcc_idx = value; 1179667ec068SMatthias Ringwald break; 1180667ec068SMatthias Ringwald case 1: 1181a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1182a0ffb263SMatthias Ringwald hfp_connection->clcc_dir = value; 1183667ec068SMatthias Ringwald break; 1184667ec068SMatthias Ringwald case 2: 1185a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1186a0ffb263SMatthias Ringwald hfp_connection->clcc_status = value; 1187667ec068SMatthias Ringwald break; 1188667ec068SMatthias Ringwald case 3: 1189a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1190a0ffb263SMatthias Ringwald hfp_connection->clcc_mpty = value; 1191667ec068SMatthias Ringwald break; 1192667ec068SMatthias Ringwald case 4: 1193a0ffb263SMatthias Ringwald strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number)); 1194a0ffb263SMatthias Ringwald hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0; 1195667ec068SMatthias Ringwald break; 1196667ec068SMatthias Ringwald case 5: 1197a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1198a0ffb263SMatthias Ringwald hfp_connection->bnip_type = value; 1199667ec068SMatthias Ringwald break; 1200667ec068SMatthias Ringwald default: 1201667ec068SMatthias Ringwald break; 1202667ec068SMatthias Ringwald } 1203a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1204667ec068SMatthias Ringwald break; 1205aa4dd815SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 1206a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1207a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = value; 1208aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CMD_SET_MICROPHONE_GAIN %d\n", value); 1209aa4dd815SMatthias Ringwald break; 1210aa4dd815SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 1211a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1212a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = value; 1213aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CMD_SET_SPEAKER_GAIN %d\n", value); 1214aa4dd815SMatthias Ringwald break; 1215aa4dd815SMatthias Ringwald case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION: 1216a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1217a0ffb263SMatthias Ringwald hfp_connection->ag_activate_voice_recognition = value; 1218aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION %d\n", value); 1219aa4dd815SMatthias Ringwald break; 1220aa4dd815SMatthias Ringwald case HFP_CMD_TURN_OFF_EC_AND_NR: 1221a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1222a0ffb263SMatthias Ringwald hfp_connection->ag_echo_and_noise_reduction = value; 1223aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CMD_TURN_OFF_EC_AND_NR %d\n", value); 1224aa4dd815SMatthias Ringwald break; 1225aa4dd815SMatthias Ringwald case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING: 1226a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1227a0ffb263SMatthias Ringwald hfp_connection->remote_supported_features = store_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE, value); 1228aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CHANGE_IN_BAND_RING_TONE_SETTING %d\n", value); 1229aa4dd815SMatthias Ringwald break; 12303deb3ec6SMatthias Ringwald case HFP_CMD_HF_CONFIRMED_CODEC: 1231a0ffb263SMatthias Ringwald hfp_connection->codec_confirmed = atoi((char*)hfp_connection->line_buffer); 1232a0ffb263SMatthias Ringwald log_info("hfp parse HFP_CMD_HF_CONFIRMED_CODEC %d\n", hfp_connection->codec_confirmed); 12333deb3ec6SMatthias Ringwald break; 12343deb3ec6SMatthias Ringwald case HFP_CMD_AG_SUGGESTED_CODEC: 1235a0ffb263SMatthias Ringwald hfp_connection->suggested_codec = atoi((char*)hfp_connection->line_buffer); 1236a0ffb263SMatthias Ringwald log_info("hfp parse HFP_CMD_AG_SUGGESTED_CODEC %d\n", hfp_connection->suggested_codec); 12373deb3ec6SMatthias Ringwald break; 12383deb3ec6SMatthias Ringwald case HFP_CMD_SUPPORTED_FEATURES: 1239a0ffb263SMatthias Ringwald hfp_connection->remote_supported_features = atoi((char*)hfp_connection->line_buffer); 1240a0ffb263SMatthias Ringwald log_info("Parsed supported feature %d\n", hfp_connection->remote_supported_features); 12413deb3ec6SMatthias Ringwald break; 12423deb3ec6SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 1243a0ffb263SMatthias Ringwald log_info("Parsed codec %s\n", hfp_connection->line_buffer); 1244a0ffb263SMatthias Ringwald hfp_connection->remote_codecs[hfp_connection->parser_item_index] = (uint16_t)atoi((char*)hfp_connection->line_buffer); 1245a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1246a0ffb263SMatthias Ringwald hfp_connection->remote_codecs_nr = hfp_connection->parser_item_index; 12473deb3ec6SMatthias Ringwald break; 1248aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 1249a0ffb263SMatthias Ringwald strcpy((char *)hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, (char *)hfp_connection->line_buffer); 1250a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].index = hfp_connection->parser_item_index+1; 1251a0ffb263SMatthias Ringwald log_info("Indicator %d: %s (", hfp_connection->ag_indicators_nr+1, hfp_connection->line_buffer); 1252aa4dd815SMatthias Ringwald break; 1253aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 1254a0ffb263SMatthias Ringwald log_info("Parsed Indicator %d with status: %s\n", hfp_connection->parser_item_index+1, hfp_connection->line_buffer); 1255a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = atoi((char *) hfp_connection->line_buffer); 1256a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 12573deb3ec6SMatthias Ringwald break; 12583deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 1259a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1260a0ffb263SMatthias Ringwald if (hfp_connection->parser_item_index != 4) break; 1261a0ffb263SMatthias Ringwald log_info("Parsed Enable indicators: %s\n", hfp_connection->line_buffer); 1262a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1263a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = (uint8_t) value; 12643deb3ec6SMatthias Ringwald break; 12653deb3ec6SMatthias Ringwald case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES: 1266a0ffb263SMatthias Ringwald log_info("Parsed Support call hold: %s\n", hfp_connection->line_buffer); 1267a0ffb263SMatthias Ringwald if (hfp_connection->line_size > 2 ) break; 1268a0ffb263SMatthias Ringwald strcpy((char *)hfp_connection->remote_call_services[hfp_connection->remote_call_services_nr].name, (char *)hfp_connection->line_buffer); 1269a0ffb263SMatthias Ringwald hfp_connection->remote_call_services_nr++; 12703deb3ec6SMatthias Ringwald break; 1271aa4dd815SMatthias Ringwald case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 1272aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 1273a0ffb263SMatthias Ringwald log_info("Parsed Generic status indicator: %s\n", hfp_connection->line_buffer); 1274a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].uuid = (uint16_t)atoi((char*)hfp_connection->line_buffer); 1275a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1276a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators_nr = hfp_connection->parser_item_index; 12773deb3ec6SMatthias Ringwald break; 1278aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 12793deb3ec6SMatthias Ringwald // HF parses inital AG gen. ind. state 1280a0ffb263SMatthias Ringwald log_info("Parsed List generic status indicator %s state: ", hfp_connection->line_buffer); 1281a0ffb263SMatthias Ringwald hfp_connection->parser_item_index = (uint8_t)atoi((char*)hfp_connection->line_buffer); 12823deb3ec6SMatthias Ringwald break; 1283ce263fc8SMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 1284a0ffb263SMatthias Ringwald hfp_connection->parser_indicator_index = (uint8_t)atoi((char*)hfp_connection->line_buffer); 1285a0ffb263SMatthias Ringwald log_info("Parsed HF indicator index %u", hfp_connection->parser_indicator_index); 1286ce263fc8SMatthias Ringwald break; 12873deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE: 12883deb3ec6SMatthias Ringwald // AG parses new gen. ind. state 1289a0ffb263SMatthias Ringwald if (hfp_connection->ignore_value){ 1290a0ffb263SMatthias Ringwald hfp_connection->ignore_value = 0; 1291a0ffb263SMatthias Ringwald log_info("Parsed Enable AG indicator pos %u('%s') - unchanged (stays %u)\n", hfp_connection->parser_item_index, 1292a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled); 1293ce263fc8SMatthias Ringwald } 1294a0ffb263SMatthias Ringwald else if (hfp_connection->ag_indicators[hfp_connection->parser_item_index].mandatory){ 1295ce263fc8SMatthias Ringwald log_info("Parsed Enable AG indicator pos %u('%s') - ignore (mandatory)\n", 1296a0ffb263SMatthias Ringwald hfp_connection->parser_item_index, hfp_connection->ag_indicators[hfp_connection->parser_item_index].name); 1297ce263fc8SMatthias Ringwald } else { 1298a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1299a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled = value; 1300a0ffb263SMatthias Ringwald log_info("Parsed Enable AG indicator pos %u('%s'): %u\n", hfp_connection->parser_item_index, 1301a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, value); 13023deb3ec6SMatthias Ringwald } 1303a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 13043deb3ec6SMatthias Ringwald break; 13053deb3ec6SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 13063deb3ec6SMatthias Ringwald // indicators are indexed starting with 1 1307a0ffb263SMatthias Ringwald hfp_connection->parser_item_index = atoi((char *)&hfp_connection->line_buffer[0]) - 1; 1308a0ffb263SMatthias Ringwald log_info("Parsed status of the AG indicator %d, status ", hfp_connection->parser_item_index); 13093deb3ec6SMatthias Ringwald break; 1310aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1311a0ffb263SMatthias Ringwald hfp_connection->network_operator.mode = atoi((char *)&hfp_connection->line_buffer[0]); 1312a0ffb263SMatthias Ringwald log_info("Parsed network operator mode: %d, ", hfp_connection->network_operator.mode); 1313aa4dd815SMatthias Ringwald break; 1314aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 1315a0ffb263SMatthias Ringwald if (hfp_connection->line_buffer[0] == '3'){ 1316a0ffb263SMatthias Ringwald log_info("Parsed Set network operator format : %s, ", hfp_connection->line_buffer); 13173deb3ec6SMatthias Ringwald break; 13183deb3ec6SMatthias Ringwald } 13193deb3ec6SMatthias Ringwald // TODO emit ERROR, wrong format 1320a0ffb263SMatthias Ringwald log_info("ERROR Set network operator format: index %s not supported\n", hfp_connection->line_buffer); 13213deb3ec6SMatthias Ringwald break; 13223deb3ec6SMatthias Ringwald case HFP_CMD_ERROR: 13233deb3ec6SMatthias Ringwald break; 13243deb3ec6SMatthias Ringwald case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1325a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 1; 1326a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error_value = (uint8_t)atoi((char*)hfp_connection->line_buffer); 13273deb3ec6SMatthias Ringwald break; 13283deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR: 1329a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = (uint8_t)atoi((char*)hfp_connection->line_buffer); 1330a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1331a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 13323deb3ec6SMatthias Ringwald break; 1333ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 1334a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1335a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1336a0ffb263SMatthias Ringwald strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number)); 1337a0ffb263SMatthias Ringwald hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0; 13383deb3ec6SMatthias Ringwald break; 13393deb3ec6SMatthias Ringwald default: 13403deb3ec6SMatthias Ringwald break; 13413deb3ec6SMatthias Ringwald } 13423deb3ec6SMatthias Ringwald } 13433deb3ec6SMatthias Ringwald 13443deb3ec6SMatthias Ringwald void hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid){ 1345a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = provide_hfp_connection_context_for_bd_addr(bd_addr); 1346a0ffb263SMatthias Ringwald log_info("hfp_connect %s, hfp_connection %p", bd_addr_to_str(bd_addr), hfp_connection); 13473deb3ec6SMatthias Ringwald 1348a0ffb263SMatthias Ringwald if (!hfp_connection) { 13493deb3ec6SMatthias Ringwald log_error("hfp_establish_service_level_connection for addr %s failed", bd_addr_to_str(bd_addr)); 13503deb3ec6SMatthias Ringwald return; 13513deb3ec6SMatthias Ringwald } 1352aa4dd815SMatthias Ringwald 1353a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 13543deb3ec6SMatthias Ringwald case HFP_W2_DISCONNECT_RFCOMM: 1355a0ffb263SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 13563deb3ec6SMatthias Ringwald return; 13573deb3ec6SMatthias Ringwald case HFP_W4_RFCOMM_DISCONNECTED: 1358a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART; 13593deb3ec6SMatthias Ringwald return; 13603deb3ec6SMatthias Ringwald case HFP_IDLE: 1361a0ffb263SMatthias Ringwald memcpy(hfp_connection->remote_addr, bd_addr, 6); 1362d63c37a1SMatthias Ringwald hfp_connection->state = HFP_W4_SDP_QUERY_COMPLETE; 1363a0ffb263SMatthias Ringwald connection_doing_sdp_query = hfp_connection; 1364a0ffb263SMatthias Ringwald hfp_connection->service_uuid = service_uuid; 13657fbb5f59SMatthias Ringwald sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, hfp_connection->remote_addr, service_uuid); 13663deb3ec6SMatthias Ringwald break; 13673deb3ec6SMatthias Ringwald default: 13683deb3ec6SMatthias Ringwald break; 13693deb3ec6SMatthias Ringwald } 13703deb3ec6SMatthias Ringwald } 13713deb3ec6SMatthias Ringwald 1372a0ffb263SMatthias Ringwald void hfp_release_service_level_connection(hfp_connection_t * hfp_connection){ 1373a0ffb263SMatthias Ringwald if (!hfp_connection) return; 1374a0ffb263SMatthias Ringwald hfp_release_audio_connection(hfp_connection); 13753deb3ec6SMatthias Ringwald 1376a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_W4_RFCOMM_CONNECTED){ 1377a0ffb263SMatthias Ringwald hfp_connection->state = HFP_IDLE; 13783deb3ec6SMatthias Ringwald return; 13793deb3ec6SMatthias Ringwald } 13803deb3ec6SMatthias Ringwald 1381a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_W4_RFCOMM_CONNECTED){ 1382a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN; 13833deb3ec6SMatthias Ringwald return; 13843deb3ec6SMatthias Ringwald } 13853deb3ec6SMatthias Ringwald 1386a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_W4_SCO_CONNECTED){ 1387a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM; 13883deb3ec6SMatthias Ringwald return; 13893deb3ec6SMatthias Ringwald } 13903deb3ec6SMatthias Ringwald 1391a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_W4_SCO_DISCONNECTED){ 1392a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W2_DISCONNECT_SCO; 13933deb3ec6SMatthias Ringwald return; 13943deb3ec6SMatthias Ringwald } 13953deb3ec6SMatthias Ringwald 13963deb3ec6SMatthias Ringwald return; 13973deb3ec6SMatthias Ringwald } 13983deb3ec6SMatthias Ringwald 1399a0ffb263SMatthias Ringwald void hfp_release_audio_connection(hfp_connection_t * hfp_connection){ 1400a0ffb263SMatthias Ringwald if (!hfp_connection) return; 1401a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return; 1402a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 1; 14033deb3ec6SMatthias Ringwald } 14043deb3ec6SMatthias Ringwald 1405ce263fc8SMatthias Ringwald static const struct link_settings { 1406ce263fc8SMatthias Ringwald const uint16_t max_latency; 1407ce263fc8SMatthias Ringwald const uint8_t retransmission_effort; 1408ce263fc8SMatthias Ringwald const uint16_t packet_types; 1409ce263fc8SMatthias Ringwald } hfp_link_settings [] = { 1410ce263fc8SMatthias Ringwald { 0xffff, 0xff, 0x03c1 }, // HFP_LINK_SETTINGS_D0, HV1 1411ce263fc8SMatthias Ringwald { 0xffff, 0xff, 0x03c4 }, // HFP_LINK_SETTINGS_D1, HV3 1412ce263fc8SMatthias Ringwald { 0x0007, 0x01, 0x03c8 }, // HFP_LINK_SETTINGS_S1, EV3 1413ce263fc8SMatthias Ringwald { 0x0007, 0x01, 0x0380 }, // HFP_LINK_SETTINGS_S2, 2-EV3 1414ce263fc8SMatthias Ringwald { 0x000a, 0x01, 0x0380 }, // HFP_LINK_SETTINGS_S3, 2-EV3 1415ce263fc8SMatthias Ringwald { 0x000c, 0x02, 0x0380 }, // HFP_LINK_SETTINGS_S4, 2-EV3 1416ce263fc8SMatthias Ringwald { 0x0008, 0x02, 0x03c8 }, // HFP_LINK_SETTINGS_T1, EV3 1417ce263fc8SMatthias Ringwald { 0x000d, 0x02, 0x0380 } // HFP_LINK_SETTINGS_T2, 2-EV3 1418ce263fc8SMatthias Ringwald }; 14193deb3ec6SMatthias Ringwald 1420eddcd308SMatthias Ringwald void hfp_setup_synchronous_connection(hfp_connection_t * hfp_connection){ 1421ce263fc8SMatthias Ringwald // all packet types, fixed bandwidth 1422eddcd308SMatthias Ringwald int setting = hfp_connection->link_setting; 1423ce263fc8SMatthias Ringwald log_info("hfp_setup_synchronous_connection using setting nr %u", setting); 1424eddcd308SMatthias Ringwald sco_establishment_active = hfp_connection; 1425d2c1d59aSMatthias Ringwald uint16_t sco_voice_setting = hci_get_sco_voice_setting(); 1426d2c1d59aSMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 14277522e673SMatthias Ringwald sco_voice_setting = 0x0043; // Transparent data 1428d2c1d59aSMatthias Ringwald } 1429eddcd308SMatthias Ringwald hci_send_cmd(&hci_setup_synchronous_connection, hfp_connection->acl_handle, 8000, 8000, hfp_link_settings[setting].max_latency, 1430d2c1d59aSMatthias Ringwald sco_voice_setting, hfp_link_settings[setting].retransmission_effort, hfp_link_settings[setting].packet_types); // all types 0x003f, only 2-ev3 0x380 1431ce263fc8SMatthias Ringwald } 1432d68dcce1SMatthias Ringwald 1433d68dcce1SMatthias Ringwald void hfp_set_packet_handler_for_rfcomm_connections(btstack_packet_handler_t handler){ 1434d68dcce1SMatthias Ringwald rfcomm_packet_handler = handler; 1435d68dcce1SMatthias Ringwald } 1436d68dcce1SMatthias Ringwald 1437