Adding Android Api for Mutual Verified Just-Works
authorSandeep Sharma <sandeep.s9@samsung.com>
Fri, 2 Dec 2016 12:00:37 +0000 (17:30 +0530)
committerRandeep Singh <randeep.s@samsung.com>
Thu, 8 Dec 2016 05:50:06 +0000 (05:50 +0000)
patch #4 Add support for Mutual verified JW OTM in android PT.
patch #5 Fix names as per C++

Change-Id: I6be03b4af14645808171d25e92b78ef0382c2e7a
Signed-off-by: Sandeep Sharma <sandeep.s9@samsung.com>
Signed-off-by: Sunil Kumar K R <sunil.k14@samsung.com>
Signed-off-by: Sandeep Sharma <sandeep.s9@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/14843
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Randeep Singh <randeep.s@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/15235

13 files changed:
android/android_api/base/jni/Android.mk
android/android_api/base/jni/JniConfirmNumListener.cpp [new file with mode: 0644]
android/android_api/base/jni/JniConfirmNumListener.h [new file with mode: 0644]
android/android_api/base/jni/JniDisplayVerifyNumListener.cpp [new file with mode: 0644]
android/android_api/base/jni/JniDisplayVerifyNumListener.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/MVJustWorksOptionMask.java [new file with mode: 0644]
android/android_api/base/src/main/java/org/iotivity/base/OcProvisioning.java
android/examples/provisioningclient/src/main/AndroidManifest.xml
android/examples/provisioningclient/src/main/assets/oic_svr_db_client.dat
android/examples/provisioningclient/src/main/java/org/iotivity/base/examples/provisioningclient/ProvisioningClient.java
android/examples/provisioningclient/src/main/res/menu/menu_secure_provision_client.xml

index d2543b5..531cb0c 100644 (file)
@@ -131,7 +131,9 @@ ifeq ($(SECURED), 1)
                         JniSecureUtils.cpp \\r
                         JniProvisionResultListner.cpp \\r
                         JniPinCheckListener.cpp \\r
-                        JniDisplayPinListener.cpp\r
+                        JniDisplayPinListener.cpp \\r
+                        JniDisplayVerifyNumListener.cpp \
+                        JniConfirmNumListener.cpp
 endif\r
 \r
 ifeq ($(WITH_CLOUD), 1)\r
