Skip to content

Commit

Permalink
Convert array to linked list to support dynamic endpoints (#12635)
Browse files Browse the repository at this point in the history
* Convert array to linked list
  • Loading branch information
mkardous-silabs authored and pull[bot] committed Aug 9, 2023
1 parent 9c5b46b commit bf9c2d9
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 38 deletions.
41 changes: 23 additions & 18 deletions src/app/clusters/identify-server/identify-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,42 +64,47 @@ using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters::Identify;

static std::array<Identify *, EMBER_AF_IDENTIFY_CLUSTER_SERVER_ENDPOINT_COUNT> instances = { 0 };
static Identify * firstIdentify = nullptr;

static void onIdentifyClusterTick(chip::System::Layer * systemLayer, void * appState);

static Identify * inst(EndpointId endpoint)
{
for (size_t i = 0; i < instances.size(); i++)
Identify * current = firstIdentify;
while (current != nullptr && current->mEndpoint != endpoint)
{
if (nullptr != instances[i] && endpoint == instances[i]->mEndpoint)
{
return instances[i];
}
current = current->next();
}

return nullptr;
return current;
}

static inline void reg(Identify * inst)
{
for (size_t i = 0; i < instances.size(); i++)
{
if (nullptr == instances[i])
{
instances[i] = inst;
break;
}
}
inst->setNext(firstIdentify);
firstIdentify = inst;
}

static inline void unreg(Identify * inst)
{
for (size_t i = 0; i < instances.size(); i++)
if (firstIdentify == inst)
{
if (inst == instances[i])
firstIdentify = firstIdentify->next();
}
else
{
Identify * previous = firstIdentify;
Identify * current = firstIdentify->next();

while (current != nullptr && current != inst)
{
previous = current;
current = current->next();
}

if (current != nullptr)
{
instances[i] = nullptr;
previous->setNext(current->next());
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/app/clusters/identify-server/identify-server.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,12 @@ struct Identify
EmberAfIdentifyEffectIdentifier mCurrentEffectIdentifier;
EmberAfIdentifyEffectIdentifier mTargetEffectIdentifier;
uint8_t mEffectVariant;
bool mActive = false;
bool mActive = false;
Identify * nextIdentify = nullptr;

bool hasNext() { return this->nextIdentify != nullptr; }

Identify * next() { return this->nextIdentify; }

void setNext(Identify * inst) { this->nextIdentify = inst; }
};
41 changes: 23 additions & 18 deletions src/app/clusters/on-off-server/on-off-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ using namespace chip::app::Clusters::OnOff;
* Attributes Definition
*********************************************************/

static std::array<OnOffEffect *, EMBER_AF_ON_OFF_CLUSTER_SERVER_ENDPOINT_COUNT> instances = { 0 };
static OnOffEffect * firstEffect = nullptr;
OnOffServer OnOffServer::instance;

/**********************************************************
Expand Down Expand Up @@ -539,36 +539,41 @@ EmberEventControl * OnOffServer::configureEventControl(EndpointId endpoint)

static OnOffEffect * inst(EndpointId endpoint)
{
for (size_t i = 0; i < instances.size(); i++)
OnOffEffect * current = firstEffect;
while (current != nullptr && current->mEndpoint != endpoint)
{
if (nullptr != instances[i] && endpoint == instances[i]->mEndpoint)
{
return instances[i];
}
current = current->next();
}

return nullptr;
return current;
}

static inline void reg(OnOffEffect * inst)
{
for (size_t i = 0; i < instances.size(); i++)
{
if (nullptr == instances[i])
{
instances[i] = inst;
break;
}
}
inst->setNext(firstEffect);
firstEffect = inst;
}

static inline void unreg(OnOffEffect * inst)
{
for (size_t i = 0; i < instances.size(); i++)
if (firstEffect == inst)
{
if (inst == instances[i])
firstEffect = firstEffect->next();
}
else
{
OnOffEffect * previous = firstEffect;
OnOffEffect * current = firstEffect->next();

while (current != nullptr && current != inst)
{
previous = current;
current = current->next();
}

if (current != nullptr)
{
instances[i] = nullptr;
previous->setNext(current->next());
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/app/clusters/on-off-server/on-off-server.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ struct OnOffEffect
OffWithEffectTriggerCommand mOffWithEffectTrigger = nullptr;
uint8_t mEffectIdentifier;
uint8_t mEffectVariant;
bool mActive = false;
OnOffEffect * nextEffect = nullptr;

OnOffEffect(
chip::EndpointId endpoint, OffWithEffectTriggerCommand offWithEffectTrigger,
Expand All @@ -95,6 +95,12 @@ struct OnOffEffect
*/
uint8_t effectVariant = static_cast<uint8_t>(EMBER_ZCL_ON_OFF_DELAYED_ALL_OFF_EFFECT_VARIANT_FADE_TO_OFF_IN_0P8_SECONDS));
~OnOffEffect();

bool hasNext() { return this->nextEffect != nullptr; }

OnOffEffect * next() { return this->nextEffect; }

void setNext(OnOffEffect * inst) { this->nextEffect = inst; }
};

/**********************************************************
Expand Down

0 comments on commit bf9c2d9

Please sign in to comment.