LCOV - code coverage report
Current view: top level - include/platform - KvsPersistentStorageDelegate.h (source / functions) Hit Total Coverage
Test: lcov_final.info Lines: 14 24 58.3 %
Date: 2024-02-15 08:20:41 Functions: 3 5 60.0 %

          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             : #pragma once
      19             : 
      20             : #include <cstdint>
      21             : 
      22             : #include <lib/core/CHIPError.h>
      23             : #include <lib/core/CHIPPersistentStorageDelegate.h>
      24             : #include <lib/support/CodeUtils.h>
      25             : #include <lib/support/SafeInt.h>
      26             : #include <platform/KeyValueStoreManager.h>
      27             : 
      28             : namespace chip {
      29             : 
      30             : // Sample version of PersistentStorageDelegate that relies only on a KeyValueStoreManager
      31             : class KvsPersistentStorageDelegate : public PersistentStorageDelegate
      32             : {
      33             : public:
      34             :     KvsPersistentStorageDelegate()          = default;
      35           1 :     virtual ~KvsPersistentStorageDelegate() = default;
      36             : 
      37             :     // No copy, move or assignment.
      38             :     KvsPersistentStorageDelegate(const KvsPersistentStorageDelegate &)             = delete;
      39             :     KvsPersistentStorageDelegate(const KvsPersistentStorageDelegate &&)            = delete;
      40             :     KvsPersistentStorageDelegate & operator=(const KvsPersistentStorageDelegate &) = delete;
      41             : 
      42             :     CHIP_ERROR Init(DeviceLayer::PersistedStorage::KeyValueStoreManager * kvsManager)
      43             :     {
      44             :         if (kvsManager == nullptr)
      45             :         {
      46             :             return CHIP_ERROR_INVALID_ARGUMENT;
      47             :         }
      48             :         mKvsManager = kvsManager;
      49             : 
      50             :         return CHIP_NO_ERROR;
      51             :     }
      52             : 
      53          55 :     CHIP_ERROR SyncGetKeyValue(const char * key, void * buffer, uint16_t & size) override
      54             :     {
      55          55 :         VerifyOrReturnError(mKvsManager != nullptr, CHIP_ERROR_INCORRECT_STATE);
      56             : 
      57          55 :         uint8_t emptyPlaceholder = 0;
      58          55 :         if (buffer == nullptr)
      59             :         {
      60           0 :             if (size != 0)
      61             :             {
      62           0 :                 return CHIP_ERROR_INVALID_ARGUMENT;
      63             :             }
      64             : 
      65             :             // When size is zero, let's give a non-nullptr to the KVS backend
      66           0 :             buffer = &emptyPlaceholder;
      67             :         }
      68             : 
      69          55 :         size_t bytesRead = 0;
      70          55 :         CHIP_ERROR err   = mKvsManager->Get(key, buffer, size, &bytesRead);
      71             : 
      72             :         // Update size only if it made sense
      73          55 :         if ((CHIP_ERROR_BUFFER_TOO_SMALL == err) || (CHIP_NO_ERROR == err))
      74             :         {
      75           0 :             size = CanCastTo<uint16_t>(bytesRead) ? static_cast<uint16_t>(bytesRead) : 0;
      76             :         }
      77             : 
      78          55 :         return err;
      79             :     }
      80             : 
      81           5 :     CHIP_ERROR SyncSetKeyValue(const char * key, const void * value, uint16_t size) override
      82             :     {
      83           5 :         VerifyOrReturnError(mKvsManager != nullptr, CHIP_ERROR_INCORRECT_STATE);
      84             : 
      85           5 :         uint8_t placeholderForEmpty = 0;
      86           5 :         if (value == nullptr)
      87             :         {
      88           0 :             if (size == 0)
      89             :             {
      90           0 :                 value = &placeholderForEmpty;
      91             :             }
      92             :             else
      93             :             {
      94           0 :                 return CHIP_ERROR_INVALID_ARGUMENT;
      95             :             }
      96             :         }
      97           5 :         return mKvsManager->Put(key, value, size);
      98             :     }
      99             : 
     100           0 :     CHIP_ERROR SyncDeleteKeyValue(const char * key) override
     101             :     {
     102           0 :         VerifyOrReturnError(mKvsManager != nullptr, CHIP_ERROR_INCORRECT_STATE);
     103           0 :         return mKvsManager->Delete(key);
     104             :     }
     105             : 
     106             : protected:
     107             :     DeviceLayer::PersistedStorage::KeyValueStoreManager * mKvsManager = nullptr;
     108             : };
     109             : 
     110             : } // namespace chip

Generated by: LCOV version 1.14