diff --git a/android/android_api/base/jni/JniConfirmNumListener.cpp b/android/android_api/base/jni/JniConfirmNumListener.cpp
new file mode 100644 (file)
index 0000000..402f3e1
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+* //******************************************************************
+* //
+* // Copyright 2016 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 "JniConfirmNumListener.h"
+
+JniConfirmNumListener::JniConfirmNumListener(JNIEnv *env, jobject jListener)
+{
+    m_jgListener = env->NewGlobalRef(jListener);
+}
+
+JniConfirmNumListener::~JniConfirmNumListener()
+{
+    LOGI("~JniConfirmNumListener()");
+    if (m_jgListener)
+    {
+        jint ret = JNI_ERR;
+        JNIEnv *env = GetJNIEnv(ret);
+        if (NULL == env) return;
+        env->DeleteGlobalRef(m_jgListener);
+        if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
+    }
+}
+
+OCStackResult JniConfirmNumListener::confirmMutualVerifNumCallback()
+{
+    jint ret = JNI_ERR;
+    JNIEnv *env = GetJNIEnv(ret);
+    if (NULL == env)
+    {
+        return OC_STACK_ERROR;
+    }
+
+    jclass clsL = env->GetObjectClass(m_jgListener);
+
+    if (!clsL)
+    {
+        if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
+        return OC_STACK_ERROR;
+    }
+
+    jmethodID midL = env->GetMethodID(clsL, "confirmNumListener", "()I");
+    if (!midL)
+    {
+        if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
+        return OC_STACK_ERROR;
+    }
+
+    OCStackResult result = (OCStackResult) env->CallIntMethod(m_jgListener, midL);
+
+    if (env->ExceptionCheck())
+    {
+        LOGE("Java exception is thrown");
+        env->ExceptionClear();
+    }
+
+    if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
+    return result;
+}
diff --git a/android/android_api/base/jni/JniConfirmNumListener.h b/android/android_api/base/jni/JniConfirmNumListener.h
new file mode 100644 (file)
index 0000000..13b30ec
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+* //******************************************************************
+* //
+* // Copyright 2016 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.hpp"
+
+#ifndef _Included_org_iotivity_base_JniConfirmNumListener
+#define _Included_org_iotivity_base_JniConfirmNumListener
+
+class JniConfirmNumListener
+{
+    public:
+        JniConfirmNumListener(JNIEnv *env, jobject jListener);
+        ~JniConfirmNumListener();
+
+        OCStackResult confirmMutualVerifNumCallback();
+
+    private:
+        jobject m_jgListener;
+};
+#endif
diff --git a/android/android_api/base/jni/JniDisplayVerifyNumListener.cpp b/android/android_api/base/jni/JniDisplayVerifyNumListener.cpp
new file mode 100644 (file)
index 0000000..20112d9
--- /dev/null
@@ -0,0 +1,99 @@
+/*
+* //******************************************************************
+* //
+* // Copyright 2016 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 "JniDisplayVerifyNumListener.h"
+#include "oic_malloc.h"
+
+JniDisplayVerifyNumListener::JniDisplayVerifyNumListener(JNIEnv *env, jobject jListener)
+{
+    m_jgListener = env->NewGlobalRef(jListener);
+}
+
+JniDisplayVerifyNumListener::~JniDisplayVerifyNumListener()
+{
+    LOGI("~JniDisplayVerifyNumListener()");
+    if (m_jgListener)
+    {
+        jint ret = JNI_ERR;
+        JNIEnv *env = GetJNIEnv(ret);
+        if (NULL == env) return;
+        env->DeleteGlobalRef(m_jgListener);
+        if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
+    }
+}
+
+OCStackResult JniDisplayVerifyNumListener::displayMutualVerifNumCallback(uint8_t verifNum[3])
+{
+    jint ret = JNI_ERR;
+    JNIEnv *env = GetJNIEnv(ret);
+    char *byteArray;
+
+    if (NULL == env)
+    {
+        return OC_STACK_ERROR;
+    }
+
+    jclass clsL = env->GetObjectClass(m_jgListener);
+
+    if (!clsL)
+    {
+        if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
+        return OC_STACK_ERROR;
+    }
+
+    jmethodID midL = env->GetMethodID(clsL, "displayNumListener", "(Ljava/lang/String;)I");
+    if (!midL)
+    {
+        if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
+        return OC_STACK_ERROR;
+    }
+
+    byteArray = (char*)OICCalloc(20, sizeof(char));
+
+    if (NULL == byteArray)
+    {
+        if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
+        return OC_STACK_NO_MEMORY;
+    }
+
+    sprintf(byteArray, "%02X%02X%02X",  verifNum[0], verifNum[1], verifNum[2]);
+
+    jstring jStr = env->NewStringUTF(byteArray);
+    if (!jStr)
+    {
+        if (JNI_EDETACHED == ret)
+        {
+            g_jvm->DetachCurrentThread();
+        }
+        return OC_STACK_ERROR;
+    }
+    OCStackResult result = (OCStackResult)env->CallIntMethod(m_jgListener, midL, jStr);
+
+    if (env->ExceptionCheck())
+    {
+        LOGE("Java exception is thrown");
+        env->ExceptionClear();
+    }
+
+    if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
+    free(byteArray);
+    return result;
+}
diff --git a/android/android_api/base/jni/JniDisplayVerifyNumListener.h b/android/android_api/base/jni/JniDisplayVerifyNumListener.h
new file mode 100644 (file)
index 0000000..ed06859
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+* //******************************************************************
+* //
+* // Copyright 2016 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.hpp"
+
+#ifndef _Included_org_iotivity_base_JniDisplayVerifyNumListener
+#define _Included_org_iotivity_base_JniDisplayVerifyNumListener
+
+class JniDisplayVerifyNumListener
+{
+    public:
+        JniDisplayVerifyNumListener(JNIEnv *env, jobject jListener);
+        ~JniDisplayVerifyNumListener();
+
+        OCStackResult displayMutualVerifNumCallback(uint8_t verfNum[3]);
+
+    private:
+        jobject m_jgListener;
+};
+#endif
index 0bacfbd..42387ab 100644 (file)
 #include "JniOcProvisioning.h"
 #include "JniPinCheckListener.h"
 #include "JniDisplayPinListener.h"
+#include "oxmverifycommon.h"
+#include "JniDisplayVerifyNumListener.h"
+#include "JniConfirmNumListener.h"
 
 using namespace OC;
 namespace PH = std::placeholders;
 
 static JniPinCheckListener *jniPinListener = nullptr;
 static JniDisplayPinListener *jniDisplayPinListener = nullptr;
+static JniDisplayVerifyNumListener *jniDisplayMutualVerifyNumListener = nullptr;
+static JniConfirmNumListener *jniConfirmMutualVerifyNumListener = nullptr;
 
 void Callback(char *buf, size_t size)
 {
@@ -54,6 +59,38 @@ void displayPinCB(char *pinBuf, size_t pinSize)
     }
 }
 
+OCStackResult displayMutualVerifNumCB(uint8_t *verifyNum)
+{
+    OCStackResult res;
+
+    if (jniDisplayMutualVerifyNumListener)
+    {
+        res = jniDisplayMutualVerifyNumListener->displayMutualVerifNumCallback(verifyNum);
+    }
+    else
+    {
+        res = OC_STACK_ERROR;
+        LOGE("DisplayMutualVerifyNumListener is null");
+    }
+    return res;
+}
+
+OCStackResult confirmMutualVerifNumCB()
+{
+    OCStackResult res;
+
+    if (jniConfirmMutualVerifyNumListener)
+    {
+        res = jniConfirmMutualVerifyNumListener->confirmMutualVerifNumCallback();
+    }
+    else
+    {
+        res = OC_STACK_ERROR;
+        LOGE("ConfirmMutualVerifyNumListener is null");
+    }
+    return res;
+}
+
 /*
  * Class:     org_iotivity_base_OcProvisioning
  * Method:    ownershipTransferCDdata
@@ -246,6 +283,136 @@ JNIEXPORT jobjectArray JNICALL Java_org_iotivity_base_OcProvisioning_getDeviceSt
 
 /*
  * Class:     org_iotivity_base_OcProvisioning
+ * Method:    setDisplayNumListener
+ * Signature: (Lorg/iotivity/base/OcProvisioning/DisplayNumListener;)V
+ */
+JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_setDisplayNumListener
+  (JNIEnv *env, jclass clazz, jobject jListener)
+{
+    LOGI("OcProvisioning_setDisplayNumListener");
+
+    if (!jListener)
+    {
+        ThrowOcException(OC_STACK_INVALID_CALLBACK, "Listner can't be null");
+        return;
+    }
+    delete jniDisplayMutualVerifyNumListener;
+    jniDisplayMutualVerifyNumListener = new JniDisplayVerifyNumListener(env, jListener);
+
+    try
+    {
+        OCStackResult result = OCSecure::registerDisplayNumCallback(displayMutualVerifNumCB);
+        if (OC_STACK_OK != result)
+        {
+            ThrowOcException(result, "Failed to set Listner");
+            return;
+        }
+    }
+    catch (OCException& e)
+    {
+        LOGE("%s", e.reason().c_str());
+        ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+        return;
+    }
+}
+
+/*
+ * Class:     org_iotivity_base_OcProvisioning
+ * Method:    unsetDisplayNumListener
+ * Signature: ()I
+ */
+JNIEXPORT jint JNICALL Java_org_iotivity_base_OcProvisioning_unsetDisplayNumListener
+  (JNIEnv * env, jclass clazz)
+{
+    LOGI("OcProvisioning_unsetDisplayNumListener");
+
+    OCStackResult result = OCSecure::deregisterDisplayNumCallback();
+
+    if (OC_STACK_OK != result)
+    {
+        ThrowOcException(OC_STACK_INVALID_CALLBACK, "Failed to unset Listener");
+    }
+
+    return result;
+}
+
+/*
+ * Class:     org_iotivity_base_OcProvisioning
+ * Method:    setConfirmNumListener
+ * Signature: (Lorg/iotivity/base/OcProvisioning/ConfirmNumListener;)V
+ */
+JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_setConfirmNumListener
+  (JNIEnv *env, jclass clazz, jobject jListener)
+{
+    LOGI("OcProvisioning_setConfirmNumListener");
+
+    if (!jListener)
+    {
+        ThrowOcException(OC_STACK_INVALID_CALLBACK, "Listner can't be null");
+        return;
+    }
+    delete jniConfirmMutualVerifyNumListener;
+    jniConfirmMutualVerifyNumListener = new JniConfirmNumListener(env, jListener);
+
+    try
+    {
+        OCStackResult result = OCSecure::registerUserConfirmCallback(confirmMutualVerifNumCB);
+        if (OC_STACK_OK != result)
+        {
+            ThrowOcException(result, "Failed to set Listner");
+            return;
+        }
+    }
+    catch (OCException& e)
+    {
+        LOGE("%s", e.reason().c_str());
+        ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
+        return;
+    }
+}
+
+/*
+ * Class:     org_iotivity_base_OcProvisioning
+ * Method:    unsetConfirmNumListener
+ * Signature: ()I
+ */
+JNIEXPORT jint JNICALL Java_org_iotivity_base_OcProvisioning_unsetConfirmNumListener
+  (JNIEnv *env, jclass clazz)
+{
+    LOGI("OcProvisioning_unsetConfirmNumListener");
+
+    OCStackResult result = OCSecure::deregisterUserConfirmCallback();
+
+    if (OC_STACK_OK != result)
+    {
+        ThrowOcException(OC_STACK_INVALID_CALLBACK, "Failed to unser Listener");
+    }
+
+    return result;
+}
+/*
+ * Class:     org_iotivity_base_OcProvisioning
+ * Method:    setMVJustWorksOptions0
+ * Signature: (I)I
+ */
+JNIEXPORT jint JNICALL Java_org_iotivity_base_OcProvisioning_setMVJustWorksOptions0
+  (JNIEnv *env, jclass clazz, jint options)
+{
+    LOGI("OcProvisioning_setMVJustWorksOptions0");
+
+    OCStackResult result = OCSecure::setVerifyOptionMask((VerifyOptionBitmask_t)options);
+
+    if (OC_STACK_OK != result)
+    {
+        ThrowOcException(OC_STACK_INVALID_CALLBACK, "setMVJustWorksOptions Failed");
+    }
+
+    return result;
+}
+
+
+/*
+ * Class:     org_iotivity_base_OcProvisioning
  * Method:    setDisplayPinListener
  * Signature: (Lorg/iotivity/base/OcProvisioning/DisplayPinListener;)V
  */
