xref: /btstack/src/classic/hfp.h (revision 6897da5c53aac5b1f90f41b5b15d0bd43d61dfff)
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 BLUEKITCHEN
24  * GMBH 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  * HFP Hands-Free (HF) unit and Audio Gateway Commons
40  *
41  */
42 
43 #ifndef BTSTACK_HFP_H
44 #define BTSTACK_HFP_H
45 
46 #include "hci.h"
47 #include "classic/sdp_client_rfcomm.h"
48 
49 #if defined __cplusplus
50 extern "C" {
51 #endif
52 
53 // period AG will send RING messages
54 #define HFP_RING_PERIOD_MS 2000
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 #define HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS      10
80 #define HFP_HFSF_VOICE_RECOGNITION_TEXT                 11
81 
82 /* AG Supported Features:
83 0: Three-way calling
84 1: EC and/or NR function
85 2: Voice recognition function
86 3: In-band ring tone capability
87 4: Attach a number to a voice tag
88 5: Ability to reject a call
89 6: Enhanced call status
90 7: Enhanced call control
91 8: Extended Error Result Codes
92 9: Codec negotiation
93 10: HF Indicators
94 11: eSCO S4 (and T2) Settings Supported
95 12-31: Reserved for future definition
96 */
97 #define HFP_AGSF_THREE_WAY_CALLING                       0
98 #define HFP_AGSF_EC_NR_FUNCTION                          1
99 #define HFP_AGSF_VOICE_RECOGNITION_FUNCTION              2
100 #define HFP_AGSF_IN_BAND_RING_TONE                       3
101 #define HFP_AGSF_ATTACH_A_NUMBER_TO_A_VOICE_TAG          4
102 #define HFP_AGSF_ABILITY_TO_REJECT_A_CALL                5
103 #define HFP_AGSF_ENHANCED_CALL_STATUS                    6
104 #define HFP_AGSF_ENHANCED_CALL_CONTROL                   7
105 #define HFP_AGSF_EXTENDED_ERROR_RESULT_CODES             8
106 #define HFP_AGSF_CODEC_NEGOTIATION                       9
107 #define HFP_AGSF_HF_INDICATORS                          10
108 #define HFP_AGSF_ESCO_S4                                11
109 #define HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS      12
110 #define HFP_AGSF_VOICE_RECOGNITION_TEXT                 13
111 
112 #define HFP_DEFAULT_HF_SUPPORTED_FEATURES 0x0000
113 #define HFP_DEFAULT_AG_SUPPORTED_FEATURES 0x0009
114 
115 #define HFP_MAX_NUM_INDICATORS                  10
116 #define HFP_MAX_NUM_CALL_SERVICES               20
117 #define HFP_CALL_SERVICE_SIZE                    3
118 #define HFP_MAX_NUM_CODECS                      10
119 #define HFP_BNEP_NUM_MAX_SIZE                   25
120 #define HFP_MAX_INDICATOR_DESC_SIZE             20
121 #define HFP_MAX_VR_TEXT_SIZE                   100
122 #define HFP_VR_TEXT_HEADER_SIZE                 27      // bytes needed for sending +BVRA message including quotes but excluding string length:
123                                                         // \r\n+BVRA: <vrect>,<vrecstate>,<textID>,<textType>,<textOperation>,"<string>"\r\n
124 
125 #define HFP_MAX_NETWORK_OPERATOR_NAME_SIZE 17
126 
127 #define HFP_HF_INDICATOR_UUID_ENHANCED_SAFETY   0x0001  // 0 - disabled, 1 - enabled
128 #define HFP_HF_INDICATOR_UUID_BATTERY_LEVEL     0X0002  // 0-100 remaining level of battery
129 
130 #define HFP_SUPPORTED_FEATURES "+BRSF"
131 #define HFP_AVAILABLE_CODECS "+BAC"
132 #define HFP_INDICATOR "+CIND"
133 #define HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS "+CMER"
134 #define HFP_ENABLE_CLIP "+CLIP"
135 #define HFP_ENABLE_CALL_WAITING_NOTIFICATION "+CCWA"
136 #define HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS "+BIA" // +BIA:<enabled>,,<enabled>,,,<enabled>
137 #define HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES "+CHLD"
138 #define HFP_GENERIC_STATUS_INDICATOR "+BIND"
139 #define HFP_TRANSFER_AG_INDICATOR_STATUS "+CIEV" // +CIEV: <index>,<value>
140 #define HFP_TRANSFER_HF_INDICATOR_STATUS "+BIEV" // +BIEC: <index>,<value>
141 #define HFP_QUERY_OPERATOR_SELECTION "+COPS"     // +COPS: <mode>,0,<opearator>
142 #define HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR "+CMEE"
143 #define HFP_EXTENDED_AUDIO_GATEWAY_ERROR "+CME ERROR"
144 #define HFP_TRIGGER_CODEC_CONNECTION_SETUP "+BCC"
145 #define HFP_CONFIRM_COMMON_CODEC "+BCS"
146 #define HFP_ANSWER_CALL "ATA"
147 #define HFP_HANG_UP_CALL "+CHUP"
148 #define HFP_CHANGE_IN_BAND_RING_TONE_SETTING "+BSIR"
149 #define HFP_CALL_PHONE_NUMBER "ATD"
150 #define HFP_REDIAL_LAST_NUMBER "+BLDN"
151 #define HFP_TURN_OFF_EC_AND_NR "+NREC" // EC (Echo CAnceling), NR (Noise Reduction)
152 #define HFP_ACTIVATE_VOICE_RECOGNITION "+BVRA" // Voice Recognition
153 #define HFP_SET_MICROPHONE_GAIN  "+VGM"
154 #define HFP_SET_SPEAKER_GAIN     "+VGS"
155 #define HFP_PHONE_NUMBER_FOR_VOICE_TAG "+BINP"
156 #define HFP_TRANSMIT_DTMF_CODES  "+VTS"
157 #define HFP_SUBSCRIBER_NUMBER_INFORMATION "+CNUM"
158 #define HFP_LIST_CURRENT_CALLS "+CLCC"
159 #define HFP_RESPONSE_AND_HOLD "+BTRH"
160 
161 // Apple Extensions
162 #define HFP_APPLE_ACCESSORY_INFORMATION "+XAPL"
163 #define HFP_APPLE_ACCESSORY_STATE "+IPHONEACCEV"
164 #define HFP_APPLE_SIRI_STATE "+APLSIRI"
165 #define HFP_APPLE_SIRI_EYES_FREE_MODE "+APLEFM"
166 #define HFP_APPLE_ACCESSORY_VERSION_LEN  16
167 
168 #define HFP_OK "OK"
169 #define HFP_ERROR "ERROR"
170 #define HFP_RING "RING"
171 
172 // Codecs
173 #define HFP_CODEC_CVSD    0x01
174 #define HFP_CODEC_MSBC    0x02
175 #define HFP_CODEC_LC3_SWB 0x03
176 
177 typedef enum {
178     HFP_ROLE_INVALID = 0,
179     HFP_ROLE_AG,
180     HFP_ROLE_HF,
181 } hfp_role_t;
182 
183 typedef enum {
184     HFP_CMD_NONE = 0,
185     HFP_CMD_ERROR,
186     HFP_CMD_UNKNOWN,
187     HFP_CMD_OK,
188     HFP_CMD_RING,
189     HFP_CMD_SUPPORTED_FEATURES,                             // 5
190     HFP_CMD_AVAILABLE_CODECS,
191     HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC,
192     HFP_CMD_RETRIEVE_AG_INDICATORS,
193     HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS,
194     HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE,                 // 10
195     HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE,
196     HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES,
197     HFP_CMD_ENABLE_CLIP,
198     HFP_CMD_AG_SENT_CLIP_INFORMATION,
199     HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION,               // 15
200     HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE,
201     HFP_CMD_LIST_GENERIC_STATUS_INDICATORS,
202     HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS,
203     HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE,
204     HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS,            // 20
205     HFP_CMD_TRANSFER_AG_INDICATOR_STATUS,
206     HFP_CMD_QUERY_OPERATOR_SELECTION_NAME,
207     HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT,
208     HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR,
209     HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR,                   // 25
210     HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP,
211     HFP_CMD_AG_SEND_COMMON_CODEC,
212     HFP_CMD_AG_SUGGESTED_CODEC,
213     HFP_CMD_HF_CONFIRMED_CODEC,
214     HFP_CMD_CALL_ANSWERED,                                  // 30
215     HFP_CMD_CALL_HOLD,
216     HFP_CMD_HANG_UP_CALL,
217     HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING,
218     HFP_CMD_CALL_PHONE_NUMBER,
219     HFP_CMD_REDIAL_LAST_NUMBER,                             // 35
220     HFP_CMD_TURN_OFF_EC_AND_NR,
221     HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION,                  // 37
222     HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION,
223     HFP_CMD_HF_REQUEST_PHONE_NUMBER,
224     HFP_CMD_AG_SENT_PHONE_NUMBER,
225     HFP_CMD_TRANSMIT_DTMF_CODES,
226     HFP_CMD_SET_MICROPHONE_GAIN,
227     HFP_CMD_SET_SPEAKER_GAIN,
228     HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION,
229     HFP_CMD_LIST_CURRENT_CALLS,
230     HFP_CMD_RESPONSE_AND_HOLD_QUERY,
231     HFP_CMD_RESPONSE_AND_HOLD_COMMAND,
232     HFP_CMD_RESPONSE_AND_HOLD_STATUS,
233     HFP_CMD_HF_INDICATOR_STATUS,
234     HFP_CMD_APPLE_ACCESSORY_INFORMATION,
235     HFP_CMD_APPLE_ACCESSORY_STATE,
236     HFP_CMD_APPLE_DEVICE_INFORMATION,
237     HFP_CMD_CUSTOM_MESSAGE
238 } hfp_command_t;
239 
240 
241 typedef enum {
242     HFP_CME_ERROR_AG_FAILURE = 0,
243     HFP_CME_ERROR_NO_CONNECTION_TO_PHONE,
244     HFP_CME_ERROR_2,
245     HFP_CME_ERROR_OPERATION_NOT_ALLOWED,
246     HFP_CME_ERROR_OPERATION_NOT_SUPPORTED,
247     HFP_CME_ERROR_PH_SIM_PIN_REQUIRED,
248     HFP_CME_ERROR_6,
249     HFP_CME_ERROR_7,
250     HFP_CME_ERROR_8,
251     HFP_CME_ERROR_9,
252     HFP_CME_ERROR_SIM_NOT_INSERTED,
253     HFP_CME_ERROR_SIM_PIN_REQUIRED,
254     HFP_CME_ERROR_SIM_PUK_REQUIRED,
255     HFP_CME_ERROR_SIM_FAILURE,
256     HFP_CME_ERROR_SIM_BUSY,
257     HFP_CME_ERROR_15,
258     HFP_CME_ERROR_INCORRECT_PASSWORD,
259     HFP_CME_ERROR_SIM_PIN2_REQUIRED,
260     HFP_CME_ERROR_SIM_PUK2_REQUIRED,
261     HFP_CME_ERROR_19,
262     HFP_CME_ERROR_MEMORY_FULL,
263     HFP_CME_ERROR_INVALID_INDEX,
264     HFP_CME_ERROR_22,
265     HFP_CME_ERROR_MEMORY_FAILURE,
266     HFP_CME_ERROR_TEXT_STRING_TOO_LONG,
267     HFP_CME_ERROR_INVALID_CHARACTERS_IN_TEXT_STRING,
268     HFP_CME_ERROR_DIAL_STRING_TOO_LONG,
269     HFP_CME_ERROR_INVALID_CHARACTERS_IN_DIAL_STRING,
270     HFP_CME_ERROR_28,
271     HFP_CME_ERROR_29,
272     HFP_CME_ERROR_NO_NETWORK_SERVICE,
273     HFP_CME_ERROR_NETWORK_TIMEOUT,
274     HFP_CME_ERROR_NETWORK_NOT_ALLOWED_EMERGENCY_CALLS_ONLY
275 } hfp_cme_error_t;
276 
277 typedef enum {
278     HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS = 0,
279     HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT
280 } hfp_call_status_t;
281 
282 typedef enum {
283     HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS = 0,
284     HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS,
285     HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE,
286     HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE
287 } hfp_callsetup_status_t;
288 
289 typedef enum {
290     HFP_CALLHELD_STATUS_NO_CALLS_HELD = 0,
291     HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED,
292     HFP_CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS
293 } hfp_callheld_status_t;
294 
295 
296 typedef enum {
297     HFP_AG_INCOMING_CALL,
298     HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG,
299     HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF,
300     HFP_AG_AUDIO_CONNECTION_ESTABLISHED,
301     HFP_AG_OUTGOING_CALL_INITIATED_BY_AG,
302     HFP_AG_OUTGOING_CALL_INITIATED_BY_HF,
303     HFP_AG_OUTGOING_CALL_REJECTED,
304     HFP_AG_OUTGOING_CALL_ACCEPTED,
305     HFP_AG_OUTGOING_CALL_RINGING,
306     HFP_AG_OUTGOING_CALL_ESTABLISHED,
307     HFP_AG_OUTGOING_REDIAL_INITIATED,
308     HFP_AG_HELD_CALL_JOINED_BY_AG,
309     HFP_AG_TERMINATE_CALL_BY_AG,
310     HFP_AG_TERMINATE_CALL_BY_HF,
311     HFP_AG_CALL_DROPPED,
312     HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG,
313     HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG,
314     HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG,
315     HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF,
316     HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF,
317     HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF,
318     HFP_AG_CALL_HOLD_USER_BUSY,
319     HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL,
320     HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL,
321     HFP_AG_CALL_HOLD_ADD_HELD_CALL,
322     HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS,
323     HFP_AG_SET_CLIP
324 } hfp_ag_call_event_t;
325 
326 
327 typedef enum {
328     HFP_PARSER_CMD_HEADER = 0,
329     HFP_PARSER_CMD_SEQUENCE,
330     HFP_PARSER_SECOND_ITEM,
331     HFP_PARSER_THIRD_ITEM,
332     HFP_PARSER_CUSTOM_COMMAND
333 } hfp_parser_state_t;
334 
335 typedef enum {
336     HFP_VOICE_RECOGNITION_STATE_AG_READY = 0,
337     HFP_VOICE_RECOGNITION_STATE_AG_READY_TO_ACCEPT_AUDIO_INPUT = 1,
338     HFP_VOICE_RECOGNITION_STATE_AG_IS_STARTING_SOUND = 2,
339     HFP_VOICE_RECOGNITION_STATE_AG_IS_PROCESSING_AUDIO_INPUT = 4
340 } hfp_voice_recognition_state_t;
341 
342 typedef enum {
343     HFP_TEXT_TYPE_RECOGNISED_FROM_HF_AUDIO = 0,
344     HFP_TEXT_TYPE_MESSAGE_FROM_AG,
345     HFP_TEXT_TYPE_QUESTION_FROM_AG,
346     HFP_TEXT_TYPE_ERROR_FROM_AG
347 } hfp_text_type_t;
348 
349 typedef enum {
350     HFP_TEXT_OPERATION_NEW_TEXT = 1,
351     HFP_TEXT_OPERATION_REPLACE,
352     HFP_TEXT_OPERATION_APPEND
353 } hfp_text_operation_t;
354 
355 typedef enum {
356     HFP_IDLE = 0, //0
357     HFP_SDP_QUERY_RFCOMM_CHANNEL,
358     HFP_W2_SEND_SDP_QUERY,
359     HFP_W4_SDP_QUERY_COMPLETE,
360     HFP_W4_RFCOMM_CONNECTED,
361 
362     HFP_EXCHANGE_SUPPORTED_FEATURES,   // 5
363     HFP_W4_EXCHANGE_SUPPORTED_FEATURES,
364 
365     HFP_NOTIFY_ON_CODECS,
366     HFP_W4_NOTIFY_ON_CODECS,
367 
368     HFP_RETRIEVE_INDICATORS,
369     HFP_W4_RETRIEVE_INDICATORS,        // 10
370 
371     HFP_RETRIEVE_INDICATORS_STATUS,
372     HFP_W4_RETRIEVE_INDICATORS_STATUS,
373 
374     HFP_ENABLE_INDICATORS_STATUS_UPDATE,
375     HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE,
376 
377     HFP_RETRIEVE_CAN_HOLD_CALL,        // 15
378     HFP_W4_RETRIEVE_CAN_HOLD_CALL,
379 
380     HFP_LIST_GENERIC_STATUS_INDICATORS,
381     HFP_W4_LIST_GENERIC_STATUS_INDICATORS,
382 
383     HFP_RETRIEVE_GENERIC_STATUS_INDICATORS,
384     HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS,  //20
385 
386     HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS,
387     HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS,
388 
389     HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED,  //23
390 
391     HFP_W2_CONNECT_SCO,
392     HFP_W4_SCO_CONNECTED,
393 
394     HFP_AUDIO_CONNECTION_ESTABLISHED,
395 
396 
397     HFP_W2_DISCONNECT_SCO,
398     HFP_W4_SCO_DISCONNECTED,
399     HFP_W4_SCO_DISCONNECTED_TO_SHUTDOWN,
400     HFP_W4_WBS_SHUTDOWN,
401 
402     HFP_W2_DISCONNECT_RFCOMM,
403     HFP_W4_RFCOMM_DISCONNECTED,
404     HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART,
405     HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN
406 } hfp_state_t;
407 
408 
409 typedef enum {
410     // shared between normal voice recognition and enhanced one
411     HFP_VRA_VOICE_RECOGNITION_OFF,
412 
413     HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF,
414     HFP_VRA_W4_VOICE_RECOGNITION_OFF,
415 
416     HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED,
417     HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED,
418     HFP_VRA_VOICE_RECOGNITION_ACTIVATED,
419 
420     HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO,
421     HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO,
422     HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO,
423 
424     HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_STATUS,
425     HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_MSG
426 } hfp_voice_recognition_activation_status_t;
427 
428 typedef struct {
429     uint16_t text_id;
430     hfp_text_type_t text_type;
431     hfp_text_operation_t text_operation;
432     const char * text;
433 } hfp_voice_recognition_message_t;
434 
435 typedef enum {
436     HFP_CODECS_IDLE,
437     HFP_CODECS_RECEIVED_LIST,
438     HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE,
439     HFP_CODECS_W4_AG_COMMON_CODEC,
440     HFP_CODECS_AG_SENT_COMMON_CODEC,
441     HFP_CODECS_AG_RESEND_COMMON_CODEC,
442     HFP_CODECS_HF_CONFIRMED_CODEC,
443     HFP_CODECS_EXCHANGED,
444     HFP_CODECS_ERROR
445 } hfp_codecs_state_t;
446 
447 typedef enum {
448     HFP_CALL_IDLE,
449     HFP_CALL_TRIGGER_AUDIO_CONNECTION,
450     HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING,
451     HFP_CALL_INCOMING_RINGING,
452     HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE,
453     HFP_CALL_ACTIVE,
454     HFP_CALL_W2_SEND_CALL_WAITING,
455     HFP_CALL_W4_CHLD,
456     HFP_CALL_OUTGOING_INITIATED,
457     HFP_CALL_OUTGOING_DIALING,
458     HFP_CALL_OUTGOING_RINGING
459 } hfp_call_state_t;
460 
461 typedef enum{
462     HFP_ENHANCED_CALL_DIR_OUTGOING,
463     HFP_ENHANCED_CALL_DIR_INCOMING
464 } hfp_enhanced_call_dir_t;
465 
466 typedef enum{
467     HFP_ENHANCED_CALL_STATUS_ACTIVE,
468     HFP_ENHANCED_CALL_STATUS_HELD,
469     HFP_ENHANCED_CALL_STATUS_OUTGOING_DIALING,
470     HFP_ENHANCED_CALL_STATUS_OUTGOING_ALERTING,
471     HFP_ENHANCED_CALL_STATUS_INCOMING,
472     HFP_ENHANCED_CALL_STATUS_INCOMING_WAITING,
473     HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD
474 } hfp_enhanced_call_status_t;
475 
476 typedef enum{
477     HFP_ENHANCED_CALL_MODE_VOICE,
478     HFP_ENHANCED_CALL_MODE_DATA,
479     HFP_ENHANCED_CALL_MODE_FAX
480 } hfp_enhanced_call_mode_t;
481 
482 typedef enum{
483     HFP_ENHANCED_CALL_MPTY_NOT_A_CONFERENCE_CALL,
484     HFP_ENHANCED_CALL_MPTY_CONFERENCE_CALL
485 } hfp_enhanced_call_mpty_t;
486 
487 typedef enum {
488     HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD = 0,
489     HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED,
490     HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED
491 } hfp_response_and_hold_state_t;
492 
493 typedef enum {
494     HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET = 0,
495     HFP_HF_QUERY_OPERATOR_SET_FORMAT,
496     HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK,
497     HFP_HF_QUERY_OPERATOR_FORMAT_SET,
498     HFP_HF_QUERY_OPERATOR_SEND_QUERY,
499     HPF_HF_QUERY_OPERATOR_W4_RESULT
500 } hfp_hf_query_operator_state_t;
501 
502 typedef enum {
503     HFP_LINK_SETTINGS_D0 = 0,
504     HFP_LINK_SETTINGS_D1,
505     HFP_LINK_SETTINGS_S1,
506     HFP_LINK_SETTINGS_S2,
507     HFP_LINK_SETTINGS_S3,
508     HFP_LINK_SETTINGS_S4,
509     HFP_LINK_SETTINGS_T1,
510     HFP_LINK_SETTINGS_T2,
511     HFP_LINK_SETTINGS_NONE,
512 } hfp_link_settings_t;
513 
514 typedef enum{
515     HFP_NONE_SM,
516     HFP_SLC_SM,
517     HFP_SLC_QUERIES_SM,
518     HFP_CODECS_CONNECTION_SM,
519     HFP_AUDIO_CONNECTION_SM,
520     HFP_CALL_SM
521 } hfp_state_machine_t;
522 
523 typedef struct{
524     uint16_t uuid;
525     uint8_t state; // enabled
526 } hfp_generic_status_indicator_t;
527 
528 typedef struct{
529     uint8_t index;
530     char name[HFP_MAX_INDICATOR_DESC_SIZE];
531     uint8_t min_range;
532     uint8_t max_range;
533     uint8_t status;
534     uint8_t mandatory;
535     uint8_t enabled;
536     uint8_t status_changed;
537 } hfp_ag_indicator_t;
538 
539 typedef struct{
540     char name[HFP_CALL_SERVICE_SIZE];
541 } hfp_call_service_t;
542 
543 
544 typedef struct{
545     uint8_t mode;
546     uint8_t format;
547     char name[HFP_MAX_NETWORK_OPERATOR_NAME_SIZE]; // enabled
548 } hfp_network_opearator_t;
549 
550 
551 typedef struct hfp_connection {
552     btstack_linked_item_t    item;
553 
554     // local role: HF or AG
555     hfp_role_t local_role;
556 
557     bd_addr_t remote_addr;
558     hci_con_handle_t acl_handle;
559     hci_con_handle_t sco_handle;
560     uint16_t packet_types;
561     uint8_t rfcomm_channel_nr;
562     uint16_t rfcomm_cid;
563     uint16_t rfcomm_mtu;
564 
565     hfp_state_machine_t state_machine;
566     hfp_call_state_t call_state;
567     hfp_state_t state;
568     hfp_codecs_state_t codecs_state;
569 
570     // needed for reestablishing connection - service uuid of the remote
571     uint16_t service_uuid;
572 
573     // used during service level connection establishment
574     hfp_command_t command;
575     hfp_parser_state_t parser_state;
576     int      parser_item_index;
577     int      parser_indicator_index;
578     uint32_t parser_indicator_value;
579     bool     parser_quoted;
580 
581     // line buffer is always \0 terminated
582     uint8_t  line_buffer[HFP_MAX_VR_TEXT_SIZE];
583     int      line_size;
584 
585     uint32_t remote_supported_features;
586 
587     uint16_t remote_codecs_nr;
588     uint8_t  remote_codecs[HFP_MAX_NUM_CODECS];
589 
590     uint16_t ag_indicators_nr;
591     hfp_ag_indicator_t ag_indicators[HFP_MAX_NUM_INDICATORS];
592     uint32_t ag_indicators_status_update_bitmap;
593     uint8_t  enable_status_update_for_ag_indicators;
594 
595     uint16_t remote_call_services_index;
596     hfp_call_service_t remote_call_services[HFP_MAX_NUM_CALL_SERVICES];
597 
598     // TODO: use bitmap.
599     uint16_t generic_status_indicators_nr;
600     uint32_t generic_status_update_bitmap;
601     hfp_generic_status_indicator_t generic_status_indicators[HFP_MAX_NUM_INDICATORS];
602 
603     hfp_network_opearator_t network_operator;
604 
605     // Retrieved during service level connection establishment, not used yet
606     uint8_t  negotiated_codec;
607 
608     // HF -> AG configuration
609     uint8_t clip_enabled;
610     uint8_t call_waiting_notification_enabled;
611 
612     // TODO: put these bit flags in a bitmap
613     uint8_t ok_pending;
614     uint8_t send_error;
615 
616     bool found_equal_sign;
617     uint8_t ignore_value;
618 
619     uint8_t change_status_update_for_individual_ag_indicators;
620     uint8_t operator_name_changed;
621 
622     uint8_t enable_extended_audio_gateway_error_report;
623     uint8_t extended_audio_gateway_error_value;
624     uint8_t extended_audio_gateway_error;
625 
626     // establish codecs connection
627     uint8_t suggested_codec;
628     uint8_t codec_confirmed;
629     uint8_t sco_for_msbc_failed;
630     uint8_t trigger_codec_exchange;
631 
632     // establish audio connection
633     hfp_link_settings_t link_setting;
634     uint8_t accept_sco; // 1 = SCO, 2 = eSCO
635 
636     uint8_t establish_audio_connection;
637     uint8_t release_audio_connection;
638     uint8_t release_slc_connection;
639 
640     uint8_t microphone_gain;
641     uint8_t send_microphone_gain;
642 
643     uint8_t speaker_gain;
644     uint8_t send_speaker_gain;
645 
646     uint8_t send_phone_number_for_voice_tag;
647     uint8_t send_ag_status_indicators;
648     uint8_t send_ag_indicators_segment;
649     uint8_t send_response_and_hold_status;  // 0 - don't send. BRTH:0 == 1, ..
650 
651     // Apple Accessory Support
652     bool send_apple_information;
653     bool apple_accessory_commands_supported;
654     // - HF to send
655     int8_t apple_accessory_battery_level;
656     int8_t apple_accessory_docked;
657     // - AG Parser
658     uint16_t apple_accessory_product_id;
659     uint16_t apple_accessory_vendor_id;
660     uint8_t  apple_accessory_features;
661     uint8_t  apple_accessory_key;
662     char     apple_accessory_version[HFP_APPLE_ACCESSORY_VERSION_LEN];
663     // HF: AT Command, AG: Unsolicited Result Code
664     const char * send_custom_message;
665 
666     // HF:  Unsolicited Result Code, AG:  AT Command
667     uint16_t custom_at_command_id;
668 
669     bool emit_vra_enabled_after_audio_established;
670     // AG only
671     uint8_t change_in_band_ring_tone_setting;
672     uint8_t ag_ring;
673     uint8_t ag_send_clip;
674     uint8_t ag_echo_and_noise_reduction;
675     // used by AG: HFP parser stores here the activation value issued by HF
676     uint8_t ag_activate_voice_recognition_value;
677     bool    ag_audio_connection_opened_before_vra;
678 
679     uint8_t ag_notify_incoming_call_waiting;
680     uint8_t send_subscriber_number;
681     uint8_t next_subscriber_number_to_send;
682     uint8_t ag_call_hold_action;
683     uint8_t ag_response_and_hold_action;
684     uint8_t ag_dtmf_code;
685     bool    ag_in_band_ring_tone_active;
686     bool    ag_send_no_carrier;
687     bool    ag_vra_send_command;
688     bool    ag_send_in_band_ring_tone_setting;
689     bool    ag_send_common_codec;
690     bool    ag_vra_requested_by_hf;
691 
692     int send_status_of_current_calls;
693     int next_call_index;
694 
695     // HF only
696     // HF: track command for which ok/error response need to be received
697     hfp_command_t response_pending_for_command;
698 
699     hfp_hf_query_operator_state_t hf_query_operator_state;
700     uint8_t hf_answer_incoming_call;
701     uint8_t hf_initiate_outgoing_call;
702     uint8_t hf_initiate_memory_dialing;
703     uint8_t hf_initiate_redial_last_number;
704     bool    hf_send_codec_confirm;
705     bool    hf_send_supported_codecs;
706 
707     int memory_id;
708 
709     uint8_t hf_send_clip_enable;
710     uint8_t hf_send_chup;
711     uint8_t hf_send_chld_0;
712     uint8_t hf_send_chld_1;
713     uint8_t hf_send_chld_2;
714     uint8_t hf_send_chld_3;
715     uint8_t hf_send_chld_4;
716     uint8_t hf_send_chld_x;
717     uint8_t hf_send_chld_x_index;
718     char    hf_send_dtmf_code;
719     uint8_t hf_send_binp;
720     uint8_t hf_send_clcc;
721     uint8_t hf_send_rrh;
722     char    hf_send_rrh_command;
723     uint8_t hf_send_cnum;
724 
725     uint8_t hf_activate_call_waiting_notification;
726     uint8_t hf_deactivate_call_waiting_notification;
727 
728     uint8_t hf_activate_calling_line_notification;
729     uint8_t hf_deactivate_calling_line_notification;
730     uint8_t hf_deactivate_echo_canceling_and_noise_reduction;
731 
732     hfp_call_status_t      hf_call_status;
733     hfp_callsetup_status_t hf_callsetup_status;
734     hfp_callheld_status_t  hf_callheld_status;
735 
736     hfp_voice_recognition_activation_status_t vra_state;
737     hfp_voice_recognition_activation_status_t vra_state_requested;
738     bool deactivate_voice_recognition;
739     bool activate_voice_recognition;
740     bool enhanced_voice_recognition_enabled;
741 
742     // ih HF, used by parser, in AG used for commands
743     uint8_t ag_vra_status;
744     hfp_voice_recognition_state_t ag_vra_state;
745     hfp_voice_recognition_message_t ag_msg;
746     uint16_t ag_vra_msg_length;
747 
748     uint8_t clcc_idx;
749     uint8_t clcc_dir;
750     uint8_t clcc_status;
751     uint8_t clcc_mode;
752     uint8_t clcc_mpty;
753 
754     uint8_t call_index;
755     // also used for CLCC, CCWA, CLIP if set
756     uint8_t bnip_type;       // 0 == not set
757     char    bnip_number[HFP_BNEP_NUM_MAX_SIZE]; //
758     bool    clip_have_alpha;
759 
760 #ifdef ENABLE_CC256X_ASSISTED_HFP
761     bool cc256x_send_write_codec_config;
762     bool cc256x_send_wbs_associate;
763     bool cc256x_send_wbs_disassociate;
764 #endif
765 #ifdef ENABLE_BCM_PCM_WBS
766     bool bcm_send_enable_wbs;
767     bool bcm_send_disable_wbs;
768     bool bcm_send_write_i2spcm_interface_param;
769 #endif
770 #ifdef ENABLE_RTK_PCM_WBS
771     bool rtk_send_sco_config;
772 #endif
773 #ifdef ENABLE_NXP_PCM_WBS
774     hci_con_handle_t nxp_start_audio_handle;
775     hci_con_handle_t nxp_stop_audio_handle;
776 #endif
777 } hfp_connection_t;
778 
779 /**
780  * @brief Struct to register custom AT Command.
781  */
782 typedef struct {
783     btstack_linked_item_t * next;
784     const char *            command;
785     uint16_t                command_id;
786 } hfp_custom_at_command_t;
787 
788 // UTILS_START : TODO move to utils
789 int send_str_over_rfcomm(uint16_t cid, const char * command);
790 int join(char * buffer, int buffer_size, uint8_t * values, int values_nr);
791 int join_bitmap(char * buffer, int buffer_size, uint32_t values, int values_nr);
792 int get_bit(uint16_t bitmap, int position);
793 int store_bit(uint32_t bitmap, int position, uint8_t value);
794 // UTILS_END
795 
796 #define HFP_H2_SYNC_FRAME_SIZE 60
797 // HFP H2 Synchronization
798 typedef struct {
799     // callback returns true if data was valid
800     bool        (*callback)(bool bad_frame, const uint8_t * frame_data, uint16_t frame_len);
801     uint8_t     frame_data[HFP_H2_SYNC_FRAME_SIZE];
802     uint16_t    frame_len;
803     uint16_t    dropped_bytes;
804 } hfp_h2_sync_t;
805 
806 /**
807  * @brief Init HFP H2 Sync state
808  * @param hfp_h2_sync
809  * @param callback
810  */
811 void hfp_h2_sync_init(hfp_h2_sync_t * hfp_h2_sync,
812                       bool (*callback)(bool bad_frame, const uint8_t * frame_data, uint16_t frame_len));
813 /**
814  * @brief Process H2 data and execute callback for frames with valid H2 header
815  * @param hfp_h2_sync
816  * @param bad_frame
817  * @param frame_data
818  * @param frame_len
819  */
820 void hfp_h2_sync_process(hfp_h2_sync_t *hfp_h2_sync, bool bad_frame, const uint8_t *frame_data, uint16_t frame_len);
821 
822 
823 // other
824 
825 void hfp_finalize_connection_context(hfp_connection_t * hfp_connection);
826 void hfp_emit_sco_connection_established(hfp_connection_t *hfp_connection, uint8_t status, uint8_t negotiated_codec,
827                                          uint16_t rx_packet_length, uint16_t tx_packet_length);
828 
829 void hfp_set_ag_callback(btstack_packet_handler_t callback);
830 void hfp_set_ag_rfcomm_packet_handler(btstack_packet_handler_t handler);
831 
832 void hfp_set_hf_callback(btstack_packet_handler_t callback);
833 void hfp_set_hf_rfcomm_packet_handler(btstack_packet_handler_t handler);
834 void hfp_set_hf_indicators(uint8_t indicators_nr, const uint8_t * indicators);
835 
836 void hfp_init(void);
837 void hfp_deinit(void);
838 
839 void hfp_register_custom_ag_command(hfp_custom_at_command_t * at_command);
840 void hfp_register_custom_hf_command(hfp_custom_at_command_t * at_command);
841 
842 void hfp_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint16_t service_uuid, int rfcomm_channel_nr, const char * name);
843 void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role);
844 void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role);
845 void hfp_emit_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, uint8_t value);
846 void hfp_emit_simple_event(hfp_connection_t * hfp_connection, uint8_t event_subtype);
847 void hfp_emit_string_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, const char * value);
848 void hfp_emit_slc_connection_event(hfp_role_t local_role, uint8_t status, hci_con_handle_t con_handle, bd_addr_t addr);
849 
850 /**
851  * @brief Emit HFP_SUBEVENT_VOICE_RECOGNITION_ENABLED event
852  * @param hfp_connection
853  * @param status ERROR_CODE_SUCCESS if successful, otherwise ERROR_CODE_COMMAND_DISALLOWED
854  */
855 void hfp_emit_voice_recognition_enabled(hfp_connection_t * hfp_connection, uint8_t status);
856 
857 /**
858  * @brief Emit HFP_SUBEVENT_VOICE_RECOGNITION_DISABLED event
859  * @param hfp_connection
860  * @param status ERROR_CODE_SUCCESS if successful, otherwise ERROR_CODE_COMMAND_DISALLOWED
861  */
862 void hfp_emit_voice_recognition_disabled(hfp_connection_t * hfp_connection, uint8_t status);
863 
864 void hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection_t * hfp_connection, uint8_t status);
865 void hfp_emit_enhanced_voice_recognition_state_event(hfp_connection_t * hfp_connection, uint8_t status);
866 
867 hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid);
868 hfp_connection_t * get_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr, hfp_role_t hfp_role);
869 hfp_connection_t * get_hfp_connection_context_for_sco_handle(uint16_t handle, hfp_role_t hfp_role);
870 hfp_connection_t * get_hfp_connection_context_for_acl_handle(uint16_t handle, hfp_role_t hfp_role);
871 
872 btstack_linked_list_t * hfp_get_connections(void);
873 void hfp_parse(hfp_connection_t * connection, uint8_t byte, int isHandsFree);
874 void hfp_parser_reset_line_buffer(hfp_connection_t *hfp_connection);
875 
876 /**
877  * @brief Establish RFCOMM connection, and perform service level connection agreement:
878  * @param bd_addr
879  * @param service_uuid
880  * @param local_role
881  * @return status ERROR_CODE_SUCCESS if successful, otherwise:
882  *                  - ERROR_CODE_COMMAND_DISALLOWED if connection already exists, or
883  *                  - BTSTACK_MEMORY_ALLOC_FAILED
884  */
885 uint8_t hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid, hfp_role_t local_role);
886 
887 /**
888  * @brief Prepare connection for audio and SLC connections release
889  * @param hfp_connection
890  */
891 void hfp_trigger_release_service_level_connection(hfp_connection_t * hfp_connection);
892 
893 /**
894  * @brief Prepare connection for audio connection release
895  * @param hfp_connection
896  */
897 uint8_t hfp_trigger_release_audio_connection(hfp_connection_t * hfp_connection);
898 
899 void hfp_reset_context_flags(hfp_connection_t * hfp_connection);
900 
901 // @returns if an SCO setup is active in either role
902 bool hfp_sco_setup_active(void);
903 
904 void hfp_setup_synchronous_connection(hfp_connection_t * hfp_connection);
905 void hfp_accept_synchronous_connection(hfp_connection_t * hfp_connection, bool incoming_eSCO);
906 
907 int hfp_supports_codec(uint8_t codec, int codecs_nr, uint8_t * codecs);
908 void hfp_hf_drop_mSBC_if_eSCO_not_supported(uint8_t * codecs, uint8_t * codecs_nr);
909 void hfp_init_link_settings(hfp_connection_t * hfp_connection, uint8_t eSCO_S4_supported);
910 hfp_link_settings_t hfp_next_link_setting(hfp_link_settings_t current_setting, uint16_t local_sco_packet_types,
911                                           uint16_t remote_sco_packet_types, bool eSCO_s4_supported,
912                                           uint8_t negotiated_codec);
913 hfp_link_settings_t hfp_safe_settings_for_context(bool use_eSCO, uint8_t negotiated_codec, bool secure_connection_in_use);
914 
915 const char * hfp_hf_feature(int index);
916 const char * hfp_ag_feature(int index);
917 
918 void hfp_log_rfcomm_message(const char * tag, uint8_t * packet, uint16_t size);
919 
920 const char * hfp_enhanced_call_dir2str(uint16_t index);
921 const char * hfp_enhanced_call_status2str(uint16_t index);
922 const char * hfp_enhanced_call_mode2str(uint16_t index);
923 const char * hfp_enhanced_call_mpty2str(uint16_t index);
924 
925 /**
926  * @brief Prepare for immediate SCO connection.
927  *        Triggers sending of vendor-specific commands to enable mSBC Codec in Controller
928  * @param hfp_connection
929  */
930 void hfp_prepare_for_sco(hfp_connection_t * hfp_connection);
931 
932 #ifdef ENABLE_BCM_PCM_WBS
933 void hfp_bcm_write_i2spcm_interface_param (hfp_connection_t * hfp_connection);
934 #endif
935 
936 /**
937  * @brief Set packet types for SCO connections
938  * @param common single packet_types: HFP_SCO_PACKET_TYPES_*
939  */
940 void hfp_set_sco_packet_types(uint16_t packet_types);
941 
942 /**
943  * @brief Get packet types for SCO connections
944  * @return packet_types
945  */
946 uint16_t hfp_get_sco_packet_types(void);
947 
948 #if defined __cplusplus
949 }
950 #endif
951 
952 #endif // BTSTACK_HFP_H
953