Merge branch 'master' into extended-easysetup
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / src / EnrolleeResource.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include <functional>
22
23 #include "EnrolleeResource.h"
24
25 #include "OCPlatform.h"
26 #include "ESException.h"
27 #include "OCResource.h"
28 #include "logger.h"
29
30 namespace OIC
31 {
32     namespace Service
33     {
34         #define ES_REMOTE_ENROLLEE_RES_TAG "ES_ENROLLEE_RESOURCE"
35
36         EnrolleeResource::EnrolleeResource(std::shared_ptr< OC::OCResource > resource)
37         {
38             m_ocResource = resource;
39         }
40
41         void EnrolleeResource::checkProvInformationCb(const HeaderOptions& /*headerOptions*/,
42                 const OCRepresentation& rep, const int eCode)
43         {
44             OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "checkProvInformationCb : %s, eCode = %d",
45                     rep.getUri().c_str(),
46                     eCode);
47
48             if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
49             {
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);
55                 return;
56             }
57
58             OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
59                     "checkProvInformationCb : Provisioning is success. ");
60
61             std::shared_ptr< DevicePropProvisioningStatus > provStatus = std::make_shared<
62                     DevicePropProvisioningStatus >(ESResult::ES_OK);
63             m_devicePropProvStatusCb(provStatus);
64         }
65
66         void EnrolleeResource::onGetStatusResponse(const HeaderOptions& /*headerOptions*/,
67                 const OCRepresentation& rep, const int eCode)
68         {
69             OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onGetStatusResponse : %s, eCode = %d",
70                     rep.getUri().c_str(), eCode);
71
72             if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
73             {
74                 ESResult result = ESResult::ES_ERROR;
75
76                 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
77                             "onGetStatusResponse : onGetStatusResponse is failed ");
78
79                 if (eCode == OCStackResult::OC_STACK_UNAUTHORIZED_REQ)
80                 {
81                     OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
82                         "Mediator is unauthorized from Enrollee.");
83                     result = ESResult::ES_UNAUTHORIZED;
84                 }
85
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);
89
90                 m_getStatusCb(getEnrolleeStatus);
91             }
92             else
93             {
94                 EnrolleeStatus enrolleeStatus = parseEnrolleeStatusFromRepresentation(rep);
95                 std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus = std::make_shared<
96                         GetEnrolleeStatus >(ESResult::ES_OK, enrolleeStatus);
97
98                 m_getStatusCb(getEnrolleeStatus);
99             }
100         }
101
102         void EnrolleeResource::onGetConfigurationResponse(const HeaderOptions& /*headerOptions*/,
103                 const OCRepresentation& rep, const int eCode)
104         {
105             OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onGetConfigurationResponse : %s, eCode = %d",
106                     rep.getUri().c_str(), eCode);
107
108             if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
109             {
110                 ESResult result  = ESResult::ES_ERROR;
111
112                 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
113                             "onGetConfigurationResponse : onGetConfigurationResponse is failed ");
114
115                 if (eCode == OCStackResult::OC_STACK_UNAUTHORIZED_REQ)
116                 {
117                     OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
118                         "Mediator is unauthorized from Enrollee.");
119                     result = ESResult::ES_UNAUTHORIZED;
120                 }
121
122                 EnrolleeConf enrolleeConf;
123                 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
124                         GetConfigurationStatus >(result, enrolleeConf);
125                 m_getConfigurationStatusCb(getConfigurationStatus);
126             }
127             else
128             {
129                 EnrolleeConf enrolleeConf = parseEnrolleeConfFromRepresentation(rep);
130
131                 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
132                         GetConfigurationStatus >(ESResult::ES_OK, enrolleeConf);
133                 m_getConfigurationStatusCb(getConfigurationStatus);
134             }
135         }
136
137         void EnrolleeResource::registerGetStatusCallback(GetStatusCb callback)
138         {
139             m_getStatusCb = callback;
140         }
141
142         void EnrolleeResource::registerGetConfigurationStatusCallback(GetConfigurationStatusCb callback)
143         {
144             m_getConfigurationStatusCb = callback;
145         }
146
147         void EnrolleeResource::registerDevicePropProvStatusCallback(DevicePropProvStatusCb callback)
148         {
149             m_devicePropProvStatusCb = callback;
150         }
151
152         void EnrolleeResource::getStatus()
153         {
154             if (m_ocResource == nullptr)
155             {
156                 throw ESBadRequestException("Resource is not initialized");
157             }
158
159             OC::QueryParamsMap query;
160             OC::OCRepresentation rep;
161
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)));
169             };
170
171             OCStackResult result = getStatus();
172
173             if (result != OCStackResult::OC_STACK_OK)
174             {
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);
178
179                 m_getStatusCb(getEnrolleeStatus);
180
181                 return;
182             }
183         }
184
185         void EnrolleeResource::getConfiguration()
186         {
187             if (m_ocResource == nullptr)
188             {
189                 throw ESBadRequestException("Resource is not initialized");
190             }
191
192             OC::QueryParamsMap query;
193             OC::OCRepresentation rep;
194
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)));
202             };
203
204             OCStackResult result = getConfigurationStatus();
205
206             if (result != OCStackResult::OC_STACK_OK)
207             {
208                 EnrolleeConf enrolleeConf;
209                 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
210                         GetConfigurationStatus >(ESResult::ES_ERROR, enrolleeConf);
211                 m_getConfigurationStatusCb(getConfigurationStatus);
212                 return;
213             }
214         }
215
216         void EnrolleeResource::provisionEnrollee(const DeviceProp& deviceProp)
217
218         {
219             if (m_ocResource == nullptr)
220             {
221                 throw ESBadRequestException("Resource is not initialized");
222             }
223
224             OC::QueryParamsMap query;
225             OC::OCRepresentation provisioningRepresentation;
226
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);
233
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());
246
247             m_ocResource->post(OC_RSRVD_ES_RES_TYPE_PROV, BATCH_INTERFACE,
248                     provisioningRepresentation, QueryParamsMap(),
249                     std::function<
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)));
255         }
256
257         EnrolleeConf EnrolleeResource::parseEnrolleeConfFromRepresentation(const OCRepresentation& rep)
258         {
259             OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_RES_TAG, "Enter parseEnrolleeConfFromRepresentation");
260
261             DeviceConfig devConf;
262             WiFiConfig wifiConf;
263             bool cloudable = false;
264
265             std::vector<OCRepresentation> children = rep.getChildren();
266
267             for(auto prop = children.begin(); prop != children.end(); ++prop)
268             {
269                 if(prop->getUri().find(OC_RSRVD_ES_URI_WIFI) != std::string::npos)
270                 {
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))
274                     {
275                         std::vector<int> modes = prop->getValue<std::vector<int>>(OC_RSRVD_ES_SUPPORTEDWIFIMODE);
276
277                         for(auto mode = modes.begin(); mode != modes.end(); ++mode)
278                         {
279                             OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_SUPPORTEDWIFIMODE = %d",
280                                 *mode);
281                             wifiConf.modes.push_back(static_cast<WIFI_MODE>(*mode));
282                         }
283
284                         wifiConf.freq = static_cast<WIFI_FREQ>(prop->getValue<int>(OC_RSRVD_ES_SUPPORTEDWIFIFREQ));
285
286
287                         OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_SUPPORTEDWIFIFREQ = %d",
288                                 wifiConf.freq);
289                     }
290                 }
291
292                 else if(prop->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos)
293                 {
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))
298                     {
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);
304
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());
311                     }
312                 }
313
314                 else if(prop->getUri().find(OC_RSRVD_ES_URI_CLOUDSERVER) != std::string::npos)
315                 {
316                     OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_RES_TAG, "Find cloudserver");
317                     cloudable = true;
318                     OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "cloudable = %d",
319                                 cloudable);
320                 }
321             }
322
323             return EnrolleeConf(devConf, wifiConf, cloudable);
324         }
325
326         EnrolleeStatus EnrolleeResource::parseEnrolleeStatusFromRepresentation(const OCRepresentation& rep)
327         {
328             OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_RES_TAG, "Enter parseEnrolleeStatusFromRepresentation");
329
330             EnrolleeStatus enrolleeStatus;
331
332             if(rep.hasAttribute(OC_RSRVD_ES_PROVSTATUS))
333             {
334                 enrolleeStatus.provStatus = static_cast<ProvStatus>(
335                                                         rep.getValue<int>(OC_RSRVD_ES_PROVSTATUS));
336             }
337
338             if(rep.hasAttribute(OC_RSRVD_ES_LAST_ERRORCODE))
339             {
340                 enrolleeStatus.lastErrCode = static_cast<ESErrorCode>(
341                                                         rep.getValue<int>(OC_RSRVD_ES_LAST_ERRORCODE));
342             }
343
344             return enrolleeStatus;
345         }
346     }
347 }