Skip to content

Commit

Permalink
Adding IsSleepyDevice() and related tests (#12447)
Browse files Browse the repository at this point in the history
  • Loading branch information
billwatersiii authored Dec 4, 2021
1 parent 0315b48 commit fc35caf
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/lib/dnssd/Resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ struct ResolvedNodeData
Optional<System::Clock::Milliseconds32> GetMrpRetryIntervalIdle() const { return mMrpRetryIntervalIdle; }
Optional<System::Clock::Milliseconds32> 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;
Expand Down Expand Up @@ -134,6 +147,20 @@ struct DiscoveredNodeData
Optional<System::Clock::Milliseconds32> GetMrpRetryIntervalIdle() const { return mrpRetryIntervalIdle; }
Optional<System::Clock::Milliseconds32> 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
Expand Down
56 changes: 56 additions & 0 deletions src/lib/dnssd/tests/TestTxtFields.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,58 @@ void TxtFieldTcpSupport(nlTestSuite * inSuite, void * inContext)
NL_TEST_ASSERT(inSuite, nodeData.*supportsTcp == false);
}

// Test IsDeviceTreatedAsSleepy() with CRI
template <class NodeData>
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 <class NodeData>
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), //
Expand All @@ -576,9 +628,13 @@ const nlTest sTests[] = {
NL_TEST_DEF("TxtDiscoveredFieldMrpRetryIntervalIdle", TxtFieldMrpRetryIntervalIdle<DiscoveredNodeData>),
NL_TEST_DEF("TxtDiscoveredFieldMrpRetryIntervalActive", TxtFieldMrpRetryIntervalActive<DiscoveredNodeData>),
NL_TEST_DEF("TxtDiscoveredFieldTcpSupport", (TxtFieldTcpSupport<DiscoveredNodeData, &DiscoveredNodeData::supportsTcp>) ),
NL_TEST_DEF("TxtDiscoveredIsDeviceSleepyIdle", TestIsDeviceSleepyIdle<DiscoveredNodeData>),
NL_TEST_DEF("TxtDiscoveredIsDeviceSleepyActive", TestIsDeviceSleepyActive<DiscoveredNodeData>),
NL_TEST_DEF("TxtResolvedFieldMrpRetryIntervalIdle", TxtFieldMrpRetryIntervalIdle<ResolvedNodeData>),
NL_TEST_DEF("TxtResolvedFieldMrpRetryIntervalActive", TxtFieldMrpRetryIntervalActive<ResolvedNodeData>),
NL_TEST_DEF("TxtResolvedFieldTcpSupport", (TxtFieldTcpSupport<ResolvedNodeData, &ResolvedNodeData::mSupportsTcp>) ),
NL_TEST_DEF("TxtResolvedIsDeviceSleepyIdle", TestIsDeviceSleepyIdle<ResolvedNodeData>),
NL_TEST_DEF("TxtResolvedIsDeviceSleepyActive", TestIsDeviceSleepyActive<ResolvedNodeData>),
NL_TEST_SENTINEL()
};

Expand Down

0 comments on commit fc35caf

Please sign in to comment.