Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / cc13x2_26x2 / ConfigurationManagerImpl.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2020 Texas Instruments Incorporated
5  *
6  *    Licensed under the Apache License, Version 2.0 (the "License");
7  *    you may not use this file except in compliance with the License.
8  *    You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *    Unless required by applicable law or agreed to in writing, software
13  *    distributed under the License is distributed on an "AS IS" BASIS,
14  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *    See the License for the specific language governing permissions and
16  *    limitations under the License.
17  */
18
19 /**
20  *    @file
21  *          Provides the implementation of the Device Layer ConfigurationManager object
22  *          for the Texas Instruments CC1352 platform.
23  *
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/internal/GenericConfigurationManagerImpl.cpp>
31
32 #include <core/CHIPVendorIdentifiers.hpp>
33 #include <platform/cc13x2_26x2/CC13X2_26X2Config.h>
34
35 #include <support/CodeUtils.h>
36 #include <support/logging/CHIPLogging.h>
37
38 #pragma GCC diagnostic push
39 #pragma GCC diagnostic ignored "-Wconversion"
40 // BSP header cannot handle conversions
41
42 // clang-format off
43 // `/` is a path delimiter, not the division operation
44
45 #include <ti/devices/DeviceFamily.h>
46 #include DeviceFamily_constructPath(driverlib/sys_ctrl.h)
47
48 // clang-format on
49
50 #pragma GCC diagnostic pop
51
52 namespace chip {
53 namespace DeviceLayer {
54
55 using namespace ::chip::DeviceLayer::Internal;
56
57 /** Singleton instance of the ConfigurationManager implementation object.
58  */
59 ConfigurationManagerImpl ConfigurationManagerImpl::sInstance;
60
61 CHIP_ERROR ConfigurationManagerImpl::_Init()
62 {
63     CHIP_ERROR err;
64     bool failSafeArmed;
65
66     // If the fail-safe was armed when the device last shutdown, initiate a factory reset.
67     if (_GetFailSafeArmed(failSafeArmed) == CHIP_NO_ERROR && failSafeArmed)
68     {
69         ChipLogProgress(DeviceLayer, "Detected fail-safe armed on reboot; initiating factory reset");
70         _InitiateFactoryReset();
71     }
72     err = CHIP_NO_ERROR;
73
74     return err;
75 }
76
77 bool ConfigurationManagerImpl::_CanFactoryReset()
78 {
79     // TODO: query the application to determine if factory reset is allowed.
80     return true;
81 }
82
83 void ConfigurationManagerImpl::_InitiateFactoryReset()
84 {
85     PlatformMgr().ScheduleWork(DoFactoryReset);
86 }
87
88 CHIP_ERROR ConfigurationManagerImpl::_ReadPersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t & value)
89 {
90     CC13X2_26X2Config::Key configKey{ { kCC13X2_26X2ChipCounters_Sysid, key } };
91
92     CHIP_ERROR err = ReadConfigValue(configKey, value);
93     if (err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND)
94     {
95         err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
96     }
97     return err;
98 }
99
100 CHIP_ERROR ConfigurationManagerImpl::_WritePersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t value)
101 {
102     CC13X2_26X2Config::Key configKey{ { kCC13X2_26X2ChipCounters_Sysid, key } };
103     return WriteConfigValue(configKey, value);
104 }
105
106 void ConfigurationManagerImpl::DoFactoryReset(intptr_t arg)
107 {
108     CHIP_ERROR err;
109
110     ChipLogProgress(DeviceLayer, "Performing factory reset");
111
112     err = FactoryResetConfig();
113     if (err != CHIP_NO_ERROR)
114     {
115         ChipLogError(DeviceLayer, "FactoryResetConfig() failed: %s", ErrorStr(err));
116     }
117
118 #if CHIP_DEVICE_CONFIG_ENABLE_THREAD
119
120     ChipLogProgress(DeviceLayer, "Clearing Thread provision");
121     ThreadStackMgr().ErasePersistentInfo();
122
123 #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD
124
125     // Restart the system.
126     ChipLogProgress(DeviceLayer, "System restarting");
127     // There should probably be an OS API for this
128     SysCtrlSystemReset();
129 }
130
131 } // namespace DeviceLayer
132 } // namespace chip