Align linux and android versions of OcPlatform.java
authorLarry Sachs <larry.j.sachs@intel.com>
Sat, 7 Jan 2017 01:22:46 +0000 (17:22 -0800)
committerRick Bell <richard.s.bell@intel.com>
Tue, 10 Jan 2017 22:31:31 +0000 (22:31 +0000)
Fix [IOT-1713] deprecate the Java support for registerDeviceInfo, in favor of setPropertyValue

Although not directly related, these two commits are intertwined
and it seemed easier to put them in a single commit rather than two
dependent commits.

Change-Id: I67513305e5d00ae942b11150189816d9689f0caa
Signed-off-by: Larry Sachs <larry.j.sachs@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/16219
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Dave Thaler <dthaler@microsoft.com>
Reviewed-by: George Nash <george.nash@intel.com>
Reviewed-by: Habib Virji <habib.virji@samsung.com>
Reviewed-by: Rick Bell <richard.s.bell@intel.com>
java/common/src/main/java/org/iotivity/base/PayloadType.java [new file with mode: 0644]
java/examples-java/simpleclient/src/main/java/org/iotivity/base/examples/Light.java
java/examples-java/simpleclient/src/main/java/org/iotivity/base/examples/SimpleClient.java
java/iotivity-android/src/main/java/org/iotivity/base/OcPlatform.java
java/iotivity-linux/src/main/java/org/iotivity/base/OcPlatform.java
java/jni/JniOcPlatform.cpp
java/jni/JniOcPlatform.h

diff --git a/java/common/src/main/java/org/iotivity/base/PayloadType.java b/java/common/src/main/java/org/iotivity/base/PayloadType.java
new file mode 100644 (file)
index 0000000..58ebcff
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ *******************************************************************
+ *
+ * Copyright 2016 Intel Corporation.
+ *
+ *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ */
+
+package org.iotivity.base;
+
+/**
+ * Payload Type.
+ */
+public enum PayloadType {
+
+    /**
+     * Contents of Payload are invalid.
+     */
+    INVALID(0),
+
+    /**
+     * Discovery Payload.
+     */
+    DISCOVERY(1),
+
+    /**
+     * Device Payload.
+     */
+    DEVICE(2),
+
+    /**
+     * Platform Payload.
+     */
+    PLATFORM(3),
+
+    /**
+     * Representation Payload.
+     */
+    REPRESENTATION(4),
+
+    /**
+     * Security Payload.
+     */
+    SECURITY(5),
+
+    /**
+     * Presence Payload.
+     */
+    PRESENCE(6);
+
+    private int value;
+
+    private PayloadType(int value) {
+        this.value = value;
+    }
+
+    public int getValue() {
+        return this.value;
+    }
+}
index d40c196..300a286 100644 (file)
@@ -64,7 +64,7 @@ public class Light {
     }
 
     public void setName(String name) {
-        this.mName = mName;
+        this.mName = name;
     }
 
     public boolean getState() {
index 52207d1..6a7cc1f 100644 (file)
@@ -159,6 +159,12 @@ public class SimpleClient implements
         }
     }
 
+    @Override
+    public synchronized void onFindResourceFailed(Throwable throwable, String uri) {
+        msg("findResource request has failed");
+        msgError(TAG, throwable.toString());
+    }
+
     /**
      * Local method to get representation of a found light resource
      */
index 965f843..9eccc63 100644 (file)
@@ -23,7 +23,6 @@
 package org.iotivity.base;
 
 import org.iotivity.ca.CaInterface;
-import org.iotivity.base.*;
 
 import java.util.EnumSet;
 import java.util.Iterator;
