Add more descriptive log messages in easy setup
[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             OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getStatus IN");
167
168             if (m_ocResource == nullptr)
169             {
170                 throw ESBadRequestException("Resource is not initialized");
171             }
172
173             OC::QueryParamsMap query;
174             OC::OCRepresentation rep;
175
176             std::function< OCStackResult(void) > getStatus = [&]
177             {
178                 return m_ocResource->get(m_ocResource->getResourceTypes().at(0),
179                         DEFAULT_INTERFACE, query, std::function<void(const HeaderOptions& headerOptions,
180                         const OCRepresentation& rep, const int eCode) >(
181                                 std::bind(&EnrolleeResource::onGetStatusResponse, this,
182                                         std::placeholders::_1, std::placeholders::_2,
183                                         std::placeholders::_3)), OC::QualityOfService::HighQos);
184             };
185
186             OCStackResult result = getStatus();
187
188             if (result != OCStackResult::OC_STACK_OK)
189             {
190                 EnrolleeStatus enrolleeStatus(rep);// = {ES_STATE_INIT, ES_ERRCODE_NO_ERROR};
191                 std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus = std::make_shared<
192                         GetEnrolleeStatus >(ESResult::ES_ERROR, enrolleeStatus);
193
194                 m_getStatusCb(getEnrolleeStatus);
195
196                 return;
197             }
198             OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getStatus OUT");
199         }
200
201         void EnrolleeResource::getConfiguration()
202         {
203             OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getConfiguration IN");
204
205             if (m_ocResource == nullptr)
206             {
207                 throw ESBadRequestException("Resource is not initialized");
208             }
209
210             OC::QueryParamsMap query;
211             OC::OCRepresentation rep;
212
213             std::function< OCStackResult(void) > getConfigurationStatus = [&]
214             {
215                 return m_ocResource->get(m_ocResource->getResourceTypes().at(0),
216                         BATCH_INTERFACE, query, std::function<void(const HeaderOptions& headerOptions,
217                         const OCRepresentation& rep, const int eCode) >(
218                                 std::bind(&EnrolleeResource::onGetConfigurationResponse, this,
219                                         std::placeholders::_1, std::placeholders::_2,
220                                         std::placeholders::_3)), OC::QualityOfService::HighQos);
221             };
222
223             OCStackResult result = getConfigurationStatus();
224
225             if (result != OCStackResult::OC_STACK_OK)
226             {
227                 EnrolleeConf enrolleeConf(rep);
228                 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
229                         GetConfigurationStatus >(ESResult::ES_ERROR, enrolleeConf);
230                 m_getConfigurationStatusCb(getConfigurationStatus);
231                 return;
232             }
233
234             OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getConfiguration OUT");
235         }
236
237         void EnrolleeResource::provisionProperties(const DeviceProp& deviceProp)
238         {
239             OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "provisionProperties IN");
240             if (m_ocResource == nullptr)
241             {
242                 throw ESBadRequestException("Resource is not initialized");
243             }
244
245             OC::QueryParamsMap query;
246             OC::OCRepresentation provisioningRepresentation = deviceProp.toOCRepresentation();
247
248             m_ocResource->post(OC_RSRVD_ES_RES_TYPE_PROV, BATCH_INTERFACE,
249                     provisioningRepresentation, QueryParamsMap(),
250                     std::function<
251                             void(const HeaderOptions& headerOptions,
252                                     const OCRepresentation& rep, const int eCode) >(
253                     std::bind(&EnrolleeResource::onProvisioningResponse, this,
254                     std::placeholders::_1, std::placeholders::_2,
255                     std::placeholders::_3)), OC::QualityOfService::HighQos);
256
257             OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "provisionProperties OUT");
258         }
259     }
260 }