Skip to content

Commit

Permalink
Add optional qr code info parsing to SetupPayload-JNI (#1470)
Browse files Browse the repository at this point in the history
* Add optional qr code info parsing to SetupPayload-JNI

Parse optional qr code info out of native setup payload
object and make it available to SetupPayload java class.

* Fix missing emty constructor
  • Loading branch information
randyrossi authored Jul 7, 2020
1 parent 8634c55 commit 9d4ffab
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/setup_payload/java/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ JARS = \
SetupPayloadParser_jar_JAVA_SRCS = \
chip/setuppayload/SetupPayload.java \
chip/setuppayload/SetupPayloadParser.java \
chip/setuppayload/OptionalQRCodeInfo.java \
$(NULL)

SetupPayloadParser_jar_JFLAGS = -source 8 -target 8
Expand Down
65 changes: 62 additions & 3 deletions src/setup_payload/java/SetupPayloadParser-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using namespace chip;

#define JNI_METHOD(RETURN, METHOD_NAME) extern "C" JNIEXPORT RETURN JNICALL Java_chip_setuppayload_SetupPayloadParser_##METHOD_NAME

static jobject TransformSetupPayload(JNIEnv * env, SetupPayload payload);
static jobject TransformSetupPayload(JNIEnv * env, SetupPayload & payload);

JNI_METHOD(jobject, fetchPayloadFromQrCode)(JNIEnv * env, jobject self, jstring qrCodeObj)
{
Expand Down Expand Up @@ -39,10 +39,11 @@ JNI_METHOD(jobject, fetchPayloadFromManualEntryCode)(JNIEnv * env, jobject self,
return TransformSetupPayload(env, payload);
}

jobject TransformSetupPayload(JNIEnv * env, SetupPayload payload)
jobject TransformSetupPayload(JNIEnv * env, SetupPayload & payload)
{
jclass setupPayloadClass = env->FindClass("chip/setuppayload/SetupPayload");
jobject setupPayload = env->AllocObject(setupPayloadClass);
jmethodID setupConstr = env->GetMethodID(setupPayloadClass, "<init>", "()V");
jobject setupPayload = env->NewObject(setupPayloadClass, setupConstr);

jfieldID version = env->GetFieldID(setupPayloadClass, "version", "I");
jfieldID vendorId = env->GetFieldID(setupPayloadClass, "vendorId", "I");
Expand All @@ -58,5 +59,63 @@ jobject TransformSetupPayload(JNIEnv * env, SetupPayload payload)
env->SetIntField(setupPayload, discriminator, payload.discriminator);
env->SetLongField(setupPayload, setUpPinCode, payload.setUpPINCode);

jmethodID addOptionalInfoMid =
env->GetMethodID(setupPayloadClass, "addOptionalQRCodeInfo", "(Lchip/setuppayload/OptionalQRCodeInfo;)V");

vector<OptionalQRCodeInfo> optional_info = payload.getAllOptionalVendorData();
for (OptionalQRCodeInfo & info : optional_info)
{

jclass optionalInfoClass = env->FindClass("chip/setuppayload/OptionalQRCodeInfo");
jobject optionalInfo = env->AllocObject(optionalInfoClass);
jfieldID tag = env->GetFieldID(optionalInfoClass, "tag", "I");
jfieldID type = env->GetFieldID(optionalInfoClass, "type", "Lchip/setuppayload/OptionalQRCodeInfo$optionalQRCodeInfoType;");
jfieldID data = env->GetFieldID(optionalInfoClass, "data", "Ljava/lang/String;");
jfieldID int32 = env->GetFieldID(optionalInfoClass, "int32", "I");

env->SetIntField(optionalInfo, tag, info.tag);

jclass enumClass = env->FindClass("chip/setuppayload/OptionalQRCodeInfo$optionalQRCodeInfoType");
jfieldID enumType = nullptr;

switch (info.tag)
{
case optionalQRCodeInfoTypeUnknown:
enumType = env->GetStaticFieldID(enumClass, "optionalQRCodeInfoTypeUnknown",
"Lchip/setuppayload/OptionalQRCodeInfo$optionalQRCodeInfoType;");
break;
case optionalQRCodeInfoTypeString:
enumType = env->GetStaticFieldID(enumClass, "optionalQRCodeInfoTypeString",
"Lchip/setuppayload/OptionalQRCodeInfo$optionalQRCodeInfoType;");
break;
case optionalQRCodeInfoTypeInt32:
enumType = env->GetStaticFieldID(enumClass, "optionalQRCodeInfoTypeInt32",
"Lchip/setuppayload/OptionalQRCodeInfo$optionalQRCodeInfoType;");
break;
case optionalQRCodeInfoTypeInt64:
enumType = env->GetStaticFieldID(enumClass, "optionalQRCodeInfoTypeInt64",
"Lchip/setuppayload/OptionalQRCodeInfo$optionalQRCodeInfoType;");
break;
case optionalQRCodeInfoTypeUInt32:
enumType = env->GetStaticFieldID(enumClass, "optionalQRCodeInfoTypeUInt32",
"Lchip/setuppayload/OptionalQRCodeInfo$optionalQRCodeInfoType;");
break;
case optionalQRCodeInfoTypeUInt64:
enumType = env->GetStaticFieldID(enumClass, "optionalQRCodeInfoTypeUInt64",
"Lchip/setuppayload/OptionalQRCodeInfo$optionalQRCodeInfoType;");
break;
default:
break;
}

jobject enumObj = env->GetStaticObjectField(enumClass, enumType);
env->SetObjectField(optionalInfo, type, enumObj);

env->SetObjectField(optionalInfo, data, env->NewStringUTF(info.data.c_str()));
env->SetIntField(optionalInfo, int32, info.int32);

env->CallVoidMethod(setupPayload, addOptionalInfoMid, optionalInfo);
}

return setupPayload;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package chip.setuppayload;

public class OptionalQRCodeInfo {
enum optionalQRCodeInfoType {
optionalQRCodeInfoTypeUnknown,
optionalQRCodeInfoTypeString,
optionalQRCodeInfoTypeInt32,
optionalQRCodeInfoTypeInt64,
optionalQRCodeInfoTypeUInt32,
optionalQRCodeInfoTypeUInt64
};

public int tag;
public optionalQRCodeInfoType type;
public String data;
public int int32;
};
10 changes: 9 additions & 1 deletion src/setup_payload/java/src/chip/setuppayload/SetupPayload.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package chip.setuppayload;

import java.util.HashMap;
import java.util.Map;

/** Class to hold the data from the scanned QR code or manual entry code. */
public class SetupPayload {
/** Version info of the SetupPayload */
Expand All @@ -17,7 +20,11 @@ public class SetupPayload {
/** The CHIP device manual setup code */
public long setupPinCode;

public SetupPayload() {}
public Map<Integer, OptionalQRCodeInfo> optionalQRCodeInfo;

public SetupPayload() {
this.optionalQRCodeInfo = new HashMap<Integer, OptionalQRCodeInfo>();
}

public SetupPayload(
int version,
Expand All @@ -34,5 +41,6 @@ public SetupPayload(
this.rendezvousInformation = rendezvousInfo;
this.discriminator = discriminator;
this.setupPinCode = setupPinCode;
this.optionalQRCodeInfo = new HashMap<Integer, OptionalQRCodeInfo>();
}
}

0 comments on commit 9d4ffab

Please sign in to comment.