40
40
#include < app/server/Server.h>
41
41
#include < app/util/attribute-storage.h>
42
42
#include < app/util/endpoint-config-api.h>
43
+ #include < app/util/persistence/DefaultAttributePersistenceProvider.h>
44
+ #include < app/util/persistence/DeferredAttributePersistenceProvider.h>
43
45
#include < data-model-providers/codegen/Instance.h>
44
46
#include < setup_payload/OnboardingCodesUtil.h>
45
47
@@ -120,6 +122,37 @@ Identify sIdentify = {
120
122
121
123
#endif
122
124
125
+ /* *
126
+ * @brief Set deferred attributes storage
127
+ *
128
+ * @see Define a custom attribute persister which makes actual write of the CurrentHue, CurrentSaturation, CurrentLevel attributes
129
+ * value to the non-volatile storage only when it has remained constant for 5 seconds. This is to reduce the flash wearout when the
130
+ * attribute changes frequently as a result of MoveToLevel command. DeferredAttribute object describes a deferred attribute, but
131
+ * also holds a buffer with a value to be written, so it must live so long as the DeferredAttributePersistenceProvider object.
132
+ *
133
+ * @param ATTRIBUTES_ARRAY_SIZE The lenght of the DeferredAttribute array
134
+ * @param DEFERRED_STORAGE_TIME The deferred time(ms) to store attributes
135
+ */
136
+ #define ATTRIBUTES_ARRAY_SIZE (3U )
137
+ #define DEFERRED_STORAGE_TIME (5000U )
138
+
139
+ DeferredAttribute gPersisters [] = {
140
+ #if CONFIG_DEFERRED_ATTR_STORAGE
141
+ DeferredAttribute (
142
+ ConcreteAttributePath (kExampleEndpointId , Clusters::ColorControl::Id, Clusters::ColorControl::Attributes::CurrentHue::Id)),
143
+ DeferredAttribute (ConcreteAttributePath (kExampleEndpointId , Clusters::ColorControl::Id,
144
+ Clusters::ColorControl::Attributes::CurrentSaturation::Id)),
145
+ DeferredAttribute (
146
+ ConcreteAttributePath (kExampleEndpointId , Clusters::LevelControl::Id, Clusters::LevelControl::Attributes::CurrentLevel::Id))
147
+ #endif // CONFIG_DEFERRED_ATTR_STORAGE
148
+ };
149
+
150
+ // Deferred persistence will be auto-initialized as soon as the default persistence is initialized
151
+ DefaultAttributePersistenceProvider gSimpleAttributePersistence ;
152
+ DeferredAttributePersistenceProvider gDeferredAttributePersister (gSimpleAttributePersistence ,
153
+ Span<DeferredAttribute>(gPersisters , ATTRIBUTES_ARRAY_SIZE),
154
+ System::Clock::Milliseconds32(DEFERRED_STORAGE_TIME));
155
+
123
156
// NOTE! This key is for test/certification only and should not be available in production devices!
124
157
uint8_t sTestEventTriggerEnableKey [TestEventTriggerDelegate::kEnableKeyLength ] = { 0x00 , 0x11 , 0x22 , 0x33 , 0x44 , 0x55 , 0x66 , 0x77 ,
125
158
0x88 , 0x99 , 0xaa , 0xbb , 0xcc , 0xdd , 0xee , 0xff };
@@ -590,6 +623,8 @@ CHIP_ERROR AppTaskCommon::InitCommonParts(void)
590
623
VerifyOrDie (sTestEventTriggerDelegate .AddHandler (&sOtaTestEventTriggerHandler ) == CHIP_NO_ERROR);
591
624
#endif
592
625
(void ) initParams.InitializeStaticResourcesBeforeServerInit ();
626
+ VerifyOrDie (gSimpleAttributePersistence .Init (initParams.persistentStorageDelegate ) == CHIP_NO_ERROR);
627
+
593
628
initParams.dataModelProvider = CodegenDataModelProviderInstance (initParams.persistentStorageDelegate );
594
629
initParams.appDelegate = &sCallbacks ;
595
630
initParams.testEventTriggerDelegate = &sTestEventTriggerDelegate ;
@@ -600,6 +635,9 @@ CHIP_ERROR AppTaskCommon::InitCommonParts(void)
600
635
chip::DeviceLayer::SetDeviceInfoProvider (&gExampleDeviceInfoProvider );
601
636
#endif
602
637
638
+ /* add deferred storage attribute for provider */
639
+ app::SetAttributePersistenceProvider (&gDeferredAttributePersister );
640
+
603
641
ConfigurationMgr ().LogDeviceConfig ();
604
642
PrintOnboardingCodes (chip::RendezvousInformationFlags (chip::RendezvousInformationFlag::kBLE ));
605
643
0 commit comments