Skip to content

Commit

Permalink
Fix the issue of unreleased JNI references. (#23655)
Browse files Browse the repository at this point in the history
* Fix the issue of unreleased JNI references.

* Restyled by clang-format

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Oct 20, 2023
1 parent 6ec144a commit 4015364
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,27 @@ CHIP_ERROR AppContentLauncherManager::HandleGetAcceptHeaderList(AttributeValueEn
ChipLogProgress(Zcl, "AppContentLauncherManager::HandleGetAcceptHeaderList");
chip::app::ConcreteReadAttributePath aPath(mEndpointId, chip::app::Clusters::ContentLauncher::Id,
chip::app::Clusters::ContentLauncher::Attributes::AcceptHeader::Id);
const char * resStr = mAttributeDelegate->Read(aPath);
ChipLogProgress(Zcl, "AppContentLauncherManager::HandleGetSupportedStreamingProtocols response %s", resStr);
std::string resStr = mAttributeDelegate->Read(aPath);
ChipLogProgress(Zcl, "AppContentLauncherManager::HandleGetAcceptHeaderList response %s", resStr.c_str());

if (resStr != nullptr && *resStr != 0)
if (resStr.length() != 0)
{
Json::Reader reader;
Json::Value value;
if (reader.parse(resStr, value))
{
std::string attrId = to_string(chip::app::Clusters::ContentLauncher::Attributes::AcceptHeader::Id);
ChipLogProgress(
Zcl, "AppContentLauncherManager::HandleGetSupportedStreamingProtocols response parsing done. reading attr %s",
attrId.c_str());
ChipLogProgress(Zcl, "AppContentLauncherManager::HandleGetAcceptHeaderList response parsing done. reading attr %s",
attrId.c_str());
if (value[attrId].isArray())
{
mAcceptHeaderList.clear();
for (Json::Value & entry : value[attrId])
{
mAcceptHeaderList.push_back(entry.asString());
if (entry.isString())
{
mAcceptHeaderList.push_back(entry.asString());
}
}
}
}
Expand All @@ -124,10 +126,10 @@ uint32_t AppContentLauncherManager::HandleGetSupportedStreamingProtocols()
ChipLogProgress(Zcl, "AppContentLauncherManager::HandleGetSupportedStreamingProtocols");
chip::app::ConcreteReadAttributePath aPath(mEndpointId, chip::app::Clusters::ContentLauncher::Id,
chip::app::Clusters::ContentLauncher::Attributes::SupportedStreamingProtocols::Id);
const char * resStr = mAttributeDelegate->Read(aPath);
ChipLogProgress(Zcl, "AppContentLauncherManager::HandleGetSupportedStreamingProtocols response %s", resStr);
std::string resStr = mAttributeDelegate->Read(aPath);
ChipLogProgress(Zcl, "AppContentLauncherManager::HandleGetSupportedStreamingProtocols response %s", resStr.c_str());

if (resStr == nullptr || *resStr == 0)
if (resStr.length() == 0)
{
return mSupportedStreamingProtocols;
}
Expand All @@ -141,7 +143,7 @@ uint32_t AppContentLauncherManager::HandleGetSupportedStreamingProtocols()
std::string attrId = to_string(chip::app::Clusters::ContentLauncher::Attributes::SupportedStreamingProtocols::Id);
ChipLogProgress(Zcl, "AppContentLauncherManager::HandleGetSupportedStreamingProtocols response parsing done. reading attr %s",
attrId.c_str());
if (!value[attrId].empty())
if (!value[attrId].empty() && value[attrId].isInt())
{
uint32_t supportedStreamingProtocols = static_cast<uint32_t>(value[attrId].asInt());
mSupportedStreamingProtocols = supportedStreamingProtocols;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ uint64_t AppMediaPlaybackManager::HandleMediaRequestGetAttribute(chip::Attribute
{
ChipLogProgress(Zcl, "Received AppMediaPlaybackManager::HandleMediaRequestGetAttribute:%d", attributeId);
chip::app::ConcreteReadAttributePath aPath(mEndpointId, chip::app::Clusters::MediaPlayback::Id, attributeId);
const char * resStr = mAttributeDelegate->Read(aPath);
ChipLogProgress(Zcl, "AppMediaPlaybackManager::HandleMediaRequestGetAttribute response %s", resStr);
std::string resStr = mAttributeDelegate->Read(aPath);
ChipLogProgress(Zcl, "AppMediaPlaybackManager::HandleMediaRequestGetAttribute response %s", resStr.c_str());

uint64_t ret = std::numeric_limits<uint64_t>::max();
if (resStr != nullptr && *resStr != 0)
if (resStr.length() != 0)
{
Json::Reader reader;
Json::Value value;
Expand All @@ -143,7 +143,7 @@ uint64_t AppMediaPlaybackManager::HandleMediaRequestGetAttribute(chip::Attribute
std::string attrId = to_string(attributeId);
ChipLogProgress(Zcl, "AppMediaPlaybackManager::HandleMediaRequestGetAttribute response parsing done. reading attr %s",
attrId.c_str());
if (!value[attrId].empty())
if (!value[attrId].empty() && value[attrId].isUInt())
{
ret = static_cast<uint64_t>(value[attrId].asUInt());
return ret;
Expand Down Expand Up @@ -177,9 +177,10 @@ CHIP_ERROR AppMediaPlaybackManager::HandleGetSampledPosition(AttributeValueEncod
ChipLogProgress(Zcl, "AppMediaPlaybackManager::HandleGetSampledPosition");
chip::app::ConcreteReadAttributePath aPath(mEndpointId, chip::app::Clusters::MediaPlayback::Id,
chip::app::Clusters::MediaPlayback::Attributes::SampledPosition::Id);
const char * resStr = mAttributeDelegate->Read(aPath);
std::string resStr = mAttributeDelegate->Read(aPath);
ChipLogProgress(Zcl, "AppMediaPlaybackManager::HandleGetSampledPosition response %s", resStr.c_str());

if (resStr != nullptr && *resStr != 0)
if (resStr.length() != 0)
{
Json::Reader reader;
Json::Value value;
Expand All @@ -188,13 +189,14 @@ CHIP_ERROR AppMediaPlaybackManager::HandleGetSampledPosition(AttributeValueEncod
std::string attrId = to_string(chip::app::Clusters::MediaPlayback::Attributes::SampledPosition::Id);
ChipLogProgress(Zcl, "AppContentLauncherManager::HandleGetSampledPosition response parsing done. reading attr %s",
attrId.c_str());
if (!value[attrId].empty())
if (!value[attrId].empty() && value[attrId].isObject())
{
std::string updatedAt = to_string(
static_cast<uint32_t>(chip::app::Clusters::MediaPlayback::Structs::PlaybackPosition::Fields::kUpdatedAt));
std::string position = to_string(
static_cast<uint32_t>(chip::app::Clusters::MediaPlayback::Structs::PlaybackPosition::Fields::kPosition));
if (!value[attrId][updatedAt].empty() && !value[attrId][position].empty())
if (!value[attrId][updatedAt].empty() && !value[attrId][position].empty() && value[attrId][updatedAt].isUInt() &&
value[attrId][position].isUInt())
{
// valid response
response.updatedAt = value[attrId][updatedAt].asUInt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ CHIP_ERROR TargetNavigatorManager::HandleGetTargetList(AttributeValueEncoder & a
{
chip::app::ConcreteReadAttributePath aPath(mEndpointId, chip::app::Clusters::TargetNavigator::Id,
chip::app::Clusters::TargetNavigator::Attributes::TargetList::Id);
const char * resStr = mAttributeDelegate->Read(aPath);
ChipLogProgress(Zcl, "TargetNavigatorManager::HandleNavigateTarget response %s", resStr);
std::string resStr = mAttributeDelegate->Read(aPath);
ChipLogProgress(Zcl, "TargetNavigatorManager::HandleNavigateTarget response %s", resStr.c_str());

if (resStr != nullptr && *resStr != 0)
if (resStr.length() != 0)
{
Json::Reader reader;
Json::Value value;
Expand Down Expand Up @@ -104,10 +104,10 @@ uint8_t TargetNavigatorManager::HandleGetCurrentTarget()
{
chip::app::ConcreteReadAttributePath aPath(mEndpointId, chip::app::Clusters::TargetNavigator::Id,
chip::app::Clusters::TargetNavigator::Attributes::TargetList::Id);
const char * resStr = mAttributeDelegate->Read(aPath);
ChipLogProgress(Zcl, "TargetNavigatorManager::HandleGetCurrentTarget response %s", resStr);
std::string resStr = mAttributeDelegate->Read(aPath);
ChipLogProgress(Zcl, "TargetNavigatorManager::HandleGetCurrentTarget response %s", resStr.c_str());

if (resStr != nullptr && *resStr != 0)
if (resStr.length() != 0)
{
Json::Reader reader;
Json::Value value;
Expand Down
11 changes: 8 additions & 3 deletions examples/tv-app/android/java/ContentAppAttributeDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ namespace AppPlatform {

using LaunchResponseType = chip::app::Clusters::ContentLauncher::Commands::LaunchResponse::Type;

const char * ContentAppAttributeDelegate::Read(const chip::app::ConcreteReadAttributePath & aPath)
std::string ContentAppAttributeDelegate::Read(const chip::app::ConcreteReadAttributePath & aPath)
{
if (aPath.mEndpointId < FIXED_ENDPOINT_COUNT)
{
// returning blank string causes the caller to default to output required by the tests
return "";
}
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();

JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
ChipLogProgress(Zcl, "ContentAppAttributeDelegate::Read being called for endpoint %d cluster %d attribute %d",
aPath.mEndpointId, aPath.mClusterId, aPath.mAttributeId);

Expand All @@ -53,11 +54,15 @@ const char * ContentAppAttributeDelegate::Read(const chip::app::ConcreteReadAttr
ChipLogError(Zcl, "Java exception in ContentAppAttributeDelegate::Read");
env->ExceptionDescribe();
env->ExceptionClear();
// returning blank string causes the caller to default to output required by the tests
return "";
}
const char * respStr = env->GetStringUTFChars(resp, 0);
std::string retStr(respStr);
env->ReleaseStringUTFChars(resp, respStr);
env->DeleteLocalRef(resp);
ChipLogProgress(Zcl, "ContentAppAttributeDelegate::Read got response %s", respStr);
return respStr;
return retStr;
}

} // namespace AppPlatform
Expand Down
2 changes: 1 addition & 1 deletion examples/tv-app/android/java/ContentAppAttributeDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ContentAppAttributeDelegate
env->DeleteGlobalRef(mContentAppEndpointManager);
}

const char * Read(const chip::app::ConcreteReadAttributePath & aPath);
std::string Read(const chip::app::ConcreteReadAttributePath & aPath);

private:
void InitializeJNIObjects(jobject manager)
Expand Down
12 changes: 7 additions & 5 deletions examples/tv-app/android/java/ContentAppCommandDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ void ContentAppCommandDelegate::InvokeCommand(CommandHandlerInterface::HandlerCo
return;
}

JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
Json::Value value = json["value"];
UtfString jsonString(env, JsonToString(value).c_str());
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
Json::Value value = json["value"];
std::string payload = JsonToString(value);
UtfString jsonString(env, payload.c_str());

ChipLogProgress(Zcl, "ContentAppCommandDelegate::InvokeCommand send command being called with payload %s",
JsonToString(json).c_str());
ChipLogProgress(Zcl, "ContentAppCommandDelegate::InvokeCommand send command being called with payload %s", payload.c_str());

jstring resp = (jstring) env->CallObjectMethod(
mContentAppEndpointManager, mSendCommandMethod, static_cast<jint>(handlerContext.mRequestPath.mEndpointId),
Expand All @@ -82,6 +82,8 @@ void ContentAppCommandDelegate::InvokeCommand(CommandHandlerInterface::HandlerCo
const char * respStr = env->GetStringUTFChars(resp, 0);
ChipLogProgress(Zcl, "ContentAppCommandDelegate::InvokeCommand got response %s", respStr);
FormatResponseData(handlerContext, respStr);
env->ReleaseStringUTFChars(resp, respStr);
env->DeleteLocalRef(resp);
}
else
{
Expand Down

0 comments on commit 4015364

Please sign in to comment.