xref: /btstack/src/classic/hfp.c (revision ddaf35c7579ab4463891f4e60cea40b4398b142c)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 
39 #include "btstack_config.h"
40 
41 #include <stdint.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <inttypes.h>
46 
47 #include "btstack_debug.h"
48 #include "btstack_event.h"
49 #include "btstack_memory.h"
50 #include "btstack_run_loop.h"
51 #include "classic/core.h"
52 #include "classic/sdp_client_rfcomm.h"
53 #include "classic/sdp_server.h"
54 #include "classic/sdp_util.h"
55 #include "hci.h"
56 #include "hci_cmd.h"
57 #include "hci_dump.h"
58 #include "l2cap.h"
59 
60 #define HFP_HF_FEATURES_SIZE 10
61 #define HFP_AG_FEATURES_SIZE 12
62 
63 
64 static const char * hfp_hf_features[] = {
65     "EC and/or NR function",
66     "Three-way calling",
67     "CLI presentation capability",
68     "Voice recognition activation",
69     "Remote volume control",
70 
71     "Enhanced call status",
72     "Enhanced call control",
73 
74     "Codec negotiation",
75 
76     "HF Indicators",
77     "eSCO S4 (and T2) Settings Supported",
78     "Reserved for future definition"
79 };
80 
81 static const char * hfp_ag_features[] = {
82     "Three-way calling",
83     "EC and/or NR function",
84     "Voice recognition function",
85     "In-band ring tone capability",
86     "Attach a number to a voice tag",
87     "Ability to reject a call",
88     "Enhanced call status",
89     "Enhanced call control",
90     "Extended Error Result Codes",
91     "Codec negotiation",
92     "HF Indicators",
93     "eSCO S4 (and T2) Settings Supported",
94     "Reserved for future definition"
95 };
96 
97 static btstack_linked_list_t hfp_connections = NULL;
98 static void parse_sequence(hfp_connection_t * context);
99 static btstack_packet_handler_t hfp_callback;
100 static btstack_packet_handler_t rfcomm_packet_handler;
101 
102 static hfp_connection_t * sco_establishment_active;
103 
104 void hfp_set_callback(btstack_packet_handler_t callback){
105     hfp_callback = callback;
106 }
107 
108 const char * hfp_hf_feature(int index){
109     if (index > HFP_HF_FEATURES_SIZE){
110         return hfp_hf_features[HFP_HF_FEATURES_SIZE];
111     }
112     return hfp_hf_features[index];
113 }
114 
115 const char * hfp_ag_feature(int index){
116     if (index > HFP_AG_FEATURES_SIZE){
117         return hfp_ag_features[HFP_AG_FEATURES_SIZE];
118     }
119     return hfp_ag_features[index];
120 }
121 
122 int send_str_over_rfcomm(uint16_t cid, char * command){
123     if (!rfcomm_can_send_packet_now(cid)) return 1;
124     log_info("HFP_TX %s", command);
125     int err = rfcomm_send(cid, (uint8_t*) command, strlen(command));
126     if (err){
127         log_error("rfcomm_send -> error 0x%02x \n", err);
128     }
129     return 1;
130 }
131 
132 int hfp_supports_codec(uint8_t codec, int codecs_nr, uint8_t * codecs){
133 
134     // mSBC requires support for eSCO connections
135     if (codec == HFP_CODEC_MSBC && !hci_extended_sco_link_supported()) return 0;
136 
137     int i;
138     for (i = 0; i < codecs_nr; i++){
139         if (codecs[i] != codec) continue;
140         return 1;
141     }
142     return 0;
143 }
144 
145 #if 0
146 void hfp_set_codec(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){
147     // parse available codecs
148     int pos = 0;
149     int i;
150     for (i=0; i<size; i++){
151         pos+=8;
152         if (packet[pos] > hfp_connection->negotiated_codec){
153             hfp_connection->negotiated_codec = packet[pos];
154         }
155     }
156     printf("Negotiated Codec 0x%02x\n", hfp_connection->negotiated_codec);
157 }
158 #endif
159 
160 // UTILS
161 int get_bit(uint16_t bitmap, int position){
162     return (bitmap >> position) & 1;
163 }
164 
165 int store_bit(uint32_t bitmap, int position, uint8_t value){
166     if (value){
167         bitmap |= 1 << position;
168     } else {
169         bitmap &= ~ (1 << position);
170     }
171     return bitmap;
172 }
173 
174 int join(char * buffer, int buffer_size, uint8_t * values, int values_nr){
175     if (buffer_size < values_nr * 3) return 0;
176     int i;
177     int offset = 0;
178     for (i = 0; i < values_nr-1; i++) {
179       offset += snprintf(buffer+offset, buffer_size-offset, "%d,", values[i]); // puts string into buffer
180     }
181     if (i<values_nr){
182         offset += snprintf(buffer+offset, buffer_size-offset, "%d", values[i]);
183     }
184     return offset;
185 }
186 
187 int join_bitmap(char * buffer, int buffer_size, uint32_t values, int values_nr){
188     if (buffer_size < values_nr * 3) return 0;
189 
190     int i;
191     int offset = 0;
192     for (i = 0; i < values_nr-1; i++) {
193       offset += snprintf(buffer+offset, buffer_size-offset, "%d,", get_bit(values,i)); // puts string into buffer
194     }
195 
196     if (i<values_nr){
197         offset += snprintf(buffer+offset, buffer_size-offset, "%d", get_bit(values,i));
198     }
199     return offset;
200 }
201 
202 void hfp_emit_simple_event(btstack_packet_handler_t callback, uint8_t event_subtype){
203     if (!callback) return;
204     uint8_t event[3];
205     event[0] = HCI_EVENT_HFP_META;
206     event[1] = sizeof(event) - 2;
207     event[2] = event_subtype;
208     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
209 }
210 
211 void hfp_emit_event(btstack_packet_handler_t callback, uint8_t event_subtype, uint8_t value){
212     if (!callback) return;
213     uint8_t event[4];
214     event[0] = HCI_EVENT_HFP_META;
215     event[1] = sizeof(event) - 2;
216     event[2] = event_subtype;
217     event[3] = value; // status 0 == OK
218     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
219 }
220 
221 void hfp_emit_slc_connection_event(btstack_packet_handler_t callback, uint8_t status, hci_con_handle_t con_handle, bd_addr_t addr){
222     if (!callback) return;
223     uint8_t event[12];
224     int pos = 0;
225     event[pos++] = HCI_EVENT_HFP_META;
226     event[pos++] = sizeof(event) - 2;
227     event[pos++] = HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
228     event[pos++] = status; // status 0 == OK
229     little_endian_store_16(event, pos, con_handle);
230     pos += 2;
231     reverse_bd_addr(addr,&event[pos]);
232     pos += 6;
233     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
234 }
235 
236 static void hfp_emit_sco_event(btstack_packet_handler_t callback, uint8_t status, hci_con_handle_t con_handle, bd_addr_t addr, uint8_t  negotiated_codec){
237     if (!callback) return;
238     uint8_t event[13];
239     int pos = 0;
240     event[pos++] = HCI_EVENT_HFP_META;
241     event[pos++] = sizeof(event) - 2;
242     event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED;
243     event[pos++] = status; // status 0 == OK
244     little_endian_store_16(event, pos, con_handle);
245     pos += 2;
246     reverse_bd_addr(addr,&event[pos]);
247     pos += 6;
248     event[pos++] = negotiated_codec;
249     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
250 }
251 
252 void hfp_emit_string_event(btstack_packet_handler_t callback, uint8_t event_subtype, const char * value){
253     if (!callback) return;
254     uint8_t event[40];
255     event[0] = HCI_EVENT_HFP_META;
256     event[1] = sizeof(event) - 2;
257     event[2] = event_subtype;
258     int size = (strlen(value) < sizeof(event) - 4) ? (int) strlen(value) : sizeof(event) - 4;
259     strncpy((char*)&event[3], value, size);
260     event[3 + size] = 0;
261     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
262 }
263 
264 btstack_linked_list_t * hfp_get_connections(void){
265     return (btstack_linked_list_t *) &hfp_connections;
266 }
267 
268 hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid){
269     btstack_linked_list_iterator_t it;
270     btstack_linked_list_iterator_init(&it, hfp_get_connections());
271     while (btstack_linked_list_iterator_has_next(&it)){
272         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
273         if (hfp_connection->rfcomm_cid == cid){
274             return hfp_connection;
275         }
276     }
277     return NULL;
278 }
279 
280 hfp_connection_t * get_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr){
281     btstack_linked_list_iterator_t it;
282     btstack_linked_list_iterator_init(&it, hfp_get_connections());
283     while (btstack_linked_list_iterator_has_next(&it)){
284         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
285         if (memcmp(hfp_connection->remote_addr, bd_addr, 6) == 0) {
286             return hfp_connection;
287         }
288     }
289     return NULL;
290 }
291 
292 hfp_connection_t * get_hfp_connection_context_for_sco_handle(uint16_t handle){
293     btstack_linked_list_iterator_t it;
294     btstack_linked_list_iterator_init(&it, hfp_get_connections());
295     while (btstack_linked_list_iterator_has_next(&it)){
296         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
297         if (hfp_connection->sco_handle == handle){
298             return hfp_connection;
299         }
300     }
301     return NULL;
302 }
303 
304 hfp_connection_t * get_hfp_connection_context_for_acl_handle(uint16_t handle){
305     btstack_linked_list_iterator_t it;
306     btstack_linked_list_iterator_init(&it, hfp_get_connections());
307     while (btstack_linked_list_iterator_has_next(&it)){
308         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
309         if (hfp_connection->acl_handle == handle){
310             return hfp_connection;
311         }
312     }
313     return NULL;
314 }
315 
316 void hfp_reset_context_flags(hfp_connection_t * hfp_connection){
317     if (!hfp_connection) return;
318     hfp_connection->ok_pending = 0;
319     hfp_connection->send_error = 0;
320 
321     hfp_connection->keep_byte = 0;
322 
323     hfp_connection->change_status_update_for_individual_ag_indicators = 0;
324     hfp_connection->operator_name_changed = 0;
325 
326     hfp_connection->enable_extended_audio_gateway_error_report = 0;
327     hfp_connection->extended_audio_gateway_error = 0;
328 
329     // establish codecs hfp_connection
330     hfp_connection->suggested_codec = 0;
331     hfp_connection->negotiated_codec = 0;
332     hfp_connection->codec_confirmed = 0;
333 
334     hfp_connection->establish_audio_connection = 0;
335     hfp_connection->call_waiting_notification_enabled = 0;
336     hfp_connection->command = HFP_CMD_NONE;
337     hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
338 }
339 
340 static hfp_connection_t * create_hfp_connection_context(void){
341     hfp_connection_t * hfp_connection = btstack_memory_hfp_connection_get();
342     if (!hfp_connection) return NULL;
343     // init state
344     memset(hfp_connection,0, sizeof(hfp_connection_t));
345 
346     hfp_connection->state = HFP_IDLE;
347     hfp_connection->call_state = HFP_CALL_IDLE;
348     hfp_connection->codecs_state = HFP_CODECS_IDLE;
349 
350     hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
351     hfp_connection->command = HFP_CMD_NONE;
352 
353     hfp_reset_context_flags(hfp_connection);
354 
355     btstack_linked_list_add(&hfp_connections, (btstack_linked_item_t*)hfp_connection);
356     return hfp_connection;
357 }
358 
359 static void remove_hfp_connection_context(hfp_connection_t * hfp_connection){
360     btstack_linked_list_remove(&hfp_connections, (btstack_linked_item_t*) hfp_connection);
361 }
362 
363 static hfp_connection_t * provide_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr){
364     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
365     if (hfp_connection) return  hfp_connection;
366     hfp_connection = create_hfp_connection_context();
367     memcpy(hfp_connection->remote_addr, bd_addr, 6);
368     return hfp_connection;
369 }
370 
371 /* @param network.
372  * 0 == no ability to reject a call.
373  * 1 == ability to reject a call.
374  */
375 
376 /* @param suported_features
377  * HF bit 0: EC and/or NR function (yes/no, 1 = yes, 0 = no)
378  * HF bit 1: Call waiting or three-way calling(yes/no, 1 = yes, 0 = no)
379  * HF bit 2: CLI presentation capability (yes/no, 1 = yes, 0 = no)
380  * HF bit 3: Voice recognition activation (yes/no, 1= yes, 0 = no)
381  * HF bit 4: Remote volume control (yes/no, 1 = yes, 0 = no)
382  * HF bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
383  */
384  /* Bit position:
385  * AG bit 0: Three-way calling (yes/no, 1 = yes, 0 = no)
386  * AG bit 1: EC and/or NR function (yes/no, 1 = yes, 0 = no)
387  * AG bit 2: Voice recognition function (yes/no, 1 = yes, 0 = no)
388  * AG bit 3: In-band ring tone capability (yes/no, 1 = yes, 0 = no)
389  * AG bit 4: Attach a phone number to a voice tag (yes/no, 1 = yes, 0 = no)
390  * AG bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
391  */
392 
393 void hfp_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint16_t service_uuid, int rfcomm_channel_nr, const char * name){
394     uint8_t* attribute;
395     de_create_sequence(service);
396 
397     // 0x0000 "Service Record Handle"
398     de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ServiceRecordHandle);
399     de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle);
400 
401     // 0x0001 "Service Class ID List"
402     de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_ServiceClassIDList);
403     attribute = de_push_sequence(service);
404     {
405         //  "UUID for Service"
406         de_add_number(attribute, DE_UUID, DE_SIZE_16, service_uuid);
407         de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_GenericAudio);
408     }
409     de_pop_sequence(service, attribute);
410 
411     // 0x0004 "Protocol Descriptor List"
412     de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_ProtocolDescriptorList);
413     attribute = de_push_sequence(service);
414     {
415         uint8_t* l2cpProtocol = de_push_sequence(attribute);
416         {
417             de_add_number(l2cpProtocol,  DE_UUID, DE_SIZE_16, SDP_L2CAPProtocol);
418         }
419         de_pop_sequence(attribute, l2cpProtocol);
420 
421         uint8_t* rfcomm = de_push_sequence(attribute);
422         {
423             de_add_number(rfcomm,  DE_UUID, DE_SIZE_16, SDP_RFCOMMProtocol);  // rfcomm_service
424             de_add_number(rfcomm,  DE_UINT, DE_SIZE_8,  rfcomm_channel_nr);  // rfcomm channel
425         }
426         de_pop_sequence(attribute, rfcomm);
427     }
428     de_pop_sequence(service, attribute);
429 
430 
431     // 0x0005 "Public Browse Group"
432     de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_BrowseGroupList); // public browse group
433     attribute = de_push_sequence(service);
434     {
435         de_add_number(attribute,  DE_UUID, DE_SIZE_16, SDP_PublicBrowseGroup);
436     }
437     de_pop_sequence(service, attribute);
438 
439     // 0x0009 "Bluetooth Profile Descriptor List"
440     de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_BluetoothProfileDescriptorList);
441     attribute = de_push_sequence(service);
442     {
443         uint8_t *sppProfile = de_push_sequence(attribute);
444         {
445             de_add_number(sppProfile,  DE_UUID, DE_SIZE_16, SDP_Handsfree);
446             de_add_number(sppProfile,  DE_UINT, DE_SIZE_16, 0x0107); // Verision 1.7
447         }
448         de_pop_sequence(attribute, sppProfile);
449     }
450     de_pop_sequence(service, attribute);
451 
452     // 0x0100 "Service Name"
453     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0100);
454     de_add_data(service,  DE_STRING, strlen(name), (uint8_t *) name);
455 }
456 
457 static hfp_connection_t * connection_doing_sdp_query = NULL;
458 
459 static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
460     hfp_connection_t * hfp_connection = connection_doing_sdp_query;
461 
462     if ( hfp_connection->state != HFP_W4_SDP_QUERY_COMPLETE) return;
463 
464     switch (hci_event_packet_get_type(packet)){
465         case SDP_EVENT_QUERY_RFCOMM_SERVICE:
466             if (!hfp_connection) {
467                 log_error("handle_query_rfcomm_event alloc connection for RFCOMM port %u failed", sdp_event_query_rfcomm_service_get_rfcomm_channel(packet));
468                 return;
469             }
470             hfp_connection->rfcomm_channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet);
471             break;
472         case SDP_EVENT_QUERY_COMPLETE:
473             connection_doing_sdp_query = NULL;
474             if (hfp_connection->rfcomm_channel_nr > 0){
475                 hfp_connection->state = HFP_W4_RFCOMM_CONNECTED;
476                 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);
477                 rfcomm_create_channel(rfcomm_packet_handler, hfp_connection->remote_addr, hfp_connection->rfcomm_channel_nr, NULL);
478                 break;
479             }
480             log_info("rfcomm service not found, status %u.", sdp_event_query_complete_get_status(packet));
481             break;
482         default:
483             break;
484     }
485 }
486 
487 static void hfp_handle_failed_sco_connection(uint8_t status){
488 
489     if (!sco_establishment_active){
490         log_error("(e)SCO Connection failed but not started by us");
491         return;
492     }
493     log_error("(e)SCO Connection failed status 0x%02x", status);
494 
495     // invalid params / unspecified error
496     if (status != 0x11 && status != 0x1f) return;
497 
498      switch (sco_establishment_active->link_setting){
499         case HFP_LINK_SETTINGS_D0:
500             return; // no other option left
501         case HFP_LINK_SETTINGS_D1:
502             sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D0;
503             break;
504         case HFP_LINK_SETTINGS_S1:
505             sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D1;
506             break;
507         case HFP_LINK_SETTINGS_S2:
508             sco_establishment_active->link_setting = HFP_LINK_SETTINGS_S1;
509             break;
510         case HFP_LINK_SETTINGS_S3:
511             sco_establishment_active->link_setting = HFP_LINK_SETTINGS_S2;
512             break;
513         case HFP_LINK_SETTINGS_S4:
514             sco_establishment_active->link_setting = HFP_LINK_SETTINGS_S3;
515             break;
516         case HFP_LINK_SETTINGS_T1:
517             log_info("T1 failed, fallback to CVSD - D1");
518             sco_establishment_active->negotiated_codec = HFP_CODEC_CVSD;
519             sco_establishment_active->sco_for_msbc_failed = 1;
520             sco_establishment_active->command = HFP_CMD_AG_SEND_COMMON_CODEC;
521             sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D1;
522             break;
523         case HFP_LINK_SETTINGS_T2:
524             sco_establishment_active->link_setting = HFP_LINK_SETTINGS_T1;
525             break;
526     }
527     sco_establishment_active->establish_audio_connection = 1;
528     sco_establishment_active = 0;
529 }
530 
531 
532 void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
533     bd_addr_t event_addr;
534     uint16_t rfcomm_cid, handle;
535     hfp_connection_t * hfp_connection = NULL;
536     uint8_t status;
537 
538     log_info("AG packet_handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size);
539 
540     switch (hci_event_packet_get_type(packet)) {
541         case HCI_EVENT_CONNECTION_REQUEST:
542             // printf("hfp HCI_EVENT_CONNECTION_REQUEST\n");
543             switch(hci_event_connection_request_get_link_type(packet)){
544                 case 0: //  SCO
545                 case 2: // eSCO
546                     hci_event_connection_request_get_bd_addr(packet, event_addr);
547                     hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr);
548                     if (!hfp_connection) break;
549                     hfp_connection->hf_accept_sco = 1;
550                     break;
551                 default:
552                     break;
553             }
554             break;
555 
556         case RFCOMM_EVENT_INCOMING_CONNECTION:
557             // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
558             rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
559             hfp_connection = provide_hfp_connection_context_for_bd_addr(event_addr);
560             if (!hfp_connection || hfp_connection->state != HFP_IDLE) return;
561 
562             hfp_connection->rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
563             hfp_connection->state = HFP_W4_RFCOMM_CONNECTED;
564             // printf("RFCOMM channel %u requested for %s\n", hfp_connection->rfcomm_cid, bd_addr_to_str(hfp_connection->remote_addr));
565             rfcomm_accept_connection(hfp_connection->rfcomm_cid);
566             break;
567 
568         case RFCOMM_EVENT_CHANNEL_OPENED:
569             // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16)
570 
571             rfcomm_event_channel_opened_get_bd_addr(packet, event_addr);
572             status = rfcomm_event_channel_opened_get_status(packet);
573 
574             hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr);
575             if (!hfp_connection || hfp_connection->state != HFP_W4_RFCOMM_CONNECTED) return;
576 
577             if (status) {
578                 hfp_emit_slc_connection_event(hfp_callback, status, rfcomm_event_channel_opened_get_con_handle(packet), event_addr);
579                 remove_hfp_connection_context(hfp_connection);
580             } else {
581                 hfp_connection->acl_handle = rfcomm_event_channel_opened_get_con_handle(packet);
582                 hfp_connection->rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
583                 bd_addr_copy(hfp_connection->remote_addr, event_addr);
584                 // uint16_t mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
585                 // 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);
586 
587                 switch (hfp_connection->state){
588                     case HFP_W4_RFCOMM_CONNECTED:
589                         hfp_connection->state = HFP_EXCHANGE_SUPPORTED_FEATURES;
590                         break;
591                     case HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN:
592                         hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
593                         // printf("Shutting down RFCOMM.\n");
594                         break;
595                     default:
596                         break;
597                 }
598                 rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
599             }
600             break;
601 
602         case HCI_EVENT_COMMAND_STATUS:
603             if (hci_event_command_status_get_command_opcode(packet) == hci_setup_synchronous_connection.opcode) {
604                 status = hci_event_command_status_get_status(packet);
605                 if (status) {
606                     hfp_handle_failed_sco_connection(hci_event_command_status_get_status(packet));
607                }
608             }
609             break;
610 
611         case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{
612             hci_event_synchronous_connection_complete_get_bd_addr(packet, event_addr);
613             hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr);
614             if (!hfp_connection) {
615                 log_error("HFP: connection does not exist for remote with addr %s.", bd_addr_to_str(event_addr));
616                 return;
617             }
618 
619             status = hci_event_synchronous_connection_complete_get_status(packet);
620             if (status != 0){
621                 hfp_connection->hf_accept_sco = 0;
622                 hfp_handle_failed_sco_connection(status);
623                 break;
624             }
625 
626             uint16_t sco_handle = hci_event_synchronous_connection_complete_get_handle(packet);
627             uint8_t  link_type = hci_event_synchronous_connection_complete_get_link_type(packet);
628             uint8_t  transmission_interval = hci_event_synchronous_connection_complete_get_transmission_interval(packet);  // measured in slots
629             uint8_t  retransmission_interval = hci_event_synchronous_connection_complete_get_retransmission_interval(packet);// measured in slots
630             uint16_t rx_packet_length = hci_event_synchronous_connection_complete_get_rx_packet_length(packet); // measured in bytes
631             uint16_t tx_packet_length = hci_event_synchronous_connection_complete_get_tx_packet_length(packet); // measured in bytes
632             uint8_t  air_mode = hci_event_synchronous_connection_complete_get_air_mode(packet);
633 
634             switch (link_type){
635                 case 0x00:
636                     log_info("SCO Connection established.");
637                     if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval);
638                     if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval);
639                     if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length);
640                     if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length);
641                     break;
642                 case 0x02:
643                     log_info("eSCO Connection established. \n");
644                     break;
645                 default:
646                     log_error("(e)SCO reserved link_type 0x%2x", link_type);
647                     break;
648             }
649             log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, "
650                  " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)\n", sco_handle,
651                  bd_addr_to_str(event_addr), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length, air_mode);
652 
653             hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr);
654 
655             if (!hfp_connection) {
656                 log_error("SCO link created, hfp_connection for address %s not found.", bd_addr_to_str(event_addr));
657                 break;
658             }
659 
660             if (hfp_connection->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){
661                 log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN");
662                 hfp_connection->state = HFP_W2_DISCONNECT_SCO;
663                 break;
664             }
665             hfp_connection->sco_handle = sco_handle;
666             hfp_connection->establish_audio_connection = 0;
667             hfp_connection->state = HFP_AUDIO_CONNECTION_ESTABLISHED;
668             hfp_emit_sco_event(hfp_callback, packet[2], sco_handle, event_addr, hfp_connection->negotiated_codec);
669             break;
670         }
671 
672         case RFCOMM_EVENT_CHANNEL_CLOSED:
673             rfcomm_cid = little_endian_read_16(packet,2);
674             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid);
675             if (!hfp_connection) break;
676             if (hfp_connection->state == HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART){
677                 hfp_connection->state = HFP_IDLE;
678                 hfp_establish_service_level_connection(hfp_connection->remote_addr, hfp_connection->service_uuid);
679                 break;
680             }
681 
682             hfp_emit_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0);
683             remove_hfp_connection_context(hfp_connection);
684             break;
685 
686         case HCI_EVENT_DISCONNECTION_COMPLETE:
687             handle = little_endian_read_16(packet,3);
688             hfp_connection = get_hfp_connection_context_for_sco_handle(handle);
689 
690             if (!hfp_connection) break;
691 
692             if (hfp_connection->state != HFP_W4_SCO_DISCONNECTED){
693                 log_info("Received gap disconnect in wrong hfp state");
694             }
695             log_info("Check SCO handle: incoming 0x%02x, hfp_connection 0x%02x\n", handle, hfp_connection->sco_handle);
696 
697             if (handle == hfp_connection->sco_handle){
698                 log_info("SCO disconnected, w2 disconnect RFCOMM\n");
699                 hfp_connection->sco_handle = 0;
700                 hfp_connection->release_audio_connection = 0;
701                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
702                 hfp_emit_event(hfp_callback, HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED, 0);
703                 break;
704             }
705             break;
706 
707         default:
708             break;
709     }
710 }
711 
712 // translates command string into hfp_command_t CMD
713 static hfp_command_t parse_command(const char * line_buffer, int isHandsFree){
714     int offset = isHandsFree ? 0 : 2;
715 
716     if (strncmp(line_buffer+offset, HFP_LIST_CURRENT_CALLS, strlen(HFP_LIST_CURRENT_CALLS)) == 0){
717         return HFP_CMD_LIST_CURRENT_CALLS;
718     }
719 
720     if (strncmp(line_buffer+offset, HFP_SUBSCRIBER_NUMBER_INFORMATION, strlen(HFP_SUBSCRIBER_NUMBER_INFORMATION)) == 0){
721         return HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION;
722     }
723 
724     if (strncmp(line_buffer+offset, HFP_PHONE_NUMBER_FOR_VOICE_TAG, strlen(HFP_PHONE_NUMBER_FOR_VOICE_TAG)) == 0){
725         if (isHandsFree) return HFP_CMD_AG_SENT_PHONE_NUMBER;
726         return HFP_CMD_HF_REQUEST_PHONE_NUMBER;
727     }
728 
729     if (strncmp(line_buffer+offset, HFP_TRANSMIT_DTMF_CODES, strlen(HFP_TRANSMIT_DTMF_CODES)) == 0){
730         return HFP_CMD_TRANSMIT_DTMF_CODES;
731     }
732 
733     if (strncmp(line_buffer+offset, HFP_SET_MICROPHONE_GAIN, strlen(HFP_SET_MICROPHONE_GAIN)) == 0){
734         return HFP_CMD_SET_MICROPHONE_GAIN;
735     }
736 
737     if (strncmp(line_buffer+offset, HFP_SET_SPEAKER_GAIN, strlen(HFP_SET_SPEAKER_GAIN)) == 0){
738         return HFP_CMD_SET_SPEAKER_GAIN;
739     }
740 
741     if (strncmp(line_buffer+offset, HFP_ACTIVATE_VOICE_RECOGNITION, strlen(HFP_ACTIVATE_VOICE_RECOGNITION)) == 0){
742         if (isHandsFree) return HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION;
743         return HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
744     }
745 
746     if (strncmp(line_buffer+offset, HFP_TURN_OFF_EC_AND_NR, strlen(HFP_TURN_OFF_EC_AND_NR)) == 0){
747         return HFP_CMD_TURN_OFF_EC_AND_NR;
748     }
749 
750     if (strncmp(line_buffer, HFP_CALL_ANSWERED, strlen(HFP_CALL_ANSWERED)) == 0){
751         return HFP_CMD_CALL_ANSWERED;
752     }
753 
754     if (strncmp(line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0){
755         return HFP_CMD_CALL_PHONE_NUMBER;
756     }
757 
758     if (strncmp(line_buffer+offset, HFP_REDIAL_LAST_NUMBER, strlen(HFP_REDIAL_LAST_NUMBER)) == 0){
759         return HFP_CMD_REDIAL_LAST_NUMBER;
760     }
761 
762     if (strncmp(line_buffer+offset, HFP_CHANGE_IN_BAND_RING_TONE_SETTING, strlen(HFP_CHANGE_IN_BAND_RING_TONE_SETTING)) == 0){
763         return HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING;
764     }
765 
766     if (strncmp(line_buffer+offset, HFP_HANG_UP_CALL, strlen(HFP_HANG_UP_CALL)) == 0){
767         return HFP_CMD_HANG_UP_CALL;
768     }
769 
770     if (strncmp(line_buffer+offset, HFP_ERROR, strlen(HFP_ERROR)) == 0){
771         return HFP_CMD_ERROR;
772     }
773 
774     if (strncmp(line_buffer+offset, HFP_RING, strlen(HFP_RING)) == 0){
775         return HFP_CMD_RING;
776     }
777 
778     if (isHandsFree && strncmp(line_buffer+offset, HFP_OK, strlen(HFP_OK)) == 0){
779         return HFP_CMD_OK;
780     }
781 
782     if (strncmp(line_buffer+offset, HFP_SUPPORTED_FEATURES, strlen(HFP_SUPPORTED_FEATURES)) == 0){
783         return HFP_CMD_SUPPORTED_FEATURES;
784     }
785 
786     if (strncmp(line_buffer+offset, HFP_TRANSFER_HF_INDICATOR_STATUS, strlen(HFP_TRANSFER_HF_INDICATOR_STATUS)) == 0){
787         return HFP_CMD_HF_INDICATOR_STATUS;
788     }
789 
790     if (strncmp(line_buffer+offset, HFP_RESPONSE_AND_HOLD, strlen(HFP_RESPONSE_AND_HOLD)) == 0){
791         if (strncmp(line_buffer+strlen(HFP_RESPONSE_AND_HOLD)+offset, "?", 1) == 0){
792             return HFP_CMD_RESPONSE_AND_HOLD_QUERY;
793         }
794         if (strncmp(line_buffer+strlen(HFP_RESPONSE_AND_HOLD)+offset, "=", 1) == 0){
795             return HFP_CMD_RESPONSE_AND_HOLD_COMMAND;
796         }
797         return HFP_CMD_RESPONSE_AND_HOLD_STATUS;
798     }
799 
800     if (strncmp(line_buffer+offset, HFP_INDICATOR, strlen(HFP_INDICATOR)) == 0){
801         if (strncmp(line_buffer+strlen(HFP_INDICATOR)+offset, "?", 1) == 0){
802             return HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS;
803         }
804 
805         if (strncmp(line_buffer+strlen(HFP_INDICATOR)+offset, "=?", 2) == 0){
806             return HFP_CMD_RETRIEVE_AG_INDICATORS;
807         }
808     }
809 
810     if (strncmp(line_buffer+offset, HFP_AVAILABLE_CODECS, strlen(HFP_AVAILABLE_CODECS)) == 0){
811         return HFP_CMD_AVAILABLE_CODECS;
812     }
813 
814     if (strncmp(line_buffer+offset, HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, strlen(HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS)) == 0){
815         return HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE;
816     }
817 
818     if (strncmp(line_buffer+offset, HFP_ENABLE_CLIP, strlen(HFP_ENABLE_CLIP)) == 0){
819         if (isHandsFree) return HFP_CMD_AG_SENT_CLIP_INFORMATION;
820         return HFP_CMD_ENABLE_CLIP;
821     }
822 
823     if (strncmp(line_buffer+offset, HFP_ENABLE_CALL_WAITING_NOTIFICATION, strlen(HFP_ENABLE_CALL_WAITING_NOTIFICATION)) == 0){
824         if (isHandsFree) return HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE;
825         return HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION;
826     }
827 
828     if (strncmp(line_buffer+offset, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, strlen(HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES)) == 0){
829 
830         if (isHandsFree) return HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES;
831 
832         if (strncmp(line_buffer+strlen(HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES)+offset, "=?", 2) == 0){
833             return HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES;
834         }
835         if (strncmp(line_buffer+strlen(HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES)+offset, "=", 1) == 0){
836             return HFP_CMD_CALL_HOLD;
837         }
838 
839         return HFP_CMD_UNKNOWN;
840     }
841 
842     if (strncmp(line_buffer+offset, HFP_GENERIC_STATUS_INDICATOR, strlen(HFP_GENERIC_STATUS_INDICATOR)) == 0){
843         if (isHandsFree) {
844             return HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS;
845         }
846         if (strncmp(line_buffer+strlen(HFP_GENERIC_STATUS_INDICATOR)+offset, "=?", 2) == 0){
847             return HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS;
848         }
849         if (strncmp(line_buffer+strlen(HFP_GENERIC_STATUS_INDICATOR)+offset, "=", 1) == 0){
850             return HFP_CMD_LIST_GENERIC_STATUS_INDICATORS;
851         }
852         return HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE;
853     }
854 
855     if (strncmp(line_buffer+offset, HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS, strlen(HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS)) == 0){
856         return HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE;
857     }
858 
859 
860     if (strncmp(line_buffer+offset, HFP_QUERY_OPERATOR_SELECTION, strlen(HFP_QUERY_OPERATOR_SELECTION)) == 0){
861         if (strncmp(line_buffer+strlen(HFP_QUERY_OPERATOR_SELECTION)+offset, "=", 1) == 0){
862             return HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT;
863         }
864         return HFP_CMD_QUERY_OPERATOR_SELECTION_NAME;
865     }
866 
867     if (strncmp(line_buffer+offset, HFP_TRANSFER_AG_INDICATOR_STATUS, strlen(HFP_TRANSFER_AG_INDICATOR_STATUS)) == 0){
868         return HFP_CMD_TRANSFER_AG_INDICATOR_STATUS;
869     }
870 
871     if (isHandsFree && strncmp(line_buffer+offset, HFP_EXTENDED_AUDIO_GATEWAY_ERROR, strlen(HFP_EXTENDED_AUDIO_GATEWAY_ERROR)) == 0){
872         return HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR;
873     }
874 
875     if (!isHandsFree && strncmp(line_buffer+offset, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, strlen(HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR)) == 0){
876         return HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR;
877     }
878 
879     if (strncmp(line_buffer+offset, HFP_TRIGGER_CODEC_CONNECTION_SETUP, strlen(HFP_TRIGGER_CODEC_CONNECTION_SETUP)) == 0){
880         return HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP;
881     }
882 
883     if (strncmp(line_buffer+offset, HFP_CONFIRM_COMMON_CODEC, strlen(HFP_CONFIRM_COMMON_CODEC)) == 0){
884         if (isHandsFree){
885             return HFP_CMD_AG_SUGGESTED_CODEC;
886         } else {
887             return HFP_CMD_HF_CONFIRMED_CODEC;
888         }
889     }
890 
891     if (strncmp(line_buffer+offset, "AT+", 3) == 0){
892         log_info("process unknown HF command %s \n", line_buffer);
893         return HFP_CMD_UNKNOWN;
894     }
895 
896     if (strncmp(line_buffer+offset, "+", 1) == 0){
897         log_info(" process unknown AG command %s \n", line_buffer);
898         return HFP_CMD_UNKNOWN;
899     }
900 
901     if (strncmp(line_buffer+offset, "NOP", 3) == 0){
902         return HFP_CMD_NONE;
903     }
904 
905     return HFP_CMD_NONE;
906 }
907 
908 static void hfp_parser_store_byte(hfp_connection_t * hfp_connection, uint8_t byte){
909     // printf("hfp_parser_store_byte %c at pos %u\n", (char) byte, context->line_size);
910     // TODO: add limit
911     hfp_connection->line_buffer[hfp_connection->line_size++] = byte;
912     hfp_connection->line_buffer[hfp_connection->line_size] = 0;
913 }
914 static int hfp_parser_is_buffer_empty(hfp_connection_t * hfp_connection){
915     return hfp_connection->line_size == 0;
916 }
917 
918 static int hfp_parser_is_end_of_line(uint8_t byte){
919     return byte == '\n' || byte == '\r';
920 }
921 
922 static int hfp_parser_is_end_of_header(uint8_t byte){
923     return hfp_parser_is_end_of_line(byte) || byte == ':' || byte == '?';
924 }
925 
926 static int hfp_parser_found_separator(hfp_connection_t * hfp_connection, uint8_t byte){
927     if (hfp_connection->keep_byte == 1) return 1;
928 
929     int found_separator =   byte == ',' || byte == '\n'|| byte == '\r'||
930                             byte == ')' || byte == '(' || byte == ':' ||
931                             byte == '-' || byte == '"' ||  byte == '?'|| byte == '=';
932     return found_separator;
933 }
934 
935 static void hfp_parser_next_state(hfp_connection_t * hfp_connection, uint8_t byte){
936     hfp_connection->line_size = 0;
937     if (hfp_parser_is_end_of_line(byte)){
938         hfp_connection->parser_item_index = 0;
939         hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
940         return;
941     }
942     switch (hfp_connection->parser_state){
943         case HFP_PARSER_CMD_HEADER:
944             hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
945             if (hfp_connection->keep_byte == 1){
946                 hfp_parser_store_byte(hfp_connection, byte);
947                 hfp_connection->keep_byte = 0;
948             }
949             break;
950         case HFP_PARSER_CMD_SEQUENCE:
951             switch (hfp_connection->command){
952                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
953                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
954                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
955                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
956                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
957                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
958                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
959                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
960                 case HFP_CMD_HF_INDICATOR_STATUS:
961                     hfp_connection->parser_state = HFP_PARSER_SECOND_ITEM;
962                     break;
963                 default:
964                     break;
965             }
966             break;
967         case HFP_PARSER_SECOND_ITEM:
968             hfp_connection->parser_state = HFP_PARSER_THIRD_ITEM;
969             break;
970         case HFP_PARSER_THIRD_ITEM:
971             if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS){
972                 hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
973                 break;
974             }
975             hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
976             break;
977     }
978 }
979 
980 void hfp_parse(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){
981     // handle ATD<dial_string>;
982     if (strncmp((const char*)hfp_connection->line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0){
983         // check for end-of-line or ';'
984         if (byte == ';' || hfp_parser_is_end_of_line(byte)){
985             hfp_connection->line_buffer[hfp_connection->line_size] = 0;
986             hfp_connection->line_size = 0;
987             hfp_connection->command = HFP_CMD_CALL_PHONE_NUMBER;
988         } else {
989             hfp_connection->line_buffer[hfp_connection->line_size++] = byte;
990         }
991         return;
992     }
993 
994     // TODO: handle space inside word
995     if (byte == ' ' && hfp_connection->parser_state > HFP_PARSER_CMD_HEADER) return;
996 
997     if (byte == ',' && hfp_connection->command == HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE){
998         if (hfp_connection->line_size == 0){
999             hfp_connection->line_buffer[0] = 0;
1000             hfp_connection->ignore_value = 1;
1001             parse_sequence(hfp_connection);
1002             return;
1003         }
1004     }
1005 
1006     if (!hfp_parser_found_separator(hfp_connection, byte)){
1007         hfp_parser_store_byte(hfp_connection, byte);
1008         return;
1009     }
1010 
1011     if (hfp_parser_is_end_of_line(byte)) {
1012         if (hfp_parser_is_buffer_empty(hfp_connection)){
1013             hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
1014         }
1015     }
1016     if (hfp_parser_is_buffer_empty(hfp_connection)) return;
1017 
1018     switch (hfp_connection->parser_state){
1019         case HFP_PARSER_CMD_HEADER: // header
1020             if (byte == '='){
1021                 hfp_connection->keep_byte = 1;
1022                 hfp_parser_store_byte(hfp_connection, byte);
1023                 return;
1024             }
1025 
1026             if (byte == '?'){
1027                 hfp_connection->keep_byte = 0;
1028                 hfp_parser_store_byte(hfp_connection, byte);
1029                 return;
1030             }
1031 
1032             if (byte == ','){
1033                 hfp_connection->resolve_byte = 1;
1034             }
1035 
1036             // printf(" parse header 2 %s, keep separator $ %d\n", hfp_connection->line_buffer, hfp_connection->keep_byte);
1037             if (hfp_parser_is_end_of_header(byte) || hfp_connection->keep_byte == 1){
1038                 // printf(" parse header 3 %s, keep separator $ %d\n", hfp_connection->line_buffer, hfp_connection->keep_byte);
1039                 char * line_buffer = (char *)hfp_connection->line_buffer;
1040                 hfp_connection->command = parse_command(line_buffer, isHandsFree);
1041 
1042                 /* resolve command name according to hfp_connection */
1043                 if (hfp_connection->command == HFP_CMD_UNKNOWN){
1044                     switch(hfp_connection->state){
1045                         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
1046                             hfp_connection->command = HFP_CMD_LIST_GENERIC_STATUS_INDICATORS;
1047                             break;
1048                         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
1049                             hfp_connection->command = HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS;
1050                             break;
1051                         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
1052                             hfp_connection->command = HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE;
1053                             break;
1054                         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
1055                             hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS;
1056                             break;
1057                         case HFP_W4_RETRIEVE_INDICATORS:
1058                             hfp_connection->send_ag_indicators_segment = 0;
1059                             hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS;
1060                             break;
1061                         default:
1062                             break;
1063                     }
1064                 }
1065             }
1066             break;
1067 
1068         case HFP_PARSER_CMD_SEQUENCE:
1069             parse_sequence(hfp_connection);
1070             break;
1071         case HFP_PARSER_SECOND_ITEM:
1072             switch (hfp_connection->command){
1073                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1074                     log_info("format %s, ", hfp_connection->line_buffer);
1075                     hfp_connection->network_operator.format =  atoi((char *)&hfp_connection->line_buffer[0]);
1076                     break;
1077                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1078                     log_info("format %s \n", hfp_connection->line_buffer);
1079                     hfp_connection->network_operator.format =  atoi((char *)&hfp_connection->line_buffer[0]);
1080                     break;
1081                 case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1082                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1083                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
1084                     hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].state = (uint8_t)atoi((char*)hfp_connection->line_buffer);
1085                     break;
1086                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1087                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = (uint8_t)atoi((char*)hfp_connection->line_buffer);
1088                     log_info("%d \n", hfp_connection->ag_indicators[hfp_connection->parser_item_index].status);
1089                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status_changed = 1;
1090                     break;
1091                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
1092                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].min_range = atoi((char *)hfp_connection->line_buffer);
1093                     log_info("%s, ", hfp_connection->line_buffer);
1094                     break;
1095                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
1096                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1097                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1098                     hfp_connection->bnip_type = (uint8_t)atoi((char*)hfp_connection->line_buffer);
1099                     break;
1100                 default:
1101                     break;
1102             }
1103             break;
1104 
1105         case HFP_PARSER_THIRD_ITEM:
1106              switch (hfp_connection->command){
1107                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1108                     strcpy(hfp_connection->network_operator.name, (char *)hfp_connection->line_buffer);
1109                     log_info("name %s\n", hfp_connection->line_buffer);
1110                     break;
1111                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
1112                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].max_range = atoi((char *)hfp_connection->line_buffer);
1113                     hfp_connection->parser_item_index++;
1114                     hfp_connection->ag_indicators_nr = hfp_connection->parser_item_index;
1115                     log_info("%s)\n", hfp_connection->line_buffer);
1116                     break;
1117                 default:
1118                     break;
1119             }
1120             break;
1121     }
1122     hfp_parser_next_state(hfp_connection, byte);
1123 
1124     if (hfp_connection->resolve_byte && hfp_connection->command == HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE){
1125         hfp_connection->resolve_byte = 0;
1126         hfp_connection->ignore_value = 1;
1127         parse_sequence(hfp_connection);
1128         hfp_connection->line_buffer[0] = 0;
1129         hfp_connection->line_size = 0;
1130     }
1131 }
1132 
1133 static void parse_sequence(hfp_connection_t * hfp_connection){
1134     int value;
1135     switch (hfp_connection->command){
1136         case HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS:
1137             value = atoi((char *)&hfp_connection->line_buffer[0]);
1138             int i;
1139             switch (hfp_connection->parser_item_index){
1140                 case 0:
1141                     for (i=0;i<hfp_connection->generic_status_indicators_nr;i++){
1142                         if (hfp_connection->generic_status_indicators[i].uuid == value){
1143                             hfp_connection->parser_indicator_index = i;
1144                             break;
1145                         }
1146                     }
1147                     break;
1148                 case 1:
1149                     if (hfp_connection->parser_indicator_index <0) break;
1150                     hfp_connection->generic_status_indicators[hfp_connection->parser_indicator_index].state = value;
1151                     log_info("HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS set indicator at index %u, to %u\n",
1152                      hfp_connection->parser_item_index, value);
1153                     break;
1154                 default:
1155                     break;
1156             }
1157             hfp_connection->parser_item_index++;
1158             break;
1159 
1160         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1161             switch(hfp_connection->parser_item_index){
1162                 case 0:
1163                     strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1164                     hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
1165                     break;
1166                 case 1:
1167                     value = atoi((char *)&hfp_connection->line_buffer[0]);
1168                     hfp_connection->bnip_type = value;
1169                     break;
1170                 default:
1171                     break;
1172             }
1173             hfp_connection->parser_item_index++;
1174             break;
1175         case HFP_CMD_LIST_CURRENT_CALLS:
1176             switch(hfp_connection->parser_item_index){
1177                 case 0:
1178                     value = atoi((char *)&hfp_connection->line_buffer[0]);
1179                     hfp_connection->clcc_idx = value;
1180                     break;
1181                 case 1:
1182                     value = atoi((char *)&hfp_connection->line_buffer[0]);
1183                     hfp_connection->clcc_dir = value;
1184                     break;
1185                 case 2:
1186                     value = atoi((char *)&hfp_connection->line_buffer[0]);
1187                     hfp_connection->clcc_status = value;
1188                     break;
1189                 case 3:
1190                     value = atoi((char *)&hfp_connection->line_buffer[0]);
1191                     hfp_connection->clcc_mpty = value;
1192                     break;
1193                 case 4:
1194                     strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1195                     hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
1196                     break;
1197                 case 5:
1198                     value = atoi((char *)&hfp_connection->line_buffer[0]);
1199                     hfp_connection->bnip_type = value;
1200                     break;
1201                 default:
1202                     break;
1203             }
1204             hfp_connection->parser_item_index++;
1205             break;
1206         case HFP_CMD_SET_MICROPHONE_GAIN:
1207             value = atoi((char *)&hfp_connection->line_buffer[0]);
1208             hfp_connection->microphone_gain = value;
1209             log_info("hfp parse HFP_CMD_SET_MICROPHONE_GAIN %d\n", value);
1210             break;
1211         case HFP_CMD_SET_SPEAKER_GAIN:
1212             value = atoi((char *)&hfp_connection->line_buffer[0]);
1213             hfp_connection->speaker_gain = value;
1214             log_info("hfp parse HFP_CMD_SET_SPEAKER_GAIN %d\n", value);
1215             break;
1216         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
1217             value = atoi((char *)&hfp_connection->line_buffer[0]);
1218             hfp_connection->ag_activate_voice_recognition = value;
1219             log_info("hfp parse HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION %d\n", value);
1220             break;
1221         case HFP_CMD_TURN_OFF_EC_AND_NR:
1222             value = atoi((char *)&hfp_connection->line_buffer[0]);
1223             hfp_connection->ag_echo_and_noise_reduction = value;
1224             log_info("hfp parse HFP_CMD_TURN_OFF_EC_AND_NR %d\n", value);
1225             break;
1226         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
1227             value = atoi((char *)&hfp_connection->line_buffer[0]);
1228             hfp_connection->remote_supported_features = store_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE, value);
1229             log_info("hfp parse HFP_CHANGE_IN_BAND_RING_TONE_SETTING %d\n", value);
1230             break;
1231         case HFP_CMD_HF_CONFIRMED_CODEC:
1232             hfp_connection->codec_confirmed = atoi((char*)hfp_connection->line_buffer);
1233             log_info("hfp parse HFP_CMD_HF_CONFIRMED_CODEC %d\n", hfp_connection->codec_confirmed);
1234             break;
1235         case HFP_CMD_AG_SUGGESTED_CODEC:
1236             hfp_connection->suggested_codec = atoi((char*)hfp_connection->line_buffer);
1237             log_info("hfp parse HFP_CMD_AG_SUGGESTED_CODEC %d\n", hfp_connection->suggested_codec);
1238             break;
1239         case HFP_CMD_SUPPORTED_FEATURES:
1240             hfp_connection->remote_supported_features = atoi((char*)hfp_connection->line_buffer);
1241             log_info("Parsed supported feature %d\n", hfp_connection->remote_supported_features);
1242             break;
1243         case HFP_CMD_AVAILABLE_CODECS:
1244             log_info("Parsed codec %s\n", hfp_connection->line_buffer);
1245             hfp_connection->remote_codecs[hfp_connection->parser_item_index] = (uint16_t)atoi((char*)hfp_connection->line_buffer);
1246             hfp_connection->parser_item_index++;
1247             hfp_connection->remote_codecs_nr = hfp_connection->parser_item_index;
1248             break;
1249         case HFP_CMD_RETRIEVE_AG_INDICATORS:
1250             strcpy((char *)hfp_connection->ag_indicators[hfp_connection->parser_item_index].name,  (char *)hfp_connection->line_buffer);
1251             hfp_connection->ag_indicators[hfp_connection->parser_item_index].index = hfp_connection->parser_item_index+1;
1252             log_info("Indicator %d: %s (", hfp_connection->ag_indicators_nr+1, hfp_connection->line_buffer);
1253             break;
1254         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1255             log_info("Parsed Indicator %d with status: %s\n", hfp_connection->parser_item_index+1, hfp_connection->line_buffer);
1256             hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = atoi((char *) hfp_connection->line_buffer);
1257             hfp_connection->parser_item_index++;
1258             break;
1259         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
1260             hfp_connection->parser_item_index++;
1261             if (hfp_connection->parser_item_index != 4) break;
1262             log_info("Parsed Enable indicators: %s\n", hfp_connection->line_buffer);
1263             value = atoi((char *)&hfp_connection->line_buffer[0]);
1264             hfp_connection->enable_status_update_for_ag_indicators = (uint8_t) value;
1265             break;
1266         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
1267             log_info("Parsed Support call hold: %s\n", hfp_connection->line_buffer);
1268             if (hfp_connection->line_size > 2 ) break;
1269             strcpy((char *)hfp_connection->remote_call_services[hfp_connection->remote_call_services_nr].name,  (char *)hfp_connection->line_buffer);
1270             hfp_connection->remote_call_services_nr++;
1271             break;
1272         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1273         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1274             log_info("Parsed Generic status indicator: %s\n", hfp_connection->line_buffer);
1275             hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].uuid = (uint16_t)atoi((char*)hfp_connection->line_buffer);
1276             hfp_connection->parser_item_index++;
1277             hfp_connection->generic_status_indicators_nr = hfp_connection->parser_item_index;
1278             break;
1279         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
1280             // HF parses inital AG gen. ind. state
1281             log_info("Parsed List generic status indicator %s state: ", hfp_connection->line_buffer);
1282             hfp_connection->parser_item_index = (uint8_t)atoi((char*)hfp_connection->line_buffer);
1283             break;
1284         case HFP_CMD_HF_INDICATOR_STATUS:
1285             hfp_connection->parser_indicator_index = (uint8_t)atoi((char*)hfp_connection->line_buffer);
1286             log_info("Parsed HF indicator index %u", hfp_connection->parser_indicator_index);
1287             break;
1288         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
1289             // AG parses new gen. ind. state
1290             if (hfp_connection->ignore_value){
1291                 hfp_connection->ignore_value = 0;
1292                 log_info("Parsed Enable AG indicator pos %u('%s') - unchanged (stays %u)\n", hfp_connection->parser_item_index,
1293                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled);
1294             }
1295             else if (hfp_connection->ag_indicators[hfp_connection->parser_item_index].mandatory){
1296                 log_info("Parsed Enable AG indicator pos %u('%s') - ignore (mandatory)\n",
1297                     hfp_connection->parser_item_index, hfp_connection->ag_indicators[hfp_connection->parser_item_index].name);
1298             } else {
1299                 value = atoi((char *)&hfp_connection->line_buffer[0]);
1300                 hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled = value;
1301                 log_info("Parsed Enable AG indicator pos %u('%s'): %u\n", hfp_connection->parser_item_index,
1302                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, value);
1303             }
1304             hfp_connection->parser_item_index++;
1305             break;
1306         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1307             // indicators are indexed starting with 1
1308             hfp_connection->parser_item_index = atoi((char *)&hfp_connection->line_buffer[0]) - 1;
1309             log_info("Parsed status of the AG indicator %d, status ", hfp_connection->parser_item_index);
1310             break;
1311         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1312             hfp_connection->network_operator.mode = atoi((char *)&hfp_connection->line_buffer[0]);
1313             log_info("Parsed network operator mode: %d, ", hfp_connection->network_operator.mode);
1314             break;
1315         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1316             if (hfp_connection->line_buffer[0] == '3'){
1317                 log_info("Parsed Set network operator format : %s, ", hfp_connection->line_buffer);
1318                 break;
1319             }
1320             // TODO emit ERROR, wrong format
1321             log_info("ERROR Set network operator format: index %s not supported\n", hfp_connection->line_buffer);
1322             break;
1323         case HFP_CMD_ERROR:
1324             break;
1325         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1326             hfp_connection->extended_audio_gateway_error = 1;
1327             hfp_connection->extended_audio_gateway_error_value = (uint8_t)atoi((char*)hfp_connection->line_buffer);
1328             break;
1329         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
1330             hfp_connection->enable_extended_audio_gateway_error_report = (uint8_t)atoi((char*)hfp_connection->line_buffer);
1331             hfp_connection->ok_pending = 1;
1332             hfp_connection->extended_audio_gateway_error = 0;
1333             break;
1334         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1335         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1336         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1337             strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1338             hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
1339             break;
1340         default:
1341             break;
1342     }
1343 }
1344 
1345 void hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid){
1346     hfp_connection_t * hfp_connection = provide_hfp_connection_context_for_bd_addr(bd_addr);
1347     log_info("hfp_connect %s, hfp_connection %p", bd_addr_to_str(bd_addr), hfp_connection);
1348 
1349     if (!hfp_connection) {
1350         log_error("hfp_establish_service_level_connection for addr %s failed", bd_addr_to_str(bd_addr));
1351         return;
1352     }
1353 
1354     switch (hfp_connection->state){
1355         case HFP_W2_DISCONNECT_RFCOMM:
1356             hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
1357             return;
1358         case HFP_W4_RFCOMM_DISCONNECTED:
1359             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART;
1360             return;
1361         case HFP_IDLE:
1362             memcpy(hfp_connection->remote_addr, bd_addr, 6);
1363             hfp_connection->state = HFP_W4_SDP_QUERY_COMPLETE;
1364             connection_doing_sdp_query = hfp_connection;
1365             hfp_connection->service_uuid = service_uuid;
1366             sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, hfp_connection->remote_addr, service_uuid);
1367             break;
1368         default:
1369             break;
1370     }
1371 }
1372 
1373 void hfp_release_service_level_connection(hfp_connection_t * hfp_connection){
1374     if (!hfp_connection) return;
1375     hfp_release_audio_connection(hfp_connection);
1376 
1377     if (hfp_connection->state < HFP_W4_RFCOMM_CONNECTED){
1378         hfp_connection->state = HFP_IDLE;
1379         return;
1380     }
1381 
1382     if (hfp_connection->state == HFP_W4_RFCOMM_CONNECTED){
1383         hfp_connection->state = HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN;
1384         return;
1385     }
1386 
1387     if (hfp_connection->state < HFP_W4_SCO_CONNECTED){
1388         hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
1389         return;
1390     }
1391 
1392     if (hfp_connection->state < HFP_W4_SCO_DISCONNECTED){
1393         hfp_connection->state = HFP_W2_DISCONNECT_SCO;
1394         return;
1395     }
1396 
1397     return;
1398 }
1399 
1400 void hfp_release_audio_connection(hfp_connection_t * hfp_connection){
1401     if (!hfp_connection) return;
1402     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
1403     hfp_connection->release_audio_connection = 1;
1404 }
1405 
1406 static const struct link_settings {
1407     const uint16_t max_latency;
1408     const uint8_t  retransmission_effort;
1409     const uint16_t packet_types;
1410 } hfp_link_settings [] = {
1411     { 0xffff, 0xff, 0x03c1 }, // HFP_LINK_SETTINGS_D0,   HV1
1412     { 0xffff, 0xff, 0x03c4 }, // HFP_LINK_SETTINGS_D1,   HV3
1413     { 0x0007, 0x01, 0x03c8 }, // HFP_LINK_SETTINGS_S1,   EV3
1414     { 0x0007, 0x01, 0x0380 }, // HFP_LINK_SETTINGS_S2, 2-EV3
1415     { 0x000a, 0x01, 0x0380 }, // HFP_LINK_SETTINGS_S3, 2-EV3
1416     { 0x000c, 0x02, 0x0380 }, // HFP_LINK_SETTINGS_S4, 2-EV3
1417     { 0x0008, 0x02, 0x03c8 }, // HFP_LINK_SETTINGS_T1,   EV3
1418     { 0x000d, 0x02, 0x0380 }  // HFP_LINK_SETTINGS_T2, 2-EV3
1419 };
1420 
1421 void hfp_setup_synchronous_connection(hfp_connection_t * hfp_connection){
1422     // all packet types, fixed bandwidth
1423     int setting = hfp_connection->link_setting;
1424     log_info("hfp_setup_synchronous_connection using setting nr %u", setting);
1425     sco_establishment_active = hfp_connection;
1426     uint16_t sco_voice_setting = hci_get_sco_voice_setting();
1427     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
1428         sco_voice_setting = 0x0043; // Transparent data
1429     }
1430     hci_send_cmd(&hci_setup_synchronous_connection, hfp_connection->acl_handle, 8000, 8000, hfp_link_settings[setting].max_latency,
1431         sco_voice_setting, hfp_link_settings[setting].retransmission_effort, hfp_link_settings[setting].packet_types); // all types 0x003f, only 2-ev3 0x380
1432 }
1433 
1434 void hfp_set_packet_handler_for_rfcomm_connections(btstack_packet_handler_t handler){
1435     rfcomm_packet_handler = handler;
1436 }
1437 
1438