3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2020 Texas Instruments Incorporated
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * Provides the implementation of the Device Layer ConfigurationManager object
22 * for the Texas Instruments CC1352 platform.
26 /* this file behaves like a config.h, comes first */
27 #include <platform/internal/CHIPDeviceLayerInternal.h>
29 #include <platform/ConfigurationManager.h>
30 #include <platform/internal/GenericConfigurationManagerImpl.cpp>
32 #include <core/CHIPVendorIdentifiers.hpp>
33 #include <platform/cc13x2_26x2/CC13X2_26X2Config.h>
35 #include <support/CodeUtils.h>
36 #include <support/logging/CHIPLogging.h>
38 #pragma GCC diagnostic push
39 #pragma GCC diagnostic ignored "-Wconversion"
40 // BSP header cannot handle conversions
43 // `/` is a path delimiter, not the division operation
45 #include <ti/devices/DeviceFamily.h>
46 #include DeviceFamily_constructPath(driverlib/sys_ctrl.h)
50 #pragma GCC diagnostic pop
53 namespace DeviceLayer {
55 using namespace ::chip::DeviceLayer::Internal;
57 /** Singleton instance of the ConfigurationManager implementation object.
59 ConfigurationManagerImpl ConfigurationManagerImpl::sInstance;
61 CHIP_ERROR ConfigurationManagerImpl::_Init()
66 // If the fail-safe was armed when the device last shutdown, initiate a factory reset.
67 if (_GetFailSafeArmed(failSafeArmed) == CHIP_NO_ERROR && failSafeArmed)
69 ChipLogProgress(DeviceLayer, "Detected fail-safe armed on reboot; initiating factory reset");
70 _InitiateFactoryReset();
77 bool ConfigurationManagerImpl::_CanFactoryReset()
79 // TODO: query the application to determine if factory reset is allowed.
83 void ConfigurationManagerImpl::_InitiateFactoryReset()
85 PlatformMgr().ScheduleWork(DoFactoryReset);
88 CHIP_ERROR ConfigurationManagerImpl::_ReadPersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t & value)
90 CC13X2_26X2Config::Key configKey{ { kCC13X2_26X2ChipCounters_Sysid, key } };
92 CHIP_ERROR err = ReadConfigValue(configKey, value);
93 if (err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND)
95 err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
100 CHIP_ERROR ConfigurationManagerImpl::_WritePersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t value)
102 CC13X2_26X2Config::Key configKey{ { kCC13X2_26X2ChipCounters_Sysid, key } };
103 return WriteConfigValue(configKey, value);
106 void ConfigurationManagerImpl::DoFactoryReset(intptr_t arg)
110 ChipLogProgress(DeviceLayer, "Performing factory reset");
112 err = FactoryResetConfig();
113 if (err != CHIP_NO_ERROR)
115 ChipLogError(DeviceLayer, "FactoryResetConfig() failed: %s", ErrorStr(err));
118 #if CHIP_DEVICE_CONFIG_ENABLE_THREAD
120 ChipLogProgress(DeviceLayer, "Clearing Thread provision");
121 ThreadStackMgr().ErasePersistentInfo();
123 #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD
125 // Restart the system.
126 ChipLogProgress(DeviceLayer, "System restarting");
127 // There should probably be an OS API for this
128 SysCtrlSystemReset();
131 } // namespace DeviceLayer