xref: /btstack/src/classic/hfp.h (revision 5611a760af48d1ce1beea59c7908be73bd2393f1)
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 // *****************************************************************************
39 //
40 //  HFP Hands-Free (HF) unit and Audio-Gateway Commons
41 //
42 // *****************************************************************************
43 
44 
45 #ifndef btstack_hfp_h
46 #define btstack_hfp_h
47 
48 #include "hci.h"
49 #include "classic/sdp_query_rfcomm.h"
50 
51 #if defined __cplusplus
52 extern "C" {
53 #endif
54 
55 
56 /* HF Supported Features:
57 0: EC and/or NR function
58 1: Three-way calling
59 2: CLI presentation capability
60 3: Voice recognition activation
61 4: Remote volume control
62 5: Enhanced call status
63 6: Enhanced call control
64 7: Codec negotiation
65 8: HF Indicators
66 9: eSCO S4 (and T2) Settings Supported
67 10-31: Reserved for future definition
68 */
69 #define HFP_HFSF_EC_NR_FUNCTION     0
70 #define HFP_HFSF_THREE_WAY_CALLING  1
71 #define HFP_HFSF_VOICE_RECOGNITION_FUNCTION 3
72 #define HFP_HFSF_CODEC_NEGOTIATION  7
73 #define HFP_HFSF_HF_INDICATORS      8
74 #define HFP_HFSF_ESCO_S4            9
75 
76 /* AG Supported Features:
77 0: Three-way calling
78 1: EC and/or NR function
79 2: Voice recognition function
80 3: In-band ring tone capability
81 4: Attach a number to a voice tag
82 5: Ability to reject a call
83 6: Enhanced call status
84 7: Enhanced call control
85 8: Extended Error Result Codes
86 9: Codec negotiation
87 10: HF Indicators
88 11: eSCO S4 (and T2) Settings Supported
89 12-31: Reserved for future definition
90 */
91 #define HFP_AGSF_THREE_WAY_CALLING  0
92 #define HFP_AGSF_EC_NR_FUNCTION     1
93 #define HFP_AGSF_VOICE_RECOGNITION_FUNCTION     2
94 #define HFP_AGSF_IN_BAND_RING_TONE  3
95 #define HFP_AGSF_CODEC_NEGOTIATION  9
96 #define HFP_AGSF_HF_INDICATORS      10
97 #define HFP_AGSF_ESCO_S4            11
98 
99 #define HFP_DEFAULT_HF_SUPPORTED_FEATURES 0x0000
100 #define HFP_DEFAULT_AG_SUPPORTED_FEATURES 0x0009
101 
102 #define HFP_MAX_NUM_CODECS 20
103 #define HFP_MAX_NUM_AG_INDICATORS 20
104 #define HFP_MAX_NUM_HF_INDICATORS 20
105 #define HFP_MAX_INDICATOR_DESC_SIZE 20
106 
107 #define HFP_SUPPORTED_FEATURES "+BRSF"
108 #define HFP_AVAILABLE_CODECS "+BAC"
109 #define HFP_INDICATOR "+CIND"
110 #define HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS "+CMER"
111 #define HFP_ENABLE_CLIP "+CLIP"
112 #define HFP_ENABLE_CALL_WAITING_NOTIFICATION "+CCWA"
113 #define HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS "+BIA" // +BIA:<enabled>,,<enabled>,,,<enabled>
114 #define HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES "+CHLD"
115 #define HFP_GENERIC_STATUS_INDICATOR "+BIND"
116 #define HFP_TRANSFER_AG_INDICATOR_STATUS "+CIEV" // +CIEV: <index>,<value>
117 #define HFP_TRANSFER_HF_INDICATOR_STATUS "+BIEV" // +BIEC: <index>,<value>
118 #define HFP_QUERY_OPERATOR_SELECTION "+COPS"     // +COPS: <mode>,0,<opearator>
119 #define HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR "+CMEE"
120 #define HFP_EXTENDED_AUDIO_GATEWAY_ERROR "+CME ERROR"
121 #define HFP_TRIGGER_CODEC_CONNECTION_SETUP "+BCC"
122 #define HFP_CONFIRM_COMMON_CODEC "+BCS"
123 #define HFP_CALL_ANSWERED "ATA"
124 #define HFP_HANG_UP_CALL "+CHUP"
125 #define HFP_CHANGE_IN_BAND_RING_TONE_SETTING "+BSIR"
126 #define HFP_CALL_PHONE_NUMBER "ATD"
127 #define HFP_REDIAL_LAST_NUMBER "+BLDN"
128 #define HFP_TURN_OFF_EC_AND_NR "+NREC" // EC (Echo CAnceling), NR (Noise Reduction)
129 #define HFP_ACTIVATE_VOICE_RECOGNITION "+BVRA" // EC (Echo CAnceling), NR (Noise Reduction)
130 #define HFP_SET_MICROPHONE_GAIN  "+VGM"
131 #define HFP_SET_SPEAKER_GAIN     "+VGS"
132 #define HFP_PHONE_NUMBER_FOR_VOICE_TAG "+BINP"
133 #define HFP_TRANSMIT_DTMF_CODES  "+VTS"
134 #define HFP_SUBSCRIBER_NUMBER_INFORMATION "+CNUM"
135 #define HFP_LIST_CURRENT_CALLS "+CLCC"
136 #define HFP_RESPONSE_AND_HOLD "+BTRH"
137 
138 #define HFP_OK "OK"
139 #define HFP_ERROR "ERROR"
140 #define HFP_RING "RING"
141 
142 // Codecs
143 #define HFP_CODEC_CVSD 0x01
144 #define HFP_CODEC_MSBC 0x02
145 
146 typedef enum {
147     HFP_CMD_NONE = 0,
148     HFP_CMD_ERROR,
149     HFP_CMD_UNKNOWN,
150     HFP_CMD_OK,
151     HFP_CMD_RING,
152     HFP_CMD_SUPPORTED_FEATURES,
153     HFP_CMD_AVAILABLE_CODECS,
154 
155     HFP_CMD_RETRIEVE_AG_INDICATORS,
156     HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS,
157 
158     HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE,
159     HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE,
160     HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES,
161     HFP_CMD_ENABLE_CLIP,
162     HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION,
163 
164     HFP_CMD_LIST_GENERIC_STATUS_INDICATORS,
165     HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS,
166     HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE,
167     HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS,
168 
169     HFP_CMD_TRANSFER_AG_INDICATOR_STATUS,
170 
171     HFP_CMD_QUERY_OPERATOR_SELECTION_NAME,
172     HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT,
173 
174     HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR,
175     HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR,
176     HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP,
177     HFP_CMD_AG_SEND_COMMON_CODEC,
178     HFP_CMD_AG_SUGGESTED_CODEC,
179     HFP_CMD_HF_CONFIRMED_CODEC,
180     HFP_CMD_CALL_ANSWERED,
181     HFP_CMD_CALL_HOLD,
182     HFP_CMD_AG_ANSWER_CALL,
183     HFP_CMD_HANG_UP_CALL,
184     HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING,
185     HFP_CMD_CALL_PHONE_NUMBER,
186     HFP_CMD_REDIAL_LAST_NUMBER,
187     HFP_CMD_TURN_OFF_EC_AND_NR,
188     HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION,
189     HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION,
190     HFP_CMD_HF_REQUEST_PHONE_NUMBER,
191     HFP_CMD_AG_SENT_PHONE_NUMBER,
192     HFP_CMD_TRANSMIT_DTMF_CODES,
193     HFP_CMD_SET_MICROPHONE_GAIN,
194     HFP_CMD_SET_SPEAKER_GAIN,
195     HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION,
196     HFP_CMD_LIST_CURRENT_CALLS,
197     HFP_CMD_RESPONSE_AND_HOLD_QUERY,
198     HFP_CMD_RESPONSE_AND_HOLD_COMMAND,
199     HFP_CMD_RESPONSE_AND_HOLD_STATUS,
200     HFP_CMD_HF_INDICATOR_STATUS
201 } hfp_command_t;
202 
203 
204 typedef enum {
205     HFP_CME_ERROR_AG_FAILURE = 0,
206     HFP_CME_ERROR_NO_CONNECTION_TO_PHONE,
207     HFP_CME_ERROR_2,
208     HFP_CME_ERROR_OPERATION_NOT_ALLOWED,
209     HFP_CME_ERROR_OPERATION_NOT_SUPPORTED,
210     HFP_CME_ERROR_PH_SIM_PIN_REQUIRED,
211     HFP_CME_ERROR_6,
212     HFP_CME_ERROR_7,
213     HFP_CME_ERROR_8,
214     HFP_CME_ERROR_9,
215     HFP_CME_ERROR_SIM_NOT_INSERTED,
216     HFP_CME_ERROR_SIM_PIN_REQUIRED,
217     HFP_CME_ERROR_SIM_PUK_REQUIRED,
218     HFP_CME_ERROR_SIM_FAILURE,
219     HFP_CME_ERROR_SIM_BUSY,
220     HFP_CME_ERROR_15,
221     HFP_CME_ERROR_INCORRECT_PASSWORD,
222     HFP_CME_ERROR_SIM_PIN2_REQUIRED,
223     HFP_CME_ERROR_SIM_PUK2_REQUIRED,
224     HFP_CME_ERROR_19,
225     HFP_CME_ERROR_MEMORY_FULL,
226     HFP_CME_ERROR_INVALID_INDEX,
227     HFP_CME_ERROR_22,
228     HFP_CME_ERROR_MEMORY_FAILURE,
229     HFP_CME_ERROR_TEXT_STRING_TOO_LONG,
230     HFP_CME_ERROR_INVALID_CHARACTERS_IN_TEXT_STRING,
231     HFP_CME_ERROR_DIAL_STRING_TOO_LONG,
232     HFP_CME_ERROR_INVALID_CHARACTERS_IN_DIAL_STRING,
233     HFP_CME_ERROR_28,
234     HFP_CME_ERROR_29,
235     HFP_CME_ERROR_NO_NETWORK_SERVICE,
236     HFP_CME_ERROR_NETWORK_TIMEOUT,
237     HFP_CME_ERROR_NETWORK_NOT_ALLOWED_EMERGENCY_CALLS_ONLY
238 } hfp_cme_error_t;
239 
240 typedef enum {
241     HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS = 0,
242     HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT
243 } hfp_call_status_t;
244 
245 typedef enum {
246     HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS = 0,
247     HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS,
248     HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE,
249     HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE
250 } hfp_callsetup_status_t;
251 
252 typedef enum {
253     HFP_CALLHELD_STATUS_NO_CALLS_HELD = 0,
254     HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED,
255     HFP_CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS
256 } hfp_callheld_status_t;
257 
258 
259 typedef enum {
260     HFP_AG_INCOMING_CALL,
261     HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG,
262     HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF,
263     HFP_AG_AUDIO_CONNECTION_ESTABLISHED,
264     HFP_AG_OUTGOING_CALL_INITIATED,
265     HFP_AG_OUTGOING_CALL_REJECTED,
266     HFP_AG_OUTGOING_CALL_ACCEPTED,
267     HFP_AG_OUTGOING_CALL_RINGING,
268     HFP_AG_OUTGOING_CALL_ESTABLISHED,
269     HFP_AG_OUTGOING_REDIAL_INITIATED,
270     HFP_AG_HELD_CALL_JOINED_BY_AG,
271     HFP_AG_TERMINATE_CALL_BY_AG,
272     HFP_AG_TERMINATE_CALL_BY_HF,
273     HFP_AG_CALL_DROPPED,
274     HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG,
275     HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG,
276     HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG,
277     HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF,
278     HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF,
279     HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF,
280     HFP_AG_CALL_HOLD_USER_BUSY,
281     HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL,
282     HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL,
283     HFP_AG_CALL_HOLD_ADD_HELD_CALL,
284     HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS,
285     HFP_AG_SET_CLIP
286 } hfp_ag_call_event_t;
287 
288 
289 typedef enum {
290     HFP_PARSER_CMD_HEADER = 0,
291     HFP_PARSER_CMD_SEQUENCE,
292     HFP_PARSER_SECOND_ITEM,
293     HFP_PARSER_THIRD_ITEM
294 } hfp_parser_state_t;
295 
296 
297 typedef enum {
298     HFP_IDLE = 0, //0
299     HFP_SDP_QUERY_RFCOMM_CHANNEL,
300     HFP_W4_SDP_EVENT_QUERY_COMPLETE,
301     HFP_W4_RFCOMM_CONNECTED,
302 
303     HFP_EXCHANGE_SUPPORTED_FEATURES,
304     HFP_W4_EXCHANGE_SUPPORTED_FEATURES, // 5
305 
306     HFP_NOTIFY_ON_CODECS,
307     HFP_W4_NOTIFY_ON_CODECS,
308 
309     HFP_RETRIEVE_INDICATORS,
310     HFP_W4_RETRIEVE_INDICATORS,
311 
312     HFP_RETRIEVE_INDICATORS_STATUS, // 10
313     HFP_W4_RETRIEVE_INDICATORS_STATUS,
314 
315     HFP_ENABLE_INDICATORS_STATUS_UPDATE,
316     HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE,
317 
318     HFP_RETRIEVE_CAN_HOLD_CALL,
319     HFP_W4_RETRIEVE_CAN_HOLD_CALL, // 15
320 
321     HFP_LIST_GENERIC_STATUS_INDICATORS,
322     HFP_W4_LIST_GENERIC_STATUS_INDICATORS,
323 
324     HFP_RETRIEVE_GENERIC_STATUS_INDICATORS,
325     HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS,
326 
327     HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS,
328     HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS,
329 
330     HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED,
331 
332     HFP_W2_CONNECT_SCO,
333     HFP_W4_SCO_CONNECTED,
334 
335     HFP_AUDIO_CONNECTION_ESTABLISHED,
336 
337     HFP_W2_DISCONNECT_SCO,
338     HFP_W4_SCO_DISCONNECTED,
339 
340     HFP_W2_DISCONNECT_RFCOMM,
341     HFP_W4_RFCOMM_DISCONNECTED,
342     HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART,
343     HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN
344 } hfp_state_t;
345 
346 typedef enum {
347     HFP_CODECS_IDLE,
348     HFP_CODECS_RECEIVED_LIST,
349     HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE,
350     HFP_CODECS_W4_AG_COMMON_CODEC,
351     HFP_CODECS_AG_SENT_COMMON_CODEC,
352     HFP_CODECS_AG_RESEND_COMMON_CODEC,
353     HFP_CODECS_HF_CONFIRMED_CODEC,
354     HFP_CODECS_EXCHANGED,
355     HFP_CODECS_ERROR
356 } hfp_codecs_state_t;
357 
358 typedef enum {
359     HFP_CALL_IDLE,
360     HFP_CALL_TRIGGER_AUDIO_CONNECTION,
361     HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING,
362     HFP_CALL_RINGING,
363     HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE,
364     HFP_CALL_ACTIVE,
365     HFP_CALL_W2_SEND_CALL_WAITING,
366     HFP_CALL_W4_CHLD,
367     HFP_CALL_OUTGOING_INITIATED,
368     HFP_CALL_OUTGOING_DIALING,
369     HFP_CALL_OUTGOING_RINGING
370 } hfp_call_state_t;
371 
372 typedef enum{
373     HFP_ENHANCED_CALL_DIR_OUTGOING,
374     HFP_ENHANCED_CALL_DIR_INCOMING
375 } hfp_enhanced_call_dir_t;
376 
377 typedef enum{
378     HFP_ENHANCED_CALL_STATUS_ACTIVE,
379     HFP_ENHANCED_CALL_STATUS_HELD,
380     HFP_ENHANCED_CALL_STATUS_OUTGOING_DIALING,
381     HFP_ENHANCED_CALL_STATUS_OUTGOING_ALERTING,
382     HFP_ENHANCED_CALL_STATUS_INCOMING,
383     HFP_ENHANCED_CALL_STATUS_INCOMING_WAITING,
384     HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD
385 } hfp_enhanced_call_status_t;
386 
387 typedef enum{
388     HFP_ENHANCED_CALL_MODE_VOICE,
389     HFP_ENHANCED_CALL_MODE_DATA,
390     HFP_ENHANCED_CALL_MODE_FAX
391 } hfp_enhanced_call_mode_t;
392 
393 typedef enum{
394     HFP_ENHANCED_CALL_MPTY_NOT_A_CONFERENCE_CALL,
395     HFP_ENHANCED_CALL_MPTY_CONFERENCE_CALL
396 } hfp_enhanced_call_mpty_t;
397 
398 typedef enum {
399     HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD = 0,
400     HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED,
401     HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED
402 } hfp_response_and_hold_state_t;
403 
404 typedef enum {
405     HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET = 0,
406     HFP_HF_QUERY_OPERATOR_SET_FORMAT,
407     HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK,
408     HFP_HF_QUERY_OPERATOR_FORMAT_SET,
409     HFP_HF_QUERY_OPERATOR_SEND_QUERY,
410     HPF_HF_QUERY_OPERATOR_W4_RESULT
411 } hfp_hf_query_operator_state_t;
412 
413 typedef enum {
414     HFP_LINK_SETTINGS_D0 = 0,
415     HFP_LINK_SETTINGS_D1,
416     HFP_LINK_SETTINGS_S1,
417     HFP_LINK_SETTINGS_S2,
418     HFP_LINK_SETTINGS_S3,
419     HFP_LINK_SETTINGS_S4,
420     HFP_LINK_SETTINGS_T1,
421     HFP_LINK_SETTINGS_T2
422 } hfp_link_setttings_t;
423 
424 typedef enum{
425     HFP_NONE_SM,
426     HFP_SLC_SM,
427     HFP_SLC_QUERIES_SM,
428     HFP_CODECS_CONNECTION_SM,
429     HFP_AUDIO_CONNECTION_SM,
430     HFP_CALL_SM
431 } hfp_state_machine_t;
432 
433 typedef void (*hfp_callback_t)(uint8_t * event, uint16_t event_size);
434 
435 typedef struct{
436     uint16_t uuid;
437     uint8_t state; // enabled
438 } hfp_generic_status_indicator_t;
439 
440 typedef struct{
441     uint8_t index;
442     char name[HFP_MAX_INDICATOR_DESC_SIZE];
443     uint8_t min_range;
444     uint8_t max_range;
445     uint8_t status;
446     uint8_t mandatory;
447     uint8_t enabled;
448     uint8_t status_changed;
449 } hfp_ag_indicator_t;
450 
451 typedef struct{
452     char name[3];
453 } hfp_call_service_t;
454 
455 
456 typedef struct{
457     uint8_t mode;
458     uint8_t format;
459     char name[17]; // enabled
460 } hfp_network_opearator_t;
461 
462 
463 typedef struct hfp_connection {
464     btstack_linked_item_t    item;
465 
466     bd_addr_t remote_addr;
467     uint16_t con_handle;
468     uint16_t sco_handle;
469     uint16_t rfcomm_channel_nr;
470     uint16_t rfcomm_cid;
471 
472     hfp_state_machine_t state_machine;
473     hfp_call_state_t call_state;
474     hfp_state_t state;
475     hfp_codecs_state_t codecs_state;
476 
477     // needed for reestablishing connection
478     uint16_t service_uuid;
479 
480     // used during service level connection establishment
481     hfp_command_t command;
482     hfp_parser_state_t parser_state;
483     int      parser_item_index;
484     int      parser_indicator_index;
485     uint8_t  line_buffer[HFP_MAX_INDICATOR_DESC_SIZE];
486     int      line_size;
487 
488     uint32_t remote_supported_features;
489 
490     // TODO: rename into hf_codecs_nr
491     int      remote_codecs_nr;
492     uint16_t remote_codecs[HFP_MAX_INDICATOR_DESC_SIZE];
493 
494     int      ag_indicators_nr;
495     hfp_ag_indicator_t ag_indicators[HFP_MAX_INDICATOR_DESC_SIZE];
496     uint32_t ag_indicators_status_update_bitmap;
497     uint8_t  enable_status_update_for_ag_indicators;
498 
499     int      remote_call_services_nr;
500     hfp_call_service_t remote_call_services[HFP_MAX_INDICATOR_DESC_SIZE];
501 
502     // TODO: use bitmap.
503     int      generic_status_indicators_nr;
504     uint32_t generic_status_update_bitmap;
505     hfp_generic_status_indicator_t generic_status_indicators[HFP_MAX_INDICATOR_DESC_SIZE];
506 
507     hfp_network_opearator_t network_operator;
508 
509     // Retrieved during service level connection establishment, not used yet
510     uint8_t  negotiated_codec;
511 
512     // HF -> AG configuration
513     uint8_t clip_enabled;
514     uint8_t call_waiting_notification_enabled;
515 
516     // TODO: put these bit flags in a bitmap
517     uint8_t ok_pending;
518     // uint8_t send_ok;
519     uint8_t send_error;
520 
521     uint8_t keep_byte;
522     uint8_t ignore_value;
523     uint8_t resolve_byte;
524 
525     uint8_t change_status_update_for_individual_ag_indicators;
526     uint8_t operator_name_changed;
527 
528     uint8_t enable_extended_audio_gateway_error_report;
529     uint8_t extended_audio_gateway_error;
530 
531     // establish codecs connection
532     uint8_t suggested_codec;
533     uint8_t codec_confirmed;
534 
535     hfp_link_setttings_t link_setting;
536 
537     uint8_t establish_audio_connection;
538     uint8_t release_audio_connection;
539 
540     btstack_timer_source_t hfp_timeout;
541 
542     uint8_t microphone_gain;
543     uint8_t send_microphone_gain;
544 
545     uint8_t speaker_gain;
546     uint8_t send_speaker_gain;
547 
548     uint8_t send_phone_number_for_voice_tag;
549     uint8_t send_ag_status_indicators;
550     uint8_t send_ag_indicators_segment;
551     uint8_t send_response_and_hold_status;  // 0 - don't send. BRTH:0 == 1, ..
552 
553     // AG only
554     uint8_t change_in_band_ring_tone_setting;
555     uint8_t ag_ring;
556     uint8_t ag_send_clip;
557     uint8_t ag_echo_and_noise_reduction;
558     uint8_t ag_activate_voice_recognition;
559     uint8_t send_subscriber_number;
560     uint8_t next_subscriber_number_to_send;
561 
562     int send_status_of_current_calls;
563     int next_call_index;
564 
565     // HF only
566     hfp_hf_query_operator_state_t hf_query_operator_state;
567     uint8_t hf_answer_incoming_call;
568     uint8_t hf_initiate_outgoing_call;
569     uint8_t hf_initiate_memory_dialing;
570     uint8_t hf_initiate_redial_last_number;
571 
572     uint8_t hf_send_clip_enable;
573     uint8_t hf_send_chup;
574     uint8_t hf_send_chld_0;
575     uint8_t hf_send_chld_1;
576     uint8_t hf_send_chld_2;
577     uint8_t hf_send_chld_3;
578     uint8_t hf_send_chld_4;
579     uint8_t hf_send_chld_x;
580     uint8_t hf_send_chld_x_index;
581     char    hf_send_dtmf_code;
582     uint8_t hf_send_binp;
583     uint8_t hf_send_clcc;
584     uint8_t hf_send_rrh;
585     char    hf_send_rrh_command;
586     uint8_t hf_send_cnum;
587 
588     uint8_t hf_activate_call_waiting_notification;
589     uint8_t hf_deactivate_call_waiting_notification;
590 
591     uint8_t hf_activate_calling_line_notification;
592     uint8_t hf_deactivate_calling_line_notification;
593     uint8_t hf_activate_echo_canceling_and_noise_reduction;
594     uint8_t hf_deactivate_echo_canceling_and_noise_reduction;
595     uint8_t hf_activate_voice_recognition_notification;
596     uint8_t hf_deactivate_voice_recognition_notification;
597 
598     uint8_t clcc_idx;
599     uint8_t clcc_dir;
600     uint8_t clcc_status;
601     uint8_t clcc_mode;
602     uint8_t clcc_mpty;
603 
604     uint8_t call_index;
605     // also used for CLCC if set
606     uint8_t bnip_type;       // 0 == not set
607     char    bnip_number[25]; //
608 
609 } hfp_connection_t;
610 
611 // UTILS_START : TODO move to utils
612 int send_str_over_rfcomm(uint16_t cid, char * command);
613 int join(char * buffer, int buffer_size, uint8_t * values, int values_nr);
614 int join_bitmap(char * buffer, int buffer_size, uint32_t values, int values_nr);
615 int get_bit(uint16_t bitmap, int position);
616 int store_bit(uint32_t bitmap, int position, uint8_t value);
617 // UTILS_END
618 
619 void hfp_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint16_t service_uuid, int rfcomm_channel_nr, const char * name);
620 void hfp_handle_hci_event(hfp_callback_t callback, uint8_t packet_type, uint8_t *packet, uint16_t size);
621 void hfp_emit_event(hfp_callback_t callback, uint8_t event_subtype, uint8_t value);
622 void hfp_emit_string_event(hfp_callback_t callback, uint8_t event_subtype, const char * value);
623 
624 hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid);
625 hfp_connection_t * get_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr);
626 hfp_connection_t * get_hfp_connection_context_for_sco_handle(uint16_t handle);
627 
628 int get_hfp_generic_status_indicators_nr(void);
629 hfp_generic_status_indicator_t * get_hfp_generic_status_indicators(void);
630 void set_hfp_generic_status_indicators(hfp_generic_status_indicator_t * indicators, int indicator_nr);
631 
632 btstack_linked_list_t * hfp_get_connections(void);
633 void hfp_parse(hfp_connection_t * context, uint8_t byte, int isHandsFree);
634 
635 void hfp_init(uint16_t rfcomm_channel_nr);
636 void hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid);
637 void hfp_release_service_level_connection(hfp_connection_t * connection);
638 void hfp_reset_context_flags(hfp_connection_t * context);
639 
640 void hfp_release_audio_connection(hfp_connection_t * context);
641 
642 void hfp_setup_synchronous_connection(hci_con_handle_t handle, hfp_link_setttings_t link_settings);
643 
644 const char * hfp_hf_feature(int index);
645 const char * hfp_ag_feature(int index);
646 
647 #if defined __cplusplus
648 }
649 #endif
650 
651 #endif