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