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