Skip to content

Commit

Permalink
Pull request project-chip#336: Feature/UIC-2597
Browse files Browse the repository at this point in the history
Merge in WMN_TOOLS/matter from feature/UIC-2597 to silabs

Squashed commit of the following:

commit e15376f4885bc0887c097c4fb90fecbd9e64d7f6
Author: Anders Esbensen <Anders.Esbensen@silabs.com>
Date:   Fri Dec 2 11:37:15 2022 +0100

    Fixed more Unit test.

commit 1c0ed441673cbf6f27142f0f9ccfb836a6407a64
Author: Anders Esbensen <Anders.Esbensen@silabs.com>
Date:   Thu Dec 1 11:52:45 2022 +0100

    Fixed GCC build error.

commit f3e2048380ffba618f89fe0afb37ba5603f1d8ef
Author: Anders Esbensen <Anders.Esbensen@silabs.com>
Date:   Thu Dec 1 09:52:24 2022 +0100

    Removed foo_test

... and 16 more commits
  • Loading branch information
Anders Esbensen authored and jmartinez-silabs committed Oct 17, 2023
1 parent b349fb2 commit 45ce4cd
Show file tree
Hide file tree
Showing 40 changed files with 17,370 additions and 41,587 deletions.
4 changes: 2 additions & 2 deletions silabs_examples/unify-matter-bridge/linux/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ config("unify-config") {
"zap-generated/data_model_translator"
]
ldflags = [ "-lboost_system" ]
cflags = ["-Wno-unused-const-variable"]
}

config("coverage") {
cflags = [ "-fprofile-arcs", "-ftest-coverage" ]
ldflags = cflags

}

static_library("unify-matter-bridge-lib") {
Expand Down Expand Up @@ -78,7 +79,6 @@ static_library("unify-matter-bridge-lib") {
if (use_coverage) {
configs += [ ":coverage" ]
}

}

executable("unify-matter-bridge") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ void cluster_interactor::build_matter_cluster(const std::unordered_map<std::stri
if (auto attribute_id = translator.get_attribute_id(cluster_name, attribute))
{
attr_type_size type_size = get_attribute_type_size(cluster_id.value(), attribute_id.value());
//TODO we should have the unify node state monitor detect if attributes are writable
cluster_builder.attributes.emplace_back(EmberAfAttributeMetadata{
attribute_id.value(), type_size.attrType, type_size.attrSize, CLUSTER_MASK_SERVER, ZAP_EMPTY_DEFAULT() });
attribute_id.value(), type_size.attrType, type_size.attrSize, ATTRIBUTE_MASK_EXTERNAL_STORAGE | ATTRIBUTE_MASK_WRITABLE, ZAP_EMPTY_DEFAULT() });
}
}
if (cluster_name == "Basic")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
// Third party library
#include <nlunit-test.h>

#include "TestHelpers.hpp"

