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>
JniOcProvisioning.cpp \
JniSecureUtils.cpp \
JniProvisionResultListner.cpp \
- JniPinCheckListener.cpp
+ JniPinCheckListener.cpp \
+ JniDisplayPinListener.cpp
endif
\r
LOCAL_LDLIBS := -llog\r
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
--- /dev/null
+/*
+* //******************************************************************
+* //
+* // 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();
+}
--- /dev/null
+/*
+* //******************************************************************
+* //
+* // 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
#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");
}
}
{
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;
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());
+ }
+}
/*
* 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;
*/
}
/**
+ * 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.