@@ -384,29 +384,22 @@ CHIP_ERROR EmberAttributeDataBuffer::EncodeInteger(chip::TLV::TLVWriter & writer
384
384
385
385
uint8_t raw_bytes[8 ];
386
386
387
- bool isSigned = (mAttributeType == ZCL_INT8S_ATTRIBUTE_TYPE) //
388
- || (mAttributeType == ZCL_INT16S_ATTRIBUTE_TYPE) //
389
- || (mAttributeType == ZCL_INT24S_ATTRIBUTE_TYPE) //
390
- || (mAttributeType == ZCL_INT32S_ATTRIBUTE_TYPE) //
391
- || (mAttributeType == ZCL_INT40S_ATTRIBUTE_TYPE) //
392
- || (mAttributeType == ZCL_INT48S_ATTRIBUTE_TYPE) //
393
- || (mAttributeType == ZCL_INT56S_ATTRIBUTE_TYPE) //
394
- || (mAttributeType == ZCL_INT64S_ATTRIBUTE_TYPE);
387
+ const bool isSigned = IsSignedAttributeType (mAttributeType );
395
388
396
389
unsigned byteCount;
397
- uint64_t nullValue ;
390
+ uint64_t nullValueAsU64 ;
398
391
399
392
if (isSigned)
400
393
{
401
394
const SignedDecodeInfo info = GetSignedDecodeInfo (mAttributeType );
402
395
byteCount = info.byteCount ;
403
- nullValue = static_cast <uint64_t >(info.minValue ); // just a bit cast for easy compare
396
+ nullValueAsU64 = static_cast <uint64_t >(info.minValue );
404
397
}
405
398
else
406
399
{
407
400
const UnsignedDecodeInfo info = GetUnsignedDecodeInfo (mAttributeType );
408
401
byteCount = info.byteCount ;
409
- nullValue = info.maxValue ;
402
+ nullValueAsU64 = info.maxValue ;
410
403
}
411
404
412
405
VerifyOrDie (sizeof (raw_bytes) >= byteCount);
@@ -445,36 +438,21 @@ CHIP_ERROR EmberAttributeDataBuffer::EncodeInteger(chip::TLV::TLVWriter & writer
445
438
value.uint_value = (value.uint_value & ~0xFFULL ) | raw_bytes[i];
446
439
}
447
440
448
- if (mIsNullable && (value.uint_value == nullValue))
441
+ // We place the null value as either int_value or uint_value into a union that is
442
+ // bit-formatted as both int64 and uint64. When we define the nullValue,
443
+ // it is bitcast into u64 hence this comparison. This is ugly, however this
444
+ // code prioritizes code size over readability here.
445
+ if (mIsNullable && (value.uint_value == nullValueAsU64))
449
446
{
450
- // MaxValue is used for NULL setting
451
447
return writer.PutNull (tag);
452
448
}
453
449
454
- switch ( mAttributeType )
450
+ if (isSigned )
455
451
{
456
- case ZCL_INT8U_ATTRIBUTE_TYPE: // Unsigned 8-bit integer
457
- return writer.Put (tag, static_cast <uint8_t >(value.uint_value ));
458
- case ZCL_INT16U_ATTRIBUTE_TYPE: // Unsigned 16-bit integer
459
- return writer.Put (tag, static_cast <uint16_t >(value.uint_value ));
460
- case ZCL_INT24U_ATTRIBUTE_TYPE: // Unsigned 24-bit integer
461
- case ZCL_INT32U_ATTRIBUTE_TYPE: // Unsigned 32-bit integer
462
- return writer.Put (tag, static_cast <uint32_t >(value.uint_value ));
463
- case ZCL_INT40U_ATTRIBUTE_TYPE: // Unsigned 40-bit integer
464
- case ZCL_INT48U_ATTRIBUTE_TYPE: // Unsigned 48-bit integer
465
- case ZCL_INT56U_ATTRIBUTE_TYPE: // Signed 56-bit integer
466
- case ZCL_INT64U_ATTRIBUTE_TYPE: // Signed 64-bit integer
467
- return writer.Put (tag, static_cast <uint64_t >(value.uint_value ));
468
- case ZCL_INT8S_ATTRIBUTE_TYPE: // Signed 8-bit integer
469
- return writer.Put (tag, static_cast <int8_t >(value.int_value ));
470
- case ZCL_INT16S_ATTRIBUTE_TYPE: // Signed 16-bit integer
471
- return writer.Put (tag, static_cast <int16_t >(value.int_value ));
472
- case ZCL_INT24S_ATTRIBUTE_TYPE: // Signed 24-bit integer
473
- case ZCL_INT32S_ATTRIBUTE_TYPE: // Signed 32-bit integer
474
- return writer.Put (tag, static_cast <int32_t >(value.int_value ));
475
- default :
476
- return writer.Put (tag, static_cast <int64_t >(value.int_value ));
452
+ return writer.Put (tag, value.int_value );
477
453
}
454
+
455
+ return writer.Put (tag, value.uint_value );
478
456
}
479
457
480
458
CHIP_ERROR EmberAttributeDataBuffer::Encode (chip::TLV::TLVWriter & writer, TLV::Tag tag) const
@@ -497,10 +475,11 @@ CHIP_ERROR EmberAttributeDataBuffer::Encode(chip::TLV::TLVWriter & writer, TLV::
497
475
case 1 :
498
476
return writer.PutBoolean (tag, value != 0 );
499
477
case 0xFF :
478
+ VerifyOrReturnError (mIsNullable , CHIP_ERROR_INVALID_ARGUMENT);
500
479
return writer.PutNull (tag);
501
480
default :
502
481
// Unknown types
503
- return CHIP_ERROR_INCORRECT_STATE ;
482
+ return CHIP_ERROR_INVALID_ARGUMENT ;
504
483
}
505
484
}
506
485
case ZCL_INT8U_ATTRIBUTE_TYPE: // Unsigned 8-bit integer
@@ -531,7 +510,7 @@ CHIP_ERROR EmberAttributeDataBuffer::Encode(chip::TLV::TLVWriter & writer, TLV::
531
510
{
532
511
return endianReader.StatusCode ();
533
512
}
534
- if (NumericAttributeTraits<float >::IsNullValue (value.value ))
513
+ if (mIsNullable && NumericAttributeTraits<float >::IsNullValue (value.value ))
535
514
{
536
515
return writer.PutNull (tag);
537
516
}
@@ -548,7 +527,7 @@ CHIP_ERROR EmberAttributeDataBuffer::Encode(chip::TLV::TLVWriter & writer, TLV::
548
527
{
549
528
return endianReader.StatusCode ();
550
529
}
551
- if (NumericAttributeTraits<double >::IsNullValue (value.value ))
530
+ if (mIsNullable && NumericAttributeTraits<double >::IsNullValue (value.value ))
552
531
{
553
532
return writer.PutNull (tag);
554
533
}
0 commit comments