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",
50 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
51 "checkProvInformationCb : Provisioning is failed ");
52 std::shared_ptr< DataProvisioningStatus > provStatus = std::make_shared<
53 DataProvisioningStatus >(ESResult::ES_ERROR, ESDataProvState::ES_PROVISIONING_ERROR);
54 m_dataProvStatusCb(provStatus);
58 // rep.getValue(OC_RSRVD_ES_PROVSTATUS, ps);
59 // OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "checkProvInformationCb : ps - %d", ps);
61 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
62 "checkProvInformationCb : Provisioning is success. "
63 "Now trigger network connection ");
65 std::shared_ptr< DataProvisioningStatus > provStatus = std::make_shared<
66 DataProvisioningStatus >(ESResult::ES_OK, ESDataProvState::ES_PROVISIONING_SUCCESS);
67 m_dataProvStatusCb(provStatus);
70 void EnrolleeResource::onRequestPropertyDataResponse(const HeaderOptions& /*headerOptions*/,
71 const OCRepresentation& rep, const int eCode)
73 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onRequestPropertyDataResponse : %s, eCode = %d",
74 rep.getUri().c_str(), eCode);
78 ESResult result = ESResult::ES_ERROR;
80 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,"onRequestPropertyDataResponse : onRequestPropertyDataResponse is failed ");
82 if (eCode == OCStackResult::OC_STACK_UNAUTHORIZED_REQ)
84 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
85 "Mediator is unauthorized from Enrollee.");
86 result = ESResult::ES_UNAUTHORIZED;
89 PropertyData propertyData;
90 std::shared_ptr< RequestPropertyDataStatus > requestPropertyDataStatus = std::make_shared<
91 RequestPropertyDataStatus >(result, propertyData );
92 m_RequestPropertyDataStatusCb(requestPropertyDataStatus);
97 PropertyData propertyData = parsePropertyDataFromRepresentation(rep);
99 std::shared_ptr< RequestPropertyDataStatus > requestPropertyDataStatus = std::make_shared<
100 RequestPropertyDataStatus >(ESResult::ES_OK, propertyData);
101 m_RequestPropertyDataStatusCb(requestPropertyDataStatus);
105 void EnrolleeResource::getProvStatusResponse(const HeaderOptions& /*headerOptions*/,
106 const OCRepresentation& rep, const int eCode)
108 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : %s, eCode = %d",
109 rep.getUri().c_str(),
114 ESResult result = ESResult::ES_ERROR;
116 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,"getProvStatusResponse : Provisioning is failed ");
118 if (eCode == OCStackResult::OC_STACK_UNAUTHORIZED_REQ)
120 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
121 "Mediator is unauthorized from Enrollee.");
122 result = ESResult::ES_UNAUTHORIZED;
124 std::shared_ptr< DataProvisioningStatus > provStatus = std::make_shared<
125 DataProvisioningStatus >(result, ESDataProvState::ES_PROVISIONING_ERROR);
126 m_dataProvStatusCb(provStatus);
133 rep.getValue(OC_RSRVD_ES_PROVSTATUS, ps);
135 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : ps - %d",
138 //if (ps == ES_PS_NEED_PROVISIONING) //Indicates the need for provisioning
139 if (ps == 0) //Indicates the need for provisioning
141 OCRepresentation provisioningRepresentation;
143 provisioningRepresentation.setValue(OC_RSRVD_ES_SSID, m_dataProvInfo.WIFI.ssid);
144 provisioningRepresentation.setValue(OC_RSRVD_ES_CRED, m_dataProvInfo.WIFI.pwd);
145 provisioningRepresentation.setValue(OC_RSRVD_ES_AUTHTYPE, m_dataProvInfo.WIFI.authtype);
146 provisioningRepresentation.setValue(OC_RSRVD_ES_ENCTYPE, m_dataProvInfo.WIFI.enctype);
147 provisioningRepresentation.setValue(OC_RSRVD_ES_LANGUAGE, m_dataProvInfo.Device.language);
148 provisioningRepresentation.setValue(OC_RSRVD_ES_COUNTRY, m_dataProvInfo.Device.country);
150 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : ssid - %s",
151 (m_dataProvInfo.WIFI.ssid).c_str());
152 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : pwd - %s",
153 (m_dataProvInfo.WIFI.pwd).c_str());
154 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : authtype - %d",
155 m_dataProvInfo.WIFI.authtype);
156 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : enctype - %d",
157 m_dataProvInfo.WIFI.enctype);
158 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : language - %s",
159 (m_dataProvInfo.Device.language).c_str());
160 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : country - %s",
161 (m_dataProvInfo.Device.country).c_str());
163 m_ocResource->post(OC_RSRVD_ES_PROV_RES_TYPE, BATCH_INTERFACE,
164 provisioningRepresentation, QueryParamsMap(),
166 void(const HeaderOptions& headerOptions,
167 const OCRepresentation& rep, const int eCode) >(
168 std::bind(&EnrolleeResource::checkProvInformationCb, this,
169 std::placeholders::_1, std::placeholders::_2,
170 std::placeholders::_3)));
172 else if (ps == ES_PS_PROVISIONING_COMPLETED) //Indicates that provisioning is completed
174 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
175 "getProvStatusResponse : Provisioning is successful");
176 std::shared_ptr< DataProvisioningStatus > provStatus = std::make_shared<
177 DataProvisioningStatus >(ESResult::ES_OK, ESDataProvState::ES_PROVISIONED_ALREADY);
178 m_dataProvStatusCb(provStatus);
182 void EnrolleeResource::registerRequestPropertyDataStatusCallback(RequestPropertyDataStatusCb callback)
184 m_RequestPropertyDataStatusCb = callback;
187 void EnrolleeResource::registerProvStatusCallback(DataProvStatusCb callback)
189 m_dataProvStatusCb = callback;
192 void EnrolleeResource::RequestPropertyData()
194 if (m_ocResource == nullptr)
196 throw ESBadRequestException("Resource is not initialized");
199 OC::QueryParamsMap query;
200 OC::OCRepresentation rep;
202 std::function< OCStackResult(void) > requestPropertyDataStatus = [&]
203 { return m_ocResource->get(m_ocResource->getResourceTypes().at(0),
204 BATCH_INTERFACE, query, std::function<void(const HeaderOptions& headerOptions,
205 const OCRepresentation& rep, const int eCode) >(
206 std::bind(&EnrolleeResource::onRequestPropertyDataResponse, this,
207 std::placeholders::_1, std::placeholders::_2,
208 std::placeholders::_3)));
211 OCStackResult result = requestPropertyDataStatus();
213 if (result != OCStackResult::OC_STACK_OK)
215 PropertyData propertyData;
216 std::shared_ptr< RequestPropertyDataStatus > requestPropertyDataStatus = std::make_shared<
217 RequestPropertyDataStatus >(ESResult::ES_ERROR, propertyData);
218 m_RequestPropertyDataStatusCb(requestPropertyDataStatus);
223 void EnrolleeResource::provisionEnrollee(const DataProvInfo& dataProvInfo)
226 if (m_ocResource == nullptr)
228 throw ESBadRequestException("Resource is not initialized");
231 m_dataProvInfo = dataProvInfo;
233 OC::QueryParamsMap query;
234 OC::OCRepresentation rep;
236 std::function< OCStackResult(void) > getProvisioingStatus = [&]
237 { return m_ocResource->get(m_ocResource->getResourceTypes().at(0),
238 m_ocResource->getResourceInterfaces().at(0), query,
240 void(const HeaderOptions& headerOptions, const OCRepresentation& rep,
242 std::bind(&EnrolleeResource::getProvStatusResponse, this,
243 std::placeholders::_1, std::placeholders::_2,
244 std::placeholders::_3)));
247 OCStackResult result = getProvisioingStatus();
249 if (result != OCStackResult::OC_STACK_OK)
251 std::shared_ptr< DataProvisioningStatus > provStatus = std::make_shared<
252 DataProvisioningStatus >(ESResult::ES_ERROR, ESDataProvState::ES_PROVISIONING_ERROR);
253 m_dataProvStatusCb(provStatus);
258 void EnrolleeResource::unprovisionEnrollee()
260 if (m_ocResource == nullptr)
262 throw ESBadRequestException("Resource is not initialized");
265 OCRepresentation provisioningRepresentation;
267 provisioningRepresentation.setValue(OC_RSRVD_ES_SSID, "");
268 provisioningRepresentation.setValue(OC_RSRVD_ES_CRED, "");
270 m_ocResource->post(provisioningRepresentation, QueryParamsMap(),
272 void(const HeaderOptions& headerOptions, const OCRepresentation& rep,
274 std::bind(&EnrolleeResource::checkProvInformationCb, this,
275 std::placeholders::_1, std::placeholders::_2,
276 std::placeholders::_3)));
279 PropertyData EnrolleeResource::parsePropertyDataFromRepresentation(const OCRepresentation& rep)
281 OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_RES_TAG, "Enter parsePropertyDataFromRepresentation");
283 DeviceConfig devInfo;
285 bool cloudable = false;
287 std::vector<OCRepresentation> children = rep.getChildren();
289 for(auto prop = children.begin(); prop != children.end(); ++prop)
291 if(prop->getUri().find(OC_RSRVD_ES_URI_WIFI) != std::string::npos)
293 OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_RES_TAG, "Find wifi resource");
294 if(prop->hasAttribute(OC_RSRVD_ES_SUPPORTEDWIFIMODE)
295 && prop->hasAttribute(OC_RSRVD_ES_SUPPORTEDWIFIFREQ))
297 std::vector<int> types = prop->getValue<std::vector<int>>(OC_RSRVD_ES_SUPPORTEDWIFIMODE);
299 for(auto type = types.begin(); type != types.end(); ++type)
301 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_SUPPORTEDWIFIMODE = %d",
303 netInfo.types.push_back(static_cast<WIFI_MODE>(*type));
306 netInfo.freq = static_cast<WIFI_FREQ>(prop->getValue<int>(OC_RSRVD_ES_SUPPORTEDWIFIFREQ));
309 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_SUPPORTEDWIFIFREQ = %d",
314 else if(prop->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos)
316 OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_RES_TAG, "Find devconf");
317 if(prop->hasAttribute(OC_RSRVD_ES_DEVNAME)
318 && prop->hasAttribute(OC_RSRVD_ES_LANGUAGE)
319 && prop->hasAttribute(OC_RSRVD_ES_COUNTRY))
321 //TODO:: setting DeviceID.
322 //devInfo.id = devId;
323 devInfo.name = prop->getValue<std::string>(OC_RSRVD_ES_DEVNAME);
324 devInfo.language = prop->getValue<std::string>(OC_RSRVD_ES_LANGUAGE);
325 devInfo.country = prop->getValue<std::string>(OC_RSRVD_ES_COUNTRY);
327 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_DEVNAME = %s",
328 devInfo.name.c_str());
329 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_LANGUAGE = %s",
330 devInfo.language.c_str());
331 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_COUNTRY = %s",
332 devInfo.country.c_str());
336 else if(prop->getUri().find(OC_RSRVD_ES_URI_CLOUDSERVER) != std::string::npos)
338 OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_RES_TAG, "Find cloudserver");
340 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "cloudable = %d",
345 return PropertyData(devInfo, netInfo, cloudable);