index 290744e..bad4a8d 100644 (file)
@@ -80,11 +80,52 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_setDisplayPinListen
 
 /*
  * Class:     org_iotivity_base_OcProvisioning
+ * Method:    setDisplayNumListener
+ * Signature: (Lorg/iotivity/base/OcProvisioning/DisplayNumListener;)V
+ */
+JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_setDisplayNumListener
+  (JNIEnv *, jclass, jobject);
+
+/*
+ * Class:     org_iotivity_base_OcProvisioning
+ * Method:    unsetDisplayNumListener
+ * Signature: ()I
+ */
+JNIEXPORT jint JNICALL Java_org_iotivity_base_OcProvisioning_unsetDisplayNumListener
+  (JNIEnv *, jclass);
+
+/*
+ * Class:     org_iotivity_base_OcProvisioning
+ * Method:    setConfirmNumListener
+ * Signature: (Lorg/iotivity/base/OcProvisioning/ConfirmNumListener;)V
+ */
+JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_setConfirmNumListener
+  (JNIEnv *, jclass, jobject);
+
+/*
+ * Class:     org_iotivity_base_OcProvisioning
+ * Method:    unsetConfirmNumListener
+ * Signature: ()I
+ */
+JNIEXPORT jint JNICALL Java_org_iotivity_base_OcProvisioning_unsetConfirmNumListener
+  (JNIEnv *, jclass);
+
+/*
+ * Class:     org_iotivity_base_OcProvisioning
+ * Method:    setMVJustWorksOptions0
+ * Signature: (I)I
+ */
+JNIEXPORT jint JNICALL Java_org_iotivity_base_OcProvisioning_setMVJustWorksOptions0
+  (JNIEnv *, jclass, jint);
+
+/*
+ * Class:     org_iotivity_base_OcProvisioning
  * Method:    getDevicestatusLists
  * Signature: (I)[Lorg/iotivity/base/OcSecureResource;
  */
 JNIEXPORT jobjectArray JNICALL Java_org_iotivity_base_OcProvisioning_getDeviceStatusList1
   (JNIEnv *, jclass, jint);
