Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Return JSON in wildcard interactions #16258

Merged
merged 2 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/controller/java/AndroidCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <lib/support/ErrorStr.h>
#include <lib/support/JniReferences.h>
#include <lib/support/JniTypeWrappers.h>
#include <lib/support/jsontlv/TlvJson.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/PlatformManager.h>
#include <type_traits>
Expand Down Expand Up @@ -214,8 +215,10 @@ void ReportCallback::OnAttributeData(const app::ConcreteDataAttributePath & aPat

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

jobject value = DecodeAttributeValue(aPath, readerForJavaObject, &err);
// If we don't know this attribute, just skip it.
Expand All @@ -238,14 +241,20 @@ void ReportCallback::OnAttributeData(const app::ConcreteDataAttributePath & aPat
size = writer.GetLengthWritten();
chip::ByteArray jniByteArray(env, reinterpret_cast<jbyte *>(buffer.get()), size);

// Convert TLV to JSON
Json::Value json;
err = TlvToJson(readerForJson, json);
UtfString jsonString(env, JsonToString(json).c_str());

// Create AttributeState object
jclass attributeStateCls;
err = JniReferences::GetInstance().GetClassRef(env, "chip/devicecontroller/model/AttributeState", attributeStateCls);
VerifyOrReturn(attributeStateCls != nullptr, ChipLogError(Controller, "Could not find AttributeState class"));
chip::JniClass attributeStateJniCls(attributeStateCls);
jmethodID attributeStateCtor = env->GetMethodID(attributeStateCls, "<init>", "(Ljava/lang/Object;[B)V");
jmethodID attributeStateCtor = env->GetMethodID(attributeStateCls, "<init>", "(Ljava/lang/Object;[BLjava/lang/String;)V");
VerifyOrReturn(attributeStateCtor != nullptr, ChipLogError(Controller, "Could not find AttributeState constructor"));
jobject attributeStateObj = env->NewObject(attributeStateCls, attributeStateCtor, value, jniByteArray.jniValue());
jobject attributeStateObj =
env->NewObject(attributeStateCls, attributeStateCtor, value, jniByteArray.jniValue(), jsonString.jniValue());
VerifyOrReturn(attributeStateObj != nullptr, ChipLogError(Controller, "Could not create AttributeState object"));

// Add AttributeState to NodeState
Expand Down
1 change: 1 addition & 0 deletions src/controller/java/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ shared_library("jni") {
"${chip_root}/src/credentials:default_attestation_verifier",
"${chip_root}/src/inet",
"${chip_root}/src/lib",
"${chip_root}/src/lib/support/jsontlv",
"${chip_root}/src/platform",
"${chip_root}/src/platform/android",
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,26 @@
*/
package chip.devicecontroller.model;

/** Represents the reported value of an attribute in object form AND TLV. */
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;

/** Represents the reported value of an attribute in object form, TLV and JSON. */
public final class AttributeState {
private static final String TAG = "AttributeState";

private Object valueObject;
private byte[] tlv;
private JSONObject json;

public AttributeState(Object valueObject, byte[] tlv) {
public AttributeState(Object valueObject, byte[] tlv, String jsonString) {
this.valueObject = valueObject;
this.tlv = tlv;
try {
this.json = new JSONObject(jsonString);
} catch (JSONException ex) {
Log.e(TAG, "Error parsing JSON string", ex);
}
}

public Object getValue() {
Expand All @@ -37,4 +49,8 @@ public Object getValue() {
public byte[] getTlv() {
return tlv;
}

public JSONObject getJson() {
return json;
}
}