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 "RemoteEnrolleeResource.h"
23 #include "ESException.h"
26 #include "EnrolleeSecurity.h"
31 #define ES_REMOTE_ENROLLEE_TAG "ES_REMOTE_ENROLLEE"
35 RemoteEnrollee::RemoteEnrollee(const EnrolleeNWProvInfo& enrolleeNWProvInfo) :
36 m_enrolleeNWProvInfo(enrolleeNWProvInfo)
38 m_currentESState = CurrentESState::ES_UNKNOWN;
39 m_needSecuredEasysetup = enrolleeNWProvInfo.needSecuredEasysetup;
41 OC_LOG ( DEBUG, ES_REMOTE_ENROLLEE_TAG, "Inside RemoteEnrollee constr");
45 ESResult RemoteEnrollee::registerSecurityCallbackHandler(SecurityPinCb securityPinCb,
46 SecProvisioningDbPathCb secProvisioningDbPathCb)
48 // No need to check NULL for m_secProvisioningDbPathCB as this is not a mandatory
49 // callback function. If m_secProvisioningDbPathCB is NULL, provisioning manager
50 // in security layer will try to find the PDM.db file in the local path.
51 // If PDM.db is found, the provisioning manager operations will succeed.
52 // Otherwise all the provisioning manager operations will fail.
53 m_secProvisioningDbPathCb = secProvisioningDbPathCb;
54 m_securityPinCb = securityPinCb;
57 #endif //__WITH_DTLS__
59 void RemoteEnrollee::registerEasySetupStatusHandler(EasySetupStatusCB callback)
61 OC_LOG ( DEBUG, ES_REMOTE_ENROLLEE_TAG, "Entered registerStatusHandler");
64 throw ESInvalidParameterException("Callback is empty");
67 if (m_easySetupStatusCb)
69 throw ESBadRequestException("Callback handler already registered");
73 m_easySetupStatusCb = callback;
75 m_remoteResource = std::make_shared< RemoteEnrolleeResource >(m_enrolleeNWProvInfo);
79 void RemoteEnrollee::easySetupSecurityStatusCallback(
80 std::shared_ptr< SecProvisioningResult > secProvisioningResult)
82 OC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_TAG, "easySetupStatusCallback status is, UUID = %s, "
83 "Status = %d", secProvisioningResult->getDeviceUUID().c_str(),
84 secProvisioningResult->getResult());
86 if(secProvisioningResult->getResult() == ES_OK)
88 OC_LOG(DEBUG, ES_REMOTE_ENROLLEE_TAG, "Ownership and ACL are successful. "
89 "Continue with Network information provisioning");
91 m_currentESState = CurrentESState::ES_OWNED;
93 OC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Before ProvisionEnrollee");
95 RemoteEnrolleeResource::ProvStatusCb provStatusCb = std::bind(
96 &RemoteEnrollee::provisioningStatusHandler, this, std::placeholders::_1);
98 m_remoteResource->registerProvStatusCallback(provStatusCb);
99 m_remoteResource->provisionEnrollee();
103 OC_LOG(DEBUG, ES_REMOTE_ENROLLEE_TAG, "Ownership and ACL are successful");
104 std::shared_ptr< EasySetupStatus > easySetupStatus = nullptr;
105 easySetupStatus = std::make_shared< EasySetupStatus >(DEVICE_NOT_PROVISIONED,
106 m_enrolleeNWProvInfo);
107 if (m_easySetupStatusCb)
111 m_easySetupStatusCb(easySetupStatus);
115 m_easySetupStatusCb(nullptr);
121 void RemoteEnrollee::provisioningStatusHandler(
122 std::shared_ptr< ProvisioningStatus > provStatus)
124 OC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Entering ProvisioningStatusHandler");
126 OC_LOG_V(DEBUG,ES_REMOTE_ENROLLEE_TAG,"ProvStatus = %d", provStatus->getESResult());
128 std::shared_ptr< EasySetupStatus > easySetupStatus = nullptr;
130 if (m_enrolleeNWProvInfo.isSecured)
132 if (m_currentESState > CurrentESState::ES_OWNED)
148 if (provStatus->getESResult() == ES_OK)
150 if (provStatus->getESState() >= ESState::ES_PROVISIONED_ALREADY)
152 easySetupStatus = std::make_shared< EasySetupStatus >(DEVICE_PROVISIONED,
153 m_enrolleeNWProvInfo);
157 easySetupStatus = std::make_shared< EasySetupStatus >(DEVICE_NOT_PROVISIONED,
158 m_enrolleeNWProvInfo);
163 easySetupStatus = std::make_shared< EasySetupStatus >(DEVICE_NOT_PROVISIONED,
164 m_enrolleeNWProvInfo);
167 if (m_easySetupStatusCb)
171 m_easySetupStatusCb(easySetupStatus);
175 m_easySetupStatusCb(nullptr);
183 easySetupStatus = std::make_shared< EasySetupStatus >(DEVICE_NOT_PROVISIONED,
184 m_enrolleeNWProvInfo);
188 m_easySetupStatusCb(easySetupStatus);
192 m_easySetupStatusCb(nullptr);
197 void RemoteEnrollee::startProvisioning()
199 OC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Entering startProvisioning");
200 if (m_remoteResource == nullptr)
202 throw ESBadRequestException ("Device not created");
205 ESResult result = ES_ERROR;
207 result = m_remoteResource->constructResourceObject();
209 if (result == ES_ERROR)
211 OC_LOG(ERROR,ES_REMOTE_ENROLLEE_TAG,
212 "Failed to create device using constructResourceObject");
213 throw ESBadRequestException ("Device not created");
216 m_currentESState = CurrentESState::ES_ONBOARDED;
219 if (m_needSecuredEasysetup && m_currentESState < CurrentESState::ES_OWNED)
221 EnrolleeSecStatusCb securityProvStatusCb = std::bind(
222 &RemoteEnrollee::easySetupSecurityStatusCallback,
224 std::placeholders::_1);
225 //TODO : DBPath is passed empty as of now. Need to take dbpath from application.
226 m_enrolleeSecurity = std::make_shared <EnrolleeSecurity> (m_remoteResource, "");
228 m_enrolleeSecurity->registerCallbackHandler(securityProvStatusCb,
229 m_securityPinCb, m_secProvisioningDbPathCb);
233 EasySetupState easySetupState = m_enrolleeSecurity->performOwnershipTransfer();
234 if (easySetupState == DEVICE_NOT_OWNED)
236 OC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_TAG,
237 "performOwnershipTransfer returned : %d",
241 else if (easySetupState == DEVICE_OWNED)
243 OC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_TAG,
244 "performOwnershipTransfer returned : %d",
246 OC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Before ProvisionEnrollee");
248 RemoteEnrolleeResource::ProvStatusCb provStatusCb = std::bind(
249 &RemoteEnrollee::provisioningStatusHandler,
250 this, std::placeholders::_1);
252 m_remoteResource->registerProvStatusCallback(provStatusCb);
253 m_remoteResource->provisionEnrollee();
256 catch (OCException & e)
258 OC_LOG_V(ERROR, ES_REMOTE_ENROLLEE_TAG,
259 "Exception for performOwnershipTransfer : %s", e.reason().c_str());
264 OC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Before ProvisionEnrollee");
266 RemoteEnrolleeResource::ProvStatusCb provStatusCb = std::bind(
267 &RemoteEnrollee::provisioningStatusHandler, this, std::placeholders::_1);
269 m_remoteResource->registerProvStatusCallback(provStatusCb);
270 m_remoteResource->provisionEnrollee();
274 void RemoteEnrollee::stopProvisioning()
276 m_currentESState = CurrentESState::ES_UNKNOWN;
278 m_remoteResource->unprovisionEnrollee();
281 bool RemoteEnrollee::isEnrolleeProvisioned()
283 if(m_currentESState >= CurrentESState::ES_PROVISIONED)
293 EnrolleeNWProvInfo& RemoteEnrollee::getEnrolleeProvisioningInfo ()
295 return m_enrolleeNWProvInfo;