|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 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 <lib/support/ScopedBuffer.h> |
| 20 | +#include <lib/support/Span.h> |
| 21 | + |
| 22 | +namespace chip { |
| 23 | +namespace app { |
| 24 | + |
| 25 | +class DeferredAttribute |
| 26 | +{ |
| 27 | +public: |
| 28 | + explicit DeferredAttribute(const ConcreteAttributePath & path) : mPath(path) {} |
| 29 | + |
| 30 | + bool Matches(const ConcreteAttributePath & path) const { return mPath == path; } |
| 31 | + bool IsArmed() const { return static_cast<bool>(mValue); } |
| 32 | + System::Clock::Timestamp GetFlushTime() const { return mFlushTime; } |
| 33 | + |
| 34 | + CHIP_ERROR PrepareWrite(System::Clock::Timestamp flushTime, const ByteSpan & value); |
| 35 | + void Flush(AttributePersistenceProvider & persister); |
| 36 | + |
| 37 | +private: |
| 38 | + const ConcreteAttributePath mPath; |
| 39 | + System::Clock::Timestamp mFlushTime; |
| 40 | + Platform::ScopedMemoryBufferWithSize<uint8_t> mValue; |
| 41 | +}; |
| 42 | + |
| 43 | +/** |
| 44 | + * Decorator class for the AttributePersistenceProvider implementation that |
| 45 | + * defers writes of selected attributes. |
| 46 | + * |
| 47 | + * This class is useful to increase the flash lifetime by reducing the number |
| 48 | + * of writes of fast-changing attributes, such as CurrentLevel attribute of the |
| 49 | + * LevelControl cluster. |
| 50 | + */ |
| 51 | +class DeferredAttributePersistenceProvider : public AttributePersistenceProvider |
| 52 | +{ |
| 53 | +public: |
| 54 | + DeferredAttributePersistenceProvider(AttributePersistenceProvider & persister, |
| 55 | + const Span<DeferredAttribute> & deferredAttributes, |
| 56 | + System::Clock::Milliseconds32 writeDelay) : |
| 57 | + mPersister(persister), |
| 58 | + mDeferredAttributes(deferredAttributes), mWriteDelay(writeDelay) |
| 59 | + {} |
| 60 | + |
| 61 | + /* |
| 62 | + * If the written attribute is one of the deferred attributes specified in the constructor, |
| 63 | + * postpone the write operation by the configured delay. If this attribute changes within the |
| 64 | + * delay period, further postpone the operation so that the actual write happens once the |
| 65 | + * attribute has remained constant for the write delay period. |
| 66 | + * |
| 67 | + * For other attributes, immediately pass the write operation to the decorated persister. |
| 68 | + */ |
| 69 | + CHIP_ERROR WriteValue(const ConcreteAttributePath & path, const ByteSpan & value) override; |
| 70 | + CHIP_ERROR ReadValue(const ConcreteAttributePath & path, const EmberAfAttributeMetadata * metadata, |
| 71 | + MutableByteSpan & value) override; |
| 72 | + |
| 73 | +private: |
| 74 | + void FlushAndScheduleNext(); |
| 75 | + |
| 76 | + AttributePersistenceProvider & mPersister; |
| 77 | + const Span<DeferredAttribute> mDeferredAttributes; |
| 78 | + const System::Clock::Milliseconds32 mWriteDelay; |
| 79 | +}; |
| 80 | + |
| 81 | +} // namespace app |
| 82 | +} // namespace chip |
0 commit comments