39
39
#include < app/InteractionModelEngine.h>
40
40
#endif
41
41
42
- #ifdef CONFIG_CHIP_FACTORY_RESET_ERASE_NVS
43
42
#include < zephyr/fs/nvs.h>
44
43
#include < zephyr/settings/settings.h>
45
- #endif
46
44
47
45
using namespace chip ::app;
48
46
@@ -103,10 +101,11 @@ Button sThreadStartButton;
103
101
k_timer sFactoryResetTimer ;
104
102
uint8_t sFactoryResetCntr = 0 ;
105
103
106
- bool sIsThreadProvisioned = false ;
107
- bool sIsThreadEnabled = false ;
108
- bool sIsThreadAttached = false ;
109
- bool sHaveBLEConnections = false ;
104
+ bool sIsCommissioningFailed = false ;
105
+ bool sIsThreadProvisioned = false ;
106
+ bool sIsThreadEnabled = false ;
107
+ bool sIsThreadAttached = false ;
108
+ bool sHaveBLEConnections = false ;
110
109
111
110
#if APP_SET_DEVICE_INFO_PROVIDER
112
111
chip::DeviceLayer::DeviceInfoProviderImpl gExampleDeviceInfoProvider ;
@@ -138,9 +137,10 @@ class AppCallbacks : public AppDelegate
138
137
bool isComissioningStarted;
139
138
140
139
public:
141
- void OnCommissioningSessionEstablishmentStarted () { }
140
+ void OnCommissioningSessionEstablishmentStarted () override { sIsCommissioningFailed = false ; }
142
141
void OnCommissioningSessionStarted () override { isComissioningStarted = true ; }
143
142
void OnCommissioningSessionStopped () override { isComissioningStarted = false ; }
143
+ void OnCommissioningSessionEstablishmentError (CHIP_ERROR err) override { sIsCommissioningFailed = true ; }
144
144
void OnCommissioningWindowClosed () override
145
145
{
146
146
if (!isComissioningStarted)
@@ -157,51 +157,41 @@ class AppFabricTableDelegate : public FabricTable::Delegate
157
157
{
158
158
if (chip::Server::GetInstance ().GetFabricTable ().FabricCount () == 0 )
159
159
{
160
- bool isBasicCommissioningMode = chip::Server::GetInstance ().GetCommissioningWindowManager ().GetCommissioningMode () ==
161
- Dnssd::CommissioningMode::kEnabledBasic ;
162
160
ChipLogProgress (DeviceLayer, " Performing erasing of settings partition" );
163
161
164
- #ifdef CONFIG_CHIP_FACTORY_RESET_ERASE_NVS
165
- void * storage = nullptr ;
166
- int status = settings_storage_get (&storage);
167
-
168
- if (status == 0 )
162
+ // Do FactoryReset in case of failed commissioning to allow new pairing via BLE
163
+ if (sIsCommissioningFailed )
169
164
{
170
- status = nvs_clear ( static_cast <nvs_fs *>(storage) );
165
+ chip::Server::GetInstance (). ScheduleFactoryReset ( );
171
166
}
172
-
173
- if (!isBasicCommissioningMode)
167
+ // TC-OPCREDS-3.6 (device doesn't need to reboot automatically after the last fabric is removed) can't use FactoryReset
168
+ else
174
169
{
170
+ void * storage = nullptr ;
171
+ int status = settings_storage_get (&storage);
172
+
175
173
if (!status)
176
174
{
177
- status = nvs_mount (static_cast <nvs_fs *>(storage));
175
+ status = nvs_clear (static_cast <nvs_fs *>(storage));
178
176
}
179
- }
180
-
181
- if (status)
182
- {
183
- ChipLogError (DeviceLayer, " Storage clearance failed: %d" , status);
184
- }
185
- #else
186
- const CHIP_ERROR err = PersistedStorage::KeyValueStoreMgrImpl ().DoFactoryReset ();
187
177
188
- if (err != CHIP_NO_ERROR )
189
- {
190
- ChipLogError (DeviceLayer, " Factory reset failed: % " CHIP_ERROR_FORMAT, err. Format ( ));
191
- }
178
+ if (!status )
179
+ {
180
+ status = nvs_mount ( static_cast <nvs_fs *>(storage ));
181
+ }
192
182
193
- ConnectivityMgr ().ErasePersistentInfo ();
194
- #endif
195
- if (isBasicCommissioningMode)
196
- {
197
- PlatformMgr ().Shutdown ();
183
+ if (status)
184
+ {
185
+ ChipLogError (DeviceLayer, " Storage clearance failed: %d" , status);
186
+ }
198
187
}
199
188
}
200
189
}
201
190
};
202
191
203
192
class PlatformMgrDelegate : public DeviceLayer ::PlatformManagerDelegate
204
193
{
194
+ // Disable openthread before reset to prevent writing to NVS
205
195
void OnShutDown () override
206
196
{
207
197
if (ThreadStackManagerImpl ().IsThreadEnabled ())
0 commit comments