Skip to content

Commit

Permalink
[Bouffalolab] Improve factory reset to make sure all configuration er…
Browse files Browse the repository at this point in the history
…ased (project-chip#29367)

* do factory reset during startup if pending flag set

* fix restyle
  • Loading branch information
wy-hh authored Sep 22, 2023
1 parent 162ea95 commit dffed35
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 8 deletions.
6 changes: 3 additions & 3 deletions examples/platform/bouffalolab/common/plat/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

#include <AppTask.h>

#include <easyflash.h>
#include <BLConfig.h>

#include <plat.h>

using namespace ::chip;
Expand Down Expand Up @@ -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__);
Expand Down
33 changes: 30 additions & 3 deletions src/platform/bouffalolab/common/BLConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions src/platform/bouffalolab/common/BLConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion third_party/bouffalolab/bl602/bl_iot_sdk.gni
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion third_party/bouffalolab/bl702/bl_iot_sdk.gni
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions third_party/bouffalolab/bl702l/bl_iot_sdk.gni
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit dffed35

Please sign in to comment.