1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "RemoteEnrollee.h"
22 #include "EnrolleeResource.h"
23 #include "CloudResource.h"
24 #include "OCPlatform.h"
25 #include "ESException.h"
27 #include "OCResource.h"
28 #include "oic_string.h"
30 #include "EnrolleeSecurity.h"
32 #include "oic_malloc.h"
40 static const char ES_BASE_RES_URI[] = "/oic/res";
41 #define ES_REMOTE_ENROLLEE_TAG "ES_REMOTE_ENROLLEE"
42 #define DISCOVERY_TIMEOUT 1
44 RemoteEnrollee::RemoteEnrollee(const std::shared_ptr< OC::OCResource > resource)
46 m_ocResource = resource;
47 m_enrolleeResource = std::make_shared<EnrolleeResource>(m_ocResource);
48 m_securityProvStatusCb = nullptr;
49 m_getConfigurationStatusCb = nullptr;
50 m_securityPinCb = nullptr;
51 m_secProvisioningDbPathCb = nullptr;
52 m_devicePropProvStatusCb = nullptr;
53 m_cloudPropProvStatusCb = nullptr;
54 m_connectRequestStatusCb = nullptr;
56 m_deviceId = resource->sid();
59 void RemoteEnrollee::onSecurityStatusHandlerCallback(
60 const std::shared_ptr< SecProvisioningStatus > status,
61 std::weak_ptr<RemoteEnrollee> this_ptr)
63 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG,"onSecurityStatusHandlerCallback");
64 std::shared_ptr<RemoteEnrollee> Ptr = this_ptr.lock();
67 Ptr->securityStatusHandler(status);
71 void RemoteEnrollee::securityStatusHandler(
72 const std::shared_ptr< SecProvisioningStatus > status) const
74 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "securityStatusHandlr IN");
75 OIC_LOG_V(INFO_PRIVATE, ES_REMOTE_ENROLLEE_TAG, "UUID = %s", status->getDeviceUUID().c_str());
76 OIC_LOG_V(INFO, ES_REMOTE_ENROLLEE_TAG, "ESResult = %d", status->getESResult());
78 if(status->getESResult() == ES_OK)
80 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "Ownership transfer is successfully done.");
81 m_securityProvStatusCb(status);
85 OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_TAG, "Ownership transfer is failed.");
86 m_securityProvStatusCb(status);
88 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "securityStatusHandlr OUT");
91 ESOwnershipTransferData RemoteEnrollee::onSecurityStatusWithOptionHandlerCallback(
92 const std::shared_ptr< SecProvisioningStatus > status,
93 std::weak_ptr<RemoteEnrollee> this_ptr)
95 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG,"onSecurityStatusWithOptionHandlerCallback");
96 std::shared_ptr<RemoteEnrollee> Ptr = this_ptr.lock();
99 return Ptr->securityStatusWithOptionHandler(status);
101 return ESOwnershipTransferData();
104 ESOwnershipTransferData RemoteEnrollee::securityStatusWithOptionHandler(
105 const std::shared_ptr< SecProvisioningStatus > status) const
107 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "securityStatusWithOptionHandler IN");
108 OIC_LOG_V(INFO_PRIVATE, ES_REMOTE_ENROLLEE_TAG, "UUID = %s", status->getDeviceUUID().c_str());
109 OIC_LOG_V(INFO, ES_REMOTE_ENROLLEE_TAG, "ESResult = %d", status->getESResult());
111 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "securityStatusWithOptionHandler OUT");
112 return m_securityProvStatusCbWithOption(status);
115 void RemoteEnrollee::onGetStatusHandlerCallback(
116 const std::shared_ptr< GetEnrolleeStatus > status,
117 std::weak_ptr<RemoteEnrollee> this_ptr)
119 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG,"onGetStatusHandlerCallback");
120 std::shared_ptr<RemoteEnrollee> Ptr = this_ptr.lock();
123 Ptr->getStatusHandler(status);
127 void RemoteEnrollee::getStatusHandler(
128 const std::shared_ptr< GetEnrolleeStatus > status) const
130 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "getStatusHandler IN");
132 OIC_LOG_V(INFO, ES_REMOTE_ENROLLEE_TAG, "getStatusHandler = %d",
133 status->getESResult());
134 m_getStatusCb(status);
136 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "getStatusHandler OUT");
139 void RemoteEnrollee::onGetConfigurationStatusHandlerCallback(
140 const std::shared_ptr< GetConfigurationStatus > status,
141 std::weak_ptr<RemoteEnrollee> this_ptr)
143 OIC_LOG(INFO,ES_REMOTE_ENROLLEE_TAG,"onGetConfigurationStatusHandlerCallback");
144 std::shared_ptr<RemoteEnrollee> Ptr = this_ptr.lock();
147 Ptr->getConfigurationStatusHandler(status);
151 void RemoteEnrollee::getConfigurationStatusHandler (
152 const std::shared_ptr< GetConfigurationStatus > status) const
154 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "getConfigurationStatusHandler IN");
156 OIC_LOG_V(INFO, ES_REMOTE_ENROLLEE_TAG,"GetConfigurationStatus = %d",
157 status->getESResult());
158 m_getConfigurationStatusCb(status);
160 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "getConfigurationStatusHandler OUT");
163 void RemoteEnrollee::onDevicePropProvisioningStatusHandlerCallback(
164 const std::shared_ptr< DevicePropProvisioningStatus > status,
165 std::weak_ptr<RemoteEnrollee> this_ptr)
167 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG,"onDevicePropProvisioningStatusHandlerCallback");
168 std::shared_ptr<RemoteEnrollee> Ptr = this_ptr.lock();
171 Ptr->devicePropProvisioningStatusHandler(status);
175 void RemoteEnrollee::devicePropProvisioningStatusHandler(
176 const std::shared_ptr< DevicePropProvisioningStatus > status) const
178 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "devicePropProvisioningStatusHandler IN");
180 OIC_LOG_V(INFO, ES_REMOTE_ENROLLEE_TAG, "DeviceProvStatus = %d", status->getESResult());
181 m_devicePropProvStatusCb(status);
183 if( m_ocResource.get() != nullptr &&
184 !(m_ocResource->connectivityType() & CT_ADAPTER_GATT_BTLE) &&
185 ES_OK == status->getESResult() )
187 // NOTE: Temporary patch
188 CAEndpoint_t endpoint = {
189 .adapter = CA_ADAPTER_IP,
190 .flags = CA_DEFAULT_FLAGS,
195 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
199 OCDevAddr address = m_ocResource->getDevAddr();
200 OICStrcpy(endpoint.addr, MAX_ADDR_STR_SIZE, address.addr);
201 endpoint.port = address.port;
203 OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_TAG, "HOST = %s", endpoint.addr);
204 OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_TAG, "PORT = %u", endpoint.port);
205 CAcloseSslSession(&endpoint);
208 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "devicePropProvisioningStatusHandler OUT");
211 void RemoteEnrollee::onCloudPropProvisioningStatusHandlerCallback(
212 const std::shared_ptr< CloudPropProvisioningStatus > status,
213 std::weak_ptr<RemoteEnrollee> this_ptr)
215 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG,"onCloudPropProvisioningStatusHandlerCallback");
216 std::shared_ptr<RemoteEnrollee> Ptr = this_ptr.lock();
219 Ptr->cloudPropProvisioningStatusHandler(status);
223 void RemoteEnrollee::cloudPropProvisioningStatusHandler (
224 const std::shared_ptr< CloudPropProvisioningStatus > status) const
226 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "cloudPropProvisioningStatusHandler IN");
228 OIC_LOG_V(INFO,ES_REMOTE_ENROLLEE_TAG,"CloudProvStatus = %d",
229 status->getESResult());
230 m_cloudPropProvStatusCb(status);
232 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "cloudPropProvisioningStatusHandler OUT");
235 void RemoteEnrollee::onConnectRequestStatusHandlerCallback(
236 const std::shared_ptr< ConnectRequestStatus > status,
237 std::weak_ptr<RemoteEnrollee> this_ptr)
239 OIC_LOG_V(DEBUG,ES_REMOTE_ENROLLEE_TAG,"onConnectRequestStatusHandlerCallback");
240 std::shared_ptr<RemoteEnrollee> Ptr = this_ptr.lock();
243 Ptr->connectRequestStatusHandler(status);
247 void RemoteEnrollee::connectRequestStatusHandler(
248 const std::shared_ptr< ConnectRequestStatus > status) const
250 OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_TAG, "connectRequestStatusHandler IN");
252 OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_TAG, "RequestConnectStatus = %d", status->getESResult());
253 m_connectRequestStatusCb(status);
255 OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_TAG, "connectRequestStatusHandler OUT");
258 void RemoteEnrollee::onDiscoveredCallback(const std::shared_ptr<OC::OCResource> resource,
259 std::weak_ptr<RemoteEnrollee> this_ptr)
261 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG,"onDiscoveredCallback()");
262 std::shared_ptr<RemoteEnrollee> Ptr = this_ptr.lock();
265 Ptr->onDeviceDiscovered(resource);
269 void RemoteEnrollee::onDeviceDiscovered(std::shared_ptr<OC::OCResource> resource)
271 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "onDeviceDiscovered IN");
277 if(!(resource->connectivityType() & CT_ADAPTER_TCP))
279 std::string resourceURI;
280 std::string hostAddress;
281 std::string hostDeviceID;
283 // Get the resource URI
284 resourceURI = resource->uri();
285 OIC_LOG_V(INFO_PRIVATE, ES_REMOTE_ENROLLEE_TAG,
286 "URI of the resource: %s", resourceURI.c_str());
288 // Get the resource host address
289 hostAddress = resource->host();
290 OIC_LOG_V(INFO_PRIVATE, ES_REMOTE_ENROLLEE_TAG,
291 "Host address of the resource: %s", hostAddress.c_str());
293 hostDeviceID = resource->sid();
294 OIC_LOG_V(INFO_PRIVATE, ES_REMOTE_ENROLLEE_TAG,
295 "Host DeviceID of the resource: %s", hostDeviceID.c_str());
297 if(!m_deviceId.empty() && m_deviceId == hostDeviceID)
299 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "Find matched resource for cloud provisioning");
300 m_ocResource = resource;
301 m_discoveryResponse = true;
307 catch(std::exception& e)
309 OIC_LOG_V(INFO, ES_REMOTE_ENROLLEE_TAG,
310 "Exception in foundResource: %s", e.what());
313 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "onDeviceDiscovered OUT");
316 ESResult RemoteEnrollee::discoverResource()
318 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "discoverResource IN");
320 std::string query("");
321 query.append(ES_BASE_RES_URI);
322 query.append("?rt=");
323 query.append(OC_RSRVD_ES_RES_TYPE_EASYSETUP);
325 OIC_LOG_V(INFO, ES_REMOTE_ENROLLEE_TAG, "query = %s", query.c_str());
327 m_discoveryResponse = false;
329 onDeviceDiscoveredCb cb = std::bind(&RemoteEnrollee::onDiscoveredCallback,
330 std::placeholders::_1,
333 OCStackResult result = OC::OCPlatform::findResource("", query, CT_DEFAULT, cb);
335 if (result != OCStackResult::OC_STACK_OK)
337 OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_TAG,
338 "Failed discoverResource");
342 std::unique_lock<std::mutex> lck(m_discoverymtx);
343 m_cond.wait_for(lck, std::chrono::seconds(DISCOVERY_TIMEOUT));
345 if (!m_discoveryResponse)
347 OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_TAG,
348 "Failed discoverResource because timeout");
355 void RemoteEnrollee::provisionSecurity(const SecurityProvStatusCb callback)
357 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "provisionSecurity IN");
359 ESResult res = ESResult::ES_ERROR;
362 throw ESInvalidParameterException("Callback is empty");
364 m_securityProvStatusCb = callback;
366 SecurityProvStatusCb securityProvStatusCb = std::bind(
367 &RemoteEnrollee::onSecurityStatusHandlerCallback,
368 std::placeholders::_1,
370 //TODO : DBPath is passed empty as of now. Need to take dbpath from application.
371 if(!m_localEnrolleeSecurity.get())
373 m_localEnrolleeSecurity = std::make_shared <EnrolleeSecurity> (m_ocResource);
377 /* NOTE: Execute cloud resource discovery and secure resource discovery in
378 different thread to avoid blocking of main event loop of Tizen */
379 std::thread secProvThread = std::thread([this, securityProvStatusCb](void)
381 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG,"provisionSecurity - thread IN");
382 ESResult res = m_localEnrolleeSecurity->provisionOwnership(NULL);
384 std::shared_ptr< SecProvisioningStatus > securityProvisioningStatus =
385 std::make_shared< SecProvisioningStatus >(m_localEnrolleeSecurity->getUUID(), res);
386 securityProvStatusCb(securityProvisioningStatus);
387 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG,"provisionSecurity - thread OUT");
391 secProvThread.detach();
393 res = m_localEnrolleeSecurity->provisionOwnership(NULL);
395 std::shared_ptr< SecProvisioningStatus > securityProvisioningStatus =
396 std::make_shared< SecProvisioningStatus >(m_localEnrolleeSecurity->getUUID(), res);
397 securityProvStatusCb(securityProvisioningStatus);
400 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG,"Mediator is unsecured built.");
404 throw ESInvalidParameterException("Callback is empty");
406 std::shared_ptr< SecProvisioningStatus > securityProvisioningStatus =
407 std::make_shared< SecProvisioningStatus >
408 ("", ESResult::ES_SEC_OPERATION_IS_NOT_SUPPORTED);
409 callback(securityProvisioningStatus);
411 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "provisionSecurity OUT");
414 void RemoteEnrollee::provisionSecurity(const SecurityProvStatusCbWithOption callback)
416 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "provisionSecurity IN");
418 ESResult res = ESResult::ES_ERROR;
421 throw ESInvalidParameterException("Callback is empty");
423 m_securityProvStatusCbWithOption = callback;
425 SecurityProvStatusCbWithOption securityProvStatusCbWithOption = std::bind(
426 &RemoteEnrollee::onSecurityStatusWithOptionHandlerCallback,
427 std::placeholders::_1,
430 if(!m_localEnrolleeSecurity.get())
432 m_localEnrolleeSecurity = std::make_shared <EnrolleeSecurity> (m_ocResource);
435 /* NOTE: Execute cloud resource discovery and secure resource discovery in
436 different thread to avoid blocking of main event loop of Tizen */
437 std::thread secProvThread = std::thread([this, securityProvStatusCbWithOption](void)
439 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG,"provisionSecurity - thread IN");
440 ESResult res = m_localEnrolleeSecurity->provisionOwnership(securityProvStatusCbWithOption);
442 std::shared_ptr< SecProvisioningStatus > securityProvisioningStatus =
443 std::make_shared< SecProvisioningStatus >(m_localEnrolleeSecurity->getUUID(), res);
444 securityProvStatusCbWithOption(securityProvisioningStatus);
445 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG,"provisionSecurity - thread OUT");
449 secProvThread.detach();
451 res = m_localEnrolleeSecurity->provisionOwnership(securityProvStatusCbWithOption);
453 std::shared_ptr< SecProvisioningStatus > securityProvisioningStatus =
454 std::make_shared< SecProvisioningStatus >(m_localEnrolleeSecurity->getUUID(), res);
455 securityProvStatusCbWithOption(securityProvisioningStatus);
458 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG,"Mediator is unsecured built.");
462 throw ESInvalidParameterException("Callback is empty");
464 std::shared_ptr< SecProvisioningStatus > securityProvisioningStatus =
465 std::make_shared< SecProvisioningStatus >
466 ("", ESResult::ES_SEC_OPERATION_IS_NOT_SUPPORTED);
467 callback(securityProvisioningStatus);
469 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "provisionSecurity OUT");
472 void RemoteEnrollee::getStatus(const GetStatusCb callback)
474 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "getStatus IN");
478 throw ESInvalidParameterException("Callback is empty");
481 if (m_enrolleeResource == nullptr)
483 throw ESBadRequestException ("Device not created");
486 m_getStatusCb = callback;
488 GetStatusCb getStatusCb = std::bind(
489 &RemoteEnrollee::onGetStatusHandlerCallback,
490 std::placeholders::_1,
493 m_enrolleeResource->registerGetStatusCallback(getStatusCb);
494 m_enrolleeResource->getStatus();
496 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "getStatus OUT");
499 void RemoteEnrollee::getConfiguration(const GetConfigurationStatusCb callback)
501 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "getConfiguration IN");
505 throw ESInvalidParameterException("Callback is empty");
508 if (m_enrolleeResource == nullptr)
510 throw ESBadRequestException ("Device not created");
513 m_getConfigurationStatusCb = callback;
515 GetConfigurationStatusCb getConfigurationStatusCb = std::bind(
516 &RemoteEnrollee::onGetConfigurationStatusHandlerCallback,
517 std::placeholders::_1,
520 m_enrolleeResource->registerGetConfigurationStatusCallback(getConfigurationStatusCb);
521 m_enrolleeResource->getConfiguration();
523 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "getConfiguration OUT");
526 void RemoteEnrollee::provisionDeviceProperties(const DeviceProp& deviceProp,
527 const DevicePropProvStatusCb callback)
529 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "provisionDeviceProperties IN");
533 throw ESInvalidParameterException("Callback is empty");
536 m_devicePropProvStatusCb = callback;
538 if (m_enrolleeResource == nullptr)
540 throw ESBadRequestException ("Device not created");
543 DevicePropProvStatusCb devicePropProvStatusCb = std::bind(
544 &RemoteEnrollee::onDevicePropProvisioningStatusHandlerCallback,
545 std::placeholders::_1,
548 m_enrolleeResource->registerDevicePropProvStatusCallback(devicePropProvStatusCb);
549 m_enrolleeResource->provisionProperties(deviceProp);
551 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "provisionDeviceProperties OUT");
554 void RemoteEnrollee::initCloudResource()
556 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "initCloudResource IN");
558 ESResult result = ES_ERROR;
560 result = discoverResource();
562 if (result == ES_ERROR)
564 OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_TAG,
565 "Failed to create resource object using discoverResource");
566 throw ESBadRequestException ("Resource object not created");
571 if(m_ocResource != nullptr)
573 m_cloudResource = std::make_shared<CloudResource>(m_ocResource);
577 throw ESBadGetException ("Resource handle is invalid");
581 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "initCloudResource OUT");
584 void RemoteEnrollee::provisionCloudProperties(const CloudProp& cloudProp,
585 const CloudPropProvStatusCb callback)
587 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "provisionCloudProperties w/o OCResource IN");
589 provisionCloudProperties(NULL, cloudProp, callback);
591 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "provisionCloudProperties w/o OCResource OUT");
595 void RemoteEnrollee::provisionCloudProperties(const std::shared_ptr< OC::OCResource > resource,
596 const CloudProp& cloudProp,
597 const CloudPropProvStatusCb callback)
599 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "provisionCloudProperties IN");
603 throw ESInvalidParameterException("Callback is empty");
606 m_cloudPropProvStatusCb = callback;
608 if((cloudProp.getAuthCode().empty() && cloudProp.getAccessToken().empty()) ||
609 cloudProp.getAuthProvider().empty() ||
610 cloudProp.getCiServer().empty())
612 throw ESBadRequestException ("Invalid Cloud Provisiong Info.");
617 if(resource->getResourceTypes().at(0) != OC_RSRVD_ES_RES_TYPE_EASYSETUP ||
618 resource->connectivityType() & CT_ADAPTER_TCP)
620 OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_TAG, "Given resource is not valid due to wrong rt or conntype");
621 throw ESInvalidParameterException("A given OCResource is wrong");
624 auto interfaces = resource->getResourceInterfaces();
625 bool isFound = false;
626 for(auto interface : interfaces)
628 if(interface.compare(BATCH_INTERFACE) == 0)
630 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "RemoteEnrollee object is succeessfully created");
631 OIC_LOG_V(INFO_PRIVATE, ES_REMOTE_ENROLLEE_TAG, "HOST: %s", resource->host().c_str());
632 OIC_LOG_V(INFO_PRIVATE, ES_REMOTE_ENROLLEE_TAG, "URI: %s", resource->uri().c_str());
633 OIC_LOG_V(INFO_PRIVATE, ES_REMOTE_ENROLLEE_TAG, "SID: %s", resource->sid().c_str());
634 OIC_LOG_V(INFO_PRIVATE, ES_REMOTE_ENROLLEE_TAG, "CONNECTIVITY: %d", resource->connectivityType());
641 throw ESInvalidParameterException("A given OCResource has no batch interface");
653 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "Skip to find a provisioning resource");
654 m_ocResource = resource;
655 m_cloudResource = std::make_shared<CloudResource>(m_ocResource);
658 catch (const std::exception& e)
660 OIC_LOG_V(ERROR, ES_REMOTE_ENROLLEE_TAG,
661 "Exception caught in provisionCloudProperties = %s", e.what());
663 std::shared_ptr< CloudPropProvisioningStatus > provStatus = std::make_shared<
664 CloudPropProvisioningStatus >(ESResult::ES_ENROLLEE_DISCOVERY_FAILURE);
665 m_cloudPropProvStatusCb(provStatus);
668 #if defined(__WITH_DTLS__) && defined(__WITH_TLS__)
669 if(!(cloudProp.getCloudID().empty() && cloudProp.getCredID() <= 0))
671 ESResult res = ESResult::ES_ERROR;
672 if(!m_cloudEnrolleeSecurity.get())
674 m_cloudEnrolleeSecurity = std::make_shared <EnrolleeSecurity> (m_ocResource);
678 res = m_cloudEnrolleeSecurity->provisionSecurityForCloudServer(cloudProp.getCloudID(),
679 cloudProp.getCredID());
681 if(res != ESResult::ES_OK)
683 m_cloudResource = nullptr;
684 std::shared_ptr< CloudPropProvisioningStatus > provStatus = std::make_shared<
685 CloudPropProvisioningStatus >(res);
686 m_cloudPropProvStatusCb(provStatus);
692 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "ACL and Cert. provisioning are skipped.");
694 #endif //defined(__WITH_DTLS__) && defined(__WITH_TLS__)
696 if (m_cloudResource == nullptr)
698 throw ESBadRequestException ("Cloud Resource not created");
701 CloudPropProvStatusCb cloudPropProvStatusCb = std::bind(
702 &RemoteEnrollee::onCloudPropProvisioningStatusHandlerCallback,
703 std::placeholders::_1,
706 m_cloudResource->registerCloudPropProvisioningStatusCallback(cloudPropProvStatusCb);
707 m_cloudResource->provisionProperties(cloudProp);
709 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "provisionCloudProperties OUT");
712 void RemoteEnrollee::provisionCloudProperties(const std::shared_ptr< OC::OCResource > resource,
713 const CloudProp& cloudProp,
714 const CloudPropProvStatusCb callback)
716 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "provisionCloudProperties IN");
720 throw ESInvalidParameterException("Callback is empty");
723 m_cloudPropProvStatusCb = callback;
725 if((cloudProp.getAuthCode().empty() && cloudProp.getAccessToken().empty()) ||
726 cloudProp.getAuthProvider().empty() ||
727 cloudProp.getCiServer().empty())
729 throw ESBadRequestException ("Invalid Cloud Provisiong Info.");
734 if(resource->getResourceTypes().at(0) != OC_RSRVD_ES_RES_TYPE_EASYSETUP ||
735 resource->connectivityType() & CT_ADAPTER_TCP)
737 OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_TAG, "Given resource is not valid due to wrong rt or conntype");
738 throw ESInvalidParameterException("A given OCResource is wrong");
741 auto interfaces = resource->getResourceInterfaces();
742 bool isFound = false;
743 for(auto interface : interfaces)
745 if(interface.compare(BATCH_INTERFACE) == 0)
747 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "RemoteEnrollee object is succeessfully created");
748 OIC_LOG_V(INFO_PRIVATE, ES_REMOTE_ENROLLEE_TAG, "HOST: %s", resource->host().c_str());
749 OIC_LOG_V(INFO_PRIVATE, ES_REMOTE_ENROLLEE_TAG, "URI: %s", resource->uri().c_str());
750 OIC_LOG_V(INFO_PRIVATE, ES_REMOTE_ENROLLEE_TAG, "SID: %s", resource->sid().c_str());
751 OIC_LOG_V(INFO_PRIVATE, ES_REMOTE_ENROLLEE_TAG, "CONNECTIVITY: %d", resource->connectivityType());
758 throw ESInvalidParameterException("A given OCResource has no batch interface");
761 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "Skip to find a provisioning resource");
762 m_ocResource = resource;
763 m_cloudResource = std::make_shared<CloudResource>(m_ocResource);
764 if (m_cloudResource == nullptr)
766 throw ESBadRequestException ("Cloud Resource not created");
770 /* NOTE: Execute cloud resource discovery and secure resource discovery in
771 different thread to avoid blocking of main event loop of Tizen */
772 std::thread cloudProvThread = std::thread([this, cloudProp]()
774 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "provisionCloudProperties - thread IN");
777 // Discover easy-setup resource only if it is not provided by API caller
781 if (m_cloudResource == nullptr)
783 OIC_LOG_V(ERROR, ES_REMOTE_ENROLLEE_TAG, "Cloud resource is not created");
784 std::shared_ptr< CloudPropProvisioningStatus > provStatus = std::make_shared<
785 CloudPropProvisioningStatus >(ESResult::ES_ENROLLEE_DISCOVERY_FAILURE);
786 m_cloudPropProvStatusCb(provStatus);
791 catch (const std::exception& e)
793 OIC_LOG_V(ERROR, ES_REMOTE_ENROLLEE_TAG,
794 "Exception caught in provisionCloudProperties = %s", e.what());
796 std::shared_ptr< CloudPropProvisioningStatus > provStatus = std::make_shared<
797 CloudPropProvisioningStatus >(ESResult::ES_ENROLLEE_DISCOVERY_FAILURE);
798 m_cloudPropProvStatusCb(provStatus);
802 #if defined(__WITH_DTLS__) && defined(__WITH_TLS__)
803 if(!(cloudProp.getCloudID().empty() && cloudProp.getCredID() <= 0))
805 ESResult res = ESResult::ES_ERROR;
806 if(!m_cloudEnrolleeSecurity.get())
808 m_cloudEnrolleeSecurity = std::make_shared <EnrolleeSecurity> (m_ocResource);
812 res = m_cloudEnrolleeSecurity->provisionSecurityForCloudServer(cloudProp.getCloudID(),
813 cloudProp.getCredID());
815 if(res != ESResult::ES_OK)
817 m_cloudResource = nullptr;
818 std::shared_ptr< CloudPropProvisioningStatus > provStatus = std::make_shared<
819 CloudPropProvisioningStatus >(res);
820 m_cloudPropProvStatusCb(provStatus);
826 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "ACL and Cert. provisioning are skipped.");
828 #endif //defined(__WITH_DTLS__) && defined(__WITH_TLS__)
830 CloudPropProvStatusCb cloudPropProvStatusCb = std::bind(
831 &RemoteEnrollee::onCloudPropProvisioningStatusHandlerCallback,
832 std::placeholders::_1,
835 m_cloudResource->registerCloudPropProvisioningStatusCallback(cloudPropProvStatusCb);
836 m_cloudResource->provisionProperties(cloudProp);
837 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "provisionCloudProperties - thread OUT");
841 cloudProvThread.detach();
842 OIC_LOG(INFO, ES_REMOTE_ENROLLEE_TAG, "provisionCloudProperties OUT");
845 void RemoteEnrollee::requestToConnect(const std::vector<ES_CONNECT_TYPE> &connectTypes, const ConnectRequestStatusCb callback)
847 OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_TAG, "connect IN");
851 throw ESInvalidParameterException("Callback is empty");
854 m_connectRequestStatusCb = callback;
856 ConnectRequestStatusCb connectRequestStatusCb = std::bind(
857 &RemoteEnrollee::onConnectRequestStatusHandlerCallback,
858 std::placeholders::_1,
861 m_enrolleeResource->registerConnectRequestStatusCallback(connectRequestStatusCb);
862 m_enrolleeResource->requestToConnect(connectTypes);
864 OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_TAG, "connect OUT");