1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef __BTC_GAP_BLE_H__
16 #define __BTC_GAP_BLE_H__
17 
18 #include "esp_bt_defs.h"
19 #include "esp_gap_ble_api.h"
20 
21 #if BTC_DYNAMIC_MEMORY == TRUE
22 #include "bta/bta_api.h"
23 extern tBTA_BLE_ADV_DATA *gl_bta_adv_data_ptr;
24 extern tBTA_BLE_ADV_DATA *gl_bta_scan_rsp_data_ptr;
25 #define gl_bta_adv_data     (*gl_bta_adv_data_ptr)
26 #define gl_bta_scan_rsp_data    (*gl_bta_scan_rsp_data_ptr)
27 #endif
28 
29 #define BLE_ISVALID_PARAM(x, min, max)  (((x) >= (min) && (x) <= (max)) || ((x) == ESP_BLE_CONN_PARAM_UNDEF))
30 
31 typedef enum {
32 #if (BLE_42_FEATURE_SUPPORT == TRUE)
33     BTC_GAP_BLE_ACT_CFG_ADV_DATA = 0,
34     BTC_GAP_BLE_ACT_SET_SCAN_PARAM,
35     BTC_GAP_BLE_ACT_START_SCAN,
36     BTC_GAP_BLE_ACT_STOP_SCAN,
37     BTC_GAP_BLE_ACT_START_ADV,
38     BTC_GAP_BLE_ACT_STOP_ADV,
39 #endif // #if (BLE_42_FEATURE_SUPPORT == TRUE)
40     BTC_GAP_BLE_ACT_UPDATE_CONN_PARAM = 6,
41     BTC_GAP_BLE_ACT_SET_PKT_DATA_LEN,
42     BTC_GAP_BLE_ACT_SET_RAND_ADDRESS,
43     BTC_GAP_BLE_ACT_CLEAR_RAND_ADDRESS,
44     BTC_GAP_BLE_ACT_CONFIG_LOCAL_PRIVACY,
45     BTC_GAP_BLE_ACT_CONFIG_LOCAL_ICON,
46     BTC_GAP_BLE_ACT_UPDATE_WHITE_LIST,
47     BTC_GAP_BLE_ACT_CLEAR_WHITE_LIST,
48 #if (BLE_42_FEATURE_SUPPORT == TRUE)
49     BTC_GAP_BLE_ACT_SET_CONN_PARAMS,
50 #endif // #if (BLE_42_FEATURE_SUPPORT == TRUE)
51     BTC_GAP_BLE_ACT_SET_DEV_NAME = 15,
52 #if (BLE_42_FEATURE_SUPPORT == TRUE)
53     BTC_GAP_BLE_ACT_CFG_ADV_DATA_RAW,
54     BTC_GAP_BLE_ACT_CFG_SCAN_RSP_DATA_RAW,
55 #endif // #if (BLE_42_FEATURE_SUPPORT == TRUE)
56     BTC_GAP_BLE_ACT_READ_RSSI = 18,
57     BTC_GAP_BLE_SET_ENCRYPTION_EVT,
58     BTC_GAP_BLE_SET_SECURITY_PARAM_EVT,
59     BTC_GAP_BLE_SECURITY_RSP_EVT,
60     BTC_GAP_BLE_PASSKEY_REPLY_EVT,
61     BTC_GAP_BLE_CONFIRM_REPLY_EVT,
62     BTC_GAP_BLE_DISCONNECT_EVT,
63     BTC_GAP_BLE_REMOVE_BOND_DEV_EVT,
64     BTC_GAP_BLE_OOB_REQ_REPLY_EVT,
65     BTC_GAP_BLE_UPDATE_DUPLICATE_SCAN_EXCEPTIONAL_LIST,
66     BTC_GAP_BLE_SET_AFH_CHANNELS,
67 #if (BLE_50_FEATURE_SUPPORT == TRUE)
68     BTC_GAP_BLE_READ_PHY,
69     BTC_GAP_BLE_SET_PREFERED_DEF_PHY,
70     BTC_GAP_BLE_SET_DEF_PHY,
71     BTC_GAP_BLE_SET_EXT_ADV_RAND_ADDR,
72     BTC_GAP_BLE_SET_EXT_ADV_PARAMS,
73     BTC_GAP_BLE_CFG_EXT_ADV_DATA_RAW,
74     BTC_GAP_BLE_CFG_EXT_SCAN_RSP_DATA_RAW,
75     BTC_GAP_BLE_EXT_ADV_START,
76     BTC_GAP_BLE_EXT_ADV_STOP,
77     BTC_GAP_BLE_EXT_ADV_SET_REMOVE,
78     BTC_GAP_BLE_EXT_ADV_SET_CLEAR,
79     BTC_GAP_BLE_SET_PERIODIC_ADV_PARAMS,
80     BTC_GAP_BLE_CFG_PERIODIC_ADV_DATA_RAW,
81     BTC_GAP_BLE_PERIODIC_ADV_START,
82     BTC_GAP_BLE_PERIODIC_ADV_STOP,
83     BTC_GAP_BLE_PERIODIC_ADV_CREATE_SYNC,
84     BTC_GAP_BLE_PERIODIC_ADV_SYNC_CANCEL,
85     BTC_GAP_BLE_PERIODIC_ADV_SYNC_TERMINATE,
86     BTC_GAP_BLE_PERIODIC_ADV_ADD_DEV_TO_LIST,
87     BTC_GAP_BLE_PERIODIC_REMOVE_ADD_DEV_FROM_LIST,
88     BTC_GAP_BLE_PERIODIC_CLEAR_DEV,
89     BTC_GAP_BLE_SET_EXT_SCAN_PARAMS,
90     BTC_GAP_BLE_START_EXT_SCAN,
91     BTC_GAP_BLE_STOP_EXT_SCAN,
92     BTC_GAP_BLE_SET_EXT_PEFER_CONNET_PARAMS,
93 #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
94 } btc_gap_ble_act_t;
95 
96 /* btc_ble_gap_args_t */
97 typedef union {
98 #if (BLE_42_FEATURE_SUPPORT == TRUE)
99     //BTC_GAP_BLE_ACT_CFG_ADV_DATA = 0,
100     struct config_adv_data_args {
101         esp_ble_adv_data_t adv_data;
102     } cfg_adv_data;
103     //BTC_GAP_BLE_ACT_SET_SCAN_PARAM,
104     struct set_scan_params_args {
105         esp_ble_scan_params_t  scan_params;
106     } set_scan_param;
107     //BTC_GAP_BLE_ACT_START_SCAN,
108     struct start_scan_args {
109         uint32_t duration;
110     } start_scan;
111     //BTC_GAP_BLE_ACT_STOP_SCAN, no args
112     //BTC_GAP_BLE_ACT_START_ADV,
113     struct start_adv_args {
114         esp_ble_adv_params_t adv_params;
115     } start_adv;
116     //BTC_GAP_BLE_ACT_STOP_ADV, no args
117 #endif // #if (BLE_42_FEATURE_SUPPORT == TRUE)
118     //BTC_GAP_BLE_ACT_UPDATE_CONN_PARAM,
119     struct conn_update_params_args {
120         esp_ble_conn_update_params_t conn_params;
121     } conn_update_params;
122     //BTC_GAP_BLE_ACT_SET_PKT_DATA_LEN
123     struct set_pkt_data_len_args {
124         esp_bd_addr_t remote_device;
125         uint16_t tx_data_length;
126     } set_pkt_data_len;
127     //BTC_GAP_BLE_ACT_SET_RAND_ADDRESS,
128     struct set_rand_addr_args {
129         esp_bd_addr_t rand_addr;
130     } set_rand_addr;
131     //BTC_GAP_BLE_ACT_CONFIG_LOCAL_PRIVACY,
132     struct cfg_local_privacy_args {
133         bool privacy_enable;
134     } cfg_local_privacy;
135     //BTC_GAP_BLE_ACT_CONFIG_LOCAL_ICON,
136     struct cfg_local_icon_args {
137         uint16_t icon;
138     } cfg_local_icon;
139     //BTC_GAP_BLE_ACT_UPDATE_WHITE_LIST
140     struct update_white_list_args {
141         bool add_remove;
142         esp_bd_addr_t remote_bda;
143         esp_ble_wl_addr_type_t wl_addr_type;
144     } update_white_list;
145 #if (BLE_42_FEATURE_SUPPORT == TRUE)
146     //BTC_GAP_BLE_UPDATE_DUPLICATE_SCAN_EXCEPTIONAL_LIST
147     struct update_duplicate_exceptional_list_args {
148         uint8_t  subcode;
149         uint32_t  info_type;
150         esp_duplicate_info_t device_info;
151     } update_duplicate_exceptional_list;
152     //BTC_GAP_BLE_ACT_SET_CONN_PARAMS
153     struct set_conn_params_args {
154         esp_bd_addr_t bd_addr;
155         uint16_t min_conn_int;
156         uint16_t max_conn_int;
157         uint16_t slave_latency;
158         uint16_t supervision_tout;
159     } set_conn_params;
160 #endif // #if (BLE_42_FEATURE_SUPPORT == TRUE)
161     //BTC_GAP_BLE_ACT_SET_DEV_NAME,
162     struct set_dev_name_args {
163 #define ESP_GAP_DEVICE_NAME_MAX (32)
164         char device_name[ESP_GAP_DEVICE_NAME_MAX + 1];
165     } set_dev_name;
166 #if (BLE_42_FEATURE_SUPPORT == TRUE)
167     //BTC_GAP_BLE_ACT_CFG_ADV_DATA_RAW,
168     struct config_adv_data_raw_args {
169         uint8_t *raw_adv;
170         uint32_t raw_adv_len;
171     } cfg_adv_data_raw;
172     //BTC_GAP_BLE_ACT_CFG_SCAN_RSP_DATA_RAW,
173     struct config_scan_rsp_data_raw_args {
174         uint8_t *raw_scan_rsp;
175         uint32_t raw_scan_rsp_len;
176     } cfg_scan_rsp_data_raw;
177 #endif // #if (BLE_42_FEATURE_SUPPORT == TRUE)
178     //BTC_GAP_BLE_SET_ENCRYPTION_EVT
179     struct set_encryption_args {
180         esp_bd_addr_t bd_addr;
181         esp_ble_sec_act_t sec_act;
182     } set_encryption;
183     //BTC_GAP_BLE_SET_SECURITY_PARAM_EVT
184     struct set_security_param_args {
185         esp_ble_sm_param_t param_type;
186         uint8_t len;
187         uint8_t *value;
188     } set_security_param;
189     //BTC_GAP_BLE_SECURITY_RSP_EVT
190     struct enc_rsp_args {
191         esp_bd_addr_t bd_addr;
192         bool accept;
193     } sec_rsp;
194     //BTC_GAP_BLE_PASSKEY_REPLY_EVT
195     struct enc_passkey_reply_args {
196         esp_bd_addr_t bd_addr;
197         bool accept;
198         uint32_t passkey;
199     } enc_passkey_replay;
200     //BTC_GAP_BLE_CONFIRM_REPLY_EVT
201     struct enc_comfirm_reply_args {
202         esp_bd_addr_t bd_addr;
203         bool accept;
204     } enc_comfirm_replay;
205     //BTC_GAP_BLE_OOB_DATA_REPLY_EVT
206     struct oob_req_reply_args {
207         esp_bd_addr_t bd_addr;
208         uint8_t len;
209         uint8_t *p_value;
210     } oob_req_reply;
211     //BTC_GAP_BLE_DISCONNECT_EVT
212     struct disconnect_args {
213         esp_bd_addr_t remote_device;
214     } disconnect;
215     //BTC_GAP_BLE_REMOVE_BOND_DEV_EVT
216     struct remove_bond_device_args {
217         esp_bd_addr_t bd_addr;
218     } remove_bond_device;
219     //BTC_GAP_BLE_ACT_READ_RSSI
220     struct read_rssi_args {
221         esp_bd_addr_t remote_addr;
222     } read_rssi;
223     // BTC_GAP_BLE_SET_AFH_CHANNELS
224     struct set_channels_args {
225        esp_gap_ble_channels channels;
226     } set_channels;
227 } btc_ble_gap_args_t;
228 #if (BLE_50_FEATURE_SUPPORT == TRUE)
229 
230 typedef union {
231     struct read_phy_args {
232         esp_bd_addr_t bd_addr;
233     } read_phy;
234 
235     struct set_perf_def_phy_args {
236         esp_ble_gap_phy_mask_t tx_phy_mask;
237         esp_ble_gap_phy_mask_t rx_phy_mask;
238     } set_perf_def_phy;
239 
240     struct set_def_phy_args {
241         esp_bd_addr_t bd_addr;
242         esp_ble_gap_all_phys_t all_phys_mask;
243         esp_ble_gap_phy_mask_t tx_phy_mask;
244         esp_ble_gap_phy_mask_t rx_phy_mask;
245         uint16_t phy_options;
246     } set_def_phy;
247 
248     struct ext_adv_set_rand_addr_args {
249         uint8_t instance;
250         esp_bd_addr_t rand_addr;
251     } ext_adv_set_rand_addr;
252 
253     struct ext_adv_set_params_args {
254         uint8_t instance;
255         esp_ble_gap_ext_adv_params_t params;
256     } ext_adv_set_params;
257 
258     struct ext_adv_cfg_data_args {
259         uint8_t instance;
260         uint16_t length;
261         uint8_t *data;
262     } ext_adv_cfg_data;
263 
264     struct ext_adv_cfg_scan_rsp_args {
265         uint8_t instance;
266         uint16_t length;
267         uint8_t *data;
268     } cfg_scan_rsp;
269 
270     struct ext_adv_start_args {
271         uint8_t num_adv;
272         esp_ble_gap_ext_adv_t *ext_adv;
273     } ext_adv_start;
274 
275     struct ext_adv_stop_args {
276         uint8_t num_adv;
277         uint8_t *ext_adv_inst;
278     } ext_adv_stop;
279 
280     struct ext_adv_set_remove_args {
281         uint8_t instance;
282     } ext_adv_set_remove;
283 
284     struct peridic_adv_set_params_args {
285         uint8_t instance;
286         esp_ble_gap_periodic_adv_params_t params;
287     } peridic_adv_set_params;
288 
289     struct periodic_adv_cfg_data_args {
290         uint8_t instance;
291         uint16_t len;
292         uint8_t *data;
293     } periodic_adv_cfg_data;
294 
295     struct periodic_adv_start_args {
296         uint8_t instance;
297     } periodic_adv_start;
298 
299     struct periodic_adv_stop_args {
300         uint8_t instance;
301     } periodic_adv_stop;
302 
303     struct periodic_adv_create_sync_args {
304         esp_ble_gap_periodic_adv_sync_params_t params;
305     } periodic_adv_create_sync;
306 
307     struct periodic_adv_sync_term_args {
308         uint16_t sync_handle;
309     } periodic_adv_sync_term;
310 
311     struct periodic_adv_add_dev_args {
312         esp_ble_addr_type_t addr_type;
313         esp_bd_addr_t addr;
314         uint16_t sid;
315     } periodic_adv_add_dev;
316 
317     struct periodic_adv_remove_dev_args {
318         esp_ble_addr_type_t addr_type;
319         esp_bd_addr_t addr;
320         uint16_t sid;
321     } periodic_adv_remove_dev;
322 
323     struct set_ext_scan_params_args {
324         esp_ble_ext_scan_params_t params;
325     } set_ext_scan_params;
326 
327     struct start_ext_scan_args {
328         uint32_t duration;
329         uint16_t period;
330     } start_ext_scan;
331 
332     struct set_ext_conn_params_args {
333         esp_bd_addr_t addr;
334         uint8_t phy_mask;
335         esp_ble_gap_conn_params_t phy_1m_conn_params;
336         esp_ble_gap_conn_params_t phy_2m_conn_params;
337         esp_ble_gap_conn_params_t phy_coded_conn_params;
338     } set_ext_conn_params;
339 
340     struct ext_conn_args {
341         esp_ble_addr_type_t own_addr_type;
342         esp_bd_addr_t peer_addr;
343     } ext_conn;
344 
345 } btc_ble_5_gap_args_t;
346 #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
347 
348 void btc_gap_ble_call_handler(btc_msg_t *msg);
349 void btc_gap_ble_cb_handler(btc_msg_t *msg);
350 void btc_get_whitelist_size(uint16_t *length);
351 void btc_gap_ble_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src);
352 void btc_gap_ble_arg_deep_free(btc_msg_t *msg);
353 void btc_gap_ble_cb_deep_free(btc_msg_t *msg);
354 void btc_gap_ble_cb_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src);
355 void btc_gap_callback_init(void);
356 void btc_gap_ble_deinit(void);
357 void btc_adv_list_init(void);
358 void btc_adv_list_deinit(void);
359 
360 #endif /* __BTC_GAP_BLE_H__ */
361