Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / EFR32 / EFR32Config.h
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2019 Nest Labs, Inc.
5  *
6  *    Licensed under the Apache License, Version 2.0 (the "License");
7  *    you may not use this file except in compliance with the License.
8  *    You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *    Unless required by applicable law or agreed to in writing, software
13  *    distributed under the License is distributed on an "AS IS" BASIS,
14  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *    See the License for the specific language governing permissions and
16  *    limitations under the License.
17  */
18
19 /**
20  *    @file
21  *          Utilities for accessing persisted device configuration on
22  *          platforms based on the Silicon Labs SDK.
23  */
24
25 #pragma once
26
27 #include <functional>
28
29 #include <platform/internal/CHIPDeviceLayerInternal.h>
30
31 #include "nvm3.h"
32 #include "nvm3_hal_flash.h"
33
34 namespace chip {
35 namespace DeviceLayer {
36 namespace Internal {
37
38 /**
39  *
40  * This implementation uses the Silicon Labs EFR32 NVM3 flash data storage library
41  * as the underlying storage layer.
42  *
43  * NOTE: This class is designed to be mixed-in to the concrete subclass of the
44  * GenericConfigurationManagerImpl<> template.  When used this way, the class
45  * naturally provides implementations for the delegated members referenced by
46  * the template class (e.g. the ReadConfigValue() method).
47  */
48
49 // Silabs NVM3 objects use a 20-bit number, however User key range is
50 // restricted to 16 bits i.e. 0x0000 -> 0xFFFF.
51 // e.g. key = 0xA201
52 // 'A2' = the nv group base offest (Factory, Config or Counter)
53 // '01' = the id offset inside the group.
54 constexpr inline uint32_t EFR32ConfigKey(uint8_t keyBaseOffset, uint8_t id)
55 {
56     return static_cast<uint32_t>(keyBaseOffset) << 8 | id;
57 }
58
59 class EFR32Config
60 {
61 public:
62 public:
63     // Definitions for Silicon Labs EFR32 NVM3 driver:-
64
65     using Key = uint32_t;
66
67     // NVM3 key base offsets used by the CHIP Device Layer.
68     static constexpr uint8_t kChipFactory_KeyBase =
69         0xA2; // Persistent config values set at manufacturing time. Retained during factory reset.
70     static constexpr uint8_t kChipConfig_KeyBase = 0xA3; // Persistent config values set at runtime. Cleared during factory reset.
71     static constexpr uint8_t kChipCounter_KeyBase =
72         0xA4; // Persistent counter values set at runtime. Retained during factory reset.
73
74     // Key definitions for well-known configuration values.
75     // Factory config keys
76     static constexpr Key kConfigKey_SerialNum           = EFR32ConfigKey(kChipFactory_KeyBase, 0x00);
77     static constexpr Key kConfigKey_MfrDeviceId         = EFR32ConfigKey(kChipFactory_KeyBase, 0x01);
78     static constexpr Key kConfigKey_MfrDeviceCert       = EFR32ConfigKey(kChipFactory_KeyBase, 0x02);
79     static constexpr Key kConfigKey_MfrDevicePrivateKey = EFR32ConfigKey(kChipFactory_KeyBase, 0x03);
80     static constexpr Key kConfigKey_ManufacturingDate   = EFR32ConfigKey(kChipFactory_KeyBase, 0x04);
81     static constexpr Key kConfigKey_SetupPinCode        = EFR32ConfigKey(kChipFactory_KeyBase, 0x05);
82     static constexpr Key kConfigKey_MfrDeviceICACerts   = EFR32ConfigKey(kChipFactory_KeyBase, 0x06);
83     static constexpr Key kConfigKey_SetupDiscriminator  = EFR32ConfigKey(kChipFactory_KeyBase, 0x07);
84     // CHIP Config Keys
85     static constexpr Key kConfigKey_FabricId                    = EFR32ConfigKey(kChipConfig_KeyBase, 0x00);
86     static constexpr Key kConfigKey_ServiceConfig               = EFR32ConfigKey(kChipConfig_KeyBase, 0x01);
87     static constexpr Key kConfigKey_PairedAccountId             = EFR32ConfigKey(kChipConfig_KeyBase, 0x02);
88     static constexpr Key kConfigKey_ServiceId                   = EFR32ConfigKey(kChipConfig_KeyBase, 0x03);
89     static constexpr Key kConfigKey_FabricSecret                = EFR32ConfigKey(kChipConfig_KeyBase, 0x04);
90     static constexpr Key kConfigKey_LastUsedEpochKeyId          = EFR32ConfigKey(kChipConfig_KeyBase, 0x05);
91     static constexpr Key kConfigKey_FailSafeArmed               = EFR32ConfigKey(kChipConfig_KeyBase, 0x06);
92     static constexpr Key kConfigKey_GroupKey                    = EFR32ConfigKey(kChipConfig_KeyBase, 0x07);
93     static constexpr Key kConfigKey_ProductRevision             = EFR32ConfigKey(kChipConfig_KeyBase, 0x08);
94     static constexpr Key kConfigKey_OperationalDeviceId         = EFR32ConfigKey(kChipConfig_KeyBase, 0x09);
95     static constexpr Key kConfigKey_OperationalDeviceCert       = EFR32ConfigKey(kChipConfig_KeyBase, 0x0A);
96     static constexpr Key kConfigKey_OperationalDeviceICACerts   = EFR32ConfigKey(kChipConfig_KeyBase, 0x0B);
97     static constexpr Key kConfigKey_OperationalDevicePrivateKey = EFR32ConfigKey(kChipConfig_KeyBase, 0x0C);
98
99     static constexpr Key kConfigKey_GroupKeyBase = EFR32ConfigKey(kChipConfig_KeyBase, 0x0D);
100     static constexpr Key kConfigKey_GroupKeyMax  = EFR32ConfigKey(kChipConfig_KeyBase, 0x1C); // Allows 16 Group Keys to be created.
101
102     // Set key id limits for each group.
103     static constexpr Key kMinConfigKey_ChipFactory = EFR32ConfigKey(kChipFactory_KeyBase, 0x00);
104     static constexpr Key kMaxConfigKey_ChipFactory = EFR32ConfigKey(kChipFactory_KeyBase, 0x07);
105     static constexpr Key kMinConfigKey_ChipConfig  = EFR32ConfigKey(kChipConfig_KeyBase, 0x00);
106     static constexpr Key kMaxConfigKey_ChipConfig  = EFR32ConfigKey(kChipConfig_KeyBase, 0x1C);
107     static constexpr Key kMinConfigKey_ChipCounter = EFR32ConfigKey(kChipCounter_KeyBase, 0x00);
108     static constexpr Key kMaxConfigKey_ChipCounter =
109         EFR32ConfigKey(kChipCounter_KeyBase, 0x1F); // Allows 32 Counters to be created.
110
111     static CHIP_ERROR Init(void);
112
113     // Configuration methods used by the GenericConfigurationManagerImpl<> template.
114     static CHIP_ERROR ReadConfigValue(Key key, bool & val);
115     static CHIP_ERROR ReadConfigValue(Key key, uint32_t & val);
116     static CHIP_ERROR ReadConfigValue(Key key, uint64_t & val);
117     static CHIP_ERROR ReadConfigValueStr(Key key, char * buf, size_t bufSize, size_t & outLen);
118     static CHIP_ERROR ReadConfigValueBin(Key key, uint8_t * buf, size_t bufSize, size_t & outLen);
119     static CHIP_ERROR ReadConfigValueCounter(uint8_t counterIdx, uint32_t & val);
120     static CHIP_ERROR WriteConfigValue(Key key, bool val);
121     static CHIP_ERROR WriteConfigValue(Key key, uint32_t val);
122     static CHIP_ERROR WriteConfigValue(Key key, uint64_t val);
123     static CHIP_ERROR WriteConfigValueStr(Key key, const char * str);
124     static CHIP_ERROR WriteConfigValueStr(Key key, const char * str, size_t strLen);
125     static CHIP_ERROR WriteConfigValueBin(Key key, const uint8_t * data, size_t dataLen);
126     static CHIP_ERROR WriteConfigValueCounter(uint8_t counterIdx, uint32_t val);
127     static CHIP_ERROR ClearConfigValue(Key key);
128     static bool ConfigValueExists(Key key);
129     static CHIP_ERROR FactoryResetConfig(void);
130     static bool ValidConfigKey(Key key);
131
132     static void RunConfigUnitTest(void);
133     static void RepackNvm3Flash(void);
134
135 protected:
136     using ForEachRecordFunct = std::function<CHIP_ERROR(const Key & nvm3Key, const size_t & length)>;
137     static CHIP_ERROR ForEachRecord(Key firstKey, Key lastKey, bool addNewRecord, ForEachRecordFunct funct);
138
139 private:
140     static CHIP_ERROR MapNvm3Error(Ecode_t nvm3Res);
141     static void OnExit(void);
142 };
143
144 } // namespace Internal
145 } // namespace DeviceLayer
146 } // namespace chip