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;
39 m_getStatusCb = nullptr;
40 m_getConfigurationStatusCb = nullptr;
41 m_devicePropProvStatusCb = nullptr;
42 m_connectRequestStatusCb = nullptr;
45 void EnrolleeResource::onEnrolleeResourceSafetyCB(const HeaderOptions& headerOptions,
46 const OCRepresentation& rep,
48 ESEnrolleeResourceCb cb,
49 std::weak_ptr<EnrolleeResource> this_ptr)
51 OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onEnrolleeResourceSafetyCB");
52 std::shared_ptr<EnrolleeResource> Ptr = this_ptr.lock();
55 cb(headerOptions, rep, eCode);
59 void EnrolleeResource::onProvisioningResponse(const HeaderOptions& /*headerOptions*/,
60 const OCRepresentation& /*rep*/, const int eCode)
62 OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onProvisioningResponse : eCode = %d",
65 if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
67 ESResult result = ESResult::ES_ERROR;
69 OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_RES_TAG,
70 "onProvisioningResponse : Provisioning is failed ");
72 if(eCode == OCStackResult::OC_STACK_COMM_ERROR)
74 OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_RES_TAG,
75 "can't receive any response from Enrollee by a timeout threshold.");
76 result = ESResult::ES_COMMUNICATION_ERROR;
79 std::shared_ptr< DevicePropProvisioningStatus > provStatus = std::make_shared<
80 DevicePropProvisioningStatus >(result);
81 m_devicePropProvStatusCb(provStatus);
85 OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
86 "onProvisioningResponse : Provisioning is success. ");
88 std::shared_ptr< DevicePropProvisioningStatus > provStatus = std::make_shared<
89 DevicePropProvisioningStatus >(ESResult::ES_OK);
90 m_devicePropProvStatusCb(provStatus);
93 void EnrolleeResource::onGetStatusResponse(const HeaderOptions& /*headerOptions*/,
94 const OCRepresentation& rep, const int eCode)
96 OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onGetStatusResponse : eCode = %d",
99 if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
101 ESResult result = ESResult::ES_ERROR;
103 OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_RES_TAG,
104 "onGetStatusResponse : onGetStatusResponse is failed ");
106 if(eCode == OCStackResult::OC_STACK_COMM_ERROR)
108 OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_RES_TAG,
109 "can't receive any response from Enrollee by a timeout threshold.");
110 result = ESResult::ES_COMMUNICATION_ERROR;
113 EnrolleeStatus enrolleeStatus(rep);
114 std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus = std::make_shared<
115 GetEnrolleeStatus >(result, enrolleeStatus);
117 m_getStatusCb(getEnrolleeStatus);
121 EnrolleeStatus enrolleeStatus(rep);
122 std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus = std::make_shared<
123 GetEnrolleeStatus >(ESResult::ES_OK, enrolleeStatus);
125 m_getStatusCb(getEnrolleeStatus);
129 void EnrolleeResource::onGetConfigurationResponse(const HeaderOptions& /*headerOptions*/,
130 const OCRepresentation& rep, const int eCode)
132 OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onGetConfigurationResponse : eCode = %d",
135 if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
137 ESResult result = ESResult::ES_ERROR;
139 OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_RES_TAG,
140 "onGetConfigurationResponse : onGetConfigurationResponse is failed ");
142 if(eCode == OCStackResult::OC_STACK_COMM_ERROR)
144 OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_RES_TAG,
145 "can't receive any response from Enrollee by a timeout threshold.");
146 result = ESResult::ES_COMMUNICATION_ERROR;
149 EnrolleeConf enrolleeConf(rep);
150 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
151 GetConfigurationStatus >(result, enrolleeConf);
152 m_getConfigurationStatusCb(getConfigurationStatus);
156 EnrolleeConf enrolleeConf(rep);
158 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
159 GetConfigurationStatus >(ESResult::ES_OK, enrolleeConf);
160 m_getConfigurationStatusCb(getConfigurationStatus);
164 void EnrolleeResource::onConnectRequestResponse(const HeaderOptions& /*headerOptions*/,
165 const OCRepresentation& /*rep*/, const int eCode)
167 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onConnectRequestResponse : eCode = %d",
170 if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
172 ESResult result = ESResult::ES_ERROR;
174 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
175 "onConnectRequestResponse : onConnectRequestResponse is failed ");
177 if(eCode == OCStackResult::OC_STACK_COMM_ERROR)
179 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
180 "can't receive any response from Enrollee by a timeout threshold.");
181 result = ESResult::ES_COMMUNICATION_ERROR;
184 std::shared_ptr< ConnectRequestStatus > connectRequestStatus = std::make_shared<
185 ConnectRequestStatus >(result);
186 m_connectRequestStatusCb(connectRequestStatus);
190 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
191 "onConnectRequestResponse : Provisioning is success. ");
193 std::shared_ptr< ConnectRequestStatus > connectRequestStatus = std::make_shared<
194 ConnectRequestStatus >(ESResult::ES_OK);
195 m_connectRequestStatusCb(connectRequestStatus);
199 void EnrolleeResource::registerGetStatusCallback(
200 const GetStatusCb callback)
202 m_getStatusCb = callback;
205 void EnrolleeResource::registerGetConfigurationStatusCallback(
206 const GetConfigurationStatusCb callback)
208 m_getConfigurationStatusCb = callback;
211 void EnrolleeResource::registerDevicePropProvStatusCallback(
212 const DevicePropProvStatusCb callback)
214 m_devicePropProvStatusCb = callback;
217 void EnrolleeResource::registerConnectRequestStatusCallback(
218 const ConnectRequestStatusCb callback)
220 m_connectRequestStatusCb = callback;
223 void EnrolleeResource::getStatus()
225 OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getStatus IN");
227 if (m_ocResource == nullptr)
229 throw ESBadRequestException("Resource is not initialized");
232 OC::QueryParamsMap query;
233 OC::OCRepresentation rep;
235 std::function< OCStackResult(void) > getStatus = [&]
237 ESEnrolleeResourceCb cb = std::bind(&EnrolleeResource::onEnrolleeResourceSafetyCB,
238 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
239 static_cast<ESEnrolleeResourceCb>(
240 std::bind(&EnrolleeResource::onGetStatusResponse, this,
241 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
244 return m_ocResource->get(m_ocResource->getResourceTypes().at(0),
245 DEFAULT_INTERFACE, query, cb, OC::QualityOfService::HighQos);
248 OCStackResult result = getStatus();
250 if (result != OCStackResult::OC_STACK_OK)
252 EnrolleeStatus enrolleeStatus(rep);// = {ES_STATE_INIT, ES_ERRCODE_NO_ERROR};
253 std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus = std::make_shared<
254 GetEnrolleeStatus >(ESResult::ES_ERROR, enrolleeStatus);
256 m_getStatusCb(getEnrolleeStatus);
260 OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getStatus OUT");
263 void EnrolleeResource::getConfiguration()
265 OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getConfiguration IN");
267 if (m_ocResource == nullptr)
269 throw ESBadRequestException("Resource is not initialized");
272 OC::QueryParamsMap query;
273 OC::OCRepresentation rep;
275 std::function< OCStackResult(void) > getConfigurationStatus = [&]
277 ESEnrolleeResourceCb cb = std::bind(&EnrolleeResource::onEnrolleeResourceSafetyCB,
278 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
279 static_cast<ESEnrolleeResourceCb>(
280 std::bind(&EnrolleeResource::onGetConfigurationResponse, this,
281 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
284 return m_ocResource->get(m_ocResource->getResourceTypes().at(0),
285 BATCH_INTERFACE, query, cb, OC::QualityOfService::HighQos);
288 OCStackResult result = getConfigurationStatus();
290 if (result != OCStackResult::OC_STACK_OK)
292 EnrolleeConf enrolleeConf(rep);
293 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
294 GetConfigurationStatus >(ESResult::ES_ERROR, enrolleeConf);
295 m_getConfigurationStatusCb(getConfigurationStatus);
299 OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getConfiguration OUT");
302 void EnrolleeResource::provisionProperties(const DeviceProp& deviceProp)
304 OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "provisionProperties IN");
305 if (m_ocResource == nullptr)
307 throw ESBadRequestException("Resource is not initialized");
310 OC::QueryParamsMap query;
311 OC::OCRepresentation provisioningRepresentation = deviceProp.toOCRepresentation();
313 ESEnrolleeResourceCb cb = std::bind(&EnrolleeResource::onEnrolleeResourceSafetyCB,
314 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
315 static_cast<ESEnrolleeResourceCb>(
316 std::bind(&EnrolleeResource::onProvisioningResponse, this,
317 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
320 m_ocResource->post(OC_RSRVD_ES_RES_TYPE_EASYSETUP, BATCH_INTERFACE,
321 provisioningRepresentation, QueryParamsMap(), cb, OC::QualityOfService::HighQos);
323 OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "provisionProperties OUT");
326 void EnrolleeResource::requestToConnect(const std::vector<ES_CONNECT_TYPE> &connectTypes)
328 OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "requestToConnect IN");
329 if (m_ocResource == nullptr)
331 throw ESBadRequestException("Resource is not initialized");
334 OC::QueryParamsMap query;
335 OC::OCRepresentation requestRepresentation;
336 std::vector<int> connectTypes_int;
337 connectTypes_int.clear();
339 for(auto it : connectTypes)
341 connectTypes_int.push_back(static_cast<int>(it));
344 requestRepresentation.setValue<std::vector<int>>(OC_RSRVD_ES_CONNECT, connectTypes_int);
346 ESEnrolleeResourceCb cb = std::bind(&EnrolleeResource::onEnrolleeResourceSafetyCB,
347 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
348 static_cast<ESEnrolleeResourceCb>(
349 std::bind(&EnrolleeResource::onConnectRequestResponse, this,
350 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
353 m_ocResource->post(OC_RSRVD_ES_RES_TYPE_EASYSETUP, OC_RSRVD_INTERFACE_DEFAULT,
354 requestRepresentation, QueryParamsMap(), cb, OC::QualityOfService::HighQos);
356 OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "requestToConnect OUT");