Skip to content

Commit

Permalink
Remove GenericConfigurationManagerImpl's CRTP (#11784)
Browse files Browse the repository at this point in the history
* Explicitly add the config value methods to the generic configuration manager's as a protected interface.

Add the config class as a template parameter.

* Remove the ImplClass from the GenericConfigurationManagerImpl template.

* Minor cleanup.

* Restyling with clang-format.

* Apply the same changes to the other platforms.

* Fix Linux with the same changes.

* Fix remaining platforms.

* Remove the Key namespacing since it's already defined in the superclass.

This fixes the doxygen generation.

* Remove the explicit template definitions.
  • Loading branch information
harimau-qirex authored and pull[bot] committed May 14, 2022
1 parent ca6c3d6 commit 2143ea5
Show file tree
Hide file tree
Showing 31 changed files with 1,186 additions and 305 deletions.
202 changes: 100 additions & 102 deletions src/include/platform/internal/GenericConfigurationManagerImpl.cpp

Large diffs are not rendered by default.

48 changes: 28 additions & 20 deletions src/include/platform/internal/GenericConfigurationManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ namespace Internal {
*
* This template contains implementations of select features from the ConfigurationManager abstract
* interface that are suitable for use on all platforms. It is intended to be inherited (directly
* or indirectly) by the ConfigurationManagerImpl class, which also appears as the template's ImplClass
* parameter.
* or indirectly) by the ConfigurationManagerImpl class.
*/
template <class ImplClass>
template <class ConfigClass>
class GenericConfigurationManagerImpl : public ConfigurationManager
{
public:
Expand Down Expand Up @@ -114,50 +113,59 @@ class GenericConfigurationManagerImpl : public ConfigurationManager
#endif
CHIP_ERROR PersistProvisioningData(ProvisioningDataSet & provData);

private:
ImplClass * Impl() { return static_cast<ImplClass *>(this); }
// Methods to read and write configuration values, as well as run the configuration unit test.
typedef typename ConfigClass::Key Key;
virtual CHIP_ERROR ReadConfigValue(Key key, bool & val) = 0;
virtual CHIP_ERROR ReadConfigValue(Key key, uint32_t & val) = 0;
virtual CHIP_ERROR ReadConfigValue(Key key, uint64_t & val) = 0;
virtual CHIP_ERROR ReadConfigValueStr(Key key, char * buf, size_t bufSize, size_t & outLen) = 0;
virtual CHIP_ERROR ReadConfigValueBin(Key key, uint8_t * buf, size_t bufSize, size_t & outLen) = 0;
virtual CHIP_ERROR WriteConfigValue(Key key, bool val) = 0;
virtual CHIP_ERROR WriteConfigValue(Key key, uint32_t val) = 0;
virtual CHIP_ERROR WriteConfigValue(Key key, uint64_t val) = 0;
virtual CHIP_ERROR WriteConfigValueStr(Key key, const char * str) = 0;
virtual CHIP_ERROR WriteConfigValueStr(Key key, const char * str, size_t strLen) = 0;
virtual CHIP_ERROR WriteConfigValueBin(Key key, const uint8_t * data, size_t dataLen) = 0;
virtual void RunConfigUnitTest(void) = 0;
};

// Instruct the compiler to instantiate the template only when explicitly told to do so.
extern template class Internal::GenericConfigurationManagerImpl<ConfigurationManagerImpl>;

template <class ImplClass>
inline CHIP_ERROR GenericConfigurationManagerImpl<ImplClass>::GetVendorId(uint16_t & vendorId)
template <class ConfigClass>
inline CHIP_ERROR GenericConfigurationManagerImpl<ConfigClass>::GetVendorId(uint16_t & vendorId)
{
vendorId = static_cast<uint16_t>(CHIP_DEVICE_CONFIG_DEVICE_VENDOR_ID);
return CHIP_NO_ERROR;
}

template <class ImplClass>
inline CHIP_ERROR GenericConfigurationManagerImpl<ImplClass>::GetProductId(uint16_t & productId)
template <class ConfigClass>
inline CHIP_ERROR GenericConfigurationManagerImpl<ConfigClass>::GetProductId(uint16_t & productId)
{
productId = static_cast<uint16_t>(CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID);
return CHIP_NO_ERROR;
}

