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"
34 #include "cainterface.h"
35 #include "OCPlatform.h"
37 using namespace OIC::Service;
39 class ESMediatorSimulator
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;
55 : m_remoteEnrollee(), m_discoveryCb(), m_getConfigurationCb(), m_getStatusCb(),
56 m_DevicePropProvisioningCb(), m_CloudPropProvisioningCb(),
57 m_getWifiCb(), m_getCloudCb(), m_getDevConfCb(), m_provPutCb()
60 ~ESMediatorSimulator() = default;
62 ESMediatorSimulator(const ESMediatorSimulator &) = delete;
63 ESMediatorSimulator & operator = (const ESMediatorSimulator &) = delete;
65 ESMediatorSimulator(ESMediatorSimulator &&) = delete;
66 ESMediatorSimulator & operator = (ESMediatorSimulator &&) = delete;
68 void discoverRemoteEnrollee(std::function<void(std::shared_ptr<OC::OCResource> resource)> 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));
79 void getConfiguration(std::function<void(std::shared_ptr< GetConfigurationStatus >)> cb)
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));
90 void getWifiRsrc(std::function<void(const OCRepresentation& rep)> 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));
100 void getCloudRsrc(std::function<void(const OCRepresentation& rep)> 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));
110 void getDevConfiguration(std::function<void(const OCRepresentation& rep)> 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));
120 void getStatus(std::function<void(std::shared_ptr< GetEnrolleeStatus >)> 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));
131 void provisionDeviceProperties(std::function<void(std::shared_ptr< DevicePropProvisioningStatus >)> cb)
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));
142 void provisionCloudProperties(std::function<void(std::shared_ptr< CloudPropProvisioningStatus >)> cb)
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));
153 void putProvRsrc(std::function<void(const OCRepresentation& rep)> 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));
165 bool isValidResourceToTest(std::shared_ptr<OC::OCResource> resource)
167 if((resource->connectivityType() & CT_ADAPTER_TCP) == CT_ADAPTER_TCP)
172 CAEndpoint_t *tempInfo = NULL;
173 uint32_t tempSize = 0;
175 CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
176 if (CA_STATUS_OK != res || NULL == tempInfo || 0 >= tempSize)
182 for (uint32_t index = 0; index < tempSize; index++)
184 if (CA_ADAPTER_IP == tempInfo[index].adapter)
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)
197 void discoverRemoteEnrolleeCb(std::shared_ptr<OC::OCResource> resource)
199 if(!isValidResourceToTest(resource))
204 if(!resource->getResourceTypes().at(0).compare(OC_RSRVD_ES_RES_TYPE_PROV) && m_discoveryCb)
206 m_discoveryCb(resource);
207 m_discoveryCb = NULL;
211 void getConfigurationCallback(std::shared_ptr< GetConfigurationStatus > getConfigurationStatus)
213 if(m_getConfigurationCb != NULL)
215 m_getConfigurationCb(getConfigurationStatus);
216 m_getConfigurationCb = NULL;
220 void discoverRemoteEnrolleeCbToGetConfiguration(std::shared_ptr<OC::OCResource> resource)
222 if(!isValidResourceToTest(resource))
227 if(!resource->getResourceTypes().at(0).compare(OC_RSRVD_ES_RES_TYPE_PROV) && m_getConfigurationCb
228 && !m_remoteEnrollee)
230 m_remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource);
232 if(m_remoteEnrollee != NULL)
234 m_remoteEnrollee->getConfiguration(std::bind(
235 &ESMediatorSimulator::getConfigurationCallback, this, std::placeholders::_1));
240 void getWifiRsrcCallback(const HeaderOptions& , const OCRepresentation& rep, const int eCode)
244 if(eCode == OC_STACK_OK)
246 std::cout << "GET request was successful" << std::endl;
247 std::cout << "Resource URI: " << rep.getUri() << std::endl;
248 if(m_getWifiCb != NULL)
256 std::cout << "onGET Response error: " << eCode << std::endl;
260 catch(std::exception& e)
262 std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
266 void discoverRemoteEnrolleeCbToGetWifiRsrc(std::shared_ptr<OC::OCResource> resource)
269 if(!isValidResourceToTest(resource))
274 if(!resource->getResourceTypes().at(0).compare(OC_RSRVD_ES_RES_TYPE_WIFI) && m_getWifiCb)
277 resource->get(test, std::bind(
278 &ESMediatorSimulator::getWifiRsrcCallback, this, std::placeholders::_1,
279 std::placeholders::_2, std::placeholders::_3));
283 void getCloudRsrcCallback(const HeaderOptions& , const OCRepresentation& rep, const int eCode)
287 if(eCode == OC_STACK_OK)
289 std::cout << "GET request was successful" << std::endl;
290 std::cout << "Resource URI: " << rep.getUri() << std::endl;
291 if(m_getCloudCb != NULL)
299 std::cout << "onGET Response error: " << eCode << std::endl;
303 catch(std::exception& e)
305 std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
310 void discoverRemoteEnrolleeCbToGetCloudRsrc(std::shared_ptr<OC::OCResource> resource)
312 if(!isValidResourceToTest(resource))
317 if(!resource->getResourceTypes().at(0).compare(OC_RSRVD_ES_RES_TYPE_CLOUDSERVER) && m_getCloudCb)
320 resource->get(test, std::bind(
321 &ESMediatorSimulator::getCloudRsrcCallback, this, std::placeholders::_1,
322 std::placeholders::_2, std::placeholders::_3));
326 void getDevConfCallback(const HeaderOptions&, const OCRepresentation& rep, const int eCode)
330 if(eCode == OC_STACK_OK)
332 std::cout << "GET request was successful" << std::endl;
333 std::cout << "Resource URI: " << rep.getUri() << std::endl;
334 if(m_getDevConfCb != NULL)
337 m_getDevConfCb = NULL;
342 std::cout << "onGET Response error: " << eCode << std::endl;
346 catch(std::exception& e)
348 std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
352 void discoverRemoteEnrolleeCbToGetDevConf(std::shared_ptr<OC::OCResource> resource)
354 if(!isValidResourceToTest(resource))
359 if(!resource->getResourceTypes().at(0).compare(OC_RSRVD_ES_RES_TYPE_DEVCONF) && m_getDevConfCb)
362 resource->get(test, std::bind(
363 &ESMediatorSimulator::getDevConfCallback, this, std::placeholders::_1,
364 std::placeholders::_2, std::placeholders::_3));
368 void getStatusCallback(std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus)
370 if(m_getStatusCb != NULL)
372 m_getStatusCb(getEnrolleeStatus);
373 m_getStatusCb = NULL;
377 void discoverRemoteEnrolleeCbToGetStatus(std::shared_ptr<OC::OCResource> resource)
379 if(!isValidResourceToTest(resource))
384 if(!resource->getResourceTypes().at(0).compare(OC_RSRVD_ES_RES_TYPE_PROV) && m_getStatusCb
385 && !m_remoteEnrollee)
387 m_remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource);
389 if(m_remoteEnrollee != NULL)
391 m_remoteEnrollee->getStatus(std::bind(
392 &ESMediatorSimulator::getStatusCallback, this, std::placeholders::_1));
397 void deviceProvisioningStatusCallback(std::shared_ptr< DevicePropProvisioningStatus >
398 devicePropProvisioningStatus)
400 if(m_DevicePropProvisioningCb != NULL)
402 m_DevicePropProvisioningCb(devicePropProvisioningStatus);
403 m_DevicePropProvisioningCb = NULL;
407 void discoverRemoteEnrolleeCbToProvisionDeviceProperties(std::shared_ptr<OC::OCResource> resource)
409 if(!isValidResourceToTest(resource))
414 if(!resource->getResourceTypes().at(0).compare(OC_RSRVD_ES_RES_TYPE_PROV) &&
415 m_DevicePropProvisioningCb && !m_remoteEnrollee)
417 m_remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource);
419 if(m_remoteEnrollee != NULL)
422 devProp.setWiFiProp("Iotivity_SSID", "Iotivity_PWD", WPA2_PSK, TKIP_AES);
423 devProp.setDevConfProp("korean", "Korea", "Location");
425 m_remoteEnrollee->provisionDeviceProperties(devProp,
426 std::bind(&ESMediatorSimulator::deviceProvisioningStatusCallback,
427 this, std::placeholders::_1));
432 void cloudProvisioningStatusCallback(std::shared_ptr< CloudPropProvisioningStatus >
433 cloudPropProvisioningStatus)
435 if(m_CloudPropProvisioningCb != NULL)
437 m_CloudPropProvisioningCb(cloudPropProvisioningStatus);
438 if(cloudPropProvisioningStatus->getESResult() == ES_OK)
440 m_CloudPropProvisioningCb = NULL;
445 void discoverRemoteEnrolleeCbToProvisionCloudProperties(std::shared_ptr<OC::OCResource> resource)
447 if(!isValidResourceToTest(resource))
452 if(!resource->getResourceTypes().at(0).compare(OC_RSRVD_ES_RES_TYPE_PROV) &&
453 m_CloudPropProvisioningCb && !m_remoteEnrollee)
455 m_remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource);
457 if(m_remoteEnrollee != NULL)
460 cloudProp.setCloudProp("authCode", "authProvider", "ciServer");
462 m_remoteEnrollee->provisionCloudProperties(cloudProp,
463 std::bind(&ESMediatorSimulator::cloudProvisioningStatusCallback,
464 this, std::placeholders::_1));
469 void putProvRsrcCallabck(const HeaderOptions&, const OCRepresentation& rep, const int )
471 cout << "putProvRsrcCallback is called" << endl;
473 if(m_provPutCb != NULL){
480 void discoverRemoteEnrolleeCbToPutProvRsrc(std::shared_ptr<OC::OCResource> resource)
482 m_remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource);
484 if(m_remoteEnrollee != NULL)
488 OCRepresentation rep;
490 resource->put(rep, test, std::bind(
491 &ESMediatorSimulator::putProvRsrcCallabck, this, std::placeholders::_1,
492 std::placeholders::_2, std::placeholders::_3));
502 #endif //_NS_CONSUMER_SIMULATOR_H_