C++ mediator changes to keep common code in Provision.cpp file
authorHemant Mahsky <h.mahsky@samsung.com>
Wed, 23 Sep 2015 13:45:16 +0000 (19:15 +0530)
committerMadan Lanka <lanka.madan@samsung.com>
Thu, 24 Sep 2015 08:50:32 +0000 (08:50 +0000)
- Fixed stop enrollee process issue at C++ layer
- Updated Build_Instructions_Android_Arduino.txt with .SO path <iotivity>/service/easy-setup/sdk/mediator/android/libs/armeabi
- Fixed issue for waiting logic, it has to be moved to the EnrolleeDeviceWiFiOnboarding.java from EasySetupService.java as this may not require for all EnrolleeDevice. BLE does not require it.
- Fixed merge conflicts & review comment
- Code formmating

Change-Id: I80b6f79aa1f9b46e3ed33e68d80a685453df2b9d
Signed-off-by: Hemant Mahsky <h.mahsky@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2995
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Madan Lanka <lanka.madan@samsung.com>
12 files changed:
service/easy-setup/Build_Instructions_Android_Arduino.txt
service/easy-setup/SConscript
service/easy-setup/sampleapp/mediator/linux/mediator.cpp
service/easy-setup/sdk/mediator/android/EasySetupCore/src/main/java/org/iotivity/service/easysetup/core/EasySetupService.java
service/easy-setup/sdk/mediator/android/EasySetupCore/src/main/java/org/iotivity/service/easysetup/impl/EnrolleeDeviceWiFiOnboarding.java
service/easy-setup/sdk/mediator/android/jni/jni_easy_setup.cpp
service/easy-setup/sdk/mediator/inc/prov_adapter.h
service/easy-setup/sdk/mediator/inc/provisioning.h
service/easy-setup/sdk/mediator/src/ble_provisioning.cpp
service/easy-setup/sdk/mediator/src/prov_adapter.cpp
service/easy-setup/sdk/mediator/src/provisioning.cpp [new file with mode: 0644]
service/easy-setup/sdk/mediator/src/wifi_provisioning.cpp

index d11dc07..9056422 100755 (executable)
@@ -32,9 +32,10 @@ Steps to build and deploy Easysetup Mediator in Android platform
         <iotivity-root>/service/easy-setup/sdk/mediator/android
 
     d) Execute NDK-Build with the following command to cross compiled Easysetup JNI .so files
         <iotivity-root>/service/easy-setup/sdk/mediator/android
 
     d) Execute NDK-Build with the following command to cross compiled Easysetup JNI .so files
-         ~/madan/android-ndk-r10d/ndk-build
+       <iotivity>/extlibs/android/ndk/android-ndk-r10d/ndk-build
 
     e) Step (d) will generate cross compiled Easysetup JNI .so files in the following folder
 
     e) Step (d) will generate cross compiled Easysetup JNI .so files in the following folder
+       <iotivity>/service/easy-setup/sdk/mediator/android/libs/armeabi
 
 2) Compilation using Android Studio
 
 
 2) Compilation using Android Studio
 
index b5452bf..c0bcecf 100644 (file)
@@ -111,17 +111,20 @@ if target_os == 'android':
        if 'BLE' in transport:
                es_sdk_shared = easy_setup_env.StaticLibrary('libESSDK',
                                                ['sdk/mediator/src/prov_adapter.cpp',
        if 'BLE' in transport:
                es_sdk_shared = easy_setup_env.StaticLibrary('libESSDK',
                                                ['sdk/mediator/src/prov_adapter.cpp',
-                                               'sdk/mediator/src/ble_provisioning.cpp'])
+                                               'sdk/mediator/src/ble_provisioning.cpp',
+                                               'sdk/mediator/src/provisioning.cpp'])
        else :
                es_sdk_shared = easy_setup_env.StaticLibrary('libESSDK',
                                                ['sdk/mediator/src/prov_adapter.cpp',
        else :
                es_sdk_shared = easy_setup_env.StaticLibrary('libESSDK',
                                                ['sdk/mediator/src/prov_adapter.cpp',
-                                               'sdk/mediator/src/wifi_provisioning.cpp'])
+                                               'sdk/mediator/src/wifi_provisioning.cpp',
+                                                'sdk/mediator/src/provisioning.cpp'])
        easy_setup_env.InstallTarget(es_sdk_shared, 'libESSDK')
 
 if target_os == 'linux':
        es_sdk_shared = easy_setup_env.SharedLibrary('ESSDKLibrary',
                                        ['sdk/mediator/src/prov_adapter.cpp',
        easy_setup_env.InstallTarget(es_sdk_shared, 'libESSDK')
 
 if target_os == 'linux':
        es_sdk_shared = easy_setup_env.SharedLibrary('ESSDKLibrary',
                                        ['sdk/mediator/src/prov_adapter.cpp',
-                                       'sdk/mediator/src/wifi_provisioning.cpp'])
+                                       'sdk/mediator/src/wifi_provisioning.cpp',
+                                        'sdk/mediator/src/provisioning.cpp'])
        easy_setup_env.InstallTarget(es_sdk_shared, 'libESSDK')
 
 #Go to build sample apps
        easy_setup_env.InstallTarget(es_sdk_shared, 'libESSDK')
 
 #Go to build sample apps
