From 59e2803bd3db39bc2a2ee29f3d94ab1b92a9f720 Mon Sep 17 00:00:00 2001 From: Joonhaeng Heo Date: Wed, 22 May 2024 14:38:33 +0900 Subject: [PATCH] Fix android icd checkin message --- .../google/chip/chiptool/CHIPToolActivity.kt | 10 ++++++++++ .../com/google/chip/chiptool/ChipClient.kt | 15 ++++++++++++++ .../clusterclient/AddressUpdateFragment.kt | 4 ++++ .../java/AndroidDeviceControllerWrapper.cpp | 12 +++++++++++ .../java/AndroidDeviceControllerWrapper.h | 4 ++++ .../java/CHIPDeviceController-JNI.cpp | 20 +++++++++++++++++++ .../ChipDeviceController.java | 12 +++++++++++ .../platform/NsdManagerServiceResolver.java | 13 +++++++++--- 8 files changed, 87 insertions(+), 3 deletions(-) diff --git a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/CHIPToolActivity.kt b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/CHIPToolActivity.kt index f5a451f240f0e3..2b2078dde5e688 100644 --- a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/CHIPToolActivity.kt +++ b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/CHIPToolActivity.kt @@ -238,6 +238,16 @@ class CHIPToolActivity : } } + override fun onResume() { + super.onResume() + ChipClient.startDnssd(this) + } + + override fun onPause() { + ChipClient.stopDnssd(this) + super.onPause() + } + companion object { private const val TAG = "CHIPToolActivity" private const val ADDRESS_COMMISSIONING_FRAGMENT_TAG = "address_commissioning_fragment" diff --git a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/ChipClient.kt b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/ChipClient.kt index bee06caf2b50d6..de8e598ee0ed9c 100644 --- a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/ChipClient.kt +++ b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/ChipClient.kt @@ -114,6 +114,21 @@ object ChipClient { icdCheckInCallback = callback } + fun startDnssd(context: Context) { + if (!this::chipDeviceController.isInitialized) { + getDeviceController(context) + } else { + chipDeviceController.startDnssd() + } + } + + fun stopDnssd(context: Context) { + if (!this::chipDeviceController.isInitialized) { + getDeviceController(context) + } + chipDeviceController.stopDnssd() + } + /** * Wrapper around [ChipDeviceController.getConnectedDevicePointer] to return the value directly. */ diff --git a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/AddressUpdateFragment.kt b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/AddressUpdateFragment.kt index 43eeb2b892c51c..81693c94d355cf 100644 --- a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/AddressUpdateFragment.kt +++ b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/AddressUpdateFragment.kt @@ -230,6 +230,10 @@ class AddressUpdateFragment : ICDCheckInCallback, Fragment() { val runnable = object : Runnable { override fun run() { + if (!isAdded) { + Log.d(TAG, "Fragment is not attached") + return + } if (icdTotalRemainStayActiveTimeMs >= ICD_PROGRESS_STEP) { icdDeviceRemainStayActiveTimeMs -= ICD_PROGRESS_STEP icdTotalRemainStayActiveTimeMs -= ICD_PROGRESS_STEP diff --git a/src/controller/java/AndroidDeviceControllerWrapper.cpp b/src/controller/java/AndroidDeviceControllerWrapper.cpp index 1576e5883ca256..ac997d48607476 100644 --- a/src/controller/java/AndroidDeviceControllerWrapper.cpp +++ b/src/controller/java/AndroidDeviceControllerWrapper.cpp @@ -25,6 +25,8 @@ #include +#include + #include #include #include @@ -1085,3 +1087,13 @@ CHIP_ERROR AndroidDeviceControllerWrapper::SyncDeleteKeyValue(const char * key) ChipLogProgress(chipTool, "KVS: Deleting key %s", StringOrNullMarker(key)); return chip::DeviceLayer::PersistedStorage::KeyValueStoreMgr().Delete(key); } + +void AndroidDeviceControllerWrapper::StartDnssd() { + FabricTable * fabricTable = DeviceControllerFactory::GetInstance().GetSystemState()->Fabrics(); + chip::app::DnssdServer::Instance().SetFabricTable(fabricTable); + chip::app::DnssdServer::Instance().StartServer(); +} + +void AndroidDeviceControllerWrapper::StopDnssd() { + chip::app::DnssdServer::Instance().StopServer(); +} diff --git a/src/controller/java/AndroidDeviceControllerWrapper.h b/src/controller/java/AndroidDeviceControllerWrapper.h index 51beae2d0eba48..1d26d31d112774 100644 --- a/src/controller/java/AndroidDeviceControllerWrapper.h +++ b/src/controller/java/AndroidDeviceControllerWrapper.h @@ -214,6 +214,10 @@ class AndroidDeviceControllerWrapper : public chip::Controller::DevicePairingDel CHIP_ERROR SetICDCheckInDelegate(jobject checkInDelegate); + void StartDnssd(); + + void StopDnssd(); + private: using ChipDeviceControllerPtr = std::unique_ptr; diff --git a/src/controller/java/CHIPDeviceController-JNI.cpp b/src/controller/java/CHIPDeviceController-JNI.cpp index 9b3d10a2bf1d31..786c9e55569088 100644 --- a/src/controller/java/CHIPDeviceController-JNI.cpp +++ b/src/controller/java/CHIPDeviceController-JNI.cpp @@ -2154,6 +2154,26 @@ JNI_METHOD(jbyteArray, validateAndExtractCSR)(JNIEnv * env, jclass clazz, jbyteA return javaCsr; } +JNI_METHOD(void, startDnssd)(JNIEnv * env, jobject self, jlong handle) +{ + ChipLogProgress(Controller, "startDnssd() called"); + chip::DeviceLayer::StackLock lock; + + AndroidDeviceControllerWrapper * wrapper = AndroidDeviceControllerWrapper::FromJNIHandle(handle); + + wrapper->StartDnssd(); +} + +JNI_METHOD(void, stopDnssd)(JNIEnv * env, jobject self, jlong handle) +{ + ChipLogProgress(Controller, "stopDnssd() called"); + chip::DeviceLayer::StackLock lock; + + AndroidDeviceControllerWrapper * wrapper = AndroidDeviceControllerWrapper::FromJNIHandle(handle); + + wrapper->StopDnssd(); +} + void * IOThreadMain(void * arg) { JNIEnv * env; diff --git a/src/controller/java/src/chip/devicecontroller/ChipDeviceController.java b/src/controller/java/src/chip/devicecontroller/ChipDeviceController.java index 653cebf89d1030..4cf4c9ee0f83cc 100644 --- a/src/controller/java/src/chip/devicecontroller/ChipDeviceController.java +++ b/src/controller/java/src/chip/devicecontroller/ChipDeviceController.java @@ -803,6 +803,14 @@ public byte[] getAttestationChallenge(long devicePtr) { return getAttestationChallenge(deviceControllerPtr, devicePtr); } + public void startDnssd() { + startDnssd(deviceControllerPtr); + } + + public void stopDnssd() { + stopDnssd(deviceControllerPtr); + } + /** * @brief Auto-Resubscribe to the given attribute path with keepSubscriptions and isFabricFiltered * @param SubscriptionEstablishedCallback Callback when a subscribe response has been received and @@ -1587,6 +1595,10 @@ private native void updateCommissioningICDRegistrationInfo( private native void shutdownCommissioning(long deviceControllerPtr); + private native void startDnssd(long deviceControllerPtr); + + private native void stopDnssd(long deviceControllerPtr); + static { System.loadLibrary("CHIPController"); } diff --git a/src/platform/android/java/chip/platform/NsdManagerServiceResolver.java b/src/platform/android/java/chip/platform/NsdManagerServiceResolver.java index 1f1a9efa4307f0..11b53537bf7d71 100644 --- a/src/platform/android/java/chip/platform/NsdManagerServiceResolver.java +++ b/src/platform/android/java/chip/platform/NsdManagerServiceResolver.java @@ -39,6 +39,7 @@ public class NsdManagerServiceResolver implements ServiceResolver { private static final long RESOLVE_SERVICE_TIMEOUT = 30000; private final NsdManager nsdManager; private MulticastLock multicastLock; + private MulticastLock publishMulticastLock; private List registrationListeners = new ArrayList<>(); private final CopyOnWriteArrayList mMFServiceName = new CopyOnWriteArrayList<>(); @Nullable private final NsdManagerResolverAvailState nsdManagerResolverAvailState; @@ -60,6 +61,12 @@ public NsdManagerServiceResolver( ((WifiManager) context.getSystemService(Context.WIFI_SERVICE)) .createMulticastLock("chipMulticastLock"); this.multicastLock.setReferenceCounted(true); + + this.publishMulticastLock = + ((WifiManager) context.getSystemService(Context.WIFI_SERVICE)) + .createMulticastLock("chipPublishMulticastLock"); + this.publishMulticastLock.setReferenceCounted(true); + this.nsdManagerResolverAvailState = nsdManagerResolverAvailState; this.timeout = timeout; } @@ -204,7 +211,7 @@ public void onServiceUnregistered(NsdServiceInfo serviceInfo) { } }; if (registrationListeners.size() == 0) { - multicastLock.acquire(); + publishMulticastLock.acquire(); } registrationListeners.add(registrationListener); mMFServiceName.add(serviceName); @@ -216,8 +223,8 @@ public void onServiceUnregistered(NsdServiceInfo serviceInfo) { @Override public void removeServices() { Log.d(TAG, "removeServices: "); - if (registrationListeners.size() > 0) { - multicastLock.release(); + if (registrationListeners.size() > 0 && publishMulticastLock.isHeld()) { + publishMulticastLock.release(); } for (NsdManager.RegistrationListener l : registrationListeners) { Log.i(TAG, "Remove " + l);