Skip to content

Commit

Permalink
Make TLVReader getter const for ByteSpan and CharSpan (#28169)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored and pull[bot] committed Jan 23, 2024
1 parent c81b2a5 commit 1056250
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 35 deletions.
41 changes: 15 additions & 26 deletions src/lib/core/TLVReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ CHIP_ERROR TLVReader::Get(double & v) const
return CHIP_NO_ERROR;
}

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

CHIP_ERROR TLVReader::Get(CharSpan & v)
CHIP_ERROR TLVReader::Get(CharSpan & v) const
{
if (!TLVTypeIsUTF8String(ElementType()))
{
Expand Down Expand Up @@ -460,32 +460,22 @@ CHIP_ERROR TLVReader::DupString(char *& buf)
return err;
}

CHIP_ERROR TLVReader::GetDataPtr(const uint8_t *& data)
CHIP_ERROR TLVReader::GetDataPtr(const uint8_t *& data) const
{
CHIP_ERROR err;

if (!TLVTypeIsString(ElementType()))
return CHIP_ERROR_WRONG_TLV_TYPE;
VerifyOrReturnError(TLVTypeIsString(ElementType()), CHIP_ERROR_WRONG_TLV_TYPE);

if (GetLength() == 0)
{
data = nullptr;
return CHIP_NO_ERROR;
}

err = EnsureData(CHIP_ERROR_TLV_UNDERRUN);
if (err != CHIP_NO_ERROR)
return err;

uint32_t remainingLen = static_cast<decltype(mMaxLen)>(mBufEnd - mReadPoint);

// Verify that the entirety of the data is available in the buffer.
// Note that this may not be possible if the reader is reading from a chain of buffers.
if (remainingLen < static_cast<uint32_t>(mElemLenOrVal))
return CHIP_ERROR_TLV_UNDERRUN;

VerifyOrReturnError(remainingLen >= static_cast<uint32_t>(mElemLenOrVal), CHIP_ERROR_TLV_UNDERRUN);
data = mReadPoint;

return CHIP_NO_ERROR;
}

Expand Down Expand Up @@ -576,20 +566,19 @@ CHIP_ERROR TLVReader::VerifyEndOfContainer()

CHIP_ERROR TLVReader::Next()
{
CHIP_ERROR err;
TLVElementType elemType = ElementType();
ReturnErrorOnFailure(Skip());
ReturnErrorOnFailure(ReadElement());

err = Skip();
if (err != CHIP_NO_ERROR)
return err;
TLVElementType elemType = ElementType();

err = ReadElement();
if (err != CHIP_NO_ERROR)
return err;
VerifyOrReturnError(elemType != TLVElementType::EndOfContainer, CHIP_END_OF_TLV);

elemType = ElementType();
if (elemType == TLVElementType::EndOfContainer)
return CHIP_END_OF_TLV;
// Ensure that GetDataPtr calls can be called immediately after Next, so
// that `Get(ByteSpan&)` does not need to advance buffers and just works
if (TLVTypeIsString(elemType) && (GetLength() != 0))
{
ReturnErrorOnFailure(EnsureData(CHIP_ERROR_TLV_UNDERRUN));
}

return CHIP_NO_ERROR;
}
Expand Down
13 changes: 6 additions & 7 deletions src/lib/core/TLVReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ class DLL_EXPORT TLVReader
* the reader is not positioned on an element.
*
*/
CHIP_ERROR Get(ByteSpan & v);
CHIP_ERROR Get(ByteSpan & v) const;

/**
* Get the value of the current element as a FixedByteSpan
Expand All @@ -453,7 +453,7 @@ class DLL_EXPORT TLVReader
*
*/
template <size_t N>
CHIP_ERROR Get(FixedByteSpan<N> & v)
CHIP_ERROR Get(FixedByteSpan<N> & v) const
{
const uint8_t * val;
ReturnErrorOnFailure(GetDataPtr(val));
Expand All @@ -472,7 +472,7 @@ class DLL_EXPORT TLVReader
* the reader is not positioned on an element.
*
*/
CHIP_ERROR Get(CharSpan & v);
CHIP_ERROR Get(CharSpan & v) const;

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

/**
* Prepares a TLVReader object for reading the members of TLV container element.
Expand Down
4 changes: 2 additions & 2 deletions src/lib/format/protocol_decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ByTag
const Tag mTag;
};

CHIP_ERROR FormatCurrentValue(TLVReader & reader, chip::StringBuilderBase & out)
CHIP_ERROR FormatCurrentValue(const TLVReader & reader, chip::StringBuilderBase & out)
{
switch (reader.GetType())
{
Expand Down Expand Up @@ -105,7 +105,7 @@ CHIP_ERROR FormatCurrentValue(TLVReader & reader, chip::StringBuilderBase & out)
}

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

Expand Down

0 comments on commit 1056250

Please sign in to comment.