mirror of
				https://github.com/espressif/esp-idf.git
				synced 2025-11-04 06:11:06 +00:00 
			
		
		
		
	component/bt: use the OSI utility "osi_event" to handle adv report in btc_layer
This commit is contained in:
		@@ -56,6 +56,10 @@
 | 
			
		||||
#endif /* #if CLASSIC_BT_INCLUDED */
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#if (BLE_INCLUDED == TRUE)
 | 
			
		||||
#include "btc_gap_ble.h"
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#if CONFIG_BLE_MESH
 | 
			
		||||
#include "btc_ble_mesh_ble.h"
 | 
			
		||||
#include "btc_ble_mesh_prov.h"
 | 
			
		||||
@@ -416,6 +420,7 @@ bt_status_t btc_init(void)
 | 
			
		||||
 | 
			
		||||
#if (BLE_INCLUDED == TRUE)
 | 
			
		||||
    btc_gap_callback_init();
 | 
			
		||||
    btc_gap_ble_init();
 | 
			
		||||
#endif  ///BLE_INCLUDED == TRUE
 | 
			
		||||
 | 
			
		||||
#if SCAN_QUEUE_CONGEST_CHECK
 | 
			
		||||
@@ -433,7 +438,9 @@ void btc_deinit(void)
 | 
			
		||||
 | 
			
		||||
    osi_thread_free(btc_thread);
 | 
			
		||||
    btc_thread = NULL;
 | 
			
		||||
 | 
			
		||||
#if (BLE_INCLUDED == TRUE)
 | 
			
		||||
    btc_gap_ble_deinit();
 | 
			
		||||
#endif  ///BLE_INCLUDED == TRUE
 | 
			
		||||
#if SCAN_QUEUE_CONGEST_CHECK
 | 
			
		||||
    btc_adv_list_deinit();
 | 
			
		||||
#endif
 | 
			
		||||
 
 | 
			
		||||
@@ -82,9 +82,6 @@ static void btc_deinit_bluetooth(void)
 | 
			
		||||
#if BTA_DYNAMIC_MEMORY
 | 
			
		||||
    xSemaphoreTake(deinit_semaphore, BTA_DISABLE_DELAY / portTICK_PERIOD_MS);
 | 
			
		||||
#endif /* #if BTA_DYNAMIC_MEMORY */
 | 
			
		||||
#if (BLE_INCLUDED == TRUE)
 | 
			
		||||
    btc_gap_ble_deinit();
 | 
			
		||||
#endif  ///BLE_INCLUDED == TRUE
 | 
			
		||||
    bta_dm_sm_deinit();
 | 
			
		||||
#if (GATTC_INCLUDED)
 | 
			
		||||
    bta_gattc_deinit();
 | 
			
		||||
 
 | 
			
		||||
@@ -21,6 +21,8 @@
 | 
			
		||||
#include "btc/btc_dm.h"
 | 
			
		||||
#include "btc/btc_util.h"
 | 
			
		||||
#include "osi/mutex.h"
 | 
			
		||||
#include "osi/thread.h"
 | 
			
		||||
#include "osi/pkt_queue.h"
 | 
			
		||||
#include "esp_bt.h"
 | 
			
		||||
 | 
			
		||||
#if (BLE_INCLUDED == TRUE)
 | 
			
		||||
@@ -48,6 +50,17 @@ static uint16_t btc_adv_list_count = 0;
 | 
			
		||||
#define  BTC_ADV_LIST_MAX_COUNT     200
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#define BTC_GAP_BLE_ADV_RPT_QUEUE_IDX            (1)
 | 
			
		||||
 | 
			
		||||
#if (BLE_42_FEATURE_SUPPORT == TRUE)
 | 
			
		||||
typedef struct {
 | 
			
		||||
    struct pkt_queue *adv_rpt_queue;
 | 
			
		||||
    struct osi_event *adv_rpt_ready;
 | 
			
		||||
} btc_gap_ble_env_t;
 | 
			
		||||
 | 
			
		||||
