[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 <commits@restyled.io>
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 a755b1a..f03f68e 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 @@
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 5d787dc..f6d9499 100644
--- a/src/controller/java/AndroidCallbacks.cpp
+++ b/src/controller/java/AndroidCallbacks.cpp
@@ -21,6 +21,7 @@
#include <controller/java/CHIPAttributeTLVValueDecoder.h>
#include <controller/java/CHIPEventTLVValueDecoder.h>
#endif
+#include <app/EventLoggingTypes.h>
#include <jni.h>
#include <lib/support/CHIPJNIError.h>
#include <lib/support/CodeUtils.h>
@@ -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 @@
readerForJavaTLV.Init(*apData);
readerForJson.Init(*apData);
- jlong eventNumber = static_cast<jlong>(aEventHeader.mEventNumber);
- jint priorityLevel = static_cast<jint>(aEventHeader.mPriorityLevel);
- jlong timestamp = static_cast<jlong>(aEventHeader.mTimestamp.mValue);
+ jlong eventNumber = static_cast<jlong>(aEventHeader.mEventNumber);
+ jint priorityLevel = static_cast<jint>(aEventHeader.mPriorityLevel);
+ jlong timestampValue = static_cast<jlong>(aEventHeader.mTimestamp.mValue);
+
+ jint timestampType = 0;
+ if (aEventHeader.mTimestamp.mType == app::Timestamp::Type::kSystem)
+ {
+ timestampType = static_cast<jint>(MILLIS_SINCE_BOOT);
+ }
+ else if (aEventHeader.mTimestamp.mType == app::Timestamp::Type::kEpoch)
+ {
+ timestampType = static_cast<jint>(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 @@
// 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, "<init>", "(JIJLjava/lang/Object;[BLjava/lang/String;)V");
+ jmethodID eventStateCtor = env->GetMethodID(eventStateCls, "<init>", "(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 2856f54..23988c7 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 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 @@
return priorityLevel;
}
- public long getSystemTimeStamp() {
- return systemTimeStamp;
+ public int getTimestampType() {
+ return timestampType;
+ }
+
+ public long getTimestampValue() {
+ return timestampValue;
}
public Object getValue() {