Line data Source code
1 : /* 2 : * Copyright (c) 2021 Project CHIP Authors 3 : * 4 : * Licensed under the Apache License, Version 2.0 (the "License"); 5 : * you may not use this file except in compliance with the License. 6 : * You may obtain a copy of the License at 7 : * 8 : * http://www.apache.org/licenses/LICENSE-2.0 9 : * 10 : * Unless required by applicable law or agreed to in writing, software 11 : * distributed under the License is distributed on an "AS IS" BASIS, 12 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 : * See the License for the specific language governing permissions and 14 : * limitations under the License. 15 : */ 16 : #pragma once 17 : 18 : #include <app/AttributePersistenceProvider.h> 19 : #include <app/SafeAttributePersistenceProvider.h> 20 : #include <lib/core/CHIPPersistentStorageDelegate.h> 21 : #include <lib/support/DefaultStorageKeyAllocator.h> 22 : 23 : namespace chip { 24 : namespace app { 25 : 26 : /** 27 : * Default implementation of AttributePersistenceProvider. This uses 28 : * PersistentStorageDelegate to store the attribute values. 29 : * 30 : * NOTE: SetAttributePersistenceProvider must still be called with an instance 31 : * of this class, since it can't be constructed automatically without knowing 32 : * what PersistentStorageDelegate is to be used. 33 : */ 34 : class DefaultAttributePersistenceProvider : public AttributePersistenceProvider, public SafeAttributePersistenceProvider 35 : { 36 : public: 37 1 : DefaultAttributePersistenceProvider() {} 38 : 39 : // Passed-in storage must outlive this object. 40 1 : CHIP_ERROR Init(PersistentStorageDelegate * storage) 41 : { 42 1 : if (storage == nullptr) 43 : { 44 0 : return CHIP_ERROR_INVALID_ARGUMENT; 45 : } 46 1 : mStorage = storage; 47 1 : return CHIP_NO_ERROR; 48 : } 49 : 50 1 : void Shutdown() {} 51 : 52 : // AttributePersistenceProvider implementation. 53 : CHIP_ERROR WriteValue(const ConcreteAttributePath & aPath, const ByteSpan & aValue) override; 54 : CHIP_ERROR ReadValue(const ConcreteAttributePath & aPath, const EmberAfAttributeMetadata * aMetadata, 55 : MutableByteSpan & aValue) override; 56 : 57 : // SafeAttributePersistenceProvider implementation. 58 : CHIP_ERROR SafeWriteValue(const ConcreteAttributePath & aPath, const ByteSpan & aValue) override; 59 : CHIP_ERROR SafeReadValue(const ConcreteAttributePath & aPath, MutableByteSpan & aValue) override; 60 : 61 : protected: 62 : PersistentStorageDelegate * mStorage; 63 : 64 : private: 65 : CHIP_ERROR InternalWriteValue(const StorageKeyName & aKey, const ByteSpan & aValue); 66 : CHIP_ERROR InternalReadValue(const StorageKeyName & aKey, EmberAfAttributeType aType, size_t aSize, MutableByteSpan & aValue); 67 : }; 68 : 69 : } // namespace app 70 : } // namespace chip