Skip to content

Commit f42189d

Browse files
committed
Don't embed 'enum' or min/max in the Attribute class
1 parent 41fe827 commit f42189d

File tree

5 files changed

+118
-61
lines changed

5 files changed

+118
-61
lines changed

examples/wifi-echo/server/esp32/main/wifi-echo.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ void PrintDataModel(ClusterServer & server)
374374
cluster->mAttrs.Foreach([](Attribute * attr) -> void {
375375
printf(" Attribute: 0x%04x\n", attr->mAttrId);
376376
char printstr[20];
377-
attr->mValue.ValueToStr(printstr, sizeof(printstr));
377+
attr->Get().ValueToStr(printstr, sizeof(printstr));
378378
printf(" Value: %s\n", printstr);
379379
});
380380
});

src/lib/datamodel/Attribute.h

+43-46
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
/**
1919
* @file
20-
* This file contains definitions for working with CHIP values.
20+
* This file contains definitions for working with CHIP Attributes
2121
*
2222
*/
2323

@@ -45,38 +45,16 @@ class Attribute
4545

4646
public:
4747
AttributeId_t mAttrId;
48-
Value mValue;
49-
Value mMin;
50-
Value mMax;
51-
52-
Attribute(AttributeId_t attrId, ValueTypes type) : mDeque(this), mAttrId(attrId), mValue(type), mMin(type), mMax(type) {}
53-
Attribute(AttributeId_t attrId, Value value) :
54-
mDeque(this), mAttrId(attrId), mValue(value), mMin(value.mType), mMax(value.mType)
55-
{}
56-
Attribute(AttributeId_t attrId, ValueTypes type, uint64_t min, uint64_t max) :
57-
mDeque(this), mAttrId(attrId), mValue(type), mMin(type, min), mMax(type, max)
58-
{}
48+
49+
Attribute(AttributeId_t attrId) : mDeque(this), mAttrId(attrId) {}
5950

6051
/**
6152
* @brief
6253
* Set this attribute to a value
6354
*
6455
* @param value the new value that this attribute should be updated with
6556
*/
66-
CHIP_ERROR Set(const Value & newValue)
67-
{
68-
/* We have to check the element type match in this case */
69-
if (mValue.mType != newValue.mType)
70-
{
71-
return CHIP_ERROR_INTERNAL;
72-
}
73-
if (withinRange(newValue))
74-
{
75-
mValue = newValue;
76-
return CHIP_NO_ERROR;
77-
}
78-
return CHIP_ERROR_INTERNAL;
79-
}
57+
virtual CHIP_ERROR Set(const Value & newValue) = 0;
8058

8159
/* Need to define the behaviour when Value contains pointers
8260
* to allocated data
@@ -86,32 +64,51 @@ class Attribute
8664
* Get the value of this attribute
8765
*
8866
*/
89-
Value Get() { return mValue; }
67+
virtual Value Get(void) = 0;
68+
};
9069

91-
protected:
92-
bool withinRange(const uint64_t & value) { return (value >= mMin.Int64) && (value <= mMax.Int64); }
70+
template <typename ValueType>
71+
class AttributeSimple : public Attribute
72+
{
73+
private:
74+
ValueType mValue;
9375

94-
bool withinRange(const Value value)
76+
public:
77+
AttributeSimple(AttributeId_t attrId) : Attribute(attrId) {}
78+
AttributeSimple(AttributeId_t attrId, ValueType value) : Attribute(attrId), mValue(value) {}
79+
80+
CHIP_ERROR Set(const Value & newValue) { return ValueToType(newValue, mValue); }
81+
82+
Value Get(void) { return TypeToValue(mValue); }
83+
};
84+
85+
template <typename ValueType, ValueType min, ValueType max>
86+
class AttributeWithRange : public Attribute
87+
{
88+
private:
89+
ValueType mValue;
90+
const ValueType mMin = min;
91+
const ValueType mMax = max;
92+
93+
public:
94+
AttributeWithRange(AttributeId_t attrId) : Attribute(attrId) {}
95+
AttributeWithRange(AttributeId_t attrId, ValueType value) : Attribute(attrId), mValue(value) {}
96+
97+
CHIP_ERROR Set(const Value & newValue)
9598
{
96-
switch (mValue.mType)
99+
ValueType tmp;
100+
if (ValueToType(newValue, tmp) == CHIP_NO_ERROR)
97101
{
98-
case kCHIPValueType_Int8:
99-
case kCHIPValueType_Int16:
100-
case kCHIPValueType_Int32:
101-
case kCHIPValueType_Int64:
102-
case kCHIPValueType_UInt8:
103-
case kCHIPValueType_UInt16:
104-
case kCHIPValueType_UInt32:
105-
case kCHIPValueType_UInt64:
106-
return withinRange(value.Int64);
107-
break;
108-
case kCHIPValueType_Bool:
109-
return true;
110-
default:
111-
return false;
102+
if ((tmp >= mMin) && (tmp <= mMax))
103+
{
104+
mValue = tmp;
105+
return CHIP_NO_ERROR;
106+
}
112107
}
113-
return false;
108+
return CHIP_ERROR_INTERNAL;
114109
}
110+
111+
Value Get(void) { return TypeToValue(mValue); }
115112
};
116113

117114
} // namespace DataModel

src/lib/datamodel/ClusterBasic.h

