Skip to content

Commit

Permalink
Update Content App Command Delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
lazarkov committed Aug 8, 2024
1 parent 17fd793 commit 4256669
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions examples/tv-app/android/java/ContentAppCommandDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 +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);
}
Expand Down Expand Up @@ -141,22 +176,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,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<Json::CharReader> testReader(readerBuilder.newCharReader());

Expand Down

0 comments on commit 4256669

Please sign in to comment.