Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change chip-tool to put its platform KVS in its storage directory. #29133

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions examples/chip-tool/commands/common/Commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <lib/support/Base64.h>
#include <lib/support/CHIPMem.h>
#include <lib/support/CodeUtils.h>
#include <platform/KeyValueStoreManager.h>

#include "../clusters/JsonParser.h"

Expand All @@ -41,6 +42,35 @@ constexpr const char * kJsonCommandKey = "command";
constexpr const char * kJsonCommandSpecifierKey = "command_specifier";
constexpr const char * kJsonArgumentsKey = "arguments";

template <typename T>
struct HasInitWithString
{
template <typename U>
static constexpr auto check(U *) -> typename std::is_same<decltype(std::declval<U>().Init("")), CHIP_ERROR>::type;

template <typename>
static constexpr std::false_type check(...);

typedef decltype(check<std::remove_reference_t<T>>(nullptr)) type;

public:
static constexpr bool value = type::value;
};

// Template so we can do conditional enabling
template <typename T, std::enable_if_t<HasInitWithString<T>::value, int> = 0>
static void UseStorageDirectory(T & storageManagerImpl, const char * storageDirectory)
{
#if !CHIP_DISABLE_PLATFORM_KVS
std::string platformKVS = std::string(storageDirectory) + "/chip_tool_kvs";
tcarmelveilleux marked this conversation as resolved.
Show resolved Hide resolved
storageManagerImpl.Init(platformKVS.c_str());
#endif // !CHIP_DISABLE_PLATFORM_KVS
}

template <typename T, std::enable_if_t<!HasInitWithString<T>::value, int> = 0>
static void UseStorageDirectory(T & storageManagerImpl, const char * storageDirectory)
{}

bool GetArgumentsFromJson(Command * command, Json::Value & value, bool optional, std::vector<std::string> & outArgs)
{
auto memberNames = value.getMemberNames();
Expand Down Expand Up @@ -290,6 +320,8 @@ CHIP_ERROR Commands::RunCommand(int argc, char ** argv, bool interactive,
}

chip::Logging::SetLogFilter(mStorage.GetLoggingLevel());

UseStorageDirectory(chip::DeviceLayer::PersistedStorage::KeyValueStoreMgrImpl(), mStorage.GetDirectory());
#endif // CONFIG_USE_LOCAL_STORAGE

return command->Run();
Expand Down
14 changes: 13 additions & 1 deletion src/controller/ExamplePersistentStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ constexpr const char kLocalNodeIdKey[] = "LocalNodeId";
constexpr const char kCommissionerCATsKey[] = "CommissionerCATs";
constexpr LogCategory kDefaultLoggingLevel = kLogCategory_Automation;

std::string GetFilename(const char * directory, const char * name)
const char * GetUsedDirectory(const char * directory)
{
const char * dir = directory;

Expand All @@ -53,6 +53,13 @@ std::string GetFilename(const char * directory, const char * name)
dir = "/tmp";
}

return dir;
}

std::string GetFilename(const char * directory, const char * name)
{
const char * dir = GetUsedDirectory(directory);

if (name == nullptr)
{
return std::string(dir) + "/chip_tool_config.ini";
Expand Down Expand Up @@ -182,6 +189,11 @@ CHIP_ERROR PersistentStorage::SyncClearAll()
return CommitConfig(mDirectory, mName);
}

const char * PersistentStorage::GetDirectory() const
{
return GetUsedDirectory(mDirectory);
}

CHIP_ERROR PersistentStorage::CommitConfig(const char * directory, const char * name)
{
CHIP_ERROR err = CHIP_NO_ERROR;
Expand Down
3 changes: 3 additions & 0 deletions src/controller/ExamplePersistentStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ class PersistentStorage : public chip::PersistentStorageDelegate
// Clear all of the persistent storage for running session.
CHIP_ERROR SyncClearAll();

// Get the directory actually being used for the storage.
const char * GetDirectory() const;

private:
CHIP_ERROR CommitConfig(const char * directory, const char * name);
inipp::Ini<char> mConfig;
Expand Down
Loading