From 9dd1ed34c85149ee545ea34c9b8e6cd545a62f97 Mon Sep 17 00:00:00 2001 From: EricZijian_Siter Date: Wed, 28 Jun 2023 01:19:58 +1000 Subject: [PATCH] [EFR32] Update support for TestEventTrigger (#27494) Co-authored-by: Hare --- .../platform/silabs/SiWx917/matter_config.cpp | 46 ++----------------- .../silabs/SilabsTestEventTriggerDelegate.cpp | 6 +++ .../silabs/SilabsTestEventTriggerDelegate.h | 14 +++++- .../platform/silabs/efr32/matter_config.cpp | 46 ++----------------- 4 files changed, 27 insertions(+), 85 deletions(-) diff --git a/examples/platform/silabs/SiWx917/matter_config.cpp b/examples/platform/silabs/SiWx917/matter_config.cpp index 4422a5474c862c..e0a7dc1e570079 100644 --- a/examples/platform/silabs/SiWx917/matter_config.cpp +++ b/examples/platform/silabs/SiWx917/matter_config.cpp @@ -56,7 +56,7 @@ using namespace ::chip::DeviceLayer; #include "SilabsDeviceDataProvider.h" #include "SilabsTestEventTriggerDelegate.h" -#include +#include #if SILABS_OTA_ENABLED void SilabsMatterConfig::InitOTARequestorHandler(System::Layer * systemLayer, void * appState) @@ -86,45 +86,6 @@ void SilabsMatterConfig::ConnectivityEventCallback(const ChipDeviceEvent * event static uint8_t sTestEventTriggerEnableKey[TestEventTriggerDelegate::kEnableKeyLength] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; - -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) -{ - size_t 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 // SILABS_TEST_EVENT_TRIGGER_ENABLED CHIP_ERROR SilabsMatterConfig::InitMatter(const char * appName) @@ -180,8 +141,9 @@ CHIP_ERROR SilabsMatterConfig::InitMatter(const char * appName) static chip::CommonCaseDeviceServerInitParams initParams; #if SILABS_TEST_EVENT_TRIGGER_ENABLED - if (hex_string_to_binary(SILABS_TEST_EVENT_TRIGGER_ENABLE_KEY, sTestEventTriggerEnableKey, - sizeof(sTestEventTriggerEnableKey)) == 0) + if (Encoding::HexToBytes(SILABS_TEST_EVENT_TRIGGER_ENABLE_KEY, strlen(SILABS_TEST_EVENT_TRIGGER_ENABLE_KEY), + sTestEventTriggerEnableKey, + TestEventTriggerDelegate::kEnableKeyLength) != TestEventTriggerDelegate::kEnableKeyLength) { SILABS_LOG("Failed to convert the EnableKey string to octstr type value"); memset(sTestEventTriggerEnableKey, 0, sizeof(sTestEventTriggerEnableKey)); diff --git a/examples/platform/silabs/SilabsTestEventTriggerDelegate.cpp b/examples/platform/silabs/SilabsTestEventTriggerDelegate.cpp index f9a11df74d7ae9..f231067cec851b 100644 --- a/examples/platform/silabs/SilabsTestEventTriggerDelegate.cpp +++ b/examples/platform/silabs/SilabsTestEventTriggerDelegate.cpp @@ -27,4 +27,10 @@ bool SilabsTestEventTriggerDelegate::DoesEnableKeyMatch(const ByteSpan & enableK return !mEnableKey.empty() && mEnableKey.data_equal(enableKey); } +CHIP_ERROR SilabsTestEventTriggerDelegate::HandleEventTrigger(uint64_t eventTrigger) +{ + bool success = emberAfHandleEventTrigger(eventTrigger); + return success ? CHIP_NO_ERROR : CHIP_ERROR_INVALID_ARGUMENT; +} + } // namespace chip diff --git a/examples/platform/silabs/SilabsTestEventTriggerDelegate.h b/examples/platform/silabs/SilabsTestEventTriggerDelegate.h index a69bd56c633afd..e9cd62fb813e50 100644 --- a/examples/platform/silabs/SilabsTestEventTriggerDelegate.h +++ b/examples/platform/silabs/SilabsTestEventTriggerDelegate.h @@ -37,7 +37,7 @@ class SilabsTestEventTriggerDelegate : public TestEventTriggerDelegate /** * @brief User handler for handling the test event trigger based on `eventTrigger` provided. * @param eventTrigger Event trigger to handle. - * @return CHIP_NO_ERROR on success or another CHIP_ERROR on failure. + * @return CHIP_NO_ERROR on success or CHIP_ERROR_INVALID_ARGUMENT on failure. */ CHIP_ERROR HandleEventTrigger(uint64_t eventTrigger) override; @@ -46,3 +46,15 @@ class SilabsTestEventTriggerDelegate : public TestEventTriggerDelegate }; } // namespace chip + +/** + * @brief User handler for handling the test event trigger + * + * @note If TestEventTrigger is enabled, it needs to be implemented in the app + * + * @param eventTrigger Event trigger to handle + * + * @retval true on success + * @retval false if error happened + */ +bool emberAfHandleEventTrigger(uint64_t eventTrigger); diff --git a/examples/platform/silabs/efr32/matter_config.cpp b/examples/platform/silabs/efr32/matter_config.cpp index fdecec6279b3da..88da81bf787f1c 100644 --- a/examples/platform/silabs/efr32/matter_config.cpp +++ b/examples/platform/silabs/efr32/matter_config.cpp @@ -56,7 +56,7 @@ static chip::DeviceLayer::Internal::Efr32PsaOperationalKeystore gOperationalKeys #include "SilabsDeviceDataProvider.h" #include "SilabsTestEventTriggerDelegate.h" #include -#include +#include #ifdef CHIP_CONFIG_USE_ICD_SUBSCRIPTION_CALLBACKS ICDSubscriptionCallback SilabsMatterConfig::mICDSubscriptionHandler; @@ -139,45 +139,6 @@ void SilabsMatterConfig::ConnectivityEventCallback(const ChipDeviceEvent * event static uint8_t sTestEventTriggerEnableKey[TestEventTriggerDelegate::kEnableKeyLength] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; - -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) -{ - size_t 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 // SILABS_TEST_EVENT_TRIGGER_ENABLED CHIP_ERROR SilabsMatterConfig::InitMatter(const char * appName) @@ -222,8 +183,9 @@ CHIP_ERROR SilabsMatterConfig::InitMatter(const char * appName) static chip::CommonCaseDeviceServerInitParams initParams; #if SILABS_TEST_EVENT_TRIGGER_ENABLED - if (hex_string_to_binary(SILABS_TEST_EVENT_TRIGGER_ENABLE_KEY, sTestEventTriggerEnableKey, - sizeof(sTestEventTriggerEnableKey)) == 0) + if (Encoding::HexToBytes(SILABS_TEST_EVENT_TRIGGER_ENABLE_KEY, strlen(SILABS_TEST_EVENT_TRIGGER_ENABLE_KEY), + sTestEventTriggerEnableKey, + TestEventTriggerDelegate::kEnableKeyLength) != TestEventTriggerDelegate::kEnableKeyLength) { SILABS_LOG("Failed to convert the EnableKey string to octstr type value"); memset(sTestEventTriggerEnableKey, 0, sizeof(sTestEventTriggerEnableKey));