Skip to content

Commit

Permalink
[ESP32] Update factory reset logic and introduced MatterDataReset API
Browse files Browse the repository at this point in the history
  • Loading branch information
jadhavrohit924 committed Oct 31, 2023
1 parent 2af62c5 commit 4fe5a23
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 45 deletions.
11 changes: 11 additions & 0 deletions examples/platform/esp32/common/CommonDeviceCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ void CommonDeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, i
chip::app::DnssdServer::Instance().StartServer();
}
break;

case DeviceEventType::PublicEventTypes::kFactoryReset: {
ESP_LOGI(TAG, "App performing factory reset");
}
break;

case DeviceEventType::PublicEventTypes::kMatterDataReset: {
ESP_LOGI(TAG, "App performing matter data reset");
}
break;

}

ESP_LOGI(TAG, "Current free heap: %u\n", static_cast<unsigned int>(heap_caps_get_free_size(MALLOC_CAP_8BIT)));
Expand Down
12 changes: 12 additions & 0 deletions src/include/platform/CHIPDeviceEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,18 @@ enum PublicEventTypes
* sending messages to other nodes.
*/
kServerReady,

/**
* This is an event for the application to erase application data
* occures when device is going to factory reset.
*/
kFactoryReset,

/**
* This is an event for the application to erase application data
* occures when device is going to reset matter data.
*/
kMatterDataReset,
};

