Skip to content

Commit

Permalink
[EFR32] Add support for TestEventTrigger on Silabs (#27471)
Browse files Browse the repository at this point in the history
* [EFR32] Add support for TestEventTrigger on Silabs

* Restyled by clang-format

---------

Co-authored-by: Hare <[email protected]>
Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
3 people authored and pull[bot] committed Sep 7, 2023
1 parent fa3b1d5 commit 84331fe
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 1 deletion.
14 changes: 14 additions & 0 deletions examples/platform/silabs/SiWx917/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ declare_args() {

#default Wifi Password
chip_default_wifi_psk = ""

# Enable TestEventTrigger in GeneralDiagnostics cluster
silabs_test_event_trigger_enabled = false

# The EnableKey in hex string format used by TestEventTrigger command in
# GeneralDiagnostics cluster. The length of the string should be 32.
silabs_test_event_trigger_enable_key = "00112233445566778899aabbccddeeff"
}

import("${chip_root}/src/platform/silabs/wifi_args.gni")
Expand Down Expand Up @@ -226,6 +233,7 @@ config("silabs-wifi-config") {

source_set("siwx917-common") {
deps = []
defines = []
public_deps = []
public_configs = [
":siwx917-common-config",
Expand Down Expand Up @@ -294,6 +302,12 @@ source_set("siwx917-common") {
public_deps += [ ":silabs-factory-data-provider" ]
}

if (silabs_test_event_trigger_enabled) {
sources +=
[ "${silabs_common_plat_dir}/SilabsTestEventTriggerDelegate.cpp" ]
defines += [ "SILABS_TEST_EVENT_TRIGGER_ENABLE_KEY=\"${silabs_test_event_trigger_enable_key}\"" ]
}

public_deps += [
"${chip_root}/examples/providers:device_info_provider",
"${chip_root}/src/lib",
Expand Down
61 changes: 60 additions & 1 deletion examples/platform/silabs/SiWx917/matter_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ using namespace ::chip::DeviceLayer;
// If building with the SiWx917-provided crypto backend, we can use the

#include "SilabsDeviceDataProvider.h"
#include "SilabsTestEventTriggerDelegate.h"
#include <string.h>

#if SILABS_OTA_ENABLED
void SilabsMatterConfig::InitOTARequestorHandler(System::Layer * systemLayer, void * appState)
Expand All @@ -65,7 +67,8 @@ void SilabsMatterConfig::InitOTARequestorHandler(System::Layer * systemLayer, vo
}
#endif

void SilabsMatterConfig::ConnectivityEventCallback(const ChipDeviceEvent * event, intptr_t arg){
void SilabsMatterConfig::ConnectivityEventCallback(const ChipDeviceEvent * event, intptr_t arg)
{
// Initialize OTA only when Thread or WiFi connectivity is established
/*if (((event->Type == DeviceEventType::kThreadConnectivityChange) &&
(event->ThreadConnectivityChange.Result == kConnectivity_Established)) ||
Expand All @@ -79,6 +82,51 @@ void SilabsMatterConfig::ConnectivityEventCallback(const ChipDeviceEvent * event
SILABS_LOG("Scheduling OTA Requestor initialization")
}

#if SILABS_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 };

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)
{
CHIP_ERROR err;
Expand Down Expand Up @@ -131,6 +179,17 @@ CHIP_ERROR SilabsMatterConfig::InitMatter(const char * appName)
// Create initParams with SDK example defaults here
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)
{
SILABS_LOG("Failed to convert the EnableKey string to octstr type value");
memset(sTestEventTriggerEnableKey, 0, sizeof(sTestEventTriggerEnableKey));
}
static SilabsTestEventTriggerDelegate testEventTriggerDelegate{ ByteSpan(sTestEventTriggerEnableKey) };
initParams.testEventTriggerDelegate = &testEventTriggerDelegate;
#endif // SILABS_TEST_EVENT_TRIGGER_ENABLED

// Initialize the remaining (not overridden) providers to the SDK example defaults
(void) initParams.InitializeStaticResourcesBeforeServerInit();

Expand Down
30 changes: 30 additions & 0 deletions examples/platform/silabs/SilabsTestEventTriggerDelegate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
*
* Copyright (c) 2023 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "SilabsTestEventTriggerDelegate.h"

using namespace ::chip::DeviceLayer;

namespace chip {

bool SilabsTestEventTriggerDelegate::DoesEnableKeyMatch(const ByteSpan & enableKey) const
{
return !mEnableKey.empty() && mEnableKey.data_equal(enableKey);
}

} // namespace chip
48 changes: 48 additions & 0 deletions examples/platform/silabs/SilabsTestEventTriggerDelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
*
* Copyright (c) 2023 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <app/TestEventTriggerDelegate.h>

namespace chip {

class SilabsTestEventTriggerDelegate : public TestEventTriggerDelegate
{
public:
explicit SilabsTestEventTriggerDelegate(const ByteSpan & enableKey) : mEnableKey(enableKey) {}

/**
* @brief Checks to see if `enableKey` provided matches value chosen by the manufacturer.
* @param enableKey Buffer of the key to verify.
* @return True or False.
*/
bool DoesEnableKeyMatch(const ByteSpan & enableKey) const override;

/**
* @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.
*/
CHIP_ERROR HandleEventTrigger(uint64_t eventTrigger) override;

private:
ByteSpan mEnableKey;
};

} // namespace chip
17 changes: 17 additions & 0 deletions examples/platform/silabs/efr32/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ declare_args() {

# Use default handler to negotiate subscription max interval
chip_config_use_icd_subscription_callbacks = enable_sleepy_device

# Enable TestEventTrigger in GeneralDiagnostics cluster
silabs_test_event_trigger_enabled = false

# The EnableKey in hex string format used by TestEventTrigger command in
# GeneralDiagnostics cluster. The length of the string should be 32.
silabs_test_event_trigger_enable_key = "00112233445566778899aabbccddeeff"
}

silabs_common_plat_dir = "${chip_root}/examples/platform/silabs"
Expand Down Expand Up @@ -262,6 +269,7 @@ config("silabs-wifi-config") {

source_set("efr32-common") {
deps = []
defines = []
public_deps = []
public_configs = [
":efr32-common-config",
Expand Down Expand Up @@ -351,6 +359,15 @@ source_set("efr32-common") {
public_deps += [ ":efr32-ICD-subscription-callback" ]
}

if (silabs_test_event_trigger_enabled) {
sources +=
[ "${silabs_common_plat_dir}/SilabsTestEventTriggerDelegate.cpp" ]
defines += [
"SILABS_TEST_EVENT_TRIGGER_ENABLED=true",
"SILABS_TEST_EVENT_TRIGGER_ENABLE_KEY=\"${silabs_test_event_trigger_enable_key}\"",
]
}

public_deps += [
"${chip_root}/examples/providers:device_info_provider",
"${chip_root}/src/app/server",
Expand Down
58 changes: 58 additions & 0 deletions examples/platform/silabs/efr32/matter_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ static chip::DeviceLayer::Internal::Efr32PsaOperationalKeystore gOperationalKeys
#endif

#include "SilabsDeviceDataProvider.h"
#include "SilabsTestEventTriggerDelegate.h"
#include <app/InteractionModelEngine.h>
#include <string.h>

#ifdef CHIP_CONFIG_USE_ICD_SUBSCRIPTION_CALLBACKS
ICDSubscriptionCallback SilabsMatterConfig::mICDSubscriptionHandler;
Expand Down Expand Up @@ -133,6 +135,51 @@ void SilabsMatterConfig::ConnectivityEventCallback(const ChipDeviceEvent * event
}
}

#if SILABS_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 };

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)
{
CHIP_ERROR err;
Expand Down Expand Up @@ -174,6 +221,17 @@ CHIP_ERROR SilabsMatterConfig::InitMatter(const char * appName)
// Create initParams with SDK example defaults here
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)
{
SILABS_LOG("Failed to convert the EnableKey string to octstr type value");
memset(sTestEventTriggerEnableKey, 0, sizeof(sTestEventTriggerEnableKey));
}
static SilabsTestEventTriggerDelegate testEventTriggerDelegate{ ByteSpan(sTestEventTriggerEnableKey) };
initParams.testEventTriggerDelegate = &testEventTriggerDelegate;
#endif // SILABS_TEST_EVENT_TRIGGER_ENABLED

#if CHIP_CRYPTO_PLATFORM
// When building with EFR32 crypto, use the opaque key store
// instead of the default (insecure) one.
Expand Down

0 comments on commit 84331fe

Please sign in to comment.