Skip to content

Commit

Permalink
Add a config variable for disabling platform KVS. (#17777)
Browse files Browse the repository at this point in the history
Implement support for doing that on Darwin, and enable that support
when building the Darwin framework, via either Xcode or
chip-tool-darwin.
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Feb 17, 2024
1 parent ca4bae4 commit 2632405
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 24 deletions.
2 changes: 2 additions & 0 deletions examples/chip-tool-darwin/.gn
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ check_system_includes = true

default_args = {
import("//args.gni")

chip_disable_platform_kvs = true
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
auto params = [[MatterControllerFactoryParams alloc] initWithStorage:storage];
params.port = @(kListenPort);
params.startServer = YES;
params.kvsPath = @("/tmp/chip_kvs_darwin");

if ([factory startup:params] == NO) {
ChipLogError(chipTool, "Controller factory startup failed");
Expand Down
5 changes: 0 additions & 5 deletions src/darwin/Framework/CHIP/MatterControllerFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ NS_ASSUME_NONNULL_BEGIN
* connections. Defaults to NO.
*/
@property (nonatomic) BOOL startServer;
/*
* Path to a file to use for backing our KVS storage. This should not
* be used, generally; it will be removed soon. Defaults to nil.
*/
@property (strong, nonatomic, nullable) NSString * kvsPath;

- (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithStorage:(id<CHIPPersistentStorageDelegate>)storageDelegate;
Expand Down
11 changes: 0 additions & 11 deletions src/darwin/Framework/CHIP/MatterControllerFactory.mm
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
static NSString * const kErrorAttestationTrustStoreInit = @"Init failure while creating the attestation trust store";
static NSString * const kInfoFactoryShutdown = @"Shutting down the Matter controller factory";
static NSString * const kErrorGroupProviderInit = @"Init failure while initializing group data provider";
static NSString * const kErrorKVSInit = @"Init Key Value Store failure";
static NSString * const kErrorControllersInit = @"Init controllers array failure";
static NSString * const kErrorControllerFactoryInit = @"Init failure while initializing controller factory";

Expand Down Expand Up @@ -179,15 +178,6 @@ - (BOOL)startup:(MatterControllerFactoryParams *)startupParams

[CHIPControllerAccessControl init];

if (startupParams.kvsPath != nil) {
// TODO: We should stop needing a KeyValueStoreManager on the client side, then remove this code.
CHIP_ERROR errorCode = DeviceLayer::PersistedStorage::KeyValueStoreMgrImpl().Init([startupParams.kvsPath UTF8String]);
if (errorCode != CHIP_NO_ERROR) {
CHIP_LOG_ERROR("Error: %@", kErrorKVSInit);
return;
}
}

_persistentStorageDelegateBridge = new CHIPPersistentStorageDelegateBridge(startupParams.storageDelegate);
if (_persistentStorageDelegateBridge == nil) {
CHIP_LOG_ERROR("Error: %@", kErrorPersistentStorageInit);
Expand Down Expand Up @@ -468,7 +458,6 @@ - (instancetype)initWithStorage:(id<CHIPPersistentStorageDelegate>)storageDelega
_paaCerts = nil;
_port = nil;
_startServer = NO;
_kvsPath = nil;

return self;
}
Expand Down
1 change: 1 addition & 0 deletions src/darwin/Framework/chip_xcode_build_connector.sh
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ declare -a args=(
'chip_crypto="mbedtls"'
'chip_build_tools=false'
'chip_build_tests=false'
'chip_disable_platform_kvs=true'
'target_cpu="'"$target_cpu"'"'
'target_defines='"$target_defines"
'target_cflags=['"$target_cflags"']'
Expand Down
1 change: 1 addition & 0 deletions src/platform/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ if (chip_device_platform != "none" && chip_device_platform != "external") {
"CHIP_STACK_LOCK_TRACKING_ERROR_FATAL=${chip_stack_lock_tracking_fatal}",
"CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING=${chip_enable_additional_data_advertising}",
"CHIP_DEVICE_CONFIG_RUN_AS_ROOT=${chip_device_config_run_as_root}",
"CHIP_DISABLE_PLATFORM_KVS=${chip_disable_platform_kvs}",
]

if (chip_device_platform == "linux" || chip_device_platform == "darwin" ||
Expand Down
9 changes: 7 additions & 2 deletions src/platform/Darwin/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ static_library("Darwin") {
"DnssdImpl.cpp",
"DnssdImpl.h",
"InetPlatformConfig.h",
"KeyValueStoreManagerImpl.h",
"KeyValueStoreManagerImpl.mm",
"Logging.cpp",
"MdnsError.cpp",
"MdnsError.h",
Expand All @@ -68,6 +66,13 @@ static_library("Darwin") {
"SystemTimeSupport.cpp",
]

if (chip_disable_platform_kvs == false) {
sources += [
"KeyValueStoreManagerImpl.h",
"KeyValueStoreManagerImpl.mm",
]
}

deps = [
"${chip_root}/src/lib/dnssd:platform_header",
"${chip_root}/src/setup_payload",
Expand Down
116 changes: 116 additions & 0 deletions src/platform/Darwin/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ ConfigurationManagerImpl & ConfigurationManagerImpl::GetDefaultInstance()

CHIP_ERROR ConfigurationManagerImpl::Init()
{
#if CHIP_DISABLE_PLATFORM_KVS
return CHIP_NO_ERROR;
#else // CHIP_DISABLE_PLATFORM_KVS
// Initialize the generic implementation base class.
ReturnErrorOnFailure(Internal::GenericConfigurationManagerImpl<PosixConfig>::Init());

Expand Down Expand Up @@ -199,6 +202,7 @@ CHIP_ERROR ConfigurationManagerImpl::Init()
}

return CHIP_NO_ERROR;
#endif // CHIP_DISABLE_PLATFORM_KVS
}

CHIP_ERROR ConfigurationManagerImpl::GetPrimaryWiFiMACAddress(uint8_t * buf)
Expand Down Expand Up @@ -232,56 +236,99 @@ void ConfigurationManagerImpl::InitiateFactoryReset()

CHIP_ERROR ConfigurationManagerImpl::GetVendorId(uint16_t & vendorId)
{
#if CHIP_DISABLE_PLATFORM_KVS
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#else // CHIP_DISABLE_PLATFORM_KVS
return ReadConfigValue(PosixConfig::kConfigKey_VendorId, vendorId);
#endif // CHIP_DISABLE_PLATFORM_KVS
}

CHIP_ERROR ConfigurationManagerImpl::GetProductId(uint16_t & productId)
{
#if CHIP_DISABLE_PLATFORM_KVS
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#else // CHIP_DISABLE_PLATFORM_KVS
return ReadConfigValue(PosixConfig::kConfigKey_ProductId, productId);
#endif // CHIP_DISABLE_PLATFORM_KVS
}

CHIP_ERROR ConfigurationManagerImpl::StoreVendorId(uint16_t vendorId)
{
#if CHIP_DISABLE_PLATFORM_KVS
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#else // CHIP_DISABLE_PLATFORM_KVS
return WriteConfigValue(PosixConfig::kConfigKey_VendorId, vendorId);
#endif // CHIP_DISABLE_PLATFORM_KVS
}

CHIP_ERROR ConfigurationManagerImpl::StoreProductId(uint16_t productId)
{
#if CHIP_DISABLE_PLATFORM_KVS
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#else // CHIP_DISABLE_PLATFORM_KVS
return WriteConfigValue(PosixConfig::kConfigKey_ProductId, productId);
#endif // CHIP_DISABLE_PLATFORM_KVS
}

CHIP_ERROR ConfigurationManagerImpl::GetRebootCount(uint32_t & rebootCount)
{
#if CHIP_DISABLE_PLATFORM_KVS
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#else // CHIP_DISABLE_PLATFORM_KVS
return ReadConfigValue(PosixConfig::kCounterKey_RebootCount, rebootCount);
#endif // CHIP_DISABLE_PLATFORM_KVS
}

CHIP_ERROR ConfigurationManagerImpl::StoreRebootCount(uint32_t rebootCount)
{
#if CHIP_DISABLE_PLATFORM_KVS
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#else // CHIP_DISABLE_PLATFORM_KVS
return WriteConfigValue(PosixConfig::kCounterKey_RebootCount, rebootCount);
#endif // CHIP_DISABLE_PLATFORM_KVS
}

CHIP_ERROR ConfigurationManagerImpl::GetTotalOperationalHours(uint32_t & totalOperationalHours)
{
#if CHIP_DISABLE_PLATFORM_KVS
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#else // CHIP_DISABLE_PLATFORM_KVS
return ReadConfigValue(PosixConfig::kCounterKey_TotalOperationalHours, totalOperationalHours);
#endif // CHIP_DISABLE_PLATFORM_KVS
}

CHIP_ERROR ConfigurationManagerImpl::StoreTotalOperationalHours(uint32_t totalOperationalHours)
{
#if CHIP_DISABLE_PLATFORM_KVS
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#else // CHIP_DISABLE_PLATFORM_KVS
return WriteConfigValue(PosixConfig::kCounterKey_TotalOperationalHours, totalOperationalHours);
#endif // CHIP_DISABLE_PLATFORM_KVS
}

CHIP_ERROR ConfigurationManagerImpl::GetBootReason(uint32_t & bootReason)
{
#if CHIP_DISABLE_PLATFORM_KVS
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#else // CHIP_DISABLE_PLATFORM_KVS
return ReadConfigValue(PosixConfig::kCounterKey_BootReason, bootReason);
#endif // CHIP_DISABLE_PLATFORM_KVS
}

CHIP_ERROR ConfigurationManagerImpl::StoreBootReason(uint32_t bootReason)
{
#if CHIP_DISABLE_PLATFORM_KVS
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#else // CHIP_DISABLE_PLATFORM_KVS
return WriteConfigValue(PosixConfig::kCounterKey_BootReason, bootReason);
#endif // CHIP_DISABLE_PLATFORM_KVS
}

CHIP_ERROR ConfigurationManagerImpl::GetRegulatoryLocation(uint8_t & location)
{
#if CHIP_DISABLE_PLATFORM_KVS
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#else // CHIP_DISABLE_PLATFORM_KVS
uint32_t value = 0;

CHIP_ERROR err = ReadConfigValue(PosixConfig::kConfigKey_RegulatoryLocation, value);
Expand All @@ -293,10 +340,14 @@ CHIP_ERROR ConfigurationManagerImpl::GetRegulatoryLocation(uint8_t & location)
}

return err;
#endif // CHIP_DISABLE_PLATFORM_KVS
}

CHIP_ERROR ConfigurationManagerImpl::GetLocationCapability(uint8_t & location)
{
#if CHIP_DISABLE_PLATFORM_KVS
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#else // CHIP_DISABLE_PLATFORM_KVS
uint32_t value = 0;

CHIP_ERROR err = ReadConfigValue(PosixConfig::kConfigKey_LocationCapability, value);
Expand All @@ -308,10 +359,14 @@ CHIP_ERROR ConfigurationManagerImpl::GetLocationCapability(uint8_t & location)
}

return err;
#endif // CHIP_DISABLE_PLATFORM_KVS
}

CHIP_ERROR ConfigurationManagerImpl::ReadPersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t & value)
{
#if CHIP_DISABLE_PLATFORM_KVS
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#else // CHIP_DISABLE_PLATFORM_KVS
PosixConfig::Key configKey{ PosixConfig::kConfigNamespace_ChipCounters, key };

CHIP_ERROR err = ReadConfigValue(configKey, value);
Expand All @@ -320,82 +375,143 @@ CHIP_ERROR ConfigurationManagerImpl::ReadPersistedStorageValue(::chip::Platform:
err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
}
return err;
#endif // CHIP_DISABLE_PLATFORM_KVS
}

CHIP_ERROR ConfigurationManagerImpl::WritePersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t value)
{
#if CHIP_DISABLE_PLATFORM_KVS
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#else // CHIP_DISABLE_PLATFORM_KVS
PosixConfig::Key configKey{ PosixConfig::kConfigNamespace_ChipCounters, key };
return WriteConfigValue(configKey, value);
#endif // CHIP_DISABLE_PLATFORM_KVS
}

CHIP_ERROR ConfigurationManagerImpl::ReadConfigValue(Key key, bool & val)
{
#if CHIP_DISABLE_PLATFORM_KVS
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#else // CHIP_DISABLE_PLATFORM_KVS
return PosixConfig::ReadConfigValue(key, val);
#endif // CHIP_DISABLE_PLATFORM_KVS
}

CHIP_ERROR ConfigurationManagerImpl::ReadConfigValue(Key key, uint16_t & val)
{
#if CHIP_DISABLE_PLATFORM_KVS
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#else // CHIP_DISABLE_PLATFORM_KVS
return PosixConfig::ReadConfigValue(key, val);
#endif // CHIP_DISABLE_PLATFORM_KVS
}

CHIP_ERROR ConfigurationManagerImpl::ReadConfigValue(Key key, uint32_t & val)
{
#if CHIP_DISABLE_PLATFORM_KVS
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#else // CHIP_DISABLE_PLATFORM_KVS
return PosixConfig::ReadConfigValue(key, val);
#endif // CHIP_DISABLE_PLATFORM_KVS
}

CHIP_ERROR ConfigurationManagerImpl::ReadConfigValue(Key key, uint64_t & val)
{
#if CHIP_DISABLE_PLATFORM_KVS
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#else // CHIP_DISABLE_PLATFORM_KVS
return PosixConfig::ReadConfigValue(key, val);
#endif // CHIP_DISABLE_PLATFORM_KVS
}

CHIP_ERROR ConfigurationManagerImpl::ReadConfigValueStr(Key key, char * buf, size_t bufSize, size_t & outLen)
{
#if CHIP_DISABLE_PLATFORM_KVS
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#else // CHIP_DISABLE_PLATFORM_KVS
return PosixConfig::ReadConfigValueStr(key, buf, bufSize, outLen);
#endif // CHIP_DISABLE_PLATFORM_KVS
}

CHIP_ERROR ConfigurationManagerImpl::ReadConfigValueBin(Key key, uint8_t * buf, size_t bufSize, size_t & outLen)
{
#if CHIP_DISABLE_PLATFORM_KVS
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#else // CHIP_DISABLE_PLATFORM_KVS
return PosixConfig::ReadConfigValueBin(key, buf, bufSize, outLen);
#endif // CHIP_DISABLE_PLATFORM_KVS
}

CHIP_ERROR ConfigurationManagerImpl::WriteConfigValue(Key key, bool val)
{
#if CHIP_DISABLE_PLATFORM_KVS
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#else // CHIP_DISABLE_PLATFORM_KVS
return PosixConfig::WriteConfigValue(key, val);
#endif // CHIP_DISABLE_PLATFORM_KVS
}

CHIP_ERROR ConfigurationManagerImpl::WriteConfigValue(Key key, uint16_t val)
{
#if CHIP_DISABLE_PLATFORM_KVS
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#else // CHIP_DISABLE_PLATFORM_KVS
return PosixConfig::WriteConfigValue(key, val);
#endif // CHIP_DISABLE_PLATFORM_KVS
}

CHIP_ERROR ConfigurationManagerImpl::WriteConfigValue(Key key, uint32_t val)
{
#if CHIP_DISABLE_PLATFORM_KVS
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#else // CHIP_DISABLE_PLATFORM_KVS
return PosixConfig::WriteConfigValue(key, val);
#endif // CHIP_DISABLE_PLATFORM_KVS
}

CHIP_ERROR ConfigurationManagerImpl::WriteConfigValue(Key key, uint64_t val)
{
#if CHIP_DISABLE_PLATFORM_KVS
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#else // CHIP_DISABLE_PLATFORM_KVS
return PosixConfig::WriteConfigValue(key, val);
#endif // CHIP_DISABLE_PLATFORM_KVS
}

CHIP_ERROR ConfigurationManagerImpl::WriteConfigValueStr(Key key, const char * str)
{
#if CHIP_DISABLE_PLATFORM_KVS
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#else // CHIP_DISABLE_PLATFORM_KVS
return PosixConfig::WriteConfigValueStr(key, str);
#endif // CHIP_DISABLE_PLATFORM_KVS
}

CHIP_ERROR ConfigurationManagerImpl::WriteConfigValueStr(Key key, const char * str, size_t strLen)
{
#if CHIP_DISABLE_PLATFORM_KVS
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#else // CHIP_DISABLE_PLATFORM_KVS
return PosixConfig::WriteConfigValueStr(key, str, strLen);
#endif // CHIP_DISABLE_PLATFORM_KVS
}

CHIP_ERROR ConfigurationManagerImpl::WriteConfigValueBin(Key key, const uint8_t * data, size_t dataLen)
{
#if CHIP_DISABLE_PLATFORM_KVS
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#else // CHIP_DISABLE_PLATFORM_KVS
return PosixConfig::WriteConfigValueBin(key, data, dataLen);
#endif // CHIP_DISABLE_PLATFORM_KVS
}

void ConfigurationManagerImpl::RunConfigUnitTest(void)
{
#if CHIP_DISABLE_PLATFORM_KVS
return;
#else // CHIP_DISABLE_PLATFORM_KVS
PosixConfig::RunConfigUnitTest();
#endif // CHIP_DISABLE_PLATFORM_KVS
}

} // namespace DeviceLayer
Expand Down
Loading

0 comments on commit 2632405

Please sign in to comment.