@@ -40,8 +40,8 @@ class SafeAttributePersistenceProvider
40
40
41
41
// The following API provides helper functions to simplify the access of commonly used types.
42
42
// The API may not be complete.
43
- // Currently implemented write and read types are: uint8_t, uint16_t, uint32_t, unit64_t and
44
- // their nullable varieties, and bool .
43
+ // Currently implemented write and read types are: bool, uint8_t, uint16_t, uint32_t, unit64_t and
44
+ // their nullable varieties, as well as ByteSpans .
45
45
46
46
/* *
47
47
* Write an attribute value of type intX, uintX or bool to non-volatile memory.
@@ -50,7 +50,7 @@ class SafeAttributePersistenceProvider
50
50
* @param [in] aValue the data to write.
51
51
*/
52
52
template <typename T, std::enable_if_t <std::is_integral<T>::value, bool > = true >
53
- CHIP_ERROR WriteScalarValue (const ConcreteAttributePath & aPath, T & aValue)
53
+ CHIP_ERROR WriteScalarValue (const ConcreteAttributePath & aPath, T aValue)
54
54
{
55
55
uint8_t value[sizeof (T)];
56
56
auto w = Encoding::LittleEndian::BufferWriter (value, sizeof (T));
@@ -71,18 +71,16 @@ class SafeAttributePersistenceProvider
71
71
*
72
72
* @param [in] aPath the attribute path for the data being persisted.
73
73
* @param [in,out] aValue where to place the data.
74
+ *
75
+ * @retval CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND if no stored value exists for the attribute
74
76
*/
75
77
template <typename T, std::enable_if_t <std::is_integral<T>::value, bool > = true >
76
78
CHIP_ERROR ReadScalarValue (const ConcreteAttributePath & aPath, T & aValue)
77
79
{
78
80
uint8_t attrData[sizeof (T)];
79
81
MutableByteSpan tempVal (attrData);
80
- auto err = SafeReadValue (aPath, tempVal);
81
- if (err != CHIP_NO_ERROR)
82
- {
83
- return err;
84
- }
85
-
82
+ ReturnErrorOnFailure (SafeReadValue (aPath, tempVal));
83
+ VerifyOrReturnError (tempVal.size () == sizeof (T), CHIP_ERROR_INCORRECT_STATE);
86
84
Encoding::LittleEndian::Reader r (tempVal.data (), tempVal.size ());
87
85
r.RawReadLowLevelBeCareful (&aValue);
88
86
return r.StatusCode ();
@@ -95,7 +93,7 @@ class SafeAttributePersistenceProvider
95
93
* @param [in] aValue the data to write.
96
94
*/
97
95
template <typename T, std::enable_if_t <std::is_integral<T>::value, bool > = true >
98
- CHIP_ERROR WriteScalarValue (const ConcreteAttributePath & aPath, DataModel::Nullable<T> & aValue)
96
+ CHIP_ERROR WriteScalarValue (const ConcreteAttributePath & aPath, const DataModel::Nullable<T> & aValue)
99
97
{
100
98
typename NumericAttributeTraits<T>::StorageType storageValue;
101
99
if (aValue.IsNull ())
@@ -114,16 +112,14 @@ class SafeAttributePersistenceProvider
114
112
*
115
113
* @param [in] aPath the attribute path for the data being persisted.
116
114
* @param [in,out] aValue where to place the data.
115
+ *
116
+ * @retval CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND if no stored value exists for the attribute
117
117
*/
118
118
template <typename T, std::enable_if_t <std::is_integral<T>::value, bool > = true >
119
119
CHIP_ERROR ReadScalarValue (const ConcreteAttributePath & aPath, DataModel::Nullable<T> & aValue)
120
120
{
121
121
typename NumericAttributeTraits<T>::StorageType storageValue;
122
- CHIP_ERROR err = ReadScalarValue (aPath, storageValue);
123
- if (err != CHIP_NO_ERROR)
124
- {
125
- return err;
126
- }
122
+ ReturnErrorOnFailure (ReadScalarValue (aPath, storageValue));
127
123
128
124
if (NumericAttributeTraits<T>::IsNullValue (storageValue))
129
125
{
@@ -137,25 +133,25 @@ class SafeAttributePersistenceProvider
137
133
return CHIP_NO_ERROR;
138
134
}
139
135
140
- protected:
141
136
/* *
142
137
* Write an attribute value from the attribute store (i.e. not a struct or
143
138
* list) to non-volatile memory.
144
139
*
145
140
* @param [in] aPath the attribute path for the data being written.
146
- * @param [in] aValue the data to write. The value should be stored as-is.
141
+ * @param [in] aValue the data to write. The value will be stored as-is.
147
142
*/
148
143
virtual CHIP_ERROR SafeWriteValue (const ConcreteAttributePath & aPath, const ByteSpan & aValue) = 0;
149
144
150
145
/* *
151
- * Read an attribute value from non-volatile memory.
152
- * It can be assumed that this method will never be called upon to read
153
- * an attribute of type string or long-string.
146
+ * Read an attribute value as a raw sequence of bytes from non-volatile memory.
154
147
*
155
148
* @param [in] aPath the attribute path for the data being persisted.
156
149
* @param [in,out] aValue where to place the data. The size of the buffer
157
- * will be equal to `aValue.size()`. The callee is expected to adjust
158
- * aValue's size to the actual number of bytes read.
150
+ * will be equal to `aValue.size()`. On success aValue.size()
151
+ * will be the actual number of bytes read.
152
+ *
153
+ * @retval CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND if no stored value exists for the attribute
154
+ * @retval CHIP_ERROR_BUFFER_TOO_SMALL aValue.size() is too small to hold the value.
159
155
*/
160
156
virtual CHIP_ERROR SafeReadValue (const ConcreteAttributePath & aPath, MutableByteSpan & aValue) = 0;
161
157
};
0 commit comments