From fa1abc93493d5dc8f3210b557dd277bb3ea7e750 Mon Sep 17 00:00:00 2001 From: Shubham Patil Date: Tue, 28 Jun 2022 19:34:19 +0530 Subject: [PATCH] [ESP32] Implement few methods from DeviceInstanceInfoProvider (#19995) * [ESP32] Implement few methods from DeviceInstanceInfoProvider GenericDeviceInstanceInfoProvider do not read all the device instance info from the flash. This overrides few methods to read VendorId, VendorName, ProductId, ProductName, and HardwareVersionString from the flash. * Support ESP32FactoryDataProvider in all apps * Move invalid string length check to ESP32Config --- config/esp32/components/chip/CMakeLists.txt | 1 + config/esp32/components/chip/Kconfig | 10 +++++ examples/all-clusters-app/esp32/main/main.cpp | 21 ++++++++++ .../esp32/main/main.cpp | 21 ++++++++++ examples/bridge-app/esp32/main/main.cpp | 23 ++++++++++ examples/light-switch-app/esp32/main/main.cpp | 24 +++++++++++ examples/lighting-app/esp32/main/main.cpp | 20 ++++++--- examples/lock-app/esp32/main/main.cpp | 23 ++++++++++ examples/ota-provider-app/esp32/main/main.cpp | 21 ++++++++++ .../ota-requestor-app/esp32/main/main.cpp | 21 ++++++++++ .../platform/esp32/common/Esp32AppServer.cpp | 16 ------- .../esp32/main/main.cpp | 23 ++++++++++ src/platform/ESP32/BUILD.gn | 15 ++++++- src/platform/ESP32/CHIPDevicePlatformConfig.h | 1 + src/platform/ESP32/ESP32Config.cpp | 6 +++ src/platform/ESP32/ESP32Config.h | 5 +++ .../ESP32/ESP32FactoryDataProvider.cpp | 42 +++++++++++++++++++ src/platform/ESP32/ESP32FactoryDataProvider.h | 38 +++++++++++------ 18 files changed, 296 insertions(+), 35 deletions(-) diff --git a/config/esp32/components/chip/CMakeLists.txt b/config/esp32/components/chip/CMakeLists.txt index 6744546453060f..5b2b0812818ddd 100644 --- a/config/esp32/components/chip/CMakeLists.txt +++ b/config/esp32/components/chip/CMakeLists.txt @@ -157,6 +157,7 @@ endif() if (CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER) chip_gn_arg_append("chip_use_transitional_commissionable_data_provider" "false") + chip_gn_arg_append("chip_use_factory_data_provider" "true") endif() set(args_gn_input "${CMAKE_CURRENT_BINARY_DIR}/args.gn.in") diff --git a/config/esp32/components/chip/Kconfig b/config/esp32/components/chip/Kconfig index 35e926e781340c..af902c28af2019 100644 --- a/config/esp32/components/chip/Kconfig +++ b/config/esp32/components/chip/Kconfig @@ -635,6 +635,16 @@ menu "CHIP Device Layer" nvs partition. Factory partition can be configured using CONFIG_CHIP_FACTORY_NAMESPACE_PARTITION_LABEL option, default is "nvs". + config ENABLE_ESP32_DEVICE_INSTANCE_INFO_PROVIDER + depends on ENABLE_ESP32_FACTORY_DATA_PROVIDER + bool "Use ESP32 Device Instance Info Provider" + default n + help + Use ESP32 Device Instance Info Provider to get device instance info from factory partition. + Details like Vendor Name, Vendor Id, Product Name, Product Id, Serial Number, Manufacturing Date, + Hardware Version, Hardware Version String, and Rotating Device Id UniqueId will be read from factory + partition. + endmenu diff --git a/examples/all-clusters-app/esp32/main/main.cpp b/examples/all-clusters-app/esp32/main/main.cpp index 80e0b9ecf15edb..2e4ec3a9ef87b0 100644 --- a/examples/all-clusters-app/esp32/main/main.cpp +++ b/examples/all-clusters-app/esp32/main/main.cpp @@ -35,6 +35,8 @@ #include "platform/PlatformManager.h" #include "shell_extension/launch.h" #include +#include +#include #include #include @@ -53,9 +55,14 @@ #include #endif +#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER +#include +#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + using namespace ::chip; using namespace ::chip::Shell; using namespace ::chip::DeviceManager; +using namespace ::chip::Credentials; // Used to indicate that an IP address has been added to the QRCode #define EXAMPLE_VENDOR_TAG_IP 1 @@ -83,6 +90,10 @@ AppCallbacks sCallbacks; constexpr EndpointId kNetworkCommissioningEndpointSecondary = 0xFFFE; +#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER +chip::DeviceLayer::ESP32FactoryDataProvider sFactoryDataProvider; +#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + } // namespace static void InitServer(intptr_t context) @@ -135,6 +146,16 @@ extern "C" void app_main() return; } +#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + SetCommissionableDataProvider(&sFactoryDataProvider); + SetDeviceAttestationCredentialsProvider(&sFactoryDataProvider); +#if CONFIG_ENABLE_ESP32_DEVICE_INSTANCE_INFO_PROVIDER + SetDeviceInstanceInfoProvider(&sFactoryDataProvider); +#endif +#else + SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); +#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + ESP_LOGI(TAG, "------------------------Starting App Task---------------------------"); error = GetAppTask().StartAppTask(); if (error != CHIP_NO_ERROR) diff --git a/examples/all-clusters-minimal-app/esp32/main/main.cpp b/examples/all-clusters-minimal-app/esp32/main/main.cpp index 483f00b41cf678..118a98678f2abb 100644 --- a/examples/all-clusters-minimal-app/esp32/main/main.cpp +++ b/examples/all-clusters-minimal-app/esp32/main/main.cpp @@ -40,6 +40,8 @@ #include #include #include +#include +#include #if CONFIG_HAVE_DISPLAY #include "DeviceWithDisplay.h" @@ -53,8 +55,13 @@ #include #endif +#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER +#include +#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + using namespace ::chip; using namespace ::chip::Shell; +using namespace ::chip::Credentials; using namespace ::chip::DeviceManager; // Used to indicate that an IP address has been added to the QRCode @@ -83,6 +90,10 @@ AppCallbacks sCallbacks; constexpr EndpointId kNetworkCommissioningEndpointSecondary = 0xFFFE; +#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER +chip::DeviceLayer::ESP32FactoryDataProvider sFactoryDataProvider; +#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + } // namespace static void InitServer(intptr_t context) @@ -135,6 +146,16 @@ extern "C" void app_main() return; } +#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + SetCommissionableDataProvider(&sFactoryDataProvider); + SetDeviceAttestationCredentialsProvider(&sFactoryDataProvider); +#if CONFIG_ENABLE_ESP32_DEVICE_INSTANCE_INFO_PROVIDER + SetDeviceInstanceInfoProvider(&sFactoryDataProvider); +#endif +#else + SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); +#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + ESP_LOGI(TAG, "------------------------Starting App Task---------------------------"); error = GetAppTask().StartAppTask(); if (error != CHIP_NO_ERROR) diff --git a/examples/bridge-app/esp32/main/main.cpp b/examples/bridge-app/esp32/main/main.cpp index b09f5d7ddaf950..3b94d192d216e1 100644 --- a/examples/bridge-app/esp32/main/main.cpp +++ b/examples/bridge-app/esp32/main/main.cpp @@ -26,17 +26,30 @@ #include #include #include +#include +#include #include #include #include #include +#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER +#include +#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + +namespace { +#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER +chip::DeviceLayer::ESP32FactoryDataProvider sFactoryDataProvider; +#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER +} // namespace + const char * TAG = "bridge-app"; using namespace ::chip; using namespace ::chip::DeviceManager; using namespace ::chip::Platform; +using namespace ::chip::Credentials; static AppDeviceCallbacks AppCallback; @@ -397,5 +410,15 @@ extern "C" void app_main() return; } +#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + SetCommissionableDataProvider(&sFactoryDataProvider); + SetDeviceAttestationCredentialsProvider(&sFactoryDataProvider); +#if CONFIG_ENABLE_ESP32_DEVICE_INSTANCE_INFO_PROVIDER + SetDeviceInstanceInfoProvider(&sFactoryDataProvider); +#endif +#else + SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); +#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + chip::DeviceLayer::PlatformMgr().ScheduleWork(InitServer, reinterpret_cast(nullptr)); } diff --git a/examples/light-switch-app/esp32/main/main.cpp b/examples/light-switch-app/esp32/main/main.cpp index 0d76460e52c854..4edd3c31b9fe6f 100644 --- a/examples/light-switch-app/esp32/main/main.cpp +++ b/examples/light-switch-app/esp32/main/main.cpp @@ -31,8 +31,21 @@ #include "shell_extension/launch.h" #include +#include +#include + +#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER +#include +#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + +namespace { +#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER +chip::DeviceLayer::ESP32FactoryDataProvider sFactoryDataProvider; +#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER +} // namespace using namespace ::chip; +using namespace ::chip::Credentials; using namespace ::chip::DeviceManager; static const char * TAG = "light-switch-app"; @@ -75,6 +88,17 @@ extern "C" void app_main() ESP_LOGE(TAG, "device.Init() failed: %s", ErrorStr(error)); return; } + +#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + SetCommissionableDataProvider(&sFactoryDataProvider); + SetDeviceAttestationCredentialsProvider(&sFactoryDataProvider); +#if CONFIG_ENABLE_ESP32_DEVICE_INSTANCE_INFO_PROVIDER + SetDeviceInstanceInfoProvider(&sFactoryDataProvider); +#endif +#else + SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); +#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + #if CHIP_DEVICE_CONFIG_ENABLE_THREAD if (ThreadStackMgr().InitThreadStack() != CHIP_NO_ERROR) { diff --git a/examples/lighting-app/esp32/main/main.cpp b/examples/lighting-app/esp32/main/main.cpp index 7e882d432006aa..682c4168068bd6 100644 --- a/examples/lighting-app/esp32/main/main.cpp +++ b/examples/lighting-app/esp32/main/main.cpp @@ -30,12 +30,15 @@ #include "shell_extension/launch.h" #include #include +#include +#include #if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER #include #endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER using namespace ::chip; +using namespace ::chip::Credentials; using namespace ::chip::DeviceManager; using namespace ::chip::DeviceLayer; @@ -45,7 +48,7 @@ static AppDeviceCallbacks EchoCallbacks; namespace { #if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER -ESP32FactoryDataProvider sFactoryDataProvider; +chip::DeviceLayer::ESP32FactoryDataProvider sFactoryDataProvider; #endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER } // namespace @@ -71,10 +74,6 @@ extern "C" void app_main() ESP_LOGI(TAG, "chip-esp32-light-example starting"); ESP_LOGI(TAG, "=================================================="); -#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER - SetCommissionableDataProvider(&sFactoryDataProvider); -#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER - #if CONFIG_ENABLE_CHIP_SHELL chip::LaunchShell(); #endif @@ -86,6 +85,17 @@ extern "C" void app_main() ESP_LOGE(TAG, "device.Init() failed: %s", ErrorStr(error)); return; } + +#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + SetCommissionableDataProvider(&sFactoryDataProvider); + SetDeviceAttestationCredentialsProvider(&sFactoryDataProvider); +#if CONFIG_ENABLE_ESP32_DEVICE_INSTANCE_INFO_PROVIDER + SetDeviceInstanceInfoProvider(&sFactoryDataProvider); +#endif +#else + SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); +#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + #if CHIP_DEVICE_CONFIG_ENABLE_THREAD if (ThreadStackMgr().InitThreadStack() != CHIP_NO_ERROR) { diff --git a/examples/lock-app/esp32/main/main.cpp b/examples/lock-app/esp32/main/main.cpp index 9ef90a02e1deab..9a325e59896a92 100644 --- a/examples/lock-app/esp32/main/main.cpp +++ b/examples/lock-app/esp32/main/main.cpp @@ -29,6 +29,8 @@ #include "shell_extension/launch.h" #include #include +#include +#include #include #include @@ -42,8 +44,19 @@ #include "Rpc.h" #endif +#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER +#include +#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + +namespace { +#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER +chip::DeviceLayer::ESP32FactoryDataProvider sFactoryDataProvider; +#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER +} // namespace + using namespace ::chip; using namespace ::chip::DeviceManager; +using namespace ::chip::Credentials; static const char * TAG = "lock-app"; @@ -92,5 +105,15 @@ extern "C" void app_main() return; } +#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + SetCommissionableDataProvider(&sFactoryDataProvider); + SetDeviceAttestationCredentialsProvider(&sFactoryDataProvider); +#if CONFIG_ENABLE_ESP32_DEVICE_INSTANCE_INFO_PROVIDER + SetDeviceInstanceInfoProvider(&sFactoryDataProvider); +#endif +#else + SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); +#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + chip::DeviceLayer::PlatformMgr().ScheduleWork(InitServer, reinterpret_cast(nullptr)); } diff --git a/examples/ota-provider-app/esp32/main/main.cpp b/examples/ota-provider-app/esp32/main/main.cpp index e1c7b760bc2c4a..f20425af23e3d0 100644 --- a/examples/ota-provider-app/esp32/main/main.cpp +++ b/examples/ota-provider-app/esp32/main/main.cpp @@ -23,6 +23,8 @@ #include #include #include +#include +#include #include #include @@ -32,12 +34,17 @@ #include #include +#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER +#include +#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + using chip::Callback::Callback; using namespace chip; using namespace chip::Shell; using namespace chip::System; using namespace chip::DeviceManager; using namespace chip::app::Clusters::OtaSoftwareUpdateProvider; +using namespace ::chip::Credentials; CHIP_ERROR OnBlockQuery(void * context, chip::System::PacketBufferHandle & blockBuf, size_t & size, bool & isEof, uint32_t offset); void OnTransferComplete(void * context); @@ -130,6 +137,10 @@ static void InitServer(intptr_t context) otaProviderCommands.Register(); } +#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER +chip::DeviceLayer::ESP32FactoryDataProvider sFactoryDataProvider; +#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + } // namespace CHIP_ERROR OnBlockQuery(void * context, chip::System::PacketBufferHandle & blockBuf, size_t & size, bool & isEof, uint32_t offset) @@ -229,5 +240,15 @@ extern "C" void app_main() return; } +#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + SetCommissionableDataProvider(&sFactoryDataProvider); + SetDeviceAttestationCredentialsProvider(&sFactoryDataProvider); +#if CONFIG_ENABLE_ESP32_DEVICE_INSTANCE_INFO_PROVIDER + SetDeviceInstanceInfoProvider(&sFactoryDataProvider); +#endif +#else + SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); +#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + chip::DeviceLayer::PlatformMgr().ScheduleWork(InitServer, reinterpret_cast(nullptr)); } diff --git a/examples/ota-requestor-app/esp32/main/main.cpp b/examples/ota-requestor-app/esp32/main/main.cpp index 7935b7003281f1..e9a7f0b30becfa 100644 --- a/examples/ota-requestor-app/esp32/main/main.cpp +++ b/examples/ota-requestor-app/esp32/main/main.cpp @@ -29,6 +29,8 @@ #include "nvs_flash.h" #include #include +#include +#include #include #include #include @@ -39,10 +41,15 @@ #include "Rpc.h" #endif +#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER +#include +#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + using namespace ::chip; using namespace ::chip::System; using namespace ::chip::DeviceManager; using namespace chip::Shell; +using namespace ::chip::Credentials; namespace { const char * TAG = "ota-requester-app"; @@ -58,6 +65,10 @@ static void InitServer(intptr_t context) emberAfEndpointEnableDisable(kNetworkCommissioningEndpointSecondary, false); } +#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER +chip::DeviceLayer::ESP32FactoryDataProvider sFactoryDataProvider; +#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + } // namespace extern "C" void app_main() @@ -101,5 +112,15 @@ extern "C" void app_main() return; } +#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + SetCommissionableDataProvider(&sFactoryDataProvider); + SetDeviceAttestationCredentialsProvider(&sFactoryDataProvider); +#if CONFIG_ENABLE_ESP32_DEVICE_INSTANCE_INFO_PROVIDER + SetDeviceInstanceInfoProvider(&sFactoryDataProvider); +#endif +#else + SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); +#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + chip::DeviceLayer::PlatformMgr().ScheduleWork(InitServer, reinterpret_cast(nullptr)); } diff --git a/examples/platform/esp32/common/Esp32AppServer.cpp b/examples/platform/esp32/common/Esp32AppServer.cpp index ee3dbdd31df218..2edd7b8dc167b4 100644 --- a/examples/platform/esp32/common/Esp32AppServer.cpp +++ b/examples/platform/esp32/common/Esp32AppServer.cpp @@ -21,14 +21,8 @@ #include #include #include -#include -#include #include -#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER -#include -#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER - using namespace chip; using namespace chip::Credentials; using namespace chip::DeviceLayer; @@ -39,9 +33,6 @@ app::Clusters::NetworkCommissioning::Instance sWiFiNetworkCommissioningInstance(0 /* Endpoint Id */, &(NetworkCommissioning::ESPWiFiDriver::GetInstance())); #endif -#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER -ESP32FactoryDataProvider sFactoryDataProvider; -#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER } // namespace void Esp32AppServer::Init(AppDelegate * sAppDelegate) @@ -55,13 +46,6 @@ void Esp32AppServer::Init(AppDelegate * sAppDelegate) } chip::Server::GetInstance().Init(initParams); - // Initialize device attestation config -#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER - SetDeviceAttestationCredentialsProvider(&sFactoryDataProvider); -#else - SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); -#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER - #if CHIP_DEVICE_CONFIG_ENABLE_WIFI sWiFiNetworkCommissioningInstance.Init(); #endif diff --git a/examples/temperature-measurement-app/esp32/main/main.cpp b/examples/temperature-measurement-app/esp32/main/main.cpp index 1d8ec2687ac23b..983ff895b03d91 100644 --- a/examples/temperature-measurement-app/esp32/main/main.cpp +++ b/examples/temperature-measurement-app/esp32/main/main.cpp @@ -28,6 +28,8 @@ #include #include #include +#include +#include #include #include @@ -40,8 +42,19 @@ #include "Rpc.h" #endif +#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER +#include +#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + +namespace { +#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER +chip::DeviceLayer::ESP32FactoryDataProvider sFactoryDataProvider; +#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER +} // namespace + using namespace ::chip; using namespace ::chip::DeviceManager; +using namespace ::chip::Credentials; const char * TAG = "temperature-measurement-app"; @@ -89,5 +102,15 @@ extern "C" void app_main() return; } +#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + SetCommissionableDataProvider(&sFactoryDataProvider); + SetDeviceAttestationCredentialsProvider(&sFactoryDataProvider); +#if CONFIG_ENABLE_ESP32_DEVICE_INSTANCE_INFO_PROVIDER + SetDeviceInstanceInfoProvider(&sFactoryDataProvider); +#endif +#else + SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); +#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER + chip::DeviceLayer::PlatformMgr().ScheduleWork(InitServer, reinterpret_cast(nullptr)); } diff --git a/src/platform/ESP32/BUILD.gn b/src/platform/ESP32/BUILD.gn index 33b5ccb306013e..822ffa58efcd5c 100644 --- a/src/platform/ESP32/BUILD.gn +++ b/src/platform/ESP32/BUILD.gn @@ -18,6 +18,12 @@ import("${chip_root}/src/platform/device.gni") assert(chip_device_platform == "esp32") +declare_args() { + # By default use default/example implementation of CommissionableDataProvider, + # DeviceAttestationCredentialsProvider and DeviceInstanceInforProvider + chip_use_factory_data_provider = false +} + static_library("ESP32") { sources = [ "../SingletonConfigurationManager.cpp", @@ -36,8 +42,6 @@ static_library("ESP32") { "DiagnosticDataProviderImpl.h", "ESP32Config.cpp", "ESP32Config.h", - "ESP32FactoryDataProvider.cpp", - "ESP32FactoryDataProvider.h", "ESP32Utils.cpp", "ESP32Utils.h", "KeyValueStoreManagerImpl.cpp", @@ -98,4 +102,11 @@ static_library("ESP32") { ] configs -= [ "${chip_root}/build/config/compiler:warnings_default" ] } + + if (chip_use_factory_data_provider) { + sources += [ + "ESP32FactoryDataProvider.cpp", + "ESP32FactoryDataProvider.h", + ] + } } diff --git a/src/platform/ESP32/CHIPDevicePlatformConfig.h b/src/platform/ESP32/CHIPDevicePlatformConfig.h index 9770775158fccc..96368be8a18bbf 100644 --- a/src/platform/ESP32/CHIPDevicePlatformConfig.h +++ b/src/platform/ESP32/CHIPDevicePlatformConfig.h @@ -98,3 +98,4 @@ #define CHIP_DEVICE_CONFIG_CHIP_FACTORY_NAMESPACE_PARTITION CONFIG_CHIP_FACTORY_NAMESPACE_PARTITION_LABEL #define CHIP_DEVICE_CONFIG_CHIP_CONFIG_NAMESPACE_PARTITION CONFIG_CHIP_CONFIG_NAMESPACE_PARTITION_LABEL #define CHIP_DEVICE_CONFIG_CHIP_COUNTERS_NAMESPACE_PARTITION CONFIG_CHIP_COUNTERS_NAMESPACE_PARTITION_LABEL +#define CHIP_DEVICE_CONFIG_ENABLE_DEVICE_INSTANCE_INFO_PROVIDER CONFIG_ENABLE_ESP32_DEVICE_INSTANCE_INFO_PROVIDER diff --git a/src/platform/ESP32/ESP32Config.cpp b/src/platform/ESP32/ESP32Config.cpp index 8152c89092b052..2097f918764b9d 100644 --- a/src/platform/ESP32/ESP32Config.cpp +++ b/src/platform/ESP32/ESP32Config.cpp @@ -56,6 +56,7 @@ const ESP32Config::Key ESP32Config::kConfigKey_MfrDeviceCert = { kConfig const ESP32Config::Key ESP32Config::kConfigKey_MfrDeviceICACerts = { kConfigNamespace_ChipFactory, "device-ca-certs" }; const ESP32Config::Key ESP32Config::kConfigKey_MfrDevicePrivateKey = { kConfigNamespace_ChipFactory, "device-key" }; const ESP32Config::Key ESP32Config::kConfigKey_HardwareVersion = { kConfigNamespace_ChipFactory, "hardware-ver" }; +const ESP32Config::Key ESP32Config::kConfigKey_HardwareVersionString = { kConfigNamespace_ChipFactory, "hw-ver-str" }; const ESP32Config::Key ESP32Config::kConfigKey_ManufacturingDate = { kConfigNamespace_ChipFactory, "mfg-date" }; const ESP32Config::Key ESP32Config::kConfigKey_SetupPinCode = { kConfigNamespace_ChipFactory, "pin-code" }; const ESP32Config::Key ESP32Config::kConfigKey_SetupDiscriminator = { kConfigNamespace_ChipFactory, "discriminator" }; @@ -67,6 +68,10 @@ const ESP32Config::Key ESP32Config::kConfigKey_DACPrivateKey = { kConfig const ESP32Config::Key ESP32Config::kConfigKey_DACPublicKey = { kConfigNamespace_ChipFactory, "dac-pub-key" }; const ESP32Config::Key ESP32Config::kConfigKey_PAICert = { kConfigNamespace_ChipFactory, "pai-cert" }; const ESP32Config::Key ESP32Config::kConfigKey_CertDeclaration = { kConfigNamespace_ChipFactory, "cert-dclrn" }; +const ESP32Config::Key ESP32Config::kConfigKey_VendorId = { kConfigNamespace_ChipFactory, "vendor-id" }; +const ESP32Config::Key ESP32Config::kConfigKey_VendorName = { kConfigNamespace_ChipFactory, "vendor-name" }; +const ESP32Config::Key ESP32Config::kConfigKey_ProductId = { kConfigNamespace_ChipFactory, "product-id" }; +const ESP32Config::Key ESP32Config::kConfigKey_ProductName = { kConfigNamespace_ChipFactory, "product-name" }; // Keys stored in the chip-config namespace const ESP32Config::Key ESP32Config::kConfigKey_ServiceConfig = { kConfigNamespace_ChipConfig, "service-config" }; @@ -191,6 +196,7 @@ CHIP_ERROR ESP32Config::ReadConfigValueStr(Key key, char * buf, size_t bufSize, { return CHIP_ERROR_BUFFER_TOO_SMALL; } + ReturnErrorCodeIf(buf[outLen - 1] != 0, CHIP_ERROR_INVALID_STRING_LENGTH); ReturnMappedErrorOnFailure(err); outLen -= 1; // Don't count trailing nul. diff --git a/src/platform/ESP32/ESP32Config.h b/src/platform/ESP32/ESP32Config.h index 01d9609aaf2e70..fd39dfc91a8c8e 100644 --- a/src/platform/ESP32/ESP32Config.h +++ b/src/platform/ESP32/ESP32Config.h @@ -58,6 +58,7 @@ class ESP32Config static const Key kConfigKey_MfrDeviceICACerts; static const Key kConfigKey_MfrDevicePrivateKey; static const Key kConfigKey_HardwareVersion; + static const Key kConfigKey_HardwareVersionString; static const Key kConfigKey_ManufacturingDate; static const Key kConfigKey_SetupPinCode; static const Key kConfigKey_ServiceConfig; @@ -77,6 +78,10 @@ class ESP32Config static const Key kConfigKey_DACPublicKey; static const Key kConfigKey_PAICert; static const Key kConfigKey_CertDeclaration; + static const Key kConfigKey_VendorId; + static const Key kConfigKey_VendorName; + static const Key kConfigKey_ProductId; + static const Key kConfigKey_ProductName; // CHIP Counter keys static const Key kCounterKey_RebootCount; diff --git a/src/platform/ESP32/ESP32FactoryDataProvider.cpp b/src/platform/ESP32/ESP32FactoryDataProvider.cpp index f73a6f8eaed801..4839e19867d9d1 100644 --- a/src/platform/ESP32/ESP32FactoryDataProvider.cpp +++ b/src/platform/ESP32/ESP32FactoryDataProvider.cpp @@ -154,5 +154,47 @@ CHIP_ERROR ESP32FactoryDataProvider::SignWithDeviceAttestationKey(const ByteSpan return CopySpanToMutableSpan(ByteSpan{ signature.ConstBytes(), signature.Length() }, outSignBuffer); } +#if CHIP_DEVICE_CONFIG_ENABLE_DEVICE_INSTANCE_INFO_PROVIDER +CHIP_ERROR ESP32FactoryDataProvider::GetVendorName(char * buf, size_t bufSize) +{ + size_t vendorNameLen = 0; // without counting null-terminator + return ESP32Config::ReadConfigValueStr(ESP32Config::kConfigKey_VendorName, buf, bufSize, vendorNameLen); +} + +CHIP_ERROR ESP32FactoryDataProvider::GetVendorId(uint16_t & vendorId) +{ + ChipError err = CHIP_NO_ERROR; + uint32_t valInt = 0; + + err = ESP32Config::ReadConfigValue(ESP32Config::kConfigKey_VendorId, valInt); + ReturnErrorOnFailure(err); + vendorId = static_cast(valInt); + return err; +} + +CHIP_ERROR ESP32FactoryDataProvider::GetProductName(char * buf, size_t bufSize) +{ + size_t productNameLen = 0; // without counting null-terminator + return ESP32Config::ReadConfigValueStr(ESP32Config::kConfigKey_ProductName, buf, bufSize, productNameLen); +} + +CHIP_ERROR ESP32FactoryDataProvider::GetProductId(uint16_t & productId) +{ + ChipError err = CHIP_NO_ERROR; + uint32_t valInt = 0; + + err = ESP32Config::ReadConfigValue(ESP32Config::kConfigKey_ProductId, valInt); + ReturnErrorOnFailure(err); + productId = static_cast(valInt); + return err; +} + +CHIP_ERROR ESP32FactoryDataProvider::GetHardwareVersionString(char * buf, size_t bufSize) +{ + size_t hardwareVersionStringLen = 0; // without counting null-terminator + return ESP32Config::ReadConfigValueStr(ESP32Config::kConfigKey_HardwareVersionString, buf, bufSize, hardwareVersionStringLen); +} +#endif // CHIP_DEVICE_CONFIG_ENABLE_DEVICE_INSTANCE_INFO_PROVIDER + } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/ESP32/ESP32FactoryDataProvider.h b/src/platform/ESP32/ESP32FactoryDataProvider.h index 76ef623fd061be..67cf2740c75e41 100644 --- a/src/platform/ESP32/ESP32FactoryDataProvider.h +++ b/src/platform/ESP32/ESP32FactoryDataProvider.h @@ -18,42 +18,56 @@ #include #include +#include namespace chip { namespace DeviceLayer { /** - * @brief This class provides Commissionable data and Device Attestation Credentials. + * @brief This class provides Commissionable data, Device Attestation Credentials, + * and Device Instance Info. */ -class ESP32FactoryDataProvider : public CommissionableDataProvider, public chip::Credentials::DeviceAttestationCredentialsProvider +class ESP32FactoryDataProvider : public CommissionableDataProvider, + public Credentials::DeviceAttestationCredentialsProvider +#if CHIP_DEVICE_CONFIG_ENABLE_DEVICE_INSTANCE_INFO_PROVIDER + , + public Internal::GenericDeviceInstanceInfoProvider +#endif // CHIP_DEVICE_CONFIG_ENABLE_DEVICE_INSTANCE_INFO_PROVIDER { public: + ESP32FactoryDataProvider() : + CommissionableDataProvider(), Credentials::DeviceAttestationCredentialsProvider() +#if CHIP_DEVICE_CONFIG_ENABLE_DEVICE_INSTANCE_INFO_PROVIDER + , + Internal::GenericDeviceInstanceInfoProvider(ConfigurationManagerImpl::GetDefaultInstance()) +#endif // CHIP_DEVICE_CONFIG_ENABLE_DEVICE_INSTANCE_INFO_PROVIDER + {} + // ===== Members functions that implement the CommissionableDataProvider CHIP_ERROR GetSetupDiscriminator(uint16_t & setupDiscriminator) override; - CHIP_ERROR SetSetupDiscriminator(uint16_t setupDiscriminator) override { return CHIP_ERROR_NOT_IMPLEMENTED; } - CHIP_ERROR GetSpake2pIterationCount(uint32_t & iterationCount) override; - CHIP_ERROR GetSpake2pSalt(MutableByteSpan & saltBuf) override; - CHIP_ERROR GetSpake2pVerifier(MutableByteSpan & verifierBuf, size_t & verifierLen) override; - CHIP_ERROR GetSetupPasscode(uint32_t & setupPasscode) override { return CHIP_ERROR_NOT_IMPLEMENTED; } - CHIP_ERROR SetSetupPasscode(uint32_t setupPasscode) override { return CHIP_ERROR_NOT_IMPLEMENTED; } // ===== Members functions that implement the DeviceAttestationCredentialsProvider CHIP_ERROR GetCertificationDeclaration(MutableByteSpan & outBuffer) override; - CHIP_ERROR GetFirmwareInformation(MutableByteSpan & out_firmware_info_buffer) override; - CHIP_ERROR GetDeviceAttestationCert(MutableByteSpan & outBuffer) override; - CHIP_ERROR GetProductAttestationIntermediateCert(MutableByteSpan & outBuffer) override; - CHIP_ERROR SignWithDeviceAttestationKey(const ByteSpan & digestToSign, MutableByteSpan & outSignBuffer) override; + +#if CHIP_DEVICE_CONFIG_ENABLE_DEVICE_INSTANCE_INFO_PROVIDER + // ===== Members functions that implement the GenericDeviceInstanceInfoProvider + CHIP_ERROR GetVendorName(char * buf, size_t bufSize) override; + 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; +#endif // CHIP_DEVICE_CONFIG_ENABLE_DEVICE_INSTANCE_INFO_PROVIDER }; } // namespace DeviceLayer