From f2ae5f6e32fb7b9c50eb902751d4716f03500718 Mon Sep 17 00:00:00 2001 From: lly Date: Tue, 26 May 2020 17:33:46 +0800 Subject: [PATCH 01/12] ble_mesh: ci: Enable settings in sdkconfig.ci.xxx --- .../fast_prov_client/sdkconfig.ci.bluedroid | 3 ++- .../fast_prov_client/sdkconfig.ci.nimble | 3 ++- .../fast_prov_client/sdkconfig.defaults | 2 +- .../fast_prov_server/sdkconfig.ci.bluedroid | 3 ++- .../fast_prov_server/sdkconfig.ci.nimble | 3 ++- .../fast_prov_server/sdkconfig.defaults | 2 +- .../ble_mesh_node/onoff_client/sdkconfig.ci.bluedroid | 1 + .../ble_mesh_node/onoff_client/sdkconfig.ci.nimble | 1 + .../ble_mesh_node/onoff_server/sdkconfig.ci.bluedroid | 1 + .../ble_mesh_node/onoff_server/sdkconfig.ci.nimble | 1 + .../esp_ble_mesh/ble_mesh_provisioner/sdkconfig.ci.bluedroid | 1 + .../esp_ble_mesh/ble_mesh_provisioner/sdkconfig.ci.nimble | 1 + .../ble_mesh_sensor_model/sensor_client/sdkconfig.ci.bluedroid | 1 + .../ble_mesh_sensor_model/sensor_client/sdkconfig.ci.nimble | 1 + .../ble_mesh_sensor_model/sensor_server/sdkconfig.ci.bluedroid | 1 + .../ble_mesh_sensor_model/sensor_server/sdkconfig.ci.nimble | 1 + .../ble_mesh_vendor_model/vendor_client/sdkconfig.ci.bluedroid | 3 ++- .../ble_mesh_vendor_model/vendor_client/sdkconfig.ci.nimble | 3 ++- .../ble_mesh_vendor_model/vendor_server/sdkconfig.ci.bluedroid | 3 ++- .../ble_mesh_vendor_model/vendor_server/sdkconfig.ci.nimble | 3 ++- 20 files changed, 28 insertions(+), 10 deletions(-) diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/fast_prov_client/sdkconfig.ci.bluedroid b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/fast_prov_client/sdkconfig.ci.bluedroid index 24fdd23d36..0df6818d5b 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/fast_prov_client/sdkconfig.ci.bluedroid +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/fast_prov_client/sdkconfig.ci.bluedroid @@ -10,5 +10,6 @@ CONFIG_BT_BTU_TASK_STACK_SIZE=4512 CONFIG_BLE_MESH=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 \ No newline at end of file +CONFIG_BLE_MESH_GENERIC_ONOFF_CLI=y diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/fast_prov_client/sdkconfig.ci.nimble b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/fast_prov_client/sdkconfig.ci.nimble index 6b9179b431..9fd415dc2e 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/fast_prov_client/sdkconfig.ci.nimble +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/fast_prov_client/sdkconfig.ci.nimble @@ -10,5 +10,6 @@ CONFIG_BT_NIMBLE_ENABLED=y CONFIG_BLE_MESH=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 \ No newline at end of file +CONFIG_BLE_MESH_GENERIC_ONOFF_CLI=y diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/fast_prov_client/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/fast_prov_client/sdkconfig.defaults index 0e430712d5..7996f647c9 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/fast_prov_client/sdkconfig.defaults +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/fast_prov_client/sdkconfig.defaults @@ -18,4 +18,4 @@ CONFIG_BLE_MESH_ADV_BUF_COUNT=100 CONFIG_BLE_MESH_TX_SEG_MSG_COUNT=10 CONFIG_BLE_MESH_RX_SEG_MSG_COUNT=10 CONFIG_BLE_MESH_CFG_CLI=y -CONFIG_BLE_MESH_GENERIC_ONOFF_CLI=y \ No newline at end of file +CONFIG_BLE_MESH_GENERIC_ONOFF_CLI=y diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/fast_prov_server/sdkconfig.ci.bluedroid b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/fast_prov_server/sdkconfig.ci.bluedroid index 8b41d90417..26fe2b783e 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/fast_prov_server/sdkconfig.ci.bluedroid +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/fast_prov_server/sdkconfig.ci.bluedroid @@ -11,4 +11,5 @@ CONFIG_BT_BTU_TASK_STACK_SIZE=4512 CONFIG_BLE_MESH=y CONFIG_BLE_MESH_FAST_PROV=y CONFIG_BLE_MESH_PB_GATT=y -CONFIG_BLE_MESH_CFG_CLI=y \ No newline at end of file +CONFIG_BLE_MESH_SETTINGS=y +CONFIG_BLE_MESH_CFG_CLI=y diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/fast_prov_server/sdkconfig.ci.nimble b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/fast_prov_server/sdkconfig.ci.nimble index f4e032046c..93a3519d93 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/fast_prov_server/sdkconfig.ci.nimble +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/fast_prov_server/sdkconfig.ci.nimble @@ -10,4 +10,5 @@ CONFIG_BT_NIMBLE_ENABLED=y CONFIG_BLE_MESH=y CONFIG_BLE_MESH_FAST_PROV=y CONFIG_BLE_MESH_PB_GATT=y -CONFIG_BLE_MESH_CFG_CLI=y \ No newline at end of file +CONFIG_BLE_MESH_SETTINGS=y +CONFIG_BLE_MESH_CFG_CLI=y diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/fast_prov_server/sdkconfig.defaults b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/fast_prov_server/sdkconfig.defaults index a2a2abb544..476fe84e1c 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/fast_prov_server/sdkconfig.defaults +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_fast_provision/fast_prov_server/sdkconfig.defaults @@ -22,4 +22,4 @@ CONFIG_BLE_MESH_ADV_BUF_COUNT=200 CONFIG_BLE_MESH_TX_SEG_MSG_COUNT=10 CONFIG_BLE_MESH_RX_SEG_MSG_COUNT=10 CONFIG_BLE_MESH_TRACE_LEVEL_ERROR=y -CONFIG_BLE_MESH_CFG_CLI=y \ No newline at end of file +CONFIG_BLE_MESH_CFG_CLI=y diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_client/sdkconfig.ci.bluedroid b/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_client/sdkconfig.ci.bluedroid index 27b31a65d7..7c177948b7 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_client/sdkconfig.ci.bluedroid +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_client/sdkconfig.ci.bluedroid @@ -13,4 +13,5 @@ CONFIG_BT_BTU_TASK_STACK_SIZE=4512 CONFIG_BLE_MESH=y CONFIG_BLE_MESH_NODE=y CONFIG_BLE_MESH_PB_GATT=y +CONFIG_BLE_MESH_SETTINGS=y CONFIG_BLE_MESH_GENERIC_ONOFF_CLI=y diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_client/sdkconfig.ci.nimble b/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_client/sdkconfig.ci.nimble index 3fb117cc61..03052e9836 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_client/sdkconfig.ci.nimble +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_client/sdkconfig.ci.nimble @@ -10,4 +10,5 @@ CONFIG_BT_NIMBLE_ENABLED=y CONFIG_BLE_MESH=y CONFIG_BLE_MESH_NODE=y CONFIG_BLE_MESH_PB_GATT=y +CONFIG_BLE_MESH_SETTINGS=y CONFIG_BLE_MESH_GENERIC_ONOFF_CLI=y diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server/sdkconfig.ci.bluedroid b/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server/sdkconfig.ci.bluedroid index ed83641136..bafeda3a89 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server/sdkconfig.ci.bluedroid +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server/sdkconfig.ci.bluedroid @@ -12,3 +12,4 @@ CONFIG_BLE_MESH=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.nimble b/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server/sdkconfig.ci.nimble index 9ccbf71f0b..479825abca 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server/sdkconfig.ci.nimble +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server/sdkconfig.ci.nimble @@ -11,3 +11,4 @@ CONFIG_BLE_MESH=y CONFIG_BLE_MESH_NODE=y CONFIG_BLE_MESH_PB_GATT=y CONFIG_BLE_MESH_LOW_POWER=y +CONFIG_BLE_MESH_SETTINGS=y diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/sdkconfig.ci.bluedroid b/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/sdkconfig.ci.bluedroid index 843622f6ea..7823dee3f8 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/sdkconfig.ci.bluedroid +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/sdkconfig.ci.bluedroid @@ -12,5 +12,6 @@ CONFIG_BT_BTU_TASK_STACK_SIZE=4512 CONFIG_BLE_MESH=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.nimble b/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/sdkconfig.ci.nimble index 555bac7584..9fd415dc2e 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/sdkconfig.ci.nimble +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_provisioner/sdkconfig.ci.nimble @@ -10,5 +10,6 @@ CONFIG_BT_NIMBLE_ENABLED=y CONFIG_BLE_MESH=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_sensor_model/sensor_client/sdkconfig.ci.bluedroid b/examples/bluetooth/esp_ble_mesh/ble_mesh_sensor_model/sensor_client/sdkconfig.ci.bluedroid index 5ddfab8976..1801460441 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_sensor_model/sensor_client/sdkconfig.ci.bluedroid +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_sensor_model/sensor_client/sdkconfig.ci.bluedroid @@ -10,5 +10,6 @@ CONFIG_BT_BTU_TASK_STACK_SIZE=4512 CONFIG_BLE_MESH=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_SENSOR_CLI=y diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_sensor_model/sensor_client/sdkconfig.ci.nimble b/examples/bluetooth/esp_ble_mesh/ble_mesh_sensor_model/sensor_client/sdkconfig.ci.nimble index 5fed736d39..0655ea038d 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_sensor_model/sensor_client/sdkconfig.ci.nimble +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_sensor_model/sensor_client/sdkconfig.ci.nimble @@ -10,5 +10,6 @@ CONFIG_BT_NIMBLE_ENABLED=y CONFIG_BLE_MESH=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_SENSOR_CLI=y diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_sensor_model/sensor_server/sdkconfig.ci.bluedroid b/examples/bluetooth/esp_ble_mesh/ble_mesh_sensor_model/sensor_server/sdkconfig.ci.bluedroid index 2e83669941..4f51503284 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_sensor_model/sensor_server/sdkconfig.ci.bluedroid +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_sensor_model/sensor_server/sdkconfig.ci.bluedroid @@ -11,3 +11,4 @@ CONFIG_BT_BTU_TASK_STACK_SIZE=4512 CONFIG_BLE_MESH=y CONFIG_BLE_MESH_NODE=y CONFIG_BLE_MESH_PB_GATT=y +CONFIG_BLE_MESH_SETTINGS=y diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_sensor_model/sensor_server/sdkconfig.ci.nimble b/examples/bluetooth/esp_ble_mesh/ble_mesh_sensor_model/sensor_server/sdkconfig.ci.nimble index a2c0bcc017..3eaeb9cafe 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_sensor_model/sensor_server/sdkconfig.ci.nimble +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_sensor_model/sensor_server/sdkconfig.ci.nimble @@ -10,3 +10,4 @@ CONFIG_BT_NIMBLE_ENABLED=y CONFIG_BLE_MESH=y CONFIG_BLE_MESH_NODE=y CONFIG_BLE_MESH_PB_GATT=y +CONFIG_BLE_MESH_SETTINGS=y diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_vendor_model/vendor_client/sdkconfig.ci.bluedroid b/examples/bluetooth/esp_ble_mesh/ble_mesh_vendor_model/vendor_client/sdkconfig.ci.bluedroid index 48977b32ae..a9595bda9e 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_vendor_model/vendor_client/sdkconfig.ci.bluedroid +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_vendor_model/vendor_client/sdkconfig.ci.bluedroid @@ -10,4 +10,5 @@ CONFIG_BT_BTU_TASK_STACK_SIZE=4512 CONFIG_BLE_MESH=y CONFIG_BLE_MESH_PROVISIONER=y CONFIG_BLE_MESH_PB_GATT=y -CONFIG_BLE_MESH_CFG_CLI=y \ No newline at end of file +CONFIG_BLE_MESH_SETTINGS=y +CONFIG_BLE_MESH_CFG_CLI=y diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_vendor_model/vendor_client/sdkconfig.ci.nimble b/examples/bluetooth/esp_ble_mesh/ble_mesh_vendor_model/vendor_client/sdkconfig.ci.nimble index 3e178ea9dd..cafb1b1fb9 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_vendor_model/vendor_client/sdkconfig.ci.nimble +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_vendor_model/vendor_client/sdkconfig.ci.nimble @@ -10,4 +10,5 @@ CONFIG_BT_NIMBLE_ENABLED=y CONFIG_BLE_MESH=y CONFIG_BLE_MESH_PROVISIONER=y CONFIG_BLE_MESH_PB_GATT=y -CONFIG_BLE_MESH_CFG_CLI=y \ No newline at end of file +CONFIG_BLE_MESH_SETTINGS=y +CONFIG_BLE_MESH_CFG_CLI=y diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_vendor_model/vendor_server/sdkconfig.ci.bluedroid b/examples/bluetooth/esp_ble_mesh/ble_mesh_vendor_model/vendor_server/sdkconfig.ci.bluedroid index 93bd203b49..4f51503284 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_vendor_model/vendor_server/sdkconfig.ci.bluedroid +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_vendor_model/vendor_server/sdkconfig.ci.bluedroid @@ -10,4 +10,5 @@ CONFIG_BT_BTU_TASK_STACK_SIZE=4512 CONFIG_BLE_MESH=y CONFIG_BLE_MESH_NODE=y -CONFIG_BLE_MESH_PB_GATT=y \ No newline at end of file +CONFIG_BLE_MESH_PB_GATT=y +CONFIG_BLE_MESH_SETTINGS=y diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_vendor_model/vendor_server/sdkconfig.ci.nimble b/examples/bluetooth/esp_ble_mesh/ble_mesh_vendor_model/vendor_server/sdkconfig.ci.nimble index 451ecc4ff6..3eaeb9cafe 100644 --- a/examples/bluetooth/esp_ble_mesh/ble_mesh_vendor_model/vendor_server/sdkconfig.ci.nimble +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_vendor_model/vendor_server/sdkconfig.ci.nimble @@ -9,4 +9,5 @@ CONFIG_BT_NIMBLE_ENABLED=y CONFIG_BLE_MESH=y CONFIG_BLE_MESH_NODE=y -CONFIG_BLE_MESH_PB_GATT=y \ No newline at end of file +CONFIG_BLE_MESH_PB_GATT=y +CONFIG_BLE_MESH_SETTINGS=y From ace471c266db0093e693d9ce03cab918cd07fcd7 Mon Sep 17 00:00:00 2001 From: lly Date: Tue, 26 May 2020 17:44:59 +0800 Subject: [PATCH 02/12] ble_mesh: stack: Define a type for mesh nvs handle --- .../mesh_core/storage/settings_nvs.c | 29 +++++++++---------- .../mesh_core/storage/settings_nvs.h | 3 ++ 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.c b/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.c index 7ea6343a87..3d6d4ef50f 100644 --- a/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.c +++ b/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.c @@ -15,9 +15,6 @@ #include #include -#include "nvs.h" -#include "nvs_flash.h" - #include "mesh_common.h" #include "settings_nvs.h" #include "settings.h" @@ -31,7 +28,7 @@ enum settings_type { struct settings_context { char *nvs_name; - nvs_handle handle; + bt_mesh_nvs_handle_t handle; int (*settings_init)(void); int (*settings_load)(void); @@ -123,14 +120,14 @@ void bt_mesh_settings_deforeach(void) /* API used to get BLE Mesh related nvs handle */ -static inline nvs_handle settings_get_nvs_handle(enum settings_type type) +static inline bt_mesh_nvs_handle_t settings_get_nvs_handle(enum settings_type type) { return settings_ctx[type].handle; } /* API used to store/erase BLE Mesh related settings */ -static int settings_save(nvs_handle handle, const char *key, const u8_t *val, size_t len) +static int settings_save(bt_mesh_nvs_handle_t handle, const char *key, const u8_t *val, size_t len) { int err = 0; @@ -167,13 +164,13 @@ static int settings_save(nvs_handle handle, const char *key, const u8_t *val, si int bt_mesh_save_core_settings(const char *key, const u8_t *val, size_t len) { - nvs_handle handle = settings_get_nvs_handle(SETTINGS_CORE); + bt_mesh_nvs_handle_t handle = settings_get_nvs_handle(SETTINGS_CORE); return settings_save(handle, key, val, len); } /* API used to load BLE Mesh related settings */ -static int settings_load(nvs_handle handle, const char *key, +static int settings_load(bt_mesh_nvs_handle_t handle, const char *key, u8_t *buf, size_t buf_len, bool *exist) { int err = 0; @@ -201,13 +198,13 @@ static int settings_load(nvs_handle handle, const char *key, int bt_mesh_load_core_settings(const char *key, u8_t *buf, size_t buf_len, bool *exist) { - nvs_handle handle = settings_get_nvs_handle(SETTINGS_CORE); + bt_mesh_nvs_handle_t handle = settings_get_nvs_handle(SETTINGS_CORE); return settings_load(handle, key, buf, buf_len, exist); } /* API used to get length of BLE Mesh related settings */ -static size_t settings_get_length(nvs_handle handle, const char *key) +static size_t settings_get_length(bt_mesh_nvs_handle_t handle, const char *key) { size_t len = 0U; int err = 0; @@ -233,7 +230,7 @@ static size_t settings_get_length(nvs_handle handle, const char *key) * Mesh settings. */ -static struct net_buf_simple *settings_get_item(nvs_handle handle, const char *key) +static struct net_buf_simple *settings_get_item(bt_mesh_nvs_handle_t handle, const char *key) { struct net_buf_simple *buf = NULL; size_t length = 0U; @@ -272,7 +269,7 @@ static struct net_buf_simple *settings_get_item(nvs_handle handle, const char *k struct net_buf_simple *bt_mesh_get_core_settings_item(const char *key) { - nvs_handle handle = settings_get_nvs_handle(SETTINGS_CORE); + bt_mesh_nvs_handle_t handle = settings_get_nvs_handle(SETTINGS_CORE); return settings_get_item(handle, key); } @@ -305,7 +302,7 @@ static bool is_settings_item_exist(struct net_buf_simple *buf, const u16_t val) /* API used to add the settings item */ -static int settings_add_item(nvs_handle handle, const char *key, const u16_t val) +static int settings_add_item(bt_mesh_nvs_handle_t handle, const char *key, const u16_t val) { struct net_buf_simple *store = NULL; struct net_buf_simple *buf = NULL; @@ -344,13 +341,13 @@ static int settings_add_item(nvs_handle handle, const char *key, const u16_t val int bt_mesh_add_core_settings_item(const char *key, const u16_t val) { - nvs_handle handle = settings_get_nvs_handle(SETTINGS_CORE); + bt_mesh_nvs_handle_t handle = settings_get_nvs_handle(SETTINGS_CORE); return settings_add_item(handle, key, val); } /* API used to remove the settings item */ -static int settings_remove_item(nvs_handle handle, const char *key, const u16_t val) +static int settings_remove_item(bt_mesh_nvs_handle_t handle, const char *key, const u16_t val) { struct net_buf_simple *store = NULL; struct net_buf_simple *buf = NULL; @@ -399,7 +396,7 @@ static int settings_remove_item(nvs_handle handle, const char *key, const u16_t int bt_mesh_remove_core_settings_item(const char *key, const u16_t val) { - nvs_handle handle = settings_get_nvs_handle(SETTINGS_CORE); + bt_mesh_nvs_handle_t handle = settings_get_nvs_handle(SETTINGS_CORE); return settings_remove_item(handle, key, val); } diff --git a/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.h b/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.h index 099efa54d8..948ad21ef5 100644 --- a/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.h +++ b/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.h @@ -15,12 +15,15 @@ #ifndef _BLE_MESH_SETTINGS_NVS_H_ #define _BLE_MESH_SETTINGS_NVS_H_ +#include "nvs_flash.h" #include "mesh_buf.h" #ifdef __cplusplus extern "C" { #endif +typedef nvs_handle_t bt_mesh_nvs_handle_t; + #define SETTINGS_ITEM_SIZE sizeof(u16_t) #define BLE_MESH_GET_ELEM_IDX(x) ((u8_t)((x) >> 8)) From 2bb65fac6056a79f15afb7dab7f84be290d990ba Mon Sep 17 00:00:00 2001 From: lly Date: Tue, 26 May 2020 18:17:41 +0800 Subject: [PATCH 03/12] ble_mesh: stack: Add role check before enabling device --- components/bt/esp_ble_mesh/mesh_core/main.c | 51 +++++++++++++++---- components/bt/esp_ble_mesh/mesh_core/mesh.h | 13 ++--- .../bt/esp_ble_mesh/mesh_core/settings.c | 11 ++-- .../bt/esp_ble_mesh/mesh_core/settings.h | 5 ++ 4 files changed, 59 insertions(+), 21 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/main.c b/components/bt/esp_ble_mesh/mesh_core/main.c index 9f55a26120..f755910c40 100644 --- a/components/bt/esp_ble_mesh/mesh_core/main.c +++ b/components/bt/esp_ble_mesh/mesh_core/main.c @@ -194,10 +194,27 @@ int bt_mesh_prov_enable(bt_mesh_prov_bearer_t bearers) return -EINVAL; } - bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_NODE); + /* Add this judgement here in case the device worked as a + * Provisioner previously. Before the corresponding info + * of Provisioner is erased from flash, users try to use + * the device as a node, which will cause the information + * in NVS been handled incorrectly. + */ + u8_t role = bt_mesh_atomic_get(bt_mesh.flags) & BLE_MESH_SETTINGS_ROLE_BIT_MASK; + if (role != BLE_MESH_SETTINGS_ROLE_NONE && + role != BLE_MESH_SETTINGS_ROLE_NODE) { + BT_ERR("%s, Mismatch role %u", __func__, role); + return -EIO; + } - if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { - bt_mesh_store_role(); + if (role == BLE_MESH_SETTINGS_ROLE_NONE) { + bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_NODE); + } + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS_BACKWARD_COMPATIBILITY) || + role == BLE_MESH_SETTINGS_ROLE_NONE) { + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_role(); + } } if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && @@ -220,6 +237,7 @@ int bt_mesh_prov_enable(bt_mesh_prov_bearer_t bearers) int bt_mesh_prov_disable(bt_mesh_prov_bearer_t bearers) { if (bt_mesh_is_provisioned()) { + BT_WARN("%s, Already provisioned", __func__); return -EALREADY; } @@ -527,6 +545,27 @@ int bt_mesh_provisioner_enable(bt_mesh_prov_bearer_t bearers) return -EINVAL; } + /* Add this judgement here in case the device worked as a + * node previously. Before the corresponding information + * of the node is erased from flash, users try to use the + * device as a Provisioner, which will cause the information + * in NVS been handled incorrectly. + */ + u8_t role = bt_mesh_atomic_get(bt_mesh.flags) & BLE_MESH_SETTINGS_ROLE_BIT_MASK; + if (role != BLE_MESH_SETTINGS_ROLE_NONE && + role != BLE_MESH_SETTINGS_ROLE_PROV) { + BT_ERR("%s, Mismatch role %u", __func__, role); + return -EIO; + } + + if (role == BLE_MESH_SETTINGS_ROLE_NONE) { + bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_PROVISIONER); + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_role(); + } + } + err = bt_mesh_provisioner_net_create(); if (err) { BT_ERR("Failed to create network"); @@ -543,12 +582,6 @@ int bt_mesh_provisioner_enable(bt_mesh_prov_bearer_t bearers) bt_mesh_comp_provision(bt_mesh_provisioner_get_primary_elem_addr()); - bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_PROVISIONER); - - if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { - bt_mesh_store_role(); - } - #if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && (bearers & BLE_MESH_PROV_ADV)) { diff --git a/components/bt/esp_ble_mesh/mesh_core/mesh.h b/components/bt/esp_ble_mesh/mesh_core/mesh.h index 5a409a891b..8387470a44 100644 --- a/components/bt/esp_ble_mesh/mesh_core/mesh.h +++ b/components/bt/esp_ble_mesh/mesh_core/mesh.h @@ -2,6 +2,7 @@ /* * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2020 Espressif Systems (Shanghai) PTE LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -15,13 +16,13 @@ extern "C" { #endif -#define BLE_MESH_KEY_PRIMARY 0x0000 -#define BLE_MESH_KEY_ANY 0xffff +#define BLE_MESH_KEY_PRIMARY 0x0000 +#define BLE_MESH_KEY_ANY 0xffff -#define BLE_MESH_ADDR_IS_UNICAST(addr) ((addr) && (addr) < 0x8000) -#define BLE_MESH_ADDR_IS_GROUP(addr) ((addr) >= 0xc000 && (addr) <= 0xff00) -#define BLE_MESH_ADDR_IS_VIRTUAL(addr) ((addr) >= 0x8000 && (addr) < 0xc000) -#define BLE_MESH_ADDR_IS_RFU(addr) ((addr) >= 0xff00 && (addr) <= 0xfffb) +#define BLE_MESH_ADDR_IS_UNICAST(addr) ((addr) && (addr) < 0x8000) +#define BLE_MESH_ADDR_IS_GROUP(addr) ((addr) >= 0xc000 && (addr) <= 0xff00) +#define BLE_MESH_ADDR_IS_VIRTUAL(addr) ((addr) >= 0x8000 && (addr) < 0xc000) +#define BLE_MESH_ADDR_IS_RFU(addr) ((addr) >= 0xff00 && (addr) <= 0xfffb) struct bt_mesh_net; diff --git a/components/bt/esp_ble_mesh/mesh_core/settings.c b/components/bt/esp_ble_mesh/mesh_core/settings.c index 97893b66aa..a102f39379 100644 --- a/components/bt/esp_ble_mesh/mesh_core/settings.c +++ b/components/bt/esp_ble_mesh/mesh_core/settings.c @@ -19,6 +19,7 @@ #include "cfg_srv.h" #include "mesh_common.h" #include "settings_nvs.h" +#include "settings.h" #include "provisioner_main.h" #include "provisioner_prov.h" @@ -167,8 +168,6 @@ struct node_info { u8_t dev_key[16]; } __packed; -#define DEVICE_ROLE_BITS (BIT(BLE_MESH_NODE) | BIT(BLE_MESH_PROVISIONER)) - static int role_set(const char *name) { bool exist = false; @@ -1257,15 +1256,15 @@ int settings_core_load(void) settings[i].func(settings[i].name); if (!strcmp(settings[i].name, "mesh/role")) { - u8_t role = bt_mesh_atomic_get(bt_mesh.flags) & DEVICE_ROLE_BITS; + u8_t role = bt_mesh_atomic_get(bt_mesh.flags) & BLE_MESH_SETTINGS_ROLE_BIT_MASK; switch (role) { case 0U: BT_INFO("Mesh device just starts up, no restore"); return 0; - case BIT(BLE_MESH_NODE): + case BLE_MESH_SETTINGS_ROLE_NODE: BT_INFO("Restored mesh device role: Node"); break; - case BIT(BLE_MESH_PROVISIONER): + case BLE_MESH_SETTINGS_ROLE_PROV: BT_INFO("Restored mesh device role: Provisioner"); break; default: @@ -1486,7 +1485,7 @@ static void store_pending_net(void) void bt_mesh_store_role(void) { - BT_DBG("Store, device role %lu", bt_mesh_atomic_get(bt_mesh.flags) & DEVICE_ROLE_BITS); + BT_DBG("Store, device role %lu", bt_mesh_atomic_get(bt_mesh.flags) & BLE_MESH_SETTINGS_ROLE_BIT_MASK); bt_mesh_save_core_settings("mesh/role", (const u8_t *)bt_mesh.flags, sizeof(bt_mesh.flags)); } diff --git a/components/bt/esp_ble_mesh/mesh_core/settings.h b/components/bt/esp_ble_mesh/mesh_core/settings.h index 52a7b2cd0f..5bb50bda04 100644 --- a/components/bt/esp_ble_mesh/mesh_core/settings.h +++ b/components/bt/esp_ble_mesh/mesh_core/settings.h @@ -14,6 +14,11 @@ extern "C" { #endif +#define BLE_MESH_SETTINGS_ROLE_NONE 0 +#define BLE_MESH_SETTINGS_ROLE_NODE (BIT(BLE_MESH_NODE)) +#define BLE_MESH_SETTINGS_ROLE_PROV (BIT(BLE_MESH_PROVISIONER)) +#define BLE_MESH_SETTINGS_ROLE_BIT_MASK (BIT(BLE_MESH_NODE) | BIT(BLE_MESH_PROVISIONER)) + int settings_core_init(void); int settings_core_load(void); int settings_core_commit(void); From 5ad4166d413a58c39bc69071cbb25dde0ab2bc78 Mon Sep 17 00:00:00 2001 From: lly Date: Tue, 26 May 2020 18:31:46 +0800 Subject: [PATCH 04/12] ble_mesh: stack: Use mutex for settings operation Also expose the settings functions with the parameter bt_mesh_nvs_handle_t, which will be used for further updates. --- .../bt/esp_ble_mesh/mesh_core/settings.c | 30 ++++++++- .../bt/esp_ble_mesh/mesh_core/settings.h | 3 + .../mesh_core/storage/settings_nvs.c | 61 ++++++++++++++++--- .../mesh_core/storage/settings_nvs.h | 13 +++- 4 files changed, 95 insertions(+), 12 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/settings.c b/components/bt/esp_ble_mesh/mesh_core/settings.c index a102f39379..5284f27041 100644 --- a/components/bt/esp_ble_mesh/mesh_core/settings.c +++ b/components/bt/esp_ble_mesh/mesh_core/settings.c @@ -168,6 +168,30 @@ struct node_info { u8_t dev_key[16]; } __packed; +static bt_mesh_mutex_t settings_lock; + +static void bt_mesh_settings_mutex_new(void) +{ + if (settings_lock.mutex == NULL) { + bt_mesh_mutex_create(&settings_lock); + } +} + +static void bt_mesh_settings_mutex_free(void) +{ + bt_mesh_mutex_free(&settings_lock); +} + +void bt_mesh_settings_lock(void) +{ + bt_mesh_mutex_lock(&settings_lock); +} + +void bt_mesh_settings_unlock(void) +{ + bt_mesh_mutex_unlock(&settings_lock); +} + static int role_set(const char *name) { bool exist = false; @@ -2557,7 +2581,8 @@ int bt_mesh_settings_init(void) { BT_DBG("%s", __func__); - bt_mesh_settings_foreach(); + bt_mesh_settings_mutex_new(); + bt_mesh_settings_init_foreach(); return 0; } @@ -2571,7 +2596,8 @@ int settings_core_deinit(void) int bt_mesh_settings_deinit(void) { - bt_mesh_settings_deforeach(); + bt_mesh_settings_deinit_foreach(); + bt_mesh_settings_mutex_free(); return 0; } diff --git a/components/bt/esp_ble_mesh/mesh_core/settings.h b/components/bt/esp_ble_mesh/mesh_core/settings.h index 5bb50bda04..b889e97eab 100644 --- a/components/bt/esp_ble_mesh/mesh_core/settings.h +++ b/components/bt/esp_ble_mesh/mesh_core/settings.h @@ -64,6 +64,9 @@ void bt_mesh_store_node_name(struct bt_mesh_node *node); void bt_mesh_store_node_comp_data(struct bt_mesh_node *node); #endif +void bt_mesh_settings_lock(void); +void bt_mesh_settings_unlock(void); + int bt_mesh_settings_init(void); int bt_mesh_settings_deinit(void); diff --git a/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.c b/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.c index 3d6d4ef50f..6f996818b8 100644 --- a/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.c +++ b/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.c @@ -55,7 +55,7 @@ static struct settings_context settings_ctx[] = { /* API used to initialize, load and commit BLE Mesh related settings */ -void bt_mesh_settings_foreach(void) +void bt_mesh_settings_init_foreach(void) { int err = 0; int i; @@ -98,7 +98,7 @@ void bt_mesh_settings_foreach(void) } } -void bt_mesh_settings_deforeach(void) +void bt_mesh_settings_deinit_foreach(void) { int i; @@ -162,10 +162,20 @@ static int settings_save(bt_mesh_nvs_handle_t handle, const char *key, const u8_ return 0; } +int bt_mesh_save_settings(bt_mesh_nvs_handle_t handle, const char *key, + const u8_t *val, size_t len) +{ + int err = 0; + bt_mesh_settings_lock(); + err = settings_save(handle, key, val, len); + bt_mesh_settings_unlock(); + return err; +} + int bt_mesh_save_core_settings(const char *key, const u8_t *val, size_t len) { bt_mesh_nvs_handle_t handle = settings_get_nvs_handle(SETTINGS_CORE); - return settings_save(handle, key, val, len); + return bt_mesh_save_settings(handle, key, val, len); } /* API used to load BLE Mesh related settings */ @@ -196,10 +206,20 @@ static int settings_load(bt_mesh_nvs_handle_t handle, const char *key, return 0; } +int bt_mesh_load_settings(bt_mesh_nvs_handle_t handle, const char *key, + u8_t *buf, size_t buf_len, bool *exist) +{ + int err = 0; + bt_mesh_settings_lock(); + err = settings_load(handle, key, buf, buf_len, exist); + bt_mesh_settings_unlock(); + return err; +} + int bt_mesh_load_core_settings(const char *key, u8_t *buf, size_t buf_len, bool *exist) { bt_mesh_nvs_handle_t handle = settings_get_nvs_handle(SETTINGS_CORE); - return settings_load(handle, key, buf, buf_len, exist); + return bt_mesh_load_settings(handle, key, buf, buf_len, exist); } /* API used to get length of BLE Mesh related settings */ @@ -267,10 +287,19 @@ static struct net_buf_simple *settings_get_item(bt_mesh_nvs_handle_t handle, con return buf; } +struct net_buf_simple *bt_mesh_get_settings_item(bt_mesh_nvs_handle_t handle, const char *key) +{ + struct net_buf_simple *buf = NULL; + bt_mesh_settings_lock(); + buf = settings_get_item(handle, key); + bt_mesh_settings_unlock(); + return buf; +} + struct net_buf_simple *bt_mesh_get_core_settings_item(const char *key) { bt_mesh_nvs_handle_t handle = settings_get_nvs_handle(SETTINGS_CORE); - return settings_get_item(handle, key); + return bt_mesh_get_settings_item(handle, key); } /* API used to check if the settings item exists */ @@ -339,10 +368,19 @@ static int settings_add_item(bt_mesh_nvs_handle_t handle, const char *key, const return err; } +int bt_mesh_add_settings_item(bt_mesh_nvs_handle_t handle, const char *key, const u16_t val) +{ + int err = 0; + bt_mesh_settings_lock(); + err = settings_add_item(handle, key, val); + bt_mesh_settings_unlock(); + return err; +} + int bt_mesh_add_core_settings_item(const char *key, const u16_t val) { bt_mesh_nvs_handle_t handle = settings_get_nvs_handle(SETTINGS_CORE); - return settings_add_item(handle, key, val); + return bt_mesh_add_settings_item(handle, key, val); } /* API used to remove the settings item */ @@ -394,10 +432,19 @@ static int settings_remove_item(bt_mesh_nvs_handle_t handle, const char *key, co return err; } +int bt_mesh_remove_settings_item(bt_mesh_nvs_handle_t handle, const char *key, const u16_t val) +{ + int err = 0; + bt_mesh_settings_lock(); + err = settings_remove_item(handle, key, val); + bt_mesh_settings_unlock(); + return err; +} + int bt_mesh_remove_core_settings_item(const char *key, const u16_t val) { bt_mesh_nvs_handle_t handle = settings_get_nvs_handle(SETTINGS_CORE); - return settings_remove_item(handle, key, val); + return bt_mesh_remove_settings_item(handle, key, val); } #endif /* CONFIG_BLE_MESH_SETTINGS */ diff --git a/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.h b/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.h index 948ad21ef5..3dd2785fa7 100644 --- a/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.h +++ b/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.h @@ -28,19 +28,26 @@ typedef nvs_handle_t bt_mesh_nvs_handle_t; #define BLE_MESH_GET_ELEM_IDX(x) ((u8_t)((x) >> 8)) #define BLE_MESH_GET_MODEL_IDX(x) ((u8_t)(x)) -#define BLE_MESH_GET_MODEL_KEY(a, b) ((u16_t)(((u16_t)((a) << 8)) | b)) +#define BLE_MESH_GET_MODEL_KEY(a, b) ((u16_t)(((u16_t)((a) << 8)) | (b))) -void bt_mesh_settings_foreach(void); -void bt_mesh_settings_deforeach(void); +void bt_mesh_settings_init_foreach(void); +void bt_mesh_settings_deinit_foreach(void); +int bt_mesh_save_settings(bt_mesh_nvs_handle_t handle, const char *key, + const u8_t *val, size_t len); int bt_mesh_save_core_settings(const char *key, const u8_t *val, size_t len); +int bt_mesh_load_settings(bt_mesh_nvs_handle_t handle, const char *key, + u8_t *buf, size_t buf_len, bool *exist); int bt_mesh_load_core_settings(const char *key, u8_t *buf, size_t buf_len, bool *exist); +struct net_buf_simple *bt_mesh_get_settings_item(bt_mesh_nvs_handle_t handle, const char *key); struct net_buf_simple *bt_mesh_get_core_settings_item(const char *key); +int bt_mesh_add_settings_item(bt_mesh_nvs_handle_t handle, const char *key, const u16_t val); int bt_mesh_add_core_settings_item(const char *key, const u16_t val); +int bt_mesh_remove_settings_item(bt_mesh_nvs_handle_t handle, const char *key, const u16_t val); int bt_mesh_remove_core_settings_item(const char *key, const u16_t val); #ifdef __cplusplus From f010c363661f9a750fba6499e247f15aba728c7a Mon Sep 17 00:00:00 2001 From: lly Date: Wed, 27 May 2020 09:11:53 +0800 Subject: [PATCH 05/12] ble_mesh: stack: Use erase settings functions --- .../bt/esp_ble_mesh/mesh_core/settings.c | 135 +++++++++--------- .../mesh_core/storage/settings_nvs.c | 10 ++ .../mesh_core/storage/settings_nvs.h | 3 + 3 files changed, 83 insertions(+), 65 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/settings.c b/components/bt/esp_ble_mesh/mesh_core/settings.c index 5284f27041..5d01228476 100644 --- a/components/bt/esp_ble_mesh/mesh_core/settings.c +++ b/components/bt/esp_ble_mesh/mesh_core/settings.c @@ -1211,43 +1211,44 @@ const struct bt_mesh_setting { const char *name; int (*func)(const char *name); } settings[] = { - { "mesh/role", role_set }, - { "mesh/net", net_set }, - { "mesh/iv", iv_set }, - { "mesh/seq", seq_set }, - { "mesh/rpl", rpl_set }, - { "mesh/netkey", net_key_set }, - { "mesh/appkey", app_key_set }, - { "mesh/hb_pub", hb_pub_set }, - { "mesh/cfg", cfg_set }, - { "mesh/sig", sig_mod_set }, - { "mesh/vnd", vnd_mod_set }, + { "mesh/role", role_set }, /* For Node & Provisioner */ + { "mesh/net", net_set }, /* For Node */ + { "mesh/iv", iv_set }, /* For Node & Provisioner */ + { "mesh/seq", seq_set }, /* For Node & Provisioner */ + { "mesh/rpl", rpl_set }, /* For Node & Provisioner */ + { "mesh/netkey", net_key_set }, /* For Node */ + { "mesh/appkey", app_key_set }, /* For Node */ + { "mesh/hb_pub", hb_pub_set }, /* For Node */ + { "mesh/cfg", cfg_set }, /* For Node */ + { "mesh/sig", sig_mod_set }, /* For Node & Provisioner */ + { "mesh/vnd", vnd_mod_set }, /* For Node & Provisioner */ #if CONFIG_BLE_MESH_LABEL_COUNT > 0 - { "mesh/vaddr", va_set }, + { "mesh/vaddr", va_set }, /* For Node */ #endif #if CONFIG_BLE_MESH_PROVISIONER - { "mesh/p_prov", p_prov_set }, - { "mesh/p_netidx", p_net_idx_set }, - { "mesh/p_appidx", p_app_idx_set }, - { "mesh/p_netkey", p_net_key_set }, - { "mesh/p_appkey", p_app_key_set }, - { "mesh/p_node", p_node_set }, + { "mesh/p_prov", p_prov_set }, /* For Provisioner */ + { "mesh/p_netidx", p_net_idx_set }, /* For Provisioner */ + { "mesh/p_appidx", p_app_idx_set }, /* For Provisioner */ + { "mesh/p_netkey", p_net_key_set }, /* For Provisioner */ + { "mesh/p_appkey", p_app_key_set }, /* For Provisioner */ + { "mesh/p_node", p_node_set }, /* For Provisioner */ #endif }; /** * For Provisioner, the load operation needs the following actions: + * role_set: Need, restore the device role * net_set: Not needed - * iv_set: Need, although Provisioner will do some initialization of IV Index - * during startup, but we need to restore the last IV Index status + * iv_set: Need, restore the last IV Index status * seq_set: Need, restore the previous sequence number * rpl_set: Need, restore the previous Replay Protection List - * net_key_set: Need, restore the previous network keys - * app_key_set: Need, restore the previous application keys + * net_key_set: Not needed + * app_key_set: Not needed * hb_pub_set: Not needed currently * cfg_set: Not needed currently * sig_mod_set: Need, restore SIG models related info (app, sub, pub) * vnd_mod_set: Need, restore vendor models related info (app, sub, pub) + * va_set: Not needed currently */ int settings_core_load(void) { @@ -1260,7 +1261,8 @@ int settings_core_load(void) !strcmp(settings[i].name, "mesh/netkey") || !strcmp(settings[i].name, "mesh/appkey") || !strcmp(settings[i].name, "mesh/hb_pub") || - !strcmp(settings[i].name, "mesh/cfg")) && + !strcmp(settings[i].name, "mesh/cfg") || + !strcmp(settings[i].name, "mesh/vaddr")) && (!IS_ENABLED(CONFIG_BLE_MESH_NODE) || bt_mesh_is_provisioner())) { BT_DBG("Not restoring %s for Provisioner", settings[i].name); continue; @@ -1282,7 +1284,7 @@ int settings_core_load(void) if (!strcmp(settings[i].name, "mesh/role")) { u8_t role = bt_mesh_atomic_get(bt_mesh.flags) & BLE_MESH_SETTINGS_ROLE_BIT_MASK; switch (role) { - case 0U: + case BLE_MESH_SETTINGS_ROLE_NONE: BT_INFO("Mesh device just starts up, no restore"); return 0; case BLE_MESH_SETTINGS_ROLE_NODE: @@ -1482,16 +1484,10 @@ static void schedule_store(int flag) } } -static void clear_iv(void) -{ - BT_DBG("Clearing IV"); - bt_mesh_save_core_settings("mesh/iv", NULL, 0); -} - static void clear_net(void) { BT_DBG("Clearing Network"); - bt_mesh_save_core_settings("mesh/net", NULL, 0); + bt_mesh_erase_core_settings("mesh/net"); } static void store_pending_net(void) @@ -1542,7 +1538,8 @@ void bt_mesh_store_iv(bool only_duration) void bt_mesh_clear_iv(void) { - clear_iv(); + BT_DBG("Clearing IV"); + bt_mesh_erase_core_settings("mesh/iv"); } static void store_pending_seq(void) @@ -1566,7 +1563,7 @@ void bt_mesh_store_seq(void) void bt_mesh_clear_seq(void) { - bt_mesh_save_core_settings("mesh/seq", NULL, 0); + bt_mesh_erase_core_settings("mesh/seq"); } static void store_rpl(struct bt_mesh_rpl *entry) @@ -1609,7 +1606,7 @@ static void clear_rpl(void) buf = bt_mesh_get_core_settings_item("mesh/rpl"); if (!buf) { - bt_mesh_save_core_settings("mesh/rpl", NULL, 0); + bt_mesh_erase_core_settings("mesh/rpl"); return; } @@ -1617,11 +1614,17 @@ static void clear_rpl(void) for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) { src = net_buf_simple_pull_le16(buf); + + if (!BLE_MESH_ADDR_IS_UNICAST(src)) { + BT_ERR("Invalid source address 0x%04x", src); + continue; + } + sprintf(name, "mesh/rpl/%04x", src); - bt_mesh_save_core_settings(name, NULL, 0); + bt_mesh_erase_core_settings(name); } - bt_mesh_save_core_settings("mesh/rpl", NULL, 0); + bt_mesh_erase_core_settings("mesh/rpl"); bt_mesh_free_buf(buf); return; @@ -1687,7 +1690,7 @@ static void store_pending_cfg(void) static void clear_cfg(void) { BT_DBG("Clearing configuration"); - bt_mesh_save_core_settings("mesh/cfg", NULL, 0); + bt_mesh_erase_core_settings("mesh/cfg"); } static void clear_app_key(u16_t app_idx) @@ -1698,7 +1701,7 @@ static void clear_app_key(u16_t app_idx) BT_DBG("AppKeyIndex 0x%03x", app_idx); sprintf(name, "mesh/ak/%04x", app_idx); - bt_mesh_save_core_settings(name, NULL, 0); + bt_mesh_erase_core_settings(name); err = bt_mesh_remove_core_settings_item("mesh/appkey", app_idx); if (err) { @@ -1716,7 +1719,7 @@ static void clear_net_key(u16_t net_idx) BT_DBG("NetKeyIndex 0x%03x", net_idx); sprintf(name, "mesh/nk/%04x", net_idx); - bt_mesh_save_core_settings(name, NULL, 0); + bt_mesh_erase_core_settings(name); err = bt_mesh_remove_core_settings_item("mesh/netkey", net_idx); if (err) { @@ -1834,7 +1837,7 @@ static void store_pending_mod_bind(struct bt_mesh_model *model, bool vnd) if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && !bt_mesh_is_provisioned()) { - bt_mesh_save_core_settings(name, NULL, 0); + bt_mesh_erase_core_settings(name); return; } @@ -1865,7 +1868,7 @@ static void store_pending_mod_sub(struct bt_mesh_model *model, bool vnd) if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && !bt_mesh_is_provisioned()) { - bt_mesh_save_core_settings(name, NULL, 0); + bt_mesh_erase_core_settings(name); return; } @@ -1896,6 +1899,15 @@ static void store_pending_mod_pub(struct bt_mesh_model *model, bool vnd) return; } + model_key = BLE_MESH_GET_MODEL_KEY(model->elem_idx, model->model_idx); + sprintf(name, "mesh/%s/%04x/p", vnd ? "v" : "s", model_key); + + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && + !bt_mesh_is_provisioned()) { + bt_mesh_erase_core_settings(name); + return; + } + pub.addr = model->pub->addr; pub.key = model->pub->key; pub.ttl = model->pub->ttl; @@ -1904,16 +1916,6 @@ static void store_pending_mod_pub(struct bt_mesh_model *model, bool vnd) pub.period_div = model->pub->period_div; pub.cred = model->pub->cred; - model_key = BLE_MESH_GET_MODEL_KEY(model->elem_idx, model->model_idx); - - sprintf(name, "mesh/%s/%04x/p", vnd ? "v" : "s", model_key); - - if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && - !bt_mesh_is_provisioned()) { - bt_mesh_save_core_settings(name, NULL, 0); - return; - } - err = bt_mesh_save_core_settings(name, (const u8_t *)&pub, sizeof(pub)); if (err) { BT_ERR("Failed to store %s", name); @@ -1971,7 +1973,7 @@ static void store_pending_va(void) sprintf(name, "mesh/va/%04x", i); if (IS_VA_DEL(lab)) { - err = bt_mesh_save_core_settings(name, NULL, 0); + err = bt_mesh_erase_core_settings(name); } else { va.ref = lab->ref; va.addr = lab->addr; @@ -2029,7 +2031,7 @@ static void store_pending(struct k_work *work) if (!IS_ENABLED(CONFIG_BLE_MESH_NODE) || bt_mesh_is_provisioned()) { store_pending_iv(); } else { - clear_iv(); + bt_mesh_clear_iv(); } } @@ -2055,8 +2057,8 @@ static void store_pending(struct k_work *work) bt_mesh_model_foreach(store_pending_mod, NULL); if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && !bt_mesh_is_provisioned()) { - bt_mesh_save_core_settings("mesh/sig", NULL, 0); - bt_mesh_save_core_settings("mesh/vnd", NULL, 0); + bt_mesh_erase_core_settings("mesh/sig"); + bt_mesh_erase_core_settings("mesh/vnd"); } } @@ -2166,7 +2168,7 @@ void bt_mesh_store_cfg(void) void bt_mesh_clear_role(void) { BT_DBG("Clear device role"); - bt_mesh_save_core_settings("mesh/role", NULL, 0); + bt_mesh_erase_core_settings("mesh/role"); } void bt_mesh_clear_net(void) @@ -2286,7 +2288,8 @@ void bt_mesh_store_prov_info(u16_t primary_addr, u16_t alloc_addr) void bt_mesh_clear_prov_info(void) { - bt_mesh_save_core_settings("mesh/p_prov", NULL, 0); + BT_DBG("Clearing prov info"); + bt_mesh_erase_core_settings("mesh/p_prov"); } static void clear_p_net_key(u16_t net_idx) @@ -2295,7 +2298,7 @@ static void clear_p_net_key(u16_t net_idx) int err = 0; sprintf(name, "mesh/pnk/%04x", net_idx); - bt_mesh_save_core_settings(name, NULL, 0); + bt_mesh_erase_core_settings(name); err = bt_mesh_remove_core_settings_item("mesh/p_netkey", net_idx); if (err) { @@ -2309,7 +2312,7 @@ static void clear_p_app_key(u16_t app_idx) int err = 0; sprintf(name, "mesh/pak/%04x", app_idx); - bt_mesh_save_core_settings(name, NULL, 0); + bt_mesh_erase_core_settings(name); err = bt_mesh_remove_core_settings_item("mesh/p_appkey", app_idx); if (err) { @@ -2375,7 +2378,8 @@ void bt_mesh_store_p_net_idx(void) void bt_mesh_clear_p_net_idx(void) { - bt_mesh_save_core_settings("mesh/p_netidx", NULL, 0); + BT_DBG("Clearing NetKey Index"); + bt_mesh_erase_core_settings("mesh/p_netidx"); } void bt_mesh_store_p_app_idx(void) @@ -2388,7 +2392,8 @@ void bt_mesh_store_p_app_idx(void) void bt_mesh_clear_p_app_idx(void) { - bt_mesh_save_core_settings("mesh/p_appidx", NULL, 0); + BT_DBG("Clearing AppKey Index"); + bt_mesh_erase_core_settings("mesh/p_appidx"); } void bt_mesh_store_p_subnet(struct bt_mesh_subnet *sub) @@ -2452,7 +2457,7 @@ void bt_mesh_clear_rpl_single(u16_t src) } sprintf(name, "mesh/rpl/%04x", src); - bt_mesh_save_core_settings(name, NULL, 0); + bt_mesh_erase_core_settings(name); err = bt_mesh_remove_core_settings_item("mesh/rpl", src); if (err) { @@ -2502,15 +2507,15 @@ static void clear_node(u16_t addr) /* Clear node information */ sprintf(name, "mesh/pn/%04x/i", addr); - bt_mesh_save_core_settings(name, NULL, 0); + bt_mesh_erase_core_settings(name); /* Clear node name */ sprintf(name, "mesh/pn/%04x/n", addr); - bt_mesh_save_core_settings(name, NULL, 0); + bt_mesh_erase_core_settings(name); /* Clear node composition data */ sprintf(name, "mesh/pn/%04x/c", addr); - bt_mesh_save_core_settings(name, NULL, 0); + bt_mesh_erase_core_settings(name); err = bt_mesh_remove_core_settings_item("mesh/p_node", addr); if (err) { diff --git a/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.c b/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.c index 6f996818b8..2747d274d9 100644 --- a/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.c +++ b/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.c @@ -178,6 +178,16 @@ int bt_mesh_save_core_settings(const char *key, const u8_t *val, size_t len) return bt_mesh_save_settings(handle, key, val, len); } +int bt_mesh_erase_settings(bt_mesh_nvs_handle_t handle, const char *key) +{ + return bt_mesh_save_settings(handle, key, NULL, 0); +} + +int bt_mesh_erase_core_settings(const char *key) +{ + return bt_mesh_save_core_settings(key, NULL, 0); +} + /* API used to load BLE Mesh related settings */ static int settings_load(bt_mesh_nvs_handle_t handle, const char *key, diff --git a/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.h b/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.h index 3dd2785fa7..b83c358956 100644 --- a/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.h +++ b/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.h @@ -37,6 +37,9 @@ int bt_mesh_save_settings(bt_mesh_nvs_handle_t handle, const char *key, const u8_t *val, size_t len); int bt_mesh_save_core_settings(const char *key, const u8_t *val, size_t len); +int bt_mesh_erase_settings(bt_mesh_nvs_handle_t handle, const char *key); +int bt_mesh_erase_core_settings(const char *key); + int bt_mesh_load_settings(bt_mesh_nvs_handle_t handle, const char *key, u8_t *buf, size_t buf_len, bool *exist); int bt_mesh_load_core_settings(const char *key, u8_t *buf, size_t buf_len, bool *exist); From 88cc07b6ca1fbac7e3685db42533d5a38ec63643 Mon Sep 17 00:00:00 2001 From: lly Date: Wed, 27 May 2020 09:31:33 +0800 Subject: [PATCH 06/12] ble_mesh: stack: Transport rx reset settings update When reset the rx info of transport layer, the rpl list will always cleared, and rpl stored in the nvs will only be erased when erase flag is true and BLE_MESH_SETTINGS is enabled. Compared with the previous solution, it should be more clear. --- .../bt/esp_ble_mesh/api/esp_ble_mesh_defs.h | 4 +-- components/bt/esp_ble_mesh/mesh_core/access.c | 5 ++++ components/bt/esp_ble_mesh/mesh_core/lpn.c | 2 +- components/bt/esp_ble_mesh/mesh_core/main.c | 2 +- .../esp_ble_mesh/mesh_core/provisioner_prov.c | 5 ++++ .../bt/esp_ble_mesh/mesh_core/settings.c | 18 ++++++------- .../bt/esp_ble_mesh/mesh_core/transport.c | 25 ++++--------------- .../bt/esp_ble_mesh/mesh_core/transport.h | 4 +-- 8 files changed, 29 insertions(+), 36 deletions(-) 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 79d12e93cb..ff0ee666d9 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 @@ -1176,14 +1176,14 @@ typedef union { /** * @brief ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_UUID_COMP_EVT */ - struct ble_mesh_provisioner_delete_node_with_uuid_comp_data_comp_param { + struct ble_mesh_provisioner_delete_node_with_uuid_comp_param { int err_code; /*!< Indicate the result of deleting node with uuid by the Provisioner */ uint8_t uuid[16]; /*!< Node device uuid */ } provisioner_delete_node_with_uuid_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_UUID_COMP_EVT */ /** * @brief ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_ADDR_COMP_EVT */ - struct ble_mesh_provisioner_delete_node_with_addr_comp_data_comp_param { + struct ble_mesh_provisioner_delete_node_with_addr_comp_param { int err_code; /*!< Indicate the result of deleting node with unicast address by the Provisioner */ uint16_t unicast_addr; /*!< Node unicast address */ } provisioner_delete_node_with_addr_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_ADDR_COMP_EVT */ diff --git a/components/bt/esp_ble_mesh/mesh_core/access.c b/components/bt/esp_ble_mesh/mesh_core/access.c index c204aad3e1..2f6fe91402 100644 --- a/components/bt/esp_ble_mesh/mesh_core/access.c +++ b/components/bt/esp_ble_mesh/mesh_core/access.c @@ -35,6 +35,11 @@ void bt_mesh_model_foreach(void (*func)(struct bt_mesh_model *mod, { int i, j; + if (dev_comp == NULL) { + BT_ERR("Invalid device composition"); + return; + } + for (i = 0; i < dev_comp->elem_count; i++) { struct bt_mesh_elem *elem = &dev_comp->elem[i]; diff --git a/components/bt/esp_ble_mesh/mesh_core/lpn.c b/components/bt/esp_ble_mesh/mesh_core/lpn.c index 5ee5700460..7ba704573a 100644 --- a/components/bt/esp_ble_mesh/mesh_core/lpn.c +++ b/components/bt/esp_ble_mesh/mesh_core/lpn.c @@ -218,7 +218,7 @@ static void clear_friendship(bool force, bool disable) return; } - bt_mesh_rx_reset(); + bt_mesh_rx_reset(true); k_delayed_work_cancel(&lpn->timer); diff --git a/components/bt/esp_ble_mesh/mesh_core/main.c b/components/bt/esp_ble_mesh/mesh_core/main.c index f755910c40..17d0a21c08 100644 --- a/components/bt/esp_ble_mesh/mesh_core/main.c +++ b/components/bt/esp_ble_mesh/mesh_core/main.c @@ -102,7 +102,7 @@ void bt_mesh_node_reset(void) bt_mesh_cfg_reset(); - bt_mesh_rx_reset(); + bt_mesh_rx_reset(true); bt_mesh_tx_reset(); if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) { 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 18497ae0b3..cdab4b974d 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c @@ -1134,6 +1134,11 @@ int bt_mesh_provisioner_init_prov_info(void) /* If unicast address of primary element of Provisioner has not been set * before, then the following initialization procedure will be used. */ + if (prov == NULL) { + BT_ERR("No provisioning context provided"); + return -EINVAL; + } + if (!BLE_MESH_ADDR_IS_UNICAST(prov->prov_unicast_addr) || !BLE_MESH_ADDR_IS_UNICAST(prov->prov_start_address)) { BT_ERR("Invalid address, own 0x%04x, start 0x%04x", diff --git a/components/bt/esp_ble_mesh/mesh_core/settings.c b/components/bt/esp_ble_mesh/mesh_core/settings.c index 5d01228476..beca5ad363 100644 --- a/components/bt/esp_ble_mesh/mesh_core/settings.c +++ b/components/bt/esp_ble_mesh/mesh_core/settings.c @@ -1355,13 +1355,13 @@ int settings_core_commit(void) #if defined(CONFIG_BLE_MESH_NODE) if (bt_mesh_is_node()) { - BT_INFO("sub[0].net_idx 0x%03x", bt_mesh.sub[0].net_idx); - if (bt_mesh.sub[0].net_idx == BLE_MESH_KEY_UNUSED) { /* Nothing to do since we're not yet provisioned */ return 0; } + BT_INFO("Settings commit, sub[0].net_idx 0x%03x", bt_mesh.sub[0].net_idx); + if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) { bt_mesh_proxy_server_prov_disable(true); } @@ -1388,7 +1388,7 @@ int settings_core_commit(void) return 0; } - BT_INFO("p_sub[0]->net_idx 0x%03x", bt_mesh.p_sub[0]->net_idx); + BT_INFO("Settings commit, p_sub[0]->net_idx 0x%03x", bt_mesh.p_sub[0]->net_idx); for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { sub = bt_mesh.p_sub[i]; @@ -1405,11 +1405,13 @@ int settings_core_commit(void) } #endif /* CONFIG_BLE_MESH_PROVISIONER */ - if (bt_mesh.ivu_duration < BLE_MESH_IVU_MIN_HOURS) { - k_delayed_work_submit(&bt_mesh.ivu_timer, BLE_MESH_IVU_TIMEOUT); - } + if (bt_mesh_is_node() || bt_mesh_is_provisioner()) { + if (bt_mesh.ivu_duration < BLE_MESH_IVU_MIN_HOURS) { + k_delayed_work_submit(&bt_mesh.ivu_timer, BLE_MESH_IVU_TIMEOUT); + } - bt_mesh_model_foreach(commit_model, NULL); + bt_mesh_model_foreach(commit_model, NULL); + } #if defined(CONFIG_BLE_MESH_NODE) if (bt_mesh_is_node()) { @@ -1602,8 +1604,6 @@ static void clear_rpl(void) BT_DBG("%s", __func__); - bt_mesh_rpl_clear(); - buf = bt_mesh_get_core_settings_item("mesh/rpl"); if (!buf) { bt_mesh_erase_core_settings("mesh/rpl"); diff --git a/components/bt/esp_ble_mesh/mesh_core/transport.c b/components/bt/esp_ble_mesh/mesh_core/transport.c index bcb0ac47c5..313decc8f9 100644 --- a/components/bt/esp_ble_mesh/mesh_core/transport.c +++ b/components/bt/esp_ble_mesh/mesh_core/transport.c @@ -1768,7 +1768,7 @@ int bt_mesh_trans_recv(struct net_buf_simple *buf, struct bt_mesh_net_rx *rx) return err; } -void bt_mesh_rx_reset(void) +void bt_mesh_rx_reset(bool erase) { int i; @@ -1778,10 +1778,10 @@ void bt_mesh_rx_reset(void) seg_rx_reset(&seg_rx[i], true); } - if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + (void)memset(bt_mesh.rpl, 0, sizeof(bt_mesh.rpl)); + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && erase) { bt_mesh_clear_rpl(); - } else { - (void)memset(bt_mesh.rpl, 0, sizeof(bt_mesh.rpl)); } } @@ -1863,16 +1863,7 @@ void bt_mesh_trans_deinit(bool erase) { int i; - for (i = 0; i < ARRAY_SIZE(seg_rx); i++) { - seg_rx_reset(&seg_rx[i], true); - } - - if (erase && IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { - bt_mesh_clear_rpl(); - } else { - bt_mesh_rpl_clear(); - } - + bt_mesh_rx_reset(erase); bt_mesh_tx_reset(); for (i = 0; i < ARRAY_SIZE(seg_tx); i++) { @@ -1887,12 +1878,6 @@ void bt_mesh_trans_deinit(bool erase) bt_mesh_rx_seg_mutex_free(); } -void bt_mesh_rpl_clear(void) -{ - BT_DBG("%s", __func__); - (void)memset(bt_mesh.rpl, 0, sizeof(bt_mesh.rpl)); -} - void bt_mesh_heartbeat_send(void) { struct bt_mesh_cfg_srv *cfg = bt_mesh_cfg_get(); diff --git a/components/bt/esp_ble_mesh/mesh_core/transport.h b/components/bt/esp_ble_mesh/mesh_core/transport.h index ac80273b0d..043f8f77a1 100644 --- a/components/bt/esp_ble_mesh/mesh_core/transport.h +++ b/components/bt/esp_ble_mesh/mesh_core/transport.h @@ -97,7 +97,7 @@ struct bt_mesh_app_key *bt_mesh_app_key_find(u16_t app_idx); bool bt_mesh_tx_in_progress(void); -void bt_mesh_rx_reset(void); +void bt_mesh_rx_reset(bool erase); void bt_mesh_tx_reset(void); void bt_mesh_rx_reset_single(u16_t src); void bt_mesh_tx_reset_single(u16_t dst); @@ -114,8 +114,6 @@ int bt_mesh_trans_recv(struct net_buf_simple *buf, struct bt_mesh_net_rx *rx); void bt_mesh_trans_init(void); void bt_mesh_trans_deinit(bool erase); -void bt_mesh_rpl_clear(void); - void bt_mesh_heartbeat_send(void); int bt_mesh_app_key_get(const struct bt_mesh_subnet *subnet, u16_t app_idx, From 80b16c58bbc517e085d66f11a62b7aabe0310048 Mon Sep 17 00:00:00 2001 From: lly Date: Wed, 27 May 2020 09:34:33 +0800 Subject: [PATCH 07/12] ble_mesh: stack: Add clear hb_pub in settings --- .../bt/esp_ble_mesh/mesh_core/settings.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/settings.c b/components/bt/esp_ble_mesh/mesh_core/settings.c index beca5ad363..f3b711d93d 100644 --- a/components/bt/esp_ble_mesh/mesh_core/settings.c +++ b/components/bt/esp_ble_mesh/mesh_core/settings.c @@ -550,13 +550,13 @@ static int hb_pub_set(const char *name) BT_DBG("%s", __func__); if (!hb_pub) { - BT_ERR("Invalid heartbeat pub"); + BT_ERR("Invalid heartbeat publication"); return -EINVAL; } err = bt_mesh_load_core_settings(name, (u8_t *)&hb_val, sizeof(hb_val), &exist); if (err) { - BT_ERR("Failed to load heartbeat pub"); + BT_ERR("Failed to load heartbeat publication"); hb_pub->dst = BLE_MESH_ADDR_UNASSIGNED; hb_pub->count = 0U; hb_pub->ttl = 0U; @@ -1652,7 +1652,7 @@ static void store_pending_hb_pub(void) struct hb_pub_val val = {0}; if (!hb_pub) { - BT_WARN("NULL heartbeat publication"); + BT_ERR("Invalid heartbeat publication"); return; } @@ -1666,6 +1666,12 @@ static void store_pending_hb_pub(void) bt_mesh_save_core_settings("mesh/hb_pub", (const u8_t *)&val, sizeof(val)); } +static void clear_hb_pub(void) +{ + BT_DBG("Clear heartbeat publication"); + bt_mesh_erase_core_settings("mesh/hb_pub"); +} + static void store_pending_cfg(void) { struct bt_mesh_cfg_srv *cfg = bt_mesh_cfg_get(); @@ -2041,7 +2047,11 @@ static void store_pending(struct k_work *work) if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_HB_PUB_PENDING)) { - store_pending_hb_pub(); + if (bt_mesh_is_provisioned()) { + store_pending_hb_pub(); + } else { + clear_hb_pub(); + } } if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && From f8c38181821de5dbe159388388b11bae51b9dba2 Mon Sep 17 00:00:00 2001 From: lly Date: Wed, 27 May 2020 09:42:50 +0800 Subject: [PATCH 08/12] ble_mesh: stack: Erase netkey and appkey with key index --- .../esp_ble_mesh/mesh_core/provisioner_main.c | 8 +-- .../bt/esp_ble_mesh/mesh_core/settings.c | 68 +++++++------------ .../bt/esp_ble_mesh/mesh_core/settings.h | 4 +- 3 files changed, 30 insertions(+), 50 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c index 416fbd3f83..9a2899ae12 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c @@ -165,7 +165,7 @@ int bt_mesh_provisioner_deinit(bool erase) for (i = 0; i < CONFIG_BLE_MESH_PROVISIONER_SUBNET_COUNT; i++) { if (bt_mesh.p_sub[i]) { if (erase && IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { - bt_mesh_clear_p_subnet(bt_mesh.p_sub[i]); + bt_mesh_clear_p_subnet(bt_mesh.p_sub[i]->net_idx); } bt_mesh_free(bt_mesh.p_sub[i]); bt_mesh.p_sub[i] = NULL; @@ -175,7 +175,7 @@ int bt_mesh_provisioner_deinit(bool erase) for (i = 0; i < CONFIG_BLE_MESH_PROVISIONER_APP_KEY_COUNT; i++) { if (bt_mesh.p_app_keys[i]) { if (erase && IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { - bt_mesh_clear_p_app_key(bt_mesh.p_app_keys[i]); + bt_mesh_clear_p_app_key(bt_mesh.p_app_keys[i]->app_idx); } bt_mesh_free(bt_mesh.p_app_keys[i]); bt_mesh.p_app_keys[i] = NULL; @@ -1198,7 +1198,7 @@ int bt_mesh_provisioner_local_app_key_delete(u16_t net_idx, u16_t app_idx) bt_mesh_model_foreach(_model_unbind, &app_idx); if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { - bt_mesh_clear_p_app_key(key); + bt_mesh_clear_p_app_key(app_idx); } bt_mesh_free(bt_mesh.p_app_keys[i]); @@ -1391,7 +1391,7 @@ int bt_mesh_provisioner_local_net_key_delete(u16_t net_idx) } if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { - bt_mesh_clear_p_subnet(sub); + bt_mesh_clear_p_subnet(net_idx); } bt_mesh_free(bt_mesh.p_sub[i]); diff --git a/components/bt/esp_ble_mesh/mesh_core/settings.c b/components/bt/esp_ble_mesh/mesh_core/settings.c index f3b711d93d..21cc5cc67e 100644 --- a/components/bt/esp_ble_mesh/mesh_core/settings.c +++ b/components/bt/esp_ble_mesh/mesh_core/settings.c @@ -2302,34 +2302,6 @@ void bt_mesh_clear_prov_info(void) bt_mesh_erase_core_settings("mesh/p_prov"); } -static void clear_p_net_key(u16_t net_idx) -{ - char name[16] = {'\0'}; - int err = 0; - - sprintf(name, "mesh/pnk/%04x", net_idx); - bt_mesh_erase_core_settings(name); - - err = bt_mesh_remove_core_settings_item("mesh/p_netkey", net_idx); - if (err) { - BT_ERR("Failed to remove 0x%03x from mesh/p_netkey", net_idx); - } -} - -static void clear_p_app_key(u16_t app_idx) -{ - char name[16] = {'\0'}; - int err = 0; - - sprintf(name, "mesh/pak/%04x", app_idx); - bt_mesh_erase_core_settings(name); - - err = bt_mesh_remove_core_settings_item("mesh/p_appkey", app_idx); - if (err) { - BT_ERR("Failed to remove 0x%03x from mesh/p_appkey", app_idx); - } -} - static void store_p_net_key(struct bt_mesh_subnet *sub) { struct net_key_val key = {0}; @@ -2432,28 +2404,36 @@ void bt_mesh_store_p_app_key(struct bt_mesh_app_key *key) store_p_app_key(key); } -void bt_mesh_clear_p_subnet(struct bt_mesh_subnet *sub) +void bt_mesh_clear_p_subnet(u16_t net_idx) { - if (sub == NULL) { - BT_ERR("Invalid subnet"); - return; + char name[16] = {'\0'}; + int err = 0; + + BT_DBG("NetKeyIndex 0x%03x", net_idx); + + sprintf(name, "mesh/pnk/%04x", net_idx); + bt_mesh_erase_core_settings(name); + + err = bt_mesh_remove_core_settings_item("mesh/p_netkey", net_idx); + if (err) { + BT_ERR("Failed to remove 0x%04x from mesh/p_netkey", net_idx); } - - BT_DBG("NetKeyIndex 0x%03x", sub->net_idx); - - clear_p_net_key(sub->net_idx); } -void bt_mesh_clear_p_app_key(struct bt_mesh_app_key *key) +void bt_mesh_clear_p_app_key(u16_t app_idx) { - if (key == NULL) { - BT_ERR("Invalid AppKey"); - return; + char name[16] = {'\0'}; + int err = 0; + + BT_DBG("AppKeyIndex 0x%03x", app_idx); + + sprintf(name, "mesh/pak/%04x", app_idx); + bt_mesh_erase_core_settings(name); + + err = bt_mesh_remove_core_settings_item("mesh/p_appkey", app_idx); + if (err) { + BT_ERR("Failed to remove 0x%04x from mesh/p_appkey", app_idx); } - - BT_DBG("AppKeyIndex 0x%03x", key->app_idx); - - clear_p_app_key(key->app_idx); } void bt_mesh_clear_rpl_single(u16_t src) diff --git a/components/bt/esp_ble_mesh/mesh_core/settings.h b/components/bt/esp_ble_mesh/mesh_core/settings.h index b889e97eab..85a7d93c0e 100644 --- a/components/bt/esp_ble_mesh/mesh_core/settings.h +++ b/components/bt/esp_ble_mesh/mesh_core/settings.h @@ -55,8 +55,8 @@ void bt_mesh_store_p_app_idx(void); void bt_mesh_clear_p_app_idx(void); void bt_mesh_store_p_subnet(struct bt_mesh_subnet *sub); void bt_mesh_store_p_app_key(struct bt_mesh_app_key *key); -void bt_mesh_clear_p_subnet(struct bt_mesh_subnet *sub); -void bt_mesh_clear_p_app_key(struct bt_mesh_app_key *key); +void bt_mesh_clear_p_subnet(u16_t net_idx); +void bt_mesh_clear_p_app_key(u16_t app_idx); void bt_mesh_clear_rpl_single(u16_t src); void bt_mesh_store_node_info(struct bt_mesh_node *node); void bt_mesh_clear_node_info(u16_t unicast_addr); From e18ea178b8f00ae1e71606fd6aacf210dd32139f Mon Sep 17 00:00:00 2001 From: lly Date: Wed, 27 May 2020 10:04:01 +0800 Subject: [PATCH 09/12] ble_mesh: stack: Split model settings store and clear --- .../bt/esp_ble_mesh/mesh_core/settings.c | 101 +++++++++++++----- 1 file changed, 74 insertions(+), 27 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/settings.c b/components/bt/esp_ble_mesh/mesh_core/settings.c index 21cc5cc67e..b7886aa22d 100644 --- a/components/bt/esp_ble_mesh/mesh_core/settings.c +++ b/components/bt/esp_ble_mesh/mesh_core/settings.c @@ -1565,6 +1565,7 @@ void bt_mesh_store_seq(void) void bt_mesh_clear_seq(void) { + BT_DBG("Clearing Seq"); bt_mesh_erase_core_settings("mesh/seq"); } @@ -1838,15 +1839,8 @@ static void store_pending_mod_bind(struct bt_mesh_model *model, bool vnd) int err = 0; model_key = BLE_MESH_GET_MODEL_KEY(model->elem_idx, model->model_idx); - sprintf(name, "mesh/%s/%04x/b", vnd ? "v" : "s", model_key); - if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && - !bt_mesh_is_provisioned()) { - bt_mesh_erase_core_settings(name); - return; - } - err = bt_mesh_save_core_settings(name, (const u8_t *)model->keys, sizeof(model->keys)); if (err) { BT_ERR("Failed to store %s", name); @@ -1869,15 +1863,8 @@ static void store_pending_mod_sub(struct bt_mesh_model *model, bool vnd) int err = 0; model_key = BLE_MESH_GET_MODEL_KEY(model->elem_idx, model->model_idx); - sprintf(name, "mesh/%s/%04x/s", vnd ? "v" : "s", model_key); - if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && - !bt_mesh_is_provisioned()) { - bt_mesh_erase_core_settings(name); - return; - } - err = bt_mesh_save_core_settings(name, (const u8_t *)model->groups, sizeof(model->groups)); if (err) { BT_ERR("Failed to store %s", name); @@ -1908,12 +1895,6 @@ static void store_pending_mod_pub(struct bt_mesh_model *model, bool vnd) model_key = BLE_MESH_GET_MODEL_KEY(model->elem_idx, model->model_idx); sprintf(name, "mesh/%s/%04x/p", vnd ? "v" : "s", model_key); - if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && - !bt_mesh_is_provisioned()) { - bt_mesh_erase_core_settings(name); - return; - } - pub.addr = model->pub->addr; pub.key = model->pub->key; pub.ttl = model->pub->ttl; @@ -1961,6 +1942,66 @@ static void store_pending_mod(struct bt_mesh_model *model, } } +static void clear_mod_bind(struct bt_mesh_model *model, bool vnd) +{ + char name[16] = {'\0'}; + u16_t model_key = 0U; + + model_key = BLE_MESH_GET_MODEL_KEY(model->elem_idx, model->model_idx); + sprintf(name, "mesh/%s/%04x/b", vnd ? "v" : "s", model_key); + + bt_mesh_erase_core_settings(name); + bt_mesh_remove_core_settings_item(vnd ? "mesh/vnd" : "mesh/sig", model_key); +} + +static void clear_mod_sub(struct bt_mesh_model *model, bool vnd) +{ + char name[16] = {'\0'}; + u16_t model_key = 0U; + + model_key = BLE_MESH_GET_MODEL_KEY(model->elem_idx, model->model_idx); + sprintf(name, "mesh/%s/%04x/s", vnd ? "v" : "s", model_key); + + bt_mesh_erase_core_settings(name); + bt_mesh_remove_core_settings_item(vnd ? "mesh/vnd" : "mesh/sig", model_key); +} + +static void clear_mod_pub(struct bt_mesh_model *model, bool vnd) +{ + char name[16] = {'\0'}; + u16_t model_key = 0U; + + model_key = BLE_MESH_GET_MODEL_KEY(model->elem_idx, model->model_idx); + sprintf(name, "mesh/%s/%04x/p", vnd ? "v" : "s", model_key); + + bt_mesh_erase_core_settings(name); + bt_mesh_remove_core_settings_item(vnd ? "mesh/vnd" : "mesh/sig", model_key); +} + +static void clear_pending_mod(struct bt_mesh_model *model, + struct bt_mesh_elem *elem, bool vnd, + bool primary, void *user_data) +{ + if (!model->flags) { + return; + } + + if (model->flags & BLE_MESH_MOD_BIND_PENDING) { + model->flags &= ~BLE_MESH_MOD_BIND_PENDING; + clear_mod_bind(model, vnd); + } + + if (model->flags & BLE_MESH_MOD_SUB_PENDING) { + model->flags &= ~BLE_MESH_MOD_SUB_PENDING; + clear_mod_sub(model, vnd); + } + + if (model->flags & BLE_MESH_MOD_PUB_PENDING) { + model->flags &= ~BLE_MESH_MOD_PUB_PENDING; + clear_mod_pub(model, vnd); + } +} + #define IS_VA_DEL(_label) ((_label)->ref == 0) static void store_pending_va(void) { @@ -2012,7 +2053,7 @@ static void store_pending(struct k_work *work) BT_DBG("%s", __func__); if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_RPL_PENDING)) { - if (!IS_ENABLED(CONFIG_BLE_MESH_NODE) || bt_mesh_is_provisioned()) { + if (bt_mesh_is_provisioned() || bt_mesh_is_provisioner_en()) { store_pending_rpl(); } else { clear_rpl(); @@ -2034,7 +2075,7 @@ static void store_pending(struct k_work *work) } if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_IV_PENDING)) { - if (!IS_ENABLED(CONFIG_BLE_MESH_NODE) || bt_mesh_is_provisioned()) { + if (bt_mesh_is_provisioned() || bt_mesh_is_provisioner_en()) { store_pending_iv(); } else { bt_mesh_clear_iv(); @@ -2042,7 +2083,11 @@ static void store_pending(struct k_work *work) } if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_SEQ_PENDING)) { - store_pending_seq(); + if (bt_mesh_is_provisioned() || bt_mesh_is_provisioner_en()) { + store_pending_seq(); + } else { + bt_mesh_clear_seq(); + } } if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && @@ -2064,15 +2109,17 @@ static void store_pending(struct k_work *work) } if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_MOD_PENDING)) { - bt_mesh_model_foreach(store_pending_mod, NULL); - if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && - !bt_mesh_is_provisioned()) { + if (bt_mesh_is_provisioned() || bt_mesh_is_provisioner_en()) { + bt_mesh_model_foreach(store_pending_mod, NULL); + } else { + bt_mesh_model_foreach(clear_pending_mod, NULL); bt_mesh_erase_core_settings("mesh/sig"); bt_mesh_erase_core_settings("mesh/vnd"); } } - if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_VA_PENDING)) { + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && + bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_VA_PENDING)) { store_pending_va(); } } From f34c7df26d833f6547eac7296b757ba923311dba Mon Sep 17 00:00:00 2001 From: lly Date: Wed, 27 May 2020 10:38:45 +0800 Subject: [PATCH 10/12] ble_mesh: stack: Continue restore even if failure happens Previously we have used this solution for node info restore. Here use the same solution for other mesh information restore. --- .../bt/esp_ble_mesh/mesh_core/settings.c | 60 ++++++++----------- 1 file changed, 25 insertions(+), 35 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/settings.c b/components/bt/esp_ble_mesh/mesh_core/settings.c index b7886aa22d..26a4d6d56a 100644 --- a/components/bt/esp_ble_mesh/mesh_core/settings.c +++ b/components/bt/esp_ble_mesh/mesh_core/settings.c @@ -363,13 +363,18 @@ static int rpl_set(const char *name) for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) { u16_t src = net_buf_simple_pull_le16(buf); + + if (!BLE_MESH_ADDR_IS_UNICAST(src)) { + BT_ERR("Invalid source address 0x%04x", src); + continue; + } + sprintf(get, "mesh/rpl/%04x", src); err = bt_mesh_load_core_settings(get, (u8_t *)&rpl, sizeof(rpl), &exist); if (err) { BT_ERR("Failed to load RPL entry 0x%04x", src); - bt_mesh_rpl_reset(); - goto free; + continue; } if (exist == false) { @@ -386,10 +391,11 @@ static int rpl_set(const char *name) } } - BT_INFO("Restored RPL entry 0x%04x: seq 0x%06x, old_iv %u", src, rpl.seq, rpl.old_iv); entry->src = src; entry->seq = rpl.seq; entry->old_iv = rpl.old_iv; + + BT_INFO("Restored RPL entry 0x%04x: seq 0x%06x, old_iv %u", src, rpl.seq, rpl.old_iv); } free: @@ -438,7 +444,7 @@ static int net_key_set(const char *name) err = bt_mesh_load_core_settings(get, (u8_t *)&key, sizeof(key), &exist); if (err) { BT_ERR("Failed to load NetKey 0x%03x", net_idx); - goto free; + continue; } if (exist == false) { @@ -498,7 +504,7 @@ static int app_key_set(const char *name) err = bt_mesh_load_core_settings(get, (u8_t *)&key, sizeof(key), &exist); if (err) { BT_ERR("Failed to load AppKey 0x%03x", app_idx); - goto free; + continue; } if (exist == false) { @@ -508,8 +514,7 @@ static int app_key_set(const char *name) sub = bt_mesh_subnet_get(key.net_idx); if (!sub) { BT_ERR("Failed to find subnet 0x%03x", key.net_idx); - err = -ENOENT; - goto free; + continue; } app = bt_mesh_app_key_find(app_idx); @@ -685,7 +690,7 @@ static int model_set_pub(bool vnd, struct bt_mesh_model *model, u16_t model_key) model->pub->period = 0U; model->pub->retransmit = 0U; model->pub->count = 0U; - return 0; + return -EIO; } if (exist == false) { @@ -700,8 +705,7 @@ static int model_set_pub(bool vnd, struct bt_mesh_model *model, u16_t model_key) model->pub->retransmit = pub.retransmit; model->pub->count = 0U; - BT_INFO("Restored Model Publication, address 0x%04x, app_idx 0x%03x", - pub.addr, pub.key); + BT_INFO("Restored Model Publication, address 0x%04x, app_idx 0x%03x", pub.addr, pub.key); return 0; } @@ -712,7 +716,6 @@ static int model_set(bool vnd, const char *name) struct net_buf_simple *buf = NULL; u8_t elem_idx = 0U, model_idx = 0U; size_t length = 0U; - int err = 0; int i; BT_DBG("%s", __func__); @@ -726,6 +729,7 @@ static int model_set(bool vnd, const char *name) for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) { u16_t model_key = net_buf_simple_pull_le16(buf); + elem_idx = BLE_MESH_GET_ELEM_IDX(model_key); model_idx = BLE_MESH_GET_MODEL_IDX(model_key); @@ -733,29 +737,16 @@ static int model_set(bool vnd, const char *name) if (!model) { BT_ERR("%s model not found, elem_idx %u, model_idx %u", vnd ? "vnd" : "sig", elem_idx, model_idx); - err = -ENOENT; - goto free; + continue; } - err = model_set_bind(vnd, model, model_key); - if (err) { - goto free; - } - - err = model_set_sub(vnd, model, model_key); - if (err) { - goto free; - } - - err = model_set_pub(vnd, model, model_key); - if (err) { - goto free; - } + model_set_bind(vnd, model, model_key); + model_set_sub(vnd, model, model_key); + model_set_pub(vnd, model, model_key); } -free: bt_mesh_free_buf(buf); - return err; + return 0; } static int sig_mod_set(const char *name) @@ -796,7 +787,7 @@ static int va_set(const char *name) err = bt_mesh_load_core_settings(get, (u8_t *)&va, sizeof(va), &exist); if (err) { BT_ERR("Failed to load virtual address 0x%04x", index); - goto free; + continue; } if (exist == false) { @@ -805,7 +796,7 @@ static int va_set(const char *name) if (va.ref == 0) { BT_DBG("Ignore virtual address %s with ref = 0", get); - goto free; + continue; } lab = get_label(index); @@ -970,7 +961,7 @@ static int p_net_key_set(const char *name) err = bt_mesh_load_core_settings(get, (u8_t *)&key, sizeof(key), &exist); if (err) { BT_ERR("Failed to load NetKey 0x%03x", net_idx); - goto free; + continue; } if (exist == false) { @@ -1030,7 +1021,7 @@ static int p_app_key_set(const char *name) err = bt_mesh_load_core_settings(get, (u8_t *)&key, sizeof(key), &exist); if (err) { BT_ERR("Failed to load AppKey 0x%03x", app_idx); - goto free; + continue; } if (exist == false) { @@ -1040,8 +1031,7 @@ static int p_app_key_set(const char *name) sub = bt_mesh_provisioner_subnet_get(key.net_idx); if (!sub) { BT_ERR("Failed to find subnet 0x%03x", key.net_idx); - err = -ENOENT; - goto free; + continue; } app = bt_mesh_provisioner_app_key_find(app_idx); From 825ceb726dd84054a13b212cfdcbc08f4bd55359 Mon Sep 17 00:00:00 2001 From: lly Date: Wed, 27 May 2020 11:27:20 +0800 Subject: [PATCH 11/12] ble_mesh: stack: Fix storing next net_idx and app_idx --- components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h | 1 + components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c | 5 +++-- components/bt/esp_ble_mesh/mesh_core/provisioner_main.c | 2 ++ components/bt/esp_ble_mesh/mesh_core/settings.c | 4 ++-- 4 files changed, 8 insertions(+), 4 deletions(-) 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 ff0ee666d9..e33c960364 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 @@ -1132,6 +1132,7 @@ typedef union { */ struct ble_mesh_provisioner_add_local_app_key_comp_param { int err_code; /*!< Indicate the result of adding local AppKey by the Provisioner */ + uint16_t net_idx; /*!< NetKey Index */ uint16_t app_idx; /*!< AppKey Index */ } provisioner_add_app_key_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_APP_KEY_COMP_EVT */ /** 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 e61118c6be..22f9b7ce05 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 @@ -1818,10 +1818,11 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg) app_key = arg->add_local_app_key.app_key; } act = ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_APP_KEY_COMP_EVT; - param.provisioner_add_app_key_comp.app_idx = arg->add_local_app_key.app_idx; param.provisioner_add_app_key_comp.err_code = bt_mesh_provisioner_local_app_key_add(app_key, arg->add_local_app_key.net_idx, &arg->add_local_app_key.app_idx); + param.provisioner_add_app_key_comp.net_idx = arg->add_local_app_key.net_idx; + param.provisioner_add_app_key_comp.app_idx = arg->add_local_app_key.app_idx; break; } case BTC_BLE_MESH_ACT_PROVISIONER_UPDATE_LOCAL_APP_KEY: @@ -1851,9 +1852,9 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg) net_key = arg->add_local_net_key.net_key; } act = ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_NET_KEY_COMP_EVT; - param.provisioner_add_net_key_comp.net_idx = arg->add_local_net_key.net_idx; param.provisioner_add_net_key_comp.err_code = bt_mesh_provisioner_local_net_key_add(net_key, &arg->add_local_net_key.net_idx); + param.provisioner_add_net_key_comp.net_idx = arg->add_local_net_key.net_idx; break; } case BTC_BLE_MESH_ACT_PROVISIONER_UPDATE_LOCAL_NET_KEY: diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c index 9a2899ae12..59c64f1cf2 100644 --- a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c @@ -1027,6 +1027,7 @@ int bt_mesh_provisioner_local_app_key_add(const u8_t app_key[16], } } *app_idx = key->app_idx; + bt_mesh.p_app_idx_next++; } key->updated = false; @@ -1283,6 +1284,7 @@ int bt_mesh_provisioner_local_net_key_add(const u8_t net_key[16], u16_t *net_idx } } *net_idx = sub->net_idx; + bt_mesh.p_net_idx_next++; } sub->kr_phase = BLE_MESH_KR_NORMAL; sub->kr_flag = false; diff --git a/components/bt/esp_ble_mesh/mesh_core/settings.c b/components/bt/esp_ble_mesh/mesh_core/settings.c index 26a4d6d56a..3b43bed824 100644 --- a/components/bt/esp_ble_mesh/mesh_core/settings.c +++ b/components/bt/esp_ble_mesh/mesh_core/settings.c @@ -2389,7 +2389,7 @@ static void store_p_app_key(struct bt_mesh_app_key *app) void bt_mesh_store_p_net_idx(void) { - BT_DBG("p_net_idx_next 0x%03x", bt_mesh.p_net_idx_next); + BT_DBG("Store, p_net_idx_next 0x%03x", bt_mesh.p_net_idx_next); bt_mesh_save_core_settings("mesh/p_netidx", (const u8_t *)&bt_mesh.p_net_idx_next, sizeof(bt_mesh.p_net_idx_next)); @@ -2403,7 +2403,7 @@ void bt_mesh_clear_p_net_idx(void) void bt_mesh_store_p_app_idx(void) { - BT_DBG("p_app_idx_next 0x%03x", bt_mesh.p_app_idx_next); + BT_DBG("Store, p_app_idx_next 0x%03x", bt_mesh.p_app_idx_next); bt_mesh_save_core_settings("mesh/p_appidx", (const u8_t *)&bt_mesh.p_app_idx_next, sizeof(bt_mesh.p_app_idx_next)); From b453c1268a37757f2d5ff78f43f4355b2d59e389 Mon Sep 17 00:00:00 2001 From: lly Date: Fri, 29 May 2020 10:06:34 +0800 Subject: [PATCH 12/12] ble_mesh: stack: Use settings_core_erase when deinit --- components/bt/esp_ble_mesh/mesh_core/main.c | 5 +- .../bt/esp_ble_mesh/mesh_core/settings.c | 51 +++++++++++-------- .../bt/esp_ble_mesh/mesh_core/settings.h | 13 ++--- .../mesh_core/storage/settings_nvs.c | 15 +++--- .../mesh_core/storage/settings_nvs.h | 2 +- 5 files changed, 47 insertions(+), 39 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_core/main.c b/components/bt/esp_ble_mesh/mesh_core/main.c index 17d0a21c08..85934f0a9d 100644 --- a/components/bt/esp_ble_mesh/mesh_core/main.c +++ b/components/bt/esp_ble_mesh/mesh_core/main.c @@ -517,10 +517,7 @@ int bt_mesh_deinit(struct bt_mesh_deinit_param *param) bt_mesh_comp_unprovision(); if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { - if (param->erase) { - bt_mesh_clear_role(); - } - bt_mesh_settings_deinit(); + bt_mesh_settings_deinit(param->erase); } bt_mesh_timer_deinit(); diff --git a/components/bt/esp_ble_mesh/mesh_core/settings.c b/components/bt/esp_ble_mesh/mesh_core/settings.c index 3b43bed824..6a7af58aeb 100644 --- a/components/bt/esp_ble_mesh/mesh_core/settings.c +++ b/components/bt/esp_ble_mesh/mesh_core/settings.c @@ -585,7 +585,8 @@ static int hb_pub_set(const char *name) hb_pub->count = 0U; } - BT_INFO("Restored Heartbeat Publication, dst 0x%04x", hb_pub->dst); + BT_INFO("Restored Heartbeat Publication, dst 0x%04x, period %d, net_idx 0x%03x", + hb_pub->dst, hb_pub->period, hb_pub->net_idx); return 0; } @@ -618,7 +619,10 @@ static int cfg_set(const char *name) memcpy(&stored_cfg.cfg, &val, sizeof(val)); stored_cfg.valid = true; - BT_INFO("Restored Configuration State"); + + BT_INFO("Restored Configuration, ttl %d, transmit 0x%02x, retransmit 0x%02x", + val.default_ttl, val.net_transmit, val.relay_retransmit); + return 0; } @@ -641,6 +645,10 @@ static int model_set_bind(bool vnd, struct bt_mesh_model *model, u16_t model_key return -EIO; } + if (exist == true) { + BT_INFO("Restored Model Bound AppKey, index %s", bt_hex(model->keys, sizeof(model->keys))); + } + return 0; } @@ -663,6 +671,10 @@ static int model_set_sub(bool vnd, struct bt_mesh_model *model, u16_t model_key) return -EIO; } + if (exist == true) { + BT_INFO("Restored Model Subscription, address %s", bt_hex(model->groups, sizeof(model->groups))); + } + return 0; } @@ -1467,7 +1479,7 @@ static void schedule_store(int flag) return; } - BT_INFO("Waiting %d seconds", timeout / MSEC_PER_SEC); + BT_INFO("Settings store, waiting %d seconds", timeout / MSEC_PER_SEC); if (timeout) { k_delayed_work_submit(&pending_store, timeout); @@ -2602,35 +2614,34 @@ void bt_mesh_store_node_comp_data(struct bt_mesh_node *node) int settings_core_init(void) { - BT_DBG("%s", __func__); - k_delayed_work_init(&pending_store, store_pending); - - return 0; -} - -int bt_mesh_settings_init(void) -{ - BT_DBG("%s", __func__); - - bt_mesh_settings_mutex_new(); - bt_mesh_settings_init_foreach(); - return 0; } int settings_core_deinit(void) { k_delayed_work_free(&pending_store); - return 0; } -int bt_mesh_settings_deinit(void) +int settings_core_erase(void) { - bt_mesh_settings_deinit_foreach(); - bt_mesh_settings_mutex_free(); + /* Erase here must not use the pending_store timer. */ + bt_mesh_clear_role(); + return 0; +} +int bt_mesh_settings_init(void) +{ + bt_mesh_settings_mutex_new(); + bt_mesh_settings_init_foreach(); + return 0; +} + +int bt_mesh_settings_deinit(bool erase) +{ + bt_mesh_settings_deinit_foreach(erase); + bt_mesh_settings_mutex_free(); return 0; } diff --git a/components/bt/esp_ble_mesh/mesh_core/settings.h b/components/bt/esp_ble_mesh/mesh_core/settings.h index 85a7d93c0e..39d33e0a69 100644 --- a/components/bt/esp_ble_mesh/mesh_core/settings.h +++ b/components/bt/esp_ble_mesh/mesh_core/settings.h @@ -19,11 +19,6 @@ extern "C" { #define BLE_MESH_SETTINGS_ROLE_PROV (BIT(BLE_MESH_PROVISIONER)) #define BLE_MESH_SETTINGS_ROLE_BIT_MASK (BIT(BLE_MESH_NODE) | BIT(BLE_MESH_PROVISIONER)) -int settings_core_init(void); -int settings_core_load(void); -int settings_core_commit(void); -int settings_core_deinit(void); - void bt_mesh_store_role(void); void bt_mesh_store_net(void); void bt_mesh_store_iv(bool only_duration); @@ -67,8 +62,14 @@ void bt_mesh_store_node_comp_data(struct bt_mesh_node *node); void bt_mesh_settings_lock(void); void bt_mesh_settings_unlock(void); +int settings_core_init(void); +int settings_core_load(void); +int settings_core_commit(void); +int settings_core_deinit(void); +int settings_core_erase(void); + int bt_mesh_settings_init(void); -int bt_mesh_settings_deinit(void); +int bt_mesh_settings_deinit(bool erase); #ifdef __cplusplus } diff --git a/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.c b/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.c index 2747d274d9..af84ab4d7e 100644 --- a/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.c +++ b/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.c @@ -23,7 +23,6 @@ enum settings_type { SETTINGS_CORE, - SETTINGS_SERVER, }; struct settings_context { @@ -44,12 +43,7 @@ static struct settings_context settings_ctx[] = { .settings_load = settings_core_load, .settings_commit = settings_core_commit, .settings_deinit = settings_core_deinit, - }, - [SETTINGS_SERVER] = { - .nvs_name = "mesh_server", - .settings_init = NULL, - .settings_load = NULL, - .settings_commit = NULL, + .settings_erase = settings_core_erase, }, }; @@ -98,7 +92,7 @@ void bt_mesh_settings_init_foreach(void) } } -void bt_mesh_settings_deinit_foreach(void) +void bt_mesh_settings_deinit_foreach(bool erase) { int i; @@ -110,6 +104,11 @@ void bt_mesh_settings_deinit_foreach(void) continue; } + if (erase && ctx->settings_erase && ctx->settings_erase()) { + BT_ERR("Erase settings failed, name %s", ctx->nvs_name); + continue; + } + nvs_close(ctx->handle); } diff --git a/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.h b/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.h index b83c358956..91c7341e67 100644 --- a/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.h +++ b/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.h @@ -31,7 +31,7 @@ typedef nvs_handle_t bt_mesh_nvs_handle_t; #define BLE_MESH_GET_MODEL_KEY(a, b) ((u16_t)(((u16_t)((a) << 8)) | (b))) void bt_mesh_settings_init_foreach(void); -void bt_mesh_settings_deinit_foreach(void); +void bt_mesh_settings_deinit_foreach(bool erase); int bt_mesh_save_settings(bt_mesh_nvs_handle_t handle, const char *key, const u8_t *val, size_t len);