Skip to content

Commit 38f72b8

Browse files
markaj-nordicanangl
authored andcommitted
samples: matter: Multiple door lock's schedules refinements
* increase maximum number of secure entries that can be stored * align the maximum asset size for scheduled access feature * align the OT and Matter stack sizes * allow for re-setting of the same schedule with different data * added missing serialization of mAvailable flag - it was not stored because of that * refactor the *Schedule structs to avoid boiler plate code * decrease the log level for schedule prints Signed-off-by: Marcin Kajor <[email protected]>
1 parent ad60249 commit 38f72b8

File tree

6 files changed

+122
-187
lines changed

6 files changed

+122
-187
lines changed

samples/matter/common/src/persistent_storage/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ if NCS_SAMPLE_MATTER_SECURE_STORAGE_BACKEND
2424

2525
config NCS_SAMPLE_MATTER_SECURE_STORAGE_MAX_ENTRY_NUMBER
2626
int "Maximum number of entries that can be stored securely"
27-
default 30
27+
default 64
2828

2929
config NCS_SAMPLE_MATTER_SECURE_STORAGE_PSA_KEY_VALUE_OFFSET
3030
hex "The PSA key offset dedicated for Matter application"

samples/matter/lock/Kconfig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,22 @@ config NCS_SAMPLE_MATTER_SECURE_STORAGE_BACKEND
148148
config NCS_SAMPLE_MATTER_SETTINGS_STORAGE_BACKEND
149149
default n if !CHIP_WIFI
150150

151+
# Increase the storage capacity if the schedules are enabled with secure storage.
152+
# This also implies increasing of the OT and Matter stacks (the latter is tuned in chip_project_config.h)
153+
# because some operations performed during commissioning seem to allocate stack buffers based on the
154+
# maximum possible secure asset size.
155+
if LOCK_SCHEDULES && NCS_SAMPLE_MATTER_SECURE_STORAGE_BACKEND
156+
157+
config NCS_SAMPLE_MATTER_SECURE_STORAGE_MAX_ENTRY_NUMBER
158+
default 128
159+
160+
config TRUSTED_STORAGE_BACKEND_AEAD_MAX_DATA_SIZE
161+
default 3072
162+
163+
config OPENTHREAD_THREAD_STACK_SIZE
164+
default 5120
165+
endif
166+
151167
source "${ZEPHYR_CONNECTEDHOMEIP_MODULE_DIR}/config/nrfconnect/chip-module/Kconfig.features"
152168
source "${ZEPHYR_CONNECTEDHOMEIP_MODULE_DIR}/config/nrfconnect/chip-module/Kconfig.defaults"
153169
source "${ZEPHYR_NRF_MODULE_DIR}/samples/matter/common/src/Kconfig"

samples/matter/lock/src/access/access_data_types.cpp

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,6 @@ CHIP_ERROR User::Deserialize(const void *buff, size_t buffSize)
180180