+6-7
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,17 @@ static const AttributeId_t kAttributeIdHWVersion = 0x0003;
4545
class ClusterBasic : public Cluster
4646
{
4747
private:
48-
Attribute mZCLVersion;
49-
Attribute mApplicationVersion;
50-
Attribute mStackVersion;
51-
Attribute mHWVersion;
48+
AttributeSimple<uint8_t> mZCLVersion;
49+
AttributeSimple<uint8_t> mApplicationVersion;
50+
AttributeSimple<uint8_t> mStackVersion;
51+
AttributeSimple<uint8_t> mHWVersion;
5252

5353
public:
5454
ClusterBasic(uint8_t ZCLVersion, uint8_t applicationVersion, uint8_t stackVersion, uint8_t HWVersion) :
5555
Cluster(kClusterIdBase),
5656
/* Attributes */
57-
mZCLVersion(kAttributeIdZCLVersion, ValueUInt8(ZCLVersion)),
58-
mApplicationVersion(kAttributeIdApplicationVersion, ValueUInt8(applicationVersion)),
59-
mStackVersion(kAttributeIdStackVersion, ValueUInt8(stackVersion)), mHWVersion(kAttributeIdHWVersion, ValueUInt8(HWVersion))
57+
mZCLVersion(kAttributeIdZCLVersion, ZCLVersion), mApplicationVersion(kAttributeIdApplicationVersion, applicationVersion),
58+
mStackVersion(kAttributeIdStackVersion, stackVersion), mHWVersion(kAttributeIdHWVersion, HWVersion)
6059
{
6160
AddAttribute(&mZCLVersion);
6261
AddAttribute(&mApplicationVersion);

src/lib/datamodel/ClusterOnOff.h

+6-7
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,15 @@ class ClusterOnOff : public Cluster
4646
{
4747
// TODO: these should
4848
private:
49-
Attribute mOnOff;
50-
Attribute mGlobalSceneControl;
51-
Attribute mOnTime;
52-
Attribute mOffWaitTime;
49+
AttributeSimple<bool> mOnOff;
50+
AttributeSimple<bool> mGlobalSceneControl;
51+
AttributeSimple<uint16_t> mOnTime;
52+
AttributeSimple<uint16_t> mOffWaitTime;
5353

5454
public:
5555
ClusterOnOff() :
56-
Cluster(kClusterIdOnOff), mOnOff(kAttributeIdOnOff, kCHIPValueType_Bool),
57-
mGlobalSceneControl(kAttributeIdGlobalSceneControl, kCHIPValueType_Bool),
58-
mOnTime(kAttributeIdOnTime, kCHIPValueType_UInt16), mOffWaitTime(kAttributeIdOffWaitTime, kCHIPValueType_UInt16)
56+
Cluster(kClusterIdOnOff), mOnOff(kAttributeIdOnOff), mGlobalSceneControl(kAttributeIdGlobalSceneControl),
57+
mOnTime(kAttributeIdOnTime), mOffWaitTime(kAttributeIdOffWaitTime)
5958
{
6059
AddAttribute(&mOnOff);
6160
AddAttribute(&mGlobalSceneControl);

src/lib/datamodel/Value.h

+62
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#ifndef CHIP_VALUE_H_
2525
#define CHIP_VALUE_H_
2626

27+
#include <core/CHIPError.h>
2728
#include <inttypes.h>
2829
#include <stdbool.h>
2930
#include <stdint.h>
@@ -117,6 +118,67 @@ static inline uint8_t ValueToUInt8(Value v)
117118
return (uint8_t) v.Int64;
118119
}
119120

121+
/* TODO: Once we use this, I don't think we will need the above convenience API */
122+
static inline Value TypeToValue(bool b)
123+
{
124+
return Value(kCHIPValueType_Bool, (uint64_t) b);
125+
}
126+
127+
static inline Value TypeToValue(uint8_t i)
128+
{
129+
return Value(kCHIPValueType_UInt8, (uint64_t) i);
130+
}
131+
132+
static inline Value TypeToValue(uint16_t i)
133+
{
134+
return Value(kCHIPValueType_UInt16, (uint64_t) i);
135+
}
136+
137+
static inline Value TypeToValue(uint32_t i)
138+
{
139+
return Value(kCHIPValueType_UInt32, (uint64_t) i);
140+
}
141+
142+
static inline CHIP_ERROR ValueToType(Value v, bool & b)
143+
{
144+
if (v.mType == kCHIPValueType_Bool)
145+
{
146+
b = (bool) v.Int64;
147+
return CHIP_NO_ERROR;
148+
}
149+
return CHIP_ERROR_INTERNAL;
150+
}
151+
152+
static inline CHIP_ERROR ValueToType(Value v, uint8_t & i)
153+
{
154+
if (v.mType == kCHIPValueType_UInt8)
155+
{
156+
i = (uint8_t) v.Int64;
157+
return CHIP_NO_ERROR;
158+
}
159+
return CHIP_ERROR_INTERNAL;
160+
}
161+
162+
static inline CHIP_ERROR ValueToType(Value v, uint16_t & i)
163+
{
164+
if (v.mType == kCHIPValueType_UInt16)
165+
{
166+
i = (uint16_t) v.Int64;
167+
return CHIP_NO_ERROR;
168+
}
169+
return CHIP_ERROR_INTERNAL;
170+
}
171+
172+
static inline CHIP_ERROR ValueToType(Value v, uint32_t & i)
173+
{
174+
if (v.mType == kCHIPValueType_UInt32)
175+
{
176+
i = (uint32_t) v.Int64;
177+
return CHIP_NO_ERROR;
178+
}
179+
return CHIP_ERROR_INTERNAL;
180+
}
181+
120182
} // namespace DataModel
121183
} // namespace chip
122184

0 commit comments

Comments
 (0)