From 5c2e8930f9d67fb0464f13add38ddf5304ffb637 Mon Sep 17 00:00:00 2001 From: lly Date: Wed, 4 Mar 2020 18:40:14 +0800 Subject: [PATCH] ble_mesh: Add an API to directly erase mesh info --- .../api/core/esp_ble_mesh_networking_api.c | 13 +++++++ .../include/esp_ble_mesh_networking_api.h | 12 ++++++ .../bt/esp_ble_mesh/api/esp_ble_mesh_defs.h | 7 ++++ .../bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c | 6 +++ .../btc/include/btc_ble_mesh_prov.h | 1 + .../bt/esp_ble_mesh/mesh_core/settings.c | 33 ++++++++++++++++ .../bt/esp_ble_mesh/mesh_core/settings.h | 1 + .../mesh_core/storage/settings_nvs.c | 39 +++++++++++++++++++ .../mesh_core/storage/settings_nvs.h | 3 ++ 9 files changed, 115 insertions(+) diff --git a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c index b21cc9bafe..19fd4921cd 100644 --- a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c +++ b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c @@ -724,6 +724,19 @@ uint8_t esp_ble_mesh_provisioner_get_free_settings_user_id_count(void) } #endif /* CONFIG_BLE_MESH_USE_MULTIPLE_NAMESPACE */ +esp_err_t esp_ble_mesh_provisioner_direct_erase_settings(void) +{ + btc_msg_t msg = {0}; + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_DIRECT_ERASE_SETTINGS; + + return (btc_transfer_context(&msg, NULL, 0, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + esp_err_t esp_ble_mesh_provisioner_start_recv_heartbeat(void) { btc_msg_t msg = {0}; diff --git a/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_networking_api.h b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_networking_api.h index 63f71cbd7d..dc826167e9 100644 --- a/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_networking_api.h +++ b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_networking_api.h @@ -614,6 +614,18 @@ uint8_t esp_ble_mesh_provisioner_get_settings_index(const char *user_id); */ uint8_t esp_ble_mesh_provisioner_get_free_settings_user_id_count(void); +/** + * @brief This function is called by Provisioner to directly erase the stored + * mesh information from flash. + * + * @note This function can be invoked when the mesh stack is not initialized + * or has been deinitialized. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_direct_erase_settings(void); + /** * @brief This function is called by Provisioner to start receiving and processing * heartbeat messages. 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 f1b6261477..ff908c82b7 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 @@ -806,6 +806,7 @@ typedef enum { ESP_BLE_MESH_PROVISIONER_RELEASE_SETTINGS_WITH_USER_ID_COMP_EVT, /*!< Provisioner release settings with user_id completion event */ ESP_BLE_MESH_PROVISIONER_DELETE_SETTINGS_WITH_INDEX_COMP_EVT, /*!< Provisioner delete settings with index completion event */ ESP_BLE_MESH_PROVISIONER_DELETE_SETTINGS_WITH_USER_ID_COMP_EVT, /*!< Provisioner delete settings with user_id completion event */ + ESP_BLE_MESH_PROVISIONER_DRIECT_ERASE_SETTINGS_COMP_EVT, /*!< Provisioner directly erase settings completion event */ ESP_BLE_MESH_PROVISIONER_START_RECV_HEARTBEAT_COMP_EVT, /*!< Provisioner start to receive Heartbeat message completion event */ ESP_BLE_MESH_PROVISIONER_SET_HEARTBEAT_FILTER_TYPE_COMP_EVT, /*!< Provisioner set the filter type of receiving heartbeat message completion event */ ESP_BLE_MESH_PROVISIONER_SET_HEARTBEAT_FILTER_INFO_COMP_EVT, /*!< Provisioner set the filter information of receiving heartbeat message completion event */ @@ -1217,6 +1218,12 @@ typedef union { int err_code; /*!< Indicate the result of deleting settings with user_id by the Provisioner */ uint8_t index; /*!< Index of Provisioner settings */ } provisioner_delete_settings_with_user_id_comp; /*!< Event parameters of ESP_BLE_MESH_PROVISIONER_DELETE_SETTINGS_WITH_USER_ID_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_DRIECT_ERASE_SETTINGS_COMP_EVT + */ + struct ble_mesh_provisioner_direct_erase_settings_comp_param { + int err_code; /*!< Indicate the result of directly erasing settings by the Provisioner */ + } provisioner_direct_erase_settings_comp; /*!< Event parameters of ESP_BLE_MESH_PROVISIONER_DRIECT_ERASE_SETTINGS_COMP_EVT */ /** * @brief ESP_BLE_MESH_PROVISIONER_START_RECV_HEARTBEAT_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 83795758b1..29b072d31d 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 @@ -1890,6 +1890,12 @@ void btc_ble_mesh_prov_call_handler(btc_msg_t *msg) ¶m.provisioner_delete_settings_with_user_id_comp.index); break; #endif /* CONFIG_BLE_MESH_USE_MULTIPLE_NAMESPACE */ +#if CONFIG_BLE_MESH_SETTINGS + case BTC_BLE_MESH_ACT_PROVISIONER_DIRECT_ERASE_SETTINGS: + act = ESP_BLE_MESH_PROVISIONER_DRIECT_ERASE_SETTINGS_COMP_EVT; + param.provisioner_direct_erase_settings_comp.err_code = bt_mesh_provisioner_direct_erase_settings(); + break; +#endif /* CONFIG_BLE_MESH_SETTINGS */ case BTC_BLE_MESH_ACT_PROVISIONER_START_RECV_HEARTBEAT: act = ESP_BLE_MESH_PROVISIONER_START_RECV_HEARTBEAT_COMP_EVT; param.provisioner_start_recv_heartbeat_comp.err_code = diff --git a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h index 66174eac03..57a47b485d 100644 --- a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h +++ b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h @@ -66,6 +66,7 @@ typedef enum { BTC_BLE_MESH_ACT_PROVISIONER_START_RECV_HEARTBEAT, BTC_BLE_MESH_ACT_PROVISIONER_SET_HEARTBEAT_FILTER_TYPE, BTC_BLE_MESH_ACT_PROVISIONER_SET_HEARTBEAT_FILTER_INFO, + BTC_BLE_MESH_ACT_PROVISIONER_DIRECT_ERASE_SETTINGS, BTC_BLE_MESH_ACT_SET_FAST_PROV_INFO, BTC_BLE_MESH_ACT_SET_FAST_PROV_ACTION, BTC_BLE_MESH_ACT_LPN_ENABLE, diff --git a/components/bt/esp_ble_mesh/mesh_core/settings.c b/components/bt/esp_ble_mesh/mesh_core/settings.c index b58ae32efa..b4ff517a79 100644 --- a/components/bt/esp_ble_mesh/mesh_core/settings.c +++ b/components/bt/esp_ble_mesh/mesh_core/settings.c @@ -3189,6 +3189,7 @@ int bt_mesh_provisioner_release_settings_with_user_id(const char *user_id, bool return provisioner_settings_release(idx, erase); } +#endif /* CONFIG_BLE_MESH_USE_MULTIPLE_NAMESPACE */ static int role_erase(nvs_handle handle, const char *name) { @@ -3518,6 +3519,7 @@ int bt_mesh_settings_erase_by_handle(nvs_handle handle) return 0; } +#if CONFIG_BLE_MESH_USE_MULTIPLE_NAMESPACE static int settings_user_id_delete(u8_t index) { char nvs_name[16] = {'\0'}; @@ -3639,6 +3641,37 @@ u8_t bt_mesh_provisioner_get_free_settings_user_id_count(void) return count; } #endif /* CONFIG_BLE_MESH_USE_MULTIPLE_NAMESPACE */ + +int bt_mesh_provisioner_direct_erase_settings(void) +{ + nvs_handle handle = 0; + int err = 0; + + err = bt_mesh_settings_direct_open(&handle); + if (err) { + return err; + } + +#if CONFIG_BLE_MESH_USE_MULTIPLE_NAMESPACE + for (int i = 0; i < ARRAY_SIZE(settings_name); i++) { + err = settings_user_id_delete(i); + if (err) { + BT_ERR("%s, Failed to erase settings %d", __func__, i); + return err; + } + } + bt_mesh_save_user_id_settings("mesh/uid", NULL, 0); +#else + err = bt_mesh_settings_erase_by_handle(handle); + if (err) { + BT_ERR("%s, Failed to erase settings", __func__); + return err; + } +#endif + + bt_mesh_settings_direct_close(); + return 0; +} #endif /* CONFIG_BLE_MESH_PROVISIONER */ int settings_core_init(void) diff --git a/components/bt/esp_ble_mesh/mesh_core/settings.h b/components/bt/esp_ble_mesh/mesh_core/settings.h index 09ce73905a..ddf3559ce7 100644 --- a/components/bt/esp_ble_mesh/mesh_core/settings.h +++ b/components/bt/esp_ble_mesh/mesh_core/settings.h @@ -78,6 +78,7 @@ const char *bt_mesh_provisioner_get_settings_user_id(u8_t index); u8_t bt_mesh_provisioner_get_settings_index(const char *user_id); u8_t bt_mesh_provisioner_get_free_settings_user_id_count(void); #endif /* CONFIG_BLE_MESH_USE_MULTIPLE_NAMESPACE */ +int bt_mesh_provisioner_direct_erase_settings(void); #endif /* CONFIG_BLE_MESH_PROVISIONER */ void bt_mesh_settings_lock(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 84645d4cec..9b6c4c1f27 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 @@ -80,6 +80,45 @@ void bt_mesh_settings_nvs_close(nvs_handle handle) nvs_close(handle); } +int bt_mesh_settings_direct_open(nvs_handle *handle) +{ + int err = 0; + +#if CONFIG_BLE_MESH_SPECIFIC_PARTITION + err = nvs_flash_init_partition(CONFIG_BLE_MESH_PARTITION_NAME); + if (err != ESP_OK) { + BT_ERR("Failed to init partition %s, err %d", CONFIG_BLE_MESH_PARTITION_NAME, err); + return -EIO; + } +#endif + + for (int i = 0; i < ARRAY_SIZE(settings_ctx); i++) { + struct settings_context *ctx = &settings_ctx[i]; + err = bt_mesh_settings_nvs_open(ctx->nvs_name, &ctx->handle); + if (err) { + BT_ERR("%s, Failed to open %s, err %d", __func__, ctx->nvs_name, err); + return -EIO; + } + if (i == SETTINGS_CORE && handle) { + *handle = ctx->handle; + } + } + + return 0; +} + +void bt_mesh_settings_direct_close(void) +{ + for (int i = 0; i < ARRAY_SIZE(settings_ctx); i++) { + struct settings_context *ctx = &settings_ctx[i]; + bt_mesh_settings_nvs_close(ctx->handle); + } + +#if CONFIG_BLE_MESH_SPECIFIC_PARTITION + nvs_flash_deinit_partition(CONFIG_BLE_MESH_PARTITION_NAME); +#endif +} + void bt_mesh_settings_foreach(void) { struct settings_context *ctx = NULL; 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 4e15bfadcf..f6d76090c5 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 @@ -35,6 +35,9 @@ void bt_mesh_settings_deforeach(bool erase); int bt_mesh_settings_nvs_open(const char* name, nvs_handle *handle); void bt_mesh_settings_nvs_close(nvs_handle handle); +int bt_mesh_settings_direct_open(nvs_handle *handle); +void bt_mesh_settings_direct_close(void); + int bt_mesh_save_settings(nvs_handle handle, const char *key, const u8_t *val, size_t len); int bt_mesh_load_settings(nvs_handle handle, const char *key, u8_t *buf, size_t buf_len, bool *exist); struct net_buf_simple *bt_mesh_get_settings_item(nvs_handle handle, const char *key);