diff --git a/scripts/build/builders/bouffalolab.py b/scripts/build/builders/bouffalolab.py index d276cd12a5ff64..09d5f250f5b221 100644 --- a/scripts/build/builders/bouffalolab.py +++ b/scripts/build/builders/bouffalolab.py @@ -162,7 +162,7 @@ def __init__(self, self.argsOpt.append(f'chip_mdns="{chip_mdns}"') self.argsOpt.append(f'chip_inet_config_enable_ipv4={str(enable_ethernet or enable_wifi).lower()}') - + if enable_easyflash and enable_littlefs: raise Exception("Only one of easyflash and littlefs can be enabled.") diff --git a/src/platform/bouffalolab/BL602/BUILD.gn b/src/platform/bouffalolab/BL602/BUILD.gn index aaae151c20a34e..d4cd04c739c6cd 100644 --- a/src/platform/bouffalolab/BL602/BUILD.gn +++ b/src/platform/bouffalolab/BL602/BUILD.gn @@ -61,11 +61,10 @@ static_library("BL602") { "../common/FactoryDataProvider.h", ] } - + if (bouffalo_sdk_component_easyflash_enabled) { sources += [ "../common/BLConfig.cpp" ] - } - else { + } else { sources += [ "../common/BLConfig_littlefs.cpp" ] } diff --git a/src/platform/bouffalolab/BL702/BUILD.gn b/src/platform/bouffalolab/BL702/BUILD.gn index 7e12fb9cd62405..e0daee04056a51 100644 --- a/src/platform/bouffalolab/BL702/BUILD.gn +++ b/src/platform/bouffalolab/BL702/BUILD.gn @@ -65,11 +65,10 @@ static_library("BL702") { "../common/FactoryDataProvider.h", ] } - + if (bouffalo_sdk_component_easyflash_enabled) { sources += [ "../common/BLConfig.cpp" ] - } - else { + } else { sources += [ "../common/BLConfig_littlefs.cpp" ] } diff --git a/src/platform/bouffalolab/BL702L/BUILD.gn b/src/platform/bouffalolab/BL702L/BUILD.gn index 227a93101d3f1f..2529c8d3a7e736 100644 --- a/src/platform/bouffalolab/BL702L/BUILD.gn +++ b/src/platform/bouffalolab/BL702L/BUILD.gn @@ -63,11 +63,10 @@ static_library("BL702L") { "../common/FactoryDataProvider.h", ] } - + if (bouffalo_sdk_component_easyflash_enabled) { sources += [ "../common/BLConfig.cpp" ] - } - else { + } else { sources += [ "../common/BLConfig_littlefs.cpp" ] } diff --git a/src/platform/bouffalolab/common/BLConfig_littlefs.cpp b/src/platform/bouffalolab/common/BLConfig_littlefs.cpp index 999e315daaac23..5e9a0b1daab619 100644 --- a/src/platform/bouffalolab/common/BLConfig_littlefs.cpp +++ b/src/platform/bouffalolab/common/BLConfig_littlefs.cpp @@ -15,19 +15,19 @@ * limitations under the License. */ -#include +#include #include +#include #include #include -#include #include extern "C" { #include } -#include #include +#include #ifndef BLCONFIG_LFS_NAMESPACE #define BLCONFIG_LFS_NAMESPACE "/_blcfg_" @@ -41,66 +41,73 @@ namespace chip { namespace DeviceLayer { namespace Internal { -static lfs_t * blconfig_lfs = nullptr; +static lfs_t * blconfig_lfs = nullptr; -static inline std::string blcfg_convert_key(std::string key, std::string lfs_ns = std::string("")) +static inline std::string blcfg_convert_key(std::string key, std::string lfs_ns = std::string("")) { return lfs_ns + std::string("/") + regex_replace(key, std::regex("/"), std::string(BLCONFIG_SLASH)); } -static CHIP_ERROR blcfg_do_factory_reset(void) +static CHIP_ERROR blcfg_do_factory_reset(void) { - CHIP_ERROR err = CHIP_NO_ERROR; - int ret; + CHIP_ERROR err = CHIP_NO_ERROR; + int ret; struct lfs_info stat; - lfs_file_t file; - lfs_dir_t dir = {}; - std::string factory_reset_key = blcfg_convert_key(std::string(BLConfig::kBLKey_factoryResetFlag)); + lfs_file_t file; + lfs_dir_t dir = {}; + std::string factory_reset_key = blcfg_convert_key(std::string(BLConfig::kBLKey_factoryResetFlag)); blconfig_lfs->cfg->lock(blconfig_lfs->cfg); - ret = lfs_stat(blconfig_lfs, const_cast(factory_reset_key.c_str()), &stat); + ret = lfs_stat(blconfig_lfs, const_cast(factory_reset_key.c_str()), &stat); - if (LFS_ERR_OK == ret) { + if (LFS_ERR_OK == ret) + { err = CHIP_ERROR_PERSISTED_STORAGE_FAILED; - do { - ret = lfs_file_open(blconfig_lfs, &file, const_cast(factory_reset_key.c_str()), LFS_O_RDONLY); + do + { + ret = lfs_file_open(blconfig_lfs, &file, const_cast(factory_reset_key.c_str()), LFS_O_RDONLY); VerifyOrExit(ret == LFS_ERR_OK, err = CHIP_ERROR_PERSISTED_STORAGE_FAILED); lfs_file_close(blconfig_lfs, &file); ret = lfs_dir_open(blconfig_lfs, &dir, BLCONFIG_LFS_NAMESPACE); VerifyOrExit(ret == LFS_ERR_OK, err = CHIP_ERROR_PERSISTED_STORAGE_FAILED); - while (1) { + while (1) + { ret = lfs_dir_read(blconfig_lfs, &dir, &stat); - if (ret <= 0) { + if (ret <= 0) + { break; } - if (stat.type != LFS_TYPE_REG) { + if (stat.type != LFS_TYPE_REG) + { continue; } std::string delete_key = blcfg_convert_key(std::string(stat.name), std::string(BLCONFIG_LFS_NAMESPACE)); - ret = lfs_remove(blconfig_lfs, const_cast(delete_key.c_str())); - if (ret != LFS_ERR_OK) { + ret = lfs_remove(blconfig_lfs, const_cast(delete_key.c_str())); + if (ret != LFS_ERR_OK) + { break; } } lfs_dir_close(blconfig_lfs, &dir); - ret = lfs_remove(blconfig_lfs, const_cast(factory_reset_key.c_str())); - if (ret != LFS_ERR_OK) { + ret = lfs_remove(blconfig_lfs, const_cast(factory_reset_key.c_str())); + if (ret != LFS_ERR_OK) + { break; } err = CHIP_NO_ERROR; - } while(0); + } while (0); } -exit: +exit: blconfig_lfs->cfg->unlock(blconfig_lfs->cfg); return err; @@ -108,16 +115,17 @@ static CHIP_ERROR blcfg_do_factory_reset(void) void BLConfig::Init(void) { - CHIP_ERROR err = CHIP_NO_ERROR; - int ret; + CHIP_ERROR err = CHIP_NO_ERROR; + int ret; struct lfs_info stat; - + blconfig_lfs = lfs_xip_init(); VerifyOrExit(blconfig_lfs != NULL, err = CHIP_ERROR_PERSISTED_STORAGE_FAILED); /* init namespace */ ret = lfs_stat(blconfig_lfs, BLCONFIG_LFS_NAMESPACE, &stat); - if (ret != LFS_ERR_OK) { + if (ret != LFS_ERR_OK) + { ret = lfs_mkdir(blconfig_lfs, BLCONFIG_LFS_NAMESPACE); VerifyOrExit(ret == LFS_ERR_OK, err = CHIP_ERROR_PERSISTED_STORAGE_FAILED); @@ -132,18 +140,19 @@ void BLConfig::Init(void) CHIP_ERROR BLConfig::ReadConfigValue(const char * key, uint8_t * val, size_t size, size_t & readsize) { - CHIP_ERROR err = CHIP_NO_ERROR; - int ret = LFS_ERR_OK; - lfs_file_t file; - std::string read_key = blcfg_convert_key(std::string(key), std::string(BLCONFIG_LFS_NAMESPACE)); + CHIP_ERROR err = CHIP_NO_ERROR; + int ret = LFS_ERR_OK; + lfs_file_t file; + std::string read_key = blcfg_convert_key(std::string(key), std::string(BLCONFIG_LFS_NAMESPACE)); blconfig_lfs->cfg->lock(blconfig_lfs->cfg); - ret = lfs_file_open(blconfig_lfs, &file, const_cast(read_key.c_str()), LFS_O_RDONLY); + ret = lfs_file_open(blconfig_lfs, &file, const_cast(read_key.c_str()), LFS_O_RDONLY); VerifyOrExit(ret == LFS_ERR_OK, err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND); - if (val && size) { - ret = lfs_file_read(blconfig_lfs, &file, val, size); + if (val && size) + { + ret = lfs_file_read(blconfig_lfs, &file, val, size); readsize = ret; } lfs_file_close(blconfig_lfs, &file); @@ -203,20 +212,20 @@ CHIP_ERROR BLConfig::ReadConfigValueBin(const char * key, uint8_t * buf, size_t CHIP_ERROR BLConfig::WriteConfigValue(const char * key, uint8_t * val, size_t size) { - int ret = LFS_ERR_OK; - CHIP_ERROR err = CHIP_NO_ERROR; - lfs_file_t file; - std::string write_key = blcfg_convert_key(std::string(key), std::string(BLCONFIG_LFS_NAMESPACE)); + int ret = LFS_ERR_OK; + CHIP_ERROR err = CHIP_NO_ERROR; + lfs_file_t file; + std::string write_key = blcfg_convert_key(std::string(key), std::string(BLCONFIG_LFS_NAMESPACE)); blconfig_lfs->cfg->lock(blconfig_lfs->cfg); - ret = lfs_file_open(blconfig_lfs, &file, const_cast(write_key.c_str()), LFS_O_CREAT | LFS_O_RDWR); + ret = lfs_file_open(blconfig_lfs, &file, const_cast(write_key.c_str()), LFS_O_CREAT | LFS_O_RDWR); VerifyOrExit(ret == LFS_ERR_OK, err = CHIP_ERROR_PERSISTED_STORAGE_FAILED); lfs_file_write(blconfig_lfs, &file, val, size); lfs_file_close(blconfig_lfs, &file); -exit: +exit: blconfig_lfs->cfg->unlock(blconfig_lfs->cfg); return err; @@ -254,24 +263,25 @@ CHIP_ERROR BLConfig::WriteConfigValueBin(const char * key, const uint8_t * data, CHIP_ERROR BLConfig::ClearConfigValue(const char * key) { - std::string delete_key = blcfg_convert_key(std::string(key), std::string(BLCONFIG_LFS_NAMESPACE)); + std::string delete_key = blcfg_convert_key(std::string(key), std::string(BLCONFIG_LFS_NAMESPACE)); - int ret = lfs_remove(blconfig_lfs, const_cast(delete_key.c_str())); + int ret = lfs_remove(blconfig_lfs, const_cast(delete_key.c_str())); - return (ret >= LFS_ERR_OK || ret == LFS_ERR_NOENT) ? CHIP_NO_ERROR: CHIP_ERROR_PERSISTED_STORAGE_FAILED; + return (ret >= LFS_ERR_OK || ret == LFS_ERR_NOENT) ? CHIP_NO_ERROR : CHIP_ERROR_PERSISTED_STORAGE_FAILED; } CHIP_ERROR BLConfig::FactoryResetConfig(void) { - int ret = LFS_ERR_OK; - lfs_file_t file; - std::string reset_key = blcfg_convert_key(std::string(kBLKey_factoryResetFlag)); - const char reset_key_value[] = "pending"; + int ret = LFS_ERR_OK; + lfs_file_t file; + std::string reset_key = blcfg_convert_key(std::string(kBLKey_factoryResetFlag)); + const char reset_key_value[] = "pending"; blconfig_lfs->cfg->lock(blconfig_lfs->cfg); - ret = lfs_file_open(blconfig_lfs, &file, const_cast(reset_key.c_str()), LFS_O_CREAT | LFS_O_RDWR); - if (ret != LFS_ERR_OK) { + ret = lfs_file_open(blconfig_lfs, &file, const_cast(reset_key.c_str()), LFS_O_CREAT | LFS_O_RDWR); + if (ret != LFS_ERR_OK) + { blconfig_lfs->cfg->unlock(blconfig_lfs->cfg); return CHIP_ERROR_PERSISTED_STORAGE_FAILED; } @@ -288,38 +298,43 @@ void BLConfig::RunConfigUnitTest() {} bool BLConfig::ConfigValueExists(const char * key) { - std::string exist_key = blcfg_convert_key(std::string(key), std::string(BLCONFIG_LFS_NAMESPACE)); + std::string exist_key = blcfg_convert_key(std::string(key), std::string(BLCONFIG_LFS_NAMESPACE)); struct lfs_info stat; - return lfs_stat(blconfig_lfs, const_cast(exist_key.c_str()), &stat) == LFS_ERR_OK; + return lfs_stat(blconfig_lfs, const_cast(exist_key.c_str()), &stat) == LFS_ERR_OK; } CHIP_ERROR BLConfig::ReadKVS(const char * key, void * value, size_t value_size, size_t * read_bytes_size, size_t offset_bytes) { - CHIP_ERROR err = CHIP_NO_ERROR; - int ret = LFS_ERR_OK; - lfs_file_t file; - std::string read_key = blcfg_convert_key(std::string(key), std::string(BLCONFIG_LFS_NAMESPACE)); + CHIP_ERROR err = CHIP_NO_ERROR; + int ret = LFS_ERR_OK; + lfs_file_t file; + std::string read_key = blcfg_convert_key(std::string(key), std::string(BLCONFIG_LFS_NAMESPACE)); - if (read_bytes_size) { - * read_bytes_size = 0; + if (read_bytes_size) + { + *read_bytes_size = 0; } blconfig_lfs->cfg->lock(blconfig_lfs->cfg); - ret = lfs_file_open(blconfig_lfs, &file, const_cast(read_key.c_str()), LFS_O_RDONLY); + ret = lfs_file_open(blconfig_lfs, &file, const_cast(read_key.c_str()), LFS_O_RDONLY); VerifyOrExit(ret == LFS_ERR_OK, err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND); - if (value && value_size) { - do { + if (value && value_size) + { + do + { ret = 0; - if (offset_bytes > 0 && lfs_file_seek(blconfig_lfs, &file, offset_bytes, 0) < 0) { + if (offset_bytes > 0 && lfs_file_seek(blconfig_lfs, &file, offset_bytes, 0) < 0) + { err = CHIP_ERROR_PERSISTED_STORAGE_FAILED; break; } ret = lfs_file_read(blconfig_lfs, &file, value, value_size); - if (ret > 0) { - * read_bytes_size = ret; + if (ret > 0) + { + *read_bytes_size = ret; } } while (0); } @@ -333,7 +348,7 @@ CHIP_ERROR BLConfig::ReadKVS(const char * key, void * value, size_t value_size, CHIP_ERROR BLConfig::WriteKVS(const char * key, const void * value, size_t value_size) { - return WriteConfigValueBin(key, (const uint8_t *)value, value_size); + return WriteConfigValueBin(key, (const uint8_t *) value, value_size); } CHIP_ERROR BLConfig::ClearKVS(const char * key) diff --git a/third_party/bouffalolab/bl602/bl_iot_sdk.gni b/third_party/bouffalolab/bl602/bl_iot_sdk.gni index 65e762596d88a8..77b40cf306aa08 100644 --- a/third_party/bouffalolab/bl602/bl_iot_sdk.gni +++ b/third_party/bouffalolab/bl602/bl_iot_sdk.gni @@ -361,11 +361,11 @@ template("bl_iot_sdk") { if (defined(invoker.bouffalo_sdk_component_easyflash_enabled) && invoker.bouffalo_sdk_component_easyflash_enabled) { include_dirs += [ "${bl_iot_sdk_root}/components/stage/easyflash4/inc" ] - } - else { + } else { defines = [ "LFS_THREADSAFE" ] - include_dirs += [ "${bl_iot_sdk_root}/components/stage/littlefs/littlefs" ] + include_dirs += + [ "${bl_iot_sdk_root}/components/stage/littlefs/littlefs" ] include_dirs += [ "${bl_iot_sdk_root}/components/stage/littlefs/port" ] } } @@ -412,16 +412,15 @@ template("bl_iot_sdk") { "${bl_iot_sdk_root}/components/stage/easyflash4/src/ef_port.c", "${bl_iot_sdk_root}/components/stage/easyflash4/src/ef_utils.c", ] - } - else { + } else { defines += [ "CONFIG_LITTLEFS", "CONFIG_FREERTOS", ] sources += [ - "${bl_iot_sdk_root}/components/stage/littlefs/littlefs/lfs_util.c", "${bl_iot_sdk_root}/components/stage/littlefs/littlefs/lfs.c", + "${bl_iot_sdk_root}/components/stage/littlefs/littlefs/lfs_util.c", "${bl_iot_sdk_root}/components/stage/littlefs/port/lfs_xip_flash.c", ] configs += [ ":${sdk_target_name}_config_hosal" ] diff --git a/third_party/bouffalolab/bl702/bl_iot_sdk.gni b/third_party/bouffalolab/bl702/bl_iot_sdk.gni index ad169396df8e98..d90262483f24a2 100644 --- a/third_party/bouffalolab/bl702/bl_iot_sdk.gni +++ b/third_party/bouffalolab/bl702/bl_iot_sdk.gni @@ -405,11 +405,11 @@ template("bl_iot_sdk") { if (defined(invoker.bouffalo_sdk_component_easyflash_enabled) && invoker.bouffalo_sdk_component_easyflash_enabled) { include_dirs += [ "${bl_iot_sdk_root}/components/stage/easyflash4/inc" ] - } - else { + } else { defines = [ "LFS_THREADSAFE" ] - include_dirs += [ "${bl_iot_sdk_root}/components/stage/littlefs/littlefs" ] + include_dirs += + [ "${bl_iot_sdk_root}/components/stage/littlefs/littlefs" ] include_dirs += [ "${bl_iot_sdk_root}/components/stage/littlefs/port" ] } @@ -459,16 +459,15 @@ template("bl_iot_sdk") { "${bl_iot_sdk_root}/components/stage/easyflash4/src/ef_port.c", "${bl_iot_sdk_root}/components/stage/easyflash4/src/ef_utils.c", ] - } - else { + } else { defines += [ "CONFIG_LITTLEFS", "CONFIG_FREERTOS", ] sources += [ - "${bl_iot_sdk_root}/components/stage/littlefs/littlefs/lfs_util.c", "${bl_iot_sdk_root}/components/stage/littlefs/littlefs/lfs.c", + "${bl_iot_sdk_root}/components/stage/littlefs/littlefs/lfs_util.c", "${bl_iot_sdk_root}/components/stage/littlefs/port/lfs_xip_flash.c", ] configs += [ ":${sdk_target_name}_config_hosal" ] @@ -779,8 +778,7 @@ template("bl_iot_sdk") { if (defined(invoker.bouffalo_sdk_component_easyflash_enabled) && invoker.bouffalo_sdk_component_easyflash_enabled) { sources += [ "${bl_iot_sdk_root}/components/network/thread/openthread_port/ot_settings_easyflash.c" ] - } - else { + } else { sources += [ "${bl_iot_sdk_root}/components/network/thread/openthread_port/ot_settings_littlefs.c" ] } diff --git a/third_party/bouffalolab/bl702l/bl_iot_sdk.gni b/third_party/bouffalolab/bl702l/bl_iot_sdk.gni index 961076e3098f0a..cdbf9228ca0e58 100644 --- a/third_party/bouffalolab/bl702l/bl_iot_sdk.gni +++ b/third_party/bouffalolab/bl702l/bl_iot_sdk.gni @@ -368,11 +368,11 @@ template("bl_iot_sdk") { if (defined(invoker.bouffalo_sdk_component_easyflash_enabled) && invoker.bouffalo_sdk_component_easyflash_enabled) { include_dirs += [ "${bl_iot_sdk_root}/components/stage/easyflash4/inc" ] - } - else { + } else { defines = [ "LFS_THREADSAFE" ] - include_dirs += [ "${bl_iot_sdk_root}/components/stage/littlefs/littlefs" ] + include_dirs += + [ "${bl_iot_sdk_root}/components/stage/littlefs/littlefs" ] include_dirs += [ "${bl_iot_sdk_root}/components/stage/littlefs/port" ] } } @@ -418,16 +418,15 @@ template("bl_iot_sdk") { "${bl_iot_sdk_root}/components/stage/easyflash4/src/ef_port.c", "${bl_iot_sdk_root}/components/stage/easyflash4/src/ef_utils.c", ] - } - else { + } else { defines += [ "CONFIG_LITTLEFS", "CONFIG_FREERTOS", ] sources += [ - "${bl_iot_sdk_root}/components/stage/littlefs/littlefs/lfs_util.c", "${bl_iot_sdk_root}/components/stage/littlefs/littlefs/lfs.c", + "${bl_iot_sdk_root}/components/stage/littlefs/littlefs/lfs_util.c", "${bl_iot_sdk_root}/components/stage/littlefs/port/lfs_xip_flash.c", ] configs += [ ":${sdk_target_name}_config_hosal" ] @@ -725,8 +724,7 @@ template("bl_iot_sdk") { if (defined(invoker.bouffalo_sdk_component_easyflash_enabled) && invoker.bouffalo_sdk_component_easyflash_enabled) { sources += [ "${bl_iot_sdk_root}/components/network/thread/openthread_port/ot_settings_easyflash.c" ] - } - else { + } else { sources += [ "${bl_iot_sdk_root}/components/network/thread/openthread_port/ot_settings_littlefs.c" ] }