template <class ImplClass>
inline CHIP_ERROR GenericConfigurationManagerImpl<ImplClass>::GetFirmwareRevision(uint16_t & firmwareRev)
template <class ConfigClass>
inline CHIP_ERROR GenericConfigurationManagerImpl<ConfigClass>::GetFirmwareRevision(uint16_t & firmwareRev)
{
firmwareRev = static_cast<uint32_t>(CHIP_DEVICE_CONFIG_DEVICE_FIRMWARE_REVISION);
return CHIP_NO_ERROR;
}

template <class ImplClass>
inline CHIP_ERROR GenericConfigurationManagerImpl<ImplClass>::GetDeviceTypeId(uint16_t & deviceType)
template <class ConfigClass>
inline CHIP_ERROR GenericConfigurationManagerImpl<ConfigClass>::GetDeviceTypeId(uint16_t & deviceType)
{
deviceType = static_cast<uint16_t>(CHIP_DEVICE_CONFIG_DEVICE_TYPE);
return CHIP_NO_ERROR;
}

template <class ImplClass>
inline CHIP_ERROR GenericConfigurationManagerImpl<ImplClass>::GetInitialPairingHint(uint16_t & pairingHint)
template <class ConfigClass>
inline CHIP_ERROR GenericConfigurationManagerImpl<ConfigClass>::GetInitialPairingHint(uint16_t & pairingHint)
{
pairingHint = static_cast<uint16_t>(CHIP_DEVICE_CONFIG_PAIRING_INITIAL_HINT);
return CHIP_NO_ERROR;
}

