Merge branch 'master' into extended-easysetup
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / src / RemoteEnrollee.cpp
index 22fd384..24c9705 100755 (executable)
@@ -37,16 +37,18 @@ namespace OIC
         #define ES_REMOTE_ENROLLEE_TAG "ES_REMOTE_ENROLLEE"
         #define DISCOVERY_TIMEOUT 5
 
-        RemoteEnrollee::RemoteEnrollee()
+        RemoteEnrollee::RemoteEnrollee(std::shared_ptr< OC::OCResource > resource)
         {
-            m_enrolleeSecStatusCb = nullptr;
-            m_RequestPropertyDataStatusCb = nullptr;
+            m_ocResource = resource;
+            m_enrolleeResource = std::make_shared<EnrolleeResource>(m_ocResource);
+            m_securityProvStatusCb = nullptr;
+            m_getConfigurationStatusCb = nullptr;
             m_securityPinCb = nullptr;
             m_secProvisioningDbPathCb = nullptr;
-            m_dataProvStatusCb = nullptr;
-            m_cloudProvStatusCb = nullptr;
+            m_devicePropProvStatusCb = nullptr;
+            m_cloudPropProvStatusCb = nullptr;
 
-            //m_deviceId = nullptr;
+            m_deviceId = resource->sid();
         }
 
 #ifdef __WITH_DTLS__
@@ -64,72 +66,60 @@ namespace OIC
         }
 #endif //__WITH_DTLS__
 
-        void RemoteEnrollee::easySetupSecurityStatusCallback(
+        void RemoteEnrollee::securityStatusHandler(
                         std::shared_ptr< SecProvisioningStatus > status)
         {
             OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_TAG, "easySetupStatusCallback status is, UUID = %s, "
                     "Status = %d", status->getDeviceUUID().c_str(),
-                    status->getResult());
+                    status->getESResult());
 
-            if(status->getResult() == ES_OK)
+            if(status->getESResult() == ES_OK)
             {
                 OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_TAG, "Ownership and ACL are successful. "
                         "Continue with Network information provisioning");
 
                 OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Before ProvisionEnrollee");
 
-                m_enrolleeSecStatusCb(status);
+                m_securityProvStatusCb(status);
             }
             else
             {
                 OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_TAG, "Ownership and ACL are fail");
 
-                m_enrolleeSecStatusCb(status);
+                m_securityProvStatusCb(status);
             }
         }
 
-        void RemoteEnrollee::RequestPropertyDataStatusHandler (
-                std::shared_ptr< RequestPropertyDataStatus > status)
+        void RemoteEnrollee::getConfigurationStatusHandler (
+                std::shared_ptr< GetConfigurationStatus > status)
         {
-            OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Entering RequestPropertyDataStatusHandler");
-
-            OIC_LOG_V(DEBUG,ES_REMOTE_ENROLLEE_TAG,"RequestPropertyDataStatus = %d", status->getESResult());
+            OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Entering getConfigurationStatusHandler");
 
-            if(status->getESResult() == ES_OK)
-            {
-                m_propertyData = status->getPropertyData();
-            }
+            OIC_LOG_V(DEBUG,ES_REMOTE_ENROLLEE_TAG,"GetConfigurationStatus = %d", status->getESResult());
 
-            m_RequestPropertyDataStatusCb(status);
+            m_getConfigurationStatusCb(status);
         }
 
-        void RemoteEnrollee::dataProvisioningStatusHandler(
-                std::shared_ptr< DataProvisioningStatus > status)
+        void RemoteEnrollee::devicePropProvisioningStatusHandler(
+                std::shared_ptr< DevicePropProvisioningStatus > status)
         {
-            OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Entering dataProvisioningStatusHandler");
+            OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Entering DevicePropProvisioningStatusHandler");
 
             OIC_LOG_V(DEBUG,ES_REMOTE_ENROLLEE_TAG,"ProvStatus = %d", status->getESResult());
 
-            if (status->getESResult() == ES_OK)
-            {
-                if (status->getESState() >= ESState::ES_PROVISIONED_ALREADY)
-                {
-                    OIC_LOG_V(DEBUG,ES_REMOTE_ENROLLEE_TAG,"ProvStatus = %d", status->getESResult());
-                }
-            }
-            m_dataProvStatusCb(status);
+            m_devicePropProvStatusCb(status);
 
             return;
         }
 
