1 //******************************************************************
\r
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
\r
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\r
7 // Licensed under the Apache License, Version 2.0 (the "License");
\r
8 // you may not use this file except in compliance with the License.
\r
9 // You may obtain a copy of the License at
\r
11 // http://www.apache.org/licenses/LICENSE-2.0
\r
13 // Unless required by applicable law or agreed to in writing, software
\r
14 // distributed under the License is distributed on an "AS IS" BASIS,
\r
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 // See the License for the specific language governing permissions and
\r
17 // limitations under the License.
\r
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\r
21 #include "RemoteEnrollee.h"
\r
22 #include "RemoteEnrolleeResource.h"
\r
23 #include "ESException.h"
\r
25 #ifdef __WITH_DTLS__
\r
26 #include "EnrolleeSecurity.h"
\r
27 #endif //__WITH_DTLS
\r
31 #define ES_REMOTE_ENROLLEE_TAG "ES_REMOTE_ENROLLEE"
\r
35 RemoteEnrollee::RemoteEnrollee(const EnrolleeNWProvInfo& enrolleeNWProvInfo) :
\r
36 m_enrolleeNWProvInfo(enrolleeNWProvInfo)
\r
38 m_currentESState = CurrentESState::ES_UNKNOWN;
\r
39 m_needSecuredEasysetup = enrolleeNWProvInfo.needSecuredEasysetup;
\r
41 OC_LOG ( DEBUG, ES_REMOTE_ENROLLEE_TAG, "Inside RemoteEnrollee constr");
\r
44 #ifdef __WITH_DTLS__
\r
45 ESResult RemoteEnrollee::registerSecurityCallbackHandler(SecurityPinCb securityPinCb,
\r
46 SecProvisioningDbPathCb secProvisioningDbPathCb)
\r
48 // No need to check NULL for m_secProvisioningDbPathCB as this is not a mandatory
\r
49 // callback function. If m_secProvisioningDbPathCB is NULL, provisioning manager
\r
50 // in security layer will try to find the PDM.db file in the local path.
\r
51 // If PDM.db is found, the provisioning manager operations will succeed.
\r
52 // Otherwise all the provisioning manager operations will fail.
\r
53 m_secProvisioningDbPathCb = secProvisioningDbPathCb;
\r
54 m_securityPinCb = securityPinCb;
\r
57 #endif //__WITH_DTLS__
\r
59 void RemoteEnrollee::registerEasySetupStatusHandler(EasySetupStatusCB callback)
\r
61 OC_LOG ( DEBUG, ES_REMOTE_ENROLLEE_TAG, "Entered registerStatusHandler");
\r
64 throw ESInvalidParameterException("Callback is empty");
\r
67 if (m_easySetupStatusCb)
\r
69 throw ESBadRequestException("Callback handler already registered");
\r
73 m_easySetupStatusCb = callback;
\r
75 m_remoteResource = std::make_shared< RemoteEnrolleeResource >(m_enrolleeNWProvInfo);
\r
79 void RemoteEnrollee::easySetupSecurityStatusCallback(
\r
80 std::shared_ptr< SecProvisioningResult > secProvisioningResult)
\r
82 OC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_TAG, "easySetupStatusCallback status is, UUID = %s, "
\r
83 "Status = %d", secProvisioningResult->getDeviceUUID().c_str(),
\r
84 secProvisioningResult->getResult());
\r
86 if(secProvisioningResult->getResult() == ES_OK)
\r
88 OC_LOG(DEBUG, ES_REMOTE_ENROLLEE_TAG, "Ownership and ACL are successful. "
\r
89 "Continue with Network information provisioning");
\r
91 m_currentESState = CurrentESState::ES_OWNED;
\r
93 OC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Before ProvisionEnrollee");
\r
95 RemoteEnrolleeResource::ProvStatusCb provStatusCb = std::bind(
\r
96 &RemoteEnrollee::provisioningStatusHandler, this, std::placeholders::_1);
\r
98 m_remoteResource->registerProvStatusCallback(provStatusCb);
\r
99 m_remoteResource->provisionEnrollee();
\r
103 OC_LOG(DEBUG, ES_REMOTE_ENROLLEE_TAG, "Ownership and ACL are successful");
\r
104 std::shared_ptr< EasySetupStatus > easySetupStatus = nullptr;
\r
105 easySetupStatus = std::make_shared< EasySetupStatus >(DEVICE_NOT_PROVISIONED,
\r
106 m_enrolleeNWProvInfo);
\r
107 if (m_easySetupStatusCb)
\r
109 if (easySetupStatus)
\r
111 m_easySetupStatusCb(easySetupStatus);
\r
115 m_easySetupStatusCb(nullptr);
\r
121 void RemoteEnrollee::provisioningStatusHandler(
\r
122 std::shared_ptr< ProvisioningStatus > provStatus)
\r
124 OC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Entering ProvisioningStatusHandler");
\r
126 OC_LOG_V(DEBUG,ES_REMOTE_ENROLLEE_TAG,"ProvStatus = %d", provStatus->getESResult());
\r
128 std::shared_ptr< EasySetupStatus > easySetupStatus = nullptr;
\r
130 if (m_enrolleeNWProvInfo.isSecured)
\r
132 if (m_currentESState > CurrentESState::ES_OWNED)
\r
134 goto CALLBACK_CHECK;
\r
143 goto CALLBACK_CHECK;
\r
148 if (provStatus->getESResult() == ES_OK)
\r
150 if (provStatus->getESState() >= ESState::ES_PROVISIONED_ALREADY)
\r
152 easySetupStatus = std::make_shared< EasySetupStatus >(DEVICE_PROVISIONED,
\r
153 m_enrolleeNWProvInfo);
\r
157 easySetupStatus = std::make_shared< EasySetupStatus >(DEVICE_NOT_PROVISIONED,
\r
158 m_enrolleeNWProvInfo);
\r
163 easySetupStatus = std::make_shared< EasySetupStatus >(DEVICE_NOT_PROVISIONED,
\r
164 m_enrolleeNWProvInfo);
\r
167 if (m_easySetupStatusCb)
\r
169 if (easySetupStatus)
\r
171 m_easySetupStatusCb(easySetupStatus);
\r
175 m_easySetupStatusCb(nullptr);
\r
183 easySetupStatus = std::make_shared< EasySetupStatus >(DEVICE_NOT_PROVISIONED,
\r
184 m_enrolleeNWProvInfo);
\r
186 if (easySetupStatus)
\r
188 m_easySetupStatusCb(easySetupStatus);
\r
192 m_easySetupStatusCb(nullptr);
\r
197 void RemoteEnrollee::startProvisioning()
\r
199 OC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Entering startProvisioning");
\r
200 if (m_remoteResource == nullptr)
\r
202 throw ESBadRequestException ("Device not created");
\r
205 ESResult result = ES_ERROR;
\r
207 result = m_remoteResource->constructResourceObject();
\r
209 if (result == ES_ERROR)
\r
211 OC_LOG(ERROR,ES_REMOTE_ENROLLEE_TAG,
\r
212 "Failed to create device using constructResourceObject");
\r
213 throw ESBadRequestException ("Device not created");
\r
216 m_currentESState = CurrentESState::ES_ONBOARDED;
\r
218 #ifdef __WITH_DTLS__
\r
219 if (m_needSecuredEasysetup && m_currentESState < CurrentESState::ES_OWNED)
\r
221 EnrolleeSecStatusCb securityProvStatusCb = std::bind(
\r
222 &RemoteEnrollee::easySetupSecurityStatusCallback,
\r
224 std::placeholders::_1);
\r
225 //TODO : DBPath is passed empty as of now. Need to take dbpath from application.
\r
226 m_enrolleeSecurity = std::make_shared <EnrolleeSecurity> (m_remoteResource, "");
\r
228 m_enrolleeSecurity->registerCallbackHandler(securityProvStatusCb,
\r
229 m_securityPinCb, m_secProvisioningDbPathCb);
\r
233 EasySetupState easySetupState = m_enrolleeSecurity->performOwnershipTransfer();
\r
234 if (easySetupState == DEVICE_NOT_OWNED)
\r
236 OC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_TAG,
\r
237 "performOwnershipTransfer returned : %d",
\r
241 else if (easySetupState == DEVICE_OWNED)
\r
243 OC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_TAG,
\r
244 "performOwnershipTransfer returned : %d",
\r
246 OC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Before ProvisionEnrollee");
\r
248 RemoteEnrolleeResource::ProvStatusCb provStatusCb = std::bind(
\r
249 &RemoteEnrollee::provisioningStatusHandler,
\r
250 this, std::placeholders::_1);
\r
252 m_remoteResource->registerProvStatusCallback(provStatusCb);
\r
253 m_remoteResource->provisionEnrollee();
\r
256 catch (OCException & e)
\r
258 OC_LOG_V(ERROR, ES_REMOTE_ENROLLEE_TAG,
\r
259 "Exception for performOwnershipTransfer : %s", e.reason().c_str());
\r
264 OC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Before ProvisionEnrollee");
\r
266 RemoteEnrolleeResource::ProvStatusCb provStatusCb = std::bind(
\r
267 &RemoteEnrollee::provisioningStatusHandler, this, std::placeholders::_1);
\r
269 m_remoteResource->registerProvStatusCallback(provStatusCb);
\r
270 m_remoteResource->provisionEnrollee();
\r
274 void RemoteEnrollee::stopProvisioning()
\r
276 m_currentESState = CurrentESState::ES_UNKNOWN;
\r
278 m_remoteResource->unprovisionEnrollee();
\r
281 bool RemoteEnrollee::isEnrolleeProvisioned()
\r
283 if(m_currentESState >= CurrentESState::ES_PROVISIONED)
\r
293 EnrolleeNWProvInfo& RemoteEnrollee::getEnrolleeProvisioningInfo ()
\r
295 return m_enrolleeNWProvInfo;
\r