replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / src / EnrolleeResource.cpp
index 47b4545..a1e35e9 100755 (executable)
@@ -19,7 +19,6 @@
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
 #include <functional>
-#include <time.h>
 
 #include "EnrolleeResource.h"
 
@@ -32,487 +31,239 @@ namespace OIC
 {
     namespace Service
     {
-        #define ES_REMOTE_ENROLLEE_RES_TAG "ES_REMOTE_ENROLLEE_RES"
-        #define DISCOVERY_TIMEOUT 5
+        #define ES_REMOTE_ENROLLEE_RES_TAG "ES_ENROLLEE_RESOURCE"
 
-        static const char ES_BASE_RES_URI[] = "/oic/res";
-        static const char ES_PROV_RES_URI[] = "/oic/prov";
-        static const char ES_PROV_RES_TYPE[] = "oic.r.prov";
-
-        EnrolleeResource::EnrolleeResource(const WiFiOnboadingConnection &onboardingconn)
-        {
-            m_wifiOnboardingconn = onboardingconn;
-            m_discoveryResponse = false;
-        }
-/*
-        EnrolleeResource::EnrolleeResource(const ProvConfig &provConfig,
-                                                  const WiFiOnboadingConnection &onboardingconn)
+        EnrolleeResource::EnrolleeResource(std::shared_ptr< OC::OCResource > resource)
         {
-            m_ProvConfig = provConfig;
-            m_wifiOnboardingconn = onboardingconn;
-            m_discoveryResponse = false;
+            m_ocResource = resource;
+            m_getStatusCb = nullptr;
+            m_getConfigurationStatusCb = nullptr;
+            m_devicePropProvStatusCb = nullptr;
+            m_connectRequestStatusCb = nullptr;
         }
-*/
-        void EnrolleeResource::triggerNetworkConnectionCb(
-                const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep,
-                const int eCode)
-        {
-            OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "checkProvInformationCb : %s, eCode = %d",
-                    rep.getUri().c_str(),
-                    eCode);
 
-            if (eCode != 0)
-            {
-                OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
-                        "triggerNetworkConnectionCb : Trigger action failed ");
-                std::shared_ptr< ProvisioningStatus > provStatus = std::make_shared<
-                        ProvisioningStatus >(ESResult::ES_ERROR, ESState::ES_PROVISIONING_ERROR);
-                m_dataProvStatusCb(provStatus);
-                return;
-            }
-            else
+        void EnrolleeResource::onEnrolleeResourceSafetyCB(const HeaderOptions& headerOptions,
+                                                        const OCRepresentation& rep,
+                                                        const int eCode,
+                                                        ESEnrolleeResourceCb cb,
+                                                        std::weak_ptr<EnrolleeResource> this_ptr)
+        {
+            OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onEnrolleeResourceSafetyCB");
+            std::shared_ptr<EnrolleeResource> Ptr = this_ptr.lock();
+            if(Ptr)
             {
-                OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
-                        "triggerNetworkConnectionCb : Provisioning is success ");
-                std::shared_ptr< ProvisioningStatus > provStatus = std::make_shared<
-                        ProvisioningStatus >(ESResult::ES_OK, ESState::ES_PROVISIONING_SUCCESS);
-                m_dataProvStatusCb(provStatus);
-                return;
+                cb(headerOptions, rep, eCode);
             }
         }
 
-        void EnrolleeResource::triggerNetworkConnection()
+        void EnrolleeResource::onProvisioningResponse(const HeaderOptions& /*headerOptions*/,
+                const OCRepresentation& /*rep*/, const int eCode)
         {
-            if (m_ocResource == nullptr)
+            OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onProvisioningResponse : eCode = %d",
+                        eCode);
+
+            if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
             {
-                throw ESBadRequestException("Resource is not initialized");
-            }
+                ESResult result = ESResult::ES_ERROR;
+
+                OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_RES_TAG,
+                            "onProvisioningResponse : Provisioning is failed ");
 
