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 132*df327ff3SMilanka Ringwald int hfp_supports_codec(uint8_t codec, int codecs_nr, uint16_t * codecs){ 133*df327ff3SMilanka Ringwald int i; 134*df327ff3SMilanka Ringwald for (i = 0; i < codecs_nr; i++){ 135*df327ff3SMilanka Ringwald if (codecs[i] == codec) return 1; 136*df327ff3SMilanka Ringwald } 137*df327ff3SMilanka Ringwald return 0; 138*df327ff3SMilanka Ringwald } 139*df327ff3SMilanka Ringwald 1403deb3ec6SMatthias Ringwald #if 0 141a0ffb263SMatthias Ringwald void hfp_set_codec(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){ 1423deb3ec6SMatthias Ringwald // parse available codecs 1433deb3ec6SMatthias Ringwald int pos = 0; 1443deb3ec6SMatthias Ringwald int i; 1453deb3ec6SMatthias Ringwald for (i=0; i<size; i++){ 1463deb3ec6SMatthias Ringwald pos+=8; 147a0ffb263SMatthias Ringwald if (packet[pos] > hfp_connection->negotiated_codec){ 148a0ffb263SMatthias Ringwald hfp_connection->negotiated_codec = packet[pos]; 1493deb3ec6SMatthias Ringwald } 1503deb3ec6SMatthias Ringwald } 151a0ffb263SMatthias Ringwald printf("Negotiated Codec 0x%02x\n", hfp_connection->negotiated_codec); 1523deb3ec6SMatthias Ringwald } 1533deb3ec6SMatthias Ringwald #endif 1543deb3ec6SMatthias Ringwald 1553deb3ec6SMatthias Ringwald // UTILS 1563deb3ec6SMatthias Ringwald int get_bit(uint16_t bitmap, int position){ 1573deb3ec6SMatthias Ringwald return (bitmap >> position) & 1; 1583deb3ec6SMatthias Ringwald } 1593deb3ec6SMatthias Ringwald 1603deb3ec6SMatthias Ringwald int store_bit(uint32_t bitmap, int position, uint8_t value){ 1613deb3ec6SMatthias Ringwald if (value){ 1623deb3ec6SMatthias Ringwald bitmap |= 1 << position; 1633deb3ec6SMatthias Ringwald } else { 1643deb3ec6SMatthias Ringwald bitmap &= ~ (1 << position); 1653deb3ec6SMatthias Ringwald } 1663deb3ec6SMatthias Ringwald return bitmap; 1673deb3ec6SMatthias Ringwald } 1683deb3ec6SMatthias Ringwald 1693deb3ec6SMatthias Ringwald int join(char * buffer, int buffer_size, uint8_t * values, int values_nr){ 1703deb3ec6SMatthias Ringwald if (buffer_size < values_nr * 3) return 0; 1713deb3ec6SMatthias Ringwald int i; 1723deb3ec6SMatthias Ringwald int offset = 0; 1733deb3ec6SMatthias Ringwald for (i = 0; i < values_nr-1; i++) { 1743deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", values[i]); // puts string into buffer 1753deb3ec6SMatthias Ringwald } 1763deb3ec6SMatthias Ringwald if (i<values_nr){ 1773deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d", values[i]); 1783deb3ec6SMatthias Ringwald } 1793deb3ec6SMatthias Ringwald return offset; 1803deb3ec6SMatthias Ringwald } 1813deb3ec6SMatthias Ringwald 1823deb3ec6SMatthias Ringwald int join_bitmap(char * buffer, int buffer_size, uint32_t values, int values_nr){ 1833deb3ec6SMatthias Ringwald if (buffer_size < values_nr * 3) return 0; 1843deb3ec6SMatthias Ringwald 1853deb3ec6SMatthias Ringwald int i; 1863deb3ec6SMatthias Ringwald int offset = 0; 1873deb3ec6SMatthias Ringwald for (i = 0; i < values_nr-1; i++) { 1883deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d,", get_bit(values,i)); // puts string into buffer 1893deb3ec6SMatthias Ringwald } 1903deb3ec6SMatthias Ringwald 1913deb3ec6SMatthias Ringwald if (i<values_nr){ 1923deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, buffer_size-offset, "%d", get_bit(values,i)); 1933deb3ec6SMatthias Ringwald } 1943deb3ec6SMatthias Ringwald return offset; 1953deb3ec6SMatthias Ringwald } 1963deb3ec6SMatthias Ringwald 19713839019SMatthias Ringwald void hfp_emit_simple_event(btstack_packet_handler_t callback, uint8_t event_subtype){ 198a0ffb263SMatthias Ringwald if (!callback) return; 199a0ffb263SMatthias Ringwald uint8_t event[3]; 200a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 201a0ffb263SMatthias Ringwald event[1] = sizeof(event) - 2; 202a0ffb263SMatthias Ringwald event[2] = event_subtype; 20313839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 204a0ffb263SMatthias Ringwald } 205a0ffb263SMatthias Ringwald 20613839019SMatthias Ringwald void hfp_emit_event(btstack_packet_handler_t callback, uint8_t event_subtype, uint8_t value){ 2073deb3ec6SMatthias Ringwald if (!callback) return; 2083deb3ec6SMatthias Ringwald uint8_t event[4]; 2093deb3ec6SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 2103deb3ec6SMatthias Ringwald event[1] = sizeof(event) - 2; 2113deb3ec6SMatthias Ringwald event[2] = event_subtype; 2123deb3ec6SMatthias Ringwald event[3] = value; // status 0 == OK 21313839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 2143deb3ec6SMatthias Ringwald } 2153deb3ec6SMatthias Ringwald 21613839019SMatthias Ringwald void hfp_emit_connection_event(btstack_packet_handler_t callback, uint8_t event_subtype, uint8_t status, hci_con_handle_t con_handle){ 217a0653c3bSMilanka Ringwald if (!callback) return; 218a0653c3bSMilanka Ringwald uint8_t event[6]; 219a0653c3bSMilanka Ringwald event[0] = HCI_EVENT_HFP_META; 220a0653c3bSMilanka Ringwald event[1] = sizeof(event) - 2; 221a0653c3bSMilanka Ringwald event[2] = event_subtype; 222a0653c3bSMilanka Ringwald event[3] = status; // status 0 == OK 223a0653c3bSMilanka Ringwald little_endian_store_16(event, 4, con_handle); 22413839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 225a0653c3bSMilanka Ringwald } 226a0653c3bSMilanka Ringwald 22713839019SMatthias Ringwald void hfp_emit_string_event(btstack_packet_handler_t callback, uint8_t event_subtype, const char * value){ 228aa4dd815SMatthias Ringwald if (!callback) return; 229c1797c7dSMatthias Ringwald uint8_t event[40]; 230aa4dd815SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 231aa4dd815SMatthias Ringwald event[1] = sizeof(event) - 2; 232aa4dd815SMatthias Ringwald event[2] = event_subtype; 233aa4dd815SMatthias Ringwald int size = (strlen(value) < sizeof(event) - 4) ? strlen(value) : sizeof(event) - 4; 234aa4dd815SMatthias Ringwald strncpy((char*)&event[3], value, size); 235aa4dd815SMatthias Ringwald event[3 + size] = 0; 23613839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 237aa4dd815SMatthias Ringwald } 238aa4dd815SMatthias Ringwald 2390cb5b971SMatthias Ringwald btstack_linked_list_t * hfp_get_connections(void){ 2408f2a52f4SMatthias Ringwald return (btstack_linked_list_t *) &hfp_connections; 2413deb3ec6SMatthias Ringwald } 2423deb3ec6SMatthias Ringwald 2433deb3ec6SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid){ 244665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 245665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 246665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 247a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 248a0ffb263SMatthias Ringwald if (hfp_connection->rfcomm_cid == cid){ 249a0ffb263SMatthias Ringwald return hfp_connection; 2503deb3ec6SMatthias Ringwald } 2513deb3ec6SMatthias Ringwald } 2523deb3ec6SMatthias Ringwald return NULL; 2533deb3ec6SMatthias Ringwald } 2543deb3ec6SMatthias Ringwald 2553deb3ec6SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr){ 256665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 257665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 258665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 259a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 260a0ffb263SMatthias Ringwald if (memcmp(hfp_connection->remote_addr, bd_addr, 6) == 0) { 261a0ffb263SMatthias Ringwald return hfp_connection; 2623deb3ec6SMatthias Ringwald } 2633deb3ec6SMatthias Ringwald } 2643deb3ec6SMatthias Ringwald return NULL; 2653deb3ec6SMatthias Ringwald } 2663deb3ec6SMatthias Ringwald 267aa4dd815SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_sco_handle(uint16_t handle){ 268665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 269665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 270665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 271a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 272a0ffb263SMatthias Ringwald if (hfp_connection->sco_handle == handle){ 273a0ffb263SMatthias Ringwald return hfp_connection; 2743deb3ec6SMatthias Ringwald } 2753deb3ec6SMatthias Ringwald } 2763deb3ec6SMatthias Ringwald return NULL; 2773deb3ec6SMatthias Ringwald } 2783deb3ec6SMatthias Ringwald 279a0ffb263SMatthias Ringwald void hfp_reset_context_flags(hfp_connection_t * hfp_connection){ 280a0ffb263SMatthias Ringwald if (!hfp_connection) return; 281a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 282a0ffb263SMatthias Ringwald hfp_connection->send_error = 0; 2833deb3ec6SMatthias Ringwald 284a0ffb263SMatthias Ringwald hfp_connection->keep_byte = 0; 2853deb3ec6SMatthias Ringwald 286a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 0; 287a0ffb263SMatthias Ringwald hfp_connection->operator_name_changed = 0; 2883deb3ec6SMatthias Ringwald 289a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = 0; 290a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 2913deb3ec6SMatthias Ringwald 292a0ffb263SMatthias Ringwald // establish codecs hfp_connection 293a0ffb263SMatthias Ringwald hfp_connection->suggested_codec = 0; 294a0ffb263SMatthias Ringwald hfp_connection->negotiated_codec = 0; 295a0ffb263SMatthias Ringwald hfp_connection->codec_confirmed = 0; 2963deb3ec6SMatthias Ringwald 297a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 298a0ffb263SMatthias Ringwald hfp_connection->call_waiting_notification_enabled = 0; 299a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 300a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 3013deb3ec6SMatthias Ringwald } 3023deb3ec6SMatthias Ringwald 303fffdd288SMatthias Ringwald static hfp_connection_t * create_hfp_connection_context(void){ 304a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = btstack_memory_hfp_connection_get(); 305a0ffb263SMatthias Ringwald if (!hfp_connection) return NULL; 3063deb3ec6SMatthias Ringwald // init state 307a0ffb263SMatthias Ringwald memset(hfp_connection,0, sizeof(hfp_connection_t)); 3083deb3ec6SMatthias Ringwald 309a0ffb263SMatthias Ringwald hfp_connection->state = HFP_IDLE; 310a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE; 311a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_IDLE; 312aa4dd815SMatthias Ringwald 313a0ffb263SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 314a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 3153deb3ec6SMatthias Ringwald 316a0ffb263SMatthias Ringwald hfp_reset_context_flags(hfp_connection); 3173deb3ec6SMatthias Ringwald 318d63c37a1SMatthias Ringwald btstack_linked_list_add(&hfp_connections, (btstack_linked_item_t*)hfp_connection); 319a0ffb263SMatthias Ringwald return hfp_connection; 3203deb3ec6SMatthias Ringwald } 3213deb3ec6SMatthias Ringwald 322a0ffb263SMatthias Ringwald static void remove_hfp_connection_context(hfp_connection_t * hfp_connection){ 323a0ffb263SMatthias Ringwald btstack_linked_list_remove(&hfp_connections, (btstack_linked_item_t*) hfp_connection); 3243deb3ec6SMatthias Ringwald } 3253deb3ec6SMatthias Ringwald 3263deb3ec6SMatthias Ringwald static hfp_connection_t * provide_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr){ 327a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 328a0ffb263SMatthias Ringwald if (hfp_connection) return hfp_connection; 329a0ffb263SMatthias Ringwald hfp_connection = create_hfp_connection_context(); 330a0ffb263SMatthias Ringwald printf("created hfp_connection for address %s\n", bd_addr_to_str(bd_addr)); 331a0ffb263SMatthias Ringwald memcpy(hfp_connection->remote_addr, bd_addr, 6); 332a0ffb263SMatthias Ringwald return hfp_connection; 3333deb3ec6SMatthias Ringwald } 3343deb3ec6SMatthias Ringwald 335aa4dd815SMatthias Ringwald /* @param network. 336aa4dd815SMatthias Ringwald * 0 == no ability to reject a call. 337aa4dd815SMatthias Ringwald * 1 == ability to reject a call. 338aa4dd815SMatthias Ringwald */ 3393deb3ec6SMatthias Ringwald 3403deb3ec6SMatthias Ringwald /* @param suported_features 3413deb3ec6SMatthias Ringwald * HF bit 0: EC and/or NR function (yes/no, 1 = yes, 0 = no) 3423deb3ec6SMatthias Ringwald * HF bit 1: Call waiting or three-way calling(yes/no, 1 = yes, 0 = no) 3433deb3ec6SMatthias Ringwald * HF bit 2: CLI presentation capability (yes/no, 1 = yes, 0 = no) 3443deb3ec6SMatthias Ringwald * HF bit 3: Voice recognition activation (yes/no, 1= yes, 0 = no) 3453deb3ec6SMatthias Ringwald * HF bit 4: Remote volume control (yes/no, 1 = yes, 0 = no) 3463deb3ec6SMatthias Ringwald * HF bit 5: Wide band speech (yes/no, 1 = yes, 0 = no) 3473deb3ec6SMatthias Ringwald */ 3483deb3ec6SMatthias Ringwald /* Bit position: 3493deb3ec6SMatthias Ringwald * AG bit 0: Three-way calling (yes/no, 1 = yes, 0 = no) 3503deb3ec6SMatthias Ringwald * AG bit 1: EC and/or NR function (yes/no, 1 = yes, 0 = no) 3513deb3ec6SMatthias Ringwald * AG bit 2: Voice recognition function (yes/no, 1 = yes, 0 = no) 3523deb3ec6SMatthias Ringwald * AG bit 3: In-band ring tone capability (yes/no, 1 = yes, 0 = no) 3533deb3ec6SMatthias Ringwald * AG bit 4: Attach a phone number to a voice tag (yes/no, 1 = yes, 0 = no) 3543deb3ec6SMatthias Ringwald * AG bit 5: Wide band speech (yes/no, 1 = yes, 0 = no) 3553deb3ec6SMatthias Ringwald */ 3563deb3ec6SMatthias Ringwald 3579b1c3b4dSMatthias 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){ 3583deb3ec6SMatthias Ringwald uint8_t* attribute; 3593deb3ec6SMatthias Ringwald de_create_sequence(service); 3603deb3ec6SMatthias Ringwald 3613deb3ec6SMatthias Ringwald // 0x0000 "Service Record Handle" 3623deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ServiceRecordHandle); 3639b1c3b4dSMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle); 3643deb3ec6SMatthias Ringwald 3653deb3ec6SMatthias Ringwald // 0x0001 "Service Class ID List" 3663deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ServiceClassIDList); 3673deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 3683deb3ec6SMatthias Ringwald { 3693deb3ec6SMatthias Ringwald // "UUID for Service" 3703deb3ec6SMatthias Ringwald de_add_number(attribute, DE_UUID, DE_SIZE_16, service_uuid); 3713deb3ec6SMatthias Ringwald de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_GenericAudio); 3723deb3ec6SMatthias Ringwald } 3733deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 3743deb3ec6SMatthias Ringwald 3753deb3ec6SMatthias Ringwald // 0x0004 "Protocol Descriptor List" 3763deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ProtocolDescriptorList); 3773deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 3783deb3ec6SMatthias Ringwald { 3793deb3ec6SMatthias Ringwald uint8_t* l2cpProtocol = de_push_sequence(attribute); 3803deb3ec6SMatthias Ringwald { 3813deb3ec6SMatthias Ringwald de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, SDP_L2CAPProtocol); 3823deb3ec6SMatthias Ringwald } 3833deb3ec6SMatthias Ringwald de_pop_sequence(attribute, l2cpProtocol); 3843deb3ec6SMatthias Ringwald 3853deb3ec6SMatthias Ringwald uint8_t* rfcomm = de_push_sequence(attribute); 3863deb3ec6SMatthias Ringwald { 3873deb3ec6SMatthias Ringwald de_add_number(rfcomm, DE_UUID, DE_SIZE_16, SDP_RFCOMMProtocol); // rfcomm_service 3883deb3ec6SMatthias Ringwald de_add_number(rfcomm, DE_UINT, DE_SIZE_8, rfcomm_channel_nr); // rfcomm channel 3893deb3ec6SMatthias Ringwald } 3903deb3ec6SMatthias Ringwald de_pop_sequence(attribute, rfcomm); 3913deb3ec6SMatthias Ringwald } 3923deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 3933deb3ec6SMatthias Ringwald 3943deb3ec6SMatthias Ringwald 3953deb3ec6SMatthias Ringwald // 0x0005 "Public Browse Group" 3963deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, SDP_BrowseGroupList); // public browse group 3973deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 3983deb3ec6SMatthias Ringwald { 3993deb3ec6SMatthias Ringwald de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_PublicBrowseGroup); 4003deb3ec6SMatthias Ringwald } 4013deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 4023deb3ec6SMatthias Ringwald 4033deb3ec6SMatthias Ringwald // 0x0009 "Bluetooth Profile Descriptor List" 4043deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, SDP_BluetoothProfileDescriptorList); 4053deb3ec6SMatthias Ringwald attribute = de_push_sequence(service); 4063deb3ec6SMatthias Ringwald { 4073deb3ec6SMatthias Ringwald uint8_t *sppProfile = de_push_sequence(attribute); 4083deb3ec6SMatthias Ringwald { 4093deb3ec6SMatthias Ringwald de_add_number(sppProfile, DE_UUID, DE_SIZE_16, SDP_Handsfree); 4103deb3ec6SMatthias Ringwald de_add_number(sppProfile, DE_UINT, DE_SIZE_16, 0x0107); // Verision 1.7 4113deb3ec6SMatthias Ringwald } 4123deb3ec6SMatthias Ringwald de_pop_sequence(attribute, sppProfile); 4133deb3ec6SMatthias Ringwald } 4143deb3ec6SMatthias Ringwald de_pop_sequence(service, attribute); 4153deb3ec6SMatthias Ringwald 4163deb3ec6SMatthias Ringwald // 0x0100 "Service Name" 4173deb3ec6SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); 4183deb3ec6SMatthias Ringwald de_add_data(service, DE_STRING, strlen(name), (uint8_t *) name); 4193deb3ec6SMatthias Ringwald } 4203deb3ec6SMatthias Ringwald 4213deb3ec6SMatthias Ringwald static hfp_connection_t * connection_doing_sdp_query = NULL; 422a8747467SMatthias Ringwald 4236c927b22SMatthias Ringwald static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 424a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = connection_doing_sdp_query; 4253deb3ec6SMatthias Ringwald 426d63c37a1SMatthias Ringwald if ( hfp_connection->state != HFP_W4_SDP_QUERY_COMPLETE) return; 4273deb3ec6SMatthias Ringwald 4280e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 4295611a760SMatthias Ringwald case SDP_EVENT_QUERY_RFCOMM_SERVICE: 430a0ffb263SMatthias Ringwald if (!hfp_connection) { 43172b50801SMatthias Ringwald log_error("handle_query_rfcomm_event alloc connection for RFCOMM port %u failed", sdp_event_query_rfcomm_service_get_rfcomm_channel(packet)); 4323deb3ec6SMatthias Ringwald return; 4333deb3ec6SMatthias Ringwald } 434a0ffb263SMatthias Ringwald hfp_connection->rfcomm_channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet); 4353deb3ec6SMatthias Ringwald break; 4365611a760SMatthias Ringwald case SDP_EVENT_QUERY_COMPLETE: 4373deb3ec6SMatthias Ringwald connection_doing_sdp_query = NULL; 438a0ffb263SMatthias Ringwald if (hfp_connection->rfcomm_channel_nr > 0){ 439a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_CONNECTED; 440a0ffb263SMatthias 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); 441d68dcce1SMatthias Ringwald rfcomm_create_channel(rfcomm_packet_handler, hfp_connection->remote_addr, hfp_connection->rfcomm_channel_nr, NULL); 4423deb3ec6SMatthias Ringwald break; 4433deb3ec6SMatthias Ringwald } 44472b50801SMatthias Ringwald log_info("rfcomm service not found, status %u.", sdp_event_query_complete_get_status(packet)); 4453deb3ec6SMatthias Ringwald break; 4463deb3ec6SMatthias Ringwald default: 4473deb3ec6SMatthias Ringwald break; 4483deb3ec6SMatthias Ringwald } 4493deb3ec6SMatthias Ringwald } 4503deb3ec6SMatthias Ringwald 451eddcd308SMatthias Ringwald static void hfp_handle_failed_sco_connection(uint8_t status){ 452eddcd308SMatthias Ringwald 453eddcd308SMatthias Ringwald if (!sco_establishment_active){ 454eddcd308SMatthias Ringwald log_error("(e)SCO Connection failed but not started by us"); 455eddcd308SMatthias Ringwald return; 456eddcd308SMatthias Ringwald } 4576c5b2002SMatthias Ringwald log_error("(e)SCO Connection failed status 0x%02x", status); 458eddcd308SMatthias Ringwald 459eddcd308SMatthias Ringwald // invalid params / unspecified error 460eddcd308SMatthias Ringwald if (status != 0x11 && status != 0x1f) return; 461eddcd308SMatthias Ringwald 462eddcd308SMatthias Ringwald switch (sco_establishment_active->link_setting){ 463eddcd308SMatthias Ringwald case HFP_LINK_SETTINGS_D0: 464eddcd308SMatthias Ringwald return; // no other option left 465eddcd308SMatthias Ringwald case HFP_LINK_SETTINGS_D1: 466eddcd308SMatthias Ringwald sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D0; 467eddcd308SMatthias Ringwald break; 468eddcd308SMatthias Ringwald case HFP_LINK_SETTINGS_S1: 469eddcd308SMatthias Ringwald sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D1; 470eddcd308SMatthias Ringwald break; 471eddcd308SMatthias Ringwald case HFP_LINK_SETTINGS_S2: 472eddcd308SMatthias Ringwald case HFP_LINK_SETTINGS_S3: 473eddcd308SMatthias Ringwald case HFP_LINK_SETTINGS_S4: 474eddcd308SMatthias Ringwald sco_establishment_active->link_setting = HFP_LINK_SETTINGS_S1; 475eddcd308SMatthias Ringwald break; 476eddcd308SMatthias Ringwald case HFP_LINK_SETTINGS_T1: 477eddcd308SMatthias Ringwald case HFP_LINK_SETTINGS_T2: 478eddcd308SMatthias Ringwald sco_establishment_active->link_setting = HFP_LINK_SETTINGS_S3; 479eddcd308SMatthias Ringwald break; 480eddcd308SMatthias Ringwald } 481eddcd308SMatthias Ringwald sco_establishment_active->establish_audio_connection = 1; 482eddcd308SMatthias Ringwald sco_establishment_active = 0; 483eddcd308SMatthias Ringwald } 484eddcd308SMatthias Ringwald 485eddcd308SMatthias Ringwald 486e30a6a47SMatthias Ringwald void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 4873deb3ec6SMatthias Ringwald bd_addr_t event_addr; 4883deb3ec6SMatthias Ringwald uint16_t rfcomm_cid, handle; 489a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = NULL; 490674515e8SMatthias Ringwald uint8_t status; 4913deb3ec6SMatthias Ringwald 492eddcd308SMatthias Ringwald log_info("AG packet_handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size); 493aa4dd815SMatthias Ringwald 4940e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 4953deb3ec6SMatthias Ringwald 4963deb3ec6SMatthias Ringwald case RFCOMM_EVENT_INCOMING_CONNECTION: 4973deb3ec6SMatthias Ringwald // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 498caa82391SMilanka Ringwald rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr); 499d63c37a1SMatthias Ringwald hfp_connection = provide_hfp_connection_context_for_bd_addr(event_addr); 500d63c37a1SMatthias Ringwald if (!hfp_connection || hfp_connection->state != HFP_IDLE) return; 5013deb3ec6SMatthias Ringwald 502caa82391SMilanka Ringwald hfp_connection->rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet); 503d63c37a1SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_CONNECTED; 504674515e8SMatthias Ringwald // printf("RFCOMM channel %u requested for %s\n", hfp_connection->rfcomm_cid, bd_addr_to_str(hfp_connection->remote_addr)); 505d63c37a1SMatthias Ringwald rfcomm_accept_connection(hfp_connection->rfcomm_cid); 5063deb3ec6SMatthias Ringwald break; 5073deb3ec6SMatthias Ringwald 508f8f6a918SMatthias Ringwald case RFCOMM_EVENT_CHANNEL_OPENED: 5093deb3ec6SMatthias Ringwald // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16) 510aa4dd815SMatthias Ringwald 511674515e8SMatthias Ringwald rfcomm_event_channel_opened_get_bd_addr(packet, event_addr); 512674515e8SMatthias Ringwald status = rfcomm_event_channel_opened_get_status(packet); 513674515e8SMatthias Ringwald // printf("RFCOMM_EVENT_CHANNEL_OPENED packet_handler adddr %s, status %u\n", bd_addr_to_str(event_addr), status); 514674515e8SMatthias Ringwald 515d63c37a1SMatthias Ringwald hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr); 516d63c37a1SMatthias Ringwald if (!hfp_connection || hfp_connection->state != HFP_W4_RFCOMM_CONNECTED) return; 5173deb3ec6SMatthias Ringwald 518674515e8SMatthias Ringwald if (status) { 519a0653c3bSMilanka Ringwald hfp_emit_connection_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED, status, rfcomm_event_channel_opened_get_con_handle(packet)); 520d63c37a1SMatthias Ringwald remove_hfp_connection_context(hfp_connection); 5213deb3ec6SMatthias Ringwald } else { 522caa82391SMilanka Ringwald hfp_connection->acl_handle = rfcomm_event_channel_opened_get_con_handle(packet); 523caa82391SMilanka Ringwald hfp_connection->rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet); 524674515e8SMatthias Ringwald // uint16_t mtu = rfcomm_event_channel_opened_get_max_frame_size(packet); 525674515e8SMatthias 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); 5263deb3ec6SMatthias Ringwald 527d63c37a1SMatthias Ringwald switch (hfp_connection->state){ 5283deb3ec6SMatthias Ringwald case HFP_W4_RFCOMM_CONNECTED: 529d63c37a1SMatthias Ringwald hfp_connection->state = HFP_EXCHANGE_SUPPORTED_FEATURES; 5303deb3ec6SMatthias Ringwald break; 5313deb3ec6SMatthias Ringwald case HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN: 532d63c37a1SMatthias Ringwald hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM; 533674515e8SMatthias Ringwald // printf("Shutting down RFCOMM.\n"); 5343deb3ec6SMatthias Ringwald break; 5353deb3ec6SMatthias Ringwald default: 5363deb3ec6SMatthias Ringwald break; 5373deb3ec6SMatthias Ringwald } 538bb6944afSMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 5393deb3ec6SMatthias Ringwald } 5403deb3ec6SMatthias Ringwald break; 5413deb3ec6SMatthias Ringwald 542eddcd308SMatthias Ringwald case HCI_EVENT_COMMAND_STATUS: 543eddcd308SMatthias Ringwald if (hci_event_command_status_get_command_opcode(packet) == hci_setup_synchronous_connection.opcode) { 544be2a1a78SMatthias Ringwald status = hci_event_command_status_get_status(packet); 5456c5b2002SMatthias Ringwald if (status) { 546eddcd308SMatthias Ringwald hfp_handle_failed_sco_connection(hci_event_command_status_get_status(packet)); 547eddcd308SMatthias Ringwald } 5486c5b2002SMatthias Ringwald } 549eddcd308SMatthias Ringwald break; 550eddcd308SMatthias Ringwald 5513deb3ec6SMatthias Ringwald case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{ 552ce263fc8SMatthias Ringwald 553724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], event_addr); 5543deb3ec6SMatthias Ringwald int index = 2; 555ba92fcf6SMatthias Ringwald status = packet[index++]; 556aa4dd815SMatthias Ringwald 557aa4dd815SMatthias Ringwald if (status != 0){ 558eddcd308SMatthias Ringwald hfp_handle_failed_sco_connection(status); 559aa4dd815SMatthias Ringwald break; 560aa4dd815SMatthias Ringwald } 561aa4dd815SMatthias Ringwald 562f8fbdce0SMatthias Ringwald uint16_t sco_handle = little_endian_read_16(packet, index); 5633deb3ec6SMatthias Ringwald index+=2; 564aa4dd815SMatthias Ringwald 565724d70a2SMatthias Ringwald reverse_bd_addr(&packet[index], event_addr); 5663deb3ec6SMatthias Ringwald index+=6; 567aa4dd815SMatthias Ringwald 5683deb3ec6SMatthias Ringwald uint8_t link_type = packet[index++]; 5693deb3ec6SMatthias Ringwald uint8_t transmission_interval = packet[index++]; // measured in slots 5703deb3ec6SMatthias Ringwald uint8_t retransmission_interval = packet[index++];// measured in slots 571f8fbdce0SMatthias Ringwald uint16_t rx_packet_length = little_endian_read_16(packet, index); // measured in bytes 5723deb3ec6SMatthias Ringwald index+=2; 573f8fbdce0SMatthias Ringwald uint16_t tx_packet_length = little_endian_read_16(packet, index); // measured in bytes 5743deb3ec6SMatthias Ringwald index+=2; 5753deb3ec6SMatthias Ringwald uint8_t air_mode = packet[index]; 5763deb3ec6SMatthias Ringwald 5773deb3ec6SMatthias Ringwald switch (link_type){ 5783deb3ec6SMatthias Ringwald case 0x00: 579aa4dd815SMatthias Ringwald log_info("SCO Connection established."); 5803deb3ec6SMatthias Ringwald if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval); 5813deb3ec6SMatthias Ringwald if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval); 5823deb3ec6SMatthias Ringwald if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length); 5833deb3ec6SMatthias Ringwald if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length); 5843deb3ec6SMatthias Ringwald break; 5853deb3ec6SMatthias Ringwald case 0x02: 586aa4dd815SMatthias Ringwald log_info("eSCO Connection established. \n"); 5873deb3ec6SMatthias Ringwald break; 5883deb3ec6SMatthias Ringwald default: 5893deb3ec6SMatthias Ringwald log_error("(e)SCO reserved link_type 0x%2x", link_type); 5903deb3ec6SMatthias Ringwald break; 5913deb3ec6SMatthias Ringwald } 5923deb3ec6SMatthias Ringwald log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, " 593aa4dd815SMatthias Ringwald " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)\n", sco_handle, 594aa4dd815SMatthias Ringwald bd_addr_to_str(event_addr), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length, air_mode); 5953deb3ec6SMatthias Ringwald 596d63c37a1SMatthias Ringwald hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr); 5973deb3ec6SMatthias Ringwald 598d63c37a1SMatthias Ringwald if (!hfp_connection) { 599d63c37a1SMatthias Ringwald log_error("SCO link created, hfp_connection for address %s not found.", bd_addr_to_str(event_addr)); 6003deb3ec6SMatthias Ringwald break; 6013deb3ec6SMatthias Ringwald } 6023deb3ec6SMatthias Ringwald 603d63c37a1SMatthias Ringwald if (hfp_connection->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){ 604aa4dd815SMatthias Ringwald log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN"); 605d63c37a1SMatthias Ringwald hfp_connection->state = HFP_W2_DISCONNECT_SCO; 606aa4dd815SMatthias Ringwald break; 607aa4dd815SMatthias Ringwald } 608d63c37a1SMatthias Ringwald hfp_connection->sco_handle = sco_handle; 609d63c37a1SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 610d63c37a1SMatthias Ringwald hfp_connection->state = HFP_AUDIO_CONNECTION_ESTABLISHED; 611a0653c3bSMilanka Ringwald hfp_emit_connection_event(hfp_callback, HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED, packet[2], sco_handle); 6123deb3ec6SMatthias Ringwald break; 6133deb3ec6SMatthias Ringwald } 6143deb3ec6SMatthias Ringwald 6153deb3ec6SMatthias Ringwald case RFCOMM_EVENT_CHANNEL_CLOSED: 616f8fbdce0SMatthias Ringwald rfcomm_cid = little_endian_read_16(packet,2); 617d63c37a1SMatthias Ringwald hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid); 618d63c37a1SMatthias Ringwald if (!hfp_connection) break; 619d63c37a1SMatthias Ringwald if (hfp_connection->state == HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART){ 620d63c37a1SMatthias Ringwald hfp_connection->state = HFP_IDLE; 621d63c37a1SMatthias Ringwald hfp_establish_service_level_connection(hfp_connection->remote_addr, hfp_connection->service_uuid); 6223deb3ec6SMatthias Ringwald break; 6233deb3ec6SMatthias Ringwald } 6243deb3ec6SMatthias Ringwald 625f4000eebSMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0); 626d63c37a1SMatthias Ringwald remove_hfp_connection_context(hfp_connection); 6273deb3ec6SMatthias Ringwald break; 6283deb3ec6SMatthias Ringwald 6293deb3ec6SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 630f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet,3); 631d63c37a1SMatthias Ringwald hfp_connection = get_hfp_connection_context_for_sco_handle(handle); 632aa4dd815SMatthias Ringwald 633d63c37a1SMatthias Ringwald if (!hfp_connection) break; 634aa4dd815SMatthias Ringwald 635d63c37a1SMatthias Ringwald if (hfp_connection->state != HFP_W4_SCO_DISCONNECTED){ 636aa4dd815SMatthias Ringwald log_info("Received gap disconnect in wrong hfp state"); 637aa4dd815SMatthias Ringwald } 638d63c37a1SMatthias Ringwald log_info("Check SCO handle: incoming 0x%02x, hfp_connection 0x%02x\n", handle, hfp_connection->sco_handle); 639aa4dd815SMatthias Ringwald 640d63c37a1SMatthias Ringwald if (handle == hfp_connection->sco_handle){ 641aa4dd815SMatthias Ringwald log_info("SCO disconnected, w2 disconnect RFCOMM\n"); 642d63c37a1SMatthias Ringwald hfp_connection->sco_handle = 0; 643d63c37a1SMatthias Ringwald hfp_connection->release_audio_connection = 0; 644d63c37a1SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 645f4000eebSMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED, 0); 6463deb3ec6SMatthias Ringwald break; 6473deb3ec6SMatthias Ringwald } 6483deb3ec6SMatthias Ringwald break; 6493deb3ec6SMatthias Ringwald 650fc64f94aSMatthias Ringwald default: 6513deb3ec6SMatthias Ringwald break; 6523deb3ec6SMatthias Ringwald } 6533deb3ec6SMatthias Ringwald } 6543deb3ec6SMatthias Ringwald 655aa4dd815SMatthias Ringwald // translates command string into hfp_command_t CMD 656aa4dd815SMatthias Ringwald static hfp_command_t parse_command(const char * line_buffer, int isHandsFree){ 657aa4dd815SMatthias Ringwald int offset = isHandsFree ? 0 : 2; 6583deb3ec6SMatthias Ringwald 659ce263fc8SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_LIST_CURRENT_CALLS, strlen(HFP_LIST_CURRENT_CALLS)) == 0){ 660ce263fc8SMatthias Ringwald return HFP_CMD_LIST_CURRENT_CALLS; 661ce263fc8SMatthias Ringwald } 662ce263fc8SMatthias Ringwald 663ce263fc8SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_SUBSCRIBER_NUMBER_INFORMATION, strlen(HFP_SUBSCRIBER_NUMBER_INFORMATION)) == 0){ 664ce263fc8SMatthias Ringwald return HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION; 665ce263fc8SMatthias Ringwald } 666ce263fc8SMatthias Ringwald 667aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_PHONE_NUMBER_FOR_VOICE_TAG, strlen(HFP_PHONE_NUMBER_FOR_VOICE_TAG)) == 0){ 668ce263fc8SMatthias Ringwald if (isHandsFree) return HFP_CMD_AG_SENT_PHONE_NUMBER; 669aa4dd815SMatthias Ringwald return HFP_CMD_HF_REQUEST_PHONE_NUMBER; 6703deb3ec6SMatthias Ringwald } 6713deb3ec6SMatthias Ringwald 672aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_TRANSMIT_DTMF_CODES, strlen(HFP_TRANSMIT_DTMF_CODES)) == 0){ 673aa4dd815SMatthias Ringwald return HFP_CMD_TRANSMIT_DTMF_CODES; 6743deb3ec6SMatthias Ringwald } 6753deb3ec6SMatthias Ringwald 676aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_SET_MICROPHONE_GAIN, strlen(HFP_SET_MICROPHONE_GAIN)) == 0){ 677aa4dd815SMatthias Ringwald return HFP_CMD_SET_MICROPHONE_GAIN; 6783deb3ec6SMatthias Ringwald } 6793deb3ec6SMatthias Ringwald 680aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_SET_SPEAKER_GAIN, strlen(HFP_SET_SPEAKER_GAIN)) == 0){ 681aa4dd815SMatthias Ringwald return HFP_CMD_SET_SPEAKER_GAIN; 6823deb3ec6SMatthias Ringwald } 6833deb3ec6SMatthias Ringwald 684aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_ACTIVATE_VOICE_RECOGNITION, strlen(HFP_ACTIVATE_VOICE_RECOGNITION)) == 0){ 685aa4dd815SMatthias Ringwald if (isHandsFree) return HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION; 686aa4dd815SMatthias Ringwald return HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION; 6873deb3ec6SMatthias Ringwald } 6883deb3ec6SMatthias Ringwald 689aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_TURN_OFF_EC_AND_NR, strlen(HFP_TURN_OFF_EC_AND_NR)) == 0){ 690aa4dd815SMatthias Ringwald return HFP_CMD_TURN_OFF_EC_AND_NR; 6913deb3ec6SMatthias Ringwald } 6923deb3ec6SMatthias Ringwald 693aa4dd815SMatthias Ringwald if (strncmp(line_buffer, HFP_CALL_ANSWERED, strlen(HFP_CALL_ANSWERED)) == 0){ 694aa4dd815SMatthias Ringwald return HFP_CMD_CALL_ANSWERED; 6953deb3ec6SMatthias Ringwald } 6963deb3ec6SMatthias Ringwald 697aa4dd815SMatthias Ringwald if (strncmp(line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0){ 698aa4dd815SMatthias Ringwald return HFP_CMD_CALL_PHONE_NUMBER; 6993deb3ec6SMatthias Ringwald } 7003deb3ec6SMatthias Ringwald 701ce263fc8SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_REDIAL_LAST_NUMBER, strlen(HFP_REDIAL_LAST_NUMBER)) == 0){ 702aa4dd815SMatthias Ringwald return HFP_CMD_REDIAL_LAST_NUMBER; 7033deb3ec6SMatthias Ringwald } 7043deb3ec6SMatthias Ringwald 705aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_CHANGE_IN_BAND_RING_TONE_SETTING, strlen(HFP_CHANGE_IN_BAND_RING_TONE_SETTING)) == 0){ 706aa4dd815SMatthias Ringwald return HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING; 707aa4dd815SMatthias Ringwald } 708aa4dd815SMatthias Ringwald 709aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_HANG_UP_CALL, strlen(HFP_HANG_UP_CALL)) == 0){ 710aa4dd815SMatthias Ringwald return HFP_CMD_HANG_UP_CALL; 711aa4dd815SMatthias Ringwald } 712aa4dd815SMatthias Ringwald 713aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_ERROR, strlen(HFP_ERROR)) == 0){ 714aa4dd815SMatthias Ringwald return HFP_CMD_ERROR; 715aa4dd815SMatthias Ringwald } 716aa4dd815SMatthias Ringwald 717ce263fc8SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_RING, strlen(HFP_RING)) == 0){ 718ce263fc8SMatthias Ringwald return HFP_CMD_RING; 719ce263fc8SMatthias Ringwald } 720ce263fc8SMatthias Ringwald 721aa4dd815SMatthias Ringwald if (isHandsFree && strncmp(line_buffer+offset, HFP_OK, strlen(HFP_OK)) == 0){ 722aa4dd815SMatthias Ringwald return HFP_CMD_OK; 723aa4dd815SMatthias Ringwald } 724aa4dd815SMatthias Ringwald 725aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_SUPPORTED_FEATURES, strlen(HFP_SUPPORTED_FEATURES)) == 0){ 726aa4dd815SMatthias Ringwald return HFP_CMD_SUPPORTED_FEATURES; 727aa4dd815SMatthias Ringwald } 728aa4dd815SMatthias Ringwald 729ce263fc8SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_TRANSFER_HF_INDICATOR_STATUS, strlen(HFP_TRANSFER_HF_INDICATOR_STATUS)) == 0){ 730ce263fc8SMatthias Ringwald return HFP_CMD_HF_INDICATOR_STATUS; 731ce263fc8SMatthias Ringwald } 732ce263fc8SMatthias Ringwald 733ce263fc8SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_RESPONSE_AND_HOLD, strlen(HFP_RESPONSE_AND_HOLD)) == 0){ 734ce263fc8SMatthias Ringwald if (strncmp(line_buffer+strlen(HFP_RESPONSE_AND_HOLD)+offset, "?", 1) == 0){ 735ce263fc8SMatthias Ringwald return HFP_CMD_RESPONSE_AND_HOLD_QUERY; 736ce263fc8SMatthias Ringwald } 737ce263fc8SMatthias Ringwald if (strncmp(line_buffer+strlen(HFP_RESPONSE_AND_HOLD)+offset, "=", 1) == 0){ 738ce263fc8SMatthias Ringwald return HFP_CMD_RESPONSE_AND_HOLD_COMMAND; 739ce263fc8SMatthias Ringwald } 740667ec068SMatthias Ringwald return HFP_CMD_RESPONSE_AND_HOLD_STATUS; 741ce263fc8SMatthias Ringwald } 742ce263fc8SMatthias Ringwald 743aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_INDICATOR, strlen(HFP_INDICATOR)) == 0){ 744aa4dd815SMatthias Ringwald if (strncmp(line_buffer+strlen(HFP_INDICATOR)+offset, "?", 1) == 0){ 745aa4dd815SMatthias Ringwald return HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS; 746aa4dd815SMatthias Ringwald } 747aa4dd815SMatthias Ringwald 748aa4dd815SMatthias Ringwald if (strncmp(line_buffer+strlen(HFP_INDICATOR)+offset, "=?", 2) == 0){ 749aa4dd815SMatthias Ringwald return HFP_CMD_RETRIEVE_AG_INDICATORS; 750aa4dd815SMatthias Ringwald } 751aa4dd815SMatthias Ringwald } 752aa4dd815SMatthias Ringwald 753aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_AVAILABLE_CODECS, strlen(HFP_AVAILABLE_CODECS)) == 0){ 754aa4dd815SMatthias Ringwald return HFP_CMD_AVAILABLE_CODECS; 755aa4dd815SMatthias Ringwald } 756aa4dd815SMatthias Ringwald 757aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, strlen(HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS)) == 0){ 758aa4dd815SMatthias Ringwald return HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE; 759aa4dd815SMatthias Ringwald } 760aa4dd815SMatthias Ringwald 761aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_ENABLE_CLIP, strlen(HFP_ENABLE_CLIP)) == 0){ 762a0ffb263SMatthias Ringwald if (isHandsFree) return HFP_CMD_AG_SENT_CLIP_INFORMATION; 763aa4dd815SMatthias Ringwald return HFP_CMD_ENABLE_CLIP; 764aa4dd815SMatthias Ringwald } 765aa4dd815SMatthias Ringwald 766aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_ENABLE_CALL_WAITING_NOTIFICATION, strlen(HFP_ENABLE_CALL_WAITING_NOTIFICATION)) == 0){ 767a0ffb263SMatthias Ringwald if (isHandsFree) return HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE; 768aa4dd815SMatthias Ringwald return HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION; 769aa4dd815SMatthias Ringwald } 770aa4dd815SMatthias Ringwald 771aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, strlen(HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES)) == 0){ 772aa4dd815SMatthias Ringwald 773aa4dd815SMatthias Ringwald if (isHandsFree) return HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES; 774aa4dd815SMatthias Ringwald 775aa4dd815SMatthias Ringwald if (strncmp(line_buffer+strlen(HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES)+offset, "=?", 2) == 0){ 776aa4dd815SMatthias Ringwald return HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES; 777aa4dd815SMatthias Ringwald } 778667ec068SMatthias Ringwald if (strncmp(line_buffer+strlen(HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES)+offset, "=", 1) == 0){ 779aa4dd815SMatthias Ringwald return HFP_CMD_CALL_HOLD; 780aa4dd815SMatthias Ringwald } 781aa4dd815SMatthias Ringwald 782aa4dd815SMatthias Ringwald return HFP_CMD_UNKNOWN; 783aa4dd815SMatthias Ringwald } 784aa4dd815SMatthias Ringwald 785aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_GENERIC_STATUS_INDICATOR, strlen(HFP_GENERIC_STATUS_INDICATOR)) == 0){ 786667ec068SMatthias Ringwald if (isHandsFree) { 787667ec068SMatthias Ringwald return HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS; 788667ec068SMatthias Ringwald } 789aa4dd815SMatthias Ringwald if (strncmp(line_buffer+strlen(HFP_GENERIC_STATUS_INDICATOR)+offset, "=?", 2) == 0){ 790aa4dd815SMatthias Ringwald return HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS; 791aa4dd815SMatthias Ringwald } 792aa4dd815SMatthias Ringwald if (strncmp(line_buffer+strlen(HFP_GENERIC_STATUS_INDICATOR)+offset, "=", 1) == 0){ 793aa4dd815SMatthias Ringwald return HFP_CMD_LIST_GENERIC_STATUS_INDICATORS; 794aa4dd815SMatthias Ringwald } 795aa4dd815SMatthias Ringwald return HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE; 796aa4dd815SMatthias Ringwald } 797aa4dd815SMatthias Ringwald 798aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS, strlen(HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS)) == 0){ 799aa4dd815SMatthias Ringwald return HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE; 8003deb3ec6SMatthias Ringwald } 8013deb3ec6SMatthias Ringwald 8023deb3ec6SMatthias Ringwald 803aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_QUERY_OPERATOR_SELECTION, strlen(HFP_QUERY_OPERATOR_SELECTION)) == 0){ 804aa4dd815SMatthias Ringwald if (strncmp(line_buffer+strlen(HFP_QUERY_OPERATOR_SELECTION)+offset, "=", 1) == 0){ 805aa4dd815SMatthias Ringwald return HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT; 8063deb3ec6SMatthias Ringwald } 807aa4dd815SMatthias Ringwald return HFP_CMD_QUERY_OPERATOR_SELECTION_NAME; 8083deb3ec6SMatthias Ringwald } 8093deb3ec6SMatthias Ringwald 810aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_TRANSFER_AG_INDICATOR_STATUS, strlen(HFP_TRANSFER_AG_INDICATOR_STATUS)) == 0){ 811aa4dd815SMatthias Ringwald return HFP_CMD_TRANSFER_AG_INDICATOR_STATUS; 8123deb3ec6SMatthias Ringwald } 8133deb3ec6SMatthias Ringwald 814aa4dd815SMatthias Ringwald if (isHandsFree && strncmp(line_buffer+offset, HFP_EXTENDED_AUDIO_GATEWAY_ERROR, strlen(HFP_EXTENDED_AUDIO_GATEWAY_ERROR)) == 0){ 815aa4dd815SMatthias Ringwald return HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR; 8163deb3ec6SMatthias Ringwald } 8173deb3ec6SMatthias Ringwald 818aa4dd815SMatthias Ringwald if (!isHandsFree && strncmp(line_buffer+offset, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, strlen(HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR)) == 0){ 819aa4dd815SMatthias Ringwald return HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR; 8203deb3ec6SMatthias Ringwald } 8213deb3ec6SMatthias Ringwald 822aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_TRIGGER_CODEC_CONNECTION_SETUP, strlen(HFP_TRIGGER_CODEC_CONNECTION_SETUP)) == 0){ 823aa4dd815SMatthias Ringwald return HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP; 824aa4dd815SMatthias Ringwald } 825aa4dd815SMatthias Ringwald 826aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, HFP_CONFIRM_COMMON_CODEC, strlen(HFP_CONFIRM_COMMON_CODEC)) == 0){ 8273deb3ec6SMatthias Ringwald if (isHandsFree){ 828aa4dd815SMatthias Ringwald return HFP_CMD_AG_SUGGESTED_CODEC; 8293deb3ec6SMatthias Ringwald } else { 830aa4dd815SMatthias Ringwald return HFP_CMD_HF_CONFIRMED_CODEC; 8313deb3ec6SMatthias Ringwald } 8323deb3ec6SMatthias Ringwald } 8333deb3ec6SMatthias Ringwald 834aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, "AT+", 3) == 0){ 835667ec068SMatthias Ringwald log_info("process unknown HF command %s \n", line_buffer); 836aa4dd815SMatthias Ringwald return HFP_CMD_UNKNOWN; 8373deb3ec6SMatthias Ringwald } 8383deb3ec6SMatthias Ringwald 839aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, "+", 1) == 0){ 840667ec068SMatthias Ringwald log_info(" process unknown AG command %s \n", line_buffer); 841aa4dd815SMatthias Ringwald return HFP_CMD_UNKNOWN; 842aa4dd815SMatthias Ringwald } 8433deb3ec6SMatthias Ringwald 844aa4dd815SMatthias Ringwald if (strncmp(line_buffer+offset, "NOP", 3) == 0){ 845aa4dd815SMatthias Ringwald return HFP_CMD_NONE; 846aa4dd815SMatthias Ringwald } 847aa4dd815SMatthias Ringwald 848aa4dd815SMatthias Ringwald return HFP_CMD_NONE; 8493deb3ec6SMatthias Ringwald } 8503deb3ec6SMatthias Ringwald 851a0ffb263SMatthias Ringwald static void hfp_parser_store_byte(hfp_connection_t * hfp_connection, uint8_t byte){ 852ce263fc8SMatthias Ringwald // printf("hfp_parser_store_byte %c at pos %u\n", (char) byte, context->line_size); 8533deb3ec6SMatthias Ringwald // TODO: add limit 854a0ffb263SMatthias Ringwald hfp_connection->line_buffer[hfp_connection->line_size++] = byte; 855a0ffb263SMatthias Ringwald hfp_connection->line_buffer[hfp_connection->line_size] = 0; 8563deb3ec6SMatthias Ringwald } 857a0ffb263SMatthias Ringwald static int hfp_parser_is_buffer_empty(hfp_connection_t * hfp_connection){ 858a0ffb263SMatthias Ringwald return hfp_connection->line_size == 0; 8593deb3ec6SMatthias Ringwald } 8603deb3ec6SMatthias Ringwald 8613deb3ec6SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){ 8623deb3ec6SMatthias Ringwald return byte == '\n' || byte == '\r'; 8633deb3ec6SMatthias Ringwald } 8643deb3ec6SMatthias Ringwald 8653deb3ec6SMatthias Ringwald static int hfp_parser_is_end_of_header(uint8_t byte){ 8663deb3ec6SMatthias Ringwald return hfp_parser_is_end_of_line(byte) || byte == ':' || byte == '?'; 8673deb3ec6SMatthias Ringwald } 8683deb3ec6SMatthias Ringwald 869a0ffb263SMatthias Ringwald static int hfp_parser_found_separator(hfp_connection_t * hfp_connection, uint8_t byte){ 870a0ffb263SMatthias Ringwald if (hfp_connection->keep_byte == 1) return 1; 8713deb3ec6SMatthias Ringwald 8723deb3ec6SMatthias Ringwald int found_separator = byte == ',' || byte == '\n'|| byte == '\r'|| 8733deb3ec6SMatthias Ringwald byte == ')' || byte == '(' || byte == ':' || 8743deb3ec6SMatthias Ringwald byte == '-' || byte == '"' || byte == '?'|| byte == '='; 8753deb3ec6SMatthias Ringwald return found_separator; 8763deb3ec6SMatthias Ringwald } 8773deb3ec6SMatthias Ringwald 878a0ffb263SMatthias Ringwald static void hfp_parser_next_state(hfp_connection_t * hfp_connection, uint8_t byte){ 879a0ffb263SMatthias Ringwald hfp_connection->line_size = 0; 8803deb3ec6SMatthias Ringwald if (hfp_parser_is_end_of_line(byte)){ 881a0ffb263SMatthias Ringwald hfp_connection->parser_item_index = 0; 882a0ffb263SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 8833deb3ec6SMatthias Ringwald return; 8843deb3ec6SMatthias Ringwald } 885a0ffb263SMatthias Ringwald switch (hfp_connection->parser_state){ 8863deb3ec6SMatthias Ringwald case HFP_PARSER_CMD_HEADER: 887a0ffb263SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE; 888a0ffb263SMatthias Ringwald if (hfp_connection->keep_byte == 1){ 889a0ffb263SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 890a0ffb263SMatthias Ringwald hfp_connection->keep_byte = 0; 8913deb3ec6SMatthias Ringwald } 8923deb3ec6SMatthias Ringwald break; 8933deb3ec6SMatthias Ringwald case HFP_PARSER_CMD_SEQUENCE: 894a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 895ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 896a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 897a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 8983deb3ec6SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 899aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 900aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 901aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 902aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 903ce263fc8SMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 904a0ffb263SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_SECOND_ITEM; 9053deb3ec6SMatthias Ringwald break; 9063deb3ec6SMatthias Ringwald default: 9073deb3ec6SMatthias Ringwald break; 9083deb3ec6SMatthias Ringwald } 9093deb3ec6SMatthias Ringwald break; 9103deb3ec6SMatthias Ringwald case HFP_PARSER_SECOND_ITEM: 911a0ffb263SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_THIRD_ITEM; 9123deb3ec6SMatthias Ringwald break; 9133deb3ec6SMatthias Ringwald case HFP_PARSER_THIRD_ITEM: 914a0ffb263SMatthias Ringwald if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS){ 915a0ffb263SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE; 9163deb3ec6SMatthias Ringwald break; 9173deb3ec6SMatthias Ringwald } 918a0ffb263SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 9193deb3ec6SMatthias Ringwald break; 9203deb3ec6SMatthias Ringwald } 9213deb3ec6SMatthias Ringwald } 9223deb3ec6SMatthias Ringwald 923a0ffb263SMatthias Ringwald void hfp_parse(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){ 924aa4dd815SMatthias Ringwald // handle ATD<dial_string>; 925a0ffb263SMatthias Ringwald if (strncmp((const char*)hfp_connection->line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0){ 926aa4dd815SMatthias Ringwald // check for end-of-line or ';' 927aa4dd815SMatthias Ringwald if (byte == ';' || hfp_parser_is_end_of_line(byte)){ 928a0ffb263SMatthias Ringwald hfp_connection->line_buffer[hfp_connection->line_size] = 0; 929a0ffb263SMatthias Ringwald hfp_connection->line_size = 0; 930a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_CALL_PHONE_NUMBER; 931aa4dd815SMatthias Ringwald } else { 932a0ffb263SMatthias Ringwald hfp_connection->line_buffer[hfp_connection->line_size++] = byte; 933aa4dd815SMatthias Ringwald } 934aa4dd815SMatthias Ringwald return; 935aa4dd815SMatthias Ringwald } 936aa4dd815SMatthias Ringwald 9373deb3ec6SMatthias Ringwald // TODO: handle space inside word 938a0ffb263SMatthias Ringwald if (byte == ' ' && hfp_connection->parser_state > HFP_PARSER_CMD_HEADER) return; 9393deb3ec6SMatthias Ringwald 940a0ffb263SMatthias Ringwald if (byte == ',' && hfp_connection->command == HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE){ 941a0ffb263SMatthias Ringwald if (hfp_connection->line_size == 0){ 942a0ffb263SMatthias Ringwald hfp_connection->line_buffer[0] = 0; 943a0ffb263SMatthias Ringwald hfp_connection->ignore_value = 1; 944a0ffb263SMatthias Ringwald parse_sequence(hfp_connection); 945ce263fc8SMatthias Ringwald return; 946ce263fc8SMatthias Ringwald } 947ce263fc8SMatthias Ringwald } 948ce263fc8SMatthias Ringwald 949a0ffb263SMatthias Ringwald if (!hfp_parser_found_separator(hfp_connection, byte)){ 950a0ffb263SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 9513deb3ec6SMatthias Ringwald return; 9523deb3ec6SMatthias Ringwald } 953ce263fc8SMatthias Ringwald 9543deb3ec6SMatthias Ringwald if (hfp_parser_is_end_of_line(byte)) { 955a0ffb263SMatthias Ringwald if (hfp_parser_is_buffer_empty(hfp_connection)){ 956a0ffb263SMatthias Ringwald hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 9573deb3ec6SMatthias Ringwald } 9583deb3ec6SMatthias Ringwald } 959a0ffb263SMatthias Ringwald if (hfp_parser_is_buffer_empty(hfp_connection)) return; 9603deb3ec6SMatthias Ringwald 961a0ffb263SMatthias Ringwald switch (hfp_connection->parser_state){ 9623deb3ec6SMatthias Ringwald case HFP_PARSER_CMD_HEADER: // header 9633deb3ec6SMatthias Ringwald if (byte == '='){ 964a0ffb263SMatthias Ringwald hfp_connection->keep_byte = 1; 965a0ffb263SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 9663deb3ec6SMatthias Ringwald return; 9673deb3ec6SMatthias Ringwald } 9683deb3ec6SMatthias Ringwald 9693deb3ec6SMatthias Ringwald if (byte == '?'){ 970a0ffb263SMatthias Ringwald hfp_connection->keep_byte = 0; 971a0ffb263SMatthias Ringwald hfp_parser_store_byte(hfp_connection, byte); 9723deb3ec6SMatthias Ringwald return; 9733deb3ec6SMatthias Ringwald } 974ce263fc8SMatthias Ringwald 975ce263fc8SMatthias Ringwald if (byte == ','){ 976a0ffb263SMatthias Ringwald hfp_connection->resolve_byte = 1; 977ce263fc8SMatthias Ringwald } 978ce263fc8SMatthias Ringwald 979a0ffb263SMatthias Ringwald // printf(" parse header 2 %s, keep separator $ %d\n", hfp_connection->line_buffer, hfp_connection->keep_byte); 980a0ffb263SMatthias Ringwald if (hfp_parser_is_end_of_header(byte) || hfp_connection->keep_byte == 1){ 981a0ffb263SMatthias Ringwald // printf(" parse header 3 %s, keep separator $ %d\n", hfp_connection->line_buffer, hfp_connection->keep_byte); 982a0ffb263SMatthias Ringwald char * line_buffer = (char *)hfp_connection->line_buffer; 983a0ffb263SMatthias Ringwald hfp_connection->command = parse_command(line_buffer, isHandsFree); 984aa4dd815SMatthias Ringwald 985a0ffb263SMatthias Ringwald /* resolve command name according to hfp_connection */ 986a0ffb263SMatthias Ringwald if (hfp_connection->command == HFP_CMD_UNKNOWN){ 987a0ffb263SMatthias Ringwald switch(hfp_connection->state){ 988aa4dd815SMatthias Ringwald case HFP_W4_LIST_GENERIC_STATUS_INDICATORS: 989a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_LIST_GENERIC_STATUS_INDICATORS; 990aa4dd815SMatthias Ringwald break; 991aa4dd815SMatthias Ringwald case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS: 992a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS; 993aa4dd815SMatthias Ringwald break; 994aa4dd815SMatthias Ringwald case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 995a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE; 996aa4dd815SMatthias Ringwald break; 997aa4dd815SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS_STATUS: 998a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS; 999aa4dd815SMatthias Ringwald break; 1000aa4dd815SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS: 1001a0ffb263SMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0; 1002a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS; 1003aa4dd815SMatthias Ringwald break; 1004aa4dd815SMatthias Ringwald default: 1005aa4dd815SMatthias Ringwald break; 1006aa4dd815SMatthias Ringwald } 1007aa4dd815SMatthias Ringwald } 10083deb3ec6SMatthias Ringwald } 10093deb3ec6SMatthias Ringwald break; 10103deb3ec6SMatthias Ringwald 1011ce263fc8SMatthias Ringwald case HFP_PARSER_CMD_SEQUENCE: 1012a0ffb263SMatthias Ringwald parse_sequence(hfp_connection); 1013ce263fc8SMatthias Ringwald break; 1014ce263fc8SMatthias Ringwald case HFP_PARSER_SECOND_ITEM: 1015a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1016ce263fc8SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1017a0ffb263SMatthias Ringwald log_info("format %s, ", hfp_connection->line_buffer); 1018a0ffb263SMatthias Ringwald hfp_connection->network_operator.format = atoi((char *)&hfp_connection->line_buffer[0]); 1019ce263fc8SMatthias Ringwald break; 1020ce263fc8SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 1021a0ffb263SMatthias Ringwald log_info("format %s \n", hfp_connection->line_buffer); 1022a0ffb263SMatthias Ringwald hfp_connection->network_operator.format = atoi((char *)&hfp_connection->line_buffer[0]); 1023ce263fc8SMatthias Ringwald break; 1024ce263fc8SMatthias Ringwald case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 1025ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 1026ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 1027a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].state = (uint8_t)atoi((char*)hfp_connection->line_buffer); 1028ce263fc8SMatthias Ringwald break; 1029ce263fc8SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 1030a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = (uint8_t)atoi((char*)hfp_connection->line_buffer); 1031a0ffb263SMatthias Ringwald log_info("%d \n", hfp_connection->ag_indicators[hfp_connection->parser_item_index].status); 1032a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].status_changed = 1; 1033ce263fc8SMatthias Ringwald break; 1034ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 1035a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].min_range = atoi((char *)hfp_connection->line_buffer); 1036a0ffb263SMatthias Ringwald log_info("%s, ", hfp_connection->line_buffer); 1037ce263fc8SMatthias Ringwald break; 1038ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 1039a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1040a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1041a0ffb263SMatthias Ringwald hfp_connection->bnip_type = (uint8_t)atoi((char*)hfp_connection->line_buffer); 1042ce263fc8SMatthias Ringwald break; 1043ce263fc8SMatthias Ringwald default: 1044ce263fc8SMatthias Ringwald break; 1045ce263fc8SMatthias Ringwald } 1046ce263fc8SMatthias Ringwald break; 1047ce263fc8SMatthias Ringwald 1048ce263fc8SMatthias Ringwald case HFP_PARSER_THIRD_ITEM: 1049a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1050ce263fc8SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1051a0ffb263SMatthias Ringwald strcpy(hfp_connection->network_operator.name, (char *)hfp_connection->line_buffer); 1052a0ffb263SMatthias Ringwald log_info("name %s\n", hfp_connection->line_buffer); 1053ce263fc8SMatthias Ringwald break; 1054ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 1055a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].max_range = atoi((char *)hfp_connection->line_buffer); 1056a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1057a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr = hfp_connection->parser_item_index; 1058a0ffb263SMatthias Ringwald log_info("%s)\n", hfp_connection->line_buffer); 1059ce263fc8SMatthias Ringwald break; 1060ce263fc8SMatthias Ringwald default: 1061ce263fc8SMatthias Ringwald break; 1062ce263fc8SMatthias Ringwald } 1063ce263fc8SMatthias Ringwald break; 1064ce263fc8SMatthias Ringwald } 1065a0ffb263SMatthias Ringwald hfp_parser_next_state(hfp_connection, byte); 1066ce263fc8SMatthias Ringwald 1067a0ffb263SMatthias Ringwald if (hfp_connection->resolve_byte && hfp_connection->command == HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE){ 1068a0ffb263SMatthias Ringwald hfp_connection->resolve_byte = 0; 1069a0ffb263SMatthias Ringwald hfp_connection->ignore_value = 1; 1070a0ffb263SMatthias Ringwald parse_sequence(hfp_connection); 1071a0ffb263SMatthias Ringwald hfp_connection->line_buffer[0] = 0; 1072a0ffb263SMatthias Ringwald hfp_connection->line_size = 0; 1073ce263fc8SMatthias Ringwald } 1074ce263fc8SMatthias Ringwald } 1075ce263fc8SMatthias Ringwald 1076a0ffb263SMatthias Ringwald static void parse_sequence(hfp_connection_t * hfp_connection){ 1077ce263fc8SMatthias Ringwald int value; 1078a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1079667ec068SMatthias Ringwald case HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS: 1080a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1081667ec068SMatthias Ringwald int i; 1082a0ffb263SMatthias Ringwald switch (hfp_connection->parser_item_index){ 1083667ec068SMatthias Ringwald case 0: 1084a0ffb263SMatthias Ringwald for (i=0;i<hfp_connection->generic_status_indicators_nr;i++){ 1085a0ffb263SMatthias Ringwald if (hfp_connection->generic_status_indicators[i].uuid == value){ 1086a0ffb263SMatthias Ringwald hfp_connection->parser_indicator_index = i; 1087667ec068SMatthias Ringwald break; 1088667ec068SMatthias Ringwald } 1089667ec068SMatthias Ringwald } 1090667ec068SMatthias Ringwald break; 1091667ec068SMatthias Ringwald case 1: 1092a0ffb263SMatthias Ringwald if (hfp_connection->parser_indicator_index <0) break; 1093a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[hfp_connection->parser_indicator_index].state = value; 1094667ec068SMatthias Ringwald log_info("HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS set indicator at index %u, to %u\n", 1095a0ffb263SMatthias Ringwald hfp_connection->parser_item_index, value); 1096667ec068SMatthias Ringwald break; 1097667ec068SMatthias Ringwald default: 1098667ec068SMatthias Ringwald break; 1099667ec068SMatthias Ringwald } 1100a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1101667ec068SMatthias Ringwald break; 1102667ec068SMatthias Ringwald 1103667ec068SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1104a0ffb263SMatthias Ringwald switch(hfp_connection->parser_item_index){ 1105667ec068SMatthias Ringwald case 0: 1106a0ffb263SMatthias Ringwald strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number)); 1107a0ffb263SMatthias Ringwald hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0; 1108667ec068SMatthias Ringwald break; 1109667ec068SMatthias Ringwald case 1: 1110a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1111a0ffb263SMatthias Ringwald hfp_connection->bnip_type = value; 1112667ec068SMatthias Ringwald break; 1113667ec068SMatthias Ringwald default: 1114667ec068SMatthias Ringwald break; 1115667ec068SMatthias Ringwald } 1116a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1117667ec068SMatthias Ringwald break; 1118667ec068SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 1119a0ffb263SMatthias Ringwald switch(hfp_connection->parser_item_index){ 1120667ec068SMatthias Ringwald case 0: 1121a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1122a0ffb263SMatthias Ringwald hfp_connection->clcc_idx = value; 1123667ec068SMatthias Ringwald break; 1124667ec068SMatthias Ringwald case 1: 1125a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1126a0ffb263SMatthias Ringwald hfp_connection->clcc_dir = value; 1127667ec068SMatthias Ringwald break; 1128667ec068SMatthias Ringwald case 2: 1129a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1130a0ffb263SMatthias Ringwald hfp_connection->clcc_status = value; 1131667ec068SMatthias Ringwald break; 1132667ec068SMatthias Ringwald case 3: 1133a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1134a0ffb263SMatthias Ringwald hfp_connection->clcc_mpty = value; 1135667ec068SMatthias Ringwald break; 1136667ec068SMatthias Ringwald case 4: 1137a0ffb263SMatthias Ringwald strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number)); 1138a0ffb263SMatthias Ringwald hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0; 1139667ec068SMatthias Ringwald break; 1140667ec068SMatthias Ringwald case 5: 1141a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1142a0ffb263SMatthias Ringwald hfp_connection->bnip_type = value; 1143667ec068SMatthias Ringwald break; 1144667ec068SMatthias Ringwald default: 1145667ec068SMatthias Ringwald break; 1146667ec068SMatthias Ringwald } 1147a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1148667ec068SMatthias Ringwald break; 1149aa4dd815SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 1150a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1151a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = value; 1152aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CMD_SET_MICROPHONE_GAIN %d\n", value); 1153aa4dd815SMatthias Ringwald break; 1154aa4dd815SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 1155a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1156a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = value; 1157aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CMD_SET_SPEAKER_GAIN %d\n", value); 1158aa4dd815SMatthias Ringwald break; 1159aa4dd815SMatthias Ringwald case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION: 1160a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1161a0ffb263SMatthias Ringwald hfp_connection->ag_activate_voice_recognition = value; 1162aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION %d\n", value); 1163aa4dd815SMatthias Ringwald break; 1164aa4dd815SMatthias Ringwald case HFP_CMD_TURN_OFF_EC_AND_NR: 1165a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1166a0ffb263SMatthias Ringwald hfp_connection->ag_echo_and_noise_reduction = value; 1167aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CMD_TURN_OFF_EC_AND_NR %d\n", value); 1168aa4dd815SMatthias Ringwald break; 1169aa4dd815SMatthias Ringwald case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING: 1170a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1171a0ffb263SMatthias Ringwald hfp_connection->remote_supported_features = store_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE, value); 1172aa4dd815SMatthias Ringwald log_info("hfp parse HFP_CHANGE_IN_BAND_RING_TONE_SETTING %d\n", value); 1173aa4dd815SMatthias Ringwald break; 11743deb3ec6SMatthias Ringwald case HFP_CMD_HF_CONFIRMED_CODEC: 1175a0ffb263SMatthias Ringwald hfp_connection->codec_confirmed = atoi((char*)hfp_connection->line_buffer); 1176a0ffb263SMatthias Ringwald log_info("hfp parse HFP_CMD_HF_CONFIRMED_CODEC %d\n", hfp_connection->codec_confirmed); 11773deb3ec6SMatthias Ringwald break; 11783deb3ec6SMatthias Ringwald case HFP_CMD_AG_SUGGESTED_CODEC: 1179a0ffb263SMatthias Ringwald hfp_connection->suggested_codec = atoi((char*)hfp_connection->line_buffer); 1180a0ffb263SMatthias Ringwald log_info("hfp parse HFP_CMD_AG_SUGGESTED_CODEC %d\n", hfp_connection->suggested_codec); 11813deb3ec6SMatthias Ringwald break; 11823deb3ec6SMatthias Ringwald case HFP_CMD_SUPPORTED_FEATURES: 1183a0ffb263SMatthias Ringwald hfp_connection->remote_supported_features = atoi((char*)hfp_connection->line_buffer); 1184a0ffb263SMatthias Ringwald log_info("Parsed supported feature %d\n", hfp_connection->remote_supported_features); 11853deb3ec6SMatthias Ringwald break; 11863deb3ec6SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 1187a0ffb263SMatthias Ringwald log_info("Parsed codec %s\n", hfp_connection->line_buffer); 1188a0ffb263SMatthias Ringwald hfp_connection->remote_codecs[hfp_connection->parser_item_index] = (uint16_t)atoi((char*)hfp_connection->line_buffer); 1189a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1190a0ffb263SMatthias Ringwald hfp_connection->remote_codecs_nr = hfp_connection->parser_item_index; 11913deb3ec6SMatthias Ringwald break; 1192aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS: 1193a0ffb263SMatthias Ringwald strcpy((char *)hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, (char *)hfp_connection->line_buffer); 1194a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].index = hfp_connection->parser_item_index+1; 1195a0ffb263SMatthias Ringwald log_info("Indicator %d: %s (", hfp_connection->ag_indicators_nr+1, hfp_connection->line_buffer); 1196aa4dd815SMatthias Ringwald break; 1197aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 1198a0ffb263SMatthias Ringwald log_info("Parsed Indicator %d with status: %s\n", hfp_connection->parser_item_index+1, hfp_connection->line_buffer); 1199a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = atoi((char *) hfp_connection->line_buffer); 1200a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 12013deb3ec6SMatthias Ringwald break; 12023deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 1203a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1204a0ffb263SMatthias Ringwald if (hfp_connection->parser_item_index != 4) break; 1205a0ffb263SMatthias Ringwald log_info("Parsed Enable indicators: %s\n", hfp_connection->line_buffer); 1206a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1207a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = (uint8_t) value; 12083deb3ec6SMatthias Ringwald break; 12093deb3ec6SMatthias Ringwald case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES: 1210a0ffb263SMatthias Ringwald log_info("Parsed Support call hold: %s\n", hfp_connection->line_buffer); 1211a0ffb263SMatthias Ringwald if (hfp_connection->line_size > 2 ) break; 1212a0ffb263SMatthias Ringwald strcpy((char *)hfp_connection->remote_call_services[hfp_connection->remote_call_services_nr].name, (char *)hfp_connection->line_buffer); 1213a0ffb263SMatthias Ringwald hfp_connection->remote_call_services_nr++; 12143deb3ec6SMatthias Ringwald break; 1215aa4dd815SMatthias Ringwald case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 1216aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 1217a0ffb263SMatthias Ringwald log_info("Parsed Generic status indicator: %s\n", hfp_connection->line_buffer); 1218a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].uuid = (uint16_t)atoi((char*)hfp_connection->line_buffer); 1219a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 1220a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators_nr = hfp_connection->parser_item_index; 12213deb3ec6SMatthias Ringwald break; 1222aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 12233deb3ec6SMatthias Ringwald // HF parses inital AG gen. ind. state 1224a0ffb263SMatthias Ringwald log_info("Parsed List generic status indicator %s state: ", hfp_connection->line_buffer); 1225a0ffb263SMatthias Ringwald hfp_connection->parser_item_index = (uint8_t)atoi((char*)hfp_connection->line_buffer); 12263deb3ec6SMatthias Ringwald break; 1227ce263fc8SMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS: 1228a0ffb263SMatthias Ringwald hfp_connection->parser_indicator_index = (uint8_t)atoi((char*)hfp_connection->line_buffer); 1229a0ffb263SMatthias Ringwald log_info("Parsed HF indicator index %u", hfp_connection->parser_indicator_index); 1230ce263fc8SMatthias Ringwald break; 12313deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE: 12323deb3ec6SMatthias Ringwald // AG parses new gen. ind. state 1233a0ffb263SMatthias Ringwald if (hfp_connection->ignore_value){ 1234a0ffb263SMatthias Ringwald hfp_connection->ignore_value = 0; 1235a0ffb263SMatthias Ringwald log_info("Parsed Enable AG indicator pos %u('%s') - unchanged (stays %u)\n", hfp_connection->parser_item_index, 1236a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled); 1237ce263fc8SMatthias Ringwald } 1238a0ffb263SMatthias Ringwald else if (hfp_connection->ag_indicators[hfp_connection->parser_item_index].mandatory){ 1239ce263fc8SMatthias Ringwald log_info("Parsed Enable AG indicator pos %u('%s') - ignore (mandatory)\n", 1240a0ffb263SMatthias Ringwald hfp_connection->parser_item_index, hfp_connection->ag_indicators[hfp_connection->parser_item_index].name); 1241ce263fc8SMatthias Ringwald } else { 1242a0ffb263SMatthias Ringwald value = atoi((char *)&hfp_connection->line_buffer[0]); 1243a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled = value; 1244a0ffb263SMatthias Ringwald log_info("Parsed Enable AG indicator pos %u('%s'): %u\n", hfp_connection->parser_item_index, 1245a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, value); 12463deb3ec6SMatthias Ringwald } 1247a0ffb263SMatthias Ringwald hfp_connection->parser_item_index++; 12483deb3ec6SMatthias Ringwald break; 12493deb3ec6SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 12503deb3ec6SMatthias Ringwald // indicators are indexed starting with 1 1251a0ffb263SMatthias Ringwald hfp_connection->parser_item_index = atoi((char *)&hfp_connection->line_buffer[0]) - 1; 1252a0ffb263SMatthias Ringwald log_info("Parsed status of the AG indicator %d, status ", hfp_connection->parser_item_index); 12533deb3ec6SMatthias Ringwald break; 1254aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1255a0ffb263SMatthias Ringwald hfp_connection->network_operator.mode = atoi((char *)&hfp_connection->line_buffer[0]); 1256a0ffb263SMatthias Ringwald log_info("Parsed network operator mode: %d, ", hfp_connection->network_operator.mode); 1257aa4dd815SMatthias Ringwald break; 1258aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 1259a0ffb263SMatthias Ringwald if (hfp_connection->line_buffer[0] == '3'){ 1260a0ffb263SMatthias Ringwald log_info("Parsed Set network operator format : %s, ", hfp_connection->line_buffer); 12613deb3ec6SMatthias Ringwald break; 12623deb3ec6SMatthias Ringwald } 12633deb3ec6SMatthias Ringwald // TODO emit ERROR, wrong format 1264a0ffb263SMatthias Ringwald log_info("ERROR Set network operator format: index %s not supported\n", hfp_connection->line_buffer); 12653deb3ec6SMatthias Ringwald break; 12663deb3ec6SMatthias Ringwald case HFP_CMD_ERROR: 12673deb3ec6SMatthias Ringwald break; 12683deb3ec6SMatthias Ringwald case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1269a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 1; 1270a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error_value = (uint8_t)atoi((char*)hfp_connection->line_buffer); 12713deb3ec6SMatthias Ringwald break; 12723deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR: 1273a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = (uint8_t)atoi((char*)hfp_connection->line_buffer); 1274a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 1275a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 12763deb3ec6SMatthias Ringwald break; 1277ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 1278a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1279a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1280a0ffb263SMatthias Ringwald strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number)); 1281a0ffb263SMatthias Ringwald hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0; 12823deb3ec6SMatthias Ringwald break; 12833deb3ec6SMatthias Ringwald default: 12843deb3ec6SMatthias Ringwald break; 12853deb3ec6SMatthias Ringwald } 12863deb3ec6SMatthias Ringwald } 12873deb3ec6SMatthias Ringwald 12883deb3ec6SMatthias Ringwald void hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid){ 1289a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = provide_hfp_connection_context_for_bd_addr(bd_addr); 1290a0ffb263SMatthias Ringwald log_info("hfp_connect %s, hfp_connection %p", bd_addr_to_str(bd_addr), hfp_connection); 12913deb3ec6SMatthias Ringwald 1292a0ffb263SMatthias Ringwald if (!hfp_connection) { 12933deb3ec6SMatthias Ringwald log_error("hfp_establish_service_level_connection for addr %s failed", bd_addr_to_str(bd_addr)); 12943deb3ec6SMatthias Ringwald return; 12953deb3ec6SMatthias Ringwald } 1296aa4dd815SMatthias Ringwald 1297a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 12983deb3ec6SMatthias Ringwald case HFP_W2_DISCONNECT_RFCOMM: 1299a0ffb263SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 13003deb3ec6SMatthias Ringwald return; 13013deb3ec6SMatthias Ringwald case HFP_W4_RFCOMM_DISCONNECTED: 1302a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART; 13033deb3ec6SMatthias Ringwald return; 13043deb3ec6SMatthias Ringwald case HFP_IDLE: 1305a0ffb263SMatthias Ringwald memcpy(hfp_connection->remote_addr, bd_addr, 6); 1306d63c37a1SMatthias Ringwald hfp_connection->state = HFP_W4_SDP_QUERY_COMPLETE; 1307a0ffb263SMatthias Ringwald connection_doing_sdp_query = hfp_connection; 1308a0ffb263SMatthias Ringwald hfp_connection->service_uuid = service_uuid; 13097fbb5f59SMatthias Ringwald sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, hfp_connection->remote_addr, service_uuid); 13103deb3ec6SMatthias Ringwald break; 13113deb3ec6SMatthias Ringwald default: 13123deb3ec6SMatthias Ringwald break; 13133deb3ec6SMatthias Ringwald } 13143deb3ec6SMatthias Ringwald } 13153deb3ec6SMatthias Ringwald 1316a0ffb263SMatthias Ringwald void hfp_release_service_level_connection(hfp_connection_t * hfp_connection){ 1317a0ffb263SMatthias Ringwald if (!hfp_connection) return; 1318a0ffb263SMatthias Ringwald hfp_release_audio_connection(hfp_connection); 13193deb3ec6SMatthias Ringwald 1320a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_W4_RFCOMM_CONNECTED){ 1321a0ffb263SMatthias Ringwald hfp_connection->state = HFP_IDLE; 13223deb3ec6SMatthias Ringwald return; 13233deb3ec6SMatthias Ringwald } 13243deb3ec6SMatthias Ringwald 1325a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_W4_RFCOMM_CONNECTED){ 1326a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN; 13273deb3ec6SMatthias Ringwald return; 13283deb3ec6SMatthias Ringwald } 13293deb3ec6SMatthias Ringwald 1330a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_W4_SCO_CONNECTED){ 1331a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM; 13323deb3ec6SMatthias Ringwald return; 13333deb3ec6SMatthias Ringwald } 13343deb3ec6SMatthias Ringwald 1335a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_W4_SCO_DISCONNECTED){ 1336a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W2_DISCONNECT_SCO; 13373deb3ec6SMatthias Ringwald return; 13383deb3ec6SMatthias Ringwald } 13393deb3ec6SMatthias Ringwald 13403deb3ec6SMatthias Ringwald return; 13413deb3ec6SMatthias Ringwald } 13423deb3ec6SMatthias Ringwald 1343a0ffb263SMatthias Ringwald void hfp_release_audio_connection(hfp_connection_t * hfp_connection){ 1344a0ffb263SMatthias Ringwald if (!hfp_connection) return; 1345a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return; 1346a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 1; 13473deb3ec6SMatthias Ringwald } 13483deb3ec6SMatthias Ringwald 1349ce263fc8SMatthias Ringwald static const struct link_settings { 1350ce263fc8SMatthias Ringwald const uint16_t max_latency; 1351ce263fc8SMatthias Ringwald const uint8_t retransmission_effort; 1352ce263fc8SMatthias Ringwald const uint16_t packet_types; 1353ce263fc8SMatthias Ringwald } hfp_link_settings [] = { 1354ce263fc8SMatthias Ringwald { 0xffff, 0xff, 0x03c1 }, // HFP_LINK_SETTINGS_D0, HV1 1355ce263fc8SMatthias Ringwald { 0xffff, 0xff, 0x03c4 }, // HFP_LINK_SETTINGS_D1, HV3 1356ce263fc8SMatthias Ringwald { 0x0007, 0x01, 0x03c8 }, // HFP_LINK_SETTINGS_S1, EV3 1357ce263fc8SMatthias Ringwald { 0x0007, 0x01, 0x0380 }, // HFP_LINK_SETTINGS_S2, 2-EV3 1358ce263fc8SMatthias Ringwald { 0x000a, 0x01, 0x0380 }, // HFP_LINK_SETTINGS_S3, 2-EV3 1359ce263fc8SMatthias Ringwald { 0x000c, 0x02, 0x0380 }, // HFP_LINK_SETTINGS_S4, 2-EV3 1360ce263fc8SMatthias Ringwald { 0x0008, 0x02, 0x03c8 }, // HFP_LINK_SETTINGS_T1, EV3 1361ce263fc8SMatthias Ringwald { 0x000d, 0x02, 0x0380 } // HFP_LINK_SETTINGS_T2, 2-EV3 1362ce263fc8SMatthias Ringwald }; 13633deb3ec6SMatthias Ringwald 1364eddcd308SMatthias Ringwald void hfp_setup_synchronous_connection(hfp_connection_t * hfp_connection){ 1365ce263fc8SMatthias Ringwald // all packet types, fixed bandwidth 1366eddcd308SMatthias Ringwald int setting = hfp_connection->link_setting; 1367ce263fc8SMatthias Ringwald log_info("hfp_setup_synchronous_connection using setting nr %u", setting); 1368eddcd308SMatthias Ringwald sco_establishment_active = hfp_connection; 1369eddcd308SMatthias Ringwald hci_send_cmd(&hci_setup_synchronous_connection, hfp_connection->acl_handle, 8000, 8000, hfp_link_settings[setting].max_latency, 1370ce263fc8SMatthias 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 1371ce263fc8SMatthias Ringwald } 1372d68dcce1SMatthias Ringwald 1373d68dcce1SMatthias Ringwald void hfp_set_packet_handler_for_rfcomm_connections(btstack_packet_handler_t handler){ 1374d68dcce1SMatthias Ringwald rfcomm_packet_handler = handler; 1375d68dcce1SMatthias Ringwald } 1376d68dcce1SMatthias Ringwald 1377