@@ -34,14 +34,6 @@ namespace DeviceLayer {
34
34
namespace PersistedStorage {
35
35
namespace {
36
36
37
- struct ReadEntry
38
- {
39
- void * destination; // destination address
40
- size_t destinationBufferSize; // size of destination buffer
41
- size_t readSize; // [out] size of read entry value
42
- CHIP_ERROR result; // [out] read result
43
- };
44
-
45
37
struct DeleteSubtreeEntry
46
38
{
47
39
int result;
@@ -87,43 +79,6 @@ CHIP_ERROR MakeFullKey(char (&fullKey)[SETTINGS_MAX_NAME_LEN + 1], const char *
87
79
return CHIP_NO_ERROR;
88
80
}
89
81
90
- int LoadEntryCallback (const char * name, size_t entrySize, settings_read_cb readCb, void * cbArg, void * param)
91
- {
92
- ReadEntry & entry = *static_cast <ReadEntry *>(param);
93
-
94
- // If requested key X, process just node X and ignore all its descendants: X/*
95
- if (name != nullptr && *name != ' \0 ' )
96
- return 0 ;
97
-
98
- // Found requested key.
99
- uint8_t emptyValue[kEmptyValueSize ];
100
-
101
- if (entrySize == kEmptyValueSize && readCb (cbArg, emptyValue, kEmptyValueSize ) == kEmptyValueSize &&
102
- memcmp (emptyValue, kEmptyValue , kEmptyValueSize ) == 0 )
103
- {
104
- // Special case - an empty value represented by known magic bytes.
105
- entry.result = CHIP_NO_ERROR;
106
-
107
- // Return 1 to stop processing further keys
108
- return 1 ;
109
- }
110
-
111
- const ssize_t bytesRead = readCb (cbArg, entry.destination , entry.destinationBufferSize );
112
- entry.readSize = bytesRead > 0 ? bytesRead : 0 ;
113
-
114
- if (entrySize > entry.destinationBufferSize )
115
- {
116
- entry.result = CHIP_ERROR_BUFFER_TOO_SMALL;
117
- }
118
- else
119
- {
120
- entry.result = bytesRead > 0 ? CHIP_NO_ERROR : CHIP_ERROR_PERSISTED_STORAGE_FAILED;
121
- }
122
-
123
- // Return 1 to stop processing further keys
124
- return 1 ;
125
- }
126
-
127
82
int DeleteSubtreeCallback (const char * name, size_t /* entrySize */ , settings_read_cb /* readCb */ , void * /* cbArg */ ,
128
83
void * param)
129
84
{
@@ -155,23 +110,48 @@ void KeyValueStoreManagerImpl::Init()
155
110
CHIP_ERROR KeyValueStoreManagerImpl::_Get (const char * key, void * value, size_t value_size, size_t * read_bytes_size,
156
111
size_t offset_bytes) const
157
112
{
113
+ CHIP_ERROR result;
114
+ size_t readSize;
158
115
// Offset and partial reads are not supported, for now just return NOT_IMPLEMENTED.
159
116
// Support can be added in the future if this is needed.
160
117
VerifyOrReturnError (offset_bytes == 0 , CHIP_ERROR_NOT_IMPLEMENTED);
161
118
162
119
char fullKey[SETTINGS_MAX_NAME_LEN + 1 ];
163
120
ReturnErrorOnFailure (MakeFullKey (fullKey, key));
164
121
165
- ReadEntry entry{ value, value_size, 0 , CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND };
166
- settings_load_subtree_direct (fullKey, LoadEntryCallback, &entry);
122
+ // Found requested key.
123
+ const ssize_t bytesRead = settings_load_one (fullKey, value, value_size);
124
+ // If the return code is -ENOENT the key is not found
125
+ VerifyOrReturnError (bytesRead != -ENOENT, CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND);
126
+
127
+ if (bytesRead == kEmptyValueSize &&
128
+ memcmp (value, kEmptyValue , kEmptyValueSize ) == 0 )
129
+ {
130
+ // Special case - an empty value represented by known magic bytes.
131
+ result = CHIP_NO_ERROR;
132
+ readSize = 0 ;
133
+ }
134
+ else
135
+ {
136
+ readSize = bytesRead > 0 ? bytesRead : 0 ;
137
+
138
+ if ((size_t )bytesRead > value_size)
139
+ {
140
+ result = CHIP_ERROR_BUFFER_TOO_SMALL;
141
+ }
142
+ else
143
+ {
144
+ result = bytesRead > 0 ? CHIP_NO_ERROR : CHIP_ERROR_PERSISTED_STORAGE_FAILED;
145
+ }
146
+ }
167
147
168
148
// Assign readSize only in case read_bytes_size is not nullptr, as it is optional argument
169
149
if (read_bytes_size)
170
150
{
171
- *read_bytes_size = entry. readSize ;
151
+ *read_bytes_size = readSize;
172
152
}
173
153
174
- return entry. result ;
154
+ return result;
175
155
}
176
156
177
157
CHIP_ERROR KeyValueStoreManagerImpl::_Put (const char * key, const void * value, size_t value_size)
@@ -190,12 +170,36 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Put(const char * key, const void * value,
190
170
return CHIP_NO_ERROR;
191
171
}
192
172
173
+ CHIP_ERROR KeyValueStoreManagerImpl::_Exist (const char * key, size_t * val_len)
174
+ {
175
+ char fullKey[SETTINGS_MAX_NAME_LEN + 1 ];
176
+ ssize_t len;
177
+
178
+ ReturnErrorOnFailure (MakeFullKey (fullKey, key));
179
+
180
+ len = settings_exist (fullKey);
181
+
182
+ if (len > 0 )
183
+ {
184
+ *val_len = len;
185
+ return CHIP_NO_ERROR;;
186
+ }
187
+ if (len < 0 )
188
+ {
189
+ return CHIP_ERROR_PERSISTED_STORAGE_FAILED;
190
+ }
191
+
192
+ return CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
193
+ }
194
+
193
195
CHIP_ERROR KeyValueStoreManagerImpl::_Delete (const char * key)
194
196
{
195
197
char fullKey[SETTINGS_MAX_NAME_LEN + 1 ];
198
+ size_t val_len;
199
+
196
200
ReturnErrorOnFailure (MakeFullKey (fullKey, key));
197
201
198
- ReturnErrorCodeIf (Get (key, nullptr , 0 ) == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND,
202
+ ReturnErrorCodeIf (_Exist (key, &val_len ) == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND,
199
203
CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND);
200
204
ReturnErrorCodeIf (settings_delete (fullKey) != 0 , CHIP_ERROR_PERSISTED_STORAGE_FAILED);
201
205
0 commit comments