1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 #include "EnrolleeResource.h"
25 #include "OCPlatform.h"
26 #include "ESException.h"
27 #include "OCResource.h"
34 #define ES_REMOTE_ENROLLEE_RES_TAG "ES_ENROLLEE_RESOURCE"
36 EnrolleeResource::EnrolleeResource(std::shared_ptr< OC::OCResource > resource)
38 m_ocResource = resource;
41 void EnrolleeResource::onProvisioningResponse(const HeaderOptions& /*headerOptions*/,
42 const OCRepresentation& /*rep*/, const int eCode)
44 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onProvisioningResponse : eCode = %d",
47 if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
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);
57 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
58 "onProvisioningResponse : Provisioning is success. ");
60 std::shared_ptr< DevicePropProvisioningStatus > provStatus = std::make_shared<
61 DevicePropProvisioningStatus >(ESResult::ES_OK);
62 m_devicePropProvStatusCb(provStatus);
65 void EnrolleeResource::onGetStatusResponse(const HeaderOptions& /*headerOptions*/,
66 const OCRepresentation& rep, const int eCode)
68 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onGetStatusResponse : eCode = %d",
71 if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
73 ESResult result = ESResult::ES_ERROR;
75 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
76 "onGetStatusResponse : onGetStatusResponse is failed ");
78 if (eCode == OCStackResult::OC_STACK_UNAUTHORIZED_REQ)
80 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
81 "Mediator is unauthorized from Enrollee.");
82 result = ESResult::ES_UNAUTHORIZED_REQ;
85 EnrolleeStatus enrolleeStatus(rep);
86 std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus = std::make_shared<
87 GetEnrolleeStatus >(result, enrolleeStatus);
89 m_getStatusCb(getEnrolleeStatus);
93 EnrolleeStatus enrolleeStatus(rep);
94 std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus = std::make_shared<
95 GetEnrolleeStatus >(ESResult::ES_OK, enrolleeStatus);
97 m_getStatusCb(getEnrolleeStatus);
101 void EnrolleeResource::onGetConfigurationResponse(const HeaderOptions& /*headerOptions*/,
102 const OCRepresentation& rep, const int eCode)
104 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onGetConfigurationResponse : eCode = %d",
107 if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
109 ESResult result = ESResult::ES_ERROR;
111 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
112 "onGetConfigurationResponse : onGetConfigurationResponse is failed ");
114 if (eCode == OCStackResult::OC_STACK_UNAUTHORIZED_REQ)
116 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
117 "Mediator is unauthorized from Enrollee.");
118 result = ESResult::ES_UNAUTHORIZED_REQ;
121 EnrolleeConf enrolleeConf(rep);
122 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
123 GetConfigurationStatus >(result, enrolleeConf);
124 m_getConfigurationStatusCb(getConfigurationStatus);
128 EnrolleeConf enrolleeConf(rep);
130 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
131 GetConfigurationStatus >(ESResult::ES_OK, enrolleeConf);
132 m_getConfigurationStatusCb(getConfigurationStatus);
136 void EnrolleeResource::registerGetStatusCallback(
137 const GetStatusCb callback)
139 m_getStatusCb = callback;
142 void EnrolleeResource::registerGetConfigurationStatusCallback(
143 const GetConfigurationStatusCb callback)
145 m_getConfigurationStatusCb = callback;
148 void EnrolleeResource::registerDevicePropProvStatusCallback(
149 const DevicePropProvStatusCb callback)
151 m_devicePropProvStatusCb = callback;
154 void EnrolleeResource::getStatus()
156 if (m_ocResource == nullptr)
158 throw ESBadRequestException("Resource is not initialized");
161 OC::QueryParamsMap query;
162 OC::OCRepresentation rep;
164 std::function< OCStackResult(void) > getStatus = [&]
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)));
174 OCStackResult result = getStatus();
176 if (result != OCStackResult::OC_STACK_OK)
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);
182 m_getStatusCb(getEnrolleeStatus);
188 void EnrolleeResource::getConfiguration()
190 if (m_ocResource == nullptr)
192 throw ESBadRequestException("Resource is not initialized");
195 OC::QueryParamsMap query;
196 OC::OCRepresentation rep;
198 std::function< OCStackResult(void) > getConfigurationStatus = [&]
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)));
208 OCStackResult result = getConfigurationStatus();
210 if (result != OCStackResult::OC_STACK_OK)
212 EnrolleeConf enrolleeConf(rep);
213 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
214 GetConfigurationStatus >(ESResult::ES_ERROR, enrolleeConf);
215 m_getConfigurationStatusCb(getConfigurationStatus);
220 void EnrolleeResource::provisionProperties(const DeviceProp& deviceProp)
222 if (m_ocResource == nullptr)
224 throw ESBadRequestException("Resource is not initialized");
227 OC::QueryParamsMap query;
228 OC::OCRepresentation provisioningRepresentation = deviceProp.toOCRepresentation();
230 m_ocResource->post(OC_RSRVD_ES_RES_TYPE_PROV, BATCH_INTERFACE,
231 provisioningRepresentation, QueryParamsMap(),
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)));