Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / Darwin / 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  *          Darwin 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 /**
36  * Provides functions and definitions for accessing device configuration information on the Posix.
37  *
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.
40  */
41 class PosixConfig
42 {
43 public:
44     struct Key;
45
46     // Maximum length of an NVS key name.
47     static constexpr size_t kMaxConfigKeyNameLength = 15;
48
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[];
53
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;
77
78     static const char kGroupKeyNamePrefix[];
79
80     static CHIP_ERROR Init(void);
81
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);
97
98     static void RunConfigUnitTest(void);
99
100 protected:
101     // NVS Namespace helper functions.
102     static CHIP_ERROR EnsureNamespace(const char * ns);
103     static CHIP_ERROR ClearNamespace(const char * ns);
104 };
105
106 struct PosixConfig::Key
107 {
108     const char * Namespace;
109     const char * Name;
110
111     bool operator==(const Key & other) const;
112 };
113
114 inline bool PosixConfig::Key::operator==(const Key & other) const
115 {
116     return strcmp(Namespace, other.Namespace) == 0 && strcmp(Name, other.Name) == 0;
117 }
118
119 } // namespace Internal
120 } // namespace DeviceLayer
121 } // namespace chip