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
29 #include <platform/internal/CHIPDeviceLayerInternal.h>
32 namespace DeviceLayer {
36 * Provides functions and definitions for accessing device configuration information on the Posix.
38 * This class is designed to be mixed-in to concrete implementation classes as a means to
39 * provide access to configuration information to generic base classes.
46 // Maximum length of an NVS key name.
47 static constexpr size_t kMaxConfigKeyNameLength = 15;
49 // NVS namespaces used to store device configuration information.
50 static const char kConfigNamespace_ChipFactory[];
51 static const char kConfigNamespace_ChipConfig[];
52 static const char kConfigNamespace_ChipCounters[];
54 // Key definitions for well-known keys.
55 static const Key kConfigKey_SerialNum;
56 static const Key kConfigKey_MfrDeviceId;
57 static const Key kConfigKey_MfrDeviceCert;
58 static const Key kConfigKey_MfrDeviceICACerts;
59 static const Key kConfigKey_MfrDevicePrivateKey;
60 static const Key kConfigKey_ProductRevision;
61 static const Key kConfigKey_ManufacturingDate;
62 static const Key kConfigKey_SetupPinCode;
63 static const Key kConfigKey_FabricId;
64 static const Key kConfigKey_ServiceConfig;
65 static const Key kConfigKey_PairedAccountId;
66 static const Key kConfigKey_ServiceId;
67 static const Key kConfigKey_FabricSecret;
68 static const Key kConfigKey_GroupKeyIndex;
69 static const Key kConfigKey_LastUsedEpochKeyId;
70 static const Key kConfigKey_FailSafeArmed;
71 static const Key kConfigKey_WiFiStationSecType;
72 static const Key kConfigKey_OperationalDeviceId;
73 static const Key kConfigKey_OperationalDeviceCert;
74 static const Key kConfigKey_OperationalDeviceICACerts;
75 static const Key kConfigKey_OperationalDevicePrivateKey;
76 static const Key kConfigKey_SetupDiscriminator;
78 static const char kGroupKeyNamePrefix[];
80 static CHIP_ERROR Init(void);
82 // Config value accessors.
83 static CHIP_ERROR ReadConfigValue(Key key, bool & val);
84 static CHIP_ERROR ReadConfigValue(Key key, uint32_t & val);
85 static CHIP_ERROR ReadConfigValue(Key key, uint64_t & val);
86 static CHIP_ERROR ReadConfigValueStr(Key key, char * buf, size_t bufSize, size_t & outLen);
87 static CHIP_ERROR ReadConfigValueBin(Key key, uint8_t * buf, size_t bufSize, size_t & outLen);
88 static CHIP_ERROR WriteConfigValue(Key key, bool val);
89 static CHIP_ERROR WriteConfigValue(Key key, uint32_t val);
90 static CHIP_ERROR WriteConfigValue(Key key, uint64_t val);
91 static CHIP_ERROR WriteConfigValueStr(Key key, const char * str);
92 static CHIP_ERROR WriteConfigValueStr(Key key, const char * str, size_t strLen);
93 static CHIP_ERROR WriteConfigValueBin(Key key, const uint8_t * data, size_t dataLen);
94 static CHIP_ERROR ClearConfigValue(Key key);
95 static bool ConfigValueExists(Key key);
96 static CHIP_ERROR FactoryResetConfig(void);
98 static void RunConfigUnitTest(void);
101 // NVS Namespace helper functions.
102 static CHIP_ERROR EnsureNamespace(const char * ns);
103 static CHIP_ERROR ClearNamespace(const char * ns);
106 struct PosixConfig::Key
108 const char * Namespace;
111 bool operator==(const Key & other) const;
114 inline bool PosixConfig::Key::operator==(const Key & other) const
116 return strcmp(Namespace, other.Namespace) == 0 && strcmp(Name, other.Name) == 0;
119 } // namespace Internal
120 } // namespace DeviceLayer