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 : * Defines the public interface for the Device Layer ConfigurationManager object.
23 : */
24 :
25 : #pragma once
26 :
27 : #include <cstdint>
28 :
29 : #include <platform/CHIPDeviceConfig.h>
30 :
31 : #if CHIP_HAVE_CONFIG_H
32 : #include <setup_payload/CHIPAdditionalDataPayloadBuildConfig.h>
33 : #endif
34 :
35 : #include <lib/core/ClusterEnums.h>
36 : #include <lib/support/Span.h>
37 : #include <platform/PersistedStorage.h>
38 : #include <platform/internal/CHIPDeviceLayerInternal.h>
39 :
40 : namespace chip {
41 : namespace Ble {
42 : struct ChipBLEDeviceIdentificationInfo;
43 : }
44 : } // namespace chip
45 :
46 : namespace chip {
47 : namespace DeviceLayer {
48 :
49 : class PlatformManagerImpl;
50 : class ConfigurationManagerImpl;
51 : namespace Internal {
52 : template <class>
53 : class GenericPlatformManagerImpl;
54 : template <class>
55 : class GenericPlatformManagerImpl_POSIX;
56 : } // namespace Internal
57 :
58 : /**
59 : * Provides access to runtime and build-time configuration information for a chip device.
60 : */
61 : class ConfigurationManager
62 : {
63 : public:
64 : // ===== Members that define the public interface of the ConfigurationManager
65 :
66 : enum
67 : {
68 : kMaxVendorNameLength = 32,
69 : kMaxProductNameLength = 32,
70 : kMaxLocationLength = 2,
71 : kMaxHardwareVersionStringLength = 64,
72 : kMaxSoftwareVersionStringLength = 64,
73 : kMaxManufacturingDateLength = 16,
74 : kMaxPartNumberLength = 32,
75 : kMaxProductURLLength = 256,
76 : kMaxProductLabelLength = 64,
77 : kMaxSerialNumberLength = 32,
78 : kMaxUniqueIDLength = 32,
79 : #if CHIP_ENABLE_ROTATING_DEVICE_ID && defined(CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID)
80 : kMinRotatingDeviceIDUniqueIDLength = 16,
81 : kRotatingDeviceIDUniqueIDLength = CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID_LENGTH,
82 : #endif
83 : kEthernetMACAddressLength = 6,
84 : kThreadMACAddressLength = 8,
85 : #if CHIP_DEVICE_CONFIG_ENABLE_THREAD
86 : kPrimaryMACAddressLength = kThreadMACAddressLength,
87 : #else
88 : kPrimaryMACAddressLength = kEthernetMACAddressLength,
89 : #endif
90 : kMaxMACAddressLength = 8,
91 : kMaxLanguageTagLength = 5 // ISO 639-1 standard language codes
92 : };
93 :
94 : // Copies the primary MAC into a mutable span, which must be of size kPrimaryMACAddressLength.
95 : // Upon success, the span will be reduced to the size of the MAC address being returned, which
96 : // can be less than kPrimaryMACAddressLength on a device that supports Thread.
97 : virtual CHIP_ERROR GetPrimaryMACAddress(MutableByteSpan & buf) = 0;
98 :
99 : // Copies the primary WiFi MAC into a buffer of size kEthernetMACAddressLength
100 : virtual CHIP_ERROR GetPrimaryWiFiMACAddress(uint8_t * buf) = 0;
101 :
102 : // Copies the primary Thread (802.15.4) MAC into a buffer of size kThreadMACAddressLength
103 : virtual CHIP_ERROR GetPrimary802154MACAddress(uint8_t * buf) = 0;
104 : virtual CHIP_ERROR GetSoftwareVersionString(char * buf, size_t bufSize) = 0;
105 : virtual CHIP_ERROR GetSoftwareVersion(uint32_t & softwareVer) = 0;
106 : virtual CHIP_ERROR GetConfigurationVersion(uint32_t & configurationVer) = 0;
107 : virtual CHIP_ERROR GetFirmwareBuildChipEpochTime(System::Clock::Seconds32 & buildTime) = 0;
108 0 : virtual CHIP_ERROR SetFirmwareBuildChipEpochTime(System::Clock::Seconds32 buildTime) { return CHIP_ERROR_NOT_IMPLEMENTED; }
109 : #if CHIP_ENABLE_ROTATING_DEVICE_ID && defined(CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID)
110 : // Lifetime counter is monotonic counter that is incremented upon each commencement of advertising
111 : virtual CHIP_ERROR GetLifetimeCounter(uint16_t & lifetimeCounter) = 0;
112 : virtual CHIP_ERROR IncrementLifetimeCounter() = 0;
113 : virtual CHIP_ERROR SetRotatingDeviceIdUniqueId(const ByteSpan & uniqueIdSpan) = 0;
114 : virtual CHIP_ERROR GetRotatingDeviceIdUniqueId(MutableByteSpan & uniqueIdSpan) = 0;
115 : #endif
116 : virtual CHIP_ERROR GetRegulatoryLocation(uint8_t & location) = 0;
117 : virtual CHIP_ERROR GetCountryCode(char * buf, size_t bufSize, size_t & codeLen) = 0;
118 : virtual CHIP_ERROR StoreSerialNumber(const char * serialNum, size_t serialNumLen) = 0;
119 : virtual CHIP_ERROR StoreManufacturingDate(const char * mfgDate, size_t mfgDateLen) = 0;
120 : virtual CHIP_ERROR StoreSoftwareVersion(uint32_t softwareVer) = 0;
121 : virtual CHIP_ERROR StoreConfigurationVersion(uint32_t configurationVer) = 0;
122 : virtual CHIP_ERROR StoreHardwareVersion(uint16_t hardwareVer) = 0;
123 : virtual CHIP_ERROR StoreRegulatoryLocation(uint8_t location) = 0;
124 : virtual CHIP_ERROR StoreCountryCode(const char * code, size_t codeLen) = 0;
125 : virtual CHIP_ERROR GetRebootCount(uint32_t & rebootCount) = 0;
126 : virtual CHIP_ERROR StoreRebootCount(uint32_t rebootCount) = 0;
127 : virtual CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours) = 0;
128 : virtual CHIP_ERROR StoreTotalOperationalHours(uint32_t totalOperationalHours) = 0;
129 : virtual CHIP_ERROR GetBootReason(uint32_t & bootReason) = 0;
130 : virtual CHIP_ERROR StoreBootReason(uint32_t bootReason) = 0;
131 : virtual CHIP_ERROR GetUniqueId(char * buf, size_t bufSize) = 0;
132 : virtual CHIP_ERROR StoreUniqueId(const char * uniqueId, size_t uniqueIdLen) = 0;
133 : virtual CHIP_ERROR GenerateUniqueId(char * buf, size_t bufSize) = 0;
134 : virtual CHIP_ERROR GetFailSafeArmed(bool & val) = 0;
135 : virtual CHIP_ERROR SetFailSafeArmed(bool val) = 0;
136 :
137 : virtual CHIP_ERROR GetBLEDeviceIdentificationInfo(Ble::ChipBLEDeviceIdentificationInfo & deviceIdInfo) = 0;
138 :
139 : #if CHIP_CONFIG_TEST
140 : virtual void RunUnitTests() = 0;
141 : #endif
142 :
143 : virtual bool IsFullyProvisioned() = 0;
144 : virtual void InitiateFactoryReset() = 0;
145 :
146 : // Gets called when starting BLE/DNS-SD advertisement
147 : #if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
148 : virtual void NotifyOfAdvertisementStart() {}
149 : #else
150 : void NotifyOfAdvertisementStart() {}
151 : #endif
152 :
153 : virtual void LogDeviceConfig() = 0;
154 :
155 : virtual bool IsCommissionableDeviceTypeEnabled() = 0;
156 : virtual CHIP_ERROR GetDeviceTypeId(uint32_t & deviceType) = 0;
157 : virtual bool IsCommissionableDeviceNameEnabled() = 0;
158 : virtual CHIP_ERROR GetCommissionableDeviceName(char * buf, size_t bufSize) = 0;
159 : virtual CHIP_ERROR GetInitialPairingHint(uint16_t & pairingHint) = 0;
160 : virtual CHIP_ERROR GetInitialPairingInstruction(char * buf, size_t bufSize) = 0;
161 : virtual CHIP_ERROR GetSecondaryPairingHint(uint16_t & pairingHint) = 0;
162 : virtual CHIP_ERROR GetSecondaryPairingInstruction(char * buf, size_t bufSize) = 0;
163 :
164 : virtual CHIP_ERROR GetLocationCapability(uint8_t & location);
165 :
166 : protected:
167 : // ===== Members for internal use by the following friends.
168 :
169 : friend class ::chip::DeviceLayer::PlatformManagerImpl;
170 : template <class>
171 : friend class ::chip::DeviceLayer::Internal::GenericPlatformManagerImpl;
172 : template <class>
173 : friend class ::chip::DeviceLayer::Internal::GenericPlatformManagerImpl_POSIX;
174 : // Parentheses used to fix clang parsing issue with these declarations
175 : friend CHIP_ERROR(::chip::Platform::PersistedStorage::Read)(::chip::Platform::PersistedStorage::Key key, uint32_t & value);
176 : friend CHIP_ERROR(::chip::Platform::PersistedStorage::Write)(::chip::Platform::PersistedStorage::Key key, uint32_t value);
177 :
178 : virtual CHIP_ERROR Init() = 0;
179 : virtual bool CanFactoryReset() = 0;
180 : virtual CHIP_ERROR ReadPersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t & value) = 0;
181 : virtual CHIP_ERROR WritePersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t value) = 0;
182 :
183 : // Construction/destruction limited to subclasses.
184 : ConfigurationManager() = default;
185 : virtual ~ConfigurationManager() = default;
186 :
187 : // No copy, move or assignment.
188 : ConfigurationManager(const ConfigurationManager &) = delete;
189 : ConfigurationManager(const ConfigurationManager &&) = delete;
190 : ConfigurationManager & operator=(const ConfigurationManager &) = delete;
191 : };
192 :
193 : /**
194 : * Returns a reference to a ConfigurationManager object.
195 : *
196 : * Applications should use this to access features of the ConfigurationManager object
197 : * that are common to all platforms.
198 : */
199 : ConfigurationManager & ConfigurationMgr();
200 :
201 : /**
202 : * Returns the platform-specific implementation of the ConfigurationManager object.
203 : *
204 : * Applications can use this to gain access to features of the ConfigurationManager
205 : * that are specific to the selected platform.
206 : */
207 : extern ConfigurationManager & ConfigurationMgrImpl();
208 :
209 : /**
210 : * Sets a reference to a ConfigurationManager object.
211 : *
212 : * This must be called before any calls to ConfigurationMgr. If a nullptr is passed in,
213 : * no changes will be made.
214 : */
215 : void SetConfigurationMgr(ConfigurationManager * configurationManager);
216 :
217 0 : inline CHIP_ERROR ConfigurationManager::GetLocationCapability(uint8_t & location)
218 : {
219 0 : location = to_underlying(chip::app::Clusters::GeneralCommissioning::RegulatoryLocationTypeEnum::kIndoor);
220 0 : return CHIP_NO_ERROR;
221 : }
222 :
223 : } // namespace DeviceLayer
224 : } // namespace chip
225 :
226 : /* Include a header file containing the implementation of the ConfigurationManager
227 : * object for the selected platform.
228 : */
229 : #ifdef EXTERNAL_CONFIGURATIONMANAGERIMPL_HEADER
230 : #include EXTERNAL_CONFIGURATIONMANAGERIMPL_HEADER
231 : #elif defined(CHIP_DEVICE_LAYER_TARGET)
232 : #define CONFIGURATIONMANAGERIMPL_HEADER <platform/CHIP_DEVICE_LAYER_TARGET/ConfigurationManagerImpl.h>
233 : #include CONFIGURATIONMANAGERIMPL_HEADER
234 : #endif // defined(CHIP_DEVICE_LAYER_TARGET)
|