Line data Source code
1 : /**
2 : *
3 : * Copyright (c) 2025 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 : #pragma once
19 :
20 : #include <app/data-model-provider/MetadataTypes.h>
21 : #include <app/storage/FabricTableImpl.h>
22 : #include <app/util/endpoint-config-api.h>
23 : #include <lib/support/CodeUtils.h>
24 : #include <lib/support/DefaultStorageKeyAllocator.h>
25 : #include <lib/support/ReadOnlyBuffer.h>
26 : #include <lib/support/TypeTraits.h>
27 :
28 : #include <cstdlib>
29 :
30 : namespace chip {
31 : namespace app {
32 : namespace Storage {
33 :
34 : using EntryIndex = Data::EntryIndex;
35 :
36 : /// @brief Tags Used to serialize entries so they can be stored in flash memory.
37 : /// kEndpointEntryCount: Number of entries in an endpoint
38 : /// kEntryCount: Number of entries in a Fabric
39 : /// kStorageIdArray: Array of StorageId struct
40 : enum class TagEntry : uint8_t
41 : {
42 : kEndpointEntryCount = 1,
43 : kEntryCount,
44 : kStorageIdArray,
45 : kFabricTableFirstSpecializationReservedTag,
46 : kFabricTableLastSpecializationReservedTag = 127,
47 : // Add new entries here; kFabricTableFirstSpecializationReservedTag through
48 : // kFabricTableLastSpecializationReservedTag are reserved for specializations
49 : };
50 :
51 : // Currently takes 5 Bytes to serialize Container and value in a TLV: 1 byte start struct, 2 bytes control + tag for the value, 1
52 : // byte value, 1 byte end struct. 8 Bytes leaves space for potential increase in count_value size.
53 : static constexpr size_t kPersistentBufferEntryCountBytes = 8;
54 :
55 : struct BaseEntryCount : public PersistableData<kPersistentBufferEntryCountBytes>
56 : {
57 : uint8_t count_value = 0;
58 605 : BaseEntryCount(uint8_t count = 0) : count_value(count) {}
59 :
60 605 : void Clear() override { count_value = 0; }
61 :
62 288 : CHIP_ERROR Serialize(TLV::TLVWriter & writer) const override
63 : {
64 : TLV::TLVType container;
65 288 : ReturnErrorOnFailure(writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, container));
66 288 : ReturnErrorOnFailure(writer.Put(TLV::ContextTag(TagEntry::kEndpointEntryCount), count_value));
67 288 : return writer.EndContainer(container);
68 : }
69 :
70 540 : CHIP_ERROR Deserialize(TLV::TLVReader & reader) override
71 : {
72 540 : ReturnErrorOnFailure(reader.Next(TLV::kTLVType_Structure, TLV::AnonymousTag()));
73 :
74 : TLV::TLVType container;
75 540 : ReturnErrorOnFailure(reader.EnterContainer(container));
76 540 : ReturnErrorOnFailure(reader.Next(TLV::ContextTag(TagEntry::kEndpointEntryCount)));
77 540 : ReturnErrorOnFailure(reader.Get(count_value));
78 540 : return reader.ExitContainer(container);
79 : }
80 :
81 605 : CHIP_ERROR Load(PersistentStorageDelegate * storage) // NOLINT(bugprone-derived-method-shadowing-base-method)
82 : {
83 605 : CHIP_ERROR err = PersistableData::Load(storage);
84 1210 : return err.NoErrorIf(CHIP_ERROR_NOT_FOUND); // NOT_FOUND is OK; DataAccessor::Load already called Clear()
85 : }
86 : };
87 :
88 : template <class StorageId, class StorageData>
89 : struct EndpointEntryCount : public BaseEntryCount
90 : {
91 : using Serializer = DefaultSerializer<StorageId, StorageData>;
92 :
93 : EndpointId endpoint_id = kInvalidEndpointId;
94 :
95 605 : EndpointEntryCount(EndpointId endpoint, uint8_t count = 0) : BaseEntryCount(count), endpoint_id(endpoint) {}
96 605 : ~EndpointEntryCount() {}
97 :
98 893 : CHIP_ERROR UpdateKey(StorageKeyName & key) const override
99 : {
100 893 : VerifyOrReturnError(kInvalidEndpointId != endpoint_id, CHIP_ERROR_INVALID_ARGUMENT);
101 893 : key = Serializer::EndpointEntryCountKey(endpoint_id);
102 893 : return CHIP_NO_ERROR;
103 : }
104 : };
105 :
106 : // Prevent mutations from happening in TableEntryData::Serialize
107 : // If we just used a raw reference for TableEntryData::mEntry, C++ allows us
108 : // to mutate mEntry.mStorageId & mEntry.mStorageData in TableEntryData::Serialize
109 : // without having to do a const_cast; as an example, if we were to accidentally introduce
110 : // the following code in TableEntryData::Serialize (a const method):
111 : //
112 : // this->mEntry->mStorageData = StorageData();
113 : //
114 : // If TableEntryData::mEntry is a reference, it allows this with no compilation error;
115 : // But with ConstCorrectRef, we get a compile-time error that TableEntryData::mEntry->mStorageData
116 : // cannot be modified because it is a const value
117 : template <typename T>
118 : class ConstCorrectRef
119 : {
120 : T & mRef;
121 :
122 : public:
123 804 : inline ConstCorrectRef(T & ref) : mRef(ref) {}
124 :
125 : inline const T * operator->() const { return &mRef; }
126 : inline T * operator->() { return &mRef; }
127 :
128 410 : inline const T & operator*() const { return mRef; }
129 342 : inline T & operator*() { return mRef; }
130 : };
131 :
132 : template <class StorageId, class StorageData>
133 : struct TableEntryData : DataAccessor
134 : {
135 : using Serializer = DefaultSerializer<StorageId, StorageData>;
136 :
137 : EndpointId endpoint_id = kInvalidEndpointId;
138 : FabricIndex fabric_index = kUndefinedFabricIndex;
139 : EntryIndex index = 0;
140 : bool first = true;
141 : ConstCorrectRef<StorageId> storage_id;
142 : ConstCorrectRef<StorageData> storage_data;
143 :
144 402 : TableEntryData(EndpointId endpoint, FabricIndex fabric, StorageId & id, StorageData & data, EntryIndex idx = 0) :
145 402 : endpoint_id(endpoint), fabric_index(fabric), index(idx), storage_id(id), storage_data(data)
146 402 : {}
147 :
148 319 : CHIP_ERROR UpdateKey(StorageKeyName & key) const override
149 : {
150 319 : VerifyOrReturnError(kUndefinedFabricIndex != fabric_index, CHIP_ERROR_INVALID_FABRIC_INDEX);
151 319 : VerifyOrReturnError(kInvalidEndpointId != endpoint_id, CHIP_ERROR_INVALID_ARGUMENT);
152 319 : key = Serializer::FabricEntryKey(fabric_index, endpoint_id, index);
153 319 : return CHIP_NO_ERROR;
154 : }
155 :
156 114 : void Clear() override { Serializer::Clear(*storage_data); }
157 :
158 205 : CHIP_ERROR Serialize(TLV::TLVWriter & writer) const override
159 : {
160 : TLV::TLVType container;
161 205 : ReturnErrorOnFailure(writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, container));
162 :
163 205 : ReturnErrorOnFailure(Serializer::SerializeId(writer, *storage_id));
164 :
165 205 : ReturnErrorOnFailure(Serializer::SerializeData(writer, *storage_data));
166 :
167 205 : return writer.EndContainer(container);
168 : }
169 :
170 114 : CHIP_ERROR Deserialize(TLV::TLVReader & reader) override
171 : {
172 114 : ReturnErrorOnFailure(reader.Next(TLV::kTLVType_Structure, TLV::AnonymousTag()));
173 :
174 : TLV::TLVType container;
175 114 : ReturnErrorOnFailure(reader.EnterContainer(container));
176 :
177 114 : ReturnErrorOnFailure(Serializer::DeserializeId(reader, *storage_id));
178 :
179 114 : ReturnErrorOnFailure(Serializer::DeserializeData(reader, *storage_data));
180 :
181 114 : return reader.ExitContainer(container);
182 : }
183 : };
184 :
185 : /**
186 : * @brief Class that holds a map to all entries in a fabric for a specific endpoint
187 : *
188 : * FabricEntryData is an access to a linked list of entries
189 : */
190 : template <class StorageId, class StorageData, size_t kEntryMaxBytes, size_t kFabricMaxBytes, uint16_t kMaxPerFabric>
191 : struct FabricEntryData : public PersistableData<kFabricMaxBytes>
192 : {
193 : using Serializer = DefaultSerializer<StorageId, StorageData>;
194 : using TypedTableEntryData = TableEntryData<StorageId, StorageData>;
195 : using Buffer = PersistenceBuffer<kEntryMaxBytes>;
196 : using TypedEndpointEntryCount = EndpointEntryCount<StorageId, StorageData>;
197 :
198 : EndpointId endpoint_id;
199 : FabricIndex fabric_index;
200 : uint8_t entry_count = 0;
201 : uint16_t max_per_fabric;
202 : uint16_t max_per_endpoint;
203 : StorageId entry_map[kMaxPerFabric];
204 :
205 1636 : FabricEntryData(EndpointId endpoint = kInvalidEndpointId, FabricIndex fabric = kUndefinedFabricIndex,
206 : uint16_t maxPerFabric = kMaxPerFabric, uint16_t maxPerEndpoint = Serializer::kMaxPerEndpoint()) :
207 1636 : endpoint_id(endpoint),
208 17264 : fabric_index(fabric), max_per_fabric(maxPerFabric), max_per_endpoint(maxPerEndpoint)
209 1636 : {}
210 :
211 1941 : CHIP_ERROR UpdateKey(StorageKeyName & key) const override
212 : {
213 1941 : VerifyOrReturnError(kUndefinedFabricIndex != fabric_index, CHIP_ERROR_INVALID_FABRIC_INDEX);
214 1941 : VerifyOrReturnError(kInvalidEndpointId != endpoint_id, CHIP_ERROR_INVALID_ARGUMENT);
215 1941 : key = Serializer::FabricEntryDataKey(fabric_index, endpoint_id);
216 1941 : return CHIP_NO_ERROR;
217 : }
218 :
219 1636 : void Clear() override
220 : {
221 1636 : entry_count = 0;
222 17574 : for (uint16_t i = 0; i < max_per_fabric; i++)
223 : {
224 15938 : entry_map[i].Clear();
225 : }
226 1636 : }
227 :
228 288 : CHIP_ERROR Serialize(TLV::TLVWriter & writer) const override
229 : {
230 : TLV::TLVType fabricEntryContainer;
231 288 : ReturnErrorOnFailure(writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, fabricEntryContainer));
232 288 : ReturnErrorOnFailure(writer.Put(TLV::ContextTag(TagEntry::kEntryCount), entry_count));
233 :
234 : // Storing the entry map
235 : TLV::TLVType entryMapContainer;
236 288 : ReturnErrorOnFailure(
237 : writer.StartContainer(TLV::ContextTag(TagEntry::kStorageIdArray), TLV::kTLVType_Array, entryMapContainer));
238 2686 : for (uint16_t i = 0; i < max_per_fabric; i++)
239 : {
240 : TLV::TLVType entryIdContainer;
241 2398 : ReturnErrorOnFailure(writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, entryIdContainer));
242 2398 : ReturnErrorOnFailure(Serializer::SerializeId(writer, entry_map[i]));
243 2398 : ReturnErrorOnFailure(writer.EndContainer(entryIdContainer));
244 : }
245 288 : ReturnErrorOnFailure(writer.EndContainer(entryMapContainer));
246 :
247 288 : return writer.EndContainer(fabricEntryContainer);
248 : }
249 :
250 : /// @brief This Deserialize method is implemented only to allow compilation. It is not used throughout the code.
251 : /// @param reader TLV reader
252 : /// @return CHIP_NO_ERROR
253 0 : CHIP_ERROR Deserialize(TLV::TLVReader & reader) override { return CHIP_ERROR_INCORRECT_STATE; }
254 :
255 : /// @brief This Deserialize method checks that the recovered entries from the deserialization fit in the current max and if
256 : /// there are too many entries in nvm, it deletes them. The method sets the deleted_entries output parameter to true if entries
257 : /// were deleted so that the load function can know it needs to save the Fabric entry data to update the entry_count and the
258 : /// entry map in stored memory.
259 : /// @param reade [in] TLV reader, must be big enough to hold the entry size
260 : /// @param storage [in] Persistent Storage Delegate, required to delete entries if the number of entries in storage is greater
261 : /// than the maximum allowed
262 : /// @param deleted_entries_count [out] uint8_t letting the caller (in this case the load method) know how many entries were
263 : /// deleted so it can adjust the fabric and global entry count accordingly. Even if Deserialize fails, this value will return
264 : /// the number of entries deleted before the failure happened.
265 : /// @return CHIP_NO_ERROR on success, specific CHIP_ERROR otherwise
266 831 : CHIP_ERROR Deserialize(TLV::TLVReader & reader, PersistentStorageDelegate & storage, uint8_t & deleted_entries_count)
267 : {
268 831 : ReturnErrorOnFailure(reader.Next(TLV::kTLVType_Structure, TLV::AnonymousTag()));
269 : TLV::TLVType fabricEntryContainer;
270 831 : ReturnErrorOnFailure(reader.EnterContainer(fabricEntryContainer));
271 831 : ReturnErrorOnFailure(reader.Next(TLV::ContextTag(TagEntry::kEntryCount)));
272 831 : ReturnErrorOnFailure(reader.Get(entry_count));
273 831 : entry_count = std::min(entry_count, static_cast<uint8_t>(max_per_fabric));
274 831 : ReturnErrorOnFailure(reader.Next(TLV::kTLVType_Array, TLV::ContextTag(TagEntry::kStorageIdArray)));
275 : TLV::TLVType entryMapContainer;
276 831 : ReturnErrorOnFailure(reader.EnterContainer(entryMapContainer));
277 :
278 831 : uint16_t i = 0;
279 : CHIP_ERROR err;
280 831 : deleted_entries_count = 0;
281 :
282 16718 : while ((err = reader.Next(TLV::AnonymousTag())) == CHIP_NO_ERROR)
283 : {
284 : TLV::TLVType entryIdContainer;
285 7528 : if (i < max_per_fabric)
286 : {
287 7518 : ReturnErrorOnFailure(reader.EnterContainer(entryIdContainer));
288 7518 : ReturnErrorOnFailure(Serializer::DeserializeId(reader, entry_map[i]));
289 7518 : ReturnErrorOnFailure(reader.ExitContainer(entryIdContainer));
290 : }
291 : else
292 : {
293 10 : StorageId unused;
294 10 : ReturnErrorOnFailure(reader.EnterContainer(entryIdContainer));
295 10 : ReturnErrorOnFailure(Serializer::DeserializeId(reader, unused));
296 10 : ReturnErrorOnFailure(reader.ExitContainer(entryIdContainer));
297 10 : ReturnErrorOnFailure(DeleteValue(storage, i));
298 10 : deleted_entries_count++;
299 : }
300 :
301 7528 : i++;
302 : }
303 :
304 1662 : VerifyOrReturnError(err == CHIP_END_OF_TLV, err);
305 831 : ReturnErrorOnFailure(reader.ExitContainer(entryMapContainer));
306 831 : return reader.ExitContainer(fabricEntryContainer);
307 : }
308 :
309 : /// @brief Finds the id of the entry with the specified index
310 : /// @return CHIP_NO_ERROR if managed to find the target entry, CHIP_ERROR_NOT_FOUND if not found
311 119 : CHIP_ERROR FindByIndex(PersistentStorageDelegate & storage, EntryIndex index, StorageId & entry_id)
312 : {
313 119 : VerifyOrReturnError(entry_map[index].IsValid(), CHIP_ERROR_NOT_FOUND);
314 65 : VerifyOrReturnError(kUndefinedFabricIndex != fabric_index, CHIP_ERROR_INVALID_FABRIC_INDEX);
315 65 : VerifyOrReturnError(kInvalidEndpointId != endpoint_id, CHIP_ERROR_INVALID_ARGUMENT);
316 65 : if (!storage.SyncDoesKeyExist(Serializer::FabricEntryKey(fabric_index, endpoint_id, index).KeyName()))
317 : {
318 0 : return CHIP_ERROR_NOT_FOUND;
319 : }
320 65 : entry_id = entry_map[index];
321 65 : return CHIP_NO_ERROR;
322 : }
323 :
324 : /// @brief Finds the index where the current entry should be inserted by going through the endpoint's table and checking
325 : /// whether the entry is already there. If the target is not in the table, sets idx to the first empty space.
326 : /// If the target was not found and the table is full, sets idx to kUndefinedEntryIndex.
327 : /// @param[in] target_entry StorageId of entry to find
328 : /// @param[out] idx Index where target or space is found.
329 : /// @return CHIP_NO_ERROR if managed to find the target entry, CHIP_ERROR_NOT_FOUND if not found and space left
330 : /// CHIP_ERROR_NO_MEMORY if target was not found and table is full
331 427 : CHIP_ERROR Find(const StorageId & target_entry, EntryIndex & idx)
332 : {
333 427 : EntryIndex firstFreeIdx = Data::kUndefinedEntryIndex; // storage index if entry not found
334 427 : uint16_t index = 0;
335 :
336 2747 : while (index < max_per_fabric)
337 : {
338 2533 : if (entry_map[index] == target_entry)
339 : {
340 213 : idx = index;
341 213 : return CHIP_NO_ERROR; // return entry at current index if entry found
342 : }
343 2320 : if (!entry_map[index].IsValid() && firstFreeIdx == Data::kUndefinedEntryIndex)
344 : {
345 274 : firstFreeIdx = index;
346 : }
347 2320 : index++;
348 : }
349 :
350 214 : if (firstFreeIdx < max_per_fabric)
351 : {
352 208 : idx = firstFreeIdx;
353 208 : return CHIP_ERROR_NOT_FOUND;
354 : }
355 6 : idx = Data::kUndefinedEntryIndex;
356 6 : return CHIP_ERROR_NO_MEMORY;
357 : }
358 :
359 209 : CHIP_ERROR SaveEntry(PersistentStorageDelegate & storage, const StorageId & id, const StorageData & data, Buffer & buffer)
360 : {
361 209 : CHIP_ERROR err = CHIP_NO_ERROR;
362 : // Look for empty storage space
363 :
364 209 : EntryIndex index = Data::kUndefinedEntryIndex;
365 209 : err = this->Find(id, index);
366 :
367 : // C++ doesn't have const constructors; variable is declared const
368 209 : const TypedTableEntryData entry(endpoint_id, fabric_index, const_cast<StorageId &>(id), const_cast<StorageData &>(data),
369 : index);
370 :
371 418 : if (CHIP_NO_ERROR == err)
372 : {
373 10 : return entry.Save(&storage, buffer.BufferSpan());
374 : }
375 :
376 398 : if (CHIP_ERROR_NOT_FOUND == err) // If not found, entry.index should be the first free index
377 : {
378 : // Update the global entry count
379 197 : TypedEndpointEntryCount endpoint_count(endpoint_id);
380 197 : ReturnErrorOnFailure(endpoint_count.Load(&storage));
381 197 : VerifyOrReturnError(endpoint_count.count_value < max_per_endpoint, CHIP_ERROR_NO_MEMORY);
382 195 : endpoint_count.count_value++;
383 195 : ReturnErrorOnFailure(endpoint_count.Save(&storage));
384 :
385 195 : entry_count++;
386 195 : entry_map[entry.index] = id;
387 :
388 195 : err = this->Save(&storage);
389 390 : if (CHIP_NO_ERROR != err)
390 : {
391 0 : endpoint_count.count_value--;
392 0 : ReturnErrorOnFailure(endpoint_count.Save(&storage));
393 0 : return err;
394 : }
395 :
396 195 : err = entry.Save(&storage, buffer.BufferSpan());
397 :
398 : // on failure to save the entry, undoes the changes to Fabric Entry Data
399 390 : if (CHIP_NO_ERROR != err)
400 : {
401 0 : endpoint_count.count_value--;
402 0 : ReturnErrorOnFailure(endpoint_count.Save(&storage));
403 :
404 0 : entry_count--;
405 0 : entry_map[entry.index].Clear();
406 0 : ReturnErrorOnFailure(this->Save(&storage));
407 0 : return err;
408 : }
409 197 : }
410 :
411 197 : return err;
412 209 : }
413 :
414 : /// @brief Removes an entry from the non-volatile memory and clears its index in the entry map. Decreases the number of entries
415 : /// in the global entry count and in the entry fabric data if successful. As the entry map size is not compressed upon removal,
416 : /// this only clears the entry corresponding to the entry from the entry map.
417 : /// @param storage Storage delegate to access the entry
418 : /// @param entry_id Entry to remove
419 : /// @return CHIP_NO_ERROR if successful, specific CHIP_ERROR otherwise
420 92 : CHIP_ERROR RemoveEntry(PersistentStorageDelegate & storage, const StorageId & entry_id)
421 : {
422 92 : CHIP_ERROR err = CHIP_NO_ERROR;
423 : EntryIndex entryIndex;
424 :
425 : // Empty Entry Fabric Data returns CHIP_NO_ERROR on remove
426 92 : if (entry_count > 0)
427 : {
428 : // If Find doesn't return CHIP_NO_ERROR, the entry wasn't found, which doesn't return an error
429 184 : VerifyOrReturnValue(this->Find(entry_id, entryIndex) == CHIP_NO_ERROR, CHIP_NO_ERROR);
430 :
431 : // Update the global entry count
432 91 : TypedEndpointEntryCount endpoint_entry_count(endpoint_id);
433 91 : ReturnErrorOnFailure(endpoint_entry_count.Load(&storage));
434 91 : endpoint_entry_count.count_value--;
435 91 : ReturnErrorOnFailure(endpoint_entry_count.Save(&storage));
436 :
437 91 : entry_count--;
438 91 : entry_map[entryIndex].Clear();
439 91 : err = this->Save(&storage);
440 :
441 : // On failure to update the entry map, undo the global count modification
442 182 : if (CHIP_NO_ERROR != err)
443 : {
444 0 : endpoint_entry_count.count_value++;
445 0 : ReturnErrorOnFailure(endpoint_entry_count.Save(&storage));
446 0 : return err;
447 : }
448 :
449 91 : err = DeleteValue(storage, entryIndex);
450 :
451 : // On failure to delete entry, undo the change to the Fabric Entry Data and the global entry count
452 182 : if (CHIP_NO_ERROR != err)
453 : {
454 0 : endpoint_entry_count.count_value++;
455 0 : ReturnErrorOnFailure(endpoint_entry_count.Save(&storage));
456 :
457 0 : entry_count++;
458 0 : entry_map[entryIndex] = entry_id;
459 0 : ReturnErrorOnFailure(this->Save(&storage));
460 0 : return err;
461 : }
462 91 : }
463 91 : return err;
464 : }
465 :
466 1636 : CHIP_ERROR Load(PersistentStorageDelegate * storage) // NOLINT(bugprone-derived-method-shadowing-base-method)
467 : {
468 1636 : VerifyOrReturnError(nullptr != storage, CHIP_ERROR_INVALID_ARGUMENT);
469 1636 : uint8_t deleted_entries_count = 0;
470 :
471 1636 : uint8_t buffer[kFabricMaxBytes] = { 0 };
472 1636 : StorageKeyName key = StorageKeyName::Uninitialized();
473 :
474 : // Set data to defaults
475 1636 : Clear();
476 :
477 : // Update storage key
478 1636 : ReturnErrorOnFailure(UpdateKey(key));
479 :
480 : // Load the serialized data
481 1636 : uint16_t size = static_cast<uint16_t>(sizeof(buffer));
482 1636 : CHIP_ERROR err = storage->SyncGetKeyValue(key.KeyName(), buffer, size);
483 3272 : VerifyOrReturnError(CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND != err, CHIP_ERROR_NOT_FOUND);
484 831 : ReturnErrorOnFailure(err);
485 :
486 : // Decode serialized data
487 831 : TLV::TLVReader reader;
488 831 : reader.Init(buffer, size);
489 :
490 831 : err = Deserialize(reader, *storage, deleted_entries_count);
491 :
492 : // If Deserialize sets the "deleted_entries" variable, the table in flash memory held too many entries (can happen
493 : // if max_per_fabric was reduced during an OTA) and was adjusted during deserializing . The fabric data must then
494 : // be updated
495 831 : if (deleted_entries_count)
496 : {
497 2 : TypedEndpointEntryCount global_count(endpoint_id);
498 2 : ReturnErrorOnFailure(global_count.Load(storage));
499 2 : global_count.count_value = static_cast<uint8_t>(global_count.count_value - deleted_entries_count);
500 2 : ReturnErrorOnFailure(global_count.Save(storage));
501 2 : ReturnErrorOnFailure(this->Save(storage));
502 2 : }
503 :
504 831 : return err;
505 1636 : }
506 :
507 : private:
508 101 : CHIP_ERROR DeleteValue(PersistentStorageDelegate & storage, EntryIndex index)
509 : {
510 101 : StorageKeyName key = Serializer::FabricEntryKey(fabric_index, endpoint_id, index);
511 101 : return storage.SyncDeleteKeyValue(key.KeyName());
512 101 : }
513 : };
514 :
515 : template <class StorageId, class StorageData>
516 89 : CHIP_ERROR FabricTableImpl<StorageId, StorageData>::Init(PersistentStorageDelegate & storage)
517 : {
518 : // Verify the initialized parameter respects the maximum allowed values for entry capacity
519 89 : VerifyOrReturnError(mMaxPerFabric <= Serializer::kMaxPerFabric() && mMaxPerEndpoint <= Serializer::kMaxPerEndpoint(),
520 : CHIP_ERROR_INVALID_INTEGER_VALUE);
521 87 : this->mStorage = &storage;
522 87 : return CHIP_NO_ERROR;
523 : }
524 :
525 : template <class StorageId, class StorageData>
526 104 : void FabricTableImpl<StorageId, StorageData>::Finish()
527 104 : {}
528 :
529 : template <class StorageId, class StorageData>
530 146 : CHIP_ERROR FabricTableImpl<StorageId, StorageData>::GetFabricEntryCount(FabricIndex fabric_index, uint8_t & entry_count)
531 : {
532 : using TypedFabricEntryData = FabricEntryData<StorageId, StorageData, Serializer::kEntryMaxBytes(),
533 : Serializer::kFabricMaxBytes(), Serializer::kMaxPerFabric()>;
534 :
535 146 : VerifyOrReturnError(IsInitialized(), CHIP_ERROR_INTERNAL);
536 :
537 146 : TypedFabricEntryData fabric(mEndpointId, fabric_index);
538 146 : CHIP_ERROR err = fabric.Load(mStorage);
539 301 : VerifyOrReturnError(CHIP_NO_ERROR == err || CHIP_ERROR_NOT_FOUND == err, err);
540 :
541 292 : entry_count = (CHIP_ERROR_NOT_FOUND == err) ? 0 : fabric.entry_count;
542 :
543 146 : return CHIP_NO_ERROR;
544 146 : }
545 :
546 : template <class StorageId, class StorageData>
547 315 : CHIP_ERROR FabricTableImpl<StorageId, StorageData>::GetEndpointEntryCount(uint8_t & entry_count)
548 : {
549 : using TypedEndpointEntryCount = EndpointEntryCount<StorageId, StorageData>;
550 :
551 315 : VerifyOrReturnError(IsInitialized(), CHIP_ERROR_INTERNAL);
552 :
553 315 : TypedEndpointEntryCount endpoint_entry_count(mEndpointId);
554 :
555 315 : ReturnErrorOnFailure(endpoint_entry_count.Load(mStorage));
556 315 : entry_count = endpoint_entry_count.count_value;
557 :
558 315 : return CHIP_NO_ERROR;
559 315 : }
560 :
561 : template <class StorageId, class StorageData>
562 0 : CHIP_ERROR FabricTableImpl<StorageId, StorageData>::SetEndpointEntryCount(const uint8_t & entry_count)
563 : {
564 : using TypedEndpointEntryCount = EndpointEntryCount<StorageId, StorageData>;
565 0 : VerifyOrReturnError(IsInitialized(), CHIP_ERROR_INTERNAL);
566 :
567 0 : TypedEndpointEntryCount endpoint_entry_count(mEndpointId, entry_count);
568 0 : return endpoint_entry_count.Save(mStorage);
569 0 : }
570 :
571 : template <class StorageId, class StorageData>
572 290 : CHIP_ERROR FabricTableImpl<StorageId, StorageData>::GetRemainingCapacity(FabricIndex fabric_index, uint8_t & capacity)
573 : {
574 : using TypedFabricEntryData = FabricEntryData<StorageId, StorageData, Serializer::kEntryMaxBytes(),
575 : Serializer::kFabricMaxBytes(), Serializer::kMaxPerFabric()>;
576 :
577 290 : VerifyOrReturnError(IsInitialized(), CHIP_ERROR_INTERNAL);
578 :
579 290 : uint8_t endpoint_entry_count = 0;
580 290 : ReturnErrorOnFailure(GetEndpointEntryCount(endpoint_entry_count));
581 :
582 : // If the global entry count is higher than the maximal Global entry capacity, this returns a capacity of 0 until enough entries
583 : // have been deleted to bring the global number of entries under the global maximum.
584 290 : if (endpoint_entry_count > mMaxPerEndpoint)
585 : {
586 6 : capacity = 0;
587 6 : return CHIP_NO_ERROR;
588 : }
589 284 : uint8_t remaining_capacity_global = static_cast<uint8_t>(mMaxPerEndpoint - endpoint_entry_count);
590 284 : uint8_t remaining_capacity_fabric = static_cast<uint8_t>(mMaxPerFabric);
591 :
592 284 : TypedFabricEntryData fabric(mEndpointId, fabric_index);
593 :
594 : // Load fabric data (defaults to zero)TypedFabricEntryData
595 284 : CHIP_ERROR err = fabric.Load(mStorage);
596 624 : VerifyOrReturnError(CHIP_NO_ERROR == err || CHIP_ERROR_NOT_FOUND == err, err);
597 :
598 568 : if (err == CHIP_NO_ERROR)
599 : {
600 228 : remaining_capacity_fabric = static_cast<uint8_t>(mMaxPerFabric - fabric.entry_count);
601 : }
602 :
603 284 : capacity = std::min(remaining_capacity_fabric, remaining_capacity_global);
604 :
605 284 : return CHIP_NO_ERROR;
606 284 : }
607 :
608 : template <class StorageId, class StorageData>
609 : template <size_t kEntryMaxBytes>
610 209 : CHIP_ERROR FabricTableImpl<StorageId, StorageData>::SetTableEntry(FabricIndex fabric_index, const StorageId & id,
611 : const StorageData & data,
612 : PersistenceBuffer<kEntryMaxBytes> & writeBuffer)
613 : {
614 : using TypedFabricEntryData = FabricEntryData<StorageId, StorageData, Serializer::kEntryMaxBytes(),
615 : Serializer::kFabricMaxBytes(), Serializer::kMaxPerFabric()>;
616 :
617 209 : VerifyOrReturnError(IsInitialized(), CHIP_ERROR_INTERNAL);
618 :
619 209 : TypedFabricEntryData fabric(mEndpointId, fabric_index, mMaxPerFabric, mMaxPerEndpoint);
620 :
621 : // Load fabric data (defaults to zero)
622 209 : CHIP_ERROR err = fabric.Load(mStorage);
623 466 : VerifyOrReturnError(CHIP_NO_ERROR == err || CHIP_ERROR_NOT_FOUND == err, err);
624 :
625 209 : err = fabric.SaveEntry(*mStorage, id, data, writeBuffer);
626 209 : return err;
627 209 : }
628 :
629 : template <class StorageId, class StorageData>
630 : template <size_t kEntryMaxBytes>
631 189 : CHIP_ERROR FabricTableImpl<StorageId, StorageData>::GetTableEntry(FabricIndex fabric_index, StorageId & entry_id,
632 : StorageData & data, PersistenceBuffer<kEntryMaxBytes> & buffer)
633 : {
634 : using TypedFabricEntryData = FabricEntryData<StorageId, StorageData, Serializer::kEntryMaxBytes(),
635 : Serializer::kFabricMaxBytes(), Serializer::kMaxPerFabric()>;
636 189 : VerifyOrReturnError(IsInitialized(), CHIP_ERROR_INTERNAL);
637 :
638 189 : TypedFabricEntryData fabric(mEndpointId, fabric_index, mMaxPerFabric, mMaxPerEndpoint);
639 189 : TableEntryData<StorageId, StorageData> table_entry(mEndpointId, fabric_index, entry_id, data);
640 :
641 189 : ReturnErrorOnFailure(fabric.Load(mStorage));
642 244 : VerifyOrReturnError(fabric.Find(entry_id, table_entry.index) == CHIP_NO_ERROR, CHIP_ERROR_NOT_FOUND);
643 :
644 110 : CHIP_ERROR err = table_entry.Load(mStorage, buffer.BufferSpan());
645 :
646 : // If entry.Load returns "buffer too small", the entry in memory is too big to be retrieved (this could happen if the
647 : // kEntryMaxBytes was reduced by OTA) and therefore must be deleted as is is no longer considered accessible.
648 220 : if (err == CHIP_ERROR_BUFFER_TOO_SMALL)
649 : {
650 0 : ReturnErrorOnFailure(this->RemoveTableEntry(fabric_index, entry_id));
651 : }
652 110 : ReturnErrorOnFailure(err);
653 :
654 110 : return CHIP_NO_ERROR;
655 189 : }
656 :
657 : template <class StorageId, class StorageData>
658 4 : CHIP_ERROR FabricTableImpl<StorageId, StorageData>::FindTableEntry(FabricIndex fabric_index, const StorageId & entry_id,
659 : EntryIndex & idx)
660 : {
661 : using TypedFabricEntryData = FabricEntryData<StorageId, StorageData, Serializer::kEntryMaxBytes(),
662 : Serializer::kFabricMaxBytes(), Serializer::kMaxPerFabric()>;
663 4 : VerifyOrReturnError(IsInitialized(), CHIP_ERROR_INTERNAL);
664 :
665 4 : TypedFabricEntryData fabric(mEndpointId, fabric_index, mMaxPerFabric, mMaxPerEndpoint);
666 :
667 4 : ReturnErrorOnFailure(fabric.Load(mStorage));
668 8 : VerifyOrReturnError(fabric.Find(entry_id, idx) == CHIP_NO_ERROR, CHIP_ERROR_NOT_FOUND);
669 :
670 2 : return CHIP_NO_ERROR;
671 4 : }
672 :
673 : template <class StorageId, class StorageData>
674 18 : CHIP_ERROR FabricTableImpl<StorageId, StorageData>::RemoveTableEntry(FabricIndex fabric_index, const StorageId & entry_id)
675 : {
676 : using TypedFabricEntryData = FabricEntryData<StorageId, StorageData, Serializer::kEntryMaxBytes(),
677 : Serializer::kFabricMaxBytes(), Serializer::kMaxPerFabric()>;
678 :
679 18 : VerifyOrReturnError(IsInitialized(), CHIP_ERROR_INTERNAL);
680 18 : TypedFabricEntryData fabric(mEndpointId, fabric_index, mMaxPerFabric, mMaxPerEndpoint);
681 :
682 18 : ReturnErrorOnFailure(fabric.Load(mStorage));
683 :
684 18 : return fabric.RemoveEntry(*mStorage, entry_id);
685 18 : }
686 :
687 : /// @brief This function is meant to provide a way to empty the entry table without knowing any specific entry Id. Outside of this
688 : /// specific use case, RemoveTableEntry should be used.
689 : /// @param fabric_index Fabric in which the entry belongs
690 : /// @param entry_idx Position in the Table
691 : /// @return CHIP_NO_ERROR if removal was successful, errors if failed to remove the entry or to update the fabric after removing it
692 : template <class StorageId, class StorageData>
693 119 : CHIP_ERROR FabricTableImpl<StorageId, StorageData>::RemoveTableEntryAtPosition(EndpointId endpoint, FabricIndex fabric_index,
694 : EntryIndex entry_idx)
695 : {
696 : using TypedFabricEntryData = FabricEntryData<StorageId, StorageData, Serializer::kEntryMaxBytes(),
697 : Serializer::kFabricMaxBytes(), Serializer::kMaxPerFabric()>;
698 :
699 119 : VerifyOrReturnError(IsInitialized(), CHIP_ERROR_INTERNAL);
700 :
701 119 : TypedFabricEntryData fabric(endpoint, fabric_index, mMaxPerFabric, mMaxPerEndpoint);
702 :
703 119 : ReturnErrorOnFailure(fabric.Load(mStorage));
704 119 : StorageId entryId;
705 119 : CHIP_ERROR err = fabric.FindByIndex(*mStorage, entry_idx, entryId);
706 238 : VerifyOrReturnValue(CHIP_ERROR_NOT_FOUND != err, CHIP_NO_ERROR);
707 65 : ReturnErrorOnFailure(err);
708 :
709 65 : return fabric.RemoveEntry(*mStorage, entryId);
710 119 : }
711 :
712 : template <class StorageId, class StorageData>
713 40 : CHIP_ERROR FabricTableImpl<StorageId, StorageData>::RemoveFabric(DataModel::ProviderMetadataTree & provider,
714 : FabricIndex fabric_index)
715 : {
716 : using TypedFabricEntryData = FabricEntryData<StorageId, StorageData, Serializer::kEntryMaxBytes(),
717 : Serializer::kFabricMaxBytes(), Serializer::kMaxPerFabric()>;
718 :
719 40 : VerifyOrReturnError(IsInitialized(), CHIP_ERROR_INTERNAL);
720 :
721 40 : ReadOnlyBufferBuilder<DataModel::EndpointEntry> endpointsBuilder;
722 40 : ReturnErrorOnFailure(provider.Endpoints(endpointsBuilder));
723 :
724 310 : for (const auto & ep : endpointsBuilder.TakeBuffer())
725 : {
726 135 : EndpointId endpoint = ep.id;
727 135 : TypedFabricEntryData fabric(endpoint, fabric_index);
728 135 : EntryIndex idx = 0;
729 135 : CHIP_ERROR err = fabric.Load(mStorage);
730 391 : VerifyOrReturnError(CHIP_NO_ERROR == err || CHIP_ERROR_NOT_FOUND == err, err);
731 270 : if (CHIP_ERROR_NOT_FOUND == err)
732 : {
733 121 : continue;
734 : }
735 :
736 106 : while (idx < mMaxPerFabric)
737 : {
738 92 : err = RemoveTableEntryAtPosition(endpoint, fabric_index, idx);
739 184 : VerifyOrReturnError(CHIP_NO_ERROR == err || CHIP_ERROR_NOT_FOUND == err, err);
740 92 : idx++;
741 : }
742 :
743 : // Remove fabric entries on endpoint
744 14 : ReturnErrorOnFailure(fabric.Delete(mStorage));
745 : }
746 :
747 40 : return CHIP_NO_ERROR;
748 40 : }
749 :
750 : template <class StorageId, class StorageData>
751 2 : CHIP_ERROR FabricTableImpl<StorageId, StorageData>::RemoveEndpoint()
752 : {
753 : using TypedFabricEntryData = FabricEntryData<StorageId, StorageData, Serializer::kEntryMaxBytes(),
754 : Serializer::kFabricMaxBytes(), Serializer::kMaxPerFabric()>;
755 :
756 2 : VerifyOrReturnError(IsInitialized(), CHIP_ERROR_INTERNAL);
757 :
758 1014 : for (FabricIndex fabric_index = kMinValidFabricIndex; fabric_index < kMaxValidFabricIndex; fabric_index++)
759 : {
760 506 : TypedFabricEntryData fabric(mEndpointId, fabric_index);
761 506 : CHIP_ERROR err = fabric.Load(mStorage);
762 1515 : VerifyOrReturnError(CHIP_NO_ERROR == err || CHIP_ERROR_NOT_FOUND == err, err);
763 1012 : if (CHIP_ERROR_NOT_FOUND == err)
764 : {
765 503 : continue;
766 : }
767 :
768 3 : EntryIndex idx = 0;
769 28 : while (idx < mMaxPerFabric)
770 : {
771 25 : err = RemoveTableEntryAtPosition(mEndpointId, fabric_index, idx);
772 50 : VerifyOrReturnError(CHIP_NO_ERROR == err || CHIP_ERROR_NOT_FOUND == err, err);
773 25 : idx++;
774 : };
775 :
776 : // Remove fabric entries on endpoint
777 3 : ReturnErrorOnFailure(fabric.Delete(mStorage));
778 : }
779 :
780 2 : return CHIP_NO_ERROR;
781 : }
782 :
783 : template <class StorageId, class StorageData>
784 94 : void FabricTableImpl<StorageId, StorageData>::SetEndpoint(EndpointId endpoint)
785 : {
786 94 : mEndpointId = endpoint;
787 94 : }
788 :
789 : template <class StorageId, class StorageData>
790 51 : void FabricTableImpl<StorageId, StorageData>::SetTableSize(uint16_t endpointTableSize, uint16_t maxPerFabric)
791 : {
792 : // Verify the endpoint passed size respects the limits of the device configuration
793 51 : VerifyOrDie(Serializer::kMaxPerFabric() > 0);
794 51 : VerifyOrDie(Serializer::kMaxPerEndpoint() > 0);
795 51 : mMaxPerEndpoint = std::min(Serializer::kMaxPerEndpoint(), endpointTableSize);
796 51 : mMaxPerFabric = std::min(endpointTableSize, std::min(Serializer::kMaxPerFabric(), maxPerFabric));
797 51 : }
798 :
799 : template <class StorageId, class StorageData>
800 : template <size_t kEntryMaxBytes, class UnaryFunc>
801 2 : CHIP_ERROR FabricTableImpl<StorageId, StorageData>::IterateEntries(FabricIndex fabric, PersistenceBuffer<kEntryMaxBytes> & buffer,
802 : UnaryFunc iterateFn)
803 : {
804 2 : VerifyOrReturnError(IsInitialized(), CHIP_ERROR_INTERNAL);
805 :
806 2 : EntryIteratorImpl<kEntryMaxBytes> iterator(*this, fabric, mEndpointId, mMaxPerFabric, mMaxPerEndpoint, buffer);
807 2 : return iterateFn(iterator);
808 2 : }
809 :
810 : template <class StorageId, class StorageData>
811 : template <size_t kEntryMaxBytes>
812 2 : FabricTableImpl<StorageId, StorageData>::EntryIteratorImpl<kEntryMaxBytes>::EntryIteratorImpl(
813 : FabricTableImpl & provider, FabricIndex fabricIdx, EndpointId endpoint, uint16_t maxPerFabric, uint16_t maxPerEndpoint,
814 : PersistenceBuffer<kEntryMaxBytes> & buffer) :
815 2 : mProvider(provider),
816 2 : mBuffer(buffer), mFabric(fabricIdx), mEndpoint(endpoint), mMaxPerFabric(maxPerFabric), mMaxPerEndpoint(maxPerEndpoint)
817 : {
818 : using TypedFabricEntryData = FabricEntryData<StorageId, StorageData, Serializer::kEntryMaxBytes(),
819 : Serializer::kFabricMaxBytes(), Serializer::kMaxPerFabric()>;
820 :
821 2 : TypedFabricEntryData fabric(mEndpoint, fabricIdx, mMaxPerFabric, mMaxPerEndpoint);
822 2 : ReturnOnFailure(fabric.Load(provider.mStorage));
823 2 : mTotalEntries = fabric.entry_count;
824 2 : mEntryIndex = 0;
825 2 : }
826 :
827 : template <class StorageId, class StorageData>
828 : template <size_t kEntryMaxBytes>
829 0 : size_t FabricTableImpl<StorageId, StorageData>::EntryIteratorImpl<kEntryMaxBytes>::Count()
830 : {
831 0 : return mTotalEntries;
832 : }
833 :
834 : template <class StorageId, class StorageData>
835 : template <size_t kEntryMaxBytes>
836 6 : bool FabricTableImpl<StorageId, StorageData>::EntryIteratorImpl<kEntryMaxBytes>::Next(TableEntry & output)
837 : {
838 : using TypedFabricEntryData = FabricEntryData<StorageId, StorageData, Serializer::kEntryMaxBytes(),
839 : Serializer::kFabricMaxBytes(), Serializer::kMaxPerFabric()>;
840 :
841 6 : TypedFabricEntryData fabric(mEndpoint, mFabric);
842 :
843 12 : VerifyOrReturnError(fabric.Load(mProvider.mStorage) == CHIP_NO_ERROR, false);
844 :
845 : // looks for next available entry
846 12 : while (mEntryIndex < mMaxPerFabric)
847 : {
848 10 : if (fabric.entry_map[mEntryIndex].IsValid())
849 : {
850 4 : TableEntryData<StorageId, StorageData> entry(mEndpoint, mFabric, output.mStorageId, output.mStorageData, mEntryIndex);
851 8 : VerifyOrReturnError(entry.Load(mProvider.mStorage, mBuffer.BufferSpan()) == CHIP_NO_ERROR, false);
852 4 : mEntryIndex++;
853 :
854 4 : return true;
855 4 : }
856 :
857 6 : mEntryIndex++;
858 : }
859 :
860 2 : return false;
861 6 : }
862 :
863 : template <class StorageId, class StorageData>
864 : template <size_t kEntryMaxBytes>
865 0 : void FabricTableImpl<StorageId, StorageData>::EntryIteratorImpl<kEntryMaxBytes>::Release()
866 0 : {}
867 : } // namespace Storage
868 : } // namespace app
869 : } // namespace chip
|