-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
KeyValueStoreManagerImpl.cpp
220 lines (173 loc) · 6.76 KB
/
KeyValueStoreManagerImpl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
*
* Copyright (c) 2021-2022 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file
* Platform-specific key value storage implementation for Zephyr
*/
#include <platform/internal/CHIPDeviceLayerInternal.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
#include <system/SystemError.h>
#include <zephyr/settings/settings.h>
namespace chip {
namespace DeviceLayer {
namespace PersistedStorage {
namespace {
struct ReadEntry
{
void * destination; // destination address
size_t destinationBufferSize; // size of destination buffer
size_t readSize; // [out] size of read entry value
CHIP_ERROR result; // [out] read result
};
struct DeleteSubtreeEntry
{
int result;
};
// Random magic bytes to represent an empty value.
// It is needed because Zephyr settings subsystem does not distinguish an empty value from no value.
constexpr uint8_t kEmptyValue[] = { 0x22, 0xa6, 0x54, 0xd1, 0x39 };
constexpr size_t kEmptyValueSize = sizeof(kEmptyValue);
// Prefix the input key with CHIP_DEVICE_CONFIG_SETTINGS_KEY "/"
CHIP_ERROR MakeFullKey(char (&fullKey)[SETTINGS_MAX_NAME_LEN + 1], const char * key)
{
VerifyOrReturnError(key != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
strcpy(fullKey, CHIP_DEVICE_CONFIG_SETTINGS_KEY "/");
char * dest = fullKey + strlen(CHIP_DEVICE_CONFIG_SETTINGS_KEY "/");
char * destEnd = fullKey + SETTINGS_MAX_NAME_LEN;
while (*key != '\0')
{
char keyChar = *key++;
bool escape = keyChar == '\\' || keyChar == '=';
if (keyChar == '=')
{
// '=' character is forbidden in a Zephyr setting key, so it must be escaped with "\e".
keyChar = 'e';
}
if (escape)
{
VerifyOrReturnError(dest < destEnd, CHIP_ERROR_INVALID_ARGUMENT);
*dest++ = '\\';
}
VerifyOrReturnError(dest < destEnd, CHIP_ERROR_INVALID_ARGUMENT);
*dest++ = keyChar;
}
*dest = 0;
return CHIP_NO_ERROR;
}
int LoadEntryCallback(const char * name, size_t entrySize, settings_read_cb readCb, void * cbArg, void * param)
{
ReadEntry & entry = *static_cast<ReadEntry *>(param);
// If requested key X, process just node X and ignore all its descendants: X/*
if (name != nullptr && *name != '\0')
return 0;
// Found requested key.
uint8_t emptyValue[kEmptyValueSize];
if (entrySize == kEmptyValueSize && readCb(cbArg, emptyValue, kEmptyValueSize) == kEmptyValueSize &&
memcmp(emptyValue, kEmptyValue, kEmptyValueSize) == 0)
{
// Special case - an empty value represented by known magic bytes.
entry.result = CHIP_NO_ERROR;
// Return 1 to stop processing further keys
return 1;
}
const ssize_t bytesRead = readCb(cbArg, entry.destination, entry.destinationBufferSize);
entry.readSize = bytesRead > 0 ? bytesRead : 0;
if (entrySize > entry.destinationBufferSize)
{
entry.result = CHIP_ERROR_BUFFER_TOO_SMALL;
}
else
{
entry.result = bytesRead > 0 ? CHIP_NO_ERROR : CHIP_ERROR_PERSISTED_STORAGE_FAILED;
}
// Return 1 to stop processing further keys
return 1;
}
int DeleteSubtreeCallback(const char * name, size_t /* entrySize */, settings_read_cb /* readCb */, void * /* cbArg */,
void * param)
{
DeleteSubtreeEntry & entry = *static_cast<DeleteSubtreeEntry *>(param);
char fullKey[SETTINGS_MAX_NAME_LEN + 1];
// name comes from Zephyr settings subsystem so it is guaranteed to fit in the buffer.
(void) snprintf(fullKey, sizeof(fullKey), CHIP_DEVICE_CONFIG_SETTINGS_KEY "/%s", StringOrNullMarker(name));
const int result = settings_delete(fullKey);
// Return the first error, but continue removing remaining keys anyway.
if (entry.result == 0)
{
entry.result = result;
}
return 0;
}
} // namespace
KeyValueStoreManagerImpl KeyValueStoreManagerImpl::sInstance;
void KeyValueStoreManagerImpl::Init()
{
VerifyOrDie(settings_subsys_init() == 0);
}
CHIP_ERROR KeyValueStoreManagerImpl::_Get(const char * key, void * value, size_t value_size, size_t * read_bytes_size,
size_t offset_bytes) const
{
// Offset and partial reads are not supported, for now just return NOT_IMPLEMENTED.
// Support can be added in the future if this is needed.
VerifyOrReturnError(offset_bytes == 0, CHIP_ERROR_NOT_IMPLEMENTED);
char fullKey[SETTINGS_MAX_NAME_LEN + 1];
ReturnErrorOnFailure(MakeFullKey(fullKey, key));
ReadEntry entry{ value, value_size, 0, CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND };
settings_load_subtree_direct(fullKey, LoadEntryCallback, &entry);
// Assign readSize only in case read_bytes_size is not nullptr, as it is optional argument
if (read_bytes_size)
{
*read_bytes_size = entry.readSize;
}
return entry.result;
}
CHIP_ERROR KeyValueStoreManagerImpl::_Put(const char * key, const void * value, size_t value_size)
{
char fullKey[SETTINGS_MAX_NAME_LEN + 1];
ReturnErrorOnFailure(MakeFullKey(fullKey, key));
if (value_size == 0)
{
value = kEmptyValue;
value_size = kEmptyValueSize;
}
VerifyOrReturnError(settings_save_one(fullKey, value, value_size) == 0, CHIP_ERROR_PERSISTED_STORAGE_FAILED);
return CHIP_NO_ERROR;
}
CHIP_ERROR KeyValueStoreManagerImpl::_Delete(const char * key)
{
char fullKey[SETTINGS_MAX_NAME_LEN + 1];
ReturnErrorOnFailure(MakeFullKey(fullKey, key));
VerifyOrReturnError(Get(key, nullptr, 0) != CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND,
CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND);
VerifyOrReturnError(settings_delete(fullKey) == 0, CHIP_ERROR_PERSISTED_STORAGE_FAILED);
return CHIP_NO_ERROR;
}
CHIP_ERROR KeyValueStoreManagerImpl::DoFactoryReset()
{
DeleteSubtreeEntry entry{ /* success */ 0 };
int result = settings_load_subtree_direct(CHIP_DEVICE_CONFIG_SETTINGS_KEY, DeleteSubtreeCallback, &entry);
if (result == 0)
{
result = entry.result;
}
return System::MapErrorZephyr(result);
}
} // namespace PersistedStorage
} // namespace DeviceLayer
} // namespace chip