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 4756042629SMatthias Ringwald #include "hci_cmd.h" 4882636622SMatthias Ringwald #include "btstack_run_loop.h" 493deb3ec6SMatthias Ringwald 503deb3ec6SMatthias Ringwald #include "hci.h" 513deb3ec6SMatthias Ringwald #include "btstack_memory.h" 523deb3ec6SMatthias Ringwald #include "hci_dump.h" 533deb3ec6SMatthias Ringwald #include "l2cap.h" 54023f2764SMatthias Ringwald #include "btstack_debug.h" 55efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h" 56746ccb7eSMatthias Ringwald #include "classic/sdp_server.h" 57023f2764SMatthias Ringwald #include "classic/sdp_util.h" 587bd8e93bSMatthias Ringwald #include "btstack_event.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); 99f4000eebSMatthias Ringwald static hfp_callback_t hfp_callback; 100f4000eebSMatthias Ringwald 101f4000eebSMatthias Ringwald void hfp_set_callback(hfp_callback_t callback){ 102f4000eebSMatthias Ringwald hfp_callback = callback; 103f4000eebSMatthias Ringwald } 1043deb3ec6SMatthias Ringwald 1053deb3ec6SMatthias Ringwald const char * hfp_hf_feature(int index){ 1063deb3ec6SMatthias Ringwald if (index > HFP_HF_FEATURES_SIZE){ 1073deb3ec6SMatthias Ringwald return hfp_hf_features[HFP_HF_FEATURES_SIZE]; 1083deb3ec6SMatthias Ringwald } 1093deb3ec6SMatthias Ringwald return hfp_hf_features[index]; 1103deb3ec6SMatthias Ringwald } 1113deb3ec6SMatthias Ringwald 1123deb3ec6SMatthias Ringwald const char * hfp_ag_feature(int index){ 1133deb3ec6SMatthias Ringwald if (index > HFP_AG_FEATURES_SIZE){ 1143deb3ec6SMatthias Ringwald return hfp_ag_features[HFP_AG_FEATURES_SIZE]; 1153deb3ec6SMatthias Ringwald } 1163deb3ec6SMatthias Ringwald return hfp_ag_features[index]; 1173deb3ec6SMatthias Ringwald } 1183deb3ec6SMatthias Ringwald 1193deb3ec6SMatthias Ringwald int send_str_over_rfcomm(uint16_t cid, char * command){ 1203deb3ec6SMatthias Ringwald if (!rfcomm_can_send_packet_now(cid)) return 1; 12174386ee0SMatthias Ringwald log_info("HFP_TX %s", command); 12228190c0bSMatthias Ringwald int err = rfcomm_send(cid, (uint8_t*) command, strlen(command)); 1233deb3ec6SMatthias Ringwald if (err){ 12428190c0bSMatthias Ringwald log_error("rfcomm_send -> error 0x%02x \n", err); 1253deb3ec6SMatthias Ringwald } 1263deb3ec6SMatthias Ringwald return 1; 1273deb3ec6SMatthias Ringwald } 1283deb3ec6SMatthias Ringwald 1293deb3ec6SMatthias Ringwald #if 0 130a0ffb263SMatthias Ringwald void hfp_set_codec(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){ 1313deb3ec6SMatthias Ringwald // parse available codecs 1323deb3ec6SMatthias Ringwald int pos = 0; 1333deb3ec6SMatthias Ringwald int i; 1343deb3ec6SMatthias Ringwald for (i=0; i<size; i++){ 1353deb3ec6SMatthias Ringwald pos+=8; 136a0ffb263SMatthias Ringwald if (packet[pos] > hfp_connection->negotiated_codec){ 137a0ffb263SMatthias Ringwald hfp_connection->negotiated_codec = packet[pos]; 1383deb3ec6SMatthias Ringwald } 1393deb3ec6SMatthias Ringwald } 140a0ffb263SMatthias Ringwald printf("Negotiated Codec 0x%02x\n", hfp_connection->negotiated_codec); 1413deb3ec6SMatthias Ringwald } 1423deb3ec6SMatthias Ringwald #endif 1433deb3ec6SMatthias Ringwald 1443deb3ec6SMatthias Ringwald // UTILS 1453deb3ec6SMatthias Ringwald int get_bit(uint16_t bitmap, int position){ 1463deb3ec6SMatthias Ringwald return (bitmap >> position) & 1; 1473deb3ec6SMatthias Ringwald } 1483deb3ec6SMatthias Ringwald 1493deb3ec6SMatthias Ringwald int store_bit(uint32_t bitmap, int position, uint8_t value){ 1503deb3ec6SMatthias Ringwald if (value){ 1513deb3ec6SMatthias Ringwald bitmap |= 1 << position; 1523deb3ec6SMatthias Ringwald } else { 1533deb3ec6SMatthias Ringwald bitmap &= ~ (1 << position); 1543deb3ec6SMatthias Ringwald } 1553deb3ec6SMatthias Ringwald return bitmap; 1563deb3ec6SMatthias Ringwald } 1573deb3ec6SMatthias Ringwald 1583deb3ec6SMatthias Ringwald int join(char * buffer, int buffer_size, uint8_t * values, int values_nr){ 1593deb3ec6SMatthias Ringwald if (buffer_size < values_nr * 3) return 0; 1603deb3ec6SMatthias Ringwald int i; 1613deb3ec6SMatthias Ringwald int offset = 0; 1623deb3ec6SMatthias Ringwald for (i = 0; i < values_nr-1; i++) { 1633deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", values[i]); // puts string into buffer 1643deb3ec6SMatthias Ringwald } 1653deb3ec6SMatthias Ringwald if (i<values_nr){ 1663deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d", values[i]); 1673deb3ec6SMatthias Ringwald } 1683deb3ec6SMatthias Ringwald return offset; 1693deb3ec6SMatthias Ringwald } 1703deb3ec6SMatthias Ringwald 1713deb3ec6SMatthias Ringwald int join_bitmap(char * buffer, int buffer_size, uint32_t values, int values_nr){ 1723deb3ec6SMatthias Ringwald if (buffer_size < values_nr * 3) return 0; 1733deb3ec6SMatthias Ringwald 1743deb3ec6SMatthias Ringwald int i; 1753deb3ec6SMatthias Ringwald int offset = 0; 1763deb3ec6SMatthias Ringwald for (i = 0; i < values_nr-1; i++) { 1773deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", get_bit(values,i)); // puts string into buffer 1783deb3ec6SMatthias Ringwald } 1793deb3ec6SMatthias Ringwald 1803deb3ec6SMatthias Ringwald if (i<values_nr){ 1813deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d", get_bit(values,i)); 1823deb3ec6SMatthias Ringwald } 1833deb3ec6SMatthias Ringwald return offset; 1843deb3ec6SMatthias Ringwald } 1853deb3ec6SMatthias Ringwald 186a0ffb263SMatthias Ringwald void hfp_emit_simple_event(hfp_callback_t callback, uint8_t event_subtype){ 187a0ffb263SMatthias Ringwald if (!callback) return; 188a0ffb263SMatthias Ringwald uint8_t event[3]; 189a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 190a0ffb263SMatthias Ringwald event[1] = sizeof(event) - 2; 191a0ffb263SMatthias Ringwald event[2] = event_subtype; 192a0ffb263SMatthias Ringwald (*callback)(event, sizeof(event)); 193a0ffb263SMatthias Ringwald } 194a0ffb263SMatthias Ringwald 1953deb3ec6SMatthias Ringwald void hfp_emit_event(hfp_callback_t callback, uint8_t event_subtype, uint8_t value){ 1963deb3ec6SMatthias Ringwald if (!callback) return; 1973deb3ec6SMatthias Ringwald uint8_t event[4]; 1983deb3ec6SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 1993deb3ec6SMatthias Ringwald event[1] = sizeof(event) - 2; 2003deb3ec6SMatthias Ringwald event[2] = event_subtype; 2013deb3ec6SMatthias Ringwald event[3] = value; // status 0 == OK 2023deb3ec6SMatthias Ringwald (*callback)(event, sizeof(event)); 2033deb3ec6SMatthias Ringwald } 2043deb3ec6SMatthias Ringwald 205aa4dd815SMatthias Ringwald void hfp_emit_string_event(hfp_callback_t callback, uint8_t event_subtype, const char * value){ 206aa4dd815SMatthias Ringwald if (!callback) return; 207c1797c7dSMatthias Ringwald uint8_t event[40]; 208aa4dd815SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 209aa4dd815SMatthias Ringwald event[1] = sizeof(event) - 2; 210aa4dd815SMatthias Ringwald event[2] = event_subtype; 211aa4dd815SMatthias Ringwald int size = (strlen(value) < sizeof(event) - 4) ? strlen(value) : sizeof(event) - 4; 212aa4dd815SMatthias Ringwald strncpy((char*)&event[3], value, size); 213aa4dd815SMatthias Ringwald event[3 + size] = 0; 214aa4dd815SMatthias Ringwald (*callback)(event, sizeof(event)); 215aa4dd815SMatthias Ringwald } 216aa4dd815SMatthias Ringwald 217aa4dd815SMatthias Ringwald static void hfp_emit_audio_connection_established_event(hfp_callback_t callback, uint8_t value, uint16_t sco_handle){ 218aa4dd815SMatthias Ringwald if (!callback) return; 219aa4dd815SMatthias Ringwald uint8_t event[6]; 220aa4dd815SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 221aa4dd815SMatthias Ringwald event[1] = sizeof(event) - 2; 222aa4dd815SMatthias Ringwald event[2] = HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED; 223aa4dd815SMatthias Ringwald event[3] = value; // status 0 == OK 224f8fbdce0SMatthias Ringwald little_endian_store_16(event, 4, sco_handle); 225aa4dd815SMatthias Ringwald (*callback)(event, sizeof(event)); 226aa4dd815SMatthias Ringwald } 2273deb3ec6SMatthias Ringwald 2288f2a52f4SMatthias Ringwald btstack_linked_list_t * hfp_get_connections(){ 2298f2a52f4SMatthias Ringwald return (btstack_linked_list_t *) &hfp_connections; 2303deb3ec6SMatthias Ringwald } 2313deb3ec6SMatthias Ringwald 2323deb3ec6SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid){ 233665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 234665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 235665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 236a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 237a0ffb263SMatthias Ringwald if (hfp_connection->rfcomm_cid == cid){ 238a0ffb263SMatthias Ringwald return hfp_connection; 2393deb3ec6SMatthias Ringwald } 2403deb3ec6SMatthias Ringwald } 2413deb3ec6SMatthias Ringwald return NULL; 2423deb3ec6SMatthias Ringwald } 2433deb3ec6SMatthias Ringwald 2443deb3ec6SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr){ 245665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 246665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 247665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 248a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 249a0ffb263SMatthias Ringwald if (memcmp(hfp_connection->remote_addr, bd_addr, 6) == 0) { 250a0ffb263SMatthias Ringwald return hfp_connection; 2513deb3ec6SMatthias Ringwald } 2523deb3ec6SMatthias Ringwald } 2533deb3ec6SMatthias Ringwald return NULL; 2543deb3ec6SMatthias Ringwald } 2553deb3ec6SMatthias Ringwald 256aa4dd815SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_sco_handle(uint16_t handle){ 257665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 258665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 259665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 260a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 261a0ffb263SMatthias Ringwald if (hfp_connection->sco_handle == handle){ 262a0ffb263SMatthias Ringwald return hfp_connection; 2633deb3ec6SMatthias Ringwald } 2643deb3ec6SMatthias Ringwald } 2653deb3ec6SMatthias Ringwald return NULL; 2663deb3ec6SMatthias Ringwald } 2673deb3ec6SMatthias Ringwald 268a0ffb263SMatthias Ringwald void hfp_reset_context_flags(hfp_connection_t * hfp_connection){ 269a0ffb263SMatthias Ringwald if (!hfp_connection) return; 270a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 271a0ffb263SMatthias Ringwald hfp_connection->send_error = 0; 2723deb3ec6SMatthias Ringwald 273a0ffb263SMatthias Ringwald hfp_connection->keep_byte = 0; 2743deb3ec6SMatthias Ringwald 275a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 0; 276a0ffb263SMatthias Ringwald hfp_connection->operator_name_changed = 0; 2773deb3ec6SMatthias Ringwald 278a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = 0; 279a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 2803deb3ec6SMatthias Ringwald 281a0ffb263SMatthias Ringwald // establish codecs hfp_connection 282a0ffb263SMatthias Ringwald hfp_connection->suggested_codec = 0; 283a0ffb263SMatthias Ringwald hfp_connection->negotiated_codec = 0; 284a0ffb263SMatthias Ringwald hfp_connection->codec_confirmed = 0; 2853deb3ec6SMatthias Ringwald 286a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 287a0ffb263SMatthias Ringwald hfp_connection->call_waiting_notification_enabled = 0; 288a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 289a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 2903deb3ec6SMatthias Ringwald } 2913deb3ec6SMatthias Ringwald 292*fffdd288SMatthias Ringwald static hfp_connection_t * create_hfp_connection_context(void){ 293a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = btstack_memory_hfp_connection_get(); 294a0ffb263SMatthias Ringwald if (!hfp_connection) return NULL; 2953deb3ec6SMatthias Ringwald // init state 296a0ffb263SMatthias Ringwald memset(hfp_connection,0, sizeof(hfp_connection_t)); 2973deb3ec6SMatthias Ringwald 298a0ffb263SMatthias Ringwald hfp_connection->state = HFP_IDLE; 299a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 300a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_IDLE; 301aa4dd815SMatthias Ringwald 302a0ffb263SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 303a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 3043deb3ec6SMatthias Ringwald 305a0ffb263SMatthias Ringwald hfp_reset_context_flags(hfp_connection); 3063deb3ec6SMatthias Ringwald 307d63c37a1SMatthias Ringwald btstack_linked_list_add(&hfp_connections, (btstack_linked_item_t*)hfp_connection); 308a0ffb263SMatthias Ringwald return hfp_connection; 3093deb3ec6SMatthias Ringwald } 3103deb3ec6SMatthias Ringwald 311a0ffb263SMatthias Ringwald static void remove_hfp_connection_context(hfp_connection_t * hfp_connection){ 312a0ffb263SMatthias Ringwald btstack_linked_list_remove(&hfp_connections, (btstack_linked_item_t*) hfp_connection); 3133deb3ec6SMatthias Ringwald } 3143deb3ec6SMatthias Ringwald 3153deb3ec6SMatthias Ringwald static hfp_connection_t * provide_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr){ 316a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 317a0ffb263SMatthias Ringwald if (hfp_connection) return hfp_connection; 318a0ffb263SMatthias Ringwald hfp_connection = create_hfp_connection_context(); 319a0ffb263SMatthias Ringwald printf("created hfp_connection for address %s\n", bd_addr_to_str(bd_addr)); 320a0ffb263SMatthias Ringwald memcpy(hfp_connection->remote_addr, bd_addr, 6); 321a0ffb263SMatthias Ringwald return hfp_connection; 3223deb3ec6SMatthias Ringwald } 3233deb3ec6SMatthias Ringwald 324aa4dd815SMatthias Ringwald /* @param network. 325aa4dd815SMatthias Ringwald * 0 == no ability to reject a call. 326aa4dd815SMatthias Ringwald * 1 == ability to reject a call. 327aa4dd815SMatthias Ringwald */ 3283deb3ec6SMatthias Ringwald 3293deb3ec6SMatthias Ringwald /* @param suported_features 3303deb3ec6SMatthias Ringwald * HF bit 0: EC and/or NR function (yes/no, 1 = yes, 0 = no) 3313deb3ec6SMatthias Ringwald * HF bit 1: Call waiting or three-way calling(yes/no, 1 = yes, 0 = no) 3323deb3ec6SMatthias Ringwald * HF bit 2: CLI presentation capability (yes/no, 1 = yes, 0 = no) 3333deb3ec6SMatthias Ringwald * HF bit 3: Voice recognition activation (yes/no, 1= yes, 0 = no) 3343deb3ec6SMatthias Ringwald * HF bit 4: Remote volume control (yes/no, 1 = yes, 0 = no) 3353deb3ec6SMatthias Ringwald * HF bit 5: Wide band speech (yes/no, 1 = yes, 0 = no) 3363deb3ec6SMatthias Ringwald */ 3373deb3ec6SMatthias Ringwald /* Bit position: 3383deb3ec6SMatthias Ringwald * AG bit 0: Three-way calling (yes/no, 1 = yes, 0 = no) 3393deb3ec6SMatthias Ringwald * AG bit 1: EC and/or NR function (yes/no, 1 = yes, 0 = no) 3403deb3ec6SMatthias Ringwald * AG bit 2: Voice recognition function (yes/no, 1 = yes, 0 = no) 3413deb3ec6SMatthias Ringwald * AG bit 3: In-band ring tone capability (yes/no, 1 = yes, 0 = no) 3423deb3ec6SMatthias Ringwald * AG bit 4: Attach a phone number to a voice tag (yes/no, 1 = yes, 0 = no) 3433deb3ec6SMatthias Ringwald * AG bit 5: Wide band speech (yes/no, 1 = yes, 0 = no) 3443deb3ec6SMatthias Ringwald */ 3453deb3ec6SMatthias Ringwald 3469b1c3b4dSMatthias 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){ 3473deb3ec6SMatthias Ringwald uint8_t* attribute; 3483deb3ec6SMatthias Ringwald de_create_sequence(service); 3493deb3ec6SMatthias Ringwald 3503deb3ec6SMatthias Ringwald // 0x0000 "Service Record Handle" 3513deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ServiceRecordHandle); 3529b1c3b4dSMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle); 3533deb3ec6SMatthias Ringwald 3543deb3ec6SMatthias Ringwald // 0x0001 "Service Class ID List" 3553deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ServiceClassIDList); 3563deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 3573deb3ec6SMatthias Ringwald { 3583deb3ec6SMatthias Ringwald // "UUID for Service" 3593deb3ec6SMatthias Ringwald de_add_number(attribute, DE_UUID, DE_SIZE_16, service_uuid); 3603deb3ec6SMatthias Ringwald de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_GenericAudio); 3613deb3ec6SMatthias Ringwald } 3623deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 3633deb3ec6SMatthias Ringwald 3643deb3ec6SMatthias Ringwald // 0x0004 "Protocol Descriptor List" 3653deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ProtocolDescriptorList); 3663deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 3673deb3ec6SMatthias Ringwald { 3683deb3ec6SMatthias Ringwald uint8_t* l2cpProtocol = de_push_sequence(attribute); 3693deb3ec6SMatthias Ringwald { 3703deb3ec6SMatthias Ringwald de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, SDP_L2CAPProtocol); 3713deb3ec6SMatthias Ringwald } 3723deb3ec6SMatthias Ringwald de_pop_sequence(attribute, l2cpProtocol); 3733deb3ec6SMatthias Ringwald 3743deb3ec6SMatthias Ringwald uint8_t* rfcomm = de_push_sequence(attribute); 3753deb3ec6SMatthias Ringwald { 3763deb3ec6SMatthias Ringwald de_add_number(rfcomm, DE_UUID, DE_SIZE_16, SDP_RFCOMMProtocol); // rfcomm_service 3773deb3ec6SMatthias Ringwald de_add_number(rfcomm, DE_UINT, DE_SIZE_8, rfcomm_channel_nr); // rfcomm channel 3783deb3ec6SMatthias Ringwald } 3793deb3ec6SMatthias Ringwald de_pop_sequence(attribute, rfcomm); 3803deb3ec6SMatthias Ringwald } 3813deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 3823deb3ec6SMatthias Ringwald 3833deb3ec6SMatthias Ringwald 3843deb3ec6SMatthias Ringwald // 0x0005 "Public Browse Group" 3853deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, SDP_BrowseGroupList); // public browse group 3863deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 3873deb3ec6SMatthias Ringwald { 3883deb3ec6SMatthias Ringwald de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_PublicBrowseGroup); 3893deb3ec6SMatthias Ringwald } 3903deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 3913deb3ec6SMatthias Ringwald 3923deb3ec6SMatthias Ringwald // 0x0009 "Bluetooth Profile Descriptor List" 3933deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, SDP_BluetoothProfileDescriptorList); 3943deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 3953deb3ec6SMatthias Ringwald { 3963deb3ec6SMatthias Ringwald uint8_t *sppProfile = de_push_sequence(attribute); 3973deb3ec6SMatthias Ringwald { 3983deb3ec6SMatthias Ringwald de_add_number(sppProfile, DE_UUID, DE_SIZE_16, SDP_Handsfree); 3993deb3ec6SMatthias Ringwald de_add_number(sppProfile, DE_UINT, DE_SIZE_16, 0x0107); // Verision 1.7 4003deb3ec6SMatthias Ringwald } 4013deb3ec6SMatthias Ringwald de_pop_sequence(attribute, sppProfile); 4023deb3ec6SMatthias Ringwald } 4033deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 4043deb3ec6SMatthias Ringwald 4053deb3ec6SMatthias Ringwald // 0x0100 "Service Name" 4063deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); 4073deb3ec6SMatthias Ringwald de_add_data(service, DE_STRING, strlen(name), (uint8_t *) name); 4083deb3ec6SMatthias Ringwald } 4093deb3ec6SMatthias Ringwald 4103deb3ec6SMatthias Ringwald static hfp_connection_t * connection_doing_sdp_query = NULL; 411a8747467SMatthias Ringwald 41233885e44SMatthias Ringwald static void handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 413f4000eebSMatthias Ringwald hfp_handle_hci_event(packet_type, packet, size); 414f4000eebSMatthias Ringwald } 415f4000eebSMatthias Ringwald 4166c927b22SMatthias Ringwald static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 417a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = connection_doing_sdp_query; 4183deb3ec6SMatthias Ringwald 419d63c37a1SMatthias Ringwald if ( hfp_connection->state != HFP_W4_SDP_QUERY_COMPLETE) return; 4203deb3ec6SMatthias Ringwald 4210e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 4225611a760SMatthias Ringwald case SDP_EVENT_QUERY_RFCOMM_SERVICE: 423a0ffb263SMatthias Ringwald if (!hfp_connection) { 42472b50801SMatthias Ringwald log_error("handle_query_rfcomm_event alloc connection for RFCOMM port %u failed", sdp_event_query_rfcomm_service_get_rfcomm_channel(packet)); 4253deb3ec6SMatthias Ringwald return; 4263deb3ec6SMatthias Ringwald } 427a0ffb263SMatthias Ringwald hfp_connection->rfcomm_channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet); 4283deb3ec6SMatthias Ringwald break; 4295611a760SMatthias Ringwald case SDP_EVENT_QUERY_COMPLETE: 4303deb3ec6SMatthias Ringwald connection_doing_sdp_query = NULL; 431a0ffb263SMatthias Ringwald if (hfp_connection->rfcomm_channel_nr > 0){ 432a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_CONNECTED; 433a0ffb263SMatthias 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); 434a0ffb263SMatthias Ringwald rfcomm_create_channel(handle_hci_event, hfp_connection->remote_addr, hfp_connection->rfcomm_channel_nr, NULL); 4353deb3ec6SMatthias Ringwald break; 4363deb3ec6SMatthias Ringwald } 43772b50801SMatthias Ringwald log_info("rfcomm service not found, status %u.", sdp_event_query_complete_get_status(packet)); 4383deb3ec6SMatthias Ringwald break; 4393deb3ec6SMatthias Ringwald default: 4403deb3ec6SMatthias Ringwald break; 4413deb3ec6SMatthias Ringwald } 4423deb3ec6SMatthias Ringwald } 4433deb3ec6SMatthias Ringwald 444f4000eebSMatthias Ringwald void hfp_handle_hci_event(uint8_t packet_type, uint8_t *packet, uint16_t size){ 4453deb3ec6SMatthias Ringwald bd_addr_t event_addr; 4463deb3ec6SMatthias Ringwald uint16_t rfcomm_cid, handle; 447a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = NULL; 4483deb3ec6SMatthias Ringwald 4490e2df43fSMatthias Ringwald // printf("AG packet_handler type %u, event type %x, size %u\n", packet_type, hci_event_packet_get_type(packet), size); 450aa4dd815SMatthias Ringwald 4510e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 4523deb3ec6SMatthias Ringwald 4533deb3ec6SMatthias Ringwald case RFCOMM_EVENT_INCOMING_CONNECTION: 4543deb3ec6SMatthias Ringwald // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 455724d70a2SMatthias Ringwald reverse_bd_addr(&packet[2], event_addr); 456d63c37a1SMatthias Ringwald hfp_connection = provide_hfp_connection_context_for_bd_addr(event_addr); 4573deb3ec6SMatthias Ringwald 458d63c37a1SMatthias Ringwald if (!hfp_connection || hfp_connection->state != HFP_IDLE) return; 4593deb3ec6SMatthias Ringwald 460d63c37a1SMatthias Ringwald hfp_connection->rfcomm_cid = little_endian_read_16(packet, 9); 461d63c37a1SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_CONNECTED; 462d63c37a1SMatthias Ringwald printf("RFCOMM channel %u requested for %s\n", hfp_connection->rfcomm_cid, bd_addr_to_str(hfp_connection->remote_addr)); 463d63c37a1SMatthias Ringwald rfcomm_accept_connection(hfp_connection->rfcomm_cid); 4643deb3ec6SMatthias Ringwald break; 4653deb3ec6SMatthias Ringwald 4663deb3ec6SMatthias Ringwald case RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE: 4673deb3ec6SMatthias Ringwald // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16) 4680e2df43fSMatthias Ringwald printf("RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE packet_handler type %u, size %u\n", packet_type, size); 469aa4dd815SMatthias Ringwald 470724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], event_addr); 471d63c37a1SMatthias Ringwald hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr); 472d63c37a1SMatthias Ringwald if (!hfp_connection || hfp_connection->state != HFP_W4_RFCOMM_CONNECTED) return; 4733deb3ec6SMatthias Ringwald 4743deb3ec6SMatthias Ringwald if (packet[2]) { 475f4000eebSMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED, packet[2]); 476d63c37a1SMatthias Ringwald remove_hfp_connection_context(hfp_connection); 4773deb3ec6SMatthias Ringwald } else { 478d63c37a1SMatthias Ringwald hfp_connection->acl_handle = little_endian_read_16(packet, 9); 479d63c37a1SMatthias Ringwald printf("RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE con_handle 0x%02x\n", hfp_connection->acl_handle); 480aa4dd815SMatthias Ringwald 481d63c37a1SMatthias Ringwald hfp_connection->rfcomm_cid = little_endian_read_16(packet, 12); 482f8fbdce0SMatthias Ringwald uint16_t mtu = little_endian_read_16(packet, 14); 483d63c37a1SMatthias 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); 4843deb3ec6SMatthias Ringwald 485d63c37a1SMatthias Ringwald switch (hfp_connection->state){ 4863deb3ec6SMatthias Ringwald case HFP_W4_RFCOMM_CONNECTED: 487d63c37a1SMatthias Ringwald hfp_connection->state = HFP_EXCHANGE_SUPPORTED_FEATURES; 4883deb3ec6SMatthias Ringwald break; 4893deb3ec6SMatthias Ringwald case HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN: 490d63c37a1SMatthias Ringwald hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM; 4913deb3ec6SMatthias Ringwald printf("Shutting down RFCOMM.\n"); 4923deb3ec6SMatthias Ringwald break; 4933deb3ec6SMatthias Ringwald default: 4943deb3ec6SMatthias Ringwald break; 4953deb3ec6SMatthias Ringwald } 4963deb3ec6SMatthias Ringwald } 4973deb3ec6SMatthias Ringwald break; 4983deb3ec6SMatthias Ringwald 4993deb3ec6SMatthias Ringwald case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{ 500ce263fc8SMatthias Ringwald 501724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], event_addr); 5023deb3ec6SMatthias Ringwald int index = 2; 5033deb3ec6SMatthias Ringwald uint8_t status = packet[index++]; 504aa4dd815SMatthias Ringwald 505aa4dd815SMatthias Ringwald if (status != 0){ 506ce263fc8SMatthias Ringwald log_error("(e)SCO Connection failed status %u", status); 507ce263fc8SMatthias Ringwald // if outgoing && link_setting != d0 && appropriate error 508ce263fc8SMatthias Ringwald if (status != 0x11 && status != 0x1f) break; // invalid params / unspecified error 509d63c37a1SMatthias Ringwald hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr); 510d63c37a1SMatthias Ringwald if (!hfp_connection) break; 511d63c37a1SMatthias Ringwald switch (hfp_connection->link_setting){ 512ce263fc8SMatthias Ringwald case HFP_LINK_SETTINGS_D0: 513ce263fc8SMatthias Ringwald return; // no other option left 514ce263fc8SMatthias Ringwald case HFP_LINK_SETTINGS_D1: 515d63c37a1SMatthias Ringwald // hfp_connection->link_setting = HFP_LINK_SETTINGS_D0; 516ce263fc8SMatthias Ringwald // break; 517ce263fc8SMatthias Ringwald case HFP_LINK_SETTINGS_S1: 518d63c37a1SMatthias Ringwald // hfp_connection->link_setting = HFP_LINK_SETTINGS_D1; 519ce263fc8SMatthias Ringwald // break; 520ce263fc8SMatthias Ringwald case HFP_LINK_SETTINGS_S2: 521ce263fc8SMatthias Ringwald case HFP_LINK_SETTINGS_S3: 522ce263fc8SMatthias Ringwald case HFP_LINK_SETTINGS_S4: 523d63c37a1SMatthias Ringwald // hfp_connection->link_setting = HFP_LINK_SETTINGS_S1; 524ce263fc8SMatthias Ringwald // break; 525ce263fc8SMatthias Ringwald case HFP_LINK_SETTINGS_T1: 526ce263fc8SMatthias Ringwald case HFP_LINK_SETTINGS_T2: 527d63c37a1SMatthias Ringwald // hfp_connection->link_setting = HFP_LINK_SETTINGS_S3; 528d63c37a1SMatthias Ringwald hfp_connection->link_setting = HFP_LINK_SETTINGS_D0; 529ce263fc8SMatthias Ringwald break; 530ce263fc8SMatthias Ringwald } 531d63c37a1SMatthias Ringwald hfp_connection->establish_audio_connection = 1; 532aa4dd815SMatthias Ringwald break; 533aa4dd815SMatthias Ringwald } 534aa4dd815SMatthias Ringwald 535f8fbdce0SMatthias Ringwald uint16_t sco_handle = little_endian_read_16(packet, index); 5363deb3ec6SMatthias Ringwald index+=2; 537aa4dd815SMatthias Ringwald 538724d70a2SMatthias Ringwald reverse_bd_addr(&packet[index], event_addr); 5393deb3ec6SMatthias Ringwald index+=6; 540aa4dd815SMatthias Ringwald 5413deb3ec6SMatthias Ringwald uint8_t link_type = packet[index++]; 5423deb3ec6SMatthias Ringwald uint8_t transmission_interval = packet[index++]; // measured in slots 5433deb3ec6SMatthias Ringwald uint8_t retransmission_interval = packet[index++];// measured in slots 544f8fbdce0SMatthias Ringwald uint16_t rx_packet_length = little_endian_read_16(packet, index); // measured in bytes 5453deb3ec6SMatthias Ringwald index+=2; 546f8fbdce0SMatthias Ringwald uint16_t tx_packet_length = little_endian_read_16(packet, index); // measured in bytes 5473deb3ec6SMatthias Ringwald index+=2; 5483deb3ec6SMatthias Ringwald uint8_t air_mode = packet[index]; 5493deb3ec6SMatthias Ringwald 5503deb3ec6SMatthias Ringwald switch (link_type){ 5513deb3ec6SMatthias Ringwald case 0x00: 552aa4dd815SMatthias Ringwald log_info("SCO Connection established."); 5533deb3ec6SMatthias Ringwald if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval); 5543deb3ec6SMatthias Ringwald if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval); 5553deb3ec6SMatthias Ringwald if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length); 5563deb3ec6SMatthias Ringwald if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length); 5573deb3ec6SMatthias Ringwald break; 5583deb3ec6SMatthias Ringwald case 0x02: 559aa4dd815SMatthias Ringwald log_info("eSCO Connection established. \n"); 5603deb3ec6SMatthias Ringwald break; 5613deb3ec6SMatthias Ringwald default: 5623deb3ec6SMatthias Ringwald log_error("(e)SCO reserved link_type 0x%2x", link_type); 5633deb3ec6SMatthias Ringwald break; 5643deb3ec6SMatthias Ringwald } 5653deb3ec6SMatthias Ringwald log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, " 566aa4dd815SMatthias Ringwald " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)\n", sco_handle, 567aa4dd815SMatthias Ringwald bd_addr_to_str(event_addr), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length, air_mode); 5683deb3ec6SMatthias Ringwald 569d63c37a1SMatthias Ringwald hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr); 5703deb3ec6SMatthias Ringwald 571d63c37a1SMatthias Ringwald if (!hfp_connection) { 572d63c37a1SMatthias Ringwald log_error("SCO link created, hfp_connection for address %s not found.", bd_addr_to_str(event_addr)); 5733deb3ec6SMatthias Ringwald break; 5743deb3ec6SMatthias Ringwald } 5753deb3ec6SMatthias Ringwald 576d63c37a1SMatthias Ringwald if (hfp_connection->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){ 577aa4dd815SMatthias Ringwald log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN"); 578d63c37a1SMatthias Ringwald hfp_connection->state = HFP_W2_DISCONNECT_SCO; 579aa4dd815SMatthias Ringwald break; 580aa4dd815SMatthias Ringwald } 581d63c37a1SMatthias Ringwald hfp_connection->sco_handle = sco_handle; 582d63c37a1SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 583d63c37a1SMatthias Ringwald hfp_connection->state = HFP_AUDIO_CONNECTION_ESTABLISHED; 584f4000eebSMatthias Ringwald hfp_emit_audio_connection_established_event(hfp_callback, packet[2], sco_handle); 5853deb3ec6SMatthias Ringwald break; 5863deb3ec6SMatthias Ringwald } 5873deb3ec6SMatthias Ringwald 5883deb3ec6SMatthias Ringwald case RFCOMM_EVENT_CHANNEL_CLOSED: 589f8fbdce0SMatthias Ringwald rfcomm_cid = little_endian_read_16(packet,2); 590d63c37a1SMatthias Ringwald hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid); 591d63c37a1SMatthias Ringwald if (!hfp_connection) break; 592d63c37a1SMatthias Ringwald if (hfp_connection->state == HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART){ 593d63c37a1SMatthias Ringwald hfp_connection->state = HFP_IDLE; 594d63c37a1SMatthias Ringwald hfp_establish_service_level_connection(hfp_connection->remote_addr, hfp_connection->service_uuid); 5953deb3ec6SMatthias Ringwald break; 5963deb3ec6SMatthias Ringwald } 5973deb3ec6SMatthias Ringwald 598f4000eebSMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0); 599d63c37a1SMatthias Ringwald remove_hfp_connection_context(hfp_connection); 6003deb3ec6SMatthias Ringwald break; 6013deb3ec6SMatthias Ringwald 6023deb3ec6SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 603f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet,3); 604d63c37a1SMatthias Ringwald hfp_connection = get_hfp_connection_context_for_sco_handle(handle); 605aa4dd815SMatthias Ringwald 606d63c37a1SMatthias Ringwald if (!hfp_connection) break; 607aa4dd815SMatthias Ringwald 608d63c37a1SMatthias Ringwald if (hfp_connection->state != HFP_W4_SCO_DISCONNECTED){ 609aa4dd815SMatthias Ringwald log_info("Received gap disconnect in wrong hfp state"); 610aa4dd815SMatthias Ringwald } 611d63c37a1SMatthias Ringwald log_info("Check SCO handle: incoming 0x%02x, hfp_connection 0x%02x\n", handle, hfp_connection->sco_handle); 612aa4dd815SMatthias Ringwald 613d63c37a1SMatthias Ringwald if (handle == hfp_connection->sco_handle){ 614aa4dd815SMatthias Ringwald log_info("SCO disconnected, w2 disconnect RFCOMM\n"); 615d63c37a1SMatthias Ringwald hfp_connection->sco_handle = 0; 616d63c37a1SMatthias Ringwald hfp_connection->release_audio_connection = 0; 617d63c37a1SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 618f4000eebSMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED, 0); 6193deb3ec6SMatthias Ringwald break; 6203deb3ec6SMatthias Ringwald } 6213deb3ec6SMatthias Ringwald break; 6223deb3ec6SMatthias Ringwald 623fc64f94aSMatthias Ringwald default: 6243deb3ec6SMatthias Ringwald break; 6253deb3ec6SMatthias Ringwald } 6263deb3ec6SMatthias Ringwald } 6273deb3ec6SMatthias Ringwald 628aa4dd815SMatthias Ringwald // translates command string into hfp_command_t CMD 629aa4dd815SMatthias Ringwald static hfp_command_t parse_command(const char * line_buffer, int isHandsFree){ 630aa4dd815SMatthias Ringwald int offset = isHandsFree ? 0 : 2; 6313deb3ec6SMatthias Ringwald 632ce263fc8SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_LIST_CURRENT_CALLS, strlen(HFP_LIST_CURRENT_CALLS)) == 0){ 633ce263fc8SMatthias Ringwald return HFP_CMD_LIST_CURRENT_CALLS; 634ce263fc8SMatthias Ringwald } 635ce263fc8SMatthias Ringwald 636ce263fc8SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_SUBSCRIBER_NUMBER_INFORMATION, strlen(HFP_SUBSCRIBER_NUMBER_INFORMATION)) == 0){ 637ce263fc8SMatthias Ringwald return HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION; 638ce263fc8SMatthias Ringwald } 639ce263fc8SMatthias Ringwald 640aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_PHONE_NUMBER_FOR_VOICE_TAG, strlen(HFP_PHONE_NUMBER_FOR_VOICE_TAG)) == 0){ 641ce263fc8SMatthias Ringwald if (isHandsFree) return HFP_CMD_AG_SENT_PHONE_NUMBER; 642aa4dd815SMatthias Ringwald return HFP_CMD_HF_REQUEST_PHONE_NUMBER; 6433deb3ec6SMatthias Ringwald } 6443deb3ec6SMatthias Ringwald 645aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_TRANSMIT_DTMF_CODES, strlen(HFP_TRANSMIT_DTMF_CODES)) == 0){ 646aa4dd815SMatthias Ringwald return HFP_CMD_TRANSMIT_DTMF_CODES; 6473deb3ec6SMatthias Ringwald } 6483deb3ec6SMatthias Ringwald 649aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_SET_MICROPHONE_GAIN, strlen(HFP_SET_MICROPHONE_GAIN)) == 0){ 650aa4dd815SMatthias Ringwald return HFP_CMD_SET_MICROPHONE_GAIN; 6513deb3ec6SMatthias Ringwald } 6523deb3ec6SMatthias Ringwald 653aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_SET_SPEAKER_GAIN, strlen(HFP_SET_SPEAKER_GAIN)) == 0){ 654aa4dd815SMatthias Ringwald return HFP_CMD_SET_SPEAKER_GAIN; 6553deb3ec6SMatthias Ringwald } 6563deb3ec6SMatthias Ringwald 657aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_ACTIVATE_VOICE_RECOGNITION, strlen(HFP_ACTIVATE_VOICE_RECOGNITION)) == 0){ 658aa4dd815SMatthias Ringwald if (isHandsFree) return HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION; 659aa4dd815SMatthias Ringwald return HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION; 6603deb3ec6SMatthias Ringwald } 6613deb3ec6SMatthias Ringwald 662aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_TURN_OFF_EC_AND_NR, strlen(HFP_TURN_OFF_EC_AND_NR)) == 0){ 663aa4dd815SMatthias Ringwald return HFP_CMD_TURN_OFF_EC_AND_NR; 6643deb3ec6SMatthias Ringwald } 6653deb3ec6SMatthias Ringwald 666aa4dd815SMatthias Ringwald if (strncmp(line_buffer, HFP_CALL_ANSWERED, strlen(HFP_CALL_ANSWERED)) == 0){ 667aa4dd815SMatthias Ringwald return HFP_CMD_CALL_ANSWERED; 6683deb3ec6SMatthias Ringwald } 6693deb3ec6SMatthias Ringwald 670aa4dd815SMatthias Ringwald if (strncmp(line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0){ 671aa4dd815SMatthias Ringwald return HFP_CMD_CALL_PHONE_NUMBER; 6723deb3ec6SMatthias Ringwald } 6733deb3ec6SMatthias Ringwald 674ce263fc8SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_REDIAL_LAST_NUMBER, strlen(HFP_REDIAL_LAST_NUMBER)) == 0){ 675aa4dd815SMatthias Ringwald return HFP_CMD_REDIAL_LAST_NUMBER; 6763deb3ec6SMatthias Ringwald } 6773deb3ec6SMatthias Ringwald 678aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_CHANGE_IN_BAND_RING_TONE_SETTING, strlen(HFP_CHANGE_IN_BAND_RING_TONE_SETTING)) == 0){ 679aa4dd815SMatthias Ringwald return HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING; 680aa4dd815SMatthias Ringwald } 681aa4dd815SMatthias Ringwald 682aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_HANG_UP_CALL, strlen(HFP_HANG_UP_CALL)) == 0){ 683aa4dd815SMatthias Ringwald return HFP_CMD_HANG_UP_CALL; 684aa4dd815SMatthias Ringwald } 685aa4dd815SMatthias Ringwald 686aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_ERROR, strlen(HFP_ERROR)) == 0){ 687aa4dd815SMatthias Ringwald return HFP_CMD_ERROR; 688aa4dd815SMatthias Ringwald } 689aa4dd815SMatthias Ringwald 690ce263fc8SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_RING, strlen(HFP_RING)) == 0){ 691ce263fc8SMatthias Ringwald return HFP_CMD_RING; 692ce263fc8SMatthias Ringwald } 693ce263fc8SMatthias Ringwald 694aa4dd815SMatthias Ringwald if (isHandsFree && strncmp(line_buffer+offset, HFP_OK, strlen(HFP_OK)) == 0){ 695aa4dd815SMatthias Ringwald return HFP_CMD_OK; 696aa4dd815SMatthias Ringwald } 697aa4dd815SMatthias Ringwald 698aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_SUPPORTED_FEATURES, strlen(HFP_SUPPORTED_FEATURES)) == 0){ 699aa4dd815SMatthias Ringwald return HFP_CMD_SUPPORTED_FEATURES; 700aa4dd815SMatthias Ringwald } 701aa4dd815SMatthias Ringwald 702ce263fc8SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_TRANSFER_HF_INDICATOR_STATUS, strlen(HFP_TRANSFER_HF_INDICATOR_STATUS)) == 0){ 703ce263fc8SMatthias Ringwald return HFP_CMD_HF_INDICATOR_STATUS; 704ce263fc8SMatthias Ringwald } 705ce263fc8SMatthias Ringwald 706ce263fc8SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_RESPONSE_AND_HOLD, strlen(HFP_RESPONSE_AND_HOLD)) == 0){ 707ce263fc8SMatthias Ringwald if (strncmp(line_buffer+strlen(HFP_RESPONSE_AND_HOLD)+offset, "?", 1) == 0){ 708ce263fc8SMatthias Ringwald return HFP_CMD_RESPONSE_AND_HOLD_QUERY; 709ce263fc8SMatthias Ringwald } 710ce263fc8SMatthias Ringwald if (strncmp(line_buffer+strlen(HFP_RESPONSE_AND_HOLD)+offset, "=", 1) == 0){ 711ce263fc8SMatthias Ringwald return HFP_CMD_RESPONSE_AND_HOLD_COMMAND; 712ce263fc8SMatthias Ringwald } 713667ec068SMatthias Ringwald return HFP_CMD_RESPONSE_AND_HOLD_STATUS; 714ce263fc8SMatthias Ringwald } 715ce263fc8SMatthias Ringwald 716aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_INDICATOR, strlen(HFP_INDICATOR)) == 0){ 717aa4dd815SMatthias Ringwald if (strncmp(line_buffer+strlen(HFP_INDICATOR)+offset, "?", 1) == 0){ 718aa4dd815SMatthias Ringwald return HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS; 719aa4dd815SMatthias Ringwald } 720aa4dd815SMatthias Ringwald 721aa4dd815SMatthias Ringwald if (strncmp(line_buffer+strlen(HFP_INDICATOR)+offset, "=?", 2) == 0){ 722aa4dd815SMatthias Ringwald return HFP_CMD_RETRIEVE_AG_INDICATORS; 723aa4dd815SMatthias Ringwald } 724aa4dd815SMatthias Ringwald } 725aa4dd815SMatthias Ringwald 726aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_AVAILABLE_CODECS, strlen(HFP_AVAILABLE_CODECS)) == 0){ 727aa4dd815SMatthias Ringwald return HFP_CMD_AVAILABLE_CODECS; 728aa4dd815SMatthias Ringwald } 729aa4dd815SMatthias Ringwald 730aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, strlen(HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS)) == 0){ 731aa4dd815SMatthias Ringwald return HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE; 732aa4dd815SMatthias Ringwald } 733aa4dd815SMatthias Ringwald 734aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_ENABLE_CLIP, strlen(HFP_ENABLE_CLIP)) == 0){ 735a0ffb263SMatthias Ringwald if (isHandsFree) return HFP_CMD_AG_SENT_CLIP_INFORMATION; 736aa4dd815SMatthias Ringwald return HFP_CMD_ENABLE_CLIP; 737aa4dd815SMatthias Ringwald } 738aa4dd815SMatthias Ringwald 739aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_ENABLE_CALL_WAITING_NOTIFICATION, strlen(HFP_ENABLE_CALL_WAITING_NOTIFICATION)) == 0){ 740a0ffb263SMatthias Ringwald if (isHandsFree) return HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE; 741aa4dd815SMatthias Ringwald return HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION; 742aa4dd815SMatthias Ringwald } 743aa4dd815SMatthias Ringwald 744aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, strlen(HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES)) == 0){ 745aa4dd815SMatthias Ringwald 746aa4dd815SMatthias Ringwald if (isHandsFree) return HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES; 747aa4dd815SMatthias Ringwald 748aa4dd815SMatthias Ringwald if (strncmp(line_buffer+strlen(HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES)+offset, "=?", 2) == 0){ 749aa4dd815SMatthias Ringwald return HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES; 750aa4dd815SMatthias Ringwald } 751667ec068SMatthias Ringwald if (strncmp(line_buffer+strlen(HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES)+offset, "=", 1) == 0){ 752aa4dd815SMatthias Ringwald return HFP_CMD_CALL_HOLD; 753aa4dd815SMatthias Ringwald } 754aa4dd815SMatthias Ringwald 755aa4dd815SMatthias Ringwald return HFP_CMD_UNKNOWN; 756aa4dd815SMatthias Ringwald } 757aa4dd815SMatthias Ringwald 758aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_GENERIC_STATUS_INDICATOR, strlen(HFP_GENERIC_STATUS_INDICATOR)) == 0){ 759667ec068SMatthias Ringwald if (isHandsFree) { 760667ec068SMatthias Ringwald return HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS; 761667ec068SMatthias Ringwald } 762aa4dd815SMatthias Ringwald if (strncmp(line_buffer+strlen(HFP_GENERIC_STATUS_INDICATOR)+offset, "=?", 2) == 0){ 763aa4dd815SMatthias Ringwald return HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS; 764aa4dd815SMatthias Ringwald } 765aa4dd815SMatthias Ringwald if (strncmp(line_buffer+strlen(HFP_GENERIC_STATUS_INDICATOR)+offset, "=", 1) == 0){ 766aa4dd815SMatthias Ringwald return HFP_CMD_LIST_GENERIC_STATUS_INDICATORS; 767aa4dd815SMatthias Ringwald } 768aa4dd815SMatthias Ringwald return HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE; 769aa4dd815SMatthias Ringwald } 770aa4dd815SMatthias Ringwald 771aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS, strlen(HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS)) == 0){ 772aa4dd815SMatthias Ringwald return HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE; 7733deb3ec6SMatthias Ringwald } 7743deb3ec6SMatthias Ringwald 7753deb3ec6SMatthias Ringwald 776aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_QUERY_OPERATOR_SELECTION, strlen(HFP_QUERY_OPERATOR_SELECTION)) == 0){ 777aa4dd815SMatthias Ringwald if (strncmp(line_buffer+strlen(HFP_QUERY_OPERATOR_SELECTION)+offset, "=", 1) == 0){ 778aa4dd815SMatthias Ringwald return HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT; 7793deb3ec6SMatthias Ringwald } 780aa4dd815SMatthias Ringwald return HFP_CMD_QUERY_OPERATOR_SELECTION_NAME; 7813deb3ec6SMatthias Ringwald } 7823deb3ec6SMatthias Ringwald 783aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_TRANSFER_AG_INDICATOR_STATUS, strlen(HFP_TRANSFER_AG_INDICATOR_STATUS)) == 0){ 784aa4dd815SMatthias Ringwald return HFP_CMD_TRANSFER_AG_INDICATOR_STATUS; 7853deb3ec6SMatthias Ringwald } 7863deb3ec6SMatthias Ringwald 787aa4dd815SMatthias Ringwald if (isHandsFree && strncmp(line_buffer+offset, HFP_EXTENDED_AUDIO_GATEWAY_ERROR, strlen(HFP_EXTENDED_AUDIO_GATEWAY_ERROR)) == 0){ 788aa4dd815SMatthias Ringwald return HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR; 7893deb3ec6SMatthias Ringwald } 7903deb3ec6SMatthias Ringwald 791aa4dd815SMatthias Ringwald if (!isHandsFree && strncmp(line_buffer+offset, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, strlen(HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR)) == 0){ 792aa4dd815SMatthias Ringwald return HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR; 7933deb3ec6SMatthias Ringwald } 7943deb3ec6SMatthias Ringwald 795aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_TRIGGER_CODEC_CONNECTION_SETUP, strlen(HFP_TRIGGER_CODEC_CONNECTION_SETUP)) == 0){ 796aa4dd815SMatthias Ringwald return HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP; 797aa4dd815SMatthias Ringwald } 798aa4dd815SMatthias Ringwald 799aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_CONFIRM_COMMON_CODEC, strlen(HFP_CONFIRM_COMMON_CODEC)) == 0){ 8003deb3ec6SMatthias Ringwald if (isHandsFree){ 801aa4dd815SMatthias Ringwald return HFP_CMD_AG_SUGGESTED_CODEC; 8023deb3ec6SMatthias Ringwald } else { 803aa4dd815SMatthias Ringwald return HFP_CMD_HF_CONFIRMED_CODEC; 8043deb3ec6SMatthias Ringwald } 8053deb3ec6SMatthias Ringwald } 8063deb3ec6SMatthias Ringwald 807aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, "AT+", 3) == 0){ 808667ec068SMatthias Ringwald log_info("process unknown HF command %s \n", line_buffer); 809aa4dd815SMatthias Ringwald return HFP_CMD_UNKNOWN; 8103deb3ec6SMatthias Ringwald } 8113deb3ec6SMatthias Ringwald 812aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, "+", 1) == 0){ 813667ec068SMatthias Ringwald log_info(" process unknown AG command %s \n", line_buffer); 814aa4dd815SMatthias Ringwald return HFP_CMD_UNKNOWN; 815aa4dd815SMatthias Ringwald } 8163deb3ec6SMatthias Ringwald 817aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, "NOP", 3) == 0){ 818aa4dd815SMatthias Ringwald return HFP_CMD_NONE; 819aa4dd815SMatthias Ringwald } 820aa4dd815SMatthias Ringwald 821aa4dd815SMatthias Ringwald return HFP_CMD_NONE; 8223deb3ec6SMatthias Ringwald } 8233deb3ec6SMatthias Ringwald 824a0ffb263SMatthias Ringwald static void hfp_parser_store_byte(hfp_connection_t * hfp_connection, uint8_t byte){ 825ce263fc8SMatthias Ringwald // printf("hfp_parser_store_byte %c at pos %u\n", (char) byte, context->line_size); 8263deb3ec6SMatthias Ringwald // TODO: add limit 827a0ffb263SMatthias Ringwald hfp_connection->line_buffer[hfp_connection->line_size++] = byte; 828a0ffb263SMatthias Ringwald hfp_connection->line_buffer[hfp_connection->line_size] = 0; 8293deb3ec6SMatthias Ringwald } 830a0ffb263SMatthias Ringwald static int hfp_parser_is_buffer_empty(hfp_connection_t * hfp_connection){ 831a0ffb263SMatthias Ringwald return hfp_connection->line_size == 0; 8323deb3ec6SMatthias Ringwald } 8333deb3ec6SMatthias Ringwald 8343deb3ec6SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){ 8353deb3ec6SMatthias Ringwald return byte == '\n' || byte == '\r'; 8363deb3ec6SMatthias Ringwald } 8373deb3ec6SMatthias Ringwald 8383deb3ec6SMatthias Ringwald static int hfp_parser_is_end_of_header(uint8_t byte){ 8393deb3ec6SMatthias Ringwald return hfp_parser_is_end_of_line(byte) || byte == ':' || byte == '?'; 8403deb3ec6SMatthias Ringwald } 8413deb3ec6SMatthias Ringwald 842a0ffb263SMatthias Ringwald static int hfp_parser_found_separator(hfp_connection_t * hfp_connection, uint8_t byte){ 843a0ffb263SMatthias Ringwald if (hfp_connection->keep_byte == 1) return 1; 8443deb3ec6SMatthias Ringwald 8453deb3ec6SMatthias Ringwald int found_separator = byte == ',' || byte == '\n'|| byte == '\r'|| 8463deb3ec6SMatthias Ringwald byte == ')' || byte == '(' || byte == ':' || 8473deb3ec6SMatthias Ringwald byte == '-' || byte == '"' || byte == '?'|| byte == '='; 8483deb3ec6SMatthias Ringwald return found_separator; 8493deb3ec6SMatthias Ringwald } 8503deb3ec6SMatthias Ringwald 851a0ffb263SMatthias Ringwald static void hfp_parser_next_state(hfp_connection_t * hfp_connection, uint8_t byte){ 852a0ffb263SMatthias Ringwald hfp_connection->line_size = 0; 8533deb3ec6SMatthias Ringwald if (hfp_parser_is_end_of_line(byte)){ 854a0ffb263SMatthias Ringwald hfp_connection->parser_item_index = 0; 855a0ffb263SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 8563deb3ec6SMatthias Ringwald return; 8573deb3ec6SMatthias Ringwald } 858a0ffb263SMatthias Ringwald switch (hfp_connection->parser_state){ 8593deb3ec6SMatthias Ringwald case HFP_PARSER_CMD_HEADER: 860a0ffb263SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE; 861a0ffb263SMatthias Ringwald if (hfp_connection->keep_byte == 1){ 862a0ffb263SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 863a0ffb263SMatthias Ringwald hfp_connection->keep_byte = 0; 8643deb3ec6SMatthias Ringwald } 8653deb3ec6SMatthias Ringwald break; 8663deb3ec6SMatthias Ringwald case HFP_PARSER_CMD_SEQUENCE: 867a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 868ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 869a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 870a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 8713deb3ec6SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 872aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 873aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 874aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 875aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 876ce263fc8SMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 877a0ffb263SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_SECOND_ITEM; 8783deb3ec6SMatthias Ringwald break; 8793deb3ec6SMatthias Ringwald default: 8803deb3ec6SMatthias Ringwald break; 8813deb3ec6SMatthias Ringwald } 8823deb3ec6SMatthias Ringwald break; 8833deb3ec6SMatthias Ringwald case HFP_PARSER_SECOND_ITEM: 884a0ffb263SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_THIRD_ITEM; 8853deb3ec6SMatthias Ringwald break; 8863deb3ec6SMatthias Ringwald case HFP_PARSER_THIRD_ITEM: 887a0ffb263SMatthias Ringwald if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS){ 888a0ffb263SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE; 8893deb3ec6SMatthias Ringwald break; 8903deb3ec6SMatthias Ringwald } 891a0ffb263SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 8923deb3ec6SMatthias Ringwald break; 8933deb3ec6SMatthias Ringwald } 8943deb3ec6SMatthias Ringwald } 8953deb3ec6SMatthias Ringwald 896a0ffb263SMatthias Ringwald void hfp_parse(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){ 897aa4dd815SMatthias Ringwald // handle ATD<dial_string>; 898a0ffb263SMatthias Ringwald if (strncmp((const char*)hfp_connection->line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0){ 899aa4dd815SMatthias Ringwald // check for end-of-line or ';' 900aa4dd815SMatthias Ringwald if (byte == ';' || hfp_parser_is_end_of_line(byte)){ 901a0ffb263SMatthias Ringwald hfp_connection->line_buffer[hfp_connection->line_size] = 0; 902a0ffb263SMatthias Ringwald hfp_connection->line_size = 0; 903a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_CALL_PHONE_NUMBER; 904aa4dd815SMatthias Ringwald } else { 905a0ffb263SMatthias Ringwald hfp_connection->line_buffer[hfp_connection->line_size++] = byte; 906aa4dd815SMatthias Ringwald } 907aa4dd815SMatthias Ringwald return; 908aa4dd815SMatthias Ringwald } 909aa4dd815SMatthias Ringwald 9103deb3ec6SMatthias Ringwald // TODO: handle space inside word 911a0ffb263SMatthias Ringwald if (byte == ' ' && hfp_connection->parser_state > HFP_PARSER_CMD_HEADER) return; 9123deb3ec6SMatthias Ringwald 913a0ffb263SMatthias Ringwald if (byte == ',' && hfp_connection->command == HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE){ 914a0ffb263SMatthias Ringwald if (hfp_connection->line_size == 0){ 915a0ffb263SMatthias Ringwald hfp_connection->line_buffer[0] = 0; 916a0ffb263SMatthias Ringwald hfp_connection->ignore_value = 1; 917a0ffb263SMatthias Ringwald parse_sequence(hfp_connection); 918ce263fc8SMatthias Ringwald return; 919ce263fc8SMatthias Ringwald } 920ce263fc8SMatthias Ringwald } 921ce263fc8SMatthias Ringwald 922a0ffb263SMatthias Ringwald if (!hfp_parser_found_separator(hfp_connection, byte)){ 923a0ffb263SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 9243deb3ec6SMatthias Ringwald return; 9253deb3ec6SMatthias Ringwald } 926ce263fc8SMatthias Ringwald 9273deb3ec6SMatthias Ringwald if (hfp_parser_is_end_of_line(byte)) { 928a0ffb263SMatthias Ringwald if (hfp_parser_is_buffer_empty(hfp_connection)){ 929a0ffb263SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 9303deb3ec6SMatthias Ringwald } 9313deb3ec6SMatthias Ringwald } 932a0ffb263SMatthias Ringwald if (hfp_parser_is_buffer_empty(hfp_connection)) return; 9333deb3ec6SMatthias Ringwald 934a0ffb263SMatthias Ringwald switch (hfp_connection->parser_state){ 9353deb3ec6SMatthias Ringwald case HFP_PARSER_CMD_HEADER: // header 9363deb3ec6SMatthias Ringwald if (byte == '='){ 937a0ffb263SMatthias Ringwald hfp_connection->keep_byte = 1; 938a0ffb263SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 9393deb3ec6SMatthias Ringwald return; 9403deb3ec6SMatthias Ringwald } 9413deb3ec6SMatthias Ringwald 9423deb3ec6SMatthias Ringwald if (byte == '?'){ 943a0ffb263SMatthias Ringwald hfp_connection->keep_byte = 0; 944a0ffb263SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 9453deb3ec6SMatthias Ringwald return; 9463deb3ec6SMatthias Ringwald } 947ce263fc8SMatthias Ringwald 948ce263fc8SMatthias Ringwald if (byte == ','){ 949a0ffb263SMatthias Ringwald hfp_connection->resolve_byte = 1; 950ce263fc8SMatthias Ringwald } 951ce263fc8SMatthias Ringwald 952a0ffb263SMatthias Ringwald // printf(" parse header 2 %s, keep separator $ %d\n", hfp_connection->line_buffer, hfp_connection->keep_byte); 953a0ffb263SMatthias Ringwald if (hfp_parser_is_end_of_header(byte) || hfp_connection->keep_byte == 1){ 954a0ffb263SMatthias Ringwald // printf(" parse header 3 %s, keep separator $ %d\n", hfp_connection->line_buffer, hfp_connection->keep_byte); 955a0ffb263SMatthias Ringwald char * line_buffer = (char *)hfp_connection->line_buffer; 956a0ffb263SMatthias Ringwald hfp_connection->command = parse_command(line_buffer, isHandsFree); 957aa4dd815SMatthias Ringwald 958a0ffb263SMatthias Ringwald /* resolve command name according to hfp_connection */ 959a0ffb263SMatthias Ringwald if (hfp_connection->command == HFP_CMD_UNKNOWN){ 960a0ffb263SMatthias Ringwald switch(hfp_connection->state){ 961aa4dd815SMatthias Ringwald case HFP_W4_LIST_GENERIC_STATUS_INDICATORS: 962a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_LIST_GENERIC_STATUS_INDICATORS; 963aa4dd815SMatthias Ringwald break; 964aa4dd815SMatthias Ringwald case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS: 965a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS; 966aa4dd815SMatthias Ringwald break; 967aa4dd815SMatthias Ringwald case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 968a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE; 969aa4dd815SMatthias Ringwald break; 970aa4dd815SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS_STATUS: 971a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS; 972aa4dd815SMatthias Ringwald break; 973aa4dd815SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS: 974a0ffb263SMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0; 975a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS; 976aa4dd815SMatthias Ringwald break; 977aa4dd815SMatthias Ringwald default: 978aa4dd815SMatthias Ringwald break; 979aa4dd815SMatthias Ringwald } 980aa4dd815SMatthias Ringwald } 9813deb3ec6SMatthias Ringwald } 9823deb3ec6SMatthias Ringwald break; 9833deb3ec6SMatthias Ringwald 984ce263fc8SMatthias Ringwald case HFP_PARSER_CMD_SEQUENCE: 985a0ffb263SMatthias Ringwald parse_sequence(hfp_connection); 986ce263fc8SMatthias Ringwald break; 987ce263fc8SMatthias Ringwald case HFP_PARSER_SECOND_ITEM: 988a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 989ce263fc8SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 990a0ffb263SMatthias Ringwald log_info("format %s, ", hfp_connection->line_buffer); 991a0ffb263SMatthias Ringwald hfp_connection->network_operator.format = atoi((char *)&hfp_connection->line_buffer[0]); 992ce263fc8SMatthias Ringwald break; 993ce263fc8SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 994a0ffb263SMatthias Ringwald log_info("format %s \n", hfp_connection->line_buffer); 995a0ffb263SMatthias Ringwald hfp_connection->network_operator.format = atoi((char *)&hfp_connection->line_buffer[0]); 996ce263fc8SMatthias Ringwald break; 997ce263fc8SMatthias Ringwald case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 998ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 999ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 1000a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].state = (uint8_t)atoi((char*)hfp_connection->line_buffer); 1001ce263fc8SMatthias Ringwald break; 1002ce263fc8SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 1003a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = (uint8_t)atoi((char*)hfp_connection->line_buffer); 1004a0ffb263SMatthias Ringwald log_info("%d \n", hfp_connection->ag_indicators[hfp_connection->parser_item_index].status); 1005a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].status_changed = 1; 1006ce263fc8SMatthias Ringwald break; 1007ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 1008a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].min_range = atoi((char *)hfp_connection->line_buffer); 1009a0ffb263SMatthias Ringwald log_info("%s, ", hfp_connection->line_buffer); 1010ce263fc8SMatthias Ringwald break; 1011ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 1012a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1013a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1014a0ffb263SMatthias Ringwald hfp_connection->bnip_type = (uint8_t)atoi((char*)hfp_connection->line_buffer); 1015ce263fc8SMatthias Ringwald break; 1016ce263fc8SMatthias Ringwald default: 1017ce263fc8SMatthias Ringwald break; 1018ce263fc8SMatthias Ringwald } 1019ce263fc8SMatthias Ringwald break; 1020ce263fc8SMatthias Ringwald 1021ce263fc8SMatthias Ringwald case HFP_PARSER_THIRD_ITEM: 1022a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1023ce263fc8SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1024a0ffb263SMatthias Ringwald strcpy(hfp_connection->network_operator.name, (char *)hfp_connection->line_buffer); 1025a0ffb263SMatthias Ringwald log_info("name %s\n", hfp_connection->line_buffer); 1026ce263fc8SMatthias Ringwald break; 1027ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 1028a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].max_range = atoi((char *)hfp_connection->line_buffer); 1029a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1030a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr = hfp_connection->parser_item_index; 1031a0ffb263SMatthias Ringwald log_info("%s)\n", hfp_connection->line_buffer); 1032ce263fc8SMatthias Ringwald break; 1033ce263fc8SMatthias Ringwald default: 1034ce263fc8SMatthias Ringwald break; 1035ce263fc8SMatthias Ringwald } 1036ce263fc8SMatthias Ringwald break; 1037ce263fc8SMatthias Ringwald } 1038a0ffb263SMatthias Ringwald hfp_parser_next_state(hfp_connection, byte); 1039ce263fc8SMatthias Ringwald 1040a0ffb263SMatthias Ringwald if (hfp_connection->resolve_byte && hfp_connection->command == HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE){ 1041a0ffb263SMatthias Ringwald hfp_connection->resolve_byte = 0; 1042a0ffb263SMatthias Ringwald hfp_connection->ignore_value = 1; 1043a0ffb263SMatthias Ringwald parse_sequence(hfp_connection); 1044a0ffb263SMatthias Ringwald hfp_connection->line_buffer[0] = 0; 1045a0ffb263SMatthias Ringwald hfp_connection->line_size = 0; 1046ce263fc8SMatthias Ringwald } 1047ce263fc8SMatthias Ringwald } 1048ce263fc8SMatthias Ringwald 1049a0ffb263SMatthias Ringwald static void parse_sequence(hfp_connection_t * hfp_connection){ 1050ce263fc8SMatthias Ringwald int value; 1051a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1052667ec068SMatthias Ringwald case HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS: 1053a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1054667ec068SMatthias Ringwald int i; 1055a0ffb263SMatthias Ringwald switch (hfp_connection->parser_item_index){ 1056667ec068SMatthias Ringwald case 0: 1057a0ffb263SMatthias Ringwald for (i=0;i<hfp_connection->generic_status_indicators_nr;i++){ 1058a0ffb263SMatthias Ringwald if (hfp_connection->generic_status_indicators[i].uuid == value){ 1059a0ffb263SMatthias Ringwald hfp_connection->parser_indicator_index = i; 1060667ec068SMatthias Ringwald break; 1061667ec068SMatthias Ringwald } 1062667ec068SMatthias Ringwald } 1063667ec068SMatthias Ringwald break; 1064667ec068SMatthias Ringwald case 1: 1065a0ffb263SMatthias Ringwald if (hfp_connection->parser_indicator_index <0) break; 1066a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[hfp_connection->parser_indicator_index].state = value; 1067667ec068SMatthias Ringwald log_info("HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS set indicator at index %u, to %u\n", 1068a0ffb263SMatthias Ringwald hfp_connection->parser_item_index, value); 1069667ec068SMatthias Ringwald break; 1070667ec068SMatthias Ringwald default: 1071667ec068SMatthias Ringwald break; 1072667ec068SMatthias Ringwald } 1073a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1074667ec068SMatthias Ringwald break; 1075667ec068SMatthias Ringwald 1076667ec068SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1077a0ffb263SMatthias Ringwald switch(hfp_connection->parser_item_index){ 1078667ec068SMatthias Ringwald case 0: 1079a0ffb263SMatthias Ringwald strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number)); 1080a0ffb263SMatthias Ringwald hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0; 1081667ec068SMatthias Ringwald break; 1082667ec068SMatthias Ringwald case 1: 1083a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1084a0ffb263SMatthias Ringwald hfp_connection->bnip_type = value; 1085667ec068SMatthias Ringwald break; 1086667ec068SMatthias Ringwald default: 1087667ec068SMatthias Ringwald break; 1088667ec068SMatthias Ringwald } 1089a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1090667ec068SMatthias Ringwald break; 1091667ec068SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 1092a0ffb263SMatthias Ringwald switch(hfp_connection->parser_item_index){ 1093667ec068SMatthias Ringwald case 0: 1094a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1095a0ffb263SMatthias Ringwald hfp_connection->clcc_idx = value; 1096667ec068SMatthias Ringwald break; 1097667ec068SMatthias Ringwald case 1: 1098a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1099a0ffb263SMatthias Ringwald hfp_connection->clcc_dir = value; 1100667ec068SMatthias Ringwald break; 1101667ec068SMatthias Ringwald case 2: 1102a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1103a0ffb263SMatthias Ringwald hfp_connection->clcc_status = value; 1104667ec068SMatthias Ringwald break; 1105667ec068SMatthias Ringwald case 3: 1106a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1107a0ffb263SMatthias Ringwald hfp_connection->clcc_mpty = value; 1108667ec068SMatthias Ringwald break; 1109667ec068SMatthias Ringwald case 4: 1110a0ffb263SMatthias Ringwald strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number)); 1111a0ffb263SMatthias Ringwald hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0; 1112667ec068SMatthias Ringwald break; 1113667ec068SMatthias Ringwald case 5: 1114a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1115a0ffb263SMatthias Ringwald hfp_connection->bnip_type = value; 1116667ec068SMatthias Ringwald break; 1117667ec068SMatthias Ringwald default: 1118667ec068SMatthias Ringwald break; 1119667ec068SMatthias Ringwald } 1120a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1121667ec068SMatthias Ringwald break; 1122aa4dd815SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 1123a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1124a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = value; 1125aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CMD_SET_MICROPHONE_GAIN %d\n", value); 1126aa4dd815SMatthias Ringwald break; 1127aa4dd815SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 1128a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1129a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = value; 1130aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CMD_SET_SPEAKER_GAIN %d\n", value); 1131aa4dd815SMatthias Ringwald break; 1132aa4dd815SMatthias Ringwald case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION: 1133a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1134a0ffb263SMatthias Ringwald hfp_connection->ag_activate_voice_recognition = value; 1135aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION %d\n", value); 1136aa4dd815SMatthias Ringwald break; 1137aa4dd815SMatthias Ringwald case HFP_CMD_TURN_OFF_EC_AND_NR: 1138a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1139a0ffb263SMatthias Ringwald hfp_connection->ag_echo_and_noise_reduction = value; 1140aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CMD_TURN_OFF_EC_AND_NR %d\n", value); 1141aa4dd815SMatthias Ringwald break; 1142aa4dd815SMatthias Ringwald case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING: 1143a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1144a0ffb263SMatthias Ringwald hfp_connection->remote_supported_features = store_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE, value); 1145aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CHANGE_IN_BAND_RING_TONE_SETTING %d\n", value); 1146aa4dd815SMatthias Ringwald break; 11473deb3ec6SMatthias Ringwald case HFP_CMD_HF_CONFIRMED_CODEC: 1148a0ffb263SMatthias Ringwald hfp_connection->codec_confirmed = atoi((char*)hfp_connection->line_buffer); 1149a0ffb263SMatthias Ringwald log_info("hfp parse HFP_CMD_HF_CONFIRMED_CODEC %d\n", hfp_connection->codec_confirmed); 11503deb3ec6SMatthias Ringwald break; 11513deb3ec6SMatthias Ringwald case HFP_CMD_AG_SUGGESTED_CODEC: 1152a0ffb263SMatthias Ringwald hfp_connection->suggested_codec = atoi((char*)hfp_connection->line_buffer); 1153a0ffb263SMatthias Ringwald log_info("hfp parse HFP_CMD_AG_SUGGESTED_CODEC %d\n", hfp_connection->suggested_codec); 11543deb3ec6SMatthias Ringwald break; 11553deb3ec6SMatthias Ringwald case HFP_CMD_SUPPORTED_FEATURES: 1156a0ffb263SMatthias Ringwald hfp_connection->remote_supported_features = atoi((char*)hfp_connection->line_buffer); 1157a0ffb263SMatthias Ringwald log_info("Parsed supported feature %d\n", hfp_connection->remote_supported_features); 11583deb3ec6SMatthias Ringwald break; 11593deb3ec6SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 1160a0ffb263SMatthias Ringwald log_info("Parsed codec %s\n", hfp_connection->line_buffer); 1161a0ffb263SMatthias Ringwald hfp_connection->remote_codecs[hfp_connection->parser_item_index] = (uint16_t)atoi((char*)hfp_connection->line_buffer); 1162a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1163a0ffb263SMatthias Ringwald hfp_connection->remote_codecs_nr = hfp_connection->parser_item_index; 11643deb3ec6SMatthias Ringwald break; 1165aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 1166a0ffb263SMatthias Ringwald strcpy((char *)hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, (char *)hfp_connection->line_buffer); 1167a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].index = hfp_connection->parser_item_index+1; 1168a0ffb263SMatthias Ringwald log_info("Indicator %d: %s (", hfp_connection->ag_indicators_nr+1, hfp_connection->line_buffer); 1169aa4dd815SMatthias Ringwald break; 1170aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 1171a0ffb263SMatthias Ringwald log_info("Parsed Indicator %d with status: %s\n", hfp_connection->parser_item_index+1, hfp_connection->line_buffer); 1172a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = atoi((char *) hfp_connection->line_buffer); 1173a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 11743deb3ec6SMatthias Ringwald break; 11753deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 1176a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1177a0ffb263SMatthias Ringwald if (hfp_connection->parser_item_index != 4) break; 1178a0ffb263SMatthias Ringwald log_info("Parsed Enable indicators: %s\n", hfp_connection->line_buffer); 1179a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1180a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = (uint8_t) value; 11813deb3ec6SMatthias Ringwald break; 11823deb3ec6SMatthias Ringwald case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES: 1183a0ffb263SMatthias Ringwald log_info("Parsed Support call hold: %s\n", hfp_connection->line_buffer); 1184a0ffb263SMatthias Ringwald if (hfp_connection->line_size > 2 ) break; 1185a0ffb263SMatthias Ringwald strcpy((char *)hfp_connection->remote_call_services[hfp_connection->remote_call_services_nr].name, (char *)hfp_connection->line_buffer); 1186a0ffb263SMatthias Ringwald hfp_connection->remote_call_services_nr++; 11873deb3ec6SMatthias Ringwald break; 1188aa4dd815SMatthias Ringwald case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 1189aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 1190a0ffb263SMatthias Ringwald log_info("Parsed Generic status indicator: %s\n", hfp_connection->line_buffer); 1191a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].uuid = (uint16_t)atoi((char*)hfp_connection->line_buffer); 1192a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1193a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators_nr = hfp_connection->parser_item_index; 11943deb3ec6SMatthias Ringwald break; 1195aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 11963deb3ec6SMatthias Ringwald // HF parses inital AG gen. ind. state 1197a0ffb263SMatthias Ringwald log_info("Parsed List generic status indicator %s state: ", hfp_connection->line_buffer); 1198a0ffb263SMatthias Ringwald hfp_connection->parser_item_index = (uint8_t)atoi((char*)hfp_connection->line_buffer); 11993deb3ec6SMatthias Ringwald break; 1200ce263fc8SMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 1201a0ffb263SMatthias Ringwald hfp_connection->parser_indicator_index = (uint8_t)atoi((char*)hfp_connection->line_buffer); 1202a0ffb263SMatthias Ringwald log_info("Parsed HF indicator index %u", hfp_connection->parser_indicator_index); 1203ce263fc8SMatthias Ringwald break; 12043deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE: 12053deb3ec6SMatthias Ringwald // AG parses new gen. ind. state 1206a0ffb263SMatthias Ringwald if (hfp_connection->ignore_value){ 1207a0ffb263SMatthias Ringwald hfp_connection->ignore_value = 0; 1208a0ffb263SMatthias Ringwald log_info("Parsed Enable AG indicator pos %u('%s') - unchanged (stays %u)\n", hfp_connection->parser_item_index, 1209a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled); 1210ce263fc8SMatthias Ringwald } 1211a0ffb263SMatthias Ringwald else if (hfp_connection->ag_indicators[hfp_connection->parser_item_index].mandatory){ 1212ce263fc8SMatthias Ringwald log_info("Parsed Enable AG indicator pos %u('%s') - ignore (mandatory)\n", 1213a0ffb263SMatthias Ringwald hfp_connection->parser_item_index, hfp_connection->ag_indicators[hfp_connection->parser_item_index].name); 1214ce263fc8SMatthias Ringwald } else { 1215a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1216a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled = value; 1217a0ffb263SMatthias Ringwald log_info("Parsed Enable AG indicator pos %u('%s'): %u\n", hfp_connection->parser_item_index, 1218a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, value); 12193deb3ec6SMatthias Ringwald } 1220a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 12213deb3ec6SMatthias Ringwald break; 12223deb3ec6SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 12233deb3ec6SMatthias Ringwald // indicators are indexed starting with 1 1224a0ffb263SMatthias Ringwald hfp_connection->parser_item_index = atoi((char *)&hfp_connection->line_buffer[0]) - 1; 1225a0ffb263SMatthias Ringwald log_info("Parsed status of the AG indicator %d, status ", hfp_connection->parser_item_index); 12263deb3ec6SMatthias Ringwald break; 1227aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1228a0ffb263SMatthias Ringwald hfp_connection->network_operator.mode = atoi((char *)&hfp_connection->line_buffer[0]); 1229a0ffb263SMatthias Ringwald log_info("Parsed network operator mode: %d, ", hfp_connection->network_operator.mode); 1230aa4dd815SMatthias Ringwald break; 1231aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 1232a0ffb263SMatthias Ringwald if (hfp_connection->line_buffer[0] == '3'){ 1233a0ffb263SMatthias Ringwald log_info("Parsed Set network operator format : %s, ", hfp_connection->line_buffer); 12343deb3ec6SMatthias Ringwald break; 12353deb3ec6SMatthias Ringwald } 12363deb3ec6SMatthias Ringwald // TODO emit ERROR, wrong format 1237a0ffb263SMatthias Ringwald log_info("ERROR Set network operator format: index %s not supported\n", hfp_connection->line_buffer); 12383deb3ec6SMatthias Ringwald break; 12393deb3ec6SMatthias Ringwald case HFP_CMD_ERROR: 12403deb3ec6SMatthias Ringwald break; 12413deb3ec6SMatthias Ringwald case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1242a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 1; 1243a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error_value = (uint8_t)atoi((char*)hfp_connection->line_buffer); 12443deb3ec6SMatthias Ringwald break; 12453deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR: 1246a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = (uint8_t)atoi((char*)hfp_connection->line_buffer); 1247a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1248a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 12493deb3ec6SMatthias Ringwald break; 1250ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 1251a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1252a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1253a0ffb263SMatthias Ringwald strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number)); 1254a0ffb263SMatthias Ringwald hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0; 12553deb3ec6SMatthias Ringwald break; 12563deb3ec6SMatthias Ringwald default: 12573deb3ec6SMatthias Ringwald break; 12583deb3ec6SMatthias Ringwald } 12593deb3ec6SMatthias Ringwald } 12603deb3ec6SMatthias Ringwald 12613deb3ec6SMatthias Ringwald void hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid){ 1262a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = provide_hfp_connection_context_for_bd_addr(bd_addr); 1263a0ffb263SMatthias Ringwald log_info("hfp_connect %s, hfp_connection %p", bd_addr_to_str(bd_addr), hfp_connection); 12643deb3ec6SMatthias Ringwald 1265a0ffb263SMatthias Ringwald if (!hfp_connection) { 12663deb3ec6SMatthias Ringwald log_error("hfp_establish_service_level_connection for addr %s failed", bd_addr_to_str(bd_addr)); 12673deb3ec6SMatthias Ringwald return; 12683deb3ec6SMatthias Ringwald } 1269aa4dd815SMatthias Ringwald 1270a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 12713deb3ec6SMatthias Ringwald case HFP_W2_DISCONNECT_RFCOMM: 1272a0ffb263SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 12733deb3ec6SMatthias Ringwald return; 12743deb3ec6SMatthias Ringwald case HFP_W4_RFCOMM_DISCONNECTED: 1275a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART; 12763deb3ec6SMatthias Ringwald return; 12773deb3ec6SMatthias Ringwald case HFP_IDLE: 1278a0ffb263SMatthias Ringwald memcpy(hfp_connection->remote_addr, bd_addr, 6); 1279d63c37a1SMatthias Ringwald hfp_connection->state = HFP_W4_SDP_QUERY_COMPLETE; 1280a0ffb263SMatthias Ringwald connection_doing_sdp_query = hfp_connection; 1281a0ffb263SMatthias Ringwald hfp_connection->service_uuid = service_uuid; 12827fbb5f59SMatthias Ringwald sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, hfp_connection->remote_addr, service_uuid); 12833deb3ec6SMatthias Ringwald break; 12843deb3ec6SMatthias Ringwald default: 12853deb3ec6SMatthias Ringwald break; 12863deb3ec6SMatthias Ringwald } 12873deb3ec6SMatthias Ringwald } 12883deb3ec6SMatthias Ringwald 1289a0ffb263SMatthias Ringwald void hfp_release_service_level_connection(hfp_connection_t * hfp_connection){ 1290a0ffb263SMatthias Ringwald if (!hfp_connection) return; 1291a0ffb263SMatthias Ringwald hfp_release_audio_connection(hfp_connection); 12923deb3ec6SMatthias Ringwald 1293a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_W4_RFCOMM_CONNECTED){ 1294a0ffb263SMatthias Ringwald hfp_connection->state = HFP_IDLE; 12953deb3ec6SMatthias Ringwald return; 12963deb3ec6SMatthias Ringwald } 12973deb3ec6SMatthias Ringwald 1298a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_W4_RFCOMM_CONNECTED){ 1299a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN; 13003deb3ec6SMatthias Ringwald return; 13013deb3ec6SMatthias Ringwald } 13023deb3ec6SMatthias Ringwald 1303a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_W4_SCO_CONNECTED){ 1304a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM; 13053deb3ec6SMatthias Ringwald return; 13063deb3ec6SMatthias Ringwald } 13073deb3ec6SMatthias Ringwald 1308a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_W4_SCO_DISCONNECTED){ 1309a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W2_DISCONNECT_SCO; 13103deb3ec6SMatthias Ringwald return; 13113deb3ec6SMatthias Ringwald } 13123deb3ec6SMatthias Ringwald 13133deb3ec6SMatthias Ringwald return; 13143deb3ec6SMatthias Ringwald } 13153deb3ec6SMatthias Ringwald 1316a0ffb263SMatthias Ringwald void hfp_release_audio_connection(hfp_connection_t * hfp_connection){ 1317a0ffb263SMatthias Ringwald if (!hfp_connection) return; 1318a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return; 1319a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 1; 13203deb3ec6SMatthias Ringwald } 13213deb3ec6SMatthias Ringwald 1322ce263fc8SMatthias Ringwald static const struct link_settings { 1323ce263fc8SMatthias Ringwald const uint16_t max_latency; 1324ce263fc8SMatthias Ringwald const uint8_t retransmission_effort; 1325ce263fc8SMatthias Ringwald const uint16_t packet_types; 1326ce263fc8SMatthias Ringwald } hfp_link_settings [] = { 1327ce263fc8SMatthias Ringwald { 0xffff, 0xff, 0x03c1 }, // HFP_LINK_SETTINGS_D0, HV1 1328ce263fc8SMatthias Ringwald { 0xffff, 0xff, 0x03c4 }, // HFP_LINK_SETTINGS_D1, HV3 1329ce263fc8SMatthias Ringwald { 0x0007, 0x01, 0x03c8 }, // HFP_LINK_SETTINGS_S1, EV3 1330ce263fc8SMatthias Ringwald { 0x0007, 0x01, 0x0380 }, // HFP_LINK_SETTINGS_S2, 2-EV3 1331ce263fc8SMatthias Ringwald { 0x000a, 0x01, 0x0380 }, // HFP_LINK_SETTINGS_S3, 2-EV3 1332ce263fc8SMatthias Ringwald { 0x000c, 0x02, 0x0380 }, // HFP_LINK_SETTINGS_S4, 2-EV3 1333ce263fc8SMatthias Ringwald { 0x0008, 0x02, 0x03c8 }, // HFP_LINK_SETTINGS_T1, EV3 1334ce263fc8SMatthias Ringwald { 0x000d, 0x02, 0x0380 } // HFP_LINK_SETTINGS_T2, 2-EV3 1335ce263fc8SMatthias Ringwald }; 13363deb3ec6SMatthias Ringwald 1337ce263fc8SMatthias Ringwald void hfp_setup_synchronous_connection(hci_con_handle_t handle, hfp_link_setttings_t setting){ 1338ce263fc8SMatthias Ringwald // all packet types, fixed bandwidth 1339ce263fc8SMatthias Ringwald log_info("hfp_setup_synchronous_connection using setting nr %u", setting); 1340ce263fc8SMatthias Ringwald hci_send_cmd(&hci_setup_synchronous_connection, handle, 8000, 8000, hfp_link_settings[setting].max_latency, 1341ce263fc8SMatthias Ringwald hci_get_sco_voice_setting(), hfp_link_settings[setting].retransmission_effort, hfp_link_settings[setting].packet_types); // all types 0x003f, only 2-ev3 0x380 1342ce263fc8SMatthias Ringwald } 1343