Skip to content

Commit d1bd043

Browse files
committed
[nrf toup] Add cleanup argument to FabricTable::Delete
Add cleanup parameter that indicates if FabricTable should try to cleanup all fabric data regardless if fabric was initialized. Signed-off-by: Adrian Gielniewski <adrian.gielniewski@nordicsemi.no>
1 parent 5ab93d9 commit d1bd043

File tree

2 files changed

+47
-36
lines changed

2 files changed

+47
-36
lines changed

src/credentials/FabricTable.cpp

+44-33
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ FabricTable::AddOrUpdateInner(FabricIndex fabricIndex, bool isAddition, Crypto::
948948
return CHIP_NO_ERROR;
949949
}
950950

951-
CHIP_ERROR FabricTable::Delete(FabricIndex fabricIndex)
951+
CHIP_ERROR FabricTable::Delete(FabricIndex fabricIndex, bool cleanup)
952952
{
953953
MATTER_TRACE_SCOPE("Delete", "Fabric");
954954
VerifyOrReturnError(mStorage != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
@@ -1002,39 +1002,46 @@ CHIP_ERROR FabricTable::Delete(FabricIndex fabricIndex)
10021002
}
10031003
}
10041004

1005-
if (!fabricIsInitialized)
1005+
if (fabricIsInitialized)
10061006
{
1007-
// Make sure to return the error our API promises, not whatever storage
1008-
// chose to return.
1009-
return CHIP_ERROR_NOT_FOUND;
1010-
}
1011-
1012-
// Since fabricIsInitialized was true, fabric is not null.
1013-
fabricInfo->Reset();
1014-
1015-
if (!mNextAvailableFabricIndex.HasValue())
1016-
{
1017-
// We must have been in a situation where CHIP_CONFIG_MAX_FABRICS is 254
1018-
// and our fabric table was full, so there was no valid next index. We
1019-
// have a single available index now, though; use it as
1020-
// mNextAvailableFabricIndex.
1021-
mNextAvailableFabricIndex.SetValue(fabricIndex);
1022-
}
1023-
// If StoreFabricIndexInfo fails here, that's probably OK. When we try to
1024-
// read things from storage later we will realize there is nothing for this
1025-
// index.
1026-
StoreFabricIndexInfo();
1007+
// Since fabricIsInitialized was true, fabric is not null.
1008+
fabricInfo->Reset();
10271009

1028-
// If we ever start moving the FabricInfo entries around in the array on
1029-
// delete, we should update DeleteAllFabrics to handle that.
1030-
if (mFabricCount == 0)
1031-
{
1032-
ChipLogError(FabricProvisioning, "Trying to delete a fabric, but the current fabric count is already 0");
1010+
if (!mNextAvailableFabricIndex.HasValue())
1011+
{
1012+
// We must have been in a situation where CHIP_CONFIG_MAX_FABRICS is 254
1013+
// and our fabric table was full, so there was no valid next index. We
1014+
// have a single available index now, though; use it as
1015+
// mNextAvailableFabricIndex.
1016+
mNextAvailableFabricIndex.SetValue(fabricIndex);
1017+
}
1018+
// If StoreFabricIndexInfo fails here, that's probably OK. When we try to
1019+
// read things from storage later we will realize there is nothing for this
1020+
// index.
1021+
StoreFabricIndexInfo();
1022+
1023+
// If we ever start moving the FabricInfo entries around in the array on
1024+
// delete, we should update DeleteAllFabrics to handle that.
1025+
if (mFabricCount == 0)
1026+
{
1027+
ChipLogError(FabricProvisioning, "Trying to delete a fabric, but the current fabric count is already 0");
1028+
}
1029+
else
1030+
{
1031+
mFabricCount--;
1032+
ChipLogProgress(FabricProvisioning, "Fabric (0x%x) deleted.", static_cast<unsigned>(fabricIndex));
1033+
}
10331034
}
10341035
else
10351036
{
1036-
mFabricCount--;
1037-
ChipLogProgress(FabricProvisioning, "Fabric (0x%x) deleted.", static_cast<unsigned>(fabricIndex));
1037+
// Don't return here if we're doing a cleanup, as we want to remove all data,
1038+
// even if incomplete.
1039+
if (!cleanup)
1040+
{
1041+
// Make sure to return the error our API promises, not whatever storage
1042+
// chose to return.
1043+
return CHIP_ERROR_NOT_FOUND;
1044+
}
10381045
}
10391046

10401047
if (mDelegateListRoot != nullptr)
@@ -1050,10 +1057,14 @@ CHIP_ERROR FabricTable::Delete(FabricIndex fabricIndex)
10501057
}
10511058
}
10521059

1053-
// Only return error after trying really hard to remove everything we could
1054-
ReturnErrorOnFailure(metadataErr);
1055-
ReturnErrorOnFailure(opKeyErr);
1056-
ReturnErrorOnFailure(opCertsErr);
1060+
// Don't return error if we're doing a cleanup, some data may be missing.
1061+
if (!cleanup)
1062+
{
1063+
// Only return error after trying really hard to remove everything we could
1064+
ReturnErrorOnFailure(metadataErr);
1065+
ReturnErrorOnFailure(opKeyErr);
1066+
ReturnErrorOnFailure(opCertsErr);
1067+
}
10571068

10581069
return CHIP_NO_ERROR;
10591070
}

src/credentials/FabricTable.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -385,14 +385,14 @@ class DLL_EXPORT FabricTable
385385
/**
386386
* Gets called when a fabric in Fabric Table is persisted to storage, by CommitPendingFabricData.
387387
**/
388-
virtual void OnFabricCommitted(const FabricTable & fabricTable, FabricIndex fabricIndex){};
388+
virtual void OnFabricCommitted(const FabricTable & fabricTable, FabricIndex fabricIndex) {};
389389

390390
/**
391391
* Gets called when operational credentials are changed, which may not be persistent.
392392
*
393393
* Can be used to affect what is needed for UpdateNOC prior to commit.
394394
**/
395-
virtual void OnFabricUpdated(const FabricTable & fabricTable, FabricIndex fabricIndex){};
395+
virtual void OnFabricUpdated(const FabricTable & fabricTable, FabricIndex fabricIndex) {};
396396

397397
// Intrusive list pointer for FabricTable to manage the entries.
398398
Delegate * next = nullptr;
@@ -413,7 +413,7 @@ class DLL_EXPORT FabricTable
413413
};
414414

415415
// Returns CHIP_ERROR_NOT_FOUND if there is no fabric for that index.
416-
CHIP_ERROR Delete(FabricIndex fabricIndex);
416+
CHIP_ERROR Delete(FabricIndex fabricIndex, bool cleanup = false);
417417
void DeleteAllFabrics();
418418

419419
// TODO this #if CONFIG_BUILD_FOR_HOST_UNIT_TEST is temporary. There is a change incoming soon

0 commit comments

Comments
 (0)