-            OCRepresentation provisioningRepresentation;
+                if(eCode == OCStackResult::OC_STACK_COMM_ERROR)
+                {
+                    OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_RES_TAG,
+                        "can't receive any response from Enrollee by a timeout threshold.");
+                    result = ESResult::ES_COMMUNICATION_ERROR;
+                }
+
+                std::shared_ptr< DevicePropProvisioningStatus > provStatus = std::make_shared<
+                        DevicePropProvisioningStatus >(result);
+                m_devicePropProvStatusCb(provStatus);
+                return;
+            }
 
-            provisioningRepresentation.setValue(OC_RSRVD_ES_TRIGGER, 1);
+            OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
+                    "onProvisioningResponse : Provisioning is success. ");
 
-            m_ocResource->post(provisioningRepresentation, QueryParamsMap(),
-                    std::function<
-                            void(const HeaderOptions& headerOptions, const OCRepresentation& rep,
-                                    const int eCode) >(
-                            std::bind(&EnrolleeResource::triggerNetworkConnectionCb, this,
-                                    std::placeholders::_1, std::placeholders::_2,
-                                    std::placeholders::_3)));
+            std::shared_ptr< DevicePropProvisioningStatus > provStatus = std::make_shared<
+                    DevicePropProvisioningStatus >(ESResult::ES_OK);
+            m_devicePropProvStatusCb(provStatus);
         }
 
-        void EnrolleeResource::checkProvInformationCb(const HeaderOptions& /*headerOptions*/,
+        void EnrolleeResource::onGetStatusResponse(const HeaderOptions& /*headerOptions*/,
                 const OCRepresentation& rep, const int eCode)
         {
-            OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "checkProvInformationCb : %s, eCode = %d",
-                    rep.getUri().c_str(),
-                    eCode);
+            OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onGetStatusResponse : eCode = %d",
+                        eCode);
 
-            if (eCode != 0)
+            if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
             {
-                OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
-                        "checkProvInformationCb : Provisioning is failed ");
-                std::shared_ptr< ProvisioningStatus > provStatus = std::make_shared<
-                        ProvisioningStatus >(ESResult::ES_ERROR, ESState::ES_PROVISIONING_ERROR);
-                m_dataProvStatusCb(provStatus);
-                return;
-            }
-
-            int ps = -1;
+                ESResult result = ESResult::ES_ERROR;
 
-            rep.getValue(OC_RSRVD_ES_PROVSTATUS, ps);
+                OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_RES_TAG,
+                            "onGetStatusResponse : onGetStatusResponse is failed ");
 
-            OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "checkProvInformationCb : ps - %d", ps);
-
-            //Provisioning status check
-            if (ps == ES_PS_PROVISIONING_COMPLETED)
-            {
-                OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
-                        "checkProvInformationCb : Provisioning is success. "
-                        "Now trigger network connection ");
+                if(eCode == OCStackResult::OC_STACK_COMM_ERROR)
+                {
+                    OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_RES_TAG,
+                        "can't receive any response from Enrollee by a timeout threshold.");
+                    result = ESResult::ES_COMMUNICATION_ERROR;
+                }
 
-                #ifdef REMOTE_ARDUINO_ENROLEE
-                 std::shared_ptr< ProvisioningStatus > provStatus = std::make_shared<
-                        ProvisioningStatus >(ESResult::ES_OK, ESState::ES_PROVISIONING_SUCCESS);
-                m_dataProvStatusCb(provStatus);
-                #endif
+                EnrolleeStatus enrolleeStatus(rep);
+                std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus = std::make_shared<
+                        GetEnrolleeStatus >(result, enrolleeStatus);
 
-                triggerNetworkConnection();
-                return;
+                m_getStatusCb(getEnrolleeStatus);
             }
             else
             {
-                OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
-                        "checkProvInformationCb : Provisioning is failed ");
-                std::shared_ptr< ProvisioningStatus > provStatus = std::make_shared<
-                        ProvisioningStatus >(ESResult::ES_ERROR, ESState::ES_PROVISIONING_ERROR);
-                m_dataProvStatusCb(provStatus);
-                return;
+                EnrolleeStatus enrolleeStatus(rep);
+                std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus = std::make_shared<
+                        GetEnrolleeStatus >(ESResult::ES_OK, enrolleeStatus);
+
+                m_getStatusCb(getEnrolleeStatus);
             }
         }
 
