From bf0c7a1e0b331e5ac890b87bc7a78309ba37790d Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Thu, 3 Feb 2022 14:50:50 +0100 Subject: [PATCH] Add --KVS option for examples/platform/linux in order to specify where the kvs store will be created --- examples/platform/linux/AppMain.cpp | 12 ++++++++++++ examples/platform/linux/Options.cpp | 11 ++++++++++- examples/platform/linux/Options.h | 1 + scripts/tests/chiptest/test_definition.py | 3 --- src/app/server/Server.cpp | 7 ------- src/platform/Darwin/CHIPPlatformConfig.h | 4 ++++ src/platform/Darwin/KeyValueStoreManagerImpl.mm | 2 +- src/platform/Linux/KeyValueStoreManagerImpl.h | 2 +- 8 files changed, 29 insertions(+), 13 deletions(-) diff --git a/examples/platform/linux/AppMain.cpp b/examples/platform/linux/AppMain.cpp index 04f58c677e1274..0089e741c6c77a 100644 --- a/examples/platform/linux/AppMain.cpp +++ b/examples/platform/linux/AppMain.cpp @@ -123,6 +123,18 @@ int ChipLinuxAppInit(int argc, char ** argv) err = Platform::MemoryInit(); SuccessOrExit(err); +#ifdef CHIP_CONFIG_KVS_PATH + if (LinuxDeviceOptions::GetInstance().KVS == nullptr) + { + err = DeviceLayer::PersistedStorage::KeyValueStoreMgrImpl().Init(CHIP_CONFIG_KVS_PATH); + } + else + { + err = DeviceLayer::PersistedStorage::KeyValueStoreMgrImpl().Init(LinuxDeviceOptions::GetInstance().KVS); + } + SuccessOrExit(err); +#endif + err = DeviceLayer::PlatformMgr().InitChipStack(); SuccessOrExit(err); diff --git a/examples/platform/linux/Options.cpp b/examples/platform/linux/Options.cpp index b637a264253662..920757541b0935 100644 --- a/examples/platform/linux/Options.cpp +++ b/examples/platform/linux/Options.cpp @@ -47,7 +47,8 @@ enum kDeviceOption_SecuredCommissionerPort = 0x100b, kDeviceOption_UnsecuredCommissionerPort = 0x100c, kDeviceOption_Command = 0x100d, - kDeviceOption_PICS = 0x100e + kDeviceOption_PICS = 0x100e, + kDeviceOption_KVS = 0x100f }; constexpr unsigned kAppUsageLength = 64; @@ -74,6 +75,7 @@ OptionDef sDeviceOptionDefs[] = { { "unsecured-commissioner-port", kArgumentRequired, kDeviceOption_UnsecuredCommissionerPort }, { "command", kArgumentRequired, kDeviceOption_Command }, { "PICS", kArgumentRequired, kDeviceOption_PICS }, + { "KVS", kArgumentRequired, kDeviceOption_KVS }, {} }; @@ -129,6 +131,9 @@ const char * sDeviceOptionHelp = "\n" " --PICS \n" " A file containing PICS items.\n" + "\n" + " --KVS \n" + " A file to store Key Value Store items.\n" "\n"; bool HandleOption(const char * aProgram, OptionSet * aOptions, int aIdentifier, const char * aName, const char * aValue) @@ -213,6 +218,10 @@ bool HandleOption(const char * aProgram, OptionSet * aOptions, int aIdentifier, LinuxDeviceOptions::GetInstance().PICS = aValue; break; + case kDeviceOption_KVS: + LinuxDeviceOptions::GetInstance().KVS = aValue; + break; + default: PrintArgError("%s: INTERNAL ERROR: Unhandled option: %s\n", aProgram, aName); retval = false; diff --git a/examples/platform/linux/Options.h b/examples/platform/linux/Options.h index 8bb086c1b184c0..5be6667ce52e2a 100644 --- a/examples/platform/linux/Options.h +++ b/examples/platform/linux/Options.h @@ -40,6 +40,7 @@ struct LinuxDeviceOptions uint32_t unsecuredCommissionerPort = CHIP_UDC_PORT; const char * command = nullptr; const char * PICS = nullptr; + const char * KVS = nullptr; static LinuxDeviceOptions & GetInstance(); }; diff --git a/scripts/tests/chiptest/test_definition.py b/scripts/tests/chiptest/test_definition.py index d94478292dad68..71020b0eae4736 100644 --- a/scripts/tests/chiptest/test_definition.py +++ b/scripts/tests/chiptest/test_definition.py @@ -66,9 +66,6 @@ def reboot(self, discriminator): def factoryReset(self): storage = '/tmp/chip_kvs' - if platform.system() == 'Darwin': - storage = str(Path.home()) + '/Documents/chip.store' - if os.path.exists(storage): os.unlink(storage) diff --git a/src/app/server/Server.cpp b/src/app/server/Server.cpp index eb76a55b8de02b..230a0b52132542 100644 --- a/src/app/server/Server.cpp +++ b/src/app/server/Server.cpp @@ -113,13 +113,6 @@ CHIP_ERROR Server::Init(AppDelegate * delegate, uint16_t secureServicePort, uint // handler. SetAttributePersistenceProvider(&mAttributePersister); -#if CHIP_DEVICE_LAYER_TARGET_DARWIN - err = DeviceLayer::PersistedStorage::KeyValueStoreMgrImpl().Init("chip.store"); - SuccessOrExit(err); -#elif CHIP_DEVICE_LAYER_TARGET_LINUX - DeviceLayer::PersistedStorage::KeyValueStoreMgrImpl().Init(CHIP_CONFIG_KVS_PATH); -#endif - InitDataModelHandler(&mExchangeMgr); err = mFabrics.Init(&mDeviceStorage); diff --git a/src/platform/Darwin/CHIPPlatformConfig.h b/src/platform/Darwin/CHIPPlatformConfig.h index 6b99d01dc8277c..c4cd33c1efa8e3 100644 --- a/src/platform/Darwin/CHIPPlatformConfig.h +++ b/src/platform/Darwin/CHIPPlatformConfig.h @@ -74,3 +74,7 @@ // TODO - Fine tune MRP default parameters for Darwin platform #define CHIP_CONFIG_MRP_DEFAULT_INITIAL_RETRY_INTERVAL (15000) #define CHIP_CONFIG_MRP_DEFAULT_ACTIVE_RETRY_INTERVAL (2000_ms32) + +#ifndef CHIP_CONFIG_KVS_PATH +#define CHIP_CONFIG_KVS_PATH "/tmp/chip_kvs" +#endif // CHIP_CONFIG_KVS_PATH diff --git a/src/platform/Darwin/KeyValueStoreManagerImpl.mm b/src/platform/Darwin/KeyValueStoreManagerImpl.mm index 44ca337782578b..6cf59b349fa84f 100644 --- a/src/platform/Darwin/KeyValueStoreManagerImpl.mm +++ b/src/platform/Darwin/KeyValueStoreManagerImpl.mm @@ -165,7 +165,7 @@ - (instancetype)initWithContext:(nonnull NSManagedObjectContext *)context key:(n url = [NSURL URLWithString:[NSString stringWithUTF8String:fileName] relativeToURL:documentsDirectory]; } else { - url = [NSURL URLWithString:[NSString stringWithUTF8String:fileName]]; + url = [NSURL fileURLWithPath:[NSString stringWithUTF8String:fileName]]; } ReturnErrorCodeIf(url == nullptr, CHIP_ERROR_NO_MEMORY); diff --git a/src/platform/Linux/KeyValueStoreManagerImpl.h b/src/platform/Linux/KeyValueStoreManagerImpl.h index a8a9b73a8a7e94..77912c359bc0ce 100644 --- a/src/platform/Linux/KeyValueStoreManagerImpl.h +++ b/src/platform/Linux/KeyValueStoreManagerImpl.h @@ -36,7 +36,7 @@ class KeyValueStoreManagerImpl : public KeyValueStoreManager * @brief * Initalize the KVS, must be called before using. */ - void Init(const char * file) { mStorage.Init(file); } + CHIP_ERROR Init(const char * file) { return mStorage.Init(file); } CHIP_ERROR _Get(const char * key, void * value, size_t value_size, size_t * read_bytes_size = nullptr, size_t offset = 0); CHIP_ERROR _Delete(const char * key);