@@ -131,34 +131,40 @@ static void reallyUpdateCoupledColorTemp(EndpointId endpoint);
131
131
#define updateCoupledColorTemp (endpoint )
132
132
#endif // IGNORE_LEVEL_CONTROL_CLUSTER_OPTIONS && EMBER_AF_PLUGIN_COLOR_CONTROL_SERVER_TEMP
133
133
134
- typedef struct
135
- {
136
- bool isActive;
137
- chip::DeviceLayer::AsyncWorkFunct callback;
138
- chip::EndpointId endpoint;
139
- } MatterEventContext;
140
-
141
- static MatterEventContext events[FIXED_ENDPOINT_COUNT];
134
+ constexpr uint8_t levelControlMaxSimultaneousTimers = FIXED_ENDPOINT_COUNT; // for now using FIXED_ENDPOINT_COUNT. In the future we could use larger numbers or heap allocation to accommodate dynamic endpoints
135
+ static BitMapObjectPool<EndpointId, levelControlMaxSimultaneousTimers> eventEndpoints;
136
+ void emberAfLevelControlClusterServerTickCallback (intptr_t endpointPtr);
142
137
143
138
static void schedule (EndpointId endpoint, uint32_t delayMs)
144
139
{
145
- events[endpoint].isActive = true ;
140
+ auto eventEndpoint = eventEndpoints.CreateObject (endpoint);
141
+ if (eventEndpoint == nullptr ) {
142
+ ChipLogError (Zcl, " Maximum number of concurrent level transitions reached" );
143
+ return ;
144
+ }
145
+ ChipLogError (Zcl, " Allocated endpoint %d" , endpoint);
146
+
146
147
DeviceLayer::SystemLayer ().StartTimer (
147
148
chip::System::Clock::Milliseconds32 (delayMs),
148
149
[](chip::System::Layer *, void * callbackContext) {
149
- auto eventContext = reinterpret_cast <MatterEventContext *>(callbackContext);
150
- if (eventContext->callback && eventContext->isActive )
151
- {
152
- chip::DeviceLayer::PlatformMgr ().ScheduleWork (eventContext->callback ,
153
- reinterpret_cast <intptr_t >(&eventContext->endpoint ));
154
- }
150
+ auto endpoint = reinterpret_cast <EndpointId*>(callbackContext);
151
+ chip::DeviceLayer::PlatformMgr ().ScheduleWork (emberAfLevelControlClusterServerTickCallback,
152
+ reinterpret_cast <intptr_t >(endpoint));
155
153
},
156
- &events[endpoint] );
154
+ eventEndpoint );
157
155
}
158
156
159
157
static void deactivate (EndpointId endpoint)
160
158
{
161
- events[endpoint].isActive = false ;
159
+ eventEndpoints.ForEachActiveObject ([&](auto * entry) {
160
+ if (*entry == endpoint)
161
+ {
162
+ eventEndpoints.ReleaseObject (entry);
163
+ ChipLogError (Zcl, " Released endpoint %d" , endpoint);
164
+ return Loop::Break;
165
+ }
166
+ return Loop::Continue;
167
+ });
162
168
}
163
169
164
170
static EmberAfLevelControlState * getState (EndpointId endpoint)
@@ -198,6 +204,7 @@ void emberAfLevelControlClusterServerTickCallback(intptr_t endpointPtr)
198
204
199
205
if (state == nullptr )
200
206
{
207
+ deactivate (endpoint);
201
208
return ;
202
209
}
203
210
@@ -209,6 +216,7 @@ void emberAfLevelControlClusterServerTickCallback(intptr_t endpointPtr)
209
216
{
210
217
emberAfLevelControlClusterPrintln (" ERR: reading current level %x" , status);
211
218
writeRemainingTime (endpoint, 0 );
219
+ deactivate (endpoint);
212
220
return ;
213
221
}
214
222
@@ -241,6 +249,7 @@ void emberAfLevelControlClusterServerTickCallback(intptr_t endpointPtr)
241
249
{
242
250
emberAfLevelControlClusterPrintln (" ERR: writing current level %x" , status);
243
251
writeRemainingTime (endpoint, 0 );
252
+ deactivate (endpoint);
244
253
return ;
245
254
}
246
255
@@ -295,8 +304,11 @@ void emberAfLevelControlClusterServerTickCallback(intptr_t endpointPtr)
295
304
else
296
305
{
297
306
writeRemainingTime (endpoint, static_cast <uint16_t >(state->transitionTimeMs - state->elapsedTimeMs ));
307
+ deactivate (endpoint); // clears pool of previous transition before starting another one in the same endpoint
298
308
schedule (endpoint, state->eventDurationMs );
299
309
}
310
+
311
+ deactivate (endpoint);
300
312
}
301
313
302
314
static void writeRemainingTime (EndpointId endpoint, uint16_t remainingTimeMs)
@@ -1146,10 +1158,6 @@ static bool areStartUpLevelControlServerAttributesNonVolatile(EndpointId endpoin
1146
1158
}
1147
1159
#endif // IGNORE_LEVEL_CONTROL_CLUSTER_START_UP_CURRENT_LEVEL
1148
1160
1149
- void emberAfPluginLevelControlClusterServerPostInitCallback (EndpointId endpoint)
1150
- {
1151
- VerifyOrDie (endpoint < FIXED_ENDPOINT_COUNT);
1152
- events[endpoint] = { .isActive = false , .callback = emberAfLevelControlClusterServerTickCallback, .endpoint = endpoint };
1153
- }
1161
+ void emberAfPluginLevelControlClusterServerPostInitCallback (EndpointId endpoint) { }
1154
1162
1155
1163
void MatterLevelControlPluginServerInitCallback () {}
0 commit comments