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 >(ESResult::ES_ERROR, 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 = [&]
163 { return m_ocResource->get(m_ocResource->getResourceTypes().at(0),
164 DEFAULT_INTERFACE, query, std::function<void(const HeaderOptions& headerOptions,
165 const OCRepresentation& rep, const int eCode) >(
166 std::bind(&EnrolleeResource::onGetStatusResponse, this,
167 std::placeholders::_1, std::placeholders::_2,
168 std::placeholders::_3)));
171 OCStackResult result = getStatus();
173 if (result != OCStackResult::OC_STACK_OK)
175 EnrolleeStatus enrolleeStatus(rep);// = {ES_STATE_INIT, ES_ERRCODE_NO_ERROR};
176 std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus = std::make_shared<
177 GetEnrolleeStatus >(ESResult::ES_ERROR, enrolleeStatus);
179 m_getStatusCb(getEnrolleeStatus);
185 void EnrolleeResource::getConfiguration()
187 if (m_ocResource == nullptr)
189 throw ESBadRequestException("Resource is not initialized");
192 OC::QueryParamsMap query;
193 OC::OCRepresentation rep;
195 std::function< OCStackResult(void) > getConfigurationStatus = [&]
196 { return m_ocResource->get(m_ocResource->getResourceTypes().at(0),
197 BATCH_INTERFACE, query, std::function<void(const HeaderOptions& headerOptions,
198 const OCRepresentation& rep, const int eCode) >(
199 std::bind(&EnrolleeResource::onGetConfigurationResponse, this,
200 std::placeholders::_1, std::placeholders::_2,
201 std::placeholders::_3)));
204 OCStackResult result = getConfigurationStatus();
206 if (result != OCStackResult::OC_STACK_OK)
208 EnrolleeConf enrolleeConf(rep);
209 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
210 GetConfigurationStatus >(ESResult::ES_ERROR, enrolleeConf);
211 m_getConfigurationStatusCb(getConfigurationStatus);
216 void EnrolleeResource::provisionEnrollee(const DeviceProp& deviceProp)
218 if (m_ocResource == nullptr)
220 throw ESBadRequestException("Resource is not initialized");
223 OC::QueryParamsMap query;
224 OC::OCRepresentation provisioningRepresentation = deviceProp.toOCRepresentation();
226 m_ocResource->post(OC_RSRVD_ES_RES_TYPE_PROV, BATCH_INTERFACE,
227 provisioningRepresentation, QueryParamsMap(),
229 void(const HeaderOptions& headerOptions,
230 const OCRepresentation& rep, const int eCode) >(
231 std::bind(&EnrolleeResource::checkProvInformationCb, this,
232 std::placeholders::_1, std::placeholders::_2,
233 std::placeholders::_3)));