From 1088936332fc4e8afffbdfe89e20fd1932d88478 Mon Sep 17 00:00:00 2001 From: Maciej Bojczuk Date: Wed, 15 Feb 2023 18:00:45 +0200 Subject: [PATCH] [Telink] Read certificate declaration from storage (#25072) When CONFIG_CHIP_CERTIFICATION_DECLARATION_STORAGE is enabled read certificate declaration (CD) from storage instead of using default. Signed-off-by: Maciej Bojczuk --- examples/lighting-app/telink/prj.conf | 1 + src/platform/telink/FactoryDataParser.c | 4 ++++ src/platform/telink/FactoryDataParser.h | 1 + src/platform/telink/FactoryDataProvider.cpp | 15 +++++++-------- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/examples/lighting-app/telink/prj.conf b/examples/lighting-app/telink/prj.conf index 8e02f9a2fa6692..cebd1abc50fdce 100644 --- a/examples/lighting-app/telink/prj.conf +++ b/examples/lighting-app/telink/prj.conf @@ -62,6 +62,7 @@ CONFIG_CHIP_LIB_SHELL=n CONFIG_CHIP_FACTORY_DATA=n CONFIG_CHIP_FACTORY_DATA_BUILD=n CONFIG_CHIP_FACTORY_DATA_MERGE_WITH_FIRMWARE=n +CONFIG_CHIP_CERTIFICATION_DECLARATION_STORAGE=n # Enable Button IRQ mode. The poling mode is used by default. CONFIG_CHIP_BUTTON_MANAGER_IRQ_MODE=n diff --git a/src/platform/telink/FactoryDataParser.c b/src/platform/telink/FactoryDataParser.c index 87107753a71711..12260631262342 100644 --- a/src/platform/telink/FactoryDataParser.c +++ b/src/platform/telink/FactoryDataParser.c @@ -169,6 +169,10 @@ bool ParseFactoryData(uint8_t * buffer, uint16_t bufferSize, struct FactoryData { res = res && zcbor_bstr_decode(states, (struct zcbor_string *) &factoryData->user); } + else if (strncmp("cert_dclrn", (const char *) currentString.value, currentString.len) == 0) + { + res = res && zcbor_bstr_decode(states, (struct zcbor_string *) &factoryData->certificate_declaration); + } else { res = res && zcbor_any_skip(states, NULL); diff --git a/src/platform/telink/FactoryDataParser.h b/src/platform/telink/FactoryDataParser.h index 9c87589560d469..de8f7c72da838d 100644 --- a/src/platform/telink/FactoryDataParser.h +++ b/src/platform/telink/FactoryDataParser.h @@ -58,6 +58,7 @@ struct FactoryData uint32_t passcode; struct FactoryDataString enable_key; struct FactoryDataString user; + struct FactoryDataString certificate_declaration; bool vendorIdPresent; bool productIdPresent; diff --git a/src/platform/telink/FactoryDataProvider.cpp b/src/platform/telink/FactoryDataProvider.cpp index 77bacdca379dd8..806cdb2cfe9391 100644 --- a/src/platform/telink/FactoryDataProvider.cpp +++ b/src/platform/telink/FactoryDataProvider.cpp @@ -102,15 +102,14 @@ template CHIP_ERROR FactoryDataProvider::GetCertificationDeclaration(MutableByteSpan & outBuffer) { #if CONFIG_CHIP_CERTIFICATION_DECLARATION_STORAGE - size_t cdLen = 0; + ReturnErrorCodeIf(outBuffer.size() < mFactoryData.certificate_declaration.len, CHIP_ERROR_BUFFER_TOO_SMALL); + ReturnErrorCodeIf(!mFactoryData.certificate_declaration.data, CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND); - if (Internal::ZephyrConfig::ReadConfigValueBin(Internal::ZephyrConfig::kConfigKey_CertificationDeclaration, - reinterpret_cast(outBuffer.data()), outBuffer.size(), - cdLen) == CHIP_NO_ERROR) - { - outBuffer.reduce_size(cdLen); - return CHIP_NO_ERROR; - } + memcpy(outBuffer.data(), mFactoryData.certificate_declaration.data, mFactoryData.certificate_declaration.len); + + outBuffer.reduce_size(mFactoryData.certificate_declaration.len); + + return CHIP_NO_ERROR; #endif constexpr uint8_t kCdForAllExamples[] = CHIP_DEVICE_CONFIG_CERTIFICATION_DECLARATION;