Skip to content

Commit

Permalink
[nrf noup] [nrfconnect] Added FactoryDataProxy to facilitate dynamic
Browse files Browse the repository at this point in the history
assignments of FactoryDataProvider concrete types to
to the generic pointer.

Signed-off-by: Marcin Kajor <[email protected]>
  • Loading branch information
markaj-nordic committed Dec 20, 2023
1 parent d95eb1d commit ade930c
Showing 1 changed file with 70 additions and 26 deletions.
96 changes: 70 additions & 26 deletions src/platform/nrfconnect/FactoryDataProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,74 @@ struct ExternalFlashFactoryData
uint8_t mFactoryDataBuffer[PM_FACTORY_DATA_SIZE];
};

class FactoryDataProviderProxy : public chip::Credentials::DeviceAttestationCredentialsProvider,
public CommissionableDataProvider,
public DeviceInstanceInfoProvider
{
public:
virtual CHIP_ERROR Init() { return CHIP_NO_ERROR; };

// ===== Members functions that are platform-specific
virtual CHIP_ERROR GetEnableKey(MutableByteSpan & enableKey) { return CHIP_NO_ERROR; }

/**
* @brief Get the user data in CBOR format as MutableByteSpan
*
* @param userData MutableByteSpan object to obtain all user data in CBOR format
* @returns
* CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND if factory data does not contain user field, or the value cannot be read out.
* CHIP_ERROR_BUFFER_TOO_SMALL if provided MutableByteSpan is too small
*/
virtual CHIP_ERROR GetUserData(MutableByteSpan & userData) { return CHIP_NO_ERROR; }

/**
* @brief Try to find user data key and return its value
*
* @param userKey A key name to be found
* @param buf Buffer to store value of found key
* @param len Length of the buffer. This value will be updated to the actual value if the key is read.
* @returns
* CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND if factory data does not contain user field, or the value cannot be read out.
* CHIP_ERROR_BUFFER_TOO_SMALL if provided buffer length is too small
*/
virtual CHIP_ERROR GetUserKey(const char * userKey, void * buf, size_t & len) { return CHIP_NO_ERROR; }

protected:
// ===== Members functions that implement the DeviceAttestationCredentialsProvider
CHIP_ERROR GetCertificationDeclaration(MutableByteSpan & outBuffer) override { return CHIP_NO_ERROR; };
CHIP_ERROR GetFirmwareInformation(MutableByteSpan & out_firmware_info_buffer) override { return CHIP_NO_ERROR; };
CHIP_ERROR GetDeviceAttestationCert(MutableByteSpan & outBuffer) override { return CHIP_NO_ERROR; };
CHIP_ERROR GetProductAttestationIntermediateCert(MutableByteSpan & outBuffer) override { return CHIP_NO_ERROR; };
CHIP_ERROR SignWithDeviceAttestationKey(const ByteSpan & messageToSign, MutableByteSpan & outSignBuffer) override
{
return CHIP_NO_ERROR;
};

// ===== Members functions that implement the CommissionableDataProvider
CHIP_ERROR GetSetupDiscriminator(uint16_t & setupDiscriminator) override { return CHIP_NO_ERROR; };
CHIP_ERROR SetSetupDiscriminator(uint16_t setupDiscriminator) override { return CHIP_NO_ERROR; };
CHIP_ERROR GetSpake2pIterationCount(uint32_t & iterationCount) override { return CHIP_NO_ERROR; };
CHIP_ERROR GetSpake2pSalt(MutableByteSpan & saltBuf) override { return CHIP_NO_ERROR; };
CHIP_ERROR GetSpake2pVerifier(MutableByteSpan & verifierBuf, size_t & verifierLen) override;
CHIP_ERROR GetSetupPasscode(uint32_t & setupPasscode) override { return CHIP_NO_ERROR; };
CHIP_ERROR SetSetupPasscode(uint32_t setupPasscode) override { return CHIP_NO_ERROR; };

// ===== Members functions that implement the DeviceInstanceInfoProvider
CHIP_ERROR GetVendorName(char * buf, size_t bufSize) override { return CHIP_NO_ERROR; };
CHIP_ERROR GetVendorId(uint16_t & vendorId) override { return CHIP_NO_ERROR; };
CHIP_ERROR GetProductName(char * buf, size_t bufSize) override { return CHIP_NO_ERROR; };
CHIP_ERROR GetProductId(uint16_t & productId) override { return CHIP_NO_ERROR; };
CHIP_ERROR GetPartNumber(char * buf, size_t bufSize) override { return CHIP_NO_ERROR; };
CHIP_ERROR GetProductURL(char * buf, size_t bufSize) override { return CHIP_NO_ERROR; };
CHIP_ERROR GetProductLabel(char * buf, size_t bufSize) override { return CHIP_NO_ERROR; };
CHIP_ERROR GetSerialNumber(char * buf, size_t bufSize) override { return CHIP_NO_ERROR; };
CHIP_ERROR GetManufacturingDate(uint16_t & year, uint8_t & month, uint8_t & day) override { return CHIP_NO_ERROR; };
CHIP_ERROR GetHardwareVersion(uint16_t & hardwareVersion) override { return CHIP_NO_ERROR; };
CHIP_ERROR GetHardwareVersionString(char * buf, size_t bufSize) override { return CHIP_NO_ERROR; };
CHIP_ERROR GetRotatingDeviceIdUniqueId(MutableByteSpan & uniqueIdSpan) override { return CHIP_NO_ERROR; };
};
template <class FlashFactoryData>
class FactoryDataProvider : public chip::Credentials::DeviceAttestationCredentialsProvider,
public CommissionableDataProvider,
public DeviceInstanceInfoProvider
class FactoryDataProvider : public FactoryDataProviderProxy
{
public:
CHIP_ERROR Init();
Expand Down Expand Up @@ -143,29 +207,9 @@ class FactoryDataProvider : public chip::Credentials::DeviceAttestationCredentia
CHIP_ERROR GetProductPrimaryColor(app::Clusters::BasicInformation::ColorEnum * primaryColor) override;

// ===== Members functions that are platform-specific
CHIP_ERROR GetEnableKey(MutableByteSpan & enableKey);

/**
* @brief Get the user data in CBOR format as MutableByteSpan
*
* @param userData MutableByteSpan object to obtain all user data in CBOR format
* @returns
* CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND if factory data does not contain user field, or the value cannot be read out.
* CHIP_ERROR_BUFFER_TOO_SMALL if provided MutableByteSpan is too small
*/
CHIP_ERROR GetUserData(MutableByteSpan & userData);

/**
* @brief Try to find user data key and return its value
*
* @param userKey A key name to be found
* @param buf Buffer to store value of found key
* @param len Length of the buffer. This value will be updated to the actual value if the key is read.
* @returns
* CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND if factory data does not contain user field, or the value cannot be read out.
* CHIP_ERROR_BUFFER_TOO_SMALL if provided buffer length is too small
*/
CHIP_ERROR GetUserKey(const char * userKey, void * buf, size_t & len);
CHIP_ERROR GetEnableKey(MutableByteSpan & enableKey) override;
CHIP_ERROR GetUserData(MutableByteSpan & userData) override;
CHIP_ERROR GetUserKey(const char * userKey, void * buf, size_t & len) override;

private:
static constexpr uint16_t kFactoryDataPartitionSize = PM_FACTORY_DATA_SIZE;
Expand Down

0 comments on commit ade930c

Please sign in to comment.