3 * Copyright (c) 2020 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 * Utilities for accessing persisted device configuration on
21 * platforms based on the Qorvo QPG6100 SDK.
23 /* this file behaves like a config.h, comes first */
24 #include <platform/internal/CHIPDeviceLayerInternal.h>
26 #include <platform/qpg6100/qpg6100Config.h>
28 #include <core/CHIPEncoding.h>
29 #include <lib/core/CHIPSafeCasts.h>
30 #include <platform/internal/testing/ConfigUnitTest.h>
31 #include <support/CodeUtils.h>
32 #include <support/logging/CHIPLogging.h>
38 namespace DeviceLayer {
41 CHIP_ERROR QPG6100Config::Init()
48 uint16_t QPG6100Config::GetSettingsMaxValueLength(Key key)
52 return (qvCHIP_Nvm_GetMaxKeyLen(key, &keyLen) == QV_STATUS_NO_ERROR) ? keyLen : 0;
55 CHIP_ERROR QPG6100Config::ReadConfigValue(Key key, bool & val)
59 length = sizeof(bool);
61 return MapNVMError(qvCHIP_Nvm_Restore(key, reinterpret_cast<uint8_t *>(&val), &length));
64 CHIP_ERROR QPG6100Config::ReadConfigValue(Key key, uint32_t & val)
68 length = sizeof(uint32_t);
70 return MapNVMError(qvCHIP_Nvm_Restore(key, reinterpret_cast<uint8_t *>(&val), &length));
73 CHIP_ERROR QPG6100Config::ReadConfigValue(Key key, uint64_t & val)
77 length = sizeof(uint64_t);
79 return MapNVMError(qvCHIP_Nvm_Restore(key, reinterpret_cast<uint8_t *>(&val), &length));
82 CHIP_ERROR QPG6100Config::ReadConfigValueStr(Key key, char * buf, size_t bufSize, size_t & outLen)
87 if ((buf == NULL) || (bufSize > UINT16_MAX))
89 return CHIP_ERROR_INVALID_ARGUMENT;
92 length = static_cast<uint16_t>(bufSize);
93 res = qvCHIP_Nvm_Restore(key, Uint8::from_char(buf), &length);
96 return CHIP_ERROR_BUFFER_TOO_SMALL;
98 if (res == QV_STATUS_NO_ERROR)
104 return MapNVMError(res);
107 CHIP_ERROR QPG6100Config::ReadConfigValueBin(Key key, uint8_t * buf, size_t bufSize, size_t & outLen)
113 if ((buf == NULL) || (bufSize > UINT16_MAX))
115 return CHIP_ERROR_INVALID_ARGUMENT;
118 length = static_cast<uint16_t>(bufSize);
120 res = qvCHIP_Nvm_Restore(key, buffer, &length);
121 if (res == QV_STATUS_NO_ERROR)
124 if (outLen > bufSize)
126 return CHIP_ERROR_BUFFER_TOO_SMALL;
129 memcpy(buf, buffer, outLen);
133 return MapNVMError(res);
136 CHIP_ERROR QPG6100Config::WriteConfigValue(Key key, bool val)
140 length = sizeof(bool);
142 return MapNVMError(qvCHIP_Nvm_Backup(key, reinterpret_cast<uint8_t *>(&val), length));
145 CHIP_ERROR QPG6100Config::WriteConfigValue(Key key, uint32_t val)
149 length = sizeof(uint32_t);
151 return MapNVMError(qvCHIP_Nvm_Backup(key, reinterpret_cast<uint8_t *>(&val), length));
154 CHIP_ERROR QPG6100Config::WriteConfigValue(Key key, uint64_t val)
158 length = sizeof(uint64_t);
160 return MapNVMError(qvCHIP_Nvm_Backup(key, reinterpret_cast<uint8_t *>(&val), length));
163 CHIP_ERROR QPG6100Config::WriteConfigValueStr(Key key, const char * str)
165 return WriteConfigValueStr(key, str, (str != NULL) ? strlen(str) : 0);
168 CHIP_ERROR QPG6100Config::WriteConfigValueStr(Key key, const char * str, size_t strLen)
170 if (strLen > UINT16_MAX)
172 return CHIP_ERROR_INVALID_ARGUMENT;
176 qvCHIP_Nvm_ClearValue(key);
180 return MapNVMError(qvCHIP_Nvm_Backup(key, Uint8::from_const_char(str), static_cast<uint16_t>(strLen)));
183 return CHIP_NO_ERROR;
186 CHIP_ERROR QPG6100Config::WriteConfigValueBin(Key key, const uint8_t * data, size_t dataLen)
188 if (dataLen > UINT16_MAX)
190 return CHIP_ERROR_BUFFER_TOO_SMALL;
194 qvCHIP_Nvm_ClearValue(key);
198 return MapNVMError(qvCHIP_Nvm_Backup(key, data, static_cast<uint16_t>(dataLen)));
201 return CHIP_NO_ERROR;
204 CHIP_ERROR QPG6100Config::ClearConfigValue(Key key)
206 qvCHIP_Nvm_ClearValue(key);
207 return CHIP_NO_ERROR;
210 bool QPG6100Config::ConfigValueExists(Key key)
212 return qvCHIP_Nvm_ValueExists(key, NULL);
215 CHIP_ERROR QPG6100Config::FactoryResetConfig(void)
217 for (Key key = kMinConfigKey_ChipConfig; key <= kMaxConfigKey_ChipConfig; key++)
219 ClearConfigValue(key);
222 return CHIP_NO_ERROR;
225 CHIP_ERROR QPG6100Config::ForEachRecord(Key firstKey, Key lastKey, bool addNewRecord, ForEachRecordFunct funct)
227 CHIP_ERROR err = CHIP_NO_ERROR;
230 for (Key nvmKey = firstKey; nvmKey <= lastKey; ++nvmKey)
232 if (qvCHIP_Nvm_ValueExists(nvmKey, &length))
236 // Invoke the caller's function
237 // (for retrieve,store,delete,enumerate GroupKey operations).
238 err = funct(nvmKey, length);
245 // Invoke caller's function
246 // (for add GroupKey operation).
247 err = funct(nvmKey, length);
258 CHIP_ERROR QPG6100Config::MapNVMError(qvStatus_t aStatus)
262 case QV_STATUS_NO_ERROR:
263 return CHIP_NO_ERROR;
264 case QV_STATUS_BUFFER_TOO_SMALL:
265 return CHIP_ERROR_BUFFER_TOO_SMALL;
266 case QV_STATUS_INVALID_ARGUMENT:
267 return CHIP_ERROR_INVALID_ARGUMENT;
268 case QV_STATUS_KEY_LEN_TOO_SMALL:
269 return CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
270 case QV_STATUS_INVALID_DATA:
271 return CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
275 return CHIP_ERROR_INTERNAL;
278 void QPG6100Config::RunConfigUnitTest()
280 // Run common unit test
281 ::chip::DeviceLayer::Internal::RunConfigUnitTest<QPG6100Config>();
284 } // namespace Internal
285 } // namespace DeviceLayer