From 59e2803bd3db39bc2a2ee29f3d94ab1b92a9f720 Mon Sep 17 00:00:00 2001 From: Joonhaeng Heo Date: Wed, 22 May 2024 14:38:33 +0900 Subject: [PATCH 1/6] 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); From 9d7035d43fcba24919b195cfafd24a9ff9104175 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Thu, 23 May 2024 05:42:30 +0000 Subject: [PATCH 2/6] Restyled by clang-format --- src/controller/java/AndroidDeviceControllerWrapper.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/controller/java/AndroidDeviceControllerWrapper.cpp b/src/controller/java/AndroidDeviceControllerWrapper.cpp index ac997d48607476..d3fd000f686676 100644 --- a/src/controller/java/AndroidDeviceControllerWrapper.cpp +++ b/src/controller/java/AndroidDeviceControllerWrapper.cpp @@ -1088,12 +1088,14 @@ CHIP_ERROR AndroidDeviceControllerWrapper::SyncDeleteKeyValue(const char * key) return chip::DeviceLayer::PersistedStorage::KeyValueStoreMgr().Delete(key); } -void AndroidDeviceControllerWrapper::StartDnssd() { +void AndroidDeviceControllerWrapper::StartDnssd() +{ FabricTable * fabricTable = DeviceControllerFactory::GetInstance().GetSystemState()->Fabrics(); chip::app::DnssdServer::Instance().SetFabricTable(fabricTable); chip::app::DnssdServer::Instance().StartServer(); } -void AndroidDeviceControllerWrapper::StopDnssd() { +void AndroidDeviceControllerWrapper::StopDnssd() +{ chip::app::DnssdServer::Instance().StopServer(); } From eb9b48279ee19677c5e843822af6fc1a75cf4d98 Mon Sep 17 00:00:00 2001 From: yunhanw-google Date: Thu, 23 May 2024 08:58:04 -0700 Subject: [PATCH 3/6] Update CHIPDeviceController-JNI.cpp --- src/controller/java/CHIPDeviceController-JNI.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/controller/java/CHIPDeviceController-JNI.cpp b/src/controller/java/CHIPDeviceController-JNI.cpp index 786c9e55569088..7e20928a23d437 100644 --- a/src/controller/java/CHIPDeviceController-JNI.cpp +++ b/src/controller/java/CHIPDeviceController-JNI.cpp @@ -2160,7 +2160,7 @@ JNI_METHOD(void, startDnssd)(JNIEnv * env, jobject self, jlong handle) chip::DeviceLayer::StackLock lock; AndroidDeviceControllerWrapper * wrapper = AndroidDeviceControllerWrapper::FromJNIHandle(handle); - + VerifyOrReturn(wrapper != nullptr, ChipLogError(Controller, "AndroidDeviceControllerWrapper::FromJNIHandle in startDnssd fails!")); wrapper->StartDnssd(); } @@ -2170,7 +2170,7 @@ JNI_METHOD(void, stopDnssd)(JNIEnv * env, jobject self, jlong handle) chip::DeviceLayer::StackLock lock; AndroidDeviceControllerWrapper * wrapper = AndroidDeviceControllerWrapper::FromJNIHandle(handle); - + VerifyOrReturn(wrapper != nullptr, ChipLogError(Controller, "AndroidDeviceControllerWrapper::FromJNIHandle in stopDnssd fails!")); wrapper->StopDnssd(); } From 63c753d7f9a63838b979a699137ad57f708a471a Mon Sep 17 00:00:00 2001 From: yunhanw-google Date: Thu, 23 May 2024 09:01:25 -0700 Subject: [PATCH 4/6] Update AndroidDeviceControllerWrapper.cpp --- src/controller/java/AndroidDeviceControllerWrapper.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/controller/java/AndroidDeviceControllerWrapper.cpp b/src/controller/java/AndroidDeviceControllerWrapper.cpp index d3fd000f686676..83d6c8abdc8057 100644 --- a/src/controller/java/AndroidDeviceControllerWrapper.cpp +++ b/src/controller/java/AndroidDeviceControllerWrapper.cpp @@ -1065,7 +1065,7 @@ void AndroidDeviceControllerWrapper::OnICDRegistrationComplete(chip::NodeId icdN CHIP_ERROR AndroidDeviceControllerWrapper::SyncGetKeyValue(const char * key, void * value, uint16_t & size) { - ChipLogProgress(chipTool, "KVS: Getting key %s", StringOrNullMarker(key)); + ChipLogProgress(Controller, "KVS: Getting key %s", StringOrNullMarker(key)); size_t read_size = 0; @@ -1078,19 +1078,20 @@ CHIP_ERROR AndroidDeviceControllerWrapper::SyncGetKeyValue(const char * key, voi CHIP_ERROR AndroidDeviceControllerWrapper::SyncSetKeyValue(const char * key, const void * value, uint16_t size) { - ChipLogProgress(chipTool, "KVS: Setting key %s", StringOrNullMarker(key)); + ChipLogProgress(Controller, "KVS: Setting key %s", StringOrNullMarker(key)); return chip::DeviceLayer::PersistedStorage::KeyValueStoreMgr().Put(key, value, size); } CHIP_ERROR AndroidDeviceControllerWrapper::SyncDeleteKeyValue(const char * key) { - ChipLogProgress(chipTool, "KVS: Deleting key %s", StringOrNullMarker(key)); + ChipLogProgress(Controller, "KVS: Deleting key %s", StringOrNullMarker(key)); return chip::DeviceLayer::PersistedStorage::KeyValueStoreMgr().Delete(key); } void AndroidDeviceControllerWrapper::StartDnssd() { FabricTable * fabricTable = DeviceControllerFactory::GetInstance().GetSystemState()->Fabrics(); + VerifyOrReturn(fabricTable != nullptr, ChipLogError(controller, "Fail to get fabricTable in StartDnssd")); chip::app::DnssdServer::Instance().SetFabricTable(fabricTable); chip::app::DnssdServer::Instance().StartServer(); } From dba568abb3fa3d3f891539141bb7bac157b4c1fb Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Thu, 23 May 2024 16:01:46 +0000 Subject: [PATCH 5/6] Restyled by clang-format --- src/controller/java/CHIPDeviceController-JNI.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/controller/java/CHIPDeviceController-JNI.cpp b/src/controller/java/CHIPDeviceController-JNI.cpp index 7e20928a23d437..a6c93c55885070 100644 --- a/src/controller/java/CHIPDeviceController-JNI.cpp +++ b/src/controller/java/CHIPDeviceController-JNI.cpp @@ -2160,7 +2160,8 @@ JNI_METHOD(void, startDnssd)(JNIEnv * env, jobject self, jlong handle) chip::DeviceLayer::StackLock lock; AndroidDeviceControllerWrapper * wrapper = AndroidDeviceControllerWrapper::FromJNIHandle(handle); - VerifyOrReturn(wrapper != nullptr, ChipLogError(Controller, "AndroidDeviceControllerWrapper::FromJNIHandle in startDnssd fails!")); + VerifyOrReturn(wrapper != nullptr, + ChipLogError(Controller, "AndroidDeviceControllerWrapper::FromJNIHandle in startDnssd fails!")); wrapper->StartDnssd(); } @@ -2170,7 +2171,8 @@ JNI_METHOD(void, stopDnssd)(JNIEnv * env, jobject self, jlong handle) chip::DeviceLayer::StackLock lock; AndroidDeviceControllerWrapper * wrapper = AndroidDeviceControllerWrapper::FromJNIHandle(handle); - VerifyOrReturn(wrapper != nullptr, ChipLogError(Controller, "AndroidDeviceControllerWrapper::FromJNIHandle in stopDnssd fails!")); + VerifyOrReturn(wrapper != nullptr, + ChipLogError(Controller, "AndroidDeviceControllerWrapper::FromJNIHandle in stopDnssd fails!")); wrapper->StopDnssd(); } From 5ce71869223eeaf7382f05c7545daa351ac7e86f Mon Sep 17 00:00:00 2001 From: yunhanw-google Date: Thu, 23 May 2024 09:30:16 -0700 Subject: [PATCH 6/6] Update AndroidDeviceControllerWrapper.cpp --- src/controller/java/AndroidDeviceControllerWrapper.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controller/java/AndroidDeviceControllerWrapper.cpp b/src/controller/java/AndroidDeviceControllerWrapper.cpp index 83d6c8abdc8057..526f6830844f13 100644 --- a/src/controller/java/AndroidDeviceControllerWrapper.cpp +++ b/src/controller/java/AndroidDeviceControllerWrapper.cpp @@ -1091,7 +1091,7 @@ CHIP_ERROR AndroidDeviceControllerWrapper::SyncDeleteKeyValue(const char * key) void AndroidDeviceControllerWrapper::StartDnssd() { FabricTable * fabricTable = DeviceControllerFactory::GetInstance().GetSystemState()->Fabrics(); - VerifyOrReturn(fabricTable != nullptr, ChipLogError(controller, "Fail to get fabricTable in StartDnssd")); + VerifyOrReturn(fabricTable != nullptr, ChipLogError(Controller, "Fail to get fabricTable in StartDnssd")); chip::app::DnssdServer::Instance().SetFabricTable(fabricTable); chip::app::DnssdServer::Instance().StartServer(); }