index ef9646e..463a435 100644 (file)
@@ -66,9 +66,10 @@ int main(int argc, char **argv) {
     int opt;
     EnrolleeNWProvInfo_t netInfo;
     PrintUsage();
     int opt;
     EnrolleeNWProvInfo_t netInfo;
     PrintUsage();
-    InitEasySetupManager();
+    InitProvProcess();
 
 
-    RegisterProvisioningStausCallback(ProvisioningStatusCallback);
+
+    RegisterCallback(ProvisioningStatusCallback);
 
     while ((opt = getopt(argc, argv, "d:s:p:")) != -1) {
         switch (opt) {
 
     while ((opt = getopt(argc, argv, "d:s:p:")) != -1) {
         switch (opt) {
@@ -93,14 +94,14 @@ int main(int argc, char **argv) {
     OIC_LOG_V(INFO, TAG, "SSID of the Enroller is =%s\n", netInfo.netAddressInfo.WIFI.ssid);
     OIC_LOG_V(INFO, TAG, "Password of the Enroller is =%s\n", netInfo.netAddressInfo.WIFI.pwd);
 
     OIC_LOG_V(INFO, TAG, "SSID of the Enroller is =%s\n", netInfo.netAddressInfo.WIFI.ssid);
     OIC_LOG_V(INFO, TAG, "Password of the Enroller is =%s\n", netInfo.netAddressInfo.WIFI.pwd);
 
-    ProvisionEnrollee(&netInfo);
+    StartProvisioning(&netInfo);
 
     signal(SIGINT, handleSigInt);
     while (!quitFlag) {
         sleep(1);
     }
 
 
     signal(SIGINT, handleSigInt);
     while (!quitFlag) {
         sleep(1);
     }
 
-    TerminateEasySetupManager();
+    ResetProvProcess();
     OIC_LOG(INFO, TAG, "Exiting occlient main loop...");
 
     return 0;
     OIC_LOG(INFO, TAG, "Exiting occlient main loop...");
 
     return 0;
index 763a63f..c9b97e6 100755 (executable)
@@ -1,22 +1,22 @@
 /**\r
  * ***************************************************************\r
 /**\r
  * ***************************************************************\r
- * <p>\r
+ * <p/>\r
  * Copyright 2015 Samsung Electronics All Rights Reserved.\r
  * Copyright 2015 Samsung Electronics All Rights Reserved.\r
- * <p>\r
- * <p>\r
- * <p>\r
+ * <p/>\r
+ * <p/>\r
+ * <p/>\r
  * Licensed under the Apache License, Version 2.0 (the "License");\r
  * you may not use this file except in compliance with the License.\r
  * You may obtain a copy of the License at\r
  * Licensed under the Apache License, Version 2.0 (the "License");\r
  * you may not use this file except in compliance with the License.\r
  * You may obtain a copy of the License at\r
- * <p>\r
+ * <p/>\r
  * http://www.apache.org/licenses/LICENSE-2.0\r
  * http://www.apache.org/licenses/LICENSE-2.0\r
- * <p>\r
+ * <p/>\r
  * Unless required by applicable law or agreed to in writing, software\r
  * distributed under the License is distributed on an "AS IS" BASIS,\r
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
  * See the License for the specific language governing permissions and\r
  * limitations under the License.\r
  * Unless required by applicable law or agreed to in writing, software\r
  * distributed under the License is distributed on an "AS IS" BASIS,\r
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
  * See the License for the specific language governing permissions and\r
  * limitations under the License.\r
- * <p>\r
+ * <p/>\r
  * ****************************************************************\r
  */\r
 \r
  * ****************************************************************\r
  */\r
 \r
@@ -117,13 +117,6 @@ public class EasySetupService {
             public void onFinished(OnBoardingConnection connection) {\r
                 if (connection.isConnected()) {\r
                     Log.i(TAG, "On boarding is successful ");\r
             public void onFinished(OnBoardingConnection connection) {\r
                 if (connection.isConnected()) {\r
                     Log.i(TAG, "On boarding is successful ");\r
-                    /*try {\r
-                        Log.i(TAG, "waiting for 15 seconds to start provisioning");\r
-                        Thread.sleep(15000);//Sleep for allowing thin device to start the services\r
-                    } catch (InterruptedException e) {\r
-                        e.printStackTrace();\r
-                    }*/\r
-\r
                     // Start provisioning here\r
                     enrolledevice.setConnection(connection);\r
                     enrolledevice.startProvisioning(mProvisioningCallback);\r
                     // Start provisioning here\r
                     enrolledevice.setConnection(connection);\r
                     enrolledevice.startProvisioning(mProvisioningCallback);\r
index 8c63758..05af933 100755 (executable)
@@ -1,22 +1,22 @@
 /**\r
  * ***************************************************************\r
 /**\r
  * ***************************************************************\r
- * <p>\r
+ * <p/>\r
  * Copyright 2015 Samsung Electronics All Rights Reserved.\r
  * Copyright 2015 Samsung Electronics All Rights Reserved.\r
- * <p>\r
- * <p>\r
- * <p>\r
+ * <p/>\r
+ * <p/>\r
+ * <p/>\r
  * Licensed under the Apache License, Version 2.0 (the "License");\r
  * you may not use this file except in compliance with the License.\r
  * You may obtain a copy of the License at\r
  * Licensed under the Apache License, Version 2.0 (the "License");\r
  * you may not use this file except in compliance with the License.\r
  * You may obtain a copy of the License at\r
- * <p>\r
+ * <p/>\r
  * http://www.apache.org/licenses/LICENSE-2.0\r
  * http://www.apache.org/licenses/LICENSE-2.0\r
- * <p>\r
+ * <p/>\r
  * Unless required by applicable law or agreed to in writing, software\r
  * distributed under the License is distributed on an "AS IS" BASIS,\r
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
  * See the License for the specific language governing permissions and\r
  * limitations under the License.\r
  * Unless required by applicable law or agreed to in writing, software\r
  * distributed under the License is distributed on an "AS IS" BASIS,\r
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
  * See the License for the specific language governing permissions and\r
  * limitations under the License.\r
- * <p>\r
+ * <p/>\r
  * ****************************************************************\r
  */\r
 \r
  * ****************************************************************\r
  */\r
 \r
@@ -79,7 +79,7 @@ public class EnrolleeDeviceWiFiOnboarding extends EnrolleeDevice {
                         conn.setHardwareAddress(enrolleStatus.getHWAddr());\r
                         conn.setDeviceName(enrolleStatus.getDevice());\r
 \r
                         conn.setHardwareAddress(enrolleStatus.getHWAddr());\r
                         conn.setDeviceName(enrolleStatus.getDevice());\r
 \r
-                        Log.d("ESSoftAPOnBoarding", "Entered"+finalResult);\r
+                        Log.d("ESSoftAPOnBoarding", "Entered" + finalResult);\r
                         mOnBoardingCallback.onFinished(conn);\r
                         return;\r
 \r
                         mOnBoardingCallback.onFinished(conn);\r
                         return;\r
 \r
@@ -89,9 +89,7 @@ public class EnrolleeDeviceWiFiOnboarding extends EnrolleeDevice {
                 IpOnBoardingConnection conn = new IpOnBoardingConnection();\r
                 conn.setConnectivity(false);\r
                 mOnBoardingCallback.onFinished(conn);\r
                 IpOnBoardingConnection conn = new IpOnBoardingConnection();\r
                 conn.setConnectivity(false);\r
                 mOnBoardingCallback.onFinished(conn);\r
-            }\r
-            else\r
-            {\r
+            } else {\r
                 Log.e("ESSoftAPOnBoarding", "Device NOT in OnBoarding State. Ignoring the event");\r
             }\r
         }\r
                 Log.e("ESSoftAPOnBoarding", "Device NOT in OnBoarding State. Ignoring the event");\r
             }\r
         }\r
@@ -139,6 +137,14 @@ public class EnrolleeDeviceWiFiOnboarding extends EnrolleeDevice {
     protected void startProvisioningProcess(OnBoardingConnection conn) {\r
         mState = EnrolleeState.DEVICE_PROVISIONING_STATE;\r
 \r
     protected void startProvisioningProcess(OnBoardingConnection conn) {\r
         mState = EnrolleeState.DEVICE_PROVISIONING_STATE;\r
 \r
+        try {\r
+            Log.i(TAG, "waiting for 15 seconds to start provisioning");\r
+            Thread.sleep(15000);//Sleep for allowing thin device to start the services\r
+        } catch (InterruptedException e) {\r
+            e.printStackTrace();\r
+        }\r
+\r
+\r
         if (mProvConfig.getConnType() == ProvisioningConfig.ConnType.WiFi) {\r
 \r
             provisionEnrolleInstance = new ProvisionEnrollee(mContext);\r
         if (mProvConfig.getConnType() == ProvisioningConfig.ConnType.WiFi) {\r
 \r
             provisionEnrolleInstance = new ProvisionEnrollee(mContext);\r
index ffc2b0d..73851fa 100644 (file)
 #include "jni_easy_setup_jvm.h"
 #include "prov_adapter.h"
 
 #include "jni_easy_setup_jvm.h"
 #include "prov_adapter.h"
 
-void JNIProvisioningStatusCallback(ProvisioningInfo * provInfo)
-{
+void JNIProvisioningStatusCallback(ProvisioningInfo * provInfo) {
     JNIEnv *env = EasySetupJVM::getEnv();
     JNIEnv *env = EasySetupJVM::getEnv();
-    if (env == NULL)
-    {
+    if (env == NULL) {
         LOGE("JNIProvisioningStatusCallback : Getting JNIEnv failed");
         return;
     }
 
     // Get EasySetupHandler class reference
     jclass easysetupCallbacks = GetJClass(EASY_SETUP_SERVICE_CALLBACK_NATIVE_API_CLASS_PATH);
         LOGE("JNIProvisioningStatusCallback : Getting JNIEnv failed");
         return;
     }
 
     // Get EasySetupHandler class reference
     jclass easysetupCallbacks = GetJClass(EASY_SETUP_SERVICE_CALLBACK_NATIVE_API_CLASS_PATH);
-    if (NULL == easysetupCallbacks)
-    {
+    if (NULL == easysetupCallbacks) {
         LOGE("JNIProvisioningStatusCallback : GetJClass easysetupCallbacks failed");
         EasySetupJVM::releaseEnv();
         return;
         LOGE("JNIProvisioningStatusCallback : GetJClass easysetupCallbacks failed");
         EasySetupJVM::releaseEnv();
         return;
@@ -43,8 +40,7 @@ void JNIProvisioningStatusCallback(ProvisioningInfo * provInfo)
 
     // Get the easysetupCallback class instance
     jobject jobjectCallback = GetJObjectInstance(EASY_SETUP_SERVICE_CALLBACK_NATIVE_API_CLASS_PATH);
 
     // Get the easysetupCallback class instance
     jobject jobjectCallback = GetJObjectInstance(EASY_SETUP_SERVICE_CALLBACK_NATIVE_API_CLASS_PATH);
-    if (NULL == jobjectCallback)
-    {
+    if (NULL == jobjectCallback) {
         LOGE("getInstance( %s) failed!", EASY_SETUP_SERVICE_CALLBACK_NATIVE_API_CLASS_PATH);
         EasySetupJVM::releaseEnv();
         return;
         LOGE("getInstance( %s) failed!", EASY_SETUP_SERVICE_CALLBACK_NATIVE_API_CLASS_PATH);
         EasySetupJVM::releaseEnv();
         return;
@@ -52,36 +48,30 @@ void JNIProvisioningStatusCallback(ProvisioningInfo * provInfo)
 
     // Get onResourceCallback method reference
     jmethodID method_id = env->GetMethodID(easysetupCallbacks, "ProvisioningStatusCallBack",
 
     // Get onResourceCallback method reference
     jmethodID method_id = env->GetMethodID(easysetupCallbacks, "ProvisioningStatusCallBack",
-            METHOD_PROVISIONING_STATUS_INTEGER_CALLBACK);
-    if (NULL == method_id)
-    {
+    METHOD_PROVISIONING_STATUS_INTEGER_CALLBACK);
+    if (NULL == method_id) {
         LOGE("JNIProvisioningStatusCallback: onResourceCallback : GetMethodID failed");
         EasySetupJVM::releaseEnv();
         return;
     }
 
         LOGE("JNIProvisioningStatusCallback: onResourceCallback : GetMethodID failed");
         EasySetupJVM::releaseEnv();
         return;
     }
 
-    if ((env)->ExceptionCheck())
-    {
+    if ((env)->ExceptionCheck()) {
         LOGE("JNIProvisioningStatusCallback : ExceptionCheck failed");
         EasySetupJVM::releaseEnv();
         return;
     }
 
         LOGE("JNIProvisioningStatusCallback : ExceptionCheck failed");
         EasySetupJVM::releaseEnv();
         return;
     }
 
-    if (NULL == method_id)
-    {
+    if (NULL == method_id) {
         LOGI("JNI method_id is NULL");
     }
         LOGI("JNI method_id is NULL");
     }
-    else
-    {
+    else {
         LOGI("JNI method_id is VALID");
 
         jint result;
         LOGI("JNI method_id is VALID");
 
         jint result;
-        if (provInfo->provStatus == DEVICE_PROVISIONED)
-        {
+        if (provInfo->provStatus == DEVICE_PROVISIONED) {
             result = 0;
         }
             result = 0;
         }
-        else
-        {
+        else {
             result = -1;
         }
 
             result = -1;
         }
 
@@ -93,121 +83,119 @@ void JNIProvisioningStatusCallback(ProvisioningInfo * provInfo)
 
 JNIEXPORT void JNICALL
 JNIInitEasySetup(JNIEnv
 
 JNIEXPORT void JNICALL
 JNIInitEasySetup(JNIEnv
-        *env,
-        jobject thisObj
+*env,
+jobject thisObj
 )
 {
 )
 {
-    LOGI("JNI JNIInitEasySetup: Enter");
+LOGI("JNI JNIInitEasySetup: Enter");
 
 
-    InitEasySetupManager();
+InitProvProcess();
 
 
-    RegisterProvisioningStausCallback(JNIProvisioningStatusCallback);
+RegisterCallback(JNIProvisioningStatusCallback);
 }
 
 JNIEXPORT void JNICALL
 JNITerminateEasySetup(JNIEnv
 }
 
 JNIEXPORT void JNICALL
 JNITerminateEasySetup(JNIEnv
-        *env,
-        jobject thisObj
+*env,
+jobject thisObj
 )
 {
 )
 {
-    LOGI("JNI JNITerminateEasySetup: Enter");
+LOGI("JNI JNITerminateEasySetup: Enter");
 
 
-    TerminateEasySetupManager();
+ResetProvProcess();
 
 }
 
 JNIEXPORT void JNICALL
 JNIProvisionEnrollee(JNIEnv
 
 }
 
 JNIEXPORT void JNICALL
 JNIProvisionEnrollee(JNIEnv
-        *env,
-        jobject thisObj,
+*env,
+jobject thisObj,
         jstring
         jstring
-        jIPAddress,
-        jstring jNetSSID,
+jIPAddress,
+jstring jNetSSID,
         jstring
         jstring
-        jNetPWD,
-        jint jConnectivityType
+jNetPWD,
+jint jConnectivityType
 )
 {
 )
 {
-    LOGI("JNI JNIProvisionEnrollee: Enter");
-
-    if (!jIPAddress)
-    {
-        LOGE("JNI JNIProvisionEnrollee : jIPAddress is NULL!");
-        return;
-    }
-
-    const char *ipAddress = env->GetStringUTFChars(jIPAddress, NULL);
-    if (NULL == ipAddress)
-    {
-        LOGE("JNI JNIProvisionEnrollee : Failed to convert jstring to char string!");
-    }
-
-    LOGI("JNI JNIProvisionEnrollee : ipAddress is : %s",ipAddress);
+LOGI("JNI JNIProvisionEnrollee: Enter");
 
 
-    const char *netSSID = env->GetStringUTFChars(jNetSSID, NULL);
-    if (NULL == netSSID)
-    {
-        LOGE("JNI JNIProvisionEnrollee : Failed to convert jstring to char string!");
-    }
-
-    LOGI("JNI JNIProvisionEnrollee : netSSID is : %s",netSSID);
-
-    const char *netPWD = env->GetStringUTFChars(jNetPWD, NULL);
-    if (NULL == netPWD)
-    {
-        LOGE("JNI JNIProvisionEnrollee : Failed to convert jstring to char string!");
-    }
+if (!jIPAddress)
+{
+LOGE("JNI JNIProvisionEnrollee : jIPAddress is NULL!");
+return;
+}
 
 
-    LOGI("JNI JNIProvisionEnrollee : netPWD is : %s",netPWD);
+const char *ipAddress = env->GetStringUTFChars(jIPAddress, NULL);
+if (NULL == ipAddress)
+{
+LOGE("JNI JNIProvisionEnrollee : Failed to convert jstring to char string!");
+}
 
 
-    OCConnectivityType connecitivityType;
-    EnrolleeNWProvInfo_t netInfo =
-    {   0};
+LOGI("JNI JNIProvisionEnrollee : ipAddress is : %s",ipAddress);
 
 
-    netInfo.
-    connType = (OCConnectivityType) jConnectivityType;
+const char *netSSID = env->GetStringUTFChars(jNetSSID, NULL);
+if (NULL == netSSID)
+{
+LOGE("JNI JNIProvisionEnrollee : Failed to convert jstring to char string!");
+}
 
 
-    if(netInfo.connType==CT_ADAPTER_GATT_BTLE)
-    {
+LOGI("JNI JNIProvisionEnrollee : netSSID is : %s",netSSID);
 
 
-        strncpy(netInfo
-                .netAddressInfo.LE.leMacAddress,ipAddress,NET_MACADDR_SIZE-1);
-        LOGI("MAC set=%s",netInfo.netAddressInfo.LE.leMacAddress);
+const char *netPWD = env->GetStringUTFChars(jNetPWD, NULL);
+if (NULL == netPWD)
+{
+LOGE("JNI JNIProvisionEnrollee : Failed to convert jstring to char string!");
+}
 
 
-    }
-    else strncpy(netInfo.netAddressInfo.WIFI.ipAddress, ipAddress, IPV4_ADDR_SIZE-1);
+LOGI("JNI JNIProvisionEnrollee : netPWD is : %s",netPWD);
+
+OCConnectivityType connecitivityType;
+EnrolleeNWProvInfo_t netInfo =
+        {0};
+strncpy(netInfo
+.netAddressInfo.WIFI.ipAddress, ipAddress, IPV4_ADDR_SIZE-1);
+strncpy(netInfo
+.netAddressInfo.WIFI.ssid, netSSID, NET_WIFI_SSID_SIZE-1);
+strncpy(netInfo
+.netAddressInfo.WIFI.pwd, netPWD, NET_WIFI_PWD_SIZE-1);
+netInfo.
+connType = (OCConnectivityType) jConnectivityType;
+
+if(netInfo.connType==CT_ADAPTER_GATT_BTLE)
+{
 
 
-    strncpy(netInfo
-            .netAddressInfo.WIFI.ssid, netSSID, NET_WIFI_SSID_SIZE-1);
-    strncpy(netInfo
-            .netAddressInfo.WIFI.pwd, netPWD, NET_WIFI_PWD_SIZE-1);
+strncpy(netInfo
+.netAddressInfo.LE.leMacAddress,ipAddress,NET_MACADDR_SIZE-1);
+LOGI("MAC set=%s",netInfo.netAddressInfo.LE.leMacAddress);
 
 
-    netInfo.
-    isSecured = true;
+}
+netInfo.
+isSecured = true;
 
 
-    ProvisionEnrollee(&netInfo);
+StartProvisioning(&netInfo);
 
 
-    return;
+return;
 }
 
 JNIEXPORT void JNICALL
 JNIStopEnrolleeProvisioning(JNIEnv
 }
 
 JNIEXPORT void JNICALL
 JNIStopEnrolleeProvisioning(JNIEnv
-        *env,
-        jobject thisObj,
+*env,
+jobject thisObj,
         jint
         jint
-        jConnectivityType)
+jConnectivityType)
 {
 {
-    LOGI("JNI Stop Easy Setup: Entering");
+LOGI("JNI Stop Easy Setup: Entering");
 
 
-    OCConnectivityType connecitivityType = OCConnectivityType::CT_DEFAULT;
+OCConnectivityType connecitivityType = OCConnectivityType::CT_DEFAULT;
 
 
-    if(jConnectivityType == 0)
-    {
-        connecitivityType = OCConnectivityType::CT_ADAPTER_IP;
-    }
+if(jConnectivityType == 0)
+{
+connecitivityType = OCConnectivityType::CT_ADAPTER_IP;
+}
 
 
-    StopEnrolleeProvisioning(connecitivityType);
+StopProvisioning(connecitivityType);
 
 
-    return;
+return;
 }
 
 }
 
index 66b44c8..3ceb901 100644 (file)
 #ifdef __cplusplus
 extern "C" {
 #endif
 #ifdef __cplusplus
 extern "C" {
 #endif
-OCStackResult InitEasySetupManager();
 
 
-OCStackResult TerminateEasySetupManager();
+OCStackResult InitProvProcess();
 
 
-OCStackResult RegisterProvisioningStausCallback(
+OCStackResult ResetProvProcess();
+
+OCStackResult RegisterCallback(
         OCProvisioningStatusCB provisioningStatusCallback);
 
         OCProvisioningStatusCB provisioningStatusCallback);
 
-void UnRegisterProvisioningStausCallback();
+void UnRegisterCallback();
 
 
-OCStackResult ProvisionEnrollee(const EnrolleeNWProvInfo_t *netInfo);
+OCStackResult StartProvisioning(const EnrolleeNWProvInfo_t *netInfo);
 
 
-OCStackResult StopEnrolleeProvisioning(OCConnectivityType connectivityType);
+OCStackResult StopProvisioning(OCConnectivityType connectivityType);
 
 #ifdef __cplusplus
 }
 
 #ifdef __cplusplus
 }
index 63c1e64..5f63242 100644 (file)
 //-----------------------------------------------------------------------------
 // Defines
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 // Defines
 //-----------------------------------------------------------------------------
-#define TAG "provisioninghandler"
-#define DEFAULT_CONTEXT_VALUE 0x99
-#ifndef MAX_LENGTH_IPv4_ADDR
-#define MAX_LENGTH_IPv4_ADDR 16
-#endif
+#define EASY_SETUP_DEFAULT_CONTEXT_VALUE 0x99
+#define TAG "EASY_SETUP_PROVISIONING"
 
 //-----------------------------------------------------------------------------
 // Typedefs
 
 //-----------------------------------------------------------------------------
 // Typedefs
@@ -54,7 +51,7 @@ OCStackApplicationResult ProvisionEnrolleeResponse(void *ctx, OCDoHandle handle,
                                                    OCClientResponse *clientResponse);
 
 OCStackResult ProvisionEnrollee(OCQualityOfService qos, const char *query, const char *resUri,
                                                    OCClientResponse *clientResponse);
 
 OCStackResult ProvisionEnrollee(OCQualityOfService qos, const char *query, const char *resUri,
-                                OCDevAddr *destination);
+                                OCDevAddr *destination, int pauseBeforeStart);
 
 OCStackApplicationResult GetProvisioningStatusResponse(void *ctx, OCDoHandle handle,
                                                        OCClientResponse *clientResponse);
 
 OCStackApplicationResult GetProvisioningStatusResponse(void *ctx, OCDoHandle handle,
                                                        OCClientResponse *clientResponse);
@@ -68,15 +65,11 @@ OCStackResult GetProvisioningStatus(OCQualityOfService qos, const char *query,
                                     const OCDevAddr *destination);
 
 OCStackResult StartProvisioningProcess(const EnrolleeNWProvInfo_t *netInfo,
                                     const OCDevAddr *destination);
 
 OCStackResult StartProvisioningProcess(const EnrolleeNWProvInfo_t *netInfo,
-                                       OCProvisioningStatusCB provisioningStatusCallback);
+                                       OCProvisioningStatusCB provisioningStatusCallback,
+                                       char *findResQuery);
 
 void StopProvisioningProcess();
 
 
 void StopProvisioningProcess();
 
-OCStackApplicationResult SubscribeProvPresenceCallback(void *ctx, OCDoHandle handle,
-                                                       OCClientResponse *clientResponse);
-
-OCStackResult SubscribeProvPresence(OCQualityOfService qos, const char *requestURI);
-
 OCStackApplicationResult FindProvisioningResourceResponse(void *ctx, OCDoHandle handle,
                                                           OCClientResponse *clientResponse);
 
 OCStackApplicationResult FindProvisioningResourceResponse(void *ctx, OCDoHandle handle,
                                                           OCClientResponse *clientResponse);
 
@@ -117,6 +110,8 @@ bool InProgress();
 bool ValidateEasySetupParams(const EnrolleeNWProvInfo_t *netInfo,
                              OCProvisioningStatusCB provisioningStatusCallback);
 
 bool ValidateEasySetupParams(const EnrolleeNWProvInfo_t *netInfo,
                              OCProvisioningStatusCB provisioningStatusCallback);
 
+bool IsSetupStopped();
+
 
 #endif
 
 
 #endif
 
index 8e75c36..e3adf8a 100644 (file)
 //
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
 //
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
+
+// NOTE : Keeping Ble provisioning in this file to have adaptability while doing OOPs refactoring 
+
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include "common.h"
 // External includes
 
 #include "common.h"
 // External includes
 
-#include "camutex.h"
-#include "cathreadpool.h"
 #include "logger.h"
 #include "oic_malloc.h"
 
 static bool sent_provision_request = false;
 
 #include "logger.h"
 #include "oic_malloc.h"
 
 static bool sent_provision_request = false;
 
-bool g_provisioningCondFlag = false;
-
 static EnrolleeNWProvInfo_t *netProvInfo;
 
 /**
 static EnrolleeNWProvInfo_t *netProvInfo;
 
 /**
@@ -47,205 +47,37 @@ static EnrolleeNWProvInfo_t *netProvInfo;
  */
 static OCProvisioningStatusCB cbData = NULL;
 
  */
 static OCProvisioningStatusCB cbData = NULL;
 
-void ErrorCallback(ProvStatus status)
-{
-    ProvisioningInfo *provInfo = GetCallbackObjectOnError(status);
-    cbData(provInfo);
-}
-
-OCStackResult InitProvisioningHandler()
-{
-    OCStackResult ret = OC_STACK_ERROR;
-    /* Initialize OCStack*/
-    if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
-    {
-        OIC_LOG(ERROR, TAG, "OCStack init error");
-        return ret;
-    }
-
-    char *string = "listeningFunc invoked in a thread";
-
-    pthread_t thread_handle;
-
-    if (pthread_create(&thread_handle, NULL, listeningFunc, (void *) string))
-    {
-        OIC_LOG(DEBUG, TAG, "Thread creation failed");
-        return OC_STACK_ERROR;
-    }
-
-    pthread_join(thread_handle, NULL);
-
-    //TODO : Below call is done in wifi case
-    // ResetProgress();
-
-
-    return OC_STACK_OK;
-}
-
-OCStackResult TerminateProvisioningHandler()
-{
-    OCStackResult ret = OC_STACK_ERROR;
-    if (OCStop() != OC_STACK_OK)
-    {
-        OIC_LOG(ERROR, TAG, "OCStack stop error");
-    }
-
-    g_provisioningCondFlag = true;
-
-    ret = OC_STACK_OK;
-    return ret;
-}
-
-void *listeningFunc(void *data)
-{
-    while (!g_provisioningCondFlag)
-    {
-        OCStackResult result;
-
-        result = OCProcess();
 
 
-        if (result != OC_STACK_OK)
-        {
-            OIC_LOG(ERROR, TAG, "OCStack stop error");
-        }
-
-        // To minimize CPU utilization we may wish to do this with sleep
-        sleep(1);
-    }
-    return NULL;
-}
-
-OCStackApplicationResult ProvisionEnrolleeResponse(void *ctx, OCDoHandle handle,
-        OCClientResponse *clientResponse)
-{
-    OIC_LOG_V(DEBUG, TAG, "INSIDE ProvisionEnrolleeResponse");
-    ProvisioningInfo *provInfo;
-
-    if (!ValidateEnrolleResponse(clientResponse))
-    {
-        ErrorCallback( DEVICE_NOT_PROVISIONED);
-        return OC_STACK_DELETE_TRANSACTION;
-    }
-
-    char *tnn;
-    char *cd;
-
-    OCRepPayload *input = (OCRepPayload *) (clientResponse->payload);
-
-    while (input)
-    {
-
-        int64_t ps;
-        if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_PS, &ps))
-        {
-
-            if (ps == 1)
-            {
-                input = input->next;
-                continue;
-            }
-            else
-            {
-                OIC_LOG_V(DEBUG, TAG, "PS is NOT proper");
-                goto Error;
-
-            }
-        }
-
-        if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_TNN, &tnn))
-        {
-            if (!strcmp(tnn, netProvInfo->netAddressInfo.WIFI.ssid))
-            {
-                OIC_LOG_V(DEBUG, TAG, "SSID is proper");
-                input = input->next;
-                continue;
-            }
-            else
-            {
-                OIC_LOG_V(DEBUG, TAG, "SSID is NOT proper");
-                goto Error;
-            }
-        }
-
-        if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CD, &cd))
-        {
-            if (!strcmp(cd, netProvInfo->netAddressInfo.WIFI.pwd))
-            {
-                OIC_LOG_V(DEBUG, TAG, "Password is proper");
-                input = input->next;
-                continue;
-            }
-            else
-            {
-                OIC_LOG_V(DEBUG, TAG, "Password is NOT proper");
-                goto Error;
-            }
-        }
-
-        LogProvisioningResponse(input->values);
-
-        input = input->next;
-
-    }
-
-    SuccessCallback(clientResponse);
-
-    return OC_STACK_KEEP_TRANSACTION;
-
-    Error:
-    {
+OCStackApplicationResult GetProvisioningStatusResponse(void *ctx, OCDoHandle handle,
+                                                       OCClientResponse *clientResponse) {
 
 
-        ErrorCallback( DEVICE_NOT_PROVISIONED);
+    OIC_LOG_V(DEBUG, TAG, "INside  GetProvisioningStatusResponse");
 
 
+    // If user stopped the process then return from this function;
+    if (IsSetupStopped()) {
+        ErrorCallback(DEVICE_NOT_PROVISIONED);
+        ClearMemory();
         return OC_STACK_DELETE_TRANSACTION;
     }
 
         return OC_STACK_DELETE_TRANSACTION;
     }
 
-}
-
-OCStackResult ProvisionEnrollee(OCQualityOfService qos, const char *query, const char *resUri,
-        OCDevAddr *destination)
-{
-    OIC_LOG_V(INFO, TAG, "Sleeping for 6 seconds");
-    sleep(6);//sleep added to avoid BLE packet drop
-    OIC_LOG_V(INFO, TAG, "\n\nExecuting ProvisionEnrollee%s", __func__);
-
-    OCRepPayload *payload = OCRepPayloadCreate();
-
-    OCRepPayloadSetUri(payload, resUri);
-    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_TNN, netProvInfo->netAddressInfo.WIFI.ssid);
-    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CD, netProvInfo->netAddressInfo.WIFI.pwd);
-
-    OIC_LOG_V(DEBUG, TAG, "OCPayload ready for ProvisionEnrollee");
-
-    OCStackResult ret = InvokeOCDoResource(query, OC_REST_PUT, destination, OC_HIGH_QOS,
-            ProvisionEnrolleeResponse, payload, NULL, 0);
-
-    return ret;
-}
-
-OCStackApplicationResult GetProvisioningStatusResponse(void *ctx, OCDoHandle handle,
-        OCClientResponse *clientResponse)
-{
     if (sent_provision_request == true)
         return OC_STACK_DELETE_TRANSACTION;
     sent_provision_request = true;
     if (sent_provision_request == true)
         return OC_STACK_DELETE_TRANSACTION;
     sent_provision_request = true;
-    OIC_LOG_V(DEBUG, TAG, "INside  GetProvisioningStatusResponse");
 
     ProvisioningInfo *provInfo;
 
 
     ProvisioningInfo *provInfo;
 
-    if (!ValidateEnrolleResponse(clientResponse))
-    {
-        ErrorCallback( DEVICE_NOT_PROVISIONED);
+    if (!ValidateEnrolleResponse(clientResponse)) {
+        ErrorCallback(DEVICE_NOT_PROVISIONED);
         ClearMemory();
         return OC_STACK_DELETE_TRANSACTION;
     }
 
         ClearMemory();
         return OC_STACK_DELETE_TRANSACTION;
     }
 
-    OCRepPayload *input = (OCRepPayload *(clientResponse->payload);
+    OCRepPayload *input = (OCRepPayload * )(clientResponse->payload);
 
     char query[OIC_STRING_MAX_VALUE] =
 
     char query[OIC_STRING_MAX_VALUE] =
-    { '\0' };
+            {'\0'};
     char resURI[MAX_URI_LENGTH] =
     char resURI[MAX_URI_LENGTH] =
-    { '\0' };
+            {'\0'};
 
     OIC_LOG_V(DEBUG, TAG, "resUri = %s", input->uri);
 
 
     OIC_LOG_V(DEBUG, TAG, "resUri = %s", input->uri);
 
@@ -260,12 +92,12 @@ OCStackApplicationResult GetProvisioningStatusResponse(void *ctx, OCDoHandle han
 
     //OCPayloadLogRep(DEBUG,TAG,input);
 
 
     //OCPayloadLogRep(DEBUG,TAG,input);
 
-    if (ProvisionEnrollee(OC_HIGH_QOS, query, OC_RSRVD_ES_URI_PROV, devaddress) != OC_STACK_OK)
-    {
+    if (ProvisionEnrollee(OC_HIGH_QOS, query, OC_RSRVD_ES_URI_PROV, devaddress,
+                          5  /* 5s delay to avoid packet drop*/) != OC_STACK_OK) {
         OIC_LOG(INFO, TAG,
                 "GetProvisioningStatusResponse received NULL clientResponse.Invoking Provisioing Status Callback");
 
         OIC_LOG(INFO, TAG,
                 "GetProvisioningStatusResponse received NULL clientResponse.Invoking Provisioing Status Callback");
 
-        ErrorCallback( DEVICE_NOT_PROVISIONED);
+        ErrorCallback(DEVICE_NOT_PROVISIONED);
         ClearMemory();
         return OC_STACK_DELETE_TRANSACTION;
     }
         ClearMemory();
         return OC_STACK_DELETE_TRANSACTION;
     }
@@ -274,101 +106,31 @@ OCStackApplicationResult GetProvisioningStatusResponse(void *ctx, OCDoHandle han
 
 }
 
 
 }
 
-OCStackResult InvokeOCDoResource(const char *query, OCMethod method, const OCDevAddr *dest,
-        OCQualityOfService qos, OCClientResponseHandler cb, OCRepPayload *payload,
-        OCHeaderOption *options, uint8_t numOptions)
-{
-    OCStackResult ret;
-    OCCallbackData cbData;
-
-    cbData.cb = cb;
-    cbData.context = (void *) DEFAULT_CONTEXT_VALUE;
-    cbData.cd = NULL;
-
-    ret = OCDoResource(NULL, method, query, dest, (OCPayload *) payload, OC_CONNTYPE_BLE, qos,
-            &cbData, options, numOptions);
-
-    if (ret != OC_STACK_OK)
-    {
-        OIC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
-    }
-
-    return ret;
-}
-
 OCStackResult GetProvisioningStatus(OCQualityOfService qos, const char *query,
 OCStackResult GetProvisioningStatus(OCQualityOfService qos, const char *query,
-        const OCDevAddr *destination)
-{
+                                    const OCDevAddr *destination) {
     OCStackResult ret = OC_STACK_ERROR;
     OIC_LOG_V(INFO, TAG, "\n\nExecuting %s %d", __func__, destination->adapter);
     ret = InvokeOCDoResource(query, OC_REST_GET, destination, OC_HIGH_QOS,
     OCStackResult ret = OC_STACK_ERROR;
     OIC_LOG_V(INFO, TAG, "\n\nExecuting %s %d", __func__, destination->adapter);
     ret = InvokeOCDoResource(query, OC_REST_GET, destination, OC_HIGH_QOS,
-            GetProvisioningStatusResponse, NULL, NULL, 0);
+                             GetProvisioningStatusResponse, NULL, NULL, 0);
     return ret;
 }
 
     return ret;
 }
 
-OCStackResult StartProvisioningProcess(const EnrolleeNWProvInfo_t *netInfo,
-        OCProvisioningStatusCB provisioningStatusCallback)
-{
-
-    OCStackResult result = OC_STACK_ERROR;
-
-    char *string = "Starting provisioning process ";
-
-    pthread_t thread_handle;
-
-    if (!ValidateEasySetupParams(netInfo, provisioningStatusCallback))
-    {
-        goto Error;
-    }
-
-    //Only basis test is done for below API
-    if (!SetProgress(provisioningStatusCallback))
-    {
-        // Device provisioning session is running already.
-        OIC_LOG(INFO, TAG, PCF("Device provisioning session is running already"));
-        goto Error;
-    }
-
-    if (!ConfigEnrolleeObject(netInfo))
-    {
-        goto Error;
-    }
-
-    if (pthread_create(&thread_handle, NULL, FindProvisioningResource, (void *) string))
-    {
-        goto Error;
-
-    }
-
-    pthread_join(thread_handle, NULL);
-
-    return OC_STACK_OK;
-
-    Error:
-    {
-        ErrorCallback( DEVICE_NOT_PROVISIONED);
-        ClearMemory();
-        return OC_STACK_ERROR;
-    }
-
-}
-
-void StopProvisioningProcess()
-{
-    //Only basis test is done for below API
-    ResetProgress();
-}
 
 // This is a function called back when a device is discovered
 OCStackApplicationResult FindProvisioningResourceResponse(void *ctx, OCDoHandle handle,
 
 // This is a function called back when a device is discovered
 OCStackApplicationResult FindProvisioningResourceResponse(void *ctx, OCDoHandle handle,
-        OCClientResponse *clientResponse)
-{
+                                                          OCClientResponse *clientResponse) {
 
     OIC_LOG(INFO, TAG, PCF("Entering FindProvisioningResourceResponse"));
 
 
     OIC_LOG(INFO, TAG, PCF("Entering FindProvisioningResourceResponse"));
 
-    if (!ValidateFinddResourceResponse(clientResponse))
-    {
-        ErrorCallback( DEVICE_NOT_PROVISIONED);
+    // If user stopped the process then return from this function;
+    if (IsSetupStopped()) {
+        ErrorCallback(DEVICE_NOT_PROVISIONED);
+        ClearMemory();
+        return OC_STACK_DELETE_TRANSACTION;
+    }
+
+    if (!ValidateFinddResourceResponse(clientResponse)) {
+        ErrorCallback(DEVICE_NOT_PROVISIONED);
         return OC_STACK_DELETE_TRANSACTION;
     }
 
         return OC_STACK_DELETE_TRANSACTION;
     }
 
@@ -376,39 +138,37 @@ OCStackApplicationResult FindProvisioningResourceResponse(void *ctx, OCDoHandle
 
     ProvisioningInfo *provInfo;
     char szQueryUri[64] =
 
     ProvisioningInfo *provInfo;
     char szQueryUri[64] =
-    { 0 };
+            {0};
 
 
-    OCDiscoveryPayload *discoveryPayload = (OCDiscoveryPayload *(clientResponse->payload);
+    OCDiscoveryPayload *discoveryPayload = (OCDiscoveryPayload * )(clientResponse->payload);
 
     // Need to conform if below check is required or not. As Null check of clientResponse->payload is already performed above
 
     // Need to conform if below check is required or not. As Null check of clientResponse->payload is already performed above
-    if (!discoveryPayload)
-    {
+    if (!discoveryPayload) {
         OIC_LOG_V(DEBUG, TAG, "Failed To parse");
         OIC_LOG_V(DEBUG, TAG, "Failed To parse");
-        ErrorCallback( DEVICE_NOT_PROVISIONED);
+        ErrorCallback(DEVICE_NOT_PROVISIONED);
         return OC_STACK_DELETE_TRANSACTION;
     }
 
     OIC_LOG_V(DEBUG, TAG, "resUri = %s", discoveryPayload->resources->uri);
 
     snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_PROVISIONING_QUERY_BLE,
         return OC_STACK_DELETE_TRANSACTION;
     }
 
     OIC_LOG_V(DEBUG, TAG, "resUri = %s", discoveryPayload->resources->uri);
 
     snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_PROVISIONING_QUERY_BLE,
-            clientResponse->addr->addr);
+             clientResponse->addr->addr);
 
     OCDevAddr *devaddress = &clientResponse->devAddr;
     devaddress->adapter = OC_ADAPTER_GATT_BTLE;
 
     if (strcmp(netProvInfo->netAddressInfo.WIFI.ipAddress, clientResponse->devAddr.addr))
         OIC_LOG_V(INFO, TAG, "unequal %s %s", netProvInfo->netAddressInfo.WIFI.ipAddress,
 
     OCDevAddr *devaddress = &clientResponse->devAddr;
     devaddress->adapter = OC_ADAPTER_GATT_BTLE;
 
     if (strcmp(netProvInfo->netAddressInfo.WIFI.ipAddress, clientResponse->devAddr.addr))
         OIC_LOG_V(INFO, TAG, "unequal %s %s", netProvInfo->netAddressInfo.WIFI.ipAddress,
-                clientResponse->devAddr.addr);
+                  clientResponse->devAddr.addr);
     else
         OIC_LOG_V(INFO, TAG, "unequal %s %s", netProvInfo->netAddressInfo.WIFI.ipAddress,
     else
         OIC_LOG_V(INFO, TAG, "unequal %s %s", netProvInfo->netAddressInfo.WIFI.ipAddress,
-                clientResponse->devAddr.addr);
+                  clientResponse->devAddr.addr);
 
     OIC_LOG_V(DEBUG, TAG, "query before GetProvisioningStatus call = %s %d", szQueryUri,
 
     OIC_LOG_V(DEBUG, TAG, "query before GetProvisioningStatus call = %s %d", szQueryUri,
-            devaddress->adapter);
+              devaddress->adapter);
 
 
-    if (GetProvisioningStatus(OC_HIGH_QOS, szQueryUri, devaddress) != OC_STACK_OK)
-    {
-        ErrorCallback( DEVICE_NOT_PROVISIONED);
+    if (GetProvisioningStatus(OC_HIGH_QOS, szQueryUri, devaddress) != OC_STACK_OK) {
+        ErrorCallback(DEVICE_NOT_PROVISIONED);
         return OC_STACK_DELETE_TRANSACTION;
     }
 
         return OC_STACK_DELETE_TRANSACTION;
     }
 
@@ -416,88 +176,15 @@ OCStackApplicationResult FindProvisioningResourceResponse(void *ctx, OCDoHandle
 
 }
 
 
 }
 
-void *FindProvisioningResource(void *data)
-{
-    OCStackResult ret = OC_STACK_ERROR;
-
-    /* Start a discovery query*/
-    char szQueryUri[64] =
-    { 0 };
-
-    snprintf(szQueryUri, sizeof(szQueryUri), MULTICAST_PROVISIONING_QUERY_BLE);
-
-    OIC_LOG_V(DEBUG, TAG, "szQueryUri = %s", szQueryUri);
-
-    OCCallbackData ocCBData;
-
-    ocCBData.cb = FindProvisioningResourceResponse;
-    ocCBData.context = (void *) DEFAULT_CONTEXT_VALUE;
-    ocCBData.cd = NULL;
-
-    ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, NULL, OC_CONNTYPE_BLE, OC_LOW_QOS,
-            &ocCBData, NULL, 0);
-
-    if (ret != OC_STACK_OK)
-    {
-        ErrorCallback( DEVICE_NOT_PROVISIONED);
-        ClearMemory();
-    }
-    return NULL;
-}
-
-OCStackResult FindNetworkResource()
-{
-    OCStackResult ret = OC_STACK_ERROR;
-    if (OCStop() != OC_STACK_OK)
-    {
-        OIC_LOG(ERROR, TAG, "OCStack stop error");
-    }
-
-    return ret;
-}
-
-ProvisioningInfo *PrepareProvisioingStatusCB(OCClientResponse *clientResponse,
-        ProvStatus provStatus)
-{
-
-    ProvisioningInfo *provInfo = (ProvisioningInfo *) OICCalloc(1, sizeof(ProvisioningInfo));
-
-    if (provInfo == NULL)
-    {
-        OIC_LOG_V(ERROR, TAG, "Failed to allocate memory");
-        return NULL;
-    }
-
-    OCDevAddr *devAddr = (OCDevAddr *) OICCalloc(1, sizeof(OCDevAddr));
-
-    if (devAddr == NULL)
-    {
-        OIC_LOG_V(ERROR, TAG, "Failed to allocate memory");
-        return NULL;
-    }
-
-    strncpy(devAddr->addr, clientResponse->addr->addr, sizeof(devAddr->addr));
-    devAddr->port = clientResponse->addr->port;
-
-    provInfo->provDeviceInfo.addr = devAddr;
-
-    provInfo->provStatus = provStatus;
-
-    return provInfo;
-}
-
 bool ValidateEasySetupParams(const EnrolleeNWProvInfo_t *netInfo,
 bool ValidateEasySetupParams(const EnrolleeNWProvInfo_t *netInfo,
-        OCProvisioningStatusCB provisioningStatusCallback)
-{
+                             OCProvisioningStatusCB provisioningStatusCallback) {
 
 
-    if (netInfo == NULL || strlen(netInfo->netAddressInfo.LE.leMacAddress) == 0)
-    {
+    if (netInfo == NULL || strlen(netInfo->netAddressInfo.LE.leMacAddress) == 0) {
         OIC_LOG(ERROR, TAG, "Request URI is NULL");
         return false;
     }
 
         OIC_LOG(ERROR, TAG, "Request URI is NULL");
         return false;
     }
 
-    if (provisioningStatusCallback == NULL)
-    {
+    if (provisioningStatusCallback == NULL) {
         OIC_LOG(ERROR, TAG, "ProvisioningStatusCallback is NULL");
         return false;
     }
         OIC_LOG(ERROR, TAG, "ProvisioningStatusCallback is NULL");
         return false;
     }
@@ -506,224 +193,3 @@ bool ValidateEasySetupParams(const EnrolleeNWProvInfo_t *netInfo,
 
 }
 
 
 }
 
-bool InProgress()
-{
-
-    // It means already Easy Setup provisioning session is going on.
-    if (NULL != cbData)
-    {
-        OIC_LOG(ERROR, TAG, "Easy setup session is already in progress");
-        return true;
-    }
-
-    return false;
-}
-
-bool SetProgress(OCProvisioningStatusCB provisioningStatusCallback)
-{
-
-    if (InProgress())
-        return false;
-
-    cbData = provisioningStatusCallback;
-
-    return true;
-}
-
-bool ResetProgress()
-{
-
-    cbData = NULL;
-
-    return true;
-}
-
-ProvisioningInfo *CreateCallBackObject()
-{
-
-    ProvisioningInfo *provInfo = (ProvisioningInfo *) OICCalloc(1, sizeof(ProvisioningInfo));
-
-    if (provInfo == NULL)
-    {
-        OIC_LOG_V(ERROR, TAG, "Failed to allocate memory");
-        return NULL;
-    }
-
-    OCDevAddr *devAddr = (OCDevAddr *) OICCalloc(1, sizeof(OCDevAddr));
-
-    if (devAddr == NULL)
-    {
-        OIC_LOG_V(ERROR, TAG, "Failed to allocate memory");
-        return NULL;
-    }
-
-    provInfo->provDeviceInfo.addr = devAddr;
-
-    return provInfo;
-
-}
-
-ProvisioningInfo *GetCallbackObjectOnError(ProvStatus status)
-{
-
-    ProvisioningInfo *provInfo = CreateCallBackObject();
-    strncpy(provInfo->provDeviceInfo.addr->addr, netProvInfo->netAddressInfo.WIFI.ipAddress,
-            sizeof(provInfo->provDeviceInfo.addr->addr));
-    provInfo->provDeviceInfo.addr->port = IP_PORT;
-    provInfo->provStatus = status;
-    return provInfo;
-}
-
-ProvisioningInfo *GetCallbackObjectOnSuccess(OCClientResponse *clientResponse,
-        ProvStatus provStatus)
-{
-    ProvisioningInfo *provInfo = CreateCallBackObject();
-    strncpy(provInfo->provDeviceInfo.addr->addr, clientResponse->addr->addr,
-            sizeof(provInfo->provDeviceInfo.addr->addr));
-    provInfo->provDeviceInfo.addr->port = clientResponse->addr->port;
-    provInfo->provStatus = provStatus;
-    return provInfo;
-}
-
-bool ValidateFinddResourceResponse(OCClientResponse * clientResponse)
-{
-
-    if (!(clientResponse) || !(clientResponse->payload))
-    {
-
-        OIC_LOG_V(INFO, TAG, "ProvisionEnrolleeResponse received Null clientResponse");
-
-        return false;
-
-    }
-    return true;
-}
-
-bool ValidateEnrolleResponse(OCClientResponse * clientResponse)
-{
-
-    if (!(clientResponse) || !(clientResponse->payload))
-    {
-
-        OIC_LOG_V(INFO, TAG, "ProvisionEnrolleeResponse received Null clientResponse");
-
-        return false;
-
-    }
-
-    if (clientResponse->payload->type != PAYLOAD_TYPE_REPRESENTATION)
-    {
-
-        OIC_LOG_V(DEBUG, TAG, "Incoming payload not a representation");
-        return false;
-
-    }
-
-    // If flow reachese here means no error condition hit.
-    return true;
-
-}
-
-void SuccessCallback(OCClientResponse * clientResponse)
-{
-    ProvisioningInfo *provInfo = GetCallbackObjectOnSuccess(clientResponse, DEVICE_PROVISIONED);
-    cbData(provInfo);
-}
-
-bool ClearMemory()
-{
-
-    OIC_LOG(DEBUG, TAG, "thread_pool_add_task of FindProvisioningResource failed");
-
-    return true;
-
-}
-
-bool ConfigEnrolleeObject(const EnrolleeNWProvInfo_t *netInfo)
-{
-
-    //Copy Network Provisioning  Information
-    netProvInfo = (EnrolleeNWProvInfo_t *) OICCalloc(1, sizeof(EnrolleeNWProvInfo_t));
-
-    if (netProvInfo == NULL)
-    {
-        OIC_LOG(ERROR, TAG, "Invalid input..");
-        return false;
-    }
-
-    memcpy(netProvInfo, netInfo, sizeof(EnrolleeNWProvInfo_t));
-
-    OIC_LOG_V(DEBUG, TAG, "Network Provisioning Info. SSID = %s",
-            netProvInfo->netAddressInfo.WIFI.ssid);
-
-    OIC_LOG_V(DEBUG, TAG, "Network Provisioning Info. PWD = %s",
-            netProvInfo->netAddressInfo.WIFI.pwd);
-
-    OIC_LOG_V(DEBUG, TAG, "Network Provisioning Info. MAC ADDRESS = %s",
-            netInfo->netAddressInfo.LE.leMacAddress);
-
-    return true;
-
-}
-
-void LogProvisioningResponse(OCRepPayloadValue * val)
-{
-
-    switch (val->type)
-    {
-        case OCREP_PROP_NULL:
-            OIC_LOG_V(DEBUG, TAG, "\t\t%s: NULL", val->name);
-            break;
-        case OCREP_PROP_INT:
-            OIC_LOG_V(DEBUG, TAG, "\t\t%s(int):%lld", val->name, val->i);
-            break;
-        case OCREP_PROP_DOUBLE:
-            OIC_LOG_V(DEBUG, TAG, "\t\t%s(double):%f", val->name, val->d);
-            break;
-        case OCREP_PROP_BOOL:
-            OIC_LOG_V(DEBUG, TAG, "\t\t%s(bool):%s", val->name, val->b ? "true" : "false");
-            break;
-        case OCREP_PROP_STRING:
-            OIC_LOG_V(DEBUG, TAG, "\t\t%s(string):%s", val->name, val->str);
-            break;
-        case OCREP_PROP_OBJECT:
-            // Note: Only prints the URI (if available), to print further, you'll
-            // need to dig into the object better!
-            OIC_LOG_V(DEBUG, TAG, "\t\t%s(OCRep):%s", val->name, val->obj->uri);
-            break;
-        case OCREP_PROP_ARRAY:
-            switch (val->arr.type)
-            {
-                case OCREP_PROP_INT:
-                    OIC_LOG_V(DEBUG, TAG, "\t\t%s(int array):%lld x %lld x %lld", val->name,
-                            val->arr.dimensions[0], val->arr.dimensions[1], val->arr.dimensions[2]);
-                    break;
-                case OCREP_PROP_DOUBLE:
-                    OIC_LOG_V(DEBUG, TAG, "\t\t%s(double array):%lld x %lld x %lld", val->name,
-                            val->arr.dimensions[0], val->arr.dimensions[1], val->arr.dimensions[2]);
-                    break;
-                case OCREP_PROP_BOOL:
-                    OIC_LOG_V(DEBUG, TAG, "\t\t%s(bool array):%lld x %lld x %lld", val->name,
-                            val->arr.dimensions[0], val->arr.dimensions[1], val->arr.dimensions[2]);
-                    break;
-                case OCREP_PROP_STRING:
-                    OIC_LOG_V(DEBUG, TAG, "\t\t%s(string array):%lld x %lld x %lld", val->name,
-                            val->arr.dimensions[0], val->arr.dimensions[1], val->arr.dimensions[2]);
-                    break;
-                case OCREP_PROP_OBJECT:
-                    OIC_LOG_V(DEBUG, TAG, "\t\t%s(OCRep array):%lld x %lld x %lld", val->name,
-                            val->arr.dimensions[0], val->arr.dimensions[1], val->arr.dimensions[2]);
-                    break;
-                default:
-                    //OIC_LOG_V(ERROR, TAG, "\t\t%s <-- Unknown/unsupported array type!",
-                    //  val->name);
-                    break;
-            }
-            break;
-        default:
-            /*OC_LOG_V(ERROR, TAG
-             , "\t\t%s <-- Unknown type!", val->name);*/
-            break;
-    }
-}
-
index 5f2adfd..bc71e4c 100755 (executable)
@@ -30,9 +30,9 @@
 //Use ipv4addr for both InitDiscovery and InitDeviceDiscovery
 char ipv4addr[IPV4_ADDR_SIZE] = {0};
 
 //Use ipv4addr for both InitDiscovery and InitDeviceDiscovery
 char ipv4addr[IPV4_ADDR_SIZE] = {0};
 
-static OCProvisioningStatusCB cbData = NULL;
+volatile static OCProvisioningStatusCB cbData = NULL;
 
 
-OCStackResult InitEasySetupManager() {
+OCStackResult InitProvProcess() {
 
 
     OCStackResult result = OC_STACK_ERROR;
 
 
     OCStackResult result = OC_STACK_ERROR;
@@ -49,11 +49,11 @@ OCStackResult InitEasySetupManager() {
     return result;
 }
 
     return result;
 }
 
-OCStackResult TerminateEasySetupManager() {
+OCStackResult ResetProvProcess() {
     return TerminateProvisioningHandler();
 }
 
     return TerminateProvisioningHandler();
 }
 
-OCStackResult RegisterProvisioningStausCallback(
+OCStackResult RegisterCallback(
         OCProvisioningStatusCB provisioningStatusCallback) {
     OCStackResult result = OC_STACK_OK;
 
         OCProvisioningStatusCB provisioningStatusCallback) {
     OCStackResult result = OC_STACK_OK;
 
@@ -68,20 +68,31 @@ OCStackResult RegisterProvisioningStausCallback(
     return result;
 }
 
     return result;
 }
 
-void UnRegisterProvisioningStausCallback() {
+void UnRegisterCallback() {
     if (cbData) {
         cbData = NULL;
     }
 }
 
     if (cbData) {
         cbData = NULL;
     }
 }
 
-OCStackResult ProvisionEnrollee(const EnrolleeNWProvInfo_t *netInfo) {
-    return StartProvisioningProcess(netInfo, cbData);
+OCStackResult StartProvisioning(const EnrolleeNWProvInfo_t *netInfo) {
+
+    char findQuery[64] = {0};
+
+    if (netInfo->connType == CT_IP_USE_V4) {
+        snprintf(findQuery, sizeof(findQuery), UNICAST_PROVISIONING_QUERY,
+                 netInfo->netAddressInfo.WIFI.ipAddress, IP_PORT);
+    } else {
+
+        snprintf(findQuery, sizeof(findQuery),
+                 MULTICAST_PROVISIONING_QUERY_BLE);
+    }
+
+    return StartProvisioningProcess(netInfo, cbData, findQuery);
 }
 
 }
 
-OCStackResult StopEnrolleeProvisioning(OCConnectivityType connectivityType) {
+OCStackResult StopProvisioning(OCConnectivityType connectivityType) {
     OCStackResult result = OC_STACK_OK;
 
     OCStackResult result = OC_STACK_OK;
 
-    //TODO: Have to handle the transport specific easy setup termination
     StopProvisioningProcess();
 
     return result;
     StopProvisioningProcess();
 
     return result;
diff --git a/service/easy-setup/sdk/mediator/src/provisioning.cpp b/service/easy-setup/sdk/mediator/src/provisioning.cpp
new file mode 100644 (file)
index 0000000..cf0d8a2
--- /dev/null
@@ -0,0 +1,562 @@
+//******************************************************************
+//
+// 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 "provisioning.h"
+
+//Standard includes
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <signal.h>
+#include <unistd.h>
+#include <pthread.h>
+
+//EasySetup include files
+#include "ocpayload.h"
+#include "common.h"
+
+
+// External includes
+#include "logger.h"
+#include "oic_malloc.h"
+
+bool g_provisioningCondFlag = false;
+
+static EnrolleeNWProvInfo_t *netProvInfo;
+
+char szFindResourceQueryUri[64] = {0};
+
+/**
+ * @var cbData
+ * @brief Callback for providing provisioning status callback to application
+ */
+static OCProvisioningStatusCB cbData = NULL;
+
+
+void ErrorCallback(ProvStatus status) {
+    ProvisioningInfo *provInfo = GetCallbackObjectOnError(status);
+    cbData(provInfo);
+    ResetProgress();
+}
+
+
+OCStackResult InitProvisioningHandler() {
+    OCStackResult ret = OC_STACK_ERROR;
+    /* Initialize OCStack*/
+    if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK) {
+        OIC_LOG(ERROR, TAG, "OCStack init error");
+        return ret;
+    }
+
+
+    char *string = "listeningFunc invoked in a thread";
+
+    pthread_t thread_handle;
+
+    if (pthread_create(&thread_handle, NULL, listeningFunc, (void *) string)) {
+        OIC_LOG(DEBUG, TAG, "Thread creation failed");
+        return OC_STACK_ERROR;
+    }
+
+    pthread_join(thread_handle, NULL);
+
+    ResetProgress();
+
+    return OC_STACK_OK;
+}
+
+OCStackResult TerminateProvisioningHandler() {
+    OCStackResult ret = OC_STACK_ERROR;
+    if (OCStop() != OC_STACK_OK) {
+        OIC_LOG(ERROR, TAG, "OCStack stop error");
+    }
+
+    g_provisioningCondFlag = true;
+
+    ret = OC_STACK_OK;
+    return ret;
+}
+
+void *listeningFunc(void *data) {
+    while (!g_provisioningCondFlag) {
+        OCStackResult result;
+
+        result = OCProcess();
+
+        if (result != OC_STACK_OK) {
+            OIC_LOG(ERROR, TAG, "OCStack stop error");
+        }
+
+        // To minimize CPU utilization we may wish to do this with sleep
+        sleep(1);
+    }
+    return NULL;
+}
+
+
+OCStackApplicationResult ProvisionEnrolleeResponse(void *ctx, OCDoHandle handle,
+                                                   OCClientResponse *clientResponse) {
+    OIC_LOG_V(DEBUG, TAG, "INSIDE ProvisionEnrolleeResponse");
+
+    // If user stopped the process then return from this function;
+    if (IsSetupStopped()) {
+        ErrorCallback(DEVICE_NOT_PROVISIONED);
+        ClearMemory();
+        return OC_STACK_DELETE_TRANSACTION;
+    }
+
+    ProvisioningInfo *provInfo;
+
+    if (!ValidateEnrolleResponse(clientResponse)) {
+        ErrorCallback(DEVICE_NOT_PROVISIONED);
+        return OC_STACK_DELETE_TRANSACTION;
+    }
+
+    char *tnn;
+    char *cd;
+
+    OCRepPayload *input = (OCRepPayload * )(clientResponse->payload);
+
+    while (input) {
+
+        int64_t ps;
+        if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_PS, &ps)) {
+
+            if (ps == 1) {
+                input = input->next;
+                continue;
+            }
+            else {
+                OIC_LOG_V(DEBUG, TAG, "PS is NOT proper");
+                goto Error;
+
+            }
+        }
+
+        if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_TNN, &tnn)) {
+            if (!strcmp(tnn, netProvInfo->netAddressInfo.WIFI.ssid)) {
+                OIC_LOG_V(DEBUG, TAG, "SSID is proper");
+                input = input->next;
+                continue;
+            }
+            else {
+                OIC_LOG_V(DEBUG, TAG, "SSID is NOT proper");
+                goto Error;
+            }
+        }
+
+        if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CD, &cd)) {
+            if (!strcmp(cd, netProvInfo->netAddressInfo.WIFI.pwd)) {
+                OIC_LOG_V(DEBUG, TAG, "Password is proper");
+                input = input->next;
+                continue;
+            }
+            else {
+                OIC_LOG_V(DEBUG, TAG, "Password is NOT proper");
+                goto Error;
+            }
+        }
+
+        LogProvisioningResponse(input->values);
+
+        input = input->next;
+
+    }
+
+    SuccessCallback(clientResponse);
+
+    return OC_STACK_KEEP_TRANSACTION;
+
+    Error:
+    {
+
+        ErrorCallback(DEVICE_NOT_PROVISIONED);
+
+        return OC_STACK_DELETE_TRANSACTION;
+    }
+
+}
+
+OCStackResult StartProvisioningProcess(const EnrolleeNWProvInfo_t *netInfo,
+                                       OCProvisioningStatusCB provisioningStatusCallback,
+                                       char *findResQuery) {
+
+    OCStackResult result = OC_STACK_ERROR;
+
+    strncpy(szFindResourceQueryUri, findResQuery, sizeof(szFindResourceQueryUri, findResQuery) - 1);
+
+    char *string = "Starting provisioning process ";
+
+    pthread_t thread_handle;
+
+    if (!ValidateEasySetupParams(netInfo, provisioningStatusCallback)) {
+        goto Error;
+    }
+
+    if (!SetProgress(provisioningStatusCallback)) {
+        // Device provisioning session is running already.
+        OIC_LOG(INFO, TAG, PCF("Device provisioning session is running already"));
+        goto Error;
+    }
+
+    if (!ConfigEnrolleeObject(netInfo)) {
+        goto Error;
+    }
+
+    if (pthread_create(&thread_handle, NULL, FindProvisioningResource, (void *) string)) {
+        goto Error;
+
+    }
+
+    pthread_join(thread_handle, NULL);
+
+
+    return OC_STACK_OK;
+
+    Error:
+    {
+        ErrorCallback(DEVICE_NOT_PROVISIONED);
+        ClearMemory();
+        return OC_STACK_ERROR;
+    }
+
+}
+
+void StopProvisioningProcess() {
+    //Only basis test is done for below API
+    ResetProgress();
+}
+
+bool ClearMemory() {
+
+    OIC_LOG(DEBUG, TAG, "thread_pool_add_task of FindProvisioningResource failed");
+    return true;
+
+}
+
+bool ConfigEnrolleeObject(const EnrolleeNWProvInfo_t *netInfo) {
+
+    //Copy Network Provisioning  Information
+    netProvInfo = (EnrolleeNWProvInfo_t *) OICCalloc(1, sizeof(EnrolleeNWProvInfo_t));
+
+    if (netProvInfo == NULL) {
+        OIC_LOG(ERROR, TAG, "Invalid input..");
+        return false;
+    }
+
+    memcpy(netProvInfo, netInfo, sizeof(EnrolleeNWProvInfo_t));
+
+    OIC_LOG_V(DEBUG, TAG, "Network Provisioning Info. SSID = %s",
+              netProvInfo->netAddressInfo.WIFI.ssid);
+
+    OIC_LOG_V(DEBUG, TAG, "Network Provisioning Info. PWD = %s",
+              netProvInfo->netAddressInfo.WIFI.pwd);
+
+    return true;
+
+}
+
+void LogProvisioningResponse(OCRepPayloadValue * val) {
+
+    switch (val->type) {
+        case OCREP_PROP_NULL:
+            OIC_LOG_V(DEBUG, TAG, "\t\t%s: NULL", val->name);
+            break;
+        case OCREP_PROP_INT:
+            OIC_LOG_V(DEBUG, TAG, "\t\t%s(int):%lld", val->name, val->i);
+            break;
+        case OCREP_PROP_DOUBLE:
+            OIC_LOG_V(DEBUG, TAG, "\t\t%s(double):%f", val->name, val->d);
+            break;
+        case OCREP_PROP_BOOL:
+            OIC_LOG_V(DEBUG, TAG, "\t\t%s(bool):%s", val->name, val->b ? "true" : "false");
+            break;
+        case OCREP_PROP_STRING:
+            OIC_LOG_V(DEBUG, TAG, "\t\t%s(string):%s", val->name, val->str);
+            break;
+        case OCREP_PROP_OBJECT:
+            // Note: Only prints the URI (if available), to print further, you'll
+            // need to dig into the object better!
+            OIC_LOG_V(DEBUG, TAG, "\t\t%s(OCRep):%s", val->name, val->obj->uri);
+            break;
+        case OCREP_PROP_ARRAY:
+            switch (val->arr.type) {
+                case OCREP_PROP_INT:
+                    OIC_LOG_V(DEBUG, TAG, "\t\t%s(int array):%lld x %lld x %lld", val->name,
+                              val->arr.dimensions[0], val->arr.dimensions[1],
+                              val->arr.dimensions[2]);
+                    break;
+                case OCREP_PROP_DOUBLE:
+                    OIC_LOG_V(DEBUG, TAG, "\t\t%s(double array):%lld x %lld x %lld", val->name,
+                              val->arr.dimensions[0], val->arr.dimensions[1],
+                              val->arr.dimensions[2]);
+                    break;
+                case OCREP_PROP_BOOL:
+                    OIC_LOG_V(DEBUG, TAG, "\t\t%s(bool array):%lld x %lld x %lld", val->name,
+                              val->arr.dimensions[0], val->arr.dimensions[1],
+                              val->arr.dimensions[2]);
+                    break;
+                case OCREP_PROP_STRING:
+                    OIC_LOG_V(DEBUG, TAG, "\t\t%s(string array):%lld x %lld x %lld", val->name,
+                              val->arr.dimensions[0], val->arr.dimensions[1],
+                              val->arr.dimensions[2]);
+                    break;
+                case OCREP_PROP_OBJECT:
+                    OIC_LOG_V(DEBUG, TAG, "\t\t%s(OCRep array):%lld x %lld x %lld", val->name,
+                              val->arr.dimensions[0], val->arr.dimensions[1],
+                              val->arr.dimensions[2]);
+                    break;
+                default:
+                    break;
+            }
+            break;
+        default:
+            break;
+    }
+}
+
+OCStackResult FindNetworkResource() {
+    OCStackResult ret = OC_STACK_ERROR;
+    if (OCStop() != OC_STACK_OK) {
+        OIC_LOG(ERROR, TAG, "OCStack stop error");
+    }
+
+    return ret;
+}
+
+ProvisioningInfo *PrepareProvisioingStatusCB(OCClientResponse *clientResponse,
+                                             ProvStatus provStatus) {
+
+    ProvisioningInfo *provInfo = (ProvisioningInfo *) OICCalloc(1, sizeof(ProvisioningInfo));
+
+    if (provInfo == NULL) {
+        OIC_LOG_V(ERROR, TAG, "Failed to allocate memory");
+        return NULL;
+    }
+
+    OCDevAddr *devAddr = (OCDevAddr *) OICCalloc(1, sizeof(OCDevAddr));
+
+    if (devAddr == NULL) {
+        OIC_LOG_V(ERROR, TAG, "Failed to allocate memory");
+        return NULL;
+    }
+
+    strncpy(devAddr->addr, clientResponse->addr->addr, sizeof(devAddr->addr));
+    devAddr->port = clientResponse->addr->port;
+
+    provInfo->provDeviceInfo.addr = devAddr;
+
+    provInfo->provStatus = provStatus;
+
+    return provInfo;
+}
+
+
+bool InProgress() {
+
+    // It means already Easy Setup provisioning session is going on.
+    if (NULL != cbData) {
+        OIC_LOG(ERROR, TAG, "Easy setup session is already in progress");
+        return true;
+    }
+
+    return false;
+}
+
+bool SetProgress(OCProvisioningStatusCB provisioningStatusCallback) {
+
+    if (InProgress())
+        return false;
+
+    cbData = provisioningStatusCallback;
+
+
+    return true;
+}
+
+bool ResetProgress() {
+
+    cbData = NULL;
+    return true;
+}
+
+ProvisioningInfo *CreateCallBackObject() {
+
+    ProvisioningInfo *provInfo = (ProvisioningInfo *) OICCalloc(1, sizeof(ProvisioningInfo));
+
+    if (provInfo == NULL) {
+        OIC_LOG_V(ERROR, TAG, "Failed to allocate memory");
+        return NULL;
+    }
+
+    OCDevAddr *devAddr = (OCDevAddr *) OICCalloc(1, sizeof(OCDevAddr));
+
+    if (devAddr == NULL) {
+        OIC_LOG_V(ERROR, TAG, "Failed to allocate memory");
+        return NULL;
+    }
+
+    provInfo->provDeviceInfo.addr = devAddr;
+
+    return provInfo;
+
+}
+
+ProvisioningInfo *GetCallbackObjectOnError(ProvStatus status) {
+
+    ProvisioningInfo *provInfo = CreateCallBackObject();
+    strncpy(provInfo->provDeviceInfo.addr->addr, netProvInfo->netAddressInfo.WIFI.ipAddress,
+            sizeof(provInfo->provDeviceInfo.addr->addr));
+    provInfo->provDeviceInfo.addr->port = IP_PORT;
+    provInfo->provStatus = status;
+    return provInfo;
+}
+
+ProvisioningInfo *GetCallbackObjectOnSuccess(OCClientResponse *clientResponse,
+                                             ProvStatus provStatus) {
+    ProvisioningInfo *provInfo = CreateCallBackObject();
+    strncpy(provInfo->provDeviceInfo.addr->addr, clientResponse->addr->addr,
+            sizeof(provInfo->provDeviceInfo.addr->addr));
+    provInfo->provDeviceInfo.addr->port = clientResponse->addr->port;
+    provInfo->provStatus = provStatus;
+    return provInfo;
+}
+
+bool ValidateFinddResourceResponse(OCClientResponse * clientResponse) {
+
+    if (!(clientResponse) || !(clientResponse->payload)) {
+
+        OIC_LOG_V(INFO, TAG, "ProvisionEnrolleeResponse received Null clientResponse");
+
+        return false;
+
+    }
+    return true;
+}
+
+bool ValidateEnrolleResponse(OCClientResponse * clientResponse) {
+
+    if (!(clientResponse) || !(clientResponse->payload)) {
+
+        OIC_LOG_V(INFO, TAG, "ProvisionEnrolleeResponse received Null clientResponse");
+
+        return false;
+
+    }
+
+    if (clientResponse->payload->type != PAYLOAD_TYPE_REPRESENTATION) {
+
+        OIC_LOG_V(DEBUG, TAG, "Incoming payload not a representation");
+        return false;
+
+    }
+
+    // If flow reachese here means no error condition hit.
+    return true;
+
+}
+
+void SuccessCallback(OCClientResponse * clientResponse) {
+    ProvisioningInfo *provInfo = GetCallbackObjectOnSuccess(clientResponse, DEVICE_PROVISIONED);
+    cbData(provInfo);
+    ResetProgress();
+}
+
+void *FindProvisioningResource(void *data) {
+
+    // If user stopped the process before thread get scheduled then check and return from this function;
+    if (IsSetupStopped()) {
+        ErrorCallback(DEVICE_NOT_PROVISIONED);
+        ClearMemory();
+        return NULL;
+    }
+
+    OCStackResult ret = OC_STACK_ERROR;
+
+    OIC_LOG_V(DEBUG, TAG, "szFindResourceQueryUri = %s", szFindResourceQueryUri);
+
+    OCCallbackData ocCBData;
+
+    ocCBData.cb = FindProvisioningResourceResponse;
+    ocCBData.context = (void *) EASY_SETUP_DEFAULT_CONTEXT_VALUE;
+    ocCBData.cd = NULL;
+
+
+    ret = OCDoResource(NULL, OC_REST_DISCOVER, szFindResourceQueryUri, NULL, NULL,
+                       netProvInfo->connType, OC_LOW_QOS,
+                       &ocCBData, NULL, 0);
+
+    if (ret != OC_STACK_OK) {
+        ErrorCallback(DEVICE_NOT_PROVISIONED);
+        ClearMemory();
+    }
+
+    return NULL;
+}
+
+OCStackResult InvokeOCDoResource(const char *query, OCMethod method, const OCDevAddr *dest,
+                                 OCQualityOfService qos, OCClientResponseHandler cb,
+                                 OCRepPayload *payload,
+                                 OCHeaderOption *options, uint8_t numOptions) {
+    OCStackResult ret;
+    OCCallbackData cbData;
+
+    cbData.cb = cb;
+    cbData.context = (void *) EASY_SETUP_DEFAULT_CONTEXT_VALUE;
+    cbData.cd = NULL;
+
+    ret = OCDoResource(NULL, method, query, dest, (OCPayload *) payload, netProvInfo->connType, qos,
+                       &cbData, options, numOptions);
+
+    if (ret != OC_STACK_OK) {
+        OIC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
+    }
+
+    return ret;
+}
+
+OCStackResult ProvisionEnrollee(OCQualityOfService qos, const char *query, const char *resUri,
+                                OCDevAddr *destination, int pauseBeforeStart) {
+
+
+    // This sleep is required in case of BLE provisioning due to packet drop issue.
+    OIC_LOG_V(INFO, TAG, "Sleeping for %d seconds", pauseBeforeStart);
+    sleep(pauseBeforeStart);
+    OIC_LOG_V(INFO, TAG, "\n\nExecuting ProvisionEnrollee%s", __func__);
+
+    OCRepPayload *payload = OCRepPayloadCreate();
+
+    OCRepPayloadSetUri(payload, resUri);
+    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_TNN, netProvInfo->netAddressInfo.WIFI.ssid);
+    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CD, netProvInfo->netAddressInfo.WIFI.pwd);
+
+    OIC_LOG_V(DEBUG, TAG, "OCPayload ready for ProvisionEnrollee");
+
+    OCStackResult ret = InvokeOCDoResource(query, OC_REST_PUT, destination, OC_HIGH_QOS,
+                                           ProvisionEnrolleeResponse, payload, NULL, 0);
+
+    return ret;
+}
+
+bool IsSetupStopped() {
+    return (cbData == NULL) ? true : false;
+}
+
+
index 17527d3..b7afd28 100755 (executable)
@@ -18,6 +18,8 @@
 //
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
 //
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
+// NOTE : Keeping Wifi provisioning in this file to have adaptability while doing OOPs refactoring 
+
 #include "provisioning.h"
 
 //Standard includes
 #include "provisioning.h"
 
 //Standard includes
 #include "common.h"
 
 // External includes
 #include "common.h"
 
 // External includes
-#include "camutex.h"
-#include "cathreadpool.h"
 #include "logger.h"
 #include "oic_malloc.h"
 
 #include "logger.h"
 #include "oic_malloc.h"
 
-bool g_provisioningCondFlag = false;
 
 static EnrolleeNWProvInfo_t *netProvInfo;
 
 
 static EnrolleeNWProvInfo_t *netProvInfo;
 
@@ -48,220 +47,49 @@ static EnrolleeNWProvInfo_t *netProvInfo;
  */
 static OCProvisioningStatusCB cbData = NULL;
 
  */
 static OCProvisioningStatusCB cbData = NULL;
 
-void ErrorCallback(ProvStatus status)
-{
-    ProvisioningInfo *provInfo = GetCallbackObjectOnError(status);
-    cbData(provInfo);
-    ResetProgress();
-}
-
-OCStackResult InitProvisioningHandler()
-{
-    OCStackResult ret = OC_STACK_ERROR;
-    /* Initialize OCStack*/
-    if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
-    {
-        OIC_LOG(ERROR, TAG, "OCStack init error");
-        return ret;
-    }
-
-    // start
-
-    char *string = "listeningFunc invoked in a thread";
-
-    pthread_t thread_handle;
-
-    if (pthread_create(&thread_handle, NULL, listeningFunc, (void *) string))
-    {
-        OIC_LOG(DEBUG, TAG, "Thread creation failed");
-        return OC_STACK_ERROR;
-    }
-
-    pthread_join(thread_handle, NULL);
-
-    // end 
-
-    ResetProgress();
-
-    return OC_STACK_OK;
-}
-
-OCStackResult TerminateProvisioningHandler()
-{
-    OCStackResult ret = OC_STACK_ERROR;
-    if (OCStop() != OC_STACK_OK)
-    {
-        OIC_LOG(ERROR, TAG, "OCStack stop error");
-    }
-
-    g_provisioningCondFlag = true;
-
-    ret = OC_STACK_OK;
-    return ret;
-}
-
-void *listeningFunc(void *data)
-{
-    while (!g_provisioningCondFlag)
-    {
-        OCStackResult result;
-
-        result = OCProcess();
 
 
-        if (result != OC_STACK_OK)
-        {
-            OIC_LOG(ERROR, TAG, "OCStack stop error");
-        }
-
-        // To minimize CPU utilization we may wish to do this with sleep
-        sleep(1);
-    }
-    return NULL;
-}
-
-OCStackApplicationResult ProvisionEnrolleeResponse(void *ctx, OCDoHandle handle,
-        OCClientResponse *clientResponse)
-{
-
-    ProvisioningInfo *provInfo;
-
-    if (!ValidateEnrolleResponse(clientResponse))
-    {
-        ErrorCallback( DEVICE_NOT_PROVISIONED);
-        return OC_STACK_DELETE_TRANSACTION;
-    }
-
-    char *tnn;
-    char *cd;
-
-    OCRepPayload *input = (OCRepPayload *) (clientResponse->payload);
-
-    while (input)
-    {
-
-        int64_t ps;
-        if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_PS, &ps))
-        {
-
-            if (ps == 1)
-            {
-                OIC_LOG_V(DEBUG, TAG, "PS is proper");
-                input = input->next;
-                continue;
-            }
-            else
-            {
-                OIC_LOG_V(DEBUG, TAG, "PS is NOT proper");
-                goto Error;
-
-            }
-        }
-
-        if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_TNN, &tnn))
-        {
-            if (!strcmp(tnn, netProvInfo->netAddressInfo.WIFI.ssid))
-            {
-                OIC_LOG_V(DEBUG, TAG, "SSID is proper");
-                input = input->next;
-                continue;
-            }
-            else
-            {
-                OIC_LOG_V(DEBUG, TAG, "SSID is NOT proper");
-                goto Error;
-            }
-        }
-
-        if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CD, &cd))
-        {
-            if (!strcmp(cd, netProvInfo->netAddressInfo.WIFI.pwd))
-            {
-                OIC_LOG_V(DEBUG, TAG, "Password is proper");
-                input = input->next;
-                continue;
-            }
-            else
-            {
-                OIC_LOG_V(DEBUG, TAG, "Password is NOT proper");
-                goto Error;
-            }
-        }
-
-        LogProvisioningResponse(input->values);
-
-        input = input->next;
-
-    }
-
-    SuccessCallback(clientResponse);
-
-    return OC_STACK_KEEP_TRANSACTION;
-
-    Error:
-    {
+OCStackApplicationResult GetProvisioningStatusResponse(void *ctx, OCDoHandle handle,
+                                                       OCClientResponse *clientResponse) {
 
 
-        ErrorCallback( DEVICE_NOT_PROVISIONED);
 
 
+    // If user stopped the process then return from this function;
+    if (IsSetupStopped()) {
+        ErrorCallback(DEVICE_NOT_PROVISIONED);
+        ClearMemory();
         return OC_STACK_DELETE_TRANSACTION;
     }
 
         return OC_STACK_DELETE_TRANSACTION;
     }
 
-}
-
-OCStackResult ProvisionEnrollee(OCQualityOfService qos, const char *query, const char *resUri,
-        OCDevAddr *destination)
-{
-    OIC_LOG_V(INFO, TAG, "\n\nExecuting ProvisionEnrollee%s", __func__);
-
-    OCRepPayload *payload = OCRepPayloadCreate();
-
-    OCRepPayloadSetUri(payload, resUri);
-    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_TNN, netProvInfo->netAddressInfo.WIFI.ssid);
-    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CD, netProvInfo->netAddressInfo.WIFI.pwd);
-
-    OIC_LOG_V(DEBUG, TAG, "OCPayload ready for ProvisionEnrollee");
-
-    OCStackResult ret = InvokeOCDoResource(query, OC_REST_PUT, destination, OC_HIGH_QOS,
-            ProvisionEnrolleeResponse, payload, NULL, 0);
-
-    return ret;
-}
-
-OCStackApplicationResult GetProvisioningStatusResponse(void *ctx, OCDoHandle handle,
-        OCClientResponse *clientResponse)
-{
 
     ProvisioningInfo *provInfo;
 
 
     ProvisioningInfo *provInfo;
 
-    if (!ValidateEnrolleResponse(clientResponse))
-    {
-        ErrorCallback( DEVICE_NOT_PROVISIONED);
+    if (!ValidateEnrolleResponse(clientResponse)) {
+        ErrorCallback(DEVICE_NOT_PROVISIONED);
         ClearMemory();
         return OC_STACK_DELETE_TRANSACTION;
     }
 
         ClearMemory();
         return OC_STACK_DELETE_TRANSACTION;
     }
 
-    OCRepPayload *input = (OCRepPayload *(clientResponse->payload);
+    OCRepPayload *input = (OCRepPayload * )(clientResponse->payload);
 
     char query[OIC_STRING_MAX_VALUE] =
 
     char query[OIC_STRING_MAX_VALUE] =
-    { '\0' };
+            {'\0'};
     char resURI[MAX_URI_LENGTH] =
     char resURI[MAX_URI_LENGTH] =
-    { '\0' };
+            {'\0'};
 
     OIC_LOG_V(DEBUG, TAG, "resUri = %s", input->uri);
 
     strncpy(resURI, input->uri, sizeof(resURI) - 1);
 
     snprintf(query, sizeof(query), UNICAST_PROV_STATUS_QUERY, clientResponse->addr->addr, IP_PORT,
 
     OIC_LOG_V(DEBUG, TAG, "resUri = %s", input->uri);
 
     strncpy(resURI, input->uri, sizeof(resURI) - 1);
 
     snprintf(query, sizeof(query), UNICAST_PROV_STATUS_QUERY, clientResponse->addr->addr, IP_PORT,
-            resURI);
+             resURI);
 
     //OCPayloadLogRep(DEBUG,TAG,input);
 
 
     //OCPayloadLogRep(DEBUG,TAG,input);
 
-    if (ProvisionEnrollee(OC_HIGH_QOS, query, OC_RSRVD_ES_URI_PROV, clientResponse->addr)
-            != OC_STACK_OK)
-    {
+    if (ProvisionEnrollee(OC_HIGH_QOS, query, OC_RSRVD_ES_URI_PROV, clientResponse->addr, 0)
+        != OC_STACK_OK) {
         OIC_LOG(INFO, TAG,
                 "GetProvisioningStatusResponse received NULL clientResponse.Invoking Provisioing Status Callback");
 
         OIC_LOG(INFO, TAG,
                 "GetProvisioningStatusResponse received NULL clientResponse.Invoking Provisioing Status Callback");
 
-        ErrorCallback( DEVICE_NOT_PROVISIONED);
+        ErrorCallback(DEVICE_NOT_PROVISIONED);
         ClearMemory();
         return OC_STACK_DELETE_TRANSACTION;
     }
         ClearMemory();
         return OC_STACK_DELETE_TRANSACTION;
     }
@@ -270,40 +98,17 @@ OCStackApplicationResult GetProvisioningStatusResponse(void *ctx, OCDoHandle han
 
 }
 
 
 }
 
-OCStackResult InvokeOCDoResource(const char *query, OCMethod method, const OCDevAddr *dest,
-        OCQualityOfService qos, OCClientResponseHandler cb, OCRepPayload *payload,
-        OCHeaderOption *options, uint8_t numOptions)
-{
-    OCStackResult ret;
-    OCCallbackData cbData;
-
-    cbData.cb = cb;
-    cbData.context = (void *) DEFAULT_CONTEXT_VALUE;
-    cbData.cd = NULL;
-
-    ret = OCDoResource(NULL, method, query, dest, (OCPayload *) payload, OC_CONNTYPE, qos, &cbData,
-            options, numOptions);
-
-    if (ret != OC_STACK_OK)
-    {
-        OIC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
-    }
-
-    return ret;
-}
-
 OCStackResult GetProvisioningStatus(OCQualityOfService qos, const char *query,
 OCStackResult GetProvisioningStatus(OCQualityOfService qos, const char *query,
-        const OCDevAddr *destination)
-{
+                                    const OCDevAddr *destination) {
     OCStackResult ret = OC_STACK_ERROR;
     OCHeaderOption options[MAX_HEADER_OPTIONS];
 
     OIC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
 
     uint8_t option0[] =
     OCStackResult ret = OC_STACK_ERROR;
     OCHeaderOption options[MAX_HEADER_OPTIONS];
 
     OIC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
 
     uint8_t option0[] =
-    { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+            {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
     uint8_t option1[] =
     uint8_t option1[] =
-    { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
+            {11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
     memset(options, 0, sizeof(OCHeaderOption) * MAX_HEADER_OPTIONS);
     options[0].protocolID = OC_COAP_ID;
     options[0].optionID = 2048;
     memset(options, 0, sizeof(OCHeaderOption) * MAX_HEADER_OPTIONS);
     options[0].protocolID = OC_COAP_ID;
     options[0].optionID = 2048;
@@ -315,73 +120,27 @@ OCStackResult GetProvisioningStatus(OCQualityOfService qos, const char *query,
     options[1].optionLength = 10;
 
     ret = InvokeOCDoResource(query, OC_REST_GET, destination, OC_HIGH_QOS,
     options[1].optionLength = 10;
 
     ret = InvokeOCDoResource(query, OC_REST_GET, destination, OC_HIGH_QOS,
-            GetProvisioningStatusResponse, NULL, options, 2);
+                             GetProvisioningStatusResponse, NULL, options, 2);
     return ret;
 }
 
     return ret;
 }
 
-OCStackResult StartProvisioningProcess(const EnrolleeNWProvInfo_t *netInfo,
-        OCProvisioningStatusCB provisioningStatusCallback)
-{
-
-    OCStackResult result = OC_STACK_ERROR;
-
-    char *string = "StartProvisioningProcess in a thread";
-
-    pthread_t thread_handle;
-
-    if (!ValidateEasySetupParams(netInfo, provisioningStatusCallback))
-    {
-        goto Error;
-    }
-
-    //Only basis test is done for below API
-    if (!SetProgress(provisioningStatusCallback))
-    {
-        // Device provisioning session is running already.
-        OIC_LOG(INFO, TAG, PCF("Device provisioning session is running already"));
-        goto Error;
-    }
-
-    if (!ConfigEnrolleeObject(netInfo))
-    {
-        goto Error;
-    }
-
-    if (pthread_create(&thread_handle, NULL, FindProvisioningResource, string))
-    {
-        OIC_LOG(DEBUG, TAG, "Thread creation failed");
-        goto Error;
-    }
 
 
-    pthread_join(thread_handle, NULL);
+// This is a function called back when a device is discovered
+OCStackApplicationResult FindProvisioningResourceResponse(void *ctx, OCDoHandle handle,
+                                                          OCClientResponse *clientResponse) {
 
 
-    return OC_STACK_OK;
+    OIC_LOG(INFO, TAG, PCF("Entering FindProvisioningResourceResponse"));
 
 
-    Error:
-    {
-        ErrorCallback( DEVICE_NOT_PROVISIONED);
+    // If user stopped the process then return from this function;
+    if (IsSetupStopped()) {
+        ErrorCallback(DEVICE_NOT_PROVISIONED);
         ClearMemory();
         ClearMemory();
-        return OC_STACK_ERROR;
+        return OC_STACK_DELETE_TRANSACTION;
     }
 
     }
 
-}
-
-void StopProvisioningProcess()
-{
-    //Only basis test is done for below API
-    ResetProgress();
-}
-
-// This is a function called back when a device is discovered
-OCStackApplicationResult FindProvisioningResourceResponse(void *ctx, OCDoHandle handle,
-        OCClientResponse *clientResponse)
-{
-
-    OIC_LOG(INFO, TAG, PCF("Entering FindProvisioningResourceResponse"));
 
 
-    if (!ValidateFinddResourceResponse(clientResponse))
-    {
-        ErrorCallback( DEVICE_NOT_PROVISIONED);
+    if (!ValidateFinddResourceResponse(clientResponse)) {
+        ErrorCallback(DEVICE_NOT_PROVISIONED);
         return OC_STACK_DELETE_TRANSACTION;
     }
 
         return OC_STACK_DELETE_TRANSACTION;
     }
 
@@ -389,28 +148,26 @@ OCStackApplicationResult FindProvisioningResourceResponse(void *ctx, OCDoHandle
 
     ProvisioningInfo *provInfo;
     char szQueryUri[64] =
 
     ProvisioningInfo *provInfo;
     char szQueryUri[64] =
-    { 0 };
+            {0};
 
 
-    OCDiscoveryPayload *discoveryPayload = (OCDiscoveryPayload *(clientResponse->payload);
+    OCDiscoveryPayload *discoveryPayload = (OCDiscoveryPayload * )(clientResponse->payload);
 
     // Need to conform if below check is required or not. As Null check of clientResponse->payload is already performed above
 
     // Need to conform if below check is required or not. As Null check of clientResponse->payload is already performed above
-    if (!discoveryPayload)
-    {
+    if (!discoveryPayload) {
         OIC_LOG_V(DEBUG, TAG, "Failed To parse");
         OIC_LOG_V(DEBUG, TAG, "Failed To parse");
-        ErrorCallback( DEVICE_NOT_PROVISIONED);
+        ErrorCallback(DEVICE_NOT_PROVISIONED);
         return OC_STACK_DELETE_TRANSACTION;
     }
 
     OIC_LOG_V(DEBUG, TAG, "resUri = %s", discoveryPayload->resources->uri);
 
     snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_PROV_STATUS_QUERY,
         return OC_STACK_DELETE_TRANSACTION;
     }
 
     OIC_LOG_V(DEBUG, TAG, "resUri = %s", discoveryPayload->resources->uri);
 
     snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_PROV_STATUS_QUERY,
-            clientResponse->devAddr.addr, IP_PORT, discoveryPayload->resources->uri);
+             clientResponse->devAddr.addr, IP_PORT, discoveryPayload->resources->uri);
 
     OIC_LOG_V(DEBUG, TAG, "query before GetProvisioningStatus call = %s", szQueryUri);
 
 
     OIC_LOG_V(DEBUG, TAG, "query before GetProvisioningStatus call = %s", szQueryUri);
 
-    if (GetProvisioningStatus(OC_HIGH_QOS, szQueryUri, &clientResponse->devAddr) != OC_STACK_OK)
-    {
-        ErrorCallback( DEVICE_NOT_PROVISIONED);
+    if (GetProvisioningStatus(OC_HIGH_QOS, szQueryUri, &clientResponse->devAddr) != OC_STACK_OK) {
+        ErrorCallback(DEVICE_NOT_PROVISIONED);
         return OC_STACK_DELETE_TRANSACTION;
     }
 
         return OC_STACK_DELETE_TRANSACTION;
     }
 
@@ -418,89 +175,15 @@ OCStackApplicationResult FindProvisioningResourceResponse(void *ctx, OCDoHandle
 
 }
 
 
 }
 
-void *FindProvisioningResource(void *data)
-{
-    OCStackResult ret = OC_STACK_ERROR;
-
-    /* Start a discovery query*/
-    char szQueryUri[64] =
-    { 0 };
-
-    snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_PROVISIONING_QUERY,
-            netProvInfo->netAddressInfo.WIFI.ipAddress, IP_PORT);
-
-    OIC_LOG_V(DEBUG, TAG, "szQueryUri = %s", szQueryUri);
-
-    OCCallbackData ocCBData;
-
-    ocCBData.cb = FindProvisioningResourceResponse;
-    ocCBData.context = (void *) DEFAULT_CONTEXT_VALUE;
-    ocCBData.cd = NULL;
-
-    ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, NULL, NULL, OC_CONNTYPE, OC_LOW_QOS,
-            &ocCBData, NULL, 0);
-
-    if (ret != OC_STACK_OK)
-    {
-        ErrorCallback( DEVICE_NOT_PROVISIONED);
-        ClearMemory();
-    }
-    return NULL;
-}
-
-OCStackResult FindNetworkResource()
-{
-    OCStackResult ret = OC_STACK_ERROR;
-    if (OCStop() != OC_STACK_OK)
-    {
-        OIC_LOG(ERROR, TAG, "OCStack stop error");
-    }
-
-    return ret;
-}
-
-ProvisioningInfo *PrepareProvisioingStatusCB(OCClientResponse *clientResponse,
-        ProvStatus provStatus)
-{
-
-    ProvisioningInfo *provInfo = (ProvisioningInfo *) OICCalloc(1, sizeof(ProvisioningInfo));
-
-    if (provInfo == NULL)
-    {
-        OIC_LOG_V(ERROR, TAG, "Failed to allocate memory");
-        return NULL;
-    }
-
-    OCDevAddr *devAddr = (OCDevAddr *) OICCalloc(1, sizeof(OCDevAddr));
-
-    if (devAddr == NULL)
-    {
-        OIC_LOG_V(ERROR, TAG, "Failed to allocate memory");
-        return NULL;
-    }
-
-    strncpy(devAddr->addr, clientResponse->addr->addr, sizeof(devAddr->addr));
-    devAddr->port = clientResponse->addr->port;
-
-    provInfo->provDeviceInfo.addr = devAddr;
-
-    provInfo->provStatus = provStatus;
-
-    return provInfo;
-}
-
 bool ValidateEasySetupParams(const EnrolleeNWProvInfo_t *netInfo,
 bool ValidateEasySetupParams(const EnrolleeNWProvInfo_t *netInfo,
-        OCProvisioningStatusCB provisioningStatusCallback)
-{
+                             OCProvisioningStatusCB provisioningStatusCallback) {
 
 
-    if (netInfo == NULL || strlen(netInfo->netAddressInfo.WIFI.ipAddress) == 0)
-    {
+    if (netInfo == NULL || strlen(netInfo->netAddressInfo.WIFI.ipAddress) == 0) {
         OIC_LOG(ERROR, TAG, "Request URI is NULL");
         return false;
     }
 
         OIC_LOG(ERROR, TAG, "Request URI is NULL");
         return false;
     }
 
-    if (provisioningStatusCallback == NULL)
-    {
+    if (provisioningStatusCallback == NULL) {
         OIC_LOG(ERROR, TAG, "ProvisioningStatusCallback is NULL");
         return false;
     }
         OIC_LOG(ERROR, TAG, "ProvisioningStatusCallback is NULL");
         return false;
     }
@@ -509,220 +192,5 @@ bool ValidateEasySetupParams(const EnrolleeNWProvInfo_t *netInfo,
 
 }
 
 
 }
 
-bool InProgress()
-{
-
-    // It means already Easy Setup provisioning session is going on.
-    if (NULL != cbData)
-    {
-        OIC_LOG(ERROR, TAG, "Easy setup session is already in progress");
-        return true;
-    }
-
-    return false;
-}
-
-bool SetProgress(OCProvisioningStatusCB provisioningStatusCallback)
-{
-
-    if (InProgress())
-        return false;
-
-    cbData = provisioningStatusCallback;
-
-    return true;
-}
-
-bool ResetProgress()
-{
-
-    cbData = NULL;
-    return true;
-}
-
-ProvisioningInfo *CreateCallBackObject()
-{
-
-    ProvisioningInfo *provInfo = (ProvisioningInfo *) OICCalloc(1, sizeof(ProvisioningInfo));
-
-    if (provInfo == NULL)
-    {
-        OIC_LOG_V(ERROR, TAG, "Failed to allocate memory");
-        return NULL;
-    }
-
-    OCDevAddr *devAddr = (OCDevAddr *) OICCalloc(1, sizeof(OCDevAddr));
-
-    if (devAddr == NULL)
-    {
-        OIC_LOG_V(ERROR, TAG, "Failed to allocate memory");
-        return NULL;
-    }
-
-    provInfo->provDeviceInfo.addr = devAddr;
-
-    return provInfo;
-
-}
-
-ProvisioningInfo *GetCallbackObjectOnError(ProvStatus status)
-{
-
-    ProvisioningInfo *provInfo = CreateCallBackObject();
-    strncpy(provInfo->provDeviceInfo.addr->addr, netProvInfo->netAddressInfo.WIFI.ipAddress,
-            sizeof(provInfo->provDeviceInfo.addr->addr));
-    provInfo->provDeviceInfo.addr->port = IP_PORT;
-    provInfo->provStatus = status;
-    return provInfo;
-}
-
-ProvisioningInfo *GetCallbackObjectOnSuccess(OCClientResponse *clientResponse,
-        ProvStatus provStatus)
-{
-    ProvisioningInfo *provInfo = CreateCallBackObject();
-    strncpy(provInfo->provDeviceInfo.addr->addr, clientResponse->addr->addr,
-            sizeof(provInfo->provDeviceInfo.addr->addr));
-    provInfo->provDeviceInfo.addr->port = clientResponse->addr->port;
-    provInfo->provStatus = provStatus;
-    return provInfo;
-}
-
-bool ValidateFinddResourceResponse(OCClientResponse * clientResponse)
-{
-
-    if (!(clientResponse) || !(clientResponse->payload))
-    {
-
-        OIC_LOG_V(INFO, TAG, "ProvisionEnrolleeResponse received Null clientResponse");
-
-        return false;
-
-    }
-    return true;
-}
-
-bool ValidateEnrolleResponse(OCClientResponse * clientResponse)
-{
-
-    if (!(clientResponse) || !(clientResponse->payload))
-    {
-
-        OIC_LOG_V(INFO, TAG, "ProvisionEnrolleeResponse received Null clientResponse");
-
-        return false;
-
-    }
-
-    if (clientResponse->payload->type != PAYLOAD_TYPE_REPRESENTATION)
-    {
-
-        OIC_LOG_V(DEBUG, TAG, "Incoming payload not a representation");
-        return false;
-
-    }
-
-    // If flow reachese here means no error condition hit.
-    return true;
-
-}
-
-void SuccessCallback(OCClientResponse * clientResponse)
-{
-    ProvisioningInfo *provInfo = GetCallbackObjectOnSuccess(clientResponse, DEVICE_PROVISIONED);
-    cbData(provInfo);
-    ResetProgress();
-}
-
-bool ClearMemory()
-{
-
-    OIC_LOG(DEBUG, TAG, "thread_pool_add_task of FindProvisioningResource failed");
-    return true;
-
-}
-
-bool ConfigEnrolleeObject(const EnrolleeNWProvInfo_t *netInfo)
-{
-
-    //Copy Network Provisioning  Information
-    netProvInfo = (EnrolleeNWProvInfo_t *) OICCalloc(1, sizeof(EnrolleeNWProvInfo_t));
 
 
-    if (netProvInfo == NULL)
-    {
-        OIC_LOG(ERROR, TAG, "Invalid input..");
-        return false;
-    }
-
-    memcpy(netProvInfo, netInfo, sizeof(EnrolleeNWProvInfo_t));
-
-    OIC_LOG_V(DEBUG, TAG, "Network Provisioning Info. SSID = %s",
-            netProvInfo->netAddressInfo.WIFI.ssid);
-
-    OIC_LOG_V(DEBUG, TAG, "Network Provisioning Info. PWD = %s",
-            netProvInfo->netAddressInfo.WIFI.pwd);
-
-    return true;
-
-}
-
-void LogProvisioningResponse(OCRepPayloadValue * val)
-{
-
-    switch (val->type)
-    {
-        case OCREP_PROP_NULL:
-            OIC_LOG_V(DEBUG, TAG, "\t\t%s: NULL", val->name);
-            break;
-        case OCREP_PROP_INT:
-            OIC_LOG_V(DEBUG, TAG, "\t\t%s(int):%lld", val->name, val->i);
-            break;
-        case OCREP_PROP_DOUBLE:
-            OIC_LOG_V(DEBUG, TAG, "\t\t%s(double):%f", val->name, val->d);
-            break;
-        case OCREP_PROP_BOOL:
-            OIC_LOG_V(DEBUG, TAG, "\t\t%s(bool):%s", val->name, val->b ? "true" : "false");
-            break;
-        case OCREP_PROP_STRING:
-            OIC_LOG_V(DEBUG, TAG, "\t\t%s(string):%s", val->name, val->str);
-            break;
-        case OCREP_PROP_OBJECT:
-            // Note: Only prints the URI (if available), to print further, you'll
-            // need to dig into the object better!
-            OIC_LOG_V(DEBUG, TAG, "\t\t%s(OCRep):%s", val->name, val->obj->uri);
-            break;
-        case OCREP_PROP_ARRAY:
-            switch (val->arr.type)
-            {
-                case OCREP_PROP_INT:
-                    OIC_LOG_V(DEBUG, TAG, "\t\t%s(int array):%lld x %lld x %lld", val->name,
-                            val->arr.dimensions[0], val->arr.dimensions[1], val->arr.dimensions[2]);
-                    break;
-                case OCREP_PROP_DOUBLE:
-                    OIC_LOG_V(DEBUG, TAG, "\t\t%s(double array):%lld x %lld x %lld", val->name,
-                            val->arr.dimensions[0], val->arr.dimensions[1], val->arr.dimensions[2]);
-                    break;
-                case OCREP_PROP_BOOL:
-                    OIC_LOG_V(DEBUG, TAG, "\t\t%s(bool array):%lld x %lld x %lld", val->name,
-                            val->arr.dimensions[0], val->arr.dimensions[1], val->arr.dimensions[2]);
-                    break;
-                case OCREP_PROP_STRING:
-                    OIC_LOG_V(DEBUG, TAG, "\t\t%s(string array):%lld x %lld x %lld", val->name,
-                            val->arr.dimensions[0], val->arr.dimensions[1], val->arr.dimensions[2]);
-                    break;
-                case OCREP_PROP_OBJECT:
-                    OIC_LOG_V(DEBUG, TAG, "\t\t%s(OCRep array):%lld x %lld x %lld", val->name,
-                            val->arr.dimensions[0], val->arr.dimensions[1], val->arr.dimensions[2]);
-                    break;
-                default:
-                    //OIC_LOG_V(ERROR, TAG, "\t\t%s <-- Unknown/unsupported array type!",
-                    //  val->name);
-                    break;
-            }
-            break;
-        default:
-            /*OC_LOG_V(ERROR, TAG
-             , "\t\t%s <-- Unknown type!", val->name);*/
-            break;
-    }
-}