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::onGetConfigurationResponse(const HeaderOptions& /*headerOptions*/,
67                 const OCRepresentation& rep, const int eCode)
68         {
69             OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onGetConfigurationResponse : %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,"onGetConfigurationResponse : onGetConfigurationResponse is failed ");
77
78                 if (eCode == OCStackResult::OC_STACK_UNAUTHORIZED_REQ)
79                 {
80                     OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
81                         "Mediator is unauthorized from Enrollee.");
82                     result = ESResult::ES_UNAUTHORIZED;
83                 }
84
85                 EnrolleeConf enrolleeConf;
86                 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
87                         GetConfigurationStatus >(result, enrolleeConf);
88                 m_getConfigurationStatusCb(getConfigurationStatus);
89             }
90             else
91             {
92                 EnrolleeConf enrolleeConf = parseEnrolleeConfFromRepresentation(rep);
93
94                 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
95                         GetConfigurationStatus >(ESResult::ES_OK, enrolleeConf);
96                 m_getConfigurationStatusCb(getConfigurationStatus);
97             }
98         }
99
100         void EnrolleeResource::registerGetConfigurationStatusCallback(GetConfigurationStatusCb callback)
101         {
102             m_getConfigurationStatusCb = callback;
103         }
104
105         void EnrolleeResource::registerDevicePropProvStatusCallback(DevicePropProvStatusCb callback)
106         {
107             m_devicePropProvStatusCb = callback;
108         }
109
110         void EnrolleeResource::getConfiguration()
111         {
112             if (m_ocResource == nullptr)
113             {
114                 throw ESBadRequestException("Resource is not initialized");
115             }
116
117             OC::QueryParamsMap query;
118             OC::OCRepresentation rep;
119
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)));
127             };
128
129             OCStackResult result = getConfigurationStatus();
130
131             if (result != OCStackResult::OC_STACK_OK)
132             {
133                 EnrolleeConf enrolleeConf;
134                 std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared<
135                         GetConfigurationStatus >(ESResult::ES_ERROR, enrolleeConf);
136                 m_getConfigurationStatusCb(getConfigurationStatus);
137                 return;
138             }
139         }
140
141         void EnrolleeResource::provisionEnrollee(const DeviceProp& deviceProp)
142
143         {
144             if (m_ocResource == nullptr)
145             {
146                 throw ESBadRequestException("Resource is not initialized");
147             }
148
149             OC::QueryParamsMap query;
150             OC::OCRepresentation provisioningRepresentation;
151
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);
158
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());
171
172             m_ocResource->post(OC_RSRVD_ES_RES_TYPE_PROV, BATCH_INTERFACE,
173                     provisioningRepresentation, QueryParamsMap(),
174                     std::function<
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)));
180         }
181
182         EnrolleeConf EnrolleeResource::parseEnrolleeConfFromRepresentation(const OCRepresentation& rep)
183         {
184             OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_RES_TAG, "Enter parseEnrolleeConfFromRepresentation");
185
186             DeviceConfig devConf;
187             WiFiConfig wifiConf;
188             bool cloudable = false;
189
190             std::vector<OCRepresentation> children = rep.getChildren();
191
192             for(auto prop = children.begin(); prop != children.end(); ++prop)
193             {
194                 if(prop->getUri().find(OC_RSRVD_ES_URI_WIFI) != std::string::npos)
195                 {
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))
199                     {
200                         std::vector<int> modes = prop->getValue<std::vector<int>>(OC_RSRVD_ES_SUPPORTEDWIFIMODE);
201
202                         for(auto mode = modes.begin(); mode != modes.end(); ++mode)
203                         {
204                             OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_SUPPORTEDWIFIMODE = %d",
205                                 *mode);
206                             wifiConf.modes.push_back(static_cast<WIFI_MODE>(*mode));
207                         }
208
209                         wifiConf.freq = static_cast<WIFI_FREQ>(prop->getValue<int>(OC_RSRVD_ES_SUPPORTEDWIFIFREQ));
210
211
212                         OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_SUPPORTEDWIFIFREQ = %d",
213                                 wifiConf.freq);
214                     }
215                 }
216
217                 else if(prop->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos)
218                 {
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))
223                     {
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);
229
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());
236                     }
237                 }
238
239                 else if(prop->getUri().find(OC_RSRVD_ES_URI_CLOUDSERVER) != std::string::npos)
240                 {
241                     OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_RES_TAG, "Find cloudserver");
242                     cloudable = true;
243                     OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "cloudable = %d",
244                                 cloudable);
245                 }
246             }
247
248             return EnrolleeConf(devConf, wifiConf, cloudable);
249         }
250
251     }
252 }