xref: /btstack/src/classic/hfp.c (revision bc37f7b0d0a3eaa5763a873c5730bc14b849aaa0)
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                 default:
551                     break;
552             }
553             break;
554 
555         case RFCOMM_EVENT_INCOMING_CONNECTION:
556             // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
557             rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
558             hfp_connection = provide_hfp_connection_context_for_bd_addr(event_addr);
559             if (!hfp_connection || hfp_connection->state != HFP_IDLE) return;
560 
561             hfp_connection->rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
562             hfp_connection->state = HFP_W4_RFCOMM_CONNECTED;
563             // printf("RFCOMM channel %u requested for %s\n", hfp_connection->rfcomm_cid, bd_addr_to_str(hfp_connection->remote_addr));
564             rfcomm_accept_connection(hfp_connection->rfcomm_cid);
565             break;
566 
567         case RFCOMM_EVENT_CHANNEL_OPENED:
568             // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16)
569 
570             rfcomm_event_channel_opened_get_bd_addr(packet, event_addr);
571             status = rfcomm_event_channel_opened_get_status(packet);
572 
573             hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr);
574             if (!hfp_connection || hfp_connection->state != HFP_W4_RFCOMM_CONNECTED) return;
575 
576             if (status) {
577                 hfp_emit_slc_connection_event(hfp_callback, status, rfcomm_event_channel_opened_get_con_handle(packet), event_addr);
578                 remove_hfp_connection_context(hfp_connection);
579             } else {
580                 hfp_connection->acl_handle = rfcomm_event_channel_opened_get_con_handle(packet);
581                 hfp_connection->rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
582                 bd_addr_copy(hfp_connection->remote_addr, event_addr);
583                 // uint16_t mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
584                 // 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);
585 
586                 switch (hfp_connection->state){
587                     case HFP_W4_RFCOMM_CONNECTED:
588                         hfp_connection->state = HFP_EXCHANGE_SUPPORTED_FEATURES;
589                         break;
590                     case HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN:
591                         hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
592                         // printf("Shutting down RFCOMM.\n");
593                         break;
594                     default:
595                         break;
596                 }
597                 rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
598             }
599             break;
600 
601         case HCI_EVENT_COMMAND_STATUS:
602             if (hci_event_command_status_get_command_opcode(packet) == hci_setup_synchronous_connection.opcode) {
603                 status = hci_event_command_status_get_status(packet);
604                 if (status) {
605                     hfp_handle_failed_sco_connection(hci_event_command_status_get_status(packet));
606                }
607             }
608             break;
609 
610         case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{
611             hci_event_synchronous_connection_complete_get_bd_addr(packet, event_addr);
612             hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr);
613             if (!hfp_connection) {
614                 log_error("HFP: connection does not exist for remote with addr %s.", bd_addr_to_str(event_addr));
615                 return;
616             }
617 
618             status = hci_event_synchronous_connection_complete_get_status(packet);
619             if (status != 0){
620                 hfp_connection->hf_accept_sco = 0;
621                 hfp_handle_failed_sco_connection(status);
622                 break;
623             }
624 
625             uint16_t sco_handle = hci_event_synchronous_connection_complete_get_handle(packet);
626             uint8_t  link_type = hci_event_synchronous_connection_complete_get_link_type(packet);
627             uint8_t  transmission_interval = hci_event_synchronous_connection_complete_get_transmission_interval(packet);  // measured in slots
628             uint8_t  retransmission_interval = hci_event_synchronous_connection_complete_get_retransmission_interval(packet);// measured in slots
629             uint16_t rx_packet_length = hci_event_synchronous_connection_complete_get_rx_packet_length(packet); // measured in bytes
630             uint16_t tx_packet_length = hci_event_synchronous_connection_complete_get_tx_packet_length(packet); // measured in bytes
631             uint8_t  air_mode = hci_event_synchronous_connection_complete_get_air_mode(packet);
632 
633             switch (link_type){
634                 case 0x00:
635                     log_info("SCO Connection established.");
636                     if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval);
637                     if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval);
638                     if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length);
639                     if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length);
640                     break;
641                 case 0x02:
642                     log_info("eSCO Connection established. \n");
643                     break;
644                 default:
645                     log_error("(e)SCO reserved link_type 0x%2x", link_type);
646                     break;
647             }
648             log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, "
649                  " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)\n", sco_handle,
650                  bd_addr_to_str(event_addr), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length, air_mode);
651 
652             hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr);
653 
654             if (!hfp_connection) {
655                 log_error("SCO link created, hfp_connection for address %s not found.", bd_addr_to_str(event_addr));
656                 break;
657             }
658 
659             if (hfp_connection->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){
660                 log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN");
661                 hfp_connection->state = HFP_W2_DISCONNECT_SCO;
662                 break;
663             }
664             hfp_connection->sco_handle = sco_handle;
665             hfp_connection->establish_audio_connection = 0;
666             hfp_connection->state = HFP_AUDIO_CONNECTION_ESTABLISHED;
667             hfp_emit_sco_event(hfp_callback, packet[2], sco_handle, event_addr, hfp_connection->negotiated_codec);
668             break;
669         }
670 
671         case RFCOMM_EVENT_CHANNEL_CLOSED:
672             rfcomm_cid = little_endian_read_16(packet,2);
673             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid);
674             if (!hfp_connection) break;
675             if (hfp_connection->state == HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART){
676                 hfp_connection->state = HFP_IDLE;
677                 hfp_establish_service_level_connection(hfp_connection->remote_addr, hfp_connection->service_uuid);
678                 break;
679             }
680 
681             hfp_emit_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0);
682             remove_hfp_connection_context(hfp_connection);
683             break;
684 
685         case HCI_EVENT_DISCONNECTION_COMPLETE:
686             handle = little_endian_read_16(packet,3);
687             hfp_connection = get_hfp_connection_context_for_sco_handle(handle);
688 
689             if (!hfp_connection) break;
690 
691             if (hfp_connection->state != HFP_W4_SCO_DISCONNECTED){
692                 log_info("Received gap disconnect in wrong hfp state");
693             }
694             log_info("Check SCO handle: incoming 0x%02x, hfp_connection 0x%02x\n", handle, hfp_connection->sco_handle);
695 
696             if (handle == hfp_connection->sco_handle){
697                 log_info("SCO disconnected, w2 disconnect RFCOMM\n");
698                 hfp_connection->sco_handle = 0;
699                 hfp_connection->release_audio_connection = 0;
700                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
701                 hfp_emit_event(hfp_callback, HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED, 0);
702                 break;
703             }
704             break;
705 
706         default:
707             break;
708     }
709 }
710 
711 // translates command string into hfp_command_t CMD
712 static hfp_command_t parse_command(const char * line_buffer, int isHandsFree){
713     int offset = isHandsFree ? 0 : 2;
714 
715     if (strncmp(line_buffer+offset, HFP_LIST_CURRENT_CALLS, strlen(HFP_LIST_CURRENT_CALLS)) == 0){
716         return HFP_CMD_LIST_CURRENT_CALLS;
717     }
718 
719     if (strncmp(line_buffer+offset, HFP_SUBSCRIBER_NUMBER_INFORMATION, strlen(HFP_SUBSCRIBER_NUMBER_INFORMATION)) == 0){
720         return HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION;
721     }
722 
723     if (strncmp(line_buffer+offset, HFP_PHONE_NUMBER_FOR_VOICE_TAG, strlen(HFP_PHONE_NUMBER_FOR_VOICE_TAG)) == 0){
724         if (isHandsFree) return HFP_CMD_AG_SENT_PHONE_NUMBER;
725         return HFP_CMD_HF_REQUEST_PHONE_NUMBER;
726     }
727 
728     if (strncmp(line_buffer+offset, HFP_TRANSMIT_DTMF_CODES, strlen(HFP_TRANSMIT_DTMF_CODES)) == 0){
729         return HFP_CMD_TRANSMIT_DTMF_CODES;
730     }
731 
732     if (strncmp(line_buffer+offset, HFP_SET_MICROPHONE_GAIN, strlen(HFP_SET_MICROPHONE_GAIN)) == 0){
733         return HFP_CMD_SET_MICROPHONE_GAIN;
734     }
735 
736     if (strncmp(line_buffer+offset, HFP_SET_SPEAKER_GAIN, strlen(HFP_SET_SPEAKER_GAIN)) == 0){
737         return HFP_CMD_SET_SPEAKER_GAIN;
738     }
739 
740     if (strncmp(line_buffer+offset, HFP_ACTIVATE_VOICE_RECOGNITION, strlen(HFP_ACTIVATE_VOICE_RECOGNITION)) == 0){
741         if (isHandsFree) return HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION;
742         return HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
743     }
744 
745     if (strncmp(line_buffer+offset, HFP_TURN_OFF_EC_AND_NR, strlen(HFP_TURN_OFF_EC_AND_NR)) == 0){
746         return HFP_CMD_TURN_OFF_EC_AND_NR;
747     }
748 
749     if (strncmp(line_buffer, HFP_CALL_ANSWERED, strlen(HFP_CALL_ANSWERED)) == 0){
750         return HFP_CMD_CALL_ANSWERED;
751     }
752 
753     if (strncmp(line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0){
754         return HFP_CMD_CALL_PHONE_NUMBER;
755     }
756 
757     if (strncmp(line_buffer+offset, HFP_REDIAL_LAST_NUMBER, strlen(HFP_REDIAL_LAST_NUMBER)) == 0){
758         return HFP_CMD_REDIAL_LAST_NUMBER;
759     }
760 
761     if (strncmp(line_buffer+offset, HFP_CHANGE_IN_BAND_RING_TONE_SETTING, strlen(HFP_CHANGE_IN_BAND_RING_TONE_SETTING)) == 0){
762         return HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING;
763     }
764 
765     if (strncmp(line_buffer+offset, HFP_HANG_UP_CALL, strlen(HFP_HANG_UP_CALL)) == 0){
766         return HFP_CMD_HANG_UP_CALL;
767     }
768 
769     if (strncmp(line_buffer+offset, HFP_ERROR, strlen(HFP_ERROR)) == 0){
770         return HFP_CMD_ERROR;
771     }
772 
773     if (strncmp(line_buffer+offset, HFP_RING, strlen(HFP_RING)) == 0){
774         return HFP_CMD_RING;
775     }
776 
777     if (isHandsFree && strncmp(line_buffer+offset, HFP_OK, strlen(HFP_OK)) == 0){
778         return HFP_CMD_OK;
779     }
780 
781     if (strncmp(line_buffer+offset, HFP_SUPPORTED_FEATURES, strlen(HFP_SUPPORTED_FEATURES)) == 0){
782         return HFP_CMD_SUPPORTED_FEATURES;
783     }
784 
785     if (strncmp(line_buffer+offset, HFP_TRANSFER_HF_INDICATOR_STATUS, strlen(HFP_TRANSFER_HF_INDICATOR_STATUS)) == 0){
786         return HFP_CMD_HF_INDICATOR_STATUS;
787     }
788 
789     if (strncmp(line_buffer+offset, HFP_RESPONSE_AND_HOLD, strlen(HFP_RESPONSE_AND_HOLD)) == 0){
790         if (strncmp(line_buffer+strlen(HFP_RESPONSE_AND_HOLD)+offset, "?", 1) == 0){
791             return HFP_CMD_RESPONSE_AND_HOLD_QUERY;
792         }
793         if (strncmp(line_buffer+strlen(HFP_RESPONSE_AND_HOLD)+offset, "=", 1) == 0){
794             return HFP_CMD_RESPONSE_AND_HOLD_COMMAND;
795         }
796         return HFP_CMD_RESPONSE_AND_HOLD_STATUS;
797     }
798 
799     if (strncmp(line_buffer+offset, HFP_INDICATOR, strlen(HFP_INDICATOR)) == 0){
800         if (strncmp(line_buffer+strlen(HFP_INDICATOR)+offset, "?", 1) == 0){
801             return HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS;
802         }
803 
804         if (strncmp(line_buffer+strlen(HFP_INDICATOR)+offset, "=?", 2) == 0){
805             return HFP_CMD_RETRIEVE_AG_INDICATORS;
806         }
807     }
808 
809     if (strncmp(line_buffer+offset, HFP_AVAILABLE_CODECS, strlen(HFP_AVAILABLE_CODECS)) == 0){
810         return HFP_CMD_AVAILABLE_CODECS;
811     }
812 
813     if (strncmp(line_buffer+offset, HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, strlen(HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS)) == 0){
814         return HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE;
815     }
816 
817     if (strncmp(line_buffer+offset, HFP_ENABLE_CLIP, strlen(HFP_ENABLE_CLIP)) == 0){
818         if (isHandsFree) return HFP_CMD_AG_SENT_CLIP_INFORMATION;
819         return HFP_CMD_ENABLE_CLIP;
820     }
821 
822     if (strncmp(line_buffer+offset, HFP_ENABLE_CALL_WAITING_NOTIFICATION, strlen(HFP_ENABLE_CALL_WAITING_NOTIFICATION)) == 0){
823         if (isHandsFree) return HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE;
824         return HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION;
825     }
826 
827     if (strncmp(line_buffer+offset, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, strlen(HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES)) == 0){
828 
829         if (isHandsFree) return HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES;
830 
831         if (strncmp(line_buffer+strlen(HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES)+offset, "=?", 2) == 0){
832             return HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES;
833         }
834         if (strncmp(line_buffer+strlen(HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES)+offset, "=", 1) == 0){
835             return HFP_CMD_CALL_HOLD;
836         }
837 
838         return HFP_CMD_UNKNOWN;
839     }
840 
841     if (strncmp(line_buffer+offset, HFP_GENERIC_STATUS_INDICATOR, strlen(HFP_GENERIC_STATUS_INDICATOR)) == 0){
842         if (isHandsFree) {
843             return HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS;
844         }
845         if (strncmp(line_buffer+strlen(HFP_GENERIC_STATUS_INDICATOR)+offset, "=?", 2) == 0){
846             return HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS;
847         }
848         if (strncmp(line_buffer+strlen(HFP_GENERIC_STATUS_INDICATOR)+offset, "=", 1) == 0){
849             return HFP_CMD_LIST_GENERIC_STATUS_INDICATORS;
850         }
851         return HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE;
852     }
853 
854     if (strncmp(line_buffer+offset, HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS, strlen(HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS)) == 0){
855         return HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE;
856     }
857 
858 
859     if (strncmp(line_buffer+offset, HFP_QUERY_OPERATOR_SELECTION, strlen(HFP_QUERY_OPERATOR_SELECTION)) == 0){
860         if (strncmp(line_buffer+strlen(HFP_QUERY_OPERATOR_SELECTION)+offset, "=", 1) == 0){
861             return HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT;
862         }
863         return HFP_CMD_QUERY_OPERATOR_SELECTION_NAME;
864     }
865 
866     if (strncmp(line_buffer+offset, HFP_TRANSFER_AG_INDICATOR_STATUS, strlen(HFP_TRANSFER_AG_INDICATOR_STATUS)) == 0){
867         return HFP_CMD_TRANSFER_AG_INDICATOR_STATUS;
868     }
869 
870     if (isHandsFree && strncmp(line_buffer+offset, HFP_EXTENDED_AUDIO_GATEWAY_ERROR, strlen(HFP_EXTENDED_AUDIO_GATEWAY_ERROR)) == 0){
871         return HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR;
872     }
873 
874     if (!isHandsFree && strncmp(line_buffer+offset, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, strlen(HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR)) == 0){
875         return HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR;
876     }
877 
878     if (strncmp(line_buffer+offset, HFP_TRIGGER_CODEC_CONNECTION_SETUP, strlen(HFP_TRIGGER_CODEC_CONNECTION_SETUP)) == 0){
879         return HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP;
880     }
881 
882     if (strncmp(line_buffer+offset, HFP_CONFIRM_COMMON_CODEC, strlen(HFP_CONFIRM_COMMON_CODEC)) == 0){
883         if (isHandsFree){
884             return HFP_CMD_AG_SUGGESTED_CODEC;
885         } else {
886             return HFP_CMD_HF_CONFIRMED_CODEC;
887         }
888     }
889 
890     if (strncmp(line_buffer+offset, "AT+", 3) == 0){
891         log_info("process unknown HF command %s \n", line_buffer);
892         return HFP_CMD_UNKNOWN;
893     }
894 
895     if (strncmp(line_buffer+offset, "+", 1) == 0){
896         log_info(" process unknown AG command %s \n", line_buffer);
897         return HFP_CMD_UNKNOWN;
898     }
899 
900     if (strncmp(line_buffer+offset, "NOP", 3) == 0){
901         return HFP_CMD_NONE;
902     }
903 
904     return HFP_CMD_NONE;
905 }
906 
907 static void hfp_parser_store_byte(hfp_connection_t * hfp_connection, uint8_t byte){
908     // printf("hfp_parser_store_byte %c at pos %u\n", (char) byte, context->line_size);
909     // TODO: add limit
910     hfp_connection->line_buffer[hfp_connection->line_size++] = byte;
911     hfp_connection->line_buffer[hfp_connection->line_size] = 0;
912 }
913 static int hfp_parser_is_buffer_empty(hfp_connection_t * hfp_connection){
914     return hfp_connection->line_size == 0;
915 }
916 
917 static int hfp_parser_is_end_of_line(uint8_t byte){
918     return byte == '\n' || byte == '\r';
919 }
920 
921 static int hfp_parser_is_end_of_header(uint8_t byte){
922     return hfp_parser_is_end_of_line(byte) || byte == ':' || byte == '?';
923 }
924 
925 static int hfp_parser_found_separator(hfp_connection_t * hfp_connection, uint8_t byte){
926     if (hfp_connection->keep_byte == 1) return 1;
927 
928     int found_separator =   byte == ',' || byte == '\n'|| byte == '\r'||
929                             byte == ')' || byte == '(' || byte == ':' ||
930                             byte == '-' || byte == '"' ||  byte == '?'|| byte == '=';
931     return found_separator;
932 }
933 
934 static void hfp_parser_next_state(hfp_connection_t * hfp_connection, uint8_t byte){
935     hfp_connection->line_size = 0;
936     if (hfp_parser_is_end_of_line(byte)){
937         hfp_connection->parser_item_index = 0;
938         hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
939         return;
940     }
941     switch (hfp_connection->parser_state){
942         case HFP_PARSER_CMD_HEADER:
943             hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
944             if (hfp_connection->keep_byte == 1){
945                 hfp_parser_store_byte(hfp_connection, byte);
946                 hfp_connection->keep_byte = 0;
947             }
948             break;
949         case HFP_PARSER_CMD_SEQUENCE:
950             switch (hfp_connection->command){
951                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
952                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
953                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
954                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
955                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
956                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
957                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
958                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
959                 case HFP_CMD_HF_INDICATOR_STATUS:
960                     hfp_connection->parser_state = HFP_PARSER_SECOND_ITEM;
961                     break;
962                 default:
963                     break;
964             }
965             break;
966         case HFP_PARSER_SECOND_ITEM:
967             hfp_connection->parser_state = HFP_PARSER_THIRD_ITEM;
968             break;
969         case HFP_PARSER_THIRD_ITEM:
970             if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS){
971                 hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
972                 break;
973             }
974             hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
975             break;
976     }
977 }
978 
979 void hfp_parse(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){
980     // handle ATD<dial_string>;
981     if (strncmp((const char*)hfp_connection->line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0){
982         // check for end-of-line or ';'
983         if (byte == ';' || hfp_parser_is_end_of_line(byte)){
984             hfp_connection->line_buffer[hfp_connection->line_size] = 0;
985             hfp_connection->line_size = 0;
986             hfp_connection->command = HFP_CMD_CALL_PHONE_NUMBER;
987         } else {
988             hfp_connection->line_buffer[hfp_connection->line_size++] = byte;
989         }
990         return;
991     }
992 
993     // TODO: handle space inside word
994     if (byte == ' ' && hfp_connection->parser_state > HFP_PARSER_CMD_HEADER) return;
995 
996     if (byte == ',' && hfp_connection->command == HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE){
997         if (hfp_connection->line_size == 0){
998             hfp_connection->line_buffer[0] = 0;
999             hfp_connection->ignore_value = 1;
1000             parse_sequence(hfp_connection);
1001             return;
1002         }
1003     }
1004 
1005     if (!hfp_parser_found_separator(hfp_connection, byte)){
1006         hfp_parser_store_byte(hfp_connection, byte);
1007         return;
1008     }
1009 
1010     if (hfp_parser_is_end_of_line(byte)) {
1011         if (hfp_parser_is_buffer_empty(hfp_connection)){
1012             hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
1013         }
1014     }
1015     if (hfp_parser_is_buffer_empty(hfp_connection)) return;
1016 
1017     switch (hfp_connection->parser_state){
1018         case HFP_PARSER_CMD_HEADER: // header
1019             if (byte == '='){
1020                 hfp_connection->keep_byte = 1;
1021                 hfp_parser_store_byte(hfp_connection, byte);
1022                 return;
1023             }
1024 
1025             if (byte == '?'){
1026                 hfp_connection->keep_byte = 0;
1027                 hfp_parser_store_byte(hfp_connection, byte);
1028                 return;
1029             }
1030 
1031             if (byte == ','){
1032                 hfp_connection->resolve_byte = 1;
1033             }
1034 
1035             // printf(" parse header 2 %s, keep separator $ %d\n", hfp_connection->line_buffer, hfp_connection->keep_byte);
1036             if (hfp_parser_is_end_of_header(byte) || hfp_connection->keep_byte == 1){
1037                 // printf(" parse header 3 %s, keep separator $ %d\n", hfp_connection->line_buffer, hfp_connection->keep_byte);
1038                 char * line_buffer = (char *)hfp_connection->line_buffer;
1039                 hfp_connection->command = parse_command(line_buffer, isHandsFree);
1040 
1041                 /* resolve command name according to hfp_connection */
1042                 if (hfp_connection->command == HFP_CMD_UNKNOWN){
1043                     switch(hfp_connection->state){
1044                         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
1045                             hfp_connection->command = HFP_CMD_LIST_GENERIC_STATUS_INDICATORS;
1046                             break;
1047                         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
1048                             hfp_connection->command = HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS;
1049                             break;
1050                         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
1051                             hfp_connection->command = HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE;
1052                             break;
1053                         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
1054                             hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS;
1055                             break;
1056                         case HFP_W4_RETRIEVE_INDICATORS:
1057                             hfp_connection->send_ag_indicators_segment = 0;
1058                             hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS;
1059                             break;
1060                         default:
1061                             break;
1062                     }
1063                 }
1064             }
1065             break;
1066 
1067         case HFP_PARSER_CMD_SEQUENCE:
1068             parse_sequence(hfp_connection);
1069             break;
1070         case HFP_PARSER_SECOND_ITEM:
1071             switch (hfp_connection->command){
1072                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1073                     log_info("format %s, ", hfp_connection->line_buffer);
1074                     hfp_connection->network_operator.format =  atoi((char *)&hfp_connection->line_buffer[0]);
1075                     break;
1076                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1077                     log_info("format %s \n", hfp_connection->line_buffer);
1078                     hfp_connection->network_operator.format =  atoi((char *)&hfp_connection->line_buffer[0]);
1079                     break;
1080                 case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1081                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1082                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
1083                     hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].state = (uint8_t)atoi((char*)hfp_connection->line_buffer);
1084                     break;
1085                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1086                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = (uint8_t)atoi((char*)hfp_connection->line_buffer);
1087                     log_info("%d \n", hfp_connection->ag_indicators[hfp_connection->parser_item_index].status);
1088                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status_changed = 1;
1089                     break;
1090                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
1091                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].min_range = atoi((char *)hfp_connection->line_buffer);
1092                     log_info("%s, ", hfp_connection->line_buffer);
1093                     break;
1094                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
1095                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1096                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1097                     hfp_connection->bnip_type = (uint8_t)atoi((char*)hfp_connection->line_buffer);
1098                     break;
1099                 default:
1100                     break;
1101             }
1102             break;
1103 
1104         case HFP_PARSER_THIRD_ITEM:
1105              switch (hfp_connection->command){
1106                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1107                     strcpy(hfp_connection->network_operator.name, (char *)hfp_connection->line_buffer);
1108                     log_info("name %s\n", hfp_connection->line_buffer);
1109                     break;
1110                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
1111                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].max_range = atoi((char *)hfp_connection->line_buffer);
1112                     hfp_connection->parser_item_index++;
1113                     hfp_connection->ag_indicators_nr = hfp_connection->parser_item_index;
1114                     log_info("%s)\n", hfp_connection->line_buffer);
1115                     break;
1116                 default:
1117                     break;
1118             }
1119             break;
1120     }
1121     hfp_parser_next_state(hfp_connection, byte);
1122 
1123     if (hfp_connection->resolve_byte && hfp_connection->command == HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE){
1124         hfp_connection->resolve_byte = 0;
1125         hfp_connection->ignore_value = 1;
1126         parse_sequence(hfp_connection);
1127         hfp_connection->line_buffer[0] = 0;
1128         hfp_connection->line_size = 0;
1129     }
1130 }
1131 
1132 static void parse_sequence(hfp_connection_t * hfp_connection){
1133     int value;
1134     switch (hfp_connection->command){
1135         case HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS:
1136             value = atoi((char *)&hfp_connection->line_buffer[0]);
1137             int i;
1138             switch (hfp_connection->parser_item_index){
1139                 case 0:
1140                     for (i=0;i<hfp_connection->generic_status_indicators_nr;i++){
1141                         if (hfp_connection->generic_status_indicators[i].uuid == value){
1142                             hfp_connection->parser_indicator_index = i;
1143                             break;
1144                         }
1145                     }
1146                     break;
1147                 case 1:
1148                     if (hfp_connection->parser_indicator_index <0) break;
1149                     hfp_connection->generic_status_indicators[hfp_connection->parser_indicator_index].state = value;
1150                     log_info("HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS set indicator at index %u, to %u\n",
1151                      hfp_connection->parser_item_index, value);
1152                     break;
1153                 default:
1154                     break;
1155             }
1156             hfp_connection->parser_item_index++;
1157             break;
1158 
1159         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1160             switch(hfp_connection->parser_item_index){
1161                 case 0:
1162                     strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1163                     hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
1164                     break;
1165                 case 1:
1166                     value = atoi((char *)&hfp_connection->line_buffer[0]);
1167                     hfp_connection->bnip_type = value;
1168                     break;
1169                 default:
1170                     break;
1171             }
1172             hfp_connection->parser_item_index++;
1173             break;
1174         case HFP_CMD_LIST_CURRENT_CALLS:
1175             switch(hfp_connection->parser_item_index){
1176                 case 0:
1177                     value = atoi((char *)&hfp_connection->line_buffer[0]);
1178                     hfp_connection->clcc_idx = value;
1179                     break;
1180                 case 1:
1181                     value = atoi((char *)&hfp_connection->line_buffer[0]);
1182                     hfp_connection->clcc_dir = value;
1183                     break;
1184                 case 2:
1185                     value = atoi((char *)&hfp_connection->line_buffer[0]);
1186                     hfp_connection->clcc_status = value;
1187                     break;
1188                 case 3:
1189                     value = atoi((char *)&hfp_connection->line_buffer[0]);
1190                     hfp_connection->clcc_mpty = value;
1191                     break;
1192                 case 4:
1193                     strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1194                     hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
1195                     break;
1196                 case 5:
1197                     value = atoi((char *)&hfp_connection->line_buffer[0]);
1198                     hfp_connection->bnip_type = value;
1199                     break;
1200                 default:
1201                     break;
1202             }
1203             hfp_connection->parser_item_index++;
1204             break;
1205         case HFP_CMD_SET_MICROPHONE_GAIN:
1206             value = atoi((char *)&hfp_connection->line_buffer[0]);
1207             hfp_connection->microphone_gain = value;
1208             log_info("hfp parse HFP_CMD_SET_MICROPHONE_GAIN %d\n", value);
1209             break;
1210         case HFP_CMD_SET_SPEAKER_GAIN:
1211             value = atoi((char *)&hfp_connection->line_buffer[0]);
1212             hfp_connection->speaker_gain = value;
1213             log_info("hfp parse HFP_CMD_SET_SPEAKER_GAIN %d\n", value);
1214             break;
1215         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
1216             value = atoi((char *)&hfp_connection->line_buffer[0]);
1217             hfp_connection->ag_activate_voice_recognition = value;
1218             log_info("hfp parse HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION %d\n", value);
1219             break;
1220         case HFP_CMD_TURN_OFF_EC_AND_NR:
1221             value = atoi((char *)&hfp_connection->line_buffer[0]);
1222             hfp_connection->ag_echo_and_noise_reduction = value;
1223             log_info("hfp parse HFP_CMD_TURN_OFF_EC_AND_NR %d\n", value);
1224             break;
1225         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
1226             value = atoi((char *)&hfp_connection->line_buffer[0]);
1227             hfp_connection->remote_supported_features = store_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE, value);
1228             log_info("hfp parse HFP_CHANGE_IN_BAND_RING_TONE_SETTING %d\n", value);
1229             break;
1230         case HFP_CMD_HF_CONFIRMED_CODEC:
1231             hfp_connection->codec_confirmed = atoi((char*)hfp_connection->line_buffer);
1232             log_info("hfp parse HFP_CMD_HF_CONFIRMED_CODEC %d\n", hfp_connection->codec_confirmed);
1233             break;
1234         case HFP_CMD_AG_SUGGESTED_CODEC:
1235             hfp_connection->suggested_codec = atoi((char*)hfp_connection->line_buffer);
1236             log_info("hfp parse HFP_CMD_AG_SUGGESTED_CODEC %d\n", hfp_connection->suggested_codec);
1237             break;
1238         case HFP_CMD_SUPPORTED_FEATURES:
1239             hfp_connection->remote_supported_features = atoi((char*)hfp_connection->line_buffer);
1240             log_info("Parsed supported feature %d\n", hfp_connection->remote_supported_features);
1241             break;
1242         case HFP_CMD_AVAILABLE_CODECS:
1243             log_info("Parsed codec %s\n", hfp_connection->line_buffer);
1244             hfp_connection->remote_codecs[hfp_connection->parser_item_index] = (uint16_t)atoi((char*)hfp_connection->line_buffer);
1245             hfp_connection->parser_item_index++;
1246             hfp_connection->remote_codecs_nr = hfp_connection->parser_item_index;
1247             break;
1248         case HFP_CMD_RETRIEVE_AG_INDICATORS:
1249             strcpy((char *)hfp_connection->ag_indicators[hfp_connection->parser_item_index].name,  (char *)hfp_connection->line_buffer);
1250             hfp_connection->ag_indicators[hfp_connection->parser_item_index].index = hfp_connection->parser_item_index+1;
1251             log_info("Indicator %d: %s (", hfp_connection->ag_indicators_nr+1, hfp_connection->line_buffer);
1252             break;
1253         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1254             log_info("Parsed Indicator %d with status: %s\n", hfp_connection->parser_item_index+1, hfp_connection->line_buffer);
1255             hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = atoi((char *) hfp_connection->line_buffer);
1256             hfp_connection->parser_item_index++;
1257             break;
1258         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
1259             hfp_connection->parser_item_index++;
1260             if (hfp_connection->parser_item_index != 4) break;
1261             log_info("Parsed Enable indicators: %s\n", hfp_connection->line_buffer);
1262             value = atoi((char *)&hfp_connection->line_buffer[0]);
1263             hfp_connection->enable_status_update_for_ag_indicators = (uint8_t) value;
1264             break;
1265         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
1266             log_info("Parsed Support call hold: %s\n", hfp_connection->line_buffer);
1267             if (hfp_connection->line_size > 2 ) break;
1268             strcpy((char *)hfp_connection->remote_call_services[hfp_connection->remote_call_services_nr].name,  (char *)hfp_connection->line_buffer);
1269             hfp_connection->remote_call_services_nr++;
1270             break;
1271         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1272         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1273             log_info("Parsed Generic status indicator: %s\n", hfp_connection->line_buffer);
1274             hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].uuid = (uint16_t)atoi((char*)hfp_connection->line_buffer);
1275             hfp_connection->parser_item_index++;
1276             hfp_connection->generic_status_indicators_nr = hfp_connection->parser_item_index;
1277             break;
1278         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
1279             // HF parses inital AG gen. ind. state
1280             log_info("Parsed List generic status indicator %s state: ", hfp_connection->line_buffer);
1281             hfp_connection->parser_item_index = (uint8_t)atoi((char*)hfp_connection->line_buffer);
1282             break;
1283         case HFP_CMD_HF_INDICATOR_STATUS:
1284             hfp_connection->parser_indicator_index = (uint8_t)atoi((char*)hfp_connection->line_buffer);
1285             log_info("Parsed HF indicator index %u", hfp_connection->parser_indicator_index);
1286             break;
1287         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
1288             // AG parses new gen. ind. state
1289             if (hfp_connection->ignore_value){
1290                 hfp_connection->ignore_value = 0;
1291                 log_info("Parsed Enable AG indicator pos %u('%s') - unchanged (stays %u)\n", hfp_connection->parser_item_index,
1292                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled);
1293             }
1294             else if (hfp_connection->ag_indicators[hfp_connection->parser_item_index].mandatory){
1295                 log_info("Parsed Enable AG indicator pos %u('%s') - ignore (mandatory)\n",
1296                     hfp_connection->parser_item_index, hfp_connection->ag_indicators[hfp_connection->parser_item_index].name);
1297             } else {
1298                 value = atoi((char *)&hfp_connection->line_buffer[0]);
1299                 hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled = value;
1300                 log_info("Parsed Enable AG indicator pos %u('%s'): %u\n", hfp_connection->parser_item_index,
1301                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, value);
1302             }
1303             hfp_connection->parser_item_index++;
1304             break;
1305         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1306             // indicators are indexed starting with 1
1307             hfp_connection->parser_item_index = atoi((char *)&hfp_connection->line_buffer[0]) - 1;
1308             log_info("Parsed status of the AG indicator %d, status ", hfp_connection->parser_item_index);
1309             break;
1310         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1311             hfp_connection->network_operator.mode = atoi((char *)&hfp_connection->line_buffer[0]);
1312             log_info("Parsed network operator mode: %d, ", hfp_connection->network_operator.mode);
1313             break;
1314         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1315             if (hfp_connection->line_buffer[0] == '3'){
1316                 log_info("Parsed Set network operator format : %s, ", hfp_connection->line_buffer);
1317                 break;
1318             }
1319             // TODO emit ERROR, wrong format
1320             log_info("ERROR Set network operator format: index %s not supported\n", hfp_connection->line_buffer);
1321             break;
1322         case HFP_CMD_ERROR:
1323             break;
1324         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1325             hfp_connection->extended_audio_gateway_error = 1;
1326             hfp_connection->extended_audio_gateway_error_value = (uint8_t)atoi((char*)hfp_connection->line_buffer);
1327             break;
1328         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
1329             hfp_connection->enable_extended_audio_gateway_error_report = (uint8_t)atoi((char*)hfp_connection->line_buffer);
1330             hfp_connection->ok_pending = 1;
1331             hfp_connection->extended_audio_gateway_error = 0;
1332             break;
1333         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1334         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1335         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1336             strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1337             hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
1338             break;
1339         default:
1340             break;
1341     }
1342 }
1343 
1344 void hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid){
1345     hfp_connection_t * hfp_connection = provide_hfp_connection_context_for_bd_addr(bd_addr);
1346     log_info("hfp_connect %s, hfp_connection %p", bd_addr_to_str(bd_addr), hfp_connection);
1347 
1348     if (!hfp_connection) {
1349         log_error("hfp_establish_service_level_connection for addr %s failed", bd_addr_to_str(bd_addr));
1350         return;
1351     }
1352 
1353     switch (hfp_connection->state){
1354         case HFP_W2_DISCONNECT_RFCOMM:
1355             hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
1356             return;
1357         case HFP_W4_RFCOMM_DISCONNECTED:
1358             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART;
1359             return;
1360         case HFP_IDLE:
1361             memcpy(hfp_connection->remote_addr, bd_addr, 6);
1362             hfp_connection->state = HFP_W4_SDP_QUERY_COMPLETE;
1363             connection_doing_sdp_query = hfp_connection;
1364             hfp_connection->service_uuid = service_uuid;
1365             sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, hfp_connection->remote_addr, service_uuid);
1366             break;
1367         default:
1368             break;
1369     }
1370 }
1371 
1372 void hfp_release_service_level_connection(hfp_connection_t * hfp_connection){
1373     if (!hfp_connection) return;
1374     hfp_release_audio_connection(hfp_connection);
1375 
1376     if (hfp_connection->state < HFP_W4_RFCOMM_CONNECTED){
1377         hfp_connection->state = HFP_IDLE;
1378         return;
1379     }
1380 
1381     if (hfp_connection->state == HFP_W4_RFCOMM_CONNECTED){
1382         hfp_connection->state = HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN;
1383         return;
1384     }
1385 
1386     if (hfp_connection->state < HFP_W4_SCO_CONNECTED){
1387         hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
1388         return;
1389     }
1390 
1391     if (hfp_connection->state < HFP_W4_SCO_DISCONNECTED){
1392         hfp_connection->state = HFP_W2_DISCONNECT_SCO;
1393         return;
1394     }
1395 
1396     return;
1397 }
1398 
1399 void hfp_release_audio_connection(hfp_connection_t * hfp_connection){
1400     if (!hfp_connection) return;
1401     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
1402     hfp_connection->release_audio_connection = 1;
1403 }
1404 
1405 static const struct link_settings {
1406     const uint16_t max_latency;
1407     const uint8_t  retransmission_effort;
1408     const uint16_t packet_types;
1409 } hfp_link_settings [] = {
1410     { 0xffff, 0xff, 0x03c1 }, // HFP_LINK_SETTINGS_D0,   HV1
1411     { 0xffff, 0xff, 0x03c4 }, // HFP_LINK_SETTINGS_D1,   HV3
1412     { 0x0007, 0x01, 0x03c8 }, // HFP_LINK_SETTINGS_S1,   EV3
1413     { 0x0007, 0x01, 0x0380 }, // HFP_LINK_SETTINGS_S2, 2-EV3
1414     { 0x000a, 0x01, 0x0380 }, // HFP_LINK_SETTINGS_S3, 2-EV3
1415     { 0x000c, 0x02, 0x0380 }, // HFP_LINK_SETTINGS_S4, 2-EV3
1416     { 0x0008, 0x02, 0x03c8 }, // HFP_LINK_SETTINGS_T1,   EV3
1417     { 0x000d, 0x02, 0x0380 }  // HFP_LINK_SETTINGS_T2, 2-EV3
1418 };
1419 
1420 void hfp_setup_synchronous_connection(hfp_connection_t * hfp_connection){
1421     // all packet types, fixed bandwidth
1422     int setting = hfp_connection->link_setting;
1423     log_info("hfp_setup_synchronous_connection using setting nr %u", setting);
1424     sco_establishment_active = hfp_connection;
1425     uint16_t sco_voice_setting = hci_get_sco_voice_setting();
1426     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
1427         sco_voice_setting = 0x0043; // Transparent data
1428     }
1429     hci_send_cmd(&hci_setup_synchronous_connection, hfp_connection->acl_handle, 8000, 8000, hfp_link_settings[setting].max_latency,
1430         sco_voice_setting, hfp_link_settings[setting].retransmission_effort, hfp_link_settings[setting].packet_types); // all types 0x003f, only 2-ev3 0x380
1431 }
1432 
1433 void hfp_set_packet_handler_for_rfcomm_connections(btstack_packet_handler_t handler){
1434     rfcomm_packet_handler = handler;
1435 }
1436 
1437