Skip to content

Commit

Permalink
Add a generalised server API for MatterDataReset and emit event from …
Browse files Browse the repository at this point in the history
…the server
  • Loading branch information
jadhavrohit924 committed Jan 18, 2024
1 parent cafc7ae commit 87e754c
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 19 deletions.
29 changes: 29 additions & 0 deletions src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,11 +515,40 @@ void Server::ScheduleFactoryReset()
PlatformMgr().ScheduleWork([](intptr_t) {
// Delete all fabrics and emit Leave event.
GetInstance().GetFabricTable().DeleteAllFabrics();
if (GetInstance().GetFabricTable().FabricCount() == 0)
{
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");
}
}
PlatformMgr().HandleServerShuttingDown();
ConfigurationMgr().InitiateFactoryReset();
});
}

void Server::ScheduleMatterDataReset()
{
PlatformMgr().ScheduleWork([](intptr_t) {
// Delete all fabrics and emit Leave event.
GetInstance().GetFabricTable().DeleteAllFabrics();
if (GetInstance().GetFabricTable().FabricCount() == 0)
{
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");
}
}
ConfigurationMgr().InitiateMatterDataReset();
});
}

void Server::Shutdown()
{
assertChipStackLockedByCurrentThread();
Expand Down
2 changes: 2 additions & 0 deletions src/app/server/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ class Server

void ScheduleFactoryReset();

void ScheduleMatterDataReset();

System::Clock::Microseconds64 TimeSinceInit() const
{
return System::SystemClock().GetMonotonicMicroseconds64() - mInitTimestamp;
Expand Down
2 changes: 1 addition & 1 deletion src/include/platform/CHIPDeviceEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ enum PublicEventTypes
kFactoryReset,

/**
* Signals the application that device is going to reset matter data.
* Signals the application that device is going to reset matter data (on last fabric removed).
*/
kMatterDataReset,
};
Expand Down
5 changes: 3 additions & 2 deletions src/include/platform/ConfigurationManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ class ConfigurationManager
virtual void RunUnitTests() = 0;
#endif

virtual bool IsFullyProvisioned() = 0;
virtual void InitiateFactoryReset() = 0;
virtual bool IsFullyProvisioned() = 0;
virtual void InitiateFactoryReset() = 0;
virtual void InitiateMatterDataReset() = 0;

// Gets called when starting BLE/DNS-SD advertisement
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
Expand Down
16 changes: 1 addition & 15 deletions src/platform/ESP32/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ void ConfigurationManagerImpl::DoMatterDataReset(intptr_t arg)
{
CHIP_ERROR err;

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

// Erase all values in the chip-config NVS namespace.
err = ESP32Config::ClearNamespace(ESP32Config::kConfigNamespace_ChipConfig);
Expand Down Expand Up @@ -383,25 +383,11 @@ void ConfigurationManagerImpl::DoFactoryReset(intptr_t arg)

void ConfigurationManagerImpl::InitiateMatterDataReset()
{
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");
}
PlatformMgr().ScheduleWork(DoMatterDataReset);
}

void ConfigurationManagerImpl::InitiateFactoryReset()
{
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");
}
PlatformMgr().ScheduleWork(DoFactoryReset);
}

Expand Down
2 changes: 1 addition & 1 deletion src/platform/ESP32/ConfigurationManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImp
void InitiateFactoryReset(void) override;

// InitiateMatterDataReset erases matter data only (chip config, counters, kvs namespaces).
void InitiateMatterDataReset(void);
void InitiateMatterDataReset(void) override;

CHIP_ERROR MapConfigError(esp_err_t error);
CHIP_ERROR ReadPersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t & value) override;
Expand Down

0 comments on commit 87e754c

Please sign in to comment.