Skip to content

Commit

Permalink
Support --storage-space=path to assign config dir
Browse files Browse the repository at this point in the history
Using --KVS=<kvs file> and --storage-space=<dir path>
now we are able to reassing all the configs / counters /
data as well as key-value store to folder other than "/tmp/"
  • Loading branch information
erwinpan1 authored and GinyWang committed Feb 5, 2025
1 parent 577b3f2 commit 7194a26
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 10 deletions.
3 changes: 3 additions & 0 deletions examples/platform/linux/AppMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,9 @@ int ChipLinuxAppInit(int argc, char * const argv[], OptionSet * customOptions,
SuccessOrExit(err);

sSecondaryNetworkCommissioningEndpoint = secondaryNetworkCommissioningEndpoint;
if (LinuxDeviceOptions::GetInstance().StorageSpace != nullptr) {
ConfigurationMgr().SetStorageSpace(LinuxDeviceOptions::GetInstance().StorageSpace);
}

#ifdef CHIP_CONFIG_KVS_PATH
if (LinuxDeviceOptions::GetInstance().KVS == nullptr)
Expand Down
9 changes: 9 additions & 0 deletions examples/platform/linux/Options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ enum
kDeviceOption_TestEventTriggerEnableKey,
kTraceTo,
kOptionSimulateNoInternalTime,
kDeviceOption_StorageSpace
#if defined(PW_RPC_ENABLED)
kOptionRpcServerPort,
#endif
Expand Down Expand Up @@ -170,6 +171,7 @@ OptionDef sDeviceOptionDefs[] = {
#endif
{ "command", kArgumentRequired, kDeviceOption_Command },
{ "PICS", kArgumentRequired, kDeviceOption_PICS },
{ "storage-space", kArgumentRequired, kDeviceOption_StorageSpace },
{ "KVS", kArgumentRequired, kDeviceOption_KVS },
{ "interface-id", kArgumentRequired, kDeviceOption_InterfaceId },
#if CHIP_CONFIG_TRANSPORT_TRACE_ENABLED
Expand Down Expand Up @@ -301,6 +303,9 @@ const char * sDeviceOptionHelp =
" --PICS <filepath>\n"
" A file containing PICS items.\n"
"\n"
" --storage-space <dirpath>\n"
" A folder to store factory data, configs, counters\n"
"\n"
" --KVS <filepath>\n"
" A file to store Key Value Store items.\n"
"\n"
Expand Down Expand Up @@ -602,6 +607,10 @@ bool HandleOption(const char * aProgram, OptionSet * aOptions, int aIdentifier,
LinuxDeviceOptions::GetInstance().PICS = aValue;
break;

case kDeviceOption_StorageSpace:
LinuxDeviceOptions::GetInstance().StorageSpace = aValue;
break;

case kDeviceOption_KVS:
LinuxDeviceOptions::GetInstance().KVS = aValue;
break;
Expand Down
1 change: 1 addition & 0 deletions examples/platform/linux/Options.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ struct LinuxDeviceOptions
#endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
const char * command = nullptr;
const char * PICS = nullptr;
const char * StorageSpace = nullptr;
const char * KVS = nullptr;
chip::Inet::InterfaceId interfaceId = chip::Inet::InterfaceId::Null();
#if CHIP_CONFIG_TRANSPORT_TRACE_ENABLED
Expand Down
4 changes: 3 additions & 1 deletion src/include/platform/ConfigurationManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ class ConfigurationManager

virtual CHIP_ERROR GetLocationCapability(uint8_t & location);

// Set storage path for all configs/data
virtual CHIP_ERROR SetStorageSpace(const char *path) {return CHIP_NO_ERROR;};

protected:
// ===== Members for internal use by the following friends.

Expand All @@ -172,7 +175,6 @@ class ConfigurationManager
// Parentheses used to fix clang parsing issue with these declarations
friend CHIP_ERROR(::chip::Platform::PersistedStorage::Read)(::chip::Platform::PersistedStorage::Key key, uint32_t & value);
friend CHIP_ERROR(::chip::Platform::PersistedStorage::Write)(::chip::Platform::PersistedStorage::Key key, uint32_t value);

virtual CHIP_ERROR Init() = 0;
virtual bool CanFactoryReset() = 0;
virtual CHIP_ERROR ReadPersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t & value) = 0;
Expand Down
24 changes: 18 additions & 6 deletions src/platform/Linux/CHIPLinuxStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,27 @@
#define LOCALSTATEDIR "/tmp"
#endif

#ifndef FATCONFFILE
#define FATCONFFILE "/chip_factory.ini"
#endif

#ifndef SYSCONFFILE
#define SYSCONFFILE "/chip_config.ini"
#endif

#ifndef LOCALSTATEFILE
#define LOCALSTATEFILE "/chip_counters.ini"
#endif

#define CHIP_DEFAULT_FACTORY_PATH \
FATCONFDIR "/" \
"chip_factory.ini"
FATCONFDIR \
FATCONFFILE
#define CHIP_DEFAULT_CONFIG_PATH \
SYSCONFDIR "/" \
"chip_config.ini"
SYSCONFDIR \
SYSCONFFILE
#define CHIP_DEFAULT_DATA_PATH \
LOCALSTATEDIR "/" \
"chip_counters.ini"
LOCALSTATEDIR \
LOCALSTATEFILE

namespace chip {
namespace DeviceLayer {
Expand Down
10 changes: 10 additions & 0 deletions src/platform/Linux/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ ConfigurationManagerImpl & ConfigurationManagerImpl::GetDefaultInstance()
return sInstance;
}

CHIP_ERROR ConfigurationManagerImpl::SetStorageSpace(const char *path)
{
CHIP_ERROR err;

err = PosixConfig::SetStorageSpace(path);

return err;
}


CHIP_ERROR ConfigurationManagerImpl::Init()
{
CHIP_ERROR err;
Expand Down
1 change: 1 addition & 0 deletions src/platform/Linux/ConfigurationManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImp
private:
// ===== Members that implement the ConfigurationManager public interface.

CHIP_ERROR SetStorageSpace(const char *path) override;
CHIP_ERROR Init() override;
CHIP_ERROR GetPrimaryWiFiMACAddress(uint8_t * buf) override;
bool CanFactoryReset() override;
Expand Down
42 changes: 39 additions & 3 deletions src/platform/Linux/PosixConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ namespace Internal {
static ChipLinuxStorage gChipLinuxFactoryStorage;
static ChipLinuxStorage gChipLinuxConfigStorage;
static ChipLinuxStorage gChipLinuxCountersStorage;
static std::string mStorageFolderPath;


// *** CAUTION ***: Changing the names or namespaces of these values will *break* existing devices.

Expand Down Expand Up @@ -95,6 +97,21 @@ ChipLinuxStorage * PosixConfig::GetStorageForNamespace(Key key)
return nullptr;
}

CHIP_ERROR PosixConfig::SetStorageSpace(const char * path)
{
CHIP_ERROR err = CHIP_NO_ERROR;

if (!mStorageFolderPath.empty()) {
ChipLogError(DeviceLayer, "Storage space path has already been set: %s", path);
return CHIP_ERROR_PERSISTED_STORAGE_FAILED;
}
ChipLogDetail(DeviceLayer, "Set storage space path %s for factory data, config, counters.", path);
mStorageFolderPath.assign(path);

return err;
}


CHIP_ERROR PosixConfig::Init()
{
return PersistedStorage::KeyValueStoreMgrImpl().Init(CHIP_CONFIG_KVS_PATH);
Expand Down Expand Up @@ -458,20 +475,39 @@ CHIP_ERROR PosixConfig::EnsureNamespace(const char * ns)
CHIP_ERROR err = CHIP_NO_ERROR;
ChipLinuxStorage * storage = nullptr;

ChipLogDetail(DeviceLayer, "%s, %d, storage folder path: %s", __func__, __LINE__, mStorageFolderPath.c_str());
if (strcmp(ns, kConfigNamespace_ChipFactory) == 0)
{
storage = &gChipLinuxFactoryStorage;
err = storage->Init(CHIP_DEFAULT_FACTORY_PATH);
if (mStorageFolderPath.empty()) {
err = storage->Init(CHIP_DEFAULT_FACTORY_PATH);
} else {
std::string tmp = mStorageFolderPath;
tmp += FATCONFFILE;
err = storage->Init(tmp.c_str());
}
}
else if (strcmp(ns, kConfigNamespace_ChipConfig) == 0)
{
storage = &gChipLinuxConfigStorage;
err = storage->Init(CHIP_DEFAULT_CONFIG_PATH);
if (mStorageFolderPath.empty()) {
err = storage->Init(CHIP_DEFAULT_CONFIG_PATH);
} else {
std::string tmp = mStorageFolderPath;
tmp += SYSCONFFILE;
err = storage->Init(tmp.c_str());
}
}
else if (strcmp(ns, kConfigNamespace_ChipCounters) == 0)
{
storage = &gChipLinuxCountersStorage;
err = storage->Init(CHIP_DEFAULT_DATA_PATH);
if (mStorageFolderPath.empty()) {
err = storage->Init(CHIP_DEFAULT_DATA_PATH);
} else {
std::string tmp = mStorageFolderPath;
tmp += LOCALSTATEFILE;
err = storage->Init(tmp.c_str());
}
}

SuccessOrExit(err);
Expand Down
1 change: 1 addition & 0 deletions src/platform/Linux/PosixConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class PosixConfig
static const Key kCounterKey_TotalOperationalHours;
static const Key kCounterKey_BootReason;

static CHIP_ERROR SetStorageSpace(const char * path);
static CHIP_ERROR Init();

// Config value accessors.
Expand Down

0 comments on commit 7194a26

Please sign in to comment.