181181
CHIP_ERROR WeekDaySchedule::FillFromPlugin(EmberAfPluginDoorLockWeekDaySchedule &plugin)
182182
{
183-
static_assert(sizeof(EmberAfPluginDoorLockWeekDaySchedule) == RequiredBufferSize());
184-
185183
mData.mFields.mDaysMask = static_cast<uint8_t>(plugin.daysMask);
186184
mData.mFields.mStartHour = plugin.startHour;
187185
mData.mFields.mStartMinute = plugin.startMinute;
@@ -193,8 +191,6 @@ CHIP_ERROR WeekDaySchedule::FillFromPlugin(EmberAfPluginDoorLockWeekDaySchedule
193191

194192
CHIP_ERROR YearDaySchedule::FillFromPlugin(EmberAfPluginDoorLockYearDaySchedule &plugin)
195193
{
196-
static_assert(sizeof(EmberAfPluginDoorLockYearDaySchedule) == RequiredBufferSize());
197-
198194
mData.mFields.mLocalStartTime = plugin.localStartTime;
199195
mData.mFields.mLocalEndTime = plugin.localEndTime;
200196

@@ -203,8 +199,6 @@ CHIP_ERROR YearDaySchedule::FillFromPlugin(EmberAfPluginDoorLockYearDaySchedule
203199

204200
CHIP_ERROR HolidaySchedule::FillFromPlugin(EmberAfPluginDoorLockHolidaySchedule &plugin)
205201
{
206-
static_assert(sizeof(EmberAfPluginDoorLockHolidaySchedule) == RequiredBufferSize());
207-
208202
mData.mFields.mLocalStartTime = plugin.localStartTime;
209203
mData.mFields.mLocalEndTime = plugin.localEndTime;
210204
mData.mFields.mOperatingMode = static_cast<uint8_t>(plugin.operatingMode);
@@ -214,8 +208,6 @@ CHIP_ERROR HolidaySchedule::FillFromPlugin(EmberAfPluginDoorLockHolidaySchedule
214208

215209
CHIP_ERROR WeekDaySchedule::ConvertToPlugin(EmberAfPluginDoorLockWeekDaySchedule &plugin) const
216210
{
217-
static_assert(sizeof(EmberAfPluginDoorLockWeekDaySchedule) == RequiredBufferSize());
218-
219211
plugin.daysMask = static_cast<DaysMaskMap>(mData.mFields.mDaysMask);
220212
plugin.startHour = mData.mFields.mStartHour;
221213
plugin.startMinute = mData.mFields.mStartMinute;
@@ -227,8 +219,6 @@ CHIP_ERROR WeekDaySchedule::ConvertToPlugin(EmberAfPluginDoorLockWeekDaySchedule
227219

228220
CHIP_ERROR YearDaySchedule::ConvertToPlugin(EmberAfPluginDoorLockYearDaySchedule &plugin) const
229221
{
230-
static_assert(sizeof(EmberAfPluginDoorLockYearDaySchedule) == RequiredBufferSize());
231-
232222
plugin.localStartTime = mData.mFields.mLocalStartTime;
233223
plugin.localEndTime = mData.mFields.mLocalEndTime;
234224

@@ -237,93 +227,13 @@ CHIP_ERROR YearDaySchedule::ConvertToPlugin(EmberAfPluginDoorLockYearDaySchedule
237227

238228
CHIP_ERROR HolidaySchedule::ConvertToPlugin(EmberAfPluginDoorLockHolidaySchedule &plugin) const
239229
{
240-
static_assert(sizeof(EmberAfPluginDoorLockHolidaySchedule) == RequiredBufferSize());
241-
242230
plugin.localStartTime = mData.mFields.mLocalStartTime;
243231
plugin.localEndTime = mData.mFields.mLocalEndTime;
244232
plugin.operatingMode = static_cast<OperatingModeEnum>(mData.mFields.mOperatingMode);
245233

246234
return CHIP_NO_ERROR;
247235
}
248236

249-
size_t WeekDaySchedule::Serialize(void *buff, size_t buffSize)
250-
{
251-
if (!buff || buffSize < RequiredBufferSize()) {
252-
return 0;
253-
}
254-
255-
memcpy(buff, mData.mRaw, sizeof(mData.mRaw));
256-
257-
return sizeof(mData.mRaw);
258-
}
259-
260-
size_t YearDaySchedule::Serialize(void *buff, size_t buffSize)
261-
{
262-
if (!buff || buffSize < RequiredBufferSize()) {
263-
return 0;
264-
}
265-
266-
memcpy(buff, mData.mRaw, sizeof(mData.mRaw));
267-
268-
return sizeof(mData.mRaw);
269-
}
270-
271-
size_t HolidaySchedule::Serialize(void *buff, size_t buffSize)
272-
{
273-
if (!buff || buffSize < RequiredBufferSize()) {
274-
return 0;
275-
}
276-
277-
memcpy(buff, mData.mRaw, sizeof(mData.mRaw));
278-
279-
return sizeof(mData.mRaw);
280-
}
281-
282-
CHIP_ERROR WeekDaySchedule::Deserialize(const void *buff, size_t buffSize)
283-
{
284-
if (!buff) {
285-
return CHIP_ERROR_INVALID_ARGUMENT;
286-
}
287-
288-
if (buffSize > RequiredBufferSize()) {
289-
return CHIP_ERROR_BUFFER_TOO_SMALL;
290-
}
291-
292-
memcpy(mData.mRaw, buff, sizeof(mData.mRaw));
293-
294-
return CHIP_NO_ERROR;
295-
}
296-
297-
CHIP_ERROR YearDaySchedule::Deserialize(const void *buff, size_t buffSize)
298-
{
299-
if (!buff) {
300-
return CHIP_ERROR_INVALID_ARGUMENT;
301-
}
302-
303-
if (buffSize > RequiredBufferSize()) {
304-
return CHIP_ERROR_BUFFER_TOO_SMALL;
305-
}
306-
307-
memcpy(mData.mRaw, buff, sizeof(mData.mRaw));
308-
309-
return CHIP_NO_ERROR;
310-
}
311-
312-
CHIP_ERROR HolidaySchedule::Deserialize(const void *buff, size_t buffSize)
313-
{
314-
if (!buff) {
315-
return CHIP_ERROR_INVALID_ARGUMENT;
316-
}
317-
318-
if (buffSize > RequiredBufferSize()) {
319-
return CHIP_ERROR_BUFFER_TOO_SMALL;
320-
}
321-
322-
memcpy(mData.mRaw, buff, sizeof(mData.mRaw));
323-
324-
return CHIP_NO_ERROR;
325-
}
326-
327237
#endif /* CONFIG_LOCK_SCHEDULES */
328238

329239
} /* namespace DoorLockData */

samples/matter/lock/src/access/access_data_types.h

Lines changed: 69 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,71 @@ struct Credential {
117117
*/
118118
CHIP_ERROR Deserialize(const void *buff, size_t buffSize);
119119
};
120+
/* A base class for specific schedule types that implements the common schedule serialization. */
121+
template <typename T> class Schedule {
122+
public:
123+
/**
124+
* @brief Serialize all fields into data buffer.
125+
*
126+
* `buffSize` must be set to at least the RequiredBufferSize size.
127+
*
128+
* @param buff buffer to store serialized data.
129+
* @param buffSize size of input buffer.
130+
* @return size_t output serialized data length, 0 value means error.
131+
*/
132+
size_t Serialize(void *buff, size_t buffSize)
133+
{
134+
if (!buff || buffSize < Impl()->RequiredBufferSize()) {
135+
return 0;
136+
}
137+
138+
size_t offset = 0;
139+
140+
pack(buff, Impl()->mData.mRaw, sizeof(Impl()->mData.mRaw), offset);
141+
pack(buff, &(Impl()->mAvailable), sizeof(Impl()->mAvailable), offset);
142+
143+
return offset;
144+
}
145+
146+
/**
147+
* @brief Deserialize all fields from given buffer.
148+
*
149+
* `buffSize` must be set to at least the RequiredBufferSize size.
150+
*
151+
* @param buff buffer containing serialized data (by invoking serialized method).
152+
* @param buffSize size of output buffer.
153+
* @return CHIP_ERROR_BUFFER_TOO_SMALL if provided buffSize is too small.
154+
* @return CHIP_ERROR_INVALID_ARGUMENT if arguments are wrong.
155+
* @return CHIP_NO_ERROR if deserialization has been finished successfully.
156+
*/
157+
CHIP_ERROR Deserialize(const void *buff, size_t buffSize)
158+
{
159+
if (!buff) {
160+
return CHIP_ERROR_INVALID_ARGUMENT;
161+
}
162+
163+
if (buffSize > Impl()->RequiredBufferSize()) {
164+
return CHIP_ERROR_BUFFER_TOO_SMALL;
165+
}
166+
167+
size_t offset = 0;
168+
169+
unpack(Impl()->mData.mRaw, sizeof(Impl()->mData.mRaw), buff, offset);
170+
unpack(&(Impl()->mAvailable), sizeof(Impl()->mAvailable), buff, offset);
171+
172+
return CHIP_NO_ERROR;
173+
}
120174

121-
struct WeekDaySchedule {
175+
private:
176+
/**
177+
* @brief Get the derived class handle.
178+
*
179+
* @return Pointer to the derived class.
180+
*/
181+
T *Impl() { return static_cast<T *>(this); };
182+
};
183+
184+
struct WeekDaySchedule : public Schedule<WeekDaySchedule> {
122185
union Data {
123186
struct Fields {
124187
uint8_t mDaysMask;
@@ -141,7 +204,7 @@ struct WeekDaySchedule {
141204
*
142205
* @return size of single credential entry.
143206
*/
144-
constexpr static size_t RequiredBufferSize() { return sizeof(Data); }
207+
constexpr static size_t RequiredBufferSize() { return sizeof(Data) + sizeof(bool); }
145208

146209
/**
147210
* @brief fill User entry with data from the EmberAfPluginDoorLockWeekDaySchedule plugin
@@ -158,33 +221,9 @@ struct WeekDaySchedule {
158221
* @return CHIP_NO_ERROR if conversion has been finished successfully.
159222
*/
160223
CHIP_ERROR ConvertToPlugin(EmberAfPluginDoorLockWeekDaySchedule &plugin) const;
161-
162-
/**
163-
* @brief Serialize all fields into data buffer.
164-
*
165-
* `buffSize` must be set to at least the RequiredBufferSize size.
166-
*
167-
* @param buff buffer to store serialized data.
168-
* @param buffSize size of input buffer.
169-
* @return size_t output serialized data length, 0 value means error.
170-
*/
171-
size_t Serialize(void *buff, size_t buffSize);
172-
173-
/**
174-
* @brief Deserialize all fields from given buffer.
175-
*
176-
* `buffSize` must be set to at least the RequiredBufferSize size.
177-
*
178-
* @param buff buffer containing serialized data (by invoking serialized method).
179-
* @param buffSize size of output buffer.
180-
* @return CHIP_ERROR_BUFFER_TOO_SMALL if provided buffSize is too small.
181-
* @return CHIP_ERROR_INVALID_ARGUMENT if arguments are wrong.
182-
* @return CHIP_NO_ERROR if deserialization has been finished successfully.
183-
*/
184-
CHIP_ERROR Deserialize(const void *buff, size_t buffSize);
185224
};
186225

187-
struct YearDaySchedule {
226+
struct YearDaySchedule : public Schedule<YearDaySchedule> {
188227
union Data {
189228
struct Fields {
190229
uint32_t mLocalStartTime;
@@ -204,7 +243,7 @@ struct YearDaySchedule {
204243
*
205244
* @return size of single credential entry.
206245
*/
207-
constexpr static size_t RequiredBufferSize() { return sizeof(Data); }
246+
constexpr static size_t RequiredBufferSize() { return sizeof(Data) + sizeof(bool); }
208247

209248
/**
210249
* @brief fill User entry with data from one the EmberAfPluginDoorLockYearDaySchedule plugin.
@@ -221,33 +260,9 @@ struct YearDaySchedule {
221260
* @return CHIP_NO_ERROR if conversion has been finished successfully.
222261
*/
223262
CHIP_ERROR ConvertToPlugin(EmberAfPluginDoorLockYearDaySchedule &plugin) const;
224-
225-
/**
226-
* @brief Serialize all fields into data buffer.
227-
*
228-
* `buffSize` must be set to at least the RequiredBufferSize size.
229-
*
230-
* @param buff buffer to store serialized data.
231-
* @param buffSize size of input buffer.
232-
* @return size_t output serialized data length, 0 value means error.
233-
*/
234-
size_t Serialize(void *buff, size_t buffSize);
235-
236-
/**
237-
* @brief Deserialize all fields from given buffer.
238-
*
239-
* `buffSize` must be set to at least the RequiredBufferSize size.
240-
*
241-
* @param buff buffer containing serialized data (by invoking serialized method).
242-
* @param buffSize size of output buffer.
243-
* @return CHIP_ERROR_BUFFER_TOO_SMALL if provided buffSize is too small.
244-
* @return CHIP_ERROR_INVALID_ARGUMENT if arguments are wrong.
245-
* @return CHIP_NO_ERROR if deserialization has been finished successfully.
246-
*/
247-
CHIP_ERROR Deserialize(const void *buff, size_t buffSize);
248263
};
249264

250-
struct HolidaySchedule {
265+
struct HolidaySchedule : public Schedule<HolidaySchedule> {
251266
union Data {
252267
struct Fields {
253268
uint32_t mLocalStartTime;
@@ -268,7 +283,7 @@ struct HolidaySchedule {
268283
*
269284
* @return size of single credential entry.
270285
*/
271-
constexpr static size_t RequiredBufferSize() { return sizeof(Data); }
286+
constexpr static size_t RequiredBufferSize() { return sizeof(Data) + sizeof(bool); }
272287

273288
/**
274289
* @brief fill User entry with data from one the EmberAfPluginDoorLockHolidaySchedule plugin.
@@ -285,30 +300,6 @@ struct HolidaySchedule {
285300
* @return CHIP_NO_ERROR if conversion has been finished successfully.
286301
*/
287302
CHIP_ERROR ConvertToPlugin(EmberAfPluginDoorLockHolidaySchedule &plugin) const;
288-
289-
/**
290-
* @brief Serialize all fields into data buffer.
291-
*
292-
* `buffSize` must be set to at least the RequiredBufferSize size.
293-
*
294-
* @param buff buffer to store serialized data.
295-
* @param buffSize size of input buffer.
296-
* @return size_t output serialized data length, 0 value means error.
297-
*/
298-
size_t Serialize(void *buff, size_t buffSize);
299-
300-
/**
301-
* @brief Deserialize all fields from given buffer.
302-
*
303-
* `buffSize` must be set to at least the RequiredBufferSize size.
304-
*
305-
* @param buff buffer containing serialized data (by invoking serialized method).
306-
* @param buffSize size of output buffer.
307-
* @return CHIP_ERROR_BUFFER_TOO_SMALL if provided buffSize is too small.
308-
* @return CHIP_ERROR_INVALID_ARGUMENT if arguments are wrong.
309-
* @return CHIP_NO_ERROR if deserialization has been finished successfully.
310-
*/
311-
CHIP_ERROR Deserialize(const void *buff, size_t buffSize);
312303
};
313304

314305
struct User {

0 commit comments

Comments
 (0)