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()
54 ~ESMediatorSimulator() = default;
56 ESMediatorSimulator(const ESMediatorSimulator &) = delete;
57 ESMediatorSimulator & operator = (const ESMediatorSimulator &) = delete;
59 ESMediatorSimulator(ESMediatorSimulator &&) = delete;
60 ESMediatorSimulator & operator = (ESMediatorSimulator &&) = delete;
62 void discoverRemoteEnrollee(std::function<void(std::shared_ptr<OC::OCResource> resource)> 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));
73 void getConfiguration(std::function<void(std::shared_ptr< GetConfigurationStatus >)> cb)
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));
84 void getStatus(std::function<void(std::shared_ptr< GetEnrolleeStatus >)> 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));
95 void provisionDeviceProperties(std::function<void(std::shared_ptr< DevicePropProvisioningStatus >)> cb)
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));
106 void provisionCloudProperties(std::function<void(std::shared_ptr< CloudPropProvisioningStatus >)> cb)
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));
119 void discoverRemoteEnrolleeCb(std::shared_ptr<OC::OCResource> resource)
121 if(!resource->getResourceTypes().at(0).compare(PROV_RESOURCE_TYPE) && m_discoveryCb)
123 m_discoveryCb(resource);
124 m_discoveryCb = NULL;
128 void getConfigurationCallback(std::shared_ptr< GetConfigurationStatus > getConfigurationStatus)
130 if(m_getConfigurationCb != NULL)
132 m_getConfigurationCb(getConfigurationStatus);
133 m_getConfigurationCb = NULL;
137 void discoverRemoteEnrolleeCbToGetConfiguration(std::shared_ptr<OC::OCResource> resource)
139 if(!resource->getResourceTypes().at(0).compare(PROV_RESOURCE_TYPE) && m_getConfigurationCb
140 && !m_remoteEnrollee)
142 m_remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource);
144 if(m_remoteEnrollee != NULL)
146 m_remoteEnrollee->getConfiguration(std::bind(
147 &ESMediatorSimulator::getConfigurationCallback, this, std::placeholders::_1));
152 void getStatusCallback(std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus)
154 if(m_getStatusCb != NULL)
156 m_getStatusCb(getEnrolleeStatus);
157 m_getStatusCb = NULL;
161 void discoverRemoteEnrolleeCbToGetStatus(std::shared_ptr<OC::OCResource> resource)
163 if(!resource->getResourceTypes().at(0).compare(PROV_RESOURCE_TYPE) && m_getStatusCb
164 && !m_remoteEnrollee)
166 m_remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource);
168 if(m_remoteEnrollee != NULL)
170 m_remoteEnrollee->getStatus(std::bind(
171 &ESMediatorSimulator::getStatusCallback, this, std::placeholders::_1));
176 void deviceProvisioningStatusCallback(std::shared_ptr< DevicePropProvisioningStatus >
177 devicePropProvisioningStatus)
179 if(m_DevicePropProvisioningCb != NULL)
181 m_DevicePropProvisioningCb(devicePropProvisioningStatus);
182 m_DevicePropProvisioningCb = NULL;
186 void discoverRemoteEnrolleeCbToProvisionDeviceProperties(std::shared_ptr<OC::OCResource> resource)
188 if(!resource->getResourceTypes().at(0).compare(PROV_RESOURCE_TYPE) &&
189 m_DevicePropProvisioningCb && !m_remoteEnrollee)
191 m_remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource);
193 if(m_remoteEnrollee != NULL)
196 devProp.setWiFiProp("Iotivity_SSID", "Iotivity_PWD", WPA2_PSK, TKIP_AES);
197 devProp.setDevConfProp("korean", "Korea");
199 m_remoteEnrollee->provisionDeviceProperties(devProp,
200 std::bind(&ESMediatorSimulator::deviceProvisioningStatusCallback,
201 this, std::placeholders::_1));
206 void cloudProvisioningStatusCallback(std::shared_ptr< CloudPropProvisioningStatus >
207 cloudPropProvisioningStatus)
209 if(m_CloudPropProvisioningCb != NULL)
211 m_CloudPropProvisioningCb(cloudPropProvisioningStatus);
212 if(cloudPropProvisioningStatus->getESCloudState() == ES_CLOUD_PROVISIONING_SUCCESS)
214 m_CloudPropProvisioningCb = NULL;
219 void discoverRemoteEnrolleeCbToProvisionCloudProperties(std::shared_ptr<OC::OCResource> resource)
221 if(!resource->getResourceTypes().at(0).compare(PROV_RESOURCE_TYPE) &&
222 m_CloudPropProvisioningCb && !m_remoteEnrollee)
224 m_remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource);
226 if(m_remoteEnrollee != NULL)
229 cloudProp.setCloudProp("authCode", "authProvider", "ciServer");
231 m_remoteEnrollee->provisionCloudProperties(cloudProp,
232 std::bind(&ESMediatorSimulator::cloudProvisioningStatusCallback,
233 this, std::placeholders::_1));
240 #endif //_NS_CONSUMER_SIMULATOR_H_