Skip to content

Commit

Permalink
samples: matter: Implemented bridge persistent storage module.
Browse files Browse the repository at this point in the history
Added a module that allows to persist information related to the
Matter bridge in the storage:

* Added persistent storage util that is a generic module
that can be used to handle custom keys hierarchy in the
various applications.
* Created bridge storage manager that uses persistent storage
util to describe data hierarchy of information stored for
Matter bridge device.

Signed-off-by: Kamil Kasperczyk <kamil.kasperczyk@nordicsemi.no>
  • Loading branch information
kkasperczyk-no committed Aug 21, 2023
1 parent 63a1d9c commit 368a76e
Show file tree
Hide file tree
Showing 9 changed files with 738 additions and 3 deletions.
2 changes: 2 additions & 0 deletions applications/matter_bridge/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ target_sources(app PRIVATE
src/bridge_shell.cpp
${COMMON_ROOT}/src/bridge/bridge_manager.cpp
${COMMON_ROOT}/src/bridge/bridged_device.cpp
${COMMON_ROOT}/src/bridge/bridge_storage_manager.cpp
src/zap-generated/IMClusterCommandHandler.cpp
src/zap-generated/callback-stub.cpp
${COMMON_ROOT}/src/led_widget.cpp
${COMMON_ROOT}/src/persistent_storage_util.cpp
)

