replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / src / EnrolleeResource.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 <functional>
22
23 #include "EnrolleeResource.h"
24
25 #include "OCPlatform.h"
26 #include "ESException.h"
27 #include "OCResource.h"
28 #include "logger.h"
29
30 namespace OIC
31 {
32     namespace Service
33     {
34         #define ES_REMOTE_ENROLLEE_RES_TAG "ES_ENROLLEE_RESOURCE"
35
36         EnrolleeResource::EnrolleeResource(std::shared_ptr< OC::OCResource > resource)
37         {
38             m_ocResource = resource;
39             m_getStatusCb = nullptr;
40             m_getConfigurationStatusCb = nullptr;
41             m_devicePropProvStatusCb = nullptr;
42             m_connectRequestStatusCb = nullptr;
43         }
44
45         void EnrolleeResource::onEnrolleeResourceSafetyCB(const HeaderOptions& headerOptions,
46                                                         const OCRepresentation& rep,
47                                                         const int eCode,
48                                                         ESEnrolleeResourceCb cb,
49                                                         std::weak_ptr<EnrolleeResource> this_ptr)
50         {
51             OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onEnrolleeResourceSafetyCB");
52             std::shared_ptr<EnrolleeResource> Ptr = this_ptr.lock();
53             if(Ptr)
54             {
55                 cb(headerOptions, rep, eCode);
56             }
57         }
58
59         void EnrolleeResource::onProvisioningResponse(const HeaderOptions& /*headerOptions*/,
60                 const OCRepresentation& /*rep*/, const int eCode)
61         {
62             OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onProvisioningResponse : eCode = %d",
63                         eCode);
64
65             if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
66             {
67                 ESResult result = ESResult::ES_ERROR;
68
69                 OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_RES_TAG,
70                             "onProvisioningResponse : Provisioning is failed ");
71
72                 if(eCode == OCStackResult::OC_STACK_COMM_ERROR)
73                 {
74                     OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_RES_TAG,
75                         "can't receive any response from Enrollee by a timeout threshold.");
76                     result = ESResult::ES_COMMUNICATION_ERROR;
77                 }
78
79                 std::shared_ptr< DevicePropProvisioningStatus > provStatus = std::make_shared<
80                         DevicePropProvisioningStatus >(result);
81                 m_devicePropProvStatusCb(provStatus);
82                 return;
83             }
84
85             OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
86                     "onProvisioningResponse : Provisioning is success. ");
87
88             std::shared_ptr< DevicePropProvisioningStatus > provStatus = std::make_shared<
89                     DevicePropProvisioningStatus >(ESResult::ES_OK);
90             m_devicePropProvStatusCb(provStatus);
91         }
92
93         void EnrolleeResource::onGetStatusResponse(const HeaderOptions& /*headerOptions*/,
94                 const OCRepresentation& rep, const int eCode)
95         {
96             OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onGetStatusResponse : eCode = %d",
97                         eCode);
98
99             if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
100             {
101                 ESResult result = ESResult::ES_ERROR;
102
103                 OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_RES_TAG,
104                             "onGetStatusResponse : onGetStatusResponse is failed ");
105
106                 if(eCode == OCStackResult::OC_STACK_COMM_ERROR)
107                 {
108                     OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_RES_TAG,
109                         "can't receive any response from Enrollee by a timeout threshold.");
110                     result = ESResult::ES_COMMUNICATION_ERROR;
111                 }
112
113                 EnrolleeStatus enrolleeStatus(rep);
114                 std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus = std::make_shared<
115                         GetEnrolleeStatus >(result, enrolleeStatus);
116
117                 m_getStatusCb(getEnrolleeStatus);
118             }
119             else
120             {
121                 EnrolleeStatus enrolleeStatus(rep);
122                 std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus = std::make_shared<
123                         GetEnrolleeStatus >(ESResult::ES_OK, enrolleeStatus);
124
125                 m_getStatusCb(getEnrolleeStatus);
126             }
127         }
128
129         void EnrolleeResource::onGetConfigurationResponse(const HeaderOptions& /*headerOptions*/,
130                 const OCRepresentation& rep, const int eCode)
131         {
132             OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onGetConfigurationResponse : eCode = %d",
133                         eCode);
134
135             if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
136             {
137                 ESResult result = ESResult::ES_ERROR;
138
139                 OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_RES_TAG,
140                             "onGetConfigurationResponse : onGetConfigurationResponse is failed ");
141
142                 if(eCode == OCStackResult::OC_STACK_COMM_ERROR)
143                 {
144                     OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_RES_TAG,
145                         "can't receive any response from Enrollee by a timeout threshold.");
146                     result = ESResult::ES_COMMUNICATION_ERROR;
147                 }
148
149                 EnrolleeConf enrolleeConf(rep);
150                 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
151                         GetConfigurationStatus >(result, enrolleeConf);
152                 m_getConfigurationStatusCb(getConfigurationStatus);
153             }
154             else
155             {
156                 EnrolleeConf enrolleeConf(rep);
157
158                 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
159                         GetConfigurationStatus >(ESResult::ES_OK, enrolleeConf);
160                 m_getConfigurationStatusCb(getConfigurationStatus);
161             }
162         }
163
164          void EnrolleeResource::onConnectRequestResponse(const HeaderOptions& /*headerOptions*/,
165                 const OCRepresentation& /*rep*/, const int eCode)
166         {
167             OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onConnectRequestResponse : eCode = %d",
168                         eCode);
169
170             if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
171             {
172                 ESResult result = ESResult::ES_ERROR;
173
174                 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
175                             "onConnectRequestResponse : onConnectRequestResponse is failed ");
176
177                 if(eCode == OCStackResult::OC_STACK_COMM_ERROR)
178                 {
179                     OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
180                         "can't receive any response from Enrollee by a timeout threshold.");
181                     result = ESResult::ES_COMMUNICATION_ERROR;
182                 }
183
184                 std::shared_ptr< ConnectRequestStatus > connectRequestStatus = std::make_shared<
185                         ConnectRequestStatus >(result);
186                 m_connectRequestStatusCb(connectRequestStatus);
187                 return;
188             }
189
190             OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
191                     "onConnectRequestResponse : Provisioning is success. ");
192
193             std::shared_ptr< ConnectRequestStatus > connectRequestStatus = std::make_shared<
194                     ConnectRequestStatus >(ESResult::ES_OK);
195             m_connectRequestStatusCb(connectRequestStatus);
196         }
197
198
199         void EnrolleeResource::registerGetStatusCallback(
200             const GetStatusCb callback)
201         {
202             m_getStatusCb = callback;
203         }
204
205         void EnrolleeResource::registerGetConfigurationStatusCallback(
206             const GetConfigurationStatusCb callback)
207         {
208             m_getConfigurationStatusCb = callback;
209         }
210
211         void EnrolleeResource::registerDevicePropProvStatusCallback(
212             const DevicePropProvStatusCb callback)
213         {
214             m_devicePropProvStatusCb = callback;
215         }
216
217         void EnrolleeResource::registerConnectRequestStatusCallback(
218             const ConnectRequestStatusCb callback)
219         {
220             m_connectRequestStatusCb = callback;
221         }
222
223         void EnrolleeResource::getStatus()
224         {
225             OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getStatus IN");
226
227             if (m_ocResource == nullptr)
228             {
229                 throw ESBadRequestException("Resource is not initialized");
230             }
231
232             OC::QueryParamsMap query;
233             OC::OCRepresentation rep;
234
235             std::function< OCStackResult(void) > getStatus = [&]
236             {
237                 ESEnrolleeResourceCb cb = std::bind(&EnrolleeResource::onEnrolleeResourceSafetyCB,
238                                 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
239                                 static_cast<ESEnrolleeResourceCb>(
240                                 std::bind(&EnrolleeResource::onGetStatusResponse, this,
241                                 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
242                                 shared_from_this());
243
244                 return m_ocResource->get(m_ocResource->getResourceTypes().at(0),
245                         DEFAULT_INTERFACE, query, cb, OC::QualityOfService::HighQos);
246             };
247
248             OCStackResult result = getStatus();
249
250             if (result != OCStackResult::OC_STACK_OK)
251             {
252                 EnrolleeStatus enrolleeStatus(rep);// = {ES_STATE_INIT, ES_ERRCODE_NO_ERROR};
253                 std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus = std::make_shared<
254                         GetEnrolleeStatus >(ESResult::ES_ERROR, enrolleeStatus);
255
256                 m_getStatusCb(getEnrolleeStatus);
257
258                 return;
259             }
260             OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getStatus OUT");
261         }
262
263         void EnrolleeResource::getConfiguration()
264         {
265             OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getConfiguration IN");
266
267             if (m_ocResource == nullptr)
268             {
269                 throw ESBadRequestException("Resource is not initialized");
270             }
271
272             OC::QueryParamsMap query;
273             OC::OCRepresentation rep;
274
275             std::function< OCStackResult(void) > getConfigurationStatus = [&]
276             {
277                 ESEnrolleeResourceCb cb = std::bind(&EnrolleeResource::onEnrolleeResourceSafetyCB,
278                                 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
279                                 static_cast<ESEnrolleeResourceCb>(
280                                 std::bind(&EnrolleeResource::onGetConfigurationResponse, this,
281                                 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
282                                 shared_from_this());
283
284                 return m_ocResource->get(m_ocResource->getResourceTypes().at(0),
285                         BATCH_INTERFACE, query, cb, OC::QualityOfService::HighQos);
286             };
287
288             OCStackResult result = getConfigurationStatus();
289
290             if (result != OCStackResult::OC_STACK_OK)
291             {
292                 EnrolleeConf enrolleeConf(rep);
293                 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
294                         GetConfigurationStatus >(ESResult::ES_ERROR, enrolleeConf);
295                 m_getConfigurationStatusCb(getConfigurationStatus);
296                 return;
297             }
298
299             OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getConfiguration OUT");
300         }
301
302         void EnrolleeResource::provisionProperties(const DeviceProp& deviceProp)
303         {
304             OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "provisionProperties IN");
305             if (m_ocResource == nullptr)
306             {
307                 throw ESBadRequestException("Resource is not initialized");
308             }
309
310             OC::QueryParamsMap query;
311             OC::OCRepresentation provisioningRepresentation = deviceProp.toOCRepresentation();
312
313             ESEnrolleeResourceCb cb = std::bind(&EnrolleeResource::onEnrolleeResourceSafetyCB,
314                             std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
315                             static_cast<ESEnrolleeResourceCb>(
316                             std::bind(&EnrolleeResource::onProvisioningResponse, this,
317                             std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
318                             shared_from_this());
319
320             m_ocResource->post(OC_RSRVD_ES_RES_TYPE_EASYSETUP, BATCH_INTERFACE,
321                     provisioningRepresentation, QueryParamsMap(), cb, OC::QualityOfService::HighQos);
322
323             OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "provisionProperties OUT");
324         }
325
326         void EnrolleeResource::requestToConnect(const std::vector<ES_CONNECT_TYPE> &connectTypes)
327         {
328             OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "requestToConnect IN");
329             if (m_ocResource == nullptr)
330             {
331                 throw ESBadRequestException("Resource is not initialized");
332             }
333
334             OC::QueryParamsMap query;
335             OC::OCRepresentation requestRepresentation;
336             std::vector<int> connectTypes_int;
337             connectTypes_int.clear();
338
339             for(auto it : connectTypes)
340             {
341                 connectTypes_int.push_back(static_cast<int>(it));
342             }
343
344             requestRepresentation.setValue<std::vector<int>>(OC_RSRVD_ES_CONNECT, connectTypes_int);
345
346             ESEnrolleeResourceCb cb = std::bind(&EnrolleeResource::onEnrolleeResourceSafetyCB,
347                             std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
348                             static_cast<ESEnrolleeResourceCb>(
349                             std::bind(&EnrolleeResource::onConnectRequestResponse, this,
350                             std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
351                             shared_from_this());
352
353             m_ocResource->post(OC_RSRVD_ES_RES_TYPE_EASYSETUP, OC_RSRVD_INTERFACE_DEFAULT,
354                     requestRepresentation, QueryParamsMap(), cb, OC::QualityOfService::HighQos);
355
356             OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "requestToConnect OUT");
357         }
358     }
359 }