From 1148030501e6eb82bd6ec3bb754865956a05b72e Mon Sep 17 00:00:00 2001 From: wyhong <30567533+wy-hh@users.noreply.github.com> Date: Fri, 22 Sep 2023 21:43:01 +0800 Subject: [PATCH] [Bouffalolab] Improve factory reset to make sure all configuration erased (#29367) * do factory reset during startup if pending flag set * fix restyle --- .../platform/bouffalolab/common/plat/main.cpp | 6 ++-- src/platform/bouffalolab/common/BLConfig.cpp | 33 +++++++++++++++++-- src/platform/bouffalolab/common/BLConfig.h | 4 +++ third_party/bouffalolab/bl602/bl_iot_sdk.gni | 2 +- third_party/bouffalolab/bl702/bl_iot_sdk.gni | 2 +- third_party/bouffalolab/bl702l/bl_iot_sdk.gni | 2 ++ 6 files changed, 41 insertions(+), 8 deletions(-) diff --git a/examples/platform/bouffalolab/common/plat/main.cpp b/examples/platform/bouffalolab/common/plat/main.cpp index 65e8efce1cc377..cb772c21f6ef87 100644 --- a/examples/platform/bouffalolab/common/plat/main.cpp +++ b/examples/platform/bouffalolab/common/plat/main.cpp @@ -18,7 +18,8 @@ #include -#include +#include + #include using namespace ::chip; @@ -46,8 +47,7 @@ extern "C" int START_ENTRY(void) { platform_port_init(); - easyflash_init(); - ef_load_env_cache(); + Internal::BLConfig::Init(); ChipLogProgress(NotSpecified, "=================================================="); ChipLogProgress(NotSpecified, "bouffalolab chip-lighting-example, built at " __DATE__ " " __TIME__); diff --git a/src/platform/bouffalolab/common/BLConfig.cpp b/src/platform/bouffalolab/common/BLConfig.cpp index ff03eb662a1bb7..818c64069b71df 100644 --- a/src/platform/bouffalolab/common/BLConfig.cpp +++ b/src/platform/bouffalolab/common/BLConfig.cpp @@ -24,6 +24,34 @@ namespace chip { namespace DeviceLayer { namespace Internal { +void BLConfig::Init(void) +{ + easyflash_init(); + ef_load_env_cache(); + + if (ef_get_env(kBLKey_factoryResetFlag)) + { + + ef_print_env_cb([](env_node_obj_t env, void * arg1, void * arg2) { + if (ENV_WRITE == env->status) + { + env->name[env->name_len] = '\0'; + if (strncmp(kBLKey_factoryResetFlag, env->name, sizeof(env->name) - 1)) + { + /** delete all key=value except factory reset flag */ + ef_del_and_save_env(env->name); + } + } + + return false; + }); + + ef_del_and_save_env(kBLKey_factoryResetFlag); + ef_env_set_default(); + ef_load_env_cache(); + } +} + CHIP_ERROR BLConfig::ReadConfigValue(const char * key, uint8_t * val, size_t size, size_t & readsize) { env_node_obj node; @@ -177,9 +205,8 @@ CHIP_ERROR BLConfig::ClearConfigValue(const char * key) CHIP_ERROR BLConfig::FactoryResetConfig(void) { - // Only reset config section information - - ef_env_set_default(); + /** set __factory_reset_pending here, let do factory reset operation during startup */ + ef_set_and_save_env(kBLKey_factoryResetFlag, "pending"); return CHIP_NO_ERROR; } diff --git a/src/platform/bouffalolab/common/BLConfig.h b/src/platform/bouffalolab/common/BLConfig.h index 4dfc3dc9a1f6a4..50078facffa218 100644 --- a/src/platform/bouffalolab/common/BLConfig.h +++ b/src/platform/bouffalolab/common/BLConfig.h @@ -75,6 +75,10 @@ class BLConfig static constexpr const char * kCounterKey_TotalOperationalHours = ("total-hours"); static constexpr const char * kCounterKey_UpTime = ("up-time"); + static constexpr const char * kBLKey_factoryResetFlag = ("__factory_reset_pending"); + + static void Init(void); + // Config value accessors. static CHIP_ERROR ReadConfigValue(const char * key, uint8_t * val, size_t size, size_t & readsize); static CHIP_ERROR ReadConfigValue(const char * key, bool & val); diff --git a/third_party/bouffalolab/bl602/bl_iot_sdk.gni b/third_party/bouffalolab/bl602/bl_iot_sdk.gni index 88484ce98e641b..1a7cb6b5f9f13d 100644 --- a/third_party/bouffalolab/bl602/bl_iot_sdk.gni +++ b/third_party/bouffalolab/bl602/bl_iot_sdk.gni @@ -369,7 +369,7 @@ template("bl_iot_sdk") { "${bl_iot_sdk_root}/components/stage/yloop/src/yloop.c", ] - defines = [ "EF_ENV_CACHE_TABLE_SIZE=32" ] + defines = [ "EF_ENV_CACHE_TABLE_SIZE=64" ] cflags_c = [ "-Wno-sign-compare" ] configs += [ ":${sdk_target_name}_config_BSP_Driver", diff --git a/third_party/bouffalolab/bl702/bl_iot_sdk.gni b/third_party/bouffalolab/bl702/bl_iot_sdk.gni index 805732512ace5b..5775b982b81332 100644 --- a/third_party/bouffalolab/bl702/bl_iot_sdk.gni +++ b/third_party/bouffalolab/bl702/bl_iot_sdk.gni @@ -425,7 +425,7 @@ template("bl_iot_sdk") { "${bl_iot_sdk_root}/components/stage/yloop/src/select.c", "${bl_iot_sdk_root}/components/stage/yloop/src/yloop.c", ] - defines = [ "EF_ENV_CACHE_TABLE_SIZE=32" ] + defines = [ "EF_ENV_CACHE_TABLE_SIZE=64" ] cflags_c = [ "-Wno-sign-compare" ] configs += [ ":${sdk_target_name}_config_BSP_Driver", diff --git a/third_party/bouffalolab/bl702l/bl_iot_sdk.gni b/third_party/bouffalolab/bl702l/bl_iot_sdk.gni index c84a3b2cb8d8af..3ef1e51035e88a 100644 --- a/third_party/bouffalolab/bl702l/bl_iot_sdk.gni +++ b/third_party/bouffalolab/bl702l/bl_iot_sdk.gni @@ -351,6 +351,8 @@ template("bl_iot_sdk") { "${bl_iot_sdk_root}/components/stage/yloop/src/select.c", "${bl_iot_sdk_root}/components/stage/yloop/src/yloop.c", ] + + defines = [ "EF_ENV_CACHE_TABLE_SIZE=64" ] cflags_c = [ "-Wno-sign-compare" ] configs += [ ":${sdk_target_name}_config_bl702l_rom_a0",