xref: /btstack/src/classic/hfp.c (revision ea7d62f812308726f4f4a18a3ea7fc380283c85a)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define BTSTACK_FILE__ "hfp.c"
39 
40 
41 #include "btstack_config.h"
42 
43 #include <stdint.h>
44 #include <string.h>
45 #include <inttypes.h>
46 
47 #include "bluetooth_sdp.h"
48 #include "btstack_debug.h"
49 #include "btstack_event.h"
50 #include "btstack_memory.h"
51 #include "btstack_run_loop.h"
52 #include "classic/sdp_client_rfcomm.h"
53 #include "classic/sdp_server.h"
54 #include "classic/sdp_util.h"
55 #include "classic/sdp_client.h"
56 #include "hci.h"
57 #include "hci_cmd.h"
58 #include "hci_dump.h"
59 
60 #if defined(ENABLE_CC256X_ASSISTED_HFP) && !defined(ENABLE_SCO_OVER_PCM)
61 #error "Assisted HFP is only possible over PCM/I2S. Please add define: ENABLE_SCO_OVER_PCM"
62 #endif
63 
64 #if defined(ENABLE_BCM_PCM_WBS) && !defined(ENABLE_SCO_OVER_PCM)
65 #error "WBS for PCM is only possible over PCM/I2S. Please add define: ENABLE_SCO_OVER_PCM"
66 #endif
67 
68 #define HFP_HF_FEATURES_SIZE 10
69 #define HFP_AG_FEATURES_SIZE 12
70 
71 typedef struct {
72     hfp_role_t local_role;
73     bd_addr_t  remote_address;
74 } hfp_sdp_query_context_t;
75 
76 // globals
77 
78 static hfp_sdp_query_context_t sdp_query_context;
79 static btstack_context_callback_registration_t hfp_handle_sdp_client_query_request;
80 
81 static btstack_linked_list_t hfp_connections ;
82 
83 static btstack_packet_handler_t hfp_hf_callback;
84 static btstack_packet_handler_t hfp_ag_callback;
85 
86 static btstack_packet_handler_t hfp_hf_rfcomm_packet_handler;
87 static btstack_packet_handler_t hfp_ag_rfcomm_packet_handler;
88 
89 static hfp_connection_t * sco_establishment_active;
90 
91 static uint16_t hfp_allowed_sco_packet_types;
92 
93 // prototypes
94 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);
95 static void parse_sequence(hfp_connection_t * context);
96 
97 
98 static const char * hfp_hf_features[] = {
99         "EC and/or NR function",
100         "Three-way calling",
101         "CLI presentation capability",
102         "Voice recognition activation",
103         "Remote volume control",
104 
105         "Enhanced call status",
106         "Enhanced call control",
107 
108         "Codec negotiation",
109 
110         "HF Indicators",
111         "eSCO S4 (and T2) Settings Supported",
112         "Reserved for future definition"
113 };
114 
115 static const char * hfp_ag_features[] = {
116         "Three-way calling",
117         "EC and/or NR function",
118         "Voice recognition function",
119         "In-band ring tone capability",
120         "Attach a number to a voice tag",
121         "Ability to reject a call",
122         "Enhanced call status",
123         "Enhanced call control",
124         "Extended Error Result Codes",
125         "Codec negotiation",
126         "HF Indicators",
127         "eSCO S4 (and T2) Settings Supported",
128         "Reserved for future definition"
129 };
130 
131 static const char * hfp_enhanced_call_dir[] = {
132         "outgoing",
133         "incoming"
134 };
135 
136 static const char * hfp_enhanced_call_status[] = {
137         "active",
138         "held",
139         "outgoing dialing",
140         "outgoing alerting",
141         "incoming",
142         "incoming waiting",
143         "call held by response and hold"
144 };
145 
146 static const char * hfp_enhanced_call_mode[] = {
147         "voice",
148         "data",
149         "fax"
150 };
151 
152 static const char * hfp_enhanced_call_mpty[] = {
153         "not a conference call",
154         "conference call"
155 };
156 
157 const char * hfp_enhanced_call_dir2str(uint16_t index){
158     if (index <= HFP_ENHANCED_CALL_DIR_INCOMING) return hfp_enhanced_call_dir[index];
159     return "not defined";
160 }
161 
162 const char * hfp_enhanced_call_status2str(uint16_t index){
163     if (index <= HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD) return hfp_enhanced_call_status[index];
164     return "not defined";
165 }
166 
167 const char * hfp_enhanced_call_mode2str(uint16_t index){
168     if (index <= HFP_ENHANCED_CALL_MODE_FAX) return hfp_enhanced_call_mode[index];
169     return "not defined";
170 }
171 
172 const char * hfp_enhanced_call_mpty2str(uint16_t index){
173     if (index <= HFP_ENHANCED_CALL_MPTY_CONFERENCE_CALL) return hfp_enhanced_call_mpty[index];
174     return "not defined";
175 }
176 
177 static uint16_t hfp_parse_indicator_index(hfp_connection_t * hfp_connection){
178     uint16_t index = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
179 
180     if (index > HFP_MAX_NUM_INDICATORS){
181         log_info("ignoring invalid indicator index bigger then HFP_MAX_NUM_INDICATORS");
182         return HFP_MAX_NUM_INDICATORS - 1;
183     }
184 
185     // indicator index enumeration starts with 1, we substract 1 to store in array with starting index 0
186     if (index > 0){
187         index -= 1;
188     } else {
189         log_info("ignoring invalid indicator index 0");
190         return 0;
191     }
192     return index;
193 }
194 
195 static void hfp_next_indicators_index(hfp_connection_t * hfp_connection){
196     if (hfp_connection->parser_item_index < HFP_MAX_NUM_INDICATORS - 1){
197         hfp_connection->parser_item_index++;
198     } else {
199         log_info("Ignoring additional indicator");
200     }
201 }
202 
203 static void hfp_next_codec_index(hfp_connection_t * hfp_connection){
204     if (hfp_connection->parser_item_index < HFP_MAX_NUM_CODECS - 1){
205         hfp_connection->parser_item_index++;
206     } else {
207         log_info("Ignoring additional codec index");
208     }
209 }
210 
211 static void hfp_next_remote_call_services_index(hfp_connection_t * hfp_connection){
212     if (hfp_connection->remote_call_services_index < HFP_MAX_NUM_CALL_SERVICES - 1){
213         hfp_connection->remote_call_services_index++;
214     } else {
215         log_info("Ignoring additional remote_call_services");
216     }
217 }
218 
219 const char * hfp_hf_feature(int index){
220     if (index > HFP_HF_FEATURES_SIZE){
221         return hfp_hf_features[HFP_HF_FEATURES_SIZE];
222     }
223     return hfp_hf_features[index];
224 }
225 
226 const char * hfp_ag_feature(int index){
227     if (index > HFP_AG_FEATURES_SIZE){
228         return hfp_ag_features[HFP_AG_FEATURES_SIZE];
229     }
230     return hfp_ag_features[index];
231 }
232 
233 int send_str_over_rfcomm(uint16_t cid, char * command){
234     if (!rfcomm_can_send_packet_now(cid)) return 1;
235     log_info("HFP_TX %s", command);
236     int err = rfcomm_send(cid, (uint8_t*) command, strlen(command));
237     if (err){
238         log_error("rfcomm_send -> error 0x%02x \n", err);
239     }
240 #ifdef ENABLE_HFP_AT_MESSAGES
241     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(cid);
242     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_SENT, command);
243 #endif
244     return 1;
245 }
246 
247 int hfp_supports_codec(uint8_t codec, int codecs_nr, uint8_t * codecs){
248 
249     // mSBC requires support for eSCO connections
250     if ((codec == HFP_CODEC_MSBC) && !hci_extended_sco_link_supported()) return 0;
251 
252     int i;
253     for (i = 0; i < codecs_nr; i++){
254         if (codecs[i] != codec) continue;
255         return 1;
256     }
257     return 0;
258 }
259 
260 void hfp_hf_drop_mSBC_if_eSCO_not_supported(uint8_t * codecs, uint8_t * codecs_nr){
261     if (hci_extended_sco_link_supported()) return;
262     uint8_t tmp_codecs[HFP_MAX_NUM_CODECS];
263     int i;
264     int tmp_codec_nr = 0;
265     for (i=0; i < *codecs_nr; i++){
266         if (codecs[i] == HFP_CODEC_MSBC) continue;
267         tmp_codecs[tmp_codec_nr++] = codecs[i];
268     }
269     *codecs_nr = tmp_codec_nr;
270     (void)memcpy(codecs, tmp_codecs, tmp_codec_nr);
271 }
272 
273 // UTILS
274 int get_bit(uint16_t bitmap, int position){
275     return (bitmap >> position) & 1;
276 }
277 
278 int store_bit(uint32_t bitmap, int position, uint8_t value){
279     if (value){
280         bitmap |= 1 << position;
281     } else {
282         bitmap &= ~ (1 << position);
283     }
284     return bitmap;
285 }
286 
287 int join(char * buffer, int buffer_size, uint8_t * values, int values_nr){
288     if (buffer_size < (values_nr * 3)) return 0;
289     int i;
290     int offset = 0;
291     for (i = 0; i < (values_nr-1); i++) {
292       offset += snprintf(buffer+offset, buffer_size-offset, "%d,", values[i]); // puts string into buffer
293     }
294     if (i<values_nr){
295         offset += snprintf(buffer+offset, buffer_size-offset, "%d", values[i]);
296     }
297     return offset;
298 }
299 
300 int join_bitmap(char * buffer, int buffer_size, uint32_t values, int values_nr){
301     if (buffer_size < (values_nr * 3)) return 0;
302 
303     int i;
304     int offset = 0;
305     for (i = 0; i < (values_nr-1); i++) {
306       offset += snprintf(buffer+offset, buffer_size-offset, "%d,", get_bit(values,i)); // puts string into buffer
307     }
308 
309     if (i<values_nr){
310         offset += snprintf(buffer+offset, buffer_size-offset, "%d", get_bit(values,i));
311     }
312     return offset;
313 }
314 
315 static void hfp_emit_event_for_context(hfp_connection_t * hfp_connection, uint8_t * packet, uint16_t size){
316     if (!hfp_connection) return;
317     btstack_packet_handler_t callback = NULL;
318     switch (hfp_connection->local_role){
319         case HFP_ROLE_HF:
320             callback = hfp_hf_callback;
321             break;
322         case HFP_ROLE_AG:
323             callback = hfp_ag_callback;
324             break;
325         default:
326             return;
327     }
328     (*callback)(HCI_EVENT_PACKET, 0, packet, size);
329 }
330 
331 void hfp_emit_simple_event(hfp_connection_t * hfp_connection, uint8_t event_subtype){
332     hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID;
333     uint8_t event[5];
334     event[0] = HCI_EVENT_HFP_META;
335     event[1] = sizeof(event) - 2;
336     event[2] = event_subtype;
337     little_endian_store_16(event, 3, acl_handle);
338     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
339 }
340 
341 void hfp_emit_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, uint8_t value){
342     hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID;
343     uint8_t event[6];
344     event[0] = HCI_EVENT_HFP_META;
345     event[1] = sizeof(event) - 2;
346     event[2] = event_subtype;
347     little_endian_store_16(event, 3, acl_handle);
348     event[5] = value; // status 0 == OK
349     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
350 }
351 
352 void hfp_emit_slc_connection_event(hfp_connection_t * hfp_connection, uint8_t status, hci_con_handle_t con_handle, bd_addr_t addr){
353     btstack_assert(hfp_connection != NULL);
354     uint8_t event[12];
355     int pos = 0;
356     event[pos++] = HCI_EVENT_HFP_META;
357     event[pos++] = sizeof(event) - 2;
358     event[pos++] = HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
359     event[pos++] = status; // status 0 == OK
360     little_endian_store_16(event, pos, con_handle);
361     pos += 2;
362     reverse_bd_addr(addr,&event[pos]);
363     pos += 6;
364     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
365 }
366 
367 static void hfp_emit_audio_connection_released(hfp_connection_t * hfp_connection, hci_con_handle_t sco_handle){
368     btstack_assert(hfp_connection != NULL);
369     uint8_t event[7];
370     int pos = 0;
371     event[pos++] = HCI_EVENT_HFP_META;
372     event[pos++] = sizeof(event) - 2;
373     event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED;
374     little_endian_store_16(event, pos, hfp_connection->acl_handle);
375     pos += 2;
376     little_endian_store_16(event, pos, sco_handle);
377     pos += 2;
378     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
379 }
380 
381 void hfp_emit_sco_event(hfp_connection_t * hfp_connection, uint8_t status, hci_con_handle_t sco_handle, bd_addr_t addr, uint8_t  negotiated_codec){
382     btstack_assert(hfp_connection != NULL);
383     uint8_t event[15];
384     int pos = 0;
385     event[pos++] = HCI_EVENT_HFP_META;
386     event[pos++] = sizeof(event) - 2;
387     event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED;
388     little_endian_store_16(event, pos, hfp_connection->acl_handle);
389     pos += 2;
390     event[pos++] = status; // status 0 == OK
391     little_endian_store_16(event, pos, sco_handle);
392     pos += 2;
393     reverse_bd_addr(addr,&event[pos]);
394     pos += 6;
395     event[pos++] = negotiated_codec;
396     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
397 }
398 
399 void hfp_emit_string_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, const char * value){
400     btstack_assert(hfp_connection != NULL);
401 #ifdef ENABLE_HFP_AT_MESSAGES
402     uint8_t event[256];
403 #else
404     uint8_t event[40];
405 #endif
406     event[0] = HCI_EVENT_HFP_META;
407     event[1] = sizeof(event) - 2;
408     event[2] = event_subtype;
409     little_endian_store_16(event, 3, hfp_connection->acl_handle);
410     uint16_t size = btstack_min(strlen(value), sizeof(event) - 6);
411     strncpy((char*)&event[5], value, size);
412     event[5 + size] = 0;
413     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
414 }
415 
416 btstack_linked_list_t * hfp_get_connections(void){
417     return (btstack_linked_list_t *) &hfp_connections;
418 }
419 
420 hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid){
421     btstack_linked_list_iterator_t it;
422     btstack_linked_list_iterator_init(&it, hfp_get_connections());
423     while (btstack_linked_list_iterator_has_next(&it)){
424         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
425         if (hfp_connection->rfcomm_cid == cid){
426             return hfp_connection;
427         }
428     }
429     return NULL;
430 }
431 
432 hfp_connection_t * get_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr, hfp_role_t hfp_role){
433     btstack_linked_list_iterator_t it;
434     btstack_linked_list_iterator_init(&it, hfp_get_connections());
435     while (btstack_linked_list_iterator_has_next(&it)){
436         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
437         if ((memcmp(hfp_connection->remote_addr, bd_addr, 6) == 0) && (hfp_connection->local_role == hfp_role)) {
438             return hfp_connection;
439         }
440     }
441     return NULL;
442 }
443 
444 hfp_connection_t * get_hfp_connection_context_for_sco_handle(uint16_t handle, hfp_role_t hfp_role){
445     btstack_linked_list_iterator_t it;
446     btstack_linked_list_iterator_init(&it, hfp_get_connections());
447     while (btstack_linked_list_iterator_has_next(&it)){
448         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
449         if ((hfp_connection->sco_handle == handle) && (hfp_connection->local_role == hfp_role)){
450             return hfp_connection;
451         }
452     }
453     return NULL;
454 }
455 
456 hfp_connection_t * get_hfp_connection_context_for_acl_handle(uint16_t handle, hfp_role_t hfp_role){
457     btstack_linked_list_iterator_t it;
458     btstack_linked_list_iterator_init(&it, hfp_get_connections());
459     while (btstack_linked_list_iterator_has_next(&it)){
460         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
461         if ((hfp_connection->acl_handle == handle) && (hfp_connection->local_role == hfp_role)){
462             return hfp_connection;
463         }
464     }
465     return NULL;
466 }
467 
468 void hfp_reset_context_flags(hfp_connection_t * hfp_connection){
469     if (!hfp_connection) return;
470     hfp_connection->ok_pending = 0;
471     hfp_connection->send_error = 0;
472 
473     hfp_connection->found_equal_sign = false;
474 
475     hfp_connection->change_status_update_for_individual_ag_indicators = 0;
476     hfp_connection->operator_name_changed = 0;
477 
478     hfp_connection->enable_extended_audio_gateway_error_report = 0;
479     hfp_connection->extended_audio_gateway_error = 0;
480 
481     // establish codecs hfp_connection
482     hfp_connection->suggested_codec = 0;
483     hfp_connection->negotiated_codec = 0;
484     hfp_connection->codec_confirmed = 0;
485 
486     hfp_connection->establish_audio_connection = 0;
487     hfp_connection->call_waiting_notification_enabled = 0;
488     hfp_connection->command = HFP_CMD_NONE;
489     hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
490 }
491 
492 static hfp_connection_t * create_hfp_connection_context(void){
493     hfp_connection_t * hfp_connection = btstack_memory_hfp_connection_get();
494     if (!hfp_connection) return NULL;
495 
496     hfp_connection->state = HFP_IDLE;
497     hfp_connection->call_state = HFP_CALL_IDLE;
498     hfp_connection->codecs_state = HFP_CODECS_IDLE;
499 
500     hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
501     hfp_connection->command = HFP_CMD_NONE;
502 
503     hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID;
504     hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID;
505 
506     hfp_reset_context_flags(hfp_connection);
507 
508     btstack_linked_list_add(&hfp_connections, (btstack_linked_item_t*)hfp_connection);
509     return hfp_connection;
510 }
511 
512 void hfp_finalize_connection_context(hfp_connection_t * hfp_connection){
513     btstack_run_loop_remove_timer(&hfp_connection->hfp_timeout);
514     btstack_linked_list_remove(&hfp_connections, (btstack_linked_item_t*) hfp_connection);
515     btstack_memory_hfp_connection_free(hfp_connection);
516 }
517 
518 static hfp_connection_t * provide_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr, hfp_role_t local_role){
519     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr, local_role);
520     if (hfp_connection) return  hfp_connection;
521     hfp_connection = create_hfp_connection_context();
522     (void)memcpy(hfp_connection->remote_addr, bd_addr, 6);
523     hfp_connection->local_role = local_role;
524     log_info("Create HFP context %p: role %u, addr %s", hfp_connection, local_role, bd_addr_to_str(bd_addr));
525 
526     return hfp_connection;
527 }
528 
529 /* @param network.
530  * 0 == no ability to reject a call.
531  * 1 == ability to reject a call.
532  */
533 
534 /* @param suported_features
535  * HF bit 0: EC and/or NR function (yes/no, 1 = yes, 0 = no)
536  * HF bit 1: Call waiting or three-way calling(yes/no, 1 = yes, 0 = no)
537  * HF bit 2: CLI presentation capability (yes/no, 1 = yes, 0 = no)
538  * HF bit 3: Voice recognition activation (yes/no, 1= yes, 0 = no)
539  * HF bit 4: Remote volume control (yes/no, 1 = yes, 0 = no)
540  * HF bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
541  */
542  /* Bit position:
543  * AG bit 0: Three-way calling (yes/no, 1 = yes, 0 = no)
544  * AG bit 1: EC and/or NR function (yes/no, 1 = yes, 0 = no)
545  * AG bit 2: Voice recognition function (yes/no, 1 = yes, 0 = no)
546  * AG bit 3: In-band ring tone capability (yes/no, 1 = yes, 0 = no)
547  * AG bit 4: Attach a phone number to a voice tag (yes/no, 1 = yes, 0 = no)
548  * AG bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
549  */
550 
551 void hfp_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint16_t service_uuid, int rfcomm_channel_nr, const char * name){
552     uint8_t* attribute;
553     de_create_sequence(service);
554 
555     // 0x0000 "Service Record Handle"
556     de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE);
557     de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle);
558 
559     // 0x0001 "Service Class ID List"
560     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST);
561     attribute = de_push_sequence(service);
562     {
563         //  "UUID for Service"
564         de_add_number(attribute, DE_UUID, DE_SIZE_16, service_uuid);
565         de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_GENERIC_AUDIO);
566     }
567     de_pop_sequence(service, attribute);
568 
569     // 0x0004 "Protocol Descriptor List"
570     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST);
571     attribute = de_push_sequence(service);
572     {
573         uint8_t* l2cpProtocol = de_push_sequence(attribute);
574         {
575             de_add_number(l2cpProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP);
576         }
577         de_pop_sequence(attribute, l2cpProtocol);
578 
579         uint8_t* rfcomm = de_push_sequence(attribute);
580         {
581             de_add_number(rfcomm,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_RFCOMM);  // rfcomm_service
582             de_add_number(rfcomm,  DE_UINT, DE_SIZE_8,  rfcomm_channel_nr);  // rfcomm channel
583         }
584         de_pop_sequence(attribute, rfcomm);
585     }
586     de_pop_sequence(service, attribute);
587 
588 
589     // 0x0005 "Public Browse Group"
590     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group
591     attribute = de_push_sequence(service);
592     {
593         de_add_number(attribute,  DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT);
594     }
595     de_pop_sequence(service, attribute);
596 
597     // 0x0009 "Bluetooth Profile Descriptor List"
598     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST);
599     attribute = de_push_sequence(service);
600     {
601         uint8_t *sppProfile = de_push_sequence(attribute);
602         {
603             de_add_number(sppProfile,  DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_HANDSFREE);
604             de_add_number(sppProfile,  DE_UINT, DE_SIZE_16, 0x0108); // Verision 1.8
605         }
606         de_pop_sequence(attribute, sppProfile);
607     }
608     de_pop_sequence(service, attribute);
609 
610     // 0x0100 "Service Name"
611     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0100);
612     de_add_data(service,  DE_STRING, strlen(name), (uint8_t *) name);
613 }
614 
615 static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
616     UNUSED(packet_type);    // ok: handling own sdp events
617     UNUSED(channel);        // ok: no channel
618     UNUSED(size);           // ok: handling own sdp events
619 
620     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(sdp_query_context.remote_address, sdp_query_context.local_role);
621     if (hfp_connection == NULL) {
622         log_info("connection with %s and local role %d not found", sdp_query_context.remote_address, sdp_query_context.local_role);
623         return;
624     }
625 
626     switch (hci_event_packet_get_type(packet)){
627         case SDP_EVENT_QUERY_RFCOMM_SERVICE:
628             hfp_connection->rfcomm_channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet);
629             break;
630         case SDP_EVENT_QUERY_COMPLETE:
631             if (hfp_connection->rfcomm_channel_nr > 0){
632                 hfp_connection->state = HFP_W4_RFCOMM_CONNECTED;
633                 btstack_packet_handler_t packet_handler;
634                 switch (hfp_connection->local_role){
635                     case HFP_ROLE_AG:
636                         packet_handler = hfp_ag_rfcomm_packet_handler;
637                         break;
638                     case HFP_ROLE_HF:
639                         packet_handler = hfp_hf_rfcomm_packet_handler;
640                         break;
641                     default:
642                         btstack_assert(false);
643                         return;
644                 }
645 
646                 rfcomm_create_channel(packet_handler, hfp_connection->remote_addr, hfp_connection->rfcomm_channel_nr, NULL);
647 
648             } else {
649                 hfp_connection->state = HFP_IDLE;
650                 uint8_t status = sdp_event_query_complete_get_status(packet);
651                 if (status == ERROR_CODE_SUCCESS){
652                     // report service not found
653                     status = SDP_SERVICE_NOT_FOUND;
654                 }
655                 hfp_emit_slc_connection_event(hfp_connection, status, HCI_CON_HANDLE_INVALID, hfp_connection->remote_addr);
656                 log_info("rfcomm service not found, status 0x%02x", status);
657             }
658 
659             // register the SDP Query request to check if there is another connection waiting for the query
660             // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback
661             (void) sdp_client_register_query_callback(&hfp_handle_sdp_client_query_request);
662             break;
663         default:
664             break;
665     }
666 }
667 
668 // returns 0 if unexpected error or no other link options remained, otherwise 1
669 static int hfp_handle_failed_sco_connection(uint8_t status){
670 
671     if (!sco_establishment_active){
672         log_info("(e)SCO Connection failed but not started by us");
673         return 0;
674     }
675 
676     log_info("(e)SCO Connection failed 0x%02x", status);
677     switch (status){
678         case ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE:
679         case ERROR_CODE_UNSPECIFIED_ERROR:
680         case ERROR_CODE_CONNECTION_REJECTED_DUE_TO_LIMITED_RESOURCES:
681             break;
682         default:
683             return 0;
684     }
685 
686     // note: eSCO_S4 supported flag not available, but it's only relevant for highest CVSD link setting (and the current failed)
687     hfp_link_settings_t next_setting = hfp_next_link_setting_for_connection(sco_establishment_active->link_setting, sco_establishment_active, false);
688 
689     // handle no valid setting found
690     if (next_setting == HFP_LINK_SETTINGS_NONE) {
691         if (sco_establishment_active->negotiated_codec == HFP_CODEC_MSBC){
692             log_info("T2/T1 failed, fallback to CVSD - D1");
693             sco_establishment_active->negotiated_codec = HFP_CODEC_CVSD;
694             sco_establishment_active->sco_for_msbc_failed = 1;
695             sco_establishment_active->command = HFP_CMD_AG_SEND_COMMON_CODEC;
696             sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D1;
697         } else {
698             // no other options
699             return 0;
700         }
701     }
702 
703     log_info("e)SCO Connection: try new link_setting %d", next_setting);
704     sco_establishment_active->establish_audio_connection = 1;
705     sco_establishment_active->link_setting = next_setting;
706     sco_establishment_active = NULL;
707     return 1;
708 }
709 
710 
711 void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){
712     UNUSED(packet_type);
713     UNUSED(channel);    // ok: no channel
714     UNUSED(size);
715 
716     bd_addr_t event_addr;
717     hci_con_handle_t handle;
718     hfp_connection_t * hfp_connection = NULL;
719     uint8_t status;
720 
721     log_debug("HFP HCI event handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size);
722 
723     switch (hci_event_packet_get_type(packet)) {
724 
725         case HCI_EVENT_CONNECTION_REQUEST:
726             switch(hci_event_connection_request_get_link_type(packet)){
727                 case 0: //  SCO
728                 case 2: // eSCO
729                     hci_event_connection_request_get_bd_addr(packet, event_addr);
730                     hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
731                     if (!hfp_connection) break;
732                     if (hci_event_connection_request_get_link_type(packet) == 2){
733                         hfp_connection->accept_sco = 2;
734                     } else {
735                         hfp_connection->accept_sco = 1;
736                     }
737 #ifdef ENABLE_CC256X_ASSISTED_HFP
738                     hfp_cc256x_prepare_for_sco(hfp_connection);
739 #endif
740 #ifdef ENABLE_BCM_PCM_WBS
741                     hfp_bcm_prepare_for_sco(hfp_connection);
742 #endif
743                     log_info("accept sco %u\n", hfp_connection->accept_sco);
744                     sco_establishment_active = hfp_connection;
745                     break;
746                 default:
747                     break;
748             }
749             break;
750 
751         case HCI_EVENT_COMMAND_STATUS:
752             if (hci_event_command_status_get_command_opcode(packet) == hci_setup_synchronous_connection.opcode) {
753                 if (sco_establishment_active == NULL) break;
754                 status = hci_event_command_status_get_status(packet);
755                 if (status == ERROR_CODE_SUCCESS) break;
756 
757                 hfp_connection = sco_establishment_active;
758                 if (hfp_handle_failed_sco_connection(status)) break;
759                 hfp_connection->establish_audio_connection = 0;
760                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
761                 hfp_emit_sco_event(hfp_connection, status, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
762             }
763             break;
764 
765         case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{
766             if (sco_establishment_active == NULL) break;
767             hci_event_synchronous_connection_complete_get_bd_addr(packet, event_addr);
768             hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
769             if (!hfp_connection) {
770                 log_error("HFP: connection does not exist for remote with addr %s.", bd_addr_to_str(event_addr));
771                 return;
772             }
773 
774             status = hci_event_synchronous_connection_complete_get_status(packet);
775             if (status != ERROR_CODE_SUCCESS){
776                 hfp_connection->accept_sco = 0;
777                 if (hfp_handle_failed_sco_connection(status)) break;
778 
779                 hfp_connection->establish_audio_connection = 0;
780                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
781                 hfp_emit_sco_event(hfp_connection, status, 0, event_addr, hfp_connection->negotiated_codec);
782                 break;
783             }
784 
785             uint16_t sco_handle = hci_event_synchronous_connection_complete_get_handle(packet);
786             uint8_t  link_type = hci_event_synchronous_connection_complete_get_link_type(packet);
787             uint8_t  transmission_interval = hci_event_synchronous_connection_complete_get_transmission_interval(packet);  // measured in slots
788             uint8_t  retransmission_interval = hci_event_synchronous_connection_complete_get_retransmission_interval(packet);// measured in slots
789             uint16_t rx_packet_length = hci_event_synchronous_connection_complete_get_rx_packet_length(packet); // measured in bytes
790             uint16_t tx_packet_length = hci_event_synchronous_connection_complete_get_tx_packet_length(packet); // measured in bytes
791 
792             switch (link_type){
793                 case 0x00:
794                     log_info("SCO Connection established.");
795                     if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval);
796                     if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval);
797                     if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length);
798                     if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length);
799                     break;
800                 case 0x02:
801                     log_info("eSCO Connection established. \n");
802                     break;
803                 default:
804                     log_error("(e)SCO reserved link_type 0x%2x", link_type);
805                     break;
806             }
807 
808             log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, "
809                  " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)\n", sco_handle,
810                  bd_addr_to_str(event_addr), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length,
811                  hci_event_synchronous_connection_complete_get_air_mode(packet));
812 
813             if (hfp_connection->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){
814                 log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN");
815                 hfp_connection->state = HFP_W2_DISCONNECT_SCO;
816                 break;
817             }
818             hfp_connection->sco_handle = sco_handle;
819             hfp_connection->establish_audio_connection = 0;
820             hfp_connection->state = HFP_AUDIO_CONNECTION_ESTABLISHED;
821             hfp_emit_sco_event(hfp_connection, status, sco_handle, event_addr, hfp_connection->negotiated_codec);
822             break;
823         }
824 
825         case HCI_EVENT_DISCONNECTION_COMPLETE:
826             handle = little_endian_read_16(packet,3);
827             hfp_connection = get_hfp_connection_context_for_sco_handle(handle, local_role);
828 
829             if (!hfp_connection) break;
830 
831 #ifdef ENABLE_CC256X_ASSISTED_HFP
832             hfp_connection->cc256x_send_wbs_disassociate = true;
833 #endif
834 #ifdef ENABLE_BCM_PCM_WBS
835             hfp_connection->bcm_send_disable_wbs = true;
836 #endif
837             hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID;
838             hfp_connection->release_audio_connection = 0;
839 
840             if (hfp_connection->state == HFP_W4_SCO_DISCONNECTED_TO_SHUTDOWN){
841                 // RFCOMM already closed -> remote power off
842 #if defined(ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
843                 hfp_connection->state = HFP_W4_WBS_SHUTDOWN;
844 #else
845                 hfp_finalize_connection_context(hfp_connection);
846 #endif
847                 break;
848             }
849 
850             hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
851             hfp_emit_audio_connection_released(hfp_connection, handle);
852 
853             if (hfp_connection->release_slc_connection){
854                 hfp_connection->release_slc_connection = 0;
855                 log_info("SCO disconnected, w2 disconnect RFCOMM\n");
856                 hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
857             }
858             break;
859 
860         default:
861             break;
862     }
863 }
864 
865 
866 void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){
867     UNUSED(packet_type);
868     UNUSED(channel);    // ok: no channel
869     UNUSED(size);
870 
871     bd_addr_t event_addr;
872     uint16_t rfcomm_cid;
873     hfp_connection_t * hfp_connection = NULL;
874     uint8_t status;
875 
876     log_debug("HFP packet_handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size);
877 
878     switch (hci_event_packet_get_type(packet)) {
879 
880         case RFCOMM_EVENT_INCOMING_CONNECTION:
881             // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
882             rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
883             hfp_connection = provide_hfp_connection_context_for_bd_addr(event_addr, local_role);
884             if (!hfp_connection){
885                 log_info("hfp: no memory to accept incoming connection - decline");
886                 rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet));
887                 return;
888             }
889             if (hfp_connection->state != HFP_IDLE) {
890                 log_error("hfp: incoming connection but not idle, reject");
891                 rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet));
892                 return;
893             }
894 
895             hfp_connection->rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
896             hfp_connection->state = HFP_W4_RFCOMM_CONNECTED;
897             rfcomm_accept_connection(hfp_connection->rfcomm_cid);
898             break;
899 
900         case RFCOMM_EVENT_CHANNEL_OPENED:
901             // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16)
902 
903             rfcomm_event_channel_opened_get_bd_addr(packet, event_addr);
904             status = rfcomm_event_channel_opened_get_status(packet);
905 
906             hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
907             if (!hfp_connection || (hfp_connection->state != HFP_W4_RFCOMM_CONNECTED)) return;
908 
909             if (status) {
910                 hfp_emit_slc_connection_event(hfp_connection, status, rfcomm_event_channel_opened_get_con_handle(packet), event_addr);
911                 hfp_finalize_connection_context(hfp_connection);
912             } else {
913                 hfp_connection->acl_handle = rfcomm_event_channel_opened_get_con_handle(packet);
914                 hfp_connection->rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
915                 bd_addr_copy(hfp_connection->remote_addr, event_addr);
916 
917                 switch (hfp_connection->state){
918                     case HFP_W4_RFCOMM_CONNECTED:
919                         hfp_connection->state = HFP_EXCHANGE_SUPPORTED_FEATURES;
920                         break;
921                     case HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN:
922                         hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
923                         break;
924                     default:
925                         break;
926                 }
927                 rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
928             }
929             break;
930 
931         case RFCOMM_EVENT_CHANNEL_CLOSED:
932             rfcomm_cid = little_endian_read_16(packet,2);
933             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid);
934             if (!hfp_connection) break;
935             if (hfp_connection->state == HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART){
936                 hfp_connection->state = HFP_IDLE;
937                 hfp_establish_service_level_connection(hfp_connection->remote_addr, hfp_connection->service_uuid, local_role);
938                 break;
939             }
940             if ( hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
941                 // service connection was released, this implicitly releases audio connection as well
942                 hfp_connection->release_audio_connection = 0;
943                 hci_con_handle_t sco_handle = hfp_connection->sco_handle;
944                 gap_disconnect(hfp_connection->sco_handle);
945                 hfp_connection->state = HFP_W4_SCO_DISCONNECTED_TO_SHUTDOWN;
946                 hfp_emit_audio_connection_released(hfp_connection, sco_handle);
947                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0);
948             } else {
949                 // regular case
950                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0);
951                 hfp_finalize_connection_context(hfp_connection);
952             }
953             break;
954 
955         default:
956             break;
957     }
958 }
959 // translates command string into hfp_command_t CMD
960 
961 typedef struct {
962     const char * command;
963     hfp_command_t command_id;
964 } hfp_command_entry_t;
965 
966 static hfp_command_entry_t hfp_ag_commmand_table[] = {
967     { "AT+BAC=",   HFP_CMD_AVAILABLE_CODECS },
968     { "AT+BCC",    HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP },
969     { "AT+BCS=",   HFP_CMD_HF_CONFIRMED_CODEC },
970     { "AT+BIA=",   HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE, }, // +BIA:<enabled>,,<enabled>,,,<enabled>
971     { "AT+BIEV=",  HFP_CMD_HF_INDICATOR_STATUS },
972     { "AT+BIND=",  HFP_CMD_LIST_GENERIC_STATUS_INDICATORS },
973     { "AT+BIND=?", HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS },
974     { "AT+BIND?",  HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE },
975     { "AT+BINP=",  HFP_CMD_HF_REQUEST_PHONE_NUMBER },
976     { "AT+BLDN",   HFP_CMD_REDIAL_LAST_NUMBER },
977     { "AT+BRSF=",  HFP_CMD_SUPPORTED_FEATURES },
978     { "AT+BTRH=",  HFP_CMD_RESPONSE_AND_HOLD_COMMAND },
979     { "AT+BTRH?",  HFP_CMD_RESPONSE_AND_HOLD_QUERY },
980     { "AT+BVRA=",  HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION },
981     { "AT+CCWA=",  HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION},
982     { "AT+CHLD=",  HFP_CMD_CALL_HOLD },
983     { "AT+CHLD=?", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES },
984     { "AT+CHUP",   HFP_CMD_HANG_UP_CALL },
985     { "AT+CIND=?", HFP_CMD_RETRIEVE_AG_INDICATORS },
986     { "AT+CIND?",  HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS },
987     { "AT+CLCC",   HFP_CMD_LIST_CURRENT_CALLS },
988     { "AT+CLIP=",  HFP_CMD_ENABLE_CLIP},
989     { "AT+CMEE=",  HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR},
990     { "AT+CMER=",  HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE },
991     { "AT+CNUM",   HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION },
992     { "AT+COPS=",  HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT },
993     { "AT+COPS?",  HFP_CMD_QUERY_OPERATOR_SELECTION_NAME },
994     { "AT+NREC=",  HFP_CMD_TURN_OFF_EC_AND_NR, },
995     { "AT+VGM=",   HFP_CMD_SET_MICROPHONE_GAIN },
996     { "AT+VGS=",   HFP_CMD_SET_SPEAKER_GAIN },
997     { "AT+VTS=",   HFP_CMD_TRANSMIT_DTMF_CODES },
998     { "ATA",       HFP_CMD_CALL_ANSWERED },
999 };
1000 
1001 static hfp_command_entry_t hfp_hf_commmand_table[] = {
1002     { "+BCS:",  HFP_CMD_AG_SUGGESTED_CODEC },
1003     { "+BIND:", HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS },
1004     { "+BINP",  HFP_CMD_AG_SENT_PHONE_NUMBER },
1005     { "+BRSF:", HFP_CMD_SUPPORTED_FEATURES },
1006     { "+BSIR:", HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING },
1007     { "+BTRH:", HFP_CMD_RESPONSE_AND_HOLD_STATUS },
1008     { "+BVRA:", HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION },
1009     { "+CCWA:", HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE, },
1010     { "+CHLD:", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES },
1011     { "+CIEV:", HFP_CMD_TRANSFER_AG_INDICATOR_STATUS},
1012     { "+CIND:", HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC },
1013     { "+CLCC:", HFP_CMD_LIST_CURRENT_CALLS },
1014     { "+CLIP:", HFP_CMD_AG_SENT_CLIP_INFORMATION },
1015     { "+CME ERROR:", HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR },
1016     { "+CNUM:", HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION},
1017     { "+COPS:", HFP_CMD_QUERY_OPERATOR_SELECTION_NAME },
1018     { "+VGM:",  HFP_CMD_SET_MICROPHONE_GAIN },
1019     { "+VGS:",  HFP_CMD_SET_SPEAKER_GAIN},
1020     { "ERROR",  HFP_CMD_ERROR},
1021     { "NOP",    HFP_CMD_NONE}, // dummy command used by unit tests
1022     { "OK",     HFP_CMD_OK },
1023     { "RING",   HFP_CMD_RING },
1024 };
1025 
1026 static hfp_command_t parse_command(const char * line_buffer, int isHandsFree){
1027 
1028     // table lookup based on role
1029     uint16_t num_entries;
1030     hfp_command_entry_t * table;
1031     if (isHandsFree == 0){
1032         table = hfp_ag_commmand_table;
1033         num_entries = sizeof(hfp_ag_commmand_table) / sizeof(hfp_command_entry_t);
1034     } else {
1035         table = hfp_hf_commmand_table;
1036         num_entries = sizeof(hfp_hf_commmand_table) / sizeof(hfp_command_entry_t);
1037     }
1038     // binary search
1039     uint16_t left = 0;
1040     uint16_t right = num_entries - 1;
1041     while (left <= right){
1042         uint16_t middle = left + (right - left) / 2;
1043         hfp_command_entry_t *entry = &table[middle];
1044         int match = strcmp(line_buffer, entry->command);
1045         if (match < 0){
1046             // search term is lower than middle element
1047             if (right == 0) break;
1048             right = middle - 1;
1049         } else if (match == 0){
1050             return entry->command_id;
1051         } else {
1052             // search term is higher than middle element
1053             left = middle + 1;
1054         }
1055     }
1056 
1057     // note: if parser in CMD_HEADER state would treats digits and maybe '+' as separator, match on "ATD" would work.
1058     // note: phone number is currently expected in line_buffer[3..]
1059     // prefix match on 'ATD', AG only
1060     if ((isHandsFree == 0) && (strncmp(line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0)){
1061         return HFP_CMD_CALL_PHONE_NUMBER;
1062     }
1063 
1064     // Valid looking, but unknown commands/responses
1065     if ((isHandsFree == 0) && (strncmp(line_buffer, "AT+", 3) == 0)){
1066         return HFP_CMD_UNKNOWN;
1067     }
1068 
1069     if ((isHandsFree != 0) && (strncmp(line_buffer, "+", 1) == 0)){
1070         return HFP_CMD_UNKNOWN;
1071     }
1072 
1073     return HFP_CMD_NONE;
1074 }
1075 
1076 static void hfp_parser_store_byte(hfp_connection_t * hfp_connection, uint8_t byte){
1077     if ((hfp_connection->line_size + 1 ) >= HFP_MAX_INDICATOR_DESC_SIZE) return;
1078     hfp_connection->line_buffer[hfp_connection->line_size++] = byte;
1079     hfp_connection->line_buffer[hfp_connection->line_size] = 0;
1080 }
1081 static int hfp_parser_is_buffer_empty(hfp_connection_t * hfp_connection){
1082     return hfp_connection->line_size == 0;
1083 }
1084 
1085 static int hfp_parser_is_end_of_line(uint8_t byte){
1086     return (byte == '\n') || (byte == '\r');
1087 }
1088 
1089 static void hfp_parser_reset_line_buffer(hfp_connection_t *hfp_connection) {
1090     hfp_connection->line_size = 0;
1091 }
1092 
1093 static void hfp_parser_store_if_token(hfp_connection_t * hfp_connection, uint8_t byte){
1094     switch (byte){
1095         case ',':
1096 		case '-':
1097         case ';':
1098         case '(':
1099         case ')':
1100         case '\n':
1101         case '\r':
1102             break;
1103         default:
1104             hfp_parser_store_byte(hfp_connection, byte);
1105             break;
1106     }
1107 }
1108 
1109 static bool hfp_parser_is_separator( uint8_t byte){
1110     switch (byte){
1111         case ',':
1112 		case '-':
1113         case ';':
1114         case '\n':
1115         case '\r':
1116             return true;
1117         default:
1118             return false;
1119     }
1120 }
1121 
1122 static bool hfp_parse_byte(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){
1123 
1124     // handle doubles quotes
1125     if (byte == '"'){
1126         hfp_connection->parser_quoted = !hfp_connection->parser_quoted;
1127         return true;
1128     }
1129     if (hfp_connection->parser_quoted) {
1130         hfp_parser_store_byte(hfp_connection, byte);
1131         return true;
1132     }
1133 
1134     // ignore spaces outside command or double quotes (required e.g. for '+CME ERROR:..") command
1135     if ((byte == ' ') && (hfp_connection->parser_state != HFP_PARSER_CMD_HEADER)) return true;
1136 
1137     bool processed = true;
1138 
1139     switch (hfp_connection->parser_state) {
1140         case HFP_PARSER_CMD_HEADER:
1141             switch (byte) {
1142                 case '\n':
1143                 case '\r':
1144                 case ';':
1145                     // ignore separator
1146                     break;
1147                 case ':':
1148                 case '?':
1149                     // store separator
1150                     hfp_parser_store_byte(hfp_connection, byte);
1151                     break;
1152                 case '=':
1153                     // equal sign: remember and wait for next char to decided between '=?' and '=\?'
1154                     hfp_connection->found_equal_sign = true;
1155                     hfp_parser_store_byte(hfp_connection, byte);
1156                     return true;
1157                 default:
1158                     // store if not lookahead
1159                     if (!hfp_connection->found_equal_sign) {
1160                         hfp_parser_store_byte(hfp_connection, byte);
1161                         return true;
1162                     }
1163                     // mark as lookahead
1164                     processed = false;
1165                     break;
1166             }
1167 
1168             // ignore empty tokens
1169             if (hfp_parser_is_buffer_empty(hfp_connection)) return true;
1170 
1171             // parse
1172             hfp_connection->command = parse_command((char *)hfp_connection->line_buffer, isHandsFree);
1173 
1174             // pick +CIND version based on connection state: descriptions during SLC vs. states later
1175             if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC){
1176                 switch(hfp_connection->state){
1177                     case HFP_W4_RETRIEVE_INDICATORS_STATUS:
1178                         hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS;
1179                         break;
1180                     case HFP_W4_RETRIEVE_INDICATORS:
1181                         hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS;
1182                         break;
1183                     default:
1184                         hfp_connection->command = HFP_CMD_UNKNOWN;
1185                         break;
1186                 }
1187             }
1188 
1189             log_info("command string '%s', handsfree %u -> cmd id %u", (char *)hfp_connection->line_buffer, isHandsFree, hfp_connection->command);
1190 
1191             // next state
1192             hfp_connection->found_equal_sign = false;
1193             hfp_parser_reset_line_buffer(hfp_connection);
1194             hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
1195 
1196             return processed;
1197 
1198         case HFP_PARSER_CMD_SEQUENCE:
1199             // handle empty fields
1200             if ((byte == ',' ) && (hfp_connection->line_size == 0)){
1201                 hfp_connection->line_buffer[0] = 0;
1202                 hfp_connection->ignore_value = 1;
1203                 parse_sequence(hfp_connection);
1204                 return true;
1205             }
1206 
1207             hfp_parser_store_if_token(hfp_connection, byte);
1208             if (!hfp_parser_is_separator(byte)) return true;
1209 
1210             // ignore empty tokens
1211             if (hfp_parser_is_buffer_empty(hfp_connection) && (hfp_connection->ignore_value == 0)) return true;
1212 
1213             parse_sequence(hfp_connection);
1214 
1215             hfp_parser_reset_line_buffer(hfp_connection);
1216 
1217             switch (hfp_connection->command){
1218                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
1219                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1220                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1221                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1222                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1223                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1224                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
1225                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
1226                 case HFP_CMD_HF_INDICATOR_STATUS:
1227                     hfp_connection->parser_state = HFP_PARSER_SECOND_ITEM;
1228                     break;
1229                 default:
1230                     break;
1231             }
1232             return true;
1233 
1234         case HFP_PARSER_SECOND_ITEM:
1235 
1236             hfp_parser_store_if_token(hfp_connection, byte);
1237             if (!hfp_parser_is_separator(byte)) return true;
1238 
1239             switch (hfp_connection->command){
1240                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1241                     log_info("format %s, ", hfp_connection->line_buffer);
1242                     hfp_connection->network_operator.format =  btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1243                     break;
1244                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1245                     log_info("format %s \n", hfp_connection->line_buffer);
1246                     hfp_connection->network_operator.format =  btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1247                     break;
1248                 case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1249                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1250                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
1251                     hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].state = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1252                     break;
1253                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1254                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1255                     log_info("%d \n", hfp_connection->ag_indicators[hfp_connection->parser_item_index].status);
1256                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status_changed = 1;
1257                     break;
1258                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
1259                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].min_range = btstack_atoi((char *)hfp_connection->line_buffer);
1260                     log_info("%s, ", hfp_connection->line_buffer);
1261                     break;
1262                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
1263                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1264                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1265                     hfp_connection->bnip_type = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1266                     break;
1267                 case HFP_CMD_HF_INDICATOR_STATUS:
1268                     hfp_connection->parser_indicator_value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1269                     break;
1270                 default:
1271                     break;
1272             }
1273 
1274             hfp_parser_reset_line_buffer(hfp_connection);
1275 
1276             hfp_connection->parser_state = HFP_PARSER_THIRD_ITEM;
1277 
1278             return true;
1279 
1280         case HFP_PARSER_THIRD_ITEM:
1281 
1282             hfp_parser_store_if_token(hfp_connection, byte);
1283             if (!hfp_parser_is_separator(byte)) return true;
1284 
1285             switch (hfp_connection->command){
1286                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1287                     strncpy(hfp_connection->network_operator.name, (char *)hfp_connection->line_buffer, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE);
1288                     hfp_connection->network_operator.name[HFP_MAX_NETWORK_OPERATOR_NAME_SIZE - 1] = 0;
1289                     log_info("name %s\n", hfp_connection->line_buffer);
1290                     break;
1291                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
1292                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].max_range = btstack_atoi((char *)hfp_connection->line_buffer);
1293                     hfp_next_indicators_index(hfp_connection);
1294                     hfp_connection->ag_indicators_nr = hfp_connection->parser_item_index;
1295                     log_info("%s)\n", hfp_connection->line_buffer);
1296                     break;
1297                 default:
1298                     break;
1299             }
1300 
1301             hfp_parser_reset_line_buffer(hfp_connection);
1302 
1303             if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS){
1304                 hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
1305             } else {
1306                 hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
1307             }
1308             return true;
1309 
1310         default:
1311             btstack_assert(false);
1312             return true;
1313     }
1314 }
1315 
1316 void hfp_parse(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){
1317     bool processed = false;
1318     while (!processed){
1319         processed = hfp_parse_byte(hfp_connection, byte, isHandsFree);
1320     }
1321     // reset parser state on end-of-line
1322     if (hfp_parser_is_end_of_line(byte)){
1323         hfp_connection->parser_item_index = 0;
1324         hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
1325     }
1326 }
1327 
1328 static void parse_sequence(hfp_connection_t * hfp_connection){
1329     int value;
1330     switch (hfp_connection->command){
1331         case HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS:
1332             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1333             int i;
1334             switch (hfp_connection->parser_item_index){
1335                 case 0:
1336                     for (i=0;i<hfp_connection->generic_status_indicators_nr;i++){
1337                         if (hfp_connection->generic_status_indicators[i].uuid == value){
1338                             hfp_connection->parser_indicator_index = i;
1339                             break;
1340                         }
1341                     }
1342                     break;
1343                 case 1:
1344                     if (hfp_connection->parser_indicator_index <0) break;
1345                     hfp_connection->generic_status_indicators[hfp_connection->parser_indicator_index].state = value;
1346                     log_info("HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS set indicator at index %u, to %u\n",
1347                      hfp_connection->parser_item_index, value);
1348                     break;
1349                 default:
1350                     break;
1351             }
1352             hfp_next_indicators_index(hfp_connection);
1353             break;
1354 
1355         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1356             switch(hfp_connection->parser_item_index){
1357                 case 0:
1358                     // <alpha>: This optional field is not supported, and shall be left blank.
1359                     break;
1360                 case 1:
1361                     // <number>: Quoted string containing the phone number in the format specified by <type>.
1362                     strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1363                     hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
1364                     break;
1365                 case 2:
1366                     /*
1367                       <type> field specifies the format of the phone number provided, and can be one of the following values:
1368                       - 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.
1369                      - 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.
1370                      - values 160-175: National number. No prefix nor escape digits included.
1371                      */
1372                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1373                     hfp_connection->bnip_type = value;
1374                     break;
1375                 case 3:
1376                     // <speed>: This optional field is not supported, and shall be left blank.
1377                     break;
1378                 case 4:
1379                     // <service>: Indicates which service this phone number relates to. Shall be either 4 (voice) or 5 (fax).
1380                 default:
1381                     break;
1382             }
1383             // index > 2 are ignored in switch above
1384             hfp_connection->parser_item_index++;
1385             break;
1386         case HFP_CMD_LIST_CURRENT_CALLS:
1387             switch(hfp_connection->parser_item_index){
1388                 case 0:
1389                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1390                     hfp_connection->clcc_idx = value;
1391                     break;
1392                 case 1:
1393                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1394                     hfp_connection->clcc_dir = value;
1395                     break;
1396                 case 2:
1397                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1398                     hfp_connection->clcc_status = value;
1399                     break;
1400                 case 3:
1401                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1402                     hfp_connection->clcc_mode = value;
1403                     break;
1404                 case 4:
1405                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1406                     hfp_connection->clcc_mpty = value;
1407                     break;
1408                 case 5:
1409                     strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1410                     hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
1411                     break;
1412                 case 6:
1413                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1414                     hfp_connection->bnip_type = value;
1415                     break;
1416                 default:
1417                     break;
1418             }
1419             // index > 6 are ignored in switch above
1420             hfp_connection->parser_item_index++;
1421             break;
1422         case HFP_CMD_SET_MICROPHONE_GAIN:
1423             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1424             hfp_connection->microphone_gain = value;
1425             log_info("hfp parse HFP_CMD_SET_MICROPHONE_GAIN %d\n", value);
1426             break;
1427         case HFP_CMD_SET_SPEAKER_GAIN:
1428             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1429             hfp_connection->speaker_gain = value;
1430             log_info("hfp parse HFP_CMD_SET_SPEAKER_GAIN %d\n", value);
1431             break;
1432         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
1433             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1434             hfp_connection->ag_activate_voice_recognition = value;
1435             log_info("hfp parse HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION %d\n", value);
1436             break;
1437         case HFP_CMD_TURN_OFF_EC_AND_NR:
1438             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1439             hfp_connection->ag_echo_and_noise_reduction = value;
1440             log_info("hfp parse HFP_CMD_TURN_OFF_EC_AND_NR %d\n", value);
1441             break;
1442         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
1443             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1444             hfp_connection->remote_supported_features = store_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE, value);
1445             log_info("hfp parse HFP_CHANGE_IN_BAND_RING_TONE_SETTING %d\n", value);
1446             break;
1447         case HFP_CMD_HF_CONFIRMED_CODEC:
1448             hfp_connection->codec_confirmed = btstack_atoi((char*)hfp_connection->line_buffer);
1449             log_info("hfp parse HFP_CMD_HF_CONFIRMED_CODEC %d\n", hfp_connection->codec_confirmed);
1450             break;
1451         case HFP_CMD_AG_SUGGESTED_CODEC:
1452             hfp_connection->suggested_codec = btstack_atoi((char*)hfp_connection->line_buffer);
1453             log_info("hfp parse HFP_CMD_AG_SUGGESTED_CODEC %d\n", hfp_connection->suggested_codec);
1454             break;
1455         case HFP_CMD_SUPPORTED_FEATURES:
1456             hfp_connection->remote_supported_features = btstack_atoi((char*)hfp_connection->line_buffer);
1457             log_info("Parsed supported feature %d\n", (int) hfp_connection->remote_supported_features);
1458             break;
1459         case HFP_CMD_AVAILABLE_CODECS:
1460             log_info("Parsed codec %s\n", hfp_connection->line_buffer);
1461             hfp_connection->remote_codecs[hfp_connection->parser_item_index] = (uint16_t)btstack_atoi((char*)hfp_connection->line_buffer);
1462             hfp_next_codec_index(hfp_connection);
1463             hfp_connection->remote_codecs_nr = hfp_connection->parser_item_index;
1464             break;
1465         case HFP_CMD_RETRIEVE_AG_INDICATORS:
1466             strncpy((char *)hfp_connection->ag_indicators[hfp_connection->parser_item_index].name,  (char *)hfp_connection->line_buffer, HFP_MAX_INDICATOR_DESC_SIZE);
1467             hfp_connection->ag_indicators[hfp_connection->parser_item_index].name[HFP_MAX_INDICATOR_DESC_SIZE-1] = 0;
1468             hfp_connection->ag_indicators[hfp_connection->parser_item_index].index = hfp_connection->parser_item_index+1;
1469             log_info("Indicator %d: %s (", hfp_connection->ag_indicators_nr+1, hfp_connection->line_buffer);
1470             break;
1471         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1472             log_info("Parsed Indicator %d with status: %s\n", hfp_connection->parser_item_index+1, hfp_connection->line_buffer);
1473             hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = btstack_atoi((char *) hfp_connection->line_buffer);
1474             hfp_next_indicators_index(hfp_connection);
1475             break;
1476         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
1477             hfp_next_indicators_index(hfp_connection);
1478             if (hfp_connection->parser_item_index != 4) break;
1479             log_info("Parsed Enable indicators: %s\n", hfp_connection->line_buffer);
1480             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1481             hfp_connection->enable_status_update_for_ag_indicators = (uint8_t) value;
1482             break;
1483         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
1484             log_info("Parsed Support call hold: %s\n", hfp_connection->line_buffer);
1485             if (hfp_connection->line_size > 2 ) break;
1486             memcpy((char *)hfp_connection->remote_call_services[hfp_connection->remote_call_services_index].name, (char *)hfp_connection->line_buffer, HFP_CALL_SERVICE_SIZE-1);
1487             hfp_connection->remote_call_services[hfp_connection->remote_call_services_index].name[HFP_CALL_SERVICE_SIZE - 1] = 0;
1488             hfp_next_remote_call_services_index(hfp_connection);
1489             break;
1490         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1491         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1492             log_info("Parsed Generic status indicator: %s\n", hfp_connection->line_buffer);
1493             hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].uuid = (uint16_t)btstack_atoi((char*)hfp_connection->line_buffer);
1494             hfp_next_indicators_index(hfp_connection);
1495             hfp_connection->generic_status_indicators_nr = hfp_connection->parser_item_index;
1496             break;
1497         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
1498             // HF parses inital AG gen. ind. state
1499             log_info("Parsed List generic status indicator %s state: ", hfp_connection->line_buffer);
1500             hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection);
1501             break;
1502         case HFP_CMD_HF_INDICATOR_STATUS:
1503             hfp_connection->parser_indicator_index = hfp_parse_indicator_index(hfp_connection);
1504             log_info("Parsed HF indicator index %u", hfp_connection->parser_indicator_index);
1505             break;
1506         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
1507             // AG parses new gen. ind. state
1508             if (hfp_connection->ignore_value){
1509                 hfp_connection->ignore_value = 0;
1510                 log_info("Parsed Enable AG indicator pos %u('%s') - unchanged (stays %u)\n", hfp_connection->parser_item_index,
1511                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled);
1512             }
1513             else if (hfp_connection->ag_indicators[hfp_connection->parser_item_index].mandatory){
1514                 log_info("Parsed Enable AG indicator pos %u('%s') - ignore (mandatory)\n",
1515                     hfp_connection->parser_item_index, hfp_connection->ag_indicators[hfp_connection->parser_item_index].name);
1516             } else {
1517                 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1518                 hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled = value;
1519                 log_info("Parsed Enable AG indicator pos %u('%s'): %u\n", hfp_connection->parser_item_index,
1520                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, value);
1521             }
1522             hfp_next_indicators_index(hfp_connection);
1523             break;
1524         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1525             // indicators are indexed starting with 1
1526             hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection);
1527             log_info("Parsed status of the AG indicator %d, status ", hfp_connection->parser_item_index);
1528             break;
1529         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1530             hfp_connection->network_operator.mode = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1531             log_info("Parsed network operator mode: %d, ", hfp_connection->network_operator.mode);
1532             break;
1533         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1534             if (hfp_connection->line_buffer[0] == '3'){
1535                 log_info("Parsed Set network operator format : %s, ", hfp_connection->line_buffer);
1536                 break;
1537             }
1538             // TODO emit ERROR, wrong format
1539             log_info("ERROR Set network operator format: index %s not supported\n", hfp_connection->line_buffer);
1540             break;
1541         case HFP_CMD_ERROR:
1542             break;
1543         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1544             hfp_connection->extended_audio_gateway_error = 1;
1545             hfp_connection->extended_audio_gateway_error_value = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1546             break;
1547         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
1548             hfp_connection->enable_extended_audio_gateway_error_report = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1549             hfp_connection->ok_pending = 1;
1550             hfp_connection->extended_audio_gateway_error = 0;
1551             break;
1552         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1553         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1554         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1555             strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1556             hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
1557             break;
1558         case HFP_CMD_CALL_HOLD:
1559             hfp_connection->ag_call_hold_action = hfp_connection->line_buffer[0] - '0';
1560             if (hfp_connection->line_buffer[1] != '\0'){
1561                 hfp_connection->call_index = btstack_atoi((char *)&hfp_connection->line_buffer[1]);
1562             }
1563             break;
1564         case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
1565             hfp_connection->ag_response_and_hold_action = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1566             break;
1567         case HFP_CMD_TRANSMIT_DTMF_CODES:
1568             hfp_connection->ag_dtmf_code = hfp_connection->line_buffer[0];
1569             break;
1570         case HFP_CMD_ENABLE_CLIP:
1571             hfp_connection->clip_enabled = hfp_connection->line_buffer[0] != '0';
1572             break;
1573         case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
1574             hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[0] != '0';
1575             break;
1576         default:
1577             break;
1578     }
1579 }
1580 
1581 static void hfp_handle_start_sdp_client_query(void * context){
1582     UNUSED(context);
1583 
1584     btstack_linked_list_iterator_t it;
1585     btstack_linked_list_iterator_init(&it, &hfp_connections);
1586     while (btstack_linked_list_iterator_has_next(&it)){
1587         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1588 
1589         if (connection->state != HFP_W2_SEND_SDP_QUERY) continue;
1590 
1591         connection->state = HFP_W4_SDP_QUERY_COMPLETE;
1592         sdp_query_context.local_role = connection->local_role;
1593         (void)memcpy(sdp_query_context.remote_address, connection->remote_addr, 6);
1594         sdp_client_query_rfcomm_channel_and_name_for_service_class_uuid(&handle_query_rfcomm_event, connection->remote_addr, connection->service_uuid);
1595         return;
1596     }
1597 }
1598 
1599 void hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid, hfp_role_t local_role){
1600     hfp_connection_t * hfp_connection = provide_hfp_connection_context_for_bd_addr(bd_addr, local_role);
1601     log_info("hfp_connect %s, hfp_connection %p", bd_addr_to_str(bd_addr), hfp_connection);
1602 
1603     if (!hfp_connection) {
1604         log_error("hfp_establish_service_level_connection for addr %s failed", bd_addr_to_str(bd_addr));
1605         return;
1606     }
1607     switch (hfp_connection->state){
1608         case HFP_W2_DISCONNECT_RFCOMM:
1609             hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
1610             return;
1611         case HFP_W4_RFCOMM_DISCONNECTED:
1612             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART;
1613             return;
1614         case HFP_IDLE:
1615             (void)memcpy(hfp_connection->remote_addr, bd_addr, 6);
1616             hfp_connection->state = HFP_W2_SEND_SDP_QUERY;
1617             hfp_connection->service_uuid = service_uuid;
1618 
1619             hfp_handle_sdp_client_query_request.callback = &hfp_handle_start_sdp_client_query;
1620             // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback
1621             (void) sdp_client_register_query_callback(&hfp_handle_sdp_client_query_request);
1622             break;
1623         default:
1624             break;
1625     }
1626 }
1627 
1628 void hfp_release_service_level_connection(hfp_connection_t * hfp_connection){
1629     if (!hfp_connection) return;
1630     hfp_release_audio_connection(hfp_connection);
1631 
1632     if (hfp_connection->state < HFP_W4_RFCOMM_CONNECTED){
1633         hfp_connection->state = HFP_IDLE;
1634         return;
1635     }
1636 
1637     if (hfp_connection->state == HFP_W4_RFCOMM_CONNECTED){
1638         hfp_connection->state = HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN;
1639         return;
1640     }
1641 
1642     if (hfp_connection->state < HFP_W4_SCO_CONNECTED){
1643         hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
1644         return;
1645     }
1646 
1647     if (hfp_connection->state < HFP_W4_SCO_DISCONNECTED){
1648         hfp_connection->state = HFP_W2_DISCONNECT_SCO;
1649         return;
1650     }
1651 
1652     // HFP_W4_SCO_DISCONNECTED or later
1653     hfp_connection->release_slc_connection = 1;
1654 }
1655 
1656 void hfp_release_audio_connection(hfp_connection_t * hfp_connection){
1657     if (!hfp_connection) return;
1658     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
1659     hfp_connection->release_audio_connection = 1;
1660 }
1661 
1662 static const struct link_settings {
1663     const uint16_t max_latency;
1664     const uint8_t  retransmission_effort;
1665     const uint16_t packet_types;
1666     const bool     eSCO;
1667     const uint8_t  codec;
1668 } hfp_link_settings [] = {
1669     { 0xffff, 0xff, SCO_PACKET_TYPES_HV1,  false, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_D0
1670     { 0xffff, 0xff, SCO_PACKET_TYPES_HV3,  false, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_D1
1671     { 0x0007, 0x01, SCO_PACKET_TYPES_EV3,  true,  HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S1
1672     { 0x0007, 0x01, SCO_PACKET_TYPES_2EV3, true,  HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S2
1673     { 0x000a, 0x01, SCO_PACKET_TYPES_2EV3, true,  HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S3
1674     { 0x000c, 0x02, SCO_PACKET_TYPES_2EV3, true,  HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S4
1675     { 0x0008, 0x02, SCO_PACKET_TYPES_EV3,  true,  HFP_CODEC_MSBC }, // HFP_LINK_SETTINGS_T1
1676     { 0x000d, 0x02, SCO_PACKET_TYPES_2EV3, true,  HFP_CODEC_MSBC }  // HFP_LINK_SETTINGS_T2
1677 };
1678 
1679 void hfp_setup_synchronous_connection(hfp_connection_t * hfp_connection){
1680     // all packet types, fixed bandwidth
1681     int setting = hfp_connection->link_setting;
1682     log_info("hfp_setup_synchronous_connection using setting nr %u", setting);
1683     sco_establishment_active = hfp_connection;
1684     uint16_t sco_voice_setting = hci_get_sco_voice_setting();
1685     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
1686 #ifdef ENABLE_BCM_PCM_WBS
1687         sco_voice_setting = 0x0063; // Transparent data, 16-bit for BCM controllers
1688 #else
1689         sco_voice_setting = 0x0043; // Transparent data, 8-bit otherwise
1690 #endif
1691     }
1692     // get packet types - bits 6-9 are 'don't allow'
1693     uint16_t packet_types = hfp_link_settings[setting].packet_types ^ 0x03c0;
1694     hci_send_cmd(&hci_setup_synchronous_connection, hfp_connection->acl_handle, 8000, 8000, hfp_link_settings[setting].max_latency,
1695         sco_voice_setting, hfp_link_settings[setting].retransmission_effort, packet_types);
1696 }
1697 
1698 void hfp_accept_synchronous_connection(hfp_connection_t * hfp_connection, bool incoming_eSCO){
1699 
1700     // remote supported feature eSCO is set if link type is eSCO
1701     // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms,
1702     uint16_t max_latency;
1703     uint8_t  retransmission_effort;
1704     uint16_t packet_types;
1705 
1706     if (incoming_eSCO && hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){
1707         max_latency = 0x000c;
1708         retransmission_effort = 0x02;
1709         // eSCO: EV3 and 2-EV3
1710         packet_types = 0x0048;
1711     } else {
1712         max_latency = 0xffff;
1713         retransmission_effort = 0xff;
1714         // sco: HV1 and HV3
1715         packet_types = 0x005;
1716     }
1717 
1718     // mSBC only allows for transparent data
1719     uint16_t sco_voice_setting = hci_get_sco_voice_setting();
1720     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
1721 #ifdef ENABLE_BCM_PCM_WBS
1722         sco_voice_setting = 0x0063; // Transparent data, 16-bit for BCM controllers
1723 #else
1724         sco_voice_setting = 0x0043; // Transparent data, 8-bit otherwise
1725 #endif
1726     }
1727 
1728     // filter packet types
1729     packet_types &= hfp_get_sco_packet_types();
1730 
1731     // bits 6-9 are 'don't allow'
1732     packet_types ^= 0x3c0;
1733 
1734     log_info("HFP: sending hci_accept_connection_request, packet types 0x%04x, sco_voice_setting 0x%02x", packet_types, sco_voice_setting);
1735     hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency,
1736                  sco_voice_setting, retransmission_effort, packet_types);
1737 }
1738 
1739 #ifdef ENABLE_CC256X_ASSISTED_HFP
1740 void hfp_cc256x_prepare_for_sco(hfp_connection_t * hfp_connection){
1741     hfp_connection->cc256x_send_write_codec_config = true;
1742     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
1743         hfp_connection->cc256x_send_wbs_associate = true;
1744     }
1745 }
1746 
1747 void hfp_cc256x_write_codec_config(hfp_connection_t * hfp_connection){
1748     uint32_t sample_rate_hz;
1749     uint16_t clock_rate_khz;
1750     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
1751         clock_rate_khz = 512;
1752         sample_rate_hz = 16000;
1753     } else {
1754         clock_rate_khz = 256;
1755         sample_rate_hz = 8000;
1756     }
1757     uint8_t clock_direction = 0;        // master
1758     uint16_t frame_sync_duty_cycle = 0; // i2s with 50%
1759     uint8_t  frame_sync_edge = 1;       // rising edge
1760     uint8_t  frame_sync_polarity = 0;   // active high
1761     uint8_t  reserved = 0;
1762     uint16_t size = 16;
1763     uint16_t chan_1_offset = 1;
1764     uint16_t chan_2_offset = chan_1_offset + size;
1765     uint8_t  out_edge = 1;              // rising
1766     uint8_t  in_edge = 0;               // falling
1767     hci_send_cmd(&hci_ti_write_codec_config, clock_rate_khz, clock_direction, sample_rate_hz, frame_sync_duty_cycle,
1768                  frame_sync_edge, frame_sync_polarity, reserved,
1769                  size, chan_1_offset, out_edge, size, chan_1_offset, in_edge, reserved,
1770                  size, chan_2_offset, out_edge, size, chan_2_offset, in_edge, reserved);
1771 }
1772 #endif
1773 
1774 #ifdef ENABLE_BCM_PCM_WBS
1775 void hfp_bcm_prepare_for_sco(hfp_connection_t * hfp_connection){
1776     hfp_connection->bcm_send_write_i2spcm_interface_param = true;
1777     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
1778         hfp_connection->bcm_send_enable_wbs = true;
1779     }
1780 }
1781 void hfp_bcm_write_i2spcm_interface_param(hfp_connection_t * hfp_connection){
1782     uint8_t sample_rate = (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? 1 : 0;
1783     // i2s enable, master, 8/16 kHz, 512 kHz
1784     hci_send_cmd(&hci_bcm_write_i2spcm_interface_paramhci_bcm_write_i2spcm_interface_param, 1, 1, sample_rate, 2);
1785 }
1786 #endif
1787 
1788 void hfp_set_hf_callback(btstack_packet_handler_t callback){
1789     hfp_hf_callback = callback;
1790 }
1791 
1792 void hfp_set_ag_callback(btstack_packet_handler_t callback){
1793     hfp_ag_callback = callback;
1794 }
1795 
1796 void hfp_set_ag_rfcomm_packet_handler(btstack_packet_handler_t handler){
1797     hfp_ag_rfcomm_packet_handler = handler;
1798 }
1799 
1800 void hfp_set_hf_rfcomm_packet_handler(btstack_packet_handler_t handler){
1801     hfp_hf_rfcomm_packet_handler = handler;
1802 }
1803 
1804 void hfp_init(void){
1805     hfp_allowed_sco_packet_types = SCO_PACKET_TYPES_ALL;
1806 }
1807 
1808 void hfp_deinit(void){
1809     hfp_connections = NULL;
1810     hfp_hf_callback = NULL;
1811     hfp_ag_callback = NULL;
1812     hfp_hf_rfcomm_packet_handler = NULL;
1813     hfp_ag_rfcomm_packet_handler = NULL;
1814     sco_establishment_active = NULL;
1815     (void) memset(&sdp_query_context, 0, sizeof(hfp_sdp_query_context_t));
1816     (void) memset(&hfp_handle_sdp_client_query_request, 0, sizeof(btstack_context_callback_registration_t));
1817 }
1818 
1819 void hfp_set_sco_packet_types(uint16_t packet_types){
1820     hfp_allowed_sco_packet_types = packet_types;
1821 }
1822 
1823 uint16_t hfp_get_sco_packet_types(void){
1824     return hfp_allowed_sco_packet_types;
1825 }
1826 
1827 hfp_link_settings_t hfp_next_link_setting(hfp_link_settings_t current_setting, bool local_eSCO_supported, bool remote_eSCO_supported, bool eSCO_S4_supported, uint8_t negotiated_codec){
1828     int8_t setting = (int8_t) current_setting;
1829     bool can_use_eSCO = local_eSCO_supported && remote_eSCO_supported;
1830     while (setting > 0){
1831         setting--;
1832         // skip if eSCO required but not available
1833         if (hfp_link_settings[setting].eSCO && !can_use_eSCO) continue;
1834         // skip if S4 but not supported
1835         if ((setting == (int8_t) HFP_LINK_SETTINGS_S4) && !eSCO_S4_supported) continue;
1836         // skip wrong codec
1837         if ( hfp_link_settings[setting].codec != negotiated_codec) continue;
1838         // skip disabled packet types
1839         uint16_t required_packet_types = hfp_link_settings[setting].packet_types;
1840         uint16_t allowed_packet_types  = hfp_allowed_sco_packet_types;
1841         if ((required_packet_types & allowed_packet_types) == 0) continue;
1842 
1843         // found matching setting
1844         return (hfp_link_settings_t) setting;
1845     }
1846     return HFP_LINK_SETTINGS_NONE;
1847 }
1848 
1849 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){
1850     bool local_eSCO_supported  = hci_extended_sco_link_supported();
1851     bool remote_eSCO_supported = hci_remote_esco_supported(hfp_connection->acl_handle);
1852     uint8_t negotiated_codec   = hfp_connection->negotiated_codec;
1853     return  hfp_next_link_setting(current_setting, local_eSCO_supported, remote_eSCO_supported, eSCO_S4_supported, negotiated_codec);
1854 }
1855 
1856 void hfp_init_link_settings(hfp_connection_t * hfp_connection, uint8_t eSCO_S4_supported){
1857     // get highest possible link setting
1858     hfp_connection->link_setting = hfp_next_link_setting_for_connection(HFP_LINK_SETTINGS_NONE, hfp_connection, eSCO_S4_supported);
1859     log_info("hfp_init_link_settings: %u", hfp_connection->link_setting);
1860 }
1861 
1862 #define HFP_HF_RX_DEBUG_PRINT_LINE 80
1863 
1864 void hfp_log_rfcomm_message(const char * tag, uint8_t * packet, uint16_t size){
1865 #ifdef ENABLE_LOG_INFO
1866     // encode \n\r
1867     char printable[HFP_HF_RX_DEBUG_PRINT_LINE+2];
1868     int i = 0;
1869     int pos;
1870     for (pos=0 ; (pos < size) && (i < (HFP_HF_RX_DEBUG_PRINT_LINE - 3)) ; pos++){
1871         switch (packet[pos]){
1872             case '\n':
1873                 printable[i++] = '\\';
1874                 printable[i++] = 'n';
1875                 break;
1876             case '\r':
1877                 printable[i++] = '\\';
1878                 printable[i++] = 'r';
1879                 break;
1880             default:
1881                 printable[i++] = packet[pos];
1882                 break;
1883         }
1884     }
1885     printable[i] = 0;
1886     log_info("%s: '%s'", tag, printable);
1887 #endif
1888 }
1889