3 * Copyright (c) 2020-2021 Project CHIP Authors
4 * Copyright (c) 2019-2020 Google LLC.
5 * Copyright (c) 2018 Nest Labs, Inc.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 * Provides an generic implementation of ConfigurationManager features
23 * for use on various platforms.
28 #include <support/BitFlags.h>
30 #if CHIP_ENABLE_ROTATING_DEVICE_ID
31 #include <support/LifetimePersistedCounter.h>
35 namespace DeviceLayer {
37 class ProvisioningDataSet;
42 * Provides a generic implementation of ConfigurationManager features that works on multiple platforms.
44 * This template contains implementations of select features from the ConfigurationManager abstract
45 * interface that are suitable for use on all platforms. It is intended to be inherited (directly
46 * or indirectly) by the ConfigurationManagerImpl class, which also appears as the template's ImplClass
49 template <class ImplClass>
50 class GenericConfigurationManagerImpl
53 // ===== Methods that implement the ConfigurationManager abstract interface.
56 CHIP_ERROR _ConfigureChiptack();
57 CHIP_ERROR _GetVendorId(uint16_t & vendorId);
58 CHIP_ERROR _GetProductId(uint16_t & productId);
59 CHIP_ERROR _GetProductRevision(uint16_t & productRev);
60 CHIP_ERROR _StoreProductRevision(uint16_t productRev);
61 CHIP_ERROR _GetFirmwareRevision(char * buf, size_t bufSize, size_t & outLen);
62 CHIP_ERROR _GetFirmwareBuildTime(uint16_t & year, uint8_t & month, uint8_t & dayOfMonth, uint8_t & hour, uint8_t & minute,
64 CHIP_ERROR _GetSerialNumber(char * buf, size_t bufSize, size_t & serialNumLen);
65 CHIP_ERROR _StoreSerialNumber(const char * serialNum, size_t serialNumLen);
66 CHIP_ERROR _GetPrimaryWiFiMACAddress(uint8_t * buf);
67 CHIP_ERROR _StorePrimaryWiFiMACAddress(const uint8_t * buf);
68 CHIP_ERROR _GetPrimary802154MACAddress(uint8_t * buf);
69 CHIP_ERROR _GetFactoryAssignedEUI64(uint8_t (&buf)[8]);
70 CHIP_ERROR _StorePrimary802154MACAddress(const uint8_t * buf);
71 CHIP_ERROR _GetManufacturingDate(uint16_t & year, uint8_t & month, uint8_t & dayOfMonth);
72 CHIP_ERROR _StoreManufacturingDate(const char * mfgDate, size_t mfgDateLen);
73 CHIP_ERROR _GetDeviceId(uint64_t & deviceId);
74 CHIP_ERROR _GetDeviceCertificate(uint8_t * buf, size_t bufSize, size_t & certLen);
75 CHIP_ERROR _GetDeviceIntermediateCACerts(uint8_t * buf, size_t bufSize, size_t & certsLen);
76 CHIP_ERROR _GetDevicePrivateKey(uint8_t * buf, size_t bufSize, size_t & keyLen);
77 #if CHIP_DEVICE_CONFIG_ENABLE_JUST_IN_TIME_PROVISIONING
78 CHIP_ERROR _StoreDeviceId(uint64_t deviceId);
79 CHIP_ERROR _StoreDeviceCertificate(const uint8_t * cert, size_t certLen);
80 CHIP_ERROR _StoreDeviceIntermediateCACerts(const uint8_t * certs, size_t certsLen);
81 CHIP_ERROR _StoreDevicePrivateKey(const uint8_t * key, size_t keyLen);
82 CHIP_ERROR _ClearOperationalDeviceCredentials(void);
84 CHIP_ERROR _GetManufacturerDeviceId(uint64_t & deviceId);
85 CHIP_ERROR _StoreManufacturerDeviceId(uint64_t deviceId);
86 CHIP_ERROR _GetManufacturerDeviceCertificate(uint8_t * buf, size_t bufSize, size_t & certLen);
87 CHIP_ERROR _StoreManufacturerDeviceCertificate(const uint8_t * cert, size_t certLen);
88 CHIP_ERROR _GetManufacturerDeviceIntermediateCACerts(uint8_t * buf, size_t bufSize, size_t & certsLen);
89 CHIP_ERROR _StoreManufacturerDeviceIntermediateCACerts(const uint8_t * certs, size_t certsLen);
90 CHIP_ERROR _GetManufacturerDevicePrivateKey(uint8_t * buf, size_t bufSize, size_t & keyLen);
91 CHIP_ERROR _StoreManufacturerDevicePrivateKey(const uint8_t * key, size_t keyLen);
92 CHIP_ERROR _GetSetupPinCode(uint32_t & setupPinCode);
93 CHIP_ERROR _StoreSetupPinCode(uint32_t setupPinCode);
94 CHIP_ERROR _GetSetupDiscriminator(uint16_t & setupDiscriminator);
95 CHIP_ERROR _StoreSetupDiscriminator(uint16_t setupDiscriminator);
96 CHIP_ERROR _GetFabricId(uint64_t & fabricId);
97 CHIP_ERROR _StoreFabricId(uint64_t fabricId);
98 #if CHIP_ENABLE_ROTATING_DEVICE_ID
99 CHIP_ERROR _GetLifetimeCounter(uint16_t & lifetimeCounter);
100 CHIP_ERROR _IncrementLifetimeCounter();
102 CHIP_ERROR _GetServiceId(uint64_t & serviceId);
103 CHIP_ERROR _GetServiceConfig(uint8_t * buf, size_t bufSize, size_t & serviceConfigLen);
104 CHIP_ERROR _StoreServiceConfig(const uint8_t * serviceConfig, size_t serviceConfigLen);
105 CHIP_ERROR _GetPairedAccountId(char * buf, size_t bufSize, size_t & accountIdLen);
106 CHIP_ERROR _StorePairedAccountId(const char * accountId, size_t accountIdLen);
107 CHIP_ERROR _StoreServiceProvisioningData(uint64_t serviceId, const uint8_t * serviceConfig, size_t serviceConfigLen,
108 const char * accountId, size_t accountIdLen);
109 CHIP_ERROR _ClearServiceProvisioningData();
110 CHIP_ERROR _GetFailSafeArmed(bool & val);
111 CHIP_ERROR _SetFailSafeArmed(bool val);
112 CHIP_ERROR _GetQRCodeString(char * buf, size_t bufSize);
113 CHIP_ERROR _GetWiFiAPSSID(char * buf, size_t bufSize);
114 CHIP_ERROR _GetBLEDeviceIdentificationInfo(Ble::ChipBLEDeviceIdentificationInfo & deviceIdInfo);
115 CHIP_ERROR _ConfigureChipStack();
117 CHIP_ERROR _RunUnitTests(void);
119 bool _IsServiceProvisioned();
120 bool _IsMemberOfFabric();
121 bool _IsPairedToAccount();
122 bool _IsFullyProvisioned();
123 CHIP_ERROR _ComputeProvisioningHash(uint8_t * hashBuf, size_t hashBufSize);
124 #if CHIP_DEVICE_CONFIG_ENABLE_JUST_IN_TIME_PROVISIONING
125 bool _OperationalDeviceCredentialsProvisioned();
126 void _UseManufacturerCredentialsAsOperational(bool val);
128 void _LogDeviceConfig();
131 enum class Flags : uint8_t
133 kIsServiceProvisioned = 0x01,
134 kIsMemberOfFabric = 0x02,
135 kIsPairedToAccount = 0x04,
136 kOperationalDeviceCredentialsProvisioned = 0x08,
137 kUseManufacturerCredentialsAsOperational = 0x10,
140 BitFlags<Flags> mFlags;
141 #if CHIP_ENABLE_ROTATING_DEVICE_ID
142 chip::LifetimePersistedCounter mLifetimePersistedCounter;
144 CHIP_ERROR PersistProvisioningData(ProvisioningDataSet & provData);
147 ImplClass * Impl() { return static_cast<ImplClass *>(this); }
149 #if CHIP_DEVICE_CONFIG_ENABLE_JUST_IN_TIME_PROVISIONING
150 bool UseManufacturerCredentialsAsOperational();
154 // Instruct the compiler to instantiate the template only when explicitly told to do so.
155 extern template class Internal::GenericConfigurationManagerImpl<ConfigurationManagerImpl>;
157 template <class ImplClass>
158 inline CHIP_ERROR GenericConfigurationManagerImpl<ImplClass>::_GetVendorId(uint16_t & vendorId)
160 vendorId = static_cast<uint16_t>(CHIP_DEVICE_CONFIG_DEVICE_VENDOR_ID);
161 return CHIP_NO_ERROR;
164 template <class ImplClass>
165 inline CHIP_ERROR GenericConfigurationManagerImpl<ImplClass>::_GetProductId(uint16_t & productId)
167 productId = static_cast<uint16_t>(CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID);
168 return CHIP_NO_ERROR;
171 } // namespace Internal
172 } // namespace DeviceLayer