Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
lpbeliveau-silabs committed Apr 30, 2024
1 parent 49ec44b commit d8f7cdd
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions src/app/clusters/scenes-server/SceneHandlerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ using AttributeValuePairType = app::Clusters::ScenesManagement::Structs::Attribu
/// @param EmberAfDefaultAttributeValue & defaultValue
/// @return Value converted to the given working type
template <typename Type>
Type ConvertDefaultValueToWorkingValue(const EmberAfDefaultAttributeValue & defaultValue)
typename app::NumericAttributeTraits<Type>::WorkingType
ConvertDefaultValueToWorkingValue(const EmberAfDefaultAttributeValue & defaultValue)
{
if (sizeof(Type) <= 2)
if (sizeof(typename app::NumericAttributeTraits<Type>::WorkingType) <= 2)
{
return static_cast<Type>(defaultValue.defaultValue);
return static_cast<typename app::NumericAttributeTraits<Type>::WorkingType>(defaultValue.defaultValue);
}

Type sValue = 0;
typename app::NumericAttributeTraits<Type>::StorageType sValue;
memcpy(&sValue, defaultValue.ptrToDefaultValue, sizeof(Type));
return app::NumericAttributeTraits<Type>::StorageToWorking(sValue);
}
Expand All @@ -62,26 +63,31 @@ 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;
}

// Check if the attribute type is signed
if (metadata->IsSignedIntegerAttribute())
{
// We use emberAfAttributeSize for cases like INT24S, INT40S, INT48S, INT56S where numeric_limits<WorkingType>::max()
// wouldn't work
maxValue = static_cast<WorkingType>((1ULL << (emberAfAttributeSize(metadata) * 8 - 1)) - 1);
}
else
{
// We use emberAfAttributeSize for cases like INT24U, INT40U, INT48U, INT56U where numeric_limits<WorkingType>::max()
// wouldn't work
maxValue = static_cast<WorkingType>((1ULL << (emberAfAttributeSize(metadata) * 8)) - 1);
}

// Check metadata for min and max values
if (metadata->HasMinMax())
{
const EmberAfAttributeMinMaxValue * minMaxValue = metadata->defaultValue.ptrToMinMaxValue;
WorkingType minVal = ConvertDefaultValueToWorkingValue<WorkingType>(minMaxValue->minValue);
WorkingType maxVal = ConvertDefaultValueToWorkingValue<WorkingType>(minMaxValue->maxValue);
WorkingType minVal = ConvertDefaultValueToWorkingValue<Type>(minMaxValue->minValue);
WorkingType maxVal = ConvertDefaultValueToWorkingValue<Type>(minMaxValue->maxValue);

// Cap based on minimum value
if (minVal > static_cast<WorkingType>(aVPair.attributeValue))
Expand All @@ -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<int64_t>(aVPair.attributeValue) > static_cast<int64_t>(maxValue))
// Cap on max value
if (static_cast<WorkingType>(aVPair.attributeValue) > static_cast<WorkingType>(maxValue))
{
aVPair.attributeValue = static_cast<std::make_unsigned_t<WorkingType>>(maxValue);
}
// else
// {
// // Cap on min value
// WorkingType minValue = static_cast<WorkingType>(-maxValue - 1);
// if (static_cast<int64_t>(aVPair.attributeValue) < static_cast<int64_t>(minValue))
// {
// aVPair.attributeValue = static_cast<std::make_unsigned_t<WorkingType>>(minValue);
// }
// }
}
else
{
// Casts below are there to silence the warning about comparing signed and unsigned values
if (aVPair.attributeValue > static_cast<uint64_t>(maxValue))
{
aVPair.attributeValue = static_cast<std::make_unsigned_t<WorkingType>>(maxValue);
aVPair.attributeValue = std::make_unsigned_t<WorkingType>(maxValue);
}
}
}
Expand Down

0 comments on commit d8f7cdd

Please sign in to comment.