From 43c42c3667787cc3800415f494b652898b17e8fd Mon Sep 17 00:00:00 2001 From: Sagar Dhawan Date: Tue, 26 Oct 2021 15:22:11 -0700 Subject: [PATCH] Update SimpleFabricStorage to preserve FabricIndex --- src/transport/FabricTable.cpp | 11 ++++++++++- src/transport/FabricTable.h | 17 ++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/transport/FabricTable.cpp b/src/transport/FabricTable.cpp index 18d4d9da660bec..2c6c98d47f3b72 100644 --- a/src/transport/FabricTable.cpp +++ b/src/transport/FabricTable.cpp @@ -207,7 +207,7 @@ CHIP_ERROR FabricInfo::DeleteFromStorage(FabricStorage * storage, FabricIndex fa err = storage->SyncDelete(fabricIndex, key); if (err != CHIP_NO_ERROR) { - ChipLogDetail(Discovery, "Fabric %d is not yet configured", ifabricIndexd); + ChipLogDetail(Discovery, "Fabric %d is not yet configured", fabricIndex); } return err; } @@ -600,4 +600,13 @@ CHIP_ERROR FabricTable::SetFabricDelegate(FabricTableDelegate * delegate) return CHIP_NO_ERROR; } +std::string SimpleFabricStorage::formatKey(FabricIndex fabricIndex, const char * key) +{ + char fabricPrefix[fabricPrefixSize]; + snprintf(fabricPrefix, fabricPrefixSize, "F%02X/", fabricIndex); + std::string formattedKey = fabricPrefix; + formattedKey += key; + return formattedKey; +} + } // namespace chip diff --git a/src/transport/FabricTable.h b/src/transport/FabricTable.h index 584a7fa664bfa4..5b5b7244c43c16 100644 --- a/src/transport/FabricTable.h +++ b/src/transport/FabricTable.h @@ -73,6 +73,9 @@ class DLL_EXPORT FabricStorage * @brief A default implementation of Fabric storage that preserves legacy behavior of using * the Persistent storage delegate directly. * + * This class automatically prefixes the Fabric Storage Keys with the FabricIndex. + * The keys are formatted like so: "F%02X/" + key. + * */ class DLL_EXPORT SimpleFabricStorage : public FabricStorage { @@ -91,22 +94,30 @@ class DLL_EXPORT SimpleFabricStorage : public FabricStorage CHIP_ERROR SyncStore(FabricIndex fabricIndex, const char * key, const void * buffer, uint16_t size) override { VerifyOrReturnError(mStorage != nullptr, CHIP_ERROR_INCORRECT_STATE); - return mStorage->SyncSetKeyValue(key, buffer, size); + std::string formattedKey = formatKey(fabricIndex, key); + return mStorage->SyncSetKeyValue(formattedKey.c_str(), buffer, size); }; CHIP_ERROR SyncLoad(FabricIndex fabricIndex, const char * key, void * buffer, uint16_t & size) override { VerifyOrReturnError(mStorage != nullptr, CHIP_ERROR_INCORRECT_STATE); - return mStorage->SyncGetKeyValue(key, buffer, size); + std::string formattedKey = formatKey(fabricIndex, key); + return mStorage->SyncGetKeyValue(formattedKey.c_str(), buffer, size); }; CHIP_ERROR SyncDelete(FabricIndex fabricIndex, const char * key) override { VerifyOrReturnError(mStorage != nullptr, CHIP_ERROR_INCORRECT_STATE); - return mStorage->SyncDeleteKeyValue(key); + std::string formattedKey = formatKey(fabricIndex, key); + return mStorage->SyncDeleteKeyValue(formattedKey.c_str()); }; private: + // FabricPrefix is of the format "F%02X/" + const static int fabricPrefixSize = 5; + // Returns a string that adds a FabricIndex prefix to the Key + std::string formatKey(FabricIndex fabricIndex, const char * key); + PersistentStorageDelegate * mStorage = nullptr; };