Skip to content

Commit

Permalink
samples: matter: disable mpsl before preforming factory reset
Browse files Browse the repository at this point in the history
We can speed up flash operations while performing a factory reset
by disabling mpsl before that.

Thanks to that flash operations do not wait for mpsl
synchronization and all write operations take less time.

Signed-off-by: Arkadiusz Balys <[email protected]>
  • Loading branch information
ArekBalysNordic committed Jan 3, 2025
1 parent a30fb62 commit 88ae0f3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
12 changes: 12 additions & 0 deletions doc/nrf/protocols/matter/getting_started/advanced_kconfigs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,18 @@ Additionally, in case of the secure storage backend, the following Kconfig optio
* :ref:`CONFIG_NCS_SAMPLE_MATTER_SECURE_STORAGE_MAX_ENTRY_NUMBER<CONFIG_NCS_SAMPLE_MATTER_SECURE_STORAGE_MAX_ENTRY_NUMBER>` - Defines the maximum number or assets that can be stored in the secure storage.
* :kconfig:option:`CONFIG_TRUSTED_STORAGE_BACKEND_AEAD_MAX_DATA_SIZE` - Defines the maximum length of the secret that is stored.

.. _ug_matter_persistent_storage_factory_reset:

Factory reset
-------------

The factory reset functionality allows you to reset the device to its factory settings and remove all non-volatile data related to Matter and network.
If you need to remove application-specific data, you must do it manually or use a dedicated callback function.
The default implementation of the factory reset callback is located in the :file:`ncs/nrf/samples/matter/common/src/app/matter_init.cpp` file and it disables the :ref:`mpsl` to speed up flash operations during the factory reset.
To define your own factory reset callback, implement and set it in the Configuration Manager module using the ``ConfigurationManagerImpl().SetFactoryResetDeviceCallback()`` method after initializing the Matter stack.
The Configuration Manager module implementation for Zephyr platform is located in the :file:`ncs/modules/lib/matter/src/platform/Zephyr/ConfigurationManagerImpl.h` file.
In the callback, you can remove application-specific data, such as specific Zephyr settings, and perform any actions needed before performing the factory reset and rebooting the device.

.. _ug_matter_configuration_diagnostic_logs:

Diagnostic logs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ Gazell
Matter
------

|no_changes_yet_note|
* Added the factory reset callback and described its usage in the :ref:`ug_matter_persistent_storage_factory_reset` section of the Matter documentation.
The default implementation of the factory reset callback disables the :ref:`mpsl` before performing flash operations to speed up the process.
The :ref:`mpsl` is also disabled during the last fabric removal process and is re-enabled after the flash operations are completed.

Matter fork
+++++++++++
Expand Down
9 changes: 9 additions & 0 deletions samples/matter/common/src/app/fabric_table_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <lib/support/logging/CHIPLogging.h>

#include "app/group_data_provider.h"
#include <mpsl/mpsl_lib.h>

#ifdef CONFIG_CHIP_WIFI
#include <platform/nrfconnect/wifi/WiFiManager.h>
Expand Down Expand Up @@ -57,10 +58,18 @@ class AppFabricTableDelegate : public chip::FabricTable::Delegate {
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
chip::DeviceLayer::ThreadStackMgr().ClearAllSrpHostAndServices();
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
#if !defined(CONFIG_SOC_SERIES_NRF53X) && !defined(CONFIG_SOC_SERIES_NRF54HX)
/* Disable mpsl before start removing settings to improve the performance. */
mpsl_lib_uninit();
#endif
/* Erase Matter data */
chip::DeviceLayer::PersistedStorage::KeyValueStoreMgrImpl().DoFactoryReset();
/* Erase Network credentials and disconnect */
chip::DeviceLayer::ConnectivityMgr().ErasePersistentInfo();
#if !defined(CONFIG_SOC_SERIES_NRF53X) && !defined(CONFIG_SOC_SERIES_NRF54HX)
/* Re-enable mpsl after clearing persistent data. */
mpsl_lib_init();
#endif
#ifdef CONFIG_CHIP_WIFI
chip::DeviceLayer::WiFiManager::Instance().Disconnect();
chip::DeviceLayer::ConnectivityMgr().ClearWiFiStationProvision();
Expand Down
10 changes: 10 additions & 0 deletions samples/matter/common/src/app/matter_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include <app/clusters/network-commissioning/network-commissioning.h>
#include <app/server/OnboardingCodesUtil.h>
#include <credentials/examples/DeviceAttestationCredsExample.h>
#include <mpsl/mpsl_lib.h>
#include <platform/nrfconnect/ExternalFlashManager.h>

LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL);
Expand Down Expand Up @@ -191,6 +192,14 @@ CHIP_ERROR InitNetworkingStack()
#define VerifyInitResultOrReturnError(ec, msg) \
VerifyOrReturnError(ec == CHIP_NO_ERROR, ec, LOG_ERR(msg " [Error: %d]", sInitResult.Format()))

void FactoryResetCallback(intptr_t /* unused */)
{
#if !defined(CONFIG_SOC_SERIES_NRF53X) && !defined(CONFIG_SOC_SERIES_NRF54HX)
// Disable mpsl before start removing settings to improve the performance.
mpsl_lib_uninit();
#endif
}

void DoInitChipServer(intptr_t /* unused */)
{
#ifdef CONFIG_NCS_SAMPLE_MATTER_DIAGNOSTIC_LOGS
Expand Down Expand Up @@ -294,6 +303,7 @@ void DoInitChipServer(intptr_t /* unused */)
VerifyInitResultOrReturn(sInitResult, "Cannot register CHIP event handler");

SetDeviceInfoProvider(sLocalInitData.mDeviceInfoProvider);
ConfigurationManagerImpl().SetFactoryResetDeviceCallback(FactoryResetCallback);

sInitResult = Server::GetInstance().Init(*sLocalInitData.mServerInitParams);
VerifyInitResultOrReturn(sInitResult, "Server::Init() failed");
Expand Down
2 changes: 1 addition & 1 deletion west.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ manifest:
- name: matter
repo-path: sdk-connectedhomeip
path: modules/lib/matter
revision: ad8ba68fd93b25f3fc0c0093bdaade96439b3987
revision: pull/527/head
west-commands: scripts/west/west-commands.yml
submodules:
- name: nlio
Expand Down

0 comments on commit 88ae0f3

Please sign in to comment.