Skip to content

Commit

Permalink
Fix up put as well as naming for null value and comment
Browse files Browse the repository at this point in the history
  • Loading branch information
andy31415 committed Oct 28, 2024
1 parent 2a75a2e commit 1ac822c
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,19 +387,19 @@ CHIP_ERROR EmberAttributeDataBuffer::EncodeInteger(chip::TLV::TLVWriter & writer
const bool isSigned = chip::app::IsSignedAttributeType(mAttributeType);

unsigned byteCount;
uint64_t nullValue;
uint64_t nullValueAsU64;

if (isSigned)
{
const SignedDecodeInfo info = GetSignedDecodeInfo(mAttributeType);
byteCount = info.byteCount;
nullValue = static_cast<uint64_t>(info.minValue); // just a bit cast for easy compare
nullValueAsU64 = static_cast<uint64_t>(info.minValue);
}
else
{
const UnsignedDecodeInfo info = GetUnsignedDecodeInfo(mAttributeType);
byteCount = info.byteCount;
nullValue = info.maxValue;
nullValueAsU64 = info.maxValue;
}

VerifyOrDie(sizeof(raw_bytes) >= byteCount);
Expand Down Expand Up @@ -438,6 +438,10 @@ CHIP_ERROR EmberAttributeDataBuffer::EncodeInteger(chip::TLV::TLVWriter & writer
value.uint_value = (value.uint_value & ~0xFFULL) | raw_bytes[i];
}

// We place the null value as either int_value or uint_value into a union that is
// bit-formatted as both int64 and uint64. When we define the nullValue,
// it is bitcast into u64 hence this comparison. This is ugly, however this
// code prioritizes code size over readability here.
if (mIsNullable && (value.uint_value == nullValue))
{
// MaxValue is used for NULL setting
Expand All @@ -446,27 +450,21 @@ CHIP_ERROR EmberAttributeDataBuffer::EncodeInteger(chip::TLV::TLVWriter & writer

switch (mAttributeType)
{
case ZCL_INT8U_ATTRIBUTE_TYPE: // Unsigned 8-bit integer
return writer.Put(tag, static_cast<uint8_t>(value.uint_value));
case ZCL_INT8U_ATTRIBUTE_TYPE: // Unsigned 8-bit integer
case ZCL_INT16U_ATTRIBUTE_TYPE: // Unsigned 16-bit integer
return writer.Put(tag, static_cast<uint16_t>(value.uint_value));
case ZCL_INT24U_ATTRIBUTE_TYPE: // Unsigned 24-bit integer
case ZCL_INT32U_ATTRIBUTE_TYPE: // Unsigned 32-bit integer
return writer.Put(tag, static_cast<uint32_t>(value.uint_value));
case ZCL_INT40U_ATTRIBUTE_TYPE: // Unsigned 40-bit integer
case ZCL_INT48U_ATTRIBUTE_TYPE: // Unsigned 48-bit integer
case ZCL_INT56U_ATTRIBUTE_TYPE: // Signed 56-bit integer
case ZCL_INT64U_ATTRIBUTE_TYPE: // Signed 64-bit integer
return writer.Put(tag, static_cast<uint64_t>(value.uint_value));
case ZCL_INT8S_ATTRIBUTE_TYPE: // Signed 8-bit integer
return writer.Put(tag, static_cast<int8_t>(value.int_value));
return writer.Put(tag, value.uint_value);
case ZCL_INT8S_ATTRIBUTE_TYPE: // Signed 8-bit integer
case ZCL_INT16S_ATTRIBUTE_TYPE: // Signed 16-bit integer
return writer.Put(tag, static_cast<int16_t>(value.int_value));
case ZCL_INT24S_ATTRIBUTE_TYPE: // Signed 24-bit integer
case ZCL_INT32S_ATTRIBUTE_TYPE: // Signed 32-bit integer
return writer.Put(tag, static_cast<int32_t>(value.int_value));
default:
return writer.Put(tag, static_cast<int64_t>(value.int_value));
return writer.Put(tag, value.int_value);
}
}

Expand Down

0 comments on commit 1ac822c

Please sign in to comment.