Merge all changes of easy setup in master branch to 1.2-rel branch
[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         }
40
41         void EnrolleeResource::onProvisioningResponse(const HeaderOptions& /*headerOptions*/,
42                 const OCRepresentation& /*rep*/, const int eCode)
43         {
44             OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onProvisioningResponse : eCode = %d",
45                         eCode);
46
47             if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
48             {
49                 ESResult result = ESResult::ES_ERROR;
50
51                 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
52                             "onProvisioningResponse : Provisioning is failed ");
53
54                 if(eCode == OCStackResult::OC_STACK_COMM_ERROR)
55                 {
56                     OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
57                         "can't receive any response from Enrollee by a timeout threshold.");
58                     result = ESResult::ES_COMMUNICATION_ERROR;
59                 }
60
61                 std::shared_ptr< DevicePropProvisioningStatus > provStatus = std::make_shared<
62                         DevicePropProvisioningStatus >(result);
63                 m_devicePropProvStatusCb(provStatus);
64                 return;
65             }
66
67             OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
68                     "onProvisioningResponse : Provisioning is success. ");
69
70             std::shared_ptr< DevicePropProvisioningStatus > provStatus = std::make_shared<
71                     DevicePropProvisioningStatus >(ESResult::ES_OK);
72             m_devicePropProvStatusCb(provStatus);
73         }
74
75         void EnrolleeResource::onGetStatusResponse(const HeaderOptions& /*headerOptions*/,
76                 const OCRepresentation& rep, const int eCode)
77         {
78             OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onGetStatusResponse : eCode = %d",
79                         eCode);
80
81             if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
82             {
83                 ESResult result = ESResult::ES_ERROR;
84
85                 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
86                             "onGetStatusResponse : onGetStatusResponse is failed ");
87
88                 if(eCode == OCStackResult::OC_STACK_COMM_ERROR)
89                 {
90                     OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
91                         "can't receive any response from Enrollee by a timeout threshold.");
92                     result = ESResult::ES_COMMUNICATION_ERROR;
93                 }
94
95                 EnrolleeStatus enrolleeStatus(rep);
96                 std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus = std::make_shared<
97                         GetEnrolleeStatus >(result, enrolleeStatus);
98
99                 m_getStatusCb(getEnrolleeStatus);
100             }
101             else
102             {
103                 EnrolleeStatus enrolleeStatus(rep);
104                 std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus = std::make_shared<
105                         GetEnrolleeStatus >(ESResult::ES_OK, enrolleeStatus);
106
107                 m_getStatusCb(getEnrolleeStatus);
108             }
109         }
110
111         void EnrolleeResource::onGetConfigurationResponse(const HeaderOptions& /*headerOptions*/,
112                 const OCRepresentation& rep, const int eCode)
113         {
114             OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onGetConfigurationResponse : eCode = %d",
115                         eCode);
116
117             if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
118             {
119                 ESResult result = ESResult::ES_ERROR;
120
121                 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
122                             "onGetConfigurationResponse : onGetConfigurationResponse is failed ");
123
124                 if(eCode == OCStackResult::OC_STACK_COMM_ERROR)
125                 {
126                     OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
127                         "can't receive any response from Enrollee by a timeout threshold.");
128                     result = ESResult::ES_COMMUNICATION_ERROR;
129                 }
130
131                 EnrolleeConf enrolleeConf(rep);
132                 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
133                         GetConfigurationStatus >(result, enrolleeConf);
134                 m_getConfigurationStatusCb(getConfigurationStatus);
135             }
136             else
137             {
138                 EnrolleeConf enrolleeConf(rep);
139
140                 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
141                         GetConfigurationStatus >(ESResult::ES_OK, enrolleeConf);
142                 m_getConfigurationStatusCb(getConfigurationStatus);
143             }
144         }
145
146         void EnrolleeResource::registerGetStatusCallback(
147             const GetStatusCb callback)
148         {
149             m_getStatusCb = callback;
150         }
151
152         void EnrolleeResource::registerGetConfigurationStatusCallback(
153             const GetConfigurationStatusCb callback)
154         {
155             m_getConfigurationStatusCb = callback;
156         }
157
158         void EnrolleeResource::registerDevicePropProvStatusCallback(
159             const DevicePropProvStatusCb callback)
160         {
161             m_devicePropProvStatusCb = callback;
162         }
163
164         void EnrolleeResource::getStatus()
165         {
166             if (m_ocResource == nullptr)
167             {
168                 throw ESBadRequestException("Resource is not initialized");
169             }
170
171             OC::QueryParamsMap query;
172             OC::OCRepresentation rep;
173
174             std::function< OCStackResult(void) > getStatus = [&]
175             {
176                 return m_ocResource->get(m_ocResource->getResourceTypes().at(0),
177                         DEFAULT_INTERFACE, query, std::function<void(const HeaderOptions& headerOptions,
178                         const OCRepresentation& rep, const int eCode) >(
179                                 std::bind(&EnrolleeResource::onGetStatusResponse, this,
180                                         std::placeholders::_1, std::placeholders::_2,
181                                         std::placeholders::_3)), OC::QualityOfService::HighQos);
182             };
183
184             OCStackResult result = getStatus();
185
186             if (result != OCStackResult::OC_STACK_OK)
187             {
188                 EnrolleeStatus enrolleeStatus(rep);// = {ES_STATE_INIT, ES_ERRCODE_NO_ERROR};
189                 std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus = std::make_shared<
190                         GetEnrolleeStatus >(ESResult::ES_ERROR, enrolleeStatus);
191
192                 m_getStatusCb(getEnrolleeStatus);
193
194                 return;
195             }
196         }
197
198         void EnrolleeResource::getConfiguration()
199         {
200             if (m_ocResource == nullptr)
201             {
202                 throw ESBadRequestException("Resource is not initialized");
203             }
204
205             OC::QueryParamsMap query;
206             OC::OCRepresentation rep;
207
208             std::function< OCStackResult(void) > getConfigurationStatus = [&]
209             {
210                 return m_ocResource->get(m_ocResource->getResourceTypes().at(0),
211                         BATCH_INTERFACE, query, std::function<void(const HeaderOptions& headerOptions,
212                         const OCRepresentation& rep, const int eCode) >(
213                                 std::bind(&EnrolleeResource::onGetConfigurationResponse, this,
214                                         std::placeholders::_1, std::placeholders::_2,
215                                         std::placeholders::_3)), OC::QualityOfService::HighQos);
216             };
217
218             OCStackResult result = getConfigurationStatus();
219
220             if (result != OCStackResult::OC_STACK_OK)
221             {
222                 EnrolleeConf enrolleeConf(rep);
223                 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
224                         GetConfigurationStatus >(ESResult::ES_ERROR, enrolleeConf);
225                 m_getConfigurationStatusCb(getConfigurationStatus);
226                 return;
227             }
228         }
229
230         void EnrolleeResource::provisionProperties(const DeviceProp& deviceProp)
231         {
232             if (m_ocResource == nullptr)
233             {
234                 throw ESBadRequestException("Resource is not initialized");
235             }
236
237             OC::QueryParamsMap query;
238             OC::OCRepresentation provisioningRepresentation = deviceProp.toOCRepresentation();
239
240             m_ocResource->post(OC_RSRVD_ES_RES_TYPE_PROV, BATCH_INTERFACE,
241                     provisioningRepresentation, QueryParamsMap(),
242                     std::function<
243                             void(const HeaderOptions& headerOptions,
244                                     const OCRepresentation& rep, const int eCode) >(
245                     std::bind(&EnrolleeResource::onProvisioningResponse, this,
246                     std::placeholders::_1, std::placeholders::_2,
247                     std::placeholders::_3)), OC::QualityOfService::HighQos);
248         }
249     }
250 }