From 125674337145fce0c5e4ef1db56801e2a01ef253 Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Fri, 11 Feb 2022 21:08:23 +0100 Subject: [PATCH] [Darwin] Use the KVS for the underlying store for the configuration manager (#15080) --- config/ios/CHIPProjectConfig.h | 2 + .../Framework/CHIP.xcodeproj/project.pbxproj | 2 + .../Darwin/ConfigurationManagerImpl.cpp | 14 -- .../Darwin/ConfigurationManagerImpl.h | 4 - .../Darwin/KeyValueStoreManagerImpl.h | 2 + .../Darwin/KeyValueStoreManagerImpl.mm | 5 + src/platform/Darwin/PosixConfig.cpp | 124 ++++++------------ src/platform/Darwin/PosixConfig.h | 6 + 8 files changed, 54 insertions(+), 105 deletions(-) diff --git a/config/ios/CHIPProjectConfig.h b/config/ios/CHIPProjectConfig.h index 60cf429bdd65c9..0f20f862f1b386 100644 --- a/config/ios/CHIPProjectConfig.h +++ b/config/ios/CHIPProjectConfig.h @@ -43,4 +43,6 @@ #define CHIP_CONFIG_MAX_SOFTWARE_VERSION_LENGTH 128 +#define CHIP_CONFIG_KVS_PATH "chip.store" + #endif /* CHIPPROJECTCONFIG_H */ diff --git a/src/darwin/Framework/CHIP.xcodeproj/project.pbxproj b/src/darwin/Framework/CHIP.xcodeproj/project.pbxproj index b5f01a5bee33b3..4bd000bd6dd34e 100644 --- a/src/darwin/Framework/CHIP.xcodeproj/project.pbxproj +++ b/src/darwin/Framework/CHIP.xcodeproj/project.pbxproj @@ -793,6 +793,8 @@ Foundation, "-framework", CoreBluetooth, + "-framework", + CoreData, "-Wl,-unexported_symbol,\"__Z*\"", ); PRODUCT_BUNDLE_IDENTIFIER = com.chip.CHIP; diff --git a/src/platform/Darwin/ConfigurationManagerImpl.cpp b/src/platform/Darwin/ConfigurationManagerImpl.cpp index a09c9e03c913c5..7aa55b39bf5270 100644 --- a/src/platform/Darwin/ConfigurationManagerImpl.cpp +++ b/src/platform/Darwin/ConfigurationManagerImpl.cpp @@ -194,20 +194,6 @@ CHIP_ERROR ConfigurationManagerImpl::WritePersistedStorageValue(::chip::Platform return WriteConfigValue(configKey, value); } -CHIP_ERROR ConfigurationManagerImpl::StoreCountryCode(const char * code, size_t codeLen) -{ - Platform::CopyString(mCountryCode, kCountryCodeLength + 1, code); - return CHIP_NO_ERROR; -} - -CHIP_ERROR ConfigurationManagerImpl::GetCountryCode(char * buf, size_t bufSize, size_t & codeLen) -{ - Platform::CopyString(buf, kCountryCodeLength + 1, mCountryCode); - codeLen = strlen(mCountryCode); - - return CHIP_NO_ERROR; -} - CHIP_ERROR ConfigurationManagerImpl::ReadConfigValue(Key key, bool & val) { return PosixConfig::ReadConfigValue(key, val); diff --git a/src/platform/Darwin/ConfigurationManagerImpl.h b/src/platform/Darwin/ConfigurationManagerImpl.h index c1c703efc00434..23848ff0bd3cca 100644 --- a/src/platform/Darwin/ConfigurationManagerImpl.h +++ b/src/platform/Darwin/ConfigurationManagerImpl.h @@ -42,8 +42,6 @@ class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImp static ConfigurationManagerImpl & GetDefaultInstance(); private: - char mCountryCode[kCountryCodeLength + 1]; - // ===== Members that implement the ConfigurationManager public interface. CHIP_ERROR Init(void) override; @@ -52,8 +50,6 @@ class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImp void InitiateFactoryReset(void) override; CHIP_ERROR ReadPersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t & value) override; CHIP_ERROR WritePersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t value) override; - CHIP_ERROR StoreCountryCode(const char * code, size_t codeLen) override; - CHIP_ERROR GetCountryCode(char * buf, size_t bufSize, size_t & codeLen) override; // NOTE: Other public interface methods are implemented by GenericConfigurationManagerImpl<>. diff --git a/src/platform/Darwin/KeyValueStoreManagerImpl.h b/src/platform/Darwin/KeyValueStoreManagerImpl.h index 80c75c17b386fb..a210108318660e 100644 --- a/src/platform/Darwin/KeyValueStoreManagerImpl.h +++ b/src/platform/Darwin/KeyValueStoreManagerImpl.h @@ -45,6 +45,8 @@ class KeyValueStoreManagerImpl final : public KeyValueStoreManager friend KeyValueStoreManagerImpl & KeyValueStoreMgrImpl(); static KeyValueStoreManagerImpl sInstance; + + bool mInitialized = false; }; /** diff --git a/src/platform/Darwin/KeyValueStoreManagerImpl.mm b/src/platform/Darwin/KeyValueStoreManagerImpl.mm index 6cf59b349fa84f..b7005aa4c1bf36 100644 --- a/src/platform/Darwin/KeyValueStoreManagerImpl.mm +++ b/src/platform/Darwin/KeyValueStoreManagerImpl.mm @@ -143,6 +143,10 @@ - (instancetype)initWithContext:(nonnull NSManagedObjectContext *)context key:(n CHIP_ERROR KeyValueStoreManagerImpl::Init(const char * fileName) { + if (mInitialized) { + return CHIP_NO_ERROR; + } + ReturnErrorCodeIf(gContext != nullptr, CHIP_ERROR_INCORRECT_STATE); ReturnErrorCodeIf(fileName == nullptr, CHIP_ERROR_INVALID_ARGUMENT); ReturnErrorCodeIf(fileName[0] == '\0', CHIP_ERROR_INVALID_ARGUMENT); @@ -199,6 +203,7 @@ - (instancetype)initWithContext:(nonnull NSManagedObjectContext *)context key:(n gContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; [gContext setPersistentStoreCoordinator:coordinator]; + mInitialized = true; return CHIP_NO_ERROR; } diff --git a/src/platform/Darwin/PosixConfig.cpp b/src/platform/Darwin/PosixConfig.cpp index 1afb9b450ae36b..92c34ac273a648 100644 --- a/src/platform/Darwin/PosixConfig.cpp +++ b/src/platform/Darwin/PosixConfig.cpp @@ -31,6 +31,8 @@ #include #include +#include + namespace chip { namespace DeviceLayer { namespace Internal { @@ -69,152 +71,100 @@ const PosixConfig::Key PosixConfig::kConfigKey_WiFiStationSecType = { kConfigNam const PosixConfig::Key PosixConfig::kConfigKey_RegulatoryLocation = { kConfigNamespace_ChipConfig, "regulatory-location" }; const PosixConfig::Key PosixConfig::kConfigKey_CountryCode = { kConfigNamespace_ChipConfig, "country-code" }; const PosixConfig::Key PosixConfig::kConfigKey_Breadcrumb = { kConfigNamespace_ChipConfig, "breadcrumb" }; +const PosixConfig::Key PosixConfig::kConfigKey_LocationCapability = { kConfigNamespace_ChipConfig, "location-capability" }; + +// Keys stored in the Chip-counters namespace +const PosixConfig::Key PosixConfig::kCounterKey_RebootCount = { kConfigNamespace_ChipCounters, "reboot-count" }; +const PosixConfig::Key PosixConfig::kCounterKey_UpTime = { kConfigNamespace_ChipCounters, "up-time" }; +const PosixConfig::Key PosixConfig::kCounterKey_BootReason = { kConfigNamespace_ChipCounters, "boot-reason" }; +const PosixConfig::Key PosixConfig::kCounterKey_TotalOperationalHours = { kConfigNamespace_ChipCounters, + "total-operational-hours" }; // Prefix used for NVS keys that contain Chip group encryption keys. const char PosixConfig::kGroupKeyNamePrefix[] = "gk-"; -uint16_t PosixConfig::mPosixSetupDiscriminator = 0xF00; // CHIP_DEVICE_CONFIG_USE_TEST_SETUP_DISCRIMINATOR -char PosixConfig::mPosixCountryCode[3] = "XX"; - CHIP_ERROR PosixConfig::Init() { - CHIP_ERROR err = CHIP_NO_ERROR; - return err; + return PersistedStorage::KeyValueStoreMgrImpl().Init(CHIP_CONFIG_KVS_PATH); } CHIP_ERROR PosixConfig::ReadConfigValue(Key key, bool & val) { - CHIP_ERROR err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND; - SuccessOrExit(err); - -exit: - return err; + size_t outLen = 0; + return ReadConfigValueBin(key, reinterpret_cast(&val), sizeof(val), outLen); } CHIP_ERROR PosixConfig::ReadConfigValue(Key key, uint32_t & val) { - CHIP_ERROR err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND; - - if (key == kConfigKey_SetupDiscriminator) - { - val = mPosixSetupDiscriminator; - return CHIP_NO_ERROR; - } - return err; + size_t outLen = 0; + return ReadConfigValueBin(key, reinterpret_cast(&val), sizeof(val), outLen); } CHIP_ERROR PosixConfig::ReadConfigValue(Key key, uint64_t & val) { - CHIP_ERROR err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND; - SuccessOrExit(err); - -exit: - return err; + size_t outLen = 0; + return ReadConfigValueBin(key, reinterpret_cast(&val), sizeof(val), outLen); } CHIP_ERROR PosixConfig::ReadConfigValueStr(Key key, char * buf, size_t bufSize, size_t & outLen) { - CHIP_ERROR err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND; - - if (key == kConfigKey_CountryCode) - { - // Do no assume null-termination on read-out - constexpr size_t kMaxLen = sizeof(PosixConfig::mPosixCountryCode) - 1; - outLen = strnlen(PosixConfig::mPosixCountryCode, kMaxLen); - VerifyOrReturnError(bufSize >= kMaxLen, CHIP_ERROR_BUFFER_TOO_SMALL); - memcpy(&buf[0], &PosixConfig::mPosixCountryCode[0], outLen); - return CHIP_NO_ERROR; - } - - return err; + return ReadConfigValueBin(key, reinterpret_cast(buf), bufSize, outLen); } CHIP_ERROR PosixConfig::ReadConfigValueBin(Key key, uint8_t * buf, size_t bufSize, size_t & outLen) { - CHIP_ERROR err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND; - SuccessOrExit(err); - -exit: + CHIP_ERROR err = PersistedStorage::KeyValueStoreMgr().Get(key.Name, buf, bufSize, &outLen); + if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND) + { + err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND; + } return err; } CHIP_ERROR PosixConfig::WriteConfigValue(Key key, bool val) { - CHIP_ERROR err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND; - SuccessOrExit(err); - -exit: - return err; + return WriteConfigValueBin(key, reinterpret_cast(&val), sizeof(val)); } CHIP_ERROR PosixConfig::WriteConfigValue(Key key, uint32_t val) { - CHIP_ERROR err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND; - - if (key == kConfigKey_SetupDiscriminator) - { - mPosixSetupDiscriminator = val; - return CHIP_NO_ERROR; - } - return err; + return WriteConfigValueBin(key, reinterpret_cast(&val), sizeof(val)); } CHIP_ERROR PosixConfig::WriteConfigValue(Key key, uint64_t val) { - CHIP_ERROR err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND; - SuccessOrExit(err); - -exit: - return err; + return WriteConfigValueBin(key, reinterpret_cast(&val), sizeof(val)); } CHIP_ERROR PosixConfig::WriteConfigValueStr(Key key, const char * str) { - CHIP_ERROR err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND; - SuccessOrExit(err); - -exit: - return err; + return WriteConfigValueBin(key, reinterpret_cast(str), strlen(str)); } CHIP_ERROR PosixConfig::WriteConfigValueStr(Key key, const char * str, size_t strLen) { - if (key == kConfigKey_CountryCode) - { - VerifyOrReturnError(strLen < sizeof(PosixConfig::mPosixCountryCode), CHIP_ERROR_INVALID_ARGUMENT); - memcpy(&PosixConfig::mPosixCountryCode[0], str, strLen); - // Internally null-terminate so we may be able to log later. Don't assume we got null-termination on input. - PosixConfig::mPosixCountryCode[strLen] = '\0'; - return CHIP_NO_ERROR; - } - - CHIP_ERROR err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND; - SuccessOrExit(err); - -exit: - return err; + return WriteConfigValueBin(key, reinterpret_cast(str), strLen); } CHIP_ERROR PosixConfig::WriteConfigValueBin(Key key, const uint8_t * data, size_t dataLen) { - CHIP_ERROR err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND; - SuccessOrExit(err); - -exit: - return err; + return PersistedStorage::KeyValueStoreMgr().Put(key.Name, data, dataLen); } CHIP_ERROR PosixConfig::ClearConfigValue(Key key) { - CHIP_ERROR err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND; - SuccessOrExit(err); - -exit: - return err; + return PersistedStorage::KeyValueStoreMgr().Delete(key.Name); } bool PosixConfig::ConfigValueExists(Key key) { + size_t outLen; + CHIP_ERROR err = ReadConfigValueBin(key, nullptr, 0, outLen); + if (err == CHIP_NO_ERROR || err == CHIP_ERROR_BUFFER_TOO_SMALL) + { + return true; + } + return false; } diff --git a/src/platform/Darwin/PosixConfig.h b/src/platform/Darwin/PosixConfig.h index 2b4c3b44f7d7e5..d94f9ab7a6496f 100644 --- a/src/platform/Darwin/PosixConfig.h +++ b/src/platform/Darwin/PosixConfig.h @@ -76,6 +76,12 @@ class PosixConfig static const Key kConfigKey_Spake2pIterationCount; static const Key kConfigKey_Spake2pSalt; static const Key kConfigKey_Spake2pVerifier; + static const Key kConfigKey_LocationCapability; + + static const Key kCounterKey_TotalOperationalHours; + static const Key kCounterKey_RebootCount; + static const Key kCounterKey_BootReason; + static const Key kCounterKey_UpTime; static const char kGroupKeyNamePrefix[];