|
16 | 16 | #pragma once
|
17 | 17 |
|
18 | 18 | #include <app/ConcreteAttributePath.h>
|
19 |
| -#include <app/data-model/Nullable.h> |
20 | 19 | #include <app/util/attribute-metadata.h>
|
21 |
| -#include <cstring> |
22 |
| -#include <inttypes.h> |
23 |
| -#include <lib/support/BufferReader.h> |
24 |
| -#include <lib/support/BufferWriter.h> |
25 | 20 | #include <lib/support/Span.h>
|
26 | 21 |
|
27 | 22 | namespace chip {
|
28 | 23 | namespace app {
|
29 | 24 |
|
30 | 25 | /**
|
31 |
| - * Interface for persisting attribute values. |
| 26 | + * Interface for persisting attribute values. This will write attributes in storage with platform endianness for scalars |
| 27 | + * and uses a different key space from SafeAttributePersistenceProvider. |
| 28 | + * When storing cluster attributes that are managed via the AttributeAccessInterface, it is recommended to |
| 29 | + * use SafeAttributePersistenceProvider. |
32 | 30 | */
|
33 | 31 |
|
34 | 32 | class AttributePersistenceProvider
|
@@ -61,171 +59,17 @@ class AttributePersistenceProvider
|
61 | 59 | * Read an attribute value from non-volatile memory.
|
62 | 60 | *
|
63 | 61 | * @param [in] aPath the attribute path for the data being persisted.
|
64 |
| - * @param [in] aType the attribute type. |
65 |
| - * @param [in] aSize the attribute size. |
| 62 | + * @param [in] aMetadata the attribute metadata, as a convenience. |
66 | 63 | * @param [in,out] aValue where to place the data. The size of the buffer
|
67 |
| - * will be equal to `size`. |
| 64 | + * will be equal to `size` member of aMetadata. |
68 | 65 | *
|
69 | 66 | * The data is expected to be in native endianness for
|
70 | 67 | * integers and floats. For strings, see the string
|
71 | 68 | * representation description in the WriteValue
|
72 | 69 | * documentation.
|
73 | 70 | */
|
74 |
| - virtual CHIP_ERROR ReadValue(const ConcreteAttributePath & aPath, EmberAfAttributeType aType, size_t aSize, |
| 71 | + virtual CHIP_ERROR ReadValue(const ConcreteAttributePath & aPath, const EmberAfAttributeMetadata * aMetadata, |
75 | 72 | MutableByteSpan & aValue) = 0;
|
76 |
| - |
77 |
| - /** |
78 |
| - * Get the KVS representation of null for the given type. |
79 |
| - * @tparam T The type for which the null representation should be returned. |
80 |
| - * @return A value of type T that in the KVS represents null. |
81 |
| - */ |
82 |
| - template <typename T, std::enable_if_t<std::is_same<bool, T>::value, bool> = true> |
83 |
| - static uint8_t GetNullValueForNullableType() |
84 |
| - { |
85 |
| - return 0xff; |
86 |
| - } |
87 |
| - |
88 |
| - /** |
89 |
| - * Get the KVS representation of null for the given type. |
90 |
| - * @tparam T The type for which the null representation should be returned. |
91 |
| - * @return A value of type T that in the KVS represents null. |
92 |
| - */ |
93 |
| - template <typename T, std::enable_if_t<std::is_unsigned<T>::value && !std::is_same<bool, T>::value, bool> = true> |
94 |
| - static T GetNullValueForNullableType() |
95 |
| - { |
96 |
| - T nullValue = 0; |
97 |
| - nullValue = T(~nullValue); |
98 |
| - return nullValue; |
99 |
| - } |
100 |
| - |
101 |
| - /** |
102 |
| - * Get the KVS representation of null for the given type. |
103 |
| - * @tparam T The type for which the null representation should be returned. |
104 |
| - * @return A value of type T that in the KVS represents null. |
105 |
| - */ |
106 |
| - template <typename T, std::enable_if_t<std::is_signed<T>::value && !std::is_same<bool, T>::value, bool> = true> |
107 |
| - static T GetNullValueForNullableType() |
108 |
| - { |
109 |
| - T shiftBit = 1; |
110 |
| - return T(shiftBit << ((sizeof(T) * 8) - 1)); |
111 |
| - } |
112 |
| - |
113 |
| - // The following API provides helper functions to simplify the access of commonly used types. |
114 |
| - // The API may not be complete. |
115 |
| - // Currently implemented write and read types are: uint8_t, uint16_t, uint32_t, unit64_t and |
116 |
| - // their nullable varieties, and bool. |
117 |
| - |
118 |
| - /** |
119 |
| - * Write an attribute value of type intX, uintX or bool to non-volatile memory. |
120 |
| - * |
121 |
| - * @param [in] aPath the attribute path for the data being written. |
122 |
| - * @param [in] aValue the data to write. |
123 |
| - */ |
124 |
| - template <typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true> |
125 |
| - CHIP_ERROR WriteScalarValue(const ConcreteAttributePath & aPath, T & aValue) |
126 |
| - { |
127 |
| - uint8_t value[sizeof(T)]; |
128 |
| - auto w = Encoding::LittleEndian::BufferWriter(value, sizeof(T)); |
129 |
| - w.EndianPut(uint64_t(aValue), sizeof(T)); |
130 |
| - |
131 |
| - return WriteValue(aPath, ByteSpan(value)); |
132 |
| - } |
133 |
| - |
134 |
| - /** |
135 |
| - * Read an attribute of type intX, uintX or bool from non-volatile memory. |
136 |
| - * |
137 |
| - * @param [in] aPath the attribute path for the data being persisted. |
138 |
| - * @param [in,out] aValue where to place the data. |
139 |
| - */ |
140 |
| - template <typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true> |
141 |
| - CHIP_ERROR ReadScalarValue(const ConcreteAttributePath & aPath, T & aValue) |
142 |
| - { |
143 |
| - uint8_t attrData[sizeof(T)]; |
144 |
| - MutableByteSpan tempVal(attrData); |
145 |
| - // **Note** aType in the ReadValue function is only used to check if the value is of a string type. Since this template |
146 |
| - // function is only enabled for integral values, we know that this case will not occur, so we can pass the enum of an |
147 |
| - // arbitrary integral type. 0x20 is the ZCL enum type for ZCL_INT8U_ATTRIBUTE_TYPE. |
148 |
| - auto err = ReadValue(aPath, 0x20, sizeof(T), tempVal); |
149 |
| - if (err != CHIP_NO_ERROR) |
150 |
| - { |
151 |
| - return err; |
152 |
| - } |
153 |
| - |
154 |
| - chip::Encoding::LittleEndian::Reader r(tempVal.data(), tempVal.size()); |
155 |
| - r.RawReadLowLevelBeCareful(&aValue); |
156 |
| - return r.StatusCode(); |
157 |
| - } |
158 |
| - |
159 |
| - /** |
160 |
| - * Write an attribute value of type nullable intX, uintX or bool to non-volatile memory. |
161 |
| - * |
162 |
| - * @param [in] aPath the attribute path for the data being written. |
163 |
| - * @param [in] aValue the data to write. |
164 |
| - */ |
165 |
| - template <typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true> |
166 |
| - CHIP_ERROR WriteScalarValue(const ConcreteAttributePath & aPath, DataModel::Nullable<T> & aValue) |
167 |
| - { |
168 |
| - if (aValue.IsNull()) |
169 |
| - { |
170 |
| - auto nullVal = GetNullValueForNullableType<T>(); |
171 |
| - return WriteScalarValue(aPath, nullVal); |
172 |
| - } |
173 |
| - return WriteScalarValue(aPath, aValue.Value()); |
174 |
| - } |
175 |
| - |
176 |
| - /** |
177 |
| - * Read an attribute of type nullable intX, uintX from non-volatile memory. |
178 |
| - * |
179 |
| - * @param [in] aPath the attribute path for the data being persisted. |
180 |
| - * @param [in,out] aValue where to place the data. |
181 |
| - */ |
182 |
| - template <typename T, std::enable_if_t<std::is_integral<T>::value && !std::is_same<bool, T>::value, bool> = true> |
183 |
| - CHIP_ERROR ReadScalarValue(const ConcreteAttributePath & aPath, DataModel::Nullable<T> & aValue) |
184 |
| - { |
185 |
| - T tempIntegral; |
186 |
| - |
187 |
| - CHIP_ERROR err = ReadScalarValue(aPath, tempIntegral); |
188 |
| - if (err != CHIP_NO_ERROR) |
189 |
| - { |
190 |
| - return err; |
191 |
| - } |
192 |
| - |
193 |
| - if (tempIntegral == GetNullValueForNullableType<T>()) |
194 |
| - { |
195 |
| - aValue.SetNull(); |
196 |
| - return CHIP_NO_ERROR; |
197 |
| - } |
198 |
| - |
199 |
| - aValue.SetNonNull(tempIntegral); |
200 |
| - return CHIP_NO_ERROR; |
201 |
| - } |
202 |
| - |
203 |
| - /** |
204 |
| - * Read an attribute of type nullable bool from non-volatile memory. |
205 |
| - * |
206 |
| - * @param [in] aPath the attribute path for the data being persisted. |
207 |
| - * @param [in,out] aValue where to place the data. |
208 |
| - */ |
209 |
| - template <typename T, std::enable_if_t<std::is_same<bool, T>::value, bool> = true> |
210 |
| - CHIP_ERROR ReadScalarValue(const ConcreteAttributePath & aPath, DataModel::Nullable<T> & aValue) |
211 |
| - { |
212 |
| - uint8_t tempIntegral; |
213 |
| - |
214 |
| - CHIP_ERROR err = ReadScalarValue(aPath, tempIntegral); |
215 |
| - if (err != CHIP_NO_ERROR) |
216 |
| - { |
217 |
| - return err; |
218 |
| - } |
219 |
| - |
220 |
| - if (tempIntegral == GetNullValueForNullableType<T>()) |
221 |
| - { |
222 |
| - aValue.SetNull(); |
223 |
| - return CHIP_NO_ERROR; |
224 |
| - } |
225 |
| - |
226 |
| - aValue.SetNonNull(tempIntegral); |
227 |
| - return CHIP_NO_ERROR; |
228 |
| - } |
229 | 73 | };
|
230 | 74 |
|
231 | 75 | /**
|
|
0 commit comments