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 Zephyr platforms.
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/Zephyr/ZephyrConfig.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>
39 #include <power/reboot.h>
42 namespace DeviceLayer {
44 using namespace ::chip::DeviceLayer::Internal;
46 /** Singleton instance of the ConfigurationManager implementation object.
48 ConfigurationManagerImpl ConfigurationManagerImpl::sInstance;
50 CHIP_ERROR ConfigurationManagerImpl::_Init()
55 // Initialize the generic implementation base class.
56 err = Internal::GenericConfigurationManagerImpl<ConfigurationManagerImpl>::_Init();
59 // TODO: Initialize the global GroupKeyStore object here
60 #if CHIP_DEVICE_CONFIG_ENABLE_FACTORY_PROVISIONING
62 FactoryProvisioning factoryProv;
63 uint8_t * const kDeviceRAMStart = (uint8_t *) CONFIG_SRAM_BASE_ADDRESS;
64 uint8_t * const kDeviceRAMEnd = kDeviceRAMStart + CONFIG_SRAM_SIZE * 1024 - 1;
66 // Scan device RAM for injected provisioning data and save to persistent storage if found.
67 err = factoryProv.ProvisionDeviceFromRAM(kDeviceRAMStart, kDeviceRAMEnd);
70 #endif // CHIP_DEVICE_CONFIG_ENABLE_FACTORY_PROVISIONING
72 // If the fail-safe was armed when the device last shutdown, initiate a factory reset.
73 if (_GetFailSafeArmed(failSafeArmed) == CHIP_NO_ERROR && failSafeArmed)
75 ChipLogProgress(DeviceLayer, "Detected fail-safe armed on reboot; initiating factory reset");
76 _InitiateFactoryReset();
85 void ConfigurationManagerImpl::_InitiateFactoryReset()
87 PlatformMgr().ScheduleWork(DoFactoryReset);
90 void ConfigurationManagerImpl::DoFactoryReset(intptr_t arg)
92 ChipLogProgress(DeviceLayer, "Performing factory reset");
93 const CHIP_ERROR err = FactoryResetConfig();
95 if (err != CHIP_NO_ERROR)
96 ChipLogError(DeviceLayer, "FactoryResetConfig() failed: %s", ErrorStr(err));
98 #if CHIP_DEVICE_CONFIG_ENABLE_THREAD
99 ThreadStackMgr().ErasePersistentInfo();
100 #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD
103 sys_reboot(SYS_REBOOT_WARM);
107 } // namespace DeviceLayer