Skip to content

Commit

Permalink
Don't parse JSON command if the previous one is currently in processi…
Browse files Browse the repository at this point in the history
…ng (#21433)
  • Loading branch information
yufengwangca authored and woody-apple committed Aug 1, 2022
1 parent c5e7ab6 commit 74df922
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 26 deletions.
69 changes: 47 additions & 22 deletions examples/all-clusters-app/linux/AllClustersCommandDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,40 @@ using namespace chip::app;
using namespace chip::app::Clusters;
using namespace chip::DeviceLayer;

void AllClustersCommandDelegate::OnEventCommandReceived(const char * json)
AllClustersAppCommandHandler * AllClustersAppCommandHandler::FromJSON(const char * json)
{
Json::Reader reader;
Json::Value value;

if (!reader.parse(json, mJsonValue))
if (!reader.parse(json, value))
{
ChipLogError(NotSpecified, "Error parsing JSON with error %s:", reader.getFormattedErrorMessages().c_str());
ChipLogError(NotSpecified,
"AllClusters App: Error parsing JSON with error %s:", reader.getFormattedErrorMessages().c_str());
return nullptr;
}
else

if (value.empty() || !value.isObject())
{
DeviceLayer::PlatformMgr().ScheduleWork(HandleEventCommand, reinterpret_cast<intptr_t>(this));
ChipLogError(NotSpecified, "AllClusters App: Invalid JSON command received");
return nullptr;
}
}

void AllClustersCommandDelegate::HandleEventCommand(intptr_t context)
{
auto * self = reinterpret_cast<AllClustersCommandDelegate *>(context);
if (!value.isMember("Name") || !value["Name"].isString())
{
ChipLogError(NotSpecified, "AllClusters App: Invalid JSON command received: command name is missing");
return nullptr;
}

VerifyOrReturn(!self->mJsonValue.empty(), ChipLogError(NotSpecified, "Invalid JSON event command received"));
return Platform::New<AllClustersAppCommandHandler>(value);
}

void AllClustersAppCommandHandler::HandleCommand(intptr_t context)
{
auto * self = reinterpret_cast<AllClustersAppCommandHandler *>(context);
std::string name = self->mJsonValue["Name"].asString();

VerifyOrExit(!self->mJsonValue.empty(), ChipLogError(NotSpecified, "Invalid JSON event command received"));

if (name == "SoftwareFault")
{
self->OnSoftwareFaultEventHandler(Clusters::SoftwareDiagnostics::Events::SoftwareFault::Id);
Expand Down Expand Up @@ -136,17 +148,18 @@ void AllClustersCommandDelegate::HandleEventCommand(intptr_t context)
ChipLogError(NotSpecified, "Unhandled command: Should never happens");
}

self->mJsonValue.clear();
exit:
Platform::Delete(self);
}

bool AllClustersCommandDelegate::IsClusterPresentOnAnyEndpoint(ClusterId clusterId)
bool AllClustersAppCommandHandler::IsClusterPresentOnAnyEndpoint(ClusterId clusterId)
{
EnabledEndpointsWithServerCluster enabledEndpoints(clusterId);

return (enabledEndpoints.begin() != enabledEndpoints.end());
}

void AllClustersCommandDelegate::OnRebootSignalHandler(BootReasonType bootReason)
void AllClustersAppCommandHandler::OnRebootSignalHandler(BootReasonType bootReason)
{
if (ConfigurationMgr().StoreBootReason(static_cast<uint32_t>(bootReason)) != CHIP_NO_ERROR)
{
Expand All @@ -158,7 +171,7 @@ void AllClustersCommandDelegate::OnRebootSignalHandler(BootReasonType bootReason
}
}

void AllClustersCommandDelegate::OnGeneralFaultEventHandler(uint32_t eventId)
void AllClustersAppCommandHandler::OnGeneralFaultEventHandler(uint32_t eventId)
{
if (!IsClusterPresentOnAnyEndpoint(Clusters::GeneralDiagnostics::Id))
return;
Expand Down Expand Up @@ -219,7 +232,7 @@ void AllClustersCommandDelegate::OnGeneralFaultEventHandler(uint32_t eventId)
}
}

void AllClustersCommandDelegate::OnSoftwareFaultEventHandler(uint32_t eventId)
void AllClustersAppCommandHandler::OnSoftwareFaultEventHandler(uint32_t eventId)
{
VerifyOrReturn(eventId == Clusters::SoftwareDiagnostics::Events::SoftwareFault::Id,
ChipLogError(NotSpecified, "Unknown software fault event received"));
Expand All @@ -242,7 +255,7 @@ void AllClustersCommandDelegate::OnSoftwareFaultEventHandler(uint32_t eventId)
Clusters::SoftwareDiagnosticsServer::Instance().OnSoftwareFaultDetect(softwareFault);
}

void AllClustersCommandDelegate::OnSwitchLatchedHandler(uint8_t newPosition)
void AllClustersAppCommandHandler::OnSwitchLatchedHandler(uint8_t newPosition)
{
EndpointId endpoint = 1;

Expand All @@ -253,7 +266,7 @@ void AllClustersCommandDelegate::OnSwitchLatchedHandler(uint8_t newPosition)
Clusters::SwitchServer::Instance().OnSwitchLatch(endpoint, newPosition);
}

void AllClustersCommandDelegate::OnSwitchInitialPressedHandler(uint8_t newPosition)
void AllClustersAppCommandHandler::OnSwitchInitialPressedHandler(uint8_t newPosition)
{
EndpointId endpoint = 1;

Expand All @@ -264,7 +277,7 @@ void AllClustersCommandDelegate::OnSwitchInitialPressedHandler(uint8_t newPositi
Clusters::SwitchServer::Instance().OnInitialPress(endpoint, newPosition);
}

void AllClustersCommandDelegate::OnSwitchLongPressedHandler(uint8_t newPosition)
void AllClustersAppCommandHandler::OnSwitchLongPressedHandler(uint8_t newPosition)
{
EndpointId endpoint = 1;

Expand All @@ -275,7 +288,7 @@ void AllClustersCommandDelegate::OnSwitchLongPressedHandler(uint8_t newPosition)
Clusters::SwitchServer::Instance().OnLongPress(endpoint, newPosition);
}

void AllClustersCommandDelegate::OnSwitchShortReleasedHandler(uint8_t previousPosition)
void AllClustersAppCommandHandler::OnSwitchShortReleasedHandler(uint8_t previousPosition)
{
EndpointId endpoint = 1;

Expand All @@ -287,7 +300,7 @@ void AllClustersCommandDelegate::OnSwitchShortReleasedHandler(uint8_t previousPo
Clusters::SwitchServer::Instance().OnShortRelease(endpoint, previousPosition);
}

void AllClustersCommandDelegate::OnSwitchLongReleasedHandler(uint8_t previousPosition)
void AllClustersAppCommandHandler::OnSwitchLongReleasedHandler(uint8_t previousPosition)
{
EndpointId endpoint = 1;

Expand All @@ -301,7 +314,7 @@ void AllClustersCommandDelegate::OnSwitchLongReleasedHandler(uint8_t previousPos
Clusters::SwitchServer::Instance().OnLongRelease(endpoint, previousPosition);
}

void AllClustersCommandDelegate::OnSwitchMultiPressOngoingHandler(uint8_t newPosition, uint8_t count)
void AllClustersAppCommandHandler::OnSwitchMultiPressOngoingHandler(uint8_t newPosition, uint8_t count)
{
EndpointId endpoint = 1;

Expand All @@ -314,7 +327,7 @@ void AllClustersCommandDelegate::OnSwitchMultiPressOngoingHandler(uint8_t newPos
Clusters::SwitchServer::Instance().OnMultiPressOngoing(endpoint, newPosition, count);
}

void AllClustersCommandDelegate::OnSwitchMultiPressCompleteHandler(uint8_t previousPosition, uint8_t count)
void AllClustersAppCommandHandler::OnSwitchMultiPressCompleteHandler(uint8_t previousPosition, uint8_t count)
{
EndpointId endpoint = 1;

Expand All @@ -326,3 +339,15 @@ void AllClustersCommandDelegate::OnSwitchMultiPressCompleteHandler(uint8_t previ

Clusters::SwitchServer::Instance().OnMultiPressComplete(endpoint, previousPosition, count);
}

void AllClustersCommandDelegate::OnEventCommandReceived(const char * json)
{
auto handler = AllClustersAppCommandHandler::FromJSON(json);
if (nullptr == handler)
{
ChipLogError(NotSpecified, "AllClusters App: Unable to instantiate a command handler");
return;
}

chip::DeviceLayer::PlatformMgr().ScheduleWork(AllClustersAppCommandHandler::HandleCommand, reinterpret_cast<intptr_t>(handler));
}
16 changes: 12 additions & 4 deletions examples/all-clusters-app/linux/AllClustersCommandDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@
#include <json/json.h>
#include <platform/DiagnosticDataProvider.h>

class AllClustersCommandDelegate : public NamedPipeCommandDelegate
class AllClustersAppCommandHandler
{
public:
void OnEventCommandReceived(const char * json) override;
static AllClustersAppCommandHandler * FromJSON(const char * json);

static void HandleCommand(intptr_t context);

AllClustersAppCommandHandler(Json::Value jasonValue) : mJsonValue(std::move(jasonValue)) {}

private:
Json::Value mJsonValue;

static void HandleEventCommand(intptr_t context);

bool IsClusterPresentOnAnyEndpoint(chip::ClusterId clusterId);

/**
Expand Down Expand Up @@ -87,3 +89,9 @@ class AllClustersCommandDelegate : public NamedPipeCommandDelegate
*/
void OnSwitchMultiPressCompleteHandler(uint8_t previousPosition, uint8_t count);
};

class AllClustersCommandDelegate : public NamedPipeCommandDelegate
{
public:
void OnEventCommandReceived(const char * json) override;
};

0 comments on commit 74df922

Please sign in to comment.