Skip to content

Commit 8edb9f0

Browse files
Change chip-tool to put its platform KVS in its storage directory. (project-chip#29133)
And also name it chip_tool_kvs, not chip_kvs. This should avoid chip-tool stomping on the KVS of server-side apps.
1 parent f0bb951 commit 8edb9f0

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

examples/chip-tool/commands/common/Commands.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <lib/support/Base64.h>
2929
#include <lib/support/CHIPMem.h>
3030
#include <lib/support/CodeUtils.h>
31+
#include <platform/KeyValueStoreManager.h>
3132

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

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

45+
template <typename T>
46+
struct HasInitWithString
47+
{
48+
template <typename U>
49+
static constexpr auto check(U *) -> typename std::is_same<decltype(std::declval<U>().Init("")), CHIP_ERROR>::type;
50+
51+
template <typename>
52+
static constexpr std::false_type check(...);
53+
54+
typedef decltype(check<std::remove_reference_t<T>>(nullptr)) type;
55+
56+
public:
57+
static constexpr bool value = type::value;
58+
};
59+
60+
// Template so we can do conditional enabling
61+
template <typename T, std::enable_if_t<HasInitWithString<T>::value, int> = 0>
62+
static void UseStorageDirectory(T & storageManagerImpl, const char * storageDirectory)
63+
{
64+
#if !CHIP_DISABLE_PLATFORM_KVS
65+
std::string platformKVS = std::string(storageDirectory) + "/chip_tool_kvs";
66+
storageManagerImpl.Init(platformKVS.c_str());
67+
#endif // !CHIP_DISABLE_PLATFORM_KVS
68+
}
69+
70+
template <typename T, std::enable_if_t<!HasInitWithString<T>::value, int> = 0>
71+
static void UseStorageDirectory(T & storageManagerImpl, const char * storageDirectory)
72+
{}
73+
4474
bool GetArgumentsFromJson(Command * command, Json::Value & value, bool optional, std::vector<std::string> & outArgs)
4575
{
4676
auto memberNames = value.getMemberNames();
@@ -290,6 +320,8 @@ CHIP_ERROR Commands::RunCommand(int argc, char ** argv, bool interactive,
290320
}
291321

292322
chip::Logging::SetLogFilter(mStorage.GetLoggingLevel());
323+
324+
UseStorageDirectory(chip::DeviceLayer::PersistedStorage::KeyValueStoreMgrImpl(), mStorage.GetDirectory());
293325
#endif // CONFIG_USE_LOCAL_STORAGE
294326

295327
return command->Run();

src/controller/ExamplePersistentStorage.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ constexpr const char kLocalNodeIdKey[] = "LocalNodeId";
3939
constexpr const char kCommissionerCATsKey[] = "CommissionerCATs";
4040
constexpr LogCategory kDefaultLoggingLevel = kLogCategory_Automation;
4141

42-
std::string GetFilename(const char * directory, const char * name)
42+
const char * GetUsedDirectory(const char * directory)
4343
{
4444
const char * dir = directory;
4545

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

56+
return dir;
57+
}
58+
59+
std::string GetFilename(const char * directory, const char * name)
60+
{
61+
const char * dir = GetUsedDirectory(directory);
62+
5663
if (name == nullptr)
5764
{
5865
return std::string(dir) + "/chip_tool_config.ini";
@@ -182,6 +189,11 @@ CHIP_ERROR PersistentStorage::SyncClearAll()
182189
return CommitConfig(mDirectory, mName);
183190
}
184191

192+
const char * PersistentStorage::GetDirectory() const
193+
{
194+
return GetUsedDirectory(mDirectory);
195+
}
196+
185197
CHIP_ERROR PersistentStorage::CommitConfig(const char * directory, const char * name)
186198
{
187199
CHIP_ERROR err = CHIP_NO_ERROR;

src/controller/ExamplePersistentStorage.h

+3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ class PersistentStorage : public chip::PersistentStorageDelegate
6565
// Clear all of the persistent storage for running session.
6666
CHIP_ERROR SyncClearAll();
6767

68+
// Get the directory actually being used for the storage.
69+
const char * GetDirectory() const;
70+
6871
private:
6972
CHIP_ERROR CommitConfig(const char * directory, const char * name);
7073
inipp::Ini<char> mConfig;

0 commit comments

Comments
 (0)