if(CONFIG_BRIDGED_DEVICE_BT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class HumiditySensorDevice : public BridgedDevice {
uint16_t GetRelativeHumidityMeasurementClusterRevision() { return kRelativeHumidityMeasurementClusterRevision; }
uint32_t GetRelativeHumidityMeasurementFeatureMap() { return kRelativeHumidityMeasurementFeatureMap; }

BridgedDevice::DeviceType GetDeviceType() const override { return BridgedDevice::DeviceType::HumiditySensor; }
CHIP_ERROR HandleRead(chip::ClusterId clusterId, chip::AttributeId attributeId, uint8_t *buffer,
uint16_t maxReadLength) override;
CHIP_ERROR HandleReadRelativeHumidityMeasurement(chip::AttributeId attributeId, uint8_t *buffer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class OnOffLightDevice : public BridgedDevice {
uint16_t GetOnOffClusterRevision() { return kOnOffClusterRevision; }
uint32_t GetOnOffFeatureMap() { return kOnOffFeatureMap; }

BridgedDevice::DeviceType GetDeviceType() const override { return BridgedDevice::DeviceType::OnOffLight; }
CHIP_ERROR HandleRead(chip::ClusterId clusterId, chip::AttributeId attributeId, uint8_t *buffer,
uint16_t maxReadLength) override;
CHIP_ERROR HandleReadOnOff(chip::AttributeId attributeId, uint8_t *buffer, uint16_t maxReadLength);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class TemperatureSensorDevice : public BridgedDevice {
uint16_t GetTemperatureMeasurementClusterRevision() { return kTemperatureMeasurementClusterRevision; }
uint32_t GetTemperatureMeasurementFeatureMap() { return kTemperatureMeasurementFeatureMap; }

BridgedDevice::DeviceType GetDeviceType() const override
{
return BridgedDevice::DeviceType::TemperatureSensor;
}
CHIP_ERROR HandleRead(chip::ClusterId clusterId, chip::AttributeId attributeId, uint8_t *buffer,
uint16_t maxReadLength) override;
CHIP_ERROR HandleReadTemperatureMeasurement(chip::AttributeId attributeId, uint8_t *buffer,
Expand Down
162 changes: 162 additions & 0 deletions samples/matter/common/src/bridge/bridge_storage_manager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* Copyright (c) 2023 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#include "bridge_storage_manager.h"

namespace
{
template <class T> bool LoadDataToObject(PersistentStorageNode *node, T &data)
{
size_t readSize = 0;

bool result = PersistentStorage::Instance().Load(node, &data, sizeof(T), readSize);

return result;
}

PersistentStorageNode CreateIndexNode(uint8_t bridgedDeviceIndex, PersistentStorageNode *parent)
{
char index[BridgeStorageManager::kMaxIndexLength + 1] = { 0 };

snprintf(index, sizeof(index), "%d", bridgedDeviceIndex);

return PersistentStorageNode(index, strlen(index), parent);
}

} /* namespace */

bool BridgeStorageManager::StoreBridgedDevicesCount(uint8_t count)
{
return PersistentStorage::Instance().Store(&mBridgedDevicesCount, &count, sizeof(count));
}

bool BridgeStorageManager::LoadBridgedDevicesCount(uint8_t &count)
{
return LoadDataToObject(&mBridgedDevicesCount, count);
}

bool BridgeStorageManager::StoreBridgedDevicesIndexes(uint8_t *indexes, uint8_t count)
{
if (!indexes) {
return false;
}

return PersistentStorage::Instance().Store(&mBridgedDevicesIndexes, indexes, count);
}

bool BridgeStorageManager::LoadBridgedDevicesIndexes(uint8_t *indexes, uint8_t maxCount, size_t &count)
{
if (!indexes) {
return false;
}

return PersistentStorage::Instance().Load(&mBridgedDevicesIndexes, indexes, maxCount, count);
}

bool BridgeStorageManager::StoreBridgedDeviceEndpointId(uint16_t endpointId, uint8_t bridgedDeviceIndex)
{
PersistentStorageNode id = CreateIndexNode(bridgedDeviceIndex, &mBridgedDeviceEndpointId);

return PersistentStorage::Instance().Store(&id, &endpointId, sizeof(endpointId));
}

bool BridgeStorageManager::LoadBridgedDeviceEndpointId(uint16_t &endpointId, uint8_t bridgedDeviceIndex)
{
PersistentStorageNode id = CreateIndexNode(bridgedDeviceIndex, &mBridgedDeviceEndpointId);

return LoadDataToObject(&id, endpointId);
}

bool BridgeStorageManager::RemoveBridgedDeviceEndpointId(uint8_t bridgedDeviceIndex)
{
PersistentStorageNode id = CreateIndexNode(bridgedDeviceIndex, &mBridgedDeviceEndpointId);

return PersistentStorage::Instance().Remove(&id);
}

bool BridgeStorageManager::StoreBridgedDeviceNodeLabel(const char *label, size_t labelLength,
uint8_t bridgedDeviceIndex)
{
PersistentStorageNode id = CreateIndexNode(bridgedDeviceIndex, &mBridgedDeviceNodeLabel);

return PersistentStorage::Instance().Store(&id, label, labelLength);
}

bool BridgeStorageManager::LoadBridgedDeviceNodeLabel(char *label, size_t labelMaxLength, size_t &labelLength,
uint8_t bridgedDeviceIndex)
{
PersistentStorageNode id = CreateIndexNode(bridgedDeviceIndex, &mBridgedDeviceNodeLabel);

return PersistentStorage::Instance().Load(&id, label, labelMaxLength, labelLength);
}

bool BridgeStorageManager::RemoveBridgedDeviceNodeLabel(uint8_t bridgedDeviceIndex)
{
PersistentStorageNode id = CreateIndexNode(bridgedDeviceIndex, &mBridgedDeviceNodeLabel);

return PersistentStorage::Instance().Remove(&id);
}

bool BridgeStorageManager::StoreBridgedDeviceType(uint16_t deviceType, uint8_t bridgedDeviceIndex)
{
PersistentStorageNode id = CreateIndexNode(bridgedDeviceIndex, &mBridgedDeviceType);

return PersistentStorage::Instance().Store(&id, &deviceType, sizeof(deviceType));
}

bool BridgeStorageManager::LoadBridgedDeviceType(uint16_t &deviceType, uint8_t bridgedDeviceIndex)
{
PersistentStorageNode id = CreateIndexNode(bridgedDeviceIndex, &mBridgedDeviceType);

return LoadDataToObject(&id, deviceType);
}

bool BridgeStorageManager::RemoveBridgedDeviceType(uint8_t bridgedDeviceIndex)
{
PersistentStorageNode id = CreateIndexNode(bridgedDeviceIndex, &mBridgedDeviceType);

return PersistentStorage::Instance().Remove(&id);
}

bool BridgeStorageManager::StoreBridgedDevice(const BridgedDevice *device, uint8_t index)
{
if (!device) {
return false;
}

if (!StoreBridgedDeviceEndpointId(device->GetEndpointId(), index)) {
return false;
}

if (!StoreBridgedDeviceNodeLabel(device->GetNodeLabel(), strlen(device->GetNodeLabel()), index)) {
return false;
}

return StoreBridgedDeviceType(device->GetDeviceType(), index);
}

#ifdef CONFIG_BRIDGED_DEVICE_BT
bool BridgeStorageManager::StoreBtAddress(bt_addr_le_t addr, uint8_t bridgedDeviceIndex)
{
PersistentStorageNode id = CreateIndexNode(bridgedDeviceIndex, &mBtAddress);

return PersistentStorage::Instance().Store(&id, &addr, sizeof(addr));
}

bool BridgeStorageManager::LoadBtAddress(bt_addr_le_t &addr, uint8_t bridgedDeviceIndex)
{
PersistentStorageNode id = CreateIndexNode(bridgedDeviceIndex, &mBtAddress);

return LoadDataToObject(&id, addr);
}

bool BridgeStorageManager::RemoveBtAddress(uint8_t bridgedDeviceIndex)
{
PersistentStorageNode id = CreateIndexNode(bridgedDeviceIndex, &mBtAddress);

return PersistentStorage::Instance().Remove(&id);
}
#endif
Loading

0 comments on commit 368a76e

Please sign in to comment.