template <class ImplClass>
inline CHIP_ERROR GenericConfigurationManagerImpl<ImplClass>::GetSecondaryPairingHint(uint16_t & pairingHint)
template <class ConfigClass>
inline CHIP_ERROR GenericConfigurationManagerImpl<ConfigClass>::GetSecondaryPairingHint(uint16_t & pairingHint)
{
pairingHint = static_cast<uint16_t>(CHIP_DEVICE_CONFIG_PAIRING_SECONDARY_HINT);
return CHIP_NO_ERROR;
Expand Down
92 changes: 76 additions & 16 deletions src/platform/Ameba/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ CHIP_ERROR ConfigurationManagerImpl::Init()
bool failSafeArmed;

// Force initialization of NVS namespaces if they doesn't already exist.
err = EnsureNamespace(kConfigNamespace_ChipFactory);
err = AmebaConfig::EnsureNamespace(AmebaConfig::kConfigNamespace_ChipFactory);
SuccessOrExit(err);
err = EnsureNamespace(kConfigNamespace_ChipConfig);
err = AmebaConfig::EnsureNamespace(AmebaConfig::kConfigNamespace_ChipConfig);
SuccessOrExit(err);
err = EnsureNamespace(kConfigNamespace_ChipCounters);
err = AmebaConfig::EnsureNamespace(AmebaConfig::kConfigNamespace_ChipCounters);
SuccessOrExit(err);

if (ConfigValueExists(kCounterKey_RebootCount))
if (AmebaConfig::ConfigValueExists(AmebaConfig::kCounterKey_RebootCount))
{
err = GetRebootCount(rebootCount);
SuccessOrExit(err);
Expand All @@ -71,20 +71,20 @@ CHIP_ERROR ConfigurationManagerImpl::Init()
SuccessOrExit(err);
}

if (!ConfigValueExists(kCounterKey_TotalOperationalHours))
if (!AmebaConfig::ConfigValueExists(AmebaConfig::kCounterKey_TotalOperationalHours))
{
err = StoreTotalOperationalHours(0);
SuccessOrExit(err);
}

if (!ConfigValueExists(kCounterKey_BootReason))
if (!AmebaConfig::ConfigValueExists(AmebaConfig::kCounterKey_BootReason))
{
err = StoreBootReasons(EMBER_ZCL_BOOT_REASON_TYPE_UNSPECIFIED);
SuccessOrExit(err);
}

// Initialize the generic implementation base class.
err = Internal::GenericConfigurationManagerImpl<ConfigurationManagerImpl>::Init();
err = Internal::GenericConfigurationManagerImpl<AmebaConfig>::Init();
SuccessOrExit(err);

// If the fail-safe was armed when the device last shutdown, initiate a factory reset.
Expand All @@ -101,32 +101,32 @@ CHIP_ERROR ConfigurationManagerImpl::Init()

CHIP_ERROR ConfigurationManagerImpl::GetRebootCount(uint32_t & rebootCount)
{
return ReadConfigValue(kCounterKey_RebootCount, rebootCount);
return ReadConfigValue(AmebaConfig::kCounterKey_RebootCount, rebootCount);
}

CHIP_ERROR ConfigurationManagerImpl::StoreRebootCount(uint32_t rebootCount)
{
return WriteConfigValue(kCounterKey_RebootCount, rebootCount);
return WriteConfigValue(AmebaConfig::kCounterKey_RebootCount, rebootCount);
}

CHIP_ERROR ConfigurationManagerImpl::GetTotalOperationalHours(uint32_t & totalOperationalHours)
{
return ReadConfigValue(kCounterKey_TotalOperationalHours, totalOperationalHours);
return ReadConfigValue(AmebaConfig::kCounterKey_TotalOperationalHours, totalOperationalHours);
}

CHIP_ERROR ConfigurationManagerImpl::StoreTotalOperationalHours(uint32_t totalOperationalHours)
{
return WriteConfigValue(kCounterKey_TotalOperationalHours, totalOperationalHours);
return WriteConfigValue(AmebaConfig::kCounterKey_TotalOperationalHours, totalOperationalHours);
}

CHIP_ERROR ConfigurationManagerImpl::GetBootReasons(uint32_t & bootReasons)
{
return ReadConfigValue(kCounterKey_BootReason, bootReasons);
return ReadConfigValue(AmebaConfig::kCounterKey_BootReason, bootReasons);
}

CHIP_ERROR ConfigurationManagerImpl::StoreBootReasons(uint32_t bootReasons)
{
return WriteConfigValue(kCounterKey_BootReason, bootReasons);
return WriteConfigValue(AmebaConfig::kCounterKey_BootReason, bootReasons);
}

CHIP_ERROR ConfigurationManagerImpl::GetPrimaryWiFiMACAddress(uint8_t * buf)
Expand Down Expand Up @@ -156,7 +156,7 @@ void ConfigurationManagerImpl::InitiateFactoryReset()

CHIP_ERROR ConfigurationManagerImpl::ReadPersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t & value)
{
AmebaConfig::Key configKey{ kConfigNamespace_ChipCounters, key };
AmebaConfig::Key configKey{ AmebaConfig::kConfigNamespace_ChipCounters, key };

CHIP_ERROR err = ReadConfigValue(configKey, value);
if (err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND)
Expand All @@ -168,18 +168,78 @@ CHIP_ERROR ConfigurationManagerImpl::ReadPersistedStorageValue(::chip::Platform:

CHIP_ERROR ConfigurationManagerImpl::WritePersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t value)
{
AmebaConfig::Key configKey{ kConfigNamespace_ChipCounters, key };
AmebaConfig::Key configKey{ AmebaConfig::kConfigNamespace_ChipCounters, key };
return WriteConfigValue(configKey, value);
}

CHIP_ERROR ConfigurationManagerImpl::ReadConfigValue(Key key, bool & val)
{
return AmebaConfig::ReadConfigValue(key, val);
}

CHIP_ERROR ConfigurationManagerImpl::ReadConfigValue(Key key, uint32_t & val)
{
return AmebaConfig::ReadConfigValue(key, val);
}

CHIP_ERROR ConfigurationManagerImpl::ReadConfigValue(Key key, uint64_t & val)
{
return AmebaConfig::ReadConfigValue(key, val);
}

CHIP_ERROR ConfigurationManagerImpl::ReadConfigValueStr(Key key, char * buf, size_t bufSize, size_t & outLen)
{
return AmebaConfig::ReadConfigValueStr(key, buf, bufSize, outLen);
}

CHIP_ERROR ConfigurationManagerImpl::ReadConfigValueBin(Key key, uint8_t * buf, size_t bufSize, size_t & outLen)
{
return AmebaConfig::ReadConfigValueBin(key, buf, bufSize, outLen);
}

CHIP_ERROR ConfigurationManagerImpl::WriteConfigValue(Key key, bool val)
{
return AmebaConfig::WriteConfigValue(key, val);
}

CHIP_ERROR ConfigurationManagerImpl::WriteConfigValue(Key key, uint32_t val)
{
return AmebaConfig::WriteConfigValue(key, val);
}

CHIP_ERROR ConfigurationManagerImpl::WriteConfigValue(Key key, uint64_t val)
{
return AmebaConfig::WriteConfigValue(key, val);
}

CHIP_ERROR ConfigurationManagerImpl::WriteConfigValueStr(Key key, const char * str)
{
return AmebaConfig::WriteConfigValueStr(key, str);
}

CHIP_ERROR ConfigurationManagerImpl::WriteConfigValueStr(Key key, const char * str, size_t strLen)
{
return AmebaConfig::WriteConfigValueStr(key, str, strLen);
}

CHIP_ERROR ConfigurationManagerImpl::WriteConfigValueBin(Key key, const uint8_t * data, size_t dataLen)
{
return AmebaConfig::WriteConfigValueBin(key, data, dataLen);
}

void ConfigurationManagerImpl::RunConfigUnitTest(void)
{
AmebaConfig::RunConfigUnitTest();
}

void ConfigurationManagerImpl::DoFactoryReset(intptr_t arg)
{
CHIP_ERROR err;

ChipLogProgress(DeviceLayer, "Performing factory reset");

// Erase all values in the chip-config NVS namespace.
err = ClearNamespace(kConfigNamespace_ChipConfig);
err = AmebaConfig::ClearNamespace(AmebaConfig::kConfigNamespace_ChipConfig);
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "ClearNamespace(ChipConfig) failed: %s", chip::ErrorStr(err));
Expand Down
23 changes: 15 additions & 8 deletions src/platform/Ameba/ConfigurationManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,13 @@ namespace DeviceLayer {
/**
* Concrete implementation of the ConfigurationManager singleton object for the Ameba platform.
*/
class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImpl<ConfigurationManagerImpl>,
private Internal::AmebaConfig
class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImpl<Internal::AmebaConfig>
{
public:
// This returns an instance of this class.
static ConfigurationManagerImpl & GetDefaultInstance();

private:
// Allow the GenericConfigurationManagerImpl base class to access helper methods and types
// defined on this class.
#ifndef DOXYGEN_SHOULD_SKIP_THIS
friend class Internal::GenericConfigurationManagerImpl<ConfigurationManagerImpl>;
#endif

// ===== Members that implement the ConfigurationManager public interface.

CHIP_ERROR Init(void) override;
Expand All @@ -65,6 +58,20 @@ class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImp

// NOTE: Other public interface methods are implemented by GenericConfigurationManagerImpl<>.

// ===== Members that implement the GenericConfigurationManagerImpl protected interface.
CHIP_ERROR ReadConfigValue(Key key, bool & val) override;
CHIP_ERROR ReadConfigValue(Key key, uint32_t & val) override;
CHIP_ERROR ReadConfigValue(Key key, uint64_t & val) override;
CHIP_ERROR ReadConfigValueStr(Key key, char * buf, size_t bufSize, size_t & outLen) override;
CHIP_ERROR ReadConfigValueBin(Key key, uint8_t * buf, size_t bufSize, size_t & outLen) override;
CHIP_ERROR WriteConfigValue(Key key, bool val) override;
CHIP_ERROR WriteConfigValue(Key key, uint32_t val) override;
CHIP_ERROR WriteConfigValue(Key key, uint64_t val) override;
CHIP_ERROR WriteConfigValueStr(Key key, const char * str) override;
CHIP_ERROR WriteConfigValueStr(Key key, const char * str, size_t strLen) override;
CHIP_ERROR WriteConfigValueBin(Key key, const uint8_t * data, size_t dataLen) override;
void RunConfigUnitTest(void) override;

// ===== Private members reserved for use by this class only.

static void DoFactoryReset(intptr_t arg);
Expand Down
66 changes: 63 additions & 3 deletions src/platform/Darwin/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ CHIP_ERROR ConfigurationManagerImpl::Init()
CHIP_ERROR err;

// Initialize the generic implementation base class.
err = Internal::GenericConfigurationManagerImpl<ConfigurationManagerImpl>::Init();
err = Internal::GenericConfigurationManagerImpl<PosixConfig>::Init();
SuccessOrExit(err);

exit:
Expand Down Expand Up @@ -177,7 +177,7 @@ void ConfigurationManagerImpl::InitiateFactoryReset()

CHIP_ERROR ConfigurationManagerImpl::ReadPersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t & value)
{
PosixConfig::Key configKey{ kConfigNamespace_ChipCounters, key };
PosixConfig::Key configKey{ PosixConfig::kConfigNamespace_ChipCounters, key };

CHIP_ERROR err = ReadConfigValue(configKey, value);
if (err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND)
Expand All @@ -189,9 +189,69 @@ CHIP_ERROR ConfigurationManagerImpl::ReadPersistedStorageValue(::chip::Platform:

CHIP_ERROR ConfigurationManagerImpl::WritePersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t value)
{
PosixConfig::Key configKey{ kConfigNamespace_ChipCounters, key };
PosixConfig::Key configKey{ PosixConfig::kConfigNamespace_ChipCounters, key };
return WriteConfigValue(configKey, value);
}

CHIP_ERROR ConfigurationManagerImpl::ReadConfigValue(Key key, bool & val)
{
return PosixConfig::ReadConfigValue(key, val);
}

CHIP_ERROR ConfigurationManagerImpl::ReadConfigValue(Key key, uint32_t & val)
{
return PosixConfig::ReadConfigValue(key, val);
}

CHIP_ERROR ConfigurationManagerImpl::ReadConfigValue(Key key, uint64_t & val)
{
return PosixConfig::ReadConfigValue(key, val);
}

CHIP_ERROR ConfigurationManagerImpl::ReadConfigValueStr(Key key, char * buf, size_t bufSize, size_t & outLen)
{
return PosixConfig::ReadConfigValueStr(key, buf, bufSize, outLen);
}

CHIP_ERROR ConfigurationManagerImpl::ReadConfigValueBin(Key key, uint8_t * buf, size_t bufSize, size_t & outLen)
{
return PosixConfig::ReadConfigValueBin(key, buf, bufSize, outLen);
}

CHIP_ERROR ConfigurationManagerImpl::WriteConfigValue(Key key, bool val)
{
return PosixConfig::WriteConfigValue(key, val);
}

CHIP_ERROR ConfigurationManagerImpl::WriteConfigValue(Key key, uint32_t val)
{
return PosixConfig::WriteConfigValue(key, val);
}

CHIP_ERROR ConfigurationManagerImpl::WriteConfigValue(Key key, uint64_t val)
{
return PosixConfig::WriteConfigValue(key, val);
}

CHIP_ERROR ConfigurationManagerImpl::WriteConfigValueStr(Key key, const char * str)
{
return PosixConfig::WriteConfigValueStr(key, str);
}

CHIP_ERROR ConfigurationManagerImpl::WriteConfigValueStr(Key key, const char * str, size_t strLen)
{
return PosixConfig::WriteConfigValueStr(key, str, strLen);
}

CHIP_ERROR ConfigurationManagerImpl::WriteConfigValueBin(Key key, const uint8_t * data, size_t dataLen)
{
return PosixConfig::WriteConfigValueBin(key, data, dataLen);
}

void ConfigurationManagerImpl::RunConfigUnitTest(void)
{
PosixConfig::RunConfigUnitTest();
}

} // namespace DeviceLayer
} // namespace chip
Loading

0 comments on commit 2143ea5

Please sign in to comment.