xref: /btstack/src/classic/hfp.c (revision d72e9d4b1d8cea3c48e9897368c446f554491bbf)
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 BLUEKITCHEN
24  * GMBH 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 #define BTSTACK_FILE__ "hfp.c"
39 
40 
41 #include "btstack_config.h"
42 
43 #include <stdint.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <inttypes.h>
47 
48 #include "bluetooth_sdp.h"
49 #include "btstack_debug.h"
50 #include "btstack_event.h"
51 #include "btstack_memory.h"
52 #include "btstack_run_loop.h"
53 #include "classic/sdp_client_rfcomm.h"
54 #include "classic/sdp_server.h"
55 #include "classic/sdp_util.h"
56 #include "classic/sdp_client.h"
57 #include "hci.h"
58 #include "hci_cmd.h"
59 #include "hci_dump.h"
60 
61 #if defined(ENABLE_CC256X_ASSISTED_HFP) && !defined(ENABLE_SCO_OVER_PCM)
62 #error "Assisted HFP is only possible over PCM/I2S. Please add define: ENABLE_SCO_OVER_PCM"
63 #endif
64 
65 #if defined(ENABLE_BCM_PCM_WBS) && !defined(ENABLE_SCO_OVER_PCM)
66 #error "WBS for PCM is only possible over PCM/I2S. Please add define: ENABLE_SCO_OVER_PCM"
67 #endif
68 
69 #define HFP_HF_FEATURES_SIZE 10
70 #define HFP_AG_FEATURES_SIZE 12
71 
72 typedef struct {
73     hfp_role_t local_role;
74     bd_addr_t  remote_address;
75 } hfp_sdp_query_context_t;
76 
77 // globals
78 
79 static btstack_linked_list_t hfp_connections ;
80 
81 static btstack_packet_handler_t hfp_hf_callback;
82 static btstack_packet_handler_t hfp_ag_callback;
83 
84 static btstack_packet_handler_t hfp_hf_rfcomm_packet_handler;
85 static btstack_packet_handler_t hfp_ag_rfcomm_packet_handler;
86 
87 static uint8_t  hfp_hf_indicators_nr;
88 static const uint8_t * hfp_hf_indicators;
89 
90 static uint16_t hfp_allowed_sco_packet_types;
91 static hfp_connection_t * hfp_sco_establishment_active;
92 
93 static hfp_sdp_query_context_t                 hfp_sdp_query_context;
94 static btstack_context_callback_registration_t hfp_sdp_query_request;
95 
96 
97 
98 
99 
100 // custom commands
101 static btstack_linked_list_t hfp_custom_commands_ag;
102 static btstack_linked_list_t hfp_custom_commands_hf;
103 
104 // prototypes
105 static hfp_link_settings_t hfp_next_link_setting_for_connection(hfp_link_settings_t current_setting, hfp_connection_t * hfp_connection, uint8_t eSCO_S4_supported);
106 static void parse_sequence(hfp_connection_t * context);
107 
108 
109 #define CODEC_MASK_CVSD   (1 << HFP_CODEC_CVSD)
110 #define CODEC_MASK_OTHER ((1 << HFP_CODEC_MSBC) | (1 << HFP_CODEC_LC3_SWB))
111 
112 static const struct {
113     const uint16_t max_latency;
114     const uint8_t  retransmission_effort;
115     const uint16_t packet_types;
116     const uint8_t  codec_mask;
117 } hfp_link_settings [] = {
118         {0x0004, 0xff, SCO_PACKET_TYPES_HV1,  CODEC_MASK_CVSD }, // HFP_LINK_SETTINGS_D0
119         {0x0005, 0xff, SCO_PACKET_TYPES_HV3,  CODEC_MASK_CVSD }, // HFP_LINK_SETTINGS_D1
120         {0x0007, 0x01, SCO_PACKET_TYPES_EV3,  CODEC_MASK_CVSD }, // HFP_LINK_SETTINGS_S1
121         {0x0007, 0x01, SCO_PACKET_TYPES_2EV3, CODEC_MASK_CVSD }, // HFP_LINK_SETTINGS_S2
122         {0x000a, 0x01, SCO_PACKET_TYPES_2EV3, CODEC_MASK_CVSD }, // HFP_LINK_SETTINGS_S3
123         {0x000c, 0x02, SCO_PACKET_TYPES_2EV3, CODEC_MASK_CVSD }, // HFP_LINK_SETTINGS_S4
124         {0x0008, 0x02, SCO_PACKET_TYPES_EV3,  CODEC_MASK_OTHER}, // HFP_LINK_SETTINGS_T1
125         {0x000d, 0x02, SCO_PACKET_TYPES_2EV3, CODEC_MASK_OTHER}  // HFP_LINK_SETTINGS_T2
126 };
127 
128 #ifdef ENABLE_HFP_HF_SAFE_SETTINGS
129 // HFP v1.9, table 6.10 'mandatory safe settings' for eSCO + similar entries for SCO
130 static const struct hfp_mandatory_safe_setting {
131     const uint8_t codec_mask;
132     const bool secure_connection_in_use;
133     hfp_link_settings_t link_setting;
134 } hfp_mandatory_safe_settings[] = {
135         { CODEC_MASK_CVSD,  false, HFP_LINK_SETTINGS_D1},
136         { CODEC_MASK_CVSD,  true,  HFP_LINK_SETTINGS_D1},
137         { CODEC_MASK_CVSD,  false, HFP_LINK_SETTINGS_S1},
138         { CODEC_MASK_CVSD,  true,  HFP_LINK_SETTINGS_S4},
139         { CODEC_MASK_OTHER, false, HFP_LINK_SETTINGS_T1},
140         { CODEC_MASK_OTHER, true,  HFP_LINK_SETTINGS_T2},
141 };
142 #endif
143 
144 static const char * hfp_hf_features[] = {
145         "EC and/or NR function",
146         "Three-way calling",
147         "CLI presentation capability",
148         "Voice recognition activation",
149         "Remote volume control",
150 
151         "Enhanced call status",
152         "Enhanced call control",
153 
154         "Codec negotiation",
155 
156         "HF Indicators",
157         "eSCO S4 (and T2) Settings Supported",
158         "Reserved for future definition"
159 };
160 
161 static const char * hfp_ag_features[] = {
162         "Three-way calling",
163         "EC and/or NR function",
164         "Voice recognition function",
165         "In-band ring tone capability",
166         "Attach a number to a voice tag",
167         "Ability to reject a call",
168         "Enhanced call status",
169         "Enhanced call control",
170         "Extended Error Result Codes",
171         "Codec negotiation",
172         "HF Indicators",
173         "eSCO S4 (and T2) Settings Supported",
174         "Reserved for future definition"
175 };
176 
177 static const char * hfp_enhanced_call_dir[] = {
178         "outgoing",
179         "incoming"
180 };
181 
182 static const char * hfp_enhanced_call_status[] = {
183         "active",
184         "held",
185         "outgoing dialing",
186         "outgoing alerting",
187         "incoming",
188         "incoming waiting",
189         "call held by response and hold"
190 };
191 
192 static const char * hfp_enhanced_call_mode[] = {
193         "voice",
194         "data",
195         "fax"
196 };
197 
198 static const char * hfp_enhanced_call_mpty[] = {
199         "not a conference call",
200         "conference call"
201 };
202 
203 const char * hfp_enhanced_call_dir2str(uint16_t index){
204     if (index <= HFP_ENHANCED_CALL_DIR_INCOMING) return hfp_enhanced_call_dir[index];
205     return "not defined";
206 }
207 
208 const char * hfp_enhanced_call_status2str(uint16_t index){
209     if (index <= HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD) return hfp_enhanced_call_status[index];
210     return "not defined";
211 }
212 
213 const char * hfp_enhanced_call_mode2str(uint16_t index){
214     if (index <= HFP_ENHANCED_CALL_MODE_FAX) return hfp_enhanced_call_mode[index];
215     return "not defined";
216 }
217 
218 const char * hfp_enhanced_call_mpty2str(uint16_t index){
219     if (index <= HFP_ENHANCED_CALL_MPTY_CONFERENCE_CALL) return hfp_enhanced_call_mpty[index];
220     return "not defined";
221 }
222 
223 static uint16_t hfp_parse_indicator_index(hfp_connection_t * hfp_connection){
224     uint16_t index = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
225 
226     if (index > HFP_MAX_NUM_INDICATORS){
227         log_info("ignoring invalid indicator index bigger then HFP_MAX_NUM_INDICATORS");
228         return HFP_MAX_NUM_INDICATORS - 1;
229     }
230 
231     // indicator index enumeration starts with 1, we substract 1 to store in array with starting index 0
232     if (index > 0){
233         index -= 1;
234     } else {
235         log_info("ignoring invalid indicator index 0");
236         return 0;
237     }
238     return index;
239 }
240 
241 static void hfp_next_indicators_index(hfp_connection_t * hfp_connection){
242     if (hfp_connection->parser_item_index < HFP_MAX_NUM_INDICATORS - 1){
243         hfp_connection->parser_item_index++;
244     } else {
245         log_info("Ignoring additional indicator");
246     }
247 }
248 
249 static void hfp_next_codec_index(hfp_connection_t * hfp_connection){
250     if (hfp_connection->parser_item_index < HFP_MAX_NUM_CODECS - 1){
251         hfp_connection->parser_item_index++;
252     } else {
253         log_info("Ignoring additional codec index");
254     }
255 }
256 
257 static void hfp_next_remote_call_services_index(hfp_connection_t * hfp_connection){
258     if (hfp_connection->remote_call_services_index < HFP_MAX_NUM_CALL_SERVICES - 1){
259         hfp_connection->remote_call_services_index++;
260     } else {
261         log_info("Ignoring additional remote_call_services");
262     }
263 }
264 
265 const char * hfp_hf_feature(int index){
266     if (index > HFP_HF_FEATURES_SIZE){
267         return hfp_hf_features[HFP_HF_FEATURES_SIZE];
268     }
269     return hfp_hf_features[index];
270 }
271 
272 const char * hfp_ag_feature(int index){
273     if (index > HFP_AG_FEATURES_SIZE){
274         return hfp_ag_features[HFP_AG_FEATURES_SIZE];
275     }
276     return hfp_ag_features[index];
277 }
278 
279 int send_str_over_rfcomm(uint16_t cid, const char * command){
280     if (!rfcomm_can_send_packet_now(cid)) return 1;
281     log_info("HFP_TX %s", command);
282     int err = rfcomm_send(cid, (uint8_t*) command, (uint16_t) strlen(command));
283     if (err){
284         log_error("rfcomm_send -> error 0x%02x \n", err);
285     }
286 #ifdef ENABLE_HFP_AT_MESSAGES
287     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(cid);
288     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_SENT, command);
289 #endif
290     return 1;
291 }
292 
293 int hfp_supports_codec(uint8_t codec, int codecs_nr, uint8_t * codecs){
294 
295     // mSBC requires support for eSCO connections
296     if ((codec == HFP_CODEC_MSBC) && !hci_extended_sco_link_supported()) return 0;
297 
298     int i;
299     for (i = 0; i < codecs_nr; i++){
300         if (codecs[i] != codec) continue;
301         return 1;
302     }
303     return 0;
304 }
305 
306 void hfp_hf_drop_mSBC_if_eSCO_not_supported(uint8_t * codecs, uint8_t * codecs_nr){
307     if (hci_extended_sco_link_supported()) return;
308     uint8_t i;
309     int filtered_codec_count = 0;
310     for (i=0; i < *codecs_nr; i++){
311         if (codecs[i] != HFP_CODEC_MSBC) {
312             codecs[filtered_codec_count++] = codecs[i];
313         }
314     }
315     *codecs_nr = filtered_codec_count;
316 }
317 
318 // UTILS
319 int get_bit(uint16_t bitmap, int position){
320     return (bitmap >> position) & 1;
321 }
322 
323 int store_bit(uint32_t bitmap, int position, uint8_t value){
324     if (value){
325         bitmap |= 1 << position;
326     } else {
327         bitmap &= ~ (1 << position);
328     }
329     return bitmap;
330 }
331 
332 int join(char * buffer, int buffer_size, uint8_t * values, int values_nr){
333     if (buffer_size < (values_nr * 3)) return 0;
334     int i;
335     int offset = 0;
336     for (i = 0; i < (values_nr-1); i++) {
337       offset += snprintf(buffer+offset, buffer_size-offset, "%d,", values[i]); // puts string into buffer
338     }
339     if (i<values_nr){
340         offset += snprintf(buffer+offset, buffer_size-offset, "%d", values[i]);
341     }
342     return offset;
343 }
344 
345 int join_bitmap(char * buffer, int buffer_size, uint32_t values, int values_nr){
346     if (buffer_size < (values_nr * 3)) return 0;
347 
348     int i;
349     int offset = 0;
350     for (i = 0; i < (values_nr-1); i++) {
351       offset += snprintf(buffer+offset, buffer_size-offset, "%d,", get_bit(values,i)); // puts string into buffer
352     }
353 
354     if (i<values_nr){
355         offset += snprintf(buffer+offset, buffer_size-offset, "%d", get_bit(values,i));
356     }
357     return offset;
358 }
359 static void hfp_emit_event_for_role(hfp_role_t local_role, uint8_t * packet, uint16_t size){
360     switch (local_role){
361         case HFP_ROLE_HF:
362             (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, packet, size);
363             break;
364         case HFP_ROLE_AG:
365             (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, packet, size);
366             break;
367         default:
368             btstack_unreachable();
369             break;
370     }
371 }
372 
373 static void hfp_emit_event_for_context(hfp_connection_t * hfp_connection, uint8_t * packet, uint16_t size){
374     if (!hfp_connection) return;
375     hfp_emit_event_for_role(hfp_connection->local_role, packet, size);
376 }
377 
378 void hfp_emit_simple_event(hfp_connection_t * hfp_connection, uint8_t event_subtype){
379     hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID;
380     uint8_t event[5];
381     event[0] = HCI_EVENT_HFP_META;
382     event[1] = sizeof(event) - 2;
383     event[2] = event_subtype;
384     little_endian_store_16(event, 3, acl_handle);
385     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
386 }
387 
388 void hfp_emit_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, uint8_t value){
389     hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID;
390     uint8_t event[6];
391     event[0] = HCI_EVENT_HFP_META;
392     event[1] = sizeof(event) - 2;
393     event[2] = event_subtype;
394     little_endian_store_16(event, 3, acl_handle);
395     event[5] = value; // status 0 == OK
396     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
397 }
398 
399 void hfp_emit_voice_recognition_enabled(hfp_connection_t * hfp_connection, uint8_t status){
400     btstack_assert(hfp_connection != NULL);
401 
402     uint8_t event[7];
403     event[0] = HCI_EVENT_HFP_META;
404     event[1] = sizeof(event) - 2;
405     event[2] = HFP_SUBEVENT_VOICE_RECOGNITION_ACTIVATED;
406 
407     little_endian_store_16(event, 3, hfp_connection->acl_handle);
408     event[5] = status; // 0:success
409     event[6] = hfp_connection->enhanced_voice_recognition_enabled ? 1 : 0;
410     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
411 }
412 
413 void hfp_emit_voice_recognition_disabled(hfp_connection_t * hfp_connection, uint8_t status){
414     btstack_assert(hfp_connection != NULL);
415 
416     uint8_t event[6];
417     event[0] = HCI_EVENT_HFP_META;
418     event[1] = sizeof(event) - 2;
419     event[2] = HFP_SUBEVENT_VOICE_RECOGNITION_DEACTIVATED;
420 
421     little_endian_store_16(event, 3, hfp_connection->acl_handle);
422     event[5] = status; // 0:success
423     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
424 }
425 
426 void hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection_t * hfp_connection, uint8_t status){
427     hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID;
428 
429     uint8_t event[6];
430     event[0] = HCI_EVENT_HFP_META;
431     event[1] = sizeof(event) - 2;
432     event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_HF_READY_FOR_AUDIO;
433     little_endian_store_16(event, 3, acl_handle);
434     event[5] = status;
435     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
436 }
437 
438 void hfp_emit_enhanced_voice_recognition_state_event(hfp_connection_t * hfp_connection, uint8_t status){
439     hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID;
440 
441     uint8_t event[6];
442     event[0] = HCI_EVENT_HFP_META;
443     event[1] = sizeof(event) - 2;
444     switch (hfp_connection->ag_vra_state){
445         case HFP_VOICE_RECOGNITION_STATE_AG_READY_TO_ACCEPT_AUDIO_INPUT:
446             event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_READY_TO_ACCEPT_AUDIO_INPUT;
447             break;
448         case HFP_VOICE_RECOGNITION_STATE_AG_IS_STARTING_SOUND:
449             event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_STARTING_SOUND;
450             break;
451         case HFP_VOICE_RECOGNITION_STATE_AG_IS_PROCESSING_AUDIO_INPUT:
452             event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_PROCESSING_AUDIO_INPUT;
453             break;
454         default:
455             btstack_unreachable();
456             break;
457     }
458 
459     little_endian_store_16(event, 3, acl_handle);
460     event[5] = status;
461     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
462 }
463 
464 void hfp_emit_slc_connection_event(hfp_role_t local_role, uint8_t status, hci_con_handle_t con_handle, bd_addr_t addr){
465     uint8_t event[12];
466     int pos = 0;
467     event[pos++] = HCI_EVENT_HFP_META;
468     event[pos++] = sizeof(event) - 2;
469     event[pos++] = HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
470     little_endian_store_16(event, pos, con_handle);
471     pos += 2;
472     event[pos++] = status; // status 0 == OK
473     reverse_bd_addr(addr,&event[pos]);
474     pos += 6;
475     hfp_emit_event_for_role(local_role, event, sizeof(event));
476 }
477 
478 static void hfp_emit_audio_connection_released(hfp_connection_t * hfp_connection, hci_con_handle_t sco_handle){
479     btstack_assert(hfp_connection != NULL);
480     uint8_t event[7];
481     int pos = 0;
482     event[pos++] = HCI_EVENT_HFP_META;
483     event[pos++] = sizeof(event) - 2;
484     event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED;
485     little_endian_store_16(event, pos, hfp_connection->acl_handle);
486     pos += 2;
487     little_endian_store_16(event, pos, sco_handle);
488     pos += 2;
489     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
490 }
491 
492 void hfp_emit_sco_connection_established(hfp_connection_t *hfp_connection, uint8_t status, uint8_t negotiated_codec,
493                                          uint16_t rx_packet_length, uint16_t tx_packet_length) {
494     btstack_assert(hfp_connection != NULL);
495     uint8_t event[21];
496     int pos = 0;
497     event[pos++] = HCI_EVENT_HFP_META;
498     event[pos++] = sizeof(event) - 2;
499     event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED;
500     little_endian_store_16(event, pos, hfp_connection->acl_handle);
501     pos += 2;
502     event[pos++] = status; // status 0 == OK
503     little_endian_store_16(event, pos, hfp_connection->sco_handle);
504     pos += 2;
505     reverse_bd_addr(hfp_connection->remote_addr,&event[pos]);
506     pos += 6;
507     event[pos++] = negotiated_codec;
508     little_endian_store_16(event, pos, hfp_connection->packet_types);
509     pos += 2;
510     little_endian_store_16(event, pos, rx_packet_length);
511     pos += 2;
512     little_endian_store_16(event, pos, tx_packet_length);
513     pos += 2;
514     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
515 }
516 
517 void hfp_emit_string_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, const char * value){
518     btstack_assert(hfp_connection != NULL);
519 #ifdef ENABLE_HFP_AT_MESSAGES
520     uint8_t event[256];
521 #else
522     uint8_t event[40];
523 #endif
524     uint16_t string_len = btstack_min((uint16_t) strlen(value), sizeof(event) - 6);
525     event[0] = HCI_EVENT_HFP_META;
526     event[1] = 4 + string_len;
527     event[2] = event_subtype;
528     little_endian_store_16(event, 3, hfp_connection->acl_handle);
529     memcpy((char*)&event[5], value, string_len);
530     event[5 + string_len] = 0;
531     hfp_emit_event_for_context(hfp_connection, event, 6 + string_len);
532 }
533 
534 btstack_linked_list_t * hfp_get_connections(void){
535     return (btstack_linked_list_t *) &hfp_connections;
536 }
537 
538 hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid){
539     btstack_linked_list_iterator_t it;
540     btstack_linked_list_iterator_init(&it, hfp_get_connections());
541     while (btstack_linked_list_iterator_has_next(&it)){
542         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
543         if (hfp_connection->rfcomm_cid == cid){
544             return hfp_connection;
545         }
546     }
547     return NULL;
548 }
549 
550 hfp_connection_t * get_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr, hfp_role_t hfp_role){
551     btstack_linked_list_iterator_t it;
552     btstack_linked_list_iterator_init(&it, hfp_get_connections());
553     while (btstack_linked_list_iterator_has_next(&it)){
554         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
555         if ((memcmp(hfp_connection->remote_addr, bd_addr, 6) == 0) && (hfp_connection->local_role == hfp_role)) {
556             return hfp_connection;
557         }
558     }
559     return NULL;
560 }
561 
562 hfp_connection_t * get_hfp_connection_context_for_sco_handle(uint16_t handle, hfp_role_t hfp_role){
563     btstack_linked_list_iterator_t it;
564     btstack_linked_list_iterator_init(&it, hfp_get_connections());
565     while (btstack_linked_list_iterator_has_next(&it)){
566         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
567         if ((hfp_connection->sco_handle == handle) && (hfp_connection->local_role == hfp_role)){
568             return hfp_connection;
569         }
570     }
571     return NULL;
572 }
573 
574 hfp_connection_t * get_hfp_connection_context_for_acl_handle(uint16_t handle, hfp_role_t hfp_role){
575     btstack_linked_list_iterator_t it;
576     btstack_linked_list_iterator_init(&it, hfp_get_connections());
577     while (btstack_linked_list_iterator_has_next(&it)){
578         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
579         if ((hfp_connection->acl_handle == handle) && (hfp_connection->local_role == hfp_role)){
580             return hfp_connection;
581         }
582     }
583     return NULL;
584 }
585 
586 
587 static void hfp_reset_voice_recognition(hfp_connection_t * hfp_connection){
588     btstack_assert(hfp_connection != NULL);
589     hfp_voice_recognition_activation_status_t current_vra_state = hfp_connection->vra_state;
590     hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF;
591 
592     if (current_vra_state != HFP_VRA_VOICE_RECOGNITION_OFF){
593         hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS);
594     } else if (hfp_connection->vra_state_requested != HFP_VRA_VOICE_RECOGNITION_OFF){
595         hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS);
596     }
597 
598     hfp_connection->vra_state_requested = HFP_VRA_VOICE_RECOGNITION_OFF;
599     hfp_connection->activate_voice_recognition = false;
600     hfp_connection->deactivate_voice_recognition = false;
601     hfp_connection->enhanced_voice_recognition_enabled = false;
602     hfp_connection->ag_vra_status = 0;
603     hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY;
604 }
605 
606 void hfp_reset_context_flags(hfp_connection_t * hfp_connection){
607     if (!hfp_connection) return;
608     hfp_connection->ok_pending = 0;
609     hfp_connection->send_error = 0;
610 
611     hfp_connection->found_equal_sign = false;
612 
613     hfp_connection->change_status_update_for_individual_ag_indicators = 0;
614     hfp_connection->operator_name_changed = 0;
615 
616     hfp_connection->enable_extended_audio_gateway_error_report = 0;
617     hfp_connection->extended_audio_gateway_error = 0;
618 
619     // establish codecs hfp_connection
620     hfp_connection->suggested_codec = 0;
621     hfp_connection->negotiated_codec = 0;
622     hfp_connection->codec_confirmed = 0;
623 
624     hfp_connection->establish_audio_connection = 0;
625     hfp_connection->call_waiting_notification_enabled = 0;
626     hfp_connection->command = HFP_CMD_NONE;
627     hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
628     hfp_connection->clip_have_alpha = false;
629     hfp_reset_voice_recognition(hfp_connection);
630 }
631 
632 static hfp_connection_t * create_hfp_connection_context(void){
633     hfp_connection_t * hfp_connection = btstack_memory_hfp_connection_get();
634     if (!hfp_connection) return NULL;
635 
636     hfp_connection->state = HFP_IDLE;
637     hfp_connection->call_state = HFP_CALL_IDLE;
638     hfp_connection->codecs_state = HFP_CODECS_IDLE;
639 
640     hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
641 
642     hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID;
643     hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID;
644 
645     // HF only
646     hfp_connection->hf_call_status      = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS;
647     hfp_connection->hf_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
648     hfp_connection->hf_callheld_status  = HFP_CALLHELD_STATUS_NO_CALLS_HELD;
649 
650     hfp_reset_context_flags(hfp_connection);
651 
652     btstack_linked_list_add_tail(&hfp_connections, (btstack_linked_item_t*)hfp_connection);
653     return hfp_connection;
654 }
655 
656 void hfp_finalize_connection_context(hfp_connection_t * hfp_connection){
657     btstack_linked_list_remove(&hfp_connections, (btstack_linked_item_t*) hfp_connection);
658     btstack_memory_hfp_connection_free(hfp_connection);
659 }
660 
661 static hfp_connection_t * hfp_create_connection(bd_addr_t bd_addr, hfp_role_t local_role){
662     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr, local_role);
663     if (hfp_connection) return  hfp_connection;
664     hfp_connection = create_hfp_connection_context();
665     if (!hfp_connection) {
666         return NULL;
667     }
668     (void)memcpy(hfp_connection->remote_addr, bd_addr, 6);
669     hfp_connection->local_role = local_role;
670     log_info("Create HFP context %p: role %u, addr %s", hfp_connection, local_role, bd_addr_to_str(bd_addr));
671 
672 #ifdef ENABLE_NXP_PCM_WBS
673     hfp_connection->nxp_start_audio_handle = HCI_CON_HANDLE_INVALID;
674     hfp_connection->nxp_stop_audio_handle = HCI_CON_HANDLE_INVALID;
675 #endif
676 
677     return hfp_connection;
678 }
679 
680 /* @param network.
681  * 0 == no ability to reject a call.
682  * 1 == ability to reject a call.
683  */
684 
685 /* @param suported_features
686  * HF bit 0: EC and/or NR function (yes/no, 1 = yes, 0 = no)
687  * HF bit 1: Call waiting or three-way calling(yes/no, 1 = yes, 0 = no)
688  * HF bit 2: CLI presentation capability (yes/no, 1 = yes, 0 = no)
689  * HF bit 3: Voice recognition activation (yes/no, 1= yes, 0 = no)
690  * HF bit 4: Remote volume control (yes/no, 1 = yes, 0 = no)
691  * HF bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
692  */
693  /* Bit position:
694  * AG bit 0: Three-way calling (yes/no, 1 = yes, 0 = no)
695  * AG bit 1: EC and/or NR function (yes/no, 1 = yes, 0 = no)
696  * AG bit 2: Voice recognition function (yes/no, 1 = yes, 0 = no)
697  * AG bit 3: In-band ring tone capability (yes/no, 1 = yes, 0 = no)
698  * AG bit 4: Attach a phone number to a voice tag (yes/no, 1 = yes, 0 = no)
699  * AG bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
700  */
701 
702 void hfp_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint16_t service_uuid, int rfcomm_channel_nr, const char * name){
703     uint8_t* attribute;
704     de_create_sequence(service);
705 
706     // 0x0000 "Service Record Handle"
707     de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE);
708     de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle);
709 
710     // 0x0001 "Service Class ID List"
711     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST);
712     attribute = de_push_sequence(service);
713     {
714         //  "UUID for Service"
715         de_add_number(attribute, DE_UUID, DE_SIZE_16, service_uuid);
716         de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_GENERIC_AUDIO);
717     }
718     de_pop_sequence(service, attribute);
719 
720     // 0x0004 "Protocol Descriptor List"
721     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST);
722     attribute = de_push_sequence(service);
723     {
724         uint8_t* l2cpProtocol = de_push_sequence(attribute);
725         {
726             de_add_number(l2cpProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP);
727         }
728         de_pop_sequence(attribute, l2cpProtocol);
729 
730         uint8_t* rfcomm = de_push_sequence(attribute);
731         {
732             de_add_number(rfcomm,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_RFCOMM);  // rfcomm_service
733             de_add_number(rfcomm,  DE_UINT, DE_SIZE_8,  rfcomm_channel_nr);  // rfcomm channel
734         }
735         de_pop_sequence(attribute, rfcomm);
736     }
737     de_pop_sequence(service, attribute);
738 
739 
740     // 0x0005 "Public Browse Group"
741     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group
742     attribute = de_push_sequence(service);
743     {
744         de_add_number(attribute,  DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT);
745     }
746     de_pop_sequence(service, attribute);
747 
748     // 0x0009 "Bluetooth Profile Descriptor List"
749     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST);
750     attribute = de_push_sequence(service);
751     {
752         uint8_t *sppProfile = de_push_sequence(attribute);
753         {
754             de_add_number(sppProfile,  DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_HANDSFREE);
755             de_add_number(sppProfile,  DE_UINT, DE_SIZE_16, 0x0109); // Version 1.9
756         }
757         de_pop_sequence(attribute, sppProfile);
758     }
759     de_pop_sequence(service, attribute);
760 
761     // 0x0100 "Service Name"
762     if (strlen(name) > 0){
763         de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0100);
764         de_add_data(service,  DE_STRING, (uint16_t) strlen(name), (uint8_t *) name);
765     }
766 }
767 
768 static void hfp_handle_slc_setup_error(hfp_connection_t * hfp_connection, uint8_t status){
769     // cache fields for event
770     hfp_role_t local_role = hfp_connection->local_role;
771     bd_addr_t remote_addr;
772     // cppcheck-suppress uninitvar ; remote_addr is reported as uninitialized although it's the destination of the memcpy
773     (void)memcpy(remote_addr, hfp_connection->remote_addr, 6);
774     // finalize connection struct
775     hfp_finalize_connection_context(hfp_connection);
776     // emit event
777     hfp_emit_slc_connection_event(local_role, status, HCI_CON_HANDLE_INVALID, remote_addr);
778 }
779 
780 static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
781     UNUSED(packet_type);    // ok: handling own sdp events
782     UNUSED(channel);        // ok: no channel
783     UNUSED(size);           // ok: handling own sdp events
784 
785     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(hfp_sdp_query_context.remote_address, hfp_sdp_query_context.local_role);
786     if (hfp_connection == NULL) {
787         log_info("connection with %s and local role %d not found", hfp_sdp_query_context.remote_address, hfp_sdp_query_context.local_role);
788         return;
789     }
790 
791     switch (hci_event_packet_get_type(packet)){
792         case SDP_EVENT_QUERY_RFCOMM_SERVICE:
793             hfp_connection->rfcomm_channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet);
794             break;
795         case SDP_EVENT_QUERY_COMPLETE:
796             if (hfp_connection->rfcomm_channel_nr > 0){
797                 hfp_connection->state = HFP_W4_RFCOMM_CONNECTED;
798                 btstack_packet_handler_t packet_handler;
799                 switch (hfp_connection->local_role){
800                     case HFP_ROLE_AG:
801                         packet_handler = hfp_ag_rfcomm_packet_handler;
802                         break;
803                     case HFP_ROLE_HF:
804                         packet_handler = hfp_hf_rfcomm_packet_handler;
805                         break;
806                     default:
807                         btstack_assert(false);
808                         return;
809                 }
810 
811                 rfcomm_create_channel(packet_handler, hfp_connection->remote_addr, hfp_connection->rfcomm_channel_nr, NULL);
812 
813             } else {
814                 uint8_t status = sdp_event_query_complete_get_status(packet);
815                 if (status == ERROR_CODE_SUCCESS){
816                     // report service not found
817                     status = SDP_SERVICE_NOT_FOUND;
818                 }
819                 hfp_handle_slc_setup_error(hfp_connection, status);
820                 log_info("rfcomm service not found, status 0x%02x", status);
821             }
822 
823             // register the SDP Query request to check if there is another connection waiting for the query
824             // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback
825             (void) sdp_client_register_query_callback(&hfp_sdp_query_request);
826             break;
827         default:
828             break;
829     }
830 }
831 
832 // returns 0 if unexpected error or no other link options remained, otherwise 1
833 static int hfp_handle_failed_sco_connection(uint8_t status){
834 
835     if (hfp_sco_establishment_active->accept_sco != 0){
836         log_info("(e)SCO Connection failed but not started by us");
837         return 0;
838     }
839 
840     log_info("(e)SCO Connection failed 0x%02x", status);
841     switch (status){
842         case ERROR_CODE_INVALID_LMP_PARAMETERS_INVALID_LL_PARAMETERS:
843         case ERROR_CODE_SCO_AIR_MODE_REJECTED:
844         case ERROR_CODE_SCO_INTERVAL_REJECTED:
845         case ERROR_CODE_SCO_OFFSET_REJECTED:
846         case ERROR_CODE_UNSPECIFIED_ERROR:
847         case ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE:
848         case ERROR_CODE_UNSUPPORTED_LMP_PARAMETER_VALUE_UNSUPPORTED_LL_PARAMETER_VALUE:
849         case ERROR_CODE_UNSUPPORTED_REMOTE_FEATURE_UNSUPPORTED_LMP_FEATURE:
850             break;
851         default:
852             return 0;
853     }
854 
855     // note: eSCO_S4 supported flag not available, but it's only relevant for highest CVSD link setting (and the current failed)
856     hfp_link_settings_t next_setting = hfp_next_link_setting_for_connection(hfp_sco_establishment_active->link_setting, hfp_sco_establishment_active, false);
857 
858     // handle no valid setting found
859     if (next_setting == HFP_LINK_SETTINGS_NONE) {
860         if (hfp_sco_establishment_active->negotiated_codec == HFP_CODEC_MSBC){
861             log_info("T2/T1 failed, fallback to CVSD - D1");
862             hfp_sco_establishment_active->negotiated_codec = HFP_CODEC_CVSD;
863             hfp_sco_establishment_active->sco_for_msbc_failed = 1;
864             hfp_sco_establishment_active->ag_send_common_codec = true;
865             hfp_sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D1;
866         } else {
867             // no other options
868             return 0;
869         }
870     }
871 
872     log_info("e)SCO Connection: try new link_setting %d", next_setting);
873     hfp_sco_establishment_active->establish_audio_connection = 1;
874     hfp_sco_establishment_active->link_setting = next_setting;
875     hfp_sco_establishment_active->accept_sco = 0;
876     hfp_sco_establishment_active = NULL;
877     return 1;
878 }
879 
880 void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){
881     UNUSED(packet_type);
882     UNUSED(channel);    // ok: no channel
883     UNUSED(size);
884 
885     bd_addr_t event_addr;
886     hci_con_handle_t handle;
887     hfp_connection_t * hfp_connection = NULL;
888     uint8_t status;
889 
890     log_debug("HFP HCI event handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size);
891 
892     switch (hci_event_packet_get_type(packet)) {
893 
894         case HCI_EVENT_CONNECTION_REQUEST:
895             switch(hci_event_connection_request_get_link_type(packet)){
896                 case 0: //  SCO
897                 case 2: // eSCO
898                     hci_event_connection_request_get_bd_addr(packet, event_addr);
899                     hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
900                     if (!hfp_connection) break;
901                     if (hci_event_connection_request_get_link_type(packet) == 2){
902                         hfp_connection->accept_sco = 2;
903                     } else {
904                         hfp_connection->accept_sco = 1;
905                     }
906                     // configure SBC coded if needed
907                     hfp_prepare_for_sco(hfp_connection);
908                     log_info("accept sco %u\n", hfp_connection->accept_sco);
909                     break;
910                 default:
911                     break;
912             }
913             break;
914 
915         case HCI_EVENT_COMMAND_STATUS:
916             if (hci_event_command_status_get_command_opcode(packet) == hci_setup_synchronous_connection.opcode) {
917                 if (hfp_sco_establishment_active == NULL) break;
918                 status = hci_event_command_status_get_status(packet);
919                 if (status == ERROR_CODE_SUCCESS) break;
920 
921                 hfp_connection = hfp_sco_establishment_active;
922                 if (hfp_handle_failed_sco_connection(status)) break;
923 
924                 hfp_connection->accept_sco = 0;
925                 hfp_connection->establish_audio_connection = 0;
926                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
927                 hfp_sco_establishment_active = NULL;
928                 hfp_emit_sco_connection_established(hfp_connection, status,
929                                                     hfp_connection->negotiated_codec, 0, 0);
930             }
931             break;
932 
933         case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{
934             if (hfp_sco_establishment_active == NULL) break;
935 
936             hfp_connection = hfp_sco_establishment_active;
937 
938             status = hci_event_synchronous_connection_complete_get_status(packet);
939             if (status != ERROR_CODE_SUCCESS){
940                 if (hfp_handle_failed_sco_connection(status)) break;
941 
942                 hfp_connection->accept_sco = 0;
943                 hfp_connection->establish_audio_connection = 0;
944                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
945                 hfp_sco_establishment_active = NULL;
946                 hfp_emit_sco_connection_established(hfp_connection, status,
947                                                     hfp_connection->negotiated_codec, 0, 0);
948                 break;
949             }
950 
951             uint16_t sco_handle = hci_event_synchronous_connection_complete_get_handle(packet);
952             uint8_t  link_type = hci_event_synchronous_connection_complete_get_link_type(packet);
953             uint8_t  transmission_interval = hci_event_synchronous_connection_complete_get_transmission_interval(packet);  // measured in slots
954             uint8_t  retransmission_interval = hci_event_synchronous_connection_complete_get_retransmission_interval(packet);// measured in slots
955             uint16_t rx_packet_length = hci_event_synchronous_connection_complete_get_rx_packet_length(packet); // measured in bytes
956             uint16_t tx_packet_length = hci_event_synchronous_connection_complete_get_tx_packet_length(packet); // measured in bytes
957 
958             switch (link_type){
959                 case 0x00:
960                     log_info("SCO Connection established.");
961                     if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval);
962                     if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval);
963                     if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length);
964                     if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length);
965                     break;
966                 case 0x02:
967                     log_info("eSCO Connection established. \n");
968                     break;
969                 default:
970                     log_error("(e)SCO reserved link_type 0x%2x", link_type);
971                     break;
972             }
973 
974             log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, "
975                  " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)\n", sco_handle,
976                  bd_addr_to_str(event_addr), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length,
977                  hci_event_synchronous_connection_complete_get_air_mode(packet));
978 
979             if (hfp_connection->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){
980                 log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN");
981                 hfp_connection->state = HFP_W2_DISCONNECT_SCO;
982                 break;
983             }
984             hfp_connection->sco_handle = sco_handle;
985             hfp_connection->accept_sco = 0;
986             hfp_connection->establish_audio_connection = 0;
987 
988             hfp_connection->state = HFP_AUDIO_CONNECTION_ESTABLISHED;
989 
990             switch (hfp_connection->vra_state){
991                 case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
992                     hfp_connection->ag_audio_connection_opened_before_vra = false;
993                     break;
994                 default:
995                     hfp_connection->ag_audio_connection_opened_before_vra = true;
996                     break;
997             }
998 
999             hfp_sco_establishment_active = NULL;
1000 
1001             hfp_emit_sco_connection_established(hfp_connection, status,
1002                                                 hfp_connection->negotiated_codec, rx_packet_length, tx_packet_length);
1003 
1004 #ifdef ENABLE_NXP_PCM_WBS
1005             hfp_connection->nxp_start_audio_handle = hfp_connection->sco_handle;
1006 #endif
1007             break;
1008         }
1009 
1010         case HCI_EVENT_DISCONNECTION_COMPLETE:
1011             handle = little_endian_read_16(packet,3);
1012             hfp_connection = get_hfp_connection_context_for_sco_handle(handle, local_role);
1013 
1014             if (!hfp_connection) break;
1015 
1016 #ifdef ENABLE_CC256X_ASSISTED_HFP
1017             hfp_connection->cc256x_send_wbs_disassociate = true;
1018 #endif
1019 #ifdef ENABLE_BCM_PCM_WBS
1020             hfp_connection->bcm_send_disable_wbs = true;
1021 #endif
1022 #ifdef ENABLE_NXP_PCM_WBS
1023             hfp_connection->nxp_stop_audio_handle = hfp_connection->sco_handle;
1024 #endif
1025 
1026             if (hfp_connection->sco_handle == handle){
1027                 hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID;
1028                 hfp_connection->release_audio_connection = 0;
1029                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
1030                 hfp_emit_audio_connection_released(hfp_connection, handle);
1031 
1032                 hfp_connection->ag_audio_connection_opened_before_vra = false;
1033 
1034                 if (hfp_connection->acl_handle == HCI_CON_HANDLE_INVALID){
1035                     hfp_reset_voice_recognition(hfp_connection);
1036                     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0);
1037                     hfp_finalize_connection_context(hfp_connection);
1038                     break;
1039                 } else if (hfp_connection->release_slc_connection == 1){
1040                     hfp_connection->release_slc_connection = 0;
1041                     hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
1042                     rfcomm_disconnect(hfp_connection->acl_handle);
1043                 }
1044             }
1045 
1046             if (hfp_connection->state == HFP_W4_SCO_DISCONNECTED_TO_SHUTDOWN){
1047                 // RFCOMM already closed -> remote power off
1048 #if defined(ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
1049                 hfp_connection->state = HFP_W4_WBS_SHUTDOWN;
1050 #else
1051                 hfp_finalize_connection_context(hfp_connection);
1052 #endif
1053                 break;
1054             }
1055             break;
1056 
1057         default:
1058             break;
1059     }
1060 }
1061 
1062 
1063 void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){
1064     UNUSED(packet_type);
1065     UNUSED(channel);    // ok: no channel
1066     UNUSED(size);
1067 
1068     bd_addr_t event_addr;
1069     uint16_t rfcomm_cid;
1070     hfp_connection_t * hfp_connection = NULL;
1071     uint8_t status;
1072 
1073     log_debug("HFP packet_handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size);
1074 
1075     switch (hci_event_packet_get_type(packet)) {
1076 
1077         case RFCOMM_EVENT_INCOMING_CONNECTION:
1078             // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
1079             rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
1080             hfp_connection = hfp_create_connection(event_addr, local_role);
1081             if (!hfp_connection){
1082                 log_info("hfp: no memory to accept incoming connection - decline");
1083                 rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet));
1084                 return;
1085             }
1086             if (hfp_connection->state != HFP_IDLE) {
1087                 log_error("hfp: incoming connection but not idle, reject");
1088                 rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet));
1089                 return;
1090             }
1091 
1092             hfp_connection->rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
1093             hfp_connection->state = HFP_W4_RFCOMM_CONNECTED;
1094             rfcomm_accept_connection(hfp_connection->rfcomm_cid);
1095             break;
1096 
1097         case RFCOMM_EVENT_CHANNEL_OPENED:
1098             rfcomm_event_channel_opened_get_bd_addr(packet, event_addr);
1099 
1100             hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
1101             btstack_assert(hfp_connection != NULL);
1102 
1103             if (hfp_connection->state != HFP_W4_RFCOMM_CONNECTED){
1104                 break;
1105             }
1106 
1107             status = rfcomm_event_channel_opened_get_status(packet);
1108             if (status != ERROR_CODE_SUCCESS) {
1109                 hfp_handle_slc_setup_error(hfp_connection, status);
1110                 break;
1111             }
1112 
1113             hfp_connection->acl_handle = rfcomm_event_channel_opened_get_con_handle(packet);
1114             hfp_connection->rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
1115             hfp_connection->rfcomm_mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
1116             bd_addr_copy(hfp_connection->remote_addr, event_addr);
1117             hfp_connection->state = HFP_EXCHANGE_SUPPORTED_FEATURES;
1118 
1119             if (local_role == HFP_ROLE_HF) {
1120                 // setup HF Indicators
1121                 uint8_t i;
1122                 for (i=0; i < hfp_hf_indicators_nr; i++){
1123                     hfp_connection->generic_status_indicators[i].uuid = hfp_hf_indicators[i];
1124                     hfp_connection->generic_status_indicators[i].state = 0;
1125                 }
1126             }
1127 
1128             rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1129             break;
1130 
1131         case RFCOMM_EVENT_CHANNEL_CLOSED:
1132             rfcomm_cid = little_endian_read_16(packet,2);
1133             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid);
1134             if (!hfp_connection) break;
1135             switch (hfp_connection->state){
1136                 case HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART:
1137                     hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID;
1138                     hfp_connection->state = HFP_IDLE;
1139                     hfp_establish_service_level_connection(hfp_connection->remote_addr, hfp_connection->service_uuid, local_role);
1140                     break;
1141 
1142                 case HFP_AUDIO_CONNECTION_ESTABLISHED:
1143                     // service connection was released, this implicitly releases audio connection as well
1144                     hfp_connection->release_audio_connection = 0;
1145                     hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID;
1146                     hfp_connection->state = HFP_W4_SCO_DISCONNECTED_TO_SHUTDOWN;
1147                     gap_disconnect(hfp_connection->sco_handle);
1148                     break;
1149 
1150                 default:
1151                     // regular case
1152                     hfp_reset_voice_recognition(hfp_connection);
1153                     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0);
1154                     hfp_finalize_connection_context(hfp_connection);
1155                     break;
1156             }
1157             break;
1158 
1159         default:
1160             break;
1161     }
1162 }
1163 // translates command string into hfp_command_t CMD
1164 
1165 typedef struct {
1166     const char * command;
1167     hfp_command_t command_id;
1168 } hfp_command_entry_t;
1169 
1170 static hfp_command_entry_t hfp_ag_command_table[] = {
1171     { "AT+BAC=",   HFP_CMD_AVAILABLE_CODECS },
1172     { "AT+BCC",    HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP },
1173     { "AT+BCS=",   HFP_CMD_HF_CONFIRMED_CODEC },
1174     { "AT+BIA=",   HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE, }, // +BIA:<enabled>,,<enabled>,,,<enabled>
1175     { "AT+BIEV=",  HFP_CMD_HF_INDICATOR_STATUS },
1176     { "AT+BIND=",  HFP_CMD_LIST_GENERIC_STATUS_INDICATORS },
1177     { "AT+BIND=?", HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS },
1178     { "AT+BIND?",  HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE },
1179     { "AT+BINP=",  HFP_CMD_HF_REQUEST_PHONE_NUMBER },
1180     { "AT+BLDN",   HFP_CMD_REDIAL_LAST_NUMBER },
1181     { "AT+BRSF=",  HFP_CMD_SUPPORTED_FEATURES },
1182     { "AT+BTRH=",  HFP_CMD_RESPONSE_AND_HOLD_COMMAND },
1183     { "AT+BTRH?",  HFP_CMD_RESPONSE_AND_HOLD_QUERY },
1184     { "AT+BVRA=",  HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION },
1185     { "AT+CCWA=",  HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION},
1186     { "AT+CHLD=",  HFP_CMD_CALL_HOLD },
1187     { "AT+CHLD=?", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES },
1188     { "AT+CHUP",   HFP_CMD_HANG_UP_CALL },
1189     { "AT+CIND=?", HFP_CMD_RETRIEVE_AG_INDICATORS },
1190     { "AT+CIND?",  HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS },
1191     { "AT+CLCC",   HFP_CMD_LIST_CURRENT_CALLS },
1192     { "AT+CLIP=",  HFP_CMD_ENABLE_CLIP},
1193     { "AT+CMEE=",  HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR},
1194     { "AT+CMER=",  HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE },
1195     { "AT+CNUM",   HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION },
1196     { "AT+COPS=",  HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT },
1197     { "AT+COPS?",  HFP_CMD_QUERY_OPERATOR_SELECTION_NAME },
1198     { "AT+NREC=",  HFP_CMD_TURN_OFF_EC_AND_NR, },
1199     { "AT+VGM=",   HFP_CMD_SET_MICROPHONE_GAIN },
1200     { "AT+VGS=",   HFP_CMD_SET_SPEAKER_GAIN },
1201     { "AT+VTS=",   HFP_CMD_TRANSMIT_DTMF_CODES },
1202     { "ATA",       HFP_CMD_CALL_ANSWERED },
1203 };
1204 
1205 static hfp_command_entry_t hfp_hf_command_table[] = {
1206     { "+BCS:",  HFP_CMD_AG_SUGGESTED_CODEC },
1207     { "+BIND:", HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS },
1208     { "+BINP:", HFP_CMD_AG_SENT_PHONE_NUMBER },
1209     { "+BRSF:", HFP_CMD_SUPPORTED_FEATURES },
1210     { "+BSIR:", HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING },
1211     { "+BTRH:", HFP_CMD_RESPONSE_AND_HOLD_STATUS },
1212     { "+BVRA:", HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION },
1213     { "+CCWA:", HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE, },
1214     { "+CHLD:", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES },
1215     { "+CIEV:", HFP_CMD_TRANSFER_AG_INDICATOR_STATUS},
1216     { "+CIND:", HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC },
1217     { "+CLCC:", HFP_CMD_LIST_CURRENT_CALLS },
1218     { "+CLIP:", HFP_CMD_AG_SENT_CLIP_INFORMATION },
1219     { "+CME ERROR:", HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR },
1220     { "+CNUM:", HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION},
1221     { "+COPS:", HFP_CMD_QUERY_OPERATOR_SELECTION_NAME },
1222     { "+VGM:",  HFP_CMD_SET_MICROPHONE_GAIN },
1223     { "+VGM=",  HFP_CMD_SET_MICROPHONE_GAIN },
1224     { "+VGS:",  HFP_CMD_SET_SPEAKER_GAIN},
1225     { "+VGS=",  HFP_CMD_SET_SPEAKER_GAIN},
1226     { "ERROR",  HFP_CMD_ERROR},
1227     { "NOP",    HFP_CMD_NONE}, // dummy command used by unit tests
1228     { "OK",     HFP_CMD_OK },
1229     { "RING",   HFP_CMD_RING },
1230 };
1231 
1232 static const hfp_custom_at_command_t *
1233 hfp_custom_command_lookup(bool isHandsFree, const char *text) {
1234     btstack_linked_list_t * custom_commands = isHandsFree ? &hfp_custom_commands_hf : &hfp_custom_commands_ag;
1235     btstack_linked_list_iterator_t it;
1236     btstack_linked_list_iterator_init(&it, custom_commands);
1237     while (btstack_linked_list_iterator_has_next(&it)) {
1238         hfp_custom_at_command_t *at_command = (hfp_custom_at_command_t *) btstack_linked_list_iterator_next(&it);
1239         int match = strcmp(text, at_command->command);
1240         if (match == 0) {
1241             return at_command;
1242         }
1243     }
1244     return NULL;
1245 }
1246 
1247 static hfp_command_t parse_command(const char * line_buffer, int isHandsFree){
1248 
1249     // check for custom commands, AG only
1250     const hfp_custom_at_command_t * custom_at_command = hfp_custom_command_lookup(isHandsFree, line_buffer);
1251     if (custom_at_command != NULL){
1252         return HFP_CMD_CUSTOM_MESSAGE;
1253     }
1254 
1255     // table lookup based on role
1256     uint16_t num_entries;
1257     hfp_command_entry_t * table;
1258     if (isHandsFree == 0){
1259         table = hfp_ag_command_table;
1260         num_entries = sizeof(hfp_ag_command_table) / sizeof(hfp_command_entry_t);
1261     } else {
1262         table = hfp_hf_command_table;
1263         num_entries = sizeof(hfp_hf_command_table) / sizeof(hfp_command_entry_t);
1264     }
1265     // binary search
1266     uint16_t left = 0;
1267     uint16_t right = num_entries - 1;
1268     while (left <= right){
1269         uint16_t middle = left + (right - left) / 2;
1270         hfp_command_entry_t *entry = &table[middle];
1271         int match = strcmp(line_buffer, entry->command);
1272         if (match < 0){
1273             // search term is lower than middle element
1274             if (middle == 0) break;
1275             right = middle - 1;
1276         } else if (match == 0){
1277             return entry->command_id;
1278         } else {
1279             // search term is higher than middle element
1280             left = middle + 1;
1281         }
1282     }
1283 
1284     // note: if parser in CMD_HEADER state would treats digits and maybe '+' as separator, match on "ATD" would work.
1285     // note: phone number is currently expected in line_buffer[3..]
1286     // prefix match on 'ATD', AG only
1287     if ((isHandsFree == 0) && (strncmp(line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0)){
1288         return HFP_CMD_CALL_PHONE_NUMBER;
1289     }
1290 
1291     // Valid looking, but unknown commands/responses
1292     if ((isHandsFree == 0) && (strncmp(line_buffer, "AT+", 3) == 0)){
1293         return HFP_CMD_UNKNOWN;
1294     }
1295 
1296     if ((isHandsFree != 0) && (strncmp(line_buffer, "+", 1) == 0)){
1297         return HFP_CMD_UNKNOWN;
1298     }
1299 
1300     return HFP_CMD_NONE;
1301 }
1302 
1303 static void hfp_parser_store_byte(hfp_connection_t * hfp_connection, uint8_t byte){
1304     if ((hfp_connection->line_size + 1) >= HFP_MAX_VR_TEXT_SIZE) return;
1305     hfp_connection->line_buffer[hfp_connection->line_size++] = byte;
1306     hfp_connection->line_buffer[hfp_connection->line_size] = 0;
1307 }
1308 static int hfp_parser_is_buffer_empty(hfp_connection_t * hfp_connection){
1309     return hfp_connection->line_size == 0;
1310 }
1311 
1312 static int hfp_parser_is_end_of_line(uint8_t byte){
1313     return (byte == '\n') || (byte == '\r');
1314 }
1315 
1316 void hfp_parser_reset_line_buffer(hfp_connection_t *hfp_connection) {
1317     hfp_connection->line_size = 0;
1318     // we don't set the first byte to '\0' to allow access to last argument e.g. in hfp_hf_handle_rfcommand
1319 }
1320 
1321 static void hfp_parser_store_if_token(hfp_connection_t * hfp_connection, uint8_t byte){
1322     switch (byte){
1323         case ',':
1324 		case '-':
1325         case ';':
1326         case '(':
1327         case ')':
1328         case '\n':
1329         case '\r':
1330             break;
1331         default:
1332             hfp_parser_store_byte(hfp_connection, byte);
1333             break;
1334     }
1335 }
1336 
1337 static bool hfp_parser_is_separator( uint8_t byte){
1338     switch (byte){
1339         case ',':
1340 		case '-':
1341         case ';':
1342         case '\n':
1343         case '\r':
1344             return true;
1345         default:
1346             return false;
1347     }
1348 }
1349 
1350 // returns true if received bytes was processed. Otherwise, functions will be called with same byte again
1351 // this is used to for a one byte lookahead, where an unexpected byte is pushed back by returning false
1352 static bool hfp_parse_byte(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){
1353 
1354 #ifdef HFP_DEBUG
1355     if (byte >= ' '){
1356         printf("Parse  '%c' - state %u, buffer %s\n", byte, hfp_connection->parser_state, hfp_connection->line_buffer);
1357     } else {
1358         printf("Parse 0x%02x - state %u, buffer %s\n", byte, hfp_connection->parser_state, hfp_connection->line_buffer);
1359     }
1360 #endif
1361 
1362     // handle doubles quotes
1363     if (byte == '"'){
1364         hfp_connection->parser_quoted = !hfp_connection->parser_quoted;
1365         return true;
1366     }
1367     if (hfp_connection->parser_quoted) {
1368         hfp_parser_store_byte(hfp_connection, byte);
1369         return true;
1370     }
1371 
1372     // ignore spaces outside command or double quotes (required e.g. for '+CME ERROR:..") command
1373     if ((byte == ' ') && (hfp_connection->parser_state != HFP_PARSER_CMD_HEADER)) return true;
1374 
1375     bool processed = true;
1376 
1377     switch (hfp_connection->parser_state) {
1378         case HFP_PARSER_CMD_HEADER:
1379             switch (byte) {
1380                 case '\n':
1381                 case '\r':
1382                 case ';':
1383                     // ignore separator
1384                     break;
1385                 case ':':
1386                 case '?':
1387                     // store separator
1388                     hfp_parser_store_byte(hfp_connection, byte);
1389                     break;
1390                 case '=':
1391                     // equal sign: remember and wait for next char to decided between '=?' and '=\?'
1392                     hfp_connection->found_equal_sign = true;
1393                     hfp_parser_store_byte(hfp_connection, byte);
1394                     return true;
1395                 default:
1396                     // store if not lookahead
1397                     if (!hfp_connection->found_equal_sign) {
1398                         hfp_parser_store_byte(hfp_connection, byte);
1399                         return true;
1400                     }
1401                     // mark as lookahead
1402                     processed = false;
1403                     break;
1404             }
1405 
1406             // ignore empty tokens
1407             if (hfp_parser_is_buffer_empty(hfp_connection)) return true;
1408 
1409             // parse
1410             hfp_connection->command = parse_command((char *)hfp_connection->line_buffer, isHandsFree);
1411 
1412             // pick +CIND version based on connection state: descriptions during SLC vs. states later
1413             if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC){
1414                 switch(hfp_connection->state){
1415                     case HFP_W4_RETRIEVE_INDICATORS_STATUS:
1416                         hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS;
1417                         break;
1418                     case HFP_W4_RETRIEVE_INDICATORS:
1419                         hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS;
1420                         break;
1421                     default:
1422                         hfp_connection->command = HFP_CMD_UNKNOWN;
1423                         break;
1424                 }
1425             }
1426 
1427             log_info("command string '%s', handsfree %u -> cmd id %u", (char *)hfp_connection->line_buffer, isHandsFree, hfp_connection->command);
1428 
1429             // store command id for custom command and just store rest of line
1430             if (hfp_connection->command == HFP_CMD_CUSTOM_MESSAGE){
1431                 const hfp_custom_at_command_t * at_command = hfp_custom_command_lookup(isHandsFree, (const char *) hfp_connection->line_buffer);
1432                 hfp_connection->custom_at_command_id = at_command->command_id;
1433                 hfp_connection->parser_state = HFP_PARSER_CUSTOM_COMMAND;
1434                 return processed;
1435             }
1436 
1437             // next state
1438             hfp_parser_reset_line_buffer(hfp_connection);
1439             hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
1440 
1441             return processed;
1442 
1443         case HFP_PARSER_CMD_SEQUENCE:
1444             // handle empty fields
1445             if ((byte == ',' ) && (hfp_connection->line_size == 0)){
1446                 hfp_connection->line_buffer[0] = 0;
1447                 hfp_connection->ignore_value = 1;
1448                 parse_sequence(hfp_connection);
1449                 return true;
1450             }
1451 
1452             hfp_parser_store_if_token(hfp_connection, byte);
1453             if (!hfp_parser_is_separator(byte)) return true;
1454 
1455             // ignore empty tokens
1456             switch (hfp_connection->command){
1457                 case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
1458                     // don't ignore empty string
1459                     break;
1460                 default:
1461                     if (hfp_parser_is_buffer_empty(hfp_connection) && (hfp_connection->ignore_value == 0)) {
1462                         return true;
1463                     }
1464                     break;
1465             }
1466 
1467             parse_sequence(hfp_connection);
1468 
1469             hfp_parser_reset_line_buffer(hfp_connection);
1470 
1471             switch (hfp_connection->command){
1472                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
1473                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1474                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1475                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1476                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1477                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1478                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
1479                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
1480                 case HFP_CMD_HF_INDICATOR_STATUS:
1481                     hfp_connection->parser_state = HFP_PARSER_SECOND_ITEM;
1482                     break;
1483                 default:
1484                     break;
1485             }
1486             return true;
1487 
1488         case HFP_PARSER_SECOND_ITEM:
1489 
1490             hfp_parser_store_if_token(hfp_connection, byte);
1491             if (!hfp_parser_is_separator(byte)) return true;
1492 
1493             switch (hfp_connection->command){
1494                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1495                     log_info("format %s, ", hfp_connection->line_buffer);
1496                     hfp_connection->network_operator.format =  btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1497                     break;
1498                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1499                     log_info("format %s \n", hfp_connection->line_buffer);
1500                     hfp_connection->network_operator.format =  btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1501                     break;
1502                 case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1503                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1504                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
1505                     hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].state = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1506                     break;
1507                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1508                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1509                     log_info("%d \n", hfp_connection->ag_indicators[hfp_connection->parser_item_index].status);
1510                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status_changed = 1;
1511                     break;
1512                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
1513                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].min_range = btstack_atoi((char *)hfp_connection->line_buffer);
1514                     log_info("%s, ", hfp_connection->line_buffer);
1515                     break;
1516                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
1517                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1518                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1519                     hfp_connection->bnip_type = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1520                     break;
1521                 case HFP_CMD_HF_INDICATOR_STATUS:
1522                     hfp_connection->parser_indicator_value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1523                     break;
1524                 default:
1525                     break;
1526             }
1527 
1528             hfp_parser_reset_line_buffer(hfp_connection);
1529 
1530             hfp_connection->parser_state = HFP_PARSER_THIRD_ITEM;
1531 
1532             return true;
1533 
1534         case HFP_PARSER_THIRD_ITEM:
1535 
1536             hfp_parser_store_if_token(hfp_connection, byte);
1537             if (!hfp_parser_is_separator(byte)) return true;
1538 
1539             switch (hfp_connection->command){
1540                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1541                     btstack_strcpy(hfp_connection->network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE,  (char *)hfp_connection->line_buffer);
1542                     break;
1543                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
1544                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].max_range = btstack_atoi((char *)hfp_connection->line_buffer);
1545                     hfp_next_indicators_index(hfp_connection);
1546                     hfp_connection->ag_indicators_nr = hfp_connection->parser_item_index;
1547                     break;
1548                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1549                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1550                     // track if last argument exists
1551                     hfp_connection->clip_have_alpha = hfp_connection->line_size != 0;
1552                     break;
1553                 default:
1554                     break;
1555             }
1556 
1557             hfp_parser_reset_line_buffer(hfp_connection);
1558 
1559             if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS){
1560                 hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
1561             }
1562             return true;
1563 
1564         case HFP_PARSER_CUSTOM_COMMAND:
1565             if (hfp_parser_is_end_of_line(byte) == false){
1566                 hfp_parser_store_byte(hfp_connection, byte);
1567             }
1568             return true;
1569 
1570         default:
1571             btstack_assert(false);
1572             return true;
1573     }
1574 }
1575 
1576 void hfp_parse(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){
1577     bool processed = false;
1578     while (!processed){
1579         processed = hfp_parse_byte(hfp_connection, byte, isHandsFree);
1580     }
1581     // reset parser state on end-of-line
1582     if (hfp_parser_is_end_of_line(byte)){
1583         hfp_connection->found_equal_sign = false;
1584         hfp_connection->parser_item_index = 0;
1585         hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
1586     }
1587 }
1588 
1589 static void parse_sequence(hfp_connection_t * hfp_connection){
1590     int value;
1591     switch (hfp_connection->command){
1592         case HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS:
1593             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1594             int i;
1595             switch (hfp_connection->parser_item_index){
1596                 case 0:
1597                     for (i=0;i<hfp_connection->generic_status_indicators_nr;i++){
1598                         if (hfp_connection->generic_status_indicators[i].uuid == value){
1599                             hfp_connection->parser_indicator_index = i;
1600                             break;
1601                         }
1602                     }
1603                     break;
1604                 case 1:
1605                     if (hfp_connection->parser_indicator_index <0) break;
1606                     hfp_connection->generic_status_indicators[hfp_connection->parser_indicator_index].state = value;
1607                     log_info("HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS set indicator at index %u, to %u\n",
1608                      hfp_connection->parser_item_index, value);
1609                     break;
1610                 default:
1611                     break;
1612             }
1613             hfp_next_indicators_index(hfp_connection);
1614             break;
1615 
1616         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1617             switch(hfp_connection->parser_item_index){
1618                 case 0:
1619                     // <alpha>: This optional field is not supported, and shall be left blank.
1620                     break;
1621                 case 1:
1622                     // <number>: Quoted string containing the phone number in the format specified by <type>.
1623                     btstack_strcpy(hfp_connection->bnip_number, sizeof(hfp_connection->bnip_number), (char *)hfp_connection->line_buffer);
1624                     break;
1625                 case 2:
1626                     /*
1627                       <type> field specifies the format of the phone number provided, and can be one of the following values:
1628                       - values 128-143: The phone number format may be a national or international format, and may contain prefix and/or escape digits. No changes on the number presentation are required.
1629                      - values 144-159: The phone number format is an international number, including the country code prefix. If the plus sign ("+") is not included as part of the number and shall be added by the AG as needed.
1630                      - values 160-175: National number. No prefix nor escape digits included.
1631                      */
1632                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1633                     hfp_connection->bnip_type = value;
1634                     break;
1635                 case 3:
1636                     // <speed>: This optional field is not supported, and shall be left blank.
1637                     break;
1638                 case 4:
1639                     // <service>: Indicates which service this phone number relates to. Shall be either 4 (voice) or 5 (fax).
1640                 default:
1641                     break;
1642             }
1643             // index > 2 are ignored in switch above
1644             hfp_connection->parser_item_index++;
1645             break;
1646         case HFP_CMD_LIST_CURRENT_CALLS:
1647             switch(hfp_connection->parser_item_index){
1648                 case 0:
1649                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1650                     hfp_connection->clcc_idx = value;
1651                     break;
1652                 case 1:
1653                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1654                     hfp_connection->clcc_dir = value;
1655                     break;
1656                 case 2:
1657                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1658                     hfp_connection->clcc_status = value;
1659                     break;
1660                 case 3:
1661                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1662                     hfp_connection->clcc_mode = value;
1663                     break;
1664                 case 4:
1665                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1666                     hfp_connection->clcc_mpty = value;
1667                     break;
1668                 case 5:
1669                     btstack_strcpy(hfp_connection->bnip_number, sizeof(hfp_connection->bnip_number), (char *)hfp_connection->line_buffer);
1670                     break;
1671                 case 6:
1672                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1673                     hfp_connection->bnip_type = value;
1674                     break;
1675                 default:
1676                     break;
1677             }
1678             // index > 6 are ignored in switch above
1679             hfp_connection->parser_item_index++;
1680             break;
1681         case HFP_CMD_SET_MICROPHONE_GAIN:
1682             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1683             hfp_connection->microphone_gain = value;
1684             log_info("hfp parse HFP_CMD_SET_MICROPHONE_GAIN %d\n", value);
1685             break;
1686         case HFP_CMD_SET_SPEAKER_GAIN:
1687             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1688             hfp_connection->speaker_gain = value;
1689             log_info("hfp parse HFP_CMD_SET_SPEAKER_GAIN %d\n", value);
1690             break;
1691         case HFP_CMD_TURN_OFF_EC_AND_NR:
1692             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1693             hfp_connection->ag_echo_and_noise_reduction = value;
1694             log_info("hfp parse HFP_CMD_TURN_OFF_EC_AND_NR %d\n", value);
1695             break;
1696         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
1697             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1698             hfp_connection->remote_supported_features = store_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE, value);
1699             log_info("hfp parse HFP_CHANGE_IN_BAND_RING_TONE_SETTING %d\n", value);
1700             break;
1701         case HFP_CMD_HF_CONFIRMED_CODEC:
1702             hfp_connection->codec_confirmed = btstack_atoi((char*)hfp_connection->line_buffer);
1703             log_info("hfp parse HFP_CMD_HF_CONFIRMED_CODEC %d\n", hfp_connection->codec_confirmed);
1704             break;
1705         case HFP_CMD_AG_SUGGESTED_CODEC:
1706             hfp_connection->suggested_codec = btstack_atoi((char*)hfp_connection->line_buffer);
1707             log_info("hfp parse HFP_CMD_AG_SUGGESTED_CODEC %d\n", hfp_connection->suggested_codec);
1708             break;
1709         case HFP_CMD_SUPPORTED_FEATURES:
1710             hfp_connection->remote_supported_features = btstack_atoi((char*)hfp_connection->line_buffer);
1711             log_info("Parsed supported feature %d\n", (int) hfp_connection->remote_supported_features);
1712             break;
1713         case HFP_CMD_AVAILABLE_CODECS:
1714             log_info("Parsed codec %s\n", hfp_connection->line_buffer);
1715             hfp_connection->remote_codecs[hfp_connection->parser_item_index] = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1716             hfp_next_codec_index(hfp_connection);
1717             hfp_connection->remote_codecs_nr = hfp_connection->parser_item_index;
1718             break;
1719         case HFP_CMD_RETRIEVE_AG_INDICATORS:
1720             btstack_strcpy((char *)hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, HFP_MAX_INDICATOR_DESC_SIZE, (char *)hfp_connection->line_buffer);
1721             hfp_connection->ag_indicators[hfp_connection->parser_item_index].index = hfp_connection->parser_item_index+1;
1722             log_info("Indicator %d: %s (", hfp_connection->ag_indicators_nr+1, hfp_connection->line_buffer);
1723             break;
1724         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1725             log_info("Parsed Indicator %d with status: %s\n", hfp_connection->parser_item_index+1, hfp_connection->line_buffer);
1726             hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = btstack_atoi((char *) hfp_connection->line_buffer);
1727             hfp_next_indicators_index(hfp_connection);
1728             break;
1729         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
1730             hfp_next_indicators_index(hfp_connection);
1731             if (hfp_connection->parser_item_index != 4) break;
1732             log_info("Parsed Enable indicators: %s\n", hfp_connection->line_buffer);
1733             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1734             hfp_connection->enable_status_update_for_ag_indicators = (uint8_t) value;
1735             break;
1736         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
1737             log_info("Parsed Support call hold: %s\n", hfp_connection->line_buffer);
1738             if (hfp_connection->line_size > 2 ) break;
1739             memcpy((char *)hfp_connection->remote_call_services[hfp_connection->remote_call_services_index].name, (char *)hfp_connection->line_buffer, HFP_CALL_SERVICE_SIZE-1);
1740             hfp_connection->remote_call_services[hfp_connection->remote_call_services_index].name[HFP_CALL_SERVICE_SIZE - 1] = 0;
1741             hfp_next_remote_call_services_index(hfp_connection);
1742             break;
1743         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1744         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1745             log_info("Parsed Generic status indicator: %s\n", hfp_connection->line_buffer);
1746             hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].uuid = (uint16_t)btstack_atoi((char*)hfp_connection->line_buffer);
1747             hfp_next_indicators_index(hfp_connection);
1748             hfp_connection->generic_status_indicators_nr = hfp_connection->parser_item_index;
1749             break;
1750         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
1751             // HF parses inital AG gen. ind. state
1752             log_info("Parsed List generic status indicator %s state: ", hfp_connection->line_buffer);
1753             hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection);
1754             break;
1755         case HFP_CMD_HF_INDICATOR_STATUS:
1756             hfp_connection->parser_indicator_index = hfp_parse_indicator_index(hfp_connection);
1757             log_info("Parsed HF indicator index %u", hfp_connection->parser_indicator_index);
1758             break;
1759         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
1760             // AG parses new gen. ind. state
1761             if (hfp_connection->ignore_value){
1762                 hfp_connection->ignore_value = 0;
1763                 log_info("Parsed Enable AG indicator pos %u('%s') - unchanged (stays %u)\n", hfp_connection->parser_item_index,
1764                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled);
1765             }
1766             else if (hfp_connection->ag_indicators[hfp_connection->parser_item_index].mandatory){
1767                 log_info("Parsed Enable AG indicator pos %u('%s') - ignore (mandatory)\n",
1768                     hfp_connection->parser_item_index, hfp_connection->ag_indicators[hfp_connection->parser_item_index].name);
1769             } else {
1770                 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1771                 hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled = value;
1772                 log_info("Parsed Enable AG indicator pos %u('%s'): %u\n", hfp_connection->parser_item_index,
1773                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, value);
1774             }
1775             hfp_next_indicators_index(hfp_connection);
1776             break;
1777         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1778             // indicators are indexed starting with 1
1779             hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection);
1780             log_info("Parsed status of the AG indicator %d, status ", hfp_connection->parser_item_index);
1781             break;
1782         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1783             hfp_connection->network_operator.mode = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1784             log_info("Parsed network operator mode: %d, ", hfp_connection->network_operator.mode);
1785             break;
1786         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1787             if (hfp_connection->line_buffer[0] == '3'){
1788                 log_info("Parsed Set network operator format : %s, ", hfp_connection->line_buffer);
1789                 break;
1790             }
1791             // TODO emit ERROR, wrong format
1792             log_info("ERROR Set network operator format: index %s not supported\n", hfp_connection->line_buffer);
1793             break;
1794         case HFP_CMD_ERROR:
1795             break;
1796         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1797             hfp_connection->extended_audio_gateway_error = 1;
1798             hfp_connection->extended_audio_gateway_error_value = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1799             break;
1800         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
1801             hfp_connection->enable_extended_audio_gateway_error_report = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1802             hfp_connection->extended_audio_gateway_error = 0;
1803             break;
1804         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1805         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1806         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1807             btstack_strcpy((char *)hfp_connection->bnip_number, sizeof(hfp_connection->bnip_number), (char *)hfp_connection->line_buffer);
1808             break;
1809         case HFP_CMD_CALL_HOLD:
1810             hfp_connection->ag_call_hold_action = hfp_connection->line_buffer[0] - '0';
1811             if (hfp_connection->line_buffer[1] != '\0'){
1812                 hfp_connection->call_index = btstack_atoi((char *)&hfp_connection->line_buffer[1]);
1813             }
1814             break;
1815         case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
1816             hfp_connection->ag_response_and_hold_action = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1817             break;
1818         case HFP_CMD_TRANSMIT_DTMF_CODES:
1819             hfp_connection->ag_dtmf_code = hfp_connection->line_buffer[0];
1820             break;
1821         case HFP_CMD_ENABLE_CLIP:
1822             hfp_connection->clip_enabled = hfp_connection->line_buffer[0] != '0';
1823             break;
1824         case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
1825             hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[0] != '0';
1826             break;
1827         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
1828             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1829             hfp_connection->ag_activate_voice_recognition_value = value;
1830             break;
1831         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
1832             switch(hfp_connection->parser_item_index){
1833                 case 0:
1834                     hfp_connection->ag_vra_status = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1835                     break;
1836                 case 1:
1837                     hfp_connection->ag_vra_state = (hfp_voice_recognition_state_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1838                     break;
1839                 case 2:
1840                     hfp_connection->ag_msg.text_id = 0;
1841                     for (i = 0 ; i < 4; i++){
1842                         hfp_connection->ag_msg.text_id = (hfp_connection->ag_msg.text_id << 4) | nibble_for_char(hfp_connection->line_buffer[i]);
1843                     }
1844                     break;
1845                 case 3:
1846                     hfp_connection->ag_msg.text_type = (hfp_text_type_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1847                     break;
1848                 case 4:
1849                     hfp_connection->ag_msg.text_operation = (hfp_text_operation_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1850                     break;
1851                 case 5:
1852                     hfp_connection->line_buffer[hfp_connection->line_size] = 0;
1853                     hfp_connection->ag_vra_msg_length = hfp_connection->line_size + 1;
1854                     break;
1855                 default:
1856                     break;
1857             }
1858             hfp_connection->parser_item_index++;
1859             break;
1860         default:
1861             break;
1862     }
1863 }
1864 
1865 static void hfp_handle_start_sdp_client_query(void * context){
1866     UNUSED(context);
1867 
1868     btstack_linked_list_iterator_t it;
1869     btstack_linked_list_iterator_init(&it, &hfp_connections);
1870     while (btstack_linked_list_iterator_has_next(&it)){
1871         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1872 
1873         if (connection->state != HFP_W2_SEND_SDP_QUERY) continue;
1874 
1875         connection->state = HFP_W4_SDP_QUERY_COMPLETE;
1876         hfp_sdp_query_context.local_role = connection->local_role;
1877         (void)memcpy(hfp_sdp_query_context.remote_address, connection->remote_addr, 6);
1878         sdp_client_query_rfcomm_channel_and_name_for_service_class_uuid(&handle_query_rfcomm_event, connection->remote_addr, connection->service_uuid);
1879         return;
1880     }
1881 }
1882 
1883 uint8_t hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid, hfp_role_t local_role){
1884     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr, local_role);
1885     if (connection != NULL){
1886         // allow to call hfp_establish_service_level_connection after SLC was triggered remotely
1887         // @note this also allows to call hfp_establish_service_level_connection again before SLC is complete
1888         if (connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
1889             return ERROR_CODE_SUCCESS;
1890         } else {
1891             return ERROR_CODE_COMMAND_DISALLOWED;
1892         }
1893     }
1894 
1895     connection = hfp_create_connection(bd_addr, local_role);
1896     if (!connection){
1897         return BTSTACK_MEMORY_ALLOC_FAILED;
1898     }
1899 
1900     connection->state = HFP_W2_SEND_SDP_QUERY;
1901 
1902     bd_addr_copy(connection->remote_addr, bd_addr);
1903     connection->service_uuid = service_uuid;
1904 
1905     hfp_sdp_query_request.callback = &hfp_handle_start_sdp_client_query;
1906     // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback
1907     (void) sdp_client_register_query_callback(&hfp_sdp_query_request);
1908     return ERROR_CODE_SUCCESS;
1909 }
1910 
1911 void hfp_trigger_release_service_level_connection(hfp_connection_t * hfp_connection){
1912     // called internally, NULL check already performed
1913     btstack_assert(hfp_connection != NULL);
1914 
1915     hfp_trigger_release_audio_connection(hfp_connection);
1916     if (hfp_connection->state < HFP_W4_RFCOMM_CONNECTED){
1917         hfp_connection->state = HFP_IDLE;
1918         return;
1919     }
1920 
1921     if (hfp_connection->state == HFP_W4_RFCOMM_CONNECTED){
1922         hfp_connection->state = HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN;
1923         return;
1924     }
1925     hfp_connection->release_slc_connection = 1;
1926     if (hfp_connection->state < HFP_W4_SCO_CONNECTED){
1927         hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
1928         return;
1929     }
1930 
1931     if (hfp_connection->state < HFP_W4_SCO_DISCONNECTED){
1932         hfp_connection->state = HFP_W2_DISCONNECT_SCO;
1933         hfp_connection->release_audio_connection = 1;
1934         return;
1935     }
1936 }
1937 
1938 uint8_t hfp_trigger_release_audio_connection(hfp_connection_t * hfp_connection){
1939     // called internally, NULL check already performed
1940     btstack_assert(hfp_connection != NULL);
1941     if (hfp_connection->state <= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
1942         return ERROR_CODE_COMMAND_DISALLOWED;
1943     }
1944     switch (hfp_connection->state) {
1945         case HFP_W2_CONNECT_SCO:
1946             hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
1947             break;
1948         case HFP_W4_SCO_CONNECTED:
1949         case HFP_AUDIO_CONNECTION_ESTABLISHED:
1950             hfp_connection->release_audio_connection = 1;
1951             break;
1952         default:
1953             return ERROR_CODE_COMMAND_DISALLOWED;
1954     }
1955     return ERROR_CODE_SUCCESS;
1956 }
1957 
1958 bool hfp_sco_setup_active(void){
1959     return hfp_sco_establishment_active != NULL;
1960 }
1961 
1962 void hfp_setup_synchronous_connection(hfp_connection_t * hfp_connection){
1963 
1964     hfp_sco_establishment_active = hfp_connection;
1965 
1966     // all packet types, fixed bandwidth
1967     int setting = hfp_connection->link_setting;
1968     log_info("hfp_setup_synchronous_connection using setting nr %u", setting);
1969     uint16_t sco_voice_setting = hci_get_sco_voice_setting();
1970     if (hfp_connection->negotiated_codec != HFP_CODEC_CVSD){
1971 #ifdef ENABLE_BCM_PCM_WBS
1972         sco_voice_setting = 0x0063; // Transparent data, 16-bit for BCM controllers
1973 #else
1974         sco_voice_setting = 0x0043; // Transparent data, 8-bit otherwise
1975 #endif
1976     }
1977     uint16_t packet_types = hfp_link_settings[setting].packet_types;
1978     hfp_connection->packet_types = packet_types;
1979 
1980     // get packet types - bits 6-9 are 'don't allow'
1981     uint16_t packet_types_flipped = packet_types ^ 0x03c0;
1982 #if defined(ENABLE_SCO_OVER_PCM) && defined(ENABLE_NXP_PCM_WBS)
1983     uint8_t  radio_coding_format = 3;
1984     uint32_t host_bandwidth      = 0;
1985     uint8_t  coded_data_size     = 0;
1986     switch (hfp_connection->negotiated_codec){
1987         case HFP_CODEC_CVSD:
1988             radio_coding_format = 0x02;
1989             host_bandwidth = 16000;
1990             coded_data_size = 0x10;
1991             break;
1992         case HFP_CODEC_MSBC:
1993             radio_coding_format = 0x05;
1994             host_bandwidth = 32000;
1995             coded_data_size = 0x08;
1996             break;
1997         default:
1998             log_error("Coding format %u not supported by Controller", hfp_connection->negotiated_codec);
1999             btstack_assert(false);
2000             break;
2001     }
2002     hci_send_cmd(&hci_enhanced_setup_synchronous_connection,
2003         // ACL Handle
2004         hfp_connection->acl_handle,
2005         // Transmit_Bandwidth
2006         8000,
2007         // Receive_Bandwidth
2008         8000,
2009         // Transmit_Coding_Format: radio_config_format, company, codec
2010         radio_coding_format, 0x00, 0x00,
2011         // Receive_Coding_Format: radio_config_format, company, codec
2012         radio_coding_format, 0x00, 0x00,
2013         // Transmit_Codec_Frame_Size
2014         0x3c,
2015         // Receive_Codec_Frame_Size
2016         0x3c,
2017         // Input_Bandwidth
2018         host_bandwidth,
2019         // Output_Bandwidth
2020         host_bandwidth,
2021         // Input_Coding_Format, 0x04 = Linear PCM, company, codec
2022         0x04, 0x00, 0x00,
2023         // Output_Coding_Format, 0x04 = Linear PCM, company, codec
2024         0x04, 0x00, 0x00,
2025         // Input_Coded_Data_Size
2026         coded_data_size,
2027         // Output_Coded_Data_Size
2028         coded_data_size,
2029         // Input_PCM_Data_Format, 0x02 = 2’s complement
2030         0x02,
2031         // Output_PCM_Data_Format, 0x02 = 2’s complement
2032         0x02,
2033         // Input_PCM_Sample_Payload_MSB_Position
2034         0x00,
2035         // Output_PCM_Sample_Payload_MSB_Position
2036         0x00,
2037         // Input_Data_Path - vendor specific: NXP - I2S/PCM
2038         0x01,
2039         // Output_Data_Path - vendor specific: NXP - I2S/PCM
2040         0x01,
2041         // Input_Transport_Unit_Size
2042         0x10,
2043         // Output_Transport_Unit_Size
2044         0x10,
2045         //
2046         hfp_link_settings[setting].max_latency,
2047         packet_types_flipped,
2048         hfp_link_settings[setting].retransmission_effort);
2049 #else
2050     hci_send_cmd(&hci_setup_synchronous_connection, hfp_connection->acl_handle, 8000, 8000, hfp_link_settings[setting].max_latency,
2051         sco_voice_setting, hfp_link_settings[setting].retransmission_effort, packet_types_flipped);
2052 #endif
2053 }
2054 
2055 #ifdef ENABLE_HFP_HF_SAFE_SETTINGS
2056 hfp_link_settings_t hfp_safe_settings_for_context(bool use_eSCO, uint8_t negotiated_codec, bool secure_connection_in_use){
2057     uint8_t i;
2058     hfp_link_settings_t link_setting = HFP_LINK_SETTINGS_NONE;
2059     for (i=0 ; i < (sizeof(hfp_mandatory_safe_settings) / sizeof(struct hfp_mandatory_safe_setting)) ; i++){
2060         uint16_t packet_types = hfp_link_settings[(uint8_t)(hfp_mandatory_safe_settings[i].link_setting)].packet_types;
2061         bool is_eSCO_setting = (packet_types & SCO_PACKET_TYPES_ESCO) != 0;
2062         if (is_eSCO_setting != use_eSCO) continue;
2063         if ((hfp_mandatory_safe_settings[i].codec_mask & (1 << negotiated_codec)) == 0) continue;
2064         if (hfp_mandatory_safe_settings[i].secure_connection_in_use != secure_connection_in_use) continue;
2065         link_setting = hfp_mandatory_safe_settings[i].link_setting;
2066         break;
2067     }
2068     return link_setting;
2069 }
2070 #endif
2071 
2072 void hfp_accept_synchronous_connection(hfp_connection_t * hfp_connection, bool use_eSCO){
2073 
2074     hfp_sco_establishment_active = hfp_connection;
2075 
2076     // lookup safe settings based on SCO type, SC use and Codec type
2077     uint16_t max_latency;
2078     uint16_t packet_types;
2079     uint16_t retransmission_effort;
2080 
2081 #ifdef ENABLE_HFP_HF_SAFE_SETTINGS
2082     hfp_link_settings_t link_setting = HFP_LINK_SETTINGS_NONE;
2083     // fallback for non-CVSD codec and SCO connection
2084     if ((hfp_connection->negotiated_codec != HFP_CODEC_CVSD) && (use_eSCO == false)){
2085         max_latency           = 0xffff;
2086         retransmission_effort = 0xff;
2087         packet_types          = SCO_PACKET_TYPES_HV3 | SCO_PACKET_TYPES_HV1;
2088     } else {
2089         // use safe settings from HFP v1.9, table 6.10
2090     bool secure_connection_in_use = gap_secure_connection(hfp_connection->acl_handle);
2091         link_setting = hfp_safe_settings_for_context(use_eSCO, hfp_connection->negotiated_codec, secure_connection_in_use);
2092         max_latency             = hfp_link_settings[(uint8_t) link_setting].max_latency;
2093         retransmission_effort   = hfp_link_settings[(uint8_t) link_setting].retransmission_effort;
2094         packet_types            = hfp_link_settings[(uint8_t) link_setting].packet_types;
2095     }
2096 #else
2097     max_latency           = 0xffff;
2098     retransmission_effort = 0xff;
2099     if (use_eSCO) {
2100         packet_types      = SCO_PACKET_TYPES_EV3 | SCO_PACKET_TYPES_2EV3;
2101     } else {
2102         packet_types      = SCO_PACKET_TYPES_HV3 | SCO_PACKET_TYPES_HV1;
2103     }
2104 #endif
2105 
2106     // transparent data for non-CVSD connections or if codec provided by Controller
2107     uint16_t sco_voice_setting = hci_get_sco_voice_setting();
2108     if (hfp_connection->negotiated_codec != HFP_CODEC_CVSD){
2109 #ifdef ENABLE_BCM_PCM_WBS
2110         sco_voice_setting = 0x0063; // Transparent data, 16-bit for BCM controllers
2111 #else
2112         sco_voice_setting = 0x0043; // Transparent data, 8-bit otherwise
2113 #endif
2114     }
2115 
2116     // filter packet types
2117     packet_types &= hfp_get_sco_packet_types();
2118 
2119     hfp_connection->packet_types = packet_types;
2120 
2121     // bits 6-9 are 'don't allow'
2122     uint16_t packet_types_flipped = packet_types ^ 0x3c0;
2123 
2124     log_info("Sending hci_accept_connection_request: packet types 0x%04x, sco_voice_setting 0x%02x",
2125             packet_types, sco_voice_setting);
2126 
2127 #if defined(ENABLE_SCO_OVER_PCM) && defined(ENABLE_NXP_PCM_WBS)
2128     uint8_t radio_coding_format = 3;
2129     uint32_t host_bandwidth = 0;
2130     uint8_t  coded_data_size = 0;
2131     switch (hfp_connection->negotiated_codec){
2132         case HFP_CODEC_CVSD:
2133             radio_coding_format = 0x02;
2134             host_bandwidth = 16000;
2135             coded_data_size = 0x10;
2136             break;
2137         case HFP_CODEC_MSBC:
2138             radio_coding_format = 0x05;
2139             host_bandwidth = 32000;
2140             coded_data_size = 0x08;
2141             break;
2142         default:
2143             log_error("Coding format %u not supported by Controller", hfp_connection->negotiated_codec);
2144             btstack_assert(false);
2145             break;
2146     }
2147     hci_send_cmd(&hci_enhanced_accept_synchronous_connection,
2148         // BD_ADDR
2149         hfp_connection->remote_addr,
2150         // Transmit_Bandwidth
2151         8000,
2152         // Receive_Bandwidth
2153         8000,
2154         // Transmit_Coding_Format: radio_config_format, company, codec
2155         radio_coding_format, 0x00, 0x00,
2156         // Receive_Coding_Format: radio_config_format, company, codec
2157         radio_coding_format, 0x00, 0x00,
2158         // Transmit_Codec_Frame_Size
2159         0x3c,
2160         // Receive_Codec_Frame_Size
2161         0x3c,
2162         // Input_Bandwidth
2163         host_bandwidth,
2164         // Output_Bandwidth
2165         host_bandwidth,
2166         // Input_Coding_Format, 0x04 = Linear PCM, company, codec
2167         0x04, 0x00, 0x00,
2168         // Output_Coding_Format, 0x04 = Linear PCM, company, codec
2169         0x04, 0x00, 0x00,
2170         // Input_Coded_Data_Size
2171         coded_data_size,
2172         // Output_Coded_Data_Size
2173         coded_data_size,
2174         // Input_PCM_Data_Format, 0x02 = 2’s complement
2175         0x02,
2176         // Output_PCM_Data_Format, 0x02 = 2’s complement
2177         0x02,
2178         // Input_PCM_Sample_Payload_MSB_Position
2179         0x00,
2180         // Output_PCM_Sample_Payload_MSB_Position
2181         0x00,
2182         // Input_Data_Path - vendor specific: NXP - I2S/PCM
2183         0x01,
2184         // Output_Data_Path - vendor specific: NXP - I2S/PCM
2185         0x01,
2186         // Input_Transport_Unit_Size
2187         0x10,
2188         // Output_Transport_Unit_Size
2189         0x10,
2190         //
2191         max_latency,
2192         packet_types_flipped,
2193         retransmission_effort);
2194 #else
2195     hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency,
2196                  sco_voice_setting, retransmission_effort, packet_types_flipped);
2197 #endif
2198 }
2199 
2200 #ifdef ENABLE_CC256X_ASSISTED_HFP
2201 void hfp_cc256x_write_codec_config(hfp_connection_t * hfp_connection){
2202     uint32_t sample_rate_hz;
2203     uint16_t clock_rate_khz;
2204     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
2205         clock_rate_khz = 512;
2206         sample_rate_hz = 16000;
2207     } else {
2208         clock_rate_khz = 256;
2209         sample_rate_hz = 8000;
2210     }
2211     uint8_t clock_direction = 0;        // master
2212     uint16_t frame_sync_duty_cycle = 0; // i2s with 50%
2213     uint8_t  frame_sync_edge = 1;       // rising edge
2214     uint8_t  frame_sync_polarity = 0;   // active high
2215     uint8_t  reserved = 0;
2216     uint16_t size = 16;
2217     uint16_t chan_1_offset = 1;
2218     uint16_t chan_2_offset = chan_1_offset + size;
2219     uint8_t  out_edge = 1;              // rising
2220     uint8_t  in_edge = 0;               // falling
2221     hci_send_cmd(&hci_ti_write_codec_config, clock_rate_khz, clock_direction, sample_rate_hz, frame_sync_duty_cycle,
2222                  frame_sync_edge, frame_sync_polarity, reserved,
2223                  size, chan_1_offset, out_edge, size, chan_1_offset, in_edge, reserved,
2224                  size, chan_2_offset, out_edge, size, chan_2_offset, in_edge, reserved);
2225 }
2226 #endif
2227 
2228 #ifdef ENABLE_BCM_PCM_WBS
2229 void hfp_bcm_write_i2spcm_interface_param(hfp_connection_t * hfp_connection){
2230     uint8_t sample_rate = (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? 1 : 0;
2231     // i2s enable, master, 8/16 kHz, 512 kHz
2232     hci_send_cmd(&hci_bcm_write_i2spcm_interface_param, 1, 1, sample_rate, 2);
2233 }
2234 #endif
2235 
2236 void hfp_prepare_for_sco(hfp_connection_t * hfp_connection){
2237     UNUSED(hfp_connection);
2238 #ifdef ENABLE_CC256X_ASSISTED_HFP
2239     hfp_connection->cc256x_send_write_codec_config = true;
2240     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
2241         hfp_connection->cc256x_send_wbs_associate = true;
2242     }
2243 #endif
2244 
2245 #ifdef ENABLE_BCM_PCM_WBS
2246 #ifndef HAVE_BCM_PCM_NBS_16KHZ
2247     hfp_connection->bcm_send_write_i2spcm_interface_param = true;
2248 #endif
2249     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
2250         hfp_connection->bcm_send_enable_wbs = true;
2251     }
2252 #endif
2253 
2254 #ifdef ENABLE_RTK_PCM_WBS
2255     hfp_connection->rtk_send_sco_config = true;
2256 #endif
2257 }
2258 
2259 void hfp_set_hf_callback(btstack_packet_handler_t callback){
2260     hfp_hf_callback = callback;
2261 }
2262 
2263 void hfp_set_ag_callback(btstack_packet_handler_t callback){
2264     hfp_ag_callback = callback;
2265 }
2266 
2267 void hfp_set_ag_rfcomm_packet_handler(btstack_packet_handler_t handler){
2268     hfp_ag_rfcomm_packet_handler = handler;
2269 }
2270 
2271 void hfp_set_hf_rfcomm_packet_handler(btstack_packet_handler_t handler){
2272     hfp_hf_rfcomm_packet_handler = handler;
2273 }
2274 
2275 void hfp_set_hf_indicators(uint8_t indicators_nr, const uint8_t * indicators) {
2276     hfp_hf_indicators_nr = indicators_nr;
2277     hfp_hf_indicators = indicators;
2278 }
2279 
2280 void hfp_init(void){
2281     hfp_allowed_sco_packet_types = SCO_PACKET_TYPES_ALL;
2282 }
2283 
2284 void hfp_deinit(void){
2285     hfp_allowed_sco_packet_types = 0;
2286     hfp_connections = NULL;
2287     hfp_hf_callback = NULL;
2288     hfp_ag_callback = NULL;
2289     hfp_hf_rfcomm_packet_handler = NULL;
2290     hfp_ag_rfcomm_packet_handler = NULL;
2291     hfp_sco_establishment_active = NULL;
2292     hfp_custom_commands_ag = NULL;
2293     hfp_custom_commands_hf = NULL;
2294     (void) memset(&hfp_sdp_query_context, 0, sizeof(hfp_sdp_query_context_t));
2295     (void) memset(&hfp_sdp_query_request, 0, sizeof(btstack_context_callback_registration_t));
2296 }
2297 
2298 void hfp_set_sco_packet_types(uint16_t packet_types){
2299     hfp_allowed_sco_packet_types = packet_types;
2300 }
2301 
2302 uint16_t hfp_get_sco_packet_types(void){
2303     return hfp_allowed_sco_packet_types;
2304 }
2305 
2306 hfp_link_settings_t hfp_next_link_setting(hfp_link_settings_t current_setting, uint16_t local_sco_packet_types,
2307                                           uint16_t remote_sco_packet_types, bool eSCO_S4_supported,
2308                                           uint8_t negotiated_codec) {
2309     int8_t setting = (int8_t) current_setting;
2310     while (setting > 0){
2311         setting--;
2312         // skip S4 if not supported
2313         if ((setting == (int8_t) HFP_LINK_SETTINGS_S4) && !eSCO_S4_supported) continue;
2314         // skip wrong codec
2315         if ((hfp_link_settings[setting].codec_mask & (1 << negotiated_codec)) == 0) continue;
2316         // skip disabled or not supported packet types
2317         uint16_t required_packet_types = hfp_link_settings[setting].packet_types;
2318         uint16_t allowed_packet_types  = hfp_allowed_sco_packet_types & local_sco_packet_types & remote_sco_packet_types;
2319         if ((required_packet_types & allowed_packet_types) == 0) continue;
2320 
2321         // found matching setting
2322         return (hfp_link_settings_t) setting;
2323     }
2324     return HFP_LINK_SETTINGS_NONE;
2325 }
2326 
2327 static hfp_link_settings_t hfp_next_link_setting_for_connection(hfp_link_settings_t current_setting, hfp_connection_t * hfp_connection, uint8_t eSCO_S4_supported){
2328     uint8_t negotiated_codec   = hfp_connection->negotiated_codec;
2329     uint16_t local_sco_packet_types = hci_usable_sco_packet_types();
2330     uint16_t remote_sco_packet_types = hci_remote_sco_packet_types(hfp_connection->acl_handle);
2331     return hfp_next_link_setting(current_setting, local_sco_packet_types, remote_sco_packet_types, eSCO_S4_supported,
2332                                  negotiated_codec);
2333 }
2334 
2335 void hfp_init_link_settings(hfp_connection_t * hfp_connection, uint8_t eSCO_S4_supported){
2336     // get highest possible link setting
2337     hfp_connection->link_setting = hfp_next_link_setting_for_connection(HFP_LINK_SETTINGS_NONE, hfp_connection, eSCO_S4_supported);
2338     log_info("hfp_init_link_settings: %u", hfp_connection->link_setting);
2339 }
2340 
2341 #define HFP_HF_RX_DEBUG_PRINT_LINE 80
2342 
2343 void hfp_log_rfcomm_message(const char * tag, uint8_t * packet, uint16_t size){
2344 #ifdef ENABLE_LOG_INFO
2345     // encode \n\r
2346     char printable[HFP_HF_RX_DEBUG_PRINT_LINE+2];
2347     int i = 0;
2348     int pos;
2349     for (pos=0 ; (pos < size) && (i < (HFP_HF_RX_DEBUG_PRINT_LINE - 3)) ; pos++){
2350         switch (packet[pos]){
2351             case '\n':
2352                 printable[i++] = '\\';
2353                 printable[i++] = 'n';
2354                 break;
2355             case '\r':
2356                 printable[i++] = '\\';
2357                 printable[i++] = 'r';
2358                 break;
2359             default:
2360                 printable[i++] = packet[pos];
2361                 break;
2362         }
2363     }
2364     printable[i] = 0;
2365     log_info("%s: '%s'", tag, printable);
2366 #endif
2367 }
2368 
2369 void hfp_register_custom_ag_command(hfp_custom_at_command_t * custom_at_command){
2370     btstack_linked_list_add(&hfp_custom_commands_ag, (btstack_linked_item_t *) custom_at_command);
2371 }
2372 
2373 void hfp_register_custom_hf_command(hfp_custom_at_command_t * custom_at_command){
2374     btstack_linked_list_add(&hfp_custom_commands_hf, (btstack_linked_item_t *) custom_at_command);
2375 }
2376 
2377 // HFP H2 Synchronization - might get moved into a hfp_h2.c
2378 
2379 // find position of h2 sync header, returns -1 if not found, or h2 sync position
2380 static int16_t hfp_h2_sync_find(const uint8_t * frame_data, uint16_t frame_len){
2381     uint16_t i;
2382     for (i=0;i<(frame_len - 1);i++){
2383         // check: first byte == 1
2384         uint8_t h2_first_byte = frame_data[i];
2385         if (h2_first_byte == 0x01) {
2386             uint8_t h2_second_byte = frame_data[i + 1];
2387             // check lower nibble of second byte == 0x08
2388             if ((h2_second_byte & 0x0F) == 8) {
2389                 // check if bits 0+2 == bits 1+3
2390                 uint8_t hn = h2_second_byte >> 4;
2391                 if (((hn >> 1) & 0x05) == (hn & 0x05)) {
2392                     return (int16_t) i;
2393                 }
2394             }
2395         }
2396     }
2397     return -1;
2398 }
2399 
2400 static void hfp_h2_sync_reset(hfp_h2_sync_t * hfp_h2_sync){
2401     hfp_h2_sync->frame_len = 0;
2402 }
2403 
2404 void hfp_h2_sync_init(hfp_h2_sync_t * hfp_h2_sync,
2405                       bool (*callback)(bool bad_frame, const uint8_t * frame_data, uint16_t frame_len)){
2406     hfp_h2_sync->callback = callback;
2407     hfp_h2_sync->dropped_bytes = 0;
2408     hfp_h2_sync_reset(hfp_h2_sync);
2409 }
2410 
2411 static void hfp_h2_report_bad_frames(hfp_h2_sync_t *hfp_h2_sync){
2412     // report bad frames
2413     while (hfp_h2_sync->dropped_bytes >= HFP_H2_SYNC_FRAME_SIZE){
2414         hfp_h2_sync->dropped_bytes -= HFP_H2_SYNC_FRAME_SIZE;
2415         (void)(*hfp_h2_sync->callback)(true,NULL, HFP_H2_SYNC_FRAME_SIZE);
2416     }
2417 }
2418 
2419 static void hfp_h2_sync_drop_bytes(hfp_h2_sync_t * hfp_h2_sync, uint16_t bytes_to_drop){
2420     btstack_assert(bytes_to_drop <= hfp_h2_sync->frame_len);
2421     memmove(hfp_h2_sync->frame_data, &hfp_h2_sync->frame_data[bytes_to_drop], hfp_h2_sync->frame_len - bytes_to_drop);
2422     hfp_h2_sync->dropped_bytes += bytes_to_drop;
2423     hfp_h2_sync->frame_len     -= bytes_to_drop;
2424     hfp_h2_report_bad_frames(hfp_h2_sync);
2425 }
2426 
2427 void hfp_h2_sync_process(hfp_h2_sync_t *hfp_h2_sync, bool bad_frame, const uint8_t *frame_data, uint16_t frame_len) {
2428 
2429     if (bad_frame){
2430         // drop all data
2431         hfp_h2_sync->dropped_bytes += hfp_h2_sync->frame_len;
2432         hfp_h2_sync->frame_len = 0;
2433         // all new data is bad, too
2434         hfp_h2_sync->dropped_bytes += frame_len;
2435         // report frames
2436         hfp_h2_report_bad_frames(hfp_h2_sync);
2437         return;
2438     }
2439 
2440     while (frame_len > 0){
2441         // Fill hfp_h2_sync->frame_buffer
2442         uint16_t bytes_free_in_frame_buffer = HFP_H2_SYNC_FRAME_SIZE - hfp_h2_sync->frame_len;
2443         uint16_t bytes_to_append = btstack_min(frame_len, bytes_free_in_frame_buffer);
2444         memcpy(&hfp_h2_sync->frame_data[hfp_h2_sync->frame_len], frame_data, bytes_to_append);
2445         frame_data             += bytes_to_append;
2446         frame_len              -= bytes_to_append;
2447         hfp_h2_sync->frame_len += bytes_to_append;
2448         // check complete frame for h2 sync
2449         if (hfp_h2_sync->frame_len == HFP_H2_SYNC_FRAME_SIZE){
2450             bool valid_frame = true;
2451             int16_t h2_pos = hfp_h2_sync_find(hfp_h2_sync->frame_data, hfp_h2_sync->frame_len);
2452             if (h2_pos < 0){
2453                 // no h2 sync, no valid frame, keep last byte if it is 0x01
2454                 if (hfp_h2_sync->frame_data[HFP_H2_SYNC_FRAME_SIZE-1] == 0x01){
2455                     hfp_h2_sync_drop_bytes(hfp_h2_sync, HFP_H2_SYNC_FRAME_SIZE - 1);
2456                 } else {
2457                     hfp_h2_sync_drop_bytes(hfp_h2_sync, HFP_H2_SYNC_FRAME_SIZE);
2458                 }
2459                 valid_frame = false;
2460             }
2461             else if (h2_pos > 0){
2462                 // drop data before h2 sync
2463                 hfp_h2_sync_drop_bytes(hfp_h2_sync, h2_pos);
2464                 valid_frame = false;
2465             }
2466             if (valid_frame) {
2467                 // h2 sync at pos 0 and complete frame
2468                 bool codec_ok = (*hfp_h2_sync->callback)(false, hfp_h2_sync->frame_data, hfp_h2_sync->frame_len);
2469                 if (codec_ok){
2470                     hfp_h2_sync_reset(hfp_h2_sync);
2471                 } else {
2472                     // drop first two bytes
2473                     hfp_h2_sync_drop_bytes(hfp_h2_sync, 2);
2474                 }
2475             }
2476         }
2477     }
2478 }
2479