b2969fbbcdf18c71d11091e1af1f626ab73ee925
[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.h"
31 #include "ESRichCommon.h"
32
33 #define PROV_RESOURCE_TYPE "ocf.wk.prov"
34
35 using namespace OIC::Service;
36
37 class ESMediatorSimulator
38 {
39 private:
40     std::function<void(std::shared_ptr<OC::OCResource> resource)> m_discoveryCb;
41     std::function<void(std::shared_ptr< GetConfigurationStatus > status)> m_getConfigurationCb;
42     std::function<void(std::shared_ptr< GetEnrolleeStatus >)> m_getStatusCb;
43     std::function<void(std::shared_ptr< DevicePropProvisioningStatus >)> m_DevicePropProvisioningCb;
44     std::function<void(std::shared_ptr< CloudPropProvisioningStatus >)> m_CloudPropProvisioningCb;
45
46     std::shared_ptr<RemoteEnrollee> m_remoteEnrollee;
47
48 public:
49     ESMediatorSimulator()
50     : m_remoteEnrollee(), m_discoveryCb(), m_getConfigurationCb(), m_getStatusCb(),
51     m_DevicePropProvisioningCb(), m_CloudPropProvisioningCb()
52     {
53     };
54     ~ESMediatorSimulator() = default;
55
56     ESMediatorSimulator(const ESMediatorSimulator &) = delete;
57     ESMediatorSimulator & operator = (const ESMediatorSimulator &) = delete;
58
59     ESMediatorSimulator(ESMediatorSimulator &&) = delete;
60     ESMediatorSimulator & operator = (ESMediatorSimulator &&) = delete;
61
62     void discoverRemoteEnrollee(std::function<void(std::shared_ptr<OC::OCResource> resource)> cb)
63     {
64         m_discoveryCb = cb;
65         std::string uri = std::string("/oic/res?rt=") + PROV_RESOURCE_TYPE;
66         OC::OCPlatform::findResource("", uri,
67                 OCConnectivityType::CT_DEFAULT,
68                 std::bind(&ESMediatorSimulator::discoverRemoteEnrolleeCb,
69                                                                     this, std::placeholders::_1));
70
71     }
72
73     void getConfiguration(std::function<void(std::shared_ptr< GetConfigurationStatus >)> cb)
74     {
75         m_getConfigurationCb = cb;
76         m_remoteEnrollee = NULL;
77         std::string uri = std::string("/oic/res?rt=") + PROV_RESOURCE_TYPE;
78         OC::OCPlatform::findResource("", uri,
79                 OCConnectivityType::CT_DEFAULT,
80                 std::bind(&ESMediatorSimulator::discoverRemoteEnrolleeCbToGetConfiguration,
81                                                                     this, std::placeholders::_1));
82     }
83
84     void getStatus(std::function<void(std::shared_ptr< GetEnrolleeStatus >)> cb)
85     {
86         m_getStatusCb = cb;
87         m_remoteEnrollee = NULL;
88         std::string uri = std::string("/oic/res?rt=") + PROV_RESOURCE_TYPE;
89         OC::OCPlatform::findResource("", uri,
90                 OCConnectivityType::CT_DEFAULT,
91                 std::bind(&ESMediatorSimulator::discoverRemoteEnrolleeCbToGetStatus,
92                                                                     this, std::placeholders::_1));
93     }
94
95     void provisionDeviceProperties(std::function<void(std::shared_ptr< DevicePropProvisioningStatus >)> cb)
96     {
97         m_DevicePropProvisioningCb = cb;
98         m_remoteEnrollee = NULL;
99         std::string uri = std::string("/oic/res?rt=") + PROV_RESOURCE_TYPE;
100         OC::OCPlatform::findResource("", uri,
101                 OCConnectivityType::CT_DEFAULT,
102                 std::bind(&ESMediatorSimulator::discoverRemoteEnrolleeCbToProvisionDeviceProperties,
103                                                                     this, std::placeholders::_1));
104     }
105
106     void provisionCloudProperties(std::function<void(std::shared_ptr< CloudPropProvisioningStatus >)> cb)
107     {
108         m_CloudPropProvisioningCb = cb;
109         m_remoteEnrollee = NULL;
110         std::string uri = std::string("/oic/res?rt=") + PROV_RESOURCE_TYPE;
111         OC::OCPlatform::findResource("", uri,
112                 OCConnectivityType::CT_DEFAULT,
113                 std::bind(&ESMediatorSimulator::discoverRemoteEnrolleeCbToProvisionCloudProperties,
114                                                                     this, std::placeholders::_1));
115     }
116
117 private:
118
119     void discoverRemoteEnrolleeCb(std::shared_ptr<OC::OCResource> resource)
120     {
121         if(!resource->getResourceTypes().at(0).compare(PROV_RESOURCE_TYPE) && m_discoveryCb)
122         {
123             m_discoveryCb(resource);
124             m_discoveryCb = NULL;
125         }
126     }
127
128     void getConfigurationCallback(std::shared_ptr< GetConfigurationStatus > getConfigurationStatus)
129     {
130         if(m_getConfigurationCb != NULL)
131         {
132             m_getConfigurationCb(getConfigurationStatus);
133             m_getConfigurationCb = NULL;
134         }
135     }
136
137     void discoverRemoteEnrolleeCbToGetConfiguration(std::shared_ptr<OC::OCResource> resource)
138     {
139         if(!resource->getResourceTypes().at(0).compare(PROV_RESOURCE_TYPE) && m_getConfigurationCb
140                                                                            && !m_remoteEnrollee)
141         {
142             m_remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource);
143
144             if(m_remoteEnrollee != NULL)
145             {
146               m_remoteEnrollee->getConfiguration(std::bind(
147                     &ESMediatorSimulator::getConfigurationCallback, this, std::placeholders::_1));
148             }
149         }
150     }
151
152     void getStatusCallback(std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus)
153     {
154         if(m_getStatusCb != NULL)
155         {
156             m_getStatusCb(getEnrolleeStatus);
157             m_getStatusCb = NULL;
158         }
159     }
160
161     void discoverRemoteEnrolleeCbToGetStatus(std::shared_ptr<OC::OCResource> resource)
162     {
163         if(!resource->getResourceTypes().at(0).compare(PROV_RESOURCE_TYPE) && m_getStatusCb
164                                                                             && !m_remoteEnrollee)
165         {
166             m_remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource);
167
168             if(m_remoteEnrollee != NULL)
169             {
170                 m_remoteEnrollee->getStatus(std::bind(
171                     &ESMediatorSimulator::getStatusCallback, this, std::placeholders::_1));
172             }
173         }
174     }
175
176     void deviceProvisioningStatusCallback(std::shared_ptr< DevicePropProvisioningStatus >
177                                                                     devicePropProvisioningStatus)
178     {
179         if(m_DevicePropProvisioningCb != NULL)
180         {
181             m_DevicePropProvisioningCb(devicePropProvisioningStatus);
182             m_DevicePropProvisioningCb = NULL;
183         }
184     }
185
186     void discoverRemoteEnrolleeCbToProvisionDeviceProperties(std::shared_ptr<OC::OCResource> resource)
187     {
188         if(!resource->getResourceTypes().at(0).compare(PROV_RESOURCE_TYPE) &&
189                                                 m_DevicePropProvisioningCb && !m_remoteEnrollee)
190         {
191             m_remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource);
192
193             if(m_remoteEnrollee != NULL)
194             {
195                 DeviceProp devProp;
196                 devProp.setWiFiProp("Iotivity_SSID", "Iotivity_PWD", WPA2_PSK, TKIP_AES);
197                 devProp.setDevConfProp("korean", "Korea", "Location");
198
199                 m_remoteEnrollee->provisionDeviceProperties(devProp,
200                     std::bind(&ESMediatorSimulator::deviceProvisioningStatusCallback,
201                                                             this, std::placeholders::_1));
202             }
203         }
204     }
205
206     void cloudProvisioningStatusCallback(std::shared_ptr< CloudPropProvisioningStatus >
207                                                                     cloudPropProvisioningStatus)
208     {
209         if(m_CloudPropProvisioningCb != NULL)
210         {
211             m_CloudPropProvisioningCb(cloudPropProvisioningStatus);
212             if(cloudPropProvisioningStatus->getESCloudState() == ES_CLOUD_PROVISIONING_SUCCESS)
213             {
214                 m_CloudPropProvisioningCb = NULL;
215             }
216         }
217     }
218
219     void discoverRemoteEnrolleeCbToProvisionCloudProperties(std::shared_ptr<OC::OCResource> resource)
220     {
221         if(!resource->getResourceTypes().at(0).compare(PROV_RESOURCE_TYPE) &&
222                                                 m_CloudPropProvisioningCb && !m_remoteEnrollee)
223         {
224             m_remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource);
225
226             if(m_remoteEnrollee != NULL)
227             {
228                 CloudProp cloudProp;
229                 cloudProp.setCloudProp("authCode", "authProvider", "ciServer");
230
231                 m_remoteEnrollee->provisionCloudProperties(cloudProp,
232                     std::bind(&ESMediatorSimulator::cloudProvisioningStatusCallback,
233                                                             this, std::placeholders::_1));
234             }
235         }
236     }
237 };
238
239
240 #endif //_NS_CONSUMER_SIMULATOR_H_
241