Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / qpg6100 / qpg6100Config.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  *          platforms based on the Qorvo QPG6100 SDK.
22  */
23
24 #pragma once
25
26 #include <platform/internal/CHIPDeviceLayerInternal.h>
27
28 #include "FreeRTOS.h"
29 #include "qvCHIP.h"
30
31 #include <functional>
32
33 namespace chip {
34 namespace DeviceLayer {
35 namespace Internal {
36
37 /* Base for the category calculation when determining the key IDs */
38 #define CATEGORY_BASE 0x01
39
40 constexpr inline uint16_t QorvoConfigKey(uint8_t categoryId, uint8_t id)
41 {
42     return static_cast<uint16_t>(((categoryId) << 6) | (id & 0x3F));
43 }
44
45 /**
46  * This implementation uses the Qorvo NVM component as the underlying storage layer.
47  *
48  * NOTE: This class is designed to be mixed-in to the concrete subclass of the
49  * GenericConfigurationManagerImpl<> template.  When used this way, the class
50  * naturally provides implementations for the delegated members referenced by
51  * the template class (e.g. the ReadConfigValue() method).
52  */
53 class QPG6100Config
54 {
55 public:
56     // Category ids used by the CHIP Device Layer
57     static constexpr uint8_t kFileId_ChipFactory = CATEGORY_BASE;    /**< Category containing persistent config values set at
58                                                                       * manufacturing    time. Retained during factory reset. */
59     static constexpr uint8_t kFileId_ChipConfig = CATEGORY_BASE + 1; /**< Catyegory containing dynamic config values set at runtime.
60                                                                       *   Cleared during factory reset. */
61     static constexpr uint8_t kFileId_ChipCounter = CATEGORY_BASE + 2; /**< Category containing dynamic counter values set at
62                                                                        * runtime. Retained during factory reset. */
63
64     using Key = uint16_t;
65
66     // Key definitions for well-known configuration values.
67     static constexpr Key kConfigKey_SerialNum           = QorvoConfigKey(kFileId_ChipFactory, 0x00);
68     static constexpr Key kConfigKey_MfrDeviceId         = QorvoConfigKey(kFileId_ChipFactory, 0x01);
69     static constexpr Key kConfigKey_MfrDeviceCert       = QorvoConfigKey(kFileId_ChipFactory, 0x02);
70     static constexpr Key kConfigKey_MfrDevicePrivateKey = QorvoConfigKey(kFileId_ChipFactory, 0x03);
71     static constexpr Key kConfigKey_ManufacturingDate   = QorvoConfigKey(kFileId_ChipFactory, 0x04);
72     static constexpr Key kConfigKey_SetupPinCode        = QorvoConfigKey(kFileId_ChipFactory, 0x05);
73     static constexpr Key kConfigKey_MfrDeviceICACerts   = QorvoConfigKey(kFileId_ChipFactory, 0x06);
74     static constexpr Key kConfigKey_SetupDiscriminator  = QorvoConfigKey(kFileId_ChipFactory, 0x07);
75
76     static constexpr Key kConfigKey_FabricId                    = QorvoConfigKey(kFileId_ChipConfig, 0x00);
77     static constexpr Key kConfigKey_ServiceConfig               = QorvoConfigKey(kFileId_ChipConfig, 0x01);
78     static constexpr Key kConfigKey_PairedAccountId             = QorvoConfigKey(kFileId_ChipConfig, 0x02);
79     static constexpr Key kConfigKey_ServiceId                   = QorvoConfigKey(kFileId_ChipConfig, 0x03);
80     static constexpr Key kConfigKey_FabricSecret                = QorvoConfigKey(kFileId_ChipConfig, 0x04);
81     static constexpr Key kConfigKey_LastUsedEpochKeyId          = QorvoConfigKey(kFileId_ChipConfig, 0x05);
82     static constexpr Key kConfigKey_FailSafeArmed               = QorvoConfigKey(kFileId_ChipConfig, 0x06);
83     static constexpr Key kConfigKey_GroupKey                    = QorvoConfigKey(kFileId_ChipConfig, 0x07);
84     static constexpr Key kConfigKey_ProductRevision             = QorvoConfigKey(kFileId_ChipConfig, 0x08);
85     static constexpr Key kConfigKey_OperationalDeviceId         = QorvoConfigKey(kFileId_ChipConfig, 0x09);
86     static constexpr Key kConfigKey_OperationalDeviceCert       = QorvoConfigKey(kFileId_ChipConfig, 0x0A);
87     static constexpr Key kConfigKey_OperationalDeviceICACerts   = QorvoConfigKey(kFileId_ChipConfig, 0x0B);
88     static constexpr Key kConfigKey_OperationalDevicePrivateKey = QorvoConfigKey(kFileId_ChipConfig, 0x0C);
89
90     static constexpr Key kConfigKey_GroupKeyBase = QorvoConfigKey(kFileId_ChipConfig, 0x0D);
91     static constexpr Key kConfigKey_GroupKeyMax  = QorvoConfigKey(kFileId_ChipConfig, 0x1C); // Allows 16 Group Keys to be created.
92
93     static constexpr Key kConfigKey_CounterKeyBase = QorvoConfigKey(kFileId_ChipCounter, 0x00);
94     static constexpr Key kConfigKey_CounterKeyMax =
95         QorvoConfigKey(kFileId_ChipCounter, 0x1F); // Allows 32 Counter Keys to be created.
96
97     // Set key id limits for each group.
98     static constexpr Key kMinConfigKey_ChipFactory = kConfigKey_SerialNum;
99     static constexpr Key kMaxConfigKey_ChipFactory = kConfigKey_SetupDiscriminator;
100     static constexpr Key kMinConfigKey_ChipConfig  = kConfigKey_FabricId;
101     static constexpr Key kMaxConfigKey_ChipConfig  = kConfigKey_GroupKeyMax;
102     static constexpr Key kMinConfigKey_ChipCounter = kConfigKey_CounterKeyBase;
103     static constexpr Key kMaxConfigKey_ChipCounter = kConfigKey_CounterKeyMax; // Allows 32 Counters to be created.
104
105     static CHIP_ERROR Init(void);
106
107     // Configuration methods used by the GenericConfigurationManagerImpl<> template.
108     static uint16_t GetSettingsMaxValueLength(uint16_t Key);
109     static CHIP_ERROR ReadConfigValue(Key key, bool & val);
110     static CHIP_ERROR ReadConfigValue(Key key, uint32_t & val);
111     static CHIP_ERROR ReadConfigValue(Key key, uint64_t & val);
112     static CHIP_ERROR ReadConfigValueStr(Key key, char * buf, size_t bufSize, size_t & outLen);
113     static CHIP_ERROR ReadConfigValueBin(Key key, uint8_t * buf, size_t bufSize, size_t & outLen);
114     static CHIP_ERROR WriteConfigValue(Key key, bool val);
115     static CHIP_ERROR WriteConfigValue(Key key, uint32_t val);
116     static CHIP_ERROR WriteConfigValue(Key key, uint64_t val);
117     static CHIP_ERROR WriteConfigValueStr(Key key, const char * str);
118     static CHIP_ERROR WriteConfigValueStr(Key key, const char * str, size_t strLen);
119     static CHIP_ERROR WriteConfigValueBin(Key key, const uint8_t * data, size_t dataLen);
120     static CHIP_ERROR ClearConfigValue(Key key);
121     static bool ConfigValueExists(Key key);
122     static CHIP_ERROR FactoryResetConfig(void);
123
124     static void RunConfigUnitTest(void);
125
126 protected:
127     using ForEachRecordFunct = std::function<CHIP_ERROR(const Key & key, const size_t & length)>;
128     static CHIP_ERROR ForEachRecord(uint16_t fileId, uint16_t recordKey, bool addNewRecord, ForEachRecordFunct funct);
129     static CHIP_ERROR MapNVMError(qvStatus_t status);
130
131 private:
132 };
133
134 } // namespace Internal
135 } // namespace DeviceLayer
136 } // namespace chip