namespace unify::matter_bridge {
namespace Test {
/**
Expand Down Expand Up @@ -128,7 +130,7 @@ class ClusterContext : public UnifyBridgeContext
const typename T::Type & dataResponse) {
if (happy)
{
NL_TEST_ASSERT(sSuite, dataResponse == value);
NL_TEST_ASSERT(sSuite, dataResponse == value);
}
else
{
Expand All @@ -146,6 +148,34 @@ class ClusterContext : public UnifyBridgeContext
return err;
}

template <typename T>
inline void attribute_write_test(nlTestSuite * sSuite, const std::string & topic, const std::string & json_payload,
typename T::Type value)
{

auto sessionHandle = GetSessionBobToAlice();

bool onSuccessCbInvoked = false;
bool onFailureCbInvoked = false;

mMqttHandler.reset();

auto onSuccessCb = [&onSuccessCbInvoked](const chip::app::ConcreteAttributePath & attributePath) {
onSuccessCbInvoked = true;
};
auto onFailureCb = [&onFailureCbInvoked](const chip::app::ConcreteAttributePath * attributePath, CHIP_ERROR aError) {
onFailureCbInvoked = true;
};

chip::Controller::WriteAttribute<T>(sessionHandle, kEndpointId, value, onSuccessCb, onFailureCb);

DrainAndServiceIO();

NL_TEST_ASSERT_EQUAL(sSuite, true, onSuccessCbInvoked);
NL_TEST_ASSERT_EQUAL(sSuite, false, onFailureCbInvoked);
NL_TEST_ASSERT_EQUAL_JSON(sSuite, json_payload, mMqttHandler.publish_payload);
}

/**
* @brief
* The type parameter T is generally expected to be a
Expand Down Expand Up @@ -175,7 +205,7 @@ class ClusterContext : public UnifyBridgeContext
if (err == CHIP_NO_ERROR)
{
NL_TEST_ASSERT(sSuite, mMqttHandler.publish_topic == topic);
NL_TEST_ASSERT(sSuite, mMqttHandler.publish_payload == json_payload);
NL_TEST_ASSERT_EQUAL_JSON(sSuite, json_payload,mMqttHandler.publish_payload);
}

return err;
Expand All @@ -198,7 +228,7 @@ class ClusterContext : public UnifyBridgeContext
inline CHIP_ERROR command_test(nlTestSuite * sSuite, const std::string & topic, const std::string & json_payload, T & request,
typename T::ResponseType & response)
{
auto onSuccessCb = [sSuite, &response](const chip::app::ConcreteCommandPath & commandPath,
auto onSuccessCb = [&response](const chip::app::ConcreteCommandPath & commandPath,
const chip::app::StatusIB & aStatus,
const typename T::ResponseType & dataResponse) { response = dataResponse; };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ class GroupClusterAttributeTranslatorHelper : public attribute_translator_interf
public:
GroupClusterAttributeTranslatorHelper(matter_node_state_monitor & node_state_monitor, UnifyMqtt & unify_mqtt) :
attribute_translator_interface(node_state_monitor, unify_mqtt, chip::app::Clusters::Groups::Id,
"group_cluster_attribute_handler_helper"),
m_unify_mqtt(unify_mqtt)
"group_cluster_attribute_handler_helper")
{}

CHIP_ERROR Read(const chip::app::ConcreteReadAttributePath & aPath, chip::app::AttributeValueEncoder & aEncoder) override
Expand All @@ -38,8 +37,6 @@ class GroupClusterAttributeTranslatorHelper : public attribute_translator_interf
void reported_updated(const bridged_endpoint * ep, const std::string & cluster, const std::string & attribute,
const nlohmann::json & unify_value) override{};

std::vector<const char *> unify_cluster_names() const override { return std::vector<const char *>({ "Groups" }); }

UnifyMqtt & m_unify_mqtt;
std::vector<const char *> unify_cluster_names() const override { return std::vector<const char *>({ "Groups" }); }
};
} // namespace unify::matter_bridge
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once
#include <nlunit-test.h>
#include <sl_log.h>
#include <nlohmann/json.hpp>
#include <sstream>
namespace unify::matter_bridge::test {


/** Assert two Strings containing JSON code are equal (ignoring whitespaces and order of JSON entries)*/
#define NL_TEST_ASSERT_EQUAL_JSON(_suite, _expected, _actual) {\
try {\
auto _expected_json = nlohmann::json::parse(_expected);\
auto _actual_json = nlohmann::json::parse(_actual);\
if (_expected_json != _actual_json) {\
sl_log_error("ASSERTION FAILED", "\nExpected: %s\nActual: %s\nDiff: %s", _expected_json.dump().c_str(), _actual_json.dump().c_str(), nlohmann::json::diff(_expected_json, _actual_json).dump().c_str());\
NL_TEST_ASSERT(_suite, _expected == _actual);\
}\
} catch (const nlohmann::detail::exception &_ex) {\
sl_log_error("JSON PARSE FAILED", "%s", _ex.what());\
NL_TEST_ASSERT(_suite, false);\
}\
}

/** Assert two of any type are equal. Should work for all non-custom types*/
#define NL_TEST_ASSERT_EQUAL(_suite, _expected, _actual) {\
if (_expected != _actual) {\
std::ostringstream _dbg_out;\
_dbg_out << "Expected: " << _expected << std::endl << "Actual: " << _actual;\
sl_log_error("ASSERTION FAILED", "\n%s", _dbg_out.str().c_str());\
NL_TEST_ASSERT(_suite, _expected == _actual);\
}\
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// Chip components
#include <lib/support/UnitTestContext.h>
#include <lib/support/UnitTestRegistration.h>

// Third party library
#include <nlunit-test.h>

Expand Down Expand Up @@ -50,34 +49,14 @@ static int Initialize(void * context)
return ctx->register_endpoint(ep);
}

