xref: /btstack/src/classic/hfp_hf.c (revision 93ffa3ace7a80f2069078b3bb790b1756391281c)
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     log_info("hfp_hf_run_for_context_service_level_connection state %d\n", hfp_connection->state);
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){
436         return 0;
437     }
438     int done = 0;
439     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
440         hfp_connection->ok_pending = 1;
441         done = 1;
442         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
443         return done;
444     };
445     if (hfp_connection->change_status_update_for_individual_ag_indicators){
446         hfp_connection->ok_pending = 1;
447         done = 1;
448         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
449                 hfp_connection->ag_indicators_status_update_bitmap,
450                 hfp_connection->ag_indicators_nr);
451         return done;
452     }
453 
454     switch (hfp_connection->hf_query_operator_state){
455         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
456             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
457             hfp_connection->ok_pending = 1;
458             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
459             return 1;
460         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
461             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
462             hfp_connection->ok_pending = 1;
463             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
464             return 1;
465         default:
466             break;
467     }
468 
469     if (hfp_connection->enable_extended_audio_gateway_error_report){
470         hfp_connection->ok_pending = 1;
471         done = 1;
472         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
473         return done;
474     }
475 
476     return done;
477 }
478 
479 static int hfp_hf_voice_recognition_state_machine(hfp_connection_t * hfp_connection){
480     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) {
481         return 0;
482     }
483     int done = 0;
484 
485     if (hfp_connection->ok_pending == 1){
486         return 0;
487     }
488     // voice recognition activated from AG
489     if (hfp_connection->command == HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION){
490         switch(hfp_connection->vra_state_requested){
491             case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
492                 // ignore AG command, continue to wait for OK
493                 return 0;
494 
495             case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
496                 // ignore AG command, continue to wait for OK
497                 return 0;
498 
499             default:
500                 if (hfp_connection->ag_vra_status > 0){
501                     hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
502                 } else {
503                     hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
504                 }
505                 break;
506         }
507         hfp_connection->command = HFP_CMD_NONE;
508     }
509 
510 
511     switch (hfp_connection->vra_state_requested){
512         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_OFF:
513         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
514             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
515             if (done != 0){
516                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
517                 hfp_connection->ok_pending = 1;
518             }
519             return 1;
520 
521         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
522             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF;
523             hfp_connection->vra_state_requested = hfp_connection->vra_state;
524             hfp_emit_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS, hfp_connection->vra_state);
525             break;
526 
527         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
528             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
529             if (done != 0){
530                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
531                 hfp_connection->ok_pending = 1;
532                 return 1;
533 
534             }
535             break;
536         case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
537             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_ACTIVATED;
538             hfp_connection->vra_state_requested = hfp_connection->vra_state;
539             hfp_emit_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS, hfp_connection->vra_state);
540             break;
541 
542         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_ACTIVATED:
543             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 2);
544             if (done != 0){
545                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
546                 hfp_connection->ok_pending = 1;
547             }
548             return 1;
549 
550         case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_ACTIVATED:
551             hfp_connection->vra_state = HFP_VRA_ENHANCED_VOICE_RECOGNITION_ACTIVATED;
552             hfp_connection->vra_state_requested = hfp_connection->vra_state;
553             hfp_emit_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS, hfp_connection->vra_state);
554             break;
555 
556         default:
557 
558             break;
559     }
560     return done;
561 }
562 
563 
564 static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
565     if (hfp_connection->ok_pending) return 0;
566 
567     if (hfp_connection->trigger_codec_exchange){
568 		hfp_connection->trigger_codec_exchange = 0;
569 
570 		hfp_connection->ok_pending = 1;
571 		hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
572 		return 1;
573     }
574 
575     if (hfp_connection->hf_send_codec_confirm){
576 		hfp_connection->hf_send_codec_confirm = false;
577 
578 		hfp_connection->ok_pending = 1;
579 		hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
580 		return 1;
581     }
582 
583     if (hfp_connection->hf_send_supported_codecs){
584 		hfp_connection->hf_send_supported_codecs = false;
585 
586 		hfp_connection->ok_pending = 1;
587 		hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
588 		return 1;
589     }
590 
591     return 0;
592 }
593 
594 static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
595     if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
596         (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
597 
598     if (hfp_connection->release_audio_connection){
599         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
600         hfp_connection->release_audio_connection = 0;
601         gap_disconnect(hfp_connection->sco_handle);
602         return 1;
603     }
604 
605     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
606 
607     // run codecs exchange
608     int done = codecs_exchange_state_machine(hfp_connection);
609     if (done) return 1;
610 
611     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
612     if (hfp_connection->establish_audio_connection){
613         hfp_connection->state = HFP_W4_SCO_CONNECTED;
614         hfp_connection->establish_audio_connection = 0;
615         hfp_setup_synchronous_connection(hfp_connection);
616         return 1;
617     }
618     return 0;
619 }
620 
621 
622 static int call_setup_state_machine(hfp_connection_t * hfp_connection){
623 
624 	if (hfp_connection->ok_pending) return 0;
625 
626     if (hfp_connection->hf_answer_incoming_call){
627         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
628         hfp_connection->hf_answer_incoming_call = 0;
629         return 1;
630     }
631     return 0;
632 }
633 
634 static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){
635 
636 	btstack_assert(hfp_connection != NULL);
637 	btstack_assert(hfp_connection->local_role == HFP_ROLE_HF);
638 
639 	// during SDP query, RFCOMM CID is not set
640 	if (hfp_connection->rfcomm_cid == 0) return;
641 
642 	// assert command could be sent
643 	if (hci_can_send_command_packet_now() == 0) return;
644 
645 #ifdef ENABLE_CC256X_ASSISTED_HFP
646     // WBS Disassociate
647     if (hfp_connection->cc256x_send_wbs_disassociate){
648         hfp_connection->cc256x_send_wbs_disassociate = false;
649         hci_send_cmd(&hci_ti_wbs_disassociate);
650         return;
651     }
652     // Write Codec Config
653     if (hfp_connection->cc256x_send_write_codec_config){
654         hfp_connection->cc256x_send_write_codec_config = false;
655         hfp_cc256x_write_codec_config(hfp_connection);
656         return;
657     }
658     // WBS Associate
659     if (hfp_connection->cc256x_send_wbs_associate){
660         hfp_connection->cc256x_send_wbs_associate = false;
661         hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle);
662         return;
663     }
664 #endif
665 #ifdef ENABLE_BCM_PCM_WBS
666     // Enable WBS
667     if (hfp_connection->bcm_send_enable_wbs){
668         hfp_connection->bcm_send_enable_wbs = false;
669         hci_send_cmd(&hci_bcm_enable_wbs, 1, 2);
670         return;
671     }
672     // Write I2S/PCM params
673     if (hfp_connection->bcm_send_write_i2spcm_interface_param){
674         hfp_connection->bcm_send_write_i2spcm_interface_param = false;
675         hfp_bcm_write_i2spcm_interface_param(hfp_connection);
676         return;
677     }
678     // Disable WBS
679     if (hfp_connection->bcm_send_disable_wbs){
680         hfp_connection->bcm_send_disable_wbs = false;
681         hci_send_cmd(&hci_bcm_enable_wbs, 0, 2);
682         return;
683     }
684 #endif
685 #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
686     if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){
687         hfp_finalize_connection_context(hfp_connection);
688         return;
689     }
690 #endif
691 
692     if (hfp_connection->accept_sco){
693         bool incoming_eSCO = hfp_connection->accept_sco == 2;
694         hfp_connection->accept_sco = 0;
695         // notify about codec selection if not done already
696         if (hfp_connection->negotiated_codec == 0){
697             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
698         }
699         hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO);
700         return;
701     }
702 
703     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
704         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
705         return;
706     }
707     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
708     if (!done){
709         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
710     }
711     if (!done){
712         done = hfp_hf_voice_recognition_state_machine(hfp_connection);
713     }
714     if (!done){
715         done = hfp_hf_run_for_audio_connection(hfp_connection);
716     }
717     if (!done){
718         done = call_setup_state_machine(hfp_connection);
719     }
720 
721     // don't send a new command while ok still pending
722     if (hfp_connection->ok_pending) return;
723 
724     if (hfp_connection->send_microphone_gain){
725         hfp_connection->send_microphone_gain = 0;
726         hfp_connection->ok_pending = 1;
727         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
728         return;
729     }
730 
731     if (hfp_connection->send_speaker_gain){
732         hfp_connection->send_speaker_gain = 0;
733         hfp_connection->ok_pending = 1;
734         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
735         return;
736     }
737 
738     if (hfp_connection->hf_deactivate_calling_line_notification){
739         hfp_connection->hf_deactivate_calling_line_notification = 0;
740         hfp_connection->ok_pending = 1;
741         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
742         return;
743     }
744 
745     if (hfp_connection->hf_activate_calling_line_notification){
746         hfp_connection->hf_activate_calling_line_notification = 0;
747         hfp_connection->ok_pending = 1;
748         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
749         return;
750     }
751 
752     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
753         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
754         hfp_connection->ok_pending = 1;
755         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0);
756         return;
757     }
758 
759     if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){
760         hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0;
761         hfp_connection->ok_pending = 1;
762         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1);
763         return;
764     }
765 
766     if (hfp_connection->hf_deactivate_call_waiting_notification){
767         hfp_connection->hf_deactivate_call_waiting_notification = 0;
768         hfp_connection->ok_pending = 1;
769         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
770         return;
771     }
772 
773     if (hfp_connection->hf_activate_call_waiting_notification){
774         hfp_connection->hf_activate_call_waiting_notification = 0;
775         hfp_connection->ok_pending = 1;
776         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
777         return;
778     }
779 
780     if (hfp_connection->hf_initiate_outgoing_call){
781         hfp_connection->hf_initiate_outgoing_call = 0;
782         hfp_connection->ok_pending = 1;
783         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
784         return;
785     }
786 
787     if (hfp_connection->hf_initiate_memory_dialing){
788         hfp_connection->hf_initiate_memory_dialing = 0;
789         hfp_connection->ok_pending = 1;
790         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
791         return;
792     }
793 
794     if (hfp_connection->hf_initiate_redial_last_number){
795         hfp_connection->hf_initiate_redial_last_number = 0;
796         hfp_connection->ok_pending = 1;
797         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
798         return;
799     }
800 
801     if (hfp_connection->hf_send_chup){
802         hfp_connection->hf_send_chup = 0;
803         hfp_connection->ok_pending = 1;
804         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
805         return;
806     }
807 
808     if (hfp_connection->hf_send_chld_0){
809         hfp_connection->hf_send_chld_0 = 0;
810         hfp_connection->ok_pending = 1;
811         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
812         return;
813     }
814 
815     if (hfp_connection->hf_send_chld_1){
816         hfp_connection->hf_send_chld_1 = 0;
817         hfp_connection->ok_pending = 1;
818         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
819         return;
820     }
821 
822     if (hfp_connection->hf_send_chld_2){
823         hfp_connection->hf_send_chld_2 = 0;
824         hfp_connection->ok_pending = 1;
825         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
826         return;
827     }
828 
829     if (hfp_connection->hf_send_chld_3){
830         hfp_connection->hf_send_chld_3 = 0;
831         hfp_connection->ok_pending = 1;
832         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
833         return;
834     }
835 
836     if (hfp_connection->hf_send_chld_4){
837         hfp_connection->hf_send_chld_4 = 0;
838         hfp_connection->ok_pending = 1;
839         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
840         return;
841     }
842 
843     if (hfp_connection->hf_send_chld_x){
844         hfp_connection->hf_send_chld_x = 0;
845         hfp_connection->ok_pending = 1;
846         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
847         return;
848     }
849 
850     if (hfp_connection->hf_send_dtmf_code){
851         char code = hfp_connection->hf_send_dtmf_code;
852         hfp_connection->hf_send_dtmf_code = 0;
853         hfp_connection->ok_pending = 1;
854         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
855         return;
856     }
857 
858     if (hfp_connection->hf_send_binp){
859         hfp_connection->hf_send_binp = 0;
860         hfp_connection->ok_pending = 1;
861         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
862         return;
863     }
864 
865     if (hfp_connection->hf_send_clcc){
866         hfp_connection->hf_send_clcc = 0;
867         hfp_connection->ok_pending = 1;
868         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
869         return;
870     }
871 
872     if (hfp_connection->hf_send_rrh){
873         hfp_connection->hf_send_rrh = 0;
874         char buffer[20];
875         switch (hfp_connection->hf_send_rrh_command){
876             case '?':
877                 snprintf(buffer, sizeof(buffer), "AT%s?\r",
878                          HFP_RESPONSE_AND_HOLD);
879                 buffer[sizeof(buffer) - 1] = 0;
880                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
881                 return;
882             case '0':
883             case '1':
884             case '2':
885                 snprintf(buffer, sizeof(buffer), "AT%s=%c\r",
886                          HFP_RESPONSE_AND_HOLD,
887                          hfp_connection->hf_send_rrh_command);
888                 buffer[sizeof(buffer) - 1] = 0;
889                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
890                 return;
891             default:
892                 break;
893         }
894         return;
895     }
896 
897     if (hfp_connection->hf_send_cnum){
898         hfp_connection->hf_send_cnum = 0;
899         char buffer[20];
900         snprintf(buffer, sizeof(buffer), "AT%s\r",
901                  HFP_SUBSCRIBER_NUMBER_INFORMATION);
902         buffer[sizeof(buffer) - 1] = 0;
903         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
904         return;
905     }
906 
907     // update HF indicators
908     if (hfp_connection->generic_status_update_bitmap){
909         int i;
910         for (i=0;i<hfp_indicators_nr;i++){
911             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
912                 if (hfp_connection->generic_status_indicators[i].state){
913                     hfp_connection->ok_pending = 1;
914                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
915                     char buffer[30];
916                     snprintf(buffer, sizeof(buffer), "AT%s=%u,%u\r",
917                              HFP_TRANSFER_HF_INDICATOR_STATUS,
918                              hfp_indicators[i],
919                              (unsigned int)hfp_indicators_value[i]);
920                     buffer[sizeof(buffer) - 1] = 0;
921                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
922                 } else {
923                     log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]);
924                 }
925                 return;
926             }
927         }
928     }
929 
930     if (done) return;
931     // deal with disconnect
932     switch (hfp_connection->state){
933         case HFP_W2_DISCONNECT_RFCOMM:
934             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
935             rfcomm_disconnect(hfp_connection->rfcomm_cid);
936             break;
937 
938         default:
939             break;
940     }
941 }
942 
943 static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
944     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
945 
946     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
947 
948     // restore volume settings
949     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
950     hfp_connection->send_speaker_gain = 1;
951     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
952     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
953     hfp_connection->send_microphone_gain = 1;
954     hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
955     // enable all indicators
956     int i;
957     for (i=0;i<hfp_indicators_nr;i++){
958         hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i];
959         hfp_connection->generic_status_indicators[i].state = 1;
960     }
961 }
962 
963 static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){
964 	if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){
965 		// Codec supported, confirm
966 		hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
967 		hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
968 		log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
969 		hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
970 
971 		hfp_connection->hf_send_codec_confirm = true;
972 	} else {
973 		// Codec not supported, send supported codecs
974 		hfp_connection->codec_confirmed = 0;
975 		hfp_connection->suggested_codec = 0;
976 		hfp_connection->negotiated_codec = 0;
977 		hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
978 
979 		hfp_connection->hf_send_supported_codecs = true;
980 	}
981 }
982 
983 static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){
984     switch (hfp_connection->state){
985         case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
986             if (has_codec_negotiation_feature(hfp_connection)){
987                 hfp_connection->state = HFP_NOTIFY_ON_CODECS;
988                 break;
989             }
990             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
991             break;
992 
993         case HFP_W4_NOTIFY_ON_CODECS:
994             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
995             break;
996 
997         case HFP_W4_RETRIEVE_INDICATORS:
998             hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
999             break;
1000 
1001         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
1002             hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
1003             break;
1004 
1005         case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
1006             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
1007                 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
1008                 break;
1009             }
1010             if (has_hf_indicators_feature(hfp_connection)){
1011                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
1012                 break;
1013             }
1014             hfp_ag_slc_established(hfp_connection);
1015             break;
1016 
1017         case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
1018             if (has_hf_indicators_feature(hfp_connection)){
1019                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
1020                 break;
1021             }
1022             hfp_ag_slc_established(hfp_connection);
1023             break;
1024 
1025         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
1026             hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
1027             break;
1028 
1029         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
1030             hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
1031             break;
1032 
1033         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
1034             hfp_ag_slc_established(hfp_connection);
1035             break;
1036         case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
1037             if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
1038                 hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
1039                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1040                 break;
1041             }
1042 
1043             if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
1044                 hfp_connection->change_status_update_for_individual_ag_indicators = 0;
1045                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1046                 break;
1047             }
1048 
1049             switch (hfp_connection->hf_query_operator_state){
1050                 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
1051                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1052                     break;
1053                 case HPF_HF_QUERY_OPERATOR_W4_RESULT:
1054                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
1055                     hfp_emit_network_operator_event(hfp_connection);
1056                     break;
1057                 default:
1058                     break;
1059             }
1060 
1061             if (hfp_connection->enable_extended_audio_gateway_error_report){
1062                 hfp_connection->enable_extended_audio_gateway_error_report = 0;
1063                 break;
1064             }
1065 
1066             switch (hfp_connection->codecs_state){
1067                 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1068                     hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
1069                     break;
1070                 case HFP_CODECS_HF_CONFIRMED_CODEC:
1071                     hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1072                     break;
1073                 default:
1074                     break;
1075             }
1076             hfp_hf_voice_recognition_state_machine(hfp_connection);
1077             break;
1078         case HFP_AUDIO_CONNECTION_ESTABLISHED:
1079             hfp_hf_voice_recognition_state_machine(hfp_connection);
1080             break;
1081         default:
1082             break;
1083     }
1084 
1085     // done
1086     hfp_connection->ok_pending = 0;
1087     hfp_connection->command = HFP_CMD_NONE;
1088 }
1089 
1090 
1091 static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) {
1092     uint16_t i;
1093     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1094         if (hfp_connection->ag_indicators[i].status_changed) {
1095             if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
1096                 hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
1097             } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
1098                 hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
1099                 // avoid set but not used warning
1100                 (void) hfp_callheld_status;
1101             } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
1102                 hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
1103             }
1104             hfp_connection->ag_indicators[i].status_changed = 0;
1105             hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1106             break;
1107         }
1108     }
1109 }
1110 
1111 static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){
1112     int value;
1113     int i;
1114     switch (hfp_connection->command){
1115         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1116             hfp_connection->command = HFP_CMD_NONE;
1117             hfp_hf_emit_subscriber_information(hfp_connection, 0);
1118             break;
1119         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
1120             hfp_connection->command = HFP_CMD_NONE;
1121             hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
1122             break;
1123         case HFP_CMD_LIST_CURRENT_CALLS:
1124             hfp_connection->command = HFP_CMD_NONE;
1125             hfp_hf_emit_enhanced_call_status(hfp_connection);
1126             break;
1127         case HFP_CMD_SET_SPEAKER_GAIN:
1128             hfp_connection->command = HFP_CMD_NONE;
1129             value = btstack_atoi((char*)hfp_connection->line_buffer);
1130             hfp_hf_speaker_gain = value;
1131             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1132             break;
1133         case HFP_CMD_SET_MICROPHONE_GAIN:
1134             hfp_connection->command = HFP_CMD_NONE;
1135             value = btstack_atoi((char*)hfp_connection->line_buffer);
1136             hfp_hf_microphone_gain = value;
1137             hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1138             break;
1139         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1140             hfp_connection->command = HFP_CMD_NONE;
1141             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1142             break;
1143         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1144             hfp_connection->command = HFP_CMD_NONE;
1145             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION);
1146             break;
1147         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1148             hfp_connection->command = HFP_CMD_NONE;
1149             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION);
1150             break;
1151         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1152             hfp_connection->command = HFP_CMD_NONE;
1153             hfp_connection->ok_pending = 0;
1154             hfp_connection->extended_audio_gateway_error = 0;
1155             hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1156             break;
1157         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
1158             break;
1159         case HFP_CMD_ERROR:
1160             switch (hfp_connection->state){
1161                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
1162                     switch (hfp_connection->codecs_state){
1163                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1164                             hfp_reset_context_flags(hfp_connection);
1165                             hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
1166                             return;
1167                         default:
1168                             break;
1169                     }
1170                     break;
1171                 default:
1172                     break;
1173             }
1174             // handle error response for voice activation (HF initiated)
1175             switch(hfp_connection->vra_state_requested){
1176                 case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
1177                 case HFP_VRA_VOICE_RECOGNITION_OFF:
1178                     break;
1179                 default:
1180                     hfp_connection->vra_state_requested = hfp_connection->vra_state;
1181                     hfp_emit_voice_recognition_state_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR, hfp_connection->vra_state);
1182                     hfp_reset_context_flags(hfp_connection);
1183                     return;
1184             }
1185             hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1);
1186             hfp_reset_context_flags(hfp_connection);
1187             break;
1188 
1189         case HFP_CMD_OK:
1190             hfp_hf_switch_on_ok(hfp_connection);
1191             break;
1192         case HFP_CMD_RING:
1193             hfp_connection->command = HFP_CMD_NONE;
1194             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
1195             break;
1196         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1197             hfp_connection->command = HFP_CMD_NONE;
1198             hfp_hf_handle_transfer_ag_indicator_status(hfp_connection);
1199             break;
1200         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1201             hfp_connection->command = HFP_CMD_NONE;
1202             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1203                 hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1204             }
1205             break;
1206     	case HFP_CMD_AG_SUGGESTED_CODEC:
1207             hfp_connection->command = HFP_CMD_NONE;
1208     		hfp_hf_handle_suggested_codec(hfp_connection);
1209 			break;
1210         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
1211             hfp_emit_event(hfp_connection, HFP_SUBEVENT_IN_BAND_RING_TONE, get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE));
1212         default:
1213             break;
1214     }
1215 }
1216 
1217 static int hfp_parser_is_end_of_line(uint8_t byte){
1218 	return (byte == '\n') || (byte == '\r');
1219 }
1220 
1221 static void hfp_hf_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){
1222     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
1223     if (size < 1) return;
1224 
1225     hfp_log_rfcomm_message("HFP_HF_RX", packet, size);
1226 #ifdef ENABLE_HFP_AT_MESSAGES
1227     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet);
1228 #endif
1229 
1230     // process messages byte-wise
1231     int pos;
1232     for (pos = 0; pos < size; pos++){
1233         hfp_parse(hfp_connection, packet[pos], 1);
1234         // parse until end of line "\r" or "\n"
1235         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
1236     }
1237     hfp_hf_handle_rfcomm_command(hfp_connection);
1238 }
1239 
1240 static void hfp_hf_run(void){
1241     btstack_linked_list_iterator_t it;
1242     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1243     while (btstack_linked_list_iterator_has_next(&it)){
1244         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1245         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
1246         hfp_hf_run_for_context(hfp_connection);
1247     }
1248 }
1249 
1250 static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1251     hfp_connection_t * hfp_connection;
1252     switch (packet_type){
1253         case RFCOMM_DATA_PACKET:
1254             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
1255             if (!hfp_connection) return;
1256             hfp_hf_handle_rfcomm_data(hfp_connection, packet, size);
1257             hfp_hf_run_for_context(hfp_connection);
1258             return;
1259         case HCI_EVENT_PACKET:
1260             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
1261                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
1262                 hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
1263                 return;
1264             }
1265             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1266             break;
1267         default:
1268             break;
1269     }
1270     hfp_hf_run();
1271 }
1272 
1273 static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1274     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1275     hfp_hf_run();
1276 }
1277 
1278 uint8_t hfp_hf_init(uint16_t rfcomm_channel_nr){
1279     uint8_t status = rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
1280     if (status != ERROR_CODE_SUCCESS){
1281         return status;
1282     }
1283 
1284     hfp_init();
1285     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1286     hfp_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS;
1287     hfp_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1288     hfp_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD;
1289     hfp_codecs_nr = 0;
1290     hfp_hf_speaker_gain = 9;
1291     hfp_hf_microphone_gain = 9;
1292     hfp_indicators_nr = 0;
1293     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1294 
1295     hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler;
1296     hci_add_event_handler(&hfp_hf_hci_event_callback_registration);
1297 
1298     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
1299     hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler);
1300     return ERROR_CODE_SUCCESS;
1301 }
1302 
1303 void hfp_hf_deinit(void){
1304     hfp_deinit();
1305     (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t));
1306     (void) memset(&hfp_hf_callback, 0, sizeof(btstack_packet_handler_t));
1307     (void) memset(phone_number, 0, sizeof(phone_number));
1308 }
1309 
1310 void hfp_hf_init_codecs(int codecs_nr, const uint8_t * codecs){
1311     btstack_assert(codecs_nr < HFP_MAX_NUM_CODECS);
1312 
1313     hfp_codecs_nr = codecs_nr;
1314     int i;
1315     for (i=0; i<codecs_nr; i++){
1316         hfp_codecs[i] = codecs[i];
1317     }
1318 }
1319 
1320 void hfp_hf_init_supported_features(uint32_t supported_features){
1321     hfp_supported_features = supported_features;
1322 }
1323 
1324 void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){
1325     btstack_assert(hfp_indicators_nr < HFP_MAX_NUM_INDICATORS);
1326 
1327     hfp_indicators_nr = indicators_nr;
1328     int i;
1329     for (i = 0; i < hfp_indicators_nr ; i++){
1330         hfp_indicators[i] = indicators[i];
1331     }
1332 }
1333 
1334 uint8_t hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
1335     return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
1336 }
1337 
1338 uint8_t hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
1339     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1340     if (!hfp_connection){
1341         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1342     }
1343     hfp_trigger_release_service_level_connection(hfp_connection);
1344     hfp_hf_run_for_context(hfp_connection);
1345     return ERROR_CODE_SUCCESS;
1346 }
1347 
1348 static uint8_t hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
1349     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1350     if (!hfp_connection) {
1351         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1352     }
1353     hfp_connection->enable_status_update_for_ag_indicators = enable;
1354     hfp_hf_run_for_context(hfp_connection);
1355     return ERROR_CODE_SUCCESS;
1356 }
1357 
1358 uint8_t hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1359     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1360 }
1361 
1362 uint8_t hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1363     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1364 }
1365 
1366 // TODO: returned ERROR - wrong format
1367 uint8_t hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
1368     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1369     if (!hfp_connection) {
1370         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1371     }
1372     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1373     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
1374     hfp_hf_run_for_context(hfp_connection);
1375     return ERROR_CODE_SUCCESS;
1376 }
1377 
1378 uint8_t hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
1379     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1380     if (!hfp_connection) {
1381         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1382     }
1383 
1384     switch (hfp_connection->hf_query_operator_state){
1385         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1386             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1387             break;
1388         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1389             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1390             break;
1391         default:
1392             return ERROR_CODE_COMMAND_DISALLOWED;
1393     }
1394     hfp_hf_run_for_context(hfp_connection);
1395     return ERROR_CODE_SUCCESS;
1396 }
1397 
1398 static uint8_t hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
1399     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1400     if (!hfp_connection) {
1401         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1402     }
1403     hfp_connection->enable_extended_audio_gateway_error_report = enable;
1404     hfp_hf_run_for_context(hfp_connection);
1405     return ERROR_CODE_SUCCESS;
1406 }
1407 
1408 
1409 uint8_t hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1410     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1411 }
1412 
1413 uint8_t hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1414     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1415 }
1416 
1417 static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){
1418     return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) &&  (hfp_supported_features  & (1<<HFP_HFSF_ESCO_S4));
1419 }
1420 
1421 uint8_t hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
1422     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1423     if (!hfp_connection) {
1424         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1425     }
1426 
1427     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
1428         return ERROR_CODE_COMMAND_DISALLOWED;
1429     }
1430 
1431     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){
1432         return ERROR_CODE_COMMAND_DISALLOWED;
1433     }
1434     if (has_codec_negotiation_feature(hfp_connection)) {
1435         switch (hfp_connection->codecs_state) {
1436             case HFP_CODECS_W4_AG_COMMON_CODEC:
1437                 break;
1438             case HFP_CODECS_EXCHANGED:
1439                 hfp_connection->trigger_codec_exchange = 1;
1440                 break;
1441             default:
1442                 hfp_connection->codec_confirmed = 0;
1443                 hfp_connection->suggested_codec = 0;
1444                 hfp_connection->negotiated_codec = 0;
1445                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
1446                 hfp_connection->trigger_codec_exchange = 1;
1447                 break;
1448         }
1449     } else {
1450         log_info("no codec negotiation feature, use CVSD");
1451         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1452         hfp_connection->suggested_codec = HFP_CODEC_CVSD;
1453         hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
1454         hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
1455         hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection));
1456         hfp_connection->establish_audio_connection = 1;
1457         hfp_connection->state = HFP_W4_SCO_CONNECTED;
1458     }
1459 
1460     hfp_hf_run_for_context(hfp_connection);
1461     return ERROR_CODE_SUCCESS;
1462 }
1463 
1464 uint8_t hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
1465     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1466     if (!hfp_connection) {
1467         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1468     }
1469     if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){
1470         if (!hfp_connection->ag_audio_connection_opened_after_vra){
1471             return ERROR_CODE_COMMAND_DISALLOWED;
1472         }
1473     }
1474     uint8_t status = hfp_trigger_release_audio_connection(hfp_connection);
1475     if (status == ERROR_CODE_SUCCESS){
1476         hfp_hf_run_for_context(hfp_connection);
1477     }
1478     return ERROR_CODE_SUCCESS;
1479 }
1480 
1481 uint8_t hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
1482     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1483     if (!hfp_connection) {
1484         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1485     }
1486 
1487     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1488         hfp_connection->hf_answer_incoming_call = 1;
1489         hfp_hf_run_for_context(hfp_connection);
1490     } else {
1491         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
1492         return ERROR_CODE_COMMAND_DISALLOWED;
1493     }
1494     return ERROR_CODE_SUCCESS;
1495 }
1496 
1497 uint8_t hfp_hf_terminate_call(hci_con_handle_t acl_handle){
1498     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1499     if (!hfp_connection) {
1500         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1501     }
1502     hfp_connection->hf_send_chup = 1;
1503     hfp_hf_run_for_context(hfp_connection);
1504     return ERROR_CODE_SUCCESS;
1505 }
1506 
1507 uint8_t hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){
1508     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1509     if (!hfp_connection) {
1510         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1511     }
1512 
1513     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1514         hfp_connection->hf_send_chup = 1;
1515         hfp_hf_run_for_context(hfp_connection);
1516     }
1517     return ERROR_CODE_SUCCESS;
1518 }
1519 
1520 uint8_t hfp_hf_user_busy(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         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1524     }
1525 
1526     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1527         hfp_connection->hf_send_chld_0 = 1;
1528         hfp_hf_run_for_context(hfp_connection);
1529     }
1530     return ERROR_CODE_SUCCESS;
1531 }
1532 
1533 uint8_t hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){
1534     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1535     if (!hfp_connection) {
1536         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1537     }
1538 
1539     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1540         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1541         hfp_connection->hf_send_chld_1 = 1;
1542         hfp_hf_run_for_context(hfp_connection);
1543     }
1544     return ERROR_CODE_SUCCESS;
1545 }
1546 
1547 uint8_t hfp_hf_swap_calls(hci_con_handle_t acl_handle){
1548     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1549     if (!hfp_connection) {
1550         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1551     }
1552 
1553     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1554         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1555         hfp_connection->hf_send_chld_2 = 1;
1556         hfp_hf_run_for_context(hfp_connection);
1557     }
1558     return ERROR_CODE_SUCCESS;
1559 }
1560 
1561 uint8_t hfp_hf_join_held_call(hci_con_handle_t acl_handle){
1562     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1563     if (!hfp_connection) {
1564         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1565     }
1566 
1567     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1568         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1569         hfp_connection->hf_send_chld_3 = 1;
1570         hfp_hf_run_for_context(hfp_connection);
1571     }
1572     return ERROR_CODE_SUCCESS;
1573 }
1574 
1575 uint8_t hfp_hf_connect_calls(hci_con_handle_t acl_handle){
1576     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1577     if (!hfp_connection) {
1578         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1579     }
1580 
1581     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1582         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1583         hfp_connection->hf_send_chld_4 = 1;
1584         hfp_hf_run_for_context(hfp_connection);
1585     }
1586     return ERROR_CODE_SUCCESS;
1587 }
1588 
1589 uint8_t hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){
1590     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1591     if (!hfp_connection) {
1592         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1593     }
1594 
1595     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1596         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1597         hfp_connection->hf_send_chld_x = 1;
1598         hfp_connection->hf_send_chld_x_index = 10 + index;
1599         hfp_hf_run_for_context(hfp_connection);
1600     }
1601     return ERROR_CODE_SUCCESS;
1602 }
1603 
1604 uint8_t hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
1605     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1606     if (!hfp_connection) {
1607         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1608     }
1609 
1610     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1611         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1612         hfp_connection->hf_send_chld_x = 1;
1613         hfp_connection->hf_send_chld_x_index = 20 + index;
1614         hfp_hf_run_for_context(hfp_connection);
1615     }
1616     return ERROR_CODE_SUCCESS;
1617 }
1618 
1619 uint8_t hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
1620     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1621     if (!hfp_connection) {
1622         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1623     }
1624 
1625     hfp_connection->hf_initiate_outgoing_call = 1;
1626     snprintf(phone_number, sizeof(phone_number), "%s", number);
1627     hfp_hf_run_for_context(hfp_connection);
1628     return ERROR_CODE_SUCCESS;
1629 }
1630 
1631 uint8_t hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
1632     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1633     if (!hfp_connection) {
1634         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1635     }
1636 
1637     hfp_connection->hf_initiate_memory_dialing = 1;
1638     hfp_connection->memory_id = memory_id;
1639 
1640     hfp_hf_run_for_context(hfp_connection);
1641     return ERROR_CODE_SUCCESS;
1642 }
1643 
1644 uint8_t hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
1645     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1646     if (!hfp_connection) {
1647         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1648     }
1649 
1650     hfp_connection->hf_initiate_redial_last_number = 1;
1651     hfp_hf_run_for_context(hfp_connection);
1652     return ERROR_CODE_SUCCESS;
1653 }
1654 
1655 uint8_t hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
1656     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1657     if (!hfp_connection) {
1658         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1659     }
1660 
1661     hfp_connection->hf_activate_call_waiting_notification = 1;
1662     hfp_hf_run_for_context(hfp_connection);
1663     return ERROR_CODE_SUCCESS;
1664 }
1665 
1666 
1667 uint8_t hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
1668     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1669     if (!hfp_connection) {
1670         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1671     }
1672 
1673     hfp_connection->hf_deactivate_call_waiting_notification = 1;
1674     hfp_hf_run_for_context(hfp_connection);
1675     return ERROR_CODE_SUCCESS;
1676 }
1677 
1678 
1679 uint8_t hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){
1680     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1681     if (!hfp_connection) {
1682         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1683     }
1684 
1685     hfp_connection->hf_activate_calling_line_notification = 1;
1686     hfp_hf_run_for_context(hfp_connection);
1687     return ERROR_CODE_SUCCESS;
1688 }
1689 
1690 uint8_t hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
1691     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1692     if (!hfp_connection) {
1693         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1694     }
1695 
1696     hfp_connection->hf_deactivate_calling_line_notification = 1;
1697     hfp_hf_run_for_context(hfp_connection);
1698     return ERROR_CODE_SUCCESS;
1699 }
1700 
1701 
1702 uint8_t hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
1703     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1704     if (!hfp_connection) {
1705         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1706     }
1707 
1708     hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1;
1709     hfp_hf_run_for_context(hfp_connection);
1710     return ERROR_CODE_SUCCESS;
1711 }
1712 
1713 uint8_t hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
1714     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1715     if (!hfp_connection) {
1716         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1717     }
1718 
1719     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
1720     hfp_hf_run_for_context(hfp_connection);
1721     return ERROR_CODE_SUCCESS;
1722 }
1723 
1724 static bool hfp_hf_enhanced_voice_recognition_supported(hfp_connection_t * hfp_connection){
1725     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS);
1726     int hf = get_bit(hfp_supported_features, HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS);
1727     return hf && ag;
1728 }
1729 
1730 static bool hfp_hf_voice_recognition_supported(hfp_connection_t * hfp_connection){
1731     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION);
1732     int hf = get_bit(hfp_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION);
1733     return hf && ag;
1734 }
1735 
1736 uint8_t hfp_hf_activate_voice_recognition(hci_con_handle_t acl_handle){
1737     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1738     if (!hfp_connection) {
1739         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1740     }
1741     if (!hfp_hf_voice_recognition_supported(hfp_connection)){
1742         return ERROR_CODE_COMMAND_DISALLOWED;
1743     }
1744 
1745     switch (hfp_connection->vra_state){
1746         case HFP_VRA_VOICE_RECOGNITION_OFF:
1747             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1748             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED;
1749             break;
1750         default:
1751             return ERROR_CODE_COMMAND_DISALLOWED;
1752     }
1753 
1754     hfp_hf_run_for_context(hfp_connection);
1755     return ERROR_CODE_SUCCESS;
1756 }
1757 
1758 uint8_t hfp_hf_deactivate_voice_recognition(hci_con_handle_t acl_handle){
1759     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1760     if (!hfp_connection) {
1761         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1762     }
1763     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
1764         return ERROR_CODE_COMMAND_DISALLOWED;
1765     }
1766     if (!hfp_hf_voice_recognition_supported(hfp_connection)){
1767         return ERROR_CODE_COMMAND_DISALLOWED;
1768     }
1769     switch (hfp_connection->vra_state){
1770         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
1771             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1772             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF;
1773             break;
1774         default:
1775             return ERROR_CODE_COMMAND_DISALLOWED;
1776     }
1777 
1778     hfp_hf_run_for_context(hfp_connection);
1779     return ERROR_CODE_SUCCESS;
1780 }
1781 
1782 uint8_t hfp_hf_start_enhanced_voice_recognition_session(hci_con_handle_t acl_handle){
1783     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1784     if (!hfp_connection) {
1785         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1786     }
1787     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
1788         return ERROR_CODE_COMMAND_DISALLOWED;
1789     }
1790     if (!hfp_hf_enhanced_voice_recognition_supported(hfp_connection)){
1791         return ERROR_CODE_COMMAND_DISALLOWED;
1792     }
1793 
1794     switch (hfp_connection->vra_state){
1795         case HFP_VRA_VOICE_RECOGNITION_OFF:
1796             hfp_connection->vra_state = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_ACTIVATED;
1797             break;
1798         default:
1799             return ERROR_CODE_COMMAND_DISALLOWED;
1800     }
1801 
1802     hfp_hf_run_for_context(hfp_connection);
1803     return ERROR_CODE_SUCCESS;
1804 }
1805 
1806 uint8_t hfp_hf_stop_enhanced_voice_recognition_session(hci_con_handle_t acl_handle){
1807     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1808     if (!hfp_connection) {
1809         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1810     }
1811 
1812     if (!hfp_hf_enhanced_voice_recognition_supported(hfp_connection)){
1813         return ERROR_CODE_COMMAND_DISALLOWED;
1814     }
1815 
1816     switch (hfp_connection->vra_state){
1817         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_ACTIVATED:
1818             hfp_connection->vra_state = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_OFF;
1819             hfp_hf_run_for_context(hfp_connection);
1820             break;
1821         default:
1822             return ERROR_CODE_COMMAND_DISALLOWED;
1823     }
1824 
1825     hfp_hf_run_for_context(hfp_connection);
1826     return ERROR_CODE_SUCCESS;
1827 }
1828 
1829 uint8_t hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
1830     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1831     if (!hfp_connection) {
1832         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1833     }
1834 
1835     if (hfp_connection->microphone_gain == gain) {
1836         return ERROR_CODE_SUCCESS;
1837     }
1838 
1839     if ((gain < 0) || (gain > 15)){
1840         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1841         return ERROR_CODE_COMMAND_DISALLOWED;
1842     }
1843 
1844     hfp_connection->microphone_gain = gain;
1845     hfp_connection->send_microphone_gain = 1;
1846     hfp_hf_run_for_context(hfp_connection);
1847     return ERROR_CODE_SUCCESS;
1848 }
1849 
1850 uint8_t hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
1851     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1852     if (!hfp_connection) {
1853         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1854     }
1855 
1856     if (hfp_connection->speaker_gain == gain){
1857         return ERROR_CODE_SUCCESS;
1858     }
1859 
1860     if ((gain < 0) || (gain > 15)){
1861         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1862         return ERROR_CODE_COMMAND_DISALLOWED;
1863     }
1864 
1865     hfp_connection->speaker_gain = gain;
1866     hfp_connection->send_speaker_gain = 1;
1867     hfp_hf_run_for_context(hfp_connection);
1868     return ERROR_CODE_SUCCESS;
1869 }
1870 
1871 uint8_t hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
1872     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1873     if (!hfp_connection) {
1874         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1875     }
1876     hfp_connection->hf_send_dtmf_code = code;
1877     hfp_hf_run_for_context(hfp_connection);
1878     return ERROR_CODE_SUCCESS;
1879 }
1880 
1881 uint8_t hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
1882     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1883     if (!hfp_connection) {
1884         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1885     }
1886     hfp_connection->hf_send_binp = 1;
1887     hfp_hf_run_for_context(hfp_connection);
1888     return ERROR_CODE_SUCCESS;
1889 }
1890 
1891 uint8_t hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
1892     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1893     if (!hfp_connection) {
1894         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1895     }
1896     hfp_connection->hf_send_clcc = 1;
1897     hfp_hf_run_for_context(hfp_connection);
1898     return ERROR_CODE_SUCCESS;
1899 }
1900 
1901 
1902 uint8_t hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
1903     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1904     if (!hfp_connection) {
1905         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1906     }
1907     hfp_connection->hf_send_rrh = 1;
1908     hfp_connection->hf_send_rrh_command = '?';
1909     hfp_hf_run_for_context(hfp_connection);
1910     return ERROR_CODE_SUCCESS;
1911 }
1912 
1913 uint8_t hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
1914     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1915     if (!hfp_connection) {
1916         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1917     }
1918     hfp_connection->hf_send_rrh = 1;
1919     hfp_connection->hf_send_rrh_command = '0';
1920     hfp_hf_run_for_context(hfp_connection);
1921     return ERROR_CODE_SUCCESS;
1922 }
1923 
1924 uint8_t hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
1925     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1926     if (!hfp_connection) {
1927         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1928     }
1929     hfp_connection->hf_send_rrh = 1;
1930     hfp_connection->hf_send_rrh_command = '1';
1931     hfp_hf_run_for_context(hfp_connection);
1932     return ERROR_CODE_SUCCESS;
1933 }
1934 
1935 uint8_t hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
1936     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1937     if (!hfp_connection) {
1938         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1939     }
1940     hfp_connection->hf_send_rrh = 1;
1941     hfp_connection->hf_send_rrh_command = '2';
1942     hfp_hf_run_for_context(hfp_connection);
1943     return ERROR_CODE_SUCCESS;
1944 }
1945 
1946 uint8_t hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
1947     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1948     if (!hfp_connection) {
1949         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1950     }
1951     hfp_connection->hf_send_cnum = 1;
1952     hfp_hf_run_for_context(hfp_connection);
1953     return ERROR_CODE_SUCCESS;
1954 }
1955 
1956 uint8_t hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
1957     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1958     if (!hfp_connection) {
1959         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1960     }
1961     // find index for assigned number
1962     int i;
1963     for (i = 0; i < hfp_indicators_nr ; i++){
1964         if (hfp_indicators[i] == assigned_number){
1965             // set value
1966             hfp_indicators_value[i] = value;
1967             // mark for update
1968             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
1969                 hfp_connection->generic_status_update_bitmap |= (1<<i);
1970                 // send update
1971                 hfp_hf_run_for_context(hfp_connection);
1972             }
1973             return ERROR_CODE_SUCCESS;
1974         }
1975     }
1976     return ERROR_CODE_SUCCESS;
1977 }
1978 
1979 int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){
1980     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1981     if (!hfp_connection) {
1982         return 0;
1983     }
1984     return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
1985 }
1986 
1987 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){
1988 	if (!name){
1989 		name = default_hfp_hf_service_name;
1990 	}
1991 	hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name);
1992 
1993 	// Construct SupportedFeatures for SDP bitmap:
1994 	//
1995 	// "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
1996 	//  of the Bits 0 to 4 of the unsolicited result code +BRSF"
1997 	//
1998 	// Wide band speech (bit 5) requires Codec negotiation
1999 	//
2000 	uint16_t sdp_features = supported_features & 0x1f;
2001 	if ( (wide_band_speech != 0) && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){
2002 		sdp_features |= 1 << 5;
2003 	}
2004 
2005     if (supported_features & (1 << HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS)){
2006         sdp_features |= 1 << 6;
2007     }
2008 
2009     if (supported_features & (1 << HFP_HFSF_VOICE_RECOGNITION_TEXT)){
2010         sdp_features |= 1 << 7;
2011     }
2012 
2013 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
2014 	de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
2015 }
2016 
2017 void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
2018 	btstack_assert(callback != NULL);
2019 
2020 	hfp_hf_callback = callback;
2021 	hfp_set_hf_callback(callback);
2022 }
2023