Merge branch 'bugfix/ble_mesh_add_misc_check_v4.0' into 'release/v4.0'

Bugfix/ble mesh add misc check (v4.0)

See merge request espressif/esp-idf!9631
This commit is contained in:
Island 2020-07-16 10:51:49 +08:00
commit 775fd14310
10 changed files with 142 additions and 92 deletions

View file

@ -35,11 +35,29 @@ bool esp_ble_mesh_node_is_provisioned(void)
return bt_mesh_is_provisioned();
}
static bool prov_bearers_valid(esp_ble_mesh_prov_bearer_t bearers)
{
if ((!(bearers & (ESP_BLE_MESH_PROV_ADV | ESP_BLE_MESH_PROV_GATT))) ||
(IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
!IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
!(bearers & ESP_BLE_MESH_PROV_ADV)) ||
(!IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
!(bearers & ESP_BLE_MESH_PROV_GATT))) {
return false;
}
return true;
}
esp_err_t esp_ble_mesh_node_prov_enable(esp_ble_mesh_prov_bearer_t bearers)
{
btc_ble_mesh_prov_args_t arg = {0};
btc_msg_t msg = {0};
if (prov_bearers_valid(bearers) == false) {
return ESP_ERR_INVALID_ARG;
}
ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
msg.sig = BTC_SIG_API_CALL;
@ -56,6 +74,10 @@ esp_err_t esp_ble_mesh_node_prov_disable(esp_ble_mesh_prov_bearer_t bearers)
btc_ble_mesh_prov_args_t arg = {0};
btc_msg_t msg = {0};
if (prov_bearers_valid(bearers) == false) {
return ESP_ERR_INVALID_ARG;
}
ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
msg.sig = BTC_SIG_API_CALL;
@ -232,6 +254,10 @@ esp_err_t esp_ble_mesh_provisioner_prov_enable(esp_ble_mesh_prov_bearer_t bearer
btc_ble_mesh_prov_args_t arg = {0};
btc_msg_t msg = {0};
if (prov_bearers_valid(bearers) == false) {
return ESP_ERR_INVALID_ARG;
}
ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
msg.sig = BTC_SIG_API_CALL;
@ -249,6 +275,10 @@ esp_err_t esp_ble_mesh_provisioner_prov_disable(esp_ble_mesh_prov_bearer_t beare
btc_ble_mesh_prov_args_t arg = {0};
btc_msg_t msg = {0};
if (prov_bearers_valid(bearers) == false) {
return ESP_ERR_INVALID_ARG;
}
ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
msg.sig = BTC_SIG_API_CALL;

View file

@ -971,7 +971,7 @@ int bt_mesh_scan_with_wl_enable(void)
BT_DBG("%s", __func__);
err = bt_le_scan_start(&scan_param, bt_mesh_scan_cb);
if (err) {
if (err && err != -EALREADY) {
BT_ERR("starting scan failed (err %d)", err);
return err;
}

View file

@ -458,11 +458,10 @@ int bt_le_scan_start(const struct bt_mesh_scan_param *param, bt_mesh_scan_cb_t c
{
int err = 0;
#if BLE_MESH_DEV
if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
BT_INFO("Scan is already started");
return -EALREADY;
}
#endif
if (!valid_scan_param(param)) {
return -EINVAL;
@ -481,26 +480,24 @@ int bt_le_scan_start(const struct bt_mesh_scan_param *param, bt_mesh_scan_cb_t c
return err;
}
#if BLE_MESH_DEV
bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
#endif
bt_mesh_scan_dev_found_cb = cb;
return err;
return 0;
}
int bt_le_scan_stop(void)
{
#if BLE_MESH_DEV
if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
BLE_MESH_BTM_CHECK_STATUS(BTM_BleScan(false, 0, NULL, NULL, NULL));
if (!bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
BT_INFO("Scan is already stopped");
return -EALREADY;
}
#else
BLE_MESH_BTM_CHECK_STATUS(BTM_BleScan(false, 0, NULL, NULL, NULL));
#endif
BLE_MESH_BTM_CHECK_STATUS(BTM_BleScan(false, 0, NULL, NULL, NULL));
bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
bt_mesh_scan_dev_found_cb = NULL;
return 0;
}
@ -1197,13 +1194,10 @@ int bt_mesh_gattc_conn_create(const bt_mesh_addr_t *addr, u16_t service_uuid)
return -ENOMEM;
}
#if BLE_MESH_DEV
if (bt_mesh_atomic_test_and_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
BLE_MESH_BTM_CHECK_STATUS(BTM_BleScan(false, 0, NULL, NULL, NULL));
bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
}
#else
BLE_MESH_BTM_CHECK_STATUS(BTM_BleScan(false, 0, NULL, NULL, NULL));
#endif /* BLE_MESH_DEV */
BT_DBG("%s, create conn with %s", __func__, bt_hex(addr->val, BLE_MESH_ADDR_LEN));
@ -1607,30 +1601,20 @@ static void bt_mesh_bta_gattc_cb(tBTA_GATTC_EVT event, tBTA_GATTC *p_data)
break;
case BTA_GATTC_EXEC_EVT:
break;
case BTA_GATTC_OPEN_EVT: {
case BTA_GATTC_OPEN_EVT:
BT_DBG("BTA_GATTC_OPEN_EVT");
/** After current connection is established, provisioner can
* use BTA_DmBleScan() to re-enable scan.
/* After current connection is established, Provisioner can
* use BTM_BleScan() to re-enable scan.
*/
tBTM_STATUS status;
#if BLE_MESH_DEV
if (!bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
status = BTM_BleScan(true, 0, bt_mesh_scan_results_cb, NULL, NULL);
tBTM_STATUS status = BTM_BleScan(true, 0, bt_mesh_scan_results_cb, NULL, NULL);
if (status != BTM_SUCCESS && status != BTM_CMD_STARTED) {
BT_ERR("%s, Invalid status %d", __func__, status);
BT_ERR("%s, Invalid scan status %d", __func__, status);
break;
}
bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
}
#else
status = BTM_BleScan(true, 0, bt_mesh_scan_results_cb, NULL, NULL);
if (status != BTM_SUCCESS && status != BTM_CMD_STARTED) {
BT_ERR("%s, Invalid status %d", __func__, status);
break;
}
#endif /* BLE_MESH_DEV */
break;
}
case BTA_GATTC_CLOSE_EVT:
BT_DBG("BTA_GATTC_CLOSE_EVT");
break;

View file

@ -81,9 +81,6 @@ int bt_mesh_provision(const u8_t net_key[16], u16_t net_idx,
bt_mesh_store_iv(false);
}
/* Add this to avoid "already active status" for bt_mesh_scan_enable() */
bt_mesh_scan_disable();
bt_mesh_net_start();
return 0;
@ -171,12 +168,32 @@ bool bt_mesh_is_provisioner_en(void)
}
}
static bool prov_bearers_valid(bt_mesh_prov_bearer_t bearers)
{
if ((!(bearers & (BLE_MESH_PROV_ADV | BLE_MESH_PROV_GATT))) ||
(IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
!IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
!(bearers & BLE_MESH_PROV_ADV)) ||
(!IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
!(bearers & BLE_MESH_PROV_GATT))) {
BT_ERR("Invalid bearers 0x%02x", bearers);
return false;
}
return true;
}
int bt_mesh_prov_enable(bt_mesh_prov_bearer_t bearers)
{
if (bt_mesh_is_provisioned()) {
BT_WARN("%s, Already", __func__);
return -EALREADY;
}
if (prov_bearers_valid(bearers) == false) {
return -EINVAL;
}
bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_NODE);
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
@ -206,6 +223,10 @@ int bt_mesh_prov_disable(bt_mesh_prov_bearer_t bearers)
return -EALREADY;
}
if (prov_bearers_valid(bearers) == false) {
return -EINVAL;
}
bt_mesh_atomic_clear_bit(bt_mesh.flags, BLE_MESH_NODE);
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
@ -509,13 +530,6 @@ int bt_mesh_provisioner_net_start(bt_mesh_prov_bearer_t bearers)
}
#endif
if ((IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) &&
(bearers & BLE_MESH_PROV_ADV)) ||
(IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
(bearers & BLE_MESH_PROV_GATT))) {
bt_mesh_scan_enable();
}
if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) &&
(bearers & BLE_MESH_PROV_GATT)) {
bt_mesh_provisioner_pb_gatt_enable();
@ -523,13 +537,15 @@ int bt_mesh_provisioner_net_start(bt_mesh_prov_bearer_t bearers)
bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_VALID_PROV);
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
bt_mesh_friend_init();
}
if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) {
bt_mesh_beacon_enable();
}
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
bt_mesh_friend_init();
}
bt_mesh_scan_enable();
return 0;
}
@ -543,6 +559,10 @@ int bt_mesh_provisioner_enable(bt_mesh_prov_bearer_t bearers)
return -EALREADY;
}
if (prov_bearers_valid(bearers) == false) {
return -EINVAL;
}
err = bt_mesh_provisioner_set_prov_info();
if (err) {
BT_ERR("%s, Failed to set provisioning info", __func__);
@ -573,9 +593,13 @@ int bt_mesh_provisioner_disable(bt_mesh_prov_bearer_t bearers)
return -EALREADY;
}
if (prov_bearers_valid(bearers) == false) {
return -EINVAL;
}
enable = bt_mesh_provisioner_get_prov_bearer();
if (!(enable & bearers)) {
BT_ERR("%s, Bearers mismatch", __func__);
BT_ERR("Mismatch bearers 0x%02x", bearers);
return -EINVAL;
}

