3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2020 Google LLC.
5 * Copyright (c) 2018 Nest Labs, Inc.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 * Utilities for accessing persisted device configuration on
23 * platforms based on the NXP K32W SDK.
25 /* this file behaves like a config.h, comes first */
26 #include <platform/internal/CHIPDeviceLayerInternal.h>
28 #include <platform/K32W/K32WConfig.h>
30 #include <core/CHIPEncoding.h>
31 #include <platform/internal/testing/ConfigUnitTest.h>
36 namespace DeviceLayer {
39 CHIP_ERROR K32WConfig::Init()
44 /* Initialise the Persistent Data Manager */
45 pdmStatus = PDM_Init();
46 SuccessOrExit(err = MapPdmInitStatus(pdmStatus));
52 CHIP_ERROR K32WConfig::ReadConfigValue(Key key, bool & val)
57 PDM_teStatus pdmStatus;
59 VerifyOrExit(ValidConfigKey(key), err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND); // Verify key id.
60 pdmStatus = PDM_eReadDataFromRecord((uint16_t) key, &tempVal, sizeof(bool), &bytesRead);
61 SuccessOrExit(err = MapPdmStatus(pdmStatus));
68 CHIP_ERROR K32WConfig::ReadConfigValue(Key key, uint32_t & val)
74 PDM_teStatus pdmStatus;
76 VerifyOrExit(ValidConfigKey(key), err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND); // Verify key id.
77 if (PDM_bDoesDataExist((uint16_t) key, &recordSize))
79 pdmStatus = PDM_eReadDataFromRecord((uint16_t) key, &tempVal, sizeof(uint32_t), &bytesRead);
80 SuccessOrExit(err = MapPdmStatus(pdmStatus));
85 err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;
93 CHIP_ERROR K32WConfig::ReadConfigValue(Key key, uint64_t & val)
99 PDM_teStatus pdmStatus;
101 VerifyOrExit(ValidConfigKey(key), err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND); // Verify key id.
103 if (PDM_bDoesDataExist((uint16_t) key, &recordSize))
105 pdmStatus = PDM_eReadDataFromRecord((uint16_t) key, &tempVal, sizeof(uint64_t), &bytesRead);
106 SuccessOrExit(err = MapPdmStatus(pdmStatus));
111 err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;
119 CHIP_ERROR K32WConfig::ReadConfigValueStr(Key key, char * buf, size_t bufSize, size_t & outLen)
122 const uint8_t * strEnd;
126 PDM_teStatus pdmStatus;
130 VerifyOrExit(ValidConfigKey(key), err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND); // Verify key id.
132 if (PDM_bDoesDataExist((uint16_t) key, &recordSize))
134 pData = (char *) pvPortMalloc(recordSize);
135 VerifyOrExit((pData != NULL), err = CHIP_ERROR_NO_MEMORY);
137 pdmStatus = PDM_eReadDataFromRecord((uint16_t) key, pData, recordSize, &bytesRead);
138 SuccessOrExit(err = MapPdmStatus(pdmStatus));
140 strEnd = (const uint8_t *) memchr(pData, 0, bytesRead);
141 VerifyOrExit(strEnd != NULL, err = CHIP_ERROR_INVALID_ARGUMENT);
143 outLen = strEnd - (const uint8_t *) pData;
145 // NOTE: the caller is allowed to pass NULL for buf to query the length of the stored value.
149 VerifyOrExit(bufSize > outLen, err = CHIP_ERROR_BUFFER_TOO_SMALL);
151 memcpy(buf, pData, outLen + 1);
156 err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;
163 vPortFree((void *) pData);
168 CHIP_ERROR K32WConfig::ReadConfigValueBin(Key key, uint8_t * buf, size_t bufSize, size_t & outLen)
171 uint8_t * pData = NULL;
174 PDM_teStatus pdmStatus;
178 VerifyOrExit(ValidConfigKey(key), err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND); // Verify key id.
180 if (PDM_bDoesDataExist((uint16_t) key, &recordSize))
182 pData = (uint8_t *) pvPortMalloc(recordSize);
183 VerifyOrExit((pData != NULL), err = CHIP_ERROR_NO_MEMORY);
185 pdmStatus = PDM_eReadDataFromRecord((uint16_t) key, pData, recordSize, &bytesRead);
186 SuccessOrExit(err = MapPdmStatus(pdmStatus));
190 VerifyOrExit((bufSize >= bytesRead), err = CHIP_ERROR_BUFFER_TOO_SMALL);
191 memcpy(buf, pData, bytesRead);
197 err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;
204 vPortFree((void *) pData);
210 CHIP_ERROR K32WConfig::ReadConfigValueCounter(uint8_t counterIdx, uint32_t & val)
216 PDM_teStatus pdmStatus;
218 Key key = kMinConfigKey_ChipCounter + counterIdx;
219 VerifyOrExit(ValidConfigKey(key), err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND); // Verify key id.
221 if (PDM_bDoesDataExist((uint16_t) key, &recordSize))
223 pdmStatus = PDM_eReadDataFromRecord((uint16_t) key, &tempVal, sizeof(uint32_t), &bytesRead);
224 SuccessOrExit(err = MapPdmStatus(pdmStatus));
229 err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;
237 CHIP_ERROR K32WConfig::WriteConfigValue(Key key, bool val)
240 PDM_teStatus pdmStatus;
242 VerifyOrExit(ValidConfigKey(key), err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND); // Verify key id.
243 pdmStatus = PDM_eSaveRecordData((uint16_t) key, &val, sizeof(bool));
244 SuccessOrExit(err = MapPdmStatus(pdmStatus));
250 CHIP_ERROR K32WConfig::WriteConfigValue(Key key, uint32_t val)
253 PDM_teStatus pdmStatus;
255 VerifyOrExit(ValidConfigKey(key), err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND); // Verify key id.
256 pdmStatus = PDM_eSaveRecordData((uint16_t) key, &val, sizeof(uint32_t));
257 SuccessOrExit(err = MapPdmStatus(pdmStatus));
263 CHIP_ERROR K32WConfig::WriteConfigValue(Key key, uint64_t val)
266 PDM_teStatus pdmStatus;
268 VerifyOrExit(ValidConfigKey(key), err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND); // Verify key id.
269 pdmStatus = PDM_eSaveRecordData((uint16_t) key, &val, sizeof(uint64_t));
270 SuccessOrExit(err = MapPdmStatus(pdmStatus));
276 CHIP_ERROR K32WConfig::WriteConfigValueStr(Key key, const char * str)
278 return WriteConfigValueStr(key, str, (str != NULL) ? strlen(str) : 0);
281 CHIP_ERROR K32WConfig::WriteConfigValueStr(Key key, const char * str, size_t strLen)
284 PDM_teStatus pdmStatus;
286 VerifyOrExit(ValidConfigKey(key), err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND); // Verify key id.
290 pdmStatus = PDM_eSaveRecordData((uint16_t) key, (void *) str, strLen);
291 SuccessOrExit(err = MapPdmStatus(pdmStatus));
295 err = ClearConfigValue(key);
303 CHIP_ERROR K32WConfig::WriteConfigValueBin(Key key, const uint8_t * data, size_t dataLen)
306 PDM_teStatus pdmStatus;
308 VerifyOrExit(ValidConfigKey(key), err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND); // Verify key id.
310 if ((data != NULL) && (dataLen > 0))
312 pdmStatus = PDM_eSaveRecordData((uint16_t) key, (void *) data, dataLen);
313 SuccessOrExit(err = MapPdmStatus(pdmStatus));
317 err = ClearConfigValue(key);
325 CHIP_ERROR K32WConfig::WriteConfigValueCounter(uint8_t counterIdx, uint32_t val)
328 PDM_teStatus pdmStatus;
330 Key key = kMinConfigKey_ChipCounter + counterIdx;
331 VerifyOrExit(ValidConfigKey(key), err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND); // Verify key id.
332 pdmStatus = PDM_eSaveRecordData((uint16_t) key, &val, sizeof(uint32_t));
333 SuccessOrExit(err = MapPdmStatus(pdmStatus));
339 CHIP_ERROR K32WConfig::ClearConfigValue(Key key)
341 CHIP_ERROR err = CHIP_NO_ERROR;
343 VerifyOrExit(ValidConfigKey(key), err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND); // Verify key id.
344 PDM_vDeleteDataRecord((uint16_t) key);
352 bool K32WConfig::ConfigValueExists(Key key)
357 VerifyOrExit(ValidConfigKey(key), err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND); // Verify key id.
358 VerifyOrExit(PDM_bDoesDataExist((uint16_t) key, &size), err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND);
361 // Return true if the record was found.
362 return (err == CHIP_NO_ERROR);
365 CHIP_ERROR K32WConfig::FactoryResetConfig(void)
369 // Iterate over all the CHIP Config PDM ID records and delete each one
370 err = ForEachRecord(kMinConfigKey_ChipConfig, kMaxConfigKey_ChipConfig, false,
371 [](const Key & pdmKey, const size_t & length) -> CHIP_ERROR {
374 err2 = ClearConfigValue(pdmKey);
381 // Return success at end of iterations.
382 if (err == CHIP_END_OF_INPUT)
390 CHIP_ERROR K32WConfig::MapPdmStatus(PDM_teStatus pdmStatus)
396 case PDM_E_STATUS_OK:
400 err = CHIP_CONFIG_ERROR_MIN + pdmStatus;
407 CHIP_ERROR K32WConfig::MapPdmInitStatus(int pdmStatus)
409 return (pdmStatus == 0) ? CHIP_NO_ERROR : CHIP_CONFIG_ERROR_MIN + pdmStatus;
412 bool K32WConfig::ValidConfigKey(Key key)
414 // Returns true if the key is in the valid CHIP Config PDM key range.
416 if ((key >= kMinConfigKey_ChipFactory) && (key <= kMaxConfigKey_ChipCounter))
424 CHIP_ERROR K32WConfig::ForEachRecord(Key firstKey, Key lastKey, bool addNewRecord, ForEachRecordFunct funct)
426 CHIP_ERROR err = CHIP_NO_ERROR;
428 for (Key pdmKey = firstKey; pdmKey <= lastKey; pdmKey++)
432 if (PDM_bDoesDataExist((uint16_t) pdmKey, &dataLen))
436 // Invoke the caller's function
437 // (for retrieve,store,delete,enumerate GroupKey operations).
438 err = funct(pdmKey, dataLen);
445 // Invoke caller's function
446 // (for add GroupKey operation).
447 err = funct(pdmKey, dataLen);
458 } // namespace Internal
459 } // namespace DeviceLayer