1 /*
2  * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3  */
4 #ifndef __HFP_API_H__
5 #define __HFP_API_H__
6 
7 #include "bluetooth.h"
8 #include "conmgr_api.h"
9 #include "hci_api.h"
10 
11 #ifdef __BT_ONE_BRING_TWO__
12 #define HF_CHANNEL_NUM     2
13 #else
14 #define HF_CHANNEL_NUM     1
15 #endif
16 
17 typedef void *hf_chan_handle_t;
18 
19 /* start of btif_audio_state_t*/
20 typedef enum {
21     BTIF_HF_AUDIO_DISCON = 0,
22     BTIF_HF_AUDIO_CON = 1,
23 } btif_audio_state_t;
24 
25 /* End of btif_audio_state_t*/
26 
27 /*---------------------------------------------------------------------------
28  * btif_hf_call_held_state type
29  *
30  */
31 typedef uint8_t btif_hf_call_held_state;
32 
33 #define BTIF_HF_CALL_HELD_NONE      0
34 
35 #define BTIF_HF_CALL_HELD_ACTIVE    1
36 
37 #define BTIF_HF_CALL_HELD_NO_ACTIVE 2
38 
39 /* End of btif_hf_call_held_state */
40 
41 /* start the definition of btif_hf_call_setup_t*/
42 typedef uint8_t btif_hf_call_setup_t;
43 
44 #define BTIF_HF_CALL_SETUP_NONE   0
45 
46 #define BTIF_HF_CALL_SETUP_IN     1
47 
48 #define BTIF_HF_CALL_SETUP_OUT    2
49 
50 #define BTIF_HF_CALL_SETUP_ALERT  3
51 /* end btif_hf_call_setup_t*/
52 
53 /* start the definition of btif_hf_call_active_t */
54 typedef uint8_t btif_hf_call_active_t;
55 
56 #define BTIF_HF_CALL_NONE           0
57 
58 #define BTIF_HF_CALL_ACTIVE         1
59 /* end of btif_hf_call_active_t */
60 
61 typedef enum {
62     BTIF_HF_SCO_CODEC_NONE = 0,
63     BTIF_HF_SCO_CODEC_CVSD = 1,
64     BTIF_HF_SCO_CODEC_MSBC = 2,
65 } hfp_sco_codec_t;
66 
67 typedef enum {
68     BTIF_HF_HOLD_RELEASE_HELD_CALLS = 0,
69     BTIF_HF_HOLD_RELEASE_ACTIVE_CALLS = 1,
70     BTIF_HF_HOLD_HOLD_ACTIVE_CALLS = 2,
71     BTIF_HF_HOLD_ADD_HELD_CALL = 3,
72     BTIF_HF_HOLD_CALL_TRANSFER = 4,
73 } btif_hf_hold_call_t;
74 
75 typedef enum {
76     BTIF_HF_STATE_CLOSED = 0,
77     BTIF_HF_STATE_CONN_PENDING = 1,
78     BTIF_HF_STATE_CONN_INCOMING = 2,
79     BTIF_HF_STATE_NEGOTIATE = 3,
80     BTIF_HF_STATE_OPEN = 4,
81     BTIF_HF_STATE_DISC = 5,
82 } btif_hf_chan_state_t;
83 
84 typedef uint16_t hf_gateway_version;
85 
86 /* Unable to determine the Hands Free Profile version that is supported */
87 #define BTIF_HF_GW_VERSION_UNKNOWN 0x0000
88 
89 /* Supports Version 0.96 of the Hands Free Profile */
90 #define BTIF_HF_GW_VERSION_0_96    0x0100
91 
92 /* Supports Version 1.0 of the Hands Free Profile */
93 #define BTIF_HF_GW_VERSION_1_0     0x0101
94 
95 /* Supports Version 1.5 of the Hands Free Profile */
96 #define BTIF_HF_GW_VERSION_1_5     0x0105
97 
98 #define BTIF_HF_GW_VERSION_1_6     0x0106
99 
100 #define BTIF_HF_GW_VERSION_1_7     0x0107
101 
102 /* End of hf_gateway_version */
103 
104 typedef enum {
105     BTIF_HF_EVENT_SERVICE_CONNECT_REQ = 0,
106     BTIF_HF_EVENT_SERVICE_CONNECTED = 1,
107     BTIF_HF_EVENT_SERVICE_DISCONNECTED = 2,
108     BTIF_HF_EVENT_AUDIO_CONNECTED = 3,
109     BTIF_HF_EVENT_AUDIO_DISCONNECTED = 4,
110     BTIF_HF_EVENT_AUDIO_DATA = 5,
111     BTIF_HF_EVENT_AUDIO_DATA_SENT = 6,
112     BTIF_HF_EVENT_GATEWAY_FEATURES = 7,
113     BTIF_HF_EVENT_GW_HOLD_FEATURES = 8,
114     BTIF_HF_EVENT_CALL_STATE = 9,
115     BTIF_HF_EVENT_CALLER_ID = 10,
116     BTIF_HF_EVENT_CALL_LISTING_ENABLED = 11,
117     BTIF_HF_EVENT_RESPONSE_HOLD_APPL = 12,
118     BTIF_HF_EVENT_CALL_IND = 13,
119     BTIF_HF_EVENT_CALLSETUP_IND = 14,
120     BTIF_HF_EVENT_CALLHELD_IND = 15,
121     BTIF_HF_EVENT_RING_IND = 16,
122     BTIF_HF_EVENT_WAIT_NOTIFY = 17,
123     BTIF_HF_EVENT_CALLER_ID_NOTIFY = 18,
124     BTIF_HF_EVENT_CURRENT_CALL_STATE = 19,
125     BTIF_HF_EVENT_RESPONSE_HOLD = 20,
126     BTIF_HF_EVENT_SERVICE_IND = 21,
127     BTIF_HF_EVENT_BATTERY_IND = 22,
128     BTIF_HF_EVENT_SIGNAL_IND = 23,
129     BTIF_HF_EVENT_ROAM_IND = 24,
130     BTIF_HF_EVENT_SMS_IND = 25,
131     BTIF_HF_EVENT_VOICE_REC_STATE = 26,
132     BTIF_HF_EVENT_VOICE_TAG_NUMBER = 27,
133     BTIF_HF_EVENT_SPEAKER_VOLUME = 28,
134     BTIF_HF_EVENT_MIC_VOLUME = 29,
135     BTIF_HF_EVENT_IN_BAND_RING = 30,
136     BTIF_HF_EVENT_NETWORK_OPERATOR = 31,
137     BTIF_HF_EVENT_SUBSCRIBER_NUMBER = 32,
138     BTIF_HF_EVENT_NO_CARRIER = 33,
139     BTIF_HF_EVENT_BUSY = 34,
140     BTIF_HF_EVENT_NO_ANSWER = 35,
141     BTIF_HF_EVENT_DELAYED = 36,
142     BTIF_HF_EVENT_BLACKLISTED = 37,
143     BTIF_HF_EVENT_PHONEBOOK_STORAGE = 38,
144     BTIF_HF_EVENT_PHONEBOOK_INFO = 39,
145     BTIF_HF_EVENT_PHONEBOOK_SIZE = 40,
146     BTIF_HF_EVENT_PHONEBOOK_ENTRY = 41,
147     BTIF_HF_EVENT_AT_RESULT_DATA = 42,
148     BTIF_HF_EVENT_COMMAND_COMPLETE = 43,
149     BTIF_HF_EVENT_SIRI_STATUS = 44,
150     BTIF_HF_EVENT_READ_AG_INDICATORS_STATUS = 45,
151     BTIF_HF_EVENT_BES_TEST = 46,
152     BTIF_HF_EVENT_SELECT_CHANNEL = 47,
153     BTIF_HF_EVENT_RFCOMM_CONNECTED = 48,
154 } hf_event_t;
155 
156 /* End of hf_event_t*/
157 
158 struct hfp_context {
159     uint16_t remote_dev_hcihandle;
160     /*TODO:should remove the status */
161     bt_status_t status;         /* Status of the callback event             */
162     btif_hf_chan_state_t state;
163     bt_bdaddr_t remote_dev_bdaddr;
164     const char *call_number;
165     btif_hf_call_active_t call;
166     btif_hf_call_setup_t call_setup;
167     btif_hf_call_held_state  call_held;
168     hf_event_t event;
169     uint8_t *audio_data;
170     uint16_t audio_data_len;
171     uint8_t error_code;
172     uint8_t disc_reason;
173     uint8_t disc_reason_saved;
174     uint32_t speaker_volume;
175     uint32_t voice_rec_state;
176     uint32_t **chan_sel_channel;
177     btif_remote_device_t *chan_sel_remDev;
178     char *ptr;
179 };
180 
181 struct hfp_vendor_info {
182     uint16_t vendor_id;
183     uint16_t product_id;
184     uint16_t version_id;
185     uint16_t feature_id;  //default is 3, if you do not known, set it 0
186 };
187 
188 typedef void (*hf_event_cb_t) (hf_chan_handle_t chan, struct hfp_context * ctx);
189 
190 #ifdef __cplusplus
191 extern "C" {
192 #endif
193 
194     /*
195      *  AT+XAPL=[vendorID]-[productID]-[version],[features]
196      *  if you set features as 0,this api will ignore your features setting and use default value
197      */
198     void btif_hf_init_vendor_info(struct hfp_vendor_info *info_ptr);
199 
200     int hfp_hfcommand_mempool_init(void);
201 
202     int btif_hfp_initialize(void);
203 
204     int btif_hf_register_callback(hf_event_cb_t callback);
205 
206     hf_gateway_version btif_hf_get_version(hf_chan_handle_t chan_h);
207 
208     bt_status_t btif_hf_update_indicators_batt_level(hf_chan_handle_t chan_h, uint8_t assigned_num, uint8_t level);
209 
210     bt_status_t btif_hf_batt_report(hf_chan_handle_t chan_h, uint8_t level);
211 
212     bt_status_t btif_hf_siri_report(hf_chan_handle_t chan_h);
213 
214     bt_status_t btif_hf_enable_voice_recognition(hf_chan_handle_t chan_h, bool en);
215 
216     bt_status_t btif_hf_batt_report(hf_chan_handle_t chan_h, uint8_t level);
217 
218     bool btif_hf_is_voice_rec_active(hf_chan_handle_t chan_h);
219 
220     bt_status_t btif_hf_disable_nrec(hf_chan_handle_t chan_h);
221 
222     bt_status_t btif_hf_report_speaker_volume(hf_chan_handle_t chan_h, uint8_t gain);
223 
224     bt_status_t btif_hf_send_at_cmd(hf_chan_handle_t chan_h, const char *at_str);
225 
226     bt_status_t btif_hf_list_current_calls(hf_chan_handle_t chan_h);
227 
228     bool btif_hf_is_hf_indicators_support(hf_chan_handle_t chan_h);
229 
230     bool btif_hf_is_batt_report_support(hf_chan_handle_t chan_h);
231 
232     bt_status_t btif_hf_force_disconnect_sco(hf_chan_handle_t chan_h);
233 
234     void btif_hf_set_negotiated_codec(hf_chan_handle_t chan_h, hfp_sco_codec_t codec);
235 
236     hfp_sco_codec_t btif_hf_get_negotiated_codec(hf_chan_handle_t chan_h);
237 
238     bt_status_t btif_hf_answer_call(hf_chan_handle_t chan_h);
239 
240     bt_status_t btif_hf_hang_up_call(hf_chan_handle_t chan_h);
241 
242     bt_status_t btif_hf_redial_call(hf_chan_handle_t chan_h);
243 
244     bt_status_t btif_hf_dial_number(hf_chan_handle_t chan_h, uint8_t *number, uint16_t len);
245 
246     bt_status_t btif_hf_disc_audio_link(hf_chan_handle_t chan_h);
247 
248     bt_status_t btif_hf_create_audio_link(hf_chan_handle_t chan_h);
249 
250     bt_status_t btif_hf_call_hold(hf_chan_handle_t chan_h, btif_hf_hold_call_t action,
251                                   uint8_t index);
252     bt_status_t btif_hf_switch_calls(hf_chan_handle_t hangup_chan_h,
253                                      hf_chan_handle_t answer_chan_h);
254 
255     hf_chan_handle_t btif_get_hf_chan_by_address(bt_bdaddr_t *bdaddr);
256 
257     uint8_t btif_get_hf_chan_audio_up_flag(hf_chan_handle_t chan_h);
258 
259     btif_hf_chan_state_t btif_get_hf_chan_state(hf_chan_handle_t chan_h);
260 
261     bool btif_hf_check_AudioConnect_status(hf_chan_handle_t chan_h);
262 
263     hf_chan_handle_t btif_hf_create_channel(void);
264 
265     int btif_hf_init_channel(hf_chan_handle_t chan_h);
266 
267     bt_status_t btif_hf_disconnect_service_link(hf_chan_handle_t chan_h);
268 
269     bt_status_t btif_hf_create_service_link(hf_chan_handle_t chan_h, bt_bdaddr_t * bt_addr);
270 
271     btif_cmgr_handler_t *btif_hf_get_chan_manager_handler(hf_chan_handle_t chan_h);
272 
273     bt_status_t btif_hfp_switch_sco(hf_chan_handle_t chan_h);
274 
275     bt_status_t btif_hf_set_master_role(hf_chan_handle_t chan_h, bool role);
276 
277     bt_status_t btif_hf_enable_sniff_mode(hf_chan_handle_t chan_h, bool enable);
278 
279     bool btif_hf_get_remote_bdaddr(hf_chan_handle_t chan_h, bt_bdaddr_t *bdaddr_p);
280 
281     uint16_t btif_hf_get_sco_hcihandle(hf_chan_handle_t chan_h);
282 
283     btif_hci_handle_t btif_hf_get_remote_hci_handle(hf_chan_handle_t chan_h);
284 
285     bool btif_hf_is_acl_connected(hf_chan_handle_t chan_h);
286 
287     btif_remote_device_t *btif_hf_cmgr_get_remote_device(hf_chan_handle_t chan_h);
288 
289     bt_status_t btif_hf_send_audio_data(hf_chan_handle_t chan_h, btif_bt_packet_t *packet);
290 
291     bt_status_t btif_hf_is_inbandring_enabled(hf_chan_handle_t chan_h);
292 
293     uint32_t btif_hfp_profile_save_ctx(btif_remote_device_t *rem_dev, uint8_t *buf, uint32_t buf_len);
294 
295     uint32_t btif_hfp_profile_restore_ctx(uint8_t *buf, uint32_t buf_len);
296 
297 #if defined(IBRT)
298     bt_status_t btif_hf_sync_conn_audio_connected(hfp_sco_codec_t codec,uint16_t conhdl);
299     bt_status_t btif_hf_sync_conn_audio_disconnected(uint16_t conhdl);
300     btif_hf_chan_state_t btif_hfp_profile_channel_state(uint8_t bt_device_id);
301 #endif
302 
303     bt_status_t btif_hf_indicators_1(hf_chan_handle_t chan_h);
304     bt_status_t btif_hf_indicators_2(hf_chan_handle_t chan_h);
305     bt_status_t btif_hf_indicators_3(hf_chan_handle_t chan_h);
306 
307 #if defined(HFP_MOBILE_AG_ROLE)
308     bt_status_t btif_ag_create_service_link(hf_chan_handle_t chan_h, bt_bdaddr_t * bt_addr);
309     bt_status_t btif_ag_disconnect_service_link(hf_chan_handle_t chan_h);
310     bt_status_t btif_ag_create_audio_link(hf_chan_handle_t chan_h);
311     bt_status_t btif_ag_disc_audio_link(hf_chan_handle_t chan_h);
312     bt_status_t btif_ag_send_call_active_status(hf_chan_handle_t chan_h, bool active);
313     bt_status_t btif_ag_send_callsetup_status(hf_chan_handle_t chan_h, uint8_t status);
314     bt_status_t btif_ag_send_callheld_status(hf_chan_handle_t chan_h, uint8_t status);
315     bt_status_t btif_ag_send_calling_ring(hf_chan_handle_t chan_h, const char* number);
316     bt_status_t btif_ag_set_speaker_gain(hf_chan_handle_t chan_h, uint8_t volume);
317     bt_status_t btif_ag_set_microphone_gain(hf_chan_handle_t chan_h, uint8_t volume);
318     bt_status_t btif_ag_send_result_code(hf_chan_handle_t chan_h, const char *data, int len);
319     bt_status_t btif_ag_register_mobile_module(hf_chan_handle_t chan_h, struct hfp_mobile_module_handler* handler);
320 #endif
321 
322 #ifdef __cplusplus
323 }
324 #endif                          /*  */
325 #endif /*__HFP_API_H__*/
326