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 "ESException.h"
26 #include "EnrolleeSecurity.h"
31 #define ES_REMOTE_ENROLLEE_TAG "ES_REMOTE_ENROLLEE"
35 RemoteEnrollee::RemoteEnrollee(const WiFiOnboadingConnection& wifiOnboardingconn) :
36 m_wifiOnboardingconn(wifiOnboardingconn)
38 m_requestCapabilityStatusCb = nullptr;
39 m_currentESState = CurrentESState::ES_ONBOARDED;
40 m_isSecured = m_wifiOnboardingconn.isSecured;
42 m_remoteResource = std::make_shared< EnrolleeResource >(m_wifiOnboardingconn);
44 OIC_LOG ( DEBUG, ES_REMOTE_ENROLLEE_TAG, "Inside RemoteEnrollee constr");
48 ESResult RemoteEnrollee::registerSecurityCallbackHandler(SecurityPinCb securityPinCb,
49 SecProvisioningDbPathCb secProvisioningDbPathCb)
51 // No need to check NULL for m_secProvisioningDbPathCB as this is not a mandatory
52 // callback function. If m_secProvisioningDbPathCB is NULL, provisioning manager
53 // in security layer will try to find the PDM.db file in the local path.
54 // If PDM.db is found, the provisioning manager operations will succeed.
55 // Otherwise all the provisioning manager operations will fail.
56 m_secProvisioningDbPathCb = secProvisioningDbPathCb;
57 m_securityPinCb = securityPinCb;
60 #endif //__WITH_DTLS__
62 void RemoteEnrollee::easySetupSecurityStatusCallback(
63 std::shared_ptr< SecProvisioningStatus > secProvisioningStatus)
65 OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_TAG, "easySetupStatusCallback status is, UUID = %s, "
66 "Status = %d", secProvisioningStatus->getDeviceUUID().c_str(),
67 secProvisioningStatus->getResult());
69 if(secProvisioningStatus->getResult() == ES_OK)
71 OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_TAG, "Ownership and ACL are successful. "
72 "Continue with Network information provisioning");
74 m_currentESState = CurrentESState::ES_OWNED;
76 OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Before ProvisionEnrollee");
78 m_enrolleeSecStatusCb(secProvisioningStatus);
82 OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_TAG, "Ownership and ACL are fail");
84 m_enrolleeSecStatusCb(secProvisioningStatus);
88 void RemoteEnrollee::InitRemoteEnrolleeStatusHandler (
89 std::shared_ptr< InitRemoteEnrolleeStatus > initRemoteEnrolleeStatus)
91 OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Entering InitRemoteEnrolleeStatusHandler");
93 OIC_LOG_V(DEBUG,ES_REMOTE_ENROLLEE_TAG,"initRemoteEnrolleeStatus = %d", initRemoteEnrolleeStatus->getESResult());
95 m_initRemoteEnrolleeStatusCb(initRemoteEnrolleeStatus);
98 void RemoteEnrollee::requestCapabilityStatusHandler (
99 std::shared_ptr< RequestCapabilityStatus > requestCapabilityStatus)
101 OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Entering requestCapabilityStatusHandler");
103 OIC_LOG_V(DEBUG,ES_REMOTE_ENROLLEE_TAG,"requestCapabilityStatus = %d", requestCapabilityStatus->getESResult());
105 m_requestCapabilityStatusCb(requestCapabilityStatus);
108 void RemoteEnrollee::dataProvisioningStatusHandler(
109 std::shared_ptr< ProvisioningStatus > provStatus)
111 OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Entering dataprovisioningStatusHandler");
113 OIC_LOG_V(DEBUG,ES_REMOTE_ENROLLEE_TAG,"ProvStatus = %d", provStatus->getESResult());
115 if (provStatus->getESResult() == ES_OK)
117 if (provStatus->getESState() >= ESState::ES_PROVISIONED_ALREADY)
119 OIC_LOG_V(DEBUG,ES_REMOTE_ENROLLEE_TAG,"ProvStatus = %d", provStatus->getESResult());
120 m_currentESState = CurrentESState::ES_PROVISIONED;
123 m_dataProvStatusCb(provStatus);
128 void RemoteEnrollee::initRemoteEnrollee(InitRemoteEnrolleeStatusCb callback)
130 ESResult result = ES_ERROR;
134 throw ESInvalidParameterException("Callback is empty");
137 m_initRemoteEnrolleeStatusCb = callback;
139 if (m_remoteResource != nullptr)
141 throw ESBadRequestException ("Already created");
144 InitRemoteEnrolleeStatusCb initRemoteEnrolleeStatusCb = std::bind(
145 &RemoteEnrollee::InitRemoteEnrolleeStatusHandler, this, std::placeholders::_1);
146 m_remoteResource->registerInitRemoteEnrolleeStatusCallback(initRemoteEnrolleeStatusCb);
148 result = m_remoteResource->constructResourceObject();
150 if (result == ES_ERROR)
152 OIC_LOG(ERROR,ES_REMOTE_ENROLLEE_TAG,
153 "Failed to create device using constructResourceObject");
154 throw ESBadRequestException ("Device not created");
158 void RemoteEnrollee::startSecurityProvisioning(EnrolleeSecStatusCb callback)
162 m_enrolleeSecStatusCb = callback;
164 if (m_isSecured && m_currentESState < CurrentESState::ES_OWNED)
166 EnrolleeSecStatusCb securityProvStatusCb = std::bind(
167 &RemoteEnrollee::easySetupSecurityStatusCallback,
169 std::placeholders::_1);
170 //TODO : DBPath is passed empty as of now. Need to take dbpath from application.
171 m_enrolleeSecurity = std::make_shared <EnrolleeSecurity> (m_remoteResource, "");
173 m_enrolleeSecurity->registerCallbackHandler(securityProvStatusCb,
174 m_securityPinCb, m_secProvisioningDbPathCb);
178 EasySetupState easySetupState = m_enrolleeSecurity->performOwnershipTransfer();
179 if (easySetupState == DEVICE_NOT_OWNED)
181 OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_TAG,
182 "performOwnershipTransfer returned : %d",
186 else if (easySetupState == DEVICE_OWNED)
188 OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_TAG,
189 "performOwnershipTransfer returned : %d",
191 OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Before ProvisionEnrollee");
194 catch (OCException & e)
196 OIC_LOG_V(ERROR, ES_REMOTE_ENROLLEE_TAG,
197 "Exception for performOwnershipTransfer : %s", e.reason().c_str());
204 void RemoteEnrollee::getCapabilityData(RequestCapabilityStatusCb callback)
206 ESResult result = ES_ERROR;
210 throw ESInvalidParameterException("Callback is empty");
213 m_requestCapabilityStatusCb = callback;
215 if (m_remoteResource == nullptr)
217 throw ESBadRequestException ("Device not created");
220 RequestCapabilityStatusCb requestCapabilityStatusCb = std::bind(
221 &RemoteEnrollee::requestCapabilityStatusHandler, this, std::placeholders::_1);
222 m_remoteResource->registerCapabilityStatusCallback(requestCapabilityStatusCb);
223 m_remoteResource->getCapabilityData();
226 void RemoteEnrollee::startDataProvisioning(const ProvConfig& dataProvConfig, DataProvStatusCb callback)
228 OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Before ProvisionEnrollee");
230 m_ProvConfig = dataProvConfig;
231 m_dataProvStatusCb = callback;
233 DataProvStatusCb dataProvStatusCb = std::bind(
234 &RemoteEnrollee::dataProvisioningStatusHandler, this, std::placeholders::_1);
236 m_remoteResource->registerProvStatusCallback(dataProvStatusCb);
237 m_remoteResource->provisionEnrollee();
240 void RemoteEnrollee::stopProvisioning()
242 m_currentESState = CurrentESState::ES_UNKNOWN;
244 m_remoteResource->unprovisionEnrollee();
247 bool RemoteEnrollee::isEnrolleeProvisioned()
249 if(m_currentESState >= CurrentESState::ES_PROVISIONED)
259 ProvConfig RemoteEnrollee::getProvConfig ()
264 WiFiOnboadingConnection RemoteEnrollee::getOnboardConn()
266 return m_wifiOnboardingconn;