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::checkProvInformationCb(const HeaderOptions& /*headerOptions*/,
42 const OCRepresentation& rep, const int eCode)
44 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "checkProvInformationCb : %s, eCode = %d",
48 if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
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);
58 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
59 "checkProvInformationCb : Provisioning is success. ");
61 std::shared_ptr< DevicePropProvisioningStatus > provStatus = std::make_shared<
62 DevicePropProvisioningStatus >(ESResult::ES_OK);
63 m_devicePropProvStatusCb(provStatus);
66 void EnrolleeResource::onGetStatusResponse(const HeaderOptions& /*headerOptions*/,
67 const OCRepresentation& rep, const int eCode)
69 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onGetStatusResponse : %s, eCode = %d",
70 rep.getUri().c_str(), eCode);
72 if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
74 ESResult result = ESResult::ES_ERROR;
76 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
77 "onGetStatusResponse : onGetStatusResponse is failed ");
79 if (eCode == OCStackResult::OC_STACK_UNAUTHORIZED_REQ)
81 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
82 "Mediator is unauthorized from Enrollee.");
83 result = ESResult::ES_UNAUTHORIZED;
86 EnrolleeStatus enrolleeStatus(rep);
87 std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus = std::make_shared<
88 GetEnrolleeStatus >(result, enrolleeStatus);
90 m_getStatusCb(getEnrolleeStatus);
94 EnrolleeStatus enrolleeStatus(rep);
95 std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus = std::make_shared<
96 GetEnrolleeStatus >(ESResult::ES_OK, enrolleeStatus);
98 m_getStatusCb(getEnrolleeStatus);
102 void EnrolleeResource::onGetConfigurationResponse(const HeaderOptions& /*headerOptions*/,
103 const OCRepresentation& rep, const int eCode)
105 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onGetConfigurationResponse : %s, eCode = %d",
106 rep.getUri().c_str(), eCode);
108 if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
110 ESResult result = ESResult::ES_ERROR;
112 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
113 "onGetConfigurationResponse : onGetConfigurationResponse is failed ");
115 if (eCode == OCStackResult::OC_STACK_UNAUTHORIZED_REQ)
117 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
118 "Mediator is unauthorized from Enrollee.");
119 result = ESResult::ES_UNAUTHORIZED;
122 EnrolleeConf enrolleeConf(rep);
123 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
124 GetConfigurationStatus >(result, enrolleeConf);
125 m_getConfigurationStatusCb(getConfigurationStatus);
129 EnrolleeConf enrolleeConf(rep);
131 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
132 GetConfigurationStatus >(ESResult::ES_OK, enrolleeConf);
133 m_getConfigurationStatusCb(getConfigurationStatus);
137 void EnrolleeResource::registerGetStatusCallback(GetStatusCb callback)
139 m_getStatusCb = callback;
142 void EnrolleeResource::registerGetConfigurationStatusCallback(GetConfigurationStatusCb callback)
144 m_getConfigurationStatusCb = callback;
147 void EnrolleeResource::registerDevicePropProvStatusCallback(DevicePropProvStatusCb callback)
149 m_devicePropProvStatusCb = callback;
152 void EnrolleeResource::getStatus()
154 if (m_ocResource == nullptr)
156 throw ESBadRequestException("Resource is not initialized");
159 OC::QueryParamsMap query;
160 OC::OCRepresentation rep;
162 std::function< OCStackResult(void) > getStatus = [&]
164 return m_ocResource->get(m_ocResource->getResourceTypes().at(0),
165 DEFAULT_INTERFACE, query, std::function<void(const HeaderOptions& headerOptions,
166 const OCRepresentation& rep, const int eCode) >(
167 std::bind(&EnrolleeResource::onGetStatusResponse, this,
168 std::placeholders::_1, std::placeholders::_2,
169 std::placeholders::_3)));
172 OCStackResult result = getStatus();
174 if (result != OCStackResult::OC_STACK_OK)
176 EnrolleeStatus enrolleeStatus(rep);// = {ES_STATE_INIT, ES_ERRCODE_NO_ERROR};
177 std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus = std::make_shared<
178 GetEnrolleeStatus >(ESResult::ES_ERROR, enrolleeStatus);
180 m_getStatusCb(getEnrolleeStatus);
186 void EnrolleeResource::getConfiguration()
188 if (m_ocResource == nullptr)
190 throw ESBadRequestException("Resource is not initialized");
193 OC::QueryParamsMap query;
194 OC::OCRepresentation rep;
196 std::function< OCStackResult(void) > getConfigurationStatus = [&]
198 return m_ocResource->get(m_ocResource->getResourceTypes().at(0),
199 BATCH_INTERFACE, query, std::function<void(const HeaderOptions& headerOptions,
200 const OCRepresentation& rep, const int eCode) >(
201 std::bind(&EnrolleeResource::onGetConfigurationResponse, this,
202 std::placeholders::_1, std::placeholders::_2,
203 std::placeholders::_3)));
206 OCStackResult result = getConfigurationStatus();
208 if (result != OCStackResult::OC_STACK_OK)
210 EnrolleeConf enrolleeConf(rep);
211 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
212 GetConfigurationStatus >(ESResult::ES_ERROR, enrolleeConf);
213 m_getConfigurationStatusCb(getConfigurationStatus);
218 void EnrolleeResource::provisionEnrollee(const DeviceProp& deviceProp)
220 if (m_ocResource == nullptr)
222 throw ESBadRequestException("Resource is not initialized");
225 OC::QueryParamsMap query;
226 OC::OCRepresentation provisioningRepresentation = deviceProp.toOCRepresentation();
228 m_ocResource->post(OC_RSRVD_ES_RES_TYPE_PROV, BATCH_INTERFACE,
229 provisioningRepresentation, QueryParamsMap(),
231 void(const HeaderOptions& headerOptions,
232 const OCRepresentation& rep, const int eCode) >(
233 std::bind(&EnrolleeResource::checkProvInformationCb, this,
234 std::placeholders::_1, std::placeholders::_2,
235 std::placeholders::_3)));