Merge branch 'master' into notification-service
[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(
138             const GetStatusCb callback)
139         {
140             m_getStatusCb = callback;
141         }
142
143         void EnrolleeResource::registerGetConfigurationStatusCallback(
144             const GetConfigurationStatusCb callback)
145         {
146             m_getConfigurationStatusCb = callback;
147         }
148
149         void EnrolleeResource::registerDevicePropProvStatusCallback(
150             const DevicePropProvStatusCb callback)
151         {
152             m_devicePropProvStatusCb = callback;
153         }
154
155         void EnrolleeResource::getStatus()
156         {
157             if (m_ocResource == nullptr)
158             {
159                 throw ESBadRequestException("Resource is not initialized");
160             }
161
162             OC::QueryParamsMap query;
163             OC::OCRepresentation rep;
164
165             std::function< OCStackResult(void) > getStatus = [&]
166             {
167                 return m_ocResource->get(m_ocResource->getResourceTypes().at(0),
168                         DEFAULT_INTERFACE, query, std::function<void(const HeaderOptions& headerOptions,
169                         const OCRepresentation& rep, const int eCode) >(
170                                 std::bind(&EnrolleeResource::onGetStatusResponse, this,
171                                         std::placeholders::_1, std::placeholders::_2,
172                                         std::placeholders::_3)));
173             };
174
175             OCStackResult result = getStatus();
176
177             if (result != OCStackResult::OC_STACK_OK)
178             {
179                 EnrolleeStatus enrolleeStatus(rep);// = {ES_STATE_INIT, ES_ERRCODE_NO_ERROR};
180                 std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus = std::make_shared<
181                         GetEnrolleeStatus >(ESResult::ES_ERROR, enrolleeStatus);
182
183                 m_getStatusCb(getEnrolleeStatus);
184
185                 return;
186             }
187         }
188
189         void EnrolleeResource::getConfiguration()
190         {
191             if (m_ocResource == nullptr)
192             {
193                 throw ESBadRequestException("Resource is not initialized");
194             }
195
196             OC::QueryParamsMap query;
197             OC::OCRepresentation rep;
198
199             std::function< OCStackResult(void) > getConfigurationStatus = [&]
200             {
201                 return m_ocResource->get(m_ocResource->getResourceTypes().at(0),
202                         BATCH_INTERFACE, query, std::function<void(const HeaderOptions& headerOptions,
203                         const OCRepresentation& rep, const int eCode) >(
204                                 std::bind(&EnrolleeResource::onGetConfigurationResponse, this,
205                                         std::placeholders::_1, std::placeholders::_2,
206                                         std::placeholders::_3)));
207             };
208
209             OCStackResult result = getConfigurationStatus();
210
211             if (result != OCStackResult::OC_STACK_OK)
212             {
213                 EnrolleeConf enrolleeConf(rep);
214                 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
215                         GetConfigurationStatus >(ESResult::ES_ERROR, enrolleeConf);
216                 m_getConfigurationStatusCb(getConfigurationStatus);
217                 return;
218             }
219         }
220
221         void EnrolleeResource::provisionEnrollee(const DeviceProp& deviceProp)
222         {
223             if (m_ocResource == nullptr)
224             {
225                 throw ESBadRequestException("Resource is not initialized");
226             }
227
228             OC::QueryParamsMap query;
229             OC::OCRepresentation provisioningRepresentation = deviceProp.toOCRepresentation();
230
231             m_ocResource->post(OC_RSRVD_ES_RES_TYPE_PROV, BATCH_INTERFACE,
232                     provisioningRepresentation, QueryParamsMap(),
233                     std::function<
234                             void(const HeaderOptions& headerOptions,
235                                     const OCRepresentation& rep, const int eCode) >(
236                     std::bind(&EnrolleeResource::checkProvInformationCb, this,
237                     std::placeholders::_1, std::placeholders::_2,
238                     std::placeholders::_3)));
239         }
240     }
241 }