Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / qpg6100 / 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 QPG6100 platforms .
22  */
23 /* this file behaves like a config.h, comes first */
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/qpg6100/qpg6100Config.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 namespace chip {
40 namespace DeviceLayer {
41
42 using namespace ::chip::DeviceLayer::Internal;
43
44 /** Singleton instance of the ConfigurationManager implementation object.
45  */
46 ConfigurationManagerImpl ConfigurationManagerImpl::sInstance;
47
48 CHIP_ERROR ConfigurationManagerImpl::_Init()
49 {
50     CHIP_ERROR err;
51     bool failSafeArmed;
52
53     // Initialize the generic implementation base class.
54     err = Internal::GenericConfigurationManagerImpl<ConfigurationManagerImpl>::_Init();
55     SuccessOrExit(err);
56
57     // If the fail-safe was armed when the device last shutdown, initiate a factory reset.
58     if (_GetFailSafeArmed(failSafeArmed) == CHIP_NO_ERROR && failSafeArmed)
59     {
60         ChipLogProgress(DeviceLayer, "Detected fail-safe armed on reboot; initiating factory reset");
61         _InitiateFactoryReset();
62     }
63     err = CHIP_NO_ERROR;
64
65 exit:
66     return err;
67 }
68
69 bool ConfigurationManagerImpl::_CanFactoryReset()
70 {
71     // TODO: query the application to determine if factory reset is allowed.
72     return true;
73 }
74
75 void ConfigurationManagerImpl::_InitiateFactoryReset()
76 {
77     PlatformMgr().ScheduleWork(DoFactoryReset);
78 }
79
80 CHIP_ERROR ConfigurationManagerImpl::_ReadPersistedStorageValue(::chip::Platform::PersistedStorage::Key persistedStorageKey,
81                                                                 uint32_t & value)
82 {
83     CHIP_ERROR err;
84     uintmax_t recordKey = persistedStorageKey + kConfigKey_GroupKeyBase;
85
86     VerifyOrExit(recordKey <= kConfigKey_GroupKeyMax, err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND);
87
88     err = ReadConfigValue(persistedStorageKey, value);
89     if (err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND)
90     {
91         err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
92     }
93     SuccessOrExit(err);
94
95 exit:
96     return err;
97 }
98
99 CHIP_ERROR ConfigurationManagerImpl::_WritePersistedStorageValue(::chip::Platform::PersistedStorage::Key persistedStorageKey,
100                                                                  uint32_t value)
101 {
102     CHIP_ERROR err;
103
104     uintmax_t recordKey = persistedStorageKey + kConfigKey_GroupKeyBase;
105
106     VerifyOrExit(recordKey <= kConfigKey_GroupKeyMax, err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND);
107
108     err = WriteConfigValue(persistedStorageKey, value);
109     SuccessOrExit(err);
110
111 exit:
112     return err;
113 }
114
115 void ConfigurationManagerImpl::DoFactoryReset(intptr_t arg)
116 {
117     CHIP_ERROR err;
118
119     ChipLogProgress(DeviceLayer, "Performing factory reset");
120
121     err = FactoryResetConfig();
122     if (err != CHIP_NO_ERROR)
123     {
124         ChipLogError(DeviceLayer, "FactoryResetConfig() failed: %s", ErrorStr(err));
125     }
126
127 #if CHIP_DEVICE_CONFIG_ENABLE_THREAD
128
129     ChipLogProgress(DeviceLayer, "Clearing Thread provision");
130     ThreadStackMgr().ErasePersistentInfo();
131
132 #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD
133
134     // Restart the system.
135     ChipLogProgress(DeviceLayer, "System restarting");
136     qvCHIP_ResetSystem();
137 }
138
139 } // namespace DeviceLayer
140 } // namespace chip