@@ -51,6 +51,11 @@ CHIP_ERROR DefaultICDClientStorage::UpdateFabricList(FabricIndex fabricIndex)
51
51
52
52
mFabricList .push_back (fabricIndex);
53
53
54
+ return StoreFabricList ();
55
+ }
56
+
57
+ CHIP_ERROR DefaultICDClientStorage::StoreFabricList ()
58
+ {
54
59
Platform::ScopedMemoryBuffer<uint8_t > backingBuffer;
55
60
size_t counter = mFabricList .size ();
56
61
size_t total = kFabricIndexTlvSize * counter + kArrayOverHead ;
@@ -68,7 +73,7 @@ CHIP_ERROR DefaultICDClientStorage::UpdateFabricList(FabricIndex fabricIndex)
68
73
const auto len = writer.GetLengthWritten ();
69
74
VerifyOrReturnError (CanCastTo<uint16_t >(len), CHIP_ERROR_BUFFER_TOO_SMALL);
70
75
71
- writer.Finalize (backingBuffer);
76
+ ReturnErrorOnFailure ( writer.Finalize (backingBuffer) );
72
77
return mpClientInfoStore->SyncSetKeyValue (DefaultStorageKeyAllocator::ICDFabricList ().KeyName (), backingBuffer.Get (),
73
78
static_cast <uint16_t >(len));
74
79
}
@@ -211,6 +216,7 @@ CHIP_ERROR DefaultICDClientStorage::Load(FabricIndex fabricIndex, std::vector<IC
211
216
{
212
217
size_t count = 0 ;
213
218
ReturnErrorOnFailure (LoadCounter (fabricIndex, count, clientInfoSize));
219
+ VerifyOrReturnError (count > 0 , CHIP_NO_ERROR);
214
220
size_t len = clientInfoSize * count + kArrayOverHead ;
215
221
Platform::ScopedMemoryBuffer<uint8_t > backingBuffer;
216
222
VerifyOrReturnError (CanCastTo<uint16_t >(len), CHIP_ERROR_BUFFER_TOO_SMALL);
@@ -344,8 +350,21 @@ CHIP_ERROR DefaultICDClientStorage::SerializeToTlv(TLV::TLVWriter & writer, cons
344
350
return writer.EndContainer (arrayType);
345
351
}
346
352
353
+ bool DefaultICDClientStorage::FabricExists (FabricIndex fabricIndex)
354
+ {
355
+ for (auto & fabric_idx : mFabricList )
356
+ {
357
+ if (fabric_idx == fabricIndex)
358
+ {
359
+ return true ;
360
+ }
361
+ }
362
+ return false ;
363
+ }
364
+
347
365
CHIP_ERROR DefaultICDClientStorage::StoreEntry (const ICDClientInfo & clientInfo)
348
366
{
367
+ VerifyOrReturnError (FabricExists (clientInfo.peer_node .GetFabricIndex ()), CHIP_ERROR_INVALID_FABRIC_INDEX);
349
368
std::vector<ICDClientInfo> clientInfoVector;
350
369
size_t clientInfoSize = MaxICDClientInfoSize ();
351
370
ReturnErrorOnFailure (Load (clientInfo.peer_node .GetFabricIndex (), clientInfoVector, clientInfoSize));
@@ -359,7 +378,6 @@ CHIP_ERROR DefaultICDClientStorage::StoreEntry(const ICDClientInfo & clientInfo)
359
378
break ;
360
379
}
361
380
}
362
-
363
381
clientInfoVector.push_back (clientInfo);
364
382
size_t total = clientInfoSize * clientInfoVector.size () + kArrayOverHead ;
365
383
Platform::ScopedMemoryBuffer<uint8_t > backingBuffer;
@@ -371,7 +389,7 @@ CHIP_ERROR DefaultICDClientStorage::StoreEntry(const ICDClientInfo & clientInfo)
371
389
const auto len = writer.GetLengthWritten ();
372
390
VerifyOrReturnError (CanCastTo<uint16_t >(len), CHIP_ERROR_BUFFER_TOO_SMALL);
373
391
374
- writer.Finalize (backingBuffer);
392
+ ReturnErrorOnFailure ( writer.Finalize (backingBuffer) );
375
393
ReturnErrorOnFailure (mpClientInfoStore->SyncSetKeyValue (
376
394
DefaultStorageKeyAllocator::ICDClientInfoKey (clientInfo.peer_node .GetFabricIndex ()).KeyName (), backingBuffer.Get (),
377
395
static_cast <uint16_t >(len)));
@@ -416,17 +434,19 @@ CHIP_ERROR DefaultICDClientStorage::UpdateEntryCountForFabric(FabricIndex fabric
416
434
417
435
const auto len = writer.GetLengthWritten ();
418
436
VerifyOrReturnError (CanCastTo<uint16_t >(len), CHIP_ERROR_BUFFER_TOO_SMALL);
419
- writer.Finalize (backingBuffer);
437
+ ReturnErrorOnFailure ( writer.Finalize (backingBuffer) );
420
438
421
439
return mpClientInfoStore->SyncSetKeyValue (DefaultStorageKeyAllocator::FabricICDClientInfoCounter (fabricIndex).KeyName (),
422
440
backingBuffer.Get (), static_cast <uint16_t >(len));
423
441
}
424
442
425
443
CHIP_ERROR DefaultICDClientStorage::DeleteEntry (const ScopedNodeId & peerNode)
426
444
{
445
+ VerifyOrReturnError (FabricExists (peerNode.GetFabricIndex ()), CHIP_NO_ERROR);
427
446
size_t clientInfoSize = 0 ;
428
447
std::vector<ICDClientInfo> clientInfoVector;
429
448
ReturnErrorOnFailure (Load (peerNode.GetFabricIndex (), clientInfoVector, clientInfoSize));
449
+ VerifyOrReturnError (clientInfoVector.size () > 0 , CHIP_NO_ERROR);
430
450
431
451
for (auto it = clientInfoVector.begin (); it != clientInfoVector.end (); it++)
432
452
{
@@ -440,7 +460,6 @@ CHIP_ERROR DefaultICDClientStorage::DeleteEntry(const ScopedNodeId & peerNode)
440
460
441
461
ReturnErrorOnFailure (
442
462
mpClientInfoStore->SyncDeleteKeyValue (DefaultStorageKeyAllocator::ICDClientInfoKey (peerNode.GetFabricIndex ()).KeyName ()));
443
-
444
463
size_t total = clientInfoSize * clientInfoVector.size () + kArrayOverHead ;
445
464
Platform::ScopedMemoryBuffer<uint8_t > backingBuffer;
446
465
ReturnErrorCodeIf (!backingBuffer.Calloc (total), CHIP_ERROR_NO_MEMORY);
@@ -451,7 +470,7 @@ CHIP_ERROR DefaultICDClientStorage::DeleteEntry(const ScopedNodeId & peerNode)
451
470
const auto len = writer.GetLengthWritten ();
452
471
VerifyOrReturnError (CanCastTo<uint16_t >(len), CHIP_ERROR_BUFFER_TOO_SMALL);
453
472
454
- writer.Finalize (backingBuffer);
473
+ ReturnErrorOnFailure ( writer.Finalize (backingBuffer) );
455
474
ReturnErrorOnFailure (
456
475
mpClientInfoStore->SyncSetKeyValue (DefaultStorageKeyAllocator::ICDClientInfoKey (peerNode.GetFabricIndex ()).KeyName (),
457
476
backingBuffer.Get (), static_cast <uint16_t >(len)));
@@ -461,6 +480,8 @@ CHIP_ERROR DefaultICDClientStorage::DeleteEntry(const ScopedNodeId & peerNode)
461
480
462
481
CHIP_ERROR DefaultICDClientStorage::DeleteAllEntries (FabricIndex fabricIndex)
463
482
{
483
+ VerifyOrReturnError (FabricExists (fabricIndex), CHIP_NO_ERROR);
484
+
464
485
size_t clientInfoSize = 0 ;
465
486
std::vector<ICDClientInfo> clientInfoVector;
466
487
ReturnErrorOnFailure (Load (fabricIndex, clientInfoVector, clientInfoSize));
@@ -471,7 +492,23 @@ CHIP_ERROR DefaultICDClientStorage::DeleteAllEntries(FabricIndex fabricIndex)
471
492
}
472
493
ReturnErrorOnFailure (
473
494
mpClientInfoStore->SyncDeleteKeyValue (DefaultStorageKeyAllocator::ICDClientInfoKey (fabricIndex).KeyName ()));
474
- return mpClientInfoStore->SyncDeleteKeyValue (DefaultStorageKeyAllocator::FabricICDClientInfoCounter (fabricIndex).KeyName ());
495
+ ReturnErrorOnFailure (
496
+ mpClientInfoStore->SyncDeleteKeyValue (DefaultStorageKeyAllocator::FabricICDClientInfoCounter (fabricIndex).KeyName ()));
497
+
498
+ for (auto fabric = mFabricList .begin (); fabric != mFabricList .end (); fabric++)
499
+ {
500
+ if (*fabric == fabricIndex)
501
+ {
502
+ mFabricList .erase (fabric);
503
+ break ;
504
+ }
505
+ }
506
+
507
+ if (mFabricList .size () == 0 )
508
+ {
509
+ return mpClientInfoStore->SyncDeleteKeyValue (DefaultStorageKeyAllocator::ICDFabricList ().KeyName ());
510
+ }
511
+ return StoreFabricList ();
475
512
}
476
513
477
514
CHIP_ERROR DefaultICDClientStorage::ProcessCheckInPayload (const ByteSpan & payload, ICDClientInfo & clientInfo,
0 commit comments