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