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 ESResult result = ESResult::ES_ERROR;
51 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
52 "onProvisioningResponse : Provisioning is failed ");
54 if(eCode == OCStackResult::OC_STACK_COMM_ERROR)
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;
61 std::shared_ptr< DevicePropProvisioningStatus > provStatus = std::make_shared<
62 DevicePropProvisioningStatus >(result);
63 m_devicePropProvStatusCb(provStatus);
67 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
68 "onProvisioningResponse : Provisioning is success. ");
70 std::shared_ptr< DevicePropProvisioningStatus > provStatus = std::make_shared<
71 DevicePropProvisioningStatus >(ESResult::ES_OK);
72 m_devicePropProvStatusCb(provStatus);
75 void EnrolleeResource::onGetStatusResponse(const HeaderOptions& /*headerOptions*/,
76 const OCRepresentation& rep, const int eCode)
78 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onGetStatusResponse : eCode = %d",
81 if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
83 ESResult result = ESResult::ES_ERROR;
85 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
86 "onGetStatusResponse : onGetStatusResponse is failed ");
88 if(eCode == OCStackResult::OC_STACK_COMM_ERROR)
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;
95 EnrolleeStatus enrolleeStatus(rep);
96 std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus = std::make_shared<
97 GetEnrolleeStatus >(result, enrolleeStatus);
99 m_getStatusCb(getEnrolleeStatus);
103 EnrolleeStatus enrolleeStatus(rep);
104 std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus = std::make_shared<
105 GetEnrolleeStatus >(ESResult::ES_OK, enrolleeStatus);
107 m_getStatusCb(getEnrolleeStatus);
111 void EnrolleeResource::onGetConfigurationResponse(const HeaderOptions& /*headerOptions*/,
112 const OCRepresentation& rep, const int eCode)
114 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onGetConfigurationResponse : eCode = %d",
117 if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
119 ESResult result = ESResult::ES_ERROR;
121 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
122 "onGetConfigurationResponse : onGetConfigurationResponse is failed ");
124 if(eCode == OCStackResult::OC_STACK_COMM_ERROR)
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;
131 EnrolleeConf enrolleeConf(rep);
132 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
133 GetConfigurationStatus >(result, enrolleeConf);
134 m_getConfigurationStatusCb(getConfigurationStatus);
138 EnrolleeConf enrolleeConf(rep);
140 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
141 GetConfigurationStatus >(ESResult::ES_OK, enrolleeConf);
142 m_getConfigurationStatusCb(getConfigurationStatus);
146 void EnrolleeResource::registerGetStatusCallback(
147 const GetStatusCb callback)
149 m_getStatusCb = callback;
152 void EnrolleeResource::registerGetConfigurationStatusCallback(
153 const GetConfigurationStatusCb callback)
155 m_getConfigurationStatusCb = callback;
158 void EnrolleeResource::registerDevicePropProvStatusCallback(
159 const DevicePropProvStatusCb callback)
161 m_devicePropProvStatusCb = callback;
164 void EnrolleeResource::getStatus()
166 OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getStatus IN");
168 if (m_ocResource == nullptr)
170 throw ESBadRequestException("Resource is not initialized");
173 OC::QueryParamsMap query;
174 OC::OCRepresentation rep;
176 std::function< OCStackResult(void) > getStatus = [&]
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);
186 OCStackResult result = getStatus();
188 if (result != OCStackResult::OC_STACK_OK)
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);
194 m_getStatusCb(getEnrolleeStatus);
198 OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getStatus OUT");
201 void EnrolleeResource::getConfiguration()
203 OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getConfiguration IN");
205 if (m_ocResource == nullptr)
207 throw ESBadRequestException("Resource is not initialized");
210 OC::QueryParamsMap query;
211 OC::OCRepresentation rep;
213 std::function< OCStackResult(void) > getConfigurationStatus = [&]
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);
223 OCStackResult result = getConfigurationStatus();
225 if (result != OCStackResult::OC_STACK_OK)
227 EnrolleeConf enrolleeConf(rep);
228 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
229 GetConfigurationStatus >(ESResult::ES_ERROR, enrolleeConf);
230 m_getConfigurationStatusCb(getConfigurationStatus);
234 OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getConfiguration OUT");
237 void EnrolleeResource::provisionProperties(const DeviceProp& deviceProp)
239 OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "provisionProperties IN");
240 if (m_ocResource == nullptr)
242 throw ESBadRequestException("Resource is not initialized");
245 OC::QueryParamsMap query;
246 OC::OCRepresentation provisioningRepresentation = deviceProp.toOCRepresentation();
248 m_ocResource->post(OC_RSRVD_ES_RES_TYPE_PROV, BATCH_INTERFACE,
249 provisioningRepresentation, QueryParamsMap(),
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);
257 OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "provisionProperties OUT");