static void TestLevelControlAttributeCurrentLevel(nlTestSuite * sSuite, void * apContext)
static void TestLevelControlAttributeWrite(nlTestSuite * sSuite, void * apContext)
{
using namespace chip::app::Clusters::LevelControl;
TestContext & ctx = *static_cast<TestContext *>(apContext);
CHIP_ERROR err = CHIP_NO_ERROR;
DataModel::Nullable<uint8_t> value;

// An empty json object is not acceptable here
err = ctx.attribute_test<Clusters::LevelControl::Attributes::CurrentLevel::TypeInfo, false>(
sSuite, "ucl/by-unid/zw-0x0002/ep2/Level/Attributes/CurrentLevel/Reported", "({ })", value);
NL_TEST_ASSERT(sSuite, err == CHIP_NO_ERROR);

// Non json object is also not acceptable
err = ctx.attribute_test<Clusters::LevelControl::Attributes::CurrentLevel::TypeInfo, false>(
sSuite, "ucl/by-unid/zw-0x0002/ep2/Level/Attributes/CurrentLevel/Reported", "(Nisse)", value);
NL_TEST_ASSERT(sSuite, err == CHIP_NO_ERROR);

// Unify SDK doesn't allow null value.
NL_TEST_ASSERT(sSuite, value.IsNull());
err = ctx.attribute_test<Clusters::LevelControl::Attributes::CurrentLevel::TypeInfo, false>(
sSuite, "ucl/by-unid/zw-0x0002/ep2/Level/Attributes/CurrentLevel/Reported", R"({ "value": null })", value);
NL_TEST_ASSERT(sSuite, err == CHIP_NO_ERROR);

value.SetNonNull((uint8_t) 42);
err = ctx.attribute_test<Clusters::LevelControl::Attributes::CurrentLevel::TypeInfo>(
sSuite, "ucl/by-unid/zw-0x0002/ep2/Level/Attributes/CurrentLevel/Reported", R"({ "value": 42 })", value);
NL_TEST_ASSERT(sSuite, err == CHIP_NO_ERROR);
ctx.attribute_write_test<Attributes::OnLevel::TypeInfo>(sSuite,"Level/Attributes/OnLevel",R"({ "value": 3 })",Attributes::OnLevel::TypeInfo::Type(3));
}


