Skip to content

Commit

Permalink
Allow java applications to set timeouts for commands (#22602)
Browse files Browse the repository at this point in the history
* Allow java applications to set timeouts for commands

* Add zapt template updates

* Code review comment: use MakeOptional

* Add namespace for MakeOptional

* Update src/controller/java/BaseCHIPCluster-JNI.cpp

Co-authored-by: Boris Zbarsky <[email protected]>

Co-authored-by: Boris Zbarsky <[email protected]>
  • Loading branch information
andy31415 and bzbarsky-apple authored Sep 14, 2022
1 parent 2ed6dcb commit 377ac09
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/controller/CHIPCluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ class DLL_EXPORT ClusterBase
// TODO: remove when we start using InvokeCommand everywhere
void SetCommandTimeout(Optional<System::Clock::Timeout> timeout) { mTimeout = timeout; }

/**
* Returns the current command timeout set via SetCommandTimeout, or an
* empty optional if no timeout has been set.
*/
Optional<System::Clock::Timeout> GetCommandTimeout() { return mTimeout; }

ClusterId GetClusterId() const { return mClusterId; }

/*
Expand Down
43 changes: 43 additions & 0 deletions src/controller/java/BaseCHIPCluster-JNI.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <controller/CHIPCluster.h>
#include <jni.h>
#include <lib/support/JniReferences.h>
#include <platform/PlatformManager.h>

#define JNI_METHOD(RETURN, CLASS_NAME, METHOD_NAME) \
Expand All @@ -14,3 +15,45 @@ JNI_METHOD(void, BaseChipCluster, deleteCluster)(JNIEnv * env, jobject self, jlo
delete cluster;
}
}

JNI_METHOD(jobject, BaseChipCluster, getCommandTimeout)
(JNIEnv * env, jobject self, jlong clusterPtr)
{
chip::DeviceLayer::StackLock lock;
chip::Controller::ClusterBase * cluster = reinterpret_cast<chip::Controller::ClusterBase *>(clusterPtr);

chip::Optional<chip::System::Clock::Timeout> timeout = cluster->GetCommandTimeout();
if (!timeout.HasValue())
{
jobject emptyOptional = nullptr;
chip::JniReferences::GetInstance().CreateOptional(nullptr, emptyOptional);
return emptyOptional;
}

jobject timeoutOptional = nullptr;
jobject timeoutBoxed = nullptr;
jlong timeoutPrimitive = static_cast<jlong>(timeout.Value().count());
chip::JniReferences::GetInstance().CreateBoxedObject("java/lang/Long", "(J)V", timeoutPrimitive, timeoutBoxed);
chip::JniReferences::GetInstance().CreateOptional(timeoutBoxed, timeoutOptional);
return timeoutOptional;
}

JNI_METHOD(void, BaseChipCluster, setCommandTimeout)
(JNIEnv * env, jobject self, jlong clusterPtr, jobject timeoutOptional)
{
chip::DeviceLayer::StackLock lock;
chip::Controller::ClusterBase * cluster = reinterpret_cast<chip::Controller::ClusterBase *>(clusterPtr);

jobject boxedLong = nullptr;
chip::JniReferences::GetInstance().GetOptionalValue(timeoutOptional, boxedLong);

if (boxedLong != nullptr)
{
jlong timeoutMillis = chip::JniReferences::GetInstance().LongToPrimitive(boxedLong);
cluster->SetCommandTimeout(chip::MakeOptional(chip::System::Clock::Milliseconds32(static_cast<uint64_t>(timeoutMillis))));
}
else
{
cluster->SetCommandTimeout(chip::NullOptional);
}
}
19 changes: 19 additions & 0 deletions src/controller/java/templates/ChipClusters-java.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,25 @@ public class ChipClusters {
chipClusterPtr = initWithDevice(devicePtr, endpointId);
}

/**
* Sets the timeout, in milliseconds, after which commands sent through this cluster will fail
* with a timeout (regardless of whether or not a response has been received). If set to an
* empty optional, the default timeout will be used.
*/
public void setCommandTimeout(Optional<Long> timeoutMillis) {
setCommandTimeout(chipClusterPtr, timeoutMillis);
}

private native void setCommandTimeout(long clusterPtr, Optional<Long> timeoutMillis);

/** Returns the current timeout (in milliseconds) for commands sent through this cluster. */
public Optional<Long> getCommandTimeout() {
Optional<Long> timeout = getCommandTimeout(chipClusterPtr);
return timeout == null ? Optional.empty() : timeout;
}

private native Optional<Long> getCommandTimeout(long clusterPtr);

public abstract long initWithDevice(long devicePtr, int endpointId);

public native void deleteCluster(long chipClusterPtr);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 377ac09

Please sign in to comment.