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