blob: acf7b132fad9e1bfbb1dc7f142beca715869dd67 [file] [log] [blame]
/*
*
* Copyright (c) 2020 Project CHIP Authors
* Copyright (c) 2020 Texas Instruments Incorporated
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file
* Provides the implementation of the Device Layer ConfigurationManager object
* for the Texas Instruments CC1352 platform.
*
*/
/* this file behaves like a config.h, comes first */
#include <platform/internal/CHIPDeviceLayerInternal.h>
#include <platform/ConfigurationManager.h>
#include <platform/internal/GenericConfigurationManagerImpl.cpp>
#include <lib/core/CHIPVendorIdentifiers.hpp>
#include <platform/cc13x2_26x2/CC13X2_26X2Config.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
// BSP header cannot handle conversions
// clang-format off
// `/` is a path delimiter, not the division operation
#include <ti/devices/DeviceFamily.h>
#include DeviceFamily_constructPath(driverlib/sys_ctrl.h)
// clang-format on
#pragma GCC diagnostic pop
namespace chip {
namespace DeviceLayer {
using namespace ::chip::DeviceLayer::Internal;
/** Singleton instance of the ConfigurationManager implementation object.
*/
ConfigurationManagerImpl ConfigurationManagerImpl::sInstance;
CHIP_ERROR ConfigurationManagerImpl::_Init()
{
CHIP_ERROR err;
bool failSafeArmed;
// If the fail-safe was armed when the device last shutdown, initiate a factory reset.
if (_GetFailSafeArmed(failSafeArmed) == CHIP_NO_ERROR && failSafeArmed)
{
ChipLogProgress(DeviceLayer, "Detected fail-safe armed on reboot; initiating factory reset");
_InitiateFactoryReset();
}
err = CHIP_NO_ERROR;
return err;
}
bool ConfigurationManagerImpl::_CanFactoryReset()
{
// TODO: query the application to determine if factory reset is allowed.
return true;
}
void ConfigurationManagerImpl::_InitiateFactoryReset()
{
PlatformMgr().ScheduleWork(DoFactoryReset);
}
CHIP_ERROR ConfigurationManagerImpl::_ReadPersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t & value)
{
CC13X2_26X2Config::Key configKey{ { kCC13X2_26X2ChipCounters_Sysid, key } };
CHIP_ERROR err = ReadConfigValue(configKey, value);
if (err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND)
{
err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
}
return err;
}
CHIP_ERROR ConfigurationManagerImpl::_WritePersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t value)
{
CC13X2_26X2Config::Key configKey{ { kCC13X2_26X2ChipCounters_Sysid, key } };
return WriteConfigValue(configKey, value);
}
void ConfigurationManagerImpl::DoFactoryReset(intptr_t arg)
{
CHIP_ERROR err;
ChipLogProgress(DeviceLayer, "Performing factory reset");
err = FactoryResetConfig();
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "FactoryResetConfig() failed: %s", ErrorStr(err));
}
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
ChipLogProgress(DeviceLayer, "Clearing Thread provision");
ThreadStackMgr().ErasePersistentInfo();
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD
// Restart the system.
ChipLogProgress(DeviceLayer, "System restarting");
// There should probably be an OS API for this
SysCtrlSystemReset();
}
} // namespace DeviceLayer
} // namespace chip