diff --git a/examples/all-clusters-app/linux/AllClustersCommandDelegate.cpp b/examples/all-clusters-app/linux/AllClustersCommandDelegate.cpp index 3bdc63319c972d..5e3238419cb5e8 100644 --- a/examples/all-clusters-app/linux/AllClustersCommandDelegate.cpp +++ b/examples/all-clusters-app/linux/AllClustersCommandDelegate.cpp @@ -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(this)); + ChipLogError(NotSpecified, "AllClusters App: Invalid JSON command received"); + return nullptr; } -} -void AllClustersCommandDelegate::HandleEventCommand(intptr_t context) -{ - auto * self = reinterpret_cast(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(value); +} +void AllClustersAppCommandHandler::HandleCommand(intptr_t context) +{ + auto * self = reinterpret_cast(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); @@ -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(bootReason)) != CHIP_NO_ERROR) { @@ -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; @@ -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")); @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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(handler)); +} diff --git a/examples/all-clusters-app/linux/AllClustersCommandDelegate.h b/examples/all-clusters-app/linux/AllClustersCommandDelegate.h index 5a55319ed674aa..d53b6a6b406f85 100644 --- a/examples/all-clusters-app/linux/AllClustersCommandDelegate.h +++ b/examples/all-clusters-app/linux/AllClustersCommandDelegate.h @@ -23,16 +23,18 @@ #include #include -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); /** @@ -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; +};