Skip to content

Commit d8f7cdd

Browse files
WIP
1 parent 49ec44b commit d8f7cdd

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

src/app/clusters/scenes-server/SceneHandlerImpl.cpp

+26-9
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@ using AttributeValuePairType = app::Clusters::ScenesManagement::Structs::Attribu
3434
/// @param EmberAfDefaultAttributeValue & defaultValue
3535
/// @return Value converted to the given working type
3636
template <typename Type>
37-
Type ConvertDefaultValueToWorkingValue(const EmberAfDefaultAttributeValue & defaultValue)
37+
typename app::NumericAttributeTraits<Type>::WorkingType
38+
ConvertDefaultValueToWorkingValue(const EmberAfDefaultAttributeValue & defaultValue)
3839
{
39-
if (sizeof(Type) <= 2)
40+
if (sizeof(typename app::NumericAttributeTraits<Type>::WorkingType) <= 2)
4041
{
41-
return static_cast<Type>(defaultValue.defaultValue);
42+
return static_cast<typename app::NumericAttributeTraits<Type>::WorkingType>(defaultValue.defaultValue);
4243
}
4344

44-
Type sValue = 0;
45+
typename app::NumericAttributeTraits<Type>::StorageType sValue;
4546
memcpy(&sValue, defaultValue.ptrToDefaultValue, sizeof(Type));
4647
return app::NumericAttributeTraits<Type>::StorageToWorking(sValue);
4748
}
@@ -62,26 +63,31 @@ void CapAttributeID(AttributeValuePairType & aVPair, const EmberAfAttributeMetad
6263

6364
if (metadata->IsBoolean())
6465
{
66+
// Caping the value to 1 in case values greater than 1 are set
6567
aVPair.attributeValue = aVPair.attributeValue ? 1 : 0;
6668
return;
6769
}
6870

6971
// Check if the attribute type is signed
7072
if (metadata->IsSignedIntegerAttribute())
7173
{
74+
// We use emberAfAttributeSize for cases like INT24S, INT40S, INT48S, INT56S where numeric_limits<WorkingType>::max()
75+
// wouldn't work
7276
maxValue = static_cast<WorkingType>((1ULL << (emberAfAttributeSize(metadata) * 8 - 1)) - 1);
7377
}
7478
else
7579
{
80+
// We use emberAfAttributeSize for cases like INT24U, INT40U, INT48U, INT56U where numeric_limits<WorkingType>::max()
81+
// wouldn't work
7682
maxValue = static_cast<WorkingType>((1ULL << (emberAfAttributeSize(metadata) * 8)) - 1);
7783
}
7884

7985
// Check metadata for min and max values
8086
if (metadata->HasMinMax())
8187
{
8288
const EmberAfAttributeMinMaxValue * minMaxValue = metadata->defaultValue.ptrToMinMaxValue;
83-
WorkingType minVal = ConvertDefaultValueToWorkingValue<WorkingType>(minMaxValue->minValue);
84-
WorkingType maxVal = ConvertDefaultValueToWorkingValue<WorkingType>(minMaxValue->maxValue);
89+
WorkingType minVal = ConvertDefaultValueToWorkingValue<Type>(minMaxValue->minValue);
90+
WorkingType maxVal = ConvertDefaultValueToWorkingValue<Type>(minMaxValue->maxValue);
8591

8692
// Cap based on minimum value
8793
if (minVal > static_cast<WorkingType>(aVPair.attributeValue))
@@ -98,19 +104,30 @@ void CapAttributeID(AttributeValuePairType & aVPair, const EmberAfAttributeMetad
98104
}
99105
}
100106

101-
// Cap based on maximum value
107+
// Cap based on type-enforced maximum value (and minnimum for signed types)
102108
if (metadata->IsSignedIntegerAttribute())
103109
{
104-
if (static_cast<int64_t>(aVPair.attributeValue) > static_cast<int64_t>(maxValue))
110+
// Cap on max value
111+
if (static_cast<WorkingType>(aVPair.attributeValue) > static_cast<WorkingType>(maxValue))
105112
{
106113
aVPair.attributeValue = static_cast<std::make_unsigned_t<WorkingType>>(maxValue);
107114
}
115+
// else
116+
// {
117+
// // Cap on min value
118+
// WorkingType minValue = static_cast<WorkingType>(-maxValue - 1);
119+
// if (static_cast<int64_t>(aVPair.attributeValue) < static_cast<int64_t>(minValue))
120+
// {
121+
// aVPair.attributeValue = static_cast<std::make_unsigned_t<WorkingType>>(minValue);
122+
// }
123+
// }
108124
}
109125
else
110126
{
127+
// Casts below are there to silence the warning about comparing signed and unsigned values
111128
if (aVPair.attributeValue > static_cast<uint64_t>(maxValue))
112129
{
113-
aVPair.attributeValue = static_cast<std::make_unsigned_t<WorkingType>>(maxValue);
130+
aVPair.attributeValue = std::make_unsigned_t<WorkingType>(maxValue);
114131
}
115132
}
116133
}

0 commit comments

Comments
 (0)