From 178308834af09786cf13c686cc4bd82fd0ed7919 Mon Sep 17 00:00:00 2001 From: Andy Salisbury Date: Wed, 10 Nov 2021 09:24:04 -0500 Subject: [PATCH] Set configuration manager (#11534) * Add the setter for the ConfigurationManager. Also remove the implementation getter. * Remove uses of ConfigurationMgrImpl. Promote the reboot count, boot reason, and operational hours methods to the ConfigurationManager interface. * Add a static instance getter to the ConfigurationManagerImpl. * Set the default ConfigurationManager instance in the platform manager. * Update the tests to set a ConfigurationManager instance. * Allow subclasses of the platform ConfigurationManagerImpls. * Re-fix Ameba's ConfigurationManagerImpl. * Restyled by clang-format * Improvements from pull request comments. * Add VerifyOrDie to the ConfigurationMgr getter. * Pass a pointer rather than a reference to ConfigurationMgr. * Explicitly default the instance pointer to nullptr. * Move the singleton implementation out of each platform into a shared file. * Update Ameba platform to remove the new uses of ConfigurationMgrImpl. Co-authored-by: Restyled.io --- src/controller/tests/TestDevice.cpp | 2 + src/include/platform/ConfigurationManager.h | 19 ++++--- .../GenericConfigurationManagerImpl.cpp | 36 +++++++++++++ .../GenericConfigurationManagerImpl.h | 6 +++ src/platform/Ameba/BUILD.gn | 1 + .../Ameba/ConfigurationManagerImpl.cpp | 8 +-- src/platform/Ameba/ConfigurationManagerImpl.h | 51 ++++--------------- src/platform/Ameba/PlatformManagerImpl.cpp | 12 +++-- src/platform/Darwin/BUILD.gn | 1 + .../Darwin/ConfigurationManagerImpl.cpp | 8 +-- .../Darwin/ConfigurationManagerImpl.h | 37 +++----------- src/platform/Darwin/PlatformManagerImpl.cpp | 1 + src/platform/EFR32/BUILD.gn | 1 + .../EFR32/ConfigurationManagerImpl.cpp | 8 +-- src/platform/EFR32/ConfigurationManagerImpl.h | 37 +++----------- src/platform/EFR32/PlatformManagerImpl.cpp | 1 + src/platform/ESP32/BUILD.gn | 1 + .../ESP32/ConfigurationManagerImpl.cpp | 8 +-- src/platform/ESP32/ConfigurationManagerImpl.h | 46 ++++------------- src/platform/ESP32/PlatformManagerImpl.cpp | 10 ++-- src/platform/Linux/BUILD.gn | 1 + .../Linux/ConfigurationManagerImpl.cpp | 8 +-- src/platform/Linux/ConfigurationManagerImpl.h | 46 ++++------------- src/platform/Linux/PlatformManagerImpl.cpp | 23 +++++---- src/platform/P6/BUILD.gn | 1 + src/platform/P6/ConfigurationManagerImpl.cpp | 8 +-- src/platform/P6/ConfigurationManagerImpl.h | 37 +++----------- src/platform/P6/PlatformManagerImpl.cpp | 2 + ....cpp => SingletonConfigurationManager.cpp} | 32 ++++++++++-- src/platform/Tizen/BUILD.gn | 1 + .../Tizen/ConfigurationManagerImpl.cpp | 8 +-- src/platform/Tizen/ConfigurationManagerImpl.h | 37 +++----------- src/platform/Tizen/PlatformManagerImpl.cpp | 1 + .../Zephyr/ConfigurationManagerImpl.cpp | 8 +-- .../Zephyr/ConfigurationManagerImpl.h | 37 +++----------- src/platform/Zephyr/PlatformManagerImpl.cpp | 1 + .../android/AndroidChipPlatform-JNI.cpp | 7 ++- src/platform/android/BUILD.gn | 1 + .../android/ConfigurationManagerImpl.cpp | 8 +-- .../android/ConfigurationManagerImpl.h | 34 ++----------- src/platform/android/PlatformManagerImpl.cpp | 1 + src/platform/cc13x2_26x2/BUILD.gn | 1 + .../cc13x2_26x2/ConfigurationManagerImpl.cpp | 8 +-- .../cc13x2_26x2/ConfigurationManagerImpl.h | 37 +++----------- .../cc13x2_26x2/PlatformManagerImpl.cpp | 1 + src/platform/fake/BUILD.gn | 1 - src/platform/fake/ConfigurationManagerImpl.h | 37 +++++--------- src/platform/mbed/BUILD.gn | 1 + .../mbed/ConfigurationManagerImpl.cpp | 8 +-- src/platform/mbed/ConfigurationManagerImpl.h | 37 +++----------- src/platform/mbed/PlatformManagerImpl.cpp | 2 + src/platform/nrfconnect/BUILD.gn | 1 + src/platform/nxp/k32w/k32w0/BUILD.gn | 1 + .../k32w/k32w0/ConfigurationManagerImpl.cpp | 8 +-- .../nxp/k32w/k32w0/ConfigurationManagerImpl.h | 33 ++---------- .../nxp/k32w/k32w0/PlatformManagerImpl.cpp | 1 + src/platform/qpg/BUILD.gn | 1 + src/platform/qpg/ConfigurationManagerImpl.cpp | 8 +-- src/platform/qpg/ConfigurationManagerImpl.h | 37 +++----------- src/platform/qpg/PlatformManagerImpl.cpp | 1 + src/platform/telink/BUILD.gn | 1 + .../raw/tests/NetworkTestHelpers.cpp | 2 + 62 files changed, 304 insertions(+), 519 deletions(-) rename src/platform/{fake/ConfigurationManagerImpl.cpp => SingletonConfigurationManager.cpp} (55%) diff --git a/src/controller/tests/TestDevice.cpp b/src/controller/tests/TestDevice.cpp index 5e6bd910246b3d..fe4cd909a0efe6 100644 --- a/src/controller/tests/TestDevice.cpp +++ b/src/controller/tests/TestDevice.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -44,6 +45,7 @@ using TestTransportMgr = TransportMgr; void TestDevice_EstablishSessionDirectly(nlTestSuite * inSuite, void * inContext) { Platform::MemoryInit(); + chip::DeviceLayer::SetConfigurationMgr(&chip::DeviceLayer::ConfigurationManagerImpl::GetDefaultInstance()); DeviceTransportMgr transportMgr; SessionManager sessionManager; ExchangeManager exchangeMgr; diff --git a/src/include/platform/ConfigurationManager.h b/src/include/platform/ConfigurationManager.h index 579250666a6254..18ffe60efb7beb 100644 --- a/src/include/platform/ConfigurationManager.h +++ b/src/include/platform/ConfigurationManager.h @@ -100,6 +100,12 @@ class ConfigurationManager virtual CHIP_ERROR StoreRegulatoryLocation(uint32_t location) = 0; virtual CHIP_ERROR StoreCountryCode(const char * code, size_t codeLen) = 0; virtual CHIP_ERROR StoreBreadcrumb(uint64_t breadcrumb) = 0; + virtual CHIP_ERROR GetRebootCount(uint32_t & rebootCount) = 0; + virtual CHIP_ERROR StoreRebootCount(uint32_t rebootCount) = 0; + virtual CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours) = 0; + virtual CHIP_ERROR StoreTotalOperationalHours(uint32_t totalOperationalHours) = 0; + virtual CHIP_ERROR GetBootReasons(uint32_t & bootReasons) = 0; + virtual CHIP_ERROR StoreBootReasons(uint32_t bootReasons) = 0; virtual CHIP_ERROR GetBLEDeviceIdentificationInfo(Ble::ChipBLEDeviceIdentificationInfo & deviceIdInfo) = 0; @@ -151,20 +157,19 @@ class ConfigurationManager }; /** - * Returns a reference to the public interface of the ConfigurationManager singleton object. + * Returns a reference to a ConfigurationManager object. * - * chip application should use this to access features of the ConfigurationManager object - * that are common to all platforms. + * Applications should use this to access the features of the ConfigurationManager. */ extern ConfigurationManager & ConfigurationMgr(); /** - * Returns the platform-specific implementation of the ConfigurationManager singleton object. + * Sets a reference to a ConfigurationManager object. * - * chip applications can use this to gain access to features of the ConfigurationManager - * that are specific to the selected platform. + * This must be called before any calls to ConfigurationMgr. If a nullptr is passed in, + * no changes will be made. */ -extern ConfigurationManagerImpl & ConfigurationMgrImpl(); +extern void SetConfigurationMgr(ConfigurationManager * configurationManager); } // namespace DeviceLayer } // namespace chip diff --git a/src/include/platform/internal/GenericConfigurationManagerImpl.cpp b/src/include/platform/internal/GenericConfigurationManagerImpl.cpp index ae9068179d7e11..ef8f91f08742eb 100644 --- a/src/include/platform/internal/GenericConfigurationManagerImpl.cpp +++ b/src/include/platform/internal/GenericConfigurationManagerImpl.cpp @@ -343,6 +343,42 @@ CHIP_ERROR GenericConfigurationManagerImpl::StoreBreadcrumb(uint64_t return Impl()->WriteConfigValue(ImplClass::kConfigKey_Breadcrumb, breadcrumb); } +template +CHIP_ERROR GenericConfigurationManagerImpl::GetRebootCount(uint32_t & rebootCount) +{ + return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; +} + +template +CHIP_ERROR GenericConfigurationManagerImpl::StoreRebootCount(uint32_t rebootCount) +{ + return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; +} + +template +CHIP_ERROR GenericConfigurationManagerImpl::GetTotalOperationalHours(uint32_t & totalOperationalHours) +{ + return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; +} + +template +CHIP_ERROR GenericConfigurationManagerImpl::StoreTotalOperationalHours(uint32_t totalOperationalHours) +{ + return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; +} + +template +CHIP_ERROR GenericConfigurationManagerImpl::GetBootReasons(uint32_t & bootReasons) +{ + return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; +} + +template +CHIP_ERROR GenericConfigurationManagerImpl::StoreBootReasons(uint32_t bootReasons) +{ + return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; +} + #if CHIP_ENABLE_ROTATING_DEVICE_ID template CHIP_ERROR GenericConfigurationManagerImpl::GetLifetimeCounter(uint16_t & lifetimeCounter) diff --git a/src/include/platform/internal/GenericConfigurationManagerImpl.h b/src/include/platform/internal/GenericConfigurationManagerImpl.h index 380b9540d0f86f..42a64c7781a32f 100644 --- a/src/include/platform/internal/GenericConfigurationManagerImpl.h +++ b/src/include/platform/internal/GenericConfigurationManagerImpl.h @@ -97,6 +97,12 @@ class GenericConfigurationManagerImpl : public ConfigurationManager CHIP_ERROR StoreCountryCode(const char * code, size_t codeLen) override; CHIP_ERROR GetBreadcrumb(uint64_t & breadcrumb) override; CHIP_ERROR StoreBreadcrumb(uint64_t breadcrumb) override; + CHIP_ERROR GetRebootCount(uint32_t & rebootCount) override; + CHIP_ERROR StoreRebootCount(uint32_t rebootCount) override; + CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours) override; + CHIP_ERROR StoreTotalOperationalHours(uint32_t totalOperationalHours) override; + CHIP_ERROR GetBootReasons(uint32_t & bootReasons) override; + CHIP_ERROR StoreBootReasons(uint32_t bootReasons) override; #if !defined(NDEBUG) CHIP_ERROR RunUnitTests(void) override; #endif diff --git a/src/platform/Ameba/BUILD.gn b/src/platform/Ameba/BUILD.gn index 15849c83b426f0..2626ec984d0b0b 100755 --- a/src/platform/Ameba/BUILD.gn +++ b/src/platform/Ameba/BUILD.gn @@ -21,6 +21,7 @@ assert(chip_device_platform == "ameba") static_library("Ameba") { sources = [ "../FreeRTOS/SystemTimeSupport.cpp", + "../SingletonConfigurationManager.cpp", "AmebaConfig.cpp", "AmebaConfig.h", "BLEManagerImpl.cpp", diff --git a/src/platform/Ameba/ConfigurationManagerImpl.cpp b/src/platform/Ameba/ConfigurationManagerImpl.cpp index 6109380ba61ae3..9e5525322536b9 100644 --- a/src/platform/Ameba/ConfigurationManagerImpl.cpp +++ b/src/platform/Ameba/ConfigurationManagerImpl.cpp @@ -36,9 +36,11 @@ namespace DeviceLayer { using namespace ::chip::DeviceLayer::Internal; -/** Singleton instance of the ConfigurationManager implementation object. - */ -ConfigurationManagerImpl ConfigurationManagerImpl::sInstance; +ConfigurationManagerImpl & ConfigurationManagerImpl::GetDefaultInstance() +{ + static ConfigurationManagerImpl sInstance; + return sInstance; +} CHIP_ERROR ConfigurationManagerImpl::Init() { diff --git a/src/platform/Ameba/ConfigurationManagerImpl.h b/src/platform/Ameba/ConfigurationManagerImpl.h index 22323887c43d05..c51ea2eb30175d 100644 --- a/src/platform/Ameba/ConfigurationManagerImpl.h +++ b/src/platform/Ameba/ConfigurationManagerImpl.h @@ -34,22 +34,14 @@ namespace DeviceLayer { /** * Concrete implementation of the ConfigurationManager singleton object for the Ameba platform. */ -class ConfigurationManagerImpl final : public Internal::GenericConfigurationManagerImpl, - private Internal::AmebaConfig +class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImpl, + private Internal::AmebaConfig { public: - CHIP_ERROR GetRebootCount(uint32_t & rebootCount); - CHIP_ERROR StoreRebootCount(uint32_t rebootCount); - CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours); - CHIP_ERROR StoreTotalOperationalHours(uint32_t totalOperationalHours); - CHIP_ERROR GetBootReasons(uint32_t & bootReasons); - CHIP_ERROR StoreBootReasons(uint32_t bootReasons); + // This returns an instance of this class. + static ConfigurationManagerImpl & GetDefaultInstance(); private: - // Allow the ConfigurationManager interface class to delegate method calls to - // the implementation methods provided by this class. - friend class ConfigurationManager; - // Allow the GenericConfigurationManagerImpl base class to access helper methods and types // defined on this class. #ifndef DOXYGEN_SHOULD_SKIP_THIS @@ -64,42 +56,19 @@ class ConfigurationManagerImpl final : public Internal::GenericConfigurationMana 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 GetRebootCount(uint32_t & rebootCount) override; + CHIP_ERROR StoreRebootCount(uint32_t rebootCount) override; + CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours) override; + CHIP_ERROR StoreTotalOperationalHours(uint32_t totalOperationalHours) override; + CHIP_ERROR GetBootReasons(uint32_t & bootReasons) override; + CHIP_ERROR StoreBootReasons(uint32_t bootReasons) override; // NOTE: Other public interface methods are implemented by GenericConfigurationManagerImpl<>. - // ===== Members for internal use by the following friends. - - friend ConfigurationManager & ConfigurationMgr(void); - friend ConfigurationManagerImpl & ConfigurationMgrImpl(void); - - static ConfigurationManagerImpl sInstance; - // ===== Private members reserved for use by this class only. static void DoFactoryReset(intptr_t arg); }; -/** - * Returns the public interface of the ConfigurationManager singleton object. - * - * Chip applications should use this to access features of the ConfigurationManager object - * that are common to all platforms. - */ -inline ConfigurationManager & ConfigurationMgr(void) -{ - return ConfigurationManagerImpl::sInstance; -} - -/** - * Returns the platform-specific implementation of the ConfigurationManager singleton object. - * - * Chip applications can use this to gain access to features of the ConfigurationManager - * that are specific to the Ameba platform. - */ -inline ConfigurationManagerImpl & ConfigurationMgrImpl(void) -{ - return ConfigurationManagerImpl::sInstance; -} - } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/Ameba/PlatformManagerImpl.cpp b/src/platform/Ameba/PlatformManagerImpl.cpp index 0eaf536fdee771..000824271da1d2 100644 --- a/src/platform/Ameba/PlatformManagerImpl.cpp +++ b/src/platform/Ameba/PlatformManagerImpl.cpp @@ -57,6 +57,8 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) CHIP_ERROR err; + SetConfigurationMgr(&ConfigurationManagerImpl::GetDefaultInstance()); + // Make sure the LwIP core lock has been initialized err = Internal::InitLwIPCoreLock(); @@ -86,9 +88,9 @@ CHIP_ERROR PlatformManagerImpl::_Shutdown() { uint32_t totalOperationalHours = 0; - if (ConfigurationMgrImpl().GetTotalOperationalHours(totalOperationalHours) == CHIP_NO_ERROR) + if (ConfigurationMgr().GetTotalOperationalHours(totalOperationalHours) == CHIP_NO_ERROR) { - ConfigurationMgrImpl().StoreTotalOperationalHours(totalOperationalHours + static_cast(upTime / 3600)); + ConfigurationMgr().StoreTotalOperationalHours(totalOperationalHours + static_cast(upTime / 3600)); } else { @@ -124,7 +126,7 @@ CHIP_ERROR PlatformManagerImpl::_GetRebootCount(uint16_t & rebootCount) { uint32_t count = 0; - CHIP_ERROR err = ConfigurationMgrImpl().GetRebootCount(count); + CHIP_ERROR err = ConfigurationMgr().GetRebootCount(count); if (err == CHIP_NO_ERROR) { @@ -155,7 +157,7 @@ CHIP_ERROR PlatformManagerImpl::_GetTotalOperationalHours(uint32_t & totalOperat if (_GetUpTime(upTime) == CHIP_NO_ERROR) { uint32_t totalHours = 0; - if (ConfigurationMgrImpl().GetTotalOperationalHours(totalHours) == CHIP_NO_ERROR) + if (ConfigurationMgr().GetTotalOperationalHours(totalHours) == CHIP_NO_ERROR) { VerifyOrReturnError(upTime / 3600 <= UINT32_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE); totalOperationalHours = totalHours + static_cast(upTime / 3600); @@ -170,7 +172,7 @@ CHIP_ERROR PlatformManagerImpl::_GetBootReasons(uint8_t & bootReasons) { uint32_t reason = 0; - CHIP_ERROR err = ConfigurationMgrImpl().GetBootReasons(reason); + CHIP_ERROR err = ConfigurationMgr().GetBootReasons(reason); if (err == CHIP_NO_ERROR) { diff --git a/src/platform/Darwin/BUILD.gn b/src/platform/Darwin/BUILD.gn index d54717826a997e..4e9f4f3220a3ad 100644 --- a/src/platform/Darwin/BUILD.gn +++ b/src/platform/Darwin/BUILD.gn @@ -34,6 +34,7 @@ static_library("Darwin") { sources = [ "../DeviceSafeQueue.cpp", "../DeviceSafeQueue.h", + "../SingletonConfigurationManager.cpp", "BLEManagerImpl.cpp", "BLEManagerImpl.h", "BlePlatformConfig.h", diff --git a/src/platform/Darwin/ConfigurationManagerImpl.cpp b/src/platform/Darwin/ConfigurationManagerImpl.cpp index 5bcf9057759515..1b904ffdb1b304 100644 --- a/src/platform/Darwin/ConfigurationManagerImpl.cpp +++ b/src/platform/Darwin/ConfigurationManagerImpl.cpp @@ -128,9 +128,11 @@ CHIP_ERROR GetMACAddressFromInterfaces(io_iterator_t primaryInterfaceIterator, u } #endif // TARGET_OS_OSX -/** Singleton instance of the ConfigurationManager implementation object. - */ -ConfigurationManagerImpl ConfigurationManagerImpl::sInstance; +ConfigurationManagerImpl & ConfigurationManagerImpl::GetDefaultInstance() +{ + static ConfigurationManagerImpl sInstance; + return sInstance; +} CHIP_ERROR ConfigurationManagerImpl::Init() { diff --git a/src/platform/Darwin/ConfigurationManagerImpl.h b/src/platform/Darwin/ConfigurationManagerImpl.h index c2732af625b423..193155cbc3cf9f 100644 --- a/src/platform/Darwin/ConfigurationManagerImpl.h +++ b/src/platform/Darwin/ConfigurationManagerImpl.h @@ -33,8 +33,8 @@ namespace DeviceLayer { /** * Concrete implementation of the ConfigurationManager singleton object for the Darwin platform. */ -class ConfigurationManagerImpl final : public Internal::GenericConfigurationManagerImpl, - private Internal::PosixConfig +class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImpl, + private Internal::PosixConfig { // Allow the GenericConfigurationManagerImpl base class to access helper methods and types // defined on this class. @@ -42,6 +42,10 @@ class ConfigurationManagerImpl final : public Internal::GenericConfigurationMana friend class Internal::GenericConfigurationManagerImpl; #endif +public: + // This returns an instance of this class. + static ConfigurationManagerImpl & GetDefaultInstance(); + private: // ===== Members that implement the ConfigurationManager public interface. @@ -53,36 +57,7 @@ class ConfigurationManagerImpl final : public Internal::GenericConfigurationMana CHIP_ERROR WritePersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t value) override; // NOTE: Other public interface methods are implemented by GenericConfigurationManagerImpl<>. - - // ===== Members for internal use by the following friends. - - friend ConfigurationManager & ConfigurationMgr(void); - friend ConfigurationManagerImpl & ConfigurationMgrImpl(void); - - static ConfigurationManagerImpl sInstance; }; -/** - * Returns the public interface of the ConfigurationManager singleton object. - * - * chip applications should use this to access features of the ConfigurationManager object - * that are common to all platforms. - */ -inline ConfigurationManager & ConfigurationMgr(void) -{ - return ConfigurationManagerImpl::sInstance; -} - -/** - * Returns the platform-specific implementation of the ConfigurationManager singleton object. - * - * chip applications can use this to gain access to features of the ConfigurationManager - * that are specific to the ESP32 platform. - */ -inline ConfigurationManagerImpl & ConfigurationMgrImpl(void) -{ - return ConfigurationManagerImpl::sInstance; -} - } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/Darwin/PlatformManagerImpl.cpp b/src/platform/Darwin/PlatformManagerImpl.cpp index 348ef48a21221a..ea11e5afc95b35 100644 --- a/src/platform/Darwin/PlatformManagerImpl.cpp +++ b/src/platform/Darwin/PlatformManagerImpl.cpp @@ -43,6 +43,7 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack() // Initialize the configuration system. err = Internal::PosixConfig::Init(); SuccessOrExit(err); + SetConfigurationMgr(&ConfigurationManagerImpl::GetDefaultInstance()); mRunLoopSem = dispatch_semaphore_create(0); diff --git a/src/platform/EFR32/BUILD.gn b/src/platform/EFR32/BUILD.gn index f18643f914f647..26652ffd3ee60e 100644 --- a/src/platform/EFR32/BUILD.gn +++ b/src/platform/EFR32/BUILD.gn @@ -26,6 +26,7 @@ if (chip_enable_openthread) { static_library("EFR32") { sources = [ "../FreeRTOS/SystemTimeSupport.cpp", + "../SingletonConfigurationManager.cpp", "BLEManagerImpl.cpp", "BLEManagerImpl.h", "BlePlatformConfig.h", diff --git a/src/platform/EFR32/ConfigurationManagerImpl.cpp b/src/platform/EFR32/ConfigurationManagerImpl.cpp index 506f593ef06b14..c8564cd9f1b4f5 100644 --- a/src/platform/EFR32/ConfigurationManagerImpl.cpp +++ b/src/platform/EFR32/ConfigurationManagerImpl.cpp @@ -34,9 +34,11 @@ namespace DeviceLayer { using namespace ::chip::DeviceLayer::Internal; -/** Singleton instance of the ConfigurationManager implementation object. - */ -ConfigurationManagerImpl ConfigurationManagerImpl::sInstance; +ConfigurationManagerImpl & ConfigurationManagerImpl::GetDefaultInstance() +{ + static ConfigurationManagerImpl sInstance; + return sInstance; +} CHIP_ERROR ConfigurationManagerImpl::Init() { diff --git a/src/platform/EFR32/ConfigurationManagerImpl.h b/src/platform/EFR32/ConfigurationManagerImpl.h index 3ee8754f8fbcb8..5e88832e761076 100644 --- a/src/platform/EFR32/ConfigurationManagerImpl.h +++ b/src/platform/EFR32/ConfigurationManagerImpl.h @@ -34,8 +34,8 @@ namespace DeviceLayer { /** * Concrete implementation of the ConfigurationManager singleton object for the EFR32 platform. */ -class ConfigurationManagerImpl final : public Internal::GenericConfigurationManagerImpl, - private Internal::EFR32Config +class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImpl, + private Internal::EFR32Config { // Allow the GenericConfigurationManagerImpl base class to access helper methods and types // defined on this class. @@ -43,6 +43,10 @@ class ConfigurationManagerImpl final : public Internal::GenericConfigurationMana friend class Internal::GenericConfigurationManagerImpl; #endif +public: + // This returns an instance of this class. + static ConfigurationManagerImpl & GetDefaultInstance(); + private: // ===== Members that implement the ConfigurationManager public interface. @@ -55,40 +59,11 @@ class ConfigurationManagerImpl final : public Internal::GenericConfigurationMana // NOTE: Other public interface methods are implemented by GenericConfigurationManagerImpl<>. - // ===== Members for internal use by the following friends. - - friend ConfigurationManager & ConfigurationMgr(void); - friend ConfigurationManagerImpl & ConfigurationMgrImpl(void); - - static ConfigurationManagerImpl sInstance; - // ===== Private members reserved for use by this class only. static void DoFactoryReset(intptr_t arg); }; -/** - * Returns the public interface of the ConfigurationManager singleton object. - * - * Chip applications should use this to access features of the ConfigurationManager object - * that are common to all platforms. - */ -inline ConfigurationManager & ConfigurationMgr(void) -{ - return ConfigurationManagerImpl::sInstance; -} - -/** - * Returns the platform-specific implementation of the ConfigurationManager singleton object. - * - * Chip applications can use this to gain access to features of the ConfigurationManager - * that are specific to the ESP32 platform. - */ -inline ConfigurationManagerImpl & ConfigurationMgrImpl(void) -{ - return ConfigurationManagerImpl::sInstance; -} - inline CHIP_ERROR ConfigurationManagerImpl::GetPrimaryWiFiMACAddress(uint8_t * buf) { return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; diff --git a/src/platform/EFR32/PlatformManagerImpl.cpp b/src/platform/EFR32/PlatformManagerImpl.cpp index 61fe7b2954489d..e1c3af86181520 100644 --- a/src/platform/EFR32/PlatformManagerImpl.cpp +++ b/src/platform/EFR32/PlatformManagerImpl.cpp @@ -44,6 +44,7 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) // Initialize the configuration system. err = Internal::EFR32Config::Init(); SuccessOrExit(err); + SetConfigurationMgr(&ConfigurationManagerImpl::GetDefaultInstance()); // Initialize LwIP. tcpip_init(NULL, NULL); diff --git a/src/platform/ESP32/BUILD.gn b/src/platform/ESP32/BUILD.gn index 175b83e29267ed..4c481560c2d84f 100644 --- a/src/platform/ESP32/BUILD.gn +++ b/src/platform/ESP32/BUILD.gn @@ -21,6 +21,7 @@ assert(chip_device_platform == "esp32") static_library("ESP32") { sources = [ "../FreeRTOS/SystemTimeSupport.cpp", + "../SingletonConfigurationManager.cpp", "BLEManagerImpl.h", "CHIPDevicePlatformConfig.h", "CHIPDevicePlatformEvent.h", diff --git a/src/platform/ESP32/ConfigurationManagerImpl.cpp b/src/platform/ESP32/ConfigurationManagerImpl.cpp index 649f65c6c796fd..0254d8504d7990 100644 --- a/src/platform/ESP32/ConfigurationManagerImpl.cpp +++ b/src/platform/ESP32/ConfigurationManagerImpl.cpp @@ -50,9 +50,11 @@ enum // TODO: Define a Singleton instance of CHIP Group Key Store here (#1266) -/** Singleton instance of the ConfigurationManager implementation object for the ESP32. - */ -ConfigurationManagerImpl ConfigurationManagerImpl::sInstance; +ConfigurationManagerImpl & ConfigurationManagerImpl::GetDefaultInstance() +{ + static ConfigurationManagerImpl sInstance; + return sInstance; +} CHIP_ERROR ConfigurationManagerImpl::Init() { diff --git a/src/platform/ESP32/ConfigurationManagerImpl.h b/src/platform/ESP32/ConfigurationManagerImpl.h index 2c143b90976bd3..445783f05e15ac 100644 --- a/src/platform/ESP32/ConfigurationManagerImpl.h +++ b/src/platform/ESP32/ConfigurationManagerImpl.h @@ -41,19 +41,20 @@ namespace DeviceLayer { /** * Concrete implementation of the ConfigurationManager singleton object for the ESP32 platform. */ -class ConfigurationManagerImpl final : public Internal::GenericConfigurationManagerImpl, +class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImpl, #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE - public Internal::GenericConnectivityManagerImpl_BLE, + public Internal::GenericConnectivityManagerImpl_BLE, #else - public Internal::GenericConnectivityManagerImpl_NoBLE, + public Internal::GenericConnectivityManagerImpl_NoBLE, #endif - private Internal::ESP32Config + private Internal::ESP32Config { public: - CHIP_ERROR GetRebootCount(uint32_t & rebootCount); - CHIP_ERROR StoreRebootCount(uint32_t rebootCount); - CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours); - CHIP_ERROR StoreTotalOperationalHours(uint32_t totalOperationalHours); + CHIP_ERROR GetRebootCount(uint32_t & rebootCount) override; + CHIP_ERROR StoreRebootCount(uint32_t rebootCount) override; + CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours) override; + CHIP_ERROR StoreTotalOperationalHours(uint32_t totalOperationalHours) override; + static ConfigurationManagerImpl & GetDefaultInstance(); private: // Allow the GenericConfigurationManagerImpl base class to access helper methods and types @@ -74,39 +75,10 @@ class ConfigurationManagerImpl final : public Internal::GenericConfigurationMana // NOTE: Other public interface methods are implemented by GenericConfigurationManagerImpl<>. - // ===== Members for internal use by the following friends. - - friend ConfigurationManager & ConfigurationMgr(void); - friend ConfigurationManagerImpl & ConfigurationMgrImpl(void); - - static ConfigurationManagerImpl sInstance; - // ===== Private members reserved for use by this class only. static void DoFactoryReset(intptr_t arg); }; -/** - * Returns the public interface of the ConfigurationManager singleton object. - * - * Chip applications should use this to access features of the ConfigurationManager object - * that are common to all platforms. - */ -inline ConfigurationManager & ConfigurationMgr(void) -{ - return ConfigurationManagerImpl::sInstance; -} - -/** - * Returns the platform-specific implementation of the ConfigurationManager singleton object. - * - * Chip applications can use this to gain access to features of the ConfigurationManager - * that are specific to the ESP32 platform. - */ -inline ConfigurationManagerImpl & ConfigurationMgrImpl(void) -{ - return ConfigurationManagerImpl::sInstance; -} - } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/ESP32/PlatformManagerImpl.cpp b/src/platform/ESP32/PlatformManagerImpl.cpp index 9236f51ee98d4a..0d290acef5aecd 100644 --- a/src/platform/ESP32/PlatformManagerImpl.cpp +++ b/src/platform/ESP32/PlatformManagerImpl.cpp @@ -57,6 +57,8 @@ static int app_entropy_source(void * data, unsigned char * output, size_t len, s CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) { + SetConfigurationMgr(&ConfigurationManagerImpl::GetDefaultInstance()); + esp_err_t err; // Arrange for CHIP-encapsulated ESP32 errors to be translated to text Internal::ESP32Utils::RegisterESP32ErrorFormatter(); @@ -131,9 +133,9 @@ CHIP_ERROR PlatformManagerImpl::_Shutdown() { uint32_t totalOperationalHours = 0; - if (ConfigurationMgrImpl().GetTotalOperationalHours(totalOperationalHours) == CHIP_NO_ERROR) + if (ConfigurationMgr().GetTotalOperationalHours(totalOperationalHours) == CHIP_NO_ERROR) { - ConfigurationMgrImpl().StoreTotalOperationalHours(totalOperationalHours + static_cast(upTime / 3600)); + ConfigurationMgr().StoreTotalOperationalHours(totalOperationalHours + static_cast(upTime / 3600)); } else { @@ -170,7 +172,7 @@ CHIP_ERROR PlatformManagerImpl::_GetRebootCount(uint16_t & rebootCount) { uint32_t count = 0; - CHIP_ERROR err = ConfigurationMgrImpl().GetRebootCount(count); + CHIP_ERROR err = ConfigurationMgr().GetRebootCount(count); if (err == CHIP_NO_ERROR) { @@ -201,7 +203,7 @@ CHIP_ERROR PlatformManagerImpl::_GetTotalOperationalHours(uint32_t & totalOperat if (_GetUpTime(upTime) == CHIP_NO_ERROR) { uint32_t totalHours = 0; - if (ConfigurationMgrImpl().GetTotalOperationalHours(totalHours) == CHIP_NO_ERROR) + if (ConfigurationMgr().GetTotalOperationalHours(totalHours) == CHIP_NO_ERROR) { VerifyOrReturnError(upTime / 3600 <= UINT32_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE); totalOperationalHours = totalHours + static_cast(upTime / 3600); diff --git a/src/platform/Linux/BUILD.gn b/src/platform/Linux/BUILD.gn index 5c7516bc56119c..2c39c68668afcf 100644 --- a/src/platform/Linux/BUILD.gn +++ b/src/platform/Linux/BUILD.gn @@ -36,6 +36,7 @@ static_library("Linux") { sources = [ "../DeviceSafeQueue.cpp", "../DeviceSafeQueue.h", + "../SingletonConfigurationManager.cpp", "BLEManagerImpl.cpp", "BLEManagerImpl.h", "BlePlatformConfig.h", diff --git a/src/platform/Linux/ConfigurationManagerImpl.cpp b/src/platform/Linux/ConfigurationManagerImpl.cpp index fde80ab6dfb5fe..1e542084216b93 100644 --- a/src/platform/Linux/ConfigurationManagerImpl.cpp +++ b/src/platform/Linux/ConfigurationManagerImpl.cpp @@ -40,9 +40,11 @@ namespace DeviceLayer { using namespace ::chip::DeviceLayer::Internal; -/** Singleton instance of the ConfigurationManager implementation object. - */ -ConfigurationManagerImpl ConfigurationManagerImpl::sInstance; +ConfigurationManagerImpl & ConfigurationManagerImpl::GetDefaultInstance() +{ + static ConfigurationManagerImpl sInstance; + return sInstance; +} CHIP_ERROR ConfigurationManagerImpl::Init() { diff --git a/src/platform/Linux/ConfigurationManagerImpl.h b/src/platform/Linux/ConfigurationManagerImpl.h index e0fc2188bd7c3f..2ce06c5b213655 100644 --- a/src/platform/Linux/ConfigurationManagerImpl.h +++ b/src/platform/Linux/ConfigurationManagerImpl.h @@ -34,16 +34,17 @@ namespace DeviceLayer { /** * Concrete implementation of the ConfigurationManager singleton object for the Linux platform. */ -class ConfigurationManagerImpl final : public Internal::GenericConfigurationManagerImpl, - private Internal::PosixConfig +class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImpl, + private Internal::PosixConfig { public: - CHIP_ERROR GetRebootCount(uint32_t & rebootCount); - CHIP_ERROR StoreRebootCount(uint32_t rebootCount); - CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours); - CHIP_ERROR StoreTotalOperationalHours(uint32_t totalOperationalHours); - CHIP_ERROR GetBootReasons(uint32_t & bootReasons); - CHIP_ERROR StoreBootReasons(uint32_t bootReasons); + CHIP_ERROR GetRebootCount(uint32_t & rebootCount) override; + CHIP_ERROR StoreRebootCount(uint32_t rebootCount) override; + CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours) override; + CHIP_ERROR StoreTotalOperationalHours(uint32_t totalOperationalHours) override; + CHIP_ERROR GetBootReasons(uint32_t & bootReasons) override; + CHIP_ERROR StoreBootReasons(uint32_t bootReasons) override; + static ConfigurationManagerImpl & GetDefaultInstance(); private: // Allow the GenericConfigurationManagerImpl base class to access helper methods and types @@ -68,39 +69,10 @@ class ConfigurationManagerImpl final : public Internal::GenericConfigurationMana // NOTE: Other public interface methods are implemented by GenericConfigurationManagerImpl<>. - // ===== Members for internal use by the following friends. - - friend ConfigurationManager & ConfigurationMgr(); - friend ConfigurationManagerImpl & ConfigurationMgrImpl(); - - static ConfigurationManagerImpl sInstance; - // ===== Private members reserved for use by this class only. static void DoFactoryReset(intptr_t arg); }; -/** - * Returns the public interface of the ConfigurationManager singleton object. - * - * chip applications should use this to access features of the ConfigurationManager object - * that are common to all platforms. - */ -inline ConfigurationManager & ConfigurationMgr() -{ - return ConfigurationManagerImpl::sInstance; -} - -/** - * Returns the platform-specific implementation of the ConfigurationManager singleton object. - * - * chip applications can use this to gain access to features of the ConfigurationManager - * that are specific to the ESP32 platform. - */ -inline ConfigurationManagerImpl & ConfigurationMgrImpl() -{ - return ConfigurationManagerImpl::sInstance; -} - } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/Linux/PlatformManagerImpl.cpp b/src/platform/Linux/PlatformManagerImpl.cpp index a4231fa396d4b6..39d00ff2da9950 100644 --- a/src/platform/Linux/PlatformManagerImpl.cpp +++ b/src/platform/Linux/PlatformManagerImpl.cpp @@ -60,27 +60,27 @@ void SignalHandler(int signum) switch (signum) { case SIGINT: - ConfigurationMgrImpl().StoreBootReasons(EMBER_ZCL_BOOT_REASON_TYPE_SOFTWARE_RESET); + ConfigurationMgr().StoreBootReasons(EMBER_ZCL_BOOT_REASON_TYPE_SOFTWARE_RESET); err = CHIP_ERROR_REBOOT_SIGNAL_RECEIVED; break; case SIGHUP: - ConfigurationMgrImpl().StoreBootReasons(EMBER_ZCL_BOOT_REASON_TYPE_BROWN_OUT_RESET); + ConfigurationMgr().StoreBootReasons(EMBER_ZCL_BOOT_REASON_TYPE_BROWN_OUT_RESET); err = CHIP_ERROR_REBOOT_SIGNAL_RECEIVED; break; case SIGTERM: - ConfigurationMgrImpl().StoreBootReasons(EMBER_ZCL_BOOT_REASON_TYPE_POWER_ON_REBOOT); + ConfigurationMgr().StoreBootReasons(EMBER_ZCL_BOOT_REASON_TYPE_POWER_ON_REBOOT); err = CHIP_ERROR_REBOOT_SIGNAL_RECEIVED; break; case SIGUSR1: - ConfigurationMgrImpl().StoreBootReasons(EMBER_ZCL_BOOT_REASON_TYPE_HARDWARE_WATCHDOG_RESET); + ConfigurationMgr().StoreBootReasons(EMBER_ZCL_BOOT_REASON_TYPE_HARDWARE_WATCHDOG_RESET); err = CHIP_ERROR_REBOOT_SIGNAL_RECEIVED; break; case SIGUSR2: - ConfigurationMgrImpl().StoreBootReasons(EMBER_ZCL_BOOT_REASON_TYPE_SOFTWARE_WATCHDOG_RESET); + ConfigurationMgr().StoreBootReasons(EMBER_ZCL_BOOT_REASON_TYPE_SOFTWARE_WATCHDOG_RESET); err = CHIP_ERROR_REBOOT_SIGNAL_RECEIVED; break; case SIGTSTP: - ConfigurationMgrImpl().StoreBootReasons(EMBER_ZCL_BOOT_REASON_TYPE_SOFTWARE_UPDATE_COMPLETED); + ConfigurationMgr().StoreBootReasons(EMBER_ZCL_BOOT_REASON_TYPE_SOFTWARE_UPDATE_COMPLETED); err = CHIP_ERROR_REBOOT_SIGNAL_RECEIVED; break; default: @@ -209,6 +209,7 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack() // Initialize the configuration system. err = Internal::PosixConfig::Init(); SuccessOrExit(err); + SetConfigurationMgr(&ConfigurationManagerImpl::GetDefaultInstance()); // Call _InitChipStack() on the generic implementation base class // to finish the initialization process. err = Internal::GenericPlatformManagerImpl_POSIX::_InitChipStack(); @@ -230,9 +231,9 @@ CHIP_ERROR PlatformManagerImpl::_Shutdown() { uint32_t totalOperationalHours = 0; - if (ConfigurationMgrImpl().GetTotalOperationalHours(totalOperationalHours) == CHIP_NO_ERROR) + if (ConfigurationMgr().GetTotalOperationalHours(totalOperationalHours) == CHIP_NO_ERROR) { - ConfigurationMgrImpl().StoreTotalOperationalHours(totalOperationalHours + static_cast(upTime / 3600)); + ConfigurationMgr().StoreTotalOperationalHours(totalOperationalHours + static_cast(upTime / 3600)); } else { @@ -289,7 +290,7 @@ CHIP_ERROR PlatformManagerImpl::_GetRebootCount(uint16_t & rebootCount) { uint32_t count = 0; - CHIP_ERROR err = ConfigurationMgrImpl().GetRebootCount(count); + CHIP_ERROR err = ConfigurationMgr().GetRebootCount(count); if (err == CHIP_NO_ERROR) { @@ -320,7 +321,7 @@ CHIP_ERROR PlatformManagerImpl::_GetTotalOperationalHours(uint32_t & totalOperat if (_GetUpTime(upTime) == CHIP_NO_ERROR) { uint32_t totalHours = 0; - if (ConfigurationMgrImpl().GetTotalOperationalHours(totalHours) == CHIP_NO_ERROR) + if (ConfigurationMgr().GetTotalOperationalHours(totalHours) == CHIP_NO_ERROR) { VerifyOrReturnError(upTime / 3600 <= UINT32_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE); totalOperationalHours = totalHours + static_cast(upTime / 3600); @@ -334,7 +335,7 @@ CHIP_ERROR PlatformManagerImpl::_GetBootReasons(uint8_t & bootReasons) { uint32_t reason = 0; - CHIP_ERROR err = ConfigurationMgrImpl().GetBootReasons(reason); + CHIP_ERROR err = ConfigurationMgr().GetBootReasons(reason); if (err == CHIP_NO_ERROR) { diff --git a/src/platform/P6/BUILD.gn b/src/platform/P6/BUILD.gn index 7269cd9a817f7b..4ee27012f2ac3e 100644 --- a/src/platform/P6/BUILD.gn +++ b/src/platform/P6/BUILD.gn @@ -26,6 +26,7 @@ if (chip_enable_openthread) { static_library("P6") { sources = [ "../FreeRTOS/SystemTimeSupport.cpp", + "../SingletonConfigurationManager.cpp", "BLEManagerImpl.cpp", "BLEManagerImpl.h", "BlePlatformConfig.h", diff --git a/src/platform/P6/ConfigurationManagerImpl.cpp b/src/platform/P6/ConfigurationManagerImpl.cpp index 7d90c3696432c4..b17e84fcb23d45 100644 --- a/src/platform/P6/ConfigurationManagerImpl.cpp +++ b/src/platform/P6/ConfigurationManagerImpl.cpp @@ -36,9 +36,11 @@ namespace DeviceLayer { using namespace ::chip::DeviceLayer::Internal; -/** Singleton instance of the ConfigurationManager implementation object for the PSoC6. - */ -ConfigurationManagerImpl ConfigurationManagerImpl::sInstance; +ConfigurationManagerImpl & ConfigurationManagerImpl::GetDefaultInstance() +{ + static ConfigurationManagerImpl sInstance; + return sInstance; +} CHIP_ERROR ConfigurationManagerImpl::Init() { diff --git a/src/platform/P6/ConfigurationManagerImpl.h b/src/platform/P6/ConfigurationManagerImpl.h index f7f136420cc5c2..da07d8d4904212 100644 --- a/src/platform/P6/ConfigurationManagerImpl.h +++ b/src/platform/P6/ConfigurationManagerImpl.h @@ -35,8 +35,8 @@ namespace DeviceLayer { /** * Concrete implementation of the ConfigurationManager singleton object for the PSoC6 platform. */ -class ConfigurationManagerImpl final : public Internal::GenericConfigurationManagerImpl, - private Internal::P6Config +class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImpl, + private Internal::P6Config { // Allow the GenericConfigurationManagerImpl base class to access helper methods and types // defined on this class. @@ -44,6 +44,10 @@ class ConfigurationManagerImpl final : public Internal::GenericConfigurationMana friend class Internal::GenericConfigurationManagerImpl; #endif +public: + // This returns an instance of this class. + static ConfigurationManagerImpl & GetDefaultInstance(); + private: // ===== Members that implement the ConfigurationManager public interface. @@ -56,37 +60,10 @@ class ConfigurationManagerImpl final : public Internal::GenericConfigurationMana // NOTE: Other public interface methods are implemented by GenericConfigurationManagerImpl<>. - // ===== Members for internal use by the following friends. - - friend ConfigurationManager & ConfigurationMgr(void); - friend ConfigurationManagerImpl & ConfigurationMgrImpl(void); - - static ConfigurationManagerImpl sInstance; + // ===== Private members reserved for use by this class only. static void DoFactoryReset(intptr_t arg); }; -/** - * Returns the public interface of the ConfigurationManager singleton object. - * - * Chip applications should use this to access features of the ConfigurationManager object - * that are common to all platforms. - */ -inline ConfigurationManager & ConfigurationMgr(void) -{ - return ConfigurationManagerImpl::sInstance; -} - -/** - * Returns the platform-specific implementation of the ConfigurationManager singleton object. - * - * Chip applications can use this to gain access to features of the ConfigurationManager - * that are specific to the PSoC6 platform. - */ -inline ConfigurationManagerImpl & ConfigurationMgrImpl(void) -{ - return ConfigurationManagerImpl::sInstance; -} - } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/P6/PlatformManagerImpl.cpp b/src/platform/P6/PlatformManagerImpl.cpp index 168cc7560231c1..c9503f243e1da7 100644 --- a/src/platform/P6/PlatformManagerImpl.cpp +++ b/src/platform/P6/PlatformManagerImpl.cpp @@ -42,6 +42,8 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) { CHIP_ERROR err; + SetConfigurationMgr(&ConfigurationManagerImpl::GetDefaultInstance()); + // Make sure the LwIP core lock has been initialized err = Internal::InitLwIPCoreLock(); SuccessOrExit(err); diff --git a/src/platform/fake/ConfigurationManagerImpl.cpp b/src/platform/SingletonConfigurationManager.cpp similarity index 55% rename from src/platform/fake/ConfigurationManagerImpl.cpp rename to src/platform/SingletonConfigurationManager.cpp index f0176a55738348..0658ebfbc776bc 100644 --- a/src/platform/fake/ConfigurationManagerImpl.cpp +++ b/src/platform/SingletonConfigurationManager.cpp @@ -1,7 +1,6 @@ /* * * Copyright (c) 2021 Project CHIP Authors - * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,14 +15,39 @@ * limitations under the License. */ -#include +/** + * @file + * Implements a getter and setter for a singleton ConfigurationManager object. + */ + +#include namespace chip { namespace DeviceLayer { -/** Singleton instance of the ConfigurationManager implementation object. +class ConfigurationManager; + +namespace { + +/** Singleton pointer to the ConfigurationManager implementation. */ -ConfigurationManagerImpl ConfigurationManagerImpl::sInstance; +ConfigurationManager * gInstance = nullptr; + +} // namespace + +ConfigurationManager & ConfigurationMgr() +{ + VerifyOrDie(gInstance != nullptr); + return *gInstance; +} + +void SetConfigurationMgr(ConfigurationManager * configurationManager) +{ + if (configurationManager != nullptr) + { + gInstance = configurationManager; + } +} } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/Tizen/BUILD.gn b/src/platform/Tizen/BUILD.gn index 6c3a046ad052d1..f066f8892819b5 100644 --- a/src/platform/Tizen/BUILD.gn +++ b/src/platform/Tizen/BUILD.gn @@ -26,6 +26,7 @@ static_library("Tizen") { sources = [ "../DeviceSafeQueue.cpp", "../DeviceSafeQueue.h", + "../SingletonConfigurationManager.cpp", "AppPreference.cpp", "AppPreference.h", "BLEManagerImpl.cpp", diff --git a/src/platform/Tizen/ConfigurationManagerImpl.cpp b/src/platform/Tizen/ConfigurationManagerImpl.cpp index 5a940cadda1c5b..237d114ca56e9f 100644 --- a/src/platform/Tizen/ConfigurationManagerImpl.cpp +++ b/src/platform/Tizen/ConfigurationManagerImpl.cpp @@ -37,9 +37,11 @@ namespace DeviceLayer { using namespace ::chip::DeviceLayer::Internal; -/** Singleton instance of the ConfigurationManager implementation object. - */ -ConfigurationManagerImpl ConfigurationManagerImpl::sInstance; +ConfigurationManagerImpl & ConfigurationManagerImpl::GetDefaultInstance() +{ + static ConfigurationManagerImpl sInstance; + return sInstance; +} CHIP_ERROR ConfigurationManagerImpl::Init(void) { diff --git a/src/platform/Tizen/ConfigurationManagerImpl.h b/src/platform/Tizen/ConfigurationManagerImpl.h index 73c0002b815d33..88fb2d7001c969 100644 --- a/src/platform/Tizen/ConfigurationManagerImpl.h +++ b/src/platform/Tizen/ConfigurationManagerImpl.h @@ -34,8 +34,8 @@ namespace DeviceLayer { /** * Concrete implementation of the ConfigurationManager singleton object for the Tizen platform. */ -class ConfigurationManagerImpl final : public Internal::GenericConfigurationManagerImpl, - private Internal::PosixConfig +class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImpl, + private Internal::PosixConfig { // Allow the GenericConfigurationManagerImpl base class to access helper methods and types // defined on this class. @@ -43,6 +43,10 @@ class ConfigurationManagerImpl final : public Internal::GenericConfigurationMana friend class Internal::GenericConfigurationManagerImpl; #endif +public: + // This returns an instance of this class. + static ConfigurationManagerImpl & GetDefaultInstance(); + private: // ===== Members that implement the ConfigurationManager public interface. @@ -54,36 +58,7 @@ class ConfigurationManagerImpl final : public Internal::GenericConfigurationMana CHIP_ERROR WritePersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t value) override; // NOTE: Other public interface methods are implemented by GenericConfigurationManagerImpl<>. - - // ===== Members for internal use by the following friends. - - friend ConfigurationManager & ConfigurationMgr(void); - friend ConfigurationManagerImpl & ConfigurationMgrImpl(void); - - static ConfigurationManagerImpl sInstance; }; -/** - * Returns the public interface of the ConfigurationManager singleton object. - * - * Chip applications should use this to access features of the ConfigurationManager object - * that are common to all platforms. - */ -inline ConfigurationManager & ConfigurationMgr(void) -{ - return ConfigurationManagerImpl::sInstance; -} - -/** - * Returns the platform-specific implementation of the ConfigurationManager singleton object. - * - * Chip applications can use this to gain access to features of the ConfigurationManager - * that are specific to the Tizen platform. - */ -inline ConfigurationManagerImpl & ConfigurationMgrImpl(void) -{ - return ConfigurationManagerImpl::sInstance; -} - } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/Tizen/PlatformManagerImpl.cpp b/src/platform/Tizen/PlatformManagerImpl.cpp index f4a1dc50d08328..bd4abfa8c75b0d 100644 --- a/src/platform/Tizen/PlatformManagerImpl.cpp +++ b/src/platform/Tizen/PlatformManagerImpl.cpp @@ -36,6 +36,7 @@ PlatformManagerImpl PlatformManagerImpl::sInstance; CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) { ReturnErrorOnFailure(Internal::PosixConfig::Init()); + SetConfigurationMgr(&ConfigurationManagerImpl::GetDefaultInstance()); return Internal::GenericPlatformManagerImpl_POSIX::_InitChipStack(); } diff --git a/src/platform/Zephyr/ConfigurationManagerImpl.cpp b/src/platform/Zephyr/ConfigurationManagerImpl.cpp index 397f9eca54fdd9..a6a743cf1588e9 100644 --- a/src/platform/Zephyr/ConfigurationManagerImpl.cpp +++ b/src/platform/Zephyr/ConfigurationManagerImpl.cpp @@ -43,9 +43,11 @@ namespace DeviceLayer { using namespace ::chip::DeviceLayer::Internal; -/** Singleton instance of the ConfigurationManager implementation object. - */ -ConfigurationManagerImpl ConfigurationManagerImpl::sInstance; +ConfigurationManagerImpl & ConfigurationManagerImpl::GetDefaultInstance() +{ + static ConfigurationManagerImpl sInstance; + return sInstance; +} CHIP_ERROR ConfigurationManagerImpl::Init() { diff --git a/src/platform/Zephyr/ConfigurationManagerImpl.h b/src/platform/Zephyr/ConfigurationManagerImpl.h index bad5c73cc7b4aa..92a8aff3c5771e 100644 --- a/src/platform/Zephyr/ConfigurationManagerImpl.h +++ b/src/platform/Zephyr/ConfigurationManagerImpl.h @@ -33,8 +33,8 @@ namespace DeviceLayer { /** * Concrete implementation of the ConfigurationManager singleton object for the Zephyr platform. */ -class ConfigurationManagerImpl final : public Internal::GenericConfigurationManagerImpl, - private Internal::ZephyrConfig +class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImpl, + private Internal::ZephyrConfig { // Allow the GenericConfigurationManagerImpl base class to access helper methods and types // defined on this class. @@ -42,6 +42,10 @@ class ConfigurationManagerImpl final : public Internal::GenericConfigurationMana friend class Internal::GenericConfigurationManagerImpl; #endif +public: + // This returns an instance of this class. + static ConfigurationManagerImpl & GetDefaultInstance(); + private: // ===== Members that implement the ConfigurationManager public interface. @@ -54,40 +58,11 @@ class ConfigurationManagerImpl final : public Internal::GenericConfigurationMana // NOTE: Other public interface methods are implemented by GenericConfigurationManagerImpl<>. - // ===== Members for internal use by the following friends. - - friend ConfigurationManager & ConfigurationMgr(void); - friend ConfigurationManagerImpl & ConfigurationMgrImpl(void); - - static ConfigurationManagerImpl sInstance; - // ===== Private members reserved for use by this class only. static void DoFactoryReset(intptr_t arg); }; -/** - * Returns the public interface of the ConfigurationManager singleton object. - * - * chip applications should use this to access features of the ConfigurationManager object - * that are common to all platforms. - */ -inline ConfigurationManager & ConfigurationMgr(void) -{ - return ConfigurationManagerImpl::sInstance; -} - -/** - * Returns the platform-specific implementation of the ConfigurationManager singleton object. - * - * chip applications can use this to gain access to features of the ConfigurationManager - * that are specific to the nRF Connect SDK platform. - */ -inline ConfigurationManagerImpl & ConfigurationMgrImpl(void) -{ - return ConfigurationManagerImpl::sInstance; -} - inline bool ConfigurationManagerImpl::CanFactoryReset() { return true; diff --git a/src/platform/Zephyr/PlatformManagerImpl.cpp b/src/platform/Zephyr/PlatformManagerImpl.cpp index 6ced08cff90f32..09f388058c0c03 100644 --- a/src/platform/Zephyr/PlatformManagerImpl.cpp +++ b/src/platform/Zephyr/PlatformManagerImpl.cpp @@ -72,6 +72,7 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) // Initialize the configuration system. err = Internal::ZephyrConfig::Init(); SuccessOrExit(err); + SetConfigurationMgr(&ConfigurationManagerImpl::GetDefaultInstance()); #if !CONFIG_NORDIC_SECURITY_BACKEND // Add entropy source based on Zephyr entropy driver diff --git a/src/platform/android/AndroidChipPlatform-JNI.cpp b/src/platform/android/AndroidChipPlatform-JNI.cpp index 785ed89cb42638..fd2cbd20dd7f8b 100644 --- a/src/platform/android/AndroidChipPlatform-JNI.cpp +++ b/src/platform/android/AndroidChipPlatform-JNI.cpp @@ -207,7 +207,12 @@ JNI_METHOD(void, setKeyValueStoreManager)(JNIEnv * env, jclass self, jobject man JNI_METHOD(void, setConfigurationManager)(JNIEnv * env, jclass self, jobject manager) { chip::DeviceLayer::StackLock lock; - chip::DeviceLayer::ConfigurationMgrImpl().InitializeWithObject(manager); + chip::DeviceLayer::ConfigurationManagerImpl * configurationManagerImpl = + reinterpret_cast(&chip::DeviceLayer::ConfigurationMgr()); + if (configurationManagerImpl != nullptr) + { + configurationManagerImpl->InitializeWithObject(manager); + } } // for ServiceResolver diff --git a/src/platform/android/BUILD.gn b/src/platform/android/BUILD.gn index a12d6aa55e9108..1b6cc5ea3313eb 100644 --- a/src/platform/android/BUILD.gn +++ b/src/platform/android/BUILD.gn @@ -28,6 +28,7 @@ static_library("android") { sources = [ "../DeviceSafeQueue.cpp", "../DeviceSafeQueue.h", + "../SingletonConfigurationManager.cpp", "AndroidChipPlatform-JNI.cpp", "AndroidConfig.cpp", "AndroidConfig.h", diff --git a/src/platform/android/ConfigurationManagerImpl.cpp b/src/platform/android/ConfigurationManagerImpl.cpp index 6303bb5b8e5a0f..0b9c37b55515dd 100644 --- a/src/platform/android/ConfigurationManagerImpl.cpp +++ b/src/platform/android/ConfigurationManagerImpl.cpp @@ -42,9 +42,11 @@ namespace DeviceLayer { using namespace ::chip::DeviceLayer::Internal; -/** Singleton instance of the ConfigurationManager implementation object. - */ -ConfigurationManagerImpl ConfigurationManagerImpl::sInstance; +ConfigurationManagerImpl & ConfigurationManagerImpl::GetDefaultInstance() +{ + static ConfigurationManagerImpl sInstance; + return sInstance; +} void ConfigurationManagerImpl::InitializeWithObject(jobject managerObject) { diff --git a/src/platform/android/ConfigurationManagerImpl.h b/src/platform/android/ConfigurationManagerImpl.h index 7135deb968fcaf..91b048cb3301af 100644 --- a/src/platform/android/ConfigurationManagerImpl.h +++ b/src/platform/android/ConfigurationManagerImpl.h @@ -35,8 +35,8 @@ namespace DeviceLayer { /** * Concrete implementation of the ConfigurationManager singleton object for the Android platform. */ -class ConfigurationManagerImpl final : public Internal::GenericConfigurationManagerImpl, - private Internal::AndroidConfig +class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImpl, + private Internal::AndroidConfig { // Allow the GenericConfigurationManagerImpl base class to access helper methods and types // defined on this class. @@ -46,6 +46,7 @@ class ConfigurationManagerImpl final : public Internal::GenericConfigurationMana public: void InitializeWithObject(jobject managerObject); + static ConfigurationManagerImpl & GetDefaultInstance(); private: // ===== Members that implement the ConfigurationManager public interface. @@ -64,13 +65,6 @@ class ConfigurationManagerImpl final : public Internal::GenericConfigurationMana // NOTE: Other public interface methods are implemented by GenericConfigurationManagerImpl<>. - // ===== Members for internal use by the following friends. - - friend ConfigurationManager & ConfigurationMgr(); - friend ConfigurationManagerImpl & ConfigurationMgrImpl(); - - static ConfigurationManagerImpl sInstance; - // ===== Private members reserved for use by this class only. static void DoFactoryReset(intptr_t arg); @@ -78,27 +72,5 @@ class ConfigurationManagerImpl final : public Internal::GenericConfigurationMana jobject mConfigurationManagerObject = nullptr; }; -/** - * Returns the public interface of the ConfigurationManager singleton object. - * - * chip applications should use this to access features of the ConfigurationManager object - * that are common to all platforms. - */ -inline ConfigurationManager & ConfigurationMgr() -{ - return ConfigurationManagerImpl::sInstance; -} - -/** - * Returns the platform-specific implementation of the ConfigurationManager singleton object. - * - * chip applications can use this to gain access to features of the ConfigurationManager - * that are specific to the Android platform. - */ -inline ConfigurationManagerImpl & ConfigurationMgrImpl() -{ - return ConfigurationManagerImpl::sInstance; -} - } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/android/PlatformManagerImpl.cpp b/src/platform/android/PlatformManagerImpl.cpp index 21b3509807426e..50f5d04be48420 100644 --- a/src/platform/android/PlatformManagerImpl.cpp +++ b/src/platform/android/PlatformManagerImpl.cpp @@ -43,6 +43,7 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack() // Initialize the configuration system. err = Internal::AndroidConfig::Init(); SuccessOrExit(err); + SetConfigurationMgr(&ConfigurationManagerImpl::GetDefaultInstance()); // Call _InitChipStack() on the generic implementation base class // to finish the initialization process. diff --git a/src/platform/cc13x2_26x2/BUILD.gn b/src/platform/cc13x2_26x2/BUILD.gn index 346f1c2718601b..db40cfb1918db3 100644 --- a/src/platform/cc13x2_26x2/BUILD.gn +++ b/src/platform/cc13x2_26x2/BUILD.gn @@ -25,6 +25,7 @@ if (chip_enable_openthread) { static_library("cc13x2_26x2") { sources = [ "../FreeRTOS/SystemTimeSupport.cpp", + "../SingletonConfigurationManager.cpp", "BlePlatformConfig.h", "CC13X2_26X2Config.cpp", "CC13X2_26X2Config.h", diff --git a/src/platform/cc13x2_26x2/ConfigurationManagerImpl.cpp b/src/platform/cc13x2_26x2/ConfigurationManagerImpl.cpp index e2048bde305401..d7a22577288411 100644 --- a/src/platform/cc13x2_26x2/ConfigurationManagerImpl.cpp +++ b/src/platform/cc13x2_26x2/ConfigurationManagerImpl.cpp @@ -54,9 +54,11 @@ namespace DeviceLayer { using namespace ::chip::DeviceLayer::Internal; -/** Singleton instance of the ConfigurationManager implementation object. - */ -ConfigurationManagerImpl ConfigurationManagerImpl::sInstance; +ConfigurationManagerImpl & ConfigurationManagerImpl::GetDefaultInstance() +{ + static ConfigurationManagerImpl sInstance; + return sInstance; +} CHIP_ERROR ConfigurationManagerImpl::Init() { diff --git a/src/platform/cc13x2_26x2/ConfigurationManagerImpl.h b/src/platform/cc13x2_26x2/ConfigurationManagerImpl.h index b892304ad70227..febdb5d11adfb7 100644 --- a/src/platform/cc13x2_26x2/ConfigurationManagerImpl.h +++ b/src/platform/cc13x2_26x2/ConfigurationManagerImpl.h @@ -33,13 +33,17 @@ namespace DeviceLayer { /** * Concrete implementation of the ConfigurationManager singleton object for the CC13X2_26X2 platform. */ -class ConfigurationManagerImpl final : public Internal::GenericConfigurationManagerImpl, - private Internal::CC13X2_26X2Config +class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImpl, + private Internal::CC13X2_26X2Config { // Allow the GenericConfigurationManagerImpl base class to access helper methods and types // defined on this class. friend class Internal::GenericConfigurationManagerImpl; +public: + // This returns an instance of this class. + static ConfigurationManagerImpl & GetDefaultInstance(); + private: // ===== Members that implement the ConfigurationManager public interface. @@ -52,40 +56,11 @@ class ConfigurationManagerImpl final : public Internal::GenericConfigurationMana // NOTE: Other public interface methods are implemented by GenericConfigurationManagerImpl<>. - // ===== Members for internal use by the following friends. - - friend ConfigurationManager & ConfigurationMgr(void); - friend ConfigurationManagerImpl & ConfigurationMgrImpl(void); - - static ConfigurationManagerImpl sInstance; - // ===== Private members reserved for use by this class only. static void DoFactoryReset(intptr_t arg); }; -/** - * Returns the public interface of the ConfigurationManager singleton object. - * - * chip applications should use this to access features of the ConfigurationManager object - * that are common to all platforms. - */ -inline ConfigurationManager & ConfigurationMgr(void) -{ - return ConfigurationManagerImpl::sInstance; -} - -/** - * Returns the platform-specific implementation of the ConfigurationManager singleton object. - * - * chip applications can use this to gain access to features of the ConfigurationManager - * that are specific to the CC1352 SoC - */ -inline ConfigurationManagerImpl & ConfigurationMgrImpl(void) -{ - return ConfigurationManagerImpl::sInstance; -} - inline CHIP_ERROR ConfigurationManagerImpl::GetPrimaryWiFiMACAddress(uint8_t * buf) { return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; diff --git a/src/platform/cc13x2_26x2/PlatformManagerImpl.cpp b/src/platform/cc13x2_26x2/PlatformManagerImpl.cpp index dea57bed5800b5..07178e46e3c866 100644 --- a/src/platform/cc13x2_26x2/PlatformManagerImpl.cpp +++ b/src/platform/cc13x2_26x2/PlatformManagerImpl.cpp @@ -106,6 +106,7 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) // Initialize the configuration system. err = Internal::CC13X2_26X2Config::Init(); SuccessOrExit(err); + SetConfigurationMgr(&ConfigurationManagerImpl::GetDefaultInstance()); // DMM Addition DMMPolicy_Params dmmPolicyParams; diff --git a/src/platform/fake/BUILD.gn b/src/platform/fake/BUILD.gn index 9c4d008f11300f..93d4425e604f66 100644 --- a/src/platform/fake/BUILD.gn +++ b/src/platform/fake/BUILD.gn @@ -21,7 +21,6 @@ assert(chip_device_platform == "fake") static_library("fake") { sources = [ "CHIPDevicePlatformEvent.h", - "ConfigurationManagerImpl.cpp", "ConfigurationManagerImpl.h", "ConnectivityManagerImpl.cpp", "ConnectivityManagerImpl.h", diff --git a/src/platform/fake/ConfigurationManagerImpl.h b/src/platform/fake/ConfigurationManagerImpl.h index af8ac5c6c2bbfb..cee37e551605a1 100644 --- a/src/platform/fake/ConfigurationManagerImpl.h +++ b/src/platform/fake/ConfigurationManagerImpl.h @@ -25,8 +25,9 @@ namespace DeviceLayer { /** * Concrete implementation of the ConfigurationManager singleton object for the fake platform. */ -class ConfigurationManagerImpl final : public ConfigurationManager +class ConfigurationManagerImpl : public ConfigurationManager { +public: virtual ~ConfigurationManagerImpl() = default; private: @@ -79,6 +80,12 @@ class ConfigurationManagerImpl final : public ConfigurationManager CHIP_ERROR StoreCountryCode(const char * code, size_t codeLen) override { return CHIP_ERROR_NOT_IMPLEMENTED; } CHIP_ERROR GetBreadcrumb(uint64_t & breadcrumb) override { return CHIP_ERROR_NOT_IMPLEMENTED; } CHIP_ERROR StoreBreadcrumb(uint64_t breadcrumb) override { return CHIP_ERROR_NOT_IMPLEMENTED; } + CHIP_ERROR GetRebootCount(uint32_t & rebootCount) override { return CHIP_ERROR_NOT_IMPLEMENTED; } + CHIP_ERROR StoreRebootCount(uint32_t rebootCount) override { return CHIP_ERROR_NOT_IMPLEMENTED; } + CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours) override { return CHIP_ERROR_NOT_IMPLEMENTED; } + CHIP_ERROR StoreTotalOperationalHours(uint32_t totalOperationalHours) override { return CHIP_ERROR_NOT_IMPLEMENTED; } + CHIP_ERROR GetBootReasons(uint32_t & bootReasons) override { return CHIP_ERROR_NOT_IMPLEMENTED; } + CHIP_ERROR StoreBootReasons(uint32_t bootReasons) override { return CHIP_ERROR_NOT_IMPLEMENTED; } #if !defined(NDEBUG) CHIP_ERROR RunUnitTests(void) override { return CHIP_ERROR_NOT_IMPLEMENTED; } #endif @@ -96,35 +103,15 @@ class ConfigurationManagerImpl final : public ConfigurationManager } // NOTE: Other public interface methods are implemented by GenericConfigurationManagerImpl<>. - // ===== Members for internal use by the following friends. - - friend ConfigurationManager & ConfigurationMgr(void); - friend ConfigurationManagerImpl & ConfigurationMgrImpl(void); - - static ConfigurationManagerImpl sInstance; }; -/** - * Returns the public interface of the ConfigurationManager singleton object. - * - * Chip applications should use this to access features of the ConfigurationManager object - * that are common to all platforms. - */ -inline ConfigurationManager & ConfigurationMgr(void) +ConfigurationManager & ConfigurationMgr() { - return ConfigurationManagerImpl::sInstance; + static ConfigurationManagerImpl sInstance; + return sInstance; } -/** - * Returns the platform-specific implementation of the ConfigurationManager singleton object. - * - * Chip applications can use this to gain access to features of the ConfigurationManager - * that are specific to the ESP32 platform. - */ -inline ConfigurationManagerImpl & ConfigurationMgrImpl(void) -{ - return ConfigurationManagerImpl::sInstance; -} +void SetConfigurationMgr(ConfigurationManagerImpl * configurationManager) {} } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/mbed/BUILD.gn b/src/platform/mbed/BUILD.gn index 138cc3e6bc0cfc..beee849ddc7f41 100644 --- a/src/platform/mbed/BUILD.gn +++ b/src/platform/mbed/BUILD.gn @@ -20,6 +20,7 @@ assert(chip_device_platform == "mbed") static_library("mbed") { sources = [ + "../SingletonConfigurationManager.cpp", "BLEManagerImpl.cpp", "BLEManagerImpl.h", "ConfigurationManagerImpl.cpp", diff --git a/src/platform/mbed/ConfigurationManagerImpl.cpp b/src/platform/mbed/ConfigurationManagerImpl.cpp index f6e7c387c05dfe..bd78fb8e27573c 100644 --- a/src/platform/mbed/ConfigurationManagerImpl.cpp +++ b/src/platform/mbed/ConfigurationManagerImpl.cpp @@ -42,9 +42,11 @@ namespace DeviceLayer { using namespace ::chip::DeviceLayer::Internal; -/** Singleton instance of the ConfigurationManager implementation object. - */ -ConfigurationManagerImpl ConfigurationManagerImpl::sInstance; +ConfigurationManagerImpl & ConfigurationManagerImpl::GetDefaultInstance() +{ + static ConfigurationManagerImpl sInstance; + return sInstance; +} CHIP_ERROR ConfigurationManagerImpl::Init() { diff --git a/src/platform/mbed/ConfigurationManagerImpl.h b/src/platform/mbed/ConfigurationManagerImpl.h index ca60444f3c0f5f..7acc0c2ab5e068 100644 --- a/src/platform/mbed/ConfigurationManagerImpl.h +++ b/src/platform/mbed/ConfigurationManagerImpl.h @@ -32,8 +32,8 @@ namespace DeviceLayer { /** * Concrete implementation of the ConfigurationManager singleton object for the Zephyr platform. */ -class ConfigurationManagerImpl final : public Internal::GenericConfigurationManagerImpl, - private Internal::MbedConfig +class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImpl, + private Internal::MbedConfig { // Allow the GenericConfigurationManagerImpl base class to access helper methods and types // defined on this class. @@ -41,6 +41,10 @@ class ConfigurationManagerImpl final : public Internal::GenericConfigurationMana friend class Internal::GenericConfigurationManagerImpl; #endif +public: + // This returns an instance of this class. + static ConfigurationManagerImpl & GetDefaultInstance(); + private: // ===== Members that implement the ConfigurationManager public interface. @@ -53,39 +57,10 @@ class ConfigurationManagerImpl final : public Internal::GenericConfigurationMana // NOTE: Other public interface methods are implemented by GenericConfigurationManagerImpl<>. - // ===== Members for internal use by the following friends. - - friend ConfigurationManager & ConfigurationMgr(void); - friend ConfigurationManagerImpl & ConfigurationMgrImpl(void); - - static ConfigurationManagerImpl sInstance; - // ===== Private members reserved for use by this class only. static void DoFactoryReset(intptr_t arg); }; -/** - * Returns the public interface of the ConfigurationManager singleton object. - * - * chip applications should use this to access features of the ConfigurationManager object - * that are common to all platforms. - */ -inline ConfigurationManager & ConfigurationMgr(void) -{ - return ConfigurationManagerImpl::sInstance; -} - -/** - * Returns the platform-specific implementation of the ConfigurationManager singleton object. - * - * chip applications can use this to gain access to features of the ConfigurationManager - * that are specific to the nRF Connect SDK platform. - */ -inline ConfigurationManagerImpl & ConfigurationMgrImpl(void) -{ - return ConfigurationManagerImpl::sInstance; -} - } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/mbed/PlatformManagerImpl.cpp b/src/platform/mbed/PlatformManagerImpl.cpp index 0dc462ccca1659..a76ea42dda9138 100644 --- a/src/platform/mbed/PlatformManagerImpl.cpp +++ b/src/platform/mbed/PlatformManagerImpl.cpp @@ -91,6 +91,8 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) tcpip_init(NULL, NULL); #endif + SetConfigurationMgr(&ConfigurationManagerImpl::GetDefaultInstance()); + // Call up to the base class _InitChipStack() to perform the bulk of the initialization. auto err = GenericPlatformManagerImpl::_InitChipStack(); SuccessOrExit(err); diff --git a/src/platform/nrfconnect/BUILD.gn b/src/platform/nrfconnect/BUILD.gn index 7f6cb034e92174..83f00cf1d82e30 100644 --- a/src/platform/nrfconnect/BUILD.gn +++ b/src/platform/nrfconnect/BUILD.gn @@ -20,6 +20,7 @@ assert(chip_device_platform == "nrfconnect") static_library("nrfconnect") { sources = [ + "../SingletonConfigurationManager.cpp", "../Zephyr/BLEManagerImpl.cpp", "../Zephyr/ConfigurationManagerImpl.cpp", "../Zephyr/KeyValueStoreManagerImpl.cpp", diff --git a/src/platform/nxp/k32w/k32w0/BUILD.gn b/src/platform/nxp/k32w/k32w0/BUILD.gn index 10628fd4466e5c..0a37f9caf71408 100644 --- a/src/platform/nxp/k32w/k32w0/BUILD.gn +++ b/src/platform/nxp/k32w/k32w0/BUILD.gn @@ -25,6 +25,7 @@ if (chip_enable_openthread) { static_library("k32w0") { sources = [ "../../../FreeRTOS/SystemTimeSupport.cpp", + "../../../SingletonConfigurationManager.cpp", "BLEManagerImpl.cpp", "BLEManagerImpl.h", "CHIPDevicePlatformConfig.h", diff --git a/src/platform/nxp/k32w/k32w0/ConfigurationManagerImpl.cpp b/src/platform/nxp/k32w/k32w0/ConfigurationManagerImpl.cpp index a5bbaffcecd011..9c17cc1290c09b 100644 --- a/src/platform/nxp/k32w/k32w0/ConfigurationManagerImpl.cpp +++ b/src/platform/nxp/k32w/k32w0/ConfigurationManagerImpl.cpp @@ -39,9 +39,11 @@ using namespace ::chip::DeviceLayer::Internal; // TODO: Define a Singleton instance of CHIP Group Key Store here -/** Singleton instance of the ConfigurationManager implementation object. - */ -ConfigurationManagerImpl ConfigurationManagerImpl::sInstance; +ConfigurationManagerImpl & ConfigurationManagerImpl::GetDefaultInstance() +{ + static ConfigurationManagerImpl sInstance; + return sInstance; +} CHIP_ERROR ConfigurationManagerImpl::Init() { diff --git a/src/platform/nxp/k32w/k32w0/ConfigurationManagerImpl.h b/src/platform/nxp/k32w/k32w0/ConfigurationManagerImpl.h index 14ea2c923f8d3b..c6443c52e6c798 100644 --- a/src/platform/nxp/k32w/k32w0/ConfigurationManagerImpl.h +++ b/src/platform/nxp/k32w/k32w0/ConfigurationManagerImpl.h @@ -43,6 +43,10 @@ class ConfigurationManagerImpl final : public Internal::GenericConfigurationMana friend class Internal::GenericConfigurationManagerImpl; #endif +public: + // This returns an instance of this class. + static ConfigurationManagerImpl & GetDefaultInstance(); + private: // ===== Members that implement the ConfigurationManager public interface. @@ -55,40 +59,11 @@ class ConfigurationManagerImpl final : public Internal::GenericConfigurationMana // NOTE: Other public interface methods are implemented by GenericConfigurationManagerImpl<>. - // ===== Members for internal use by the following friends. - - friend ConfigurationManager & ConfigurationMgr(void); - friend ConfigurationManagerImpl & ConfigurationMgrImpl(void); - - static ConfigurationManagerImpl sInstance; - // ===== Private members reserved for use by this class only. static void DoFactoryReset(intptr_t arg); }; -/** - * Returns the public interface of the ConfigurationManager singleton object. - * - * Chip applications should use this to access features of the ConfigurationManager object - * that are common to all platforms. - */ -inline ConfigurationManager & ConfigurationMgr(void) -{ - return ConfigurationManagerImpl::sInstance; -} - -/** - * Returns the platform-specific implementation of the ConfigurationManager singleton object. - * - * Chio applications can use this to gain access to features of the ConfigurationManager - * that are specific to the K32W platform. - */ -inline ConfigurationManagerImpl & ConfigurationMgrImpl(void) -{ - return ConfigurationManagerImpl::sInstance; -} - inline CHIP_ERROR ConfigurationManagerImpl::GetPrimaryWiFiMACAddress(uint8_t * buf) { return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; diff --git a/src/platform/nxp/k32w/k32w0/PlatformManagerImpl.cpp b/src/platform/nxp/k32w/k32w0/PlatformManagerImpl.cpp index cfcc6ad007e264..567274e3052ca8 100644 --- a/src/platform/nxp/k32w/k32w0/PlatformManagerImpl.cpp +++ b/src/platform/nxp/k32w/k32w0/PlatformManagerImpl.cpp @@ -58,6 +58,7 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) // Initialize the configuration system. err = Internal::K32WConfig::Init(); SuccessOrExit(err); + SetConfigurationMgr(&ConfigurationManagerImpl::GetDefaultInstance()); // Initialize LwIP. tcpip_init(NULL, NULL); diff --git a/src/platform/qpg/BUILD.gn b/src/platform/qpg/BUILD.gn index a4abfeb93e0284..c3526b4756b838 100644 --- a/src/platform/qpg/BUILD.gn +++ b/src/platform/qpg/BUILD.gn @@ -25,6 +25,7 @@ if (chip_enable_openthread) { static_library("qpg") { sources = [ "../FreeRTOS/SystemTimeSupport.cpp", + "../SingletonConfigurationManager.cpp", "BLEManagerImpl.cpp", "BLEManagerImpl.h", "BlePlatformConfig.h", diff --git a/src/platform/qpg/ConfigurationManagerImpl.cpp b/src/platform/qpg/ConfigurationManagerImpl.cpp index 5a85e537c6be5c..c8c059017d7cb4 100644 --- a/src/platform/qpg/ConfigurationManagerImpl.cpp +++ b/src/platform/qpg/ConfigurationManagerImpl.cpp @@ -41,9 +41,11 @@ namespace DeviceLayer { using namespace ::chip::DeviceLayer::Internal; -/** Singleton instance of the ConfigurationManager implementation object. - */ -ConfigurationManagerImpl ConfigurationManagerImpl::sInstance; +ConfigurationManagerImpl & ConfigurationManagerImpl::GetDefaultInstance() +{ + static ConfigurationManagerImpl sInstance; + return sInstance; +} CHIP_ERROR ConfigurationManagerImpl::Init() { diff --git a/src/platform/qpg/ConfigurationManagerImpl.h b/src/platform/qpg/ConfigurationManagerImpl.h index b3e57733347a67..c6f08abe227850 100644 --- a/src/platform/qpg/ConfigurationManagerImpl.h +++ b/src/platform/qpg/ConfigurationManagerImpl.h @@ -32,8 +32,8 @@ namespace DeviceLayer { /** * Concrete implementation of the ConfigurationManager singleton object for the platform. */ -class ConfigurationManagerImpl final : public Internal::GenericConfigurationManagerImpl, - private Internal::QPGConfig +class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImpl, + private Internal::QPGConfig { // Allow the GenericConfigurationManagerImpl base class to access helper methods and types // defined on this class. @@ -41,6 +41,10 @@ class ConfigurationManagerImpl final : public Internal::GenericConfigurationMana friend class Internal::GenericConfigurationManagerImpl; #endif +public: + // This returns an instance of this class. + static ConfigurationManagerImpl & GetDefaultInstance(); + private: // ===== Members that implement the ConfigurationManager public interface. @@ -53,40 +57,11 @@ class ConfigurationManagerImpl final : public Internal::GenericConfigurationMana // NOTE: Other public interface methods are implemented by GenericConfigurationManagerImpl<>. - // ===== Members for internal use by the following friends. - - friend ConfigurationManager & ConfigurationMgr(void); - friend ConfigurationManagerImpl & ConfigurationMgrImpl(void); - - static ConfigurationManagerImpl sInstance; - // ===== Private members reserved for use by this class only. static void DoFactoryReset(intptr_t arg); }; -/** - * Returns the public interface of the ConfigurationManager singleton object. - * - * chip applications should use this to access features of the ConfigurationManager object - * that are common to all platforms. - */ -inline ConfigurationManager & ConfigurationMgr(void) -{ - return ConfigurationManagerImpl::sInstance; -} - -/** - * Returns the platform-specific implementation of the ConfigurationManager singleton object. - * - * chip applications can use this to gain access to features of the ConfigurationManager - * that are specific to the platform. - */ -inline ConfigurationManagerImpl & ConfigurationMgrImpl(void) -{ - return ConfigurationManagerImpl::sInstance; -} - inline CHIP_ERROR ConfigurationManagerImpl::GetPrimaryWiFiMACAddress(uint8_t * buf) { return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; diff --git a/src/platform/qpg/PlatformManagerImpl.cpp b/src/platform/qpg/PlatformManagerImpl.cpp index 4397237f528d13..ff6e38aafc853d 100644 --- a/src/platform/qpg/PlatformManagerImpl.cpp +++ b/src/platform/qpg/PlatformManagerImpl.cpp @@ -40,6 +40,7 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) // Initialize the configuration system. err = Internal::QPGConfig::Init(); SuccessOrExit(err); + SetConfigurationMgr(&ConfigurationManagerImpl::GetDefaultInstance()); // Initialize LwIP. tcpip_init(NULL, NULL); diff --git a/src/platform/telink/BUILD.gn b/src/platform/telink/BUILD.gn index 77e03b15879532..539a8d93775747 100644 --- a/src/platform/telink/BUILD.gn +++ b/src/platform/telink/BUILD.gn @@ -20,6 +20,7 @@ assert(chip_device_platform == "telink") static_library("telink") { sources = [ + "../SingletonConfigurationManager.cpp", "../Zephyr/BLEManagerImpl.cpp", "../Zephyr/ConfigurationManagerImpl.cpp", "../Zephyr/KeyValueStoreManagerImpl.cpp", diff --git a/src/transport/raw/tests/NetworkTestHelpers.cpp b/src/transport/raw/tests/NetworkTestHelpers.cpp index f4a9ab18ea8305..280cd6b84d0a75 100644 --- a/src/transport/raw/tests/NetworkTestHelpers.cpp +++ b/src/transport/raw/tests/NetworkTestHelpers.cpp @@ -22,6 +22,7 @@ #include #include #include +#include namespace chip { namespace Test { @@ -29,6 +30,7 @@ namespace Test { CHIP_ERROR IOContext::Init() { CHIP_ERROR err = Platform::MemoryInit(); + chip::DeviceLayer::SetConfigurationMgr(&chip::DeviceLayer::ConfigurationManagerImpl::GetDefaultInstance()); gSystemLayer.Init();