Merge remote-tracking branch 'origin/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 != 0)
49             {
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);
55                 return;
56             }
57
58             OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
59                     "checkProvInformationCb : Provisioning is success. ");
60
61             std::shared_ptr< DataProvisioningStatus > provStatus = std::make_shared<
62                     DataProvisioningStatus >(ESResult::ES_OK, ESDataProvState::ES_PROVISIONING_SUCCESS);
63             m_dataProvStatusCb(provStatus);
64         }
65
66         void EnrolleeResource::onRequestPropertyDataResponse(const HeaderOptions& /*headerOptions*/,
67                 const OCRepresentation& rep, const int eCode)
68         {
69             OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onRequestPropertyDataResponse : %s, eCode = %d",
70                     rep.getUri().c_str(), eCode);
71
72             if (eCode != 0)
73             {
74                 ESResult result  = ESResult::ES_ERROR;
75
76                 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,"onRequestPropertyDataResponse : onRequestPropertyDataResponse 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                 PropertyData propertyData;
86                 std::shared_ptr< RequestPropertyDataStatus > requestPropertyDataStatus = std::make_shared<
87                         RequestPropertyDataStatus >(result, propertyData );
88                 m_RequestPropertyDataStatusCb(requestPropertyDataStatus);
89             }
90             else
91             {
92                 PropertyData propertyData = parsePropertyDataFromRepresentation(rep);
93
94                 std::shared_ptr< RequestPropertyDataStatus > requestPropertyDataStatus = std::make_shared<
95                         RequestPropertyDataStatus >(ESResult::ES_OK, propertyData);
96                 m_RequestPropertyDataStatusCb(requestPropertyDataStatus);
97             }
98         }
99
100         void EnrolleeResource::registerRequestPropertyDataStatusCallback(RequestPropertyDataStatusCb callback)
101         {
102             m_RequestPropertyDataStatusCb = callback;
103         }
104
105         void EnrolleeResource::registerProvStatusCallback(DataProvStatusCb callback)
106         {
107             m_dataProvStatusCb = callback;
108         }
109
110         void EnrolleeResource::RequestPropertyData()
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) > 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)));
127             };
128
129             OCStackResult result = requestPropertyDataStatus();
130
131             if (result != OCStackResult::OC_STACK_OK)
132             {
133                 PropertyData propertyData;
134                 std::shared_ptr< RequestPropertyDataStatus > requestPropertyDataStatus = std::make_shared<
135                         RequestPropertyDataStatus >(ESResult::ES_ERROR, propertyData);
136                 m_RequestPropertyDataStatusCb(requestPropertyDataStatus);
137                 return;
138             }
139         }
140
141         void EnrolleeResource::provisionEnrollee(const DataProvInfo& dataProvInfo)
142
143         {
144             if (m_ocResource == nullptr)
145             {
146                 throw ESBadRequestException("Resource is not initialized");
147             }
148
149             m_dataProvInfo = dataProvInfo;
150
151             OC::QueryParamsMap query;
152             OC::OCRepresentation provisioningRepresentation;
153
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);
160
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());
173
174             m_ocResource->post(OC_RSRVD_ES_RES_TYPE_PROV, BATCH_INTERFACE,
175                     provisioningRepresentation, QueryParamsMap(),
176                     std::function<
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)));
182         }
183
184         void EnrolleeResource::unprovisionEnrollee()
185         {
186             if (m_ocResource == nullptr)
187             {
188                 throw ESBadRequestException("Resource is not initialized");
189             }
190
191             OCRepresentation provisioningRepresentation;
192
193             provisioningRepresentation.setValue(OC_RSRVD_ES_SSID, "");
194             provisioningRepresentation.setValue(OC_RSRVD_ES_CRED, "");
195
196             m_ocResource->post(provisioningRepresentation, QueryParamsMap(),
197                     std::function<
198                             void(const HeaderOptions& headerOptions, const OCRepresentation& rep,
199                                     const int eCode) >(
200                     std::bind(&EnrolleeResource::checkProvInformationCb, this,
201                     std::placeholders::_1, std::placeholders::_2,
202                     std::placeholders::_3)));
203         }
204
205         PropertyData EnrolleeResource::parsePropertyDataFromRepresentation(const OCRepresentation& rep)
206         {
207             OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_RES_TAG, "Enter parsePropertyDataFromRepresentation");
208
209             DeviceConfig devInfo;
210             NetworkInfo netInfo;
211             bool cloudable = false;
212
213             std::vector<OCRepresentation> children = rep.getChildren();
214
215             for(auto prop = children.begin(); prop != children.end(); ++prop)
216             {
217                 if(prop->getUri().find(OC_RSRVD_ES_URI_WIFI) != std::string::npos)
218                 {
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))
222                     {
223                         std::vector<int> types = prop->getValue<std::vector<int>>(OC_RSRVD_ES_SUPPORTEDWIFIMODE);
224
225                         for(auto type = types.begin(); type != types.end(); ++type)
226                         {
227                             OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_SUPPORTEDWIFIMODE = %d",
228                                 *type);
229                             netInfo.types.push_back(static_cast<WIFI_MODE>(*type));
230                         }
231
232                         netInfo.freq = static_cast<WIFI_FREQ>(prop->getValue<int>(OC_RSRVD_ES_SUPPORTEDWIFIFREQ));
233
234
235                         OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_SUPPORTEDWIFIFREQ = %d",
236                                 netInfo.freq);
237                     }
238                 }
239
240                 else if(prop->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos)
241                 {
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))
246                     {
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);
252
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());
259                     }
260                 }
261
262                 else if(prop->getUri().find(OC_RSRVD_ES_URI_CLOUDSERVER) != std::string::npos)
263                 {
264                     OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_RES_TAG, "Find cloudserver");
265                     cloudable = true;
266                     OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "cloudable = %d",
267                                 cloudable);
268                 }
269             }
270
271             return PropertyData(devInfo, netInfo, cloudable);
272         }
273
274     }
275 }