-        void EnrolleeResource::getCapabilityResponse(const HeaderOptions& /*headerOptions*/,
+        void EnrolleeResource::onGetConfigurationResponse(const HeaderOptions& /*headerOptions*/,
                 const OCRepresentation& rep, const int eCode)
         {
-            OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getCapabilityResponse : %s, eCode = %d",
-                    rep.getUri().c_str(), eCode);
+            OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onGetConfigurationResponse : eCode = %d",
+                        eCode);
 
-            if (eCode != 0)
+            if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
             {
-                ESResult result  = ESResult::ES_ERROR;
+                ESResult result = ESResult::ES_ERROR;
 
-                OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,"getCapabilityResponse : getCapabilityResponse is failed ");
+                OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_RES_TAG,
+                            "onGetConfigurationResponse : onGetConfigurationResponse is failed ");
 
-                if (eCode == OCStackResult::OC_STACK_UNAUTHORIZED_REQ)
+                if(eCode == OCStackResult::OC_STACK_COMM_ERROR)
                 {
-                    OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
-                        "Mediator is unauthorized from Enrollee.");
-                    result = ESResult::ES_UNAUTHORIZED;
+                    OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_RES_TAG,
+                        "can't receive any response from Enrollee by a timeout threshold.");
+                    result = ESResult::ES_COMMUNICATION_ERROR;
                 }
 
-                CapabilityData capabilityData;
-                std::shared_ptr< RequestCapabilityStatus > requestCapabilityStatus = std::make_shared<
-                        RequestCapabilityStatus >(result, capabilityData);
-                m_requestCapabilityStatusCb(requestCapabilityStatus);
-
-                return;
+                EnrolleeConf enrolleeConf(rep);
+                std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
+                        GetConfigurationStatus >(result, enrolleeConf);
+                m_getConfigurationStatusCb(getConfigurationStatus);
             }
+            else
+            {
+                EnrolleeConf enrolleeConf(rep);
 
-            CapabilityData capabilityData = CapabilityData();
-            std::shared_ptr< RequestCapabilityStatus > requestCapabilityStatus = std::make_shared<
-                    RequestCapabilityStatus >(ESResult::ES_OK, capabilityData);
-            m_requestCapabilityStatusCb(requestCapabilityStatus);
+                std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
+                        GetConfigurationStatus >(ESResult::ES_OK, enrolleeConf);
+                m_getConfigurationStatusCb(getConfigurationStatus);
+            }
         }
 
-        void EnrolleeResource::getProvStatusResponse(const HeaderOptions& /*headerOptions*/,
-                const OCRepresentation& rep, const int eCode)
+         void EnrolleeResource::onConnectRequestResponse(const HeaderOptions& /*headerOptions*/,
+                const OCRepresentation& /*rep*/, const int eCode)
         {
-            OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : %s, eCode = %d",
-                    rep.getUri().c_str(),
-                    eCode);
+            OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onConnectRequestResponse : eCode = %d",
+                        eCode);
 
-            if (eCode != 0)
+            if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
             {
-                ESResult result  = ESResult::ES_ERROR;
+                ESResult result = ESResult::ES_ERROR;
 
-                OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,"getProvStatusResponse : Provisioning is failed ");
+                OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
+                            "onConnectRequestResponse : onConnectRequestResponse is failed ");
 
-                if (eCode == OCStackResult::OC_STACK_UNAUTHORIZED_REQ)
+                if(eCode == OCStackResult::OC_STACK_COMM_ERROR)
                 {
                     OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
-                        "Mediator is unauthorized from Enrollee.");
-                    result = ESResult::ES_UNAUTHORIZED;
+                        "can't receive any response from Enrollee by a timeout threshold.");
+                    result = ESResult::ES_COMMUNICATION_ERROR;
                 }
-                std::shared_ptr< ProvisioningStatus > provStatus = std::make_shared<
-                        ProvisioningStatus >(result, ESState::ES_PROVISIONING_ERROR);
-                m_dataProvStatusCb(provStatus);
 