static btc_gap_ble_env_t btc_gap_ble_env;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
static inline void btc_gap_ble_cb_to_app(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
 | 
			
		||||
{
 | 
			
		||||
    esp_gap_ble_cb_t btc_gap_ble_cb = (esp_gap_ble_cb_t)btc_profile_cb_get(BTC_PID_GAP_BLE);
 | 
			
		||||
@@ -548,18 +561,27 @@ static void btc_ble_set_scan_params(esp_ble_scan_params_t *scan_params, tBLE_SCA
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void btc_search_callback(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
 | 
			
		||||
static void btc_gap_ble_adv_pkt_handler(void *arg)
 | 
			
		||||
{
 | 
			
		||||
    esp_ble_gap_cb_param_t param;
 | 
			
		||||
    btc_msg_t msg = {0};
 | 
			
		||||
    btc_gap_ble_env_t *p_env = &btc_gap_ble_env;
 | 
			
		||||
    size_t pkts_to_process = pkt_queue_length(p_env->adv_rpt_queue);
 | 
			
		||||
 | 
			
		||||
    msg.sig = BTC_SIG_API_CB;
 | 
			
		||||
    msg.pid = BTC_PID_GAP_BLE;
 | 
			
		||||
    msg.act = ESP_GAP_BLE_SCAN_RESULT_EVT;
 | 
			
		||||
    for (size_t i = 0; i < pkts_to_process; i++) {
 | 
			
		||||
        pkt_linked_item_t *linked_pkt = pkt_queue_dequeue(p_env->adv_rpt_queue);
 | 
			
		||||
        if (linked_pkt != NULL) {
 | 
			
		||||
            esp_ble_gap_cb_param_t *param = (esp_ble_gap_cb_param_t *)(linked_pkt->data);
 | 
			
		||||
            btc_gap_ble_cb_to_app(ESP_GAP_BLE_SCAN_RESULT_EVT, param);
 | 
			
		||||
            osi_free(linked_pkt);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    param.scan_rst.search_evt = event;
 | 
			
		||||
    switch (event) {
 | 
			
		||||
    case BTA_DM_INQ_RES_EVT: {
 | 
			
		||||
    if (pkt_queue_length(p_env->adv_rpt_queue) != 0) {
 | 
			
		||||
        osi_thread_post_event(p_env->adv_rpt_ready, OSI_THREAD_MAX_TIMEOUT);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void btc_process_adv_rpt_pkt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
 | 
			
		||||
{
 | 
			
		||||
#if SCAN_QUEUE_CONGEST_CHECK
 | 
			
		||||
    if(btc_check_queue_is_congest()) {
 | 
			
		||||
        BTC_TRACE_DEBUG("BtcQueue is congested");
 | 
			
		||||
@@ -573,18 +595,51 @@ static void btc_search_callback(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data
 | 
			
		||||
    }
 | 
			
		||||
    btc_adv_list_count ++;
 | 
			
		||||
#endif
 | 
			
		||||
        bdcpy(param.scan_rst.bda, p_data->inq_res.bd_addr);
 | 
			
		||||
        param.scan_rst.dev_type = p_data->inq_res.device_type;
 | 
			
		||||
        param.scan_rst.rssi = p_data->inq_res.rssi;
 | 
			
		||||
        param.scan_rst.ble_addr_type = p_data->inq_res.ble_addr_type;
 | 
			
		||||
        param.scan_rst.ble_evt_type = p_data->inq_res.ble_evt_type;
 | 
			
		||||
        param.scan_rst.flag = p_data->inq_res.flag;
 | 
			
		||||
        param.scan_rst.num_resps = 1;
 | 
			
		||||
        param.scan_rst.adv_data_len = p_data->inq_res.adv_data_len;
 | 
			
		||||
        param.scan_rst.scan_rsp_len = p_data->inq_res.scan_rsp_len;
 | 
			
		||||
        memcpy(param.scan_rst.ble_adv, p_data->inq_res.p_eir, sizeof(param.scan_rst.ble_adv));
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    pkt_linked_item_t *linked_pkt = osi_calloc(BT_PKT_LINKED_HDR_SIZE + sizeof(esp_ble_gap_cb_param_t));
 | 
			
		||||
    if (linked_pkt == NULL) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    struct ble_scan_result_evt_param *scan_rst = (struct ble_scan_result_evt_param *)linked_pkt->data;
 | 
			
		||||
 | 
			
		||||
    do {
 | 
			
		||||
        scan_rst->search_evt = event;
 | 
			
		||||
        bdcpy(scan_rst->bda, p_data->inq_res.bd_addr);
 | 
			
		||||
        scan_rst->dev_type = p_data->inq_res.device_type;
 | 
			
		||||
        scan_rst->rssi = p_data->inq_res.rssi;
 | 
			
		||||
        scan_rst->ble_addr_type = p_data->inq_res.ble_addr_type;
 | 
			
		||||
        scan_rst->ble_evt_type = p_data->inq_res.ble_evt_type;
 | 
			
		||||
        scan_rst->flag = p_data->inq_res.flag;
 | 
			
		||||
        scan_rst->num_resps = 1;
 | 
			
		||||
        scan_rst->adv_data_len = p_data->inq_res.adv_data_len;
 | 
			
		||||
        scan_rst->scan_rsp_len = p_data->inq_res.scan_rsp_len;
 | 
			
		||||
        memcpy(scan_rst->ble_adv, p_data->inq_res.p_eir, sizeof(scan_rst->ble_adv));
 | 
			
		||||
    } while (0);
 | 
			
		||||
 | 
			
		||||
    btc_gap_ble_env_t *p_env = &btc_gap_ble_env;
 | 
			
		||||
    pkt_queue_enqueue(p_env->adv_rpt_queue, linked_pkt);
 | 
			
		||||
    osi_thread_post_event(p_env->adv_rpt_ready, OSI_THREAD_MAX_TIMEOUT);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void btc_search_callback(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
 | 
			
		||||
{
 | 
			
		||||
    if (event == BTA_DM_INQ_RES_EVT) {
 | 
			
		||||
        btc_process_adv_rpt_pkt(event, p_data);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    esp_ble_gap_cb_param_t param;
 | 
			
		||||
    btc_msg_t msg = {0};
 | 
			
		||||
 | 
			
		||||
    msg.sig = BTC_SIG_API_CB;
 | 
			
		||||
    msg.pid = BTC_PID_GAP_BLE;
 | 
			
		||||
    msg.act = ESP_GAP_BLE_SCAN_RESULT_EVT;
 | 
			
		||||
 | 
			
		||||
    param.scan_rst.search_evt = event;
 | 
			
		||||
    switch (event) {
 | 
			
		||||
    case BTA_DM_INQ_RES_EVT:
 | 
			
		||||
        break;
 | 
			
		||||
    case BTA_DM_INQ_CMPL_EVT: {
 | 
			
		||||
        param.scan_rst.num_resps = p_data->inq_cmpl.num_resps;
 | 
			
		||||
        BTC_TRACE_DEBUG("%s  BLE observe complete. Num Resp %d\n", __FUNCTION__, p_data->inq_cmpl.num_resps);
 | 
			
		||||
@@ -1841,9 +1896,31 @@ void btc_gap_callback_init(void)
 | 
			
		||||
#endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool btc_gap_ble_init(void)
 | 
			
		||||
{
 | 
			
		||||
#if (BLE_42_FEATURE_SUPPORT == TRUE)
 | 
			
		||||
    btc_gap_ble_env_t *p_env = &btc_gap_ble_env;
 | 
			
		||||
    p_env->adv_rpt_queue = pkt_queue_create();
 | 
			
		||||
    assert(p_env->adv_rpt_queue != NULL);
 | 
			
		||||
 | 
			
		||||
    p_env->adv_rpt_ready = osi_event_create(btc_gap_ble_adv_pkt_handler, NULL);
 | 
			
		||||
    assert(p_env->adv_rpt_ready != NULL);
 | 
			
		||||
    osi_event_bind(p_env->adv_rpt_ready, btc_get_current_thread(), BTC_GAP_BLE_ADV_RPT_QUEUE_IDX);
 | 
			
		||||
#endif
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void btc_gap_ble_deinit(void)
 | 
			
		||||
{
 | 
			
		||||
#if (BLE_42_FEATURE_SUPPORT == TRUE)
 | 
			
		||||
    btc_gap_ble_env_t *p_env = &btc_gap_ble_env;
 | 
			
		||||
 | 
			
		||||
    osi_event_delete(p_env->adv_rpt_ready);
 | 
			
		||||
    p_env->adv_rpt_ready = NULL;
 | 
			
		||||
 | 
			
		||||
    pkt_queue_destroy(p_env->adv_rpt_queue, NULL);
 | 
			
		||||
    p_env->adv_rpt_queue = NULL;
 | 
			
		||||
 | 
			
		||||
    btc_cleanup_adv_data(&gl_bta_adv_data);
 | 
			
		||||
    btc_cleanup_adv_data(&gl_bta_scan_rsp_data);
 | 
			
		||||
#endif //  #if (BLE_42_FEATURE_SUPPORT == TRUE)
 | 
			
		||||
 
 | 
			
		||||
@@ -353,6 +353,7 @@ void btc_gap_ble_arg_deep_free(btc_msg_t *msg);
 | 
			
		||||
void btc_gap_ble_cb_deep_free(btc_msg_t *msg);
 | 
			
		||||
void btc_gap_ble_cb_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src);
 | 
			
		||||
void btc_gap_callback_init(void);
 | 
			
		||||
bool btc_gap_ble_init(void);
 | 
			
		||||
void btc_gap_ble_deinit(void);
 | 
			
		||||
void btc_adv_list_init(void);
 | 
			
		||||
void btc_adv_list_deinit(void);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user