Skip to content

Commit 1e34762

Browse files
andy31415andreilitvinrestyled-commits
authored andcommitted
Make TLVReader getter const for ByteSpan and CharSpan (#28169)
* Move some TLV reader get methods to const (integers and bools) * Move floating point getters to const * Make use of new newly const support in protocol decoder printers * Restyled by clang-format * Another minor simplification --------- Co-authored-by: Andrei Litvin <[email protected]> Co-authored-by: Restyled.io <[email protected]>
1 parent 5ff7d3c commit 1e34762

File tree

3 files changed

+23
-35
lines changed

3 files changed

+23
-35
lines changed

src/lib/core/TLVReader.cpp

+15-26
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ CHIP_ERROR TLVReader::Get(double & v) const
290290
return CHIP_NO_ERROR;
291291
}
292292

293-
CHIP_ERROR TLVReader::Get(ByteSpan & v)
293+
CHIP_ERROR TLVReader::Get(ByteSpan & v) const
294294
{
295295
const uint8_t * val;
296296
ReturnErrorOnFailure(GetDataPtr(val));
@@ -304,7 +304,7 @@ constexpr int kUnicodeInformationSeparator1 = 0x1F;
304304
constexpr size_t kMaxLocalizedStringIdentifierLen = 2 * sizeof(LocalizedStringIdentifier);
305305
} // namespace
306306

