Skip to content

Commit

Permalink
OperationalDataset: Add ByteSpan extendedPanID (#12304)
Browse files Browse the repository at this point in the history
ByteSpans are const references, so we should be able to directly
reference the extendedPanId to send cluster commands if the sender
already owns the OperationalDataset object. This eliminates a copy
and doesn't require the calling object to hold separate memory
for the extended PAN ID when it's already present in the TLV.
  • Loading branch information
cecille authored and pull[bot] committed Dec 7, 2021
1 parent f5a7ba1 commit 2315209
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/lib/support/ThreadOperationalDataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,19 @@ CHIP_ERROR OperationalDataset::GetExtendedPanId(uint8_t (&aExtendedPanId)[kSizeE
return CHIP_ERROR_TLV_TAG_NOT_FOUND;
}

CHIP_ERROR OperationalDataset::GetExtendedPanIdAsByteSpan(ByteSpan & span) const
{
const ThreadTLV * tlv = Locate(ThreadTLV::kExtendedPanId);

if (tlv != nullptr)
{
span = ByteSpan(reinterpret_cast<const uint8_t *>(tlv->GetValue()), tlv->GetLength());
return CHIP_NO_ERROR;
}

return CHIP_ERROR_TLV_TAG_NOT_FOUND;
}

CHIP_ERROR OperationalDataset::SetExtendedPanId(const uint8_t (&aExtendedPanId)[kSizeExtendedPanId])
{
ThreadTLV * tlv = MakeRoom(ThreadTLV::kExtendedPanId, sizeof(*tlv) + sizeof(aExtendedPanId));
Expand Down
12 changes: 12 additions & 0 deletions src/lib/support/ThreadOperationalDataset.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ class OperationalDataset
*/
CHIP_ERROR GetExtendedPanId(uint8_t (&aExtendedPanId)[kSizeExtendedPanId]) const;

/**
* This method returns a const ByteSpan to the extended PAN ID in the dataset.
* This can be used to pass the extended PAN ID to a cluster command without the use of external memory.
*
* @param[out] span A reference to receive the location of the extended PAN ID.
*
* @retval CHIP_NO_ERROR Successfully retrieved the extended PAN ID.
* @retval CHIP_ERROR_TLV_TAG_NOT_FOUND Thread extended PAN ID is not present in the dataset.
*
*/
CHIP_ERROR GetExtendedPanIdAsByteSpan(ByteSpan & span) const;

/**
* This method sets Thread extended PAN ID to the dataset.
*
Expand Down
5 changes: 5 additions & 0 deletions src/lib/support/tests/TestThreadOperationalDataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ void TestExtendedPanId(nlTestSuite * inSuite, void * inContext)
NL_TEST_ASSERT(inSuite, dataset.SetExtendedPanId(kExtendedPanId) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, dataset.GetExtendedPanId(extendedPanId) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, memcmp(extendedPanId, kExtendedPanId, sizeof(kExtendedPanId)) == 0);

ByteSpan span;
NL_TEST_ASSERT(inSuite, dataset.GetExtendedPanIdAsByteSpan(span) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, span.size() == sizeof(kExtendedPanId));
NL_TEST_ASSERT(inSuite, memcmp(extendedPanId, span.data(), sizeof(kExtendedPanId)) == 0);
}

void TestMasterKey(nlTestSuite * inSuite, void * inContext)
Expand Down

0 comments on commit 2315209

Please sign in to comment.