From 20200ee1df4d2be2647d4894411531a5aaa62250 Mon Sep 17 00:00:00 2001 From: wyhong <30567533+wy-hh@users.noreply.github.com> Date: Wed, 2 Oct 2024 21:41:59 +0800 Subject: [PATCH] [Bouffalo Lab] Add littlefs to access flash storage (#35169) * [feat] Add littlefs to access flash storage * fix restyle * update ci build job and remove regex in BLConfig_littlefs.cpp * fix restyle * fix compile error * fix restyle * fix compile error after restyle * fix restyle with include order --- .github/workflows/examples-bouffalolab.yaml | 40 +- .../bouffalolab/common/AppTask.cpp | 2 - scripts/build/build/targets.py | 1 + scripts/build/builders/bouffalolab.py | 12 +- .../build/testdata/all_targets_linux_x64.txt | 2 +- src/platform/bouffalolab/BL602/BUILD.gn | 2 + src/platform/bouffalolab/BL702/BUILD.gn | 2 + src/platform/bouffalolab/BL702L/BUILD.gn | 2 + .../bouffalolab/common/BLConfig_littlefs.cpp | 429 ++++++++++++++++++ third_party/bouffalolab/bl602/bl_iot_sdk.gni | 18 + third_party/bouffalolab/bl702/bl_iot_sdk.gni | 20 + third_party/bouffalolab/bl702l/bl_iot_sdk.gni | 20 + 12 files changed, 526 insertions(+), 24 deletions(-) create mode 100644 src/platform/bouffalolab/common/BLConfig_littlefs.cpp diff --git a/.github/workflows/examples-bouffalolab.yaml b/.github/workflows/examples-bouffalolab.yaml index e49450f965a4e8..3ff00c6c3eff1b 100644 --- a/.github/workflows/examples-bouffalolab.yaml +++ b/.github/workflows/examples-bouffalolab.yaml @@ -57,9 +57,9 @@ jobs: run: | ./scripts/run_in_build_env.sh \ "./scripts/build/build_examples.py \ - --target bouffalolab-bl602-night-light-light \ - --target bouffalolab-bl602-night-light-light-mfd \ - --target bouffalolab-bl602-iot-matter-v1-light-115200-rpc \ + --target bouffalolab-bl602dk-light-easyflash \ + --target bouffalolab-bl602dk-light-mfd-littlefs \ + --target bouffalolab-bl602dk-light-rpc-115200-littlefs \ build \ --copy-artifacts-to out/artifacts \ " @@ -67,15 +67,15 @@ jobs: run: | .environment/pigweed-venv/bin/python3 scripts/tools/memory/gh_sizes.py \ bl602 bl602 lighting-app \ - out/artifacts/bouffalolab-bl602-night-light-light/chip-bl602-lighting-example.out \ + out/artifacts/bouffalolab-bl602dk-light-easyflash/chip-bl602-lighting-example.out \ /tmp/bloat_reports/ .environment/pigweed-venv/bin/python3 scripts/tools/memory/gh_sizes.py \ bl602 bl602+mfd lighting-app \ - out/artifacts/bouffalolab-bl602-night-light-light-mfd/chip-bl602-lighting-example.out \ + out/artifacts/bouffalolab-bl602dk-light-mfd-littlefs/chip-bl602-lighting-example.out \ /tmp/bloat_reports/ .environment/pigweed-venv/bin/python3 scripts/tools/memory/gh_sizes.py \ bl602 bl602+rpc lighting-app \ - out/artifacts/bouffalolab-bl602-iot-matter-v1-light-115200-rpc/chip-bl602-lighting-example.out \ + out/artifacts/bouffalolab-bl602dk-light-rpc-115200-littlefs/chip-bl602-lighting-example.out \ /tmp/bloat_reports/ - name: Clean out build output run: rm -rf ./out @@ -84,11 +84,11 @@ jobs: run: | ./scripts/run_in_build_env.sh \ "./scripts/build/build_examples.py \ - --target bouffalolab-bl706-night-light-light \ - --target bouffalolab-bl706-night-light-light-mfd \ - --target bouffalolab-bl706dk-light-ethernet \ - --target bouffalolab-bl706dk-light-wifi-fp \ - --target bouffalolab-xt-zb6-devkit-light-115200-rpc \ + --target bouffalolab-bl706dk-light-easyflash \ + --target bouffalolab-bl706dk-light-mfd-littlefs \ + --target bouffalolab-bl706dk-light-ethernet-littlefs \ + --target bouffalolab-bl706dk-light-wifi-littlefs \ + --target bouffalolab-bl706dk-light-rpc-115200-littlefs \ build \ --copy-artifacts-to out/artifacts \ " @@ -96,23 +96,23 @@ jobs: run: | .environment/pigweed-venv/bin/python3 scripts/tools/memory/gh_sizes.py \ bl702 bl702 lighting-app \ - out/artifacts/bouffalolab-bl706-night-light-light/chip-bl702-lighting-example.out \ + out/artifacts/bouffalolab-bl706dk-light-easyflash/chip-bl702-lighting-example.out \ /tmp/bloat_reports/ .environment/pigweed-venv/bin/python3 scripts/tools/memory/gh_sizes.py \ bl702 bl702+mfd lighting-app \ - out/artifacts/bouffalolab-bl706-night-light-light-mfd/chip-bl702-lighting-example.out \ + out/artifacts/bouffalolab-bl706dk-light-mfd-littlefs/chip-bl702-lighting-example.out \ /tmp/bloat_reports/ .environment/pigweed-venv/bin/python3 scripts/tools/memory/gh_sizes.py \ bl702 bl706-eth lighting-app \ - out/artifacts/bouffalolab-bl706dk-light-ethernet/chip-bl702-lighting-example.out \ + out/artifacts/bouffalolab-bl706dk-light-ethernet-littlefs/chip-bl702-lighting-example.out \ /tmp/bloat_reports/ .environment/pigweed-venv/bin/python3 scripts/tools/memory/gh_sizes.py \ bl702 bl706-wifi lighting-app \ - out/artifacts/bouffalolab-bl706dk-light-wifi-fp/chip-bl702-lighting-example.out \ + out/artifacts/bouffalolab-bl706dk-light-wifi-littlefs/chip-bl702-lighting-example.out \ /tmp/bloat_reports/ .environment/pigweed-venv/bin/python3 scripts/tools/memory/gh_sizes.py \ bl702 bl702+rpc lighting-app \ - out/artifacts/bouffalolab-xt-zb6-devkit-light-115200-rpc/chip-bl702-lighting-example.out \ + out/artifacts/bouffalolab-bl706dk-light-rpc-115200-littlefs/chip-bl702-lighting-example.out \ /tmp/bloat_reports/ - name: Clean out build output run: rm -rf ./out @@ -122,8 +122,8 @@ jobs: run: | ./scripts/run_in_build_env.sh \ "./scripts/build/build_examples.py \ - --target bouffalolab-bl704ldk-light \ - --target bouffalolab-bl704ldk-light-mfd \ + --target bouffalolab-bl704ldk-light-easyflash \ + --target bouffalolab-bl704ldk-light-mfd-littlefs \ build \ --copy-artifacts-to out/artifacts \ " @@ -131,11 +131,11 @@ jobs: run: | .environment/pigweed-venv/bin/python3 scripts/tools/memory/gh_sizes.py \ bl702l bl702l lighting-app \ - out/artifacts/bouffalolab-bl704ldk-light/chip-bl702l-lighting-example.out \ + out/artifacts/bouffalolab-bl704ldk-light-easyflash/chip-bl702l-lighting-example.out \ /tmp/bloat_reports/ .environment/pigweed-venv/bin/python3 scripts/tools/memory/gh_sizes.py \ bl702l bl702l+mfd lighting-app \ - out/artifacts/bouffalolab-bl704ldk-light-mfd/chip-bl702l-lighting-example.out \ + out/artifacts/bouffalolab-bl704ldk-light-mfd-littlefs/chip-bl702l-lighting-example.out \ /tmp/bloat_reports/ - name: Clean out build output run: rm -rf ./out diff --git a/examples/lighting-app/bouffalolab/common/AppTask.cpp b/examples/lighting-app/bouffalolab/common/AppTask.cpp index 7e9b4920d03783..5fc74391f05af0 100644 --- a/examples/lighting-app/bouffalolab/common/AppTask.cpp +++ b/examples/lighting-app/bouffalolab/common/AppTask.cpp @@ -46,8 +46,6 @@ #include #include -#include - extern "C" { #include #include diff --git a/scripts/build/build/targets.py b/scripts/build/build/targets.py index 33776bb84295a7..5f230e772c8fdf 100755 --- a/scripts/build/build/targets.py +++ b/scripts/build/build/targets.py @@ -712,6 +712,7 @@ def BuildBouffalolabTarget(): target.AppendModifier('wifi', enable_wifi=True) target.AppendModifier('thread', enable_thread=True) target.AppendModifier('easyflash', enable_easyflash=True) + target.AppendModifier('littlefs', enable_littlefs=True) target.AppendModifier('shell', enable_shell=True) target.AppendModifier('mfd', enable_mfd=True) target.AppendModifier('rotating_device_id', enable_rotating_device_id=True) diff --git a/scripts/build/builders/bouffalolab.py b/scripts/build/builders/bouffalolab.py index af0002457b3270..09d5f250f5b221 100644 --- a/scripts/build/builders/bouffalolab.py +++ b/scripts/build/builders/bouffalolab.py @@ -89,6 +89,7 @@ def __init__(self, enable_heap_monitoring: bool = False, use_matter_openthread: bool = False, enable_easyflash: bool = False, + enable_littlefs: bool = False ): if 'BL602' == module_type: @@ -162,7 +163,16 @@ 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()}') - self.argsOpt.append(f'bouffalo_sdk_component_easyflash_enabled=true') + if enable_easyflash and enable_littlefs: + raise Exception("Only one of easyflash and littlefs can be enabled.") + + if not enable_easyflash and not enable_littlefs: + logging.fatal('*' * 80) + logging.fatal('littlefs is added to support for flash storage access.') + logging.fatal('Please consider and select one of easyflash and littlefs to use.') + logging.fatal('*' * 80) + raise Exception("None of easyflash and littlefs select to build.") + self.argsOpt.append(f'bouffalo_sdk_component_easyflash_enabled={"false" if enable_littlefs else "true"}') if enable_thread: self.argsOpt.append('chip_system_config_use_open_thread_inet_endpoints=true') diff --git a/scripts/build/testdata/all_targets_linux_x64.txt b/scripts/build/testdata/all_targets_linux_x64.txt index 27e9a633785ccd..4ace53ba07e5de 100644 --- a/scripts/build/testdata/all_targets_linux_x64.txt +++ b/scripts/build/testdata/all_targets_linux_x64.txt @@ -1,7 +1,7 @@ ameba-amebad-{all-clusters,all-clusters-minimal,light,light-switch,pigweed} asr-{asr582x,asr595x,asr550x}-{all-clusters,all-clusters-minimal,lighting,light-switch,lock,bridge,temperature-measurement,thermostat,ota-requestor,dishwasher,refrigerator}[-ota][-shell][-no_logging][-factory][-rotating_id][-rio] android-{arm,arm64,x86,x64,androidstudio-arm,androidstudio-arm64,androidstudio-x86,androidstudio-x64}-{chip-tool,chip-test,tv-server,tv-casting-app,java-matter-controller,kotlin-matter-controller,virtual-device-app}[-no-debug] -bouffalolab-{bl602dk,bl704ldk,bl706dk,bl602-night-light,bl706-night-light,bl602-iot-matter-v1,xt-zb6-devkit}-light[-ethernet][-wifi][-thread][-easyflash][-shell][-mfd][-rotating_device_id][-rpc][-cdc][-mot][-resetcnt][-memmonitor][-115200][-fp] +bouffalolab-{bl602dk,bl704ldk,bl706dk,bl602-night-light,bl706-night-light,bl602-iot-matter-v1,xt-zb6-devkit}-light[-ethernet][-wifi][-thread][-easyflash][-littlefs][-shell][-mfd][-rotating_device_id][-rpc][-cdc][-mot][-resetcnt][-memmonitor][-115200][-fp] cc32xx-{lock,air-purifier} ti-cc13x4_26x4-{lighting,lock,pump,pump-controller}[-mtd][-ftd] cyw30739-{cyw30739b2_p5_evk_01,cyw30739b2_p5_evk_02,cyw30739b2_p5_evk_03,cyw930739m2evb_01,cyw930739m2evb_02}-{light,light-switch,lock,thermostat} diff --git a/src/platform/bouffalolab/BL602/BUILD.gn b/src/platform/bouffalolab/BL602/BUILD.gn index a85ab72011242a..d4cd04c739c6cd 100644 --- a/src/platform/bouffalolab/BL602/BUILD.gn +++ b/src/platform/bouffalolab/BL602/BUILD.gn @@ -64,6 +64,8 @@ static_library("BL602") { if (bouffalo_sdk_component_easyflash_enabled) { sources += [ "../common/BLConfig.cpp" ] + } else { + sources += [ "../common/BLConfig_littlefs.cpp" ] } deps = [ diff --git a/src/platform/bouffalolab/BL702/BUILD.gn b/src/platform/bouffalolab/BL702/BUILD.gn index a305b0d9db00ad..e0daee04056a51 100644 --- a/src/platform/bouffalolab/BL702/BUILD.gn +++ b/src/platform/bouffalolab/BL702/BUILD.gn @@ -68,6 +68,8 @@ static_library("BL702") { if (bouffalo_sdk_component_easyflash_enabled) { sources += [ "../common/BLConfig.cpp" ] + } else { + sources += [ "../common/BLConfig_littlefs.cpp" ] } if (chip_enable_wifi) { diff --git a/src/platform/bouffalolab/BL702L/BUILD.gn b/src/platform/bouffalolab/BL702L/BUILD.gn index d85f727fed4ea3..2529c8d3a7e736 100644 --- a/src/platform/bouffalolab/BL702L/BUILD.gn +++ b/src/platform/bouffalolab/BL702L/BUILD.gn @@ -66,6 +66,8 @@ static_library("BL702L") { if (bouffalo_sdk_component_easyflash_enabled) { sources += [ "../common/BLConfig.cpp" ] + } else { + sources += [ "../common/BLConfig_littlefs.cpp" ] } if (chip_enable_openthread) { diff --git a/src/platform/bouffalolab/common/BLConfig_littlefs.cpp b/src/platform/bouffalolab/common/BLConfig_littlefs.cpp new file mode 100644 index 00000000000000..ae0788ab1c8f62 --- /dev/null +++ b/src/platform/bouffalolab/common/BLConfig_littlefs.cpp @@ -0,0 +1,429 @@ +/* + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +extern "C" { +#include +} + +#include + +#include + +#ifndef BLCONFIG_LFS_NAMESPACE +#define BLCONFIG_LFS_NAMESPACE "/_blcfg_" +#endif + +#ifndef BLCONFIG_SLASH +#define BLCONFIG_SLASH '-' +#endif + +namespace chip { +namespace DeviceLayer { +namespace Internal { + +static lfs_t * blconfig_lfs = nullptr; + +static inline char * blcfg_convert_key(const char * pKey, const char * pNameSpace = NULL) +{ + int len_key = 0, len_namespace = 0; + + if (pNameSpace) + { + len_namespace = strlen(pNameSpace); + } + len_key = strlen(pKey); + + char * pName = (char *) malloc(len_namespace + 1 + len_key + 1); + if (nullptr == pName) + { + return nullptr; + } + + if (pNameSpace) + { + memcpy(pName, pNameSpace, len_namespace); + } + pName[len_namespace] = '/'; + memcpy(pName + len_namespace + 1, pKey, len_key); + pName[len_namespace + 1 + len_key] = '\0'; + + for (int i = len_namespace + 1; i < len_namespace + 1 + len_key; i++) + { + if (pName[i] == '/') + { + pName[i] = '_'; + } + } + + return pName; +} + +static CHIP_ERROR blcfg_do_factory_reset(void) +{ + CHIP_ERROR err = CHIP_NO_ERROR; + int ret; + struct lfs_info stat; + lfs_file_t file; + lfs_dir_t dir = {}; + char * factory_reset_key = blcfg_convert_key(BLConfig::kBLKey_factoryResetFlag); + + VerifyOrExit(factory_reset_key != nullptr, err = CHIP_ERROR_NO_MEMORY); + + blconfig_lfs->cfg->lock(blconfig_lfs->cfg); + + ret = lfs_stat(blconfig_lfs, factory_reset_key, &stat); + + if (LFS_ERR_OK == ret) + { + err = CHIP_ERROR_PERSISTED_STORAGE_FAILED; + + do + { + ret = lfs_file_open(blconfig_lfs, &file, factory_reset_key, 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) + { + ret = lfs_dir_read(blconfig_lfs, &dir, &stat); + if (ret <= 0) + { + break; + } + + if (stat.type != LFS_TYPE_REG) + { + continue; + } + char * delete_key = blcfg_convert_key(stat.name, BLCONFIG_LFS_NAMESPACE); + VerifyOrExit(delete_key != nullptr, err = CHIP_ERROR_NO_MEMORY); + + ret = lfs_remove(blconfig_lfs, delete_key); + free(delete_key); + + if (ret != LFS_ERR_OK) + { + break; + } + } + + lfs_dir_close(blconfig_lfs, &dir); + + ret = lfs_remove(blconfig_lfs, factory_reset_key); + if (ret != LFS_ERR_OK) + { + break; + } + + err = CHIP_NO_ERROR; + } while (0); + } + +exit: + blconfig_lfs->cfg->unlock(blconfig_lfs->cfg); + if (factory_reset_key) + { + free(factory_reset_key); + } + + return err; +} + +void BLConfig::Init(void) +{ + 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) + { + ret = lfs_mkdir(blconfig_lfs, BLCONFIG_LFS_NAMESPACE); + VerifyOrExit(ret == LFS_ERR_OK, err = CHIP_ERROR_PERSISTED_STORAGE_FAILED); + + ret = lfs_stat(blconfig_lfs, BLCONFIG_LFS_NAMESPACE, &stat); + } + VerifyOrExit(ret == LFS_ERR_OK && stat.type == LFS_TYPE_DIR, err = CHIP_ERROR_PERSISTED_STORAGE_FAILED); + + err = blcfg_do_factory_reset(); +exit: + configASSERT(err == CHIP_NO_ERROR); +} + +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; + char * read_key = blcfg_convert_key(key, BLCONFIG_LFS_NAMESPACE); + + VerifyOrExit(read_key != nullptr, err = CHIP_ERROR_NO_MEMORY); + + blconfig_lfs->cfg->lock(blconfig_lfs->cfg); + + ret = lfs_file_open(blconfig_lfs, &file, read_key, 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); + readsize = ret; + } + lfs_file_close(blconfig_lfs, &file); + +exit: + blconfig_lfs->cfg->unlock(blconfig_lfs->cfg); + if (read_key) + { + free(read_key); + } + + return err; +} + +CHIP_ERROR BLConfig::ReadConfigValue(const char * key, bool & val) +{ + size_t readlen = 0; + return ReadConfigValue(key, (uint8_t *) &val, 1, readlen); +} + +CHIP_ERROR BLConfig::ReadConfigValue(const char * key, uint32_t & val) +{ + size_t readlen = 0; + return ReadConfigValue(key, (uint8_t *) &val, sizeof(val), readlen); +} + +CHIP_ERROR BLConfig::ReadConfigValue(const char * key, uint64_t & val) +{ + size_t readlen = 0; + return ReadConfigValue(key, (uint8_t *) &val, sizeof(val), readlen); +} + +CHIP_ERROR BLConfig::ReadConfigValueStr(const char * key, char * buf, size_t bufSize, size_t & outLen) +{ + size_t readlen = 0; + if (CHIP_NO_ERROR == ReadConfigValue(key, (uint8_t *) buf, bufSize, readlen)) + { + outLen = readlen; + if (readlen && readlen < bufSize) + { + buf[readlen] = '\0'; + } + + return CHIP_NO_ERROR; + } + + return CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND; +} + +CHIP_ERROR BLConfig::ReadConfigValueBin(const char * key, uint8_t * buf, size_t bufSize, size_t & outLen) +{ + size_t readlen = 0; + if (CHIP_NO_ERROR == ReadConfigValue(key, (uint8_t *) buf, bufSize, readlen)) + { + outLen = readlen; + return CHIP_NO_ERROR; + } + + return CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND; +} + +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; + char * write_key = blcfg_convert_key(key, BLCONFIG_LFS_NAMESPACE); + + VerifyOrExit(write_key != nullptr, err = CHIP_ERROR_NO_MEMORY); + + blconfig_lfs->cfg->lock(blconfig_lfs->cfg); + + ret = lfs_file_open(blconfig_lfs, &file, write_key, 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: + blconfig_lfs->cfg->unlock(blconfig_lfs->cfg); + if (write_key) + { + free(write_key); + } + + return err; +} + +CHIP_ERROR BLConfig::WriteConfigValue(const char * key, bool val) +{ + return WriteConfigValue(key, (uint8_t *) &val, sizeof(val)); +} + +CHIP_ERROR BLConfig::WriteConfigValue(const char * key, uint32_t val) +{ + return WriteConfigValue(key, (uint8_t *) &val, sizeof(val)); +} + +CHIP_ERROR BLConfig::WriteConfigValue(const char * key, uint64_t val) +{ + return WriteConfigValue(key, (uint8_t *) &val, sizeof(val)); +} + +CHIP_ERROR BLConfig::WriteConfigValueStr(const char * key, const char * str) +{ + return WriteConfigValue(key, (uint8_t *) str, strlen(str) + 1); +} + +CHIP_ERROR BLConfig::WriteConfigValueStr(const char * key, const char * str, size_t strLen) +{ + return WriteConfigValue(key, (uint8_t *) str, strLen); +} + +CHIP_ERROR BLConfig::WriteConfigValueBin(const char * key, const uint8_t * data, size_t dataLen) +{ + return WriteConfigValue(key, (uint8_t *) data, dataLen); +} + +CHIP_ERROR BLConfig::ClearConfigValue(const char * key) +{ + char * delete_key = blcfg_convert_key(key, BLCONFIG_LFS_NAMESPACE); + + if (delete_key == nullptr) + { + return CHIP_ERROR_NO_MEMORY; + } + + int ret = lfs_remove(blconfig_lfs, delete_key); + free(delete_key); + + 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; + char * reset_key = blcfg_convert_key(kBLKey_factoryResetFlag); + const char reset_key_value[] = "pending"; + + if (nullptr == reset_key) + { + return CHIP_ERROR_NO_MEMORY; + } + + blconfig_lfs->cfg->lock(blconfig_lfs->cfg); + + ret = lfs_file_open(blconfig_lfs, &file, reset_key, LFS_O_CREAT | LFS_O_RDWR); + if (ret != LFS_ERR_OK) + { + blconfig_lfs->cfg->unlock(blconfig_lfs->cfg); + return CHIP_ERROR_PERSISTED_STORAGE_FAILED; + } + + lfs_file_write(blconfig_lfs, &file, reset_key_value, sizeof(reset_key_value)); + lfs_file_close(blconfig_lfs, &file); + + blconfig_lfs->cfg->unlock(blconfig_lfs->cfg); + free(reset_key); + + return blcfg_do_factory_reset(); +} + +void BLConfig::RunConfigUnitTest() {} + +bool BLConfig::ConfigValueExists(const char * key) +{ + char * exist_key = blcfg_convert_key(key, BLCONFIG_LFS_NAMESPACE); + struct lfs_info stat; + bool bret; + + bret = (lfs_stat(blconfig_lfs, exist_key, &stat) == LFS_ERR_OK); + free(exist_key); + + return bret; +} + +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; + char * read_key = blcfg_convert_key(key, BLCONFIG_LFS_NAMESPACE); + + VerifyOrExit(read_key != nullptr, err = CHIP_ERROR_NO_MEMORY); + + if (read_bytes_size) + { + *read_bytes_size = 0; + } + + blconfig_lfs->cfg->lock(blconfig_lfs->cfg); + + ret = lfs_file_open(blconfig_lfs, &file, read_key, LFS_O_RDONLY); + VerifyOrExit(ret == LFS_ERR_OK, err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND); + + if (value && value_size) + { + do + { + ret = 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; + } + } while (0); + } + + lfs_file_close(blconfig_lfs, &file); +exit: + blconfig_lfs->cfg->unlock(blconfig_lfs->cfg); + if (read_key) + { + free(read_key); + } + + return err; +} + +CHIP_ERROR BLConfig::WriteKVS(const char * key, const void * value, size_t value_size) +{ + return WriteConfigValueBin(key, (const uint8_t *) value, value_size); +} + +CHIP_ERROR BLConfig::ClearKVS(const char * key) +{ + return ClearConfigValue(key); +} + +} // namespace Internal +} // namespace DeviceLayer +} // namespace chip diff --git a/third_party/bouffalolab/bl602/bl_iot_sdk.gni b/third_party/bouffalolab/bl602/bl_iot_sdk.gni index d6896957e17204..77b40cf306aa08 100644 --- a/third_party/bouffalolab/bl602/bl_iot_sdk.gni +++ b/third_party/bouffalolab/bl602/bl_iot_sdk.gni @@ -361,6 +361,12 @@ 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 { + defines = [ "LFS_THREADSAFE" ] + + include_dirs += + [ "${bl_iot_sdk_root}/components/stage/littlefs/littlefs" ] + include_dirs += [ "${bl_iot_sdk_root}/components/stage/littlefs/port" ] } } @@ -406,6 +412,18 @@ 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 { + defines += [ + "CONFIG_LITTLEFS", + "CONFIG_FREERTOS", + ] + + sources += [ + "${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" ] } public_configs = [ ":${sdk_target_name}_config_stage" ] diff --git a/third_party/bouffalolab/bl702/bl_iot_sdk.gni b/third_party/bouffalolab/bl702/bl_iot_sdk.gni index cb59d002986180..d90262483f24a2 100644 --- a/third_party/bouffalolab/bl702/bl_iot_sdk.gni +++ b/third_party/bouffalolab/bl702/bl_iot_sdk.gni @@ -405,6 +405,12 @@ 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 { + defines = [ "LFS_THREADSAFE" ] + + include_dirs += + [ "${bl_iot_sdk_root}/components/stage/littlefs/littlefs" ] + include_dirs += [ "${bl_iot_sdk_root}/components/stage/littlefs/port" ] } if (defined(invoker.enable_cdc_module) && invoker.enable_cdc_module) { @@ -453,6 +459,18 @@ 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 { + defines += [ + "CONFIG_LITTLEFS", + "CONFIG_FREERTOS", + ] + + sources += [ + "${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" ] } public_configs = [ ":${sdk_target_name}_config_stage" ] @@ -760,6 +778,8 @@ 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 { + sources += [ "${bl_iot_sdk_root}/components/network/thread/openthread_port/ot_settings_littlefs.c" ] } libs = [ diff --git a/third_party/bouffalolab/bl702l/bl_iot_sdk.gni b/third_party/bouffalolab/bl702l/bl_iot_sdk.gni index 0e4ad70410467e..cdbf9228ca0e58 100644 --- a/third_party/bouffalolab/bl702l/bl_iot_sdk.gni +++ b/third_party/bouffalolab/bl702l/bl_iot_sdk.gni @@ -368,6 +368,12 @@ 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 { + defines = [ "LFS_THREADSAFE" ] + + include_dirs += + [ "${bl_iot_sdk_root}/components/stage/littlefs/littlefs" ] + include_dirs += [ "${bl_iot_sdk_root}/components/stage/littlefs/port" ] } } @@ -412,6 +418,18 @@ 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 { + defines += [ + "CONFIG_LITTLEFS", + "CONFIG_FREERTOS", + ] + + sources += [ + "${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" ] } public_configs = [ ":${sdk_target_name}_config_stage" ] @@ -706,6 +724,8 @@ 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 { + sources += [ "${bl_iot_sdk_root}/components/network/thread/openthread_port/ot_settings_littlefs.c" ] } libs = [