Skip to content

Commit

Permalink
samples: matter: Multiple door lock's schedules refinements
Browse files Browse the repository at this point in the history
* 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 <marcin.kajor@nordicsemi.no>
  • Loading branch information
markaj-nordic authored and nordicjm committed Aug 27, 2024
1 parent 7066e6e commit b60eb49
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 187 deletions.
2 changes: 1 addition & 1 deletion samples/matter/common/src/persistent_storage/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ if NCS_SAMPLE_MATTER_SECURE_STORAGE_BACKEND

config NCS_SAMPLE_MATTER_SECURE_STORAGE_MAX_ENTRY_NUMBER
int "Maximum number of entries that can be stored securely"
default 30
default 64

config NCS_SAMPLE_MATTER_SECURE_STORAGE_PSA_KEY_VALUE_OFFSET
hex "The PSA key offset dedicated for Matter application"
Expand Down
20 changes: 20 additions & 0 deletions samples/matter/lock/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,26 @@ config NCS_SAMPLE_MATTER_SECURE_STORAGE_BACKEND
config NCS_SAMPLE_MATTER_SETTINGS_STORAGE_BACKEND
default n if !CHIP_WIFI

# Increase the storage capacity if the schedules are enabled with secure storage
# This also implies increasing of the OT and Matter stacks because some operations
# performed during commissioning seem to allocate stack buffers based on the
# maximum possible secure asset size.
if LOCK_SCHEDULES && NCS_SAMPLE_MATTER_SECURE_STORAGE_BACKEND

config NCS_SAMPLE_MATTER_SECURE_STORAGE_MAX_ENTRY_NUMBER
default 128

config TRUSTED_STORAGE_BACKEND_AEAD_MAX_DATA_SIZE
default 3072

config OPENTHREAD_THREAD_STACK_SIZE
default 5120

config CHIP_TASK_STACK_SIZE
default 9216

endif

source "${ZEPHYR_CONNECTEDHOMEIP_MODULE_DIR}/config/nrfconnect/chip-module/Kconfig.features"
source "${ZEPHYR_CONNECTEDHOMEIP_MODULE_DIR}/config/nrfconnect/chip-module/Kconfig.defaults"
source "${ZEPHYR_NRF_MODULE_DIR}/samples/matter/common/src/Kconfig"
Expand Down
90 changes: 0 additions & 90 deletions samples/matter/lock/src/access/access_data_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,6 @@ CHIP_ERROR User::Deserialize(const void *buff, size_t buffSize)

