@@ -34,14 +34,15 @@ using AttributeValuePairType = app::Clusters::ScenesManagement::Structs::Attribu
34
34
// / @param EmberAfDefaultAttributeValue & defaultValue
35
35
// / @return Value converted to the given working type
36
36
template <typename Type>
37
- Type ConvertDefaultValueToWorkingValue (const EmberAfDefaultAttributeValue & defaultValue)
37
+ typename app::NumericAttributeTraits<Type>::WorkingType
38
+ ConvertDefaultValueToWorkingValue (const EmberAfDefaultAttributeValue & defaultValue)
38
39
{
39
- if (sizeof (Type) <= 2 )
40
+ if (sizeof (typename app::NumericAttributeTraits< Type>::WorkingType ) <= 2 )
40
41
{
41
- return static_cast <Type>(defaultValue.defaultValue );
42
+ return static_cast <typename app::NumericAttributeTraits< Type>::WorkingType >(defaultValue.defaultValue );
42
43
}
43
44
44
- Type sValue = 0 ;
45
+ typename app::NumericAttributeTraits< Type>::StorageType sValue ;
45
46
memcpy (&sValue , defaultValue.ptrToDefaultValue , sizeof (Type));
46
47
return app::NumericAttributeTraits<Type>::StorageToWorking (sValue );
47
48
}
@@ -62,26 +63,31 @@ void CapAttributeID(AttributeValuePairType & aVPair, const EmberAfAttributeMetad
62
63
63
64
if (metadata->IsBoolean ())
64
65
{
66
+ // Caping the value to 1 in case values greater than 1 are set
65
67
aVPair.attributeValue = aVPair.attributeValue ? 1 : 0 ;
66
68
return ;
67
69
}
68
70
69
71
// Check if the attribute type is signed
70
72
if (metadata->IsSignedIntegerAttribute ())
71
73
{
74
+ // We use emberAfAttributeSize for cases like INT24S, INT40S, INT48S, INT56S where numeric_limits<WorkingType>::max()
75
+ // wouldn't work
72
76
maxValue = static_cast <WorkingType>((1ULL << (emberAfAttributeSize (metadata) * 8 - 1 )) - 1 );
73
77
}
74
78
else
75
79
{
80
+ // We use emberAfAttributeSize for cases like INT24U, INT40U, INT48U, INT56U where numeric_limits<WorkingType>::max()
81
+ // wouldn't work
76
82
maxValue = static_cast <WorkingType>((1ULL << (emberAfAttributeSize (metadata) * 8 )) - 1 );
77
83
}
78
84
79
85
// Check metadata for min and max values
80
86
if (metadata->HasMinMax ())
81
87
{
82
88
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 );
85
91
86
92
// Cap based on minimum value
87
93
if (minVal > static_cast <WorkingType>(aVPair.attributeValue ))
@@ -98,19 +104,30 @@ void CapAttributeID(AttributeValuePairType & aVPair, const EmberAfAttributeMetad
98
104
}
99
105
}
100
106
101
- // Cap based on maximum value
107
+ // Cap based on type-enforced maximum value (and minnimum for signed types)
102
108
if (metadata->IsSignedIntegerAttribute ())
103
109
{
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))
105
112
{
106
113
aVPair.attributeValue = static_cast <std::make_unsigned_t <WorkingType>>(maxValue);
107
114
}
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
+ // }
108
124
}
109
125
else
110
126
{
127
+ // Casts below are there to silence the warning about comparing signed and unsigned values
111
128
if (aVPair.attributeValue > static_cast <uint64_t >(maxValue))
112
129
{
113
- aVPair.attributeValue = static_cast < std::make_unsigned_t <WorkingType> >(maxValue);
130
+ aVPair.attributeValue = std::make_unsigned_t <WorkingType>(maxValue);
114
131
}
115
132
}
116
133
}
0 commit comments