Added thread for security and cloud provisioning. 42/195842/1
authoragrkush <kush.agrawal@samsung.com>
Sun, 16 Dec 2018 15:11:40 +0000 (20:41 +0530)
committerDoHyun Pyun <dh79.pyun@samsung.com>
Wed, 19 Dec 2018 00:49:47 +0000 (09:49 +0900)
Executing cloud resource discovery and secure resource discovery in
different thread to avoid blocking of main event loop of Tizen

https://github.sec.samsung.net/RS7-IOTIVITY/IoTivity/pull/357
(cherry picked from commit f898d3e2f59594865e6e7b0da6811b4ead5bb4d5)

Change-Id: I4263302ee004ccbcbb27b1092c393e870099b3db
Signed-off-by: agrkush <kush.agrawal@samsung.com>
Signed-off-by: DoHyun Pyun <dh79.pyun@samsung.com>
service/easy-setup/mediator/richsdk/src/RemoteEnrollee.cpp [changed mode: 0755->0644]

old mode 100755 (executable)
new mode 100644 (file)
index 7434b41..3274d30
@@ -373,11 +373,29 @@ namespace OIC
                 m_localEnrolleeSecurity = std::make_shared <EnrolleeSecurity> (m_ocResource);
             }
 
+#ifdef __TIZEN__
+            /* NOTE: Execute cloud resource discovery and secure resource discovery in
+                different thread to avoid blocking of main event loop of Tizen */
+            std::thread secProvThread = std::thread([this, securityProvStatusCb](void)
+                {
+                    OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG,"provisionSecurity - thread IN");
+                    ESResult res = m_localEnrolleeSecurity->provisionOwnership(NULL);
+
+                    std::shared_ptr< SecProvisioningStatus > securityProvisioningStatus =
+                                    std::make_shared< SecProvisioningStatus >(m_localEnrolleeSecurity->getUUID(), res);
+                    securityProvStatusCb(securityProvisioningStatus);
+                    OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG,"provisionSecurity - thread OUT");
+                });
+
+            // TODO: Join thread
+            secProvThread.detach();
+#else
             res = m_localEnrolleeSecurity->provisionOwnership(NULL);
 
             std::shared_ptr< SecProvisioningStatus > securityProvisioningStatus =
                             std::make_shared< SecProvisioningStatus >(m_localEnrolleeSecurity->getUUID(), res);
             securityProvStatusCb(securityProvisioningStatus);
+#endif
 #else
             OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG,"Mediator is unsecured built.");
 
@@ -413,12 +431,29 @@ namespace OIC
             {
                 m_localEnrolleeSecurity = std::make_shared <EnrolleeSecurity> (m_ocResource);
             }
+#ifdef __TIZEN__
+            /* NOTE: Execute cloud resource discovery and secure resource discovery in
+                different thread to avoid blocking of main event loop of Tizen */
+            std::thread secProvThread = std::thread([this, securityProvStatusCbWithOption](void)
+                {
+                    OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG,"provisionSecurity - thread IN");
+                    ESResult res = m_localEnrolleeSecurity->provisionOwnership(securityProvStatusCbWithOption);
 
+                    std::shared_ptr< SecProvisioningStatus > securityProvisioningStatus =
+                                    std::make_shared< SecProvisioningStatus >(m_localEnrolleeSecurity->getUUID(), res);
+                    securityProvStatusCbWithOption(securityProvisioningStatus);
+                    OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG,"provisionSecurity - thread OUT");
+                });
+
+            // TODO: Join thread
+            secProvThread.detach();
+#else
             res = m_localEnrolleeSecurity->provisionOwnership(securityProvStatusCbWithOption);
 
             std::shared_ptr< SecProvisioningStatus > securityProvisioningStatus =
                             std::make_shared< SecProvisioningStatus >(m_localEnrolleeSecurity->getUUID(), res);
             securityProvStatusCbWithOption(securityProvisioningStatus);
+#endif
 #else
             OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG,"Mediator is unsecured built.");
 
@@ -556,6 +591,7 @@ namespace OIC
             OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "provisionCloudProperties w/o OCResource OUT");
         }
 
+#ifndef __TIZEN__
         void RemoteEnrollee::provisionCloudProperties(const std::shared_ptr< OC::OCResource > resource,
                                                         const CloudProp& cloudProp,
                                                         const CloudPropProvStatusCb callback)
@@ -672,7 +708,140 @@ namespace OIC
 
             OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "provisionCloudProperties OUT");
         }
