Skip to content

Commit 4d4bf1c

Browse files
kkasperczyk-noArekBalysNordic
authored andcommitted
samples: matter: Improved bridge storage manager error handling
Added checking the enum value returned by persistent storage. Changed PSErrorCode enum to enum class to prevent such unintuitive comparison in the future. Signed-off-by: Kamil Kasperczyk <[email protected]>
1 parent d05a1ae commit 4d4bf1c

File tree

3 files changed

+37
-22
lines changed

3 files changed

+37
-22
lines changed

samples/matter/common/src/bridge/bridge_storage_manager.cpp

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ namespace
1717
template <class T> bool LoadDataToObject(Nrf::PersistentStorageNode *node, T &data)
1818
{
1919
size_t readSize = 0;
20+
const Nrf::PSErrorCode status = Nrf::GetPersistentStorage().NonSecureLoad(node, &data, sizeof(T), readSize);
2021

21-
bool result = Nrf::GetPersistentStorage().NonSecureLoad(node, &data, sizeof(T), readSize);
22-
23-
return result;
22+
return status == Nrf::PSErrorCode::Success ? sizeof(T) == readSize : false;
2423
}
2524

2625
Nrf::PersistentStorageNode CreateIndexNode(uint8_t bridgedDeviceIndex, Nrf::PersistentStorageNode *parent)
@@ -47,8 +46,8 @@ template <> bool BridgeStorageManager::LoadBridgedDevice(BridgedDeviceV1 &device
4746
const uint8_t mandatoryItemsSize =
4847
sizeof(device.mEndpointId) + sizeof(device.mDeviceType) + sizeof(device.mNodeLabelLength);
4948

50-
if (!Nrf::GetPersistentStorage().NonSecureLoad(&id, buffer, sizeof(BridgedDeviceV1) + kMaxUserDataSize,
51-
readSize)) {
49+
if (Nrf::GetPersistentStorage().NonSecureLoad(&id, buffer, sizeof(BridgedDeviceV1) + kMaxUserDataSize,
50+
readSize) != PSErrorCode::Success) {
5251
return false;
5352
}
5453

@@ -114,8 +113,8 @@ template <> bool BridgeStorageManager::LoadBridgedDevice(BridgedDeviceV2 &device
114113
const uint8_t mandatoryItemsSize =
115114
sizeof(device.mEndpointId) + sizeof(device.mDeviceType) + sizeof(device.mUniqueIDLength);
116115

117-
if (!Nrf::GetPersistentStorage().NonSecureLoad(&id, buffer, sizeof(BridgedDeviceV2) + kMaxUserDataSize,
118-
readSize)) {
116+
if (Nrf::GetPersistentStorage().NonSecureLoad(&id, buffer, sizeof(BridgedDeviceV2) + kMaxUserDataSize,
117+
readSize) != PSErrorCode::Success) {
119118
return false;
120119
}
121120

@@ -189,10 +188,10 @@ template <> bool BridgeStorageManager::LoadBridgedDevice(BridgedDeviceV2 &device
189188

190189
bool BridgeStorageManager::Init()
191190
{
192-
bool result = Nrf::GetPersistentStorage().NonSecureInit();
191+
const PSErrorCode status = Nrf::GetPersistentStorage().NonSecureInit();
193192

194-
if (!result) {
195-
return result;
193+
if (status != PSErrorCode::Success) {
194+
return false;
196195
}
197196

198197
/* Perform data migration from previous data structure versions if needed. */
@@ -361,7 +360,10 @@ bool BridgeStorageManager::MigrateData()
361360

362361
bool BridgeStorageManager::StoreBridgedDevicesCount(uint8_t count)
363362
{
364-
return Nrf::GetPersistentStorage().NonSecureStore(&mBridgedDevicesCount, &count, sizeof(count));
363+
const PSErrorCode status =
364+
Nrf::GetPersistentStorage().NonSecureStore(&mBridgedDevicesCount, &count, sizeof(count));
365+
366+
return status == PSErrorCode::Success;
365367
}
366368

367369
bool BridgeStorageManager::LoadBridgedDevicesCount(uint8_t &count)
@@ -375,7 +377,9 @@ bool BridgeStorageManager::StoreBridgedDevicesIndexes(uint8_t *indexes, uint8_t
375377
return false;
376378
}
377379

378-
return Nrf::GetPersistentStorage().NonSecureStore(&mBridgedDevicesIndexes, indexes, count);
380+
const PSErrorCode status = Nrf::GetPersistentStorage().NonSecureStore(&mBridgedDevicesIndexes, indexes, count);
381+
382+
return status == PSErrorCode::Success;
379383
}
380384

381385
bool BridgeStorageManager::LoadBridgedDevicesIndexes(uint8_t *indexes, uint8_t maxCount, size_t &count)
@@ -384,7 +388,10 @@ bool BridgeStorageManager::LoadBridgedDevicesIndexes(uint8_t *indexes, uint8_t m
384388
return false;
385389
}
386390

387-
return Nrf::GetPersistentStorage().NonSecureLoad(&mBridgedDevicesIndexes, indexes, maxCount, count);
391+
const PSErrorCode status =
392+
Nrf::GetPersistentStorage().NonSecureLoad(&mBridgedDevicesIndexes, indexes, maxCount, count);
393+
394+
return status == PSErrorCode::Success;
388395
}
389396

390397
#ifdef CONFIG_BRIDGE_MIGRATE_PRE_2_7_0
@@ -398,23 +405,26 @@ bool BridgeStorageManager::LoadBridgedDeviceEndpointId(uint16_t &endpointId, uin
398405
bool BridgeStorageManager::RemoveBridgedDeviceEndpointId(uint8_t bridgedDeviceIndex)
399406
{
400407
Nrf::PersistentStorageNode id = CreateIndexNode(bridgedDeviceIndex, &mBridgedDeviceEndpointId);
408+
const PSErrorCode status = Nrf::GetPersistentStorage().NonSecureRemove(&id);
401409

402-
return Nrf::GetPersistentStorage().NonSecureRemove(&id);
410+
return status == PSErrorCode::Success;
403411
}
404412

405413
bool BridgeStorageManager::LoadBridgedDeviceNodeLabel(char *label, size_t labelMaxLength, size_t &labelLength,
406414
uint8_t bridgedDeviceIndex)
407415
{
408416
Nrf::PersistentStorageNode id = CreateIndexNode(bridgedDeviceIndex, &mBridgedDeviceNodeLabel);
417+
const PSErrorCode status = Nrf::GetPersistentStorage().NonSecureLoad(&id, label, labelMaxLength, labelLength);
409418

410-
return Nrf::GetPersistentStorage().NonSecureLoad(&id, label, labelMaxLength, labelLength);
419+
return status == PSErrorCode::Success;
411420
}
412421

413422
bool BridgeStorageManager::RemoveBridgedDeviceNodeLabel(uint8_t bridgedDeviceIndex)
414423
{
415424
Nrf::PersistentStorageNode id = CreateIndexNode(bridgedDeviceIndex, &mBridgedDeviceNodeLabel);
425+
const PSErrorCode status = Nrf::GetPersistentStorage().NonSecureRemove(&id);
416426

417-
return Nrf::GetPersistentStorage().NonSecureRemove(&id);
427+
return status == PSErrorCode::Success;
418428
}
419429

420430
bool BridgeStorageManager::LoadBridgedDeviceType(uint16_t &deviceType, uint8_t bridgedDeviceIndex)
@@ -427,8 +437,9 @@ bool BridgeStorageManager::LoadBridgedDeviceType(uint16_t &deviceType, uint8_t b
427437
bool BridgeStorageManager::RemoveBridgedDeviceType(uint8_t bridgedDeviceIndex)
428438
{
429439
Nrf::PersistentStorageNode id = CreateIndexNode(bridgedDeviceIndex, &mBridgedDeviceType);
440+
const PSErrorCode status = Nrf::GetPersistentStorage().NonSecureRemove(&id);
430441

431-
return Nrf::GetPersistentStorage().NonSecureRemove(&id);
442+
return status == PSErrorCode::Success;
432443
}
433444
#endif
434445

@@ -464,14 +475,17 @@ bool BridgeStorageManager::StoreBridgedDevice(BridgedDevice &device, uint8_t ind
464475
counter += device.mUserDataSize;
465476
}
466477

467-
return Nrf::GetPersistentStorage().NonSecureStore(&id, buffer, counter);
478+
const PSErrorCode status = Nrf::GetPersistentStorage().NonSecureStore(&id, buffer, counter);
479+
480+
return status == PSErrorCode::Success;
468481
}
469482

470483
bool BridgeStorageManager::RemoveBridgedDevice(uint8_t index)
471484
{
472485
Nrf::PersistentStorageNode id = CreateIndexNode(index, &mBridgedDevice);
486+
const PSErrorCode status = Nrf::GetPersistentStorage().NonSecureRemove(&id);
473487

474-
return Nrf::GetPersistentStorage().NonSecureRemove(&id);
488+
return status == PSErrorCode::Success;
475489
}
476490

477491
#ifdef CONFIG_BRIDGE_MIGRATE_PRE_2_7_0
@@ -486,8 +500,9 @@ bool BridgeStorageManager::LoadBtAddress(bt_addr_le_t &addr, uint8_t bridgedDevi
486500
bool BridgeStorageManager::RemoveBtAddress(uint8_t bridgedDeviceIndex)
487501
{
488502
Nrf::PersistentStorageNode id = CreateIndexNode(bridgedDeviceIndex, &mBtAddress);
503+
const PSErrorCode status = Nrf::GetPersistentStorage().NonSecureRemove(&id);
489504

490-
return Nrf::GetPersistentStorage().NonSecureRemove(&id);
505+
return status == PSErrorCode::Success;
491506
}
492507
#endif
493508
#endif

samples/matter/common/src/persistent_storage/persistent_storage_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace Nrf
1212
{
13-
enum PSErrorCode : uint8_t { Failure, Success, NotSupported };
13+
enum class PSErrorCode : uint8_t { Failure, Success, NotSupported };
1414

1515
/**
1616
* @brief Class representing single tree node and containing information about its key.

samples/matter/lock/src/access/access_storage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ bool GetStorageFreeSpace(size_t &freeBytes)
6262

6363
bool AccessStorage::Init()
6464
{
65-
return (Nrf::Success == Nrf::GetPersistentStorage().PSInit());
65+
return (Nrf::PSErrorCode::Success == Nrf::GetPersistentStorage().PSInit());
6666
}
6767

6868
bool AccessStorage::PrepareKeyName(Type storageType, uint16_t index, uint16_t subindex)

0 commit comments

Comments
 (0)