-        void RemoteEnrollee::cloudProvisioningStatusHandler (
-                std::shared_ptr< CloudProvisioningStatus > status)
+        void RemoteEnrollee::cloudPropProvisioningStatusHandler (
+                std::shared_ptr< CloudPropProvisioningStatus > status)
         {
-            OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Entering cloudProvisioningStatusHandler");
+            OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Entering cloudPropProvisioningStatusHandler");
 
             OIC_LOG_V(DEBUG,ES_REMOTE_ENROLLEE_TAG,"CloudProvStatus = %d", status->getESCloudState());
 
-            m_cloudProvStatusCb(status);
+            m_cloudPropProvStatusCb(status);
             return;
         }
 
@@ -186,42 +176,33 @@ namespace OIC
             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_TAG,
-                            "URI of the resource: %s", resourceURI.c_str());
-
-                    // Get the resource host address
-                    hostAddress = resource->host();
-                    OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG,
-                            "Host address of the resource: %s", hostAddress.c_str());
-
-                    hostDeviceID = resource->sid();
-                    OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG,
-                            "Host DeviceID of the resource: %s", hostDeviceID.c_str());
-
-                    if(m_deviceId .empty())
+                    if(!(resource->connectivityType() & CT_ADAPTER_TCP))
                     {
-                        /*
-                         * 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_deviceId = resource->sid();
-                        m_discoveryResponse = true;
-                    }
-
-                    else if(!m_deviceId.empty() && m_deviceId == hostDeviceID)
-                    {
-                        OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_TAG, "Find matched CloudResource");
-                        m_ocResource = resource;
-                        m_discoveryResponse = true;
+                        // Get the resource URI
+                        resourceURI = resource->uri();
+                        OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG,
+                                "URI of the resource: %s", resourceURI.c_str());
+
+                        // Get the resource host address
+                        hostAddress = resource->host();
+                        OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG,
+                                "Host address of the resource: %s", hostAddress.c_str());
+
+                        hostDeviceID = resource->sid();
+                        OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG,
+                                "Host DeviceID of the resource: %s", hostDeviceID.c_str());
+
+                        if(!m_deviceId.empty() && m_deviceId == hostDeviceID)
+                        {
+                            OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_TAG, "Find matched CloudResource");
+                            m_ocResource = resource;
+                            m_discoveryResponse = true;
+                        }
                     }
                 }
             }
@@ -236,15 +217,10 @@ namespace OIC
         {
             OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_TAG, "Enter discoverResource");
 
-            if (m_ocResource != nullptr)
-            {
-                throw ESBadRequestException("resource is already created");
-            }
-
             std::string query("");
             query.append(ES_BASE_RES_URI);
             query.append("?rt=");
-            query.append(OC_RSRVD_ES_PROV_RES_TYPE);
+            query.append(OC_RSRVD_ES_RES_TYPE_PROV);
 
             OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "query = %s", query.c_str());
 
@@ -274,68 +250,33 @@ namespace OIC
             return ES_OK;
         }
 
-        void RemoteEnrollee::initRemoteEnrollee()
-        {
-            ESResult result = ES_ERROR;
-
-            if (m_enrolleeResource != nullptr)
-            {
-                throw ESBadRequestException ("Already created");
-            }
-
-            result = discoverResource();
-
-            if (result == ES_ERROR)
-            {
-                OIC_LOG(ERROR,ES_REMOTE_ENROLLEE_TAG,
-                                    "Failed to create resource object using discoverResource");
-                throw ESBadRequestException ("Resource object not created");
-            }
-
-            else
-            {
-                if(m_ocResource != nullptr)
-                {
-                    m_enrolleeResource = std::make_shared<EnrolleeResource>(std::move(m_ocResource));
-                }
-                else
-                {
-                    throw ESBadGetException ("Resource handle is invalid");
-                }
-            }
-        }
-
-        void RemoteEnrollee::startSecurityProvisioning(EnrolleeSecStatusCb callback)
+        void RemoteEnrollee::provisionSecurity(SecurityProvStatusCb callback)
         {
 #ifdef __WITH_DTLS__
+            m_securityProvStatusCb = callback;
 
-            m_enrolleeSecStatusCb = callback;
-
-            EnrolleeSecStatusCb securityProvStatusCb = std::bind(
-                    &RemoteEnrollee::easySetupSecurityStatusCallback,
+            SecurityProvStatusCb securityProvStatusCb = std::bind(
+                    &RemoteEnrollee::securityStatusHandler,
                     this,
                     std::placeholders::_1);
             //TODO : DBPath is passed empty as of now. Need to take dbpath from application.
-            m_enrolleeSecurity = std::make_shared <EnrolleeSecurity> (m_enrolleeResource, "");
+            m_enrolleeSecurity = std::make_shared <EnrolleeSecurity> (m_ocResource, "");
 
-            m_enrolleeSecurity->registerCallbackHandler(securityProvStatusCb,
-                    m_securityPinCb, m_secProvisioningDbPathCb);
+            m_enrolleeSecurity->registerCallbackHandler(securityProvStatusCb, m_securityPinCb, m_secProvisioningDbPathCb);
 
             try
             {
-                if (!m_enrolleeSecurity->performOwnershipTransfer())
-                {
-                    OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Fail performOwnershipTransfer");
-                    std::shared_ptr< SecProvisioningStatus > securityProvisioningStatus =
-                            std::make_shared< SecProvisioningStatus >(nullptr, ES_ERROR);
-                    m_enrolleeSecStatusCb(securityProvisioningStatus);
-                    return;
-                }
+                m_enrolleeSecurity->performOwnershipTransfer();
             }
             catch (OCException & e)
             {
                 OIC_LOG_V(ERROR, ES_REMOTE_ENROLLEE_TAG,
                         "Exception for performOwnershipTransfer : %s", e.reason().c_str());
+
+                OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Fail performOwnershipTransfer");
+                    std::shared_ptr< SecProvisioningStatus > securityProvisioningStatus =
+                            std::make_shared< SecProvisioningStatus >(nullptr, ES_ERROR);
+                    m_securityProvStatusCb(securityProvisioningStatus);
                 return ;
             }
 #else
@@ -343,58 +284,56 @@ namespace OIC
 
             std::shared_ptr< SecProvisioningStatus > securityProvisioningStatus =
                      std::make_shared< SecProvisioningStatus >(nullptr, ES_ERROR);
-            m_enrolleeSecStatusCb(securityProvisioningStatus);
+            m_securityProvStatusCb(securityProvisioningStatus);
 #endif
         }
 
-        void RemoteEnrollee::requestPropertyData(RequestPropertyDataStatusCb callback)
+        void RemoteEnrollee::getConfiguration(GetConfigurationStatusCb callback)
         {
             if(!callback)
             {
                 throw ESInvalidParameterException("Callback is empty");
             }
 
-            m_RequestPropertyDataStatusCb = callback;
+            m_getConfigurationStatusCb = callback;
 
             if (m_enrolleeResource == nullptr)
             {
                 throw ESBadRequestException ("Device not created");
             }
 
-            RequestPropertyDataStatusCb RequestPropertyDataStatusCb = std::bind(
-                    &RemoteEnrollee::RequestPropertyDataStatusHandler, this, std::placeholders::_1);
-            m_enrolleeResource->registerRequestPropertyDataStatusCallback(RequestPropertyDataStatusCb);
-            m_enrolleeResource->RequestPropertyData();
+            GetConfigurationStatusCb getConfigurationStatusCb = std::bind(
+                    &RemoteEnrollee::getConfigurationStatusHandler, this, std::placeholders::_1);
+            m_enrolleeResource->registerGetConfigurationStatusCallback(getConfigurationStatusCb);
+            m_enrolleeResource->getConfiguration();
         }
 
-        void RemoteEnrollee::startDataProvisioning(DataProvStatusCb callback)
+        void RemoteEnrollee::provisionDeviceProperties(const DeviceProp& devProp, DevicePropProvStatusCb callback)
         {
-            OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Enter startDataProvisioning");
+            OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Enter provisionDeviceProperties");
 
             if(!callback)
             {
                 throw ESInvalidParameterException("Callback is empty");
             }
 
-            m_dataProvStatusCb = callback;
+            m_devicePropProvStatusCb = callback;
 
             if (m_enrolleeResource == nullptr)
             {
                 throw ESBadRequestException ("Device not created");
             }
 
-            if(m_dataProvInfo.WIFI.ssid.empty())
+            if(devProp.WIFI.ssid.empty())
             {
                 throw ESBadRequestException ("Invalid Provisiong Data.");
             }
 
-            m_dataProvStatusCb = callback;
-
-            DataProvStatusCb dataProvStatusCb = std::bind(
-                    &RemoteEnrollee::dataProvisioningStatusHandler, this, std::placeholders::_1);
+            DevicePropProvStatusCb devicePropProvStatusCb = std::bind(
+                    &RemoteEnrollee::devicePropProvisioningStatusHandler, this, std::placeholders::_1);
 
-            m_enrolleeResource->registerProvStatusCallback(dataProvStatusCb);
-            m_enrolleeResource->provisionEnrollee(m_dataProvInfo);
+            m_enrolleeResource->registerDevicePropProvStatusCallback(devicePropProvStatusCb);
+            m_enrolleeResource->provisionEnrollee(devProp);
         }
 
         void RemoteEnrollee::initCloudResource()
@@ -421,9 +360,10 @@ namespace OIC
                 {
                     m_cloudResource = std::make_shared<CloudResource>(std::move(m_ocResource));
 
-                    std::shared_ptr< CloudProvisioningStatus > provStatus = std::make_shared<
-                        CloudProvisioningStatus >(ESResult::ES_ERROR, ESCloudProvState::ES_ENROLLEE_FOUND);
-                    m_cloudProvStatusCb(provStatus);
+                    std::shared_ptr< CloudPropProvisioningStatus > provStatus = std::make_shared<
+                        CloudPropProvisioningStatus >(ESResult::ES_OK, ESCloudProvState::ES_CLOUD_ENROLLEE_FOUND);
+
+                    m_cloudPropProvStatusCb(provStatus);
                 }
                 else
                 {
@@ -433,9 +373,9 @@ namespace OIC
         }
 
 
-        void RemoteEnrollee::startCloudProvisioning(CloudProvStatusCb callback)
+        void RemoteEnrollee::provisionCloudProperties(const CloudProp& cloudProp, CloudPropProvStatusCb callback)
         {
-            OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Enter startCloudProvisioning");
+            OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Enter provisionCloudProperties");
 
             ESResult result = ES_ERROR;
 
@@ -444,7 +384,14 @@ namespace OIC
                 throw ESInvalidParameterException("Callback is empty");
             }
 
-            m_cloudProvStatusCb = callback;
+            m_cloudPropProvStatusCb = callback;
+
+            if(cloudProp.authCode.empty()
+                || cloudProp.authProvider.empty()
+                || cloudProp.ciServer.empty())
+            {
+                throw ESBadRequestException ("Invalid Cloud Provisiong Info.");
+            }
 
             try
             {
@@ -454,11 +401,11 @@ namespace OIC
             catch (const std::exception& e)
             {
                 OIC_LOG_V(ERROR, ES_REMOTE_ENROLLEE_TAG,
-                    "Exception caught in startCloudProvisioning = %s", e.what());
+                    "Exception caught in provisionCloudProperties = %s", e.what());
 
-                std::shared_ptr< CloudProvisioningStatus > provStatus = std::make_shared<
-                        CloudProvisioningStatus >(ESResult::ES_ERROR, ESCloudProvState::ES_ENROLLEE_NOT_FOUND);
-                m_cloudProvStatusCb(provStatus);
+                std::shared_ptr< CloudPropProvisioningStatus > provStatus = std::make_shared<
+                        CloudPropProvisioningStatus >(ESResult::ES_ERROR, ESCloudProvState::ES_CLOUD_ENROLLEE_NOT_FOUND);
+                m_cloudPropProvStatusCb(provStatus);
             }
 
             if (m_cloudResource == nullptr)
@@ -466,33 +413,11 @@ namespace OIC
                 throw ESBadRequestException ("Cloud Resource not created");
             }
 
-            if(m_cloudProvInfo.authCode.empty()
-                || m_cloudProvInfo.authProvider.empty()
-                || m_cloudProvInfo.ciServer.empty())
-            {
-                throw ESBadRequestException ("Invalid Cloud Provisiong Info.");
-            }
-
-            CloudProvStatusCb cloudProvStatusCb = std::bind(
-                    &RemoteEnrollee::cloudProvisioningStatusHandler, this, std::placeholders::_1);
+            CloudPropProvStatusCb cloudPropProvStatusCb = std::bind(
+                    &RemoteEnrollee::cloudPropProvisioningStatusHandler, this, std::placeholders::_1);
 
-            m_cloudResource->registerCloudProvisioningStatusCallback(cloudProvStatusCb);
-            m_cloudResource->provisionEnrollee(m_cloudProvInfo);
-        }
-
-        void RemoteEnrollee::setDataProvInfo(const DataProvInfo& dataProvInfo)
-        {
-            m_dataProvInfo = dataProvInfo;
-        }
-
-        void RemoteEnrollee::setCloudProvInfo(const CloudProvInfo& cloudProvInfo)
-        {
-            m_cloudProvInfo = cloudProvInfo;
-        }
-
-        DataProvInfo RemoteEnrollee::getDataProvInfo()
-        {
-            return m_dataProvInfo;
+            m_cloudResource->registerCloudPropProvisioningStatusCallback(cloudPropProvStatusCb);
+            m_cloudResource->provisionEnrollee(cloudProp);
         }
     }
 }