+                std::shared_ptr< ConnectRequestStatus > connectRequestStatus = std::make_shared<
+                        ConnectRequestStatus >(result);
+                m_connectRequestStatusCb(connectRequestStatus);
                 return;
             }
 
-            int ps = -1;
+            OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
+                    "onConnectRequestResponse : Provisioning is success. ");
 
-            rep.getValue(OC_RSRVD_ES_PROVSTATUS, ps);
-
-            OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : ps - %d",
-                    ps);
-
-            if (ps == ES_PS_NEED_PROVISIONING) //Indicates the need for provisioning
-            {
-                OCRepresentation provisioningRepresentation;
-
-                provisioningRepresentation.setValue(OC_RSRVD_ES_SSID,
-                std::string(m_ProvConfig.provData.WIFI.ssid));
-                provisioningRepresentation.setValue(OC_RSRVD_ES_CRED,
-                std::string(m_ProvConfig.provData.WIFI.pwd));
-                provisioningRepresentation.setValue(OC_RSRVD_ES_AUTHCODE,
-                std::string(m_ProvConfig.provData.WIFI.authcode));
-                provisioningRepresentation.setValue(OC_RSRVD_ES_AUTHPROVIDER,
-                std::string(m_ProvConfig.provData.WIFI.authserverUrl));
-                provisioningRepresentation.setValue(OC_RSRVD_ES_CISERVER,
-                std::string(m_ProvConfig.provData.WIFI.apiserverUrl));
-                provisioningRepresentation.setValue( OC_RSRVD_ES_AUTHTYPE,
-                m_ProvConfig.provData.WIFI.authtype);
-                provisioningRepresentation.setValue(OC_RSRVD_ES_ENCTYPE,
-                m_ProvConfig.provData.WIFI.enctype);
-
-                OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : ssid - %s",
-                        m_ProvConfig.provData.WIFI.ssid);
-                OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : pwd - %s",
-                        m_ProvConfig.provData.WIFI.pwd);
-                OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : AuthCode - %s",
-                        m_ProvConfig.provData.WIFI.authcode);
-                OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : AuthServerUrl - %s",
-                        m_ProvConfig.provData.WIFI.authserverUrl);
-                OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : APIServerUrl - %s",
-                        m_ProvConfig.provData.WIFI.apiserverUrl);
-                OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : authtype - %d",
-                        m_ProvConfig.provData.WIFI.authtype);
-                OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : enctype - %d",
-                        m_ProvConfig.provData.WIFI.enctype);
-
-                m_ocResource->post(provisioningRepresentation, QueryParamsMap(),
-                        std::function<
-                                void(const HeaderOptions& headerOptions,
-                                        const OCRepresentation& rep, const int eCode) >(
-                        std::bind(&EnrolleeResource::checkProvInformationCb, this,
-                        std::placeholders::_1, std::placeholders::_2,
-                        std::placeholders::_3)));
-            }
-            else if (ps == ES_PS_PROVISIONING_COMPLETED) //Indicates that provisioning is completed
-            {
-                OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
-                        "getProvStatusResponse : Provisioning is successful");
-                std::shared_ptr< ProvisioningStatus > provStatus = std::make_shared<
-                        ProvisioningStatus >(ESResult::ES_OK, ESState::ES_PROVISIONED_ALREADY);
-                m_dataProvStatusCb(provStatus);
-            }
+            std::shared_ptr< ConnectRequestStatus > connectRequestStatus = std::make_shared<
+                    ConnectRequestStatus >(ESResult::ES_OK);
+            m_connectRequestStatusCb(connectRequestStatus);
         }
 
-        void EnrolleeResource::registerInitRemoteEnrolleeStatusCallback (InitRemoteEnrolleeStatusCb callback)
-        {
-            m_initRemoteEnrolleeStatusCb = callback;
-        }
 
-        void EnrolleeResource::registerCapabilityStatusCallback(RequestCapabilityStatusCb callback)
+        void EnrolleeResource::registerGetStatusCallback(
+            const GetStatusCb callback)
         {
-            m_requestCapabilityStatusCb = callback;
+            m_getStatusCb = callback;
         }
 