@@ -572,9 +571,12 @@ public final class OcPlatform {
     /**
      * Register Device Info
      *
+     * @deprecated use setPropertyValue instead
+     *
      * @param ocDeviceInfo object containing all the device specific information
      * @throws OcException if failure
      */
+    @Deprecated
     public static void registerDeviceInfo(
             OcDeviceInfo ocDeviceInfo) throws OcException {
         OcPlatform.initCheck();
@@ -592,9 +594,12 @@ public final class OcPlatform {
     ) throws OcException;
 
     /**
-     * Set param Info
+     * Set Property Value (to a single value)
      *
-     * @throws OcException on failure
+     * @param path value from PayloadType
+     * @param propName property name
+     * @param propValue new property value
+     * @throws OcException if failure
      */
     public static void setPropertyValue(
             int path, String propName, String propValue) throws OcException {
@@ -602,16 +607,31 @@ public final class OcPlatform {
         OcPlatform.setPropertyValue1(path, propName, propValue);
     }
 
+    /**
+     * Set Property Value (to a list of values)
+     *
+     * @param path value from PayloadType
+     * @param propName property name
+     * @param propValue new property value
+     * @throws OcException if failure
+     */
     public static void setPropertyValue(
             int path, String propName, List<String> propValue) throws OcException {
         OcPlatform.initCheck();
         OcPlatform.setPropertyValue0(path, propName, propValue.toArray(new String[propValue.size()]));
     }
 
-    public static void getPropertyValue(
-            int path, String propName, String propValue) throws OcException {
+    /**
+     * Get Property Value
+     *
+     * @param path value from PayloadType
+     * @param propName property name
+     * @return the property value, or null if property name is not found
+     * @throws OcException if failure
+     */
+    public static String getPropertyValue(int path, String propName) throws OcException {
         OcPlatform.initCheck();
-        OcPlatform.getPropertyValue0(path, propName, propValue);
+        return OcPlatform.getPropertyValue0(path, propName);
     }
 
     private static native void setPropertyValue1(
@@ -627,10 +647,9 @@ public final class OcPlatform {
             String[] propValue
     ) throws OcException;
 
-    private static native void getPropertyValue0(
+    private static native String getPropertyValue0(
             int path,
-            String propName,
-            String propValue
+            String propName
     ) throws OcException;
 
     /**
@@ -1173,10 +1192,10 @@ public final class OcPlatform {
      * You can only create this object if OCPlatform was initialized to be a Client or
      * Client/Server. Otherwise, this will return an empty shared ptr.
      *
-     * Nnote For now, OCPlatform SHOULD be initialized to be a Client/Server(Both) for the
+     * Note: For now, OCPlatform SHOULD be initialized to be a Client/Server(Both) for the
      *       methods of this object to work since device id is not generated on Client mode.
      *
-     * @param host                Host IP Address of a account server.
+     * @param host Host IP Address of a account server.
      * @param connectivityTypeSet Set of types of connectivity. Example: CT_ADAPTER_IP
      * @return new AccountManager object
      * @throws OcException if failure
index 2548e0c..b47587f 100644 (file)
@@ -195,6 +195,10 @@ public final class OcPlatform {
             OcResourceResponse ocResourceResponse) throws OcException {
         OcPlatform.initCheck();
 
+        if (ocObservationIdList == null) {
+            throw new OcException(ErrorCode.INVALID_PARAM, "ocObservationIdList cannot be null");
+        }
+
         byte[] idArr = new byte[ocObservationIdList.size()];
         Iterator<Byte> it = ocObservationIdList.iterator();
         int i = 0;
@@ -234,6 +238,10 @@ public final class OcPlatform {
             QualityOfService qualityOfService) throws OcException {
         OcPlatform.initCheck();
 
+        if (ocObservationIdList == null) {
+            throw new OcException(ErrorCode.INVALID_PARAM, "ocObservationIdList cannot be null");
+        }
+
         byte[] idArr = new byte[ocObservationIdList.size()];
         Iterator<Byte> it = ocObservationIdList.iterator();
         int i = 0;
@@ -494,8 +502,8 @@ public final class OcPlatform {
 
     /**
      * This API registers a resource with the server
-     * <p/>
-     * Note: This API applies to server & client side.
+     * <p>
+     * Note: This API applies to server and client side.
      * </P>
      *
      * @param ocResource The instance of OcResource with all data filled
@@ -513,7 +521,7 @@ public final class OcPlatform {
 
     /**
      * This API registers a resource with the server NOTE: This API applies to server side only.
-     * <p/>
+     * <p>
      * Note: This API applies to server side only.
      * </P>
      *
@@ -557,19 +565,85 @@ public final class OcPlatform {
     /**
      * Register Device Info
      *
+     * @deprecated use setPropertyValue instead
+     *
      * @param ocDeviceInfo object containing all the device specific information
      * @throws OcException if failure
      */
+    @Deprecated
     public static void registerDeviceInfo(
             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;
+
+    /**
+     * Set Property Value (to a single value)
+     *
+     * @param path value from PayloadType
+     * @param propName property name
+     * @param propValue new property value
+     * @throws OcException if failure
+     */
+    public static void setPropertyValue(
+            int path, String propName, String propValue) throws OcException {
+        OcPlatform.initCheck();
+        OcPlatform.setPropertyValue1(path, propName, propValue);
+    }
+
+    /**
+     * Set Property Value (to a list of values)
+     *
+     * @param path value from PayloadType
+     * @param propName property name
+     * @param propValue new property value
+     * @throws OcException if failure
+     */
+    public static void setPropertyValue(
+            int path, String propName, List<String> propValue) throws OcException {
+        OcPlatform.initCheck();
+        OcPlatform.setPropertyValue0(path, propName, propValue.toArray(new String[propValue.size()]));
+    }
+
+    /**
+     * Get Property Value
+     *
+     * @param path value from PayloadType
+     * @param propName property name
+     * @return the property value, or null if property name is not found
+     * @throws OcException if failure
+     */
+    public static String getPropertyValue(int path, String propName) throws OcException {
+        OcPlatform.initCheck();
+        return OcPlatform.getPropertyValue0(path, propName);
+    }
+
+    private static native void setPropertyValue1(
+            int path,
+            String propName,
+            String propValue
+    ) throws OcException;
+
+
+    private static native void setPropertyValue0(
+            int path,
+            String propName,
+            String[] propValue
+    ) throws OcException;
+
+    private static native String getPropertyValue0(
+            int path,
+            String propName
     ) throws OcException;
 
     /**
@@ -650,6 +724,11 @@ public final class OcPlatform {
             OcResourceHandle ocResourceCollectionHandle,
             List<OcResourceHandle> ocResourceHandleList) throws OcException {
         OcPlatform.initCheck();
+
+        if (ocResourceHandleList == null) {
+            throw new OcException(ErrorCode.INVALID_PARAM, "ocResourceHandleList cannot be null");
+        }
+
         OcPlatform.bindResources0(
                 ocResourceCollectionHandle,
                 ocResourceHandleList.toArray(
@@ -691,6 +770,11 @@ public final class OcPlatform {
             OcResourceHandle ocResourceCollectionHandle,
             List<OcResourceHandle> ocResourceHandleList) throws OcException {
         OcPlatform.initCheck();
+
+        if (ocResourceHandleList == null) {
+            throw new OcException(ErrorCode.INVALID_PARAM, "ocResourceHandleList cannot be null");
+        }
+
         OcPlatform.unbindResources0(
                 ocResourceCollectionHandle,
                 ocResourceHandleList.toArray(
@@ -853,6 +937,43 @@ public final class OcPlatform {
             OcPresenceHandle ocPresenceHandle) throws OcException;
 
     /**
+     * Subscribes to a server's device presence change events.
+     *
+     * @param host                The IP address/addressable name of the server to subscribe to.
+     * @param di                  Vector which can have the devices id.
+     * @param connectivityTypeSet Set of connectivity types, e.g. IP.
+     * @param onObserveListener   The handler method will be invoked with a map
+     *                            of attribute name and values.
+     * @return a handle object that can be used to identify this subscription request.
+     *         It can be used to unsubscribe from these events in the future.
+     * @throws OcException if failure.
+     */
+    public static OcPresenceHandle subscribeDevicePresence(
+            String host,
+            List<String> di,
+            EnumSet<OcConnectivityType> connectivityTypeSet,
+            OcResource.OnObserveListener onObserveListener) throws OcException {
+        OcPlatform.initCheck();
+        int connTypeInt = 0;
+
+        for (OcConnectivityType connType : OcConnectivityType.values()) {
+            if (connectivityTypeSet.contains(connType))
+                connTypeInt |= connType.getValue();
+        }
+        return OcPlatform.subscribeDevicePresence0(
+                host,
+                di.toArray(new String[di.size()]),
+                connTypeInt,
+                onObserveListener);
+    }
+
+    private static native OcPresenceHandle subscribeDevicePresence0(
+            String host,
+            String[] di,
+            int connectivityType,
+            OcResource.OnObserveListener onObserveListener) throws OcException;
+
+    /**
      * Creates a resource proxy object so that get/put/observe functionality can be used without
      * discovering the object in advance. Note that the consumer of this method needs to provide
      * all of the details required to correctly contact and observe the object. If the consumer
@@ -920,11 +1041,87 @@ public final class OcPlatform {
             throws OcException;
 
     /**
+     *  Method to find all devices which are eligible for direct pairing and return the list.
+     *
+     *  @param timeout timeout for discovering direct pair devices.
+     *  @param onFindDirectPairingListener Callback function, which will receive the list of direct
+     *                                  pairable devices.
+     *  @throws OcException
+     */
+   public static native void findDirectPairingDevices(int timeout,
+            FindDirectPairingListener onFindDirectPairingListener) throws OcException;
+
+    /**
+     *  Method to get list of all paired devices for a given device.
+     *
+     *  @param onGetDirectPairedListener Callback function, which will receive the list of direct
+     *                                 paired devices.
+     *  @throws OcException
+     */
+    public native void getDirectPairedDevices(GetDirectPairedListener onGetDirectPairedListener)
+        throws OcException;
+
+    /**
+     *  Method to perform direct pairing between two devices.
+     *
+     *  @param peer  Target peer
+     *  @param prmType Pairing Method to be used for Pairing
+     *  @param pin pin
+     *  @param onDirectPairingListener Callback function, which will be called after
+     *                                      completion of direct pairing.
+     *  @throws OcException
+     */
+    public static void doDirectPairing(
+            OcDirectPairDevice peer,
+            OcPrmType prmType,
+            String pin,
+            DirectPairingListener onDirectPairingListener) throws OcException {
+
+        OcPlatform.doDirectPairing0(
+                peer,
+                prmType.getValue(),
+                pin,
+                onDirectPairingListener
+                );
+    }
+
+    private static native void doDirectPairing0(OcDirectPairDevice peer,
+            int pmSel, String pinNumber, DirectPairingListener onDirectPairingListener)
+    throws OcException;
+
+    /**
+     * An FindDirectPairingListener can be registered via the OcPlatform.findDirectPairingDevices call.
+     * Event listeners are notified asynchronously
+     */
+    public interface FindDirectPairingListener {
+        public void onFindDirectPairingListener(List<OcDirectPairDevice> ocPairedDeviceList);
+    }
+
+    /**
+     * Listerner to Get List of already Direct Paired devices.
+     * An GetDirectPairedListener can be registered via the OcPlatform.getDirectPairedDevices call.
+     * Event listeners are notified asynchronously
+     */
+    public interface GetDirectPairedListener {
+        public void onGetDirectPairedListener(List<OcDirectPairDevice> ocPairedDeviceList);
+    }
+
+    /**
+     * Listner to get result of doDirectPairing.
+     * An DirectPairingListener can be registered via the OcPlatform.doDirectPairing call.
+     * Event listeners are notified asynchronously
+     */
+    public interface DirectPairingListener {
+        public void onDirectPairingListener(String devId, int result);
+    }
+
+    /**
      * An OnResourceFoundListener can be registered via the OcPlatform.findResource call.
      * Event listeners are notified asynchronously
      */
     public interface OnResourceFoundListener {
         public void onResourceFound(OcResource resource);
+        public void onFindResourceFailed(Throwable ex, String uri);
     }
 
     /**
@@ -954,6 +1151,14 @@ public final class OcPlatform {
     /**
      * An EntityHandler can be registered via the OcPlatform.registerResource call.
      * Event listeners are notified asynchronously
+     *
+     * Note: entityhandler callback :
+     * When you set specific return value like EntityHandlerResult.OK, SLOW
+     * and etc in entity handler callback,
+     * ocstack will be not send response automatically to client
+     * except for error return value like EntityHandlerResult.ERROR
+     * If you want to send response to client with specific result,
+     * sendResponse API should be called with the result value.
      */
     public interface EntityHandler {
         public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest);
@@ -975,4 +1180,49 @@ public final class OcPlatform {
         OcPlatform.initCheck();
         return sPlatformQualityOfService;
     }
+
+    /**
+     * Create an account manager object that can be used for doing request to account server.
+     * You can only create this object if OCPlatform was initialized to be a Client or
+     * Client/Server. Otherwise, this will return an empty shared ptr.
+     *
+     * Note: For now, OCPlatform SHOULD be initialized to be a Client/Server(Both) for the
+     *       methods of this object to work since device id is not generated on Client mode.
+     *
+     * @param host Host IP Address of a account server.
+     * @param connectivityTypeSet Set of types of connectivity. Example: CT_ADAPTER_IP
+     * @return new AccountManager object
+     * @throws OcException if failure
+     */
+    public static OcAccountManager constructAccountManagerObject(
+            String host,
+            EnumSet<OcConnectivityType> connectivityTypeSet) throws OcException {
+        OcPlatform.initCheck();
+        int connTypeInt = 0;
+
+        for (OcConnectivityType connType : OcConnectivityType.values()) {
+            if (connectivityTypeSet.contains(connType))
+            {
+                connTypeInt |= connType.getValue();
+            }
+        }
+        return OcPlatform.constructAccountManagerObject0(
+                host,
+                connTypeInt
+                );
+    }
+
+    private static native OcAccountManager constructAccountManagerObject0(
+            String host,
+            int connectivityType) throws OcException;
+    /**
+     * Method to get device Id in byte array.
+     * @return My DeviceId.
+     */
+    public static native byte[] getDeviceId();
+
+    /**
+     * Method to set DeviceId.
+     */
+    public static native void setDeviceId(byte[] deviceId) throws OcException;
 }
index 5365104..5784c5f 100644 (file)
@@ -1489,7 +1489,7 @@ jobject jListener, jint jResourceProperty)
 /*
 * Class:     org_iotivity_base_OcPlatform
 * Method:    registerDeviceInfo0
-* Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
+* Signature: (Ljava/lang/String;[Ljava/lang/String;)V
 */
 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_registerDeviceInfo0(
     JNIEnv *env,
@@ -1693,6 +1693,11 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_registerPlatformInfo0(
         }
 }
 
+/*
+* Class:     org_iotivity_base_OcPlatform
+* Method:    setPropertyValue0
+* Signature: (ILjava/lang/String;[Ljava/lang/String;)V
+*/
 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_setPropertyValue0
     (JNIEnv *env, jclass clazz, jint jType, jstring jPropName, jobjectArray jPropValue)
 {
@@ -1727,7 +1732,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_setPropertyValue0
 /*
 * Class:     org_iotivity_base_OcPlatform
 * Method:    setPropertyValue0
-* Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
+* Signature: (ILjava/lang/String;Ljava/lang/String;)V
 */
 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_setPropertyValue1
     (JNIEnv *env, jclass clazz, jint jType, jstring jPropName, jstring jPropValue)
@@ -1762,10 +1767,10 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_setPropertyValue1
 /*
 * Class:     org_iotivity_base_OcPlatform
 * Method:    getPropertyValue
-* Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
+* Signature: (ILjava/lang/String;)Ljava/lang/String;
 */
-JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getPropertyValue0
-    (JNIEnv *env, jclass clazz, jint jType, jstring jPropName, jstring jPropValue)
+JNIEXPORT jstring JNICALL Java_org_iotivity_base_OcPlatform_getPropertyValue0
+    (JNIEnv *env, jclass clazz, jint jType, jstring jPropName)
 {
     try
     {
@@ -1781,14 +1786,16 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getPropertyValue0
         if (OC_STACK_OK != result)
         {
             ThrowOcException(result, "Failed to get property value.");
-            return;
+            return nullptr;
         }
+        return env->NewStringUTF(propValue.c_str());
     }
     catch (OCException& e)
     {
         LOGE("Error is due to %s", e.reason().c_str());
         ThrowOcException(e.code(), e.reason().c_str());
     }
+    return nullptr;
 }
 
 /*
index 4a9d134..df5ab12 100644 (file)
@@ -214,7 +214,7 @@ extern "C" {
     /*
     * Class:     org_iotivity_base_OcPlatform
     * Method:    setPropertyValue0
-    * Signature: (Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;)V
+    * Signature: (ILjava/lang/String;[Ljava/lang/String;)V
     */
     JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_setPropertyValue0
         (JNIEnv *, jclass, jint, jstring, jobjectArray);
@@ -222,7 +222,7 @@ extern "C" {
     /*
     * Class:     org_iotivity_base_OcPlatform
     * Method:    setPropertyValue1
-    * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
+    * Signature: (ILjava/lang/String;Ljava/lang/String;)V
     */
     JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_setPropertyValue1
         (JNIEnv *, jclass, jint, jstring, jstring);
@@ -230,10 +230,10 @@ extern "C" {
     /*
     * Class:     org_iotivity_base_OcPlatform
     * Method:    getPropertyValue0
-    * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
+    * Signature: (ILjava/lang/String;)Ljava/lang/String;
     */
-    JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getPropertyValue0
-        (JNIEnv *, jint, jstring, jstring);
+    JNIEXPORT jstring JNICALL Java_org_iotivity_base_OcPlatform_getPropertyValue0
+        (JNIEnv *, jclass, jint, jstring);
 
     /*
     * Class:     org_iotivity_base_OcPlatform