1 /*
2  * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef __BTC_GAP_BLE_H__
8 #define __BTC_GAP_BLE_H__
9 
10 #include "esp_bt_defs.h"
11 #include "esp_gap_ble_api.h"
12 
13 #if BTC_DYNAMIC_MEMORY == TRUE
14 #if (BLE_42_FEATURE_SUPPORT == TRUE)
15 #include "bta/bta_api.h"
16 extern tBTA_BLE_ADV_DATA *gl_bta_adv_data_ptr;
17 extern tBTA_BLE_ADV_DATA *gl_bta_scan_rsp_data_ptr;
18 #define gl_bta_adv_data     (*gl_bta_adv_data_ptr)
19 #define gl_bta_scan_rsp_data    (*gl_bta_scan_rsp_data_ptr)
20 #endif // BLE_42_FEATURE_SUPPORT
21 #endif
22 
23 #define BLE_ISVALID_PARAM(x, min, max)  (((x) >= (min) && (x) <= (max)))
24 
25 typedef enum {
26 #if (BLE_42_FEATURE_SUPPORT == TRUE)
27     BTC_GAP_BLE_ACT_CFG_ADV_DATA = 0,
28     BTC_GAP_BLE_ACT_SET_SCAN_PARAM,
29     BTC_GAP_BLE_ACT_START_SCAN,
30     BTC_GAP_BLE_ACT_STOP_SCAN,
31     BTC_GAP_BLE_ACT_START_ADV,
32     BTC_GAP_BLE_ACT_STOP_ADV,
33 #endif // #if (BLE_42_FEATURE_SUPPORT == TRUE)
34     BTC_GAP_BLE_ACT_UPDATE_CONN_PARAM = 6,
35     BTC_GAP_BLE_ACT_SET_PKT_DATA_LEN,
36     BTC_GAP_BLE_ACT_SET_RAND_ADDRESS,
37     BTC_GAP_BLE_ACT_CLEAR_RAND_ADDRESS,
38     BTC_GAP_BLE_ACT_CONFIG_LOCAL_PRIVACY,
39     BTC_GAP_BLE_ACT_CONFIG_LOCAL_ICON,
40     BTC_GAP_BLE_ACT_UPDATE_WHITE_LIST,
41     BTC_GAP_BLE_ACT_CLEAR_WHITE_LIST,
42 #if (BLE_42_FEATURE_SUPPORT == TRUE)
43     BTC_GAP_BLE_ACT_SET_CONN_PARAMS,
44 #endif // #if (BLE_42_FEATURE_SUPPORT == TRUE)
45     BTC_GAP_BLE_ACT_SET_DEV_NAME = 15,
46 #if (BLE_42_FEATURE_SUPPORT == TRUE)
47     BTC_GAP_BLE_ACT_CFG_ADV_DATA_RAW,
48     BTC_GAP_BLE_ACT_CFG_SCAN_RSP_DATA_RAW,
49 #endif // #if (BLE_42_FEATURE_SUPPORT == TRUE)
50     BTC_GAP_BLE_ACT_READ_RSSI = 18,
51     BTC_GAP_BLE_SET_ENCRYPTION_EVT,
52     BTC_GAP_BLE_SET_SECURITY_PARAM_EVT,
53     BTC_GAP_BLE_SECURITY_RSP_EVT,
54     BTC_GAP_BLE_PASSKEY_REPLY_EVT,
55     BTC_GAP_BLE_CONFIRM_REPLY_EVT,
56     BTC_GAP_BLE_DISCONNECT_EVT,
57     BTC_GAP_BLE_REMOVE_BOND_DEV_EVT,
58     BTC_GAP_BLE_OOB_REQ_REPLY_EVT,
59     BTC_GAP_BLE_SC_OOB_REQ_REPLY_EVT,
60     BTC_GAP_BLE_SC_CR_OOB_DATA_EVT,
61     BTC_GAP_BLE_UPDATE_DUPLICATE_SCAN_EXCEPTIONAL_LIST,
62     BTC_GAP_BLE_SET_AFH_CHANNELS,
63 #if (BLE_50_FEATURE_SUPPORT == TRUE)
64     BTC_GAP_BLE_READ_PHY,
65     BTC_GAP_BLE_SET_PREFERED_DEF_PHY,
66     BTC_GAP_BLE_SET_DEF_PHY,
67     BTC_GAP_BLE_SET_EXT_ADV_RAND_ADDR,
68     BTC_GAP_BLE_SET_EXT_ADV_PARAMS,
69     BTC_GAP_BLE_CFG_EXT_ADV_DATA_RAW,
70     BTC_GAP_BLE_CFG_EXT_SCAN_RSP_DATA_RAW,
71     BTC_GAP_BLE_EXT_ADV_START,
72     BTC_GAP_BLE_EXT_ADV_STOP,
73     BTC_GAP_BLE_EXT_ADV_SET_REMOVE,
74     BTC_GAP_BLE_EXT_ADV_SET_CLEAR,
75     BTC_GAP_BLE_SET_PERIODIC_ADV_PARAMS,
76     BTC_GAP_BLE_CFG_PERIODIC_ADV_DATA_RAW,
77     BTC_GAP_BLE_PERIODIC_ADV_START,
78     BTC_GAP_BLE_PERIODIC_ADV_STOP,
79     BTC_GAP_BLE_PERIODIC_ADV_CREATE_SYNC,
80     BTC_GAP_BLE_PERIODIC_ADV_SYNC_CANCEL,
81     BTC_GAP_BLE_PERIODIC_ADV_SYNC_TERMINATE,
82     BTC_GAP_BLE_PERIODIC_ADV_ADD_DEV_TO_LIST,
83     BTC_GAP_BLE_PERIODIC_REMOVE_ADD_DEV_FROM_LIST,
84     BTC_GAP_BLE_PERIODIC_CLEAR_DEV,
85     BTC_GAP_BLE_SET_EXT_SCAN_PARAMS,
86     BTC_GAP_BLE_START_EXT_SCAN,
87     BTC_GAP_BLE_STOP_EXT_SCAN,
88     BTC_GAP_BLE_SET_EXT_PEFER_CONNET_PARAMS,
89     BTC_GAP_BLE_DTM_ENH_TX_START,
90     BTC_GAP_BLE_DTM_ENH_RX_START,
91 #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
92     BTC_GAP_BLE_ACT_GET_DEV_NAME,
93 #if (BLE_FEAT_PERIODIC_ADV_SYNC_TRANSFER == TRUE)
94     BTC_GAP_BLE_PERIODIC_ADV_RECV_ENABLE,
95     BTC_GAP_BLE_PERIODIC_ADV_SYNC_TRANS,
96     BTC_GAP_BLE_PERIODIC_ADV_SET_INFO_TRANS,
97     BTC_GAP_BLE_SET_PERIODIC_ADV_SYNC_TRANS_PARAMS,
98 #endif //#if (BLE_FEAT_PERIODIC_ADV_SYNC_TRANSFER == TRUE)
99 #if (BLE_42_FEATURE_SUPPORT == TRUE)
100     BTC_GAP_BLE_DTM_TX_START,
101     BTC_GAP_BLE_DTM_RX_START,
102 #endif // #if (BLE_42_FEATURE_SUPPORT == TRUE)
103     BTC_GAP_BLE_DTM_STOP,
104 #if (BLE_42_FEATURE_SUPPORT == TRUE)
105     BTC_GAP_BLE_ACT_CLEAR_ADV,
106 #endif // #if (BLE_42_FEATURE_SUPPORT == TRUE)
107     BTC_GAP_BLE_ACT_SET_RESOLVABLE_PRIVATE_ADDRESS_TIMEOUT,
108     BTC_GAP_BLE_ACT_ADD_DEVICE_TO_RESOLVING_LIST,
109     BTC_GAP_BLE_ACT_VENDOR_HCI_CMD_EVT,
110     BTC_GAP_BLE_SET_PRIVACY_MODE,
111     BTC_GAP_BLE_SET_CSA_SUPPORT,
112 } btc_gap_ble_act_t;
113 
114 /* btc_ble_gap_args_t */
115 typedef union {
116 #if (BLE_42_FEATURE_SUPPORT == TRUE)
117     //BTC_GAP_BLE_ACT_CFG_ADV_DATA = 0,
118     struct config_adv_data_args {
119         esp_ble_adv_data_t adv_data;
120     } cfg_adv_data;
121     //BTC_GAP_BLE_ACT_SET_SCAN_PARAM,
122     struct set_scan_params_args {
123         esp_ble_scan_params_t  scan_params;
124     } set_scan_param;
125     //BTC_GAP_BLE_ACT_START_SCAN,
126     struct start_scan_args {
127         uint32_t duration;
128     } start_scan;
129     //BTC_GAP_BLE_ACT_STOP_SCAN, no args
130     //BTC_GAP_BLE_ACT_START_ADV,
131     struct start_adv_args {
132         esp_ble_adv_params_t adv_params;
133     } start_adv;
134     //BTC_GAP_BLE_ACT_STOP_ADV, no args
135 #endif // #if (BLE_42_FEATURE_SUPPORT == TRUE)
136     //BTC_GAP_BLE_ACT_UPDATE_CONN_PARAM,
137     struct conn_update_params_args {
138         esp_ble_conn_update_params_t conn_params;
139     } conn_update_params;
140     //BTC_GAP_BLE_ACT_SET_PKT_DATA_LEN
141     struct set_pkt_data_len_args {
142         esp_bd_addr_t remote_device;
143         uint16_t tx_data_length;
144     } set_pkt_data_len;
145     //BTC_GAP_BLE_ACT_SET_RAND_ADDRESS,
146     struct set_rand_addr_args {
147         esp_bd_addr_t rand_addr;
148     } set_rand_addr;
149     // BTC_GAP_BLE_ACT_SET_RESOLVABLE_PRIVATE_ADDRESS_TIMEOUT
150     struct set_rpa_timeout_args {
151         uint16_t rpa_timeout;
152     } set_rpa_timeout;
153     //BTC_GAP_BLE_ACT_ADD_DEVICE_TO_RESOLVING_LIST
154     struct add_dev_to_resolving_list_args {
155         esp_bd_addr_t addr;
156         uint8_t addr_type;
157         uint8_t irk[PEER_IRK_LEN];
158     } add_dev_to_resolving_list;
159     //BTC_GAP_BLE_ACT_CONFIG_LOCAL_PRIVACY,
160     struct cfg_local_privacy_args {
161         bool privacy_enable;
162     } cfg_local_privacy;
163     //BTC_GAP_BLE_ACT_CONFIG_LOCAL_ICON,
164     struct cfg_local_icon_args {
165         uint16_t icon;
166     } cfg_local_icon;
167     //BTC_GAP_BLE_ACT_UPDATE_WHITE_LIST
168     struct update_white_list_args {
169         bool add_remove;
170         esp_bd_addr_t remote_bda;
171         esp_ble_wl_addr_type_t wl_addr_type;
172     } update_white_list;
173 #if (BLE_42_FEATURE_SUPPORT == TRUE)
174     //BTC_GAP_BLE_UPDATE_DUPLICATE_SCAN_EXCEPTIONAL_LIST
175     struct update_duplicate_exceptional_list_args {
176         uint8_t  subcode;
177         uint32_t  info_type;
178         esp_duplicate_info_t device_info;
179     } update_duplicate_exceptional_list;
180     //BTC_GAP_BLE_ACT_SET_CONN_PARAMS
181     struct set_conn_params_args {
182         esp_bd_addr_t bd_addr;
183         uint16_t min_conn_int;
184         uint16_t max_conn_int;
185         uint16_t slave_latency;
186         uint16_t supervision_tout;
187     } set_conn_params;
188 #endif // #if (BLE_42_FEATURE_SUPPORT == TRUE)
189     //BTC_GAP_BLE_ACT_SET_DEV_NAME,
190     struct set_dev_name_args {
191 #define ESP_GAP_DEVICE_NAME_MAX (32)
192         char device_name[ESP_GAP_DEVICE_NAME_MAX + 1];
193     } set_dev_name;
194 #if (BLE_42_FEATURE_SUPPORT == TRUE)
195     //BTC_GAP_BLE_ACT_CFG_ADV_DATA_RAW,
196     struct config_adv_data_raw_args {
197         uint8_t *raw_adv;
198         uint32_t raw_adv_len;
199     } cfg_adv_data_raw;
200     //BTC_GAP_BLE_ACT_CFG_SCAN_RSP_DATA_RAW,
201     struct config_scan_rsp_data_raw_args {
202         uint8_t *raw_scan_rsp;
203         uint32_t raw_scan_rsp_len;
204     } cfg_scan_rsp_data_raw;
205 #endif // #if (BLE_42_FEATURE_SUPPORT == TRUE)
206     //BTC_GAP_BLE_SET_ENCRYPTION_EVT
207     struct set_encryption_args {
208         esp_bd_addr_t bd_addr;
209         esp_ble_sec_act_t sec_act;
210     } set_encryption;
211     //BTC_GAP_BLE_SET_SECURITY_PARAM_EVT
212     struct set_security_param_args {
213         esp_ble_sm_param_t param_type;
214         uint8_t len;
215         uint8_t *value;
216     } set_security_param;
217     //BTC_GAP_BLE_SECURITY_RSP_EVT
218     struct enc_rsp_args {
219         esp_bd_addr_t bd_addr;
220         bool accept;
221     } sec_rsp;
222     //BTC_GAP_BLE_PASSKEY_REPLY_EVT
223     struct enc_passkey_reply_args {
224         esp_bd_addr_t bd_addr;
225         bool accept;
226         uint32_t passkey;
227     } enc_passkey_replay;
228     //BTC_GAP_BLE_CONFIRM_REPLY_EVT
229     struct enc_comfirm_reply_args {
230         esp_bd_addr_t bd_addr;
231         bool accept;
232     } enc_comfirm_replay;
233     //BTC_GAP_BLE_OOB_DATA_REPLY_EVT
234     struct oob_req_reply_args {
235         esp_bd_addr_t bd_addr;
236         uint8_t len;
237         uint8_t *p_value;
238     } oob_req_reply;
239     struct sc_oob_req_reply_args {
240         esp_bd_addr_t bd_addr;
241         uint8_t *p_c;
242         uint8_t *p_r;
243     } sc_oob_req_reply;
244     //BTC_GAP_BLE_DISCONNECT_EVT
245     struct disconnect_args {
246         esp_bd_addr_t remote_device;
247     } disconnect;
248     //BTC_GAP_BLE_REMOVE_BOND_DEV_EVT
249     struct remove_bond_device_args {
250         esp_bd_addr_t bd_addr;
251     } remove_bond_device;
252     //BTC_GAP_BLE_ACT_READ_RSSI
253     struct read_rssi_args {
254         esp_bd_addr_t remote_addr;
255     } read_rssi;
256     // BTC_GAP_BLE_SET_AFH_CHANNELS
257     struct set_channels_args {
258        esp_gap_ble_channels channels;
259     } set_channels;
260     struct dtm_tx_start_args {
261         uint8_t tx_channel;
262         uint8_t len_of_data;
263         uint8_t pkt_payload;
264     } dtm_tx_start;
265     struct dtm_rx_start_args {
266         uint8_t rx_channel;
267     } dtm_rx_start;
268     //BTC_DEV_VENDOR_HCI_CMD_EVT
269     struct vendor_cmd_send_args {
270         uint16_t  opcode;
271         uint8_t  param_len;
272         uint8_t *p_param_buf;
273     } vendor_cmd_send;
274     // BTC_GAP_BLE_SET_PRIVACY_MODE
275     struct set_privacy_mode {
276         esp_ble_addr_type_t addr_type;
277         esp_bd_addr_t addr;
278         uint8_t privacy_mode;
279     } set_privacy_mode;
280     // BTC_GAP_BLE_SET_CSA_SUPPORT
281     struct set_csa_support_args {
282         uint8_t csa_select;
283     } set_csa_support;
284 } btc_ble_gap_args_t;
285 
286 #if (BLE_50_FEATURE_SUPPORT == TRUE)
287 typedef union {
288     struct read_phy_args {
289         esp_bd_addr_t bd_addr;
290     } read_phy;
291 
292     struct set_perf_def_phy_args {
293         esp_ble_gap_phy_mask_t tx_phy_mask;
294         esp_ble_gap_phy_mask_t rx_phy_mask;
295     } set_perf_def_phy;
296 
297     struct set_def_phy_args {
298         esp_bd_addr_t bd_addr;
299         esp_ble_gap_all_phys_t all_phys_mask;
300         esp_ble_gap_phy_mask_t tx_phy_mask;
301         esp_ble_gap_phy_mask_t rx_phy_mask;
302         uint16_t phy_options;
303     } set_def_phy;
304 
305     struct ext_adv_set_rand_addr_args {
306         uint8_t instance;
307         esp_bd_addr_t rand_addr;
308     } ext_adv_set_rand_addr;
309 
310     struct ext_adv_set_params_args {
311         uint8_t instance;
312         esp_ble_gap_ext_adv_params_t params;
313     } ext_adv_set_params;
314 
315     struct ext_adv_cfg_data_args {
316         uint8_t instance;
317         uint16_t length;
318         uint8_t *data;
319     } ext_adv_cfg_data;
320 
321     struct ext_adv_cfg_scan_rsp_args {
322         uint8_t instance;
323         uint16_t length;
324         uint8_t *data;
325     } cfg_scan_rsp;
326 
327     struct ext_adv_start_args {
328         uint8_t num_adv;
329         esp_ble_gap_ext_adv_t *ext_adv;
330     } ext_adv_start;
331 
332     struct ext_adv_stop_args {
333         uint8_t num_adv;
334         uint8_t *ext_adv_inst;
335     } ext_adv_stop;
336 
337     struct ext_adv_set_remove_args {
338         uint8_t instance;
339     } ext_adv_set_remove;
340 
341     struct peridic_adv_set_params_args {
342         uint8_t instance;
343         esp_ble_gap_periodic_adv_params_t params;
344     } peridic_adv_set_params;
345 
346     struct periodic_adv_cfg_data_args {
347         uint8_t instance;
348         uint16_t len;
349         uint8_t *data;
350         bool only_update_did;
351     } periodic_adv_cfg_data;
352 
353     struct periodic_adv_start_args {
354         bool    include_adi;
355         uint8_t instance;
356     } periodic_adv_start;
357 
358     struct periodic_adv_stop_args {
359         uint8_t instance;
360     } periodic_adv_stop;
361 
362     struct periodic_adv_create_sync_args {
363         esp_ble_gap_periodic_adv_sync_params_t params;
364     } periodic_adv_create_sync;
365 
366     struct periodic_adv_sync_term_args {
367         uint16_t sync_handle;
368     } periodic_adv_sync_term;
369 
370     struct periodic_adv_add_dev_args {
371         esp_ble_addr_type_t addr_type;
372         esp_bd_addr_t addr;
373         uint16_t sid;
374     } periodic_adv_add_dev;
375 
376     struct periodic_adv_remove_dev_args {
377         esp_ble_addr_type_t addr_type;
378         esp_bd_addr_t addr;
379         uint16_t sid;
380     } periodic_adv_remove_dev;
381 
382     struct set_ext_scan_params_args {
383         esp_ble_ext_scan_params_t params;
384     } set_ext_scan_params;
385 
386     struct start_ext_scan_args {
387         uint32_t duration;
388         uint16_t period;
389     } start_ext_scan;
390 
391     struct set_ext_conn_params_args {
392         esp_bd_addr_t addr;
393         uint8_t phy_mask;
394         esp_ble_gap_conn_params_t phy_1m_conn_params;
395         esp_ble_gap_conn_params_t phy_2m_conn_params;
396         esp_ble_gap_conn_params_t phy_coded_conn_params;
397     } set_ext_conn_params;
398 
399     struct ext_conn_args {
400         esp_ble_addr_type_t own_addr_type;
401         esp_bd_addr_t peer_addr;
402     } ext_conn;
403 
404 #if (BLE_FEAT_PERIODIC_ADV_SYNC_TRANSFER == TRUE)
405     struct periodic_adv_recv_en_args {
406         uint16_t sync_handle;
407         uint8_t enable;
408     } periodic_adv_recv_en;
409 
410     struct periodic_adv_sync_trans_args {
411         esp_bd_addr_t addr;
412         uint16_t service_data;
413         uint16_t sync_handle;
414     } periodic_adv_sync_trans;
415 
416     struct periodic_adv_set_info_trans_args {
417         esp_bd_addr_t addr;
418         uint16_t service_data;
419         uint16_t adv_handle;
420     } periodic_adv_set_info_trans;
421 
422     struct set_periodic_adv_sync_trans_params_args {
423         esp_bd_addr_t addr;
424         esp_ble_gap_past_params_t params;
425     } set_periodic_adv_sync_trans_params;
426 #endif
427      struct dtm_enh_tx_start_args {
428         uint8_t tx_channel;
429         uint8_t len_of_data;
430         uint8_t pkt_payload;
431         uint8_t phy;
432     } dtm_enh_tx_start;
433     struct dtm_enh_rx_start_args {
434         uint8_t rx_channel;
435         uint8_t phy;
436         uint8_t modulation_index;
437     } dtm_enh_rx_start;
438 } btc_ble_5_gap_args_t;
439 #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
440 
441 void btc_gap_ble_call_handler(btc_msg_t *msg);
442 void btc_gap_ble_cb_handler(btc_msg_t *msg);
443 void btc_get_whitelist_size(uint16_t *length);
444 void btc_gap_ble_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src);
445 void btc_gap_ble_arg_deep_free(btc_msg_t *msg);
446 void btc_gap_ble_cb_deep_free(btc_msg_t *msg);
447 void btc_gap_ble_cb_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src);
448 void btc_gap_callback_init(void);
449 bool btc_gap_ble_init(void);
450 void btc_gap_ble_deinit(void);
451 void btc_adv_list_init(void);
452 void btc_adv_list_deinit(void);
453 
454 #endif /* __BTC_GAP_BLE_H__ */
455