static void TestLevelControlAttributeRemainingTime(nlTestSuite * sSuite, void * apContext)
{
TestContext & ctx = *static_cast<TestContext *>(apContext);
Expand Down Expand Up @@ -137,7 +116,7 @@ static void TestLevelControlAttributeOptions(nlTestSuite * sSuite, void * apCont
R"({ "value": {"ExecuteIfOff":false,"CoupleColorTempToLevel":false } })", 0);
NL_TEST_ASSERT(sSuite, err == CHIP_NO_ERROR);

err = ctx.attribute_test<Clusters::LevelControl::Attributes::Options::TypeInfo, false>(
err = ctx.attribute_test<Clusters::LevelControl::Attributes::Options::TypeInfo>(
sSuite, "ucl/by-unid/zw-0x0002/ep2/Level/Attributes/Options/Reported",
R"({ "value": {"ExecuteIfOff":true,"CoupleColorTempToLevel":true } })", 3);
NL_TEST_ASSERT(sSuite, err == CHIP_NO_ERROR);
Expand Down Expand Up @@ -218,7 +197,7 @@ static void TestLevelControlCommandMoveToLevel(nlTestSuite * sSuite, void * apCo
Clusters::LevelControl::Commands::MoveToLevel::Type request;
CHIP_ERROR err = ctx.command_test<Clusters::LevelControl::Commands::MoveToLevel::Type>(
sSuite, "ucl/by-unid/zw-0x0002/ep2/Level/Commands/MoveToLevel",
R"({"Level":0,"OptionsMask":"unknown value","OptionsOverride":"unknown value","TransitionTime":null})", request);
R"({"Level":0,"OptionsMask":{"CoupleColorTempToLevel":false,"ExecuteIfOff":false},"OptionsOverride":{"CoupleColorTempToLevel":false,"ExecuteIfOff":false},"TransitionTime":null})", request);
NL_TEST_ASSERT(sSuite, err == CHIP_NO_ERROR);
}

Expand All @@ -228,7 +207,7 @@ static void TestLevelControlCommandMove(nlTestSuite * sSuite, void * apContext)
Clusters::LevelControl::Commands::Move::Type request;
CHIP_ERROR err = ctx.command_test<Clusters::LevelControl::Commands::Move::Type>(
sSuite, "ucl/by-unid/zw-0x0002/ep2/Level/Commands/Move",
R"({"MoveMode":"Up","OptionsMask":"unknown value","OptionsOverride":"unknown value","Rate":null})", request);
R"({"MoveMode":"Up","OptionsMask":{"CoupleColorTempToLevel":false,"ExecuteIfOff":false},"OptionsOverride":{"CoupleColorTempToLevel":false,"ExecuteIfOff":false},"Rate":null})", request);
NL_TEST_ASSERT(sSuite, err == CHIP_NO_ERROR);
}

Expand All @@ -238,7 +217,7 @@ static void TestLevelControlCommandStep(nlTestSuite * sSuite, void * apContext)
Clusters::LevelControl::Commands::Step::Type request;
CHIP_ERROR err = ctx.command_test<Clusters::LevelControl::Commands::Step::Type>(
sSuite, "ucl/by-unid/zw-0x0002/ep2/Level/Commands/Step",
R"({"OptionsMask":"unknown value","OptionsOverride":"unknown value","StepMode":"Up","StepSize":0,"TransitionTime":null})",
R"({"OptionsMask":{"CoupleColorTempToLevel":false,"ExecuteIfOff":false},"OptionsOverride":{"CoupleColorTempToLevel":false,"ExecuteIfOff":false},"StepMode":"Up","StepSize":0,"TransitionTime":null})",
request);
NL_TEST_ASSERT(sSuite, err == CHIP_NO_ERROR);
}
Expand All @@ -249,7 +228,7 @@ static void TestLevelControlCommandStop(nlTestSuite * sSuite, void * apContext)
Clusters::LevelControl::Commands::Stop::Type request;
CHIP_ERROR err = ctx.command_test<Clusters::LevelControl::Commands::Stop::Type>(
sSuite, "ucl/by-unid/zw-0x0002/ep2/Level/Commands/Stop",
R"({"OptionsMask":"unknown value","OptionsOverride":"unknown value"})", request);
R"({"OptionsMask":{"CoupleColorTempToLevel":false,"ExecuteIfOff":false},"OptionsOverride":{"CoupleColorTempToLevel":false,"ExecuteIfOff":false}})", request);
NL_TEST_ASSERT(sSuite, err == CHIP_NO_ERROR);
}

Expand All @@ -259,7 +238,7 @@ static void TestLevelControlCommandMoveToLevelWithOnOff(nlTestSuite * sSuite, vo
Clusters::LevelControl::Commands::MoveToLevelWithOnOff::Type request;
CHIP_ERROR err = ctx.command_test<Clusters::LevelControl::Commands::MoveToLevelWithOnOff::Type>(
sSuite, "ucl/by-unid/zw-0x0002/ep2/Level/Commands/MoveToLevelWithOnOff",
R"({"Level":0,"OptionsMask":"unknown value","OptionsOverride":"unknown value","TransitionTime":null})", request);
R"({"Level":0,"OptionsMask":{"CoupleColorTempToLevel":false,"ExecuteIfOff":false},"OptionsOverride":{"CoupleColorTempToLevel":false,"ExecuteIfOff":false},"TransitionTime":null})", request);
NL_TEST_ASSERT(sSuite, err == CHIP_NO_ERROR);
}

