Imported Upstream version 1.1.0
[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 "RemoteEnrolleeResource.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 ProvConfig& provConfig, const WiFiOnboadingConnection& connection) :
36                 m_ProvConfig(provConfig), m_wifiOnboardingconn(connection)
37         {
38             m_currentESState = CurrentESState::ES_UNKNOWN;
39             m_isSecured = connection.isSecured; //enrolleeNWProvInfo.needSecuredEasysetup;
40
41             OIC_LOG ( DEBUG, ES_REMOTE_ENROLLEE_TAG, "Inside RemoteEnrollee constr");
42         }
43
44 #ifdef __WITH_DTLS__
45         ESResult RemoteEnrollee::registerSecurityCallbackHandler(SecurityPinCb securityPinCb,
46                 SecProvisioningDbPathCb secProvisioningDbPathCb)
47         {
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;
55             return ES_OK;
56         }
57 #endif //__WITH_DTLS__
58
59         void RemoteEnrollee::registerEasySetupStatusHandler(EasySetupStatusCB callback)
60         {
61             OIC_LOG ( DEBUG, ES_REMOTE_ENROLLEE_TAG, "Entered registerStatusHandler");
62             if(!callback)
63             {
64                 throw ESInvalidParameterException("Callback is empty");
65             }
66
67             if (m_easySetupStatusCb)
68             {
69                 throw ESBadRequestException("Callback handler already registered");
70             }
71             else
72             {
73                 m_easySetupStatusCb = callback;
74
75                 m_remoteResource = std::make_shared< RemoteEnrolleeResource >(m_ProvConfig, m_wifiOnboardingconn);
76             }
77         }
78
79         void RemoteEnrollee::easySetupSecurityStatusCallback(
80                         std::shared_ptr< SecProvisioningResult > secProvisioningResult)
81         {
82             OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_TAG, "easySetupStatusCallback status is, UUID = %s, "
83                     "Status = %d", secProvisioningResult->getDeviceUUID().c_str(),
84                     secProvisioningResult->getResult());
85
86             if(secProvisioningResult->getResult() == ES_OK)
87             {
88                 OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_TAG, "Ownership and ACL are successful. "
89                         "Continue with Network information provisioning");
90
91                 m_currentESState = CurrentESState::ES_OWNED;
92
93                 OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Before ProvisionEnrollee");
94
95                 RemoteEnrolleeResource::ProvStatusCb provStatusCb = std::bind(
96                         &RemoteEnrollee::provisioningStatusHandler, this, std::placeholders::_1);
97
98                 m_remoteResource->registerProvStatusCallback(provStatusCb);
99                 m_remoteResource->provisionEnrollee();
100             }
101             else
102             {
103                 OIC_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_ProvConfig);
107                 if (m_easySetupStatusCb)
108                 {
109                     if (easySetupStatus)
110                     {
111                         m_easySetupStatusCb(easySetupStatus);
112                     }
113                     else
114                     {
115                         m_easySetupStatusCb(nullptr);
116                     }
117                 }
118             }
119         }
120
121         void RemoteEnrollee::provisioningStatusHandler(
122                 std::shared_ptr< ProvisioningStatus > provStatus)
123         {
124             OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Entering ProvisioningStatusHandler");
125
126             OIC_LOG_V(DEBUG,ES_REMOTE_ENROLLEE_TAG,"ProvStatus = %d", provStatus->getESResult());
127
128             std::shared_ptr< EasySetupStatus > easySetupStatus = nullptr;
129
130             if (m_isSecured)
131             {
132                 if (m_currentESState >= CurrentESState::ES_OWNED)
133                 {
134                     goto CALLBACK_CHECK;
135                 }
136                 else
137                 {
138                     goto FAILURE;
139                 }
140             }
141             else
142             {
143                 goto CALLBACK_CHECK;
144             }
145
146             CALLBACK_CHECK:
147
148             if (provStatus->getESResult() == ES_OK)
149             {
150                 if (provStatus->getESState() >= ESState::ES_PROVISIONED_ALREADY)
151                 {
152                     easySetupStatus = std::make_shared< EasySetupStatus >(DEVICE_PROVISIONED,
153                             m_ProvConfig);
154                 }
155                 else
156                 {
157                     easySetupStatus = std::make_shared< EasySetupStatus >(DEVICE_NOT_PROVISIONED,
158                             m_ProvConfig);
159                 }
160             }
161             else
162             {
163                 easySetupStatus = std::make_shared< EasySetupStatus >(DEVICE_NOT_PROVISIONED,
164                         m_ProvConfig);
165             }
166
167             if (m_easySetupStatusCb)
168             {
169                 if (easySetupStatus)
170                 {
171                     m_easySetupStatusCb(easySetupStatus);
172                 }
173                 else
174                 {
175                     m_easySetupStatusCb(nullptr);
176                 }
177             }
178
179             return;
180
181             FAILURE:
182
183             easySetupStatus = std::make_shared< EasySetupStatus >(DEVICE_NOT_PROVISIONED,
184                                     m_ProvConfig);
185
186             if (easySetupStatus)
187             {
188                 m_easySetupStatusCb(easySetupStatus);
189             }
190             else
191             {
192                 m_easySetupStatusCb(nullptr);
193             }
194             return;
195         }
196
197         void RemoteEnrollee::startProvisioning()
198         {
199             OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Entering startProvisioning");
200             if (m_remoteResource == nullptr)
201             {
202                 throw ESBadRequestException ("Device not created");
203             }
204
205             ESResult result = ES_ERROR;
206
207             result = m_remoteResource->constructResourceObject();
208
209             if (result == ES_ERROR)
210             {
211                 OIC_LOG(ERROR,ES_REMOTE_ENROLLEE_TAG,
212                                     "Failed to create device using constructResourceObject");
213                 throw ESBadRequestException ("Device not created");
214             }
215
216             m_currentESState = CurrentESState::ES_ONBOARDED;
217
218 #ifdef __WITH_DTLS__
219             if (m_isSecured && m_currentESState < CurrentESState::ES_OWNED)
220             {
221                 EnrolleeSecStatusCb securityProvStatusCb = std::bind(
222                         &RemoteEnrollee::easySetupSecurityStatusCallback,
223                         this,
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, "");
227
228                 m_enrolleeSecurity->registerCallbackHandler(securityProvStatusCb,
229                         m_securityPinCb, m_secProvisioningDbPathCb);
230
231                 try
232                 {
233                     EasySetupState easySetupState = m_enrolleeSecurity->performOwnershipTransfer();
234                     if (easySetupState == DEVICE_NOT_OWNED)
235                     {
236                         OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_TAG,
237                                 "performOwnershipTransfer returned : %d",
238                                 easySetupState);
239                         return;
240                     }
241                     else if (easySetupState == DEVICE_OWNED)
242                     {
243                         OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_TAG,
244                                 "performOwnershipTransfer returned : %d",
245                                 easySetupState);
246                         OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Before ProvisionEnrollee");
247
248                         RemoteEnrolleeResource::ProvStatusCb provStatusCb = std::bind(
249                                 &RemoteEnrollee::provisioningStatusHandler,
250                                 this, std::placeholders::_1);
251
252                         m_remoteResource->registerProvStatusCallback(provStatusCb);
253                         m_remoteResource->provisionEnrollee();
254                     }
255                 }
256                 catch (OCException & e)
257                 {
258                     OIC_LOG_V(ERROR, ES_REMOTE_ENROLLEE_TAG,
259                             "Exception for performOwnershipTransfer : %s", e.reason().c_str());
260                     return ;
261                 }
262             }
263 #else
264             OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Before ProvisionEnrollee");
265
266             RemoteEnrolleeResource::ProvStatusCb provStatusCb = std::bind(
267                     &RemoteEnrollee::provisioningStatusHandler, this, std::placeholders::_1);
268
269             m_remoteResource->registerProvStatusCallback(provStatusCb);
270             m_remoteResource->provisionEnrollee();
271 #endif
272         }
273
274         void RemoteEnrollee::stopProvisioning()
275         {
276             m_currentESState = CurrentESState::ES_UNKNOWN;
277
278             m_remoteResource->unprovisionEnrollee();
279         }
280
281         bool RemoteEnrollee::isEnrolleeProvisioned()
282         {
283             if(m_currentESState >= CurrentESState::ES_PROVISIONED)
284             {
285                 return true;
286             }
287             else
288             {
289                 return false;
290             }
291         }
292
293         ProvConfig RemoteEnrollee::getProvConfig ()
294         {
295             return m_ProvConfig;
296         }
297
298        WiFiOnboadingConnection RemoteEnrollee::getOnboardConn()
299        {
300          return m_wifiOnboardingconn;
301        }
302
303     }
304 }