3 * Copyright (c) 2020 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 * Provides the implementation of the Device Layer ConfigurationManager object
21 * for QPG6100 platforms .
23 /* this file behaves like a config.h, comes first */
24 #include <platform/internal/CHIPDeviceLayerInternal.h>
26 #include <platform/ConfigurationManager.h>
27 #include <platform/internal/GenericConfigurationManagerImpl.cpp>
29 #include <core/CHIPVendorIdentifiers.hpp>
30 #include <platform/qpg6100/qpg6100Config.h>
32 #if CHIP_DEVICE_CONFIG_ENABLE_FACTORY_PROVISIONING
33 #include <platform/internal/FactoryProvisioning.cpp>
34 #endif // CHIP_DEVICE_CONFIG_ENABLE_FACTORY_PROVISIONING
36 #include <support/CodeUtils.h>
37 #include <support/logging/CHIPLogging.h>
40 namespace DeviceLayer {
42 using namespace ::chip::DeviceLayer::Internal;
44 /** Singleton instance of the ConfigurationManager implementation object.
46 ConfigurationManagerImpl ConfigurationManagerImpl::sInstance;
48 CHIP_ERROR ConfigurationManagerImpl::_Init()
53 // Initialize the generic implementation base class.
54 err = Internal::GenericConfigurationManagerImpl<ConfigurationManagerImpl>::_Init();
57 // If the fail-safe was armed when the device last shutdown, initiate a factory reset.
58 if (_GetFailSafeArmed(failSafeArmed) == CHIP_NO_ERROR && failSafeArmed)
60 ChipLogProgress(DeviceLayer, "Detected fail-safe armed on reboot; initiating factory reset");
61 _InitiateFactoryReset();
69 bool ConfigurationManagerImpl::_CanFactoryReset()
71 // TODO: query the application to determine if factory reset is allowed.
75 void ConfigurationManagerImpl::_InitiateFactoryReset()
77 PlatformMgr().ScheduleWork(DoFactoryReset);
80 CHIP_ERROR ConfigurationManagerImpl::_ReadPersistedStorageValue(::chip::Platform::PersistedStorage::Key persistedStorageKey,
84 uintmax_t recordKey = persistedStorageKey + kConfigKey_GroupKeyBase;
86 VerifyOrExit(recordKey <= kConfigKey_GroupKeyMax, err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND);
88 err = ReadConfigValue(persistedStorageKey, value);
89 if (err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND)
91 err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
99 CHIP_ERROR ConfigurationManagerImpl::_WritePersistedStorageValue(::chip::Platform::PersistedStorage::Key persistedStorageKey,
104 uintmax_t recordKey = persistedStorageKey + kConfigKey_GroupKeyBase;
106 VerifyOrExit(recordKey <= kConfigKey_GroupKeyMax, err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND);
108 err = WriteConfigValue(persistedStorageKey, value);
115 void ConfigurationManagerImpl::DoFactoryReset(intptr_t arg)
119 ChipLogProgress(DeviceLayer, "Performing factory reset");
121 err = FactoryResetConfig();
122 if (err != CHIP_NO_ERROR)
124 ChipLogError(DeviceLayer, "FactoryResetConfig() failed: %s", ErrorStr(err));
127 #if CHIP_DEVICE_CONFIG_ENABLE_THREAD
129 ChipLogProgress(DeviceLayer, "Clearing Thread provision");
130 ThreadStackMgr().ErasePersistentInfo();
132 #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD
134 // Restart the system.
135 ChipLogProgress(DeviceLayer, "System restarting");
136 qvCHIP_ResetSystem();
139 } // namespace DeviceLayer