Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2020-2022 Project CHIP Authors
4 : * Copyright (c) 2019-2020 Google LLC.
5 : * Copyright (c) 2018 Nest Labs, Inc.
6 : *
7 : * Licensed under the Apache License, Version 2.0 (the "License");
8 : * you may not use this file except in compliance with the License.
9 : * You may obtain a copy of the License at
10 : *
11 : * http://www.apache.org/licenses/LICENSE-2.0
12 : *
13 : * Unless required by applicable law or agreed to in writing, software
14 : * distributed under the License is distributed on an "AS IS" BASIS,
15 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 : * See the License for the specific language governing permissions and
17 : * limitations under the License.
18 : */
19 :
20 : /**
21 : * @file
22 : * Provides an generic implementation of ConfigurationManager features
23 : * for use on various platforms.
24 : */
25 :
26 : #pragma once
27 :
28 : #include <lib/support/BitFlags.h>
29 : #include <platform/CHIPDeviceConfig.h>
30 : #include <platform/ConfigurationManager.h>
31 :
32 : #if CHIP_ENABLE_ROTATING_DEVICE_ID && defined(CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID)
33 : #include <lib/support/LifetimePersistedCounter.h>
34 : #endif
35 :
36 : namespace chip {
37 : namespace DeviceLayer {
38 :
39 : class ProvisioningDataSet;
40 :
41 : namespace Internal {
42 :
43 : template <class ConfigClass>
44 : class GenericDeviceInstanceInfoProvider;
45 :
46 : #if CHIP_USE_TRANSITIONAL_COMMISSIONABLE_DATA_PROVIDER
47 : template <class ConfigClass>
48 : class LegacyTemporaryCommissionableDataProvider;
49 : #endif // CHIP_USE_TRANSITIONAL_COMMISSIONABLE_DATA_PROVIDER
50 :
51 : /**
52 : * Provides a generic implementation of ConfigurationManager features that works on multiple platforms.
53 : *
54 : * This template contains implementations of select features from the ConfigurationManager abstract
55 : * interface that are suitable for use on all platforms. It is intended to be inherited (directly
56 : * or indirectly) by the ConfigurationManagerImpl class.
57 : */
58 : template <class ConfigClass>
59 : class GenericConfigurationManagerImpl : public ConfigurationManager
60 : {
61 : public:
62 : // ===== Methods that implement the ConfigurationManager abstract interface.
63 :
64 : CHIP_ERROR Init() override;
65 : CHIP_ERROR StoreHardwareVersion(uint16_t hardwareVer) override;
66 : CHIP_ERROR GetSoftwareVersionString(char * buf, size_t bufSize) override;
67 : CHIP_ERROR GetSoftwareVersion(uint32_t & softwareVer) override;
68 : CHIP_ERROR StoreSoftwareVersion(uint32_t softwareVer) override;
69 : CHIP_ERROR GetConfigurationVersion(uint32_t & configurationVer) override;
70 : CHIP_ERROR StoreConfigurationVersion(uint32_t softwareVer) override;
71 : CHIP_ERROR GetFirmwareBuildChipEpochTime(System::Clock::Seconds32 & buildTime) override;
72 : CHIP_ERROR SetFirmwareBuildChipEpochTime(System::Clock::Seconds32 buildTime) override;
73 : CHIP_ERROR StoreSerialNumber(const char * serialNum, size_t serialNumLen) override;
74 : CHIP_ERROR GetPrimaryMACAddress(MutableByteSpan & buf) override;
75 : CHIP_ERROR GetPrimaryWiFiMACAddress(uint8_t * buf) override;
76 : CHIP_ERROR GetPrimary802154MACAddress(uint8_t * buf) override;
77 : CHIP_ERROR StoreManufacturingDate(const char * mfgDate, size_t mfgDateLen) override;
78 : #if CHIP_ENABLE_ROTATING_DEVICE_ID && defined(CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID)
79 : CHIP_ERROR GetLifetimeCounter(uint16_t & lifetimeCounter) override;
80 : CHIP_ERROR IncrementLifetimeCounter() override;
81 : CHIP_ERROR SetRotatingDeviceIdUniqueId(const ByteSpan & uniqueIdSpan) override;
82 : CHIP_ERROR GetRotatingDeviceIdUniqueId(MutableByteSpan & uniqueIdSpan) override;
83 : #endif
84 : CHIP_ERROR GetFailSafeArmed(bool & val) override;
85 : CHIP_ERROR SetFailSafeArmed(bool val) override;
86 : CHIP_ERROR GetBLEDeviceIdentificationInfo(Ble::ChipBLEDeviceIdentificationInfo & deviceIdInfo) override;
87 : bool IsCommissionableDeviceTypeEnabled() override;
88 : CHIP_ERROR GetDeviceTypeId(uint32_t & deviceType) override;
89 : bool IsCommissionableDeviceNameEnabled() override;
90 : CHIP_ERROR GetCommissionableDeviceName(char * buf, size_t bufSize) override;
91 : CHIP_ERROR GetInitialPairingHint(uint16_t & pairingHint) override;
92 : CHIP_ERROR GetInitialPairingInstruction(char * buf, size_t bufSize) override;
93 : CHIP_ERROR GetSecondaryPairingHint(uint16_t & pairingHint) override;
94 : CHIP_ERROR GetSecondaryPairingInstruction(char * buf, size_t bufSize) override;
95 : CHIP_ERROR GetRegulatoryLocation(uint8_t & location) override;
96 : CHIP_ERROR StoreRegulatoryLocation(uint8_t location) override;
97 : CHIP_ERROR GetCountryCode(char * buf, size_t bufSize, size_t & codeLen) override;
98 : CHIP_ERROR StoreCountryCode(const char * code, size_t codeLen) override;
99 : CHIP_ERROR GetRebootCount(uint32_t & rebootCount) override;
100 : CHIP_ERROR StoreRebootCount(uint32_t rebootCount) override;
101 : CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours) override;
102 : CHIP_ERROR StoreTotalOperationalHours(uint32_t totalOperationalHours) override;
103 : CHIP_ERROR GetBootReason(uint32_t & bootReason) override;
104 : CHIP_ERROR StoreBootReason(uint32_t bootReason) override;
105 : CHIP_ERROR GetUniqueId(char * buf, size_t bufSize) override;
106 : CHIP_ERROR StoreUniqueId(const char * uniqueId, size_t uniqueIdLen) override;
107 : CHIP_ERROR GenerateUniqueId(char * buf, size_t bufSize) override;
108 : #if CHIP_CONFIG_TEST
109 : void RunUnitTests() override;
110 : #endif
111 : bool IsFullyProvisioned() override;
112 : void InitiateFactoryReset() override;
113 : #if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
114 : void NotifyOfAdvertisementStart() override;
115 : #else
116 : void NotifyOfAdvertisementStart();
117 : #endif
118 : void LogDeviceConfig() override;
119 :
120 53 : ~GenericConfigurationManagerImpl() override = default;
121 :
122 : protected:
123 : #if CHIP_ENABLE_ROTATING_DEVICE_ID && defined(CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID)
124 : chip::LifetimePersistedCounter<uint32_t> mLifetimePersistedCounter;
125 : uint8_t mRotatingDeviceIdUniqueId[CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID_LENGTH] =
126 : CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID;
127 : size_t mRotatingDeviceIdUniqueIdLength = kRotatingDeviceIDUniqueIDLength;
128 : #endif
129 :
130 : friend GenericDeviceInstanceInfoProvider<ConfigClass>;
131 :
132 : #if CHIP_USE_TRANSITIONAL_COMMISSIONABLE_DATA_PROVIDER
133 : friend LegacyTemporaryCommissionableDataProvider<ConfigClass>;
134 : #endif // CHIP_USE_TRANSITIONAL_COMMISSIONABLE_DATA_PROVIDER
135 :
136 : CHIP_ERROR PersistProvisioningData(ProvisioningDataSet & provData);
137 :
138 : // Methods to read and write configuration values, as well as run the configuration unit test.
139 : typedef typename ConfigClass::Key Key;
140 : virtual CHIP_ERROR ReadConfigValue(Key key, bool & val) = 0;
141 : virtual CHIP_ERROR ReadConfigValue(Key key, uint32_t & val) = 0;
142 : virtual CHIP_ERROR ReadConfigValue(Key key, uint64_t & val) = 0;
143 : virtual CHIP_ERROR ReadConfigValueStr(Key key, char * buf, size_t bufSize, size_t & outLen) = 0;
144 : virtual CHIP_ERROR ReadConfigValueBin(Key key, uint8_t * buf, size_t bufSize, size_t & outLen) = 0;
145 : virtual CHIP_ERROR WriteConfigValue(Key key, bool val) = 0;
146 : virtual CHIP_ERROR WriteConfigValue(Key key, uint32_t val) = 0;
147 : virtual CHIP_ERROR WriteConfigValue(Key key, uint64_t val) = 0;
148 : virtual CHIP_ERROR WriteConfigValueStr(Key key, const char * str) = 0;
149 : virtual CHIP_ERROR WriteConfigValueStr(Key key, const char * str, size_t strLen) = 0;
150 : virtual CHIP_ERROR WriteConfigValueBin(Key key, const uint8_t * data, size_t dataLen) = 0;
151 : virtual void RunConfigUnitTest(void) = 0;
152 : };
153 :
154 : } // namespace Internal
155 : } // namespace DeviceLayer
156 : } // namespace chip
|