Skip to content

Commit 2657497

Browse files
committed
chg: now using BitMapObjectPool to maintain list of timers running
1 parent 2da7542 commit 2657497

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

src/app/clusters/level-control/level-control.cpp

+30-22
Original file line numberDiff line numberDiff line change
@@ -131,34 +131,40 @@ static void reallyUpdateCoupledColorTemp(EndpointId endpoint);
131131
#define updateCoupledColorTemp(endpoint)
132132
#endif // IGNORE_LEVEL_CONTROL_CLUSTER_OPTIONS && EMBER_AF_PLUGIN_COLOR_CONTROL_SERVER_TEMP
133133

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);
142137

143138
static void schedule(EndpointId endpoint, uint32_t delayMs)
144139
{
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+
146147
DeviceLayer::SystemLayer().StartTimer(
147148
chip::System::Clock::Milliseconds32(delayMs),
148149
[](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));
155153
},
156-
&events[endpoint]);
154+
eventEndpoint);
157155
}
158156

159157
static void deactivate(EndpointId endpoint)
160158
{
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+
});
162168
}
163169

164170
static EmberAfLevelControlState * getState(EndpointId endpoint)
@@ -198,6 +204,7 @@ void emberAfLevelControlClusterServerTickCallback(intptr_t endpointPtr)
198204

199205
if (state == nullptr)
200206
{
207+
deactivate(endpoint);
201208
return;
202209
}
203210

@@ -209,6 +216,7 @@ void emberAfLevelControlClusterServerTickCallback(intptr_t endpointPtr)
209216
{
210217
emberAfLevelControlClusterPrintln("ERR: reading current level %x", status);
211218
writeRemainingTime(endpoint, 0);
219+
deactivate(endpoint);
212220
return;
213221
}
214222

@@ -241,6 +249,7 @@ void emberAfLevelControlClusterServerTickCallback(intptr_t endpointPtr)
241249
{
242250
emberAfLevelControlClusterPrintln("ERR: writing current level %x", status);
243251
writeRemainingTime(endpoint, 0);
252+
deactivate(endpoint);
244253
return;
245254
}
246255

@@ -295,8 +304,11 @@ void emberAfLevelControlClusterServerTickCallback(intptr_t endpointPtr)
295304
else
296305
{
297306
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
298308
schedule(endpoint, state->eventDurationMs);
299309
}
310+
311+
deactivate(endpoint);
300312
}
301313

302314
static void writeRemainingTime(EndpointId endpoint, uint16_t remainingTimeMs)
@@ -1146,10 +1158,6 @@ static bool areStartUpLevelControlServerAttributesNonVolatile(EndpointId endpoin
11461158
}
11471159
#endif // IGNORE_LEVEL_CONTROL_CLUSTER_START_UP_CURRENT_LEVEL
11481160

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) { }
11541162

11551163
void MatterLevelControlPluginServerInitCallback() {}

0 commit comments

Comments
 (0)