From fc35cafd0fa3a60a90d78990fb7e1e31d460c9a1 Mon Sep 17 00:00:00 2001 From: Bill Waters <65681039+billwatersiii@users.noreply.github.com> Date: Fri, 3 Dec 2021 16:13:04 -0800 Subject: [PATCH] Adding IsSleepyDevice() and related tests (#12447) --- src/lib/dnssd/Resolver.h | 27 +++++++++++++ src/lib/dnssd/tests/TestTxtFields.cpp | 56 +++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/src/lib/dnssd/Resolver.h b/src/lib/dnssd/Resolver.h index f4ef2ac059c483..0e4873c2a76750 100644 --- a/src/lib/dnssd/Resolver.h +++ b/src/lib/dnssd/Resolver.h @@ -62,6 +62,19 @@ struct ResolvedNodeData Optional GetMrpRetryIntervalIdle() const { return mMrpRetryIntervalIdle; } Optional GetMrpRetryIntervalActive() const { return mMrpRetryIntervalActive; } + bool IsDeviceTreatedAsSleepy(const ReliableMessageProtocolConfig * defaultMRPConfig) const + { + // If either retry interval (Idle - CRI, Active - CRA) has a value and that value is greater + // than the value passed to this function, then the peer device will be treated as if it is + // a Sleepy End Device (SED) + if ((mMrpRetryIntervalIdle.HasValue() && (mMrpRetryIntervalIdle.Value() > defaultMRPConfig->mIdleRetransTimeout)) || + (mMrpRetryIntervalActive.HasValue() && (mMrpRetryIntervalActive.Value() > defaultMRPConfig->mActiveRetransTimeout))) + { + return true; + } + return false; + } + PeerId mPeerId; size_t mNumIPs = 0; Inet::InterfaceId mInterfaceId; @@ -134,6 +147,20 @@ struct DiscoveredNodeData Optional GetMrpRetryIntervalIdle() const { return mrpRetryIntervalIdle; } Optional GetMrpRetryIntervalActive() const { return mrpRetryIntervalActive; } + bool IsDeviceTreatedAsSleepy(const ReliableMessageProtocolConfig * defaultMRPConfig) const + { + // If either retry interval (Idle - CRI, Active - CRA) has a value and that value is greater + // than the value passed to this function, then the peer device will be treated as if it is + // a Sleepy End Device (SED) + if ((mrpRetryIntervalIdle.HasValue() && (mrpRetryIntervalIdle.Value() > defaultMRPConfig->mIdleRetransTimeout)) || + (mrpRetryIntervalActive.HasValue() && (mrpRetryIntervalActive.Value() > defaultMRPConfig->mActiveRetransTimeout))) + + { + return true; + } + return false; + } + void LogDetail() const { #if CHIP_ENABLE_ROTATING_DEVICE_ID diff --git a/src/lib/dnssd/tests/TestTxtFields.cpp b/src/lib/dnssd/tests/TestTxtFields.cpp index 0745132543c4bc..7bc7ee44b6a9d9 100644 --- a/src/lib/dnssd/tests/TestTxtFields.cpp +++ b/src/lib/dnssd/tests/TestTxtFields.cpp @@ -560,6 +560,58 @@ void TxtFieldTcpSupport(nlTestSuite * inSuite, void * inContext) NL_TEST_ASSERT(inSuite, nodeData.*supportsTcp == false); } +// Test IsDeviceTreatedAsSleepy() with CRI +template +void TestIsDeviceSleepyIdle(nlTestSuite * inSuite, void * inContext) +{ + char key[4]; + char val[32]; + NodeData nodeData; + const ReliableMessageProtocolConfig defaultMRPConfig(CHIP_CONFIG_MRP_DEFAULT_IDLE_RETRY_INTERVAL, + CHIP_CONFIG_MRP_DEFAULT_ACTIVE_RETRY_INTERVAL); + + // No key/val set, so the device can't be sleepy + NL_TEST_ASSERT(inSuite, !nodeData.IsDeviceTreatedAsSleepy(&defaultMRPConfig)); + + // If the interval is the default value, the device is not sleepy + sprintf(key, "CRI"); + sprintf(val, "%d", CHIP_CONFIG_MRP_DEFAULT_IDLE_RETRY_INTERVAL.count()); + FillNodeDataFromTxt(GetSpan(key), GetSpan(val), nodeData); + NL_TEST_ASSERT(inSuite, !nodeData.IsDeviceTreatedAsSleepy(&defaultMRPConfig)); + + // If the interval is greater than the default value, the device is sleepy + sprintf(key, "CRI"); + sprintf(val, "%d", CHIP_CONFIG_MRP_DEFAULT_IDLE_RETRY_INTERVAL.count() + 1); + FillNodeDataFromTxt(GetSpan(key), GetSpan(val), nodeData); + NL_TEST_ASSERT(inSuite, nodeData.IsDeviceTreatedAsSleepy(&defaultMRPConfig)); +} + +// Test IsDeviceTreatedAsSleepy() with CRA +template +void TestIsDeviceSleepyActive(nlTestSuite * inSuite, void * inContext) +{ + char key[4]; + char val[32]; + NodeData nodeData; + const ReliableMessageProtocolConfig defaultMRPConfig(CHIP_CONFIG_MRP_DEFAULT_IDLE_RETRY_INTERVAL, + CHIP_CONFIG_MRP_DEFAULT_ACTIVE_RETRY_INTERVAL); + + // No key/val set, so the device can't be sleepy + NL_TEST_ASSERT(inSuite, !nodeData.IsDeviceTreatedAsSleepy(&defaultMRPConfig)); + + // If the interval is the default value, the device is not sleepy + sprintf(key, "CRA"); + sprintf(val, "%d", CHIP_CONFIG_MRP_DEFAULT_ACTIVE_RETRY_INTERVAL.count()); + FillNodeDataFromTxt(GetSpan(key), GetSpan(val), nodeData); + NL_TEST_ASSERT(inSuite, !nodeData.IsDeviceTreatedAsSleepy(&defaultMRPConfig)); + + // If the interval is greater than the default value, the device is sleepy + sprintf(key, "CRA"); + sprintf(val, "%d", CHIP_CONFIG_MRP_DEFAULT_ACTIVE_RETRY_INTERVAL.count() + 1); + FillNodeDataFromTxt(GetSpan(key), GetSpan(val), nodeData); + NL_TEST_ASSERT(inSuite, nodeData.IsDeviceTreatedAsSleepy(&defaultMRPConfig)); +} + const nlTest sTests[] = { NL_TEST_DEF("TxtFieldKey", TestGetTxtFieldKey), // NL_TEST_DEF("TxtFieldKeyCaseInsensitive", TestGetTxtFieldKeyCaseInsensitive), // @@ -576,9 +628,13 @@ const nlTest sTests[] = { NL_TEST_DEF("TxtDiscoveredFieldMrpRetryIntervalIdle", TxtFieldMrpRetryIntervalIdle), NL_TEST_DEF("TxtDiscoveredFieldMrpRetryIntervalActive", TxtFieldMrpRetryIntervalActive), NL_TEST_DEF("TxtDiscoveredFieldTcpSupport", (TxtFieldTcpSupport) ), + NL_TEST_DEF("TxtDiscoveredIsDeviceSleepyIdle", TestIsDeviceSleepyIdle), + NL_TEST_DEF("TxtDiscoveredIsDeviceSleepyActive", TestIsDeviceSleepyActive), NL_TEST_DEF("TxtResolvedFieldMrpRetryIntervalIdle", TxtFieldMrpRetryIntervalIdle), NL_TEST_DEF("TxtResolvedFieldMrpRetryIntervalActive", TxtFieldMrpRetryIntervalActive), NL_TEST_DEF("TxtResolvedFieldTcpSupport", (TxtFieldTcpSupport) ), + NL_TEST_DEF("TxtResolvedIsDeviceSleepyIdle", TestIsDeviceSleepyIdle), + NL_TEST_DEF("TxtResolvedIsDeviceSleepyActive", TestIsDeviceSleepyActive), NL_TEST_SENTINEL() };