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