Add Android interface set Display pin callback server API to
authorSandeep Sharma <sandeep.s9@samsung.com>
Mon, 28 Sep 2015 09:09:32 +0000 (14:39 +0530)
committerSachin Agrawal <sachin.agrawal@intel.com>
Mon, 5 Oct 2015 21:46:19 +0000 (21:46 +0000)
display for RANDOM_PIN provisioning.

[Patch #4] Added malloc failed check and Renaming of variable
           to be more consistent.
[Patch #5] Addressed Randeep's review comments and upated
           provisioning sample's build.gradle
[Patch #8] Addressed review comment.

Note: This change depends upon patch set #3047
      This can be cherry picked for 1.0.0-dev

Change-Id: If704a01a760ec0880ea6e1a89f7e856d2f1ba3e5
Signed-off-by: Sandeep Sharma <sandeep.s9@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/3193
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Sachin Agrawal <sachin.agrawal@intel.com>
android/android_api/base/jni/Android.mk
android/android_api/base/jni/JniDisplayPinListener.cpp [new file with mode: 0644]
android/android_api/base/jni/JniDisplayPinListener.h [new file with mode: 0644]
android/android_api/base/jni/JniOcProvisioning.cpp
android/android_api/base/jni/JniOcProvisioning.h
android/android_api/base/src/main/java/org/iotivity/base/OcProvisioning.java

index 05172ee..e07b671 100644 (file)
@@ -80,7 +80,8 @@ LOCAL_SRC_FILES +=  JniOcSecureResource.cpp \
                     JniOcProvisioning.cpp \
                     JniSecureUtils.cpp \
                     JniProvisionResultListner.cpp \
-                    JniPinCheckListener.cpp
+                    JniPinCheckListener.cpp \
+                    JniDisplayPinListener.cpp
 endif
 \r
 LOCAL_LDLIBS := -llog\r
@@ -100,6 +101,7 @@ LOCAL_CPP_FEATURES := rtti exceptions
 LOCAL_C_INCLUDES := $(OIC_SRC_PATH)/include\r
 LOCAL_C_INCLUDES += $(OIC_SRC_PATH)/c_common\r
 LOCAL_C_INCLUDES += $(OIC_SRC_PATH)/c_common/oic_string/include
+LOCAL_C_INCLUDES += $(OIC_SRC_PATH)/c_common/oic_malloc/include
 LOCAL_C_INCLUDES += $(OIC_SRC_PATH)/csdk/stack/include\r
 LOCAL_C_INCLUDES += $(OIC_SRC_PATH)/csdk/ocsocket/include\r
 LOCAL_C_INCLUDES += $(OIC_SRC_PATH)/oc_logger/include\r
diff --git a/android/android_api/base/jni/JniDisplayPinListener.cpp b/android/android_api/base/jni/JniDisplayPinListener.cpp
new file mode 100644 (file)
index 0000000..25ee926
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+* //******************************************************************
+* //
+* // Copyright 2015 Samsung Electronics All Rights Reserved.
+* //
+* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+* //
+* // 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.
+* //
+* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+*/
+#include "JniDisplayPinListener.h"
+#include "oic_string.h"
+#include "oic_malloc.h"
+
+JniDisplayPinListener::JniDisplayPinListener(JNIEnv *env, jobject jListener)
+{
+    m_jgListener = env->NewGlobalRef(jListener);
+}
+
+JniDisplayPinListener::~JniDisplayPinListener()
+{
+    LOGI("~JniDisplayPinListener()");
+    if (m_jgListener)
+    {
+        jint ret;
+        JNIEnv *env = GetJNIEnv(ret);
+        if (NULL == env) return;
+        env->DeleteGlobalRef(m_jgListener);
+        if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
+    }
+}
+
+void JniDisplayPinListener::displayPinCallback(char *pinBuf, size_t pinSize)
+{
+    jint ret;
+    JNIEnv *env = GetJNIEnv(ret);
+    if (NULL == env) return;
+
+    jclass clsL = env->GetObjectClass(m_jgListener);
+
+    if (!clsL)
+    {
+        if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
+        return;
+    }
+
+    jmethodID midL = env->GetMethodID(clsL, "displayPinListener", "(Ljava/lang/String;)V");
+    if (!midL)
+    {
+        if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
+        return;
+    }
+
+    char *pinStr = (char*)OICMalloc(pinSize + 1);
+    if (!pinStr)
+    {
+        LOGE("malloc failed");
+        if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
+        return ;
+    }
+    OICStrcpy(pinStr, (pinSize+1), pinBuf);
+
+    env->CallVoidMethod(m_jgListener, midL, env->NewStringUTF(pinStr));
+    OICFree(pinStr);
+
+    if (env->ExceptionCheck())
+    {
+        LOGE("Java exception is thrown");
+        env->ExceptionClear();
+    }
+
+    if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
+}
diff --git a/android/android_api/base/jni/JniDisplayPinListener.h b/android/android_api/base/jni/JniDisplayPinListener.h
new file mode 100644 (file)
index 0000000..67c9752
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+* //******************************************************************
+* //
+* // Copyright 2015 Samsung Electronics All Rights Reserved.
+* //
+* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+* //
+* // 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.
+* //
+* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+*/
+#include <jni.h>
+#include "JniOcStack.h"
+#include "OCProvisioningManager.h"
+
+#ifndef _Included_org_iotivity_base_DisplayPinListener
+#define _Included_org_iotivity_base_DisplayPinListener
+
+typedef std::function<void(JNIEnv* env, jobject jListener)> RemoveCallback;
+
+class JniDisplayPinListener
+{
+    public:
+        JniDisplayPinListener(JNIEnv *env, jobject jListener);
+        ~JniDisplayPinListener();
+
+        void displayPinCallback(char*, size_t);
+
+    private:
+        jobject m_jgListener;
+};
+#endif
index 7813169..eb3e990 100644 (file)
 
 #include "JniOcProvisioning.h"
 #include "JniPinCheckListener.h"
+#include "JniDisplayPinListener.h"
 
 using namespace OC;
 namespace PH = std::placeholders;
 
-static JniPinCheckListener *PinListener = nullptr;
+static JniPinCheckListener *jniPinListener = nullptr;
+static JniDisplayPinListener *jniDisplayPinListener = nullptr;
 
 void Callback(char *buf, size_t size)
 {
-    if (PinListener)
+    if (jniPinListener)
     {
-        PinListener->PinCallback(buf, size);
+        jniPinListener->PinCallback(buf, size);
     }
     else
     {
-        LOGE("PinListener is null");
+        LOGE("jniPinListener is null");
+    }
+}
+
+void displayPinCB(char *pinBuf, size_t pinSize)
+{
+    if (jniDisplayPinListener)
+    {
+        jniDisplayPinListener->displayPinCallback(pinBuf, pinSize);
+    }
+    else
+    {
+        LOGE("DisplayPinListener is null");
     }
 }
 
@@ -68,8 +82,8 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_ownershipTransferCB
         {
             if (jListener)
             {
-                delete PinListener;
-                PinListener = new JniPinCheckListener(env, jListener);
+                delete jniPinListener;
+                jniPinListener = new JniPinCheckListener(env, jListener);
                 CBData.loadSecretCB = InputPinCodeCallback;
                 CBData.createSecureSessionCB = CreateSecureSessionRandomPinCallbak;
                 CBData.createSelectOxmPayloadCB = CreatePinBasedSelectOxmPayload;
@@ -221,3 +235,38 @@ JNIEXPORT jobjectArray JNICALL Java_org_iotivity_base_OcProvisioning_getDeviceSt
         ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
     }
 }
+
+/*
+ * Class:     org_iotivity_base_OcProvisioning
+ * Method:    setDisplayPinListener
+ * Signature: (Lorg/iotivity/base/OcProvisioning/DisplayPinListener;)V
+ */
+JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_setDisplayPinListener
+  (JNIEnv *env, jclass thiz, jobject jListener)
+{
+
+    LOGI("OcProvisioning_setDisplayPinListener");
+
+    if (!jListener)
+    {
+        ThrowOcException(OC_STACK_INVALID_PARAM, "displayPinListener can't be null");
+        return;
+    }
+    delete jniDisplayPinListener;
+    jniDisplayPinListener = new JniDisplayPinListener(env, jListener);
+
+    try
+    {
+        OCStackResult result = OCSecure::setDisplayPinCB(displayPinCB);
+        if (OC_STACK_OK != result)
+        {
+            ThrowOcException(result, "Failed to set displayPinListener");
+            return;
+        }
+    }
+    catch (OCException& e)
+    {
+        LOGE("%s", e.reason().c_str());
+        ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+    }
+}
index fe5bd9a..05d3bf6 100644 (file)
@@ -72,6 +72,14 @@ JNIEXPORT jobjectArray JNICALL Java_org_iotivity_base_OcProvisioning_discoverOwn
 
 /*
  * Class:     org_iotivity_base_OcProvisioning
+ * Method:    setDisplayPinListener
+ * Signature: (Lorg/iotivity/base/OcProvisioning/DisplayPinListener;)V
+ */
+JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_setDisplayPinListener
+  (JNIEnv *, jclass, jobject);
+
+/*
+ * Class:     org_iotivity_base_OcProvisioning
  * Method:    getDevicestatusLists
  * Signature: (I)[Lorg/iotivity/base/OcSecureResource;
  */
index 636e590..e7debe7 100644 (file)
@@ -88,6 +88,19 @@ public class OcProvisioning {
     }
 
     /**
+     * Server API to set Callback for Displaying stack generated PIN.
+     *
+     * @param DisplayPinListener Pin callback Listener to be registered.
+     * @throws OcException
+     */
+    public static native void setDisplayPinListener(DisplayPinListener displayPinListener)
+        throws OcException;
+
+    public static interface DisplayPinListener {
+        public void displayPinListener(String Pin);
+    }
+
+    /**
      * Method to get Array of owned and un-owned devices in the current subnet.
      *
      * @param timeout    timeout in sec for the API to return.