Skip to content

Commit

Permalink
[Android] Add StayActive support during commission flow for LIT (#36028)
Browse files Browse the repository at this point in the history
* [Android] Add StayActive support during commission flow for LIT (#35959)

* Fix crash when handling jStayActiveMsec (#35997)
  • Loading branch information
yunhanw-google authored Oct 10, 2024
1 parent 42efe9d commit 3e1f7ad
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ class DeviceProvisioningFragment : Fragment() {
override fun onICDRegistrationInfoRequired() {
Log.d(TAG, "onICDRegistrationInfoRequired")
deviceController.updateCommissioningICDRegistrationInfo(
ICDRegistrationInfo.newBuilder().build()
ICDRegistrationInfo.newBuilder().setICDStayActiveDurationMsec(30000L).build()
)
}

Expand Down
30 changes: 26 additions & 4 deletions src/controller/java/AndroidDeviceControllerWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,28 +508,50 @@ CHIP_ERROR AndroidDeviceControllerWrapper::ApplyICDRegistrationInfo(chip::Contro
VerifyOrReturnError(icdRegistrationInfo != nullptr, CHIP_ERROR_INVALID_ARGUMENT);

JNIEnv * env = chip::JniReferences::GetInstance().GetEnvForCurrentThread();
if (env == nullptr)
{
ChipLogError(Controller, "Failed to retrieve JNIEnv in %s.", __func__);
return CHIP_ERROR_INCORRECT_STATE;
}

jmethodID getICDStayActiveDurationMsecMethod;
err = chip::JniReferences::GetInstance().FindMethod(env, icdRegistrationInfo, "getICDStayActiveDurationMsec",
"()Ljava/lang/Long;", &getICDStayActiveDurationMsecMethod);
ReturnErrorOnFailure(err);
jobject jStayActiveMsec = env->CallObjectMethod(icdRegistrationInfo, getICDStayActiveDurationMsecMethod);
if (jStayActiveMsec != nullptr)
{
jlong stayActiveMsec = chip::JniReferences::GetInstance().LongToPrimitive(jStayActiveMsec);
if (!chip::CanCastTo<uint32_t>(stayActiveMsec))
{
ChipLogError(Controller, "Failed to process stayActiveMsec in %s since this is not a valid 32-bit integer.", __func__);
return CHIP_ERROR_INVALID_ARGUMENT;
}
params.SetICDStayActiveDurationMsec(static_cast<uint32_t>(stayActiveMsec));
}

jmethodID getCheckInNodeIdMethod;
err = chip::JniReferences::GetInstance().FindMethod(env, icdRegistrationInfo, "getCheckInNodeId", "()Ljava/lang/Long;",
&getCheckInNodeIdMethod);
VerifyOrReturnError(err == CHIP_NO_ERROR, err);
ReturnErrorOnFailure(err);
jobject jCheckInNodeId = env->CallObjectMethod(icdRegistrationInfo, getCheckInNodeIdMethod);

jmethodID getMonitoredSubjectMethod;
err = chip::JniReferences::GetInstance().FindMethod(env, icdRegistrationInfo, "getMonitoredSubject", "()Ljava/lang/Long;",
&getMonitoredSubjectMethod);
VerifyOrReturnError(err == CHIP_NO_ERROR, err);
ReturnErrorOnFailure(err);
jobject jMonitoredSubject = env->CallObjectMethod(icdRegistrationInfo, getMonitoredSubjectMethod);

jmethodID getSymmetricKeyMethod;
err =
chip::JniReferences::GetInstance().FindMethod(env, icdRegistrationInfo, "getSymmetricKey", "()[B", &getSymmetricKeyMethod);
VerifyOrReturnError(err == CHIP_NO_ERROR, err);
ReturnErrorOnFailure(err);
jbyteArray jSymmetricKey = static_cast<jbyteArray>(env->CallObjectMethod(icdRegistrationInfo, getSymmetricKeyMethod));

jmethodID getClientTypeMethod;
err = chip::JniReferences::GetInstance().FindMethod(env, icdRegistrationInfo, "getClientType", "()Ljava/lang/Integer;",
&getClientTypeMethod);
VerifyOrReturnError(err == CHIP_NO_ERROR, err);
ReturnErrorOnFailure(err);
jobject jClientType = env->CallObjectMethod(icdRegistrationInfo, getClientTypeMethod);

chip::NodeId checkInNodeId = chip::kUndefinedNodeId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,19 @@ public class ICDRegistrationInfo {
@Nullable private final Long monitoredSubject;
@Nullable private final byte[] symmetricKey;
@Nullable private final Integer clientType;
@Nullable private final Long stayActiveDurationMsec;

private ICDRegistrationInfo(Builder builder) {
this.checkInNodeId = builder.checkInNodeId;
this.monitoredSubject = builder.monitoredSubject;
this.symmetricKey = builder.symmetricKey;
this.clientType = builder.clientType;
this.stayActiveDurationMsec = builder.stayActiveDurationMsec;
}

/** Returns the duration period to stay active. */
public Long getICDStayActiveDurationMsec() {
return stayActiveDurationMsec;
}

/** Returns the check in node ID. */
Expand Down Expand Up @@ -62,6 +69,7 @@ public static class Builder {
@Nullable private Long monitoredSubject = null;
@Nullable private byte[] symmetricKey = null;
@Nullable private Integer clientType = null;
@Nullable private Long stayActiveDurationMsec = null;

private Builder() {}

Expand Down Expand Up @@ -93,6 +101,15 @@ public Builder setClientType(Integer clientType) {
return this;
}

/**
* Request LIT device to stay active for specific duration after commission completes, the upper
* bound is 30 seconds.
*/
public Builder setICDStayActiveDurationMsec(Long stayActiveDurationMsec) {
this.stayActiveDurationMsec = stayActiveDurationMsec;
return this;
}

public ICDRegistrationInfo build() {
return new ICDRegistrationInfo(this);
}
Expand Down

0 comments on commit 3e1f7ad

Please sign in to comment.