Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update existing example app to support switch cluster event #19473

Merged
merged 1 commit into from
Jun 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions examples/all-clusters-app/linux/main-common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <app/clusters/identify-server/identify-server.h>
#include <app/clusters/network-commissioning/network-commissioning.h>
#include <app/clusters/software-diagnostics-server/software-diagnostics-server.h>
#include <app/clusters/switch-server/switch-server.h>
#include <app/server/Server.h>
#include <app/util/af.h>
#include <lib/support/CHIPMem.h>
Expand Down Expand Up @@ -147,6 +148,52 @@ void HandleGeneralFaultEvent(intptr_t arg)
}
}

/**
* Should be called when a switch operation takes place on the Node.
*/
void HandleSwitchEvent(intptr_t arg)
{
uint32_t eventId = static_cast<uint32_t>(arg);

EndpointId endpoint = 1;
uint8_t newPosition = 20;
uint8_t previousPosition = 10;
uint8_t count = 3;

if (eventId == Clusters::Switch::Events::SwitchLatched::Id)
{
Clusters::SwitchServer::Instance().OnSwitchLatch(endpoint, newPosition);
}
else if (eventId == Clusters::Switch::Events::InitialPress::Id)
{
Clusters::SwitchServer::Instance().OnInitialPress(endpoint, newPosition);
}
else if (eventId == Clusters::Switch::Events::LongPress::Id)
{
Clusters::SwitchServer::Instance().OnLongPress(endpoint, newPosition);
}
else if (eventId == Clusters::Switch::Events::ShortRelease::Id)
{
Clusters::SwitchServer::Instance().OnShortRelease(endpoint, previousPosition);
}
else if (eventId == Clusters::Switch::Events::LongRelease::Id)
{
Clusters::SwitchServer::Instance().OnLongRelease(endpoint, previousPosition);
}
else if (eventId == Clusters::Switch::Events::MultiPressOngoing::Id)
{
Clusters::SwitchServer::Instance().OnMultiPressOngoing(endpoint, newPosition, count);
}
else if (eventId == Clusters::Switch::Events::MultiPressComplete::Id)
{
Clusters::SwitchServer::Instance().OnMultiPressComplete(endpoint, newPosition, count);
}
else
{
ChipLogError(DeviceLayer, "Unknow event ID:%d", eventId);
}
}

// when the shell is enabled, don't intercept signals since it prevents the user from
// using expected commands like CTRL-C to quit the application. (see issue #17845)
// We should stop using signals for those faults, and move to a different notification
Expand Down Expand Up @@ -223,6 +270,42 @@ void OnGeneralFaultSignalHandler(int signum)
PlatformMgr().ScheduleWork(HandleGeneralFaultEvent, static_cast<intptr_t>(eventId));
}

void OnSwitchSignalHandler(int signum)
{
ChipLogDetail(DeviceLayer, "Caught signal %d", signum);

uint32_t eventId;
switch (signum)
{
case SIGTSTP:
eventId = Clusters::Switch::Events::SwitchLatched::Id;
break;
case SIGSTOP:
eventId = Clusters::Switch::Events::InitialPress::Id;
break;
case SIGTTOU:
eventId = Clusters::Switch::Events::LongPress::Id;
break;
case SIGWINCH:
eventId = Clusters::Switch::Events::ShortRelease::Id;
break;
case SIGQUIT:
eventId = Clusters::Switch::Events::LongRelease::Id;
break;
case SIGFPE:
eventId = Clusters::Switch::Events::MultiPressOngoing::Id;
break;
case SIGPIPE:
eventId = Clusters::Switch::Events::MultiPressComplete::Id;
break;
default:
ChipLogError(NotSpecified, "Unhandled signal: Should never happens");
chipDie();
break;
}

PlatformMgr().ScheduleWork(HandleSwitchEvent, static_cast<intptr_t>(eventId));
}
void SetupSignalHandlers()
{
// sigaction is not used here because Tsan interceptors seems to
Expand All @@ -238,6 +321,13 @@ void SetupSignalHandlers()
signal(SIGUSR2, OnGeneralFaultSignalHandler);
signal(SIGHUP, OnGeneralFaultSignalHandler);
signal(SIGTTIN, OnGeneralFaultSignalHandler);
signal(SIGTSTP, OnSwitchSignalHandler);
signal(SIGSTOP, OnSwitchSignalHandler);
signal(SIGTTOU, OnSwitchSignalHandler);
signal(SIGWINCH, OnSwitchSignalHandler);
signal(SIGQUIT, OnSwitchSignalHandler);
signal(SIGFPE, OnSwitchSignalHandler);
signal(SIGPIPE, OnSwitchSignalHandler);
}
#endif // !defined(ENABLE_CHIP_SHELL)

Expand Down