Skip to content

Commit

Permalink
Update Content App Command Delegate (#34895)
Browse files Browse the repository at this point in the history
* Update Content App Command Delegate

* Restyled by whitespace

* Restyled by clang-format

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Sep 5, 2024
1 parent b99a3a5 commit 30857e4
Showing 1 changed file with 40 additions and 28 deletions.
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

0 comments on commit 30857e4

Please sign in to comment.