-        void EnrolleeResource::registerProvStatusCallback(DataProvStatusCb callback)
+        void EnrolleeResource::registerGetConfigurationStatusCallback(
+            const GetConfigurationStatusCb callback)
         {
-            m_dataProvStatusCb = callback;
+            m_getConfigurationStatusCb = callback;
         }
 
-        ESResult EnrolleeResource::ESDiscoveryTimeout(unsigned short waittime)
+        void EnrolleeResource::registerDevicePropProvStatusCallback(
+            const DevicePropProvStatusCb callback)
         {
-            struct timespec startTime;
-            startTime.tv_sec=0;
-            startTime.tv_sec=0;
-            struct timespec currTime;
-            currTime.tv_sec=0;
-            currTime.tv_nsec=0;
-
-            ESResult res = ES_OK;
-            #ifdef _POSIX_MONOTONIC_CLOCK
-                int clock_res = clock_gettime(CLOCK_MONOTONIC, &startTime);
-            #else
-                int clock_res = clock_gettime(CLOCK_REALTIME, &startTime);
-            #endif
-
-            if (0 != clock_res)
-            {
-                return ES_ERROR;
-            }
-
-            while (ES_OK == res || m_discoveryResponse == false)
-            {
-                #ifdef _POSIX_MONOTONIC_CLOCK
-                        clock_res = clock_gettime(CLOCK_MONOTONIC, &currTime);
-                #else
-                        clock_res = clock_gettime(CLOCK_REALTIME, &currTime);
-                #endif
-
-                if (0 != clock_res)
-                {
-                    std::shared_ptr< InitRemoteEnrolleeStatus > initRemoteEnrolleeStatus = std::make_shared<
-                        InitRemoteEnrolleeStatus >(ESResult::ES_ERROR);
-                    m_initRemoteEnrolleeStatusCb(initRemoteEnrolleeStatus);
-                    return ES_ERROR;
-                }
-                long elapsed = (currTime.tv_sec - startTime.tv_sec);
-                if (elapsed > waittime)
-                {
-                    return ES_OK;
-                }
-                if (m_discoveryResponse)
-                {
-                    res = ES_OK;
-                }
-             }
-             return res;
+            m_devicePropProvStatusCb = callback;
         }
 
-        void EnrolleeResource::onDeviceDiscovered(std::shared_ptr<OC::OCResource> resource)
+        void EnrolleeResource::registerConnectRequestStatusCallback(
+            const ConnectRequestStatusCb callback)
         {
-            OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onDeviceDiscovered");
-
-            std::string resourceURI;
-            std::string hostAddress;
-            std::string hostDeviceID;
-            try
-            {
-                if(resource)
-                {
-                    // Get the resource URI
-                    resourceURI = resource->uri();
-                    OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
-                            "URI of the resource: %s", resourceURI.c_str());
-
-                    // Get the resource host address
-                    hostAddress = resource->host();
-                    OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
-                            "Host address of the resource: %s", hostAddress.c_str());
-
-                    hostDeviceID = resource->sid();
-                    OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
-                            "Host DeviceID of the resource: %s", hostDeviceID.c_str());
-                    /*
-                     * Easysetup is always performed with a single Enrollee device and
-                     * in a private network (SoftAP or BLE), so the assumption is that
-                     * only the intended device will respond for the discovery.
-                     * With the above assumption the below two statements are written.
-                     */
-                    m_ocResource = resource;
-                    m_discoveryResponse = true;
-
-                    std::shared_ptr< InitRemoteEnrolleeStatus > initRemoteEnrolleeStatus = std::make_shared<
-                        InitRemoteEnrolleeStatus >(ESResult::ES_OK);
-                    m_initRemoteEnrolleeStatusCb(initRemoteEnrolleeStatus);
-                }
-                else
-                {
-                    OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "Resource is invalid");
-
-                    std::shared_ptr< InitRemoteEnrolleeStatus > initRemoteEnrolleeStatus = std::make_shared<
-                        InitRemoteEnrolleeStatus >(ESResult::ES_ERROR);
-                    m_initRemoteEnrolleeStatusCb(initRemoteEnrolleeStatus);
-                }
-
-            }
-            catch(std::exception& e)
-            {
-                OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
-                        "Exception in foundResource: %s", e.what());
-            }
+            m_connectRequestStatusCb = callback;
         }
 
