blob: 64056a69dbe29058d7b1aa047c36f54bccd5c9c5 [file] [log] [blame]
lpbeliveau-silabsd2ae0822022-11-16 11:43:39 -05001/*
2 *
3 * Copyright (c) 2022 Project CHIP Authors
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/**
19 * @file
20 * Contains a class handling creation of linked list of stored persistent data.
21 */
lpbeliveau-silabs5c46db82023-01-24 19:39:22 -050022#pragma once
lpbeliveau-silabsd2ae0822022-11-16 11:43:39 -050023
24#include <lib/core/DataModelTypes.h>
25#include <lib/support/PersistentData.h>
26
27namespace chip {
28namespace CommonPersistentData {
29
Karsten Sperlingb15a6e62023-09-16 03:58:46 +120030inline constexpr uint8_t kdefaultUndefinedEntry = 0;
lpbeliveau-silabsd2ae0822022-11-16 11:43:39 -050031
32/// @brief Generic class to implement storage of a list persistently
33/// @tparam EntryType : Type of entry depends on the stored data
34/// @tparam kMaxSerializedSize : inherited from PersistentData class
35template <typename EntryType, size_t kMaxSerializedSize>
36struct StoredDataList : public PersistentData<kMaxSerializedSize>
37{
38 static constexpr TLV::Tag TagFirstEntry() { return TLV::ContextTag(1); }
39 static constexpr TLV::Tag TagEntryCount() { return TLV::ContextTag(2); }
40
41 EntryType first_entry = kdefaultUndefinedEntry;
42 uint16_t entry_count = 0;
43
44 StoredDataList() = default;
45 StoredDataList(EntryType first) : first_entry(first), entry_count(1) {}
46
47 CHIP_ERROR Serialize(TLV::TLVWriter & writer) const override
48 {
49 TLV::TLVType container;
50 ReturnErrorOnFailure(writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, container));
51
52 ReturnErrorOnFailure(writer.Put(TagFirstEntry(), static_cast<uint16_t>(first_entry)));
53 ReturnErrorOnFailure(writer.Put(TagEntryCount(), static_cast<uint16_t>(entry_count)));
54
55 return writer.EndContainer(container);
56 }
57
58 CHIP_ERROR Deserialize(TLV::TLVReader & reader) override
59 {
60 ReturnErrorOnFailure(reader.Next(TLV::AnonymousTag()));
61 VerifyOrReturnError(TLV::kTLVType_Structure == reader.GetType(), CHIP_ERROR_INTERNAL);
62
63 TLV::TLVType container;
64 ReturnErrorOnFailure(reader.EnterContainer(container));
65
66 // first_entry
67 ReturnErrorOnFailure(reader.Next(TagFirstEntry()));
68 ReturnErrorOnFailure(reader.Get(first_entry));
69 // entry_count
70 ReturnErrorOnFailure(reader.Next(TagEntryCount()));
71 ReturnErrorOnFailure(reader.Get(entry_count));
72
73 return reader.ExitContainer(container);
74 }
75};
76
Karsten Sperlingb15a6e62023-09-16 03:58:46 +120077inline constexpr size_t kPersistentFabricBufferMax = 32;
lpbeliveau-silabsd2ae0822022-11-16 11:43:39 -050078struct FabricList : StoredDataList<FabricIndex, kPersistentFabricBufferMax>
79{
Boris Zbarsky4088a772022-12-21 09:42:52 -050080 // Subclasses need to define UpdateKey to be whatever fabric list key they
81 // care about.
lpbeliveau-silabsd2ae0822022-11-16 11:43:39 -050082
83 void Clear() override
84 {
85 first_entry = kUndefinedFabricIndex;
86 entry_count = 0;
87 }
88};
89} // namespace CommonPersistentData
90} // namespace chip