From 63179b0f527435c2b81c306803c5eb3e9788d676 Mon Sep 17 00:00:00 2001 From: Yufeng Wang Date: Mon, 7 Aug 2023 15:47:07 -0700 Subject: [PATCH] [Java] Add support for Timestamp type in EventState (#28540) * [Java] Add Timestamp type in EventState * Restyled by whitespace * Restyled by clang-format * Address reivew comments * Restyled by clang-format --------- Co-authored-by: Restyled.io --- .../clusterclient/WildcardFragment.kt | 3 +- src/controller/java/AndroidCallbacks.cpp | 34 +++++++++++++++---- .../devicecontroller/model/EventState.java | 17 +++++++--- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/WildcardFragment.kt b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/WildcardFragment.kt index a755b1a441f03a..f03f68eee8c089 100644 --- a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/WildcardFragment.kt +++ b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/WildcardFragment.kt @@ -225,7 +225,8 @@ class WildcardFragment : Fragment() { for (event in events) { stringBuilder.append("\t\teventNumber: ${event.eventNumber}\n") stringBuilder.append("\t\tpriorityLevel: ${event.priorityLevel}\n") - stringBuilder.append("\t\tsystemTimeStamp: ${event.systemTimeStamp}\n") + stringBuilder.append("\t\ttimestampType: ${event.timestampType}\n") + stringBuilder.append("\t\ttimestampValue: ${event.timestampValue}\n") val eventName = ChipIdLookup.eventIdToName(clusterId, eventId) stringBuilder.append("\t\t$eventName: ${event.value}\n") diff --git a/src/controller/java/AndroidCallbacks.cpp b/src/controller/java/AndroidCallbacks.cpp index 5d787dc35c45cc..f6d9499d8f8d2a 100644 --- a/src/controller/java/AndroidCallbacks.cpp +++ b/src/controller/java/AndroidCallbacks.cpp @@ -21,6 +21,7 @@ #include #include #endif +#include #include #include #include @@ -35,6 +36,9 @@ namespace chip { namespace Controller { +static const int MILLIS_SINCE_BOOT = 0; +static const int MILLIS_SINCE_EPOCH = 1; + GetConnectedDeviceCallback::GetConnectedDeviceCallback(jobject wrapperCallback, jobject javaCallback) : mOnSuccess(OnDeviceConnectedFn, this), mOnFailure(OnDeviceConnectionFailureFn, this) { @@ -364,9 +368,25 @@ void ReportCallback::OnEventData(const app::EventHeader & aEventHeader, TLV::TLV readerForJavaTLV.Init(*apData); readerForJson.Init(*apData); - jlong eventNumber = static_cast(aEventHeader.mEventNumber); - jint priorityLevel = static_cast(aEventHeader.mPriorityLevel); - jlong timestamp = static_cast(aEventHeader.mTimestamp.mValue); + jlong eventNumber = static_cast(aEventHeader.mEventNumber); + jint priorityLevel = static_cast(aEventHeader.mPriorityLevel); + jlong timestampValue = static_cast(aEventHeader.mTimestamp.mValue); + + jint timestampType = 0; + if (aEventHeader.mTimestamp.mType == app::Timestamp::Type::kSystem) + { + timestampType = static_cast(MILLIS_SINCE_BOOT); + } + else if (aEventHeader.mTimestamp.mType == app::Timestamp::Type::kEpoch) + { + timestampType = static_cast(MILLIS_SINCE_EPOCH); + } + else + { + ChipLogError(Controller, "Unsupported event timestamp type"); + ReportError(nullptr, eventPathObj, CHIP_ERROR_INVALID_ARGUMENT); + return; + } jobject value = nullptr; #if USE_JAVA_TLV_ENCODE_DECODE @@ -407,13 +427,13 @@ void ReportCallback::OnEventData(const app::EventHeader & aEventHeader, TLV::TLV // Create EventState object jclass eventStateCls; err = JniReferences::GetInstance().GetClassRef(env, "chip/devicecontroller/model/EventState", eventStateCls); - VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Could not find EventState class")); + VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Failed to find EventState class")); VerifyOrReturn(eventStateCls != nullptr, ChipLogError(Controller, "Could not find EventState class")); chip::JniClass eventStateJniCls(eventStateCls); - jmethodID eventStateCtor = env->GetMethodID(eventStateCls, "", "(JIJLjava/lang/Object;[BLjava/lang/String;)V"); + jmethodID eventStateCtor = env->GetMethodID(eventStateCls, "", "(JIIJLjava/lang/Object;[BLjava/lang/String;)V"); VerifyOrReturn(eventStateCtor != nullptr, ChipLogError(Controller, "Could not find EventState constructor")); - jobject eventStateObj = env->NewObject(eventStateCls, eventStateCtor, eventNumber, priorityLevel, timestamp, value, - jniByteArray.jniValue(), jsonString.jniValue()); + jobject eventStateObj = env->NewObject(eventStateCls, eventStateCtor, eventNumber, priorityLevel, timestampType, timestampValue, + value, jniByteArray.jniValue(), jsonString.jniValue()); VerifyOrReturn(eventStateObj != nullptr, ChipLogError(Controller, "Could not create EventState object")); // Add EventState to NodeState diff --git a/src/controller/java/src/chip/devicecontroller/model/EventState.java b/src/controller/java/src/chip/devicecontroller/model/EventState.java index 2856f54ce47ce4..23988c7884687f 100644 --- a/src/controller/java/src/chip/devicecontroller/model/EventState.java +++ b/src/controller/java/src/chip/devicecontroller/model/EventState.java @@ -23,11 +23,14 @@ /** Represents the reported value of an attribute in object form, TLV and JSON. */ public final class EventState { + public static final int MILLIS_SINCE_BOOT = 0; + public static final int MILLIS_SINCE_EPOCH = 1; private static final String TAG = "EventState"; private long eventNumber; private int priorityLevel; - private long systemTimeStamp; + private int timestampType; + private long timestampValue; private Object valueObject; private byte[] tlv; @@ -36,13 +39,15 @@ public final class EventState { public EventState( long eventNumber, int priorityLevel, + int timestampType, long systemTimeStamp, Object valueObject, byte[] tlv, String jsonString) { this.eventNumber = eventNumber; this.priorityLevel = priorityLevel; - this.systemTimeStamp = systemTimeStamp; + this.timestampType = timestampType; + this.timestampValue = timestampValue; this.valueObject = valueObject; this.tlv = tlv; @@ -61,8 +66,12 @@ public int getPriorityLevel() { return priorityLevel; } - public long getSystemTimeStamp() { - return systemTimeStamp; + public int getTimestampType() { + return timestampType; + } + + public long getTimestampValue() { + return timestampValue; } public Object getValue() {