Skip to content

Commit

Permalink
[Darwin] Use the KVS for the underlying store for the configuration m…
Browse files Browse the repository at this point in the history
…anager (#15080)
  • Loading branch information
vivien-apple authored and pull[bot] committed Feb 23, 2022
1 parent 75ed145 commit 1256743
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 105 deletions.
2 changes: 2 additions & 0 deletions config/ios/CHIPProjectConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@

#define CHIP_CONFIG_MAX_SOFTWARE_VERSION_LENGTH 128

#define CHIP_CONFIG_KVS_PATH "chip.store"

#endif /* CHIPPROJECTCONFIG_H */
2 changes: 2 additions & 0 deletions src/darwin/Framework/CHIP.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,8 @@
Foundation,
"-framework",
CoreBluetooth,
"-framework",
CoreData,
"-Wl,-unexported_symbol,\"__Z*\"",
);
PRODUCT_BUNDLE_IDENTIFIER = com.chip.CHIP;
Expand Down
14 changes: 0 additions & 14 deletions src/platform/Darwin/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 0 additions & 4 deletions src/platform/Darwin/ConfigurationManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<>.

Expand Down
2 changes: 2 additions & 0 deletions src/platform/Darwin/KeyValueStoreManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class KeyValueStoreManagerImpl final : public KeyValueStoreManager
friend KeyValueStoreManagerImpl & KeyValueStoreMgrImpl();

static KeyValueStoreManagerImpl sInstance;

bool mInitialized = false;
};

/**
Expand Down
5 changes: 5 additions & 0 deletions src/platform/Darwin/KeyValueStoreManagerImpl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down
124 changes: 37 additions & 87 deletions src/platform/Darwin/PosixConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <lib/support/CodeUtils.h>
#include <platform/Darwin/PosixConfig.h>

#include <platform/KeyValueStoreManager.h>

namespace chip {
namespace DeviceLayer {
namespace Internal {
Expand Down Expand Up @@ -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<uint8_t *>(&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<uint8_t *>(&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<uint8_t *>(&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<uint8_t *>(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<uint8_t *>(&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<uint8_t *>(&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<uint8_t *>(&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<const uint8_t *>(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<const uint8_t *>(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;
}

Expand Down
6 changes: 6 additions & 0 deletions src/platform/Darwin/PosixConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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[];

Expand Down

0 comments on commit 1256743

Please sign in to comment.