Skip to content

Commit

Permalink
[Android] ICD checkin message can't receive in some Android device (#…
Browse files Browse the repository at this point in the history
…33561)

* Fix android icd checkin message

* Restyled by clang-format

* Update CHIPDeviceController-JNI.cpp

* Update AndroidDeviceControllerWrapper.cpp

---------

Co-authored-by: Restyled.io <[email protected]>
Co-authored-by: yunhanw-google <[email protected]>
  • Loading branch information
3 people authored and pull[bot] committed Nov 7, 2024
1 parent 7c9df72 commit 4153651
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 18 additions & 3 deletions src/controller/java/AndroidDeviceControllerWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

#include <string.h>

#include <app/server/Dnssd.h>

#include <lib/support/CodeUtils.h>
#include <lib/support/JniReferences.h>
#include <lib/support/JniTypeWrappers.h>
Expand Down Expand Up @@ -1063,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;

Expand All @@ -1076,12 +1078,25 @@ 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();
}

void AndroidDeviceControllerWrapper::StopDnssd()
{
chip::app::DnssdServer::Instance().StopServer();
}
4 changes: 4 additions & 0 deletions src/controller/java/AndroidDeviceControllerWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<chip::Controller::DeviceCommissioner>;

Expand Down
22 changes: 22 additions & 0 deletions src/controller/java/CHIPDeviceController-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2154,6 +2154,28 @@ 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);
VerifyOrReturn(wrapper != nullptr,
ChipLogError(Controller, "AndroidDeviceControllerWrapper::FromJNIHandle in startDnssd fails!"));
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);
VerifyOrReturn(wrapper != nullptr,
ChipLogError(Controller, "AndroidDeviceControllerWrapper::FromJNIHandle in stopDnssd fails!"));
wrapper->StopDnssd();
}

void * IOThreadMain(void * arg)
{
JNIEnv * env;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<NsdManager.RegistrationListener> registrationListeners = new ArrayList<>();
private final CopyOnWriteArrayList<String> mMFServiceName = new CopyOnWriteArrayList<>();
@Nullable private final NsdManagerResolverAvailState nsdManagerResolverAvailState;
Expand All @@ -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;
}
Expand Down Expand Up @@ -204,7 +211,7 @@ public void onServiceUnregistered(NsdServiceInfo serviceInfo) {
}
};
if (registrationListeners.size() == 0) {
multicastLock.acquire();
publishMulticastLock.acquire();
}
registrationListeners.add(registrationListener);
mMFServiceName.add(serviceName);
Expand All @@ -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);
Expand Down

0 comments on commit 4153651

Please sign in to comment.