Skip to content

Commit

Permalink
Created Device Instance Info Provider (#18767)
Browse files Browse the repository at this point in the history
The new Device Instance Info Provider provides API for
accessing factory-provisioned device instance identifiers.

All removed Configuration Manager method occurrences
were replaced with a new Device Instance Info Provider API.
Added LegacyDeviceInstanceInfoProvider implementation that
preserves the existing ConfigurationManager methods.
  • Loading branch information
ArekBalysNordic authored May 27, 2022
1 parent e7bb258 commit e023667
Show file tree
Hide file tree
Showing 21 changed files with 414 additions and 179 deletions.
3 changes: 2 additions & 1 deletion examples/common/pigweed/rpc_services/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "platform/ConfigurationManager.h"
#include "platform/DiagnosticDataProvider.h"
#include "platform/PlatformManager.h"
#include <platform/DeviceInstanceInfoProvider.h>
#include <setup_payload/QRCodeSetupPayloadGenerator.h>

namespace chip {
Expand Down Expand Up @@ -141,7 +142,7 @@ class Device : public pw_rpc::nanopb::Device::Service<Device>
response.has_pairing_info = true;
}

if (DeviceLayer::ConfigurationMgr().GetSerialNumber(response.serial_number, sizeof(response.serial_number)) ==
if (DeviceLayer::GetDeviceInstanceInfoProvider()->GetSerialNumber(response.serial_number, sizeof(response.serial_number)) ==
CHIP_NO_ERROR)
{
snprintf(response.serial_number, sizeof(response.serial_number), CHIP_DEVICE_CONFIG_TEST_SERIAL_NUMBER);
Expand Down
3 changes: 2 additions & 1 deletion examples/lighting-app/cyw30739/src/ZclCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "LightingManager.h"
#include <app-common/zap-generated/attributes/Accessors.h>
#include <platform/CHIPDeviceLayer.h>
#include <platform/DeviceInstanceInfoProvider.h>

using namespace chip;
using namespace chip::app::Clusters;
Expand All @@ -32,7 +33,7 @@ void emberAfBasicClusterInitCallback(EndpointId endpoint)
uint8_t dayOfMonth;
char cString[16] = "00000000";

if (ConfigurationMgr().GetManufacturingDate(year, month, dayOfMonth) == CHIP_NO_ERROR)
if (GetDeviceInstanceInfoProvider()->GetManufacturingDate(year, month, dayOfMonth) == CHIP_NO_ERROR)
{
snprintf(cString, sizeof(cString), "%04u%02u%02u", year, month, dayOfMonth);
}
Expand Down
3 changes: 2 additions & 1 deletion examples/lock-app/cyw30739/src/ZclCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <BoltLockManager.h>
#include <app-common/zap-generated/attributes/Accessors.h>
#include <platform/CHIPDeviceLayer.h>
#include <platform/DeviceInstanceInfoProvider.h>

using namespace chip;
using namespace chip::app::Clusters;
Expand All @@ -32,7 +33,7 @@ void emberAfBasicClusterInitCallback(EndpointId endpoint)
uint8_t dayOfMonth;
char cString[16] = "00000000";

if (ConfigurationMgr().GetManufacturingDate(year, month, dayOfMonth) == CHIP_NO_ERROR)
if (GetDeviceInstanceInfoProvider()->GetManufacturingDate(year, month, dayOfMonth) == CHIP_NO_ERROR)
{
snprintf(cString, sizeof(cString), "%04u%02u%02u", year, month, dayOfMonth);
}
Expand Down
10 changes: 6 additions & 4 deletions src/app/clusters/basic/basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <app/util/attribute-storage.h>
#include <platform/CHIPDeviceLayer.h>
#include <platform/ConfigurationManager.h>
#include <platform/DeviceInstanceInfoProvider.h>
#include <platform/PlatformManager.h>

#include <cstddef>
Expand Down Expand Up @@ -125,7 +126,7 @@ CHIP_ERROR BasicAttrAccess::Read(const ConcreteReadAttributePath & aPath, Attrib

case HardwareVersion::Id: {
uint16_t hardwareVersion = 0;
status = ConfigurationMgr().GetHardwareVersion(hardwareVersion);
status = GetDeviceInstanceInfoProvider()->GetHardwareVersion(hardwareVersion);
if (status == CHIP_NO_ERROR)
{
status = aEncoder.Encode(hardwareVersion);
Expand All @@ -136,7 +137,7 @@ CHIP_ERROR BasicAttrAccess::Read(const ConcreteReadAttributePath & aPath, Attrib
case HardwareVersionString::Id: {
constexpr size_t kMaxLen = DeviceLayer::ConfigurationManager::kMaxHardwareVersionStringLength;
char hardwareVersionString[kMaxLen + 1] = { 0 };
status = ConfigurationMgr().GetHardwareVersionString(hardwareVersionString, sizeof(hardwareVersionString));
status = GetDeviceInstanceInfoProvider()->GetHardwareVersionString(hardwareVersionString, sizeof(hardwareVersionString));
status = EncodeStringOnSuccess(status, aEncoder, hardwareVersionString, kMaxLen);
break;
}
Expand Down Expand Up @@ -165,7 +166,8 @@ CHIP_ERROR BasicAttrAccess::Read(const ConcreteReadAttributePath & aPath, Attrib
uint16_t manufacturingYear;
uint8_t manufacturingMonth;
uint8_t manufacturingDayOfMonth;
status = ConfigurationMgr().GetManufacturingDate(manufacturingYear, manufacturingMonth, manufacturingDayOfMonth);
status =
GetDeviceInstanceInfoProvider()->GetManufacturingDate(manufacturingYear, manufacturingMonth, manufacturingDayOfMonth);

// TODO: Remove defaulting once proper runtime defaulting of unimplemented factory data is done
if (status == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND || status == CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE)
Expand Down Expand Up @@ -237,7 +239,7 @@ CHIP_ERROR BasicAttrAccess::Read(const ConcreteReadAttributePath & aPath, Attrib
case SerialNumber::Id: {
constexpr size_t kMaxLen = DeviceLayer::ConfigurationManager::kMaxSerialNumberLength;
char serialNumberString[kMaxLen + 1] = { 0 };
status = ConfigurationMgr().GetSerialNumber(serialNumberString, sizeof(serialNumberString));
status = GetDeviceInstanceInfoProvider()->GetSerialNumber(serialNumberString, sizeof(serialNumberString));

// TODO: Remove defaulting once proper runtime defaulting of unimplemented factory data is done
if (status == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND || status == CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE)
Expand Down
3 changes: 2 additions & 1 deletion src/app/clusters/ota-requestor/DefaultOTARequestor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <app/clusters/ota-requestor/ota-requestor-server.h>
#include <lib/core/CHIPEncoding.h>
#include <platform/CHIPDeviceLayer.h>
#include <platform/DeviceInstanceInfoProvider.h>
#include <platform/OTAImageProcessor.h>
#include <protocols/bdx/BdxUri.h>
#include <zap-generated/CHIPClusters.h>
Expand Down Expand Up @@ -733,7 +734,7 @@ CHIP_ERROR DefaultOTARequestor::SendQueryImageRequest(OperationalDeviceProxy & d
args.requestorCanConsent.SetValue(!Basic::IsLocalConfigDisabled() && mOtaRequestorDriver->CanConsent());

uint16_t hardwareVersion;
if (DeviceLayer::ConfigurationMgr().GetHardwareVersion(hardwareVersion) == CHIP_NO_ERROR)
if (DeviceLayer::GetDeviceInstanceInfoProvider()->GetHardwareVersion(hardwareVersion) == CHIP_NO_ERROR)
{
args.hardwareVersion.SetValue(hardwareVersion);
}
Expand Down
4 changes: 3 additions & 1 deletion src/app/server/Dnssd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <platform/ConfigurationManager.h>
#include <protocols/secure_channel/PASESession.h>
#if CHIP_ENABLE_ROTATING_DEVICE_ID && defined(CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID)
#include <platform/DeviceInstanceInfoProvider.h>
#include <setup_payload/AdditionalDataPayloadGenerator.h>
#endif
#include <credentials/FabricTable.h>
Expand Down Expand Up @@ -498,7 +499,8 @@ CHIP_ERROR DnssdServer::GenerateRotatingDeviceId(char rotatingDeviceIdHexBuffer[
MutableByteSpan rotatingDeviceIdUniqueIdSpan(rotatingDeviceIdUniqueId);
size_t rotatingDeviceIdValueOutputSize = 0;

ReturnErrorOnFailure(chip::DeviceLayer::ConfigurationMgr().GetRotatingDeviceIdUniqueId(rotatingDeviceIdUniqueIdSpan));
ReturnErrorOnFailure(
chip::DeviceLayer::GetDeviceInstanceInfoProvider()->GetRotatingDeviceIdUniqueId(rotatingDeviceIdUniqueIdSpan));
ReturnErrorOnFailure(
chip::DeviceLayer::ConfigurationMgr().GetLifetimeCounter(additionalDataPayloadParams.rotatingDeviceIdLifetimeCounter));
additionalDataPayloadParams.rotatingDeviceIdUniqueId = rotatingDeviceIdUniqueIdSpan;
Expand Down
31 changes: 12 additions & 19 deletions src/include/platform/ConfigurationManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,27 +85,20 @@ class ConfigurationManager
kMaxLanguageTagLength = 5 // ISO 639-1 standard language codes
};

virtual CHIP_ERROR GetVendorName(char * buf, size_t bufSize) = 0;
virtual CHIP_ERROR GetVendorId(uint16_t & vendorId) = 0;
virtual CHIP_ERROR GetProductName(char * buf, size_t bufSize) = 0;
virtual CHIP_ERROR GetProductId(uint16_t & productId) = 0;
virtual CHIP_ERROR GetHardwareVersionString(char * buf, size_t bufSize) = 0;
virtual CHIP_ERROR GetHardwareVersion(uint16_t & hardwareVer) = 0;
virtual CHIP_ERROR GetSerialNumber(char * buf, size_t bufSize) = 0;
virtual CHIP_ERROR GetPrimaryMACAddress(MutableByteSpan buf) = 0;
virtual CHIP_ERROR GetPrimaryWiFiMACAddress(uint8_t * buf) = 0;
virtual CHIP_ERROR GetPrimary802154MACAddress(uint8_t * buf) = 0;
virtual CHIP_ERROR GetManufacturingDate(uint16_t & year, uint8_t & month, uint8_t & dayOfMonth) = 0;
virtual CHIP_ERROR GetSoftwareVersionString(char * buf, size_t bufSize) = 0;
virtual CHIP_ERROR GetSoftwareVersion(uint32_t & softwareVer) = 0;
virtual CHIP_ERROR GetVendorName(char * buf, size_t bufSize) = 0;
virtual CHIP_ERROR GetVendorId(uint16_t & vendorId) = 0;
virtual CHIP_ERROR GetProductName(char * buf, size_t bufSize) = 0;
virtual CHIP_ERROR GetProductId(uint16_t & productId) = 0;
virtual CHIP_ERROR GetPrimaryMACAddress(MutableByteSpan buf) = 0;
virtual CHIP_ERROR GetPrimaryWiFiMACAddress(uint8_t * buf) = 0;
virtual CHIP_ERROR GetPrimary802154MACAddress(uint8_t * buf) = 0;
virtual CHIP_ERROR GetSoftwareVersionString(char * buf, size_t bufSize) = 0;
virtual CHIP_ERROR GetSoftwareVersion(uint32_t & softwareVer) = 0;
#if CHIP_ENABLE_ROTATING_DEVICE_ID && defined(CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID)
// Lifetime counter is monotonic counter that is incremented upon each commencement of advertising
virtual CHIP_ERROR GetLifetimeCounter(uint16_t & lifetimeCounter) = 0;
virtual CHIP_ERROR IncrementLifetimeCounter() = 0;
// Unique ID is identifier utilized for the rotating device ID calculation purpose as an input key. It is separate identifier
// from the Basic cluster unique ID.
virtual CHIP_ERROR GetRotatingDeviceIdUniqueId(MutableByteSpan & uniqueIdSpan) = 0;
virtual CHIP_ERROR SetRotatingDeviceIdUniqueId(const ByteSpan & uniqueIdSpan) = 0;
virtual CHIP_ERROR GetLifetimeCounter(uint16_t & lifetimeCounter) = 0;
virtual CHIP_ERROR IncrementLifetimeCounter() = 0;
virtual CHIP_ERROR SetRotatingDeviceIdUniqueId(const ByteSpan & uniqueIdSpan) = 0;
#endif
virtual CHIP_ERROR GetRegulatoryLocation(uint8_t & location) = 0;
virtual CHIP_ERROR GetCountryCode(char * buf, size_t bufSize, size_t & codeLen) = 0;
Expand Down
124 changes: 124 additions & 0 deletions src/include/platform/DeviceInstanceInfoProvider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
*
* Copyright (c) 2022 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once

#include <lib/core/CHIPError.h>
#include <lib/support/Span.h>

namespace chip {
namespace DeviceLayer {

class DeviceInstanceInfoProvider
{
public:
DeviceInstanceInfoProvider() = default;
virtual ~DeviceInstanceInfoProvider() = default;

/**
* @brief Obtain the Serial Number from the device's factory data.
*
* The SerialNumber attribute specifies a human readable serial number
*
* @param[in, out] buf Buffer to copy string.
* On CHIP_NO_ERROR return from this function this buffer will be null-terminated.
* On error CHIP_ERROR_BUFFER_TOO_SMALL there is no guarantee that buffer will be null-terminated.
* @param[in] bufSize Size of data, including the null terminator, that can be written to buf.
* This size should be +1 higher than maximum possible string.
* @returns CHIP_NO_ERROR on success, or another CHIP_ERROR from the underlying implementation
* if access fails.
*/
virtual CHIP_ERROR GetSerialNumber(char * buf, size_t bufSize) = 0;

/**
* @brief Obtain a manufacturing date from the device's factory data.
*
* The ManufacturingDate attribute specifies the date that the Node was manufactured.
* Output values are returned in ISO 8601, where:
* The first month of the year is January and its returning value is equal to 1.
* The first day of a month starts from 1.
*
* @param[out] year Reference to location where manufacturing year will be stored
* @param[out] month 1-based value [range 1-12] Reference to location where manufacturing month will be stored
* @param[out] day 1-based value [range 1-31] Reference to location where manufacturing day will be stored
* @returns CHIP_NO_ERROR on success, or another CHIP_ERROR from the underlying implementation
* if access fails.
*/
virtual CHIP_ERROR GetManufacturingDate(uint16_t & year, uint8_t & month, uint8_t & day) = 0;

/**
* @brief Obtain a Hardware Version from the device's factory data.
*
* @param[out] hardwareVersion Reference to location where the hardware version integer will be copied
* @returns CHIP_NO_ERROR on success, or another CHIP_ERROR from the underlying implementation
* if access fails.
*/
virtual CHIP_ERROR GetHardwareVersion(uint16_t & hardwareVersion) = 0;

/**
* @brief Obtain a Hardware Version String from the device's factory data.
*
* The HardwareVersionString can be used to provide a more user-friendly value than that
* represented by the HardwareVersion attribute.
*
* @param[in, out] buf Buffer to copy string.
* On CHIP_NO_ERROR return from this function this buffer will be null-terminated.
* On error CHIP_ERROR_BUFFER_TOO_SMALL there is no guarantee that buffer will be null-terminated.
* @param[in] bufSize Size of data, including the null terminator, that can be written to buf.
* This size should be +1 higher than maximum possible string.
* @returns CHIP_NO_ERROR on success, CHIP_ERROR_BUFFER_TOO_SMALL if the buffer was too small to fit string and null
* terminating. or another CHIP_ERROR from the underlying implementation if access fails.
*/
virtual CHIP_ERROR GetHardwareVersionString(char * buf, size_t bufSize) = 0;

/**
* @brief Obtain a Rotating Device ID Unique ID from the device's factory data.
*
* The unique identifier consists of a randomly-generated 128-bit or longer octet string which
* was programmed during factory provisioning or delivered to the device by the vendor using
* secure means after a software update.
*
* @param[out] uniqueIdSpan Reference to location where the Rotating Device ID Unique ID will be copied
* According to specification input size of span buffer should be declared with at least 16 Bytes
* length The size of uniqueIdSpan is reduced to actual value on success
* @returns CHIP_NO_ERROR on success, or another CHIP_ERROR from the underlying implementation
* if access fails.
*/
virtual CHIP_ERROR GetRotatingDeviceIdUniqueId(MutableByteSpan & uniqueIdSpan) = 0;
};

/**
* Instance getter for the global DeviceInstanceInfoProvider.
*
* Callers have to externally synchronize usage of this function.
*
* @return The pointer to global device instance info provider. Assume never null.
*/
DeviceInstanceInfoProvider * GetDeviceInstanceInfoProvider();

/**
* Instance setter for the global DeviceInstanceInfoProvider.
*
* Callers have to externally synchronize usage of this function.
*
* If the `provider` is nullptr, no change is done.
*
* @param[in] provider the DeviceInstanceInfoProvider pointer to start returning with the getter
*/
void SetDeviceInstanceInfoProvider(DeviceInstanceInfoProvider * provider);

} // namespace DeviceLayer
} // namespace chip
14 changes: 9 additions & 5 deletions src/include/platform/internal/GenericConfigurationManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class ProvisioningDataSet;

namespace Internal {

#if CHIP_USE_TRANSITIONAL_DEVICE_INSTANCE_INFO_PROVIDER
template <class ConfigClass>
class LegacyDeviceInstanceInfoProvider;
#endif // CHIP_USE_TRANSITIONAL_DEVICE_INSTANCE_INFO_PROVIDER

#if CHIP_USE_TRANSITIONAL_COMMISSIONABLE_DATA_PROVIDER
template <class ConfigClass>
class LegacyTemporaryCommissionableDataProvider;
Expand All @@ -63,23 +68,18 @@ class GenericConfigurationManagerImpl : public ConfigurationManager
CHIP_ERROR GetVendorId(uint16_t & vendorId) override;
CHIP_ERROR GetProductName(char * buf, size_t bufSize) override;
CHIP_ERROR GetProductId(uint16_t & productId) override;
CHIP_ERROR GetHardwareVersionString(char * buf, size_t bufSize) override;
CHIP_ERROR GetHardwareVersion(uint16_t & hardwareVer) override;
CHIP_ERROR StoreHardwareVersion(uint16_t hardwareVer) override;
CHIP_ERROR GetSoftwareVersionString(char * buf, size_t bufSize) override;
CHIP_ERROR GetSoftwareVersion(uint32_t & softwareVer) override;
CHIP_ERROR StoreSoftwareVersion(uint32_t softwareVer) override;
CHIP_ERROR GetSerialNumber(char * buf, size_t bufSize) override;
CHIP_ERROR StoreSerialNumber(const char * serialNum, size_t serialNumLen) override;
CHIP_ERROR GetPrimaryMACAddress(MutableByteSpan buf) override;
CHIP_ERROR GetPrimaryWiFiMACAddress(uint8_t * buf) override;
CHIP_ERROR GetPrimary802154MACAddress(uint8_t * buf) override;
CHIP_ERROR GetManufacturingDate(uint16_t & year, uint8_t & month, uint8_t & dayOfMonth) override;
CHIP_ERROR StoreManufacturingDate(const char * mfgDate, size_t mfgDateLen) override;
#if CHIP_ENABLE_ROTATING_DEVICE_ID && defined(CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID)
CHIP_ERROR GetLifetimeCounter(uint16_t & lifetimeCounter) override;
CHIP_ERROR IncrementLifetimeCounter() override;
CHIP_ERROR GetRotatingDeviceIdUniqueId(MutableByteSpan & uniqueIdSpan) override;
CHIP_ERROR SetRotatingDeviceIdUniqueId(const ByteSpan & uniqueIdSpan) override;
#endif
CHIP_ERROR GetFailSafeArmed(bool & val) override;
Expand Down Expand Up @@ -127,6 +127,10 @@ class GenericConfigurationManagerImpl : public ConfigurationManager
uint8_t mRotatingDeviceIdUniqueId[kRotatingDeviceIDUniqueIDLength] = CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID;
#endif

#if CHIP_USE_TRANSITIONAL_DEVICE_INSTANCE_INFO_PROVIDER
friend LegacyDeviceInstanceInfoProvider<ConfigClass>;
#endif // CHIP_USE_TRANSITIONAL_DEVICE_INSTANCE_INFO_PROVIDER

#if CHIP_USE_TRANSITIONAL_COMMISSIONABLE_DATA_PROVIDER
friend LegacyTemporaryCommissionableDataProvider<ConfigClass>;
#endif // CHIP_USE_TRANSITIONAL_COMMISSIONABLE_DATA_PROVIDER
Expand Down
Loading

0 comments on commit e023667

Please sign in to comment.