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< 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::onGetConfigurationResponse(const HeaderOptions& /*headerOptions*/,
67 const OCRepresentation& rep, const int eCode)
69 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onGetConfigurationResponse : %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,"onGetConfigurationResponse : onGetConfigurationResponse 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 EnrolleeConf enrolleeConf;
86 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
87 GetConfigurationStatus >(result, enrolleeConf);
88 m_getConfigurationStatusCb(getConfigurationStatus);
92 EnrolleeConf enrolleeConf = parseEnrolleeConfFromRepresentation(rep);
94 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
95 GetConfigurationStatus >(ESResult::ES_OK, enrolleeConf);
96 m_getConfigurationStatusCb(getConfigurationStatus);
100 void EnrolleeResource::registerGetConfigurationStatusCallback(GetConfigurationStatusCb callback)
102 m_getConfigurationStatusCb = callback;
105 void EnrolleeResource::registerDevicePropProvStatusCallback(DevicePropProvStatusCb callback)
107 m_devicePropProvStatusCb = callback;
110 void EnrolleeResource::getConfiguration()
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) > getConfigurationStatus = [&]
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::onGetConfigurationResponse, this,
125 std::placeholders::_1, std::placeholders::_2,
126 std::placeholders::_3)));
129 OCStackResult result = getConfigurationStatus();
131 if (result != OCStackResult::OC_STACK_OK)
133 EnrolleeConf enrolleeConf;
134 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
135 GetConfigurationStatus >(ESResult::ES_ERROR, enrolleeConf);
136 m_getConfigurationStatusCb(getConfigurationStatus);
141 void EnrolleeResource::provisionEnrollee(const DeviceProp& deviceProp)
144 if (m_ocResource == nullptr)
146 throw ESBadRequestException("Resource is not initialized");
149 OC::QueryParamsMap query;
150 OC::OCRepresentation provisioningRepresentation;
152 provisioningRepresentation.setValue(OC_RSRVD_ES_SSID, deviceProp.WIFI.ssid);
153 provisioningRepresentation.setValue(OC_RSRVD_ES_CRED, deviceProp.WIFI.pwd);
154 provisioningRepresentation.setValue(OC_RSRVD_ES_AUTHTYPE, deviceProp.WIFI.authtype);
155 provisioningRepresentation.setValue(OC_RSRVD_ES_ENCTYPE, deviceProp.WIFI.enctype);
156 provisioningRepresentation.setValue(OC_RSRVD_ES_LANGUAGE, deviceProp.Device.language);
157 provisioningRepresentation.setValue(OC_RSRVD_ES_COUNTRY, deviceProp.Device.country);
159 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : ssid - %s",
160 (deviceProp.WIFI.ssid).c_str());
161 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : pwd - %s",
162 (deviceProp.WIFI.pwd).c_str());
163 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : authtype - %d",
164 deviceProp.WIFI.authtype);
165 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : enctype - %d",
166 deviceProp.WIFI.enctype);
167 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : language - %s",
168 (deviceProp.Device.language).c_str());
169 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : country - %s",
170 (deviceProp.Device.country).c_str());
172 m_ocResource->post(OC_RSRVD_ES_RES_TYPE_PROV, BATCH_INTERFACE,
173 provisioningRepresentation, QueryParamsMap(),
175 void(const HeaderOptions& headerOptions,
176 const OCRepresentation& rep, const int eCode) >(
177 std::bind(&EnrolleeResource::checkProvInformationCb, this,
178 std::placeholders::_1, std::placeholders::_2,
179 std::placeholders::_3)));
182 EnrolleeConf EnrolleeResource::parseEnrolleeConfFromRepresentation(const OCRepresentation& rep)
184 OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_RES_TAG, "Enter parseEnrolleeConfFromRepresentation");
186 DeviceConfig devConf;
188 bool cloudable = false;
190 std::vector<OCRepresentation> children = rep.getChildren();
192 for(auto prop = children.begin(); prop != children.end(); ++prop)
194 if(prop->getUri().find(OC_RSRVD_ES_URI_WIFI) != std::string::npos)
196 OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_RES_TAG, "Find wifi resource");
197 if(prop->hasAttribute(OC_RSRVD_ES_SUPPORTEDWIFIMODE)
198 && prop->hasAttribute(OC_RSRVD_ES_SUPPORTEDWIFIFREQ))
200 std::vector<int> types = prop->getValue<std::vector<int>>(OC_RSRVD_ES_SUPPORTEDWIFIMODE);
202 for(auto type = types.begin(); type != types.end(); ++type)
204 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_SUPPORTEDWIFIMODE = %d",
206 wifiConf.types.push_back(static_cast<WIFI_MODE>(*type));
209 wifiConf.freq = static_cast<WIFI_FREQ>(prop->getValue<int>(OC_RSRVD_ES_SUPPORTEDWIFIFREQ));
212 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_SUPPORTEDWIFIFREQ = %d",
217 else if(prop->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos)
219 OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_RES_TAG, "Find devconf");
220 if(prop->hasAttribute(OC_RSRVD_ES_DEVNAME)
221 && prop->hasAttribute(OC_RSRVD_ES_LANGUAGE)
222 && prop->hasAttribute(OC_RSRVD_ES_COUNTRY))
224 //TODO:: setting DeviceID.
225 //devInfo.id = devId;
226 devConf.name = prop->getValue<std::string>(OC_RSRVD_ES_DEVNAME);
227 devConf.language = prop->getValue<std::string>(OC_RSRVD_ES_LANGUAGE);
228 devConf.country = prop->getValue<std::string>(OC_RSRVD_ES_COUNTRY);
230 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_DEVNAME = %s",
231 devConf.name.c_str());
232 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_LANGUAGE = %s",
233 devConf.language.c_str());
234 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_COUNTRY = %s",
235 devConf.country.c_str());
239 else if(prop->getUri().find(OC_RSRVD_ES_URI_CLOUDSERVER) != std::string::npos)
241 OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_RES_TAG, "Find cloudserver");
243 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "cloudable = %d",
248 return EnrolleeConf(devConf, wifiConf, cloudable);