LCOV - code coverage report
Current view: top level - lib/support - PersistentData.h (source / functions) Hit Total Coverage
Test: lcov_final.info Lines: 29 31 93.5 %
Date: 2024-02-15 08:20:41 Functions: 9 16 56.2 %

          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        1891 :     PersistentData(PersistentStorageDelegate * storage = nullptr) : mStorage(storage) {}
      32        1891 :     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         556 :     virtual CHIP_ERROR Save(PersistentStorageDelegate * storage)
      42             :     {
      43         556 :         VerifyOrReturnError(nullptr != storage, CHIP_ERROR_INVALID_ARGUMENT);
      44             : 
      45         556 :         StorageKeyName key = StorageKeyName::Uninitialized();
      46         556 :         ReturnErrorOnFailure(UpdateKey(key));
      47             : 
      48             :         // Serialize the data
      49         556 :         TLV::TLVWriter writer;
      50         556 :         writer.Init(mBuffer, sizeof(mBuffer));
      51             : 
      52         556 :         ReturnErrorOnFailure(Serialize(writer));
      53             : 
      54             :         // Save serialized data
      55         556 :         return storage->SyncSetKeyValue(key.KeyName(), mBuffer, static_cast<uint16_t>(writer.GetLengthWritten()));
      56         556 :     }
      57             : 
      58           0 :     virtual CHIP_ERROR Load() { return this->Load(this->mStorage); }
      59             : 
      60        2215 :     virtual CHIP_ERROR Load(PersistentStorageDelegate * storage)
      61             :     {
      62        2215 :         VerifyOrReturnError(nullptr != storage, CHIP_ERROR_INVALID_ARGUMENT);
      63             : 
      64        2215 :         StorageKeyName key = StorageKeyName::Uninitialized();
      65             : 
      66             :         // Update storage key
      67        2215 :         ReturnErrorOnFailure(UpdateKey(key));
      68             : 
      69             :         // Set data to defaults
      70        2213 :         Clear();
      71             : 
      72             :         // Load the serialized data
      73        2213 :         uint16_t size  = static_cast<uint16_t>(sizeof(mBuffer));
      74        2213 :         CHIP_ERROR err = storage->SyncGetKeyValue(key.KeyName(), mBuffer, size);
      75        2213 :         VerifyOrReturnError(CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND != err, CHIP_ERROR_NOT_FOUND);
      76        2143 :         ReturnErrorOnFailure(err);
      77             : 
      78             :         // Decode serialized data
      79             :         TLV::TLVReader reader;
      80        2143 :         reader.Init(mBuffer, size);
      81        2143 :         return Deserialize(reader);
      82        2215 :     }
      83             : 
      84         126 :     virtual CHIP_ERROR Delete(PersistentStorageDelegate * storage)
      85             :     {
      86         126 :         VerifyOrReturnError(nullptr != storage, CHIP_ERROR_INVALID_ARGUMENT);
      87             : 
      88         126 :         StorageKeyName key = StorageKeyName::Uninitialized();
      89         126 :         ReturnErrorOnFailure(UpdateKey(key));
      90             : 
      91         126 :         return storage->SyncDeleteKeyValue(key.KeyName());
      92         126 :     }
      93             : 
      94             :     PersistentStorageDelegate * mStorage = nullptr;
      95             :     uint8_t mBuffer[kMaxSerializedSize]  = { 0 };
      96             : };
      97             : 
      98             : } // namespace chip

Generated by: LCOV version 1.14