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 = {ES_STATE_INIT, ES_ERRCODE_NO_ERROR};
87 std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus = std::make_shared<
88 GetEnrolleeStatus >(ESResult::ES_ERROR, enrolleeStatus);
90 m_getStatusCb(getEnrolleeStatus);
94 EnrolleeStatus enrolleeStatus = parseEnrolleeStatusFromRepresentation(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;
123 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
124 GetConfigurationStatus >(result, enrolleeConf);
125 m_getConfigurationStatusCb(getConfigurationStatus);
129 EnrolleeConf enrolleeConf = parseEnrolleeConfFromRepresentation(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 = {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;
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)
219 if (m_ocResource == nullptr)
221 throw ESBadRequestException("Resource is not initialized");
224 OC::QueryParamsMap query;
225 OC::OCRepresentation provisioningRepresentation;
227 provisioningRepresentation.setValue(OC_RSRVD_ES_SSID, deviceProp.WIFI.ssid);
228 provisioningRepresentation.setValue(OC_RSRVD_ES_CRED, deviceProp.WIFI.pwd);
229 provisioningRepresentation.setValue(OC_RSRVD_ES_AUTHTYPE, deviceProp.WIFI.authtype);
230 provisioningRepresentation.setValue(OC_RSRVD_ES_ENCTYPE, deviceProp.WIFI.enctype);
231 provisioningRepresentation.setValue(OC_RSRVD_ES_LANGUAGE, deviceProp.Device.language);
232 provisioningRepresentation.setValue(OC_RSRVD_ES_COUNTRY, deviceProp.Device.country);
234 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : ssid - %s",
235 (deviceProp.WIFI.ssid).c_str());
236 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : pwd - %s",
237 (deviceProp.WIFI.pwd).c_str());
238 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : authtype - %d",
239 deviceProp.WIFI.authtype);
240 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : enctype - %d",
241 deviceProp.WIFI.enctype);
242 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : language - %s",
243 (deviceProp.Device.language).c_str());
244 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : country - %s",
245 (deviceProp.Device.country).c_str());
247 m_ocResource->post(OC_RSRVD_ES_RES_TYPE_PROV, BATCH_INTERFACE,
248 provisioningRepresentation, QueryParamsMap(),
250 void(const HeaderOptions& headerOptions,
251 const OCRepresentation& rep, const int eCode) >(
252 std::bind(&EnrolleeResource::checkProvInformationCb, this,
253 std::placeholders::_1, std::placeholders::_2,
254 std::placeholders::_3)));
257 EnrolleeConf EnrolleeResource::parseEnrolleeConfFromRepresentation(const OCRepresentation& rep)
259 OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_RES_TAG, "Enter parseEnrolleeConfFromRepresentation");
261 DeviceConfig devConf;
263 bool cloudable = false;
265 std::vector<OCRepresentation> children = rep.getChildren();
267 for(auto prop = children.begin(); prop != children.end(); ++prop)
269 if(prop->getUri().find(OC_RSRVD_ES_URI_WIFI) != std::string::npos)
271 OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_RES_TAG, "Find wifi resource");
272 if(prop->hasAttribute(OC_RSRVD_ES_SUPPORTEDWIFIMODE)
273 && prop->hasAttribute(OC_RSRVD_ES_SUPPORTEDWIFIFREQ))
275 std::vector<int> modes = prop->getValue<std::vector<int>>(OC_RSRVD_ES_SUPPORTEDWIFIMODE);
277 for(auto mode = modes.begin(); mode != modes.end(); ++mode)
279 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_SUPPORTEDWIFIMODE = %d",
281 wifiConf.modes.push_back(static_cast<WIFI_MODE>(*mode));
284 wifiConf.freq = static_cast<WIFI_FREQ>(prop->getValue<int>(OC_RSRVD_ES_SUPPORTEDWIFIFREQ));
287 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_SUPPORTEDWIFIFREQ = %d",
292 else if(prop->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos)
294 OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_RES_TAG, "Find devconf");
295 if(prop->hasAttribute(OC_RSRVD_ES_DEVNAME)
296 && prop->hasAttribute(OC_RSRVD_ES_LANGUAGE)
297 && prop->hasAttribute(OC_RSRVD_ES_COUNTRY))
299 //TODO:: setting DeviceID.
300 //devInfo.id = devId;
301 devConf.name = prop->getValue<std::string>(OC_RSRVD_ES_DEVNAME);
302 devConf.language = prop->getValue<std::string>(OC_RSRVD_ES_LANGUAGE);
303 devConf.country = prop->getValue<std::string>(OC_RSRVD_ES_COUNTRY);
305 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_DEVNAME = %s",
306 devConf.name.c_str());
307 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_LANGUAGE = %s",
308 devConf.language.c_str());
309 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_COUNTRY = %s",
310 devConf.country.c_str());
314 else if(prop->getUri().find(OC_RSRVD_ES_URI_CLOUDSERVER) != std::string::npos)
316 OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_RES_TAG, "Find cloudserver");
318 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "cloudable = %d",
323 return EnrolleeConf(devConf, wifiConf, cloudable);
326 EnrolleeStatus EnrolleeResource::parseEnrolleeStatusFromRepresentation(const OCRepresentation& rep)
328 OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_RES_TAG, "Enter parseEnrolleeStatusFromRepresentation");
330 EnrolleeStatus enrolleeStatus;
332 if(rep.hasAttribute(OC_RSRVD_ES_PROVSTATUS))
334 enrolleeStatus.provStatus = static_cast<ProvStatus>(
335 rep.getValue<int>(OC_RSRVD_ES_PROVSTATUS));
338 if(rep.hasAttribute(OC_RSRVD_ES_LAST_ERRORCODE))
340 enrolleeStatus.lastErrCode = static_cast<ESErrorCode>(
341 rep.getValue<int>(OC_RSRVD_ES_LAST_ERRORCODE));
344 return enrolleeStatus;