From 4256669199a7f1ff801607060e25bd786db9e379 Mon Sep 17 00:00:00 2001 From: Lazar Kovacic Date: Thu, 8 Aug 2024 23:34:40 +0200 Subject: [PATCH] Update Content App Command Delegate --- .../java/ContentAppCommandDelegate.cpp | 45 +++++++++++++++---- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/examples/tv-app/android/java/ContentAppCommandDelegate.cpp b/examples/tv-app/android/java/ContentAppCommandDelegate.cpp index 8d459588a7a8d6..3c28ab4d3b61a2 100644 --- a/examples/tv-app/android/java/ContentAppCommandDelegate.cpp +++ b/examples/tv-app/android/java/ContentAppCommandDelegate.cpp @@ -49,6 +49,35 @@ using Status = chip::Protocols::InteractionModel::Status; const std::string FAILURE_KEY = "PlatformError"; const std::string FAILURE_STATUS_KEY = "Status"; +bool isValidJson(const char * response) +{ + Json::Reader reader; + + Json::CharReaderBuilder readerBuilder; + std::string errors; + + Json::Value value; + std::unique_ptr testReader(readerBuilder.newCharReader()); + + if (!testReader->parse(response, response + std::strlen(response), &value, &errors)) { + ChipLogError(Zcl, "Failed to parse JSON: %s\n", errors.c_str()); + return false; + } + + // Validate and access JSON data safely + if (!value.isObject()) { + ChipLogError(Zcl, "Invalid JSON structure: not an object"); + return false; + } + + if (!reader.parse(response, value)) + { + return false; + } + + return true; +} + void ContentAppCommandDelegate::InvokeCommand(CommandHandlerInterface::HandlerContext & handlerContext) { if (handlerContext.mRequestPath.mEndpointId >= FIXED_ENDPOINT_COUNT) @@ -94,7 +123,13 @@ void ContentAppCommandDelegate::InvokeCommand(CommandHandlerInterface::HandlerCo { JniUtfString respStr(env, resp); ChipLogProgress(Zcl, "ContentAppCommandDelegate::InvokeCommand got response %s", respStr.c_str()); - FormatResponseData(handlerContext, respStr.c_str()); + if (isValidJson(respStr.c_str())) { + FormatResponseData(handlerContext, respStr.c_str()); + } else { + // return dummy value in case JSON is invalid + FormatResponseData(handlerContext, "{\"value\":{}}"); + } + } env->DeleteLocalRef(resp); } @@ -141,7 +176,6 @@ Status ContentAppCommandDelegate::InvokeCommand(EndpointId epId, ClusterId clust if (!testReader->parse(respStr.c_str(), respStr.c_str() + std::strlen(respStr.c_str()), &value, &errors)) { ChipLogError(Zcl, "Failed to parse JSON: %s\n", errors.c_str()); - env->DeleteLocalRef(resp); return chip::Protocols::InteractionModel::Status::Failure; } @@ -149,14 +183,12 @@ Status ContentAppCommandDelegate::InvokeCommand(EndpointId epId, ClusterId clust if (!value.isObject()) { ChipLogError(Zcl, "Invalid JSON structure: not an object"); - env->DeleteLocalRef(resp); return chip::Protocols::InteractionModel::Status::Failure; } Json::Reader reader; if (!reader.parse(respStr.c_str(), value)) { - env->DeleteLocalRef(resp); return chip::Protocols::InteractionModel::Status::Failure; } } @@ -185,11 +217,6 @@ Status ContentAppCommandDelegate::InvokeCommand(EndpointId epId, ClusterId clust void ContentAppCommandDelegate::FormatResponseData(CommandHandlerInterface::HandlerContext & handlerContext, const char * response) { handlerContext.SetCommandHandled(); - Json::Reader reader; - - Json::CharReaderBuilder readerBuilder; - std::string errors; - Json::Value value; std::unique_ptr testReader(readerBuilder.newCharReader());