/**
Expand Down
163 changes: 118 additions & 45 deletions src/platform/ESP32/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ using namespace ::chip::DeviceLayer::Internal;

namespace {

const uint32_t secToMiliSec = 1000;

enum
{
kChipProduct_Connect = 0x0016
Expand Down Expand Up @@ -326,9 +328,124 @@ bool ConfigurationManagerImpl::CanFactoryReset()
return true;
}

void ConfigurationManagerImpl::TimerRebootCallback(chip::System::Layer* systemLayer, void* data) {
esp_restart();
}

void ConfigurationManagerImpl::DoFactoryReset(intptr_t arg)
{
CHIP_ERROR err;
uint8_t rebootSeconds = static_cast<uint8_t>(arg);

ChipLogProgress(DeviceLayer, "Performing complete factory reset");

// Erase all values in the chip-config NVS namespace.
err = ESP32Config::ClearNamespace(ESP32Config::kConfigNamespace_ChipConfig);
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "ClearNamespace(ChipConfig) failed: %s", chip::ErrorStr(err));
}

// Erase all values in the chip-counters NVS namespace.
err = ESP32Config::ClearNamespace(ESP32Config::kConfigNamespace_ChipCounters);
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "ClearNamespace(ChipCounters) failed: %s", chip::ErrorStr(err));
}

// Restore WiFi persistent settings to default values.
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
esp_err_t error = esp_wifi_restore();
if (error != ESP_OK)
{
ChipLogError(DeviceLayer, "esp_wifi_restore() failed: %s", esp_err_to_name(error));
}
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
ThreadStackMgr().ErasePersistentInfo();
#endif

// Erase all key-values including fabric info.
err = PersistedStorage::KeyValueStoreMgrImpl().EraseAll();
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Clear Key-Value Storage failed");
}

nvs_flash_deinit();
nvs_flash_erase();

// Restart the system.
ChipLogProgress(DeviceLayer, "System restarting in %u seconds.", rebootSeconds);
DeviceLayer::SystemLayer().StartTimer(static_cast<System::Clock::Timeout> (rebootSeconds * secToMiliSec), TimerRebootCallback, NULL);
}

void ConfigurationManagerImpl::DoMatterDataReset(intptr_t arg)
{
CHIP_ERROR err;

ChipLogProgress(DeviceLayer, "Performing factory reset without erasing network credentials");

// Erase all values in the chip-config NVS namespace.
err = ESP32Config::ClearNamespace(ESP32Config::kConfigNamespace_ChipConfig);
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "ClearNamespace(ChipConfig) failed: %s", chip::ErrorStr(err));
}

// Erase all values in the chip-counters NVS namespace.
err = ESP32Config::ClearNamespace(ESP32Config::kConfigNamespace_ChipCounters);
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "ClearNamespace(ChipCounters) failed: %s", chip::ErrorStr(err));
}

// Erase all key-values including fabric info.
err = PersistedStorage::KeyValueStoreMgrImpl().EraseAll();
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Clear Key-Value Storage failed");
}

}

void ConfigurationManagerImpl::TimerResetCallback(chip::System::Layer* systemLayer, void* data) {
uint8_t rebootSeconds = *static_cast<uint8_t*>(data);
PlatformMgr().ScheduleWork(DoFactoryReset, static_cast<intptr_t>(rebootSeconds));
}

void ConfigurationManagerImpl::StartFactoryReset(uint8_t resetSeconds = 3, uint8_t rebootSeconds = 3)
{
resetDelay = resetSeconds;
rebootDelay = rebootSeconds;
ChipDeviceEvent event;
event.Type = DeviceEventType::PublicEventTypes::kFactoryReset;
CHIP_ERROR err = PlatformMgr().PostEvent(&event);
if(err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Failed to post factory reset event");
}
ChipLogProgress(DeviceLayer, "Performing complete factory reset in %u seconds", resetDelay);
DeviceLayer::SystemLayer().StartTimer(static_cast<System::Clock::Timeout> (resetDelay * secToMiliSec), TimerResetCallback, &rebootDelay);
}

void ConfigurationManagerImpl::StartMatterReset(uint8_t resetSeconds = 3)
{
resetDelay = resetSeconds;
ChipDeviceEvent event;
event.Type = DeviceEventType::PublicEventTypes::kMatterDataReset;
CHIP_ERROR err = PlatformMgr().PostEvent(&event);
if(err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Failed to post matter data reset event");
}
ChipLogProgress(DeviceLayer, "Performing matter reset in %u seconds", resetDelay);
DeviceLayer::SystemLayer().StartTimer(static_cast<System::Clock::Timeout> (resetDelay), NULL, NULL);
}

void ConfigurationManagerImpl::InitiateFactoryReset()
{
PlatformMgr().ScheduleWork(DoFactoryReset);
StartFactoryReset();
}

CHIP_ERROR ConfigurationManagerImpl::ReadPersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t & value)
Expand Down Expand Up @@ -409,50 +526,6 @@ void ConfigurationManagerImpl::RunConfigUnitTest(void)
ESP32Config::RunConfigUnitTest();
}

void ConfigurationManagerImpl::DoFactoryReset(intptr_t arg)
{
CHIP_ERROR err;

ChipLogProgress(DeviceLayer, "Performing factory reset");

// Erase all values in the chip-config NVS namespace.
err = ESP32Config::ClearNamespace(ESP32Config::kConfigNamespace_ChipConfig);
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "ClearNamespace(ChipConfig) failed: %s", chip::ErrorStr(err));
}

// Erase all values in the chip-counters NVS namespace.
err = ESP32Config::ClearNamespace(ESP32Config::kConfigNamespace_ChipCounters);
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "ClearNamespace(ChipCounters) failed: %s", chip::ErrorStr(err));
}

// Restore WiFi persistent settings to default values.
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
esp_err_t error = esp_wifi_restore();
if (error != ESP_OK)
{
ChipLogError(DeviceLayer, "esp_wifi_restore() failed: %s", esp_err_to_name(error));
}
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
ThreadStackMgr().ErasePersistentInfo();
#endif

// Erase all key-values including fabric info.
err = PersistedStorage::KeyValueStoreMgrImpl().EraseAll();
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Clear Key-Value Storage failed");
}

// Restart the system.
ChipLogProgress(DeviceLayer, "System restarting");
esp_restart();
}

ConfigurationManager & ConfigurationMgrImpl()
{
return ConfigurationManagerImpl::GetDefaultInstance();
Expand Down
7 changes: 7 additions & 0 deletions src/platform/ESP32/ConfigurationManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImp
private:
// ===== Members that implement the ConfigurationManager public interface.

uint8_t resetDelay = 3;
uint8_t rebootDelay = 3;
CHIP_ERROR Init(void) override;
#if CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
CHIP_ERROR GetPrimaryMACAddress(MutableByteSpan buf) override;
Expand Down Expand Up @@ -96,6 +98,11 @@ class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImp
// ===== Private members reserved for use by this class only.

static void DoFactoryReset(intptr_t arg);
static void DoMatterDataReset(intptr_t arg);
static void TimerResetCallback(chip::System::Layer* systemLayer, void* data);
static void TimerRebootCallback(chip::System::Layer* systemLayer, void* data);
void StartFactoryReset(uint8_t resetSeconds, uint8_t rebootSeconds);
void StartMatterReset(uint8_t resetSeconds);
};

/**
Expand Down

0 comments on commit 4fe5a23

Please sign in to comment.