xref: /btstack/src/classic/hfp.c (revision abffc8194860aa979de23f5ebdb8af1d1c842818)
13deb3ec6SMatthias Ringwald /*
23deb3ec6SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
33deb3ec6SMatthias Ringwald  *
43deb3ec6SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
53deb3ec6SMatthias Ringwald  * modification, are permitted provided that the following conditions
63deb3ec6SMatthias Ringwald  * are met:
73deb3ec6SMatthias Ringwald  *
83deb3ec6SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
93deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
103deb3ec6SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
113deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
123deb3ec6SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
133deb3ec6SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
143deb3ec6SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
153deb3ec6SMatthias Ringwald  *    from this software without specific prior written permission.
163deb3ec6SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
173deb3ec6SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
183deb3ec6SMatthias Ringwald  *    monetary gain.
193deb3ec6SMatthias Ringwald  *
203deb3ec6SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
213deb3ec6SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
223deb3ec6SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
233deb3ec6SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
243deb3ec6SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
253deb3ec6SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
263deb3ec6SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
273deb3ec6SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
283deb3ec6SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
293deb3ec6SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
303deb3ec6SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313deb3ec6SMatthias Ringwald  * SUCH DAMAGE.
323deb3ec6SMatthias Ringwald  *
333deb3ec6SMatthias Ringwald  * Please inquire about commercial licensing options at
343deb3ec6SMatthias Ringwald  * [email protected]
353deb3ec6SMatthias Ringwald  *
363deb3ec6SMatthias Ringwald  */
373deb3ec6SMatthias Ringwald 
383deb3ec6SMatthias Ringwald 
397907f069SMatthias Ringwald #include "btstack_config.h"
403deb3ec6SMatthias Ringwald 
413deb3ec6SMatthias Ringwald #include <stdint.h>
423deb3ec6SMatthias Ringwald #include <stdio.h>
433deb3ec6SMatthias Ringwald #include <stdlib.h>
443deb3ec6SMatthias Ringwald #include <string.h>
453deb3ec6SMatthias Ringwald #include <inttypes.h>
463deb3ec6SMatthias Ringwald 
47023f2764SMatthias Ringwald #include "btstack_debug.h"
4859c6af15SMatthias Ringwald #include "btstack_event.h"
4959c6af15SMatthias Ringwald #include "btstack_memory.h"
5059c6af15SMatthias Ringwald #include "btstack_run_loop.h"
5159c6af15SMatthias Ringwald #include "classic/core.h"
52efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
53746ccb7eSMatthias Ringwald #include "classic/sdp_server.h"
54023f2764SMatthias Ringwald #include "classic/sdp_util.h"
5559c6af15SMatthias Ringwald #include "hci.h"
5659c6af15SMatthias Ringwald #include "hci_cmd.h"
5759c6af15SMatthias Ringwald #include "hci_dump.h"
5859c6af15SMatthias Ringwald #include "l2cap.h"
593deb3ec6SMatthias Ringwald 
603deb3ec6SMatthias Ringwald #define HFP_HF_FEATURES_SIZE 10
613deb3ec6SMatthias Ringwald #define HFP_AG_FEATURES_SIZE 12
623deb3ec6SMatthias Ringwald 
633deb3ec6SMatthias Ringwald 
643deb3ec6SMatthias Ringwald static const char * hfp_hf_features[] = {
653deb3ec6SMatthias Ringwald     "EC and/or NR function",
663deb3ec6SMatthias Ringwald     "Three-way calling",
673deb3ec6SMatthias Ringwald     "CLI presentation capability",
683deb3ec6SMatthias Ringwald     "Voice recognition activation",
693deb3ec6SMatthias Ringwald     "Remote volume control",
703deb3ec6SMatthias Ringwald 
713deb3ec6SMatthias Ringwald     "Enhanced call status",
723deb3ec6SMatthias Ringwald     "Enhanced call control",
733deb3ec6SMatthias Ringwald 
743deb3ec6SMatthias Ringwald     "Codec negotiation",
753deb3ec6SMatthias Ringwald 
763deb3ec6SMatthias Ringwald     "HF Indicators",
773deb3ec6SMatthias Ringwald     "eSCO S4 (and T2) Settings Supported",
783deb3ec6SMatthias Ringwald     "Reserved for future definition"
793deb3ec6SMatthias Ringwald };
803deb3ec6SMatthias Ringwald 
813deb3ec6SMatthias Ringwald static const char * hfp_ag_features[] = {
823deb3ec6SMatthias Ringwald     "Three-way calling",
833deb3ec6SMatthias Ringwald     "EC and/or NR function",
843deb3ec6SMatthias Ringwald     "Voice recognition function",
853deb3ec6SMatthias Ringwald     "In-band ring tone capability",
863deb3ec6SMatthias Ringwald     "Attach a number to a voice tag",
873deb3ec6SMatthias Ringwald     "Ability to reject a call",
883deb3ec6SMatthias Ringwald     "Enhanced call status",
893deb3ec6SMatthias Ringwald     "Enhanced call control",
903deb3ec6SMatthias Ringwald     "Extended Error Result Codes",
913deb3ec6SMatthias Ringwald     "Codec negotiation",
923deb3ec6SMatthias Ringwald     "HF Indicators",
933deb3ec6SMatthias Ringwald     "eSCO S4 (and T2) Settings Supported",
943deb3ec6SMatthias Ringwald     "Reserved for future definition"
953deb3ec6SMatthias Ringwald };
963deb3ec6SMatthias Ringwald 
978f2a52f4SMatthias Ringwald static btstack_linked_list_t hfp_connections = NULL;
98ce263fc8SMatthias Ringwald static void parse_sequence(hfp_connection_t * context);
9913839019SMatthias Ringwald static btstack_packet_handler_t hfp_callback;
100d68dcce1SMatthias Ringwald static btstack_packet_handler_t rfcomm_packet_handler;
101f4000eebSMatthias Ringwald 
102eddcd308SMatthias Ringwald static hfp_connection_t * sco_establishment_active;
103eddcd308SMatthias Ringwald 
10413839019SMatthias Ringwald void hfp_set_callback(btstack_packet_handler_t callback){
105f4000eebSMatthias Ringwald     hfp_callback = callback;
106f4000eebSMatthias Ringwald }
1073deb3ec6SMatthias Ringwald 
1083deb3ec6SMatthias Ringwald const char * hfp_hf_feature(int index){
1093deb3ec6SMatthias Ringwald     if (index > HFP_HF_FEATURES_SIZE){
1103deb3ec6SMatthias Ringwald         return hfp_hf_features[HFP_HF_FEATURES_SIZE];
1113deb3ec6SMatthias Ringwald     }
1123deb3ec6SMatthias Ringwald     return hfp_hf_features[index];
1133deb3ec6SMatthias Ringwald }
1143deb3ec6SMatthias Ringwald 
1153deb3ec6SMatthias Ringwald const char * hfp_ag_feature(int index){
1163deb3ec6SMatthias Ringwald     if (index > HFP_AG_FEATURES_SIZE){
1173deb3ec6SMatthias Ringwald         return hfp_ag_features[HFP_AG_FEATURES_SIZE];
1183deb3ec6SMatthias Ringwald     }
1193deb3ec6SMatthias Ringwald     return hfp_ag_features[index];
1203deb3ec6SMatthias Ringwald }
1213deb3ec6SMatthias Ringwald 
1223deb3ec6SMatthias Ringwald int send_str_over_rfcomm(uint16_t cid, char * command){
1233deb3ec6SMatthias Ringwald     if (!rfcomm_can_send_packet_now(cid)) return 1;
12474386ee0SMatthias Ringwald     log_info("HFP_TX %s", command);
12528190c0bSMatthias Ringwald     int err = rfcomm_send(cid, (uint8_t*) command, strlen(command));
1263deb3ec6SMatthias Ringwald     if (err){
12728190c0bSMatthias Ringwald         log_error("rfcomm_send -> error 0x%02x \n", err);
1283deb3ec6SMatthias Ringwald     }
1293deb3ec6SMatthias Ringwald     return 1;
1303deb3ec6SMatthias Ringwald }
1313deb3ec6SMatthias Ringwald 
1326a7f44bdSMilanka Ringwald int hfp_supports_codec(uint8_t codec, int codecs_nr, uint8_t * codecs){
133df327ff3SMilanka Ringwald     int i;
134df327ff3SMilanka Ringwald     for (i = 0; i < codecs_nr; i++){
135df327ff3SMilanka Ringwald         if (codecs[i] == codec) return 1;
136df327ff3SMilanka Ringwald     }
137df327ff3SMilanka Ringwald     return 0;
138df327ff3SMilanka Ringwald }
139df327ff3SMilanka 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 
2066a7f44bdSMilanka Ringwald void hfp_emit_codec_event(btstack_packet_handler_t callback, uint8_t status, uint8_t codec){
2076a7f44bdSMilanka Ringwald     if (!callback) return;
2086a7f44bdSMilanka Ringwald     uint8_t event[5];
2096a7f44bdSMilanka Ringwald     event[0] = HCI_EVENT_HFP_META;
2106a7f44bdSMilanka Ringwald     event[1] = sizeof(event) - 2;
2116a7f44bdSMilanka Ringwald     event[2] = HFP_SUBEVENT_CODECS_CONNECTION_COMPLETE;
2126a7f44bdSMilanka Ringwald     event[3] = status; // status 0 == OK
2136a7f44bdSMilanka Ringwald     event[4] = codec;
2146a7f44bdSMilanka Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
2156a7f44bdSMilanka Ringwald }
2166a7f44bdSMilanka Ringwald 
21713839019SMatthias Ringwald void hfp_emit_event(btstack_packet_handler_t callback, uint8_t event_subtype, uint8_t value){
2183deb3ec6SMatthias Ringwald     if (!callback) return;
2193deb3ec6SMatthias Ringwald     uint8_t event[4];
2203deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
2213deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
2223deb3ec6SMatthias Ringwald     event[2] = event_subtype;
2233deb3ec6SMatthias Ringwald     event[3] = value; // status 0 == OK
22413839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
2253deb3ec6SMatthias Ringwald }
2263deb3ec6SMatthias Ringwald 
2278901a7c4SMatthias Ringwald void hfp_emit_connection_event(btstack_packet_handler_t callback, uint8_t event_subtype, uint8_t status, hci_con_handle_t con_handle, bd_addr_t addr){
228a0653c3bSMilanka Ringwald     if (!callback) return;
2298901a7c4SMatthias Ringwald     uint8_t event[12];
2306a7f44bdSMilanka Ringwald     int pos = 0;
2316a7f44bdSMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
2326a7f44bdSMilanka Ringwald     event[pos++] = sizeof(event) - 2;
2336a7f44bdSMilanka Ringwald     event[pos++] = event_subtype;
2346a7f44bdSMilanka Ringwald     event[pos++] = status; // status 0 == OK
2356a7f44bdSMilanka Ringwald     little_endian_store_16(event, pos, con_handle);
2366a7f44bdSMilanka Ringwald     pos += 2;
2376a7f44bdSMilanka Ringwald     reverse_bd_addr(addr,&event[pos]);
2386a7f44bdSMilanka Ringwald     pos += 6;
23913839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
240a0653c3bSMilanka Ringwald }
241a0653c3bSMilanka Ringwald 
24213839019SMatthias Ringwald void hfp_emit_string_event(btstack_packet_handler_t callback, uint8_t event_subtype, const char * value){
243aa4dd815SMatthias Ringwald     if (!callback) return;
244c1797c7dSMatthias Ringwald     uint8_t event[40];
245aa4dd815SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
246aa4dd815SMatthias Ringwald     event[1] = sizeof(event) - 2;
247aa4dd815SMatthias Ringwald     event[2] = event_subtype;
248aa4dd815SMatthias Ringwald     int size = (strlen(value) < sizeof(event) - 4) ? strlen(value) : sizeof(event) - 4;
249aa4dd815SMatthias Ringwald     strncpy((char*)&event[3], value, size);
250aa4dd815SMatthias Ringwald     event[3 + size] = 0;
25113839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
252aa4dd815SMatthias Ringwald }
253aa4dd815SMatthias Ringwald 
2540cb5b971SMatthias Ringwald btstack_linked_list_t * hfp_get_connections(void){
2558f2a52f4SMatthias Ringwald     return (btstack_linked_list_t *) &hfp_connections;
2563deb3ec6SMatthias Ringwald }
2573deb3ec6SMatthias Ringwald 
2583deb3ec6SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid){
259665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
260665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
261665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
262a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
263a0ffb263SMatthias Ringwald         if (hfp_connection->rfcomm_cid == cid){
264a0ffb263SMatthias Ringwald             return hfp_connection;
2653deb3ec6SMatthias Ringwald         }
2663deb3ec6SMatthias Ringwald     }
2673deb3ec6SMatthias Ringwald     return NULL;
2683deb3ec6SMatthias Ringwald }
2693deb3ec6SMatthias Ringwald 
2703deb3ec6SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr){
271665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
272665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
273665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
274a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
275a0ffb263SMatthias Ringwald         if (memcmp(hfp_connection->remote_addr, bd_addr, 6) == 0) {
276a0ffb263SMatthias Ringwald             return hfp_connection;
2773deb3ec6SMatthias Ringwald         }
2783deb3ec6SMatthias Ringwald     }
2793deb3ec6SMatthias Ringwald     return NULL;
2803deb3ec6SMatthias Ringwald }
2813deb3ec6SMatthias Ringwald 
282aa4dd815SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_sco_handle(uint16_t handle){
283665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
284665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
285665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
286a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
287a0ffb263SMatthias Ringwald         if (hfp_connection->sco_handle == handle){
288a0ffb263SMatthias Ringwald             return hfp_connection;
2893deb3ec6SMatthias Ringwald         }
2903deb3ec6SMatthias Ringwald     }
2913deb3ec6SMatthias Ringwald     return NULL;
2923deb3ec6SMatthias Ringwald }
2933deb3ec6SMatthias Ringwald 
294d97d752dSMilanka Ringwald hfp_connection_t * get_hfp_connection_context_for_acl_handle(uint16_t handle){
295d97d752dSMilanka Ringwald     btstack_linked_list_iterator_t it;
296d97d752dSMilanka Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
297d97d752dSMilanka Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
298d97d752dSMilanka Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
299d97d752dSMilanka Ringwald         if (hfp_connection->acl_handle == handle){
300d97d752dSMilanka Ringwald             return hfp_connection;
301d97d752dSMilanka Ringwald         }
302d97d752dSMilanka Ringwald     }
303d97d752dSMilanka Ringwald     return NULL;
304d97d752dSMilanka Ringwald }
305d97d752dSMilanka Ringwald 
306a0ffb263SMatthias Ringwald void hfp_reset_context_flags(hfp_connection_t * hfp_connection){
307a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
308a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 0;
309a0ffb263SMatthias Ringwald     hfp_connection->send_error = 0;
3103deb3ec6SMatthias Ringwald 
311a0ffb263SMatthias Ringwald     hfp_connection->keep_byte = 0;
3123deb3ec6SMatthias Ringwald 
313a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 0;
314a0ffb263SMatthias Ringwald     hfp_connection->operator_name_changed = 0;
3153deb3ec6SMatthias Ringwald 
316a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = 0;
317a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = 0;
3183deb3ec6SMatthias Ringwald 
319a0ffb263SMatthias Ringwald     // establish codecs hfp_connection
320a0ffb263SMatthias Ringwald     hfp_connection->suggested_codec = 0;
3216a7f44bdSMilanka Ringwald     hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
322a0ffb263SMatthias Ringwald     hfp_connection->codec_confirmed = 0;
3233deb3ec6SMatthias Ringwald 
324a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
325a0ffb263SMatthias Ringwald     hfp_connection->call_waiting_notification_enabled = 0;
326a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
327a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
3283deb3ec6SMatthias Ringwald }
3293deb3ec6SMatthias Ringwald 
330fffdd288SMatthias Ringwald static hfp_connection_t * create_hfp_connection_context(void){
331a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = btstack_memory_hfp_connection_get();
332a0ffb263SMatthias Ringwald     if (!hfp_connection) return NULL;
3333deb3ec6SMatthias Ringwald     // init state
334a0ffb263SMatthias Ringwald     memset(hfp_connection,0, sizeof(hfp_connection_t));
3353deb3ec6SMatthias Ringwald 
336a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_IDLE;
337a0ffb263SMatthias Ringwald     hfp_connection->call_state = HFP_CALL_IDLE;
338a0ffb263SMatthias Ringwald     hfp_connection->codecs_state = HFP_CODECS_IDLE;
339aa4dd815SMatthias Ringwald 
340a0ffb263SMatthias Ringwald     hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
341a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
3423deb3ec6SMatthias Ringwald 
343a0ffb263SMatthias Ringwald     hfp_reset_context_flags(hfp_connection);
3443deb3ec6SMatthias Ringwald 
345d63c37a1SMatthias Ringwald     btstack_linked_list_add(&hfp_connections, (btstack_linked_item_t*)hfp_connection);
346a0ffb263SMatthias Ringwald     return hfp_connection;
3473deb3ec6SMatthias Ringwald }
3483deb3ec6SMatthias Ringwald 
349a0ffb263SMatthias Ringwald static void remove_hfp_connection_context(hfp_connection_t * hfp_connection){
350a0ffb263SMatthias Ringwald     btstack_linked_list_remove(&hfp_connections, (btstack_linked_item_t*) hfp_connection);
3513deb3ec6SMatthias Ringwald }
3523deb3ec6SMatthias Ringwald 
3533deb3ec6SMatthias Ringwald static hfp_connection_t * provide_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr){
354a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
355a0ffb263SMatthias Ringwald     if (hfp_connection) return  hfp_connection;
356a0ffb263SMatthias Ringwald     hfp_connection = create_hfp_connection_context();
357a0ffb263SMatthias Ringwald     memcpy(hfp_connection->remote_addr, bd_addr, 6);
358a0ffb263SMatthias Ringwald     return hfp_connection;
3593deb3ec6SMatthias Ringwald }
3603deb3ec6SMatthias Ringwald 
361aa4dd815SMatthias Ringwald /* @param network.
362aa4dd815SMatthias Ringwald  * 0 == no ability to reject a call.
363aa4dd815SMatthias Ringwald  * 1 == ability to reject a call.
364aa4dd815SMatthias Ringwald  */
3653deb3ec6SMatthias Ringwald 
3663deb3ec6SMatthias Ringwald /* @param suported_features
3673deb3ec6SMatthias Ringwald  * HF bit 0: EC and/or NR function (yes/no, 1 = yes, 0 = no)
3683deb3ec6SMatthias Ringwald  * HF bit 1: Call waiting or three-way calling(yes/no, 1 = yes, 0 = no)
3693deb3ec6SMatthias Ringwald  * HF bit 2: CLI presentation capability (yes/no, 1 = yes, 0 = no)
3703deb3ec6SMatthias Ringwald  * HF bit 3: Voice recognition activation (yes/no, 1= yes, 0 = no)
3713deb3ec6SMatthias Ringwald  * HF bit 4: Remote volume control (yes/no, 1 = yes, 0 = no)
3723deb3ec6SMatthias Ringwald  * HF bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
3733deb3ec6SMatthias Ringwald  */
3743deb3ec6SMatthias Ringwald  /* Bit position:
3753deb3ec6SMatthias Ringwald  * AG bit 0: Three-way calling (yes/no, 1 = yes, 0 = no)
3763deb3ec6SMatthias Ringwald  * AG bit 1: EC and/or NR function (yes/no, 1 = yes, 0 = no)
3773deb3ec6SMatthias Ringwald  * AG bit 2: Voice recognition function (yes/no, 1 = yes, 0 = no)
3783deb3ec6SMatthias Ringwald  * AG bit 3: In-band ring tone capability (yes/no, 1 = yes, 0 = no)
3793deb3ec6SMatthias Ringwald  * AG bit 4: Attach a phone number to a voice tag (yes/no, 1 = yes, 0 = no)
3803deb3ec6SMatthias Ringwald  * AG bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
3813deb3ec6SMatthias Ringwald  */
3823deb3ec6SMatthias Ringwald 
3839b1c3b4dSMatthias 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){
3843deb3ec6SMatthias Ringwald     uint8_t* attribute;
3853deb3ec6SMatthias Ringwald     de_create_sequence(service);
3863deb3ec6SMatthias Ringwald 
3873deb3ec6SMatthias Ringwald     // 0x0000 "Service Record Handle"
3883deb3ec6SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ServiceRecordHandle);
3899b1c3b4dSMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle);
3903deb3ec6SMatthias Ringwald 
3913deb3ec6SMatthias Ringwald     // 0x0001 "Service Class ID List"
3923deb3ec6SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_ServiceClassIDList);
3933deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
3943deb3ec6SMatthias Ringwald     {
3953deb3ec6SMatthias Ringwald         //  "UUID for Service"
3963deb3ec6SMatthias Ringwald         de_add_number(attribute, DE_UUID, DE_SIZE_16, service_uuid);
3973deb3ec6SMatthias Ringwald         de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_GenericAudio);
3983deb3ec6SMatthias Ringwald     }
3993deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
4003deb3ec6SMatthias Ringwald 
4013deb3ec6SMatthias Ringwald     // 0x0004 "Protocol Descriptor List"
4023deb3ec6SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_ProtocolDescriptorList);
4033deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
4043deb3ec6SMatthias Ringwald     {
4053deb3ec6SMatthias Ringwald         uint8_t* l2cpProtocol = de_push_sequence(attribute);
4063deb3ec6SMatthias Ringwald         {
4073deb3ec6SMatthias Ringwald             de_add_number(l2cpProtocol,  DE_UUID, DE_SIZE_16, SDP_L2CAPProtocol);
4083deb3ec6SMatthias Ringwald         }
4093deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, l2cpProtocol);
4103deb3ec6SMatthias Ringwald 
4113deb3ec6SMatthias Ringwald         uint8_t* rfcomm = de_push_sequence(attribute);
4123deb3ec6SMatthias Ringwald         {
4133deb3ec6SMatthias Ringwald             de_add_number(rfcomm,  DE_UUID, DE_SIZE_16, SDP_RFCOMMProtocol);  // rfcomm_service
4143deb3ec6SMatthias Ringwald             de_add_number(rfcomm,  DE_UINT, DE_SIZE_8,  rfcomm_channel_nr);  // rfcomm channel
4153deb3ec6SMatthias Ringwald         }
4163deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, rfcomm);
4173deb3ec6SMatthias Ringwald     }
4183deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
4193deb3ec6SMatthias Ringwald 
4203deb3ec6SMatthias Ringwald 
4213deb3ec6SMatthias Ringwald     // 0x0005 "Public Browse Group"
4223deb3ec6SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_BrowseGroupList); // public browse group
4233deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
4243deb3ec6SMatthias Ringwald     {
4253deb3ec6SMatthias Ringwald         de_add_number(attribute,  DE_UUID, DE_SIZE_16, SDP_PublicBrowseGroup);
4263deb3ec6SMatthias Ringwald     }
4273deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
4283deb3ec6SMatthias Ringwald 
4293deb3ec6SMatthias Ringwald     // 0x0009 "Bluetooth Profile Descriptor List"
4303deb3ec6SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_BluetoothProfileDescriptorList);
4313deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
4323deb3ec6SMatthias Ringwald     {
4333deb3ec6SMatthias Ringwald         uint8_t *sppProfile = de_push_sequence(attribute);
4343deb3ec6SMatthias Ringwald         {
4353deb3ec6SMatthias Ringwald             de_add_number(sppProfile,  DE_UUID, DE_SIZE_16, SDP_Handsfree);
4363deb3ec6SMatthias Ringwald             de_add_number(sppProfile,  DE_UINT, DE_SIZE_16, 0x0107); // Verision 1.7
4373deb3ec6SMatthias Ringwald         }
4383deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, sppProfile);
4393deb3ec6SMatthias Ringwald     }
4403deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
4413deb3ec6SMatthias Ringwald 
4423deb3ec6SMatthias Ringwald     // 0x0100 "Service Name"
4433deb3ec6SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0100);
4443deb3ec6SMatthias Ringwald     de_add_data(service,  DE_STRING, strlen(name), (uint8_t *) name);
4453deb3ec6SMatthias Ringwald }
4463deb3ec6SMatthias Ringwald 
4473deb3ec6SMatthias Ringwald static hfp_connection_t * connection_doing_sdp_query = NULL;
448a8747467SMatthias Ringwald 
4496c927b22SMatthias Ringwald static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
450a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = connection_doing_sdp_query;
4513deb3ec6SMatthias Ringwald 
452d63c37a1SMatthias Ringwald     if ( hfp_connection->state != HFP_W4_SDP_QUERY_COMPLETE) return;
4533deb3ec6SMatthias Ringwald 
4540e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
4555611a760SMatthias Ringwald         case SDP_EVENT_QUERY_RFCOMM_SERVICE:
456a0ffb263SMatthias Ringwald             if (!hfp_connection) {
45772b50801SMatthias Ringwald                 log_error("handle_query_rfcomm_event alloc connection for RFCOMM port %u failed", sdp_event_query_rfcomm_service_get_rfcomm_channel(packet));
4583deb3ec6SMatthias Ringwald                 return;
4593deb3ec6SMatthias Ringwald             }
460a0ffb263SMatthias Ringwald             hfp_connection->rfcomm_channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet);
4613deb3ec6SMatthias Ringwald             break;
4625611a760SMatthias Ringwald         case SDP_EVENT_QUERY_COMPLETE:
4633deb3ec6SMatthias Ringwald             connection_doing_sdp_query = NULL;
464a0ffb263SMatthias Ringwald             if (hfp_connection->rfcomm_channel_nr > 0){
465a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RFCOMM_CONNECTED;
466a0ffb263SMatthias 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);
467d68dcce1SMatthias Ringwald                 rfcomm_create_channel(rfcomm_packet_handler, hfp_connection->remote_addr, hfp_connection->rfcomm_channel_nr, NULL);
4683deb3ec6SMatthias Ringwald                 break;
4693deb3ec6SMatthias Ringwald             }
47072b50801SMatthias Ringwald             log_info("rfcomm service not found, status %u.", sdp_event_query_complete_get_status(packet));
4713deb3ec6SMatthias Ringwald             break;
4723deb3ec6SMatthias Ringwald         default:
4733deb3ec6SMatthias Ringwald             break;
4743deb3ec6SMatthias Ringwald     }
4753deb3ec6SMatthias Ringwald }
4763deb3ec6SMatthias Ringwald 
477eddcd308SMatthias Ringwald static void hfp_handle_failed_sco_connection(uint8_t status){
478eddcd308SMatthias Ringwald 
479eddcd308SMatthias Ringwald     if (!sco_establishment_active){
480eddcd308SMatthias Ringwald         log_error("(e)SCO Connection failed but not started by us");
481eddcd308SMatthias Ringwald         return;
482eddcd308SMatthias Ringwald     }
4836c5b2002SMatthias Ringwald     log_error("(e)SCO Connection failed status 0x%02x", status);
484eddcd308SMatthias Ringwald 
485eddcd308SMatthias Ringwald     // invalid params / unspecified error
486eddcd308SMatthias Ringwald     if (status != 0x11 && status != 0x1f) return;
487eddcd308SMatthias Ringwald 
488eddcd308SMatthias Ringwald      switch (sco_establishment_active->link_setting){
489eddcd308SMatthias Ringwald         case HFP_LINK_SETTINGS_D0:
490eddcd308SMatthias Ringwald             return; // no other option left
491eddcd308SMatthias Ringwald         case HFP_LINK_SETTINGS_D1:
492eddcd308SMatthias Ringwald             sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D0;
493eddcd308SMatthias Ringwald             break;
494eddcd308SMatthias Ringwald         case HFP_LINK_SETTINGS_S1:
495eddcd308SMatthias Ringwald             sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D1;
496eddcd308SMatthias Ringwald             break;
497eddcd308SMatthias Ringwald         case HFP_LINK_SETTINGS_S2:
498eddcd308SMatthias Ringwald         case HFP_LINK_SETTINGS_S3:
499eddcd308SMatthias Ringwald         case HFP_LINK_SETTINGS_S4:
500eddcd308SMatthias Ringwald             sco_establishment_active->link_setting = HFP_LINK_SETTINGS_S1;
501eddcd308SMatthias Ringwald             break;
502eddcd308SMatthias Ringwald         case HFP_LINK_SETTINGS_T1:
503eddcd308SMatthias Ringwald         case HFP_LINK_SETTINGS_T2:
504eddcd308SMatthias Ringwald             sco_establishment_active->link_setting = HFP_LINK_SETTINGS_S3;
505eddcd308SMatthias Ringwald             break;
506eddcd308SMatthias Ringwald     }
507eddcd308SMatthias Ringwald     sco_establishment_active->establish_audio_connection = 1;
508eddcd308SMatthias Ringwald     sco_establishment_active = 0;
509eddcd308SMatthias Ringwald }
510eddcd308SMatthias Ringwald 
511eddcd308SMatthias Ringwald 
512e30a6a47SMatthias Ringwald void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
5133deb3ec6SMatthias Ringwald     bd_addr_t event_addr;
5143deb3ec6SMatthias Ringwald     uint16_t rfcomm_cid, handle;
515a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = NULL;
516674515e8SMatthias Ringwald     uint8_t status;
5173deb3ec6SMatthias Ringwald 
518eddcd308SMatthias Ringwald     log_info("AG packet_handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size);
519aa4dd815SMatthias Ringwald 
5200e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
521011577abSMilanka Ringwald         case HCI_EVENT_CONNECTION_REQUEST:
522011577abSMilanka Ringwald             // printf("hfp HCI_EVENT_CONNECTION_REQUEST\n");
523011577abSMilanka Ringwald 
524011577abSMilanka Ringwald             hci_event_connection_request_get_bd_addr(packet, event_addr);
525011577abSMilanka Ringwald             hfp_connection = provide_hfp_connection_context_for_bd_addr(event_addr);
526011577abSMilanka Ringwald 
527011577abSMilanka Ringwald             if (!hfp_connection) break;
528*abffc819SMilanka Ringwald             hfp_connection->ag_establish_SCO = 1;
529011577abSMilanka Ringwald             break;
5303deb3ec6SMatthias Ringwald 
5313deb3ec6SMatthias Ringwald         case RFCOMM_EVENT_INCOMING_CONNECTION:
5323deb3ec6SMatthias Ringwald             // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
533caa82391SMilanka Ringwald             rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
534d63c37a1SMatthias Ringwald             hfp_connection = provide_hfp_connection_context_for_bd_addr(event_addr);
535d63c37a1SMatthias Ringwald             if (!hfp_connection || hfp_connection->state != HFP_IDLE) return;
5363deb3ec6SMatthias Ringwald 
537caa82391SMilanka Ringwald             hfp_connection->rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
538d63c37a1SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_CONNECTED;
539674515e8SMatthias Ringwald             // printf("RFCOMM channel %u requested for %s\n", hfp_connection->rfcomm_cid, bd_addr_to_str(hfp_connection->remote_addr));
540d63c37a1SMatthias Ringwald             rfcomm_accept_connection(hfp_connection->rfcomm_cid);
5413deb3ec6SMatthias Ringwald             break;
5423deb3ec6SMatthias Ringwald 
543f8f6a918SMatthias Ringwald         case RFCOMM_EVENT_CHANNEL_OPENED:
5443deb3ec6SMatthias Ringwald             // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16)
545aa4dd815SMatthias Ringwald 
546674515e8SMatthias Ringwald             rfcomm_event_channel_opened_get_bd_addr(packet, event_addr);
547674515e8SMatthias Ringwald             status = rfcomm_event_channel_opened_get_status(packet);
548674515e8SMatthias Ringwald 
549d63c37a1SMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr);
550d63c37a1SMatthias Ringwald             if (!hfp_connection || hfp_connection->state != HFP_W4_RFCOMM_CONNECTED) return;
5513deb3ec6SMatthias Ringwald 
552674515e8SMatthias Ringwald             if (status) {
5538901a7c4SMatthias Ringwald                 hfp_emit_connection_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED, status, rfcomm_event_channel_opened_get_con_handle(packet), event_addr);
554d63c37a1SMatthias Ringwald                 remove_hfp_connection_context(hfp_connection);
5553deb3ec6SMatthias Ringwald             } else {
556caa82391SMilanka Ringwald                 hfp_connection->acl_handle = rfcomm_event_channel_opened_get_con_handle(packet);
557caa82391SMilanka Ringwald                 hfp_connection->rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
5586a7f44bdSMilanka Ringwald                 bd_addr_copy(hfp_connection->remote_addr, event_addr);
559674515e8SMatthias Ringwald                 // uint16_t mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
560674515e8SMatthias 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);
5613deb3ec6SMatthias Ringwald 
562d63c37a1SMatthias Ringwald                 switch (hfp_connection->state){
5633deb3ec6SMatthias Ringwald                     case HFP_W4_RFCOMM_CONNECTED:
564d63c37a1SMatthias Ringwald                         hfp_connection->state = HFP_EXCHANGE_SUPPORTED_FEATURES;
5653deb3ec6SMatthias Ringwald                         break;
5663deb3ec6SMatthias Ringwald                     case HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN:
567d63c37a1SMatthias Ringwald                         hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
568674515e8SMatthias Ringwald                         // printf("Shutting down RFCOMM.\n");
5693deb3ec6SMatthias Ringwald                         break;
5703deb3ec6SMatthias Ringwald                     default:
5713deb3ec6SMatthias Ringwald                         break;
5723deb3ec6SMatthias Ringwald                 }
573bb6944afSMatthias Ringwald                 rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
5743deb3ec6SMatthias Ringwald             }
5753deb3ec6SMatthias Ringwald             break;
5763deb3ec6SMatthias Ringwald 
577eddcd308SMatthias Ringwald         case HCI_EVENT_COMMAND_STATUS:
578eddcd308SMatthias Ringwald             if (hci_event_command_status_get_command_opcode(packet) == hci_setup_synchronous_connection.opcode) {
579be2a1a78SMatthias Ringwald                 status = hci_event_command_status_get_status(packet);
5806c5b2002SMatthias Ringwald                 if (status) {
581eddcd308SMatthias Ringwald                     hfp_handle_failed_sco_connection(hci_event_command_status_get_status(packet));
582eddcd308SMatthias Ringwald                }
5836c5b2002SMatthias Ringwald             }
584eddcd308SMatthias Ringwald             break;
585eddcd308SMatthias Ringwald 
5863deb3ec6SMatthias Ringwald         case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{
587011577abSMilanka Ringwald             hci_event_synchronous_connection_complete_get_bd_addr(packet, event_addr);
588011577abSMilanka Ringwald             hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr);
589011577abSMilanka Ringwald             if (!hfp_connection) {
590011577abSMilanka Ringwald                 log_error("HFP: connection does not exist for remote with addr %s.", bd_addr_to_str(event_addr));
591011577abSMilanka Ringwald                 return;
592011577abSMilanka Ringwald             }
593*abffc819SMilanka Ringwald             hfp_connection->ag_establish_SCO = 0;
594ce263fc8SMatthias Ringwald 
595011577abSMilanka Ringwald             status = hci_event_synchronous_connection_complete_get_status(packet);
596aa4dd815SMatthias Ringwald             if (status != 0){
597eddcd308SMatthias Ringwald                 hfp_handle_failed_sco_connection(status);
598aa4dd815SMatthias Ringwald                 break;
599aa4dd815SMatthias Ringwald             }
600aa4dd815SMatthias Ringwald 
601011577abSMilanka Ringwald             uint16_t sco_handle = hci_event_synchronous_connection_complete_get_handle(packet);
602011577abSMilanka Ringwald             uint8_t  link_type = hci_event_synchronous_connection_complete_get_link_type(packet);
603011577abSMilanka Ringwald             uint8_t  transmission_interval = hci_event_synchronous_connection_complete_get_transmission_interval(packet);  // measured in slots
604011577abSMilanka Ringwald             uint8_t  retransmission_interval = hci_event_synchronous_connection_complete_get_retransmission_interval(packet);// measured in slots
605011577abSMilanka Ringwald             uint16_t rx_packet_length = hci_event_synchronous_connection_complete_get_rx_packet_length(packet); // measured in bytes
606011577abSMilanka Ringwald             uint16_t tx_packet_length = hci_event_synchronous_connection_complete_get_tx_packet_length(packet); // measured in bytes
607011577abSMilanka Ringwald             uint8_t  air_mode = hci_event_synchronous_connection_complete_get_air_mode(packet);
6083deb3ec6SMatthias Ringwald 
6093deb3ec6SMatthias Ringwald             switch (link_type){
6103deb3ec6SMatthias Ringwald                 case 0x00:
611aa4dd815SMatthias Ringwald                     log_info("SCO Connection established.");
6123deb3ec6SMatthias Ringwald                     if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval);
6133deb3ec6SMatthias Ringwald                     if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval);
6143deb3ec6SMatthias Ringwald                     if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length);
6153deb3ec6SMatthias Ringwald                     if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length);
6163deb3ec6SMatthias Ringwald                     break;
6173deb3ec6SMatthias Ringwald                 case 0x02:
618aa4dd815SMatthias Ringwald                     log_info("eSCO Connection established. \n");
6193deb3ec6SMatthias Ringwald                     break;
6203deb3ec6SMatthias Ringwald                 default:
6213deb3ec6SMatthias Ringwald                     log_error("(e)SCO reserved link_type 0x%2x", link_type);
6223deb3ec6SMatthias Ringwald                     break;
6233deb3ec6SMatthias Ringwald             }
6243deb3ec6SMatthias Ringwald             log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, "
625aa4dd815SMatthias Ringwald                  " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)\n", sco_handle,
626aa4dd815SMatthias Ringwald                  bd_addr_to_str(event_addr), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length, air_mode);
6273deb3ec6SMatthias Ringwald 
628d63c37a1SMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr);
6293deb3ec6SMatthias Ringwald 
630d63c37a1SMatthias Ringwald             if (!hfp_connection) {
631d63c37a1SMatthias Ringwald                 log_error("SCO link created, hfp_connection for address %s not found.", bd_addr_to_str(event_addr));
6323deb3ec6SMatthias Ringwald                 break;
6333deb3ec6SMatthias Ringwald             }
6343deb3ec6SMatthias Ringwald 
635d63c37a1SMatthias Ringwald             if (hfp_connection->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){
636aa4dd815SMatthias Ringwald                 log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN");
637d63c37a1SMatthias Ringwald                 hfp_connection->state = HFP_W2_DISCONNECT_SCO;
638aa4dd815SMatthias Ringwald                 break;
639aa4dd815SMatthias Ringwald             }
640d63c37a1SMatthias Ringwald             hfp_connection->sco_handle = sco_handle;
641d63c37a1SMatthias Ringwald             hfp_connection->establish_audio_connection = 0;
642d63c37a1SMatthias Ringwald             hfp_connection->state = HFP_AUDIO_CONNECTION_ESTABLISHED;
6438901a7c4SMatthias Ringwald             hfp_emit_connection_event(hfp_callback, HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED, packet[2], sco_handle, event_addr);
6443deb3ec6SMatthias Ringwald             break;
6453deb3ec6SMatthias Ringwald         }
6463deb3ec6SMatthias Ringwald 
6473deb3ec6SMatthias Ringwald         case RFCOMM_EVENT_CHANNEL_CLOSED:
648f8fbdce0SMatthias Ringwald             rfcomm_cid = little_endian_read_16(packet,2);
649d63c37a1SMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid);
650d63c37a1SMatthias Ringwald             if (!hfp_connection) break;
651d63c37a1SMatthias Ringwald             if (hfp_connection->state == HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART){
652d63c37a1SMatthias Ringwald                 hfp_connection->state = HFP_IDLE;
653d63c37a1SMatthias Ringwald                 hfp_establish_service_level_connection(hfp_connection->remote_addr, hfp_connection->service_uuid);
6543deb3ec6SMatthias Ringwald                 break;
6553deb3ec6SMatthias Ringwald             }
6563deb3ec6SMatthias Ringwald 
657f4000eebSMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0);
658d63c37a1SMatthias Ringwald             remove_hfp_connection_context(hfp_connection);
6593deb3ec6SMatthias Ringwald             break;
6603deb3ec6SMatthias Ringwald 
6613deb3ec6SMatthias Ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
662f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet,3);
663d63c37a1SMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_sco_handle(handle);
664aa4dd815SMatthias Ringwald 
665d63c37a1SMatthias Ringwald             if (!hfp_connection) break;
666aa4dd815SMatthias Ringwald 
667d63c37a1SMatthias Ringwald             if (hfp_connection->state != HFP_W4_SCO_DISCONNECTED){
668aa4dd815SMatthias Ringwald                 log_info("Received gap disconnect in wrong hfp state");
669aa4dd815SMatthias Ringwald             }
670d63c37a1SMatthias Ringwald             log_info("Check SCO handle: incoming 0x%02x, hfp_connection 0x%02x\n", handle, hfp_connection->sco_handle);
671aa4dd815SMatthias Ringwald 
672d63c37a1SMatthias Ringwald             if (handle == hfp_connection->sco_handle){
673aa4dd815SMatthias Ringwald                 log_info("SCO disconnected, w2 disconnect RFCOMM\n");
674d63c37a1SMatthias Ringwald                 hfp_connection->sco_handle = 0;
675d63c37a1SMatthias Ringwald                 hfp_connection->release_audio_connection = 0;
676d63c37a1SMatthias Ringwald                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
677f4000eebSMatthias Ringwald                 hfp_emit_event(hfp_callback, HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED, 0);
6783deb3ec6SMatthias Ringwald                 break;
6793deb3ec6SMatthias Ringwald             }
6803deb3ec6SMatthias Ringwald             break;
6813deb3ec6SMatthias Ringwald 
682fc64f94aSMatthias Ringwald         default:
6833deb3ec6SMatthias Ringwald             break;
6843deb3ec6SMatthias Ringwald     }
6853deb3ec6SMatthias Ringwald }
6863deb3ec6SMatthias Ringwald 
687aa4dd815SMatthias Ringwald // translates command string into hfp_command_t CMD
688aa4dd815SMatthias Ringwald static hfp_command_t parse_command(const char * line_buffer, int isHandsFree){
689aa4dd815SMatthias Ringwald     int offset = isHandsFree ? 0 : 2;
6903deb3ec6SMatthias Ringwald 
691ce263fc8SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_LIST_CURRENT_CALLS, strlen(HFP_LIST_CURRENT_CALLS)) == 0){
692ce263fc8SMatthias Ringwald         return HFP_CMD_LIST_CURRENT_CALLS;
693ce263fc8SMatthias Ringwald     }
694ce263fc8SMatthias Ringwald 
695ce263fc8SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_SUBSCRIBER_NUMBER_INFORMATION, strlen(HFP_SUBSCRIBER_NUMBER_INFORMATION)) == 0){
696ce263fc8SMatthias Ringwald         return HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION;
697ce263fc8SMatthias Ringwald     }
698ce263fc8SMatthias Ringwald 
699aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_PHONE_NUMBER_FOR_VOICE_TAG, strlen(HFP_PHONE_NUMBER_FOR_VOICE_TAG)) == 0){
700ce263fc8SMatthias Ringwald         if (isHandsFree) return HFP_CMD_AG_SENT_PHONE_NUMBER;
701aa4dd815SMatthias Ringwald         return HFP_CMD_HF_REQUEST_PHONE_NUMBER;
7023deb3ec6SMatthias Ringwald     }
7033deb3ec6SMatthias Ringwald 
704aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_TRANSMIT_DTMF_CODES, strlen(HFP_TRANSMIT_DTMF_CODES)) == 0){
705aa4dd815SMatthias Ringwald         return HFP_CMD_TRANSMIT_DTMF_CODES;
7063deb3ec6SMatthias Ringwald     }
7073deb3ec6SMatthias Ringwald 
708aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_SET_MICROPHONE_GAIN, strlen(HFP_SET_MICROPHONE_GAIN)) == 0){
709aa4dd815SMatthias Ringwald         return HFP_CMD_SET_MICROPHONE_GAIN;
7103deb3ec6SMatthias Ringwald     }
7113deb3ec6SMatthias Ringwald 
712aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_SET_SPEAKER_GAIN, strlen(HFP_SET_SPEAKER_GAIN)) == 0){
713aa4dd815SMatthias Ringwald         return HFP_CMD_SET_SPEAKER_GAIN;
7143deb3ec6SMatthias Ringwald     }
7153deb3ec6SMatthias Ringwald 
716aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_ACTIVATE_VOICE_RECOGNITION, strlen(HFP_ACTIVATE_VOICE_RECOGNITION)) == 0){
717aa4dd815SMatthias Ringwald         if (isHandsFree) return HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION;
718aa4dd815SMatthias Ringwald         return HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
7193deb3ec6SMatthias Ringwald     }
7203deb3ec6SMatthias Ringwald 
721aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_TURN_OFF_EC_AND_NR, strlen(HFP_TURN_OFF_EC_AND_NR)) == 0){
722aa4dd815SMatthias Ringwald         return HFP_CMD_TURN_OFF_EC_AND_NR;
7233deb3ec6SMatthias Ringwald     }
7243deb3ec6SMatthias Ringwald 
725aa4dd815SMatthias Ringwald     if (strncmp(line_buffer, HFP_CALL_ANSWERED, strlen(HFP_CALL_ANSWERED)) == 0){
726aa4dd815SMatthias Ringwald         return HFP_CMD_CALL_ANSWERED;
7273deb3ec6SMatthias Ringwald     }
7283deb3ec6SMatthias Ringwald 
729aa4dd815SMatthias Ringwald     if (strncmp(line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0){
730aa4dd815SMatthias Ringwald         return HFP_CMD_CALL_PHONE_NUMBER;
7313deb3ec6SMatthias Ringwald     }
7323deb3ec6SMatthias Ringwald 
733ce263fc8SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_REDIAL_LAST_NUMBER, strlen(HFP_REDIAL_LAST_NUMBER)) == 0){
734aa4dd815SMatthias Ringwald         return HFP_CMD_REDIAL_LAST_NUMBER;
7353deb3ec6SMatthias Ringwald     }
7363deb3ec6SMatthias Ringwald 
737aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_CHANGE_IN_BAND_RING_TONE_SETTING, strlen(HFP_CHANGE_IN_BAND_RING_TONE_SETTING)) == 0){
738aa4dd815SMatthias Ringwald         return HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING;
739aa4dd815SMatthias Ringwald     }
740aa4dd815SMatthias Ringwald 
741aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_HANG_UP_CALL, strlen(HFP_HANG_UP_CALL)) == 0){
742aa4dd815SMatthias Ringwald         return HFP_CMD_HANG_UP_CALL;
743aa4dd815SMatthias Ringwald     }
744aa4dd815SMatthias Ringwald 
745aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_ERROR, strlen(HFP_ERROR)) == 0){
746aa4dd815SMatthias Ringwald         return HFP_CMD_ERROR;
747aa4dd815SMatthias Ringwald     }
748aa4dd815SMatthias Ringwald 
749ce263fc8SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_RING, strlen(HFP_RING)) == 0){
750ce263fc8SMatthias Ringwald         return HFP_CMD_RING;
751ce263fc8SMatthias Ringwald     }
752ce263fc8SMatthias Ringwald 
753aa4dd815SMatthias Ringwald     if (isHandsFree && strncmp(line_buffer+offset, HFP_OK, strlen(HFP_OK)) == 0){
754aa4dd815SMatthias Ringwald         return HFP_CMD_OK;
755aa4dd815SMatthias Ringwald     }
756aa4dd815SMatthias Ringwald 
757aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_SUPPORTED_FEATURES, strlen(HFP_SUPPORTED_FEATURES)) == 0){
758aa4dd815SMatthias Ringwald         return HFP_CMD_SUPPORTED_FEATURES;
759aa4dd815SMatthias Ringwald     }
760aa4dd815SMatthias Ringwald 
761ce263fc8SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_TRANSFER_HF_INDICATOR_STATUS, strlen(HFP_TRANSFER_HF_INDICATOR_STATUS)) == 0){
762ce263fc8SMatthias Ringwald         return HFP_CMD_HF_INDICATOR_STATUS;
763ce263fc8SMatthias Ringwald     }
764ce263fc8SMatthias Ringwald 
765ce263fc8SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_RESPONSE_AND_HOLD, strlen(HFP_RESPONSE_AND_HOLD)) == 0){
766ce263fc8SMatthias Ringwald         if (strncmp(line_buffer+strlen(HFP_RESPONSE_AND_HOLD)+offset, "?", 1) == 0){
767ce263fc8SMatthias Ringwald             return HFP_CMD_RESPONSE_AND_HOLD_QUERY;
768ce263fc8SMatthias Ringwald         }
769ce263fc8SMatthias Ringwald         if (strncmp(line_buffer+strlen(HFP_RESPONSE_AND_HOLD)+offset, "=", 1) == 0){
770ce263fc8SMatthias Ringwald             return HFP_CMD_RESPONSE_AND_HOLD_COMMAND;
771ce263fc8SMatthias Ringwald         }
772667ec068SMatthias Ringwald         return HFP_CMD_RESPONSE_AND_HOLD_STATUS;
773ce263fc8SMatthias Ringwald     }
774ce263fc8SMatthias Ringwald 
775aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_INDICATOR, strlen(HFP_INDICATOR)) == 0){
776aa4dd815SMatthias Ringwald         if (strncmp(line_buffer+strlen(HFP_INDICATOR)+offset, "?", 1) == 0){
777aa4dd815SMatthias Ringwald             return HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS;
778aa4dd815SMatthias Ringwald         }
779aa4dd815SMatthias Ringwald 
780aa4dd815SMatthias Ringwald         if (strncmp(line_buffer+strlen(HFP_INDICATOR)+offset, "=?", 2) == 0){
781aa4dd815SMatthias Ringwald             return HFP_CMD_RETRIEVE_AG_INDICATORS;
782aa4dd815SMatthias Ringwald         }
783aa4dd815SMatthias Ringwald     }
784aa4dd815SMatthias Ringwald 
785aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_AVAILABLE_CODECS, strlen(HFP_AVAILABLE_CODECS)) == 0){
786aa4dd815SMatthias Ringwald         return HFP_CMD_AVAILABLE_CODECS;
787aa4dd815SMatthias Ringwald     }
788aa4dd815SMatthias Ringwald 
789aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, strlen(HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS)) == 0){
790aa4dd815SMatthias Ringwald         return HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE;
791aa4dd815SMatthias Ringwald     }
792aa4dd815SMatthias Ringwald 
793aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_ENABLE_CLIP, strlen(HFP_ENABLE_CLIP)) == 0){
794a0ffb263SMatthias Ringwald         if (isHandsFree) return HFP_CMD_AG_SENT_CLIP_INFORMATION;
795aa4dd815SMatthias Ringwald         return HFP_CMD_ENABLE_CLIP;
796aa4dd815SMatthias Ringwald     }
797aa4dd815SMatthias Ringwald 
798aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_ENABLE_CALL_WAITING_NOTIFICATION, strlen(HFP_ENABLE_CALL_WAITING_NOTIFICATION)) == 0){
799a0ffb263SMatthias Ringwald         if (isHandsFree) return HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE;
800aa4dd815SMatthias Ringwald         return HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION;
801aa4dd815SMatthias Ringwald     }
802aa4dd815SMatthias Ringwald 
803aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, strlen(HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES)) == 0){
804aa4dd815SMatthias Ringwald 
805aa4dd815SMatthias Ringwald         if (isHandsFree) return HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES;
806aa4dd815SMatthias Ringwald 
807aa4dd815SMatthias Ringwald         if (strncmp(line_buffer+strlen(HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES)+offset, "=?", 2) == 0){
808aa4dd815SMatthias Ringwald             return HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES;
809aa4dd815SMatthias Ringwald         }
810667ec068SMatthias Ringwald         if (strncmp(line_buffer+strlen(HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES)+offset, "=", 1) == 0){
811aa4dd815SMatthias Ringwald             return HFP_CMD_CALL_HOLD;
812aa4dd815SMatthias Ringwald         }
813aa4dd815SMatthias Ringwald 
814aa4dd815SMatthias Ringwald         return HFP_CMD_UNKNOWN;
815aa4dd815SMatthias Ringwald     }
816aa4dd815SMatthias Ringwald 
817aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_GENERIC_STATUS_INDICATOR, strlen(HFP_GENERIC_STATUS_INDICATOR)) == 0){
818667ec068SMatthias Ringwald         if (isHandsFree) {
819667ec068SMatthias Ringwald             return HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS;
820667ec068SMatthias Ringwald         }
821aa4dd815SMatthias Ringwald         if (strncmp(line_buffer+strlen(HFP_GENERIC_STATUS_INDICATOR)+offset, "=?", 2) == 0){
822aa4dd815SMatthias Ringwald             return HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS;
823aa4dd815SMatthias Ringwald         }
824aa4dd815SMatthias Ringwald         if (strncmp(line_buffer+strlen(HFP_GENERIC_STATUS_INDICATOR)+offset, "=", 1) == 0){
825aa4dd815SMatthias Ringwald             return HFP_CMD_LIST_GENERIC_STATUS_INDICATORS;
826aa4dd815SMatthias Ringwald         }
827aa4dd815SMatthias Ringwald         return HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE;
828aa4dd815SMatthias Ringwald     }
829aa4dd815SMatthias Ringwald 
830aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS, strlen(HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS)) == 0){
831aa4dd815SMatthias Ringwald         return HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE;
8323deb3ec6SMatthias Ringwald     }
8333deb3ec6SMatthias Ringwald 
8343deb3ec6SMatthias Ringwald 
835aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_QUERY_OPERATOR_SELECTION, strlen(HFP_QUERY_OPERATOR_SELECTION)) == 0){
836aa4dd815SMatthias Ringwald         if (strncmp(line_buffer+strlen(HFP_QUERY_OPERATOR_SELECTION)+offset, "=", 1) == 0){
837aa4dd815SMatthias Ringwald             return HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT;
8383deb3ec6SMatthias Ringwald         }
839aa4dd815SMatthias Ringwald         return HFP_CMD_QUERY_OPERATOR_SELECTION_NAME;
8403deb3ec6SMatthias Ringwald     }
8413deb3ec6SMatthias Ringwald 
842aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_TRANSFER_AG_INDICATOR_STATUS, strlen(HFP_TRANSFER_AG_INDICATOR_STATUS)) == 0){
843aa4dd815SMatthias Ringwald         return HFP_CMD_TRANSFER_AG_INDICATOR_STATUS;
8443deb3ec6SMatthias Ringwald     }
8453deb3ec6SMatthias Ringwald 
846aa4dd815SMatthias Ringwald     if (isHandsFree && strncmp(line_buffer+offset, HFP_EXTENDED_AUDIO_GATEWAY_ERROR, strlen(HFP_EXTENDED_AUDIO_GATEWAY_ERROR)) == 0){
847aa4dd815SMatthias Ringwald         return HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR;
8483deb3ec6SMatthias Ringwald     }
8493deb3ec6SMatthias Ringwald 
850aa4dd815SMatthias Ringwald     if (!isHandsFree && strncmp(line_buffer+offset, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, strlen(HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR)) == 0){
851aa4dd815SMatthias Ringwald         return HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR;
8523deb3ec6SMatthias Ringwald     }
8533deb3ec6SMatthias Ringwald 
854aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_TRIGGER_CODEC_CONNECTION_SETUP, strlen(HFP_TRIGGER_CODEC_CONNECTION_SETUP)) == 0){
855aa4dd815SMatthias Ringwald         return HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP;
856aa4dd815SMatthias Ringwald     }
857aa4dd815SMatthias Ringwald 
858aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_CONFIRM_COMMON_CODEC, strlen(HFP_CONFIRM_COMMON_CODEC)) == 0){
8593deb3ec6SMatthias Ringwald         if (isHandsFree){
860aa4dd815SMatthias Ringwald             return HFP_CMD_AG_SUGGESTED_CODEC;
8613deb3ec6SMatthias Ringwald         } else {
862aa4dd815SMatthias Ringwald             return HFP_CMD_HF_CONFIRMED_CODEC;
8633deb3ec6SMatthias Ringwald         }
8643deb3ec6SMatthias Ringwald     }
8653deb3ec6SMatthias Ringwald 
866aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, "AT+", 3) == 0){
867667ec068SMatthias Ringwald         log_info("process unknown HF command %s \n", line_buffer);
868aa4dd815SMatthias Ringwald         return HFP_CMD_UNKNOWN;
8693deb3ec6SMatthias Ringwald     }
8703deb3ec6SMatthias Ringwald 
871aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, "+", 1) == 0){
872667ec068SMatthias Ringwald         log_info(" process unknown AG command %s \n", line_buffer);
873aa4dd815SMatthias Ringwald         return HFP_CMD_UNKNOWN;
874aa4dd815SMatthias Ringwald     }
8753deb3ec6SMatthias Ringwald 
876aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, "NOP", 3) == 0){
877aa4dd815SMatthias Ringwald         return HFP_CMD_NONE;
878aa4dd815SMatthias Ringwald     }
879aa4dd815SMatthias Ringwald 
880aa4dd815SMatthias Ringwald     return HFP_CMD_NONE;
8813deb3ec6SMatthias Ringwald }
8823deb3ec6SMatthias Ringwald 
883a0ffb263SMatthias Ringwald static void hfp_parser_store_byte(hfp_connection_t * hfp_connection, uint8_t byte){
884ce263fc8SMatthias Ringwald     // printf("hfp_parser_store_byte %c at pos %u\n", (char) byte, context->line_size);
8853deb3ec6SMatthias Ringwald     // TODO: add limit
886a0ffb263SMatthias Ringwald     hfp_connection->line_buffer[hfp_connection->line_size++] = byte;
887a0ffb263SMatthias Ringwald     hfp_connection->line_buffer[hfp_connection->line_size] = 0;
8883deb3ec6SMatthias Ringwald }
889a0ffb263SMatthias Ringwald static int hfp_parser_is_buffer_empty(hfp_connection_t * hfp_connection){
890a0ffb263SMatthias Ringwald     return hfp_connection->line_size == 0;
8913deb3ec6SMatthias Ringwald }
8923deb3ec6SMatthias Ringwald 
8933deb3ec6SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
8943deb3ec6SMatthias Ringwald     return byte == '\n' || byte == '\r';
8953deb3ec6SMatthias Ringwald }
8963deb3ec6SMatthias Ringwald 
8973deb3ec6SMatthias Ringwald static int hfp_parser_is_end_of_header(uint8_t byte){
8983deb3ec6SMatthias Ringwald     return hfp_parser_is_end_of_line(byte) || byte == ':' || byte == '?';
8993deb3ec6SMatthias Ringwald }
9003deb3ec6SMatthias Ringwald 
901a0ffb263SMatthias Ringwald static int hfp_parser_found_separator(hfp_connection_t * hfp_connection, uint8_t byte){
902a0ffb263SMatthias Ringwald     if (hfp_connection->keep_byte == 1) return 1;
9033deb3ec6SMatthias Ringwald 
9043deb3ec6SMatthias Ringwald     int found_separator =   byte == ',' || byte == '\n'|| byte == '\r'||
9053deb3ec6SMatthias Ringwald                             byte == ')' || byte == '(' || byte == ':' ||
9063deb3ec6SMatthias Ringwald                             byte == '-' || byte == '"' ||  byte == '?'|| byte == '=';
9073deb3ec6SMatthias Ringwald     return found_separator;
9083deb3ec6SMatthias Ringwald }
9093deb3ec6SMatthias Ringwald 
910a0ffb263SMatthias Ringwald static void hfp_parser_next_state(hfp_connection_t * hfp_connection, uint8_t byte){
911a0ffb263SMatthias Ringwald     hfp_connection->line_size = 0;
9123deb3ec6SMatthias Ringwald     if (hfp_parser_is_end_of_line(byte)){
913a0ffb263SMatthias Ringwald         hfp_connection->parser_item_index = 0;
914a0ffb263SMatthias Ringwald         hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
9153deb3ec6SMatthias Ringwald         return;
9163deb3ec6SMatthias Ringwald     }
917a0ffb263SMatthias Ringwald     switch (hfp_connection->parser_state){
9183deb3ec6SMatthias Ringwald         case HFP_PARSER_CMD_HEADER:
919a0ffb263SMatthias Ringwald             hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
920a0ffb263SMatthias Ringwald             if (hfp_connection->keep_byte == 1){
921a0ffb263SMatthias Ringwald                 hfp_parser_store_byte(hfp_connection, byte);
922a0ffb263SMatthias Ringwald                 hfp_connection->keep_byte = 0;
9233deb3ec6SMatthias Ringwald             }
9243deb3ec6SMatthias Ringwald             break;
9253deb3ec6SMatthias Ringwald         case HFP_PARSER_CMD_SEQUENCE:
926a0ffb263SMatthias Ringwald             switch (hfp_connection->command){
927ce263fc8SMatthias Ringwald                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
928a0ffb263SMatthias Ringwald                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
929a0ffb263SMatthias Ringwald                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
9303deb3ec6SMatthias Ringwald                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
931aa4dd815SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
932aa4dd815SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
933aa4dd815SMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
934aa4dd815SMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
935ce263fc8SMatthias Ringwald                 case HFP_CMD_HF_INDICATOR_STATUS:
936a0ffb263SMatthias Ringwald                     hfp_connection->parser_state = HFP_PARSER_SECOND_ITEM;
9373deb3ec6SMatthias Ringwald                     break;
9383deb3ec6SMatthias Ringwald                 default:
9393deb3ec6SMatthias Ringwald                     break;
9403deb3ec6SMatthias Ringwald             }
9413deb3ec6SMatthias Ringwald             break;
9423deb3ec6SMatthias Ringwald         case HFP_PARSER_SECOND_ITEM:
943a0ffb263SMatthias Ringwald             hfp_connection->parser_state = HFP_PARSER_THIRD_ITEM;
9443deb3ec6SMatthias Ringwald             break;
9453deb3ec6SMatthias Ringwald         case HFP_PARSER_THIRD_ITEM:
946a0ffb263SMatthias Ringwald             if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS){
947a0ffb263SMatthias Ringwald                 hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
9483deb3ec6SMatthias Ringwald                 break;
9493deb3ec6SMatthias Ringwald             }
950a0ffb263SMatthias Ringwald             hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
9513deb3ec6SMatthias Ringwald             break;
9523deb3ec6SMatthias Ringwald     }
9533deb3ec6SMatthias Ringwald }
9543deb3ec6SMatthias Ringwald 
955a0ffb263SMatthias Ringwald void hfp_parse(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){
956aa4dd815SMatthias Ringwald     // handle ATD<dial_string>;
957a0ffb263SMatthias Ringwald     if (strncmp((const char*)hfp_connection->line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0){
958aa4dd815SMatthias Ringwald         // check for end-of-line or ';'
959aa4dd815SMatthias Ringwald         if (byte == ';' || hfp_parser_is_end_of_line(byte)){
960a0ffb263SMatthias Ringwald             hfp_connection->line_buffer[hfp_connection->line_size] = 0;
961a0ffb263SMatthias Ringwald             hfp_connection->line_size = 0;
962a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_CALL_PHONE_NUMBER;
963aa4dd815SMatthias Ringwald         } else {
964a0ffb263SMatthias Ringwald             hfp_connection->line_buffer[hfp_connection->line_size++] = byte;
965aa4dd815SMatthias Ringwald         }
966aa4dd815SMatthias Ringwald         return;
967aa4dd815SMatthias Ringwald     }
968aa4dd815SMatthias Ringwald 
9693deb3ec6SMatthias Ringwald     // TODO: handle space inside word
970a0ffb263SMatthias Ringwald     if (byte == ' ' && hfp_connection->parser_state > HFP_PARSER_CMD_HEADER) return;
9713deb3ec6SMatthias Ringwald 
972a0ffb263SMatthias Ringwald     if (byte == ',' && hfp_connection->command == HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE){
973a0ffb263SMatthias Ringwald         if (hfp_connection->line_size == 0){
974a0ffb263SMatthias Ringwald             hfp_connection->line_buffer[0] = 0;
975a0ffb263SMatthias Ringwald             hfp_connection->ignore_value = 1;
976a0ffb263SMatthias Ringwald             parse_sequence(hfp_connection);
977ce263fc8SMatthias Ringwald             return;
978ce263fc8SMatthias Ringwald         }
979ce263fc8SMatthias Ringwald     }
980ce263fc8SMatthias Ringwald 
981a0ffb263SMatthias Ringwald     if (!hfp_parser_found_separator(hfp_connection, byte)){
982a0ffb263SMatthias Ringwald         hfp_parser_store_byte(hfp_connection, byte);
9833deb3ec6SMatthias Ringwald         return;
9843deb3ec6SMatthias Ringwald     }
985ce263fc8SMatthias Ringwald 
9863deb3ec6SMatthias Ringwald     if (hfp_parser_is_end_of_line(byte)) {
987a0ffb263SMatthias Ringwald         if (hfp_parser_is_buffer_empty(hfp_connection)){
988a0ffb263SMatthias Ringwald             hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
9893deb3ec6SMatthias Ringwald         }
9903deb3ec6SMatthias Ringwald     }
991a0ffb263SMatthias Ringwald     if (hfp_parser_is_buffer_empty(hfp_connection)) return;
9923deb3ec6SMatthias Ringwald 
993a0ffb263SMatthias Ringwald     switch (hfp_connection->parser_state){
9943deb3ec6SMatthias Ringwald         case HFP_PARSER_CMD_HEADER: // header
9953deb3ec6SMatthias Ringwald             if (byte == '='){
996a0ffb263SMatthias Ringwald                 hfp_connection->keep_byte = 1;
997a0ffb263SMatthias Ringwald                 hfp_parser_store_byte(hfp_connection, byte);
9983deb3ec6SMatthias Ringwald                 return;
9993deb3ec6SMatthias Ringwald             }
10003deb3ec6SMatthias Ringwald 
10013deb3ec6SMatthias Ringwald             if (byte == '?'){
1002a0ffb263SMatthias Ringwald                 hfp_connection->keep_byte = 0;
1003a0ffb263SMatthias Ringwald                 hfp_parser_store_byte(hfp_connection, byte);
10043deb3ec6SMatthias Ringwald                 return;
10053deb3ec6SMatthias Ringwald             }
1006ce263fc8SMatthias Ringwald 
1007ce263fc8SMatthias Ringwald             if (byte == ','){
1008a0ffb263SMatthias Ringwald                 hfp_connection->resolve_byte = 1;
1009ce263fc8SMatthias Ringwald             }
1010ce263fc8SMatthias Ringwald 
1011a0ffb263SMatthias Ringwald             // printf(" parse header 2 %s, keep separator $ %d\n", hfp_connection->line_buffer, hfp_connection->keep_byte);
1012a0ffb263SMatthias Ringwald             if (hfp_parser_is_end_of_header(byte) || hfp_connection->keep_byte == 1){
1013a0ffb263SMatthias Ringwald                 // printf(" parse header 3 %s, keep separator $ %d\n", hfp_connection->line_buffer, hfp_connection->keep_byte);
1014a0ffb263SMatthias Ringwald                 char * line_buffer = (char *)hfp_connection->line_buffer;
1015a0ffb263SMatthias Ringwald                 hfp_connection->command = parse_command(line_buffer, isHandsFree);
1016aa4dd815SMatthias Ringwald 
1017a0ffb263SMatthias Ringwald                 /* resolve command name according to hfp_connection */
1018a0ffb263SMatthias Ringwald                 if (hfp_connection->command == HFP_CMD_UNKNOWN){
1019a0ffb263SMatthias Ringwald                     switch(hfp_connection->state){
1020aa4dd815SMatthias Ringwald                         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
1021a0ffb263SMatthias Ringwald                             hfp_connection->command = HFP_CMD_LIST_GENERIC_STATUS_INDICATORS;
1022aa4dd815SMatthias Ringwald                             break;
1023aa4dd815SMatthias Ringwald                         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
1024a0ffb263SMatthias Ringwald                             hfp_connection->command = HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS;
1025aa4dd815SMatthias Ringwald                             break;
1026aa4dd815SMatthias Ringwald                         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
1027a0ffb263SMatthias Ringwald                             hfp_connection->command = HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE;
1028aa4dd815SMatthias Ringwald                             break;
1029aa4dd815SMatthias Ringwald                         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
1030a0ffb263SMatthias Ringwald                             hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS;
1031aa4dd815SMatthias Ringwald                             break;
1032aa4dd815SMatthias Ringwald                         case HFP_W4_RETRIEVE_INDICATORS:
1033a0ffb263SMatthias Ringwald                             hfp_connection->send_ag_indicators_segment = 0;
1034a0ffb263SMatthias Ringwald                             hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS;
1035aa4dd815SMatthias Ringwald                             break;
1036aa4dd815SMatthias Ringwald                         default:
1037aa4dd815SMatthias Ringwald                             break;
1038aa4dd815SMatthias Ringwald                     }
1039aa4dd815SMatthias Ringwald                 }
10403deb3ec6SMatthias Ringwald             }
10413deb3ec6SMatthias Ringwald             break;
10423deb3ec6SMatthias Ringwald 
1043ce263fc8SMatthias Ringwald         case HFP_PARSER_CMD_SEQUENCE:
1044a0ffb263SMatthias Ringwald             parse_sequence(hfp_connection);
1045ce263fc8SMatthias Ringwald             break;
1046ce263fc8SMatthias Ringwald         case HFP_PARSER_SECOND_ITEM:
1047a0ffb263SMatthias Ringwald             switch (hfp_connection->command){
1048ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1049a0ffb263SMatthias Ringwald                     log_info("format %s, ", hfp_connection->line_buffer);
1050a0ffb263SMatthias Ringwald                     hfp_connection->network_operator.format =  atoi((char *)&hfp_connection->line_buffer[0]);
1051ce263fc8SMatthias Ringwald                     break;
1052ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1053a0ffb263SMatthias Ringwald                     log_info("format %s \n", hfp_connection->line_buffer);
1054a0ffb263SMatthias Ringwald                     hfp_connection->network_operator.format =  atoi((char *)&hfp_connection->line_buffer[0]);
1055ce263fc8SMatthias Ringwald                     break;
1056ce263fc8SMatthias Ringwald                 case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1057ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1058ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
1059a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].state = (uint8_t)atoi((char*)hfp_connection->line_buffer);
1060ce263fc8SMatthias Ringwald                     break;
1061ce263fc8SMatthias Ringwald                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1062a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = (uint8_t)atoi((char*)hfp_connection->line_buffer);
1063a0ffb263SMatthias Ringwald                     log_info("%d \n", hfp_connection->ag_indicators[hfp_connection->parser_item_index].status);
1064a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status_changed = 1;
1065ce263fc8SMatthias Ringwald                     break;
1066ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
1067a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].min_range = atoi((char *)hfp_connection->line_buffer);
1068a0ffb263SMatthias Ringwald                     log_info("%s, ", hfp_connection->line_buffer);
1069ce263fc8SMatthias Ringwald                     break;
1070ce263fc8SMatthias Ringwald                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
1071a0ffb263SMatthias Ringwald                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1072a0ffb263SMatthias Ringwald                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1073a0ffb263SMatthias Ringwald                     hfp_connection->bnip_type = (uint8_t)atoi((char*)hfp_connection->line_buffer);
1074ce263fc8SMatthias Ringwald                     break;
1075ce263fc8SMatthias Ringwald                 default:
1076ce263fc8SMatthias Ringwald                     break;
1077ce263fc8SMatthias Ringwald             }
1078ce263fc8SMatthias Ringwald             break;
1079ce263fc8SMatthias Ringwald 
1080ce263fc8SMatthias Ringwald         case HFP_PARSER_THIRD_ITEM:
1081a0ffb263SMatthias Ringwald              switch (hfp_connection->command){
1082ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1083a0ffb263SMatthias Ringwald                     strcpy(hfp_connection->network_operator.name, (char *)hfp_connection->line_buffer);
1084a0ffb263SMatthias Ringwald                     log_info("name %s\n", hfp_connection->line_buffer);
1085ce263fc8SMatthias Ringwald                     break;
1086ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
1087a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].max_range = atoi((char *)hfp_connection->line_buffer);
1088a0ffb263SMatthias Ringwald                     hfp_connection->parser_item_index++;
1089a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators_nr = hfp_connection->parser_item_index;
1090a0ffb263SMatthias Ringwald                     log_info("%s)\n", hfp_connection->line_buffer);
1091ce263fc8SMatthias Ringwald                     break;
1092ce263fc8SMatthias Ringwald                 default:
1093ce263fc8SMatthias Ringwald                     break;
1094ce263fc8SMatthias Ringwald             }
1095ce263fc8SMatthias Ringwald             break;
1096ce263fc8SMatthias Ringwald     }
1097a0ffb263SMatthias Ringwald     hfp_parser_next_state(hfp_connection, byte);
1098ce263fc8SMatthias Ringwald 
1099a0ffb263SMatthias Ringwald     if (hfp_connection->resolve_byte && hfp_connection->command == HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE){
1100a0ffb263SMatthias Ringwald         hfp_connection->resolve_byte = 0;
1101a0ffb263SMatthias Ringwald         hfp_connection->ignore_value = 1;
1102a0ffb263SMatthias Ringwald         parse_sequence(hfp_connection);
1103a0ffb263SMatthias Ringwald         hfp_connection->line_buffer[0] = 0;
1104a0ffb263SMatthias Ringwald         hfp_connection->line_size = 0;
1105ce263fc8SMatthias Ringwald     }
1106ce263fc8SMatthias Ringwald }
1107ce263fc8SMatthias Ringwald 
1108a0ffb263SMatthias Ringwald static void parse_sequence(hfp_connection_t * hfp_connection){
1109ce263fc8SMatthias Ringwald     int value;
1110a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
1111667ec068SMatthias Ringwald         case HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS:
1112a0ffb263SMatthias Ringwald             value = atoi((char *)&hfp_connection->line_buffer[0]);
1113667ec068SMatthias Ringwald             int i;
1114a0ffb263SMatthias Ringwald             switch (hfp_connection->parser_item_index){
1115667ec068SMatthias Ringwald                 case 0:
1116a0ffb263SMatthias Ringwald                     for (i=0;i<hfp_connection->generic_status_indicators_nr;i++){
1117a0ffb263SMatthias Ringwald                         if (hfp_connection->generic_status_indicators[i].uuid == value){
1118a0ffb263SMatthias Ringwald                             hfp_connection->parser_indicator_index = i;
1119667ec068SMatthias Ringwald                             break;
1120667ec068SMatthias Ringwald                         }
1121667ec068SMatthias Ringwald                     }
1122667ec068SMatthias Ringwald                     break;
1123667ec068SMatthias Ringwald                 case 1:
1124a0ffb263SMatthias Ringwald                     if (hfp_connection->parser_indicator_index <0) break;
1125a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_indicators[hfp_connection->parser_indicator_index].state = value;
1126667ec068SMatthias Ringwald                     log_info("HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS set indicator at index %u, to %u\n",
1127a0ffb263SMatthias Ringwald                      hfp_connection->parser_item_index, value);
1128667ec068SMatthias Ringwald                     break;
1129667ec068SMatthias Ringwald                 default:
1130667ec068SMatthias Ringwald                     break;
1131667ec068SMatthias Ringwald             }
1132a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1133667ec068SMatthias Ringwald             break;
1134667ec068SMatthias Ringwald 
1135667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1136a0ffb263SMatthias Ringwald             switch(hfp_connection->parser_item_index){
1137667ec068SMatthias Ringwald                 case 0:
1138a0ffb263SMatthias Ringwald                     strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1139a0ffb263SMatthias Ringwald                     hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
1140667ec068SMatthias Ringwald                     break;
1141667ec068SMatthias Ringwald                 case 1:
1142a0ffb263SMatthias Ringwald                     value = atoi((char *)&hfp_connection->line_buffer[0]);
1143a0ffb263SMatthias Ringwald                     hfp_connection->bnip_type = value;
1144667ec068SMatthias Ringwald                     break;
1145667ec068SMatthias Ringwald                 default:
1146667ec068SMatthias Ringwald                     break;
1147667ec068SMatthias Ringwald             }
1148a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1149667ec068SMatthias Ringwald             break;
1150667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1151a0ffb263SMatthias Ringwald             switch(hfp_connection->parser_item_index){
1152667ec068SMatthias Ringwald                 case 0:
1153a0ffb263SMatthias Ringwald                     value = atoi((char *)&hfp_connection->line_buffer[0]);
1154a0ffb263SMatthias Ringwald                     hfp_connection->clcc_idx = value;
1155667ec068SMatthias Ringwald                     break;
1156667ec068SMatthias Ringwald                 case 1:
1157a0ffb263SMatthias Ringwald                     value = atoi((char *)&hfp_connection->line_buffer[0]);
1158a0ffb263SMatthias Ringwald                     hfp_connection->clcc_dir = value;
1159667ec068SMatthias Ringwald                     break;
1160667ec068SMatthias Ringwald                 case 2:
1161a0ffb263SMatthias Ringwald                     value = atoi((char *)&hfp_connection->line_buffer[0]);
1162a0ffb263SMatthias Ringwald                     hfp_connection->clcc_status = value;
1163667ec068SMatthias Ringwald                     break;
1164667ec068SMatthias Ringwald                 case 3:
1165a0ffb263SMatthias Ringwald                     value = atoi((char *)&hfp_connection->line_buffer[0]);
1166a0ffb263SMatthias Ringwald                     hfp_connection->clcc_mpty = value;
1167667ec068SMatthias Ringwald                     break;
1168667ec068SMatthias Ringwald                 case 4:
1169a0ffb263SMatthias Ringwald                     strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1170a0ffb263SMatthias Ringwald                     hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
1171667ec068SMatthias Ringwald                     break;
1172667ec068SMatthias Ringwald                 case 5:
1173a0ffb263SMatthias Ringwald                     value = atoi((char *)&hfp_connection->line_buffer[0]);
1174a0ffb263SMatthias Ringwald                     hfp_connection->bnip_type = value;
1175667ec068SMatthias Ringwald                     break;
1176667ec068SMatthias Ringwald                 default:
1177667ec068SMatthias Ringwald                     break;
1178667ec068SMatthias Ringwald             }
1179a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1180667ec068SMatthias Ringwald             break;
1181aa4dd815SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1182a0ffb263SMatthias Ringwald             value = atoi((char *)&hfp_connection->line_buffer[0]);
1183a0ffb263SMatthias Ringwald             hfp_connection->microphone_gain = value;
1184aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_SET_MICROPHONE_GAIN %d\n", value);
1185aa4dd815SMatthias Ringwald             break;
1186aa4dd815SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1187a0ffb263SMatthias Ringwald             value = atoi((char *)&hfp_connection->line_buffer[0]);
1188a0ffb263SMatthias Ringwald             hfp_connection->speaker_gain = value;
1189aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_SET_SPEAKER_GAIN %d\n", value);
1190aa4dd815SMatthias Ringwald             break;
1191aa4dd815SMatthias Ringwald         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
1192a0ffb263SMatthias Ringwald             value = atoi((char *)&hfp_connection->line_buffer[0]);
1193a0ffb263SMatthias Ringwald             hfp_connection->ag_activate_voice_recognition = value;
1194aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION %d\n", value);
1195aa4dd815SMatthias Ringwald             break;
1196aa4dd815SMatthias Ringwald         case HFP_CMD_TURN_OFF_EC_AND_NR:
1197a0ffb263SMatthias Ringwald             value = atoi((char *)&hfp_connection->line_buffer[0]);
1198a0ffb263SMatthias Ringwald             hfp_connection->ag_echo_and_noise_reduction = value;
1199aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_TURN_OFF_EC_AND_NR %d\n", value);
1200aa4dd815SMatthias Ringwald             break;
1201aa4dd815SMatthias Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
1202a0ffb263SMatthias Ringwald             value = atoi((char *)&hfp_connection->line_buffer[0]);
1203a0ffb263SMatthias Ringwald             hfp_connection->remote_supported_features = store_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE, value);
1204aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CHANGE_IN_BAND_RING_TONE_SETTING %d\n", value);
1205aa4dd815SMatthias Ringwald             break;
12063deb3ec6SMatthias Ringwald         case HFP_CMD_HF_CONFIRMED_CODEC:
1207a0ffb263SMatthias Ringwald             hfp_connection->codec_confirmed = atoi((char*)hfp_connection->line_buffer);
1208a0ffb263SMatthias Ringwald             log_info("hfp parse HFP_CMD_HF_CONFIRMED_CODEC %d\n", hfp_connection->codec_confirmed);
12093deb3ec6SMatthias Ringwald             break;
12103deb3ec6SMatthias Ringwald         case HFP_CMD_AG_SUGGESTED_CODEC:
1211a0ffb263SMatthias Ringwald             hfp_connection->suggested_codec = atoi((char*)hfp_connection->line_buffer);
1212a0ffb263SMatthias Ringwald             log_info("hfp parse HFP_CMD_AG_SUGGESTED_CODEC %d\n", hfp_connection->suggested_codec);
12133deb3ec6SMatthias Ringwald             break;
12143deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORTED_FEATURES:
1215a0ffb263SMatthias Ringwald             hfp_connection->remote_supported_features = atoi((char*)hfp_connection->line_buffer);
1216a0ffb263SMatthias Ringwald             log_info("Parsed supported feature %d\n", hfp_connection->remote_supported_features);
12173deb3ec6SMatthias Ringwald             break;
12183deb3ec6SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
1219a0ffb263SMatthias Ringwald             log_info("Parsed codec %s\n", hfp_connection->line_buffer);
1220a0ffb263SMatthias Ringwald             hfp_connection->remote_codecs[hfp_connection->parser_item_index] = (uint16_t)atoi((char*)hfp_connection->line_buffer);
1221a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1222a0ffb263SMatthias Ringwald             hfp_connection->remote_codecs_nr = hfp_connection->parser_item_index;
12233deb3ec6SMatthias Ringwald             break;
1224aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS:
1225a0ffb263SMatthias Ringwald             strcpy((char *)hfp_connection->ag_indicators[hfp_connection->parser_item_index].name,  (char *)hfp_connection->line_buffer);
1226a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators[hfp_connection->parser_item_index].index = hfp_connection->parser_item_index+1;
1227a0ffb263SMatthias Ringwald             log_info("Indicator %d: %s (", hfp_connection->ag_indicators_nr+1, hfp_connection->line_buffer);
1228aa4dd815SMatthias Ringwald             break;
1229aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1230a0ffb263SMatthias Ringwald             log_info("Parsed Indicator %d with status: %s\n", hfp_connection->parser_item_index+1, hfp_connection->line_buffer);
1231a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = atoi((char *) hfp_connection->line_buffer);
1232a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
12333deb3ec6SMatthias Ringwald             break;
12343deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
1235a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1236a0ffb263SMatthias Ringwald             if (hfp_connection->parser_item_index != 4) break;
1237a0ffb263SMatthias Ringwald             log_info("Parsed Enable indicators: %s\n", hfp_connection->line_buffer);
1238a0ffb263SMatthias Ringwald             value = atoi((char *)&hfp_connection->line_buffer[0]);
1239a0ffb263SMatthias Ringwald             hfp_connection->enable_status_update_for_ag_indicators = (uint8_t) value;
12403deb3ec6SMatthias Ringwald             break;
12413deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
1242a0ffb263SMatthias Ringwald             log_info("Parsed Support call hold: %s\n", hfp_connection->line_buffer);
1243a0ffb263SMatthias Ringwald             if (hfp_connection->line_size > 2 ) break;
1244a0ffb263SMatthias Ringwald             strcpy((char *)hfp_connection->remote_call_services[hfp_connection->remote_call_services_nr].name,  (char *)hfp_connection->line_buffer);
1245a0ffb263SMatthias Ringwald             hfp_connection->remote_call_services_nr++;
12463deb3ec6SMatthias Ringwald             break;
1247aa4dd815SMatthias Ringwald         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1248aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1249a0ffb263SMatthias Ringwald             log_info("Parsed Generic status indicator: %s\n", hfp_connection->line_buffer);
1250a0ffb263SMatthias Ringwald             hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].uuid = (uint16_t)atoi((char*)hfp_connection->line_buffer);
1251a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1252a0ffb263SMatthias Ringwald             hfp_connection->generic_status_indicators_nr = hfp_connection->parser_item_index;
12533deb3ec6SMatthias Ringwald             break;
1254aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
12553deb3ec6SMatthias Ringwald             // HF parses inital AG gen. ind. state
1256a0ffb263SMatthias Ringwald             log_info("Parsed List generic status indicator %s state: ", hfp_connection->line_buffer);
1257a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index = (uint8_t)atoi((char*)hfp_connection->line_buffer);
12583deb3ec6SMatthias Ringwald             break;
1259ce263fc8SMatthias Ringwald         case HFP_CMD_HF_INDICATOR_STATUS:
1260a0ffb263SMatthias Ringwald             hfp_connection->parser_indicator_index = (uint8_t)atoi((char*)hfp_connection->line_buffer);
1261a0ffb263SMatthias Ringwald             log_info("Parsed HF indicator index %u", hfp_connection->parser_indicator_index);
1262ce263fc8SMatthias Ringwald             break;
12633deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
12643deb3ec6SMatthias Ringwald             // AG parses new gen. ind. state
1265a0ffb263SMatthias Ringwald             if (hfp_connection->ignore_value){
1266a0ffb263SMatthias Ringwald                 hfp_connection->ignore_value = 0;
1267a0ffb263SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s') - unchanged (stays %u)\n", hfp_connection->parser_item_index,
1268a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled);
1269ce263fc8SMatthias Ringwald             }
1270a0ffb263SMatthias Ringwald             else if (hfp_connection->ag_indicators[hfp_connection->parser_item_index].mandatory){
1271ce263fc8SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s') - ignore (mandatory)\n",
1272a0ffb263SMatthias Ringwald                     hfp_connection->parser_item_index, hfp_connection->ag_indicators[hfp_connection->parser_item_index].name);
1273ce263fc8SMatthias Ringwald             } else {
1274a0ffb263SMatthias Ringwald                 value = atoi((char *)&hfp_connection->line_buffer[0]);
1275a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled = value;
1276a0ffb263SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s'): %u\n", hfp_connection->parser_item_index,
1277a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, value);
12783deb3ec6SMatthias Ringwald             }
1279a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
12803deb3ec6SMatthias Ringwald             break;
12813deb3ec6SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
12823deb3ec6SMatthias Ringwald             // indicators are indexed starting with 1
1283a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index = atoi((char *)&hfp_connection->line_buffer[0]) - 1;
1284a0ffb263SMatthias Ringwald             log_info("Parsed status of the AG indicator %d, status ", hfp_connection->parser_item_index);
12853deb3ec6SMatthias Ringwald             break;
1286aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1287a0ffb263SMatthias Ringwald             hfp_connection->network_operator.mode = atoi((char *)&hfp_connection->line_buffer[0]);
1288a0ffb263SMatthias Ringwald             log_info("Parsed network operator mode: %d, ", hfp_connection->network_operator.mode);
1289aa4dd815SMatthias Ringwald             break;
1290aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1291a0ffb263SMatthias Ringwald             if (hfp_connection->line_buffer[0] == '3'){
1292a0ffb263SMatthias Ringwald                 log_info("Parsed Set network operator format : %s, ", hfp_connection->line_buffer);
12933deb3ec6SMatthias Ringwald                 break;
12943deb3ec6SMatthias Ringwald             }
12953deb3ec6SMatthias Ringwald             // TODO emit ERROR, wrong format
1296a0ffb263SMatthias Ringwald             log_info("ERROR Set network operator format: index %s not supported\n", hfp_connection->line_buffer);
12973deb3ec6SMatthias Ringwald             break;
12983deb3ec6SMatthias Ringwald         case HFP_CMD_ERROR:
12993deb3ec6SMatthias Ringwald             break;
13003deb3ec6SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1301a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 1;
1302a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error_value = (uint8_t)atoi((char*)hfp_connection->line_buffer);
13033deb3ec6SMatthias Ringwald             break;
13043deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
1305a0ffb263SMatthias Ringwald             hfp_connection->enable_extended_audio_gateway_error_report = (uint8_t)atoi((char*)hfp_connection->line_buffer);
1306a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1307a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
13083deb3ec6SMatthias Ringwald             break;
1309ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1310a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1311a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1312a0ffb263SMatthias Ringwald             strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1313a0ffb263SMatthias Ringwald             hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
13143deb3ec6SMatthias Ringwald             break;
13153deb3ec6SMatthias Ringwald         default:
13163deb3ec6SMatthias Ringwald             break;
13173deb3ec6SMatthias Ringwald     }
13183deb3ec6SMatthias Ringwald }
13193deb3ec6SMatthias Ringwald 
13203deb3ec6SMatthias Ringwald void hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid){
1321a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = provide_hfp_connection_context_for_bd_addr(bd_addr);
1322a0ffb263SMatthias Ringwald     log_info("hfp_connect %s, hfp_connection %p", bd_addr_to_str(bd_addr), hfp_connection);
13233deb3ec6SMatthias Ringwald 
1324a0ffb263SMatthias Ringwald     if (!hfp_connection) {
13253deb3ec6SMatthias Ringwald         log_error("hfp_establish_service_level_connection for addr %s failed", bd_addr_to_str(bd_addr));
13263deb3ec6SMatthias Ringwald         return;
13273deb3ec6SMatthias Ringwald     }
1328aa4dd815SMatthias Ringwald 
1329a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
13303deb3ec6SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
1331a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
13323deb3ec6SMatthias Ringwald             return;
13333deb3ec6SMatthias Ringwald         case HFP_W4_RFCOMM_DISCONNECTED:
1334a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART;
13353deb3ec6SMatthias Ringwald             return;
13363deb3ec6SMatthias Ringwald         case HFP_IDLE:
1337a0ffb263SMatthias Ringwald             memcpy(hfp_connection->remote_addr, bd_addr, 6);
1338d63c37a1SMatthias Ringwald             hfp_connection->state = HFP_W4_SDP_QUERY_COMPLETE;
1339a0ffb263SMatthias Ringwald             connection_doing_sdp_query = hfp_connection;
1340a0ffb263SMatthias Ringwald             hfp_connection->service_uuid = service_uuid;
13417fbb5f59SMatthias Ringwald             sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, hfp_connection->remote_addr, service_uuid);
13423deb3ec6SMatthias Ringwald             break;
13433deb3ec6SMatthias Ringwald         default:
13443deb3ec6SMatthias Ringwald             break;
13453deb3ec6SMatthias Ringwald     }
13463deb3ec6SMatthias Ringwald }
13473deb3ec6SMatthias Ringwald 
1348a0ffb263SMatthias Ringwald void hfp_release_service_level_connection(hfp_connection_t * hfp_connection){
1349a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
1350a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
13513deb3ec6SMatthias Ringwald 
1352a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_W4_RFCOMM_CONNECTED){
1353a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_IDLE;
13543deb3ec6SMatthias Ringwald         return;
13553deb3ec6SMatthias Ringwald     }
13563deb3ec6SMatthias Ringwald 
1357a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_W4_RFCOMM_CONNECTED){
1358a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN;
13593deb3ec6SMatthias Ringwald         return;
13603deb3ec6SMatthias Ringwald     }
13613deb3ec6SMatthias Ringwald 
1362a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_W4_SCO_CONNECTED){
1363a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
13643deb3ec6SMatthias Ringwald         return;
13653deb3ec6SMatthias Ringwald     }
13663deb3ec6SMatthias Ringwald 
1367a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_W4_SCO_DISCONNECTED){
1368a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W2_DISCONNECT_SCO;
13693deb3ec6SMatthias Ringwald         return;
13703deb3ec6SMatthias Ringwald     }
13713deb3ec6SMatthias Ringwald 
13723deb3ec6SMatthias Ringwald     return;
13733deb3ec6SMatthias Ringwald }
13743deb3ec6SMatthias Ringwald 
1375a0ffb263SMatthias Ringwald void hfp_release_audio_connection(hfp_connection_t * hfp_connection){
1376a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
1377a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
1378a0ffb263SMatthias Ringwald     hfp_connection->release_audio_connection = 1;
13793deb3ec6SMatthias Ringwald }
13803deb3ec6SMatthias Ringwald 
1381ce263fc8SMatthias Ringwald static const struct link_settings {
1382ce263fc8SMatthias Ringwald     const uint16_t max_latency;
1383ce263fc8SMatthias Ringwald     const uint8_t  retransmission_effort;
1384ce263fc8SMatthias Ringwald     const uint16_t packet_types;
1385ce263fc8SMatthias Ringwald } hfp_link_settings [] = {
1386ce263fc8SMatthias Ringwald     { 0xffff, 0xff, 0x03c1 }, // HFP_LINK_SETTINGS_D0,   HV1
1387ce263fc8SMatthias Ringwald     { 0xffff, 0xff, 0x03c4 }, // HFP_LINK_SETTINGS_D1,   HV3
1388ce263fc8SMatthias Ringwald     { 0x0007, 0x01, 0x03c8 }, // HFP_LINK_SETTINGS_S1,   EV3
1389ce263fc8SMatthias Ringwald     { 0x0007, 0x01, 0x0380 }, // HFP_LINK_SETTINGS_S2, 2-EV3
1390ce263fc8SMatthias Ringwald     { 0x000a, 0x01, 0x0380 }, // HFP_LINK_SETTINGS_S3, 2-EV3
1391ce263fc8SMatthias Ringwald     { 0x000c, 0x02, 0x0380 }, // HFP_LINK_SETTINGS_S4, 2-EV3
1392ce263fc8SMatthias Ringwald     { 0x0008, 0x02, 0x03c8 }, // HFP_LINK_SETTINGS_T1,   EV3
1393ce263fc8SMatthias Ringwald     { 0x000d, 0x02, 0x0380 }  // HFP_LINK_SETTINGS_T2, 2-EV3
1394ce263fc8SMatthias Ringwald };
13953deb3ec6SMatthias Ringwald 
1396eddcd308SMatthias Ringwald void hfp_setup_synchronous_connection(hfp_connection_t * hfp_connection){
1397ce263fc8SMatthias Ringwald     // all packet types, fixed bandwidth
1398eddcd308SMatthias Ringwald     int setting = hfp_connection->link_setting;
1399ce263fc8SMatthias Ringwald     log_info("hfp_setup_synchronous_connection using setting nr %u", setting);
1400eddcd308SMatthias Ringwald     sco_establishment_active = hfp_connection;
1401d2c1d59aSMatthias Ringwald     uint16_t sco_voice_setting = hci_get_sco_voice_setting();
1402d2c1d59aSMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
1403d2c1d59aSMatthias Ringwald         sco_voice_setting = 0x0003; // Transparent data
1404d2c1d59aSMatthias Ringwald     }
1405eddcd308SMatthias Ringwald     hci_send_cmd(&hci_setup_synchronous_connection, hfp_connection->acl_handle, 8000, 8000, hfp_link_settings[setting].max_latency,
1406d2c1d59aSMatthias Ringwald         sco_voice_setting, hfp_link_settings[setting].retransmission_effort, hfp_link_settings[setting].packet_types); // all types 0x003f, only 2-ev3 0x380
1407ce263fc8SMatthias Ringwald }
1408d68dcce1SMatthias Ringwald 
1409d68dcce1SMatthias Ringwald void hfp_set_packet_handler_for_rfcomm_connections(btstack_packet_handler_t handler){
1410d68dcce1SMatthias Ringwald     rfcomm_packet_handler = handler;
1411d68dcce1SMatthias Ringwald }
1412d68dcce1SMatthias Ringwald 
1413