3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2019-2020 Google LLC.
5 * Copyright (c) 2018 Nest Labs, Inc.
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
23 * Utilities for interacting with the the ESP32 "NVS" key-value store.
28 #include <platform/internal/CHIPDeviceLayerInternal.h>
33 namespace DeviceLayer {
37 * Provides functions and definitions for accessing device configuration information on the ESP32.
39 * This class is designed to be mixed-in to concrete implementation classes as a means to
40 * provide access to configuration information to generic base classes.
47 // Maximum length of an NVS key name, as specified in the ESP-IDF documentation.
48 static constexpr size_t kMaxConfigKeyNameLength = 15;
50 // NVS namespaces used to store device configuration information.
51 static const char kConfigNamespace_ChipFactory[];
52 static const char kConfigNamespace_ChipConfig[];
53 static const char kConfigNamespace_ChipCounters[];
55 // Key definitions for well-known keys.
56 static const Key kConfigKey_SerialNum;
57 static const Key kConfigKey_MfrDeviceId;
58 static const Key kConfigKey_MfrDeviceCert;
59 static const Key kConfigKey_MfrDeviceICACerts;
60 static const Key kConfigKey_MfrDevicePrivateKey;
61 static const Key kConfigKey_ProductRevision;
62 static const Key kConfigKey_ManufacturingDate;
63 static const Key kConfigKey_SetupPinCode;
64 static const Key kConfigKey_FabricId;
65 static const Key kConfigKey_ServiceConfig;
66 static const Key kConfigKey_PairedAccountId;
67 static const Key kConfigKey_ServiceId;
68 static const Key kConfigKey_FabricSecret;
69 static const Key kConfigKey_GroupKeyIndex;
70 static const Key kConfigKey_LastUsedEpochKeyId;
71 static const Key kConfigKey_FailSafeArmed;
72 static const Key kConfigKey_WiFiStationSecType;
73 static const Key kConfigKey_OperationalDeviceId;
74 static const Key kConfigKey_OperationalDeviceCert;
75 static const Key kConfigKey_OperationalDeviceICACerts;
76 static const Key kConfigKey_OperationalDevicePrivateKey;
77 static const Key kConfigKey_SetupDiscriminator;
79 static const char kGroupKeyNamePrefix[];
81 // Config value accessors.
82 static CHIP_ERROR ReadConfigValue(Key key, bool & val);
83 static CHIP_ERROR ReadConfigValue(Key key, uint32_t & val);
84 static CHIP_ERROR ReadConfigValue(Key key, uint64_t & val);
85 static CHIP_ERROR ReadConfigValueStr(Key key, char * buf, size_t bufSize, size_t & outLen);
86 static CHIP_ERROR ReadConfigValueBin(Key key, uint8_t * buf, size_t bufSize, size_t & outLen);
87 static CHIP_ERROR WriteConfigValue(Key key, bool val);
88 static CHIP_ERROR WriteConfigValue(Key key, uint32_t val);
89 static CHIP_ERROR WriteConfigValue(Key key, uint64_t val);
90 static CHIP_ERROR WriteConfigValueStr(Key key, const char * str);
91 static CHIP_ERROR WriteConfigValueStr(Key key, const char * str, size_t strLen);
92 static CHIP_ERROR WriteConfigValueBin(Key key, const uint8_t * data, size_t dataLen);
93 static CHIP_ERROR ClearConfigValue(Key key);
94 static bool ConfigValueExists(Key key);
96 // NVS Namespace helper functions.
97 static CHIP_ERROR EnsureNamespace(const char * ns);
98 static CHIP_ERROR ClearNamespace(const char * ns);
100 static void RunConfigUnitTest(void);
103 struct ESP32Config::Key
105 const char * Namespace;
108 bool operator==(const Key & other) const;
111 inline bool ESP32Config::Key::operator==(const Key & other) const
113 return strcmp(Namespace, other.Namespace) == 0 && strcmp(Name, other.Name) == 0;
116 } // namespace Internal
117 } // namespace DeviceLayer