From a74f77b4cd6c65c75961b88c47809e33378224ce Mon Sep 17 00:00:00 2001 From: Cecille Freeman Date: Fri, 26 Nov 2021 15:43:06 -0500 Subject: [PATCH] OperationalDataset: Add ByteSpan extendedPanID 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. --- src/lib/support/ThreadOperationalDataset.cpp | 13 +++++++++++++ src/lib/support/ThreadOperationalDataset.h | 12 ++++++++++++ .../support/tests/TestThreadOperationalDataset.cpp | 5 +++++ 3 files changed, 30 insertions(+) diff --git a/src/lib/support/ThreadOperationalDataset.cpp b/src/lib/support/ThreadOperationalDataset.cpp index b6a88f2aea7332..35dedc8878a8e6 100644 --- a/src/lib/support/ThreadOperationalDataset.cpp +++ b/src/lib/support/ThreadOperationalDataset.cpp @@ -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(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)); diff --git a/src/lib/support/ThreadOperationalDataset.h b/src/lib/support/ThreadOperationalDataset.h index e91ad5b6361174..3a2d58cf2675d6 100644 --- a/src/lib/support/ThreadOperationalDataset.h +++ b/src/lib/support/ThreadOperationalDataset.h @@ -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. * diff --git a/src/lib/support/tests/TestThreadOperationalDataset.cpp b/src/lib/support/tests/TestThreadOperationalDataset.cpp index 7d9552f55f4956..47eb062cf04a23 100644 --- a/src/lib/support/tests/TestThreadOperationalDataset.cpp +++ b/src/lib/support/tests/TestThreadOperationalDataset.cpp @@ -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)