-
-        ESResult EnrolleeResource::constructResourceObject()
+        void EnrolleeResource::getStatus()
         {
-            if (m_ocResource != nullptr)
-            {
-                throw ESBadRequestException("Remote resource is already created");
-            }
-
-#ifdef REMOTE_ARDUINO_ENROLEE
-            //This process will create OCResource with port 55555 which is specific
-            // to Arduino WiFi enrollee
-            try
-            {
-
-                std::vector< std::string > interface =
-                {   DEFAULT_INTERFACE};
-                std::vector< std::string > resTypes =
-                {   ES_PROV_RES_TYPE};
-
-                OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "Before OCPlatform::constructResourceObject");
-
-                OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "m_host = %s",
-                        m_wifiOnboardingconn.ipAddress);
-                OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "ES_PROV_RES_URI = %s", ES_PROV_RES_URI);
-                OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "m_connectivityType = %d",
-                        m_ProvConfig.connType);
-                OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "resTypes = %s",
-                        resTypes.at(0).c_str());
-                OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "interface = %s", interface.at(0).c_str());
-
-                std::string host;
-                if(m_wifiOnboardingconn.isSecured)
-                {
-                    host.append("coaps://");
-                }
-                else
-                {
-                    host.append("coap://");
-                }
-
-                if(m_ProvConfig.connType == CT_ADAPTER_IP)
-                {
-                    // TODO : RemoteEnrollee is current handling easysetup on IP transport.
-                    // WiFiRemoteEnrollee need to extend RemoteEnrollee for providing IP specific
-                    // Enrollee easysetup.
-
-                    host.append(m_wifiOnboardingconn.ipAddress);
-                    //TODO : If the target Enrollee is not a Arduino Wi-Fi device,
-                    // then the port number will be found during resource discovery instead of
-                    // using 55555
-                    host.append(":55555");
-                }
-
-                OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "HOST = %s", host.c_str());
-
-                m_ocResource = OC::OCPlatform::constructResourceObject(host,
-                        ES_PROV_RES_URI,
-                        m_ProvConfig.connType,
-                        true,
-                        resTypes,
-                        interface);
-                OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
-                        "created OCResource : %s", m_ocResource->uri().c_str());
-
-                return ES_OK;
-            }
-            catch (OCException & e)
-            {
-                OIC_LOG_V(ERROR, ES_REMOTE_ENROLLEE_RES_TAG,
-                        "Exception for constructResourceObject : %s", e.reason().c_str());
-                return ES_ERROR;
-            }
-#else
-            std::string host("");
-            std::string query("");
+            OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getStatus IN");
 
-            if (m_wifiOnboardingconn.isSecured)
-            {
-                host.append("coaps://");
-            }
-            else
+            if (m_ocResource == nullptr)
             {
-                host.append("coap://");
+                throw ESBadRequestException("Resource is not initialized");
             }
 
-            query.append(ES_BASE_RES_URI);
-            query.append("?rt=");
-            query.append(ES_PROV_RES_TYPE);
-
-            OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "Before OCPlatform::constructResourceObject");
+            OC::QueryParamsMap query;
+            OC::OCRepresentation rep;
 
