17
17
18
18
#include < app/clusters/scenes-server/SceneHandlerImpl.h>
19
19
#include < app/util/endpoint-config-api.h>
20
+ #include < app/util/odd-sized-integers.h>
20
21
21
22
namespace chip {
22
23
namespace scenes {
23
24
24
25
namespace {
25
26
27
+ template <int ByteSize, bool IsSigned>
28
+ using OddSizedInteger = app::OddSizedInteger<ByteSize, IsSigned>;
26
29
using ConcreteAttributePath = app::ConcreteAttributePath;
27
30
using AttributeValuePairType = app::Clusters::ScenesManagement::Structs::AttributeValuePair::Type;
28
31
29
- // / ConvertByteArrayToUInt64
30
- // / @brief Helper function to convert a byte array to a uint64_t value
32
+ // / ConvertDefaultValueToWorkingValue
33
+ // / @brief Helper function to convert a byte array to a value of the given type.
31
34
// / @param EmberAfDefaultAttributeValue & defaultValue
32
- // / @param len Length of the byte array
33
- // / @return uint64_t or int64_t Value
34
- // / @note The attribute table supports 8 bytes only for unsigned integers, 4 bytes is the maximum for signed integers
35
+ // / @return Value converted to the given working type
35
36
template <typename Type>
36
- Type ConvertByteArrayToType (const EmberAfDefaultAttributeValue & defaultValue, uint16_t len )
37
+ Type ConvertDefaultValueToWorkingValue (const EmberAfDefaultAttributeValue & defaultValue)
37
38
{
38
- if (len <= 2 )
39
+ if (sizeof (Type) <= 2 )
39
40
{
40
41
return static_cast <Type>(defaultValue.defaultValue );
41
42
}
@@ -56,32 +57,38 @@ Type ConvertByteArrayToType(const EmberAfDefaultAttributeValue & defaultValue, u
56
57
template <typename Type>
57
58
void CapAttributeID (AttributeValuePairType & aVPair, const EmberAfAttributeMetadata * metadata)
58
59
{
59
- // Calculate the maximum value that can be represented with the given number of bytes
60
- Type maxValue = 0 ;
60
+ using IntType = app::NumericAttributeTraits<Type, CHIP_CONFIG_BIG_ENDIAN_TARGET>;
61
+ using WorkingType = typename IntType::WorkingType;
62
+
63
+ WorkingType maxValue;
64
+
65
+ if (metadata->IsBoolean ())
66
+ {
67
+ aVPair.attributeValue = aVPair.attributeValue ? 1 : 0 ;
68
+ return ;
69
+ }
61
70
62
71
// Check if the attribute type is signed
63
72
if (metadata->IsSignedIntegerAttribute ())
64
73
{
65
- maxValue = static_cast <Type >((1ULL << (emberAfAttributeSize (metadata) * 8 - 1 )) - 1 );
74
+ maxValue = static_cast <WorkingType >((1ULL << (emberAfAttributeSize (metadata) * 8 - 1 )) - 1 );
66
75
}
67
76
else
68
77
{
69
- maxValue = static_cast <Type >((1ULL << (emberAfAttributeSize (metadata) * 8 )) - 1 );
78
+ maxValue = static_cast <WorkingType >((1ULL << (emberAfAttributeSize (metadata) * 8 )) - 1 );
70
79
}
71
80
72
81
// Check metadata for min and max values
73
82
if (metadata->HasMinMax ())
74
83
{
75
84
const EmberAfAttributeMinMaxValue * minMaxValue = metadata->defaultValue .ptrToMinMaxValue ;
76
- Type minVal = ConvertByteArrayToType<Type >(minMaxValue->minValue , emberAfAttributeSize (metadata) );
77
- Type maxVal = ConvertByteArrayToType<Type >(minMaxValue->maxValue , emberAfAttributeSize (metadata) );
85
+ WorkingType minVal = ConvertDefaultValueToWorkingValue<WorkingType >(minMaxValue->minValue );
86
+ WorkingType maxVal = ConvertDefaultValueToWorkingValue<WorkingType >(minMaxValue->maxValue );
78
87
79
88
// Cap based on minimum value
80
- if (minVal > static_cast <Type >(aVPair.attributeValue ))
89
+ if (minVal > static_cast <WorkingType >(aVPair.attributeValue ))
81
90
{
82
- uint64_t sValue = 0 ;
83
- memcpy (&sValue , &minVal, sizeof (Type));
84
- aVPair.attributeValue = app::NumericAttributeTraits<uint64_t >::StorageToWorking (sValue );
91
+ aVPair.attributeValue = static_cast <std::make_unsigned_t <WorkingType>>(minVal);
85
92
// We assume the max is >= min therefore we can return
86
93
return ;
87
94
}
@@ -98,18 +105,14 @@ void CapAttributeID(AttributeValuePairType & aVPair, const EmberAfAttributeMetad
98
105
{
99
106
if (static_cast <int64_t >(aVPair.attributeValue ) > static_cast <int64_t >(maxValue))
100
107
{
101
- uint64_t sValue = 0 ;
102
- memcpy (&sValue , &maxValue, sizeof (Type));
103
- aVPair.attributeValue = app::NumericAttributeTraits<uint64_t >::StorageToWorking (sValue );
108
+ aVPair.attributeValue = static_cast <std::make_unsigned_t <WorkingType>>(maxValue);
104
109
}
105
110
}
106
111
else
107
112
{
108
- if (static_cast < uint64_t >( aVPair.attributeValue ) > static_cast <uint64_t >(maxValue))
113
+ if (aVPair.attributeValue > static_cast <uint64_t >(maxValue))
109
114
{
110
- uint64_t sValue = 0 ;
111
- memcpy (&sValue , &maxValue, sizeof (Type));
112
- aVPair.attributeValue = app::NumericAttributeTraits<uint64_t >::StorageToWorking (sValue );
115
+ aVPair.attributeValue = static_cast <std::make_unsigned_t <WorkingType>>(maxValue);
113
116
}
114
117
}
115
118
}
@@ -134,15 +137,32 @@ CHIP_ERROR ValidateAttributePath(EndpointId endpoint, ClusterId cluster, Attribu
134
137
135
138
switch (metadata->attributeType )
136
139
{
140
+ case ZCL_BOOLEAN_ATTRIBUTE_TYPE:
141
+ case ZCL_BITMAP8_ATTRIBUTE_TYPE:
137
142
case ZCL_INT8U_ATTRIBUTE_TYPE:
138
143
CapAttributeID<uint8_t >(aVPair, metadata);
139
144
break ;
145
+ case ZCL_BITMAP16_ATTRIBUTE_TYPE:
140
146
case ZCL_INT16U_ATTRIBUTE_TYPE:
141
147
CapAttributeID<uint16_t >(aVPair, metadata);
142
148
break ;
149
+ case ZCL_INT24U_ATTRIBUTE_TYPE:
150
+ CapAttributeID<OddSizedInteger<3 , false >>(aVPair, metadata);
151
+ break ;
152
+ case ZCL_BITMAP32_ATTRIBUTE_TYPE:
143
153
case ZCL_INT32U_ATTRIBUTE_TYPE:
144
154
CapAttributeID<uint32_t >(aVPair, metadata);
145
155
break ;
156
+ case ZCL_INT40U_ATTRIBUTE_TYPE:
157
+ CapAttributeID<OddSizedInteger<5 , false >>(aVPair, metadata);
158
+ break ;
159
+ case ZCL_INT48U_ATTRIBUTE_TYPE:
160
+ CapAttributeID<OddSizedInteger<6 , false >>(aVPair, metadata);
161
+ break ;
162
+ case ZCL_INT56U_ATTRIBUTE_TYPE:
163
+ CapAttributeID<OddSizedInteger<7 , false >>(aVPair, metadata);
164
+ break ;
165
+ case ZCL_BITMAP64_ATTRIBUTE_TYPE:
146
166
case ZCL_INT64U_ATTRIBUTE_TYPE:
147
167
CapAttributeID<uint64_t >(aVPair, metadata);
148
168
break ;
@@ -152,9 +172,21 @@ CHIP_ERROR ValidateAttributePath(EndpointId endpoint, ClusterId cluster, Attribu
152
172
case ZCL_INT16S_ATTRIBUTE_TYPE: // fallthrough
153
173
CapAttributeID<int16_t >(aVPair, metadata);
154
174
break ;
175
+ case ZCL_INT24S_ATTRIBUTE_TYPE:
176
+ CapAttributeID<OddSizedInteger<3 , true >>(aVPair, metadata);
177
+ break ;
155
178
case ZCL_INT32S_ATTRIBUTE_TYPE:
156
179
CapAttributeID<int32_t >(aVPair, metadata);
157
180
break ;
181
+ case ZCL_INT40S_ATTRIBUTE_TYPE:
182
+ CapAttributeID<OddSizedInteger<5 , true >>(aVPair, metadata);
183
+ break ;
184
+ case ZCL_INT48S_ATTRIBUTE_TYPE:
185
+ CapAttributeID<OddSizedInteger<6 , true >>(aVPair, metadata);
186
+ break ;
187
+ case ZCL_INT56S_ATTRIBUTE_TYPE:
188
+ CapAttributeID<OddSizedInteger<7 , true >>(aVPair, metadata);
189
+ break ;
158
190
case ZCL_INT64S_ATTRIBUTE_TYPE:
159
191
CapAttributeID<int64_t >(aVPair, metadata);
160
192
break ;
0 commit comments