CHIP_ERROR WeekDaySchedule::FillFromPlugin(EmberAfPluginDoorLockWeekDaySchedule &plugin)
{
static_assert(sizeof(EmberAfPluginDoorLockWeekDaySchedule) == RequiredBufferSize());

mData.mFields.mDaysMask = static_cast<uint8_t>(plugin.daysMask);
mData.mFields.mStartHour = plugin.startHour;
mData.mFields.mStartMinute = plugin.startMinute;
Expand All @@ -193,8 +191,6 @@ CHIP_ERROR WeekDaySchedule::FillFromPlugin(EmberAfPluginDoorLockWeekDaySchedule

CHIP_ERROR YearDaySchedule::FillFromPlugin(EmberAfPluginDoorLockYearDaySchedule &plugin)
{
static_assert(sizeof(EmberAfPluginDoorLockYearDaySchedule) == RequiredBufferSize());

mData.mFields.mLocalStartTime = plugin.localStartTime;
mData.mFields.mLocalEndTime = plugin.localEndTime;

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

CHIP_ERROR HolidaySchedule::FillFromPlugin(EmberAfPluginDoorLockHolidaySchedule &plugin)
{
static_assert(sizeof(EmberAfPluginDoorLockHolidaySchedule) == RequiredBufferSize());

mData.mFields.mLocalStartTime = plugin.localStartTime;
mData.mFields.mLocalEndTime = plugin.localEndTime;
mData.mFields.mOperatingMode = static_cast<uint8_t>(plugin.operatingMode);
Expand All @@ -214,8 +208,6 @@ CHIP_ERROR HolidaySchedule::FillFromPlugin(EmberAfPluginDoorLockHolidaySchedule

CHIP_ERROR WeekDaySchedule::ConvertToPlugin(EmberAfPluginDoorLockWeekDaySchedule &plugin) const
{
static_assert(sizeof(EmberAfPluginDoorLockWeekDaySchedule) == RequiredBufferSize());

plugin.daysMask = static_cast<DaysMaskMap>(mData.mFields.mDaysMask);
plugin.startHour = mData.mFields.mStartHour;
plugin.startMinute = mData.mFields.mStartMinute;
Expand All @@ -227,8 +219,6 @@ CHIP_ERROR WeekDaySchedule::ConvertToPlugin(EmberAfPluginDoorLockWeekDaySchedule

CHIP_ERROR YearDaySchedule::ConvertToPlugin(EmberAfPluginDoorLockYearDaySchedule &plugin) const
{
static_assert(sizeof(EmberAfPluginDoorLockYearDaySchedule) == RequiredBufferSize());

plugin.localStartTime = mData.mFields.mLocalStartTime;
plugin.localEndTime = mData.mFields.mLocalEndTime;

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

CHIP_ERROR HolidaySchedule::ConvertToPlugin(EmberAfPluginDoorLockHolidaySchedule &plugin) const
{
static_assert(sizeof(EmberAfPluginDoorLockHolidaySchedule) == RequiredBufferSize());

plugin.localStartTime = mData.mFields.mLocalStartTime;
plugin.localEndTime = mData.mFields.mLocalEndTime;
plugin.operatingMode = static_cast<OperatingModeEnum>(mData.mFields.mOperatingMode);

return CHIP_NO_ERROR;
}

size_t WeekDaySchedule::Serialize(void *buff, size_t buffSize)
{
if (!buff || buffSize < RequiredBufferSize()) {
return 0;
}

memcpy(buff, mData.mRaw, sizeof(mData.mRaw));

return sizeof(mData.mRaw);
}

size_t YearDaySchedule::Serialize(void *buff, size_t buffSize)
{
if (!buff || buffSize < RequiredBufferSize()) {
return 0;
}

memcpy(buff, mData.mRaw, sizeof(mData.mRaw));

return sizeof(mData.mRaw);
}

size_t HolidaySchedule::Serialize(void *buff, size_t buffSize)
{
if (!buff || buffSize < RequiredBufferSize()) {
return 0;
}

memcpy(buff, mData.mRaw, sizeof(mData.mRaw));

return sizeof(mData.mRaw);
}

CHIP_ERROR WeekDaySchedule::Deserialize(const void *buff, size_t buffSize)
{
if (!buff) {
return CHIP_ERROR_INVALID_ARGUMENT;
}

if (buffSize > RequiredBufferSize()) {
return CHIP_ERROR_BUFFER_TOO_SMALL;
}

memcpy(mData.mRaw, buff, sizeof(mData.mRaw));

return CHIP_NO_ERROR;
}

CHIP_ERROR YearDaySchedule::Deserialize(const void *buff, size_t buffSize)
{
if (!buff) {
return CHIP_ERROR_INVALID_ARGUMENT;
}

if (buffSize > RequiredBufferSize()) {
return CHIP_ERROR_BUFFER_TOO_SMALL;
}

memcpy(mData.mRaw, buff, sizeof(mData.mRaw));

return CHIP_NO_ERROR;
}

CHIP_ERROR HolidaySchedule::Deserialize(const void *buff, size_t buffSize)
{
if (!buff) {
return CHIP_ERROR_INVALID_ARGUMENT;
}

if (buffSize > RequiredBufferSize()) {
return CHIP_ERROR_BUFFER_TOO_SMALL;
}

memcpy(mData.mRaw, buff, sizeof(mData.mRaw));

return CHIP_NO_ERROR;
}

#endif /* CONFIG_LOCK_SCHEDULES */

} /* namespace DoorLockData */
147 changes: 69 additions & 78 deletions samples/matter/lock/src/access/access_data_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,71 @@ struct Credential {
*/
CHIP_ERROR Deserialize(const void *buff, size_t buffSize);
};
/* A base class for specific schedule types that implements the common schedule serialization. */
template <typename T> class Schedule {
public:
/**
* @brief Serialize all fields into data buffer.
*
* `buffSize` must be set to at least the RequiredBufferSize size.
*
* @param buff buffer to store serialized data.
* @param buffSize size of input buffer.
* @return size_t output serialized data length, 0 value means error.
*/
size_t Serialize(void *buff, size_t buffSize)
{
if (!buff || buffSize < Impl()->RequiredBufferSize()) {
return 0;
}

size_t offset = 0;

pack(buff, Impl()->mData.mRaw, sizeof(Impl()->mData.mRaw), offset);
pack(buff, &(Impl()->mAvailable), sizeof(Impl()->mAvailable), offset);

return offset;
}

/**
* @brief Deserialize all fields from given buffer.
*
* `buffSize` must be set to at least the RequiredBufferSize size.
*
* @param buff buffer containing serialized data (by invoking serialized method).
* @param buffSize size of output buffer.
* @return CHIP_ERROR_BUFFER_TOO_SMALL if provided buffSize is too small.
* @return CHIP_ERROR_INVALID_ARGUMENT if arguments are wrong.
* @return CHIP_NO_ERROR if deserialization has been finished successfully.
*/
CHIP_ERROR Deserialize(const void *buff, size_t buffSize)
{
if (!buff) {
return CHIP_ERROR_INVALID_ARGUMENT;
}

if (buffSize > Impl()->RequiredBufferSize()) {
return CHIP_ERROR_BUFFER_TOO_SMALL;
}

size_t offset = 0;

unpack(Impl()->mData.mRaw, sizeof(Impl()->mData.mRaw), buff, offset);
unpack(&(Impl()->mAvailable), sizeof(Impl()->mAvailable), buff, offset);

return CHIP_NO_ERROR;
}

struct WeekDaySchedule {
private:
/**
* @brief Get the derived class handle.
*
* @return Pointer to the derived class.
*/
T *Impl() { return static_cast<T *>(this); };
};

struct WeekDaySchedule : public Schedule<WeekDaySchedule> {
union Data {
struct Fields {
uint8_t mDaysMask;
Expand All @@ -141,7 +204,7 @@ struct WeekDaySchedule {
*
* @return size of single credential entry.
*/
constexpr static size_t RequiredBufferSize() { return sizeof(Data); }
constexpr static size_t RequiredBufferSize() { return sizeof(Data) + sizeof(bool); }

/**
* @brief fill User entry with data from the EmberAfPluginDoorLockWeekDaySchedule plugin
Expand All @@ -158,33 +221,9 @@ struct WeekDaySchedule {
* @return CHIP_NO_ERROR if conversion has been finished successfully.
*/
CHIP_ERROR ConvertToPlugin(EmberAfPluginDoorLockWeekDaySchedule &plugin) const;

/**
* @brief Serialize all fields into data buffer.
*
* `buffSize` must be set to at least the RequiredBufferSize size.
*
* @param buff buffer to store serialized data.
* @param buffSize size of input buffer.
* @return size_t output serialized data length, 0 value means error.
*/
size_t Serialize(void *buff, size_t buffSize);

/**
* @brief Deserialize all fields from given buffer.
*
* `buffSize` must be set to at least the RequiredBufferSize size.
*
* @param buff buffer containing serialized data (by invoking serialized method).
* @param buffSize size of output buffer.
* @return CHIP_ERROR_BUFFER_TOO_SMALL if provided buffSize is too small.
* @return CHIP_ERROR_INVALID_ARGUMENT if arguments are wrong.
* @return CHIP_NO_ERROR if deserialization has been finished successfully.
*/
CHIP_ERROR Deserialize(const void *buff, size_t buffSize);
};

struct YearDaySchedule {
struct YearDaySchedule : public Schedule<YearDaySchedule> {
union Data {
struct Fields {
uint32_t mLocalStartTime;
Expand All @@ -204,7 +243,7 @@ struct YearDaySchedule {
*
* @return size of single credential entry.
*/
constexpr static size_t RequiredBufferSize() { return sizeof(Data); }
constexpr static size_t RequiredBufferSize() { return sizeof(Data) + sizeof(bool); }

/**
* @brief fill User entry with data from one the EmberAfPluginDoorLockYearDaySchedule plugin.
Expand All @@ -221,33 +260,9 @@ struct YearDaySchedule {
* @return CHIP_NO_ERROR if conversion has been finished successfully.
*/
CHIP_ERROR ConvertToPlugin(EmberAfPluginDoorLockYearDaySchedule &plugin) const;

/**
* @brief Serialize all fields into data buffer.
*
* `buffSize` must be set to at least the RequiredBufferSize size.
*
* @param buff buffer to store serialized data.
* @param buffSize size of input buffer.
* @return size_t output serialized data length, 0 value means error.
*/
size_t Serialize(void *buff, size_t buffSize);

/**
* @brief Deserialize all fields from given buffer.
*
* `buffSize` must be set to at least the RequiredBufferSize size.
*
* @param buff buffer containing serialized data (by invoking serialized method).
* @param buffSize size of output buffer.
* @return CHIP_ERROR_BUFFER_TOO_SMALL if provided buffSize is too small.
* @return CHIP_ERROR_INVALID_ARGUMENT if arguments are wrong.
* @return CHIP_NO_ERROR if deserialization has been finished successfully.
*/
CHIP_ERROR Deserialize(const void *buff, size_t buffSize);
};

struct HolidaySchedule {
struct HolidaySchedule : public Schedule<HolidaySchedule> {
union Data {
struct Fields {
uint32_t mLocalStartTime;
Expand All @@ -268,7 +283,7 @@ struct HolidaySchedule {
*
* @return size of single credential entry.
*/
constexpr static size_t RequiredBufferSize() { return sizeof(Data); }
constexpr static size_t RequiredBufferSize() { return sizeof(Data) + sizeof(bool); }

/**
* @brief fill User entry with data from one the EmberAfPluginDoorLockHolidaySchedule plugin.
Expand All @@ -285,30 +300,6 @@ struct HolidaySchedule {
* @return CHIP_NO_ERROR if conversion has been finished successfully.
*/
CHIP_ERROR ConvertToPlugin(EmberAfPluginDoorLockHolidaySchedule &plugin) const;

/**
* @brief Serialize all fields into data buffer.
*
* `buffSize` must be set to at least the RequiredBufferSize size.
*
* @param buff buffer to store serialized data.
* @param buffSize size of input buffer.
* @return size_t output serialized data length, 0 value means error.
*/
size_t Serialize(void *buff, size_t buffSize);

/**
* @brief Deserialize all fields from given buffer.
*
* `buffSize` must be set to at least the RequiredBufferSize size.
*
* @param buff buffer containing serialized data (by invoking serialized method).
* @param buffSize size of output buffer.
* @return CHIP_ERROR_BUFFER_TOO_SMALL if provided buffSize is too small.
* @return CHIP_ERROR_INVALID_ARGUMENT if arguments are wrong.
* @return CHIP_NO_ERROR if deserialization has been finished successfully.
*/
CHIP_ERROR Deserialize(const void *buff, size_t buffSize);
};

struct User {
Expand Down
Loading

0 comments on commit b60eb49

Please sign in to comment.