307-
CHIP_ERROR TLVReader::Get(CharSpan & v)
307+
CHIP_ERROR TLVReader::Get(CharSpan & v) const
308308
{
309309
if (!TLVTypeIsUTF8String(ElementType()))
310310
{
@@ -460,32 +460,22 @@ CHIP_ERROR TLVReader::DupString(char *& buf)
460460
return err;
461461
}
462462

463-
CHIP_ERROR TLVReader::GetDataPtr(const uint8_t *& data)
463+
CHIP_ERROR TLVReader::GetDataPtr(const uint8_t *& data) const
464464
{
465-
CHIP_ERROR err;
466-
467-
if (!TLVTypeIsString(ElementType()))
468-
return CHIP_ERROR_WRONG_TLV_TYPE;
465+
VerifyOrReturnError(TLVTypeIsString(ElementType()), CHIP_ERROR_WRONG_TLV_TYPE);
469466

470467
if (GetLength() == 0)
471468
{
472469
data = nullptr;
473470
return CHIP_NO_ERROR;
474471
}
475472

476-
err = EnsureData(CHIP_ERROR_TLV_UNDERRUN);
477-
if (err != CHIP_NO_ERROR)
478-
return err;
479-
480473
uint32_t remainingLen = static_cast<decltype(mMaxLen)>(mBufEnd - mReadPoint);
481474

482475
// Verify that the entirety of the data is available in the buffer.
483476
// Note that this may not be possible if the reader is reading from a chain of buffers.
484-
if (remainingLen < static_cast<uint32_t>(mElemLenOrVal))
485-
return CHIP_ERROR_TLV_UNDERRUN;
486-
477+
VerifyOrReturnError(remainingLen >= static_cast<uint32_t>(mElemLenOrVal), CHIP_ERROR_TLV_UNDERRUN);
487478
data = mReadPoint;
488-
489479
return CHIP_NO_ERROR;
490480
}
491481

@@ -576,20 +566,19 @@ CHIP_ERROR TLVReader::VerifyEndOfContainer()
576566

577567
CHIP_ERROR TLVReader::Next()
578568
{
579-
CHIP_ERROR err;
580-
TLVElementType elemType = ElementType();
569+
ReturnErrorOnFailure(Skip());
570+
ReturnErrorOnFailure(ReadElement());
581571

582-
err = Skip();
583-
if (err != CHIP_NO_ERROR)
584-
return err;
572+
TLVElementType elemType = ElementType();
585573

586-
err = ReadElement();
587-
if (err != CHIP_NO_ERROR)
588-
return err;
574+
VerifyOrReturnError(elemType != TLVElementType::EndOfContainer, CHIP_END_OF_TLV);
589575

590-
elemType = ElementType();
591-
if (elemType == TLVElementType::EndOfContainer)
592-
return CHIP_END_OF_TLV;
576+
// Ensure that GetDataPtr calls can be called immediately after Next, so
577+
// that `Get(ByteSpan&)` does not need to advance buffers and just works
578+
if (TLVTypeIsString(elemType) && (GetLength() != 0))
579+
{
580+
ReturnErrorOnFailure(EnsureData(CHIP_ERROR_TLV_UNDERRUN));
581+
}
593582

594583
return CHIP_NO_ERROR;
595584
}

src/lib/core/TLVReader.h

+6-7
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ class DLL_EXPORT TLVReader
440440
* the reader is not positioned on an element.
441441
*
442442
*/
443-
CHIP_ERROR Get(ByteSpan & v);
443+
CHIP_ERROR Get(ByteSpan & v) const;
444444

445445
/**
446446
* Get the value of the current element as a FixedByteSpan
@@ -453,7 +453,7 @@ class DLL_EXPORT TLVReader
453453
*
454454
*/
455455
template <size_t N>
456-
CHIP_ERROR Get(FixedByteSpan<N> & v)
456+
CHIP_ERROR Get(FixedByteSpan<N> & v) const
457457
{
458458
const uint8_t * val;
459459
ReturnErrorOnFailure(GetDataPtr(val));
@@ -472,7 +472,7 @@ class DLL_EXPORT TLVReader
472472
* the reader is not positioned on an element.
473473
*
474474
*/
475-
CHIP_ERROR Get(CharSpan & v);
475+
CHIP_ERROR Get(CharSpan & v) const;
476476

477477
/**
478478
* Get the Localized String Identifier contained in the current element..
@@ -634,9 +634,8 @@ class DLL_EXPORT TLVReader
634634
* Get a pointer to the initial encoded byte of a TLV byte or UTF8 string element.
635635
*
636636
* This method returns a direct pointer to the encoded string value within the underlying input buffer
637-
* if a non-zero length string payload is present. To succeed, the method requires that the entirety of the
638-
* string value be present in a single buffer. Otherwise the method returns #CHIP_ERROR_TLV_UNDERRUN.
639-
* This makes the method of limited use when reading data from multiple discontiguous buffers.
637+
* as fetched by `Next`. To succeed, the method requires that the entirety of the
638+
* string value be present in a single buffer.
640639
*
641640
* If no string data is present (i.e the length is zero), data shall be updated to point to null.
642641
*
@@ -653,7 +652,7 @@ class DLL_EXPORT TLVReader
653652
* TLVBackingStore.
654653
*
655654
*/
656-
CHIP_ERROR GetDataPtr(const uint8_t *& data);
655+
CHIP_ERROR GetDataPtr(const uint8_t *& data) const;
657656

658657
/**
659658
* Prepares a TLVReader object for reading the members of TLV container element.

src/lib/format/protocol_decoder.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class ByTag
4545
const Tag mTag;
4646
};
4747

48-
CHIP_ERROR FormatCurrentValue(TLVReader & reader, chip::StringBuilderBase & out)
48+
CHIP_ERROR FormatCurrentValue(const TLVReader & reader, chip::StringBuilderBase & out)
4949
{
5050
switch (reader.GetType())
5151
{
@@ -105,7 +105,7 @@ CHIP_ERROR FormatCurrentValue(TLVReader & reader, chip::StringBuilderBase & out)
105105
}
106106

107107
// Returns a null terminated string containing the current reader value
108-
void PrettyPrintCurrentValue(TLVReader & reader, chip::StringBuilderBase & out, PayloadDecoderBase::DecodePosition & position)
108+
void PrettyPrintCurrentValue(const TLVReader & reader, chip::StringBuilderBase & out, PayloadDecoderBase::DecodePosition & position)
109109
{
110110
CHIP_ERROR err = FormatCurrentValue(reader, out);
111111

0 commit comments

Comments
 (0)