Skip to content

Commit

Permalink
Update SimpleFabricStorage to preserve FabricIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
sagar-apple committed Oct 26, 2021
1 parent 40ec03d commit 43c42c3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/transport/FabricTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
17 changes: 14 additions & 3 deletions src/transport/FabricTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
};

Expand Down

0 comments on commit 43c42c3

Please sign in to comment.