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 : #pragma once
18 :
19 : #include <lib/core/CHIPPersistentStorageDelegate.h>
20 : #include <lib/core/TLV.h>
21 : #include <lib/support/DefaultStorageKeyAllocator.h>
22 :
23 : namespace chip {
24 :
25 : /// @brief Interface to Persistent Storage Delegate allowing storage of data of variable size such as TLV.
26 : /// @tparam kMaxSerializedSize size of the mBuffer necessary to retrieve an entry from the storage. Varies with the type of data
27 : /// stored. Will be allocated on the stack so the implementation needs to be aware of this when choosing this value.
28 : template <size_t kMaxSerializedSize>
29 : struct PersistentData
30 : {
31 1948 : PersistentData(PersistentStorageDelegate * storage = nullptr) : mStorage(storage) {}
32 1948 : virtual ~PersistentData() = default;
33 :
34 : virtual CHIP_ERROR UpdateKey(StorageKeyName & key) = 0;
35 : virtual CHIP_ERROR Serialize(TLV::TLVWriter & writer) const = 0;
36 : virtual CHIP_ERROR Deserialize(TLV::TLVReader & reader) = 0;
37 : virtual void Clear() = 0;
38 :
39 0 : virtual CHIP_ERROR Save() { return this->Save(this->mStorage); }
40 :
41 568 : virtual CHIP_ERROR Save(PersistentStorageDelegate * storage)
42 : {
43 568 : VerifyOrReturnError(nullptr != storage, CHIP_ERROR_INVALID_ARGUMENT);
44 :
45 568 : StorageKeyName key = StorageKeyName::Uninitialized();
46 568 : ReturnErrorOnFailure(UpdateKey(key));
47 :
48 : // Serialize the data
49 568 : TLV::TLVWriter writer;
50 568 : writer.Init(mBuffer, sizeof(mBuffer));
51 :
52 568 : ReturnErrorOnFailure(Serialize(writer));
53 :
54 : // Save serialized data
55 568 : return storage->SyncSetKeyValue(key.KeyName(), mBuffer, static_cast<uint16_t>(writer.GetLengthWritten()));
56 568 : }
57 :
58 0 : virtual CHIP_ERROR Load() { return this->Load(this->mStorage); }
59 :
60 2242 : virtual CHIP_ERROR Load(PersistentStorageDelegate * storage)
61 : {
62 2242 : VerifyOrReturnError(nullptr != storage, CHIP_ERROR_INVALID_ARGUMENT);
63 :
64 2242 : StorageKeyName key = StorageKeyName::Uninitialized();
65 :
66 : // Update storage key
67 2242 : ReturnErrorOnFailure(UpdateKey(key));
68 :
69 : // Set data to defaults
70 2240 : Clear();
71 :
72 : // Load the serialized data
73 2240 : uint16_t size = static_cast<uint16_t>(sizeof(mBuffer));
74 2240 : CHIP_ERROR err = storage->SyncGetKeyValue(key.KeyName(), mBuffer, size);
75 2240 : VerifyOrReturnError(CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND != err, CHIP_ERROR_NOT_FOUND);
76 2161 : ReturnErrorOnFailure(err);
77 :
78 : // Decode serialized data
79 2161 : TLV::TLVReader reader;
80 2161 : reader.Init(mBuffer, size);
81 2161 : return Deserialize(reader);
82 2242 : }
83 :
84 131 : virtual CHIP_ERROR Delete(PersistentStorageDelegate * storage)
85 : {
86 131 : VerifyOrReturnError(nullptr != storage, CHIP_ERROR_INVALID_ARGUMENT);
87 :
88 131 : StorageKeyName key = StorageKeyName::Uninitialized();
89 131 : ReturnErrorOnFailure(UpdateKey(key));
90 :
91 131 : return storage->SyncDeleteKeyValue(key.KeyName());
92 131 : }
93 :
94 : PersistentStorageDelegate * mStorage = nullptr;
95 : uint8_t mBuffer[kMaxSerializedSize] = { 0 };
96 : };
97 :
98 : } // namespace chip
|