Skip to content

Commit

Permalink
Merge 2c828b6 into 270d8e2
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhanw-google authored Aug 24, 2023
2 parents 270d8e2 + 2c828b6 commit 2401666
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import chip.devicecontroller.GetConnectedDeviceCallbackJni.GetConnectedDeviceCal
import chip.devicecontroller.ReportCallback
import chip.devicecontroller.model.ChipAttributePath
import chip.devicecontroller.model.ChipEventPath
import chip.devicecontroller.model.ChipPathId
import chip.devicecontroller.model.NodeState
import com.matter.controller.commands.common.CredentialsIssuer
import java.util.Collections
import java.util.logging.Level
import java.util.logging.Logger

Expand Down Expand Up @@ -36,7 +36,30 @@ class PairOnNetworkLongImReadCommand(
}

override fun onReport(nodeState: NodeState) {
logger.log(Level.INFO, "Read receve onReport")
logger.log(Level.INFO, nodeState.toString())

val attribute =
nodeState
.getEndpointState(0)
?.getClusterState(CLUSTER_ID_BASIC)
?.getAttributeState(ATTR_ID_LOCAL_CONFIG_DISABLED)

if (!(attribute != null && attribute.getJson().toString().equals("""{"16:BOOL":false}"""))) {
setFailure("read attribute failure")
return
}
val event =
nodeState
.getEndpointState(0)
?.getClusterState(CLUSTER_ID_BASIC)
?.getEventState(EVENT_ID_START_UP)
if (
!(event != null && event[0].getJson().toString().equals("""{"0:STRUCT":{"0:UINT":1}}"""))
) {
setFailure("read event failure")
return
}
logger.log(Level.INFO, "Read receive onReport success")
setSuccess()
}
}
Expand All @@ -62,6 +85,15 @@ class PairOnNetworkLongImReadCommand(
)
)

val eventPathList =
listOf(
ChipEventPath.newInstance(
ChipPathId.forWildcard(),
ChipPathId.forWildcard(),
ChipPathId.forWildcard()
)
)

currentCommissioner()
.pairDeviceWithAddress(
getNodeId(),
Expand All @@ -77,14 +109,7 @@ class PairOnNetworkLongImReadCommand(
.getConnectedDevicePointer(getNodeId(), InternalGetConnectedDeviceCallback())
clear()
currentCommissioner()
.readPath(
InternalReportCallback(),
devicePointer,
attributePathList,
Collections.emptyList(),
false,
0
)
.readPath(InternalReportCallback(), devicePointer, attributePathList, eventPathList, false, 0)
waitCompleteMs(getTimeoutMillis())
}

Expand All @@ -94,5 +119,6 @@ class PairOnNetworkLongImReadCommand(
private const val MATTER_PORT = 5540
private const val CLUSTER_ID_BASIC = 0x0028L
private const val ATTR_ID_LOCAL_CONFIG_DISABLED = 16L
private const val EVENT_ID_START_UP = 0L
}
}
58 changes: 38 additions & 20 deletions src/controller/java/AndroidCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include <lib/support/JniReferences.h>
#include <lib/support/JniTypeWrappers.h>
#include <lib/support/jsontlv/JsonToTlv.h>
#include <lib/support/jsontlv/TlvJson.h>
#include <lib/support/jsontlv/TlvToJson.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/PlatformManager.h>
Expand Down Expand Up @@ -251,11 +250,6 @@ void ReportCallback::OnAttributeData(const app::ConcreteDataAttributePath & aPat
return;
}

TLV::TLVReader readerForJavaTLV;
TLV::TLVReader readerForJson;
readerForJavaTLV.Init(*apData);
readerForJson.Init(*apData);

jobject value = nullptr;
#if USE_JAVA_TLV_ENCODE_DECODE
TLV::TLVReader readerForJavaObject;
Expand All @@ -273,25 +267,40 @@ void ReportCallback::OnAttributeData(const app::ConcreteDataAttributePath & aPat
ReportError(attributePathObj, nullptr, CHIP_JNI_ERROR_EXCEPTION_THROWN));
#endif
// Create TLV byte array to pass to Java layer
TLV::TLVReader readerForJavaTLV;
readerForJavaTLV.Init(*apData);
size_t bufferLen = readerForJavaTLV.GetRemainingLength() + readerForJavaTLV.GetLengthRead();
std::unique_ptr<uint8_t[]> buffer = std::unique_ptr<uint8_t[]>(new uint8_t[bufferLen]);
uint32_t size = 0;
// The TLVReader's read head is not pointing to the first element in the container, instead of the container itself, use
// a TLVWriter to get a TLV with a normalized TLV buffer (Wrapped with an anonymous tag, no extra "end of container" tag
// at the end.)

TLV::TLVWriter writer;
writer.Init(buffer.get(), bufferLen);
err = writer.CopyElement(TLV::AnonymousTag(), readerForJavaTLV);
TLV::TLVType outer;

err = writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, outer);
VerifyOrReturn(err == CHIP_NO_ERROR, ReportError(attributePathObj, nullptr, err));
TLV::Tag tag;
err = ConvertTlvTag(aPath.mAttributeId, tag);

VerifyOrReturn(err == CHIP_NO_ERROR, ReportError(attributePathObj, nullptr, err));
err = writer.CopyElement(tag, readerForJavaTLV);
VerifyOrReturn(err == CHIP_NO_ERROR, ReportError(attributePathObj, nullptr, err));
err = writer.EndContainer(outer);

VerifyOrReturn(err == CHIP_NO_ERROR, ReportError(nullptr, attributePathObj, err));
size = writer.GetLengthWritten();
chip::ByteArray jniByteArray(env, reinterpret_cast<jbyte *>(buffer.get()), size);

TLV::TLVReader readerForJson;
readerForJson.Init(buffer.get(), size);
err = readerForJson.Next();
VerifyOrReturn(err == CHIP_NO_ERROR, ReportError(nullptr, attributePathObj, err));
// Convert TLV to JSON
Json::Value json;
std::string json;
err = TlvToJson(readerForJson, json);
VerifyOrReturn(err == CHIP_NO_ERROR, ReportError(attributePathObj, nullptr, err));

UtfString jsonString(env, JsonToString(json).c_str());
UtfString jsonString(env, json.c_str());

// Create AttributeState object
jclass attributeStateCls;
Expand Down Expand Up @@ -365,11 +374,6 @@ void ReportCallback::OnEventData(const app::EventHeader & aEventHeader, TLV::TLV
return;
}

TLV::TLVReader readerForJavaTLV;
TLV::TLVReader readerForJson;
readerForJavaTLV.Init(*apData);
readerForJson.Init(*apData);

jlong eventNumber = static_cast<jlong>(aEventHeader.mEventNumber);
jint priorityLevel = static_cast<jint>(aEventHeader.mPriorityLevel);
jlong timestampValue = static_cast<jlong>(aEventHeader.mTimestamp.mValue);
Expand Down Expand Up @@ -406,6 +410,8 @@ void ReportCallback::OnEventData(const app::EventHeader & aEventHeader, TLV::TLV
#endif

// Create TLV byte array to pass to Java layer
TLV::TLVReader readerForJavaTLV;
readerForJavaTLV.Init(*apData);
size_t bufferLen = readerForJavaTLV.GetRemainingLength() + readerForJavaTLV.GetLengthRead();
std::unique_ptr<uint8_t[]> buffer = std::unique_ptr<uint8_t[]>(new uint8_t[bufferLen]);
uint32_t size = 0;
Expand All @@ -414,17 +420,29 @@ void ReportCallback::OnEventData(const app::EventHeader & aEventHeader, TLV::TLV
// at the end.)
TLV::TLVWriter writer;
writer.Init(buffer.get(), bufferLen);
err = writer.CopyElement(TLV::AnonymousTag(), readerForJavaTLV);
TLV::TLVType outer;
err = writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, outer);
VerifyOrReturn(err == CHIP_NO_ERROR, ReportError(nullptr, eventPathObj, err));
TLV::Tag tag;
err = ConvertTlvTag(aEventHeader.mPath.mEventId, tag);
VerifyOrReturn(err == CHIP_NO_ERROR, ReportError(nullptr, eventPathObj, err));
err = writer.CopyElement(tag, readerForJavaTLV);
VerifyOrReturn(err == CHIP_NO_ERROR, ReportError(nullptr, eventPathObj, err));
err = writer.EndContainer(outer);
VerifyOrReturn(err == CHIP_NO_ERROR, ReportError(nullptr, eventPathObj, err));
size = writer.GetLengthWritten();
chip::ByteArray jniByteArray(env, reinterpret_cast<jbyte *>(buffer.get()), size);

// Convert TLV to JSON
Json::Value json;
std::string json;
TLV::TLVReader readerForJson;
readerForJson.Init(buffer.get(), size);
err = readerForJson.Next();
VerifyOrReturn(err == CHIP_NO_ERROR, ReportError(nullptr, eventPathObj, err));
err = TlvToJson(readerForJson, json);
VerifyOrReturn(err == CHIP_NO_ERROR, ReportError(nullptr, eventPathObj, err));

UtfString jsonString(env, JsonToString(json).c_str());
UtfString jsonString(env, json.c_str());

// Create EventState object
jclass eventStateCls;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public String toString() {
builder.append(attributeId);
builder.append(": ");
builder.append(
attributeState.getValue() == null ? "null" : attributeState.getValue().toString());
attributeState.getJson() == null ? "null" : attributeState.getJson().toString());
builder.append("\n");
});
events.forEach(
Expand All @@ -91,7 +91,7 @@ public String toString() {
builder.append(eventId);
builder.append(": ");
builder.append(
eventState.getValue() == null ? "null" : eventState.getValue().toString());
eventState.getJson() == null ? "null" : eventState.getJson().toString());
builder.append("\n");
});
});
Expand Down

0 comments on commit 2401666

Please sign in to comment.