Skip to content

Commit

Permalink
Enable Fabric Scoped Storage
Browse files Browse the repository at this point in the history
  • Loading branch information
sagar-apple committed Oct 8, 2021
1 parent 8a4eefb commit 1259720
Show file tree
Hide file tree
Showing 34 changed files with 366 additions and 175 deletions.
37 changes: 25 additions & 12 deletions examples/chip-tool/config/PersistentStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,23 @@ CHIP_ERROR PersistentStorage::Init()
return err;
}

CHIP_ERROR PersistentStorage::SyncGetKeyValue(const char * key, void * value, uint16_t & size)
String GetSectionName(const CompressedFabricId fabricId)
{
if (fabricId == kUndefinedCompressedFabricId)
{
return kDefaultSectionName;
}
else
{
return std::to_string(fabricId);
}
}

CHIP_ERROR PersistentStorage::SyncGetKeyValue(const CompressedFabricId fabricId, const char * key, void * value, uint16_t & size)
{
std::string iniValue;

auto section = mConfig.sections[kDefaultSectionName];
auto section = mConfig.sections[GetSectionName(fabricId)];
auto it = section.find(key);
ReturnErrorCodeIf(it == section.end(), CHIP_ERROR_KEY_NOT_FOUND);

Expand All @@ -116,21 +128,22 @@ CHIP_ERROR PersistentStorage::SyncGetKeyValue(const char * key, void * value, ui
return CHIP_NO_ERROR;
}

CHIP_ERROR PersistentStorage::SyncSetKeyValue(const char * key, const void * value, uint16_t size)
CHIP_ERROR PersistentStorage::SyncSetKeyValue(const CompressedFabricId fabricId, const char * key, const void * value,
uint16_t size)
{
auto section = mConfig.sections[kDefaultSectionName];
auto section = mConfig.sections[GetSectionName(fabricId)];
section[key] = StringToBase64(std::string(static_cast<const char *>(value), size));

mConfig.sections[kDefaultSectionName] = section;
mConfig.sections[GetSectionName(fabricId)] = section;
return CommitConfig();
}

CHIP_ERROR PersistentStorage::SyncDeleteKeyValue(const char * key)
CHIP_ERROR PersistentStorage::SyncDeleteKeyValue(const CompressedFabricId fabricId, const char * key)
{
auto section = mConfig.sections[kDefaultSectionName];
auto section = mConfig.sections[GetSectionName(fabricId)];
section.erase(key);

mConfig.sections[kDefaultSectionName] = section;
mConfig.sections[GetSectionName(fabricId)] = section;
return CommitConfig();
}

Expand Down Expand Up @@ -163,7 +176,7 @@ uint16_t PersistentStorage::GetListenPort()

char value[6];
uint16_t size = static_cast<uint16_t>(sizeof(value));
err = SyncGetKeyValue(kPortKey, value, size);
err = SyncGetKeyValue(kUndefinedCompressedFabricId, kPortKey, value, size);
if (CHIP_NO_ERROR == err)
{
uint16_t tmpValue;
Expand All @@ -185,7 +198,7 @@ LogCategory PersistentStorage::GetLoggingLevel()

char value[9];
uint16_t size = static_cast<uint16_t>(sizeof(value));
err = SyncGetKeyValue(kLoggingKey, value, size);
err = SyncGetKeyValue(kUndefinedCompressedFabricId, kLoggingKey, value, size);
if (CHIP_NO_ERROR == err)
{
if (strcasecmp(value, "none") == 0)
Expand Down Expand Up @@ -215,7 +228,7 @@ NodeId PersistentStorage::GetNodeId(const char * key, NodeId defaultVal)

uint64_t nodeId;
uint16_t size = static_cast<uint16_t>(sizeof(nodeId));
err = SyncGetKeyValue(key, &nodeId, size);
err = SyncGetKeyValue(kUndefinedCompressedFabricId, key, &nodeId, size);
if (err == CHIP_NO_ERROR)
{
return static_cast<NodeId>(Encoding::LittleEndian::HostSwap64(nodeId));
Expand All @@ -237,7 +250,7 @@ NodeId PersistentStorage::GetRemoteNodeId()
CHIP_ERROR PersistentStorage::SetNodeId(const char * key, NodeId value)
{
uint64_t nodeId = Encoding::LittleEndian::HostSwap64(value);
return SyncSetKeyValue(key, &nodeId, sizeof(nodeId));
return SyncSetKeyValue(kUndefinedCompressedFabricId, key, &nodeId, sizeof(nodeId));
}

CHIP_ERROR PersistentStorage::SetLocalNodeId(NodeId nodeId)
Expand Down
7 changes: 4 additions & 3 deletions examples/chip-tool/config/PersistentStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ class PersistentStorage : public chip::PersistentStorageDelegate
CHIP_ERROR Init();

/////////// PersistentStorageDelegate Interface /////////
CHIP_ERROR SyncGetKeyValue(const char * key, void * buffer, uint16_t & size) override;
CHIP_ERROR SyncSetKeyValue(const char * key, const void * value, uint16_t size) override;
CHIP_ERROR SyncDeleteKeyValue(const char * key) override;
CHIP_ERROR SyncGetKeyValue(const chip::CompressedFabricId fabricId, const char * key, void * buffer, uint16_t & size) override;
CHIP_ERROR SyncSetKeyValue(const chip::CompressedFabricId fabricId, const char * key, const void * value,
uint16_t size) override;
CHIP_ERROR SyncDeleteKeyValue(const chip::CompressedFabricId fabricId, const char * key) override;

uint16_t GetListenPort();
chip::Logging::LogCategory GetLoggingLevel();
Expand Down
33 changes: 23 additions & 10 deletions examples/ota-requestor-app/linux/PersistentStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,23 @@ CHIP_ERROR PersistentStorage::Init()
return err;
}

CHIP_ERROR PersistentStorage::SyncGetKeyValue(const char * key, void * value, uint16_t & size)
String GetSectionName(const CompressedFabricId fabricId)
{
if (fabricId == kUndefinedCompressedFabricId)
{
return kDefaultSectionName;
}
else
{
return std::to_string(fabricId);
}
}

CHIP_ERROR PersistentStorage::SyncGetKeyValue(const CompressedFabricId fabricId, const char * key, void * value, uint16_t & size)
{
std::string iniValue;

auto section = mConfig.sections[kDefaultSectionName];
auto section = mConfig.sections[GetSectionName(fabricId)];
auto it = section.find(key);
ReturnErrorCodeIf(it == section.end(), CHIP_ERROR_KEY_NOT_FOUND);

Expand All @@ -116,18 +128,19 @@ CHIP_ERROR PersistentStorage::SyncGetKeyValue(const char * key, void * value, ui
return CHIP_NO_ERROR;
}

CHIP_ERROR PersistentStorage::SyncSetKeyValue(const char * key, const void * value, uint16_t size)
CHIP_ERROR PersistentStorage::SyncSetKeyValue(const CompressedFabricId fabricId, const char * key, const void * value,
uint16_t size)
{
auto section = mConfig.sections[kDefaultSectionName];
auto section = mConfig.sections[GetSectionName(fabricId)];
section[key] = StringToBase64(std::string(static_cast<const char *>(value), size));

mConfig.sections[kDefaultSectionName] = section;
return CommitConfig();
}

CHIP_ERROR PersistentStorage::SyncDeleteKeyValue(const char * key)
CHIP_ERROR PersistentStorage::SyncDeleteKeyValue(const CompressedFabricId fabricId, const char * key)
{
auto section = mConfig.sections[kDefaultSectionName];
auto section = mConfig.sections[GetSectionName(fabricId)];
section.erase(key);

mConfig.sections[kDefaultSectionName] = section;
Expand Down Expand Up @@ -163,7 +176,7 @@ uint16_t PersistentStorage::GetListenPort()

char value[6];
uint16_t size = static_cast<uint16_t>(sizeof(value));
err = SyncGetKeyValue(kPortKey, value, size);
err = SyncGetKeyValue(kUndefinedCompressedFabricId, kPortKey, value, size);
if (CHIP_NO_ERROR == err)
{
uint16_t tmpValue;
Expand All @@ -185,7 +198,7 @@ LogCategory PersistentStorage::GetLoggingLevel()

char value[9];
uint16_t size = static_cast<uint16_t>(sizeof(value));
err = SyncGetKeyValue(kLoggingKey, value, size);
err = SyncGetKeyValue(kUndefinedCompressedFabricId, kLoggingKey, value, size);
if (CHIP_NO_ERROR == err)
{
if (strcasecmp(value, "none") == 0)
Expand Down Expand Up @@ -215,7 +228,7 @@ NodeId PersistentStorage::GetNodeId(const char * key, NodeId defaultVal)

uint64_t nodeId;
uint16_t size = static_cast<uint16_t>(sizeof(nodeId));
err = SyncGetKeyValue(key, &nodeId, size);
err = SyncGetKeyValue(kUndefinedCompressedFabricId, key, &nodeId, size);
if (err == CHIP_NO_ERROR)
{
return static_cast<NodeId>(Encoding::LittleEndian::HostSwap64(nodeId));
Expand All @@ -237,7 +250,7 @@ NodeId PersistentStorage::GetRemoteNodeId()
CHIP_ERROR PersistentStorage::SetNodeId(const char * key, NodeId value)
{
uint64_t nodeId = Encoding::LittleEndian::HostSwap64(value);
return SyncSetKeyValue(key, &nodeId, sizeof(nodeId));
return SyncSetKeyValue(kUndefinedCompressedFabricId, key, &nodeId, sizeof(nodeId));
}

CHIP_ERROR PersistentStorage::SetLocalNodeId(NodeId nodeId)
Expand Down
7 changes: 4 additions & 3 deletions examples/ota-requestor-app/linux/PersistentStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ class PersistentStorage : public chip::PersistentStorageDelegate
CHIP_ERROR Init();

/////////// PersistentStorageDelegate Interface /////////
CHIP_ERROR SyncGetKeyValue(const char * key, void * buffer, uint16_t & size) override;
CHIP_ERROR SyncSetKeyValue(const char * key, const void * value, uint16_t size) override;
CHIP_ERROR SyncDeleteKeyValue(const char * key) override;
CHIP_ERROR SyncGetKeyValue(const chip::CompressedFabricId fabricId, const char * key, void * buffer, uint16_t & size) override;
CHIP_ERROR SyncSetKeyValue(const chip::CompressedFabricId fabricId, const char * key, const void * value,
uint16_t size) override;
CHIP_ERROR SyncDeleteKeyValue(const chip::CompressedFabricId fabricId, const char * key) override;

uint16_t GetListenPort();
chip::Logging::LogCategory GetLoggingLevel();
Expand Down
6 changes: 3 additions & 3 deletions examples/platform/linux/AppMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,19 @@ using namespace ::chip::Controller;

class MyServerStorageDelegate : public PersistentStorageDelegate
{
CHIP_ERROR SyncGetKeyValue(const char * key, void * buffer, uint16_t & size) override
CHIP_ERROR SyncGetKeyValue(const CompressedFabricId fabricId, const char * key, void * buffer, uint16_t & size) override
{
ChipLogProgress(AppServer, "Retrieved value from server storage.");
return PersistedStorage::KeyValueStoreMgr().Get(key, buffer, size);
}

CHIP_ERROR SyncSetKeyValue(const char * key, const void * value, uint16_t size) override
CHIP_ERROR SyncSetKeyValue(const CompressedFabricId fabricId, const char * key, const void * value, uint16_t size) override
{
ChipLogProgress(AppServer, "Stored value in server storage");
return PersistedStorage::KeyValueStoreMgr().Put(key, value, size);
}

CHIP_ERROR SyncDeleteKeyValue(const char * key) override
CHIP_ERROR SyncDeleteKeyValue(const CompressedFabricId fabricId, const char * key) override
{
ChipLogProgress(AppServer, "Delete value in server storage");
return PersistedStorage::KeyValueStoreMgr().Delete(key);
Expand Down
7 changes: 4 additions & 3 deletions src/app/server/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,24 @@ class Server : public Messaging::ExchangeDelegate

static Server sServer;

// TODO Server implementations do not separate storage based on fabric
class ServerStorageDelegate : public PersistentStorageDelegate
{
CHIP_ERROR SyncGetKeyValue(const char * key, void * buffer, uint16_t & size) override
CHIP_ERROR SyncGetKeyValue(const CompressedFabricId fabricId, const char * key, void * buffer, uint16_t & size) override
{
ReturnErrorOnFailure(DeviceLayer::PersistedStorage::KeyValueStoreMgr().Get(key, buffer, size));
ChipLogProgress(AppServer, "Retrieved from server storage: %s", key);
return CHIP_NO_ERROR;
}

CHIP_ERROR SyncSetKeyValue(const char * key, const void * value, uint16_t size) override
CHIP_ERROR SyncSetKeyValue(const CompressedFabricId fabricId, const char * key, const void * value, uint16_t size) override
{
ReturnErrorOnFailure(DeviceLayer::PersistedStorage::KeyValueStoreMgr().Put(key, value, size));
ChipLogProgress(AppServer, "Saved into server storage: %s", key);
return CHIP_NO_ERROR;
}

CHIP_ERROR SyncDeleteKeyValue(const char * key) override
CHIP_ERROR SyncDeleteKeyValue(const CompressedFabricId fabricId, const char * key) override
{
ReturnErrorOnFailure(DeviceLayer::PersistedStorage::KeyValueStoreMgr().Delete(key));
ChipLogProgress(AppServer, "Deleted from server storage: %s", key);
Expand Down
15 changes: 9 additions & 6 deletions src/controller/CHIPDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,11 @@ CHIP_ERROR Device::Serialize(SerializedDevice & output)
CHIP_ZERO_AT(serializable);
CHIP_ZERO_AT(output);

serializable.mOpsCreds = mPairing;
serializable.mDeviceId = Encoding::LittleEndian::HostSwap64(mDeviceId);
serializable.mDevicePort = Encoding::LittleEndian::HostSwap16(mDeviceAddress.GetPort());
serializable.mFabricIndex = Encoding::LittleEndian::HostSwap16(mFabricIndex);
serializable.mOpsCreds = mPairing;
serializable.mDeviceId = Encoding::LittleEndian::HostSwap64(mDeviceId);
serializable.mDevicePort = Encoding::LittleEndian::HostSwap16(mDeviceAddress.GetPort());
serializable.mFabricIndex = Encoding::LittleEndian::HostSwap16(mFabricIndex);
serializable.mCompressedFabricId = Encoding::LittleEndian::HostSwap64(mCompressedFabricId);

// The connection state could be null if the device is moving from PASE connection to CASE connection.
// The device parameters (e.g. mDeviceOperationalCertProvisioned) are updated during this transition.
Expand Down Expand Up @@ -186,6 +187,7 @@ CHIP_ERROR Device::Deserialize(const SerializedDevice & input)
const uint16_t index = Encoding::LittleEndian::HostSwap16(serializable.mFabricIndex);
mLocalMessageCounter = Encoding::LittleEndian::HostSwap32(serializable.mLocalMessageCounter);
mPeerMessageCounter = Encoding::LittleEndian::HostSwap32(serializable.mPeerMessageCounter);
mCompressedFabricId = Encoding::LittleEndian::HostSwap64(serializable.mCompressedFabricId);

VerifyOrReturnError(CanCastTo<FabricIndex>(index), CHIP_ERROR_INVALID_ARGUMENT);
mFabricIndex = static_cast<FabricIndex>(index);
Expand Down Expand Up @@ -243,8 +245,9 @@ CHIP_ERROR Device::Persist()
ReturnErrorOnFailure(Serialize(serialized));

// TODO: no need to base-64 the serialized values AGAIN
PERSISTENT_KEY_OP(GetDeviceId(), kPairedDeviceKeyPrefix, key,
error = mStorageDelegate->SyncSetKeyValue(key, serialized.inner, sizeof(serialized.inner)));
PERSISTENT_KEY_OP(
GetDeviceId(), kPairedDeviceKeyPrefix, key,
error = mStorageDelegate->SyncSetKeyValue(mCompressedFabricId, key, serialized.inner, sizeof(serialized.inner)));
if (error != CHIP_NO_ERROR)
{
ChipLogError(Controller, "Failed to persist device %" CHIP_ERROR_FORMAT, error.Format());
Expand Down
20 changes: 15 additions & 5 deletions src/controller/CHIPDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class DLL_EXPORT Device : public Messaging::ExchangeDelegate, public SessionEsta
* still using them, it can lead to unknown behavior and crashes.
*
* @param[in] params Wrapper object for transport manager etc.
* @param[in] fabric Local administrator that's initializing this device object
* @param[in] fabric Local administrator that's initializing this device object
*/
void Init(ControllerDeviceInitParams params, FabricIndex fabric)
{
Expand All @@ -186,6 +186,14 @@ class DLL_EXPORT Device : public Messaging::ExchangeDelegate, public SessionEsta
#if CONFIG_NETWORK_LAYER_BLE
mBleLayer = params.bleLayer;
#endif
if (mFabricsTable != nullptr && mFabricIndex != kUndefinedFabricIndex)
{
FabricInfo * fabricInfo = mFabricsTable->FindFabricWithIndex(mFabricIndex);
if (fabricInfo != nullptr)
{
mCompressedFabricId = fabricInfo->GetCompressedFabricId();
}
}
}

/**
Expand All @@ -202,7 +210,7 @@ class DLL_EXPORT Device : public Messaging::ExchangeDelegate, public SessionEsta
* @param[in] params Wrapper object for transport manager etc.
* @param[in] deviceId Node ID of the device
* @param[in] peerAddress The location of the peer. MUST be of type Transport::Type::kUdp
* @param[in] fabric Local administrator that's initializing this device object
* @param[in] fabric Local administrator that's initializing this device object
*/
void Init(ControllerDeviceInitParams params, NodeId deviceId, const Transport::PeerAddress & peerAddress, FabricIndex fabric)
{
Expand Down Expand Up @@ -526,7 +534,8 @@ class DLL_EXPORT Device : public Messaging::ExchangeDelegate, public SessionEsta
static void OnOpenPairingWindowSuccessResponse(void * context);
static void OnOpenPairingWindowFailureResponse(void * context, uint8_t status);

FabricIndex mFabricIndex = kUndefinedFabricIndex;
CompressedFabricId mCompressedFabricId = kUndefinedCompressedFabricId;
FabricIndex mFabricIndex = kUndefinedFabricIndex;

FabricTable * mFabricsTable = nullptr;

Expand Down Expand Up @@ -607,8 +616,9 @@ typedef struct SerializableDevice
PASESessionSerializable mOpsCreds;
uint64_t mDeviceId; /* This field is serialized in LittleEndian byte order */
uint8_t mDeviceAddr[INET6_ADDRSTRLEN];
uint16_t mDevicePort; /* This field is serialized in LittleEndian byte order */
uint16_t mFabricIndex; /* This field is serialized in LittleEndian byte order */
uint16_t mDevicePort; /* This field is serialized in LittleEndian byte order */
uint16_t mFabricIndex; /* This field is serialized in LittleEndian byte order */
uint64_t mCompressedFabricId; /* This field is serialized in LittleEndian byte order */
uint8_t mDeviceTransport;
uint8_t mDeviceOperationalCertProvisioned;
uint8_t mInterfaceName[kMaxInterfaceName];
Expand Down
Loading

0 comments on commit 1259720

Please sign in to comment.