Skip to content

Commit

Permalink
[Java] Add support for Timestamp type in EventState (#28540)
Browse files Browse the repository at this point in the history
* [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>
  • Loading branch information
yufengwangca and restyled-commits authored Aug 7, 2023
1 parent 963a859 commit 63179b0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
34 changes: 27 additions & 7 deletions src/controller/java/AndroidCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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)
{
Expand Down Expand Up @@ -364,9 +368,25 @@ void ReportCallback::OnEventData(const app::EventHeader & aEventHeader, TLV::TLV
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
Expand Down Expand Up @@ -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, "<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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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() {
Expand Down

0 comments on commit 63179b0

Please sign in to comment.