+
 /*
  * Class:     org_iotivity_base_OcProvisioning
  * Method:    saveTrustCertChain1
diff --git a/android/android_api/base/src/main/java/org/iotivity/base/MVJustWorksOptionMask.java b/android/android_api/base/src/main/java/org/iotivity/base/MVJustWorksOptionMask.java
new file mode 100644 (file)
index 0000000..7e111ca
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ *******************************************************************
+ *
+ * Copyright 2016 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.
+ *
+ *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ */
+
+package org.iotivity.base;
+
+import java.security.InvalidParameterException;
+import java.util.EnumSet;
+
+public enum MVJustWorksOptionMask {
+    NOT_APPLICABLE (0x0),
+    DISPLAY_MUTUAL_VERIF_NUM  (0x1 << 0),
+    CONFIRM_MUTUAL_VERIF_NUM  (0x1 << 1),
+    ;
+
+    private int value;
+
+    private MVJustWorksOptionMask(int value) {
+        this.value = value;
+    }
+
+    public int getValue() {
+        return this.value;
+    }
+
+    public static EnumSet<MVJustWorksOptionMask> convertToEnumSet(int value) {
+        EnumSet<MVJustWorksOptionMask> typeSet = null;
+
+        for (MVJustWorksOptionMask v : values()) {
+            if (0 != (value & v.getValue())) {
+                if (null == typeSet) {
+                    typeSet = EnumSet.of(v);
+                } else {
+                    typeSet.add(v);
+                }
+            }
+        }
+
+        if (null == typeSet || typeSet.isEmpty()) {
+            throw new InvalidParameterException("Unexpected MVJustWorksOptionMask value:" + value);
+        }
+
+        return typeSet;
+    }
+
+}
index 8cb282b..2130b36 100644 (file)
@@ -25,6 +25,7 @@ package org.iotivity.base;
 import java.util.List;
 import java.util.Arrays;
 import java.util.Map;
