-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathASRConfig.cpp
294 lines (258 loc) · 12.8 KB
/
ASRConfig.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
*
* Copyright (c) 2021-2022 Project CHIP Authors
* Copyright (c) 2019-2020 Google LLC.
* Copyright (c) 2018 Nest Labs, Inc.
* 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.
*/
/* this file behaves like a config.h, comes first */
#include <platform/internal/CHIPDeviceLayerInternal.h>
#include <platform/KeyValueStoreManager.h>
#include <platform/ASR/ASRConfig.h>
#include <lib/core/CHIPEncoding.h>
#include <lib/support/CHIPMem.h>
#include <lib/support/CHIPMemString.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/ASR/ASRUtils.h>
namespace chip {
namespace DeviceLayer {
namespace Internal {
// *** CAUTION ***: Changing the names or namespaces of these values will *break* existing devices.
// Namespaces used to store device configuration information.
const char ASRConfig::kConfigNamespace_ChipFactory[] = "chip-factory";
const char ASRConfig::kConfigNamespace_ChipConfig[] = "chip-config";
const char ASRConfig::kConfigNamespace_ChipCounters[] = "chip-counters";
// Keys stored in the chip-factory namespace
const ASRConfig::Key ASRConfig::kConfigKey_SerialNum = { kConfigNamespace_ChipFactory, "serial-num" };
const ASRConfig::Key ASRConfig::kConfigKey_MfrDeviceId = { kConfigNamespace_ChipFactory, "device-id" };
const ASRConfig::Key ASRConfig::kConfigKey_MfrDeviceCert = { kConfigNamespace_ChipFactory, "device-cert" };
const ASRConfig::Key ASRConfig::kConfigKey_MfrDeviceICACerts = { kConfigNamespace_ChipFactory, "device-ca-certs" };
const ASRConfig::Key ASRConfig::kConfigKey_MfrDevicePrivateKey = { kConfigNamespace_ChipFactory, "device-key" };
const ASRConfig::Key ASRConfig::kConfigKey_SoftwareVersion = { kConfigNamespace_ChipFactory, "software-ver" };
const ASRConfig::Key ASRConfig::kConfigKey_HardwareVersion = { kConfigNamespace_ChipFactory, "hardware-ver" };
const ASRConfig::Key ASRConfig::kConfigKey_ManufacturingDate = { kConfigNamespace_ChipFactory, "mfg-date" };
#if !CONFIG_ENABLE_ASR_FACTORY_DATA_PROVIDER
const ASRConfig::Key ASRConfig::kConfigKey_SetupPinCode = { kConfigNamespace_ChipFactory, "pin-code" };
const ASRConfig::Key ASRConfig::kConfigKey_SetupDiscriminator = { kConfigNamespace_ChipFactory, "discriminator" };
const ASRConfig::Key ASRConfig::kConfigKey_Spake2pIterationCount = { kConfigNamespace_ChipFactory, "iteration-count" };
const ASRConfig::Key ASRConfig::kConfigKey_Spake2pSalt = { kConfigNamespace_ChipFactory, "salt" };
const ASRConfig::Key ASRConfig::kConfigKey_Spake2pVerifier = { kConfigNamespace_ChipFactory, "verifier" };
const ASRConfig::Key ASRConfig::kConfigKey_DACCert = { kConfigNamespace_ChipFactory, "dac-cert" };
const ASRConfig::Key ASRConfig::kConfigKey_DACPrivateKey = { kConfigNamespace_ChipFactory, "dac-key" };
const ASRConfig::Key ASRConfig::kConfigKey_DACPublicKey = { kConfigNamespace_ChipFactory, "dac-pub-key" };
const ASRConfig::Key ASRConfig::kConfigKey_PAICert = { kConfigNamespace_ChipFactory, "pai-cert" };
const ASRConfig::Key ASRConfig::kConfigKey_CertDeclaration = { kConfigNamespace_ChipFactory, "cert-dclrn" };
#endif
// Keys stored in the chip-config namespace
const ASRConfig::Key ASRConfig::kConfigKey_ServiceConfig = { kConfigNamespace_ChipConfig, "service-config" };
const ASRConfig::Key ASRConfig::kConfigKey_PairedAccountId = { kConfigNamespace_ChipConfig, "account-id" };
const ASRConfig::Key ASRConfig::kConfigKey_ServiceId = { kConfigNamespace_ChipConfig, "service-id" };
const ASRConfig::Key ASRConfig::kConfigKey_LastUsedEpochKeyId = { kConfigNamespace_ChipConfig, "last-ek-id" };
const ASRConfig::Key ASRConfig::kConfigKey_FailSafeArmed = { kConfigNamespace_ChipConfig, "fail-safe-armed" };
const ASRConfig::Key ASRConfig::kConfigKey_WiFiStationSecType = { kConfigNamespace_ChipConfig, "sta-sec-type" };
const ASRConfig::Key ASRConfig::kConfigKey_RegulatoryLocation = { kConfigNamespace_ChipConfig, "regulatory-location" };
const ASRConfig::Key ASRConfig::kConfigKey_CountryCode = { kConfigNamespace_ChipConfig, "country-code" };
const ASRConfig::Key ASRConfig::kConfigKey_WiFiSSID = { kConfigNamespace_ChipConfig, "wifi-ssid" };
const ASRConfig::Key ASRConfig::kConfigKey_WiFiPassword = { kConfigNamespace_ChipConfig, "wifi-password" };
const ASRConfig::Key ASRConfig::kConfigKey_WiFiSecurity = { kConfigNamespace_ChipConfig, "wifi-security" };
const ASRConfig::Key ASRConfig::kConfigKey_WiFiMode = { kConfigNamespace_ChipConfig, "wifimode" };
const ASRConfig::Key ASRConfig::kConfigKey_UniqueId = { kConfigNamespace_ChipConfig, "unique-id" };
const ASRConfig::Key ASRConfig::kConfigKey_VendorName = { kConfigNamespace_ChipConfig, "vendor-name" };
const ASRConfig::Key ASRConfig::kConfigKey_ProductName = { kConfigNamespace_ChipConfig, "product-name" };
const ASRConfig::Key ASRConfig::kConfigKey_HardwareVersionString = { kConfigNamespace_ChipConfig, "hardware-version-string" };
const ASRConfig::Key ASRConfig::kConfigKey_SoftwareVersionString = { kConfigNamespace_ChipConfig, "software-version-string" };
// Keys stored in the Chip-counters namespace
const ASRConfig::Key ASRConfig::kCounterKey_RebootCount = { kConfigNamespace_ChipCounters, "reboot-count" };
const ASRConfig::Key ASRConfig::kCounterKey_UpTime = { kConfigNamespace_ChipCounters, "up-time" };
const ASRConfig::Key ASRConfig::kCounterKey_TotalOperationalHours = { kConfigNamespace_ChipCounters, "total-hours" };
#define _KVSTORE_MAX_KEY_SIZE 64
CHIP_ERROR ASRConfig::ReadConfigValue(Key key, bool & val)
{
bool in;
char key_str[_KVSTORE_MAX_KEY_SIZE] = { 0 };
key.to_str(key_str, _KVSTORE_MAX_KEY_SIZE);
CHIP_ERROR err = PersistedStorage::KeyValueStoreMgr().Get(key_str, static_cast<void *>(&in), sizeof(bool));
val = in;
if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
{
err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;
}
return err;
}
CHIP_ERROR ASRConfig::ReadConfigValue(Key key, uint32_t & val)
{
uint32_t in;
char key_str[_KVSTORE_MAX_KEY_SIZE] = { 0 };
key.to_str(key_str, _KVSTORE_MAX_KEY_SIZE);
CHIP_ERROR err = PersistedStorage::KeyValueStoreMgr().Get(key_str, static_cast<void *>(&in), 4);
val = in;
if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
{
err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;
}
return err;
}
CHIP_ERROR ASRConfig::ReadConfigValue(Key key, uint64_t & val)
{
uint64_t in;
char key_str[_KVSTORE_MAX_KEY_SIZE] = { 0 };
key.to_str(key_str, _KVSTORE_MAX_KEY_SIZE);
CHIP_ERROR err = PersistedStorage::KeyValueStoreMgr().Get(key_str, static_cast<void *>(&in), 8);
val = in;
if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
{
err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;
}
return err;
}
CHIP_ERROR ASRConfig::ReadConfigValueStr(Key key, char * buf, size_t bufSize, size_t & outLen)
{
char key_str[_KVSTORE_MAX_KEY_SIZE] = { 0 };
key.to_str(key_str, _KVSTORE_MAX_KEY_SIZE);
CHIP_ERROR err = PersistedStorage::KeyValueStoreMgr().Get(key_str, buf, bufSize, &outLen);
if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
{
err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;
}
return err;
}
CHIP_ERROR ASRConfig::ReadConfigValueBin(Key key, uint8_t * buf, size_t bufSize, size_t & outLen)
{
char key_str[_KVSTORE_MAX_KEY_SIZE] = { 0 };
key.to_str(key_str, _KVSTORE_MAX_KEY_SIZE);
CHIP_ERROR err = PersistedStorage::KeyValueStoreMgr().Get(key_str, buf, bufSize, &outLen);
if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
{
err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;
}
return err;
}
CHIP_ERROR ASRConfig::WriteConfigValue(Key key, bool val)
{
char key_str[_KVSTORE_MAX_KEY_SIZE] = { 0 };
key.to_str(key_str, _KVSTORE_MAX_KEY_SIZE);
return PersistedStorage::KeyValueStoreMgr().Put(key_str, static_cast<void *>(&val), sizeof(bool));
}
CHIP_ERROR ASRConfig::WriteConfigValue(Key key, uint32_t val)
{
char key_str[_KVSTORE_MAX_KEY_SIZE] = { 0 };
key.to_str(key_str, _KVSTORE_MAX_KEY_SIZE);
return PersistedStorage::KeyValueStoreMgr().Put(key_str, static_cast<void *>(&val), 4);
}
CHIP_ERROR ASRConfig::WriteConfigValue(Key key, uint64_t val)
{
char key_str[_KVSTORE_MAX_KEY_SIZE] = { 0 };
key.to_str(key_str, _KVSTORE_MAX_KEY_SIZE);
return PersistedStorage::KeyValueStoreMgr().Put(key_str, static_cast<void *>(&val), 8);
}
CHIP_ERROR ASRConfig::WriteConfigValueStr(Key key, const char * str)
{
size_t size = strlen(str) + 1;
char key_str[_KVSTORE_MAX_KEY_SIZE] = { 0 };
key.to_str(key_str, _KVSTORE_MAX_KEY_SIZE);
return PersistedStorage::KeyValueStoreMgr().Put(key_str, str, size);
}
CHIP_ERROR ASRConfig::WriteConfigValueStr(Key key, const char * str, size_t strLen)
{
char key_str[_KVSTORE_MAX_KEY_SIZE] = { 0 };
key.to_str(key_str, _KVSTORE_MAX_KEY_SIZE);
return PersistedStorage::KeyValueStoreMgr().Put(key_str, str, strLen);
}
CHIP_ERROR ASRConfig::WriteConfigValueBin(Key key, const uint8_t * data, size_t dataLen)
{
char key_str[_KVSTORE_MAX_KEY_SIZE] = { 0 };
key.to_str(key_str, _KVSTORE_MAX_KEY_SIZE);
return PersistedStorage::KeyValueStoreMgr().Put(key_str, static_cast<void *>(&data), dataLen);
}
CHIP_ERROR ASRConfig::ClearConfigValue(Key key)
{
char key_str[_KVSTORE_MAX_KEY_SIZE] = { 0 };
key.to_str(key_str, _KVSTORE_MAX_KEY_SIZE);
return PersistedStorage::KeyValueStoreMgr().Delete(key_str);
}
bool ASRConfig::ConfigValueExists(Key key)
{
char key_str[_KVSTORE_MAX_KEY_SIZE] = { 0 };
char buf[4];
size_t outLen;
key.to_str(key_str, _KVSTORE_MAX_KEY_SIZE);
if (PersistedStorage::KeyValueStoreMgr().Get(key_str, buf, 4, &outLen) == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
{
return false;
}
return true;
}
#if CONFIG_ENABLE_ASR_FACTORY_DATA_PROVIDER
CHIP_ERROR ASRConfig::ReadFactoryConfigValue(asr_matter_partition_t matter_partition, uint8_t * buf, size_t bufSize,
size_t & outLen)
{
factory_error_t ret = asr_factory_config_read(matter_partition, buf, (uint32_t) bufSize, (uint32_t *) &outLen);
if (ret != FACTORY_NO_ERROR)
{
ChipLogProgress(DeviceLayer, "asr_factory_config_read: %d failed, err = %d\n", matter_partition, ret);
outLen = 0;
return CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;
}
return CHIP_NO_ERROR;
}
CHIP_ERROR ASRConfig::ReadFactoryConfigValue(asr_matter_partition_t matter_partition, uint32_t & val)
{
uint8_t buf[4];
size_t outlen = 0;
factory_error_t ret = asr_factory_config_read(matter_partition, buf, sizeof(uint32_t), (uint32_t *) &outlen);
if (outlen > sizeof(uint32_t))
return CHIP_ERROR_BUFFER_TOO_SMALL;
if (ret != FACTORY_NO_ERROR)
{
ChipLogProgress(DeviceLayer, "asr_factory_config_read: %d failed, err = %d\n", matter_partition, ret);
return CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;
}
else
{
val = *((uint32_t *) buf);
return CHIP_NO_ERROR;
}
}
#endif
// Clear out keys in config namespace
CHIP_ERROR ASRConfig::FactoryResetConfig(void)
{
CHIP_ERROR err = CHIP_NO_ERROR;
const Key * config_keys[] = { &kConfigKey_ServiceConfig, &kConfigKey_PairedAccountId, &kConfigKey_ServiceId,
&kConfigKey_LastUsedEpochKeyId, &kConfigKey_FailSafeArmed, &kConfigKey_WiFiStationSecType,
&kConfigKey_WiFiSSID, &kConfigKey_WiFiPassword, &kConfigKey_WiFiSecurity,
&kConfigKey_WiFiMode, &kConfigKey_SoftwareVersion };
for (uint32_t i = 0; i < (sizeof(config_keys) / sizeof(config_keys[0])); i++)
{
err = ClearConfigValue(*config_keys[i]);
// Something unexpected happened
if (err != CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND && err != CHIP_NO_ERROR)
{
return err;
}
}
// Erase all key-values including fabric info.
err = PersistedStorage::KeyValueStoreMgrImpl().Erase();
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Clear Key-Value Storage failed");
}
return CHIP_NO_ERROR;
}
void ASRConfig::RunConfigUnitTest() {}
} // namespace Internal
} // namespace DeviceLayer
} // namespace chip