xref: /btstack/src/classic/hfp_hf.c (revision 0f7fd6c19b44e3fae3aaf3caf2c11dd07533fe4c)
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_hf.c"
39 
40 // *****************************************************************************
41 //
42 // HFP Hands-Free (HF) unit
43 //
44 // *****************************************************************************
45 
46 #include "btstack_config.h"
47 
48 #include <stdint.h>
49 #include <stdio.h>
50 #include <string.h>
51 
52 #include "bluetooth_sdp.h"
53 #include "btstack_debug.h"
54 #include "btstack_event.h"
55 #include "btstack_memory.h"
56 #include "btstack_run_loop.h"
57 #include "classic/core.h"
58 #include "classic/hfp.h"
59 #include "classic/hfp_hf.h"
60 #include "classic/sdp_client_rfcomm.h"
61 #include "classic/sdp_server.h"
62 #include "classic/sdp_util.h"
63 #include "hci.h"
64 #include "hci_cmd.h"
65 #include "hci_dump.h"
66 #include "l2cap.h"
67 
68 // const
69 static const char default_hfp_hf_service_name[] = "Hands-Free unit";
70 
71 // globals
72 static btstack_packet_callback_registration_t hfp_hf_hci_event_callback_registration;
73 
74 static uint16_t hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
75 static uint8_t hfp_codecs_nr;
76 static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
77 
78 static uint8_t hfp_indicators_nr;
79 static uint8_t hfp_indicators[HFP_MAX_NUM_INDICATORS];
80 static uint32_t hfp_indicators_value[HFP_MAX_NUM_INDICATORS];
81 
82 static uint8_t hfp_hf_speaker_gain;
83 static uint8_t hfp_hf_microphone_gain;
84 
85 static btstack_packet_handler_t hfp_hf_callback;
86 
87 static hfp_call_status_t hfp_call_status;
88 static hfp_callsetup_status_t hfp_callsetup_status;
89 static hfp_callheld_status_t hfp_callheld_status;
90 
91 static char phone_number[25];
92 
93 static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
94 	int hf = get_bit(hfp_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
95 	int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
96 	return hf && ag;
97 }
98 
99 static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
100 	int hf = get_bit(hfp_supported_features, HFP_HFSF_THREE_WAY_CALLING);
101 	int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING);
102 	return hf && ag;
103 }
104 
105 
106 static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
107 	int hf = get_bit(hfp_supported_features, HFP_HFSF_HF_INDICATORS);
108 	int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS);
109 	return hf && ag;
110 }
111 
112 
113 static hfp_connection_t * get_hfp_hf_connection_context_for_acl_handle(uint16_t handle){
114     btstack_linked_list_iterator_t it;
115     btstack_linked_list_iterator_init(&it, hfp_get_connections());
116     while (btstack_linked_list_iterator_has_next(&it)){
117         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
118         if (hfp_connection->acl_handle != handle)      continue;
119         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
120         return hfp_connection;
121     }
122     return NULL;
123 }
124 
125 /* emit functinos */
126 
127 static void hfp_hf_emit_subscriber_information(const hfp_connection_t * hfp_connection, uint8_t status){
128     if (hfp_hf_callback == NULL) return;
129     uint8_t event[33];
130     event[0] = HCI_EVENT_HFP_META;
131     event[1] = sizeof(event) - 2;
132     event[2] = HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION;
133     little_endian_store_16(event, 3, hfp_connection->acl_handle);
134     event[5] = status;
135     event[6] = hfp_connection->bnip_type;
136     uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - 8);
137     strncpy((char*)&event[7], hfp_connection->bnip_number, size);
138     event[7 + size] = 0;
139     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
140 }
141 
142 static void hfp_hf_emit_type_and_number(const hfp_connection_t * hfp_connection, uint8_t event_subtype){
143     if (hfp_hf_callback == NULL) return;
144     uint8_t event[32];
145     event[0] = HCI_EVENT_HFP_META;
146     event[1] = sizeof(event) - 2;
147     event[2] = event_subtype;
148     little_endian_store_16(event, 3, hfp_connection->acl_handle);
149     event[5] = hfp_connection->bnip_type;
150     uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - 7);
151     strncpy((char*)&event[6], hfp_connection->bnip_number, size);
152     event[6 + size] = 0;
153     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
154 }
155 
156 static void hfp_hf_emit_enhanced_call_status(const hfp_connection_t * hfp_connection){
157     if (hfp_hf_callback == NULL) return;
158     uint8_t event[38];
159     int pos = 0;
160     event[pos++] = HCI_EVENT_HFP_META;
161     event[pos++] = sizeof(event) - 2;
162     event[pos++] = HFP_SUBEVENT_ENHANCED_CALL_STATUS;
163     little_endian_store_16(event, pos, hfp_connection->acl_handle);
164     pos += 2;
165     event[pos++] = hfp_connection->clcc_idx;
166     event[pos++] = hfp_connection->clcc_dir;
167     event[pos++] = hfp_connection->clcc_status;
168     event[pos++] = hfp_connection->clcc_mode;
169     event[pos++] = hfp_connection->clcc_mpty;
170     event[pos++] = hfp_connection->bnip_type;
171     uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - pos - 1);
172     strncpy((char*)&event[pos], hfp_connection->bnip_number, size);
173     pos += size;
174     event[pos++] = 0;
175     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, pos);
176 }
177 
178 
179 static void hfp_emit_ag_indicator_event(const hfp_connection_t * hfp_connection, const hfp_ag_indicator_t * indicator){
180 	if (hfp_hf_callback == NULL) return;
181 	uint8_t event[12+HFP_MAX_INDICATOR_DESC_SIZE+1];
182 	int pos = 0;
183 	event[pos++] = HCI_EVENT_HFP_META;
184 	event[pos++] = sizeof(event) - 2;
185 	event[pos++] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
186     little_endian_store_16(event, pos, hfp_connection->acl_handle);
187     pos += 2;
188 	event[pos++] = indicator->index;
189 	event[pos++] = indicator->status;
190 	event[pos++] = indicator->min_range;
191 	event[pos++] = indicator->max_range;
192 	event[pos++] = indicator->mandatory;
193 	event[pos++] = indicator->enabled;
194 	event[pos++] = indicator->status_changed;
195 	strncpy((char*)&event[pos], indicator->name, HFP_MAX_INDICATOR_DESC_SIZE);
196 	pos += HFP_MAX_INDICATOR_DESC_SIZE;
197 	event[pos] = 0;
198 	(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
199 }
200 
201 static void hfp_emit_network_operator_event(const hfp_connection_t * hfp_connection){
202     if (hfp_hf_callback == NULL) return;
203 	uint8_t event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE+1];
204 	event[0] = HCI_EVENT_HFP_META;
205 	event[1] = sizeof(event) - 2;
206 	event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED;
207     little_endian_store_16(event, 3, hfp_connection->acl_handle);
208 	event[5] = hfp_connection->network_operator.mode;
209 	event[6] = hfp_connection->network_operator.format;
210 	strncpy((char*)&event[7], hfp_connection->network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE);
211 	event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE] = 0;
212 	(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
213 }
214 
215 /* send commands */
216 
217 static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){
218     char buffer[20];
219     snprintf(buffer, sizeof(buffer), "AT%s\r", cmd);
220     return send_str_over_rfcomm(cid, buffer);
221 }
222 
223 static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){
224     char buffer[20];
225     snprintf(buffer, sizeof(buffer), "AT%s%s\r", cmd, mark);
226     return send_str_over_rfcomm(cid, buffer);
227 }
228 
229 static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint16_t value){
230     char buffer[40];
231     snprintf(buffer, sizeof(buffer), "AT%s=%d\r", cmd, value);
232     return send_str_over_rfcomm(cid, buffer);
233 }
234 
235 static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){
236     char buffer[30];
237     const int size = sizeof(buffer);
238     int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS);
239     offset += join(buffer+offset, size-offset, hfp_codecs, hfp_codecs_nr);
240     offset += snprintf(buffer+offset, size-offset, "\r");
241     return send_str_over_rfcomm(cid, buffer);
242 }
243 
244 static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){
245     char buffer[50];
246     const int size = sizeof(buffer);
247     int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS);
248     offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr);
249     offset += snprintf(buffer+offset, size-offset, "\r");
250     return send_str_over_rfcomm(cid, buffer);
251 }
252 
253 static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){
254     char buffer[30];
255     const int size = sizeof(buffer);
256     int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR);
257     offset += join(buffer+offset, size-offset, hfp_indicators, hfp_indicators_nr);
258     offset += snprintf(buffer+offset, size-offset, "\r");
259     return send_str_over_rfcomm(cid, buffer);
260 }
261 
262 static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){
263     char buffer[20];
264     snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate);
265     return send_str_over_rfcomm(cid, buffer);
266 }
267 
268 static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){
269     char buffer[40];
270     snprintf(buffer, sizeof(buffer), "%s%s;\r", HFP_CALL_PHONE_NUMBER, phone_number);
271     return send_str_over_rfcomm(cid, buffer);
272 }
273 
274 static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){
275     char buffer[40];
276     snprintf(buffer, sizeof(buffer), "%s>%d;\r", HFP_CALL_PHONE_NUMBER, memory_id);
277     return send_str_over_rfcomm(cid, buffer);
278 }
279 
280 static int hfp_hf_send_chld(uint16_t cid, unsigned int number){
281     char buffer[40];
282     snprintf(buffer, sizeof(buffer), "AT%s=%u\r", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number);
283     return send_str_over_rfcomm(cid, buffer);
284 }
285 
286 static int hfp_hf_send_dtmf(uint16_t cid, char code){
287     char buffer[20];
288     snprintf(buffer, sizeof(buffer), "AT%s=%c\r", HFP_TRANSMIT_DTMF_CODES, code);
289     return send_str_over_rfcomm(cid, buffer);
290 }
291 
292 static int hfp_hf_cmd_ata(uint16_t cid){
293     return send_str_over_rfcomm(cid, (char *) "ATA\r");
294 }
295 
296 static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){
297     return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_supported_features);
298 }
299 
300 static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){
301     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?");
302 }
303 
304 static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){
305     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?");
306 }
307 
308 static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){
309     return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?");
310 }
311 
312 static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){
313     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?");
314 }
315 
316 static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){
317     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?");
318 }
319 
320 static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){
321     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0");
322 }
323 
324 static int hfp_hf_cmd_query_operator_name(uint16_t cid){
325     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?");
326 }
327 
328 static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){
329     return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP);
330 }
331 
332 static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){
333     return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
334 }
335 
336 static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){
337     return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
338 }
339 
340 static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){
341     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate);
342 }
343 
344 static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){
345     return hfp_hf_send_cmd_with_int(cid, HFP_TURN_OFF_EC_AND_NR, activate);
346 }
347 
348 static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
349     return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate);
350 }
351 
352 static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
353     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
354 }
355 
356 static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
357     return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
358 }
359 
360 static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
361     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
362 }
363 
364 static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
365     return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER);
366 }
367 
368 static int hfp_hf_send_chup(uint16_t cid){
369     return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL);
370 }
371 
372 static int hfp_hf_send_binp(uint16_t cid){
373     return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1");
374 }
375 
376 static int hfp_hf_send_clcc(uint16_t cid){
377     return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS);
378 }
379 
380 /* state machines */
381 
382 static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
383     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
384     if (hfp_connection->ok_pending) return 0;
385     int done = 1;
386 
387     switch (hfp_connection->state){
388         case HFP_EXCHANGE_SUPPORTED_FEATURES:
389             hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr);
390             hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
391             hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid);
392             break;
393         case HFP_NOTIFY_ON_CODECS:
394             hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
395             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
396             break;
397         case HFP_RETRIEVE_INDICATORS:
398             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
399             hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid);
400             break;
401         case HFP_RETRIEVE_INDICATORS_STATUS:
402             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
403             hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid);
404             break;
405         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
406             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
407             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1);
408             break;
409         case HFP_RETRIEVE_CAN_HOLD_CALL:
410             hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
411             hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid);
412             break;
413         case HFP_LIST_GENERIC_STATUS_INDICATORS:
414             hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
415             hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
416             break;
417         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
418             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
419             hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
420             break;
421         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
422             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
423             hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
424             break;
425         default:
426             done = 0;
427             break;
428     }
429     return done;
430 }
431 
432 
433 static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
434     if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
435     if (hfp_connection->ok_pending) return 0;
436 
437     int done = 0;
438     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
439         hfp_connection->ok_pending = 1;
440         done = 1;
441         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
442         return done;
443     };
444     if (hfp_connection->change_status_update_for_individual_ag_indicators){
445         hfp_connection->ok_pending = 1;
446         done = 1;
447         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
448                 hfp_connection->ag_indicators_status_update_bitmap,
449                 hfp_connection->ag_indicators_nr);
450         return done;
451     }
452 
453     switch (hfp_connection->hf_query_operator_state){
454         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
455             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
456             hfp_connection->ok_pending = 1;
457             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
458             return 1;
459         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
460             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
461             hfp_connection->ok_pending = 1;
462             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
463             return 1;
464         default:
465             break;
466     }
467 
468     if (hfp_connection->enable_extended_audio_gateway_error_report){
469         hfp_connection->ok_pending = 1;
470         done = 1;
471         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
472         return done;
473     }
474 
475     return done;
476 }
477 
478 static int voice_recognition_state_machine(hfp_connection_t * hfp_connection){
479     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) {
480         return 0;
481     }
482     int done = 0;
483     if (hfp_connection->ok_pending == 0) {
484         switch (hfp_connection->vra_status){
485             case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
486             case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_OFF:
487                 hfp_connection->ok_pending = 1;
488                 hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
489                 break;
490             case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
491                 hfp_connection->ok_pending = 1;
492                 hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
493                 break;
494 
495             case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_ACTIVATED:
496                 if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){
497                     return 0;
498                 }
499                 hfp_connection->ok_pending = 1;
500                 hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 2);
501                 break;
502 
503             default:
504                 break;
505         }
506     } else {
507         switch (hfp_connection->vra_status){
508             case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
509                 hfp_connection->vra_status = HFP_VRA_VOICE_RECOGNITION_ACTIVATED;
510                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_VOICE_RECOGNITION_STATUS, 1);
511                 break;
512 
513             case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_ACTIVATED:
514                 hfp_connection->vra_status = HFP_VRA_ENHANCED_VOICE_RECOGNITION_ACTIVATED;
515                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_VOICE_RECOGNITION_STATUS, 2);
516                 break;
517 
518             case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
519             case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_OFF:
520                 hfp_connection->vra_status = HFP_VRA_VOICE_RECOGNITION_OFF;
521                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_VOICE_RECOGNITION_STATUS, 0);
522                 break;
523             default:
524                 break;
525         }
526     }
527     return done;
528 }
529 
530 
531 static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
532     if (hfp_connection->ok_pending) return 0;
533 
534     if (hfp_connection->trigger_codec_exchange){
535 		hfp_connection->trigger_codec_exchange = 0;
536 
537 		hfp_connection->ok_pending = 1;
538 		hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
539 		return 1;
540     }
541 
542     if (hfp_connection->hf_send_codec_confirm){
543 		hfp_connection->hf_send_codec_confirm = false;
544 
545 		hfp_connection->ok_pending = 1;
546 		hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
547 		return 1;
548     }
549 
550     if (hfp_connection->hf_send_supported_codecs){
551 		hfp_connection->hf_send_supported_codecs = false;
552 
553 		hfp_connection->ok_pending = 1;
554 		hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
555 		return 1;
556     }
557 
558     return 0;
559 }
560 
561 static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
562     if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
563         (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
564 
565     if (hfp_connection->release_audio_connection){
566         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
567         hfp_connection->release_audio_connection = 0;
568         gap_disconnect(hfp_connection->sco_handle);
569         return 1;
570     }
571 
572     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
573 
574     // run codecs exchange
575     int done = codecs_exchange_state_machine(hfp_connection);
576     if (done) return 1;
577 
578     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
579     if (hfp_connection->establish_audio_connection){
580         hfp_connection->state = HFP_W4_SCO_CONNECTED;
581         hfp_connection->establish_audio_connection = 0;
582         hfp_setup_synchronous_connection(hfp_connection);
583         return 1;
584     }
585     return 0;
586 }
587 
588 
589 static int call_setup_state_machine(hfp_connection_t * hfp_connection){
590 
591 	if (hfp_connection->ok_pending) return 0;
592 
593     if (hfp_connection->hf_answer_incoming_call){
594         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
595         hfp_connection->hf_answer_incoming_call = 0;
596         return 1;
597     }
598     return 0;
599 }
600 
601 static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){
602 
603 	btstack_assert(hfp_connection != NULL);
604 	btstack_assert(hfp_connection->local_role == HFP_ROLE_HF);
605 
606 	// during SDP query, RFCOMM CID is not set
607 	if (hfp_connection->rfcomm_cid == 0) return;
608 
609 	// assert command could be sent
610 	if (hci_can_send_command_packet_now() == 0) return;
611 
612 #ifdef ENABLE_CC256X_ASSISTED_HFP
613     // WBS Disassociate
614     if (hfp_connection->cc256x_send_wbs_disassociate){
615         hfp_connection->cc256x_send_wbs_disassociate = false;
616         hci_send_cmd(&hci_ti_wbs_disassociate);
617         return;
618     }
619     // Write Codec Config
620     if (hfp_connection->cc256x_send_write_codec_config){
621         hfp_connection->cc256x_send_write_codec_config = false;
622         hfp_cc256x_write_codec_config(hfp_connection);
623         return;
624     }
625     // WBS Associate
626     if (hfp_connection->cc256x_send_wbs_associate){
627         hfp_connection->cc256x_send_wbs_associate = false;
628         hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle);
629         return;
630     }
631 #endif
632 #ifdef ENABLE_BCM_PCM_WBS
633     // Enable WBS
634     if (hfp_connection->bcm_send_enable_wbs){
635         hfp_connection->bcm_send_enable_wbs = false;
636         hci_send_cmd(&hci_bcm_enable_wbs, 1, 2);
637         return;
638     }
639     // Write I2S/PCM params
640     if (hfp_connection->bcm_send_write_i2spcm_interface_param){
641         hfp_connection->bcm_send_write_i2spcm_interface_param = false;
642         hfp_bcm_write_i2spcm_interface_param(hfp_connection);
643         return;
644     }
645     // Disable WBS
646     if (hfp_connection->bcm_send_disable_wbs){
647         hfp_connection->bcm_send_disable_wbs = false;
648         hci_send_cmd(&hci_bcm_enable_wbs, 0, 2);
649         return;
650     }
651 #endif
652 #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
653     if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){
654         hfp_finalize_connection_context(hfp_connection);
655         return;
656     }
657 #endif
658 
659     if (hfp_connection->accept_sco){
660         bool incoming_eSCO = hfp_connection->accept_sco == 2;
661         hfp_connection->accept_sco = 0;
662         // notify about codec selection if not done already
663         if (hfp_connection->negotiated_codec == 0){
664             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
665         }
666         hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO);
667         return;
668     }
669 
670     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
671         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
672         return;
673     }
674     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
675     if (!done){
676         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
677     }
678     if (!done){
679         done = voice_recognition_state_machine(hfp_connection);
680     }
681     if (!done){
682         done = hfp_hf_run_for_audio_connection(hfp_connection);
683     }
684     if (!done){
685         done = call_setup_state_machine(hfp_connection);
686     }
687 
688     // don't send a new command while ok still pending
689     if (hfp_connection->ok_pending) return;
690 
691     if (hfp_connection->send_microphone_gain){
692         hfp_connection->send_microphone_gain = 0;
693         hfp_connection->ok_pending = 1;
694         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
695         return;
696     }
697 
698     if (hfp_connection->send_speaker_gain){
699         hfp_connection->send_speaker_gain = 0;
700         hfp_connection->ok_pending = 1;
701         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
702         return;
703     }
704 
705     if (hfp_connection->hf_deactivate_calling_line_notification){
706         hfp_connection->hf_deactivate_calling_line_notification = 0;
707         hfp_connection->ok_pending = 1;
708         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
709         return;
710     }
711 
712     if (hfp_connection->hf_activate_calling_line_notification){
713         hfp_connection->hf_activate_calling_line_notification = 0;
714         hfp_connection->ok_pending = 1;
715         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
716         return;
717     }
718 
719     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
720         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
721         hfp_connection->ok_pending = 1;
722         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0);
723         return;
724     }
725 
726     if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){
727         hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0;
728         hfp_connection->ok_pending = 1;
729         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1);
730         return;
731     }
732 
733     if (hfp_connection->hf_deactivate_call_waiting_notification){
734         hfp_connection->hf_deactivate_call_waiting_notification = 0;
735         hfp_connection->ok_pending = 1;
736         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
737         return;
738     }
739 
740     if (hfp_connection->hf_activate_call_waiting_notification){
741         hfp_connection->hf_activate_call_waiting_notification = 0;
742         hfp_connection->ok_pending = 1;
743         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
744         return;
745     }
746 
747     if (hfp_connection->hf_initiate_outgoing_call){
748         hfp_connection->hf_initiate_outgoing_call = 0;
749         hfp_connection->ok_pending = 1;
750         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
751         return;
752     }
753 
754     if (hfp_connection->hf_initiate_memory_dialing){
755         hfp_connection->hf_initiate_memory_dialing = 0;
756         hfp_connection->ok_pending = 1;
757         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
758         return;
759     }
760 
761     if (hfp_connection->hf_initiate_redial_last_number){
762         hfp_connection->hf_initiate_redial_last_number = 0;
763         hfp_connection->ok_pending = 1;
764         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
765         return;
766     }
767 
768     if (hfp_connection->hf_send_chup){
769         hfp_connection->hf_send_chup = 0;
770         hfp_connection->ok_pending = 1;
771         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
772         return;
773     }
774 
775     if (hfp_connection->hf_send_chld_0){
776         hfp_connection->hf_send_chld_0 = 0;
777         hfp_connection->ok_pending = 1;
778         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
779         return;
780     }
781 
782     if (hfp_connection->hf_send_chld_1){
783         hfp_connection->hf_send_chld_1 = 0;
784         hfp_connection->ok_pending = 1;
785         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
786         return;
787     }
788 
789     if (hfp_connection->hf_send_chld_2){
790         hfp_connection->hf_send_chld_2 = 0;
791         hfp_connection->ok_pending = 1;
792         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
793         return;
794     }
795 
796     if (hfp_connection->hf_send_chld_3){
797         hfp_connection->hf_send_chld_3 = 0;
798         hfp_connection->ok_pending = 1;
799         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
800         return;
801     }
802 
803     if (hfp_connection->hf_send_chld_4){
804         hfp_connection->hf_send_chld_4 = 0;
805         hfp_connection->ok_pending = 1;
806         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
807         return;
808     }
809 
810     if (hfp_connection->hf_send_chld_x){
811         hfp_connection->hf_send_chld_x = 0;
812         hfp_connection->ok_pending = 1;
813         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
814         return;
815     }
816 
817     if (hfp_connection->hf_send_dtmf_code){
818         char code = hfp_connection->hf_send_dtmf_code;
819         hfp_connection->hf_send_dtmf_code = 0;
820         hfp_connection->ok_pending = 1;
821         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
822         return;
823     }
824 
825     if (hfp_connection->hf_send_binp){
826         hfp_connection->hf_send_binp = 0;
827         hfp_connection->ok_pending = 1;
828         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
829         return;
830     }
831 
832     if (hfp_connection->hf_send_clcc){
833         hfp_connection->hf_send_clcc = 0;
834         hfp_connection->ok_pending = 1;
835         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
836         return;
837     }
838 
839     if (hfp_connection->hf_send_rrh){
840         hfp_connection->hf_send_rrh = 0;
841         char buffer[20];
842         switch (hfp_connection->hf_send_rrh_command){
843             case '?':
844                 snprintf(buffer, sizeof(buffer), "AT%s?\r",
845                          HFP_RESPONSE_AND_HOLD);
846                 buffer[sizeof(buffer) - 1] = 0;
847                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
848                 return;
849             case '0':
850             case '1':
851             case '2':
852                 snprintf(buffer, sizeof(buffer), "AT%s=%c\r",
853                          HFP_RESPONSE_AND_HOLD,
854                          hfp_connection->hf_send_rrh_command);
855                 buffer[sizeof(buffer) - 1] = 0;
856                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
857                 return;
858             default:
859                 break;
860         }
861         return;
862     }
863 
864     if (hfp_connection->hf_send_cnum){
865         hfp_connection->hf_send_cnum = 0;
866         char buffer[20];
867         snprintf(buffer, sizeof(buffer), "AT%s\r",
868                  HFP_SUBSCRIBER_NUMBER_INFORMATION);
869         buffer[sizeof(buffer) - 1] = 0;
870         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
871         return;
872     }
873 
874     // update HF indicators
875     if (hfp_connection->generic_status_update_bitmap){
876         int i;
877         for (i=0;i<hfp_indicators_nr;i++){
878             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
879                 if (hfp_connection->generic_status_indicators[i].state){
880                     hfp_connection->ok_pending = 1;
881                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
882                     char buffer[30];
883                     snprintf(buffer, sizeof(buffer), "AT%s=%u,%u\r",
884                              HFP_TRANSFER_HF_INDICATOR_STATUS,
885                              hfp_indicators[i],
886                              (unsigned int)hfp_indicators_value[i]);
887                     buffer[sizeof(buffer) - 1] = 0;
888                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
889                 } else {
890                     log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]);
891                 }
892                 return;
893             }
894         }
895     }
896 
897     if (done) return;
898     // deal with disconnect
899     switch (hfp_connection->state){
900         case HFP_W2_DISCONNECT_RFCOMM:
901             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
902             rfcomm_disconnect(hfp_connection->rfcomm_cid);
903             break;
904 
905         default:
906             break;
907     }
908 }
909 
910 static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
911     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
912 
913     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
914 
915     // restore volume settings
916     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
917     hfp_connection->send_speaker_gain = 1;
918     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
919     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
920     hfp_connection->send_microphone_gain = 1;
921     hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
922     // enable all indicators
923     int i;
924     for (i=0;i<hfp_indicators_nr;i++){
925         hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i];
926         hfp_connection->generic_status_indicators[i].state = 1;
927     }
928 }
929 
930 static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){
931 	if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){
932 		// Codec supported, confirm
933 		hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
934 		hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
935 		log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
936 		hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
937 
938 		hfp_connection->hf_send_codec_confirm = true;
939 	} else {
940 		// Codec not supported, send supported codecs
941 		hfp_connection->codec_confirmed = 0;
942 		hfp_connection->suggested_codec = 0;
943 		hfp_connection->negotiated_codec = 0;
944 		hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
945 
946 		hfp_connection->hf_send_supported_codecs = true;
947 	}
948 }
949 
950 static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){
951     switch (hfp_connection->state){
952         case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
953             if (has_codec_negotiation_feature(hfp_connection)){
954                 hfp_connection->state = HFP_NOTIFY_ON_CODECS;
955                 break;
956             }
957             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
958             break;
959 
960         case HFP_W4_NOTIFY_ON_CODECS:
961             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
962             break;
963 
964         case HFP_W4_RETRIEVE_INDICATORS:
965             hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
966             break;
967 
968         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
969             hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
970             break;
971 
972         case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
973             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
974                 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
975                 break;
976             }
977             if (has_hf_indicators_feature(hfp_connection)){
978                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
979                 break;
980             }
981             hfp_ag_slc_established(hfp_connection);
982             break;
983 
984         case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
985             if (has_hf_indicators_feature(hfp_connection)){
986                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
987                 break;
988             }
989             hfp_ag_slc_established(hfp_connection);
990             break;
991 
992         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
993             hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
994             break;
995 
996         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
997             hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
998             break;
999 
1000         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
1001             hfp_ag_slc_established(hfp_connection);
1002             break;
1003         case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
1004             if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
1005                 hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
1006                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1007                 break;
1008             }
1009 
1010             if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
1011                 hfp_connection->change_status_update_for_individual_ag_indicators = 0;
1012                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1013                 break;
1014             }
1015 
1016             switch (hfp_connection->hf_query_operator_state){
1017                 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
1018                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1019                     break;
1020                 case HPF_HF_QUERY_OPERATOR_W4_RESULT:
1021                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
1022                     hfp_emit_network_operator_event(hfp_connection);
1023                     break;
1024                 default:
1025                     break;
1026             }
1027 
1028             if (hfp_connection->enable_extended_audio_gateway_error_report){
1029                 hfp_connection->enable_extended_audio_gateway_error_report = 0;
1030                 break;
1031             }
1032 
1033             switch (hfp_connection->codecs_state){
1034                 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1035                     hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
1036                     break;
1037                 case HFP_CODECS_HF_CONFIRMED_CODEC:
1038                     hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1039                     break;
1040                 default:
1041                     break;
1042             }
1043             voice_recognition_state_machine(hfp_connection);
1044             break;
1045         case HFP_AUDIO_CONNECTION_ESTABLISHED:
1046             voice_recognition_state_machine(hfp_connection);
1047             break;
1048         default:
1049             break;
1050     }
1051 
1052     // done
1053     hfp_connection->ok_pending = 0;
1054     hfp_connection->command = HFP_CMD_NONE;
1055 }
1056 
1057 
1058 static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) {
1059     uint16_t i;
1060     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1061         if (hfp_connection->ag_indicators[i].status_changed) {
1062             if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
1063                 hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
1064             } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
1065                 hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
1066                 // avoid set but not used warning
1067                 (void) hfp_callheld_status;
1068             } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
1069                 hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
1070             }
1071             hfp_connection->ag_indicators[i].status_changed = 0;
1072             hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1073             break;
1074         }
1075     }
1076 }
1077 
1078 static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){
1079     int value;
1080     int i;
1081     switch (hfp_connection->command){
1082         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1083             hfp_connection->command = HFP_CMD_NONE;
1084             hfp_hf_emit_subscriber_information(hfp_connection, 0);
1085             break;
1086         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
1087             hfp_connection->command = HFP_CMD_NONE;
1088             hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
1089             break;
1090         case HFP_CMD_LIST_CURRENT_CALLS:
1091             hfp_connection->command = HFP_CMD_NONE;
1092             hfp_hf_emit_enhanced_call_status(hfp_connection);
1093             break;
1094         case HFP_CMD_SET_SPEAKER_GAIN:
1095             hfp_connection->command = HFP_CMD_NONE;
1096             value = btstack_atoi((char*)hfp_connection->line_buffer);
1097             hfp_hf_speaker_gain = value;
1098             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1099             break;
1100         case HFP_CMD_SET_MICROPHONE_GAIN:
1101             hfp_connection->command = HFP_CMD_NONE;
1102             value = btstack_atoi((char*)hfp_connection->line_buffer);
1103             hfp_hf_microphone_gain = value;
1104             hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1105             break;
1106         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1107             hfp_connection->command = HFP_CMD_NONE;
1108             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1109             break;
1110         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1111             hfp_connection->command = HFP_CMD_NONE;
1112             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION);
1113             break;
1114         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1115             hfp_connection->command = HFP_CMD_NONE;
1116             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION);
1117             break;
1118         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1119             hfp_connection->command = HFP_CMD_NONE;
1120             hfp_connection->ok_pending = 0;
1121             hfp_connection->extended_audio_gateway_error = 0;
1122             hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1123             break;
1124         case HFP_CMD_ERROR:
1125             hfp_connection->ok_pending = 0;
1126             hfp_reset_context_flags(hfp_connection);
1127             hfp_connection->command = HFP_CMD_NONE;
1128 
1129             switch (hfp_connection->state){
1130                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
1131                     switch (hfp_connection->codecs_state){
1132                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1133                             hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
1134                             return;
1135                         default:
1136                             break;
1137                     }
1138                     break;
1139                 default:
1140                     break;
1141             }
1142 
1143 
1144             switch (hfp_connection->vra_status){
1145                 case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
1146                     hfp_connection->vra_status = HFP_VRA_VOICE_RECOGNITION_ACTIVATED;
1147                     break;
1148                 case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_OFF:
1149                     hfp_connection->vra_status = HFP_VRA_ENHANCED_VOICE_RECOGNITION_ACTIVATED;
1150                     break;
1151                 case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
1152                 case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_ACTIVATED:
1153                     hfp_connection->vra_status = HFP_VRA_VOICE_RECOGNITION_OFF;
1154                     break;
1155                 default:
1156                     break;
1157             }
1158 
1159             hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1);
1160             break;
1161         case HFP_CMD_OK:
1162             hfp_hf_switch_on_ok(hfp_connection);
1163             break;
1164         case HFP_CMD_RING:
1165             hfp_connection->command = HFP_CMD_NONE;
1166             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
1167             break;
1168         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1169             hfp_connection->command = HFP_CMD_NONE;
1170             hfp_hf_handle_transfer_ag_indicator_status(hfp_connection);
1171             break;
1172         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1173             hfp_connection->command = HFP_CMD_NONE;
1174             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1175                 hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1176             }
1177             break;
1178     	case HFP_CMD_AG_SUGGESTED_CODEC:
1179             hfp_connection->command = HFP_CMD_NONE;
1180     		hfp_hf_handle_suggested_codec(hfp_connection);
1181 			break;
1182         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
1183             hfp_emit_event(hfp_connection, HFP_SUBEVENT_IN_BAND_RING_TONE, get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE));
1184         default:
1185             break;
1186     }
1187 }
1188 
1189 static int hfp_parser_is_end_of_line(uint8_t byte){
1190 	return (byte == '\n') || (byte == '\r');
1191 }
1192 
1193 static void hfp_hf_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1194     UNUSED(packet_type);    // ok: only called with RFCOMM_DATA_PACKET
1195     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
1196     if (size < 1) return;
1197 
1198     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
1199     if (!hfp_connection) return;
1200 
1201     hfp_log_rfcomm_message("HFP_HF_RX", packet, size);
1202 #ifdef ENABLE_HFP_AT_MESSAGES
1203     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet);
1204 #endif
1205 
1206     // process messages byte-wise
1207     int pos;
1208     for (pos = 0; pos < size; pos++){
1209         hfp_parse(hfp_connection, packet[pos], 1);
1210 
1211         // parse until end of line "\r" or "\n"
1212         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
1213 
1214         hfp_hf_handle_rfcomm_command(hfp_connection);
1215     }
1216     hfp_hf_run_for_context(hfp_connection);
1217 }
1218 
1219 static void hfp_hf_run(void){
1220     btstack_linked_list_iterator_t it;
1221     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1222     while (btstack_linked_list_iterator_has_next(&it)){
1223         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1224         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
1225         hfp_hf_run_for_context(hfp_connection);
1226     }
1227 }
1228 
1229 static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1230     switch (packet_type){
1231         case RFCOMM_DATA_PACKET:
1232             hfp_hf_handle_rfcomm_data(packet_type, channel, packet, size);
1233             break;
1234         case HCI_EVENT_PACKET:
1235             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
1236                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
1237                 hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
1238                 return;
1239             }
1240             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1241             break;
1242         default:
1243             break;
1244     }
1245     hfp_hf_run();
1246 }
1247 
1248 static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1249     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1250     hfp_hf_run();
1251 }
1252 
1253 void hfp_hf_init(uint16_t rfcomm_channel_nr){
1254     hfp_init();
1255     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1256     hfp_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS;
1257     hfp_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1258     hfp_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD;
1259     hfp_codecs_nr = 0;
1260     hfp_hf_speaker_gain = 9;
1261     hfp_hf_microphone_gain = 9;
1262     hfp_indicators_nr = 0;
1263     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1264 
1265     hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler;
1266     hci_add_event_handler(&hfp_hf_hci_event_callback_registration);
1267 
1268     rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
1269 
1270     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
1271     hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler);
1272 }
1273 
1274 void hfp_hf_deinit(void){
1275     hfp_deinit();
1276     (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t));
1277     (void) memset(&hfp_hf_callback, 0, sizeof(btstack_packet_handler_t));
1278     (void) memset(phone_number, 0, sizeof(phone_number));
1279 }
1280 
1281 void hfp_hf_init_codecs(int codecs_nr, const uint8_t * codecs){
1282     if (codecs_nr > HFP_MAX_NUM_CODECS){
1283         log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
1284         return;
1285     }
1286 
1287     hfp_codecs_nr = codecs_nr;
1288     int i;
1289     for (i=0; i<codecs_nr; i++){
1290         hfp_codecs[i] = codecs[i];
1291     }
1292 }
1293 
1294 void hfp_hf_init_supported_features(uint32_t supported_features){
1295     hfp_supported_features = supported_features;
1296 }
1297 
1298 void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){
1299     hfp_indicators_nr = indicators_nr;
1300     int i;
1301     for (i = 0; i < hfp_indicators_nr ; i++){
1302         hfp_indicators[i] = indicators[i];
1303     }
1304 }
1305 
1306 void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
1307     hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
1308 }
1309 
1310 void hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
1311     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1312     if (!hfp_connection) {
1313         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1314         return;
1315     }
1316     hfp_release_service_level_connection(hfp_connection);
1317     hfp_hf_run_for_context(hfp_connection);
1318 }
1319 
1320 static void hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
1321     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1322     if (!hfp_connection) {
1323         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1324         return;
1325     }
1326     hfp_connection->enable_status_update_for_ag_indicators = enable;
1327     hfp_hf_run_for_context(hfp_connection);
1328 }
1329 
1330 void hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1331     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1332 }
1333 
1334 void hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1335     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1336 }
1337 
1338 // TODO: returned ERROR - wrong format
1339 void hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
1340     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1341     if (!hfp_connection) {
1342         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1343         return;
1344     }
1345     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1346     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
1347     hfp_hf_run_for_context(hfp_connection);
1348 }
1349 
1350 void hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
1351     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1352     if (!hfp_connection) {
1353         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1354         return;
1355     }
1356     switch (hfp_connection->hf_query_operator_state){
1357         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1358             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1359             break;
1360         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1361             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1362             break;
1363         default:
1364             break;
1365     }
1366     hfp_hf_run_for_context(hfp_connection);
1367 }
1368 
1369 static void hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
1370     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1371     if (!hfp_connection) {
1372         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1373         return;
1374     }
1375     hfp_connection->enable_extended_audio_gateway_error_report = enable;
1376     hfp_hf_run_for_context(hfp_connection);
1377 }
1378 
1379 
1380 void hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1381     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1382 }
1383 
1384 void hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1385     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1386 }
1387 
1388 static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){
1389     return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) &&  (hfp_supported_features  & (1<<HFP_HFSF_ESCO_S4));
1390 }
1391 
1392 void hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
1393     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1394     if (!hfp_connection) {
1395         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1396         return;
1397     }
1398 
1399     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
1400     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
1401 
1402     if (has_codec_negotiation_feature(hfp_connection)) {
1403         switch (hfp_connection->codecs_state) {
1404             case HFP_CODECS_W4_AG_COMMON_CODEC:
1405                 break;
1406             case HFP_CODECS_EXCHANGED:
1407                 hfp_connection->trigger_codec_exchange = 1;
1408                 break;
1409             default:
1410                 hfp_connection->codec_confirmed = 0;
1411                 hfp_connection->suggested_codec = 0;
1412                 hfp_connection->negotiated_codec = 0;
1413                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
1414                 hfp_connection->trigger_codec_exchange = 1;
1415                 break;
1416         }
1417     } else {
1418         log_info("no codec negotiation feature, use CVSD");
1419         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1420         hfp_connection->suggested_codec = HFP_CODEC_CVSD;
1421         hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
1422         hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
1423         hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection));
1424         hfp_connection->establish_audio_connection = 1;
1425         hfp_connection->state = HFP_W4_SCO_CONNECTED;
1426     }
1427 
1428     hfp_hf_run_for_context(hfp_connection);
1429 }
1430 
1431 void hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
1432     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1433     if (!hfp_connection) {
1434         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1435         return;
1436     }
1437     hfp_release_audio_connection(hfp_connection);
1438     hfp_hf_run_for_context(hfp_connection);
1439 }
1440 
1441 void hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
1442     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1443     if (!hfp_connection) {
1444         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1445         return;
1446     }
1447 
1448     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1449         hfp_connection->hf_answer_incoming_call = 1;
1450         hfp_hf_run_for_context(hfp_connection);
1451     } else {
1452         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
1453     }
1454 }
1455 
1456 void hfp_hf_terminate_call(hci_con_handle_t acl_handle){
1457     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1458     if (!hfp_connection) {
1459         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1460         return;
1461     }
1462     hfp_connection->hf_send_chup = 1;
1463     hfp_hf_run_for_context(hfp_connection);
1464 }
1465 
1466 void hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){
1467     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1468     if (!hfp_connection) {
1469         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1470         return;
1471     }
1472 
1473     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1474         hfp_connection->hf_send_chup = 1;
1475         hfp_hf_run_for_context(hfp_connection);
1476     }
1477 }
1478 
1479 void hfp_hf_user_busy(hci_con_handle_t acl_handle){
1480     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1481     if (!hfp_connection) {
1482         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1483         return;
1484     }
1485 
1486     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1487         hfp_connection->hf_send_chld_0 = 1;
1488         hfp_hf_run_for_context(hfp_connection);
1489     }
1490 }
1491 
1492 void hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){
1493     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1494     if (!hfp_connection) {
1495         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1496         return;
1497     }
1498 
1499     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1500         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1501         hfp_connection->hf_send_chld_1 = 1;
1502         hfp_hf_run_for_context(hfp_connection);
1503     }
1504 }
1505 
1506 void hfp_hf_swap_calls(hci_con_handle_t acl_handle){
1507     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1508     if (!hfp_connection) {
1509         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1510         return;
1511     }
1512 
1513     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1514         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1515         hfp_connection->hf_send_chld_2 = 1;
1516         hfp_hf_run_for_context(hfp_connection);
1517     }
1518 }
1519 
1520 void hfp_hf_join_held_call(hci_con_handle_t acl_handle){
1521     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1522     if (!hfp_connection) {
1523         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1524         return;
1525     }
1526 
1527     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1528         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1529         hfp_connection->hf_send_chld_3 = 1;
1530         hfp_hf_run_for_context(hfp_connection);
1531     }
1532 }
1533 
1534 void hfp_hf_connect_calls(hci_con_handle_t acl_handle){
1535     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1536     if (!hfp_connection) {
1537         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1538         return;
1539     }
1540 
1541     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1542         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1543         hfp_connection->hf_send_chld_4 = 1;
1544         hfp_hf_run_for_context(hfp_connection);
1545     }
1546 }
1547 
1548 void hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){
1549     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1550     if (!hfp_connection) {
1551         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1552         return;
1553     }
1554 
1555     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1556         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1557         hfp_connection->hf_send_chld_x = 1;
1558         hfp_connection->hf_send_chld_x_index = 10 + index;
1559         hfp_hf_run_for_context(hfp_connection);
1560     }
1561 }
1562 
1563 void hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
1564     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1565     if (!hfp_connection) {
1566         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1567         return;
1568     }
1569 
1570     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1571         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1572         hfp_connection->hf_send_chld_x = 1;
1573         hfp_connection->hf_send_chld_x_index = 20 + index;
1574         hfp_hf_run_for_context(hfp_connection);
1575     }
1576 }
1577 
1578 void hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
1579     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1580     if (!hfp_connection) {
1581         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1582         return;
1583     }
1584 
1585     hfp_connection->hf_initiate_outgoing_call = 1;
1586     snprintf(phone_number, sizeof(phone_number), "%s", number);
1587     hfp_hf_run_for_context(hfp_connection);
1588 }
1589 
1590 void hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
1591     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1592     if (!hfp_connection) {
1593         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1594         return;
1595     }
1596 
1597     hfp_connection->hf_initiate_memory_dialing = 1;
1598     hfp_connection->memory_id = memory_id;
1599 
1600     hfp_hf_run_for_context(hfp_connection);
1601 }
1602 
1603 void hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
1604     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1605     if (!hfp_connection) {
1606         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1607         return;
1608     }
1609 
1610     hfp_connection->hf_initiate_redial_last_number = 1;
1611     hfp_hf_run_for_context(hfp_connection);
1612 }
1613 
1614 void hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
1615     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1616     if (!hfp_connection) {
1617         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1618         return;
1619     }
1620 
1621     hfp_connection->hf_activate_call_waiting_notification = 1;
1622     hfp_hf_run_for_context(hfp_connection);
1623 }
1624 
1625 
1626 void hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
1627     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1628     if (!hfp_connection) {
1629         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1630         return;
1631     }
1632 
1633     hfp_connection->hf_deactivate_call_waiting_notification = 1;
1634     hfp_hf_run_for_context(hfp_connection);
1635 }
1636 
1637 
1638 void hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){
1639     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1640     if (!hfp_connection) {
1641         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1642         return;
1643     }
1644 
1645     hfp_connection->hf_activate_calling_line_notification = 1;
1646     hfp_hf_run_for_context(hfp_connection);
1647 }
1648 
1649 void hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
1650     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1651     if (!hfp_connection) {
1652         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1653         return;
1654     }
1655 
1656     hfp_connection->hf_deactivate_calling_line_notification = 1;
1657     hfp_hf_run_for_context(hfp_connection);
1658 }
1659 
1660 
1661 void hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
1662     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1663     if (!hfp_connection) {
1664         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1665         return;
1666     }
1667 
1668     hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1;
1669     hfp_hf_run_for_context(hfp_connection);
1670 }
1671 
1672 void hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
1673     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1674     if (!hfp_connection) {
1675         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1676         return;
1677     }
1678 
1679     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
1680     hfp_hf_run_for_context(hfp_connection);
1681 }
1682 
1683 static bool hfp_hf_enhanced_voice_recognition_supported(hfp_connection_t * hfp_connection){
1684     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS);
1685     int hf = get_bit(hfp_supported_features, HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS);
1686     return hf && ag;
1687 }
1688 
1689 static bool hfp_hf_voice_recognition_supported(hfp_connection_t * hfp_connection){
1690     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION);
1691     int hf = get_bit(hfp_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION);
1692     return hf && ag;
1693 }
1694 
1695 uint8_t hfp_hf_activate_voice_recognition_notification(hci_con_handle_t acl_handle){
1696     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1697     if (!hfp_connection) {
1698         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1699         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1700     }
1701     if (!hfp_hf_voice_recognition_supported(hfp_connection)){
1702         return ERROR_CODE_COMMAND_DISALLOWED;
1703     }
1704 
1705     switch (hfp_connection->vra_status){
1706         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
1707             hfp_emit_event(hfp_connection, HFP_SUBEVENT_VOICE_RECOGNITION_STATUS, 1);
1708             break;
1709         case HFP_VRA_VOICE_RECOGNITION_OFF:
1710             hfp_connection->vra_status = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
1711             hfp_hf_run_for_context(hfp_connection);
1712             break;
1713         default:
1714             return ERROR_CODE_COMMAND_DISALLOWED;
1715     }
1716     return ERROR_CODE_SUCCESS;
1717 }
1718 
1719 
1720 uint8_t hfp_hf_start_enhanced_voice_recognition_session(hci_con_handle_t acl_handle){
1721     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1722     if (!hfp_connection) {
1723         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1724         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1725     }
1726 
1727     if (!hfp_hf_enhanced_voice_recognition_supported(hfp_connection)){
1728         return ERROR_CODE_COMMAND_DISALLOWED;
1729     }
1730 
1731     switch (hfp_connection->vra_status){
1732         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_ACTIVATED:
1733         case HFP_VRA_VOICE_RECOGNITION_OFF:
1734             hfp_connection->vra_status = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_ACTIVATED;
1735             hfp_hf_run_for_context(hfp_connection);
1736             break;
1737         default:
1738             return ERROR_CODE_COMMAND_DISALLOWED;
1739     }
1740     return ERROR_CODE_SUCCESS;
1741 }
1742 
1743 static uint8_t hfp_hf_deactivate_voice_recognition(hfp_connection_t * hfp_connection){
1744     switch (hfp_connection->vra_status){
1745         case HFP_VRA_VOICE_RECOGNITION_OFF:
1746             hfp_emit_event(hfp_connection, HFP_SUBEVENT_VOICE_RECOGNITION_STATUS, 0);
1747             break;
1748         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
1749             hfp_connection->vra_status = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
1750             hfp_hf_run_for_context(hfp_connection);
1751             break;
1752         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_ACTIVATED:
1753             hfp_connection->vra_status = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_OFF;
1754             hfp_hf_run_for_context(hfp_connection);
1755             break;
1756         default:
1757             return ERROR_CODE_COMMAND_DISALLOWED;
1758     }
1759     return ERROR_CODE_SUCCESS;
1760 }
1761 
1762 
1763 uint8_t hfp_hf_deactivate_voice_recognition_notification(hci_con_handle_t acl_handle){
1764     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1765     if (!hfp_connection) {
1766         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1767         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1768     }
1769     if (!hfp_hf_voice_recognition_supported(hfp_connection)){
1770         return ERROR_CODE_COMMAND_DISALLOWED;
1771     }
1772     return hfp_hf_deactivate_voice_recognition(hfp_connection);
1773 }
1774 
1775 
1776 uint8_t hfp_hf_stop_enhanced_voice_recognition_session(hci_con_handle_t acl_handle){
1777     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1778     if (!hfp_connection) {
1779         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1780         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1781     }
1782     if (!hfp_hf_enhanced_voice_recognition_supported(hfp_connection)){
1783         return ERROR_CODE_COMMAND_DISALLOWED;
1784     }
1785     return hfp_hf_deactivate_voice_recognition(hfp_connection);
1786 }
1787 
1788 void hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
1789     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1790     if (!hfp_connection) {
1791         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1792         return;
1793     }
1794 
1795     if (hfp_connection->microphone_gain == gain) return;
1796     if ((gain < 0) || (gain > 15)){
1797         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1798         return;
1799     }
1800     hfp_connection->microphone_gain = gain;
1801     hfp_connection->send_microphone_gain = 1;
1802     hfp_hf_run_for_context(hfp_connection);
1803 }
1804 
1805 void hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
1806     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1807     if (!hfp_connection) {
1808         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1809         return;
1810     }
1811 
1812     if (hfp_connection->speaker_gain == gain) return;
1813     if ((gain < 0) || (gain > 15)){
1814         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1815         return;
1816     }
1817     hfp_connection->speaker_gain = gain;
1818     hfp_connection->send_speaker_gain = 1;
1819     hfp_hf_run_for_context(hfp_connection);
1820 }
1821 
1822 void hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
1823     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1824     if (!hfp_connection) {
1825         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1826         return;
1827     }
1828 
1829     hfp_connection->hf_send_dtmf_code = code;
1830     hfp_hf_run_for_context(hfp_connection);
1831 }
1832 
1833 void hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
1834     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1835     if (!hfp_connection) {
1836         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1837         return;
1838     }
1839     hfp_connection->hf_send_binp = 1;
1840     hfp_hf_run_for_context(hfp_connection);
1841 }
1842 
1843 void hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
1844     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1845     if (!hfp_connection) {
1846         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1847         return;
1848     }
1849     hfp_connection->hf_send_clcc = 1;
1850     hfp_hf_run_for_context(hfp_connection);
1851 }
1852 
1853 
1854 void hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
1855     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1856     if (!hfp_connection) {
1857         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1858         return;
1859     }
1860     hfp_connection->hf_send_rrh = 1;
1861     hfp_connection->hf_send_rrh_command = '?';
1862     hfp_hf_run_for_context(hfp_connection);
1863 }
1864 
1865 void hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
1866     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1867     if (!hfp_connection) {
1868         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1869         return;
1870     }
1871     hfp_connection->hf_send_rrh = 1;
1872     hfp_connection->hf_send_rrh_command = '0';
1873     hfp_hf_run_for_context(hfp_connection);
1874 }
1875 
1876 void hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
1877     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1878     if (!hfp_connection) {
1879         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1880         return;
1881     }
1882     hfp_connection->hf_send_rrh = 1;
1883     hfp_connection->hf_send_rrh_command = '1';
1884     hfp_hf_run_for_context(hfp_connection);
1885 }
1886 
1887 void hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
1888     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1889     if (!hfp_connection) {
1890         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1891         return;
1892     }
1893     hfp_connection->hf_send_rrh = 1;
1894     hfp_connection->hf_send_rrh_command = '2';
1895     hfp_hf_run_for_context(hfp_connection);
1896 }
1897 
1898 void hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
1899     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1900     if (!hfp_connection) {
1901         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1902         return;
1903     }
1904     hfp_connection->hf_send_cnum = 1;
1905     hfp_hf_run_for_context(hfp_connection);
1906 }
1907 
1908 void hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
1909     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1910     if (!hfp_connection) {
1911         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1912         return;
1913     }
1914     // find index for assigned number
1915     int i;
1916     for (i = 0; i < hfp_indicators_nr ; i++){
1917         if (hfp_indicators[i] == assigned_number){
1918             // set value
1919             hfp_indicators_value[i] = value;
1920             // mark for update
1921             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
1922                 hfp_connection->generic_status_update_bitmap |= (1<<i);
1923                 // send update
1924                 hfp_hf_run_for_context(hfp_connection);
1925             }
1926             return;
1927         }
1928     }
1929 }
1930 
1931 int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){
1932     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1933     if (!hfp_connection) {
1934         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1935         return 0;
1936     }
1937     return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
1938 }
1939 
1940 void hfp_hf_create_sdp_record(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr, const char * name, uint16_t supported_features, int wide_band_speech){
1941 	if (!name){
1942 		name = default_hfp_hf_service_name;
1943 	}
1944 	hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name);
1945 
1946 	// Construct SupportedFeatures for SDP bitmap:
1947 	//
1948 	// "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
1949 	//  of the Bits 0 to 4 of the unsolicited result code +BRSF"
1950 	//
1951 	// Wide band speech (bit 5) requires Codec negotiation
1952 	//
1953 	uint16_t sdp_features = supported_features & 0x1f;
1954 	if ( (wide_band_speech != 0) && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){
1955 		sdp_features |= 1 << 5;
1956 	}
1957 
1958     if (supported_features & (1 << HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS)){
1959         sdp_features |= 1 << 6;
1960     }
1961 
1962     if (supported_features & (1 << HFP_HFSF_VOICE_RECOGNITION_TEXT)){
1963         sdp_features |= 1 << 7;
1964     }
1965 
1966 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
1967 	de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
1968 }
1969 
1970 void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
1971 	if (callback == NULL){
1972 		log_error("hfp_hf_register_packet_handler called with NULL callback");
1973 		return;
1974 	}
1975 	hfp_hf_callback = callback;
1976 	hfp_set_hf_callback(callback);
1977 }
1978