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