Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restyle TLV reader: Fix floating support #8532

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions src/lib/core/CHIPTLVReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,35 +207,41 @@ CHIP_ERROR TLVReader::Get(uint64_t & v)
return CHIP_NO_ERROR;
}

CHIP_ERROR TLVReader::Get(double & v)
namespace {
template <typename T>
CHIP_ERROR GetFloatingImpl(TLVElementType elementType, uint64_t elemLenOrVal, T & v)
{
switch (ElementType())
switch (elementType)
{
case TLVElementType::FloatingPointNumber32: {
union
{
uint32_t u32;
float f;
} cvt;
cvt.u32 = static_cast<uint32_t>(mElemLenOrVal);
v = cvt.f;
float f;
auto u32 = static_cast<uint32_t>(elemLenOrVal);
memcpy(&f, &u32, sizeof(f));
v = f;
break;
}
case TLVElementType::FloatingPointNumber64: {
union
{
uint64_t u64;
double d;
} cvt;
cvt.u64 = mElemLenOrVal;
v = cvt.d;
double d;
memcpy(&d, &elemLenOrVal, sizeof(d));
v = d;
break;
}
default:
return CHIP_ERROR_WRONG_TLV_TYPE;
}
return CHIP_NO_ERROR;
}
} // namespace

CHIP_ERROR TLVReader::Get(float & v)
{
return GetFloatingImpl(ElementType(), mElemLenOrVal, v);
}

CHIP_ERROR TLVReader::Get(double & v)
{
return GetFloatingImpl(ElementType(), mElemLenOrVal, v);
}

CHIP_ERROR TLVReader::GetBytes(uint8_t * buf, uint32_t bufSize)
{
Expand Down