Line data Source code
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 : */ 22 : #pragma once 23 : 24 : #include <lib/core/DataModelTypes.h> 25 : #include <lib/support/PersistentData.h> 26 : 27 : namespace chip { 28 : namespace CommonPersistentData { 29 : 30 : inline constexpr uint8_t kdefaultUndefinedEntry = 0; 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 357 : static constexpr TLV::Tag TagFirstEntry() { return TLV::ContextTag(1); } 39 357 : static constexpr TLV::Tag TagEntryCount() { return TLV::ContextTag(2); } 40 : 41 : EntryType first_entry = kdefaultUndefinedEntry; 42 : uint16_t entry_count = 0; 43 : 44 320 : StoredDataList() = default; 45 : StoredDataList(EntryType first) : first_entry(first), entry_count(1) {} 46 : 47 56 : CHIP_ERROR Serialize(TLV::TLVWriter & writer) const override 48 : { 49 : TLV::TLVType container; 50 56 : ReturnErrorOnFailure(writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, container)); 51 : 52 56 : ReturnErrorOnFailure(writer.Put(TagFirstEntry(), static_cast<uint16_t>(first_entry))); 53 56 : ReturnErrorOnFailure(writer.Put(TagEntryCount(), static_cast<uint16_t>(entry_count))); 54 : 55 56 : return writer.EndContainer(container); 56 : } 57 : 58 301 : CHIP_ERROR Deserialize(TLV::TLVReader & reader) override 59 : { 60 301 : ReturnErrorOnFailure(reader.Next(TLV::AnonymousTag())); 61 301 : VerifyOrReturnError(TLV::kTLVType_Structure == reader.GetType(), CHIP_ERROR_INTERNAL); 62 : 63 : TLV::TLVType container; 64 301 : ReturnErrorOnFailure(reader.EnterContainer(container)); 65 : 66 : // first_entry 67 301 : ReturnErrorOnFailure(reader.Next(TagFirstEntry())); 68 301 : ReturnErrorOnFailure(reader.Get(first_entry)); 69 : // entry_count 70 301 : ReturnErrorOnFailure(reader.Next(TagEntryCount())); 71 301 : ReturnErrorOnFailure(reader.Get(entry_count)); 72 : 73 301 : return reader.ExitContainer(container); 74 : } 75 : }; 76 : 77 : inline constexpr size_t kPersistentFabricBufferMax = 32; 78 : struct FabricList : StoredDataList<FabricIndex, kPersistentFabricBufferMax> 79 : { 80 : // Subclasses need to define UpdateKey to be whatever fabric list key they 81 : // care about. 82 : 83 320 : void Clear() override 84 : { 85 320 : first_entry = kUndefinedFabricIndex; 86 320 : entry_count = 0; 87 320 : } 88 : }; 89 : } // namespace CommonPersistentData 90 : } // namespace chip