From b5f84506689d9a38925e37ab219243f0b9653518 Mon Sep 17 00:00:00 2001 From: Tim Kourt Date: Fri, 20 May 2016 14:40:29 -0700 Subject: [PATCH] [RI][Android]: Propagation of the mandatory device info types into Android Follow up for the https://gerrit.iotivity.org/gerrit/#/c/5747/ Change-Id: Iedafa880cea0cf5ee25e30f29429889b3b5b8b50 Signed-off-by: Tim Kourt Reviewed-on: https://gerrit.iotivity.org/gerrit/8261 Tested-by: jenkins-iotivity Reviewed-by: Rahul Rahul Reviewed-by: jihwan seo Reviewed-by: Jon A. Cruz --- android/android_api/base/jni/JniOcPlatform.cpp | 35 ++++++++++++++++++---- android/android_api/base/jni/JniOcPlatform.h | 4 +-- .../java/org/iotivity/base/SmokeTest.java | 6 +++- .../main/java/org/iotivity/base/OcDeviceInfo.java | 15 +++++++--- .../main/java/org/iotivity/base/OcPlatform.java | 8 +++-- .../base/examples/DeviceDiscoveryClient.java | 8 ++++- .../base/examples/DeviceDiscoveryServer.java | 8 ++++- 7 files changed, 68 insertions(+), 16 deletions(-) diff --git a/android/android_api/base/jni/JniOcPlatform.cpp b/android/android_api/base/jni/JniOcPlatform.cpp index 93029c9..8987a69 100644 --- a/android/android_api/base/jni/JniOcPlatform.cpp +++ b/android/android_api/base/jni/JniOcPlatform.cpp @@ -26,6 +26,7 @@ #include "JniOcResourceResponse.h" #include "JniOcSecurity.h" #include "JniUtils.h" +#include "ocpayload.h" using namespace OC; @@ -1070,20 +1071,44 @@ jobject jListener, jint jResourceProperty) JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_registerDeviceInfo0( JNIEnv *env, jclass clazz, - jstring jDeviceName) + jstring jDeviceName, + jobjectArray jDeviceTypes) { LOGI("OcPlatform_registerDeviceInfo"); - std::string deviceName; - if (jDeviceName) + if (!jDeviceName) { - deviceName = env->GetStringUTFChars(jDeviceName, nullptr); + ThrowOcException(OC_STACK_INVALID_PARAM, "deviceName cannot be null"); + return; + } + + if (!jDeviceTypes) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "deviceTypes cannot be null"); + return; } OCDeviceInfo deviceInfo; try { - DuplicateString(&deviceInfo.deviceName, deviceName); + DuplicateString(&deviceInfo.deviceName, env->GetStringUTFChars(jDeviceName, nullptr)); + deviceInfo.types = NULL; + + jsize len = env->GetArrayLength(jDeviceTypes); + for (jsize i = 0; i < len; ++i) + { + jstring jStr = (jstring)env->GetObjectArrayElement(jDeviceTypes, i); + if (!jStr) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "device type cannot be null"); + return; + } + + OCResourcePayloadAddStringLL(&deviceInfo.types, env->GetStringUTFChars(jStr, nullptr)); + if (env->ExceptionCheck()) return; + + env->DeleteLocalRef(jStr); + } } catch (std::exception &e) { diff --git a/android/android_api/base/jni/JniOcPlatform.h b/android/android_api/base/jni/JniOcPlatform.h index cfd6853..c66ad43 100644 --- a/android/android_api/base/jni/JniOcPlatform.h +++ b/android/android_api/base/jni/JniOcPlatform.h @@ -163,10 +163,10 @@ extern "C" { /* * Class: org_iotivity_base_OcPlatform * Method: registerDeviceInfo0 - * Signature: (Ljava/lang/String;)V + * Signature: (Ljava/lang/String;[Ljava/lang/String;)V */ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_registerDeviceInfo0 - (JNIEnv *, jclass, jstring); + (JNIEnv *, jclass, jstring, jobjectArray); /* * Class: org_iotivity_base_OcPlatform diff --git a/android/android_api/base/src/androidTest/java/org/iotivity/base/SmokeTest.java b/android/android_api/base/src/androidTest/java/org/iotivity/base/SmokeTest.java index 3ab7dd2..d6a67c7 100644 --- a/android/android_api/base/src/androidTest/java/org/iotivity/base/SmokeTest.java +++ b/android/android_api/base/src/androidTest/java/org/iotivity/base/SmokeTest.java @@ -25,6 +25,7 @@ package org.iotivity.base; import android.test.InstrumentationTestCase; import android.util.Log; +import java.util.Arrays; import java.util.Date; import java.util.EnumSet; import java.util.HashMap; @@ -1048,7 +1049,10 @@ public class SmokeTest extends InstrumentationTestCase { } }; - OcDeviceInfo devInfo = new OcDeviceInfo("myDeviceName"); + OcDeviceInfo devInfo = new OcDeviceInfo( + "myDeviceName", + Arrays.asList(new String[]{"oic.d.test"}) + ); try { //server diff --git a/android/android_api/base/src/main/java/org/iotivity/base/OcDeviceInfo.java b/android/android_api/base/src/main/java/org/iotivity/base/OcDeviceInfo.java index acb4cfa..4320ad1 100644 --- a/android/android_api/base/src/main/java/org/iotivity/base/OcDeviceInfo.java +++ b/android/android_api/base/src/main/java/org/iotivity/base/OcDeviceInfo.java @@ -22,20 +22,27 @@ package org.iotivity.base; +import java.util.List; + /** - * This class is expected as input for device properties. Device name is mandatory and expected - * from the application. Device id of type UUID will be generated by the stack. + * This class is expected as input for device properties. Device name and types are mandatory + * and expected from the application. Device id of type UUID will be generated by the stack. */ public class OcDeviceInfo { private String mDeviceName; + private List mDeviceTypes; - public OcDeviceInfo(String deviceName) { - + public OcDeviceInfo(String deviceName, List deviceTypes) { this.mDeviceName = deviceName; + this.mDeviceTypes = deviceTypes; } public String getDeviceName() { return mDeviceName; } + + public List getDeviceTypes() { + return mDeviceTypes; + } } diff --git a/android/android_api/base/src/main/java/org/iotivity/base/OcPlatform.java b/android/android_api/base/src/main/java/org/iotivity/base/OcPlatform.java index d08b1c6..e37838d 100644 --- a/android/android_api/base/src/main/java/org/iotivity/base/OcPlatform.java +++ b/android/android_api/base/src/main/java/org/iotivity/base/OcPlatform.java @@ -541,12 +541,16 @@ public final class OcPlatform { OcDeviceInfo ocDeviceInfo) throws OcException { OcPlatform.initCheck(); OcPlatform.registerDeviceInfo0( - ocDeviceInfo.getDeviceName() + ocDeviceInfo.getDeviceName(), + ocDeviceInfo.getDeviceTypes().toArray( + new String[ocDeviceInfo.getDeviceTypes().size()] + ) ); } private static native void registerDeviceInfo0( - String deviceName + String deviceName, + String[] deviceTypes ) throws OcException; /** diff --git a/android/examples/devicediscoveryclient/src/main/java/org/iotivity/base/examples/DeviceDiscoveryClient.java b/android/examples/devicediscoveryclient/src/main/java/org/iotivity/base/examples/DeviceDiscoveryClient.java index 6c2d317..28f9a93 100644 --- a/android/examples/devicediscoveryclient/src/main/java/org/iotivity/base/examples/DeviceDiscoveryClient.java +++ b/android/examples/devicediscoveryclient/src/main/java/org/iotivity/base/examples/DeviceDiscoveryClient.java @@ -127,7 +127,7 @@ public class DeviceDiscoveryClient extends Activity implements private final static Map DEVICE_INFO_KEYS = new HashMap() {{ put("di", "Device ID: "); put("n", "Device name: "); - put("lcv", "Spec version url: "); + put("icv", "Spec version url: "); put("dmv", "Data Model: "); }}; @@ -138,6 +138,12 @@ public class DeviceDiscoveryClient extends Activity implements for (String key : DEVICE_INFO_KEYS.keySet()) { msg("\t" + DEVICE_INFO_KEYS.get(key) + ocRepresentation.getValue(key)); } + + msg("\tDevice types:"); + + for (String type : ocRepresentation.getResourceTypes()) { + msg("\t\t" + type); + } } catch (OcException e) { Log.e(TAG, e.toString()); msg("Failed to read device info values."); diff --git a/android/examples/devicediscoveryserver/src/main/java/org/iotivity/base/examples/DeviceDiscoveryServer.java b/android/examples/devicediscoveryserver/src/main/java/org/iotivity/base/examples/DeviceDiscoveryServer.java index a2920f4..c0d96b7 100644 --- a/android/examples/devicediscoveryserver/src/main/java/org/iotivity/base/examples/DeviceDiscoveryServer.java +++ b/android/examples/devicediscoveryserver/src/main/java/org/iotivity/base/examples/DeviceDiscoveryServer.java @@ -40,6 +40,8 @@ import org.iotivity.base.PlatformConfig; import org.iotivity.base.QualityOfService; import org.iotivity.base.ServiceType; +import java.util.Arrays; + /** * This sample demonstrates platform and device discovery feature. * The server sets the platform and device related info. which can be later retrieved by a client. @@ -61,7 +63,11 @@ public class DeviceDiscoveryServer extends Activity { msg("Configuring platform."); OcPlatform.Configure(platformConfig); - OcDeviceInfo deviceInfo = new OcDeviceInfo("myDeviceName"); + OcDeviceInfo deviceInfo = new OcDeviceInfo( + "myDeviceName", + Arrays.asList(new String[]{"oic.d.phone"}) + ); + try { msg("Registering device info"); OcPlatform.registerDeviceInfo(deviceInfo); -- 2.7.4