Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / Linux / PosixConfig.h
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 /**
19  *    @file
20  *          Utilities for accessing persisted device configuration on
21  *          Linux platforms.
22  */
23
24 #pragma once
25
26 #include <functional>
27 #include <inttypes.h>
28
29 #include <platform/internal/CHIPDeviceLayerInternal.h>
30
31 namespace chip {
32 namespace DeviceLayer {
33 namespace Internal {
34
35 class ChipLinuxStorage;
36
37 /**
38  * Provides functions and definitions for accessing device configuration information on the Posix.
39  *
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.
42  */
43 class PosixConfig
44 {
45 public:
46     struct Key;
47
48     // Maximum length of an NVS key name.
49     static constexpr size_t kMaxConfigKeyNameLength = 15;
50
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[];
55
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;
79
80     static const char kGroupKeyNamePrefix[];
81
82     static CHIP_ERROR Init();
83
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();
99
100     static void RunConfigUnitTest();
101
102 protected:
103     // NVS Namespace helper functions.
104     static CHIP_ERROR EnsureNamespace(const char * ns);
105     static CHIP_ERROR ClearNamespace(const char * ns);
106
107 private:
108     static ChipLinuxStorage * GetStorageForNamespace(Key key);
109 };
110
111 struct PosixConfig::Key
112 {
113     const char * Namespace;
114     const char * Name;
115
116     bool operator==(const Key & other) const;
117 };
118
119 inline bool PosixConfig::Key::operator==(const Key & other) const
120 {
121     return strcmp(Namespace, other.Namespace) == 0 && strcmp(Name, other.Name) == 0;
122 }
123
124 } // namespace Internal
125 } // namespace DeviceLayer
126 } // namespace chip