View file

@ -418,22 +418,14 @@ static int disc_cb(struct ble_gap_event *event, void *arg)
}
}
}
#if BLE_MESH_DEV
if (!bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
rc = ble_gap_disc(BLE_OWN_ADDR_PUBLIC, BLE_HS_FOREVER, &scan_param, disc_cb, NULL);
if (rc != 0) {
BT_ERR("%s, Invalid status %d", __func__, rc);
BT_ERR("%s, Invalid scan status %d", __func__, rc);
break;
}
bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
}
#else
rc = ble_gap_disc(BLE_OWN_ADDR_PUBLIC, BLE_HS_FOREVER, &scan_param, disc_cb, NULL);
if (rc != 0) {
BT_ERR("%s, Invalid status %d", __func__, rc);
break;
}
#endif /* BLE_MESH_DEV */
break;
case BLE_GAP_EVENT_DISCONNECT:
if (bt_mesh_gattc_conn_cb != NULL && bt_mesh_gattc_conn_cb->disconnected != NULL) {
@ -937,11 +929,10 @@ int bt_le_scan_start(const struct bt_mesh_scan_param *param, bt_mesh_scan_cb_t c
{
int err;
#if BLE_MESH_DEV
if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
BT_INFO("Scan is already started");
return -EALREADY;
}
#endif
#if BLE_MESH_DEV
if (param->filter_dup) {
@ -956,26 +947,24 @@ int bt_le_scan_start(const struct bt_mesh_scan_param *param, bt_mesh_scan_cb_t c
return err;
}
#if BLE_MESH_DEV
bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
#endif
bt_mesh_scan_dev_found_cb = cb;
return err;
return 0;
}
int bt_le_scan_stop(void)
{
#if BLE_MESH_DEV
if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
ble_gap_disc_cancel();
if (!bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
BT_INFO("Scan is already stopped");
return -EALREADY;
}
#else
ble_gap_disc_cancel();
#endif
ble_gap_disc_cancel();
bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
bt_mesh_scan_dev_found_cb = NULL;
return 0;
}
@ -1419,19 +1408,13 @@ int bt_mesh_gattc_conn_create(const bt_mesh_addr_t *addr, u16_t service_uuid)
return -ENOMEM;
}
#if BLE_MESH_DEV
if (bt_mesh_atomic_test_and_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
rc = ble_gap_disc_cancel();
if (rc != 0) {
return -1;
}
bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
}
#else
rc = ble_gap_disc_cancel();
if (rc != 0) {
return -1;
}
#endif /* BLE_MESH_DEV */
BT_DBG("%s, create conn with %s", __func__, bt_hex(addr->val, BLE_MESH_ADDR_LEN));

View file

@ -154,6 +154,11 @@ static void proxy_cfg(struct bt_mesh_proxy_server *server)
u8_t opcode = 0U;
int err = 0;
if (server->buf.len > 29) {
BT_ERR("Too large proxy cfg pdu (len %d)", server->buf.len);
return;
}
err = bt_mesh_net_decode(&server->buf, BLE_MESH_NET_IF_PROXY_CFG,
&rx, &buf);
if (err) {

View file

@ -292,6 +292,11 @@ static void proxy_cfg(struct bt_mesh_proxy_client *client)
u8_t opcode = 0U;
int err = 0;
if (client->buf.len > 29) {
BT_ERR("Too large proxy cfg pdu (len %d)", client->buf.len);
return;
}
err = bt_mesh_net_decode(&client->buf, BLE_MESH_NET_IF_PROXY_CFG,
&rx, &buf);
if (err) {

View file

@ -272,12 +272,16 @@ static esp_err_t ble_mesh_init(void)
esp_ble_mesh_register_config_server_callback(example_ble_mesh_config_server_cb);
err = esp_ble_mesh_init(&provision, &composition);
if (err) {
ESP_LOGE(TAG, "Initializing mesh failed (err %d)", err);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize mesh stack (err %d)", err);
return err;
}
esp_ble_mesh_node_prov_enable(ESP_BLE_MESH_PROV_ADV | ESP_BLE_MESH_PROV_GATT);
err = esp_ble_mesh_node_prov_enable(ESP_BLE_MESH_PROV_ADV | ESP_BLE_MESH_PROV_GATT);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to enable mesh node (err %d)", err);
return err;
}
ESP_LOGI(TAG, "BLE Mesh Node initialized");

View file

@ -282,19 +282,23 @@ static void example_ble_mesh_config_server_cb(esp_ble_mesh_cfg_server_cb_event_t
static esp_err_t ble_mesh_init(void)
{
esp_err_t err;
esp_err_t err = ESP_OK;
esp_ble_mesh_register_prov_callback(example_ble_mesh_provisioning_cb);
esp_ble_mesh_register_config_server_callback(example_ble_mesh_config_server_cb);
esp_ble_mesh_register_generic_server_callback(example_ble_mesh_generic_server_cb);
err = esp_ble_mesh_init(&provision, &composition);
if (err) {
ESP_LOGE(TAG, "Initializing mesh failed (err %d)", err);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize mesh stack (err %d)", err);
return err;
}
esp_ble_mesh_node_prov_enable(ESP_BLE_MESH_PROV_ADV | ESP_BLE_MESH_PROV_GATT);
err = esp_ble_mesh_node_prov_enable(ESP_BLE_MESH_PROV_ADV | ESP_BLE_MESH_PROV_GATT);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to enable mesh node (err %d)", err);
return err;
}
ESP_LOGI(TAG, "BLE Mesh Node initialized");

View file

@ -584,7 +584,7 @@ static void example_ble_mesh_generic_client_cb(esp_ble_mesh_generic_client_cb_ev
static esp_err_t ble_mesh_init(void)
{
uint8_t match[2] = {0xdd, 0xdd};
esp_err_t err;
esp_err_t err = ESP_OK;
prov_key.net_idx = ESP_BLE_MESH_KEY_PRIMARY;
prov_key.app_idx = APP_KEY_IDX;
@ -594,18 +594,29 @@ static esp_err_t ble_mesh_init(void)
esp_ble_mesh_register_config_client_callback(example_ble_mesh_config_client_cb);
esp_ble_mesh_register_generic_client_callback(example_ble_mesh_generic_client_cb);
err = esp_ble_mesh_init(&provision, &composition);
if (err) {
ESP_LOGE(TAG, "Initializing mesh failed (err %d)", err);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize mesh stack (err %d)", err);
return err;
}
esp_ble_mesh_provisioner_set_dev_uuid_match(match, sizeof(match), 0x0, false);
err = esp_ble_mesh_provisioner_set_dev_uuid_match(match, sizeof(match), 0x0, false);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to set matching device uuid (err %d)", err);
return err;
}
esp_ble_mesh_provisioner_prov_enable(ESP_BLE_MESH_PROV_ADV | ESP_BLE_MESH_PROV_GATT);
err = esp_ble_mesh_provisioner_prov_enable(ESP_BLE_MESH_PROV_ADV | ESP_BLE_MESH_PROV_GATT);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to enable mesh provisioner (err %d)", err);
return err;
}
esp_ble_mesh_provisioner_add_local_app_key(prov_key.app_key, prov_key.net_idx, prov_key.app_idx);
err = esp_ble_mesh_provisioner_add_local_app_key(prov_key.app_key, prov_key.net_idx, prov_key.app_idx);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to add local AppKey (err %d)", err);
return err;
}
ESP_LOGI(TAG, "BLE Mesh Provisioner initialized");