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