3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2018 Nest Labs, Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 * Utilities for accessing persisted device configuration on
22 * platforms based on the NXP K32W SDK.
29 #include <platform/internal/CHIPDeviceLayerInternal.h>
31 #include "MemManager.h"
35 namespace DeviceLayer {
38 constexpr inline uint16_t K32WConfigKey(uint8_t chipId, uint8_t pdmId)
40 return static_cast<uint16_t>(chipId) << 8 | pdmId;
45 * This implementation uses the NXP Persistent Data Manager (PDM) library as the
46 * underlying storage layer.
48 * NOTE: This class is designed to be mixed-in to the concrete subclass of the
49 * GenericConfigurationManagerImpl<> template. When used this way, the class
50 * naturally provides implementations for the delegated members referenced by
51 * the template class (e.g. the ReadConfigValue() method).
56 // PDM ids used by the CHIP Device Layer
57 static constexpr uint8_t kPDMId_ChipFactory = 0x01; /**< PDM id for settings containing persistent config values set at
58 * manufacturing time. Retained during factory reset. */
59 static constexpr uint8_t kPDMId_ChipConfig = 0x02; /**< PDM id for settings containing dynamic config values set at runtime.
60 * Cleared during factory reset. */
61 static constexpr uint8_t kPDMId_ChipCounter = 0x03; /**< PDM id for settings containing dynamic counter values set at runtime.
62 * Retained during factory reset. */
66 // Key definitions for well-known configuration values.
67 // Factory config keys
68 static constexpr Key kConfigKey_SerialNum = K32WConfigKey(kPDMId_ChipFactory, 0x00);
69 static constexpr Key kConfigKey_MfrDeviceId = K32WConfigKey(kPDMId_ChipFactory, 0x01);
70 static constexpr Key kConfigKey_MfrDeviceCert = K32WConfigKey(kPDMId_ChipFactory, 0x02);
71 static constexpr Key kConfigKey_MfrDevicePrivateKey = K32WConfigKey(kPDMId_ChipFactory, 0x03);
72 static constexpr Key kConfigKey_ManufacturingDate = K32WConfigKey(kPDMId_ChipFactory, 0x04);
73 static constexpr Key kConfigKey_SetupPinCode = K32WConfigKey(kPDMId_ChipFactory, 0x05);
74 static constexpr Key kConfigKey_MfrDeviceICACerts = K32WConfigKey(kPDMId_ChipFactory, 0x06);
75 static constexpr Key kConfigKey_ProductRevision = K32WConfigKey(kPDMId_ChipFactory, 0x07);
76 static constexpr Key kConfigKey_SetupDiscriminator = K32WConfigKey(kPDMId_ChipFactory, 0x08);
78 static constexpr Key kConfigKey_FabricId = K32WConfigKey(kPDMId_ChipConfig, 0x00);
79 static constexpr Key kConfigKey_ServiceConfig = K32WConfigKey(kPDMId_ChipConfig, 0x01);
80 static constexpr Key kConfigKey_PairedAccountId = K32WConfigKey(kPDMId_ChipConfig, 0x02);
81 static constexpr Key kConfigKey_ServiceId = K32WConfigKey(kPDMId_ChipConfig, 0x03);
82 static constexpr Key kConfigKey_FabricSecret = K32WConfigKey(kPDMId_ChipConfig, 0x04);
83 static constexpr Key kConfigKey_LastUsedEpochKeyId = K32WConfigKey(kPDMId_ChipConfig, 0x05);
84 static constexpr Key kConfigKey_FailSafeArmed = K32WConfigKey(kPDMId_ChipConfig, 0x06);
86 static constexpr Key kConfigKey_OperationalDeviceId = K32WConfigKey(kPDMId_ChipConfig, 0x07);
87 static constexpr Key kConfigKey_OperationalDeviceCert = K32WConfigKey(kPDMId_ChipConfig, 0x08);
88 static constexpr Key kConfigKey_OperationalDeviceICACerts = K32WConfigKey(kPDMId_ChipConfig, 0x09);
89 static constexpr Key kConfigKey_OperationalDevicePrivateKey = K32WConfigKey(kPDMId_ChipConfig, 0x0A);
91 static constexpr Key kConfigKey_GroupKey = K32WConfigKey(kPDMId_ChipConfig, 0x0B);
92 static constexpr Key kConfigKey_GroupKey0 = K32WConfigKey(kPDMId_ChipConfig, 0x0C);
93 static constexpr Key kConfigKey_GroupKey1 = K32WConfigKey(kPDMId_ChipConfig, 0x0D);
94 static constexpr Key kConfigKey_GroupKey2 = K32WConfigKey(kPDMId_ChipConfig, 0x0E);
95 static constexpr Key kConfigKey_GroupKey3 = K32WConfigKey(kPDMId_ChipConfig, 0x0F);
96 static constexpr Key kConfigKey_GroupKey4 = K32WConfigKey(kPDMId_ChipConfig, 0x10);
97 static constexpr Key kConfigKey_GroupKey5 = K32WConfigKey(kPDMId_ChipConfig, 0x11);
98 static constexpr Key kConfigKey_GroupKey6 = K32WConfigKey(kPDMId_ChipConfig, 0x12);
99 static constexpr Key kConfigKey_GroupKey7 = K32WConfigKey(kPDMId_ChipConfig, 0x13);
100 static constexpr Key kConfigKey_GroupKey8 = K32WConfigKey(kPDMId_ChipConfig, 0x14);
101 static constexpr Key kConfigKey_GroupKey9 = K32WConfigKey(kPDMId_ChipConfig, 0x15);
102 static constexpr Key kConfigKey_GroupKey10 = K32WConfigKey(kPDMId_ChipConfig, 0x16);
103 static constexpr Key kConfigKey_GroupKey11 = K32WConfigKey(kPDMId_ChipConfig, 0x17);
104 static constexpr Key kConfigKey_GroupKey12 = K32WConfigKey(kPDMId_ChipConfig, 0x18);
105 static constexpr Key kConfigKey_GroupKey13 = K32WConfigKey(kPDMId_ChipConfig, 0x19);
106 static constexpr Key kConfigKey_GroupKey14 = K32WConfigKey(kPDMId_ChipConfig, 0x1A);
107 static constexpr Key kConfigKey_GroupKey15 = K32WConfigKey(kPDMId_ChipConfig, 0x1B);
109 static constexpr Key kConfigKey_GroupKeyBase = kConfigKey_GroupKey0;
110 static constexpr Key kConfigKey_GroupKeyMax = K32WConfigKey(kPDMId_ChipConfig, 0x1B);
111 ; // Allows 16 Group Keys to be created.
113 // Set key id limits for each group.
114 static constexpr Key kMinConfigKey_ChipFactory = K32WConfigKey(kPDMId_ChipFactory, 0x00);
115 static constexpr Key kMaxConfigKey_ChipFactory = K32WConfigKey(kPDMId_ChipFactory, 0x08);
116 static constexpr Key kMinConfigKey_ChipConfig = K32WConfigKey(kPDMId_ChipConfig, 0x00);
117 static constexpr Key kMaxConfigKey_ChipConfig = K32WConfigKey(kPDMId_ChipConfig, 0x1A);
118 static constexpr Key kMinConfigKey_ChipCounter = K32WConfigKey(kPDMId_ChipCounter, 0x00);
119 static constexpr Key kMaxConfigKey_ChipCounter = K32WConfigKey(kPDMId_ChipCounter, 0x1F); // Allows 32 Counters to be created.
121 static CHIP_ERROR Init(void);
123 // Configuration methods used by the GenericConfigurationManagerImpl<> template.
124 static CHIP_ERROR ReadConfigValue(Key key, bool & val);
125 static CHIP_ERROR ReadConfigValue(Key key, uint32_t & val);
126 static CHIP_ERROR ReadConfigValue(Key key, uint64_t & val);
127 static CHIP_ERROR ReadConfigValueStr(Key key, char * buf, size_t bufSize, size_t & outLen);
128 static CHIP_ERROR ReadConfigValueBin(Key key, uint8_t * buf, size_t bufSize, size_t & outLen);
129 static CHIP_ERROR ReadConfigValueCounter(uint8_t counterIdx, uint32_t & val);
130 static CHIP_ERROR WriteConfigValue(Key key, bool val);
131 static CHIP_ERROR WriteConfigValue(Key key, uint32_t val);
132 static CHIP_ERROR WriteConfigValue(Key key, uint64_t val);
133 static CHIP_ERROR WriteConfigValueStr(Key key, const char * str);
134 static CHIP_ERROR WriteConfigValueStr(Key key, const char * str, size_t strLen);
135 static CHIP_ERROR WriteConfigValueBin(Key key, const uint8_t * data, size_t dataLen);
136 static CHIP_ERROR WriteConfigValueCounter(uint8_t counterIdx, uint32_t val);
137 static CHIP_ERROR ClearConfigValue(Key key);
138 static bool ConfigValueExists(Key key);
139 static CHIP_ERROR FactoryResetConfig(void);
140 static bool ValidConfigKey(Key key);
142 static void RunConfigUnitTest(void);
145 using ForEachRecordFunct = std::function<CHIP_ERROR(const Key & PDMIdKey, const size_t & length)>;
146 static CHIP_ERROR ForEachRecord(Key firstKey, Key lastKey, bool addNewRecord, ForEachRecordFunct funct);
147 static constexpr uint8_t GetPDMId(uint32_t key);
148 static constexpr uint8_t GetRecordKey(uint32_t key);
151 static CHIP_ERROR MapPdmStatus(PDM_teStatus pdmStatus);
152 static CHIP_ERROR MapPdmInitStatus(int pdmStatus);
156 * Extract a PDM id from a Key value.
158 inline constexpr uint8_t K32WConfig::GetPDMId(Key key)
160 return static_cast<uint8_t>(key >> 8);
164 * Extract an NVM record key from a Key value.
166 inline constexpr uint8_t K32WConfig::GetRecordKey(Key key)
168 return static_cast<uint8_t>(key);
171 } // namespace Internal
172 } // namespace DeviceLayer