From: agrkush Date: Sun, 16 Dec 2018 15:11:40 +0000 (+0530) Subject: Added thread for security and cloud provisioning. X-Git-Tag: accepted/tizen/5.0/unified/20181219.063412~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e54440e2590fbeaaf001baf6de4510c04a3b6b02;p=platform%2Fupstream%2Fiotivity.git Added thread for security and cloud provisioning. 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 Signed-off-by: DoHyun Pyun --- diff --git a/service/easy-setup/mediator/richsdk/src/RemoteEnrollee.cpp b/service/easy-setup/mediator/richsdk/src/RemoteEnrollee.cpp old mode 100755 new mode 100644 index 7434b41..3274d30 --- a/service/easy-setup/mediator/richsdk/src/RemoteEnrollee.cpp +++ b/service/easy-setup/mediator/richsdk/src/RemoteEnrollee.cpp @@ -373,11 +373,29 @@ namespace OIC m_localEnrolleeSecurity = std::make_shared (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 (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(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 (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 &connectTypes, const ConnectRequestStatusCb callback) { OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_TAG, "connect IN");