+import java.util.EnumSet;
 
 /**
  * OcProvisionig represents functions corresponding to the provisioing of
@@ -101,6 +102,68 @@ public class OcProvisioning {
     }
 
     /**
+     * API to Set callback for displaying verifNum in verified Just-Works.
+     *
+     *@param DisplayNumListener callback Listener to be registered for
+                                            displaying VerifyNUm.
+     *@throws OcException
+     */
+    public static native void setDisplayNumListener(
+            DisplayNumListener displayNumListener) throws OcException;
+
+    public static interface DisplayNumListener {
+        public int displayNumListener(String verifyNum);
+    }
+
+    /**
+     * API to unregister DisplayNumListener Listener
+     *
+     *@return  0 on success, 1 on failure
+     *@throws OcException
+     */
+    public static native int unsetDisplayNumListener() throws OcException;
+
+    /**
+     * API to Set callback for getting user confirmation in verified
+     * Just-Works
+     *
+     *@param ConfirmNumListener callback Listener to be registered for getting user confirmation.
+     *@throws OcException
+     */
+    public static native void setConfirmNumListener(ConfirmNumListener
+            confirmNumListener) throws OcException;
+
+    public static interface ConfirmNumListener {
+        public int confirmNumListener();
+    }
+
+    /**
+     * API to unregister ConfirmMutualVerifyNum Listener
+     *
+     *@return  0 on success, 1 on failure
+     *@throws OcException
+     */
+    public static native int unsetConfirmNumListener() throws OcException;
+
+    /**
+     * API to set options for Mutual Verified Just-works
+     * Default is  for both screen PIN display and get user confirmation.
+     *
+     */
+    public static int setMVJustWorksOptions(EnumSet<MVJustWorksOptionMask> optionMask) throws OcException {
+
+        int optionMaskInt = 0;
+
+        for (MVJustWorksOptionMask ops : MVJustWorksOptionMask.values()) {
+            if (optionMask.contains(ops))
+                optionMaskInt |= ops.getValue();
+        }
+        return setMVJustWorksOptions0(optionMaskInt);
+    }
+
+    private static native int setMVJustWorksOptions0(int optionsMask) throws OcException;
+
+    /**
      * Method to get Array of owned and un-owned devices in the current subnet.
      *
      * @param timeout    timeout in sec for the API to return.
index 93a375b..1ab6976 100644 (file)
@@ -1,7 +1,8 @@
 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="org.iotivity.base.examples.provisioningclient" >
-
+    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
+    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     <application
         android:allowBackup="true"
         android:icon="@mipmap/ic_launcher"
index f3cf369..a7549d1 100644 (file)
Binary files a/android/examples/provisioningclient/src/main/assets/oic_svr_db_client.dat and b/android/examples/provisioningclient/src/main/assets/oic_svr_db_client.dat differ
index 820eddf..26b446e 100644 (file)
@@ -1,6 +1,8 @@
 package org.iotivity.base.examples.provisioningclient;
 
 import android.app.Activity;
+import android.app.AlertDialog;
+import android.content.DialogInterface;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
@@ -14,6 +16,10 @@ import android.util.Log;
 import android.view.Gravity;
 import android.widget.LinearLayout;
 import android.widget.TextView;
+import android.view.Menu;
+import android.view.MenuItem;
+import android.os.Environment;
+import android.widget.Toast;
 
 import org.iotivity.base.CredType;
 import org.iotivity.base.EncodingType;
@@ -23,6 +29,7 @@ import org.iotivity.base.ModeType;
 import org.iotivity.base.OcException;
 import org.iotivity.base.OcPlatform;
 import org.iotivity.base.OcProvisioning;
+import org.iotivity.base.MVJustWorksOptionMask;
 import org.iotivity.base.OcSecureResource;
 import org.iotivity.base.OicSecAcl;
 import org.iotivity.base.OicSecAce;
@@ -38,6 +45,7 @@ import org.iotivity.base.ServiceType;
 
 import java.io.File;
 import java.io.FileNotFoundException;
+import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -45,6 +53,7 @@ import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.EnumSet;
 import java.util.List;
+import java.nio.channels.FileChannel;
 
 import org.iotivity.ca.OicCipher;
 import org.iotivity.base.OcConnectivityType;
@@ -57,6 +66,8 @@ OcSecureResource.DoOwnershipTransferListener, OcSecureResource.ProvisionPairwise
     private static final String TAG = "Provisioning Client: ";
     private static final int BUFFER_SIZE = 1024;
     private int credId=0;
+    private int verifyVal=1;
+    private String verifyNumber;
     int unownedDevCount = StringConstants.NUMBER_ZERO;
     OcProvisioning.PinCallbackListener pinCallbackListener =
         new OcProvisioning.PinCallbackListener() {
@@ -93,6 +104,72 @@ OcSecureResource.DoOwnershipTransferListener, OcSecureResource.ProvisionPairwise
                 }
         };
 
+    OcProvisioning.DisplayNumListener displayNumListener = new OcProvisioning.DisplayNumListener() {
+        @Override
+            public int displayNumListener(String verifyNum) {
+                Log.d(TAG, "Inside verifySecretListener ");
+                logMessage(TAG + "DisplayMutualVerifyNumListener verify Number = " + verifyNum);
+                verifyNumber = verifyNum;
+                return 0;
+            }
+    };
+
+    OcProvisioning.ConfirmNumListener confirmNumListener = new OcProvisioning.ConfirmNumListener() {
+        @Override
+        public int confirmNumListener() {
+            Log.d(TAG, "Inside confirmMutualVerifyNumListener ");
+            final String message = "Is number matches with " + verifyNumber;
+            final Object lock = new Object();
+            runOnUiThread(new Runnable() {
+                @Override
+                public void run() {
+
+                    AlertDialog.Builder alertDialog = new AlertDialog.Builder(ProvisioningClient.this);
+                    alertDialog.setTitle("Confirm Number");
+                    alertDialog.setMessage(message);
+                    alertDialog.setCancelable(false);
+                    alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
+                        @Override
+                        public void onClick(DialogInterface dialog, int which) {
+                            dialog.dismiss();
+                            try {
+                                synchronized (lock) {
+                                    verifyVal = 0;
+                                    lock.notifyAll();
+                                }
+                            } catch (Exception e) {
+                                Log.d(TAG, e + "");
+                            }
+                        }
+                    });
+                    alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
+                        @Override
+                        public void onClick(DialogInterface dialog, int which) {
+                            dialog.dismiss();
+                            try {
+                                synchronized (lock) {
+                                    lock.notifyAll();
+                                }
+                            } catch (Exception e) {
+                                Log.d(TAG, e + "");
+                            }
+                        }
+                    }).show();
+                }
+            });
+            synchronized (lock) {
+                try {
+                    lock.wait();
+                } catch (InterruptedException e) {
+                    Log.d(TAG, e + "");
+                }
+            }
+            Log.d(TAG, "VerifyVal after submit =" + verifyVal);
+            logMessage(TAG + " confirmMutualVerifyNumListener " + verifyVal);
+            return verifyVal;
+        }
+    };
+
     OcSecureResource.ProvisionAclListener provisionAclListener =
         new OcSecureResource.ProvisionAclListener() {
             @Override
@@ -207,18 +284,21 @@ OcSecureResource.DoOwnershipTransferListener, OcSecureResource.ProvisionPairwise
         OcPlatform.Configure(cfg);
 
         //Get deviceId
-        byte [] deviceIdBytes= OcPlatform.getDeviceId();
-        String devId = new String(deviceIdBytes);
-        Log.d(TAG, "Get Device Id "+devId);
+        /*byte [] deviceIdBytes= OcPlatform.getDeviceId();
+          String devId = new String(deviceIdBytes);
+          Log.d(TAG, "Get Device Id "+devId);
         //Set deviceId
         try {
-            String setId = "adminDeviceUuid1";
-            OcPlatform.setDeviceId(setId.getBytes());
-            Log.d(TAG, "Set Device Id done");
+        String setId = "adminDeviceUuid1";
+        OcPlatform.setDeviceId(setId.getBytes());
+        Log.d(TAG, "Set Device Id done");
+        setId = "adminDeviceUuid0";
+        OcPlatform.setDeviceId(setId.getBytes());
+        Log.d(TAG, "Set Device Id Reverted");
         }
         catch (OcException e) {
-            Log.d(TAG, e.getMessage());
-        }
+        Log.d(TAG, e.getMessage());
+        }*/
 
         try {
             /*
@@ -234,6 +314,15 @@ OcSecureResource.DoOwnershipTransferListener, OcSecureResource.ProvisionPairwise
             }
             Log.d(TAG, "Sql db directory exists at " + sqlDbPath);
             OcProvisioning.provisionInit(sqlDbPath + StringConstants.OIC_SQL_DB_FILE);
+
+            try {
+                OcProvisioning.setMVJustWorksOptions(EnumSet.of(MVJustWorksOptionMask.DISPLAY_MUTUAL_VERIF_NUM,
+                            MVJustWorksOptionMask.CONFIRM_MUTUAL_VERIF_NUM));
+                OcProvisioning.setDisplayNumListener(displayNumListener);
+                OcProvisioning.setConfirmNumListener(confirmNumListener);
+            } catch (OcException e) {
+                e.printStackTrace();
+            }
         } catch (OcException e) {
             logMessage(TAG + "provisionInit error: " + e.getMessage());
             Log.e(TAG, e.getMessage());
@@ -796,6 +885,45 @@ OcSecureResource.DoOwnershipTransferListener, OcSecureResource.ProvisionPairwise
             }
     }
 
+    @Override
+        public boolean onCreateOptionsMenu(Menu menu) {
+            // Inflate the menu; this adds items to the action bar if it is present.
+            getMenuInflater().inflate(R.menu.menu_secure_provision_client, menu);
+            return true;
+        }
+
+    @Override
+    public boolean onOptionsItemSelected(MenuItem item) {
+        int id = item.getItemId();
+
+
+        if (id == R.id.export_dat) {
+            try {
+                File sd = Environment.getExternalStorageDirectory();
+                File data = Environment.getDataDirectory();
+                if (sd.canWrite()) {
+                    String currentDBPath = "/data/data/" + getPackageName() + "/files/" + StringConstants.OIC_CLIENT_CBOR_DB_FILE;
+
+                    File currentDB = new File(currentDBPath);
+                    File backupDB = new File(sd, StringConstants.OIC_CLIENT_CBOR_DB_FILE);
+                    if (currentDB.exists()) {
+                        FileChannel src = new FileInputStream(currentDB).getChannel();
+                        FileChannel dst = new FileOutputStream(backupDB).getChannel();
+                        dst.transferFrom(src, 0, src.size());
+                        src.close();
+                        dst.close();
+                    }
+                }
+            } catch (Exception e) {
+                Toast.makeText(ProvisioningClient.this, e.getMessage(), Toast.LENGTH_SHORT).show();
+            }
+
+            Toast.makeText(ProvisioningClient.this, StringConstants.OIC_CLIENT_CBOR_DB_FILE + " File export to sdcard", Toast.LENGTH_SHORT).show();
+            return true;
+        }
+        return super.onOptionsItemSelected(item);
+    }
+
     /**
      * to display on Server Message on Client screen
      */
index dd57e4d..1a6a274 100644 (file)
@@ -1,5 +1,7 @@
 <menu xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:tools="http://schemas.android.com/tools" tools:context=".SecureProvisionClientActivity">
-    <item android:id="@+id/action_settings" android:title="@string/action_settings"
-        android:orderInCategory="100" android:showAsAction="never" />
+    xmlns:tools="http://schemas.android.com/tools" tools:context=".ProvisioningClient">
+    <item android:id="@+id/export_dat"
+        android:orderInCategory="100"
+        android:showAsAction="never"
+        android:title="Export dat"/>
 </menu>