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 : #if CHIP_DEVICE_CONFIG_ENABLE_THREAD 84 : kPrimaryMACAddressLength = 8, 85 : #else 86 : kPrimaryMACAddressLength = 6, 87 : #endif 88 : kMaxMACAddressLength = 8, 89 : kMaxLanguageTagLength = 5 // ISO 639-1 standard language codes 90 : }; 91 : 92 : virtual CHIP_ERROR GetPrimaryMACAddress(MutableByteSpan buf) = 0; 93 : virtual CHIP_ERROR GetPrimaryWiFiMACAddress(uint8_t * buf) = 0; 94 : virtual CHIP_ERROR GetPrimary802154MACAddress(uint8_t * buf) = 0; 95 : virtual CHIP_ERROR GetSoftwareVersionString(char * buf, size_t bufSize) = 0; 96 : virtual CHIP_ERROR GetSoftwareVersion(uint32_t & softwareVer) = 0; 97 : virtual CHIP_ERROR GetFirmwareBuildChipEpochTime(System::Clock::Seconds32 & buildTime) = 0; 98 0 : virtual CHIP_ERROR SetFirmwareBuildChipEpochTime(System::Clock::Seconds32 buildTime) { return CHIP_ERROR_NOT_IMPLEMENTED; } 99 : #if CHIP_ENABLE_ROTATING_DEVICE_ID && defined(CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID) 100 : // Lifetime counter is monotonic counter that is incremented upon each commencement of advertising 101 : virtual CHIP_ERROR GetLifetimeCounter(uint16_t & lifetimeCounter) = 0; 102 : virtual CHIP_ERROR IncrementLifetimeCounter() = 0; 103 : virtual CHIP_ERROR SetRotatingDeviceIdUniqueId(const ByteSpan & uniqueIdSpan) = 0; 104 : virtual CHIP_ERROR GetRotatingDeviceIdUniqueId(MutableByteSpan & uniqueIdSpan) = 0; 105 : #endif 106 : virtual CHIP_ERROR GetRegulatoryLocation(uint8_t & location) = 0; 107 : virtual CHIP_ERROR GetCountryCode(char * buf, size_t bufSize, size_t & codeLen) = 0; 108 : virtual CHIP_ERROR StoreSerialNumber(const char * serialNum, size_t serialNumLen) = 0; 109 : virtual CHIP_ERROR StoreManufacturingDate(const char * mfgDate, size_t mfgDateLen) = 0; 110 : virtual CHIP_ERROR StoreSoftwareVersion(uint32_t softwareVer) = 0; 111 : virtual CHIP_ERROR StoreHardwareVersion(uint16_t hardwareVer) = 0; 112 : virtual CHIP_ERROR StoreRegulatoryLocation(uint8_t location) = 0; 113 : virtual CHIP_ERROR StoreCountryCode(const char * code, size_t codeLen) = 0; 114 : virtual CHIP_ERROR GetRebootCount(uint32_t & rebootCount) = 0; 115 : virtual CHIP_ERROR StoreRebootCount(uint32_t rebootCount) = 0; 116 : virtual CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours) = 0; 117 : virtual CHIP_ERROR StoreTotalOperationalHours(uint32_t totalOperationalHours) = 0; 118 : virtual CHIP_ERROR GetBootReason(uint32_t & bootReason) = 0; 119 : virtual CHIP_ERROR StoreBootReason(uint32_t bootReason) = 0; 120 : virtual CHIP_ERROR GetUniqueId(char * buf, size_t bufSize) = 0; 121 : virtual CHIP_ERROR StoreUniqueId(const char * uniqueId, size_t uniqueIdLen) = 0; 122 : virtual CHIP_ERROR GenerateUniqueId(char * buf, size_t bufSize) = 0; 123 : virtual CHIP_ERROR GetFailSafeArmed(bool & val) = 0; 124 : virtual CHIP_ERROR SetFailSafeArmed(bool val) = 0; 125 : 126 : virtual CHIP_ERROR GetBLEDeviceIdentificationInfo(Ble::ChipBLEDeviceIdentificationInfo & deviceIdInfo) = 0; 127 : 128 : #if CHIP_CONFIG_TEST 129 : virtual void RunUnitTests() = 0; 130 : #endif 131 : 132 : virtual bool IsFullyProvisioned() = 0; 133 : virtual void InitiateFactoryReset() = 0; 134 : 135 : // Gets called when starting BLE/DNS-SD advertisement 136 : #if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING 137 : virtual void NotifyOfAdvertisementStart() {} 138 : #else 139 : void NotifyOfAdvertisementStart() {} 140 : #endif 141 : 142 : virtual void LogDeviceConfig() = 0; 143 : 144 : virtual bool IsCommissionableDeviceTypeEnabled() = 0; 145 : virtual CHIP_ERROR GetDeviceTypeId(uint32_t & deviceType) = 0; 146 : virtual bool IsCommissionableDeviceNameEnabled() = 0; 147 : virtual CHIP_ERROR GetCommissionableDeviceName(char * buf, size_t bufSize) = 0; 148 : virtual CHIP_ERROR GetInitialPairingHint(uint16_t & pairingHint) = 0; 149 : virtual CHIP_ERROR GetInitialPairingInstruction(char * buf, size_t bufSize) = 0; 150 : virtual CHIP_ERROR GetSecondaryPairingHint(uint16_t & pairingHint) = 0; 151 : virtual CHIP_ERROR GetSecondaryPairingInstruction(char * buf, size_t bufSize) = 0; 152 : 153 : virtual CHIP_ERROR GetLocationCapability(uint8_t & location); 154 : 155 : protected: 156 : // ===== Members for internal use by the following friends. 157 : 158 : friend class ::chip::DeviceLayer::PlatformManagerImpl; 159 : template <class> 160 : friend class ::chip::DeviceLayer::Internal::GenericPlatformManagerImpl; 161 : template <class> 162 : friend class ::chip::DeviceLayer::Internal::GenericPlatformManagerImpl_POSIX; 163 : // Parentheses used to fix clang parsing issue with these declarations 164 : friend CHIP_ERROR(::chip::Platform::PersistedStorage::Read)(::chip::Platform::PersistedStorage::Key key, uint32_t & value); 165 : friend CHIP_ERROR(::chip::Platform::PersistedStorage::Write)(::chip::Platform::PersistedStorage::Key key, uint32_t value); 166 : 167 : virtual CHIP_ERROR Init() = 0; 168 : virtual bool CanFactoryReset() = 0; 169 : virtual CHIP_ERROR ReadPersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t & value) = 0; 170 : virtual CHIP_ERROR WritePersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t value) = 0; 171 : 172 : // Construction/destruction limited to subclasses. 173 : ConfigurationManager() = default; 174 : virtual ~ConfigurationManager() = default; 175 : 176 : // No copy, move or assignment. 177 : ConfigurationManager(const ConfigurationManager &) = delete; 178 : ConfigurationManager(const ConfigurationManager &&) = delete; 179 : ConfigurationManager & operator=(const ConfigurationManager &) = delete; 180 : }; 181 : 182 : /** 183 : * Returns a reference to a ConfigurationManager object. 184 : * 185 : * Applications should use this to access features of the ConfigurationManager object 186 : * that are common to all platforms. 187 : */ 188 : ConfigurationManager & ConfigurationMgr(); 189 : 190 : /** 191 : * Returns the platform-specific implementation of the ConfigurationManager object. 192 : * 193 : * Applications can use this to gain access to features of the ConfigurationManager 194 : * that are specific to the selected platform. 195 : */ 196 : extern ConfigurationManager & ConfigurationMgrImpl(); 197 : 198 : /** 199 : * Sets a reference to a ConfigurationManager object. 200 : * 201 : * This must be called before any calls to ConfigurationMgr. If a nullptr is passed in, 202 : * no changes will be made. 203 : */ 204 : void SetConfigurationMgr(ConfigurationManager * configurationManager); 205 : 206 0 : inline CHIP_ERROR ConfigurationManager::GetLocationCapability(uint8_t & location) 207 : { 208 0 : location = to_underlying(chip::app::Clusters::GeneralCommissioning::RegulatoryLocationTypeEnum::kIndoor); 209 0 : return CHIP_NO_ERROR; 210 : } 211 : 212 : } // namespace DeviceLayer 213 : } // namespace chip 214 : 215 : /* Include a header file containing the implementation of the ConfigurationManager 216 : * object for the selected platform. 217 : */ 218 : #ifdef EXTERNAL_CONFIGURATIONMANAGERIMPL_HEADER 219 : #include EXTERNAL_CONFIGURATIONMANAGERIMPL_HEADER 220 : #elif defined(CHIP_DEVICE_LAYER_TARGET) 221 : #define CONFIGURATIONMANAGERIMPL_HEADER <platform/CHIP_DEVICE_LAYER_TARGET/ConfigurationManagerImpl.h> 222 : #include CONFIGURATIONMANAGERIMPL_HEADER 223 : #endif // defined(CHIP_DEVICE_LAYER_TARGET)