Skip to content

Commit

Permalink
[EFR32] Update support for TestEventTrigger (#27494)
Browse files Browse the repository at this point in the history
Co-authored-by: Hare <renilr0@outlook.com>
  • Loading branch information
2 people authored and pull[bot] committed Jul 17, 2023
1 parent b7d7944 commit 3693618
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 85 deletions.
46 changes: 4 additions & 42 deletions examples/platform/silabs/SiWx917/matter_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ using namespace ::chip::DeviceLayer;

#include "SilabsDeviceDataProvider.h"
#include "SilabsTestEventTriggerDelegate.h"
#include <string.h>
#include <lib/support/BytesToHex.h>

#if SILABS_OTA_ENABLED
void SilabsMatterConfig::InitOTARequestorHandler(System::Layer * systemLayer, void * appState)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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));
Expand Down
6 changes: 6 additions & 0 deletions examples/platform/silabs/SilabsTestEventTriggerDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 13 additions & 1 deletion examples/platform/silabs/SilabsTestEventTriggerDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
46 changes: 4 additions & 42 deletions examples/platform/silabs/efr32/matter_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static chip::DeviceLayer::Internal::Efr32PsaOperationalKeystore gOperationalKeys
#include "SilabsDeviceDataProvider.h"
#include "SilabsTestEventTriggerDelegate.h"
#include <app/InteractionModelEngine.h>
#include <string.h>
#include <lib/support/BytesToHex.h>

#ifdef CHIP_CONFIG_USE_ICD_SUBSCRIPTION_CALLBACKS
ICDSubscriptionCallback SilabsMatterConfig::mICDSubscriptionHandler;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 3693618

Please sign in to comment.