Expand All @@ -269,7 +248,7 @@ static void TestLevelControlCommandMoveWithOnOff(nlTestSuite * sSuite, void * ap
Clusters::LevelControl::Commands::MoveWithOnOff::Type request;
CHIP_ERROR err = ctx.command_test<Clusters::LevelControl::Commands::MoveWithOnOff::Type>(
sSuite, "ucl/by-unid/zw-0x0002/ep2/Level/Commands/MoveWithOnOff",
R"({"MoveMode":"Up","OptionsMask":"unknown value","OptionsOverride":"unknown value","Rate":null})", request);
R"({"MoveMode":"Up","OptionsMask":{"CoupleColorTempToLevel":false,"ExecuteIfOff":false},"OptionsOverride":{"CoupleColorTempToLevel":false,"ExecuteIfOff":false},"Rate":null})", request);
NL_TEST_ASSERT(sSuite, err == CHIP_NO_ERROR);
}

Expand All @@ -279,7 +258,7 @@ static void TestLevelControlCommandStepWithOnOff(nlTestSuite * sSuite, void * ap
Clusters::LevelControl::Commands::StepWithOnOff::Type request;
CHIP_ERROR err = ctx.command_test<Clusters::LevelControl::Commands::StepWithOnOff::Type>(
sSuite, "ucl/by-unid/zw-0x0002/ep2/Level/Commands/StepWithOnOff",
R"({"OptionsMask":"unknown value","OptionsOverride":"unknown value","StepMode":"Up","StepSize":0,"TransitionTime":null})",
R"({"OptionsMask":{"CoupleColorTempToLevel":false,"ExecuteIfOff":false},"OptionsOverride":{"CoupleColorTempToLevel":false,"ExecuteIfOff":false},"StepMode":"Up","StepSize":0,"TransitionTime":null})",
request);
NL_TEST_ASSERT(sSuite, err == CHIP_NO_ERROR);
}
Expand All @@ -290,25 +269,25 @@ static void TestLevelControlCommandStopWithOnOff(nlTestSuite * sSuite, void * ap
Clusters::LevelControl::Commands::StopWithOnOff::Type request;
CHIP_ERROR err = ctx.command_test<Clusters::LevelControl::Commands::StopWithOnOff::Type>(
sSuite, "ucl/by-unid/zw-0x0002/ep2/Level/Commands/StopWithOnOff",
R"({"OptionsMask":"unknown value","OptionsOverride":"unknown value"})", request);
R"({"OptionsMask":{"CoupleColorTempToLevel":false,"ExecuteIfOff":false},"OptionsOverride":{"CoupleColorTempToLevel":false,"ExecuteIfOff":false}})", request);
NL_TEST_ASSERT(sSuite, err == CHIP_NO_ERROR);
}

static void TestLevelControlCommandMoveToClosestFrequency(nlTestSuite * sSuite, void * apContext)
{
TestContext & ctx = *static_cast<TestContext *>(apContext);
Clusters::LevelControl::Commands::MoveToClosestFrequency::Type request;
CHIP_ERROR err = ctx.command_test<Clusters::LevelControl::Commands::MoveToClosestFrequency::Type>(
sSuite, "ucl/by-unid/zw-0x0002/ep2/Level/Commands/MoveToClosestFrequency", R"({"OptionsMask":0,"OptionsOverride":0})",
request);
NL_TEST_ASSERT(sSuite, err == CHIP_ERROR(0x00000581));
chip::app::Clusters::LevelControl::Commands::MoveToClosestFrequency::Type request;
request.frequency = 142;
ctx.command_test(sSuite, "ucl/by-unid/zw-0x0002/ep2/Level/Commands/MoveToClosestFrequency",
R"({"Frequency": 142})",
request
);
}

