Skip to content

Commit

Permalink
Addressed comments by yunhanw-google related to error checking and lo…
Browse files Browse the repository at this point in the history
…gging and also fixed a tv-casting-app connection bug
  • Loading branch information
pgregorr-amazon committed Apr 16, 2024
1 parent ef44226 commit 66ff5bf
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ public void onResume() {
public void onPause() {
super.onPause();
Log.i(TAG, "onPause() called");
stopDiscovery();
}

/** Interface for notifying the host. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ JNI_METHOD(jobject, removeCastingPlayerChangeListener)(JNIEnv * env, jobject, jo

return support::convertMatterErrorFromCppToJava(CHIP_NO_ERROR);
}
else if (DiscoveryDelegateImpl::GetInstance()->castingPlayerChangeListenerJavaObject.ObjectRef() == nullptr)
else if (!DiscoveryDelegateImpl::GetInstance()->castingPlayerChangeListenerJavaObject.HasValidObjectRef())
{
ChipLogError(
AppServer,
Expand Down
19 changes: 17 additions & 2 deletions src/platform/android/DnssdImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ CHIP_ERROR ChipDnssdBrowse(const char * type, DnssdServiceProtocol protocol, Ine

std::string serviceType = GetFullTypeWithSubTypes(type, protocol);
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
VerifyOrReturnError(env != nullptr, CHIP_JNI_ERROR_NO_ENV,
ChipLogError(Discovery, "Failed to GetEnvForCurrentThread for ChipDnssdBrowse"));
UtfString jniServiceType(env, serviceType.c_str());

env->CallVoidMethod(sBrowserObject.ObjectRef(), sBrowseMethod, jniServiceType.jniValue(), reinterpret_cast<jlong>(callback),
Expand All @@ -204,22 +206,29 @@ CHIP_ERROR ChipDnssdBrowse(const char * type, DnssdServiceProtocol protocol, Ine
return CHIP_JNI_ERROR_EXCEPTION_THROWN;
}

auto sdCtx = chip::Platform::New<BrowseContext>(callback);
auto sdCtx = chip::Platform::New<BrowseContext>(callback);
VerifyOrReturnError(nullptr != sdCtx, CHIP_ERROR_NO_MEMORY,
ChipLogError(Discovery, "Failed allocate memory for BrowseContext in ChipDnssdBrowse"));
*browseIdentifier = reinterpret_cast<intptr_t>(sdCtx);

return CHIP_NO_ERROR;
}

CHIP_ERROR ChipDnssdStopBrowse(intptr_t browseIdentifier)
{
VerifyOrReturnError(browseIdentifier != 0, CHIP_ERROR_INVALID_ARGUMENT,
ChipLogError(Discovery, "ChipDnssdStopBrowse Invalid argument browseIdentifier = 0"));
VerifyOrReturnError(sBrowserObject.HasValidObjectRef() && sStopBrowseMethod != nullptr, CHIP_ERROR_INVALID_ARGUMENT);

JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
auto ctx = reinterpret_cast<BrowseContext *>(browseIdentifier);
VerifyOrReturnError(env != nullptr, CHIP_JNI_ERROR_NO_ENV,
ChipLogError(Discovery, "Failed to GetEnvForCurrentThread for ChipDnssdStopBrowse"));
auto ctx = reinterpret_cast<BrowseContext *>(browseIdentifier);

env->CallVoidMethod(sBrowserObject.ObjectRef(), sStopBrowseMethod, reinterpret_cast<jlong>(ctx->callback));

chip::Platform::Delete(ctx);
ctx = nullptr;
if (env->ExceptionCheck())
{
ChipLogError(Discovery, "Java exception in ChipDnssdStopBrowse");
Expand Down Expand Up @@ -339,6 +348,12 @@ void InitializeWithObjects(jobject resolverObject, jobject browserObject, jobjec
env->ExceptionClear();
}

if (sStopBrowseMethod == nullptr)
{
ChipLogError(Discovery, "Failed to access Discover 'stopDiscover' method");
env->ExceptionClear();
}

if (sGetTextEntryKeysMethod == nullptr)
{
ChipLogError(Discovery, "Failed to access MdnsCallback 'getTextEntryKeys' method");
Expand Down

0 comments on commit 66ff5bf

Please sign in to comment.