Skip to content

Commit

Permalink
[Telink] Turn off the CHIP_FACTORY_RESET_ERASE_NVS by Default (#28947)
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiiSalamakha authored Sep 1, 2023
1 parent 1ed3897 commit 5ccb774
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 36 deletions.
2 changes: 1 addition & 1 deletion config/telink/chip-module/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ endif #CHIP_FACTORY_DATA_BUILD
# See config/zephyr/Kconfig for full definition
config CHIP_FACTORY_RESET_ERASE_NVS
bool
default y
default n

config CHIP_LOG_SIZE_OPTIMIZATION
bool "Disable some detailed logs to decrease flash usage"
Expand Down
60 changes: 25 additions & 35 deletions examples/platform/telink/common/src/AppTaskCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@
#include <app/InteractionModelEngine.h>
#endif

#ifdef CONFIG_CHIP_FACTORY_RESET_ERASE_NVS
#include <zephyr/fs/nvs.h>
#include <zephyr/settings/settings.h>
#endif

using namespace chip::app;

Expand Down Expand Up @@ -103,10 +101,11 @@ Button sThreadStartButton;
k_timer sFactoryResetTimer;
uint8_t sFactoryResetCntr = 0;

bool sIsThreadProvisioned = false;
bool sIsThreadEnabled = false;
bool sIsThreadAttached = false;
bool sHaveBLEConnections = false;
bool sIsCommissioningFailed = false;
bool sIsThreadProvisioned = false;
bool sIsThreadEnabled = false;
bool sIsThreadAttached = false;
bool sHaveBLEConnections = false;

#if APP_SET_DEVICE_INFO_PROVIDER
chip::DeviceLayer::DeviceInfoProviderImpl gExampleDeviceInfoProvider;
Expand Down Expand Up @@ -138,9 +137,10 @@ class AppCallbacks : public AppDelegate
bool isComissioningStarted;

public:
void OnCommissioningSessionEstablishmentStarted() {}
void OnCommissioningSessionEstablishmentStarted() override { sIsCommissioningFailed = false; }
void OnCommissioningSessionStarted() override { isComissioningStarted = true; }
void OnCommissioningSessionStopped() override { isComissioningStarted = false; }
void OnCommissioningSessionEstablishmentError(CHIP_ERROR err) override { sIsCommissioningFailed = true; }
void OnCommissioningWindowClosed() override
{
if (!isComissioningStarted)
Expand All @@ -157,51 +157,41 @@ class AppFabricTableDelegate : public FabricTable::Delegate
{
if (chip::Server::GetInstance().GetFabricTable().FabricCount() == 0)
{
bool isBasicCommissioningMode = chip::Server::GetInstance().GetCommissioningWindowManager().GetCommissioningMode() ==
Dnssd::CommissioningMode::kEnabledBasic;
ChipLogProgress(DeviceLayer, "Performing erasing of settings partition");

#ifdef CONFIG_CHIP_FACTORY_RESET_ERASE_NVS
void * storage = nullptr;
int status = settings_storage_get(&storage);

if (status == 0)
// Do FactoryReset in case of failed commissioning to allow new pairing via BLE
if (sIsCommissioningFailed)
{
status = nvs_clear(static_cast<nvs_fs *>(storage));
chip::Server::GetInstance().ScheduleFactoryReset();
}

if (!isBasicCommissioningMode)
// TC-OPCREDS-3.6 (device doesn't need to reboot automatically after the last fabric is removed) can't use FactoryReset
else
{
void * storage = nullptr;
int status = settings_storage_get(&storage);

if (!status)
{
status = nvs_mount(static_cast<nvs_fs *>(storage));
status = nvs_clear(static_cast<nvs_fs *>(storage));
}
}

if (status)
{
ChipLogError(DeviceLayer, "Storage clearance failed: %d", status);
}
#else
const CHIP_ERROR err = PersistedStorage::KeyValueStoreMgrImpl().DoFactoryReset();

if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Factory reset failed: %" CHIP_ERROR_FORMAT, err.Format());
}
if (!status)
{
status = nvs_mount(static_cast<nvs_fs *>(storage));
}

ConnectivityMgr().ErasePersistentInfo();
#endif
if (isBasicCommissioningMode)
{
PlatformMgr().Shutdown();
if (status)
{
ChipLogError(DeviceLayer, "Storage clearance failed: %d", status);
}
}
}
}
};

class PlatformMgrDelegate : public DeviceLayer::PlatformManagerDelegate
{
// Disable openthread before reset to prevent writing to NVS
void OnShutDown() override
{
if (ThreadStackManagerImpl().IsThreadEnabled())
Expand Down

0 comments on commit 5ccb774

Please sign in to comment.