+#else
+        void RemoteEnrollee::provisionCloudProperties(const std::shared_ptr< OC::OCResource > resource,
+                                                        const CloudProp& cloudProp,
+                                                        const CloudPropProvStatusCb callback)
+        {
+            OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "provisionCloudProperties IN");
+
+            if(!callback)
+            {
+                throw ESInvalidParameterException("Callback is empty");
+            }
+
+            m_cloudPropProvStatusCb = callback;
+
+            if((cloudProp.getAuthCode().empty() && cloudProp.getAccessToken().empty()) ||
+                cloudProp.getAuthProvider().empty() ||
+                cloudProp.getCiServer().empty())
+            {
+                throw ESBadRequestException ("Invalid Cloud Provisiong Info.");
+            }
+
+            if(resource)
+            {
+                if(resource->getResourceTypes().at(0) != OC_RSRVD_ES_RES_TYPE_EASYSETUP ||
+                                resource->connectivityType() & CT_ADAPTER_TCP)
+                {
+                    OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_TAG, "Given resource is not valid due to wrong rt or conntype");
+                    throw ESInvalidParameterException("A given OCResource is wrong");
+                }
+
+                auto interfaces = resource->getResourceInterfaces();
+                bool isFound = false;
+                for(auto interface : interfaces)
+                {
+                    if(interface.compare(BATCH_INTERFACE) == 0)
+                    {
+                        OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "RemoteEnrollee object is succeessfully created");
+                        OIC_LOG_V(INFO_PRIVATE, ES_REMOTE_ENROLLEE_TAG, "HOST: %s", resource->host().c_str());
+                        OIC_LOG_V(INFO_PRIVATE, ES_REMOTE_ENROLLEE_TAG, "URI: %s", resource->uri().c_str());
+                        OIC_LOG_V(INFO_PRIVATE, ES_REMOTE_ENROLLEE_TAG, "SID: %s", resource->sid().c_str());
+                        OIC_LOG_V(INFO_PRIVATE, ES_REMOTE_ENROLLEE_TAG, "CONNECTIVITY: %d", resource->connectivityType());
+                        isFound = true;
+                    }
+                }
 
+                if(!isFound)
+                {
+                    throw ESInvalidParameterException("A given OCResource has no batch interface");
+                }
+
+                OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "Skip to find a provisioning resource");
+                m_ocResource = resource;
+                m_cloudResource = std::make_shared<CloudResource>(m_ocResource);
+                if (m_cloudResource == nullptr)
+                {
+                    throw ESBadRequestException ("Cloud Resource not created");
+                }
+            }
+
+            /* NOTE: Execute cloud resource discovery and secure resource discovery in
+                different thread to avoid blocking of main event loop of Tizen */
+            std::thread cloudProvThread = std::thread([this, cloudProp]()
+                {
+                    OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "provisionCloudProperties - thread IN");
+                    try
+                    {
+                        // Discover easy-setup resource only if it is not provided by API caller
+                        if(!m_cloudResource)
+                        {
+                            initCloudResource();
+                            if (m_cloudResource == nullptr)
+                            {
+                                OIC_LOG_V(ERROR, ES_REMOTE_ENROLLEE_TAG, "Cloud resource is not created");
+                                std::shared_ptr< CloudPropProvisioningStatus > provStatus = std::make_shared<
+                                        CloudPropProvisioningStatus >(ESResult::ES_ENROLLEE_DISCOVERY_FAILURE);
+                                m_cloudPropProvStatusCb(provStatus);
+                                return;
+                            }
+                        }
+                    }
+                    catch (const std::exception& e)
+                    {
+                        OIC_LOG_V(ERROR, ES_REMOTE_ENROLLEE_TAG,
+                            "Exception caught in provisionCloudProperties = %s", e.what());
+
+                        std::shared_ptr< CloudPropProvisioningStatus > provStatus = std::make_shared<
+                                CloudPropProvisioningStatus >(ESResult::ES_ENROLLEE_DISCOVERY_FAILURE);
+                        m_cloudPropProvStatusCb(provStatus);
+                        return;
+                    }
+
+#if defined(__WITH_DTLS__) && defined(__WITH_TLS__)
+                    if(!(cloudProp.getCloudID().empty() && cloudProp.getCredID() <= 0))
+                    {
+                        ESResult res = ESResult::ES_ERROR;
+                        if(!m_cloudEnrolleeSecurity.get())
+                        {
+                            m_cloudEnrolleeSecurity = std::make_shared <EnrolleeSecurity> (m_ocResource);
+                        }
+
+
+                        res = m_cloudEnrolleeSecurity->provisionSecurityForCloudServer(cloudProp.getCloudID(),
+                                                                                  cloudProp.getCredID());
+
+                        if(res != ESResult::ES_OK)
+                        {
+                            m_cloudResource = nullptr;
+                            std::shared_ptr< CloudPropProvisioningStatus > provStatus = std::make_shared<
+                                    CloudPropProvisioningStatus >(res);
+                            m_cloudPropProvStatusCb(provStatus);
+                            return;
+                        }
+                    }
+                    else
+                    {
+                        OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "ACL and Cert. provisioning are skipped.");
+                    }
+#endif //defined(__WITH_DTLS__) && defined(__WITH_TLS__)
+
+                    CloudPropProvStatusCb cloudPropProvStatusCb = std::bind(
+                            &RemoteEnrollee::onCloudPropProvisioningStatusHandlerCallback,
+                            std::placeholders::_1,
+                            shared_from_this());
+
+                    m_cloudResource->registerCloudPropProvisioningStatusCallback(cloudPropProvStatusCb);
+                    m_cloudResource->provisionProperties(cloudProp);
+                    OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "provisionCloudProperties - thread OUT");
+                });
+
+            // TODO: Join thread
+            cloudProvThread.detach();
+            OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "provisionCloudProperties OUT");
+        }
+#endif
         void RemoteEnrollee::requestToConnect(const std::vector<ES_CONNECT_TYPE> &connectTypes, const ConnectRequestStatusCb callback)
         {
             OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_TAG, "connect IN");