Skip to content

Commit cbf4925

Browse files
adigieArekBalysNordic
authored andcommitted
applications: matter_bridge: Add BridgedDevice version 1
* Add BridgedDeviceV1 structure * Rename BridgedDevice to BridgedDeviceV2 * Add BridgedDevice alias for BridgedDeviceV2 * Add LoadBridgedDevice specialization for BridgedDeviceV1 Signed-off-by: Adrian Gielniewski <[email protected]>
1 parent f4246fd commit cbf4925

File tree

2 files changed

+81
-4
lines changed

2 files changed

+81
-4
lines changed

samples/matter/common/src/bridge/bridge_storage_manager.cpp

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,82 @@ Nrf::PersistentStorageNode CreateIndexNode(uint8_t bridgedDeviceIndex, Nrf::Pers
3636
namespace Nrf
3737
{
3838

39-
template <> bool BridgeStorageManager::LoadBridgedDevice(BridgedDevice &device, uint8_t index)
39+
template <> bool BridgeStorageManager::LoadBridgedDevice(BridgedDeviceV1 &device, uint8_t index)
4040
{
4141
Nrf::PersistentStorageNode id = CreateIndexNode(index, &mBridgedDevice);
4242
size_t readSize = 0;
43-
uint8_t buffer[sizeof(BridgedDevice) + kMaxUserDataSize];
43+
uint8_t buffer[sizeof(BridgedDeviceV1) + kMaxUserDataSize];
44+
uint16_t counter = 0;
45+
const uint8_t mandatoryItemsSize =
46+
sizeof(device.mEndpointId) + sizeof(device.mDeviceType) + sizeof(device.mNodeLabelLength);
47+
48+
if (!Nrf::GetPersistentStorage().NonSecureLoad(&id, buffer, sizeof(BridgedDeviceV1) + kMaxUserDataSize,
49+
readSize)) {
50+
return false;
51+
}
52+
53+
/* Validate that read size is big enough to include mandatory data. */
54+
if (readSize < mandatoryItemsSize) {
55+
return false;
56+
}
57+
58+
/* Deserialize data and copy it from buffer into structure's fields. */
59+
memcpy(&device.mEndpointId, buffer, sizeof(device.mEndpointId));
60+
counter += sizeof(device.mEndpointId);
61+
memcpy(&device.mDeviceType, buffer + counter, sizeof(device.mDeviceType));
62+
counter += sizeof(device.mDeviceType);
63+
memcpy(&device.mNodeLabelLength, buffer + counter, sizeof(device.mNodeLabelLength));
64+
counter += sizeof(device.mNodeLabelLength);
65+
66+
/* Validate that read size is big enough to include all expected data. */
67+
if (readSize < mandatoryItemsSize + device.mNodeLabelLength) {
68+
return false;
69+
}
70+
71+
memcpy(device.mNodeLabel, buffer + counter, device.mNodeLabelLength);
72+
counter += device.mNodeLabelLength;
73+
74+
/* Check if user prepared a buffer for reading user data. It can be nullptr if not needed. */
75+
if (!device.mUserData) {
76+
device.mUserDataSize = 0;
77+
return true;
78+
}
79+
80+
uint8_t inUserDataSize = device.mUserDataSize;
81+
/* Validate if user buffer size is big enough to fit the stored user data size. */
82+
if (readSize < counter + sizeof(device.mUserDataSize)) {
83+
return false;
84+
}
85+
86+
memcpy(&device.mUserDataSize, buffer + counter, sizeof(device.mUserDataSize));
87+
counter += sizeof(device.mUserDataSize);
88+
89+
/* Validate that user data size value read from the storage is not bigger than the one expected by the user. */
90+
if (inUserDataSize < device.mUserDataSize) {
91+
return false;
92+
}
93+
94+
/* Validate that read size is big enough to include all expected data. */
95+
if (readSize < counter + device.mUserDataSize) {
96+
return false;
97+
}
98+
99+
memcpy(device.mUserData, buffer + counter, device.mUserDataSize);
100+
counter += device.mUserDataSize;
101+
102+
return true;
103+
}
104+
105+
template <> bool BridgeStorageManager::LoadBridgedDevice(BridgedDeviceV2 &device, uint8_t index)
106+
{
107+
Nrf::PersistentStorageNode id = CreateIndexNode(index, &mBridgedDevice);
108+
size_t readSize = 0;
109+
uint8_t buffer[sizeof(BridgedDeviceV2) + kMaxUserDataSize];
44110
uint16_t counter = 0;
45111
const uint8_t mandatoryItemsSize =
46112
sizeof(device.mEndpointId) + sizeof(device.mDeviceType) + sizeof(device.mUniqueIDLength);
47113

48-
if (!Nrf::GetPersistentStorage().NonSecureLoad(&id, buffer, sizeof(BridgedDevice) + kMaxUserDataSize,
114+
if (!Nrf::GetPersistentStorage().NonSecureLoad(&id, buffer, sizeof(BridgedDeviceV2) + kMaxUserDataSize,
49115
readSize)) {
50116
return false;
51117
}

samples/matter/common/src/bridge/bridge_storage_manager.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,16 @@ class BridgeStorageManager {
3434
public:
3535
static inline constexpr auto kMaxUserDataSize = 128u;
3636

37-
struct BridgedDevice {
37+
struct BridgedDeviceV1 {
38+
uint16_t mEndpointId;
39+
uint16_t mDeviceType;
40+
size_t mNodeLabelLength;
41+
char mNodeLabel[MatterBridgedDevice::kNodeLabelSize] = { 0 };
42+
size_t mUserDataSize = 0;
43+
uint8_t *mUserData = nullptr;
44+
};
45+
46+
struct BridgedDeviceV2 {
3847
uint16_t mEndpointId;
3948
uint16_t mDeviceType;
4049
size_t mUniqueIDLength;
@@ -45,6 +54,8 @@ class BridgeStorageManager {
4554
uint8_t *mUserData = nullptr;
4655
};
4756

57+
using BridgedDevice = BridgedDeviceV2;
58+
4859
static constexpr auto kMaxIndexLength = 3;
4960

5061
BridgeStorageManager()

0 commit comments

Comments
 (0)