[Update] process of provisioning was split.
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / src / RemoteEnrollee.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "RemoteEnrollee.h"
22 #include "EnrolleeResource.h"
23 #include "ESException.h"
24 #include "logger.h"
25 #ifdef __WITH_DTLS__
26 #include "EnrolleeSecurity.h"
27 #endif //__WITH_DTLS
28
29 namespace OIC
30 {
31     #define ES_REMOTE_ENROLLEE_TAG "ES_REMOTE_ENROLLEE"
32
33     namespace Service
34     {
35         RemoteEnrollee::RemoteEnrollee(const WiFiOnboadingConnection& wifiOnboardingconn) :
36                 m_wifiOnboardingconn(wifiOnboardingconn)
37         {
38             m_requestCapabilityStatusCb = nullptr;
39             m_currentESState = CurrentESState::ES_ONBOARDED;
40             m_isSecured = m_wifiOnboardingconn.isSecured;
41
42             m_remoteResource = std::make_shared< EnrolleeResource >(m_wifiOnboardingconn);
43
44             OIC_LOG ( DEBUG, ES_REMOTE_ENROLLEE_TAG, "Inside RemoteEnrollee constr");
45         }
46
47 #ifdef __WITH_DTLS__
48         ESResult RemoteEnrollee::registerSecurityCallbackHandler(SecurityPinCb securityPinCb,
49                 SecProvisioningDbPathCb secProvisioningDbPathCb)
50         {
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;
58             return ES_OK;
59         }
60 #endif //__WITH_DTLS__
61
62         void RemoteEnrollee::easySetupSecurityStatusCallback(
63                         std::shared_ptr< SecProvisioningStatus > secProvisioningStatus)
64         {
65             OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_TAG, "easySetupStatusCallback status is, UUID = %s, "
66                     "Status = %d", secProvisioningStatus->getDeviceUUID().c_str(),
67                     secProvisioningStatus->getResult());
68
69             if(secProvisioningStatus->getResult() == ES_OK)
70             {
71                 OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_TAG, "Ownership and ACL are successful. "
72                         "Continue with Network information provisioning");
73
74                 m_currentESState = CurrentESState::ES_OWNED;
75
76                 OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Before ProvisionEnrollee");
77
78                 m_enrolleeSecStatusCb(secProvisioningStatus);
79             }
80             else
81             {
82                 OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_TAG, "Ownership and ACL are fail");
83
84                 m_enrolleeSecStatusCb(secProvisioningStatus);
85             }
86         }
87
88         void RemoteEnrollee::InitRemoteEnrolleeStatusHandler (
89                 std::shared_ptr< InitRemoteEnrolleeStatus > initRemoteEnrolleeStatus)
90         {
91             OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Entering InitRemoteEnrolleeStatusHandler");
92
93             OIC_LOG_V(DEBUG,ES_REMOTE_ENROLLEE_TAG,"initRemoteEnrolleeStatus = %d", initRemoteEnrolleeStatus->getESResult());
94
95             m_initRemoteEnrolleeStatusCb(initRemoteEnrolleeStatus);
96         }
97
98         void RemoteEnrollee::requestCapabilityStatusHandler (
99                 std::shared_ptr< RequestCapabilityStatus > requestCapabilityStatus)
100         {
101             OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Entering requestCapabilityStatusHandler");
102
103             OIC_LOG_V(DEBUG,ES_REMOTE_ENROLLEE_TAG,"requestCapabilityStatus = %d", requestCapabilityStatus->getESResult());
104
105             m_requestCapabilityStatusCb(requestCapabilityStatus);
106         }
107
108         void RemoteEnrollee::dataProvisioningStatusHandler(
109                 std::shared_ptr< ProvisioningStatus > provStatus)
110         {
111             OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Entering dataprovisioningStatusHandler");
112
113             OIC_LOG_V(DEBUG,ES_REMOTE_ENROLLEE_TAG,"ProvStatus = %d", provStatus->getESResult());
114
115             if (provStatus->getESResult() == ES_OK)
116             {
117                 if (provStatus->getESState() >= ESState::ES_PROVISIONED_ALREADY)
118                 {
119                     OIC_LOG_V(DEBUG,ES_REMOTE_ENROLLEE_TAG,"ProvStatus = %d", provStatus->getESResult());
120                     m_currentESState = CurrentESState::ES_PROVISIONED;
121                 }
122             }
123             m_dataProvStatusCb(provStatus);
124
125             return;
126         }
127
128         void RemoteEnrollee::initRemoteEnrollee(InitRemoteEnrolleeStatusCb callback)
129         {
130             ESResult result = ES_ERROR;
131
132             if(!callback)
133             {
134                 throw ESInvalidParameterException("Callback is empty");
135             }
136
137             m_initRemoteEnrolleeStatusCb = callback;
138
139             if (m_remoteResource != nullptr)
140             {
141                 throw ESBadRequestException ("Already created");
142             }
143
144             InitRemoteEnrolleeStatusCb initRemoteEnrolleeStatusCb = std::bind(
145                     &RemoteEnrollee::InitRemoteEnrolleeStatusHandler, this, std::placeholders::_1);
146             m_remoteResource->registerInitRemoteEnrolleeStatusCallback(initRemoteEnrolleeStatusCb);
147
148             result = m_remoteResource->constructResourceObject();
149
150             if (result == ES_ERROR)
151             {
152                 OIC_LOG(ERROR,ES_REMOTE_ENROLLEE_TAG,
153                                     "Failed to create device using constructResourceObject");
154                 throw ESBadRequestException ("Device not created");
155             }
156         }
157
158         void RemoteEnrollee::startSecurityProvisioning(EnrolleeSecStatusCb callback)
159         {
160 #ifdef __WITH_DTLS__
161
162             m_enrolleeSecStatusCb = callback;
163
164             if (m_isSecured && m_currentESState < CurrentESState::ES_OWNED)
165             {
166                 EnrolleeSecStatusCb securityProvStatusCb = std::bind(
167                         &RemoteEnrollee::easySetupSecurityStatusCallback,
168                         this,
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, "");
172
173                 m_enrolleeSecurity->registerCallbackHandler(securityProvStatusCb,
174                         m_securityPinCb, m_secProvisioningDbPathCb);
175
176                 try
177                 {
178                     EasySetupState easySetupState = m_enrolleeSecurity->performOwnershipTransfer();
179                     if (easySetupState == DEVICE_NOT_OWNED)
180                     {
181                         OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_TAG,
182                                 "performOwnershipTransfer returned : %d",
183                                 easySetupState);
184                         return;
185                     }
186                     else if (easySetupState == DEVICE_OWNED)
187                     {
188                         OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_TAG,
189                                 "performOwnershipTransfer returned : %d",
190                                 easySetupState);
191                         OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Before ProvisionEnrollee");
192                     }
193                 }
194                 catch (OCException & e)
195                 {
196                     OIC_LOG_V(ERROR, ES_REMOTE_ENROLLEE_TAG,
197                             "Exception for performOwnershipTransfer : %s", e.reason().c_str());
198                     return ;
199                 }
200             }
201 #endif
202         }
203
204         void RemoteEnrollee::getCapabilityData(RequestCapabilityStatusCb callback)
205         {
206             ESResult result = ES_ERROR;
207
208             if(!callback)
209             {
210                 throw ESInvalidParameterException("Callback is empty");
211             }
212
213             m_requestCapabilityStatusCb = callback;
214
215             if (m_remoteResource == nullptr)
216             {
217                 throw ESBadRequestException ("Device not created");
218             }
219
220             RequestCapabilityStatusCb requestCapabilityStatusCb = std::bind(
221                     &RemoteEnrollee::requestCapabilityStatusHandler, this, std::placeholders::_1);
222             m_remoteResource->registerCapabilityStatusCallback(requestCapabilityStatusCb);
223             m_remoteResource->getCapabilityData();
224         }
225
226         void RemoteEnrollee::startDataProvisioning(const ProvConfig& dataProvConfig, DataProvStatusCb callback)
227         {
228             OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Before ProvisionEnrollee");
229
230             m_ProvConfig = dataProvConfig;
231             m_dataProvStatusCb = callback;
232
233             DataProvStatusCb dataProvStatusCb = std::bind(
234                     &RemoteEnrollee::dataProvisioningStatusHandler, this, std::placeholders::_1);
235
236             m_remoteResource->registerProvStatusCallback(dataProvStatusCb);
237             m_remoteResource->provisionEnrollee();
238         }
239
240         void RemoteEnrollee::stopProvisioning()
241         {
242             m_currentESState = CurrentESState::ES_UNKNOWN;
243
244             m_remoteResource->unprovisionEnrollee();
245         }
246
247         bool RemoteEnrollee::isEnrolleeProvisioned()
248         {
249             if(m_currentESState >= CurrentESState::ES_PROVISIONED)
250             {
251                 return true;
252             }
253             else
254             {
255                 return false;
256             }
257         }
258
259         ProvConfig RemoteEnrollee::getProvConfig ()
260         {
261             return m_ProvConfig;
262         }
263
264        WiFiOnboadingConnection RemoteEnrollee::getOnboardConn()
265        {
266          return m_wifiOnboardingconn;
267        }
268
269     }
270 }