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 Content App Command Delegate #34895

Merged
merged 9 commits into from
Aug 9, 2024
68 changes: 40 additions & 28 deletions examples/tv-app/android/java/ContentAppCommandDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,37 @@ 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<Json::CharReader> 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)
Expand Down Expand Up @@ -94,7 +125,15 @@ 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);
}
Expand Down Expand Up @@ -141,22 +180,19 @@ 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;
}

// Validate and access JSON data safely
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;
}
}
Expand Down Expand Up @@ -185,31 +221,7 @@ 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<Json::CharReader> 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;
}

// Validate and access JSON data safely
if (!value.isObject())
{
ChipLogError(Zcl, "Invalid JSON structure: not an object");
return;
}

if (!reader.parse(response, value))
{
return;
}

// handle errors from platform-app
if (!value[FAILURE_KEY].empty())
Expand Down
Loading