From d1553311a2c4e59abefccb4690881755f646e080 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 26 Oct 2020 14:23:35 +0800 Subject: [PATCH 1/5] ble_mesh: stack: Optimize handling received mesh adv packets --- .../bluedroid_host/mesh_bearer_adapt.c | 20 +++++-------------- .../mesh_core/nimble_host/mesh_bearer_adapt.c | 16 ++++++--------- 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c b/components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c index dc1c19b139..b2f0b6aad2 100644 --- a/components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c +++ b/components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c @@ -284,31 +284,21 @@ static int start_le_scan(uint8_t scan_type, uint16_t interval, uint16_t window, static void bt_mesh_scan_result_callback(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data) { + struct net_buf_simple buf = {0}; bt_mesh_addr_t addr = {0}; - uint8_t adv_type = 0U; - int8_t rssi = 0; BT_DBG("%s, event %d", __func__, event); if (event == BTA_DM_INQ_RES_EVT) { - /* TODO: How to process scan response here? */ + /* TODO: How to process scan response here? PS: p_data->inq_res.scan_rsp_len */ addr.type = p_data->inq_res.ble_addr_type; memcpy(addr.val, p_data->inq_res.bd_addr, BLE_MESH_ADDR_LEN); - rssi = p_data->inq_res.rssi; - adv_type = p_data->inq_res.ble_evt_type; - /* scan rsp len: p_data->inq_res.scan_rsp_len */ - struct net_buf_simple *buf = bt_mesh_alloc_buf(p_data->inq_res.adv_data_len); - if (!buf) { - BT_ERR("%s, Out of memory", __func__); - return; - } - net_buf_simple_add_mem(buf, p_data->inq_res.p_eir, p_data->inq_res.adv_data_len); + net_buf_simple_init_with_data(&buf, p_data->inq_res.p_eir, p_data->inq_res.adv_data_len); - if (bt_mesh_scan_dev_found_cb != NULL) { - bt_mesh_scan_dev_found_cb(&addr, rssi, adv_type, buf); + if (bt_mesh_scan_dev_found_cb) { + bt_mesh_scan_dev_found_cb(&addr, p_data->inq_res.rssi, p_data->inq_res.ble_evt_type, &buf); } - bt_mesh_free(buf); } else if (event == BTA_DM_INQ_CMPL_EVT) { BT_INFO("Scan completed, number of scan response %d", p_data->inq_cmpl.num_resps); } else { diff --git a/components/bt/esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c b/components/bt/esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c index 80389ce792..56b761e41f 100644 --- a/components/bt/esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c +++ b/components/bt/esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c @@ -387,21 +387,17 @@ static int disc_cb(struct ble_gap_event *event, void *arg) #endif switch (event->type) { - case BLE_GAP_EVENT_DISC: - desc = &event->disc; + case BLE_GAP_EVENT_DISC: { + struct net_buf_simple buf = {0}; - struct net_buf_simple *buf = bt_mesh_alloc_buf(desc->length_data); - if (!buf) { - BT_ERR("%s, Out of memory", __func__); - return 0; - } - net_buf_simple_add_mem(buf, desc->data, desc->length_data); + desc = &event->disc; + net_buf_simple_init_with_data(&buf, desc->data, desc->length_data); if (bt_mesh_scan_dev_found_cb) { - bt_mesh_scan_dev_found_cb((bt_mesh_addr_t *)&desc->addr, desc->rssi, desc->event_type, buf); + bt_mesh_scan_dev_found_cb((bt_mesh_addr_t *)&desc->addr, desc->rssi, desc->event_type, &buf); } - bt_mesh_free(buf); break; + } #if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ CONFIG_BLE_MESH_GATT_PROXY_CLIENT case BLE_GAP_EVENT_CONNECT: From 88b550ba7548d994e7b8e5cfb9f5ee3231dd6b0d Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 26 Oct 2020 15:26:45 +0800 Subject: [PATCH 2/5] ble_mesh: stack: Move ble scan funcitons to a single file --- components/bt/CMakeLists.txt | 1 + components/bt/esp_ble_mesh/mesh_core/adv.c | 280 +--------------- components/bt/esp_ble_mesh/mesh_core/adv.h | 11 - components/bt/esp_ble_mesh/mesh_core/lpn.c | 1 + components/bt/esp_ble_mesh/mesh_core/main.c | 1 + components/bt/esp_ble_mesh/mesh_core/net.c | 1 + .../esp_ble_mesh/mesh_core/provisioner_prov.c | 1 + components/bt/esp_ble_mesh/mesh_core/scan.c | 305 ++++++++++++++++++ components/bt/esp_ble_mesh/mesh_core/scan.h | 31 ++ components/bt/esp_ble_mesh/mesh_core/test.c | 1 + 10 files changed, 343 insertions(+), 290 deletions(-) create mode 100644 components/bt/esp_ble_mesh/mesh_core/scan.c create mode 100644 components/bt/esp_ble_mesh/mesh_core/scan.h diff --git a/components/bt/CMakeLists.txt b/components/bt/CMakeLists.txt index bad2fd67e8..f6e4bf0790 100644 --- a/components/bt/CMakeLists.txt +++ b/components/bt/CMakeLists.txt @@ -375,6 +375,7 @@ if(CONFIG_BT_ENABLED) "esp_ble_mesh/mesh_core/proxy_server.c" "esp_ble_mesh/mesh_core/settings_uid.c" "esp_ble_mesh/mesh_core/settings.c" + "esp_ble_mesh/mesh_core/scan.c" "esp_ble_mesh/mesh_core/test.c" "esp_ble_mesh/mesh_core/transport.c" "esp_ble_mesh/mesh_models/common/device_property.c" diff --git a/components/bt/esp_ble_mesh/mesh_core/adv.c b/components/bt/esp_ble_mesh/mesh_core/adv.c index 04a441d592..ac72ec09c7 100644 --- a/components/bt/esp_ble_mesh/mesh_core/adv.c +++ b/components/bt/esp_ble_mesh/mesh_core/adv.c @@ -29,18 +29,12 @@ /* Convert from 0.625ms units to interval(ms) */ #define ADV_SCAN_INT(val) ((val) * 5 / 8) -/* Window and Interval are equal for continuous scanning */ -#define MESH_SCAN_INTERVAL 0x20 -#define MESH_SCAN_WINDOW 0x20 - /* Pre-5.0 controllers enforce a minimum interval of 100ms * whereas 5.0+ controllers can go down to 20ms. */ #define ADV_INT_DEFAULT_MS 100 #define ADV_INT_FAST_MS 20 -static const bt_mesh_addr_t *dev_addr; - static const uint8_t adv_type[] = { [BLE_MESH_ADV_PROV] = BLE_MESH_DATA_MESH_PROV, [BLE_MESH_ADV_DATA] = BLE_MESH_DATA_MESH_MESSAGE, @@ -595,207 +589,6 @@ uint16_t bt_mesh_get_stored_relay_count(void) } #endif /* #if defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) */ -const bt_mesh_addr_t *bt_mesh_get_unprov_dev_addr(void) -{ - return dev_addr; -} - -#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ - CONFIG_BLE_MESH_GATT_PROXY_CLIENT -static bool adv_flags_valid(struct net_buf_simple *buf) -{ - uint8_t flags = 0U; - - if (buf->len != 1U) { - BT_DBG("Unexpected adv flags length %d", buf->len); - return false; - } - - flags = net_buf_simple_pull_u8(buf); - - BT_DBG("Received adv pkt with flags: 0x%02x", flags); - - /* Flags context will not be checked currently */ - ((void) flags); - - return true; -} - -static bool adv_service_uuid_valid(struct net_buf_simple *buf, uint16_t *uuid) -{ - if (buf->len != 2U) { - BT_DBG("Length not match mesh service uuid"); - return false; - } - - *uuid = net_buf_simple_pull_le16(buf); - - BT_DBG("Received adv pkt with service UUID: %d", *uuid); - - if (*uuid != BLE_MESH_UUID_MESH_PROV_VAL && - *uuid != BLE_MESH_UUID_MESH_PROXY_VAL) { - return false; - } - - if (*uuid == BLE_MESH_UUID_MESH_PROV_VAL && - bt_mesh_is_provisioner_en() == false) { - return false; - } - - if (*uuid == BLE_MESH_UUID_MESH_PROXY_VAL && - !IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_CLIENT)) { - return false; - } - - return true; -} - -#define BLE_MESH_PROV_SRV_DATA_LEN 0x12 -#define BLE_MESH_PROXY_SRV_DATA_LEN1 0x09 -#define BLE_MESH_PROXY_SRV_DATA_LEN2 0x11 - -static void handle_adv_service_data(struct net_buf_simple *buf, - const bt_mesh_addr_t *addr, - uint16_t uuid, int8_t rssi) -{ - uint16_t type = 0U; - - if (!buf || !addr) { - BT_ERR("%s, Invalid parameter", __func__); - return; - } - - type = net_buf_simple_pull_le16(buf); - if (type != uuid) { - BT_DBG("Invalid Mesh Service Data UUID 0x%04x", type); - return; - } - - switch (type) { -#if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT - case BLE_MESH_UUID_MESH_PROV_VAL: - if (bt_mesh_is_provisioner_en()) { - if (buf->len != BLE_MESH_PROV_SRV_DATA_LEN) { - BT_WARN("Invalid Mesh Prov Service Data length %d", buf->len); - return; - } - - BT_DBG("Start to handle Mesh Prov Service Data"); - bt_mesh_provisioner_prov_adv_recv(buf, addr, rssi); - } - break; -#endif -#if CONFIG_BLE_MESH_GATT_PROXY_CLIENT - case BLE_MESH_UUID_MESH_PROXY_VAL: - if (buf->len != BLE_MESH_PROXY_SRV_DATA_LEN1 && - buf->len != BLE_MESH_PROXY_SRV_DATA_LEN2) { - BT_WARN("Invalid Mesh Proxy Service Data length %d", buf->len); - return; - } - - BT_DBG("Start to handle Mesh Proxy Service Data"); - bt_mesh_proxy_client_gatt_adv_recv(buf, addr, rssi); - break; -#endif - default: - break; - } -} -#endif - -static void bt_mesh_scan_cb(const bt_mesh_addr_t *addr, int8_t rssi, - uint8_t adv_type, struct net_buf_simple *buf) -{ -#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ - CONFIG_BLE_MESH_GATT_PROXY_CLIENT - uint16_t uuid = 0U; -#endif - - if (adv_type != BLE_MESH_ADV_NONCONN_IND && adv_type != BLE_MESH_ADV_IND) { - return; - } - - BT_DBG("scan, len %u: %s", buf->len, bt_hex(buf->data, buf->len)); - - dev_addr = addr; - - while (buf->len > 1) { - struct net_buf_simple_state state; - uint8_t len, type; - - len = net_buf_simple_pull_u8(buf); - /* Check for early termination */ - if (len == 0U) { - return; - } - - if (len > buf->len) { - BT_WARN("AD malformed"); - return; - } - - net_buf_simple_save(buf, &state); - - type = net_buf_simple_pull_u8(buf); - - buf->len = len - 1; - -#if 0 - /* TODO: Check with BLE Mesh BQB test cases */ - if ((type == BLE_MESH_DATA_MESH_PROV || type == BLE_MESH_DATA_MESH_MESSAGE || - type == BLE_MESH_DATA_MESH_BEACON) && (adv_type != BLE_MESH_ADV_NONCONN_IND)) { - BT_DBG("%s, ignore BLE Mesh packet (type 0x%02x) with adv_type 0x%02x", - __func__, type, adv_type); - return; - } -#endif - - switch (type) { - case BLE_MESH_DATA_MESH_MESSAGE: - bt_mesh_net_recv(buf, rssi, BLE_MESH_NET_IF_ADV); - break; -#if CONFIG_BLE_MESH_PB_ADV - case BLE_MESH_DATA_MESH_PROV: - if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node()) { - bt_mesh_pb_adv_recv(buf); - } - if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en()) { - bt_mesh_provisioner_pb_adv_recv(buf); - } - break; -#endif /* CONFIG_BLE_MESH_PB_ADV */ - case BLE_MESH_DATA_MESH_BEACON: - bt_mesh_beacon_recv(buf, rssi); - break; -#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ - CONFIG_BLE_MESH_GATT_PROXY_CLIENT - case BLE_MESH_DATA_FLAGS: - if (!adv_flags_valid(buf)) { - BT_DBG("Adv Flags mismatch, ignore this adv pkt"); - return; - } - break; - case BLE_MESH_DATA_UUID16_ALL: - if (!adv_service_uuid_valid(buf, &uuid)) { - BT_DBG("Adv Service UUID mismatch, ignore this adv pkt"); - return; - } - break; - case BLE_MESH_DATA_SVC_DATA16: - handle_adv_service_data(buf, addr, uuid, rssi); - break; -#endif - default: - break; - } - - net_buf_simple_restore(buf, &state); - net_buf_simple_pull(buf, len); - } - - return; -} - void bt_mesh_adv_init(void) { #if !CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC @@ -918,77 +711,6 @@ void bt_mesh_adv_deinit(void) } #endif /* CONFIG_BLE_MESH_DEINIT */ -int bt_mesh_scan_enable(void) -{ - int err = 0; - - struct bt_mesh_scan_param scan_param = { - .type = BLE_MESH_SCAN_PASSIVE, -#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) - .filter_dup = BLE_MESH_SCAN_FILTER_DUP_ENABLE, -#else - .filter_dup = BLE_MESH_SCAN_FILTER_DUP_DISABLE, -#endif - .interval = MESH_SCAN_INTERVAL, - .window = MESH_SCAN_WINDOW, - .scan_fil_policy = BLE_MESH_SP_ADV_ALL, - }; - - BT_DBG("%s", __func__); - - err = bt_le_scan_start(&scan_param, bt_mesh_scan_cb); - if (err && err != -EALREADY) { - BT_ERR("starting scan failed (err %d)", err); - return err; - } - - return 0; -} - -int bt_mesh_scan_disable(void) -{ - int err = 0; - - BT_DBG("%s", __func__); - - err = bt_le_scan_stop(); - if (err && err != -EALREADY) { - BT_ERR("stopping scan failed (err %d)", err); - return err; - } - - return 0; -} - -#if CONFIG_BLE_MESH_TEST_USE_WHITE_LIST -int bt_mesh_scan_with_wl_enable(void) -{ - int err = 0; - - struct bt_mesh_scan_param scan_param = { - .type = BLE_MESH_SCAN_PASSIVE, -#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) - .filter_dup = BLE_MESH_SCAN_FILTER_DUP_ENABLE, -#else - .filter_dup = BLE_MESH_SCAN_FILTER_DUP_DISABLE, -#endif - .interval = MESH_SCAN_INTERVAL, - .window = MESH_SCAN_WINDOW, - .scan_fil_policy = BLE_MESH_SP_ADV_WL, - }; - - BT_DBG("%s", __func__); - - err = bt_le_scan_start(&scan_param, bt_mesh_scan_cb); - if (err && err != -EALREADY) { - BT_ERR("starting scan failed (err %d)", err); - return err; - } - - return 0; -} -#endif /* CONFIG_BLE_MESH_TEST_USE_WHITE_LIST */ - #if CONFIG_BLE_MESH_SUPPORT_BLE_ADV static struct bt_mesh_adv *ble_adv_alloc(int id) { @@ -1159,7 +881,7 @@ int bt_mesh_start_ble_advertising(const struct bt_mesh_ble_adv_param *param, buf = bt_mesh_ble_adv_create(BLE_MESH_ADV_BLE, 0U, K_NO_WAIT); if (!buf) { - BT_ERR("Unable to allocate buffer"); + BT_ERR("No empty ble adv buffer"); return -ENOBUFS; } diff --git a/components/bt/esp_ble_mesh/mesh_core/adv.h b/components/bt/esp_ble_mesh/mesh_core/adv.h index c33e3e1e41..d58e69b71a 100644 --- a/components/bt/esp_ble_mesh/mesh_core/adv.h +++ b/components/bt/esp_ble_mesh/mesh_core/adv.h @@ -41,9 +41,6 @@ enum bt_mesh_adv_type { BLE_MESH_ADV_BLE, }; -typedef void (*bt_mesh_adv_func_t)(struct net_buf *buf, uint16_t duration, - int err, void *user_data); - struct bt_mesh_adv { const struct bt_mesh_send_cb *cb; void *cb_data; @@ -78,8 +75,6 @@ void bt_mesh_unref_buf_from_pool(struct net_buf_pool *pool); void bt_mesh_adv_send(struct net_buf *buf, const struct bt_mesh_send_cb *cb, void *cb_data); -const bt_mesh_addr_t *bt_mesh_get_unprov_dev_addr(void); - struct net_buf *bt_mesh_relay_adv_create(enum bt_mesh_adv_type type, uint8_t xmit, int32_t timeout); @@ -93,12 +88,6 @@ void bt_mesh_adv_update(void); void bt_mesh_adv_init(void); void bt_mesh_adv_deinit(void); -int bt_mesh_scan_enable(void); - -int bt_mesh_scan_disable(void); - -int bt_mesh_scan_with_wl_enable(void); - #if CONFIG_BLE_MESH_SUPPORT_BLE_ADV int bt_mesh_start_ble_advertising(const struct bt_mesh_ble_adv_param *param, const struct bt_mesh_ble_adv_data *data, uint8_t *index); diff --git a/components/bt/esp_ble_mesh/mesh_core/lpn.c b/components/bt/esp_ble_mesh/mesh_core/lpn.c index eedc8922c1..0ac4fc6451 100644 --- a/components/bt/esp_ble_mesh/mesh_core/lpn.c +++ b/components/bt/esp_ble_mesh/mesh_core/lpn.c @@ -11,6 +11,7 @@ #include "crypto.h" #include "adv.h" +#include "scan.h" #include "mesh.h" #include "transport.h" #include "access.h" diff --git a/components/bt/esp_ble_mesh/mesh_core/main.c b/components/bt/esp_ble_mesh/mesh_core/main.c index ab279119fa..ed51fdfab1 100644 --- a/components/bt/esp_ble_mesh/mesh_core/main.c +++ b/components/bt/esp_ble_mesh/mesh_core/main.c @@ -11,6 +11,7 @@ #include #include "adv.h" +#include "scan.h" #include "prov.h" #include "beacon.h" #include "lpn.h" diff --git a/components/bt/esp_ble_mesh/mesh_core/net.c b/components/bt/esp_ble_mesh/mesh_core/net.c index 74e260c41d..78a809c85b 100644 --- a/components/bt/esp_ble_mesh/mesh_core/net.c +++ b/components/bt/esp_ble_mesh/mesh_core/net.c @@ -13,6 +13,7 @@ #include "crypto.h" #include "adv.h" +#include "scan.h" #include "mesh.h" #include "lpn.h" #include "friend.h" diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c b/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c index 479ebfeef5..94b086e16c 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c @@ -17,6 +17,7 @@ #include "crypto.h" #include "adv.h" +#include "scan.h" #include "mesh.h" #include "access.h" #include "settings.h" diff --git a/components/bt/esp_ble_mesh/mesh_core/scan.c b/components/bt/esp_ble_mesh/mesh_core/scan.c new file mode 100644 index 0000000000..52359cfb30 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/scan.c @@ -0,0 +1,305 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2020 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +#include "mesh_config.h" +#include "mesh_trace.h" +#include "mesh_buf.h" +#include "mesh_uuid.h" +#include "beacon.h" +#include "net.h" +#include "prov.h" +#include "proxy_client.h" +#include "proxy_server.h" +#include "provisioner_prov.h" +#include "mesh_bearer_adapt.h" + +/* Scan Window and Interval are equal for continuous scanning */ +#define SCAN_INTERVAL 0x20 +#define SCAN_WINDOW 0x20 + +#define PROV_SVC_DATA_LEN 0x12 +#define PROXY_SVC_DATA_LEN_NET_ID 0x09 +#define PROXY_SVC_DATA_LEN_NODE_ID 0x11 + +#if CONFIG_BLE_MESH_PROVISIONER +static const bt_mesh_addr_t *unprov_dev_addr; + +const bt_mesh_addr_t *bt_mesh_get_unprov_dev_addr(void) +{ + return unprov_dev_addr; +} +#endif /* CONFIG_BLE_MESH_PROVISIONER */ + +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT +static bool adv_flags_valid(struct net_buf_simple *buf) +{ + uint8_t flags = 0U; + + if (buf->len != 1U) { + BT_DBG("Unexpected adv flags length %d", buf->len); + return false; + } + + flags = net_buf_simple_pull_u8(buf); + + BT_DBG("Received adv pkt with flags: 0x%02x", flags); + + /* Flags context will not be checked currently */ + ARG_UNUSED(flags); + + return true; +} + +static bool adv_service_uuid_valid(struct net_buf_simple *buf, uint16_t *uuid) +{ + if (buf->len != 2U) { + BT_DBG("Length not match mesh service uuid"); + return false; + } + + *uuid = net_buf_simple_pull_le16(buf); + + BT_DBG("Received adv pkt with service UUID: %d", *uuid); + + if (*uuid != BLE_MESH_UUID_MESH_PROV_VAL && + *uuid != BLE_MESH_UUID_MESH_PROXY_VAL) { + return false; + } + + if (*uuid == BLE_MESH_UUID_MESH_PROV_VAL && + bt_mesh_is_provisioner_en() == false) { + return false; + } + + if (*uuid == BLE_MESH_UUID_MESH_PROXY_VAL && + !IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_CLIENT)) { + return false; + } + + return true; +} + +static void handle_adv_service_data(struct net_buf_simple *buf, + const bt_mesh_addr_t *addr, + uint16_t uuid, int8_t rssi) +{ + uint16_t type = 0U; + + if (!buf || !addr) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + type = net_buf_simple_pull_le16(buf); + if (type != uuid) { + BT_DBG("Invalid Mesh Service Data UUID 0x%04x", type); + return; + } + + switch (type) { +#if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT + case BLE_MESH_UUID_MESH_PROV_VAL: + if (bt_mesh_is_provisioner_en()) { + if (buf->len != PROV_SVC_DATA_LEN) { + BT_WARN("Invalid Mesh Prov Service Data length %d", buf->len); + return; + } + + BT_DBG("Start to handle Mesh Prov Service Data"); + bt_mesh_provisioner_prov_adv_recv(buf, addr, rssi); + } + break; +#endif +#if CONFIG_BLE_MESH_GATT_PROXY_CLIENT + case BLE_MESH_UUID_MESH_PROXY_VAL: + if (buf->len != PROXY_SVC_DATA_LEN_NET_ID && + buf->len != PROXY_SVC_DATA_LEN_NODE_ID) { + BT_WARN("Invalid Mesh Proxy Service Data length %d", buf->len); + return; + } + + BT_DBG("Start to handle Mesh Proxy Service Data"); + bt_mesh_proxy_client_gatt_adv_recv(buf, addr, rssi); + break; +#endif + default: + break; + } +} +#endif /* (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ + +static void bt_mesh_scan_cb(const bt_mesh_addr_t *addr, + int8_t rssi, uint8_t adv_type, + struct net_buf_simple *buf) +{ +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT + uint16_t uuid = 0U; +#endif + + if (adv_type != BLE_MESH_ADV_NONCONN_IND && adv_type != BLE_MESH_ADV_IND) { + return; + } + + BT_DBG("scan, len %u: %s", buf->len, bt_hex(buf->data, buf->len)); + +#if CONFIG_BLE_MESH_PROVISIONER + unprov_dev_addr = addr; +#endif + + while (buf->len > 1) { + struct net_buf_simple_state state; + uint8_t len, type; + + len = net_buf_simple_pull_u8(buf); + /* Check for early termination */ + if (len == 0U) { + return; + } + + if (len > buf->len) { + BT_WARN("AD malformed"); + return; + } + + net_buf_simple_save(buf, &state); + + type = net_buf_simple_pull_u8(buf); + + buf->len = len - 1; + +#if 0 + /* TODO: Check with BLE Mesh BQB test cases */ + if ((type == BLE_MESH_DATA_MESH_PROV || type == BLE_MESH_DATA_MESH_MESSAGE || + type == BLE_MESH_DATA_MESH_BEACON) && (adv_type != BLE_MESH_ADV_NONCONN_IND)) { + BT_DBG("Ignore mesh packet (type 0x%02x) with adv_type 0x%02x", type, adv_type); + return; + } +#endif + + switch (type) { + case BLE_MESH_DATA_MESH_MESSAGE: + bt_mesh_net_recv(buf, rssi, BLE_MESH_NET_IF_ADV); + break; +#if CONFIG_BLE_MESH_PB_ADV + case BLE_MESH_DATA_MESH_PROV: + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node()) { + bt_mesh_pb_adv_recv(buf); + } + if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en()) { + bt_mesh_provisioner_pb_adv_recv(buf); + } + break; +#endif /* CONFIG_BLE_MESH_PB_ADV */ + case BLE_MESH_DATA_MESH_BEACON: + bt_mesh_beacon_recv(buf, rssi); + break; +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT + case BLE_MESH_DATA_FLAGS: + if (!adv_flags_valid(buf)) { + BT_DBG("Adv Flags mismatch, ignore this adv pkt"); + return; + } + break; + case BLE_MESH_DATA_UUID16_ALL: + if (!adv_service_uuid_valid(buf, &uuid)) { + BT_DBG("Adv Service UUID mismatch, ignore this adv pkt"); + return; + } + break; + case BLE_MESH_DATA_SVC_DATA16: + handle_adv_service_data(buf, addr, uuid, rssi); + break; +#endif + default: + break; + } + + net_buf_simple_restore(buf, &state); + net_buf_simple_pull(buf, len); + } +} + +int bt_mesh_scan_enable(void) +{ + int err = 0; + + struct bt_mesh_scan_param scan_param = { + .type = BLE_MESH_SCAN_PASSIVE, +#if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN + .filter_dup = BLE_MESH_SCAN_FILTER_DUP_ENABLE, +#else + .filter_dup = BLE_MESH_SCAN_FILTER_DUP_DISABLE, +#endif + .interval = SCAN_INTERVAL, + .window = SCAN_WINDOW, + .scan_fil_policy = BLE_MESH_SP_ADV_ALL, + }; + + BT_DBG("%s", __func__); + + err = bt_le_scan_start(&scan_param, bt_mesh_scan_cb); + if (err && err != -EALREADY) { + BT_ERR("starting scan failed (err %d)", err); + return err; + } + + return 0; +} + +int bt_mesh_scan_disable(void) +{ + int err = 0; + + BT_DBG("%s", __func__); + + err = bt_le_scan_stop(); + if (err && err != -EALREADY) { + BT_ERR("stopping scan failed (err %d)", err); + return err; + } + + return 0; +} + +#if CONFIG_BLE_MESH_TEST_USE_WHITE_LIST +int bt_mesh_scan_with_wl_enable(void) +{ + int err = 0; + + struct bt_mesh_scan_param scan_param = { + .type = BLE_MESH_SCAN_PASSIVE, +#if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN + .filter_dup = BLE_MESH_SCAN_FILTER_DUP_ENABLE, +#else + .filter_dup = BLE_MESH_SCAN_FILTER_DUP_DISABLE, +#endif + .interval = SCAN_INTERVAL, + .window = SCAN_WINDOW, + .scan_fil_policy = BLE_MESH_SP_ADV_WL, + }; + + BT_DBG("%s", __func__); + + err = bt_le_scan_start(&scan_param, bt_mesh_scan_cb); + if (err && err != -EALREADY) { + BT_ERR("starting scan failed (err %d)", err); + return err; + } + + return 0; +} +#endif /* CONFIG_BLE_MESH_TEST_USE_WHITE_LIST */ diff --git a/components/bt/esp_ble_mesh/mesh_core/scan.h b/components/bt/esp_ble_mesh/mesh_core/scan.h new file mode 100644 index 0000000000..a3b37b1bdc --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/scan.h @@ -0,0 +1,31 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2020 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _SCAN_H_ +#define _SCAN_H_ + +#include "mesh_bearer_adapt.h" + +#ifdef __cplusplus +extern "C" { +#endif + +const bt_mesh_addr_t *bt_mesh_get_unprov_dev_addr(void); + +int bt_mesh_scan_enable(void); + +int bt_mesh_scan_disable(void); + +int bt_mesh_scan_with_wl_enable(void); + +#ifdef __cplusplus +} +#endif + +#endif /* _SCAN_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/test.c b/components/bt/esp_ble_mesh/mesh_core/test.c index 18b78b910b..432e372d62 100644 --- a/components/bt/esp_ble_mesh/mesh_core/test.c +++ b/components/bt/esp_ble_mesh/mesh_core/test.c @@ -11,6 +11,7 @@ #include #include "adv.h" +#include "scan.h" #include "mesh.h" #include "test.h" #include "crypto.h" From 34b5c1cf722ee4e38fa51f745202378f939328d3 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 26 Oct 2020 16:04:34 +0800 Subject: [PATCH 3/5] ble_mesh: stack: Add a btc file for ble coex functions Previously if starting/stopping BLE advertising when BLE Mesh is enabled, the corresponding events will be notified through the callback registered by esp_ble_mesh_register_prov_callback(). With this commit, the func esp_ble_mesh_register_ble_callback() needs to be invoked for resgitering the callback used for BLE coexistence related functionalities (i.e. ADV/SCAN). --- components/bt/CMakeLists.txt | 1 + components/bt/common/btc/core/btc_task.c | 4 + .../bt/common/btc/include/btc/btc_task.h | 1 + components/bt/esp_ble_mesh/Kconfig.in | 40 ++--- .../api/core/esp_ble_mesh_ble_api.c | 37 ++--- .../api/core/include/esp_ble_mesh_ble_api.h | 76 +++++++++- .../bt/esp_ble_mesh/api/esp_ble_mesh_defs.h | 46 ------ .../bt/esp_ble_mesh/btc/btc_ble_mesh_ble.c | 140 ++++++++++++++++++ .../bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c | 20 --- .../btc/include/btc_ble_mesh_ble.h | 50 +++++++ .../btc/include/btc_ble_mesh_prov.h | 9 -- 11 files changed, 313 insertions(+), 111 deletions(-) create mode 100644 components/bt/esp_ble_mesh/btc/btc_ble_mesh_ble.c create mode 100644 components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_ble.h diff --git a/components/bt/CMakeLists.txt b/components/bt/CMakeLists.txt index f6e4bf0790..d3cde9e915 100644 --- a/components/bt/CMakeLists.txt +++ b/components/bt/CMakeLists.txt @@ -324,6 +324,7 @@ if(CONFIG_BT_ENABLED) "esp_ble_mesh/api/models/esp_ble_mesh_lighting_model_api.c" "esp_ble_mesh/api/models/esp_ble_mesh_sensor_model_api.c" "esp_ble_mesh/api/models/esp_ble_mesh_time_scene_model_api.c" + "esp_ble_mesh/btc/btc_ble_mesh_ble.c" "esp_ble_mesh/btc/btc_ble_mesh_config_model.c" "esp_ble_mesh/btc/btc_ble_mesh_generic_model.c" "esp_ble_mesh/btc/btc_ble_mesh_health_model.c" diff --git a/components/bt/common/btc/core/btc_task.c b/components/bt/common/btc/core/btc_task.c index 6a97fe8af8..25342c9300 100644 --- a/components/bt/common/btc/core/btc_task.c +++ b/components/bt/common/btc/core/btc_task.c @@ -54,6 +54,7 @@ #endif #if CONFIG_BLE_MESH +#include "btc_ble_mesh_ble.h" #include "btc_ble_mesh_prov.h" #include "btc_ble_mesh_health_model.h" #include "btc_ble_mesh_config_model.h" @@ -152,6 +153,9 @@ static const btc_func_t profile_tab[BTC_PID_NUM] = { #if CONFIG_BLE_MESH_TIME_SCENE_SERVER [BTC_PID_TIME_SCENE_SERVER] = {NULL, btc_ble_mesh_time_scene_server_cb_handler}, #endif /* CONFIG_BLE_MESH_TIME_SCENE_SERVER */ +#if CONFIG_BLE_MESH_SUPPORT_BLE_ADV + [BTC_PID_BLE_MESH_BLE_COEX] = {btc_ble_mesh_ble_call_handler, btc_ble_mesh_ble_cb_handler }, +#endif /* CONFIG_BLE_MESH_SUPPORT_BLE_ADV */ #endif /* #if CONFIG_BLE_MESH */ }; diff --git a/components/bt/common/btc/include/btc/btc_task.h b/components/bt/common/btc/include/btc/btc_task.h index ce62567c45..a08bfbe9c8 100644 --- a/components/bt/common/btc/include/btc/btc_task.h +++ b/components/bt/common/btc/include/btc/btc_task.h @@ -84,6 +84,7 @@ typedef enum { BTC_PID_LIGHTING_SERVER, BTC_PID_SENSOR_SERVER, BTC_PID_TIME_SCENE_SERVER, + BTC_PID_BLE_MESH_BLE_COEX, #endif /* CONFIG_BLE_MESH */ BTC_PID_NUM, } btc_pid_t; //btc profile id diff --git a/components/bt/esp_ble_mesh/Kconfig.in b/components/bt/esp_ble_mesh/Kconfig.in index d63985d146..a1946c6cc7 100644 --- a/components/bt/esp_ble_mesh/Kconfig.in +++ b/components/bt/esp_ble_mesh/Kconfig.in @@ -85,6 +85,28 @@ if BLE_MESH If enabled, users can use the function esp_ble_mesh_deinit() to de-initialize the whole BLE Mesh stack. + menu "BLE Mesh and BLE coexistence support" + + config BLE_MESH_SUPPORT_BLE_ADV + bool "Support sending normal BLE advertising packets" + default n + help + When selected, users can send normal BLE advertising packets + with specific API. + + if BLE_MESH_SUPPORT_BLE_ADV + + config BLE_MESH_BLE_ADV_BUF_COUNT + int "Number of advertising buffers for BLE advertising packets" + default 3 + range 1 255 + help + Number of advertising buffers for BLE packets available. + + endif # BLE_MESH_SUPPORT_BLE_ADV + + endmenu # BLE Mesh and BLE coexistence support + config BLE_MESH_FAST_PROV bool "Enable BLE Mesh Fast Provisioning" select BLE_MESH_NODE @@ -493,24 +515,6 @@ if BLE_MESH SDU size is 60 bytes, which leaves 56 bytes for application layer data using a 4-byte MIC, or 52 bytes using an 8-byte MIC. - config BLE_MESH_SUPPORT_BLE_ADV - bool "Support sending normal BLE advertising packets" - default n - help - When selected, users can send normal BLE advertising packets - with specific API. - - if BLE_MESH_SUPPORT_BLE_ADV - - config BLE_MESH_BLE_ADV_BUF_COUNT - int "Number of advertising buffers for BLE advertising packets" - default 3 - range 1 255 - help - Number of advertising buffers for BLE packets available. - - endif # BLE_MESH_SUPPORT_BLE_ADV - config BLE_MESH_IVU_DIVIDER int "Divider for IV Update state refresh timer" default 4 diff --git a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_ble_api.c b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_ble_api.c index 1ba2dcd578..8039d6162e 100644 --- a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_ble_api.c +++ b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_ble_api.c @@ -1,4 +1,4 @@ -// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// Copyright 2017-2020 Espressif Systems (Shanghai) PTE LTD // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -15,19 +15,23 @@ #include #include -#include "btc/btc_manage.h" - #include "esp_err.h" -#include "btc_ble_mesh_prov.h" -#include "esp_ble_mesh_defs.h" +#include "btc_ble_mesh_ble.h" +#include "esp_ble_mesh_ble_api.h" #if CONFIG_BLE_MESH_SUPPORT_BLE_ADV +esp_err_t esp_ble_mesh_register_ble_callback(esp_ble_mesh_ble_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_BLE_MESH_BLE_COEX, callback) == 0 ? ESP_OK : ESP_FAIL); +} esp_err_t esp_ble_mesh_start_ble_advertising(const esp_ble_mesh_ble_adv_param_t *param, const esp_ble_mesh_ble_adv_data_t *data) { - btc_ble_mesh_prov_args_t arg = {0}; + btc_ble_mesh_ble_args_t arg = {0}; btc_msg_t msg = {0}; if (param == NULL) { @@ -37,21 +41,21 @@ esp_err_t esp_ble_mesh_start_ble_advertising(const esp_ble_mesh_ble_adv_param_t ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); msg.sig = BTC_SIG_API_CALL; - msg.pid = BTC_PID_PROV; - msg.act = BTC_BLE_MESH_ACT_START_BLE_ADVERTISING; + msg.pid = BTC_PID_BLE_MESH_BLE_COEX; + msg.act = BTC_BLE_MESH_ACT_START_BLE_ADV; - memcpy(&arg.start_ble_advertising.param, param, sizeof(esp_ble_mesh_ble_adv_param_t)); + memcpy(&arg.start_ble_adv.param, param, sizeof(esp_ble_mesh_ble_adv_param_t)); if (data) { - memcpy(&arg.start_ble_advertising.data, data, sizeof(esp_ble_mesh_ble_adv_data_t)); + memcpy(&arg.start_ble_adv.data, data, sizeof(esp_ble_mesh_ble_adv_data_t)); } - return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_ble_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } esp_err_t esp_ble_mesh_stop_ble_advertising(uint8_t index) { - btc_ble_mesh_prov_args_t arg = {0}; + btc_ble_mesh_ble_args_t arg = {0}; btc_msg_t msg = {0}; if (index >= CONFIG_BLE_MESH_BLE_ADV_BUF_COUNT) { @@ -61,13 +65,12 @@ esp_err_t esp_ble_mesh_stop_ble_advertising(uint8_t index) ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); msg.sig = BTC_SIG_API_CALL; - msg.pid = BTC_PID_PROV; - msg.act = BTC_BLE_MESH_ACT_STOP_BLE_ADVERTISING; + msg.pid = BTC_PID_BLE_MESH_BLE_COEX; + msg.act = BTC_BLE_MESH_ACT_STOP_BLE_ADV; - arg.stop_ble_advertising.index = index; + arg.stop_ble_adv.index = index; - return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_ble_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } - #endif /* CONFIG_BLE_MESH_SUPPORT_BLE_ADV */ diff --git a/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_ble_api.h b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_ble_api.h index 23ebca66b6..f36c2bd067 100644 --- a/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_ble_api.h +++ b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_ble_api.h @@ -1,4 +1,4 @@ -// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// Copyright 2017-2020 Espressif Systems (Shanghai) PTE LTD // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -21,6 +21,80 @@ extern "C" { #endif +/** This enum value is the event of BLE operations */ +typedef enum { + ESP_BLE_MESH_START_BLE_ADVERTISING_COMP_EVT, /*!< Start BLE advertising completion event */ + ESP_BLE_MESH_STOP_BLE_ADVERTISING_COMP_EVT, /*!< Stop BLE advertising completion event */ + ESP_BLE_MESH_BLE_EVT_MAX, +} esp_ble_mesh_ble_cb_event_t; + +/** BLE operation callback parameters */ +typedef union { + /** + * @brief ESP_BLE_MESH_START_BLE_ADVERTISING_COMP_EVT + */ + struct { + int err_code; /*!< Indicate the result of starting BLE advertising */ + uint8_t index; /*!< Index of the BLE advertising */ + } start_ble_advertising_comp; /*!< Event parameters of ESP_BLE_MESH_START_BLE_ADVERTISING_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_STOP_BLE_ADVERTISING_COMP_EVT + */ + struct { + int err_code; /*!< Indicate the result of stopping BLE advertising */ + uint8_t index; /*!< Index of the BLE advertising */ + } stop_ble_advertising_comp; /*!< Event parameters of ESP_BLE_MESH_STOP_BLE_ADVERTISING_COMP_EVT */ +} esp_ble_mesh_ble_cb_param_t; + +/** + * @brief BLE scanning callback function type + * + * @param event: BLE scanning callback event type + * @param param: BLE scanning callback parameter + */ +typedef void (* esp_ble_mesh_ble_cb_t)(esp_ble_mesh_ble_cb_event_t event, + esp_ble_mesh_ble_cb_param_t *param); + +/** + * @brief Register BLE scanning callback. + * + * @param[in] callback: Pointer to the BLE scaning callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_ble_callback(esp_ble_mesh_ble_cb_t callback); + +/** Count for sending BLE advertising packet infinitely */ +#define ESP_BLE_MESH_BLE_ADV_INFINITE 0xFFFF + +/*!< This enum value is the priority of BLE advertising packet */ +typedef enum { + ESP_BLE_MESH_BLE_ADV_PRIO_LOW, + ESP_BLE_MESH_BLE_ADV_PRIO_HIGH, +} esp_ble_mesh_ble_adv_priority_t; + +/** Context of BLE advertising parameters. */ +typedef struct { + uint16_t interval; /*!< BLE advertising interval */ + uint8_t adv_type; /*!< BLE advertising type */ + uint8_t own_addr_type; /*!< Own address type */ + uint8_t peer_addr_type; /*!< Peer address type */ + uint8_t peer_addr[BD_ADDR_LEN]; /*!< Peer address */ + uint16_t duration; /*!< Duration is milliseconds */ + uint16_t period; /*!< Period in milliseconds */ + uint16_t count; /*!< Number of advertising duration */ + uint8_t priority:2; /*!< Priority of BLE advertising packet */ +} esp_ble_mesh_ble_adv_param_t; + +/** Context of BLE advertising data. */ +typedef struct { + uint8_t adv_data_len; /*!< Advertising data length */ + uint8_t adv_data[31]; /*!< Advertising data */ + uint8_t scan_rsp_data_len; /*!< Scan response data length */ + uint8_t scan_rsp_data[31]; /*!< Scan response data */ +} esp_ble_mesh_ble_adv_data_t; + /** * @brief This function is called to start BLE advertising with the corresponding data * and parameters while BLE Mesh is working at the same time. diff --git a/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h b/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h index 3318278522..ea02c53b5d 100644 --- a/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h +++ b/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h @@ -777,36 +777,6 @@ typedef enum { PROXY_FILTER_BLACKLIST, } esp_ble_mesh_proxy_filter_type_t; -/** Count for sending BLE advertising packet infinitely */ -#define ESP_BLE_MESH_BLE_ADV_INFINITE 0xFFFF - -/*!< This enum value is the priority of BLE advertising packet */ -typedef enum { - ESP_BLE_MESH_BLE_ADV_PRIO_LOW, - ESP_BLE_MESH_BLE_ADV_PRIO_HIGH, -} esp_ble_mesh_ble_adv_priority_t; - -/** Context of BLE advertising parameters. */ -typedef struct { - uint16_t interval; /*!< BLE advertising interval */ - uint8_t adv_type; /*!< BLE advertising type */ - uint8_t own_addr_type; /*!< Own address type */ - uint8_t peer_addr_type; /*!< Peer address type */ - uint8_t peer_addr[BD_ADDR_LEN]; /*!< Peer address */ - uint16_t duration; /*!< Duration is milliseconds */ - uint16_t period; /*!< Period in milliseconds */ - uint16_t count; /*!< Number of advertising duration */ - uint8_t priority:2; /*!< Priority of BLE advertising packet */ -} esp_ble_mesh_ble_adv_param_t; - -/** Context of BLE advertising data. */ -typedef struct { - uint8_t adv_data_len; /*!< Advertising data length */ - uint8_t adv_data[31]; /*!< Advertising data */ - uint8_t scan_rsp_data_len; /*!< Scan response data length */ - uint8_t scan_rsp_data[31]; /*!< Scan response data */ -} esp_ble_mesh_ble_adv_data_t; - /*!< Provisioner heartbeat filter type */ #define ESP_BLE_MESH_HEARTBEAT_FILTER_ACCEPTLIST 0x00 #define ESP_BLE_MESH_HEARTBEAT_FILTER_REJECTLIST 0x01 @@ -902,8 +872,6 @@ typedef enum { ESP_BLE_MESH_PROXY_CLIENT_SET_FILTER_TYPE_COMP_EVT, /*!< Proxy Client set filter type completion event */ ESP_BLE_MESH_PROXY_CLIENT_ADD_FILTER_ADDR_COMP_EVT, /*!< Proxy Client add filter address completion event */ ESP_BLE_MESH_PROXY_CLIENT_REMOVE_FILTER_ADDR_COMP_EVT, /*!< Proxy Client remove filter address completion event */ - ESP_BLE_MESH_START_BLE_ADVERTISING_COMP_EVT, /*!< Start BLE advertising completion event */ - ESP_BLE_MESH_STOP_BLE_ADVERTISING_COMP_EVT, /*!< Stop BLE advertising completion event */ ESP_BLE_MESH_MODEL_SUBSCRIBE_GROUP_ADDR_COMP_EVT, /*!< Local model subscribes group address completion event */ ESP_BLE_MESH_MODEL_UNSUBSCRIBE_GROUP_ADDR_COMP_EVT, /*!< Local model unsubscribes group address completion event */ ESP_BLE_MESH_DEINIT_MESH_COMP_EVT, /*!< De-initialize BLE Mesh stack completion event */ @@ -1486,20 +1454,6 @@ typedef union { uint8_t conn_handle; /*!< Proxy connection handle */ uint16_t net_idx; /*!< Corresponding NetKey Index */ } proxy_client_remove_filter_addr_comp; /*!< Event parameter of ESP_BLE_MESH_PROXY_CLIENT_REMOVE_FILTER_ADDR_COMP_EVT */ - /** - * @brief ESP_BLE_MESH_START_BLE_ADVERTISING_COMP_EVT - */ - struct ble_mesh_start_ble_advertising_comp_param { - int err_code; /*!< Indicate the result of starting BLE advertising */ - uint8_t index; /*!< Index of the BLE advertising */ - } start_ble_advertising_comp; /*!< Event parameter of ESP_BLE_MESH_START_BLE_ADVERTISING_COMP_EVT */ - /** - * @brief ESP_BLE_MESH_STOP_BLE_ADVERTISING_COMP_EVT - */ - struct ble_mesh_stop_ble_advertising_comp_param { - int err_code; /*!< Indicate the result of stopping BLE advertising */ - uint8_t index; /*!< Index of the BLE advertising */ - } stop_ble_advertising_comp; /*!< Event parameter of ESP_BLE_MESH_STOP_BLE_ADVERTISING_COMP_EVT */ /** * @brief ESP_BLE_MESH_MODEL_SUBSCRIBE_GROUP_ADDR_COMP_EVT */ diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_ble.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_ble.c new file mode 100644 index 0000000000..c9cf8f5a23 --- /dev/null +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_ble.c @@ -0,0 +1,140 @@ +// Copyright 2020 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "btc_ble_mesh_ble.h" +#include "adv.h" +#include "mesh_bearer_adapt.h" +#include "esp_ble_mesh_ble_api.h" + +#if CONFIG_BLE_MESH_SUPPORT_BLE_ADV + +static void btc_ble_mesh_ble_copy_req_data(btc_msg_t *msg, void *p_dst, void *p_src) +{ + esp_ble_mesh_ble_cb_param_t *p_dst_data = (esp_ble_mesh_ble_cb_param_t *)p_dst; + esp_ble_mesh_ble_cb_param_t *p_src_data = (esp_ble_mesh_ble_cb_param_t *)p_src; + + if (!msg || !p_src_data || !p_dst_data) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + default: + break; + } +} + +static void btc_ble_mesh_ble_free_req_data(btc_msg_t *msg) +{ + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + default: + break; + } +} + +static void btc_ble_mesh_ble_callback(esp_ble_mesh_ble_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_BLE_MESH_BLE_COEX)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLE_MESH_BLE_COEX; + msg.act = act; + + btc_transfer_context(&msg, cb_params, sizeof(esp_ble_mesh_ble_cb_param_t), + btc_ble_mesh_ble_copy_req_data); +} + +void btc_ble_mesh_ble_call_handler(btc_msg_t *msg) +{ + esp_ble_mesh_ble_cb_param_t param = {0}; + btc_ble_mesh_ble_args_t *arg = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_ble_args_t *)msg->arg; + + switch (msg->act) { +#if CONFIG_BLE_MESH_SUPPORT_BLE_ADV + case BTC_BLE_MESH_ACT_START_BLE_ADV: { + struct bt_mesh_ble_adv_param *set = (struct bt_mesh_ble_adv_param *)&arg->start_ble_adv.param; + struct bt_mesh_ble_adv_data *data = NULL; + if (arg->start_ble_adv.data.adv_data_len || arg->start_ble_adv.data.scan_rsp_data_len) { + data = (struct bt_mesh_ble_adv_data *)&arg->start_ble_adv.data; + } + + param.start_ble_advertising_comp.err_code = + bt_mesh_start_ble_advertising(set, data, ¶m.start_ble_advertising_comp.index); + + btc_ble_mesh_ble_callback(¶m, ESP_BLE_MESH_START_BLE_ADVERTISING_COMP_EVT); + break; + } + case BTC_BLE_MESH_ACT_STOP_BLE_ADV: + param.stop_ble_advertising_comp.index = arg->stop_ble_adv.index; + param.stop_ble_advertising_comp.err_code = + bt_mesh_stop_ble_advertising(arg->stop_ble_adv.index); + + btc_ble_mesh_ble_callback(¶m, ESP_BLE_MESH_STOP_BLE_ADVERTISING_COMP_EVT); + break; +#endif /* CONFIG_BLE_MESH_SUPPORT_BLE_ADV */ + default: + return; + } +} + +static inline void btc_ble_mesh_ble_cb_to_app(esp_ble_mesh_ble_cb_event_t event, + esp_ble_mesh_ble_cb_param_t *param) +{ + esp_ble_mesh_ble_cb_t btc_ble_mesh_cb = (esp_ble_mesh_ble_cb_t)btc_profile_cb_get(BTC_PID_BLE_MESH_BLE_COEX); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +void btc_ble_mesh_ble_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_ble_cb_param_t *param = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + param = (esp_ble_mesh_ble_cb_param_t *)msg->arg; + + if (msg->act < ESP_BLE_MESH_BLE_EVT_MAX) { + btc_ble_mesh_ble_cb_to_app(msg->act, param); + } else { + BT_ERR("%s, Unknown act %d", __func__, msg->act); + } + + btc_ble_mesh_ble_free_req_data(msg); +} + +#endif /* CONFIG_BLE_MESH_SUPPORT_BLE_ADV */ diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c index 19e0732f31..6fe3da8b7e 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c @@ -2209,26 +2209,6 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg) break; } #endif /* CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ -#if CONFIG_BLE_MESH_SUPPORT_BLE_ADV - case BTC_BLE_MESH_ACT_START_BLE_ADVERTISING: { - struct bt_mesh_ble_adv_param *set = (struct bt_mesh_ble_adv_param *)&arg->start_ble_advertising.param; - struct bt_mesh_ble_adv_data *data = NULL; - if (arg->start_ble_advertising.data.adv_data_len || - arg->start_ble_advertising.data.scan_rsp_data_len) { - data = (struct bt_mesh_ble_adv_data *)&arg->start_ble_advertising.data; - } - act = ESP_BLE_MESH_START_BLE_ADVERTISING_COMP_EVT; - param.start_ble_advertising_comp.err_code = - bt_mesh_start_ble_advertising(set, data, ¶m.start_ble_advertising_comp.index); - break; - } - case BTC_BLE_MESH_ACT_STOP_BLE_ADVERTISING: - act = ESP_BLE_MESH_STOP_BLE_ADVERTISING_COMP_EVT; - param.stop_ble_advertising_comp.index = arg->stop_ble_advertising.index; - param.stop_ble_advertising_comp.err_code = - bt_mesh_stop_ble_advertising(arg->stop_ble_advertising.index); - break; -#endif /* CONFIG_BLE_MESH_SUPPORT_BLE_ADV */ case BTC_BLE_MESH_ACT_MODEL_SUBSCRIBE_GROUP_ADDR: act = ESP_BLE_MESH_MODEL_SUBSCRIBE_GROUP_ADDR_COMP_EVT; param.model_sub_group_addr_comp.element_addr = arg->model_sub_group_addr.element_addr; diff --git a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_ble.h b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_ble.h new file mode 100644 index 0000000000..2fa785ef87 --- /dev/null +++ b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_ble.h @@ -0,0 +1,50 @@ +// Copyright 2020 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _BTC_BLE_MESH_BLE_H_ +#define _BTC_BLE_MESH_BLE_H_ + +#include +#include "btc/btc_manage.h" +#include "mesh_bearer_adapt.h" +#include "esp_ble_mesh_ble_api.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef union { + struct { + esp_ble_mesh_ble_adv_param_t param; + esp_ble_mesh_ble_adv_data_t data; + } start_ble_adv; + struct { + uint8_t index; + } stop_ble_adv; +} btc_ble_mesh_ble_args_t; + +typedef enum { + BTC_BLE_MESH_ACT_START_BLE_ADV, + BTC_BLE_MESH_ACT_STOP_BLE_ADV, +} btc_ble_mesh_ble_act_t; + +void btc_ble_mesh_ble_call_handler(btc_msg_t *msg); + +void btc_ble_mesh_ble_cb_handler(btc_msg_t *msg); + +#ifdef __cplusplus +} +#endif + +#endif /* _BTC_BLE_MESH_BLE_H_ */ diff --git a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h index ac47a4ab82..86aa74796e 100644 --- a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h +++ b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h @@ -83,8 +83,6 @@ typedef enum { BTC_BLE_MESH_ACT_PROXY_CLIENT_SET_FILTER_TYPE, BTC_BLE_MESH_ACT_PROXY_CLIENT_ADD_FILTER_ADDR, BTC_BLE_MESH_ACT_PROXY_CLIENT_REMOVE_FILTER_ADDR, - BTC_BLE_MESH_ACT_START_BLE_ADVERTISING, - BTC_BLE_MESH_ACT_STOP_BLE_ADVERTISING, BTC_BLE_MESH_ACT_MODEL_SUBSCRIBE_GROUP_ADDR, BTC_BLE_MESH_ACT_MODEL_UNSUBSCRIBE_GROUP_ADDR, BTC_BLE_MESH_ACT_DEINIT_MESH, @@ -305,13 +303,6 @@ typedef union { uint16_t addr_num; uint16_t *addr; } proxy_client_remove_filter_addr; - struct ble_mesh_start_ble_advertising_args { - esp_ble_mesh_ble_adv_param_t param; - esp_ble_mesh_ble_adv_data_t data; - } start_ble_advertising; - struct ble_mesh_stop_ble_advertising_args { - uint8_t index; - } stop_ble_advertising; struct ble_mesh_model_sub_group_addr_args { uint16_t element_addr; uint16_t company_id; From f9b53a909bcfeabade028b1e0097bc10213b7df6 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 26 Oct 2020 15:51:18 +0800 Subject: [PATCH 4/5] ble_mesh: stack: Support reporting normal ble adv packets --- components/bt/common/btc/core/btc_task.c | 6 +- components/bt/esp_ble_mesh/Kconfig.in | 7 ++ .../api/core/esp_ble_mesh_ble_api.c | 36 +++++++++- .../api/core/include/esp_ble_mesh_ble_api.h | 51 ++++++++++++++ .../bt/esp_ble_mesh/btc/btc_ble_mesh_ble.c | 69 ++++++++++++++++++- .../btc/include/btc_ble_mesh_ble.h | 12 ++++ .../mesh_common/include/mesh_config.h | 3 + components/bt/esp_ble_mesh/mesh_core/scan.c | 66 +++++++++++++++++- components/bt/esp_ble_mesh/mesh_core/scan.h | 8 +++ 9 files changed, 249 insertions(+), 9 deletions(-) diff --git a/components/bt/common/btc/core/btc_task.c b/components/bt/common/btc/core/btc_task.c index 25342c9300..71de409cfc 100644 --- a/components/bt/common/btc/core/btc_task.c +++ b/components/bt/common/btc/core/btc_task.c @@ -153,9 +153,9 @@ static const btc_func_t profile_tab[BTC_PID_NUM] = { #if CONFIG_BLE_MESH_TIME_SCENE_SERVER [BTC_PID_TIME_SCENE_SERVER] = {NULL, btc_ble_mesh_time_scene_server_cb_handler}, #endif /* CONFIG_BLE_MESH_TIME_SCENE_SERVER */ -#if CONFIG_BLE_MESH_SUPPORT_BLE_ADV - [BTC_PID_BLE_MESH_BLE_COEX] = {btc_ble_mesh_ble_call_handler, btc_ble_mesh_ble_cb_handler }, -#endif /* CONFIG_BLE_MESH_SUPPORT_BLE_ADV */ +#if CONFIG_BLE_MESH_BLE_COEX_SUPPORT + [BTC_PID_BLE_MESH_BLE_COEX] = {btc_ble_mesh_ble_call_handler, btc_ble_mesh_ble_cb_handler }, +#endif /* CONFIG_BLE_MESH_BLE_COEX_SUPPORT */ #endif /* #if CONFIG_BLE_MESH */ }; diff --git a/components/bt/esp_ble_mesh/Kconfig.in b/components/bt/esp_ble_mesh/Kconfig.in index a1946c6cc7..3dff04a448 100644 --- a/components/bt/esp_ble_mesh/Kconfig.in +++ b/components/bt/esp_ble_mesh/Kconfig.in @@ -105,6 +105,13 @@ if BLE_MESH endif # BLE_MESH_SUPPORT_BLE_ADV + config BLE_MESH_SUPPORT_BLE_SCAN + bool "Support scanning normal BLE advertising packets" + default n + help + When selected, users can register a callback and receive normal BLE + advertising packets in the application layer. + endmenu # BLE Mesh and BLE coexistence support config BLE_MESH_FAST_PROV diff --git a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_ble_api.c b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_ble_api.c index 8039d6162e..78e5eecbbb 100644 --- a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_ble_api.c +++ b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_ble_api.c @@ -20,14 +20,16 @@ #include "btc_ble_mesh_ble.h" #include "esp_ble_mesh_ble_api.h" -#if CONFIG_BLE_MESH_SUPPORT_BLE_ADV +#if CONFIG_BLE_MESH_BLE_COEX_SUPPORT esp_err_t esp_ble_mesh_register_ble_callback(esp_ble_mesh_ble_cb_t callback) { ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); return (btc_profile_cb_set(BTC_PID_BLE_MESH_BLE_COEX, callback) == 0 ? ESP_OK : ESP_FAIL); } +#endif /* CONFIG_BLE_MESH_BLE_COEX_SUPPORT */ +#if CONFIG_BLE_MESH_SUPPORT_BLE_ADV esp_err_t esp_ble_mesh_start_ble_advertising(const esp_ble_mesh_ble_adv_param_t *param, const esp_ble_mesh_ble_adv_data_t *data) { @@ -74,3 +76,35 @@ esp_err_t esp_ble_mesh_stop_ble_advertising(uint8_t index) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } #endif /* CONFIG_BLE_MESH_SUPPORT_BLE_ADV */ + +#if CONFIG_BLE_MESH_SUPPORT_BLE_SCAN +esp_err_t esp_ble_mesh_start_ble_scanning(esp_ble_mesh_ble_scan_param_t *param) +{ + btc_ble_mesh_ble_args_t arg = {0}; + btc_msg_t msg = {0}; + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_BLE_MESH_BLE_COEX; + msg.act = BTC_BLE_MESH_ACT_START_BLE_SCAN; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_ble_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_stop_ble_scanning(void) +{ + btc_ble_mesh_ble_args_t arg = {0}; + btc_msg_t msg = {0}; + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_BLE_MESH_BLE_COEX; + msg.act = BTC_BLE_MESH_ACT_STOP_BLE_SCAN; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_ble_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} +#endif /* CONFIG_BLE_MESH_SUPPORT_BLE_SCAN */ diff --git a/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_ble_api.h b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_ble_api.h index f36c2bd067..3ae71d40a1 100644 --- a/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_ble_api.h +++ b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_ble_api.h @@ -25,6 +25,9 @@ extern "C" { typedef enum { ESP_BLE_MESH_START_BLE_ADVERTISING_COMP_EVT, /*!< Start BLE advertising completion event */ ESP_BLE_MESH_STOP_BLE_ADVERTISING_COMP_EVT, /*!< Stop BLE advertising completion event */ + ESP_BLE_MESH_START_BLE_SCANNING_COMP_EVT, /*!< Start BLE scanning completion event */ + ESP_BLE_MESH_STOP_BLE_SCANNING_COMP_EVT, /*!< Stop BLE scanning completion event */ + ESP_BLE_MESH_SCAN_BLE_ADVERTISING_PKT_EVT, /*!< Scanning BLE advertising packets event */ ESP_BLE_MESH_BLE_EVT_MAX, } esp_ble_mesh_ble_cb_event_t; @@ -44,6 +47,29 @@ typedef union { int err_code; /*!< Indicate the result of stopping BLE advertising */ uint8_t index; /*!< Index of the BLE advertising */ } stop_ble_advertising_comp; /*!< Event parameters of ESP_BLE_MESH_STOP_BLE_ADVERTISING_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_START_BLE_SCANNING_COMP_EVT + */ + struct { + int err_code; /*!< Indicate the result of starting BLE scanning */ + } start_ble_scan_comp; /*!< Event parameters of ESP_BLE_MESH_START_BLE_SCANNING_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_STOP_BLE_SCANNING_COMP_EVT + */ + struct { + int err_code; /*!< Indicate the result of stopping BLE scanning */ + } stop_ble_scan_comp; /*!< Event parameters of ESP_BLE_MESH_STOP_BLE_SCANNING_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_SCAN_BLE_ADVERTISING_PKT_EVT + */ + struct { + uint8_t addr[6]; /*!< Device address */ + uint8_t addr_type; /*!< Device address type */ + uint8_t adv_type; /*!< Advertising data type */ + uint8_t *data; /*!< Advertising data */ + uint16_t length; /*!< Advertising data length */ + int8_t rssi; /*!< RSSI of the advertising packet */ + } scan_ble_adv_pkt; /*!< Event parameters of ESP_BLE_MESH_SCAN_BLE_ADVERTISING_PKT_EVT */ } esp_ble_mesh_ble_cb_param_t; /** @@ -132,6 +158,31 @@ esp_err_t esp_ble_mesh_start_ble_advertising(const esp_ble_mesh_ble_adv_param_t */ esp_err_t esp_ble_mesh_stop_ble_advertising(uint8_t index); +/** Context of BLE scanning parameters. */ +typedef struct { + uint32_t duration; /*!< Duration used to scan normal BLE advertising packets */ +} esp_ble_mesh_ble_scan_param_t; + +/** + * @brief This function is called to start scanning normal BLE advertising packets + * and notifying the packets to the application layer. + * + * @param[in] param: Pointer to the BLE scanning parameters + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_start_ble_scanning(esp_ble_mesh_ble_scan_param_t *param); + +/** + * @brief This function is called to stop notifying normal BLE advertising packets + * to the application layer. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_stop_ble_scanning(void); + #ifdef __cplusplus } #endif diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_ble.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_ble.c index c9cf8f5a23..6c66a29de6 100644 --- a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_ble.c +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_ble.c @@ -17,13 +17,15 @@ #include "btc_ble_mesh_ble.h" #include "adv.h" +#include "scan.h" #include "mesh_bearer_adapt.h" #include "esp_ble_mesh_ble_api.h" -#if CONFIG_BLE_MESH_SUPPORT_BLE_ADV +#if CONFIG_BLE_MESH_BLE_COEX_SUPPORT static void btc_ble_mesh_ble_copy_req_data(btc_msg_t *msg, void *p_dst, void *p_src) { +#if CONFIG_BLE_MESH_SUPPORT_BLE_SCAN esp_ble_mesh_ble_cb_param_t *p_dst_data = (esp_ble_mesh_ble_cb_param_t *)p_dst; esp_ble_mesh_ble_cb_param_t *p_src_data = (esp_ble_mesh_ble_cb_param_t *)p_src; @@ -33,22 +35,46 @@ static void btc_ble_mesh_ble_copy_req_data(btc_msg_t *msg, void *p_dst, void *p_ } switch (msg->act) { + case ESP_BLE_MESH_SCAN_BLE_ADVERTISING_PKT_EVT: + if (p_src_data->scan_ble_adv_pkt.data && p_src_data->scan_ble_adv_pkt.length) { + p_dst_data->scan_ble_adv_pkt.length = p_src_data->scan_ble_adv_pkt.length; + p_dst_data->scan_ble_adv_pkt.data = bt_mesh_calloc(p_src_data->scan_ble_adv_pkt.length); + if (p_dst_data->scan_ble_adv_pkt.data) { + memcpy(p_dst_data->scan_ble_adv_pkt.data, p_src_data->scan_ble_adv_pkt.data, + p_src_data->scan_ble_adv_pkt.length); + } else { + BT_ERR("%s, Out of memory, act %d", __func__, msg->act); + } + } + break; default: break; } +#endif /* CONFIG_BLE_MESH_SUPPORT_BLE_SCAN */ } static void btc_ble_mesh_ble_free_req_data(btc_msg_t *msg) { +#if CONFIG_BLE_MESH_SUPPORT_BLE_SCAN + esp_ble_mesh_ble_cb_param_t *arg = NULL; + if (!msg || !msg->arg) { BT_ERR("%s, Invalid parameter", __func__); return; } + arg = (esp_ble_mesh_ble_cb_param_t *)msg->arg; + switch (msg->act) { + case ESP_BLE_MESH_SCAN_BLE_ADVERTISING_PKT_EVT: + if (arg->scan_ble_adv_pkt.data) { + bt_mesh_free(arg->scan_ble_adv_pkt.data); + } + break; default: break; } +#endif /* CONFIG_BLE_MESH_SUPPORT_BLE_SCAN */ } static void btc_ble_mesh_ble_callback(esp_ble_mesh_ble_cb_param_t *cb_params, uint8_t act) @@ -68,6 +94,31 @@ static void btc_ble_mesh_ble_callback(esp_ble_mesh_ble_cb_param_t *cb_params, ui btc_ble_mesh_ble_copy_req_data); } +#if CONFIG_BLE_MESH_SUPPORT_BLE_SCAN +void bt_mesh_ble_scan_cb_evt_to_btc(const bt_mesh_addr_t *addr, + uint8_t adv_type, uint8_t data[], + uint16_t length, int8_t rssi) +{ + esp_ble_mesh_ble_cb_param_t param = {0}; + + if (addr == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + memcpy(param.scan_ble_adv_pkt.addr, addr->val, sizeof(addr->val)); + param.scan_ble_adv_pkt.addr_type = addr->type; + if (data && length) { + param.scan_ble_adv_pkt.data = data; + param.scan_ble_adv_pkt.length = length; + } + param.scan_ble_adv_pkt.adv_type = adv_type; + param.scan_ble_adv_pkt.rssi = rssi; + + btc_ble_mesh_ble_callback(¶m, ESP_BLE_MESH_SCAN_BLE_ADVERTISING_PKT_EVT); +} +#endif /* CONFIG_BLE_MESH_SUPPORT_BLE_SCAN */ + void btc_ble_mesh_ble_call_handler(btc_msg_t *msg) { esp_ble_mesh_ble_cb_param_t param = {0}; @@ -103,6 +154,17 @@ void btc_ble_mesh_ble_call_handler(btc_msg_t *msg) btc_ble_mesh_ble_callback(¶m, ESP_BLE_MESH_STOP_BLE_ADVERTISING_COMP_EVT); break; #endif /* CONFIG_BLE_MESH_SUPPORT_BLE_ADV */ +#if CONFIG_BLE_MESH_SUPPORT_BLE_SCAN + case BTC_BLE_MESH_ACT_START_BLE_SCAN: + param.start_ble_scan_comp.err_code = + bt_mesh_start_ble_scan((struct bt_mesh_ble_scan_param *)&arg->start_ble_scan.param); + btc_ble_mesh_ble_callback(¶m, ESP_BLE_MESH_START_BLE_SCANNING_COMP_EVT); + break; + case BTC_BLE_MESH_ACT_STOP_BLE_SCAN: + param.stop_ble_scan_comp.err_code = bt_mesh_stop_ble_scan(); + btc_ble_mesh_ble_callback(¶m, ESP_BLE_MESH_STOP_BLE_SCANNING_COMP_EVT); + break; +#endif /* CONFIG_BLE_MESH_SUPPORT_BLE_SCAN */ default: return; } @@ -111,7 +173,8 @@ void btc_ble_mesh_ble_call_handler(btc_msg_t *msg) static inline void btc_ble_mesh_ble_cb_to_app(esp_ble_mesh_ble_cb_event_t event, esp_ble_mesh_ble_cb_param_t *param) { - esp_ble_mesh_ble_cb_t btc_ble_mesh_cb = (esp_ble_mesh_ble_cb_t)btc_profile_cb_get(BTC_PID_BLE_MESH_BLE_COEX); + esp_ble_mesh_ble_cb_t btc_ble_mesh_cb = + (esp_ble_mesh_ble_cb_t)btc_profile_cb_get(BTC_PID_BLE_MESH_BLE_COEX); if (btc_ble_mesh_cb) { btc_ble_mesh_cb(event, param); } @@ -137,4 +200,4 @@ void btc_ble_mesh_ble_cb_handler(btc_msg_t *msg) btc_ble_mesh_ble_free_req_data(msg); } -#endif /* CONFIG_BLE_MESH_SUPPORT_BLE_ADV */ +#endif /* CONFIG_BLE_MESH_BLE_COEX_SUPPORT */ diff --git a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_ble.h b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_ble.h index 2fa785ef87..d127f33cca 100644 --- a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_ble.h +++ b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_ble.h @@ -32,13 +32,25 @@ typedef union { struct { uint8_t index; } stop_ble_adv; + struct { + esp_ble_mesh_ble_scan_param_t param; + } start_ble_scan; + struct { + /* RFU */ + } stop_ble_scan; } btc_ble_mesh_ble_args_t; typedef enum { BTC_BLE_MESH_ACT_START_BLE_ADV, BTC_BLE_MESH_ACT_STOP_BLE_ADV, + BTC_BLE_MESH_ACT_START_BLE_SCAN, + BTC_BLE_MESH_ACT_STOP_BLE_SCAN, } btc_ble_mesh_ble_act_t; +void bt_mesh_ble_scan_cb_evt_to_btc(const bt_mesh_addr_t *addr, + uint8_t adv_type, uint8_t data[], + uint16_t length, int8_t rssi); + void btc_ble_mesh_ble_call_handler(btc_msg_t *msg); void btc_ble_mesh_ble_cb_handler(btc_msg_t *msg); diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_config.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_config.h index f019104383..d8450bebc3 100644 --- a/components/bt/esp_ble_mesh/mesh_common/include/mesh_config.h +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_config.h @@ -45,6 +45,9 @@ extern "C" { CONFIG_BLE_MESH_TIME_SCENE_SERVER | \ CONFIG_BLE_MESH_LIGHTING_SERVER) +#define CONFIG_BLE_MESH_BLE_COEX_SUPPORT (CONFIG_BLE_MESH_SUPPORT_BLE_ADV | \ + CONFIG_BLE_MESH_SUPPORT_BLE_SCAN) + #ifdef __cplusplus } #endif diff --git a/components/bt/esp_ble_mesh/mesh_core/scan.c b/components/bt/esp_ble_mesh/mesh_core/scan.c index 52359cfb30..7c25b864a8 100644 --- a/components/bt/esp_ble_mesh/mesh_core/scan.c +++ b/components/bt/esp_ble_mesh/mesh_core/scan.c @@ -11,10 +11,13 @@ #include #include +#include "btc_ble_mesh_ble.h" + #include "mesh_config.h" #include "mesh_trace.h" #include "mesh_buf.h" #include "mesh_uuid.h" +#include "scan.h" #include "beacon.h" #include "net.h" #include "prov.h" @@ -140,6 +143,43 @@ static void handle_adv_service_data(struct net_buf_simple *buf, #endif /* (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ +#if CONFIG_BLE_MESH_SUPPORT_BLE_SCAN +static bool ble_scan_en; + +int bt_mesh_start_ble_scan(struct bt_mesh_ble_scan_param *param) +{ + if (ble_scan_en == true) { + BT_WARN("%s, Already", __func__); + return -EALREADY; + } + + ble_scan_en = true; + + return 0; +} + +int bt_mesh_stop_ble_scan(void) +{ + if (ble_scan_en == false) { + BT_WARN("%s, Already", __func__); + return -EALREADY; + } + + ble_scan_en = false; + + return 0; +} + +static void inline callback_ble_adv_pkt(const bt_mesh_addr_t *addr, + uint8_t adv_type, uint8_t data[], + uint16_t length, int8_t rssi) +{ + if (ble_scan_en) { + bt_mesh_ble_scan_cb_evt_to_btc(addr, adv_type, data, length, rssi); + } +} +#endif /* CONFIG_BLE_MESH_SUPPORT_BLE_SCAN */ + static void bt_mesh_scan_cb(const bt_mesh_addr_t *addr, int8_t rssi, uint8_t adv_type, struct net_buf_simple *buf) @@ -148,8 +188,15 @@ static void bt_mesh_scan_cb(const bt_mesh_addr_t *addr, CONFIG_BLE_MESH_GATT_PROXY_CLIENT uint16_t uuid = 0U; #endif +#if CONFIG_BLE_MESH_SUPPORT_BLE_SCAN + uint8_t *adv_data = buf->data; + uint16_t adv_len = buf->len; +#endif if (adv_type != BLE_MESH_ADV_NONCONN_IND && adv_type != BLE_MESH_ADV_IND) { +#if CONFIG_BLE_MESH_SUPPORT_BLE_SCAN + callback_ble_adv_pkt(addr, adv_type, adv_data, adv_len, rssi); +#endif return; } @@ -166,11 +213,17 @@ static void bt_mesh_scan_cb(const bt_mesh_addr_t *addr, len = net_buf_simple_pull_u8(buf); /* Check for early termination */ if (len == 0U) { +#if CONFIG_BLE_MESH_SUPPORT_BLE_SCAN + callback_ble_adv_pkt(addr, adv_type, adv_data, adv_len, rssi); +#endif return; } if (len > buf->len) { - BT_WARN("AD malformed"); + BT_DBG("AD malformed"); +#if CONFIG_BLE_MESH_SUPPORT_BLE_SCAN + callback_ble_adv_pkt(addr, adv_type, adv_data, adv_len, rssi); +#endif return; } @@ -211,12 +264,18 @@ static void bt_mesh_scan_cb(const bt_mesh_addr_t *addr, case BLE_MESH_DATA_FLAGS: if (!adv_flags_valid(buf)) { BT_DBG("Adv Flags mismatch, ignore this adv pkt"); +#if CONFIG_BLE_MESH_SUPPORT_BLE_SCAN + callback_ble_adv_pkt(addr, adv_type, adv_data, adv_len, rssi); +#endif return; } break; case BLE_MESH_DATA_UUID16_ALL: if (!adv_service_uuid_valid(buf, &uuid)) { BT_DBG("Adv Service UUID mismatch, ignore this adv pkt"); +#if CONFIG_BLE_MESH_SUPPORT_BLE_SCAN + callback_ble_adv_pkt(addr, adv_type, adv_data, adv_len, rssi); +#endif return; } break; @@ -225,7 +284,10 @@ static void bt_mesh_scan_cb(const bt_mesh_addr_t *addr, break; #endif default: - break; +#if CONFIG_BLE_MESH_SUPPORT_BLE_SCAN + callback_ble_adv_pkt(addr, adv_type, adv_data, adv_len, rssi); +#endif + return; } net_buf_simple_restore(buf, &state); diff --git a/components/bt/esp_ble_mesh/mesh_core/scan.h b/components/bt/esp_ble_mesh/mesh_core/scan.h index a3b37b1bdc..229c83c341 100644 --- a/components/bt/esp_ble_mesh/mesh_core/scan.h +++ b/components/bt/esp_ble_mesh/mesh_core/scan.h @@ -24,6 +24,14 @@ int bt_mesh_scan_disable(void); int bt_mesh_scan_with_wl_enable(void); +struct bt_mesh_ble_scan_param { + uint32_t duration; +}; + +int bt_mesh_start_ble_scan(struct bt_mesh_ble_scan_param *param); + +int bt_mesh_stop_ble_scan(void); + #ifdef __cplusplus } #endif From 024017e3f269731825365506a7e0fa96d1f8c2d9 Mon Sep 17 00:00:00 2001 From: lly Date: Mon, 26 Oct 2020 16:30:16 +0800 Subject: [PATCH 5/5] ble_mesh: ci: Add ble & ble mesh coex sdkconfig test files --- .../fast_prov_server/sdkconfig.ci.adv_scan | 17 +++++++++++++++++ .../ble_mesh_node/onoff_server/sdkconfig.ci.adv | 16 ++++++++++++++++ .../onoff_server/sdkconfig.ci.scan | 16 ++++++++++++++++ .../ble_mesh_provisioner/sdkconfig.ci.adv | 16 ++++++++++++++++ .../ble_mesh_provisioner/sdkconfig.ci.scan | 16 ++++++++++++++++ 5 files changed, 81 insertions(+) create mode 100644 examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/fast_prov_server/sdkconfig.ci.adv_scan create mode 100644 examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server/sdkconfig.ci.adv create mode 100644 examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server/sdkconfig.ci.scan create mode 100644 examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/sdkconfig.ci.adv create mode 100644 examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/sdkconfig.ci.scan diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/fast_prov_server/sdkconfig.ci.adv_scan b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/fast_prov_server/sdkconfig.ci.adv_scan new file mode 100644 index 0000000000..83867c5ed1 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/fast_prov_server/sdkconfig.ci.adv_scan @@ -0,0 +1,17 @@ +CONFIG_BT_ENABLED=y +CONFIG_BTDM_CTRL_MODE_BLE_ONLY=y +CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=n +CONFIG_BTDM_CTRL_MODE_BTDM=n +CONFIG_BTDM_MODEM_SLEEP=n +CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BTDM_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 + +CONFIG_BLE_MESH=y +CONFIG_BLE_MESH_SUPPORT_BLE_ADV=y +CONFIG_BLE_MESH_SUPPORT_BLE_SCAN=y +CONFIG_BLE_MESH_FAST_PROV=y +CONFIG_BLE_MESH_PB_GATT=y +CONFIG_BLE_MESH_SETTINGS=y +CONFIG_BLE_MESH_CFG_CLI=y diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server/sdkconfig.ci.adv b/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server/sdkconfig.ci.adv new file mode 100644 index 0000000000..3e13ad16d7 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server/sdkconfig.ci.adv @@ -0,0 +1,16 @@ +CONFIG_BT_ENABLED=y +CONFIG_BTDM_CTRL_MODE_BLE_ONLY=y +CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=n +CONFIG_BTDM_CTRL_MODE_BTDM=n +CONFIG_BTDM_MODEM_SLEEP=n +CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BTDM_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 + +CONFIG_BLE_MESH=y +CONFIG_BLE_MESH_SUPPORT_BLE_ADV=y +CONFIG_BLE_MESH_NODE=y +CONFIG_BLE_MESH_PB_GATT=y +CONFIG_BLE_MESH_FRIEND=y +CONFIG_BLE_MESH_SETTINGS=y diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server/sdkconfig.ci.scan b/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server/sdkconfig.ci.scan new file mode 100644 index 0000000000..e300e76fca --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server/sdkconfig.ci.scan @@ -0,0 +1,16 @@ +CONFIG_BT_ENABLED=y +CONFIG_BTDM_CTRL_MODE_BLE_ONLY=y +CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=n +CONFIG_BTDM_CTRL_MODE_BTDM=n +CONFIG_BTDM_MODEM_SLEEP=n +CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BTDM_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 + +CONFIG_BLE_MESH=y +CONFIG_BLE_MESH_SUPPORT_BLE_SCAN=y +CONFIG_BLE_MESH_NODE=y +CONFIG_BLE_MESH_PB_GATT=y +CONFIG_BLE_MESH_FRIEND=y +CONFIG_BLE_MESH_SETTINGS=y diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/sdkconfig.ci.adv b/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/sdkconfig.ci.adv new file mode 100644 index 0000000000..19bdb75b02 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/sdkconfig.ci.adv @@ -0,0 +1,16 @@ +CONFIG_BT_ENABLED=y +CONFIG_BTDM_CTRL_MODE_BLE_ONLY=y +CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=n +CONFIG_BTDM_CTRL_MODE_BTDM=n +CONFIG_BTDM_MODEM_SLEEP=n +CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BTDM_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 + +CONFIG_BLE_MESH=y +CONFIG_BLE_MESH_SUPPORT_BLE_ADV=y +CONFIG_BLE_MESH_PROVISIONER=y +CONFIG_BLE_MESH_PB_GATT=y +CONFIG_BLE_MESH_SETTINGS=y +CONFIG_BLE_MESH_CFG_CLI=y +CONFIG_BLE_MESH_GENERIC_ONOFF_CLI=y diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/sdkconfig.ci.scan b/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/sdkconfig.ci.scan new file mode 100644 index 0000000000..48a4d1e9b5 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/sdkconfig.ci.scan @@ -0,0 +1,16 @@ +CONFIG_BT_ENABLED=y +CONFIG_BTDM_CTRL_MODE_BLE_ONLY=y +CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=n +CONFIG_BTDM_CTRL_MODE_BTDM=n +CONFIG_BTDM_MODEM_SLEEP=n +CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE=y +CONFIG_BTDM_BLE_MESH_SCAN_DUPL_EN=y +CONFIG_BT_BTU_TASK_STACK_SIZE=4512 + +CONFIG_BLE_MESH=y +CONFIG_BLE_MESH_SUPPORT_BLE_SCAN=y +CONFIG_BLE_MESH_PROVISIONER=y +CONFIG_BLE_MESH_PB_GATT=y +CONFIG_BLE_MESH_SETTINGS=y +CONFIG_BLE_MESH_CFG_CLI=y +CONFIG_BLE_MESH_GENERIC_ONOFF_CLI=y