@@ -104,101 +104,15 @@ static constexpr size_t kOnOffMaxEnpointCount =
104
104
EMBER_AF_ON_OFF_CLUSTER_SERVER_ENDPOINT_COUNT + CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT;
105
105
106
106
#ifdef EMBER_AF_PLUGIN_SCENES
107
- static EmberEventControl sceneHandlerEventControls[kOnOffMaxEnpointCount ];
108
107
static void sceneOnOffCallback (EndpointId endpoint);
108
+ using OnOffEndPointPair = scenes::DefaultSceneHandlerImpl::EndpointStatePair<bool >;
109
+ using OnOffTransitionTimeInterface =
110
+ scenes::DefaultSceneHandlerImpl::TransitionTimeInterface<kOnOffMaxEnpointCount , EMBER_AF_ON_OFF_CLUSTER_SERVER_ENDPOINT_COUNT>;
109
111
110
112
class DefaultOnOffSceneHandler : public scenes ::DefaultSceneHandlerImpl
111
113
{
112
114
public:
113
- // / @brief Struct to keep track of the desired state of the OnOff attribute between ApplyScene and
114
- // / transition time expiration
115
- struct EndpointStatePair
116
- {
117
- EndpointStatePair (EndpointId endpoint = kInvalidEndpointId , bool status = false ) : mEndpoint (endpoint), mState (status) {}
118
- EndpointId mEndpoint ;
119
- bool mState ;
120
- };
121
-
122
- // / @brief Struct holding an array of EndpointStatePair. Handles insertion, get and removal by EndpointID.
123
- // / TODO: Implement generic object to handle this boilerplate array manipulation
124
- struct StatePairBuffer
125
- {
126
- bool IsEmpty () const { return (mPairCount == 0 ); }
127
-
128
- CHIP_ERROR FindPair (const EndpointId endpoint, uint16_t & found_index) const
129
- {
130
- VerifyOrReturnError (!IsEmpty (), CHIP_ERROR_NOT_FOUND);
131
- for (found_index = 0 ; found_index < mPairCount ; found_index++)
132
- {
133
- if (endpoint == mStatePairBuffer [found_index].mEndpoint )
134
- {
135
- return CHIP_NO_ERROR;
136
- }
137
- }
138
-
139
- return CHIP_ERROR_NOT_FOUND;
140
- }
141
-
142
- CHIP_ERROR InsertPair (const EndpointStatePair & status)
143
- {
144
- uint16_t idx;
145
- CHIP_ERROR err = FindPair (status.mEndpoint , idx);
146
-
147
- if (CHIP_NO_ERROR == err)
148
- {
149
- mStatePairBuffer [idx] = status;
150
- }
151
- else if (mPairCount < MAX_ENDPOINT_COUNT)
152
- {
153
- // if not found, insert at the end
154
- mStatePairBuffer [mPairCount ] = status;
155
- mPairCount ++;
156
- }
157
- else
158
- {
159
- return CHIP_ERROR_NO_MEMORY;
160
- }
161
-
162
- return CHIP_NO_ERROR;
163
- }
164
-
165
- CHIP_ERROR GetPair (const EndpointId endpoint, EndpointStatePair & status) const
166
- {
167
- uint16_t idx;
168
- ReturnErrorOnFailure (FindPair (endpoint, idx));
169
-
170
- status = mStatePairBuffer [idx];
171
- return CHIP_NO_ERROR;
172
- }
173
-
174
- // / @brief Removes Pair and decrements Pair count if the endpoint existed in the array
175
- // / @param endpoint : endpoint id of the pair
176
- CHIP_ERROR RemovePair (const EndpointId endpoint)
177
- {
178
- uint16_t position;
179
- VerifyOrReturnValue (CHIP_NO_ERROR == FindPair (endpoint, position), CHIP_NO_ERROR);
180
-
181
- uint16_t nextPos = static_cast <uint16_t >(position + 1 );
182
- uint16_t moveNum = static_cast <uint16_t >(mPairCount - nextPos);
183
-
184
- // Compress array after removal, if the removed position is not the last
185
- if (moveNum)
186
- {
187
- memmove (&mStatePairBuffer [position], &mStatePairBuffer [nextPos], sizeof (EndpointStatePair) * moveNum);
188
- }
189
-
190
- mPairCount --;
191
- // Clear last occupied position
192
- mStatePairBuffer [mPairCount ].mEndpoint = kInvalidEndpointId ;
193
-
194
- return CHIP_NO_ERROR;
195
- }
196
-
197
- uint16_t mPairCount ;
198
- EndpointStatePair mStatePairBuffer [kOnOffMaxEnpointCount ];
199
- };
200
-
201
- StatePairBuffer mSceneEndpointStatePairs ;
115
+ DefaultSceneHandlerImpl::StatePairBuffer<bool , kOnOffMaxEnpointCount > mSceneEndpointStatePairs ;
202
116
// As per spec, 1 attribute is scenable in the on off cluster
203
117
static constexpr uint8_t scenableAttributeCount = 1 ;
204
118
@@ -279,7 +193,7 @@ class DefaultOnOffSceneHandler : public scenes::DefaultSceneHandlerImpl
279
193
auto & decodePair = pair_iterator.GetValue ();
280
194
VerifyOrReturnError (decodePair.attributeID == Attributes::OnOff::Id, CHIP_ERROR_INVALID_ARGUMENT);
281
195
ReturnErrorOnFailure (
282
- mSceneEndpointStatePairs .InsertPair (EndpointStatePair (endpoint, static_cast <bool >(decodePair.attributeValue ))));
196
+ mSceneEndpointStatePairs .InsertPair (OnOffEndPointPair (endpoint, static_cast <bool >(decodePair.attributeValue ))));
283
197
}
284
198
// Verify that the EFS was completely read
285
199
CHIP_ERROR err = pair_iterator.GetStatus ();
@@ -299,38 +213,22 @@ class DefaultOnOffSceneHandler : public scenes::DefaultSceneHandlerImpl
299
213
Scenes::ScenesServer::Instance ().IsHandlerRegistered (endpoint, LevelControlServer::GetSceneHandler ())))
300
214
#endif
301
215
{
302
- OnOffServer::Instance ().scheduleTimerCallbackMs (sceneEventControl (endpoint), timeMs);
216
+ OnOffServer::Instance ().scheduleTimerCallbackMs (mTransitionTimeInterface . sceneEventControl (endpoint), timeMs);
303
217
}
304
218
305
219
return CHIP_NO_ERROR;
306
220
}
307
221
308
222
private:
309
- /* *
310
- * @brief Configures EventControl callback when setting On Off through scenes callback
311
- *
312
- * @param[in] endpoint endpoint to start timer for
313
- * @return EmberEventControl* configured event control
314
- */
315
- EmberEventControl * sceneEventControl (EndpointId endpoint)
316
- {
317
- EmberEventControl * controller =
318
- OnOffServer::Instance ().getEventControl (endpoint, Span<EmberEventControl>(sceneHandlerEventControls));
319
- VerifyOrReturnValue (controller != nullptr , nullptr );
320
-
321
- controller->endpoint = endpoint;
322
- controller->callback = &sceneOnOffCallback;
323
-
324
- return controller;
325
- }
223
+ OnOffTransitionTimeInterface mTransitionTimeInterface = OnOffTransitionTimeInterface(Attributes::OnOff::Id, sceneOnOffCallback);
326
224
};
327
225
static DefaultOnOffSceneHandler sOnOffSceneHandler ;
328
226
329
227
static void sceneOnOffCallback (EndpointId endpoint)
330
228
{
331
- DefaultOnOffSceneHandler::EndpointStatePair savedState;
229
+ OnOffEndPointPair savedState;
332
230
ReturnOnFailure (sOnOffSceneHandler .mSceneEndpointStatePairs .GetPair (endpoint, savedState));
333
- chip:: CommandId command = (savedState.mState ) ? Commands::On::Id : Commands::Off::Id;
231
+ CommandId command = (savedState.mValue ) ? Commands::On::Id : Commands::Off::Id;
334
232
OnOffServer::Instance ().setOnOffValue (endpoint, command, false );
335
233
ReturnOnFailure (sOnOffSceneHandler .mSceneEndpointStatePairs .RemovePair (endpoint));
336
234
}
0 commit comments