-            OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "host = %s", host.c_str());
-            OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "query = %s", query.c_str());
-            OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "m_connectivityType = %d",
-                    m_ProvConfig.connType);
+            std::function< OCStackResult(void) > getStatus = [&]
+            {
+                ESEnrolleeResourceCb cb = std::bind(&EnrolleeResource::onEnrolleeResourceSafetyCB,
+                                std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
+                                static_cast<ESEnrolleeResourceCb>(
+                                std::bind(&EnrolleeResource::onGetStatusResponse, this,
+                                std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
+                                shared_from_this());
+
+                return m_ocResource->get(m_ocResource->getResourceTypes().at(0),
+                        DEFAULT_INTERFACE, query, cb, OC::QualityOfService::HighQos);
+            };
 
-            m_discoveryResponse = false;
-            std::function< void (std::shared_ptr<OC::OCResource>) > onDeviceDiscoveredCb =
-                    std::bind(&EnrolleeResource::onDeviceDiscovered, this,
-                                                    std::placeholders::_1);
-            OCStackResult result = OC::OCPlatform::findResource("", query, CT_DEFAULT,
-                    onDeviceDiscoveredCb);
+            OCStackResult result = getStatus();
 
             if (result != OCStackResult::OC_STACK_OK)
             {
-                OIC_LOG(ERROR,ES_REMOTE_ENROLLEE_RES_TAG,
-                        "Failed to create device using constructResourceObject");
-                return ES_ERROR;
-            }
+                EnrolleeStatus enrolleeStatus(rep);// = {ES_STATE_INIT, ES_ERRCODE_NO_ERROR};
+                std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus = std::make_shared<
+                        GetEnrolleeStatus >(ESResult::ES_ERROR, enrolleeStatus);
 
+                m_getStatusCb(getEnrolleeStatus);
 
-            ESResult foundResponse = ESDiscoveryTimeout (DISCOVERY_TIMEOUT);
-
-            if (foundResponse ==ES_ERROR || !m_discoveryResponse)
-            {
-                OIC_LOG(ERROR,ES_REMOTE_ENROLLEE_RES_TAG,
-                        "Failed to create device using constructResourceObject");
-                return ES_ERROR;
+                return;
             }
-
-            return ES_OK;
-#endif
+            OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getStatus OUT");
         }
 
-        void EnrolleeResource::getCapabilityData()
+        void EnrolleeResource::getConfiguration()
         {
+            OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getConfiguration IN");
+
             if (m_ocResource == nullptr)
             {
                 throw ESBadRequestException("Resource is not initialized");
@@ -521,79 +272,88 @@ namespace OIC
             OC::QueryParamsMap query;
             OC::OCRepresentation rep;
 
-            std::function< OCStackResult(void) > getCapabilityStatus = [&]
-            {   return m_ocResource->get(m_ocResource->getResourceTypes().at(0),
-                        BATCH_INTERFACE, query, std::function<void(const HeaderOptions& headerOptions,
-                        const OCRepresentation& rep, const int eCode) >(
-                                std::bind(&EnrolleeResource::getCapabilityResponse, this,
-                                        std::placeholders::_1, std::placeholders::_2,
-                                        std::placeholders::_3)));
+            std::function< OCStackResult(void) > getConfigurationStatus = [&]
+            {
+                ESEnrolleeResourceCb cb = std::bind(&EnrolleeResource::onEnrolleeResourceSafetyCB,
+                                std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
+                                static_cast<ESEnrolleeResourceCb>(
+                                std::bind(&EnrolleeResource::onGetConfigurationResponse, this,
+                                std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
+                                shared_from_this());
+
+                return m_ocResource->get(m_ocResource->getResourceTypes().at(0),
+                        BATCH_INTERFACE, query, cb, OC::QualityOfService::HighQos);
             };
 
-            OCStackResult result = getCapabilityStatus();
+            OCStackResult result = getConfigurationStatus();
 
             if (result != OCStackResult::OC_STACK_OK)
             {
-                CapabilityData capabilityData;
-                std::shared_ptr< RequestCapabilityStatus > requestCapabilityStatus = std::make_shared<
-                        RequestCapabilityStatus >(ESResult::ES_ERROR, capabilityData);
-                m_requestCapabilityStatusCb(requestCapabilityStatus);
+                EnrolleeConf enrolleeConf(rep);
+                std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
+                        GetConfigurationStatus >(ESResult::ES_ERROR, enrolleeConf);
+                m_getConfigurationStatusCb(getConfigurationStatus);
                 return;
             }
-        }
 
-        void EnrolleeResource::provisionEnrollee()
+            OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getConfiguration OUT");
+        }
 
+        void EnrolleeResource::provisionProperties(const DeviceProp& deviceProp)
         {
+            OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "provisionProperties IN");
             if (m_ocResource == nullptr)
             {
                 throw ESBadRequestException("Resource is not initialized");
             }
 
             OC::QueryParamsMap query;
-            OC::OCRepresentation rep;
+            OC::OCRepresentation provisioningRepresentation = deviceProp.toOCRepresentation();
 
-            std::function< OCStackResult(void) > getProvisioingStatus = [&]
-            {   return m_ocResource->get(m_ocResource->getResourceTypes().at(0),
-                        m_ocResource->getResourceInterfaces().at(0), query,
-                        std::function<
-                        void(const HeaderOptions& headerOptions, const OCRepresentation& rep,
-                                const int eCode) >(
-                                std::bind(&EnrolleeResource::getProvStatusResponse, this,
-                                        std::placeholders::_1, std::placeholders::_2,
-                                        std::placeholders::_3)));
-            };
+            ESEnrolleeResourceCb cb = std::bind(&EnrolleeResource::onEnrolleeResourceSafetyCB,
+                            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
+                            static_cast<ESEnrolleeResourceCb>(
+                            std::bind(&EnrolleeResource::onProvisioningResponse, this,
+                            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
+                            shared_from_this());
 
-            OCStackResult result = getProvisioingStatus();
+            m_ocResource->post(OC_RSRVD_ES_RES_TYPE_EASYSETUP, BATCH_INTERFACE,
+                    provisioningRepresentation, QueryParamsMap(), cb, OC::QualityOfService::HighQos);
 
-            if (result != OCStackResult::OC_STACK_OK)
-            {
-                std::shared_ptr< ProvisioningStatus > provStatus = std::make_shared<
-                        ProvisioningStatus >(ESResult::ES_ERROR, ESState::ES_PROVISIONING_ERROR);
-                m_dataProvStatusCb(provStatus);
-                return;
-            }
+            OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "provisionProperties OUT");
         }
 
-        void EnrolleeResource::unprovisionEnrollee()
+        void EnrolleeResource::requestToConnect(const std::vector<ES_CONNECT_TYPE> &connectTypes)
         {
+            OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "requestToConnect IN");
             if (m_ocResource == nullptr)
             {
                 throw ESBadRequestException("Resource is not initialized");
             }
 
-            OCRepresentation provisioningRepresentation;
+            OC::QueryParamsMap query;
+            OC::OCRepresentation requestRepresentation;
+            std::vector<int> connectTypes_int;
+            connectTypes_int.clear();
+
+            for(auto it : connectTypes)
+            {
+                connectTypes_int.push_back(static_cast<int>(it));
+            }
+
+            requestRepresentation.setValue<std::vector<int>>(OC_RSRVD_ES_CONNECT, connectTypes_int);
+
+            ESEnrolleeResourceCb cb = std::bind(&EnrolleeResource::onEnrolleeResourceSafetyCB,
+                            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
+                            static_cast<ESEnrolleeResourceCb>(
+                            std::bind(&EnrolleeResource::onConnectRequestResponse, this,
+                            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
+                            shared_from_this());
 
-            provisioningRepresentation.setValue(OC_RSRVD_ES_SSID, "");
-            provisioningRepresentation.setValue(OC_RSRVD_ES_CRED, "");
+            m_ocResource->post(OC_RSRVD_ES_RES_TYPE_EASYSETUP, OC_RSRVD_INTERFACE_DEFAULT,
+                    requestRepresentation, QueryParamsMap(), cb, OC::QualityOfService::HighQos);
 
-            m_ocResource->post(provisioningRepresentation, QueryParamsMap(),
-                    std::function<
-                            void(const HeaderOptions& headerOptions, const OCRepresentation& rep,
-                                    const int eCode) >(
-                    std::bind(&EnrolleeResource::checkProvInformationCb, this,
-                    std::placeholders::_1, std::placeholders::_2,
-                    std::placeholders::_3)));
+            OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "requestToConnect OUT");
         }
     }
 }