/**
* Test Suite. It lists all the test functions.
*/
static const nlTest sTests[] = {
NL_TEST_DEF("LevelControl::TestLevelControlAttributeCurrentLevel", TestLevelControlAttributeCurrentLevel),
NL_TEST_DEF("LevelControl::TestLevelControlAttributeRemainingTime", TestLevelControlAttributeRemainingTime),
NL_TEST_DEF("LevelControl::TestLevelControlAttributeMinLevel", TestLevelControlAttributeMinLevel),
NL_TEST_DEF("LevelControl::TestLevelControlAttributeMaxLevel", TestLevelControlAttributeMaxLevel),
Expand All @@ -327,6 +306,7 @@ static const nlTest sTests[] = {
NL_TEST_DEF("LevelControl::TestLevelControlAttributeAttributeList", TestLevelControlAttributeAttributeList),
NL_TEST_DEF("LevelControl::TestLevelControlAttributeFeatureMap", TestLevelControlAttributeFeatureMap),
NL_TEST_DEF("LevelControl::TestLevelControlAttributeClusterRevision", TestLevelControlAttributeClusterRevision),
NL_TEST_DEF("LevelControl::TestLevelControlCommandMoveToClosestFrequency", TestLevelControlCommandMoveToClosestFrequency),
NL_TEST_DEF("LevelControl::TestLevelControlCommandMoveToLevel", TestLevelControlCommandMoveToLevel),
NL_TEST_DEF("LevelControl::TestLevelControlCommandMove", TestLevelControlCommandMove),
NL_TEST_DEF("LevelControl::TestLevelControlCommandStep", TestLevelControlCommandStep),
Expand All @@ -336,6 +316,7 @@ static const nlTest sTests[] = {
NL_TEST_DEF("LevelControl::TestLevelControlCommandStepWithOnOff", TestLevelControlCommandStepWithOnOff),
NL_TEST_DEF("LevelControl::TestLevelControlCommandStopWithOnOff", TestLevelControlCommandStopWithOnOff),
NL_TEST_DEF("LevelControl::TestLevelControlCommandMoveToClosestFrequency", TestLevelControlCommandMoveToClosestFrequency),
NL_TEST_DEF("LevelControl::TestLevelControlAttributeWrite", TestLevelControlAttributeWrite),
NL_TEST_SENTINEL()
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ static void TestOnOffCommandOffWithEffect(nlTestSuite * sSuite, void * apContext
Clusters::OnOff::Commands::OffWithEffect::Type request;
CHIP_ERROR err = ctx.command_test<Clusters::OnOff::Commands::OffWithEffect::Type>(
sSuite, "ucl/by-unid/zw-0x0002/ep2/OnOff/Commands/OffWithEffect",
R"({"EffectId":"DelayedAllOff","EffectVariant":"FadeToOffIn0p8Seconds"})", request);
R"({"EffectIdentifier":"DelayedAllOff","EffectVariant":0})", request);
NL_TEST_ASSERT(sSuite, err == CHIP_NO_ERROR);
}

Expand All @@ -145,7 +145,7 @@ static void TestOnOffCommandOnWithTimedOff(nlTestSuite * sSuite, void * apContex
Clusters::OnOff::Commands::OnWithTimedOff::Type request;
CHIP_ERROR err = ctx.command_test<Clusters::OnOff::Commands::OnWithTimedOff::Type>(
sSuite, "ucl/by-unid/zw-0x0002/ep2/OnOff/Commands/OnWithTimedOff",
R"({"OffWaitTime":0,"OnOffControl":"unknown value","OnTime":0})", request);
R"({"OffWaitTime":0,"OnOffControl":{"AcceptOnlyWhenOn":false},"OnTime":0})", request);
NL_TEST_ASSERT(sSuite, err == CHIP_NO_ERROR);
}

Expand Down
Loading

0 comments on commit 45ce4cd

Please sign in to comment.