diff --git a/src/app/clusters/scenes-server/SceneHandlerImpl.cpp b/src/app/clusters/scenes-server/SceneHandlerImpl.cpp index f46c7b51fa7cf0..b209ee1ab73752 100644 --- a/src/app/clusters/scenes-server/SceneHandlerImpl.cpp +++ b/src/app/clusters/scenes-server/SceneHandlerImpl.cpp @@ -34,14 +34,15 @@ using AttributeValuePairType = app::Clusters::ScenesManagement::Structs::Attribu /// @param EmberAfDefaultAttributeValue & defaultValue /// @return Value converted to the given working type template -Type ConvertDefaultValueToWorkingValue(const EmberAfDefaultAttributeValue & defaultValue) +typename app::NumericAttributeTraits::WorkingType +ConvertDefaultValueToWorkingValue(const EmberAfDefaultAttributeValue & defaultValue) { - if (sizeof(Type) <= 2) + if (sizeof(typename app::NumericAttributeTraits::WorkingType) <= 2) { - return static_cast(defaultValue.defaultValue); + return static_cast::WorkingType>(defaultValue.defaultValue); } - Type sValue = 0; + typename app::NumericAttributeTraits::StorageType sValue; memcpy(&sValue, defaultValue.ptrToDefaultValue, sizeof(Type)); return app::NumericAttributeTraits::StorageToWorking(sValue); } @@ -62,6 +63,7 @@ void CapAttributeID(AttributeValuePairType & aVPair, const EmberAfAttributeMetad if (metadata->IsBoolean()) { + // Caping the value to 1 in case values greater than 1 are set aVPair.attributeValue = aVPair.attributeValue ? 1 : 0; return; } @@ -69,10 +71,14 @@ void CapAttributeID(AttributeValuePairType & aVPair, const EmberAfAttributeMetad // Check if the attribute type is signed if (metadata->IsSignedIntegerAttribute()) { + // We use emberAfAttributeSize for cases like INT24S, INT40S, INT48S, INT56S where numeric_limits::max() + // wouldn't work maxValue = static_cast((1ULL << (emberAfAttributeSize(metadata) * 8 - 1)) - 1); } else { + // We use emberAfAttributeSize for cases like INT24U, INT40U, INT48U, INT56U where numeric_limits::max() + // wouldn't work maxValue = static_cast((1ULL << (emberAfAttributeSize(metadata) * 8)) - 1); } @@ -80,8 +86,8 @@ void CapAttributeID(AttributeValuePairType & aVPair, const EmberAfAttributeMetad if (metadata->HasMinMax()) { const EmberAfAttributeMinMaxValue * minMaxValue = metadata->defaultValue.ptrToMinMaxValue; - WorkingType minVal = ConvertDefaultValueToWorkingValue(minMaxValue->minValue); - WorkingType maxVal = ConvertDefaultValueToWorkingValue(minMaxValue->maxValue); + WorkingType minVal = ConvertDefaultValueToWorkingValue(minMaxValue->minValue); + WorkingType maxVal = ConvertDefaultValueToWorkingValue(minMaxValue->maxValue); // Cap based on minimum value if (minVal > static_cast(aVPair.attributeValue)) @@ -98,19 +104,30 @@ void CapAttributeID(AttributeValuePairType & aVPair, const EmberAfAttributeMetad } } - // Cap based on maximum value + // Cap based on type-enforced maximum value (and minnimum for signed types) if (metadata->IsSignedIntegerAttribute()) { - if (static_cast(aVPair.attributeValue) > static_cast(maxValue)) + // Cap on max value + if (static_cast(aVPair.attributeValue) > static_cast(maxValue)) { aVPair.attributeValue = static_cast>(maxValue); } + // else + // { + // // Cap on min value + // WorkingType minValue = static_cast(-maxValue - 1); + // if (static_cast(aVPair.attributeValue) < static_cast(minValue)) + // { + // aVPair.attributeValue = static_cast>(minValue); + // } + // } } else { + // Casts below are there to silence the warning about comparing signed and unsigned values if (aVPair.attributeValue > static_cast(maxValue)) { - aVPair.attributeValue = static_cast>(maxValue); + aVPair.attributeValue = std::make_unsigned_t(maxValue); } } }