@@ -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
314305struct User {
0 commit comments