From 15756189266d3fdff866eec3e1a62980102a0dce Mon Sep 17 00:00:00 2001 From: Wang Qixiang <43193572+wqx6@users.noreply.github.com> Date: Mon, 3 Oct 2022 22:16:45 +0800 Subject: [PATCH] [ESP32] Add Kconfig Option for TestEventTrigger (#22809) * ESP32: Add Kconfig Option for TestEventTrigger Command * fix compile error of bridge example Co-authored-by: Andrei Litvin --- config/esp32/components/chip/Kconfig | 14 +++++ .../platform/esp32/common/Esp32AppServer.cpp | 62 ++++++++++++++++++- 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/config/esp32/components/chip/Kconfig b/config/esp32/components/chip/Kconfig index 57ace57f55bf1f..3ce6c9cf014bd6 100644 --- a/config/esp32/components/chip/Kconfig +++ b/config/esp32/components/chip/Kconfig @@ -722,6 +722,20 @@ menu "CHIP Device Layer" WARNING: This option makes it possible to circumvent basic chip security functionality. Because of this it SHOULD NEVER BE ENABLED IN PRODUCTION BUILDS. + config TEST_EVENT_TRIGGER_ENABLED + bool "Enable Test Event Trigger" + default y + help + Enable TestEventTrigger in GeneralDiagnostics cluster. + + config TEST_EVENT_TRIGGER_ENABLE_KEY + string "Test Event Trigger Enable Key" + depends on TEST_EVENT_TRIGGER_ENABLED + default "00112233445566778899aabbccddeeff" + help + The EnableKey in hex string format used by TestEventTrigger command in GeneralDiagnostics + cluster. The length of the string should be 32. + config ENABLE_FIXED_TUNNEL_SERVER bool "Use Fixed Tunnel Server" default n diff --git a/examples/platform/esp32/common/Esp32AppServer.cpp b/examples/platform/esp32/common/Esp32AppServer.cpp index 2edd7b8dc167b4..448463033c383e 100644 --- a/examples/platform/esp32/common/Esp32AppServer.cpp +++ b/examples/platform/esp32/common/Esp32AppServer.cpp @@ -19,26 +19,86 @@ #include "Esp32AppServer.h" #include "CHIPDeviceManager.h" #include +#include #include #include #include +#include using namespace chip; using namespace chip::Credentials; using namespace chip::DeviceLayer; +static constexpr char TAG[] = "ESP32Appserver"; + namespace { #if CHIP_DEVICE_CONFIG_ENABLE_WIFI app::Clusters::NetworkCommissioning::Instance sWiFiNetworkCommissioningInstance(0 /* Endpoint Id */, &(NetworkCommissioning::ESPWiFiDriver::GetInstance())); #endif +#if CONFIG_TEST_EVENT_TRIGGER_ENABLED +static uint8_t sTestEventTriggerEnableKey[TestEventTriggerDelegate::kEnableKeyLength] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, + 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, + 0xcc, 0xdd, 0xee, 0xff }; +#endif } // namespace +#if CONFIG_TEST_EVENT_TRIGGER_ENABLED +static int hex_digit_to_int(char hex) +{ + if ('A' <= hex && hex <= 'F') + { + return 10 + hex - 'A'; + } + if ('a' <= hex && hex <= 'f') + { + return 10 + hex - 'a'; + } + if ('0' <= hex && hex <= '9') + { + return hex - '0'; + } + return -1; +} + +static size_t hex_string_to_binary(const char * hex_string, uint8_t * buf, size_t buf_size) +{ + int num_char = strlen(hex_string); + if (num_char != buf_size * 2) + { + return 0; + } + for (size_t i = 0; i < num_char; i += 2) + { + int digit0 = hex_digit_to_int(hex_string[i]); + int digit1 = hex_digit_to_int(hex_string[i + 1]); + + if (digit0 < 0 || digit1 < 0) + { + return 0; + } + buf[i / 2] = (digit0 << 4) + digit1; + } + + return buf_size; +} +#endif // CONFIG_TEST_EVENT_TRIGGER_ENABLED + void Esp32AppServer::Init(AppDelegate * sAppDelegate) { // Init ZCL Data Model and CHIP App Server static chip::CommonCaseDeviceServerInitParams initParams; +#if CONFIG_TEST_EVENT_TRIGGER_ENABLED && CONFIG_ENABLE_OTA_REQUESTOR + if (hex_string_to_binary(CONFIG_TEST_EVENT_TRIGGER_ENABLE_KEY, sTestEventTriggerEnableKey, + sizeof(sTestEventTriggerEnableKey)) == 0) + { + ESP_LOGE(TAG, "Failed to convert the EnableKey string to octstr type value"); + memset(sTestEventTriggerEnableKey, 0, sizeof(sTestEventTriggerEnableKey)); + } + static OTATestEventTriggerDelegate testEventTriggerDelegate{ ByteSpan(sTestEventTriggerEnableKey) }; + initParams.testEventTriggerDelegate = &testEventTriggerDelegate; +#endif // CONFIG_TEST_EVENT_TRIGGER_ENABLED (void) initParams.InitializeStaticResourcesBeforeServerInit(); if (sAppDelegate != nullptr) { @@ -53,7 +113,7 @@ void Esp32AppServer::Init(AppDelegate * sAppDelegate) if (chip::DeviceLayer::ConnectivityMgr().IsThreadProvisioned() && (chip::Server::GetInstance().GetFabricTable().FabricCount() != 0)) { - ESP_LOGI("ESP32AppServer", "Thread has been provisioned, publish the dns service now"); + ESP_LOGI(TAG, "Thread has been provisioned, publish the dns service now"); chip::app::DnssdServer::Instance().StartServer(); } #endif