Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / K32W / ConfigurationManagerImpl.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2020 Nest Labs, Inc.
5  *    All rights reserved.
6  *
7  *    Licensed under the Apache License, Version 2.0 (the "License");
8  *    you may not use this file except in compliance with the License.
9  *    You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *    Unless required by applicable law or agreed to in writing, software
14  *    distributed under the License is distributed on an "AS IS" BASIS,
15  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *    See the License for the specific language governing permissions and
17  *    limitations under the License.
18  */
19
20 /**
21  *    @file
22  *          Provides the implementation of the Device Layer ConfigurationManager object
23  *          for K32W platforms using the NXP SDK.
24  */
25
26 /* this file behaves like a config.h, comes first */
27 #include <platform/internal/CHIPDeviceLayerInternal.h>
28
29 #include <platform/ConfigurationManager.h>
30 #include <platform/K32W/K32WConfig.h>
31 #include <platform/internal/GenericConfigurationManagerImpl.cpp>
32
33 #include "fsl_reset.h"
34
35 namespace chip {
36 namespace DeviceLayer {
37
38 using namespace ::chip::DeviceLayer::Internal;
39
40 // TODO: Define a Singleton instance of CHIP Group Key Store here
41
42 /** Singleton instance of the ConfigurationManager implementation object.
43  */
44 ConfigurationManagerImpl ConfigurationManagerImpl::sInstance;
45
46 CHIP_ERROR ConfigurationManagerImpl::_Init()
47 {
48     CHIP_ERROR err;
49     bool failSafeArmed;
50
51     // Initialize the generic implementation base class.
52     err = Internal::GenericConfigurationManagerImpl<ConfigurationManagerImpl>::_Init();
53     SuccessOrExit(err);
54
55     // TODO: Initialize the global GroupKeyStore object here
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
85     err = ReadConfigValueCounter(persistedStorageKey, value);
86     if (err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND)
87     {
88         err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
89     }
90     SuccessOrExit(err);
91
92 exit:
93     return err;
94 }
95
96 CHIP_ERROR ConfigurationManagerImpl::_WritePersistedStorageValue(::chip::Platform::PersistedStorage::Key persistedStorageKey,
97                                                                  uint32_t value)
98 {
99     // This method reads Chip Persisted Counter type nvm3 objects.
100     // (where persistedStorageKey represents an index to the counter).
101     CHIP_ERROR err;
102
103     err = WriteConfigValueCounter(persistedStorageKey, value);
104     if (err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND)
105     {
106         err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
107     }
108     SuccessOrExit(err);
109
110 exit:
111     return err;
112 }
113
114 void ConfigurationManagerImpl::DoFactoryReset(intptr_t arg)
115 {
116     CHIP_ERROR err;
117
118     ChipLogProgress(DeviceLayer, "Performing factory reset");
119
120     err = FactoryResetConfig();
121     if (err != CHIP_NO_ERROR)
122     {
123         ChipLogError(DeviceLayer, "FactoryResetConfig() failed: %s", ErrorStr(err));
124     }
125
126 #if CHIP_DEVICE_CONFIG_ENABLE_THREAD
127
128     ThreadStackMgr().ErasePersistentInfo();
129
130 #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD
131
132     // Restart the system.
133     ChipLogProgress(DeviceLayer, "System restarting");
134     RESET_SystemReset();
135 }
136
137 } // namespace DeviceLayer
138 } // namespace chip