Skip to content

Commit

Permalink
Change chip-tool to put its platform KVS in its storage directory.
Browse files Browse the repository at this point in the history
And also name it chip_tool_kvs, not chip_kvs.  This should avoid chip-tool
stomping on the KVS of server-side apps.
  • Loading branch information
bzbarsky-apple committed Sep 8, 2023
1 parent ce66c4d commit c226a76
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
25 changes: 25 additions & 0 deletions examples/chip-tool/commands/common/Commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ 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>>(0)) type;

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

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

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

{
using namespace chip::DeviceLayer::PersistedStorage;
if constexpr (HasInitWithString<decltype(KeyValueStoreMgrImpl())>::value)
{
std::string storageDirectory = mStorage.GetDirectory();
std::string platformKVS = storageDirectory + "/chip_tool_kvs";
KeyValueStoreMgrImpl().Init(platformKVS.c_str());
}
}
#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

0 comments on commit c226a76

Please sign in to comment.