1 //******************************************************************
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #ifndef _ES_MEDIATOR_SIMULATOR_H_
22 #define _ES_MEDIATOR_SIMULATOR_H_
26 #include "OCPlatform.h"
28 #include "oic_malloc.h"
30 #include "EasySetup.h"
31 #include "ESRichCommon.h"
33 #define PROV_RESOURCE_TYPE "ocf.wk.prov"
35 using namespace OIC::Service;
37 class ESMediatorSimulator
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;
46 std::shared_ptr<RemoteEnrollee> m_remoteEnrollee;
50 : m_remoteEnrollee(), m_discoveryCb(), m_getConfigurationCb(), m_getStatusCb(),
51 m_DevicePropProvisioningCb(), m_CloudPropProvisioningCb() { };
52 ~ESMediatorSimulator() = default;
54 ESMediatorSimulator(const ESMediatorSimulator &) = delete;
55 ESMediatorSimulator & operator = (const ESMediatorSimulator &) = delete;
57 ESMediatorSimulator(ESMediatorSimulator &&) = delete;
58 ESMediatorSimulator & operator = (ESMediatorSimulator &&) = delete;
60 void discoverRemoteEnrollee(std::function<void(std::shared_ptr<OC::OCResource> resource)> 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));
71 void getConfiguration(std::function<void(std::shared_ptr< GetConfigurationStatus >)> cb)
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));
82 void getStatus(std::function<void(std::shared_ptr< GetEnrolleeStatus >)> 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));
93 void provisionDeviceProperties(std::function<void(std::shared_ptr< DevicePropProvisioningStatus >)> cb)
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));
104 void provisionCloudProperties(std::function<void(std::shared_ptr< CloudPropProvisioningStatus >)> cb)
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));
117 void discoverRemoteEnrolleeCb(std::shared_ptr<OC::OCResource> resource)
119 if(!resource->getResourceTypes().at(0).compare(PROV_RESOURCE_TYPE) && m_discoveryCb)
121 m_discoveryCb(resource);
122 m_discoveryCb = NULL;
126 void getConfigurationCallback(std::shared_ptr< GetConfigurationStatus > getConfigurationStatus)
128 if(m_getConfigurationCb != NULL)
130 m_getConfigurationCb(getConfigurationStatus);
131 m_getConfigurationCb = NULL;
135 void discoverRemoteEnrolleeCbToGetConfiguration(std::shared_ptr<OC::OCResource> resource)
137 if(!resource->getResourceTypes().at(0).compare(PROV_RESOURCE_TYPE) && m_getConfigurationCb
138 && !m_remoteEnrollee)
140 m_remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource);
142 if(m_remoteEnrollee != NULL)
144 m_remoteEnrollee->getConfiguration(std::bind(
145 &ESMediatorSimulator::getConfigurationCallback, this, std::placeholders::_1));
150 void getStatusCallback(std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus)
152 if(m_getStatusCb != NULL)
154 m_getStatusCb(getEnrolleeStatus);
155 m_getStatusCb = NULL;
159 void discoverRemoteEnrolleeCbToGetStatus(std::shared_ptr<OC::OCResource> resource)
161 if(!resource->getResourceTypes().at(0).compare(PROV_RESOURCE_TYPE) && m_getStatusCb
162 && !m_remoteEnrollee)
164 m_remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource);
166 if(m_remoteEnrollee != NULL)
168 m_remoteEnrollee->getStatus(std::bind(
169 &ESMediatorSimulator::getStatusCallback, this, std::placeholders::_1));
174 void deviceProvisioningStatusCallback(std::shared_ptr< DevicePropProvisioningStatus >
175 devicePropProvisioningStatus)
177 if(m_DevicePropProvisioningCb != NULL)
179 m_DevicePropProvisioningCb(devicePropProvisioningStatus);
180 m_DevicePropProvisioningCb = NULL;
184 void discoverRemoteEnrolleeCbToProvisionDeviceProperties(std::shared_ptr<OC::OCResource> resource)
186 if(!resource->getResourceTypes().at(0).compare(PROV_RESOURCE_TYPE) &&
187 m_DevicePropProvisioningCb && !m_remoteEnrollee)
189 m_remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource);
191 if(m_remoteEnrollee != NULL)
194 devProp.setWiFiProp("Iotivity_SSID", "Iotivity_PWD", WPA2_PSK, TKIP_AES);
195 devProp.setDevConfProp("korean", "Korea");
197 m_remoteEnrollee->provisionDeviceProperties(devProp,
198 std::bind(&ESMediatorSimulator::deviceProvisioningStatusCallback,
199 this, std::placeholders::_1));
204 void cloudProvisioningStatusCallback(std::shared_ptr< CloudPropProvisioningStatus >
205 cloudPropProvisioningStatus)
207 if(m_CloudPropProvisioningCb != NULL)
209 m_CloudPropProvisioningCb(cloudPropProvisioningStatus);
210 if(cloudPropProvisioningStatus->getESCloudState() == ES_CLOUD_PROVISIONING_SUCCESS)
211 m_CloudPropProvisioningCb = NULL;
215 void discoverRemoteEnrolleeCbToProvisionCloudProperties(std::shared_ptr<OC::OCResource> resource)
217 if(!resource->getResourceTypes().at(0).compare(PROV_RESOURCE_TYPE) &&
218 m_CloudPropProvisioningCb && !m_remoteEnrollee)
220 m_remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource);
222 if(m_remoteEnrollee != NULL)
225 cloudProp.setCloudProp("authCode", "authProvider", "ciServer");
227 m_remoteEnrollee->provisionCloudProperties(cloudProp,
228 std::bind(&ESMediatorSimulator::cloudProvisioningStatusCallback,
229 this, std::placeholders::_1));
236 #endif //_NS_CONSUMER_SIMULATOR_H_