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 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
59 "checkProvInformationCb : Provisioning is success. ");
61 std::shared_ptr< DataProvisioningStatus > provStatus = std::make_shared<
62 DataProvisioningStatus >(ESResult::ES_OK, ESDataProvState::ES_PROVISIONING_SUCCESS);
63 m_dataProvStatusCb(provStatus);
66 void EnrolleeResource::onRequestPropertyDataResponse(const HeaderOptions& /*headerOptions*/,
67 const OCRepresentation& rep, const int eCode)
69 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onRequestPropertyDataResponse : %s, eCode = %d",
70 rep.getUri().c_str(), eCode);
74 ESResult result = ESResult::ES_ERROR;
76 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,"onRequestPropertyDataResponse : onRequestPropertyDataResponse is failed ");
78 if (eCode == OCStackResult::OC_STACK_UNAUTHORIZED_REQ)
80 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
81 "Mediator is unauthorized from Enrollee.");
82 result = ESResult::ES_UNAUTHORIZED;
85 PropertyData propertyData;
86 std::shared_ptr< RequestPropertyDataStatus > requestPropertyDataStatus = std::make_shared<
87 RequestPropertyDataStatus >(result, propertyData );
88 m_RequestPropertyDataStatusCb(requestPropertyDataStatus);
92 PropertyData propertyData = parsePropertyDataFromRepresentation(rep);
94 std::shared_ptr< RequestPropertyDataStatus > requestPropertyDataStatus = std::make_shared<
95 RequestPropertyDataStatus >(ESResult::ES_OK, propertyData);
96 m_RequestPropertyDataStatusCb(requestPropertyDataStatus);
100 void EnrolleeResource::registerRequestPropertyDataStatusCallback(RequestPropertyDataStatusCb callback)
102 m_RequestPropertyDataStatusCb = callback;
105 void EnrolleeResource::registerProvStatusCallback(DataProvStatusCb callback)
107 m_dataProvStatusCb = callback;
110 void EnrolleeResource::RequestPropertyData()
112 if (m_ocResource == nullptr)
114 throw ESBadRequestException("Resource is not initialized");
117 OC::QueryParamsMap query;
118 OC::OCRepresentation rep;
120 std::function< OCStackResult(void) > requestPropertyDataStatus = [&]
121 { return m_ocResource->get(m_ocResource->getResourceTypes().at(0),
122 BATCH_INTERFACE, query, std::function<void(const HeaderOptions& headerOptions,
123 const OCRepresentation& rep, const int eCode) >(
124 std::bind(&EnrolleeResource::onRequestPropertyDataResponse, this,
125 std::placeholders::_1, std::placeholders::_2,
126 std::placeholders::_3)));
129 OCStackResult result = requestPropertyDataStatus();
131 if (result != OCStackResult::OC_STACK_OK)
133 PropertyData propertyData;
134 std::shared_ptr< RequestPropertyDataStatus > requestPropertyDataStatus = std::make_shared<
135 RequestPropertyDataStatus >(ESResult::ES_ERROR, propertyData);
136 m_RequestPropertyDataStatusCb(requestPropertyDataStatus);
141 void EnrolleeResource::provisionEnrollee(const DataProvInfo& dataProvInfo)
144 if (m_ocResource == nullptr)
146 throw ESBadRequestException("Resource is not initialized");
149 m_dataProvInfo = dataProvInfo;
151 OC::QueryParamsMap query;
152 OC::OCRepresentation provisioningRepresentation;
154 provisioningRepresentation.setValue(OC_RSRVD_ES_SSID, m_dataProvInfo.WIFI.ssid);
155 provisioningRepresentation.setValue(OC_RSRVD_ES_CRED, m_dataProvInfo.WIFI.pwd);
156 provisioningRepresentation.setValue(OC_RSRVD_ES_AUTHTYPE, m_dataProvInfo.WIFI.authtype);
157 provisioningRepresentation.setValue(OC_RSRVD_ES_ENCTYPE, m_dataProvInfo.WIFI.enctype);
158 provisioningRepresentation.setValue(OC_RSRVD_ES_LANGUAGE, m_dataProvInfo.Device.language);
159 provisioningRepresentation.setValue(OC_RSRVD_ES_COUNTRY, m_dataProvInfo.Device.country);
161 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : ssid - %s",
162 (m_dataProvInfo.WIFI.ssid).c_str());
163 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : pwd - %s",
164 (m_dataProvInfo.WIFI.pwd).c_str());
165 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : authtype - %d",
166 m_dataProvInfo.WIFI.authtype);
167 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : enctype - %d",
168 m_dataProvInfo.WIFI.enctype);
169 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : language - %s",
170 (m_dataProvInfo.Device.language).c_str());
171 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : country - %s",
172 (m_dataProvInfo.Device.country).c_str());
174 m_ocResource->post(OC_RSRVD_ES_RES_TYPE_PROV, BATCH_INTERFACE,
175 provisioningRepresentation, QueryParamsMap(),
177 void(const HeaderOptions& headerOptions,
178 const OCRepresentation& rep, const int eCode) >(
179 std::bind(&EnrolleeResource::checkProvInformationCb, this,
180 std::placeholders::_1, std::placeholders::_2,
181 std::placeholders::_3)));
184 void EnrolleeResource::unprovisionEnrollee()
186 if (m_ocResource == nullptr)
188 throw ESBadRequestException("Resource is not initialized");
191 OCRepresentation provisioningRepresentation;
193 provisioningRepresentation.setValue(OC_RSRVD_ES_SSID, "");
194 provisioningRepresentation.setValue(OC_RSRVD_ES_CRED, "");
196 m_ocResource->post(provisioningRepresentation, QueryParamsMap(),
198 void(const HeaderOptions& headerOptions, const OCRepresentation& rep,
200 std::bind(&EnrolleeResource::checkProvInformationCb, this,
201 std::placeholders::_1, std::placeholders::_2,
202 std::placeholders::_3)));
205 PropertyData EnrolleeResource::parsePropertyDataFromRepresentation(const OCRepresentation& rep)
207 OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_RES_TAG, "Enter parsePropertyDataFromRepresentation");
209 DeviceConfig devInfo;
211 bool cloudable = false;
213 std::vector<OCRepresentation> children = rep.getChildren();
215 for(auto prop = children.begin(); prop != children.end(); ++prop)
217 if(prop->getUri().find(OC_RSRVD_ES_URI_WIFI) != std::string::npos)
219 OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_RES_TAG, "Find wifi resource");
220 if(prop->hasAttribute(OC_RSRVD_ES_SUPPORTEDWIFIMODE)
221 && prop->hasAttribute(OC_RSRVD_ES_SUPPORTEDWIFIFREQ))
223 std::vector<int> types = prop->getValue<std::vector<int>>(OC_RSRVD_ES_SUPPORTEDWIFIMODE);
225 for(auto type = types.begin(); type != types.end(); ++type)
227 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_SUPPORTEDWIFIMODE = %d",
229 netInfo.types.push_back(static_cast<WIFI_MODE>(*type));
232 netInfo.freq = static_cast<WIFI_FREQ>(prop->getValue<int>(OC_RSRVD_ES_SUPPORTEDWIFIFREQ));
235 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_SUPPORTEDWIFIFREQ = %d",
240 else if(prop->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos)
242 OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_RES_TAG, "Find devconf");
243 if(prop->hasAttribute(OC_RSRVD_ES_DEVNAME)
244 && prop->hasAttribute(OC_RSRVD_ES_LANGUAGE)
245 && prop->hasAttribute(OC_RSRVD_ES_COUNTRY))
247 //TODO:: setting DeviceID.
248 //devInfo.id = devId;
249 devInfo.name = prop->getValue<std::string>(OC_RSRVD_ES_DEVNAME);
250 devInfo.language = prop->getValue<std::string>(OC_RSRVD_ES_LANGUAGE);
251 devInfo.country = prop->getValue<std::string>(OC_RSRVD_ES_COUNTRY);
253 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_DEVNAME = %s",
254 devInfo.name.c_str());
255 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_LANGUAGE = %s",
256 devInfo.language.c_str());
257 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_COUNTRY = %s",
258 devInfo.country.c_str());
262 else if(prop->getUri().find(OC_RSRVD_ES_URI_CLOUDSERVER) != std::string::npos)
264 OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_RES_TAG, "Find cloudserver");
266 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "cloudable = %d",
271 return PropertyData(devInfo, netInfo, cloudable);