23
23
namespace chip {
24
24
namespace scenes {
25
25
26
- enum EFSTLVTag
26
+ // / @brief Tags Used to serialize Extension Field Sets struct as well as individual field sets.
27
+ // / kArrayContainer: Tag for the container of the Struct with the EFS array
28
+ // / kFieldSetsCount: Tag representing the number of individual field sets
29
+ // / kIndividualContainer: Tag for the container of single EFS struct
30
+ // / kClusterID: Tag for the ClusterID of a field set
31
+ // / kBufferBytes: Tag for the serialized field set data
32
+ enum class TagEFS : uint8_t
27
33
{
28
- kTagEFSArrayContainer = 1 ,
29
- kTagEFSFieldNum = 1 ,
30
- kTagEFSContainer ,
31
- kTagEFSClusterID ,
32
- kTagEFS ,
34
+ kFieldSetArrayContainer = 1 ,
35
+ kFieldSetsCount ,
36
+ kIndividualContainer ,
37
+ kClusterID ,
38
+ kClusterFieldSetData ,
33
39
};
34
40
35
- using clusterId = chip::ClusterId;
36
-
37
- struct ExtensionFieldsSet
41
+ // / @brief Struct to serialize and de serialize a cluster extension field set
42
+ // / mID: Cluster ID, allows to identify which cluster is serialized
43
+ // / mBytesBuffer: Field ID serialized into a byte array
44
+ // / mUsedBytes: Number of bytes in the Buffer containing data, used for serializing only those bytes.
45
+ struct ExtensionFieldSet
38
46
{
39
- clusterId mID = kInvalidClusterId ;
40
- uint8_t mBytesBuffer [kMaxFieldsPerCluster ] = { 0 };
41
- uint8_t mUsedBytes = 0 ;
47
+ ClusterId mID = kInvalidClusterId ;
48
+ uint8_t mBytesBuffer [kMaxFieldBytesPerCluster ] = { 0 };
49
+ uint8_t mUsedBytes = 0 ;
42
50
43
- ExtensionFieldsSet () = default ;
44
- ExtensionFieldsSet (clusterId cmID, const uint8_t * data, uint8_t dataSize) : mID (cmID), mUsedBytes (dataSize)
51
+ ExtensionFieldSet () = default ;
52
+ ExtensionFieldSet (ClusterId cmID, const uint8_t * data, uint8_t dataSize) : mID (cmID), mUsedBytes (dataSize)
45
53
{
46
- if (dataSize <= kMaxFieldsPerCluster )
54
+ if (dataSize <= sizeof ( mBytesBuffer ) )
47
55
{
48
56
memcpy (mBytesBuffer , data, mUsedBytes );
49
57
}
58
+ else
59
+ {
60
+ mUsedBytes = 0 ;
61
+ }
50
62
}
51
63
52
- ExtensionFieldsSet (clusterId cmID, ByteSpan bytes) : mID (cmID), mUsedBytes (static_cast <uint8_t >(bytes.size()))
64
+ ExtensionFieldSet (ClusterId cmID, ByteSpan bytes) : mID (cmID), mUsedBytes (static_cast <uint8_t >(bytes.size()))
53
65
{
54
- if (bytes.size () <= kMaxFieldsPerCluster )
66
+ if (bytes.size () <= sizeof ( mBytesBuffer ) )
55
67
{
56
68
memcpy (mBytesBuffer , bytes.data (), bytes.size ());
57
69
}
70
+ else
71
+ {
72
+ mUsedBytes = 0 ;
73
+ }
58
74
}
59
75
60
- ~ExtensionFieldsSet () = default ;
76
+ ~ExtensionFieldSet () = default ;
61
77
62
78
CHIP_ERROR Serialize (TLV::TLVWriter & writer) const
63
79
{
64
80
TLV::TLVType container;
65
- ReturnErrorOnFailure (writer.StartContainer (TLV::ContextTag (kTagEFSContainer ), TLV::kTLVType_Structure , container));
81
+ ReturnErrorOnFailure (
82
+ writer.StartContainer (TLV::ContextTag (TagEFS::kIndividualContainer ), TLV::kTLVType_Structure , container));
66
83
67
- ReturnErrorOnFailure (writer.Put (TLV::ContextTag (kTagEFSClusterID ), static_cast <uint16_t >( this -> mID )));
68
- ReturnErrorOnFailure (writer.PutBytes (TLV::ContextTag (kTagEFS ), mBytesBuffer , mUsedBytes ));
84
+ ReturnErrorOnFailure (writer.Put (TLV::ContextTag (TagEFS:: kClusterID ), static_cast <uint32_t >( mID )));
85
+ ReturnErrorOnFailure (writer.PutBytes (TLV::ContextTag (TagEFS:: kClusterFieldSetData ), mBytesBuffer , mUsedBytes ));
69
86
70
87
return writer.EndContainer (container);
71
88
}
@@ -74,58 +91,58 @@ struct ExtensionFieldsSet
74
91
{
75
92
ByteSpan buffer;
76
93
TLV::TLVType container;
77
- ReturnErrorOnFailure (reader.Next (TLV::kTLVType_Structure , TLV::ContextTag (kTagEFSContainer )));
94
+ ReturnErrorOnFailure (reader.Next (TLV::kTLVType_Structure , TLV::ContextTag (TagEFS:: kIndividualContainer )));
78
95
ReturnErrorOnFailure (reader.EnterContainer (container));
79
96
80
- ReturnErrorOnFailure (reader.Next (TLV::ContextTag (kTagEFSClusterID )));
81
- ReturnErrorOnFailure (reader.Get (this -> mID ));
97
+ ReturnErrorOnFailure (reader.Next (TLV::ContextTag (TagEFS:: kClusterID )));
98
+ ReturnErrorOnFailure (reader.Get (mID ));
82
99
83
- ReturnErrorOnFailure (reader.Next (TLV::ContextTag (kTagEFS )));
100
+ ReturnErrorOnFailure (reader.Next (TLV::ContextTag (TagEFS:: kClusterFieldSetData )));
84
101
ReturnErrorOnFailure (reader.Get (buffer));
85
- VerifyOrReturnError (buffer.size () <= kMaxFieldsPerCluster , CHIP_ERROR_BUFFER_TOO_SMALL);
86
- this -> mUsedBytes = static_cast <uint8_t >(buffer.size ());
87
- memcpy (this -> mBytesBuffer , buffer.data (), this -> mUsedBytes );
102
+ VerifyOrReturnError (buffer.size () <= sizeof ( mBytesBuffer ) , CHIP_ERROR_BUFFER_TOO_SMALL);
103
+ mUsedBytes = static_cast <decltype ( mUsedBytes ) >(buffer.size ());
104
+ memcpy (mBytesBuffer , buffer.data (), mUsedBytes );
88
105
89
106
return reader.ExitContainer (container);
90
107
}
91
108
92
109
void Clear ()
93
110
{
94
- this -> mID = kInvalidClusterId ;
95
- memset (this -> mBytesBuffer , 0 , kMaxFieldsPerCluster );
96
- this -> mUsedBytes = 0 ;
111
+ mID = kInvalidClusterId ;
112
+ memset (mBytesBuffer , 0 , kMaxFieldBytesPerCluster );
113
+ mUsedBytes = 0 ;
97
114
}
98
115
99
116
bool IsEmpty () const { return (this ->mUsedBytes == 0 ); }
100
117
101
- bool operator ==(const ExtensionFieldsSet & other)
118
+ bool operator ==(const ExtensionFieldSet & other) const
102
119
{
103
- return (this ->mID == other.mID && ! memcmp ( this ->mBytesBuffer , other.mBytesBuffer , this -> mUsedBytes ) &&
104
- this ->mUsedBytes == other.mUsedBytes );
120
+ return (this ->mID == other.mID && this ->mUsedBytes == other.mUsedBytes &&
121
+ ! memcmp ( this ->mBytesBuffer , other.mBytesBuffer , this -> mUsedBytes ) );
105
122
}
106
123
};
107
124
108
125
class ExtensionFieldSetsImpl : public ExtensionFieldSets
109
126
{
110
127
public:
111
- ExtensionFieldSetsImpl ();
128
+ ExtensionFieldSetsImpl (){} ;
112
129
~ExtensionFieldSetsImpl () override {};
113
130
114
131
// overrides
115
132
CHIP_ERROR Serialize (TLV::TLVWriter & writer) const override ;
116
133
CHIP_ERROR Deserialize (TLV::TLVReader & reader) override ;
117
134
void Clear () override ;
118
- bool IsEmpty () const override { return (this -> mFieldNum == 0 ); }
119
- uint8_t GetFieldNum () const override { return this -> mFieldNum ; };
135
+ bool IsEmpty () const override { return (mFieldSetsCount == 0 ); }
136
+ uint8_t GetFieldSetCount () const override { return mFieldSetsCount ; };
120
137
121
- // implementation
122
- CHIP_ERROR InsertFieldSet (const ExtensionFieldsSet & field);
123
- CHIP_ERROR GetFieldSetAtPosition (ExtensionFieldsSet & field, uint8_t position);
138
+ CHIP_ERROR InsertFieldSet (const ExtensionFieldSet & field);
139
+ CHIP_ERROR GetFieldSetAtPosition (ExtensionFieldSet & field, uint8_t position);
124
140
CHIP_ERROR RemoveFieldAtPosition (uint8_t position);
125
141
126
- bool operator ==(const ExtensionFieldSetsImpl & other)
142
+ // implementation
143
+ bool operator ==(const ExtensionFieldSetsImpl & other) const
127
144
{
128
- for (uint8_t i = 0 ; i < kMaxClusterPerScenes ; i++)
145
+ for (uint8_t i = 0 ; i < mFieldSetsCount ; i++)
129
146
{
130
147
if (!(this ->mEFS [i] == other.mEFS [i]))
131
148
{
@@ -137,18 +154,18 @@ class ExtensionFieldSetsImpl : public ExtensionFieldSets
137
154
138
155
ExtensionFieldSetsImpl & operator =(const ExtensionFieldSetsImpl & other)
139
156
{
140
- for (uint8_t i = 0 ; i < kMaxClusterPerScenes ; i++)
157
+ for (uint8_t i = 0 ; i < other. mFieldSetsCount ; i++)
141
158
{
142
159
this ->mEFS [i] = other.mEFS [i];
143
160
}
144
- mFieldNum = other.mFieldNum ;
161
+ mFieldSetsCount = other.mFieldSetsCount ;
145
162
146
163
return *this ;
147
164
}
148
165
149
166
protected:
150
- ExtensionFieldsSet mEFS [kMaxClusterPerScenes ];
151
- uint8_t mFieldNum = 0 ;
167
+ ExtensionFieldSet mEFS [kMaxClusterPerScenes ];
168
+ uint8_t mFieldSetsCount = 0 ;
152
169
};
153
170
} // namespace scenes
154
171
} // namespace chip
0 commit comments