Skip to content

Commit

Permalink
[Android] Return JSON in wildcard interactions (#16258)
Browse files Browse the repository at this point in the history
* Return JSON to Java layer

* Restyled by clang-format

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Mar 21, 2022
1 parent 7b05c96 commit eebe1d3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
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;
}
}

0 comments on commit eebe1d3

Please sign in to comment.