Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / Zephyr / ConfigurationManagerImpl.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 /**
19  *    @file
20  *          Provides the implementation of the Device Layer ConfigurationManager object
21  *          for Zephyr platforms.
22  */
23
24 #include <platform/internal/CHIPDeviceLayerInternal.h>
25
26 #include <platform/ConfigurationManager.h>
27 #include <platform/internal/GenericConfigurationManagerImpl.cpp>
28
29 #include <core/CHIPVendorIdentifiers.hpp>
30 #include <platform/Zephyr/ZephyrConfig.h>
31
32 #if CHIP_DEVICE_CONFIG_ENABLE_FACTORY_PROVISIONING
33 #include <platform/internal/FactoryProvisioning.cpp>
34 #endif // CHIP_DEVICE_CONFIG_ENABLE_FACTORY_PROVISIONING
35
36 #include <support/CodeUtils.h>
37 #include <support/logging/CHIPLogging.h>
38
39 #include <power/reboot.h>
40
41 namespace chip {
42 namespace DeviceLayer {
43
44 using namespace ::chip::DeviceLayer::Internal;
45
46 /** Singleton instance of the ConfigurationManager implementation object.
47  */
48 ConfigurationManagerImpl ConfigurationManagerImpl::sInstance;
49
50 CHIP_ERROR ConfigurationManagerImpl::_Init()
51 {
52     CHIP_ERROR err;
53     bool failSafeArmed;
54
55     // Initialize the generic implementation base class.
56     err = Internal::GenericConfigurationManagerImpl<ConfigurationManagerImpl>::_Init();
57     SuccessOrExit(err);
58
59     // TODO: Initialize the global GroupKeyStore object here
60 #if CHIP_DEVICE_CONFIG_ENABLE_FACTORY_PROVISIONING
61     {
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;
65
66         // Scan device RAM for injected provisioning data and save to persistent storage if found.
67         err = factoryProv.ProvisionDeviceFromRAM(kDeviceRAMStart, kDeviceRAMEnd);
68         SuccessOrExit(err);
69     }
70 #endif // CHIP_DEVICE_CONFIG_ENABLE_FACTORY_PROVISIONING
71
72     // If the fail-safe was armed when the device last shutdown, initiate a factory reset.
73     if (_GetFailSafeArmed(failSafeArmed) == CHIP_NO_ERROR && failSafeArmed)
74     {
75         ChipLogProgress(DeviceLayer, "Detected fail-safe armed on reboot; initiating factory reset");
76         _InitiateFactoryReset();
77     }
78
79     err = CHIP_NO_ERROR;
80
81 exit:
82     return err;
83 }
84
85 void ConfigurationManagerImpl::_InitiateFactoryReset()
86 {
87     PlatformMgr().ScheduleWork(DoFactoryReset);
88 }
89
90 void ConfigurationManagerImpl::DoFactoryReset(intptr_t arg)
91 {
92     ChipLogProgress(DeviceLayer, "Performing factory reset");
93     const CHIP_ERROR err = FactoryResetConfig();
94
95     if (err != CHIP_NO_ERROR)
96         ChipLogError(DeviceLayer, "FactoryResetConfig() failed: %s", ErrorStr(err));
97
98 #if CHIP_DEVICE_CONFIG_ENABLE_THREAD
99     ThreadStackMgr().ErasePersistentInfo();
100 #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD
101
102 #if CONFIG_REBOOT
103     sys_reboot(SYS_REBOOT_WARM);
104 #endif
105 }
106
107 } // namespace DeviceLayer
108 } // namespace chip