1 /*
2  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <string.h>
8 
9 #include "osi/allocator.h"
10 #include "stack/bt_types.h"
11 #include "common/bt_defs.h"
12 #include "bta/bta_api.h"
13 #include "bta/bta_dm_co.h"
14 #include "btc/btc_task.h"
15 #include "btc/btc_manage.h"
16 #include "btc_gap_ble.h"
17 #include "btc_gatt_util.h"
18 #include "esp_bt_defs.h"
19 #include "esp_gap_ble_api.h"
20 #include "btc/btc_ble_storage.h"
21 #include "btc/btc_dm.h"
22 #include "btc/btc_util.h"
23 #include "osi/mutex.h"
24 #include "esp_bt.h"
25 
26 #if (BLE_INCLUDED == TRUE)
27 #if (BLE_42_FEATURE_SUPPORT == TRUE)
28 #if BTC_DYNAMIC_MEMORY == FALSE
29 static tBTA_BLE_ADV_DATA gl_bta_adv_data;
30 static tBTA_BLE_ADV_DATA gl_bta_scan_rsp_data;
31 #else
32 tBTA_BLE_ADV_DATA *gl_bta_adv_data_ptr;
33 tBTA_BLE_ADV_DATA *gl_bta_scan_rsp_data_ptr;
34 #endif
35 #endif // #if (BLE_42_FEATURE_SUPPORT == TRUE)
36 
37 #if SCAN_QUEUE_CONGEST_CHECK
38 static list_t *adv_filter_list;
39 static osi_mutex_t adv_list_lock;
40 bool btc_check_adv_list(uint8_t * addr, uint8_t addr_type);
41 uint32_t btc_get_adv_list_length(void);
42 void btc_adv_list_refresh(void);
43 void btc_adv_list_lock(void);
44 void btc_adv_list_unlock(void);
45 static uint16_t btc_adv_list_count = 0;
46 
47 #define  BTC_ADV_LIST_MAX_LENGTH    50
48 #define  BTC_ADV_LIST_MAX_COUNT     200
49 #endif
50 
btc_gap_ble_cb_to_app(esp_gap_ble_cb_event_t event,esp_ble_gap_cb_param_t * param)51 static inline void btc_gap_ble_cb_to_app(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
52 {
53     esp_gap_ble_cb_t btc_gap_ble_cb = (esp_gap_ble_cb_t)btc_profile_cb_get(BTC_PID_GAP_BLE);
54     if (btc_gap_ble_cb) {
55         btc_gap_ble_cb(event, param);
56     }
57 }
58 #if (BLE_42_FEATURE_SUPPORT == TRUE)
btc_gap_adv_point_cleanup(void ** buf)59 static void btc_gap_adv_point_cleanup(void **buf)
60 {
61     if (NULL == *buf) {
62         return;
63     }
64     osi_free(*buf);
65     *buf = NULL;
66 }
67 
68 
btc_cleanup_adv_data(tBTA_BLE_ADV_DATA * bta_adv_data)69 static void btc_cleanup_adv_data(tBTA_BLE_ADV_DATA *bta_adv_data)
70 {
71     if (bta_adv_data == NULL) {
72         return;
73     }
74 
75     // Manufacturer data cleanup
76     if (bta_adv_data->p_manu != NULL) {
77         btc_gap_adv_point_cleanup((void **) &bta_adv_data->p_manu->p_val);
78         btc_gap_adv_point_cleanup((void **) &bta_adv_data->p_manu);
79     }
80 
81     // Proprietary data cleanup
82     if (bta_adv_data->p_proprietary != NULL) {
83         int i = 0;
84         tBTA_BLE_PROP_ELEM *p_elem = bta_adv_data->p_proprietary->p_elem;
85         while (i++ != bta_adv_data->p_proprietary->num_elem
86                 && p_elem) {
87             btc_gap_adv_point_cleanup((void **) &p_elem->p_val);
88             ++p_elem;
89         }
90 
91         btc_gap_adv_point_cleanup((void **) &bta_adv_data->p_proprietary->p_elem);
92         btc_gap_adv_point_cleanup((void **) &bta_adv_data->p_proprietary);
93     }
94 
95     // Service list cleanup
96     if (bta_adv_data->p_services != NULL) {
97         btc_gap_adv_point_cleanup((void **) &bta_adv_data->p_services->p_uuid);
98         btc_gap_adv_point_cleanup((void **) &bta_adv_data->p_services);
99     }
100 
101     // Service data cleanup
102     if (bta_adv_data->p_service_data != NULL) {
103         btc_gap_adv_point_cleanup((void **) &bta_adv_data->p_service_data->p_val);
104         btc_gap_adv_point_cleanup((void **) &bta_adv_data->p_service_data);
105     }
106 
107     btc_gap_adv_point_cleanup((void **) &bta_adv_data->p_services_128b);
108 
109     if (bta_adv_data->p_service_32b != NULL) {
110         btc_gap_adv_point_cleanup((void **) &bta_adv_data->p_service_32b->p_uuid);
111         btc_gap_adv_point_cleanup((void **) &bta_adv_data->p_service_32b);
112     }
113 
114     if (bta_adv_data->p_sol_services != NULL) {
115         btc_gap_adv_point_cleanup((void **) &bta_adv_data->p_sol_services->p_uuid);
116         btc_gap_adv_point_cleanup((void **) &bta_adv_data->p_sol_services);
117     }
118 
119     if (bta_adv_data->p_sol_service_32b != NULL) {
120         btc_gap_adv_point_cleanup((void **) &bta_adv_data->p_sol_service_32b->p_uuid);
121         btc_gap_adv_point_cleanup((void **) &bta_adv_data->p_sol_service_32b);
122     }
123 
124     btc_gap_adv_point_cleanup((void **) &bta_adv_data->p_sol_service_128b);
125 }
126 
btc_to_bta_adv_data(esp_ble_adv_data_t * p_adv_data,tBTA_BLE_ADV_DATA * bta_adv_data,uint32_t * data_mask)127 static void btc_to_bta_adv_data(esp_ble_adv_data_t *p_adv_data, tBTA_BLE_ADV_DATA *bta_adv_data, uint32_t *data_mask)
128 {
129     uint32_t mask;
130 
131     btc_cleanup_adv_data(bta_adv_data);
132 
133     memset(bta_adv_data, 0, sizeof(tBTA_BLE_ADV_DATA));
134     mask = 0;
135 
136     if (p_adv_data->flag != 0) {
137         mask = BTM_BLE_AD_BIT_FLAGS;
138         bta_adv_data->flag = p_adv_data->flag;
139     }
140 
141     if (p_adv_data->include_name) {
142         mask |= BTM_BLE_AD_BIT_DEV_NAME;
143     }
144 
145     if (p_adv_data->include_txpower) {
146         mask |= BTM_BLE_AD_BIT_TX_PWR;
147         bta_adv_data->tx_power = esp_ble_tx_power_get(ESP_BLE_PWR_TYPE_ADV);
148     }
149 
150     if (p_adv_data->min_interval > 0 && p_adv_data->max_interval > 0 &&
151             p_adv_data->max_interval >= p_adv_data->min_interval) {
152         mask |= BTM_BLE_AD_BIT_INT_RANGE;
153         bta_adv_data->int_range.low = p_adv_data->min_interval;
154         bta_adv_data->int_range.hi = p_adv_data->max_interval;
155     }
156 
157     if (p_adv_data->include_txpower) {
158         //TODO
159     }
160 
161     if (p_adv_data->appearance != 0) {
162         mask |= BTM_BLE_AD_BIT_APPEARANCE;
163         bta_adv_data->appearance = p_adv_data->appearance;
164     }
165 
166     if (p_adv_data->manufacturer_len > 0 && p_adv_data->p_manufacturer_data != NULL) {
167         bta_adv_data->p_manu = osi_malloc(sizeof(tBTA_BLE_MANU));
168         if (bta_adv_data->p_manu != NULL) {
169             bta_adv_data->p_manu->p_val = osi_malloc(p_adv_data->manufacturer_len);
170             if (bta_adv_data->p_manu->p_val != NULL) {
171                 mask |= BTM_BLE_AD_BIT_MANU;
172                 bta_adv_data->p_manu->len = p_adv_data->manufacturer_len;
173                 memcpy(bta_adv_data->p_manu->p_val, p_adv_data->p_manufacturer_data, p_adv_data->manufacturer_len);
174             }
175         }
176     }
177 
178     tBTA_BLE_PROP_ELEM *p_elem_service_data = NULL;
179     if (p_adv_data->service_data_len > 0 && p_adv_data->p_service_data != NULL) {
180         p_elem_service_data = osi_malloc(sizeof(tBTA_BLE_PROP_ELEM));
181         if (p_elem_service_data != NULL) {
182             p_elem_service_data->p_val = osi_malloc(p_adv_data->service_data_len);
183             if (p_elem_service_data->p_val != NULL) {
184                 p_elem_service_data->adv_type = BTM_BLE_AD_TYPE_SERVICE_DATA;
185                 p_elem_service_data->len = p_adv_data->service_data_len;
186                 memcpy(p_elem_service_data->p_val, p_adv_data->p_service_data,
187                        p_adv_data->service_data_len);
188             } else {
189                 osi_free(p_elem_service_data);
190                 p_elem_service_data = NULL;
191             }
192         }
193     }
194 
195     if (NULL != p_elem_service_data) {
196         bta_adv_data->p_proprietary = osi_malloc(sizeof(tBTA_BLE_PROPRIETARY));
197         if (NULL != bta_adv_data->p_proprietary) {
198             tBTA_BLE_PROP_ELEM *p_elem = NULL;
199             tBTA_BLE_PROPRIETARY *p_prop = bta_adv_data->p_proprietary;
200             p_prop->num_elem = 0;
201             mask |= BTM_BLE_AD_BIT_PROPRIETARY;
202             p_prop->num_elem = 1;
203             p_prop->p_elem = osi_malloc(sizeof(tBTA_BLE_PROP_ELEM) * p_prop->num_elem);
204             p_elem = p_prop->p_elem;
205             if (NULL != p_elem) {
206                 memcpy(p_elem++, p_elem_service_data, sizeof(tBTA_BLE_PROP_ELEM));
207             }
208         }
209         osi_free(p_elem_service_data);
210     }
211 
212     if (p_adv_data->service_uuid_len && p_adv_data->p_service_uuid) {
213         UINT16 *p_uuid_out16 = NULL;
214         UINT32 *p_uuid_out32 = NULL;
215         for (int position = 0; position < p_adv_data->service_uuid_len; position += LEN_UUID_128) {
216             tBT_UUID bt_uuid;
217 
218             btc128_to_bta_uuid(&bt_uuid, p_adv_data->p_service_uuid + position);
219 
220             switch (bt_uuid.len) {
221             case (LEN_UUID_16): {
222                 if (NULL == bta_adv_data->p_services) {
223                     bta_adv_data->p_services = osi_malloc(sizeof(tBTA_BLE_SERVICE));
224                     bta_adv_data->p_services->list_cmpl = FALSE;
225                     bta_adv_data->p_services->num_service = 0;
226                     bta_adv_data->p_services->p_uuid = osi_malloc(p_adv_data->service_uuid_len / LEN_UUID_128 * LEN_UUID_16);
227                     p_uuid_out16 = bta_adv_data->p_services->p_uuid;
228                 }
229 
230                 if (NULL != bta_adv_data->p_services->p_uuid) {
231                     BTC_TRACE_DEBUG("%s - In 16-UUID_data", __FUNCTION__);
232                     mask |= BTM_BLE_AD_BIT_SERVICE;
233                     ++bta_adv_data->p_services->num_service;
234                     *p_uuid_out16++ = bt_uuid.uu.uuid16;
235                 }
236                 break;
237             }
238 
239             case (LEN_UUID_32): {
240                 if (NULL == bta_adv_data->p_service_32b) {
241                     bta_adv_data->p_service_32b =
242                         osi_malloc(sizeof(tBTA_BLE_32SERVICE));
243                     bta_adv_data->p_service_32b->list_cmpl = FALSE;
244                     bta_adv_data->p_service_32b->num_service = 0;
245                     bta_adv_data->p_service_32b->p_uuid =
246                         osi_malloc(p_adv_data->service_uuid_len / LEN_UUID_128 * LEN_UUID_32);
247                     p_uuid_out32 = bta_adv_data->p_service_32b->p_uuid;
248                 }
249 
250                 if (NULL != bta_adv_data->p_service_32b->p_uuid) {
251                     BTC_TRACE_DEBUG("%s - In 32-UUID_data", __FUNCTION__);
252                     mask |= BTM_BLE_AD_BIT_SERVICE_32;
253                     ++bta_adv_data->p_service_32b->num_service;
254                     *p_uuid_out32++ = bt_uuid.uu.uuid32;
255                 }
256                 break;
257             }
258 
259             case (LEN_UUID_128): {
260                 /* Currently, only one 128-bit UUID is supported */
261                 if (NULL == bta_adv_data->p_services_128b) {
262                     bta_adv_data->p_services_128b =
263                         osi_malloc(sizeof(tBTA_BLE_128SERVICE));
264                     if (NULL != bta_adv_data->p_services_128b) {
265                         BTC_TRACE_DEBUG("%s - In 128-UUID_data", __FUNCTION__);
266                         mask |= BTM_BLE_AD_BIT_SERVICE_128;
267                         memcpy(bta_adv_data->p_services_128b->uuid128,
268                                bt_uuid.uu.uuid128, LEN_UUID_128);
269                         BTC_TRACE_DEBUG("%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x", bt_uuid.uu.uuid128[0],
270                                   bt_uuid.uu.uuid128[1], bt_uuid.uu.uuid128[2], bt_uuid.uu.uuid128[3],
271                                   bt_uuid.uu.uuid128[4], bt_uuid.uu.uuid128[5], bt_uuid.uu.uuid128[6],
272                                   bt_uuid.uu.uuid128[7], bt_uuid.uu.uuid128[8], bt_uuid.uu.uuid128[9],
273                                   bt_uuid.uu.uuid128[10], bt_uuid.uu.uuid128[11], bt_uuid.uu.uuid128[12],
274                                   bt_uuid.uu.uuid128[13], bt_uuid.uu.uuid128[14], bt_uuid.uu.uuid128[15]);
275                         bta_adv_data->p_services_128b->list_cmpl = TRUE;
276                     }
277                 }
278                 break;
279             }
280 
281             default:
282                 break;
283             }
284         }
285     }
286 
287     *data_mask = mask;
288 }
289 
btc_adv_data_callback(tBTA_STATUS status)290 static void btc_adv_data_callback(tBTA_STATUS status)
291 {
292     esp_ble_gap_cb_param_t param;
293     bt_status_t ret;
294     btc_msg_t msg = {0};
295 
296     msg.sig = BTC_SIG_API_CB;
297     msg.pid = BTC_PID_GAP_BLE;
298     msg.act = ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT;
299     param.adv_data_cmpl.status = status;
300 
301     ret = btc_transfer_context(&msg, &param,
302                                sizeof(esp_ble_gap_cb_param_t), NULL);
303 
304     if (ret != BT_STATUS_SUCCESS) {
305         BTC_TRACE_ERROR("%s btc_transfer_context failed\n", __func__);
306     }
307 }
308 
btc_scan_rsp_data_callback(tBTA_STATUS status)309 static void btc_scan_rsp_data_callback(tBTA_STATUS status)
310 {
311     esp_ble_gap_cb_param_t param;
312     bt_status_t ret;
313     btc_msg_t msg = {0};
314 
315     msg.sig = BTC_SIG_API_CB;
316     msg.pid = BTC_PID_GAP_BLE;
317     msg.act = ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT;
318     param.scan_rsp_data_cmpl.status = status;
319 
320     ret = btc_transfer_context(&msg, &param,
321                                sizeof(esp_ble_gap_cb_param_t), NULL);
322 
323     if (ret != BT_STATUS_SUCCESS) {
324         BTC_TRACE_ERROR("%s btc_transfer_context failed\n", __func__);
325     }
326 }
327 
btc_adv_data_raw_callback(tBTA_STATUS status)328 static void btc_adv_data_raw_callback(tBTA_STATUS status)
329 {
330     esp_ble_gap_cb_param_t param;
331     bt_status_t ret;
332     btc_msg_t msg = {0};
333 
334     msg.sig = BTC_SIG_API_CB;
335     msg.pid = BTC_PID_GAP_BLE;
336     msg.act = ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT;
337     param.adv_data_raw_cmpl.status = status;
338 
339     ret = btc_transfer_context(&msg, &param,
340                                sizeof(esp_ble_gap_cb_param_t), NULL);
341 
342     if (ret != BT_STATUS_SUCCESS) {
343         BTC_TRACE_ERROR("%s btc_transfer_context failed\n", __func__);
344     }
345 }
346 
btc_scan_rsp_data_raw_callback(tBTA_STATUS status)347 static void btc_scan_rsp_data_raw_callback(tBTA_STATUS status)
348 {
349     esp_ble_gap_cb_param_t param;
350     bt_status_t ret;
351     btc_msg_t msg = {0};
352 
353     msg.sig = BTC_SIG_API_CB;
354     msg.pid = BTC_PID_GAP_BLE;
355     msg.act = ESP_GAP_BLE_SCAN_RSP_DATA_RAW_SET_COMPLETE_EVT;
356     param.scan_rsp_data_raw_cmpl.status = status;
357 
358     ret = btc_transfer_context(&msg, &param,
359                                sizeof(esp_ble_gap_cb_param_t), NULL);
360 
361     if (ret != BT_STATUS_SUCCESS) {
362         BTC_TRACE_ERROR("%s btc_transfer_context failed\n", __func__);
363     }
364 }
365 
btc_ble_set_adv_data(esp_ble_adv_data_t * adv_data,tBTA_SET_ADV_DATA_CMPL_CBACK p_adv_data_cback)366 static void btc_ble_set_adv_data(esp_ble_adv_data_t *adv_data,
367                                  tBTA_SET_ADV_DATA_CMPL_CBACK p_adv_data_cback)
368 {
369     tBTA_BLE_AD_MASK data_mask = 0;
370 
371     if (!adv_data->set_scan_rsp) {
372     	btc_to_bta_adv_data(adv_data, &gl_bta_adv_data, &data_mask);
373         BTA_DmBleSetAdvConfig(data_mask, &gl_bta_adv_data, p_adv_data_cback);
374     } else {
375     	btc_to_bta_adv_data(adv_data, &gl_bta_scan_rsp_data, &data_mask);
376         BTA_DmBleSetScanRsp(data_mask, &gl_bta_scan_rsp_data, p_adv_data_cback);
377     }
378 }
379 
btc_ble_set_adv_data_raw(uint8_t * raw_adv,uint32_t raw_adv_len,tBTA_SET_ADV_DATA_CMPL_CBACK p_adv_data_cback)380 static void btc_ble_set_adv_data_raw(uint8_t *raw_adv, uint32_t raw_adv_len,
381                                  tBTA_SET_ADV_DATA_CMPL_CBACK p_adv_data_cback)
382 {
383     BTA_DmBleSetAdvConfigRaw(raw_adv, raw_adv_len, p_adv_data_cback);
384 }
385 
btc_ble_set_scan_rsp_data_raw(uint8_t * raw_scan_rsp,uint32_t raw_scan_rsp_len,tBTA_SET_ADV_DATA_CMPL_CBACK p_scan_rsp_data_cback)386 static void btc_ble_set_scan_rsp_data_raw(uint8_t *raw_scan_rsp, uint32_t raw_scan_rsp_len,
387                                  tBTA_SET_ADV_DATA_CMPL_CBACK p_scan_rsp_data_cback)
388 {
389     BTA_DmBleSetScanRspRaw(raw_scan_rsp, raw_scan_rsp_len, p_scan_rsp_data_cback);
390 }
391 
btc_start_adv_callback(uint8_t status)392 static void btc_start_adv_callback(uint8_t status)
393 {
394     esp_ble_gap_cb_param_t param;
395     bt_status_t ret;
396     btc_msg_t msg = {0};
397 
398     msg.sig = BTC_SIG_API_CB;
399     msg.pid = BTC_PID_GAP_BLE;
400     msg.act = ESP_GAP_BLE_ADV_START_COMPLETE_EVT;
401     param.adv_start_cmpl.status = btc_hci_to_esp_status(status);
402 
403     ret = btc_transfer_context(&msg, &param,
404                                sizeof(esp_ble_gap_cb_param_t), NULL);
405 
406     if (ret != BT_STATUS_SUCCESS) {
407         BTC_TRACE_ERROR("%s btc_transfer_context failed\n", __func__);
408     }
409 }
410 
btc_stop_adv_callback(uint8_t status)411 static void btc_stop_adv_callback(uint8_t status)
412 {
413     esp_ble_gap_cb_param_t param;
414     bt_status_t ret;
415     btc_msg_t msg = {0};
416 
417     msg.sig = BTC_SIG_API_CB;
418     msg.pid = BTC_PID_GAP_BLE;
419     msg.act = ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT;
420     param.adv_stop_cmpl.status = btc_hci_to_esp_status(status);
421 
422     ret = btc_transfer_context(&msg, &param,
423                                sizeof(esp_ble_gap_cb_param_t), NULL);
424 
425     if (ret != BT_STATUS_SUCCESS) {
426         BTC_TRACE_ERROR("%s btc_transfer_context failed\n", __func__);
427     }
428 }
429 
btc_update_duplicate_exceptional_list_callback(tBTA_STATUS status,uint8_t subcode,uint32_t length,uint8_t * device_info)430 void btc_update_duplicate_exceptional_list_callback(tBTA_STATUS status, uint8_t subcode, uint32_t length, uint8_t *device_info)
431 {
432     esp_ble_gap_cb_param_t param;
433     bt_status_t ret;
434     btc_msg_t msg = {0};
435 
436     msg.sig = BTC_SIG_API_CB;
437     msg.pid = BTC_PID_GAP_BLE;
438     msg.act = ESP_GAP_BLE_UPDATE_DUPLICATE_EXCEPTIONAL_LIST_COMPLETE_EVT;
439     param.update_duplicate_exceptional_list_cmpl.status = status;
440     param.update_duplicate_exceptional_list_cmpl.subcode = subcode;
441     if(length > sizeof(param.update_duplicate_exceptional_list_cmpl.device_info)) {
442         length = sizeof(param.update_duplicate_exceptional_list_cmpl.device_info);
443     }
444     param.update_duplicate_exceptional_list_cmpl.length = length;
445     memcpy(param.update_duplicate_exceptional_list_cmpl.device_info, device_info, length);
446     ret = btc_transfer_context(&msg, &param, sizeof(esp_ble_gap_cb_param_t), NULL);
447 
448     if (ret != BT_STATUS_SUCCESS) {
449         BTC_TRACE_ERROR("%s btc_transfer_context failed\n", __func__);
450     }
451 }
452 
btc_ble_update_duplicate_exceptional_list(uint8_t subcode,uint32_t info_type,BD_ADDR device_info,tBTA_UPDATE_DUPLICATE_EXCEPTIONAL_LIST_CMPL_CBACK p_update_duplicate_ignore_list_cback)453 static void btc_ble_update_duplicate_exceptional_list(uint8_t subcode, uint32_t info_type, BD_ADDR device_info,
454                                  tBTA_UPDATE_DUPLICATE_EXCEPTIONAL_LIST_CMPL_CBACK p_update_duplicate_ignore_list_cback)
455 {
456     BTA_DmUpdateDuplicateExceptionalList(subcode, info_type, device_info, p_update_duplicate_ignore_list_cback);
457 }
458 
btc_ble_start_advertising(esp_ble_adv_params_t * ble_adv_params,tBTA_START_ADV_CMPL_CBACK start_adv_cback)459 static void btc_ble_start_advertising (esp_ble_adv_params_t *ble_adv_params, tBTA_START_ADV_CMPL_CBACK start_adv_cback)
460 {
461     tBLE_BD_ADDR peer_addr;
462     esp_bt_status_t status = ESP_BT_STATUS_SUCCESS;
463     if (!BLE_ISVALID_PARAM(ble_adv_params->adv_int_min, BTM_BLE_ADV_INT_MIN, BTM_BLE_ADV_INT_MAX) ||
464             !BLE_ISVALID_PARAM(ble_adv_params->adv_int_max, BTM_BLE_ADV_INT_MIN, BTM_BLE_ADV_INT_MAX)) {
465         status = ESP_BT_STATUS_PARM_INVALID;
466         BTC_TRACE_ERROR("Invalid advertisting interval parameters.\n");
467     }
468 
469     if ((ble_adv_params->adv_type < ADV_TYPE_IND) ||
470             (ble_adv_params->adv_type > ADV_TYPE_DIRECT_IND_LOW) ) {
471         status = ESP_BT_STATUS_PARM_INVALID;
472         BTC_TRACE_ERROR("Invalid advertisting type parameters.\n");
473     }
474 
475     if ((ble_adv_params->adv_filter_policy < ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY) ||
476             (ble_adv_params->adv_filter_policy > ADV_FILTER_ALLOW_SCAN_WLST_CON_WLST) ) {
477         status = ESP_BT_STATUS_PARM_INVALID;
478         BTC_TRACE_ERROR("Invalid advertisting type parameters.\n");
479     }
480 
481     if((ble_adv_params->channel_map | ADV_CHNL_ALL) != ADV_CHNL_ALL || ble_adv_params->channel_map == 0) {
482         status = ESP_BT_STATUS_PARM_INVALID;
483         BTC_TRACE_ERROR("Invalid advertisting channel map parameters.\n");
484     }
485     if (!BLE_ISVALID_PARAM(ble_adv_params->peer_addr_type, BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_RANDOM)) {
486         status = ESP_BT_STATUS_PARM_INVALID;
487         BTC_TRACE_ERROR("Invalid advertisting peer address type parameters.\n");
488     }
489     if(status != ESP_BT_STATUS_SUCCESS) {
490         if(start_adv_cback) {
491             start_adv_cback(status);
492         }
493         return;
494     }
495 
496     BTC_TRACE_DEBUG("API_Ble_AppStartAdvertising\n");
497 
498     memcpy(peer_addr.bda, ble_adv_params->peer_addr, ESP_BD_ADDR_LEN);
499     peer_addr.type = ble_adv_params->peer_addr_type;
500     BTA_DmSetBleAdvParamsAll(ble_adv_params->adv_int_min,
501                              ble_adv_params->adv_int_max,
502                              ble_adv_params->adv_type,
503                              ble_adv_params->own_addr_type,
504                              ble_adv_params->channel_map,
505                              ble_adv_params->adv_filter_policy,
506                              &peer_addr,
507                              start_adv_cback);
508 }
509 
510 
btc_scan_params_callback(tGATT_IF gatt_if,tBTM_STATUS status)511 static void btc_scan_params_callback(tGATT_IF gatt_if, tBTM_STATUS status)
512 {
513     esp_ble_gap_cb_param_t param;
514     bt_status_t ret;
515     btc_msg_t msg = {0};
516 
517     msg.sig = BTC_SIG_API_CB;
518     msg.pid = BTC_PID_GAP_BLE;
519     msg.act = ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT;
520     param.scan_param_cmpl.status = status;
521 
522     ret = btc_transfer_context(&msg, &param,
523                                sizeof(esp_ble_gap_cb_param_t), NULL);
524 
525     if (ret != BT_STATUS_SUCCESS) {
526         BTC_TRACE_ERROR("%s btc_transfer_context failed\n", __func__);
527     }
528 }
529 
btc_ble_set_scan_params(esp_ble_scan_params_t * scan_params,tBLE_SCAN_PARAM_SETUP_CBACK scan_param_setup_cback)530 static void btc_ble_set_scan_params(esp_ble_scan_params_t *scan_params, tBLE_SCAN_PARAM_SETUP_CBACK scan_param_setup_cback)
531 {
532     if (BLE_ISVALID_PARAM(scan_params->scan_interval, BTM_BLE_SCAN_INT_MIN, BTM_BLE_SCAN_INT_MAX) &&
533         BLE_ISVALID_PARAM(scan_params->scan_window, BTM_BLE_SCAN_WIN_MIN, BTM_BLE_SCAN_WIN_MAX) &&
534         BLE_ISVALID_PARAM(scan_params->own_addr_type, BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_RPA_RANDOM) &&
535         BLE_ISVALID_PARAM(scan_params->scan_filter_policy, BLE_SCAN_FILTER_ALLOW_ALL, BLE_SCAN_FILTER_ALLOW_WLIST_RPA_DIR) &&
536         BLE_ISVALID_PARAM(scan_params->scan_duplicate, BLE_SCAN_DUPLICATE_DISABLE, BLE_SCAN_DUPLICATE_MAX -1) &&
537         (scan_params->scan_type == BTM_BLE_SCAN_MODE_ACTI || scan_params->scan_type == BTM_BLE_SCAN_MODE_PASS)) {
538         BTA_DmSetBleScanFilterParams(ESP_DEFAULT_GATT_IF,	/*client_if*/
539                                      scan_params->scan_interval,
540                                      scan_params->scan_window,
541                                      scan_params->scan_type,
542                                      scan_params->scan_filter_policy,
543                                      scan_params->own_addr_type,
544                                      scan_params->scan_duplicate,
545                                      scan_param_setup_cback);
546     } else {
547         btc_scan_params_callback(ESP_DEFAULT_GATT_IF, BTM_ILLEGAL_VALUE);
548     }
549 }
550 
btc_search_callback(tBTA_DM_SEARCH_EVT event,tBTA_DM_SEARCH * p_data)551 static void btc_search_callback(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
552 {
553     esp_ble_gap_cb_param_t param;
554     btc_msg_t msg = {0};
555 
556     msg.sig = BTC_SIG_API_CB;
557     msg.pid = BTC_PID_GAP_BLE;
558     msg.act = ESP_GAP_BLE_SCAN_RESULT_EVT;
559 
560     param.scan_rst.search_evt = event;
561     switch (event) {
562     case BTA_DM_INQ_RES_EVT: {
563 #if SCAN_QUEUE_CONGEST_CHECK
564         if(btc_check_queue_is_congest()) {
565             BTC_TRACE_DEBUG("BtcQueue is congested");
566             if(btc_get_adv_list_length() > BTC_ADV_LIST_MAX_LENGTH || btc_adv_list_count > BTC_ADV_LIST_MAX_COUNT) {
567                 btc_adv_list_refresh();
568                 btc_adv_list_count = 0;
569             }
570             if(btc_check_adv_list(p_data->inq_res.bd_addr, p_data->inq_res.ble_addr_type)) {
571                 return;
572             }
573         }
574         btc_adv_list_count ++;
575 #endif
576         bdcpy(param.scan_rst.bda, p_data->inq_res.bd_addr);
577         param.scan_rst.dev_type = p_data->inq_res.device_type;
578         param.scan_rst.rssi = p_data->inq_res.rssi;
579         param.scan_rst.ble_addr_type = p_data->inq_res.ble_addr_type;
580         param.scan_rst.ble_evt_type = p_data->inq_res.ble_evt_type;
581         param.scan_rst.flag = p_data->inq_res.flag;
582         param.scan_rst.num_resps = 1;
583         param.scan_rst.adv_data_len = p_data->inq_res.adv_data_len;
584         param.scan_rst.scan_rsp_len = p_data->inq_res.scan_rsp_len;
585         memcpy(param.scan_rst.ble_adv, p_data->inq_res.p_eir, sizeof(param.scan_rst.ble_adv));
586         break;
587     }
588     case BTA_DM_INQ_CMPL_EVT: {
589         param.scan_rst.num_resps = p_data->inq_cmpl.num_resps;
590         BTC_TRACE_DEBUG("%s  BLE observe complete. Num Resp %d\n", __FUNCTION__, p_data->inq_cmpl.num_resps);
591         break;
592     }
593     case BTA_DM_DISC_RES_EVT:
594         BTC_TRACE_DEBUG("BTA_DM_DISC_RES_EVT\n");
595         break;
596     case BTA_DM_DISC_BLE_RES_EVT:
597         BTC_TRACE_DEBUG("BTA_DM_DISC_BLE_RES_EVT\n");
598         break;
599     case BTA_DM_DISC_CMPL_EVT:
600         BTC_TRACE_DEBUG("BTA_DM_DISC_CMPL_EVT\n");
601         break;
602     case BTA_DM_DI_DISC_CMPL_EVT:
603         BTC_TRACE_DEBUG("BTA_DM_DI_DISC_CMPL_EVT\n");
604         break;
605     case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
606         BTC_TRACE_DEBUG("BTA_DM_SEARCH_CANCEL_CMPL_EVT\n");
607         break;
608     case BTA_DM_INQ_DISCARD_NUM_EVT:
609         param.scan_rst.num_dis = p_data->inq_dis.num_dis;
610         break;
611     default:
612         BTC_TRACE_ERROR("%s : Unknown event 0x%x\n", __FUNCTION__, event);
613         return;
614     }
615     btc_transfer_context(&msg, &param, sizeof(esp_ble_gap_cb_param_t), NULL);
616 }
617 
btc_start_scan_callback(uint8_t status)618 static void btc_start_scan_callback(uint8_t status)
619 {
620     esp_ble_gap_cb_param_t param;
621     bt_status_t ret;
622     btc_msg_t msg = {0};
623 
624     msg.sig = BTC_SIG_API_CB;
625     msg.pid = BTC_PID_GAP_BLE;
626     msg.act = ESP_GAP_BLE_SCAN_START_COMPLETE_EVT;
627     param.scan_start_cmpl.status = status;
628 
629     ret = btc_transfer_context(&msg, &param,
630                                sizeof(esp_ble_gap_cb_param_t), NULL);
631 
632     if (ret != BT_STATUS_SUCCESS) {
633         BTC_TRACE_ERROR("%s btc_transfer_context failed\n", __func__);
634     }
635 }
636 
btc_stop_scan_callback(tBTA_STATUS status)637 static void btc_stop_scan_callback(tBTA_STATUS status)
638 {
639     esp_ble_gap_cb_param_t param;
640     bt_status_t ret;
641     btc_msg_t msg = {0};
642 
643     msg.sig = BTC_SIG_API_CB;
644     msg.pid = BTC_PID_GAP_BLE;
645     msg.act = ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT;
646     param.scan_stop_cmpl.status = status;
647 
648     ret = btc_transfer_context(&msg, &param,
649                                sizeof(esp_ble_gap_cb_param_t), NULL);
650 
651     if (ret != BT_STATUS_SUCCESS) {
652         BTC_TRACE_ERROR("%s btc_transfer_context failed\n", __func__);
653     }
654 #if SCAN_QUEUE_CONGEST_CHECK
655     btc_adv_list_refresh();
656 #endif
657 }
658 #endif // #if (BLE_42_FEATURE_SUPPORT == TRUE)
btc_update_conn_param_callback(UINT8 status,BD_ADDR bd_addr,tBTM_LE_UPDATE_CONN_PRAMS * update_conn_params)659 void btc_update_conn_param_callback (UINT8 status, BD_ADDR bd_addr, tBTM_LE_UPDATE_CONN_PRAMS *update_conn_params)
660 {
661     esp_ble_gap_cb_param_t param;
662     bt_status_t ret;
663     btc_msg_t msg = {0};
664     msg.sig = BTC_SIG_API_CB;
665     msg.pid = BTC_PID_GAP_BLE;
666     msg.act = ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT;
667     param.update_conn_params.status = btc_hci_to_esp_status(status);
668     param.update_conn_params.min_int = update_conn_params->min_conn_int;
669     param.update_conn_params.max_int = update_conn_params->max_conn_int;
670     param.update_conn_params.conn_int = update_conn_params->conn_int;
671     param.update_conn_params.latency = update_conn_params->slave_latency;
672     param.update_conn_params.timeout = update_conn_params->supervision_tout;
673     memcpy(param.update_conn_params.bda, bd_addr, sizeof(esp_bd_addr_t));
674     ret = btc_transfer_context(&msg, &param,
675                                sizeof(esp_ble_gap_cb_param_t), NULL);
676 
677     if (ret != BT_STATUS_SUCCESS) {
678         BTC_TRACE_ERROR("%s btc_transfer_context failed\n", __func__);
679     }
680 }
681 
btc_set_pkt_length_callback(UINT8 status,tBTM_LE_SET_PKT_DATA_LENGTH_PARAMS * data_len_params)682 static void btc_set_pkt_length_callback(UINT8 status, tBTM_LE_SET_PKT_DATA_LENGTH_PARAMS *data_len_params)
683 {
684     esp_ble_gap_cb_param_t param;
685     bt_status_t ret;
686     btc_msg_t msg = {0};
687     msg.sig = BTC_SIG_API_CB;
688     msg.pid = BTC_PID_GAP_BLE;
689     msg.act = ESP_GAP_BLE_SET_PKT_LENGTH_COMPLETE_EVT;
690     param.pkt_data_lenth_cmpl.status = btc_btm_status_to_esp_status(status);
691     param.pkt_data_lenth_cmpl.params.rx_len = data_len_params->rx_len;
692     param.pkt_data_lenth_cmpl.params.tx_len = data_len_params->tx_len;
693     ret = btc_transfer_context(&msg, &param,
694                                sizeof(esp_ble_gap_cb_param_t), NULL);
695 
696     if (ret != BT_STATUS_SUCCESS) {
697         BTC_TRACE_ERROR("%s btc_transfer_context failed\n", __func__);
698     }
699 }
700 
btc_gap_ble_set_channels_cmpl_callback(void * p_data)701 static void btc_gap_ble_set_channels_cmpl_callback(void *p_data)
702 {
703     tBTA_BLE_SET_CHANNELS_RESULTS *result = (tBTA_BLE_SET_CHANNELS_RESULTS *)p_data;
704     esp_ble_gap_cb_param_t param;
705     bt_status_t ret;
706     btc_msg_t msg = {0};
707     msg.sig = BTC_SIG_API_CB;
708     msg.pid = BTC_PID_GAP_BLE;
709     msg.act = ESP_GAP_BLE_SET_CHANNELS_EVT;
710 
711     param.ble_set_channels.stat = btc_btm_status_to_esp_status(result->status);
712 
713     ret = btc_transfer_context(&msg, &param,
714                                sizeof(esp_ble_gap_cb_param_t), NULL);
715 
716     if (ret != BT_STATUS_SUCCESS) {
717         BTC_TRACE_ERROR("%s btc_transfer_context failed\n", __func__);
718     }
719 }
720 
721 
btc_add_whitelist_complete_callback(UINT8 status,tBTM_WL_OPERATION wl_opration)722 static void btc_add_whitelist_complete_callback(UINT8 status, tBTM_WL_OPERATION wl_opration)
723 {
724     esp_ble_gap_cb_param_t param;
725     bt_status_t ret;
726     btc_msg_t msg = {0};
727     msg.sig = BTC_SIG_API_CB;
728     msg.pid = BTC_PID_GAP_BLE;
729     msg.act = ESP_GAP_BLE_UPDATE_WHITELIST_COMPLETE_EVT;
730     param.update_whitelist_cmpl.status = btc_hci_to_esp_status(status);
731     param.update_whitelist_cmpl.wl_opration = wl_opration;
732     ret = btc_transfer_context(&msg, &param,
733                                sizeof(esp_ble_gap_cb_param_t), NULL);
734 
735     if (ret != BT_STATUS_SUCCESS) {
736         BTC_TRACE_ERROR("%s btc_transfer_context failed\n", __func__);
737     }
738 }
739 
btc_set_rand_addr_callback(UINT8 status)740 static void btc_set_rand_addr_callback(UINT8 status)
741 {
742     esp_ble_gap_cb_param_t param;
743     bt_status_t ret;
744     btc_msg_t msg = {0};
745     param.set_rand_addr_cmpl.status = btc_btm_status_to_esp_status(status); //todo status
746     msg.sig = BTC_SIG_API_CB;
747     msg.pid = BTC_PID_GAP_BLE;
748     msg.act = ESP_GAP_BLE_SET_STATIC_RAND_ADDR_EVT;
749     ret = btc_transfer_context(&msg, &param,
750                                sizeof(esp_ble_gap_cb_param_t), NULL);
751 
752     if (ret != BT_STATUS_SUCCESS) {
753         BTC_TRACE_ERROR("%s btc_transfer_context failed\n", __func__);
754     }
755 
756 }
757 
btc_set_local_privacy_callback(UINT8 status)758 static void btc_set_local_privacy_callback(UINT8 status)
759 {
760     esp_ble_gap_cb_param_t param;
761     bt_status_t ret;
762     btc_msg_t msg = {0};
763     msg.sig = BTC_SIG_API_CB;
764     msg.pid = BTC_PID_GAP_BLE;
765     msg.act = ESP_GAP_BLE_SET_LOCAL_PRIVACY_COMPLETE_EVT;
766     param.local_privacy_cmpl.status = btc_btm_status_to_esp_status(status);
767     ret = btc_transfer_context(&msg, &param,
768                                sizeof(esp_ble_gap_cb_param_t), NULL);
769 
770     if (ret != BT_STATUS_SUCCESS) {
771         BTC_TRACE_ERROR("%s btc_transfer_context failed\n", __func__);
772     }
773 }
774 
775 
776 #if (SMP_INCLUDED == TRUE)
btc_set_encryption_callback(BD_ADDR bd_addr,tBTA_TRANSPORT transport,tBTA_STATUS enc_status)777 static void btc_set_encryption_callback(BD_ADDR bd_addr, tBTA_TRANSPORT transport, tBTA_STATUS enc_status)
778 {
779     UNUSED(bd_addr);
780     UNUSED(transport);
781     BTC_TRACE_DEBUG("enc_status = %x\n", enc_status);
782     return;
783 }
784 #endif  ///SMP_INCLUDED == TRUE
785 
btc_read_ble_rssi_cmpl_callback(void * p_data)786 static void btc_read_ble_rssi_cmpl_callback(void *p_data)
787 {
788     tBTA_RSSI_RESULTS *result = (tBTA_RSSI_RESULTS *)p_data;
789     esp_ble_gap_cb_param_t param;
790     bt_status_t ret;
791     btc_msg_t msg = {0};
792     msg.sig = BTC_SIG_API_CB;
793     msg.pid = BTC_PID_GAP_BLE;
794     msg.act = ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT;
795     param.read_rssi_cmpl.rssi = result->rssi;
796     param.read_rssi_cmpl.status = btc_btm_status_to_esp_status(result->status);
797     memcpy(param.read_rssi_cmpl.remote_addr, result->rem_bda, sizeof(BD_ADDR));
798 
799     ret = btc_transfer_context(&msg, &param,
800                                sizeof(esp_ble_gap_cb_param_t), NULL);
801 
802     if (ret != BT_STATUS_SUCCESS) {
803         BTC_TRACE_ERROR("%s btc_transfer_context failed\n", __func__);
804     }
805 }
806 
807 #if (BLE_50_FEATURE_SUPPORT == TRUE)
btc_ble_5_gap_callback(tBTA_DM_BLE_5_GAP_EVENT event,tBTA_DM_BLE_5_GAP_CB_PARAMS * params)808 static void btc_ble_5_gap_callback(tBTA_DM_BLE_5_GAP_EVENT event,
809                                                 tBTA_DM_BLE_5_GAP_CB_PARAMS *params)
810 {
811     esp_ble_gap_cb_param_t param;
812     bt_status_t ret;
813     btc_msg_t msg;
814     msg.sig = BTC_SIG_API_CB;
815     msg.pid = BTC_PID_GAP_BLE;
816 
817     switch(event) {
818         case BTA_DM_BLE_5_GAP_READ_PHY_COMPLETE_EVT: {
819             msg.act = ESP_GAP_BLE_READ_PHY_COMPLETE_EVT;
820             param.read_phy.status = btc_btm_status_to_esp_status(params->read_phy.status);
821             memcpy(param.read_phy.bda,  params->read_phy.addr, BD_ADDR_LEN);
822             param.read_phy.tx_phy = params->read_phy.tx_phy;
823             param.read_phy.rx_phy = params->read_phy.rx_phy;
824             break;
825         }
826         case BTA_DM_BLE_5_GAP_SET_PREFERED_DEFAULT_PHY_COMPLETE_EVT: {
827             msg.act = ESP_GAP_BLE_SET_PREFERED_DEFAULT_PHY_COMPLETE_EVT;
828             param.set_perf_def_phy.status = btc_btm_status_to_esp_status(params->set_perf_def_phy.status);
829             break;
830         }
831         case BTA_DM_BLE_5_GAP_SET_PREFERED_PHY_COMPLETE_EVT: {
832             msg.act = ESP_GAP_BLE_SET_PREFERED_PHY_COMPLETE_EVT;
833             param.set_perf_phy.status = btc_btm_status_to_esp_status(params->set_perf_phy.status);
834             break;
835         }
836         case BTA_DM_BLE_5_GAP_EXT_ADV_SET_RAND_ADDR_COMPLETE_EVT:
837             msg.act = ESP_GAP_BLE_EXT_ADV_SET_RAND_ADDR_COMPLETE_EVT;
838             param.ext_adv_set_rand_addr.status = btc_btm_status_to_esp_status(params->set_ext_rand_addr.status);
839             break;
840         case BTA_DM_BLE_5_GAP_EXT_ADV_SET_PARAMS_COMPLETE_EVT: {
841             msg.act = ESP_GAP_BLE_EXT_ADV_SET_PARAMS_COMPLETE_EVT;
842             param.ext_adv_set_rand_addr.status = btc_btm_status_to_esp_status(params->set_params.status);
843             break;
844         }
845         case BTA_DM_BLE_5_GAP_EXT_ADV_DATA_SET_COMPLETE_EVT: {
846             msg.act = ESP_GAP_BLE_EXT_ADV_DATA_SET_COMPLETE_EVT;
847             param.ext_adv_data_set.status = btc_btm_status_to_esp_status(params->adv_data_set.status);
848             break;
849         }
850         case BTA_DM_BLE_5_GAP_EXT_SCAN_RSP_DATA_SET_COMPLETE_EVT: {
851             msg.act = ESP_GAP_BLE_EXT_SCAN_RSP_DATA_SET_COMPLETE_EVT;
852             param.scan_rsp_set.status = btc_btm_status_to_esp_status(params->scan_rsp_data_set.status);
853             break;
854         }
855         case BTA_DM_BLE_5_GAP_EXT_ADV_START_COMPLETE_EVT: {
856             msg.act = ESP_GAP_BLE_EXT_ADV_START_COMPLETE_EVT;
857             param.ext_adv_start.status = btc_btm_status_to_esp_status(params->adv_start.status);
858             break;
859         }
860         case BTA_DM_BLE_5_GAP_EXT_ADV_STOP_COMPLETE_EVT:
861             msg.act = ESP_GAP_BLE_EXT_ADV_STOP_COMPLETE_EVT;
862             param.ext_adv_stop.status = btc_btm_status_to_esp_status(params->adv_start.status);
863             break;
864         case BTA_DM_BLE_5_GAP_EXT_ADV_SET_REMOVE_COMPLETE_EVT:
865             msg.act = ESP_GAP_BLE_EXT_ADV_SET_REMOVE_COMPLETE_EVT;
866             param.ext_adv_remove.status = btc_btm_status_to_esp_status(params->adv_start.status);
867             break;
868         case BTA_DM_BLE_5_GAP_EXT_ADV_SET_CLEAR_COMPLETE_EVT:
869             msg.act = ESP_GAP_BLE_EXT_ADV_SET_CLEAR_COMPLETE_EVT;
870             param.ext_adv_clear.status = btc_btm_status_to_esp_status(params->adv_start.status);
871             break;
872         case BTA_DM_BLE_5_GAP_PERIODIC_ADV_SET_PARAMS_COMPLETE_EVT: {
873             msg.act = ESP_GAP_BLE_PERIODIC_ADV_SET_PARAMS_COMPLETE_EVT;
874             param.peroid_adv_set_params.status = btc_btm_status_to_esp_status(params->per_adv_set_params.status);
875             break;
876         }
877         case BTA_DM_BLE_5_GAP_PERIODIC_ADV_DATA_SET_COMPLETE_EVT: {
878             msg.act = ESP_GAP_BLE_PERIODIC_ADV_DATA_SET_COMPLETE_EVT;
879             param.period_adv_data_set.status = btc_btm_status_to_esp_status(params->per_adv_data_set.status);
880             break;
881         }
882         case BTA_DM_BLE_5_GAP_PERIODIC_ADV_START_COMPLETE_EVT: {
883             msg.act = ESP_GAP_BLE_PERIODIC_ADV_START_COMPLETE_EVT;
884             param.period_adv_start.status = btc_btm_status_to_esp_status(params->per_adv_start.status);
885             break;
886         }
887         case BTA_DM_BLE_5_GAP_PERIODIC_ADV_STOP_COMPLETE_EVT: {
888             msg.act = ESP_GAP_BLE_PERIODIC_ADV_STOP_COMPLETE_EVT;
889             param.period_adv_stop.status = btc_btm_status_to_esp_status(params->per_adv_stop.status);
890             break;
891         }
892         case BTA_DM_BLE_5_GAP_PERIODIC_ADV_CREATE_SYNC_COMPLETE_EVT: {
893             msg.act = ESP_GAP_BLE_PERIODIC_ADV_CREATE_SYNC_COMPLETE_EVT;
894             param.period_adv_create_sync.status = btc_btm_status_to_esp_status(params->per_adv_sync_create.status);
895             break;
896         }
897         case BTA_DM_BLE_5_GAP_PERIODIC_ADV_SYNC_CANCEL_COMPLETE_EVT: {
898             msg.act = ESP_GAP_BLE_PERIODIC_ADV_SYNC_CANCEL_COMPLETE_EVT;
899             param.period_adv_sync_cancel.status = btc_btm_status_to_esp_status(params->per_adv_sync_cancel.status);
900             break;
901         }
902         case BTA_DM_BLE_5_GAP_PERIODIC_ADV_SYNC_TERMINATE_COMPLETE_EVT: {
903             msg.act = ESP_GAP_BLE_PERIODIC_ADV_SYNC_TERMINATE_COMPLETE_EVT;
904             param.period_adv_sync_term.status = btc_btm_status_to_esp_status(params->per_adv_sync_term.status);
905             break;
906         }
907         case BTA_DM_BLE_5_GAP_PERIODIC_ADV_ADD_DEV_COMPLETE_EVT: {
908             msg.act = ESP_GAP_BLE_PERIODIC_ADV_ADD_DEV_COMPLETE_EVT;
909             param.period_adv_add_dev.status = btc_btm_status_to_esp_status(params->per_adv_add_dev.status);
910             break;
911         }
912         case BTA_DM_BLE_5_GAP_PERIODIC_ADV_REMOVE_DEV_COMPLETE_EVT: {
913             msg.act = ESP_GAP_BLE_PERIODIC_ADV_REMOVE_DEV_COMPLETE_EVT;
914             param.period_adv_remove_dev.status = btc_btm_status_to_esp_status(params->per_adv_remove_dev.status);
915             break;
916         }
917         case BTA_DM_BLE_5_GAP_PERIODIC_ADV_CLEAR_DEV_COMPLETE_EVT: {
918             msg.act = ESP_GAP_BLE_PERIODIC_ADV_CLEAR_DEV_COMPLETE_EVT;
919             param.period_adv_clear_dev.status = btc_btm_status_to_esp_status(params->per_adv_clear_dev.status);
920             break;
921         }
922         case BTA_DM_BLE_5_GAP_SET_EXT_SCAN_PARAMS_COMPLETE_EVT: {
923             msg.act = ESP_GAP_BLE_SET_EXT_SCAN_PARAMS_COMPLETE_EVT;
924             param.set_ext_scan_params.status = btc_btm_status_to_esp_status(params->ext_scan.status);
925             break;
926         }
927         case BTA_DM_BLE_5_GAP_EXT_SCAN_START_COMPLETE_EVT: {
928             msg.act = ESP_GAP_BLE_EXT_SCAN_START_COMPLETE_EVT;
929             param.ext_scan_start.status = btc_btm_status_to_esp_status(params->scan_start.status);
930             break;
931         }
932         case BTA_DM_BLE_5_GAP_EXT_SCAN_STOP_COMPLETE_EVT: {
933             msg.act = ESP_GAP_BLE_EXT_SCAN_STOP_COMPLETE_EVT;
934             param.ext_scan_stop.status = btc_btm_status_to_esp_status(params->scan_stop.status);
935             break;
936         }
937         case BTA_DM_BLE_5_GAP_PREFER_EXT_CONN_PARAMS_SET_COMPLETE_EVT: {
938             msg.act = ESP_GAP_BLE_PREFER_EXT_CONN_PARAMS_SET_COMPLETE_EVT;
939             param.ext_conn_params_set.status = btc_btm_status_to_esp_status(params->ext_conn_set_params.status);
940             break;
941         }
942         case BTA_DM_BLE_5_GAP_PHY_UPDATE_COMPLETE_EVT:
943             msg.act = ESP_GAP_BLE_PHY_UPDATE_COMPLETE_EVT;
944             param.phy_update.status = btc_btm_status_to_esp_status(params->phy_update.status);
945             memcpy(param.phy_update.bda, params->phy_update.addr, BD_ADDR_LEN);
946             param.phy_update.tx_phy = params->phy_update.tx_phy;
947             param.phy_update.rx_phy = params->phy_update.rx_phy;
948             break;
949         case BTA_DM_BLE_5_GAP_EXT_ADV_REPORT_EVT:
950             msg.act = ESP_GAP_BLE_EXT_ADV_REPORT_EVT;
951             memcpy(&param.ext_adv_report.params, &params->ext_adv_report, sizeof(esp_ble_gap_ext_adv_reprot_t));
952             memcpy(param.ext_adv_report.params.adv_data,
953                    params->ext_adv_report.adv_data, params->ext_adv_report.adv_data_len);
954             break;
955         case BTA_DM_BLE_5_GAP_SCAN_TIMEOUT_EVT:
956             msg.act = ESP_GAP_BLE_SCAN_TIMEOUT_EVT;
957             break;
958         case BTA_DM_BLE_5_GAP_ADV_TERMINATED_EVT: {
959             param.adv_terminate.status = params->adv_term.status;
960             param.adv_terminate.adv_instance = params->adv_term.adv_handle;
961             param.adv_terminate.conn_idx = params->adv_term.conn_handle;
962             param.adv_terminate.completed_event = params->adv_term.completed_event;
963             msg.act = ESP_GAP_BLE_ADV_TERMINATED_EVT;
964             break;
965         }
966         case BTA_DM_BLE_5_GAP_SCAN_REQ_RECEIVED_EVT: {
967             msg.act = ESP_GAP_BLE_SCAN_REQ_RECEIVED_EVT;
968             param.scan_req_received.adv_instance = params->scan_req.adv_handle;
969             param.scan_req_received.scan_addr_type = params->scan_req.scan_addr_type;
970             memcpy(param.scan_req_received.scan_addr, params->scan_req.scan_addr, sizeof(BD_ADDR));
971             break;
972         }
973         case BTA_DM_BLE_5_GAP_CHANNEL_SELETE_ALGORITHM_EVT: {
974             msg.act = ESP_GAP_BLE_CHANNEL_SELETE_ALGORITHM_EVT;
975             param.channel_sel_alg.conn_handle = params->channel_sel.conn_handle;
976             param.channel_sel_alg.channel_sel_alg = params->channel_sel.channel_sel_alg;
977             break;
978         }
979         case BTA_DM_BLE_5_GAP_PERIODIC_ADV_REPORT_EVT: {
980             msg.act = ESP_GAP_BLE_PERIODIC_ADV_REPORT_EVT;
981             memcpy(&param.period_adv_report, &params->period_adv_report,
982                    sizeof(esp_ble_gap_periodic_adv_report_t));
983             memcpy(param.period_adv_report.params.data, params->period_adv_report.data,
984                    params->period_adv_report.data_length);
985             break;
986         }
987         case BTA_DM_BLE_5_GAP_PERIODIC_ADV_SYNC_LOST_EVT: {
988             msg.act = ESP_GAP_BLE_PERIODIC_ADV_SYNC_LOST_EVT;
989             param.periodic_adv_sync_lost.sync_handle = params->sync_lost.sync_handle;
990             break;
991         }
992         case BTA_DM_BLE_5_GAP_PERIODIC_ADV_SYNC_ESTAB_EVT: {
993             msg.act = ESP_GAP_BLE_PERIODIC_ADV_SYNC_ESTAB_EVT;
994             param.periodic_adv_sync_estab.status = btc_btm_status_to_esp_status(params->sync_estab.status);
995             param.periodic_adv_sync_estab.sync_handle = params->sync_estab.sync_handle;
996             param.periodic_adv_sync_estab.sid = params->sync_estab.sid;
997             param.periodic_adv_sync_estab.adv_addr_type = params->sync_estab.adv_addr_type;
998             memcpy(param.periodic_adv_sync_estab.adv_addr, params->sync_estab.adv_addr,
999                   sizeof(BD_ADDR));
1000             param.periodic_adv_sync_estab.adv_phy = params->sync_estab.adv_phy;
1001             param.periodic_adv_sync_estab.period_adv_interval = params->sync_estab.period_adv_interval;
1002             param.periodic_adv_sync_estab.adv_clk_accuracy = params->sync_estab.adv_clk_accuracy;
1003             break;
1004         }
1005         default:
1006             break;
1007     }
1008 
1009     ret = btc_transfer_context(&msg, &param,
1010                                sizeof(esp_ble_gap_cb_param_t), NULL);
1011 
1012     if (ret != BT_STATUS_SUCCESS) {
1013         BTC_TRACE_ERROR("%s btc_transfer_context failed\n", __func__);
1014     }
1015 }
1016 #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
btc_get_whitelist_size(uint16_t * length)1017 void btc_get_whitelist_size(uint16_t *length)
1018 {
1019     BTM_BleGetWhiteListSize(length);
1020     return;
1021 }
1022 #if (BLE_42_FEATURE_SUPPORT == TRUE)
btc_ble_start_scanning(uint32_t duration,tBTA_DM_SEARCH_CBACK * results_cb,tBTA_START_STOP_SCAN_CMPL_CBACK * start_scan_cb)1023 static void btc_ble_start_scanning(uint32_t duration,
1024                                    tBTA_DM_SEARCH_CBACK *results_cb,
1025                                    tBTA_START_STOP_SCAN_CMPL_CBACK *start_scan_cb)
1026 {
1027     if ((results_cb != NULL) && (start_scan_cb != NULL)) {
1028 #if SCAN_QUEUE_CONGEST_CHECK
1029         btc_adv_list_refresh();
1030 #endif
1031         //Start scan the device
1032         BTA_DmBleScan(true, duration, results_cb, start_scan_cb);
1033     } else {
1034         BTC_TRACE_ERROR("The start_scan_cb or results_cb invalid\n");
1035     }
1036 }
1037 
btc_ble_stop_scanning(tBTA_START_STOP_SCAN_CMPL_CBACK * stop_scan_cb)1038 static void btc_ble_stop_scanning(tBTA_START_STOP_SCAN_CMPL_CBACK *stop_scan_cb)
1039 {
1040     uint8_t duration = 0;
1041     BTA_DmBleScan(false, duration, NULL, stop_scan_cb);
1042 }
1043 
1044 
btc_ble_stop_advertising(tBTA_START_STOP_ADV_CMPL_CBACK * stop_adv_cb)1045 static void btc_ble_stop_advertising(tBTA_START_STOP_ADV_CMPL_CBACK *stop_adv_cb)
1046 {
1047     bool stop_adv = false;
1048 
1049     BTA_DmBleBroadcast(stop_adv, stop_adv_cb);
1050 }
1051 #endif // #if (BLE_42_FEATURE_SUPPORT == TRUE)
btc_ble_update_conn_params(BD_ADDR bd_addr,uint16_t min_int,uint16_t max_int,uint16_t latency,uint16_t timeout)1052 static void btc_ble_update_conn_params(BD_ADDR bd_addr, uint16_t min_int,
1053                                        uint16_t max_int, uint16_t latency, uint16_t timeout)
1054 {
1055     if (min_int > max_int) {
1056         min_int = max_int;
1057     }
1058 
1059     if (min_int < BTM_BLE_CONN_INT_MIN || max_int > BTM_BLE_CONN_INT_MAX) {
1060         BTC_TRACE_ERROR("Invalid interval value.\n");
1061     }
1062 
1063     BTA_DmBleUpdateConnectionParams(bd_addr, min_int, max_int,
1064                                     latency, timeout);
1065 }
1066 
btc_ble_set_pkt_data_len(BD_ADDR remote_device,uint16_t tx_data_length,tBTA_SET_PKT_DATA_LENGTH_CBACK * p_set_pkt_data_cback)1067 static void btc_ble_set_pkt_data_len(BD_ADDR remote_device, uint16_t tx_data_length, tBTA_SET_PKT_DATA_LENGTH_CBACK *p_set_pkt_data_cback)
1068 {
1069     if (tx_data_length > BTM_BLE_DATA_SIZE_MAX) {
1070         tx_data_length =  BTM_BLE_DATA_SIZE_MAX;
1071     } else if (tx_data_length < BTM_BLE_DATA_SIZE_MIN) {
1072         tx_data_length =  BTM_BLE_DATA_SIZE_MIN;
1073     }
1074 
1075     BTA_DmBleSetDataLength(remote_device, tx_data_length, p_set_pkt_data_cback);
1076 }
1077 
btc_ble_config_local_icon(uint16_t icon)1078 static void btc_ble_config_local_icon(uint16_t icon)
1079 {
1080     BTA_DmBleConfigLocalIcon(icon);
1081 }
1082 
btc_ble_set_rand_addr(BD_ADDR rand_addr,tBTA_SET_RAND_ADDR_CBACK * p_set_rand_addr_cback)1083 static void btc_ble_set_rand_addr (BD_ADDR rand_addr, tBTA_SET_RAND_ADDR_CBACK *p_set_rand_addr_cback)
1084 {
1085     if (rand_addr != NULL) {
1086         /*
1087         A static address is a 48-bit randomly generated address and shall meet the following requirements:
1088         • The two most significant bits of the address shall be equal to 1
1089         • All bits of the random part of the address shall not be equal to 1
1090         • All bits of the random part of the address shall not be equal to 0
1091         A non-resolvable private address is a 48-bit randomly generated address and shall meet the following requirements:
1092         • The two most significant bits of the address shall be equal to 0
1093         • All bits of the random part of the address shall not be equal to 1
1094         • All bits of the random part of the address shall not be equal to 0
1095         */
1096         BD_ADDR invalid_rand_addr_a, invalid_rand_addr_b;
1097         memset(invalid_rand_addr_a, 0xff, sizeof(BD_ADDR));
1098         memset(invalid_rand_addr_b, 0x00, sizeof(BD_ADDR));
1099 
1100         if((rand_addr[0] & BT_STATIC_RAND_ADDR_MASK) == BT_STATIC_RAND_ADDR_MASK) {
1101             invalid_rand_addr_b[0] = invalid_rand_addr_b[0] | BT_STATIC_RAND_ADDR_MASK;
1102             if (memcmp(invalid_rand_addr_a, rand_addr, BD_ADDR_LEN) != 0 && memcmp(invalid_rand_addr_b, rand_addr, BD_ADDR_LEN) != 0) {
1103                 BTA_DmSetRandAddress(rand_addr, btc_set_rand_addr_callback);
1104             } else {
1105                 btc_set_rand_addr_callback(BTM_INVALID_STATIC_RAND_ADDR);
1106                 BTC_TRACE_ERROR("Invalid static random address, the high bit should be 0b11, bits of the random part shall not be all 1 or 0");
1107             }
1108         } else if ((rand_addr[0] | BT_NON_RPA_MASK) == BT_NON_RPA_MASK) {
1109             invalid_rand_addr_a[0] = invalid_rand_addr_a[0] & BT_NON_RPA_MASK;
1110             if (memcmp(invalid_rand_addr_a, rand_addr, BD_ADDR_LEN) != 0 && memcmp(invalid_rand_addr_b, rand_addr, BD_ADDR_LEN) != 0) {
1111                 BTA_DmSetRandAddress(rand_addr, btc_set_rand_addr_callback);
1112             } else {
1113                 btc_set_rand_addr_callback(BTM_INVALID_STATIC_RAND_ADDR);
1114                 BTC_TRACE_ERROR("Invalid non-resolvable private address, the high bit should be 0b00, bits of the random part shall not be all 1 or 0");
1115             }
1116         }else {
1117             btc_set_rand_addr_callback(BTM_INVALID_STATIC_RAND_ADDR);
1118             BTC_TRACE_ERROR("Invalid random address type");
1119         }
1120     } else {
1121         btc_set_rand_addr_callback(BTM_INVALID_STATIC_RAND_ADDR);
1122         BTC_TRACE_ERROR("Invalid address, the address value is NULL");
1123     }
1124 }
1125 
btc_ble_clear_rand_addr(void)1126 static void btc_ble_clear_rand_addr (void)
1127 {
1128     BTA_DmClearRandAddress();
1129 }
1130 
btc_ble_config_local_privacy(bool privacy_enable,tBTA_SET_LOCAL_PRIVACY_CBACK * set_local_privacy_cback)1131 static void btc_ble_config_local_privacy(bool privacy_enable, tBTA_SET_LOCAL_PRIVACY_CBACK *set_local_privacy_cback)
1132 {
1133     BTA_DmBleConfigLocalPrivacy(privacy_enable, set_local_privacy_cback);
1134 }
1135 
btc_ble_disconnect(BD_ADDR bd_addr)1136 static void btc_ble_disconnect(BD_ADDR bd_addr)
1137 {
1138     BTA_DmBleDisconnect(bd_addr);
1139 }
1140 
btc_gap_ble_set_channels(esp_gap_ble_channels channels)1141 static void btc_gap_ble_set_channels(esp_gap_ble_channels channels)
1142 {
1143     BTA_DmBleSetChannels(channels, btc_gap_ble_set_channels_cmpl_callback);
1144 }
1145 
1146 
btc_gap_ble_cb_handler(btc_msg_t * msg)1147 void btc_gap_ble_cb_handler(btc_msg_t *msg)
1148 {
1149     esp_ble_gap_cb_param_t *param = (esp_ble_gap_cb_param_t *)msg->arg;
1150 
1151     if (msg->act < ESP_GAP_BLE_EVT_MAX) {
1152         btc_gap_ble_cb_to_app(msg->act, param);
1153     } else {
1154         BTC_TRACE_ERROR("%s, unknow msg->act = %d", __func__, msg->act);
1155     }
1156 
1157     btc_gap_ble_cb_deep_free(msg);
1158 
1159 }
1160 
btc_gap_ble_arg_deep_copy(btc_msg_t * msg,void * p_dest,void * p_src)1161 void btc_gap_ble_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src)
1162 {
1163     switch (msg->act) {
1164 #if (BLE_42_FEATURE_SUPPORT == TRUE)
1165     case BTC_GAP_BLE_ACT_CFG_ADV_DATA: {
1166         btc_ble_gap_args_t *src = (btc_ble_gap_args_t *)p_src;
1167         btc_ble_gap_args_t  *dst = (btc_ble_gap_args_t *) p_dest;
1168 
1169         if (src->cfg_adv_data.adv_data.p_manufacturer_data) {
1170             dst->cfg_adv_data.adv_data.p_manufacturer_data = osi_malloc(src->cfg_adv_data.adv_data.manufacturer_len);
1171             memcpy(dst->cfg_adv_data.adv_data.p_manufacturer_data, src->cfg_adv_data.adv_data.p_manufacturer_data,
1172                    src->cfg_adv_data.adv_data.manufacturer_len);
1173         }
1174 
1175         if (src->cfg_adv_data.adv_data.p_service_data) {
1176             dst->cfg_adv_data.adv_data.p_service_data = osi_malloc(src->cfg_adv_data.adv_data.service_data_len);
1177             memcpy(dst->cfg_adv_data.adv_data.p_service_data, src->cfg_adv_data.adv_data.p_service_data, src->cfg_adv_data.adv_data.service_data_len);
1178         }
1179 
1180         if (src->cfg_adv_data.adv_data.p_service_uuid) {
1181             dst->cfg_adv_data.adv_data.p_service_uuid = osi_malloc(src->cfg_adv_data.adv_data.service_uuid_len);
1182             memcpy(dst->cfg_adv_data.adv_data.p_service_uuid, src->cfg_adv_data.adv_data.p_service_uuid, src->cfg_adv_data.adv_data.service_uuid_len);
1183         }
1184         break;
1185     }
1186     case BTC_GAP_BLE_ACT_CFG_ADV_DATA_RAW: {
1187         btc_ble_gap_args_t *src = (btc_ble_gap_args_t *)p_src;
1188         btc_ble_gap_args_t *dst = (btc_ble_gap_args_t *) p_dest;
1189 
1190         if (src && src->cfg_adv_data_raw.raw_adv && src->cfg_adv_data_raw.raw_adv_len > 0) {
1191             dst->cfg_adv_data_raw.raw_adv = osi_malloc(src->cfg_adv_data_raw.raw_adv_len);
1192             if (dst->cfg_adv_data_raw.raw_adv) {
1193                 memcpy(dst->cfg_adv_data_raw.raw_adv, src->cfg_adv_data_raw.raw_adv, src->cfg_adv_data_raw.raw_adv_len);
1194             }
1195         }
1196         break;
1197     }
1198     case BTC_GAP_BLE_ACT_CFG_SCAN_RSP_DATA_RAW: {
1199         btc_ble_gap_args_t *src = (btc_ble_gap_args_t *)p_src;
1200         btc_ble_gap_args_t *dst = (btc_ble_gap_args_t *) p_dest;
1201 
1202         if (src && src->cfg_scan_rsp_data_raw.raw_scan_rsp && src->cfg_scan_rsp_data_raw.raw_scan_rsp_len > 0) {
1203             dst->cfg_scan_rsp_data_raw.raw_scan_rsp = osi_malloc(src->cfg_scan_rsp_data_raw.raw_scan_rsp_len);
1204             if (dst->cfg_scan_rsp_data_raw.raw_scan_rsp) {
1205                 memcpy(dst->cfg_scan_rsp_data_raw.raw_scan_rsp, src->cfg_scan_rsp_data_raw.raw_scan_rsp, src->cfg_scan_rsp_data_raw.raw_scan_rsp_len);
1206             }
1207         }
1208           break;
1209        }
1210 #endif // #if (BLE_42_FEATURE_SUPPORT == TRUE)
1211     case BTC_GAP_BLE_SET_SECURITY_PARAM_EVT: {
1212         btc_ble_gap_args_t *src = (btc_ble_gap_args_t *)p_src;
1213         btc_ble_gap_args_t  *dst = (btc_ble_gap_args_t *) p_dest;
1214         uint8_t length = 0;
1215         if (src->set_security_param.value) {
1216             length = dst->set_security_param.len;
1217             dst->set_security_param.value = osi_malloc(length);
1218             if (dst->set_security_param.value != NULL) {
1219                 memcpy(dst->set_security_param.value, src->set_security_param.value, length);
1220             } else {
1221                 BTC_TRACE_ERROR("%s %d no mem\n",__func__, msg->act);
1222             }
1223         }
1224         break;
1225     }
1226     case BTC_GAP_BLE_OOB_REQ_REPLY_EVT: {
1227         btc_ble_gap_args_t *src = (btc_ble_gap_args_t *)p_src;
1228         btc_ble_gap_args_t  *dst = (btc_ble_gap_args_t *) p_dest;
1229         uint8_t length = 0;
1230         if (src->oob_req_reply.p_value) {
1231             length = dst->oob_req_reply.len;
1232             dst->oob_req_reply.p_value = osi_malloc(length);
1233             if (dst->oob_req_reply.p_value != NULL) {
1234                 memcpy(dst->oob_req_reply.p_value, src->oob_req_reply.p_value, length);
1235             } else {
1236                 BTC_TRACE_ERROR("%s %d no mem\n",__func__, msg->act);
1237             }
1238         }
1239         break;
1240     }
1241 #if (BLE_50_FEATURE_SUPPORT == TRUE)
1242     case BTC_GAP_BLE_CFG_EXT_ADV_DATA_RAW:
1243     case BTC_GAP_BLE_CFG_EXT_SCAN_RSP_DATA_RAW: {
1244         btc_ble_5_gap_args_t *src = (btc_ble_5_gap_args_t *)p_src;
1245         btc_ble_5_gap_args_t *dst = (btc_ble_5_gap_args_t *)p_dest;
1246         uint16_t length = 0;
1247 
1248         if (src->ext_adv_cfg_data.data) {
1249             length = src->ext_adv_cfg_data.length;
1250             dst->ext_adv_cfg_data.data = osi_malloc(length);
1251             if (dst->ext_adv_cfg_data.data) {
1252                 memcpy(dst->ext_adv_cfg_data.data, src->ext_adv_cfg_data.data, length);
1253             } else {
1254                 BTC_TRACE_ERROR("%s %d no mem\n",__func__, msg->act);
1255             }
1256         }
1257         break;
1258     }
1259     case BTC_GAP_BLE_CFG_PERIODIC_ADV_DATA_RAW: {
1260         btc_ble_5_gap_args_t *src = (btc_ble_5_gap_args_t *)p_src;
1261         btc_ble_5_gap_args_t *dst = (btc_ble_5_gap_args_t *)p_dest;
1262         uint16_t length = 0;
1263 
1264         if (src->periodic_adv_cfg_data.data) {
1265             length = src->periodic_adv_cfg_data.len;
1266             dst->periodic_adv_cfg_data.data = osi_malloc(length);
1267             if (dst->periodic_adv_cfg_data.data) {
1268                 memcpy(dst->periodic_adv_cfg_data.data, src->periodic_adv_cfg_data.data, length);
1269             } else {
1270                 BTC_TRACE_ERROR("%s %d no mem\n",__func__, msg->act);
1271             }
1272         }
1273         break;
1274     }
1275     case BTC_GAP_BLE_EXT_ADV_START: {
1276         btc_ble_5_gap_args_t *src = (btc_ble_5_gap_args_t *)p_src;
1277         btc_ble_5_gap_args_t *dst = (btc_ble_5_gap_args_t *)p_dest;
1278 
1279         if (src->ext_adv_start.ext_adv) {
1280             dst->ext_adv_start.ext_adv = osi_malloc(src->ext_adv_start.num_adv*sizeof(esp_ble_gap_ext_adv_t));
1281             if (dst->ext_adv_start.ext_adv) {
1282                 memcpy(dst->ext_adv_start.ext_adv, src->ext_adv_start.ext_adv,
1283                         src->ext_adv_start.num_adv*sizeof(esp_ble_gap_ext_adv_t));
1284             }
1285         }
1286 	break;
1287     }
1288     case BTC_GAP_BLE_EXT_ADV_STOP: {
1289         btc_ble_5_gap_args_t *src = (btc_ble_5_gap_args_t *)p_src;
1290         btc_ble_5_gap_args_t *dst = (btc_ble_5_gap_args_t *)p_dest;
1291 
1292         if (src->ext_adv_stop.ext_adv_inst) {
1293             dst->ext_adv_stop.ext_adv_inst = osi_malloc(src->ext_adv_stop.num_adv*sizeof(uint8_t));
1294             if (dst->ext_adv_stop.ext_adv_inst) {
1295                 memcpy(dst->ext_adv_stop.ext_adv_inst, src->ext_adv_stop.ext_adv_inst,
1296                         src->ext_adv_stop.num_adv * sizeof(uint8_t));
1297             }
1298         }
1299 	break;
1300     }
1301 #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
1302     default:
1303         BTC_TRACE_ERROR("Unhandled deep copy %d\n", msg->act);
1304         break;
1305     }
1306 }
1307 
btc_gap_ble_cb_deep_copy(btc_msg_t * msg,void * p_dest,void * p_src)1308 void btc_gap_ble_cb_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src)
1309 {
1310     switch (msg->act) {
1311     default:
1312        BTC_TRACE_ERROR("%s, Unhandled deep copy %d\n", __func__, msg->act);
1313        break;
1314     }
1315 }
1316 
btc_gap_ble_arg_deep_free(btc_msg_t * msg)1317 void btc_gap_ble_arg_deep_free(btc_msg_t *msg)
1318 {
1319     BTC_TRACE_DEBUG("%s \n", __func__);
1320     switch (msg->act) {
1321 #if (BLE_42_FEATURE_SUPPORT == TRUE)
1322     case BTC_GAP_BLE_ACT_CFG_ADV_DATA: {
1323         esp_ble_adv_data_t *adv = &((btc_ble_gap_args_t *)msg->arg)->cfg_adv_data.adv_data;
1324         if (adv->p_service_data) {
1325             osi_free(adv->p_service_data);
1326         }
1327 
1328         if (adv->p_service_uuid) {
1329             osi_free(adv->p_service_uuid);
1330         }
1331 
1332         if (adv->p_manufacturer_data) {
1333             osi_free(adv->p_manufacturer_data);
1334         }
1335         break;
1336     }
1337     case BTC_GAP_BLE_ACT_CFG_ADV_DATA_RAW: {
1338         uint8_t *raw_adv = ((btc_ble_gap_args_t *)msg->arg)->cfg_adv_data_raw.raw_adv;
1339         if (raw_adv) {
1340             osi_free(raw_adv);
1341         }
1342         break;
1343     }
1344     case BTC_GAP_BLE_ACT_CFG_SCAN_RSP_DATA_RAW: {
1345         uint8_t *raw_scan_rsp = ((btc_ble_gap_args_t *)msg->arg)->cfg_scan_rsp_data_raw.raw_scan_rsp;
1346         if (raw_scan_rsp) {
1347             osi_free(raw_scan_rsp);
1348         }
1349         break;
1350     }
1351 #endif // #if (BLE_42_FEATURE_SUPPORT == TRUE)
1352     case BTC_GAP_BLE_SET_SECURITY_PARAM_EVT: {
1353         uint8_t *value = ((btc_ble_gap_args_t *)msg->arg)->set_security_param.value;
1354         if (value) {
1355             osi_free(value);
1356         }
1357         break;
1358     }
1359     case BTC_GAP_BLE_OOB_REQ_REPLY_EVT: {
1360         uint8_t *value = ((btc_ble_gap_args_t *)msg->arg)->oob_req_reply.p_value;
1361         if (value) {
1362             osi_free(value);
1363         }
1364         break;
1365     }
1366 #if (BLE_50_FEATURE_SUPPORT == TRUE)
1367     case BTC_GAP_BLE_CFG_EXT_ADV_DATA_RAW:
1368     case BTC_GAP_BLE_CFG_EXT_SCAN_RSP_DATA_RAW: {
1369         uint8_t *value = ((btc_ble_5_gap_args_t *)msg->arg)->ext_adv_cfg_data.data;
1370         if (value) {
1371             osi_free(value);
1372         }
1373 	break;
1374     }
1375     case BTC_GAP_BLE_CFG_PERIODIC_ADV_DATA_RAW: {
1376         uint8_t *value = ((btc_ble_5_gap_args_t *)msg->arg)->periodic_adv_cfg_data.data;
1377         if (value) {
1378             osi_free(value);
1379         }
1380 	break;
1381     }
1382     case BTC_GAP_BLE_EXT_ADV_START: {
1383         esp_ble_gap_ext_adv_t *value = ((btc_ble_5_gap_args_t *)msg->arg)->ext_adv_start.ext_adv;
1384         if (value) {
1385             osi_free(value);
1386         }
1387 	break;
1388     }
1389     case BTC_GAP_BLE_EXT_ADV_STOP: {
1390         uint8_t *value = ((btc_ble_5_gap_args_t *)msg->arg)->ext_adv_stop.ext_adv_inst;
1391         if (value) {
1392             osi_free(value);
1393         }
1394 	break;
1395     }
1396 #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
1397     default:
1398         BTC_TRACE_DEBUG("Unhandled deep free %d\n", msg->act);
1399         break;
1400     }
1401 }
1402 
btc_gap_ble_cb_deep_free(btc_msg_t * msg)1403 void btc_gap_ble_cb_deep_free(btc_msg_t *msg)
1404 {
1405     BTC_TRACE_DEBUG("%s", __func__);
1406     switch (msg->act) {
1407         default:
1408             BTC_TRACE_DEBUG("Unhandled deep free %d", msg->act);
1409             break;
1410     }
1411 }
1412 
btc_gap_ble_call_handler(btc_msg_t * msg)1413 void btc_gap_ble_call_handler(btc_msg_t *msg)
1414 {
1415     btc_ble_gap_args_t *arg = (btc_ble_gap_args_t *)msg->arg;
1416 #if (BLE_50_FEATURE_SUPPORT == TRUE)
1417     btc_ble_5_gap_args_t *arg_5 = (btc_ble_5_gap_args_t *)msg->arg;
1418 #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
1419 
1420     BTC_TRACE_DEBUG("%s act %d\n", __FUNCTION__, msg->act);
1421 
1422     switch (msg->act) {
1423 #if (BLE_42_FEATURE_SUPPORT == TRUE)
1424     case BTC_GAP_BLE_ACT_CFG_ADV_DATA: {
1425         if (arg->cfg_adv_data.adv_data.set_scan_rsp == false) {
1426             btc_ble_set_adv_data(&arg->cfg_adv_data.adv_data, btc_adv_data_callback);
1427         } else {
1428             btc_ble_set_adv_data(&arg->cfg_adv_data.adv_data, btc_scan_rsp_data_callback);
1429         }
1430         break;
1431     }
1432     case BTC_GAP_BLE_ACT_SET_SCAN_PARAM:
1433         btc_ble_set_scan_params(&arg->set_scan_param.scan_params, btc_scan_params_callback);
1434         break;
1435     case BTC_GAP_BLE_ACT_START_SCAN:
1436         btc_ble_start_scanning(arg->start_scan.duration, btc_search_callback, btc_start_scan_callback);
1437         break;
1438     case BTC_GAP_BLE_ACT_STOP_SCAN:
1439         btc_ble_stop_scanning(btc_stop_scan_callback);
1440         break;
1441     case BTC_GAP_BLE_ACT_START_ADV:
1442         btc_ble_start_advertising(&arg->start_adv.adv_params, btc_start_adv_callback);
1443         break;
1444     case BTC_GAP_BLE_ACT_STOP_ADV:
1445         btc_ble_stop_advertising(btc_stop_adv_callback);
1446         break;
1447 #endif // #if (BLE_42_FEATURE_SUPPORT == TRUE)
1448     case BTC_GAP_BLE_ACT_UPDATE_CONN_PARAM:
1449         btc_ble_update_conn_params(arg->conn_update_params.conn_params.bda,
1450                                    arg->conn_update_params.conn_params.min_int,
1451                                    arg->conn_update_params.conn_params.max_int,
1452                                    arg->conn_update_params.conn_params.latency,
1453                                    arg->conn_update_params.conn_params.timeout);
1454         break;
1455     case BTC_GAP_BLE_ACT_SET_PKT_DATA_LEN:
1456         btc_ble_set_pkt_data_len(arg->set_pkt_data_len.remote_device, arg->set_pkt_data_len.tx_data_length, btc_set_pkt_length_callback);
1457         break;
1458     case BTC_GAP_BLE_ACT_SET_RAND_ADDRESS: {
1459         BD_ADDR bd_addr;
1460         memcpy(bd_addr, arg->set_rand_addr.rand_addr, sizeof(BD_ADDR));
1461         btc_ble_set_rand_addr(bd_addr, btc_set_rand_addr_callback);
1462         break;
1463     }
1464     case BTC_GAP_BLE_ACT_CLEAR_RAND_ADDRESS: {
1465         btc_ble_clear_rand_addr();
1466         break;
1467     }
1468     case BTC_GAP_BLE_ACT_CONFIG_LOCAL_PRIVACY:
1469         btc_ble_config_local_privacy(arg->cfg_local_privacy.privacy_enable, btc_set_local_privacy_callback);
1470         break;
1471     case BTC_GAP_BLE_ACT_CONFIG_LOCAL_ICON:
1472         btc_ble_config_local_icon(arg->cfg_local_icon.icon);
1473         break;
1474     case BTC_GAP_BLE_ACT_UPDATE_WHITE_LIST:
1475         BTA_DmUpdateWhiteList(arg->update_white_list.add_remove, arg->update_white_list.remote_bda, arg->update_white_list.wl_addr_type, btc_add_whitelist_complete_callback);
1476         break;
1477     case BTC_GAP_BLE_ACT_CLEAR_WHITE_LIST:
1478         BTA_DmClearWhiteList();
1479         break;
1480     case BTC_GAP_BLE_ACT_READ_RSSI:
1481         BTA_DmReadRSSI(arg->read_rssi.remote_addr, BTA_TRANSPORT_LE, btc_read_ble_rssi_cmpl_callback);
1482         break;
1483 #if (BLE_42_FEATURE_SUPPORT == TRUE)
1484     case BTC_GAP_BLE_ACT_SET_CONN_PARAMS:
1485         BTA_DmSetBlePrefConnParams(arg->set_conn_params.bd_addr, arg->set_conn_params.min_conn_int,
1486                                                         arg->set_conn_params.max_conn_int, arg->set_conn_params.slave_latency,
1487                                                         arg->set_conn_params.supervision_tout);
1488         break;
1489 #endif // #if (BLE_42_FEATURE_SUPPORT == TRUE)
1490     case BTC_GAP_BLE_ACT_SET_DEV_NAME:
1491         BTA_DmSetDeviceName(arg->set_dev_name.device_name);
1492         break;
1493 #if (BLE_42_FEATURE_SUPPORT == TRUE)
1494     case BTC_GAP_BLE_ACT_CFG_ADV_DATA_RAW:
1495         btc_ble_set_adv_data_raw(arg->cfg_adv_data_raw.raw_adv,
1496                                  arg->cfg_adv_data_raw.raw_adv_len,
1497                                  btc_adv_data_raw_callback);
1498         break;
1499     case BTC_GAP_BLE_ACT_CFG_SCAN_RSP_DATA_RAW:
1500         btc_ble_set_scan_rsp_data_raw(arg->cfg_scan_rsp_data_raw.raw_scan_rsp,
1501                                       arg->cfg_scan_rsp_data_raw.raw_scan_rsp_len,
1502                                       btc_scan_rsp_data_raw_callback);
1503         break;
1504     case BTC_GAP_BLE_UPDATE_DUPLICATE_SCAN_EXCEPTIONAL_LIST:
1505         btc_ble_update_duplicate_exceptional_list(arg->update_duplicate_exceptional_list.subcode,
1506                                                 arg->update_duplicate_exceptional_list.info_type,
1507                                                 arg->update_duplicate_exceptional_list.device_info,
1508                                                 btc_update_duplicate_exceptional_list_callback);
1509         break;
1510 #endif // #if (BLE_42_FEATURE_SUPPORT == TRUE)
1511 #if (SMP_INCLUDED == TRUE)
1512     case BTC_GAP_BLE_SET_ENCRYPTION_EVT: {
1513         BD_ADDR bd_addr;
1514         memcpy(bd_addr, arg->set_encryption.bd_addr, sizeof(BD_ADDR));
1515         BTA_DmSetEncryption(bd_addr, BT_TRANSPORT_LE, btc_set_encryption_callback,
1516                                           (tBTA_DM_BLE_SEC_ACT)arg->set_encryption.sec_act);
1517         break;
1518     }
1519     case BTC_GAP_BLE_SET_SECURITY_PARAM_EVT: {
1520         uint8_t *value = arg->set_security_param.value;
1521         switch(arg->set_security_param.param_type) {
1522             case ESP_BLE_SM_PASSKEY:
1523                 break;
1524             case ESP_BLE_SM_AUTHEN_REQ_MODE: {
1525                 uint8_t authen_req = 0;
1526                 STREAM_TO_UINT8(authen_req, value);
1527                 bta_dm_co_ble_set_auth_req(authen_req);
1528                 break;
1529             }
1530             case ESP_BLE_SM_IOCAP_MODE: {
1531                 uint8_t iocap = 0;
1532                 STREAM_TO_UINT8(iocap, value);
1533                 bta_dm_co_ble_set_io_cap(iocap);
1534                 break;
1535             }
1536             case ESP_BLE_SM_SET_INIT_KEY: {
1537                 uint8_t init_key = 0;
1538                 STREAM_TO_UINT8(init_key, value);
1539                 bta_dm_co_ble_set_init_key_req(init_key);
1540                 break;
1541             }
1542             case ESP_BLE_SM_SET_RSP_KEY: {
1543                 uint8_t rsp_key = 0;
1544                 STREAM_TO_UINT8(rsp_key, value);
1545                 bta_dm_co_ble_set_rsp_key_req(rsp_key);
1546                 break;
1547             }
1548             case ESP_BLE_SM_MAX_KEY_SIZE: {
1549                 uint8_t key_size = 0;
1550                 STREAM_TO_UINT8(key_size, value);
1551                 bta_dm_co_ble_set_max_key_size(key_size);
1552                 break;
1553             }
1554             case ESP_BLE_SM_MIN_KEY_SIZE: {
1555                 uint8_t key_size = 0;
1556                 STREAM_TO_UINT8(key_size, value);
1557                 bta_dm_co_ble_set_min_key_size(key_size);
1558                 break;
1559             }
1560             case ESP_BLE_SM_SET_STATIC_PASSKEY: {
1561                 uint32_t passkey = 0;
1562                 for(uint8_t i = 0; i < arg->set_security_param.len; i++)
1563                 {
1564                     passkey += (((uint8_t *)value)[i]<<(8*i));
1565                 }
1566                 BTA_DmBleSetStaticPasskey(true, passkey);
1567                 break;
1568             }
1569             case ESP_BLE_SM_CLEAR_STATIC_PASSKEY: {
1570                 BTA_DmBleSetStaticPasskey(false, 0);
1571                 break;
1572             }
1573             case ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH: {
1574                 uint8_t enable = 0;
1575                 STREAM_TO_UINT8(enable, value);
1576                 bta_dm_co_ble_set_accept_auth_enable(enable);
1577                 break;
1578             }
1579             case ESP_BLE_SM_OOB_SUPPORT: {
1580                 uint8_t enable = 0;
1581                 STREAM_TO_UINT8(enable, value);
1582                 bta_dm_co_ble_oob_support(enable);
1583                 break;
1584             }
1585             case ESP_BLE_APP_ENC_KEY_SIZE: {
1586                 uint8_t key_size = 0;
1587                 STREAM_TO_UINT8(key_size, value);
1588                 bta_dm_co_ble_set_appl_enc_key_size(key_size);
1589                 break;
1590             }
1591             default:
1592                 break;
1593         }
1594         break;
1595     }
1596     case BTC_GAP_BLE_SECURITY_RSP_EVT: {
1597         BD_ADDR bd_addr;
1598         tBTA_DM_BLE_SEC_GRANT res = arg->sec_rsp.accept ? BTA_DM_SEC_GRANTED : BTA_DM_SEC_PAIR_NOT_SPT;
1599         memcpy(bd_addr, arg->sec_rsp.bd_addr, sizeof(BD_ADDR));
1600         BTA_DmBleSecurityGrant(bd_addr, res);
1601         break;
1602     }
1603     case BTC_GAP_BLE_PASSKEY_REPLY_EVT: {
1604         BD_ADDR bd_addr;
1605         memcpy(bd_addr, arg->enc_passkey_replay.bd_addr, sizeof(BD_ADDR));
1606         BTA_DmBlePasskeyReply(bd_addr, arg->enc_passkey_replay.accept, arg->enc_passkey_replay.passkey);
1607         break;
1608     }
1609     case BTC_GAP_BLE_CONFIRM_REPLY_EVT: {
1610         BD_ADDR bd_addr;
1611         memcpy(bd_addr, arg->enc_comfirm_replay.bd_addr, sizeof(BD_ADDR));
1612         BTA_DmBleConfirmReply(bd_addr, arg->enc_comfirm_replay.accept);
1613         break;
1614     }
1615     case BTC_GAP_BLE_REMOVE_BOND_DEV_EVT: {
1616         BD_ADDR bd_addr;
1617         memcpy(bd_addr, arg->remove_bond_device.bd_addr, sizeof(BD_ADDR));
1618         BTA_DmRemoveDevice(bd_addr, BT_TRANSPORT_LE);
1619         break;
1620     }
1621     case BTC_GAP_BLE_OOB_REQ_REPLY_EVT:
1622         BTA_DmOobReply(arg->oob_req_reply.bd_addr, arg->oob_req_reply.len, arg->oob_req_reply.p_value);
1623         break;
1624 #endif  ///SMP_INCLUDED == TRUE
1625     case BTC_GAP_BLE_DISCONNECT_EVT:
1626         btc_ble_disconnect(arg->disconnect.remote_device);
1627         break;
1628     case BTC_GAP_BLE_SET_AFH_CHANNELS:
1629         btc_gap_ble_set_channels(arg->set_channels.channels);
1630         break;
1631 #if (BLE_50_FEATURE_SUPPORT == TRUE)
1632     case BTC_GAP_BLE_READ_PHY:
1633         BTC_TRACE_DEBUG("BTC_GAP_BLE_READ_PHY");
1634         BTA_DmBleGapReadPHY(arg_5->read_phy.bd_addr);
1635         break;
1636     case BTC_GAP_BLE_SET_PREFERED_DEF_PHY:
1637         BTC_TRACE_DEBUG("BTC_GAP_BLE_SET_PREFERED_DEF_PHY");
1638         BTA_DmBleGapSetPreferedDefaultPHY(arg_5->set_perf_def_phy.tx_phy_mask,
1639                                           arg_5->set_perf_def_phy.rx_phy_mask);
1640         break;
1641     case BTC_GAP_BLE_SET_DEF_PHY:
1642         BTC_TRACE_DEBUG("BTC_GAP_BLE_SET_DEF_PHY");
1643         BTA_DmBleGapSetPreferedPHY(arg_5->set_def_phy.bd_addr,
1644                                    arg_5->set_def_phy.all_phys_mask,
1645                                    arg_5->set_def_phy.tx_phy_mask,
1646                                    arg_5->set_def_phy.rx_phy_mask,
1647                                    arg_5->set_def_phy.phy_options);
1648         break;
1649     case BTC_GAP_BLE_SET_EXT_ADV_RAND_ADDR:
1650         BTA_DmBleGapExtAdvSetRandaddr(arg_5->ext_adv_set_rand_addr.instance, arg_5->ext_adv_set_rand_addr.rand_addr);
1651         break;
1652     case BTC_GAP_BLE_SET_EXT_ADV_PARAMS: {
1653         tBTA_DM_BLE_GAP_EXT_ADV_PARAMS params = {0};
1654         params.type = arg_5->ext_adv_set_params.params.type;
1655         params.interval_min = arg_5->ext_adv_set_params.params.interval_min;
1656         params.interval_max = arg_5->ext_adv_set_params.params.interval_max;
1657         params.channel_map = arg_5->ext_adv_set_params.params.channel_map;
1658         params.own_addr_type = arg_5->ext_adv_set_params.params.own_addr_type;
1659         params.peer_addr_type = arg_5->ext_adv_set_params.params.peer_addr_type;
1660         params.filter_policy = arg_5->ext_adv_set_params.params.filter_policy;
1661         params.tx_power = arg_5->ext_adv_set_params.params.tx_power;
1662         params.primary_phy = arg_5->ext_adv_set_params.params.primary_phy;
1663         params.max_skip = arg_5->ext_adv_set_params.params.max_skip;
1664         params.secondary_phy = arg_5->ext_adv_set_params.params.secondary_phy;
1665         params.sid = arg_5->ext_adv_set_params.params.sid;
1666         params.scan_req_notif = arg_5->ext_adv_set_params.params.scan_req_notif;
1667 
1668         memcpy(params.peer_addr, arg_5->ext_adv_set_params.params.peer_addr, sizeof(BD_ADDR));
1669 
1670      	BTC_TRACE_DEBUG("BTC_GAP_BLE_SET_EXT_ADV_PARAMS");
1671         BTA_DmBleGapExtAdvSetParams(arg_5->ext_adv_set_params.instance,
1672                                    (const tBTA_DM_BLE_GAP_EXT_ADV_PARAMS *)&params);
1673         break;
1674 					 }
1675     case BTC_GAP_BLE_CFG_EXT_ADV_DATA_RAW:
1676         BTC_TRACE_DEBUG("BTC_GAP_BLE_CFG_EXT_ADV_DATA_RAW");
1677         BTA_DmBleGapConfigExtAdvDataRaw(FALSE, arg_5->ext_adv_cfg_data.instance,
1678                                        arg_5->ext_adv_cfg_data.length,
1679                                        (const UINT8 *)arg_5->ext_adv_cfg_data.data);
1680         break;
1681     case BTC_GAP_BLE_CFG_EXT_SCAN_RSP_DATA_RAW:
1682         BTC_TRACE_DEBUG("BTC_GAP_BLE_CFG_EXT_SCAN_RSP_DATA_RAW");
1683         BTA_DmBleGapConfigExtAdvDataRaw(TRUE, arg_5->ext_adv_cfg_data.instance,
1684                                        arg_5->ext_adv_cfg_data.length,
1685                                        (const UINT8 *)arg_5->ext_adv_cfg_data.data);
1686         break;
1687     case BTC_GAP_BLE_EXT_ADV_START: {
1688         BTC_TRACE_DEBUG("BTC_GAP_BLE_EXT_ADV_START");
1689 	    for (int k = 0; k < arg_5->ext_adv_start.num_adv; k++) {
1690 	       BTC_TRACE_DEBUG("adv_handle[%d] = %d, duration[%d] = %d, max_adv_evt[%d] = %d", k, arg_5->ext_adv_start.ext_adv[k].instance, k,
1691 			    arg_5->ext_adv_start.ext_adv[k].duration, k, arg_5->ext_adv_start.ext_adv[k].max_events);
1692 	    }
1693         BTA_DmBleGapExtAdvEnable(TRUE, arg_5->ext_adv_start.num_adv,
1694                                (tBTA_DM_BLE_EXT_ADV *)arg_5->ext_adv_start.ext_adv);
1695         break;
1696 	}
1697     case BTC_GAP_BLE_EXT_ADV_STOP: {
1698         BTC_TRACE_DEBUG("BTC_GAP_BLE_EXT_ADV_STOP");
1699         uint8_t num_adv = arg_5->ext_adv_stop.num_adv;
1700         if(num_adv > 0) {
1701             tBTA_DM_BLE_EXT_ADV *ext_adv = osi_malloc(num_adv * sizeof(esp_ble_gap_ext_adv_t));
1702             if(ext_adv) {
1703                 for (uint8_t i = 0; i < num_adv; i++) {
1704                     ext_adv[i].instance = arg_5->ext_adv_stop.ext_adv_inst[i];
1705                     ext_adv[i].duration = 0;
1706                     ext_adv[i].max_events = 0;
1707                 }
1708                 BTA_DmBleGapExtAdvEnable(FALSE, num_adv, ext_adv);
1709                 osi_free(ext_adv);
1710             } else {
1711                 BTC_TRACE_ERROR("%s no mem\n", __func__);
1712             }
1713         } else {
1714             BTA_DmBleGapExtAdvEnable(FALSE, 0, NULL);
1715         }
1716 	break;
1717     }
1718     case BTC_GAP_BLE_EXT_ADV_SET_REMOVE:
1719         BTA_DmBleGapExtAdvSetRemove(arg_5->ext_adv_set_remove.instance);
1720         break;
1721     case BTC_GAP_BLE_EXT_ADV_SET_CLEAR:
1722         BTA_DmBleGapExtAdvSetClear();
1723         break;
1724     case BTC_GAP_BLE_SET_PERIODIC_ADV_PARAMS: {
1725         tBTA_DM_BLE_Periodic_Adv_Params params = {0};
1726         params.interval_min = arg_5->peridic_adv_set_params.params.interval_min;
1727         params.interval_max = arg_5->peridic_adv_set_params.params.interval_max;
1728         params.properties = arg_5->peridic_adv_set_params.params.properties;
1729         BTC_TRACE_DEBUG("BTC_GAP_BLE_SET_PERIODIC_ADV_PARAMS");
1730         BTA_DmBleGapPeriodicAdvSetParams(arg_5->peridic_adv_set_params.instance,
1731                                          &params);
1732         break;
1733     }
1734     case BTC_GAP_BLE_CFG_PERIODIC_ADV_DATA_RAW:
1735         BTC_TRACE_DEBUG("BTC_GAP_BLE_CFG_PERIODIC_ADV_DATA_RAW");
1736         BTA_DmBleGapPeriodicAdvCfgDataRaw(arg_5->periodic_adv_cfg_data.instance,
1737                                           arg_5->periodic_adv_cfg_data.len,
1738                                           (const UINT8 *)arg_5->periodic_adv_cfg_data.data);
1739         break;
1740     case BTC_GAP_BLE_PERIODIC_ADV_START:
1741         BTC_TRACE_DEBUG("BTC_GAP_BLE_PERIODIC_ADV_START");
1742         BTA_DmBleGapPeriodicAdvEnable(TRUE, arg_5->periodic_adv_start.instance);
1743         break;
1744     case BTC_GAP_BLE_PERIODIC_ADV_STOP:
1745         BTC_TRACE_DEBUG("BTC_GAP_BLE_PERIODIC_ADV_STOP");
1746         BTA_DmBleGapPeriodicAdvEnable(FALSE, arg_5->periodic_adv_stop.instance);
1747         break;
1748     case BTC_GAP_BLE_PERIODIC_ADV_CREATE_SYNC: {
1749         tBTA_DM_BLE_Periodic_Sync_Params params = {0};
1750         params.filter_policy = arg_5->periodic_adv_create_sync.params.filter_policy;
1751         params.sid = arg_5->periodic_adv_create_sync.params.sid;
1752         params.addr_type = arg_5->periodic_adv_create_sync.params.addr_type;
1753         params.skip = arg_5->periodic_adv_create_sync.params.skip;
1754         params.sync_timeout = arg_5->periodic_adv_create_sync.params.sync_timeout;
1755 
1756         memcpy(params.addr, arg_5->periodic_adv_create_sync.params.addr, sizeof(BD_ADDR));
1757         BTC_TRACE_DEBUG("BTC_GAP_BLE_PERIODIC_ADV_CREATE_SYNC");
1758         BTA_DmBleGapPeriodicAdvCreateSync(&params);
1759         break;
1760     }
1761     case BTC_GAP_BLE_PERIODIC_ADV_SYNC_CANCEL:
1762         BTC_TRACE_DEBUG("BTC_GAP_BLE_PERIODIC_ADV_SYNC_CANCEL");
1763         BTA_DmBleGapPeriodicAdvSyncCancel();
1764         break;
1765     case BTC_GAP_BLE_PERIODIC_ADV_SYNC_TERMINATE:
1766         BTC_TRACE_DEBUG("BTC_GAP_BLE_PERIODIC_ADV_SYNC_TERMINATE");
1767         BTA_DmBleGapPeriodicAdvSyncTerm(arg_5->periodic_adv_sync_term.sync_handle);
1768         break;
1769     case BTC_GAP_BLE_PERIODIC_ADV_ADD_DEV_TO_LIST:
1770         BTC_TRACE_DEBUG("BTC_GAP_BLE_PERIODIC_ADV_ADD_DEV_TO_LIST");
1771         BTA_DmBleGapPeriodicAdvAddDevToList(arg_5->periodic_adv_add_dev.addr_type,
1772                                             arg_5->periodic_adv_add_dev.addr,
1773                                             arg_5->periodic_adv_add_dev.sid);
1774         break;
1775     case BTC_GAP_BLE_PERIODIC_REMOVE_ADD_DEV_FROM_LIST:
1776         BTC_TRACE_DEBUG("BTC_GAP_BLE_PERIODIC_REMOVE_ADD_DEV_FROM_LIST");
1777         BTA_DmBleGapPeriodicAdvRemoveDevFromList(arg_5->periodic_adv_remove_dev.addr_type,
1778                                             arg_5->periodic_adv_remove_dev.addr,
1779                                             arg_5->periodic_adv_remove_dev.sid);
1780         break;
1781     case BTC_GAP_BLE_PERIODIC_CLEAR_DEV:
1782         BTC_TRACE_DEBUG("BTC_GAP_BLE_PERIODIC_CLEAR_DEV");
1783         BTA_DmBleGapPeriodicAdvClearDev();
1784         break;
1785     case BTC_GAP_BLE_SET_EXT_SCAN_PARAMS: {
1786         tBTA_DM_BLE_EXT_SCAN_PARAMS params = {0};
1787         params.own_addr_type = arg_5->set_ext_scan_params.params.own_addr_type;
1788         params.filter_policy = arg_5->set_ext_scan_params.params.filter_policy;
1789         params.scan_duplicate = arg_5->set_ext_scan_params.params.scan_duplicate;
1790         params.cfg_mask = arg_5->set_ext_scan_params.params.cfg_mask;
1791         if (params.cfg_mask & BTA_DM_BLE_GAP_EXT_SCAN_UNCODE_MASK) {
1792             params.uncoded_cfg.scan_type = arg_5->set_ext_scan_params.params.uncoded_cfg.scan_type;
1793             params.uncoded_cfg.scan_interval = arg_5->set_ext_scan_params.params.uncoded_cfg.scan_interval;
1794             params.uncoded_cfg.scan_window = arg_5->set_ext_scan_params.params.uncoded_cfg.scan_window;
1795         }
1796 
1797         if (params.cfg_mask & BTA_DM_BLE_GAP_EXT_SCAN_CODE_MASK) {
1798             params.coded_cfg.scan_type = arg_5->set_ext_scan_params.params.coded_cfg.scan_type;
1799             params.coded_cfg.scan_interval = arg_5->set_ext_scan_params.params.coded_cfg.scan_interval;
1800             params.coded_cfg.scan_window = arg_5->set_ext_scan_params.params.coded_cfg.scan_window;
1801         }
1802 
1803         BTC_TRACE_DEBUG("BTC_GAP_BLE_SET_EXT_SCAN_PARAMS");
1804         BTA_DmBleGapSetExtScanParams(&params);
1805         break;
1806     }
1807     case BTC_GAP_BLE_START_EXT_SCAN:
1808         BTC_TRACE_DEBUG("BTC_GAP_BLE_START_EXT_SCAN");
1809         BTA_DmBleGapExtScan(TRUE, arg_5->start_ext_scan.duration, arg_5->start_ext_scan.period);
1810         break;
1811     case BTC_GAP_BLE_STOP_EXT_SCAN:
1812         BTC_TRACE_DEBUG("BTC_GAP_BLE_STOP_EXT_SCAN");
1813         BTA_DmBleGapExtScan(FALSE, 0, 0);
1814         break;
1815     case BTC_GAP_BLE_SET_EXT_PEFER_CONNET_PARAMS:
1816         BTC_TRACE_DEBUG("BTC_GAP_BLE_SET_EXT_PEFER_CONNET_PARAMS");
1817         BTA_DmBleGapPreferExtConnectParamsSet(arg_5->set_ext_conn_params.addr,
1818                                               arg_5->set_ext_conn_params.phy_mask,
1819                                               (const tBTA_DM_BLE_CONN_PARAMS *)&arg_5->set_ext_conn_params.phy_1m_conn_params,
1820                                               (const tBTA_DM_BLE_CONN_PARAMS *)&arg_5->set_ext_conn_params.phy_2m_conn_params,
1821                                               (const tBTA_DM_BLE_CONN_PARAMS *)&arg_5->set_ext_conn_params.phy_coded_conn_params);
1822         break;
1823 #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
1824     default:
1825         break;
1826     }
1827 
1828     btc_gap_ble_arg_deep_free(msg);
1829 }
1830 
1831 //register connection parameter update callback
btc_gap_callback_init(void)1832 void btc_gap_callback_init(void)
1833 {
1834     BTM_BleRegiseterConnParamCallback(btc_update_conn_param_callback);
1835 #if (BLE_50_FEATURE_SUPPORT == TRUE)
1836     BTM_BleGapRegisterCallback(btc_ble_5_gap_callback);
1837 #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
1838 }
1839 
btc_gap_ble_deinit(void)1840 void btc_gap_ble_deinit(void)
1841 {
1842  #if (BLE_42_FEATURE_SUPPORT == TRUE)
1843     btc_cleanup_adv_data(&gl_bta_adv_data);
1844     btc_cleanup_adv_data(&gl_bta_scan_rsp_data);
1845 #endif //  #if (BLE_42_FEATURE_SUPPORT == TRUE)
1846 }
1847 
1848 #if SCAN_QUEUE_CONGEST_CHECK
btc_adv_list_free(void * data)1849 void btc_adv_list_free(void *data)
1850 {
1851     osi_free(data);
1852 }
1853 
btc_adv_list_init(void)1854 void btc_adv_list_init(void)
1855 {
1856     osi_mutex_new(&adv_list_lock);
1857     adv_filter_list = list_new(btc_adv_list_free);
1858 }
1859 
btc_adv_list_deinit(void)1860 void btc_adv_list_deinit(void)
1861 {
1862     osi_mutex_free(&adv_list_lock);
1863     if(adv_filter_list) {
1864         list_free(adv_filter_list);
1865         adv_filter_list = NULL;
1866     }
1867 }
btc_adv_list_add_packet(void * data)1868 void btc_adv_list_add_packet(void * data)
1869 {
1870     if(!data) {
1871         BTC_TRACE_ERROR("%s data is NULL", __func__);
1872         return;
1873     }
1874     btc_adv_list_lock();
1875     list_prepend(adv_filter_list, data);
1876     btc_adv_list_unlock();
1877 }
1878 
btc_get_adv_list_length(void)1879 uint32_t btc_get_adv_list_length(void)
1880 {
1881     if(!adv_filter_list) {
1882         BTC_TRACE_ERROR("%s adv_filter_list is NULL", __func__);
1883         return 0;
1884     }
1885     btc_adv_list_lock();
1886     size_t length = list_length(adv_filter_list);
1887     btc_adv_list_unlock();
1888 
1889     return length;
1890 }
1891 
btc_adv_list_refresh(void)1892 void btc_adv_list_refresh(void)
1893 {
1894     if(!adv_filter_list) {
1895         BTC_TRACE_ERROR("%s adv_filter_list is NULL", __func__);
1896         return ;
1897     }
1898     btc_adv_list_lock();
1899     list_clear(adv_filter_list);
1900     btc_adv_list_unlock();
1901 }
1902 
btc_check_adv_list(uint8_t * addr,uint8_t addr_type)1903 bool btc_check_adv_list(uint8_t * addr, uint8_t addr_type)
1904 {
1905     bool found = false;
1906     if(!adv_filter_list || !addr) {
1907         BTC_TRACE_ERROR("%s adv_filter_list is NULL", __func__);
1908         return found;
1909     }
1910 
1911     btc_adv_list_lock();
1912     for (const list_node_t *node = list_begin(adv_filter_list); node != list_end(adv_filter_list); node = list_next(node)) {
1913         btc_adv_packet_t *packet = (btc_adv_packet_t *)list_node(node);
1914         if(!bdcmp(addr, packet->addr) && packet->addr_type == addr_type) {
1915             found = true;
1916             break;
1917         }
1918      }
1919      btc_adv_list_unlock();
1920      if(!found) {
1921          btc_adv_packet_t *adv_packet = osi_malloc(sizeof(btc_adv_packet_t));
1922          if(adv_packet) {
1923              adv_packet->addr_type = addr_type;
1924              bdcpy(adv_packet->addr, addr);
1925              btc_adv_list_add_packet(adv_packet);
1926          } else {
1927              BTC_TRACE_ERROR("%s adv_packet malloc failed", __func__);
1928          }
1929      }
1930     return found;
1931 }
1932 
btc_adv_list_lock(void)1933 void btc_adv_list_lock(void)
1934 {
1935     osi_mutex_lock(&adv_list_lock, OSI_MUTEX_MAX_TIMEOUT);
1936 }
1937 
btc_adv_list_unlock(void)1938 void btc_adv_list_unlock(void)
1939 {
1940     osi_mutex_unlock(&adv_list_lock);
1941 }
1942 #endif
1943 #endif  ///BLE_INCLUDED == TRUE
1944