lpbeliveau-silabs | d2ae082 | 2022-11-16 11:43:39 -0500 | [diff] [blame] | 1 | /* |
| 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-silabs | 5c46db8 | 2023-01-24 19:39:22 -0500 | [diff] [blame] | 22 | #pragma once |
lpbeliveau-silabs | d2ae082 | 2022-11-16 11:43:39 -0500 | [diff] [blame] | 23 | |
| 24 | #include <lib/core/DataModelTypes.h> |
| 25 | #include <lib/support/PersistentData.h> |
| 26 | |
| 27 | namespace chip { |
| 28 | namespace CommonPersistentData { |
| 29 | |
Karsten Sperling | b15a6e6 | 2023-09-16 03:58:46 +1200 | [diff] [blame] | 30 | inline constexpr uint8_t kdefaultUndefinedEntry = 0; |
lpbeliveau-silabs | d2ae082 | 2022-11-16 11:43:39 -0500 | [diff] [blame] | 31 | |
| 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 |
| 35 | template <typename EntryType, size_t kMaxSerializedSize> |
| 36 | struct 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 Sperling | b15a6e6 | 2023-09-16 03:58:46 +1200 | [diff] [blame] | 77 | inline constexpr size_t kPersistentFabricBufferMax = 32; |
lpbeliveau-silabs | d2ae082 | 2022-11-16 11:43:39 -0500 | [diff] [blame] | 78 | struct FabricList : StoredDataList<FabricIndex, kPersistentFabricBufferMax> |
| 79 | { |
Boris Zbarsky | 4088a77 | 2022-12-21 09:42:52 -0500 | [diff] [blame] | 80 | // Subclasses need to define UpdateKey to be whatever fabric list key they |
| 81 | // care about. |
lpbeliveau-silabs | d2ae082 | 2022-11-16 11:43:39 -0500 | [diff] [blame] | 82 | |
| 83 | void Clear() override |
| 84 | { |
| 85 | first_entry = kUndefinedFabricIndex; |
| 86 | entry_count = 0; |
| 87 | } |
| 88 | }; |
| 89 | } // namespace CommonPersistentData |
| 90 | } // namespace chip |