392cf6c7f9435b15018149243290bc808798d085
[platform/upstream/iotivity.git] / service / easy-setup / enrollee / unittests / ESMediatorSimulator.h
1 //******************************************************************
2 //
3 // Copyright 2016 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 #ifndef _ES_MEDIATOR_SIMULATOR_H_
22 #define _ES_MEDIATOR_SIMULATOR_H_
23
24 #include <iostream>
25
26 #include "OCPlatform.h"
27 #include "OCApi.h"
28 #include "oic_malloc.h"
29
30 #include "EasySetup.hpp"
31 #include "ESRichCommon.h"
32
33 #include "ocrandom.h"
34 #include "cainterface.h"
35 #include "OCPlatform.h"
36
37 using namespace OIC::Service;
38
39 class ESMediatorSimulator
40 {
41 private:
42     std::shared_ptr<RemoteEnrollee> m_remoteEnrollee;
43     std::function<void(std::shared_ptr<OC::OCResource> resource)> m_discoveryCb;
44     std::function<void(std::shared_ptr< GetConfigurationStatus > status)> m_getConfigurationCb;
45     std::function<void(std::shared_ptr< GetEnrolleeStatus >)> m_getStatusCb;
46     std::function<void(std::shared_ptr< DevicePropProvisioningStatus >)> m_DevicePropProvisioningCb;
47     std::function<void(std::shared_ptr< CloudPropProvisioningStatus >)> m_CloudPropProvisioningCb;
48     std::function<void(const OCRepresentation&)> m_getWifiCb;
49     std::function<void(const OCRepresentation&)> m_getCloudCb;
50     std::function<void(const OCRepresentation&)> m_getDevConfCb;
51     std::function<void(const OCRepresentation&)> m_provPutCb;
52
53 public:
54     ESMediatorSimulator()
55     : m_remoteEnrollee(), m_discoveryCb(), m_getConfigurationCb(), m_getStatusCb(),
56     m_DevicePropProvisioningCb(), m_CloudPropProvisioningCb(),
57     m_getWifiCb(), m_getCloudCb(), m_getDevConfCb(), m_provPutCb()
58     {
59     };
60     ~ESMediatorSimulator() = default;
61
62     ESMediatorSimulator(const ESMediatorSimulator &) = delete;
63     ESMediatorSimulator & operator = (const ESMediatorSimulator &) = delete;
64
65     ESMediatorSimulator(ESMediatorSimulator &&) = delete;
66     ESMediatorSimulator & operator = (ESMediatorSimulator &&) = delete;
67
68     void discoverRemoteEnrollee(std::function<void(std::shared_ptr<OC::OCResource> resource)> cb)
69     {
70         m_discoveryCb = cb;
71         std::string uri = std::string("/oic/res?rt=") + OC_RSRVD_ES_RES_TYPE_PROV;
72         OC::OCPlatform::findResource("", uri,
73                 OCConnectivityType::CT_DEFAULT,
74                 std::bind(&ESMediatorSimulator::discoverRemoteEnrolleeCb,
75                                                                     this, std::placeholders::_1));
76
77     }
78
79     void getConfiguration(std::function<void(std::shared_ptr< GetConfigurationStatus >)> cb)
80     {
81         m_getConfigurationCb = cb;
82         m_remoteEnrollee = NULL;
83         std::string uri = std::string("/oic/res?rt=") + OC_RSRVD_ES_RES_TYPE_PROV;
84         OC::OCPlatform::findResource("", uri,
85                 OCConnectivityType::CT_DEFAULT,
86                 std::bind(&ESMediatorSimulator::discoverRemoteEnrolleeCbToGetConfiguration,
87                                                                     this, std::placeholders::_1));
88     }
89
90     void getWifiRsrc(std::function<void(const OCRepresentation& rep)> cb)
91     {
92         m_getWifiCb = cb;
93         std::string uri = std::string("/oic/res?rt=") + OC_RSRVD_ES_RES_TYPE_WIFI;
94         OC::OCPlatform::findResource("", uri,
95                 OCConnectivityType::CT_DEFAULT,
96                 std::bind(&ESMediatorSimulator::discoverRemoteEnrolleeCbToGetWifiRsrc,
97                                                                     this, std::placeholders::_1));
98     }
99
100     void getCloudRsrc(std::function<void(const OCRepresentation& rep)> cb)
101     {
102         m_getCloudCb = cb;
103         std::string uri = std::string("/oic/res?rt=") + OC_RSRVD_ES_RES_TYPE_CLOUDSERVER;
104         OC::OCPlatform::findResource("", uri,
105                 OCConnectivityType::CT_DEFAULT,
106                 std::bind(&ESMediatorSimulator::discoverRemoteEnrolleeCbToGetCloudRsrc,
107                                                                     this, std::placeholders::_1));
108     }
109
110     void getDevConfiguration(std::function<void(const OCRepresentation& rep)> cb)
111     {
112         m_getDevConfCb = cb;
113         std::string uri = std::string("/oic/res?rt=") + OC_RSRVD_ES_RES_TYPE_DEVCONF;
114         OC::OCPlatform::findResource("", uri,
115                 OCConnectivityType::CT_DEFAULT,
116                 std::bind(&ESMediatorSimulator::discoverRemoteEnrolleeCbToGetDevConf,
117                                                                     this, std::placeholders::_1));
118     }
119
120     void getStatus(std::function<void(std::shared_ptr< GetEnrolleeStatus >)> cb)
121     {
122         m_getStatusCb = cb;
123         m_remoteEnrollee = NULL;
124         std::string uri = std::string("/oic/res?rt=") + OC_RSRVD_ES_RES_TYPE_PROV;
125         OC::OCPlatform::findResource("", uri,
126                 OCConnectivityType::CT_DEFAULT,
127                 std::bind(&ESMediatorSimulator::discoverRemoteEnrolleeCbToGetStatus,
128                                                                     this, std::placeholders::_1));
129     }
130
131     void provisionDeviceProperties(std::function<void(std::shared_ptr< DevicePropProvisioningStatus >)> cb)
132     {
133         m_DevicePropProvisioningCb = cb;
134         m_remoteEnrollee = NULL;
135         std::string uri = std::string("/oic/res?rt=") + OC_RSRVD_ES_RES_TYPE_PROV;
136         OC::OCPlatform::findResource("", uri,
137                 OCConnectivityType::CT_DEFAULT,
138                 std::bind(&ESMediatorSimulator::discoverRemoteEnrolleeCbToProvisionDeviceProperties,
139                                                                     this, std::placeholders::_1));
140     }
141
142     void provisionCloudProperties(std::function<void(std::shared_ptr< CloudPropProvisioningStatus >)> cb)
143     {
144         m_CloudPropProvisioningCb = cb;
145         m_remoteEnrollee = NULL;
146         std::string uri = std::string("/oic/res?rt=") + OC_RSRVD_ES_RES_TYPE_PROV;
147         OC::OCPlatform::findResource("", uri,
148                 OCConnectivityType::CT_DEFAULT,
149                 std::bind(&ESMediatorSimulator::discoverRemoteEnrolleeCbToProvisionCloudProperties,
150                                                                     this, std::placeholders::_1));
151     }
152
153     void putProvRsrc(std::function<void(const OCRepresentation& rep)> cb)
154     {
155         m_provPutCb = cb;
156         m_remoteEnrollee = NULL;
157         std::string uri = std::string("/oic/res?rt=") + OC_RSRVD_ES_RES_TYPE_PROV;
158         OC::OCPlatform::findResource("", uri,
159                 OCConnectivityType::CT_DEFAULT,
160                 std::bind(&ESMediatorSimulator::discoverRemoteEnrolleeCbToPutProvRsrc,
161                                                                     this, std::placeholders::_1));
162     }
163
164 private:
165     bool isValidResourceToTest(std::shared_ptr<OC::OCResource> resource)
166     {
167         if((resource->connectivityType() & CT_ADAPTER_TCP) == CT_ADAPTER_TCP)
168         {
169             return false;
170         }
171
172         CAEndpoint_t *tempInfo = NULL;
173         uint32_t tempSize = 0;
174
175         CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
176         if (CA_STATUS_OK != res || NULL == tempInfo || 0 >= tempSize)
177         {
178             free(tempInfo);
179             return false;
180         }
181
182         for (uint32_t index = 0; index  < tempSize; index++)
183         {
184             if (CA_ADAPTER_IP == tempInfo[index].adapter)
185             {
186                 if(resource->host().find(tempInfo[index].addr) != std::string::npos &&
187                     resource->host().find(std::to_string(tempInfo[index].port).c_str()) != std::string::npos)
188                 {
189                     return true;
190                 }
191             }
192         }
193
194         return false;
195     }
196
197     void discoverRemoteEnrolleeCb(std::shared_ptr<OC::OCResource> resource)
198     {
199         if(!isValidResourceToTest(resource))
200         {
201             return ;
202         }
203
204         if(!resource->getResourceTypes().at(0).compare(OC_RSRVD_ES_RES_TYPE_PROV) && m_discoveryCb)
205         {
206             m_discoveryCb(resource);
207             m_discoveryCb = NULL;
208         }
209     }
210
211     void getConfigurationCallback(std::shared_ptr< GetConfigurationStatus > getConfigurationStatus)
212     {
213         if(m_getConfigurationCb != NULL)
214         {
215             m_getConfigurationCb(getConfigurationStatus);
216             m_getConfigurationCb = NULL;
217         }
218     }
219
220     void discoverRemoteEnrolleeCbToGetConfiguration(std::shared_ptr<OC::OCResource> resource)
221     {
222         if(!isValidResourceToTest(resource))
223         {
224             return ;
225         }
226
227         if(!resource->getResourceTypes().at(0).compare(OC_RSRVD_ES_RES_TYPE_PROV) && m_getConfigurationCb
228                                                                            && !m_remoteEnrollee)
229         {
230             m_remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource);
231
232             if(m_remoteEnrollee != NULL)
233             {
234               m_remoteEnrollee->getConfiguration(std::bind(
235                     &ESMediatorSimulator::getConfigurationCallback, this, std::placeholders::_1));
236             }
237         }
238     }
239
240     void getWifiRsrcCallback(const HeaderOptions& , const OCRepresentation& rep, const int eCode)
241     {
242         try
243         {
244             if(eCode == OC_STACK_OK)
245             {
246                 std::cout << "GET request was successful" << std::endl;
247                 std::cout << "Resource URI: " << rep.getUri() << std::endl;
248                 if(m_getWifiCb != NULL)
249                 {
250                     m_getWifiCb(rep);
251                     m_getWifiCb = NULL;
252                 }
253             }
254             else
255             {
256                 std::cout << "onGET Response error: " << eCode << std::endl;
257                 std::exit(-1);
258             }
259         }
260         catch(std::exception& e)
261         {
262             std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
263         }
264     }
265
266     void discoverRemoteEnrolleeCbToGetWifiRsrc(std::shared_ptr<OC::OCResource> resource)
267     {
268
269         if(!isValidResourceToTest(resource))
270         {
271             return ;
272         }
273
274         if(!resource->getResourceTypes().at(0).compare(OC_RSRVD_ES_RES_TYPE_WIFI) && m_getWifiCb)
275         {
276             QueryParamsMap test;
277             resource->get(test, std::bind(
278                     &ESMediatorSimulator::getWifiRsrcCallback, this, std::placeholders::_1,
279                                             std::placeholders::_2, std::placeholders::_3));
280         }
281     }
282
283     void getCloudRsrcCallback(const HeaderOptions& , const OCRepresentation& rep, const int eCode)
284     {
285         try
286         {
287             if(eCode == OC_STACK_OK)
288             {
289                 std::cout << "GET request was successful" << std::endl;
290                 std::cout << "Resource URI: " << rep.getUri() << std::endl;
291                 if(m_getCloudCb != NULL)
292                 {
293                     m_getCloudCb(rep);
294                     m_getCloudCb = NULL;
295                 }
296             }
297             else
298             {
299                 std::cout << "onGET Response error: " << eCode << std::endl;
300                 std::exit(-1);
301             }
302         }
303         catch(std::exception& e)
304         {
305             std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
306         }
307     }
308
309
310     void discoverRemoteEnrolleeCbToGetCloudRsrc(std::shared_ptr<OC::OCResource> resource)
311     {
312         if(!isValidResourceToTest(resource))
313         {
314             return ;
315         }
316
317         if(!resource->getResourceTypes().at(0).compare(OC_RSRVD_ES_RES_TYPE_CLOUDSERVER) && m_getCloudCb)
318         {
319             QueryParamsMap test;
320             resource->get(test, std::bind(
321                     &ESMediatorSimulator::getCloudRsrcCallback, this, std::placeholders::_1,
322                                             std::placeholders::_2, std::placeholders::_3));
323         }
324     }
325
326     void getDevConfCallback(const HeaderOptions&, const OCRepresentation& rep, const int eCode)
327     {
328         try
329         {
330             if(eCode == OC_STACK_OK)
331             {
332                 std::cout << "GET request was successful" << std::endl;
333                 std::cout << "Resource URI: " << rep.getUri() << std::endl;
334                 if(m_getDevConfCb != NULL)
335                 {
336                     m_getDevConfCb(rep);
337                     m_getDevConfCb = NULL;
338                 }
339             }
340             else
341             {
342                 std::cout << "onGET Response error: " << eCode << std::endl;
343                 std::exit(-1);
344             }
345         }
346         catch(std::exception& e)
347         {
348             std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
349         }
350     }
351
352     void discoverRemoteEnrolleeCbToGetDevConf(std::shared_ptr<OC::OCResource> resource)
353     {
354         if(!isValidResourceToTest(resource))
355         {
356             return ;
357         }
358
359         if(!resource->getResourceTypes().at(0).compare(OC_RSRVD_ES_RES_TYPE_DEVCONF) && m_getDevConfCb)
360         {
361             QueryParamsMap test;
362             resource->get(test, std::bind(
363                     &ESMediatorSimulator::getDevConfCallback, this, std::placeholders::_1,
364                                             std::placeholders::_2, std::placeholders::_3));
365         }
366     }
367
368     void getStatusCallback(std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus)
369     {
370         if(m_getStatusCb != NULL)
371         {
372             m_getStatusCb(getEnrolleeStatus);
373             m_getStatusCb = NULL;
374         }
375     }
376
377     void discoverRemoteEnrolleeCbToGetStatus(std::shared_ptr<OC::OCResource> resource)
378     {
379         if(!isValidResourceToTest(resource))
380         {
381             return ;
382         }
383
384         if(!resource->getResourceTypes().at(0).compare(OC_RSRVD_ES_RES_TYPE_PROV) && m_getStatusCb
385                                                                             && !m_remoteEnrollee)
386         {
387             m_remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource);
388
389             if(m_remoteEnrollee != NULL)
390             {
391                 m_remoteEnrollee->getStatus(std::bind(
392                     &ESMediatorSimulator::getStatusCallback, this, std::placeholders::_1));
393             }
394         }
395     }
396
397     void deviceProvisioningStatusCallback(std::shared_ptr< DevicePropProvisioningStatus >
398                                                                     devicePropProvisioningStatus)
399     {
400         if(m_DevicePropProvisioningCb != NULL)
401         {
402             m_DevicePropProvisioningCb(devicePropProvisioningStatus);
403             m_DevicePropProvisioningCb = NULL;
404         }
405     }
406
407     void discoverRemoteEnrolleeCbToProvisionDeviceProperties(std::shared_ptr<OC::OCResource> resource)
408     {
409         if(!isValidResourceToTest(resource))
410         {
411             return ;
412         }
413
414         if(!resource->getResourceTypes().at(0).compare(OC_RSRVD_ES_RES_TYPE_PROV) &&
415                                                 m_DevicePropProvisioningCb && !m_remoteEnrollee)
416         {
417             m_remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource);
418
419             if(m_remoteEnrollee != NULL)
420             {
421                 DeviceProp devProp;
422                 devProp.setWiFiProp("Iotivity_SSID", "Iotivity_PWD", WPA2_PSK, TKIP_AES);
423                 devProp.setDevConfProp("korean", "Korea", "Location");
424
425                 m_remoteEnrollee->provisionDeviceProperties(devProp,
426                     std::bind(&ESMediatorSimulator::deviceProvisioningStatusCallback,
427                                                             this, std::placeholders::_1));
428             }
429         }
430     }
431
432     void cloudProvisioningStatusCallback(std::shared_ptr< CloudPropProvisioningStatus >
433                                                                     cloudPropProvisioningStatus)
434     {
435         if(m_CloudPropProvisioningCb != NULL)
436         {
437             m_CloudPropProvisioningCb(cloudPropProvisioningStatus);
438             if(cloudPropProvisioningStatus->getESResult() == ES_OK)
439             {
440                 m_CloudPropProvisioningCb = NULL;
441             }
442         }
443     }
444
445     void discoverRemoteEnrolleeCbToProvisionCloudProperties(std::shared_ptr<OC::OCResource> resource)
446     {
447         if(!isValidResourceToTest(resource))
448         {
449             return ;
450         }
451
452         if(!resource->getResourceTypes().at(0).compare(OC_RSRVD_ES_RES_TYPE_PROV) &&
453                                                 m_CloudPropProvisioningCb && !m_remoteEnrollee)
454         {
455             m_remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource);
456
457             if(m_remoteEnrollee != NULL)
458             {
459                 CloudProp cloudProp;
460                 cloudProp.setCloudProp("authCode", "authProvider", "ciServer");
461
462                 m_remoteEnrollee->provisionCloudProperties(cloudProp,
463                     std::bind(&ESMediatorSimulator::cloudProvisioningStatusCallback,
464                                                             this, std::placeholders::_1));
465             }
466         }
467     }
468
469     void putProvRsrcCallabck(const HeaderOptions&, const OCRepresentation& rep, const int )
470     {
471         cout << "putProvRsrcCallback is called" << endl;
472
473         if(m_provPutCb != NULL){
474             m_provPutCb(rep);
475             m_provPutCb = NULL;
476         }
477
478     }
479
480     void discoverRemoteEnrolleeCbToPutProvRsrc(std::shared_ptr<OC::OCResource> resource)
481     {
482         m_remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource);
483
484         if(m_remoteEnrollee != NULL)
485         {
486             if(m_provPutCb)
487             {
488                 OCRepresentation rep;
489                 QueryParamsMap test;
490                 resource->put(rep, test, std::bind(
491                         &ESMediatorSimulator::putProvRsrcCallabck, this, std::placeholders::_1,
492                                                 std::placeholders::_2, std::placeholders::_3));
493                 }
494
495         }
496
497     }
498
499 };
500
501
502 #endif //_NS_CONSUMER_SIMULATOR_H_
503