1 //******************************************************************
3 // Copyright 2015 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 #include <condition_variable>
24 #include "OCPlatform.h"
26 #include "OCProvisioningManager.hpp"
27 #include "securevirtualresourcetypes.h"
29 #include "EasySetup.hpp"
30 #include "ESRichCommon.h"
32 #define ES_SAMPLE_APP_TAG "ES_SAMPLE_APP_TAG"
33 #define DECLARE_MENU(FUNC, ...) { #FUNC, FUNC }
35 #define JSON_DB_PATH "./oic_svr_db_subclient.dat"
38 using namespace OIC::Service;
40 static std::shared_ptr<RemoteEnrollee> remoteEnrollee = nullptr;
41 static std::shared_ptr<OC::OCResource> curResource = nullptr;
43 static std::mutex g_discoverymtx;
44 static std::condition_variable g_cond;
46 typedef void (*Runner)();
50 int processUserInput(int min = std::numeric_limits<int>::min(),
51 int max = std::numeric_limits<int>::max())
58 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
60 if (!std::cin.fail() && min <= input && input <= max)
66 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
68 throw std::runtime_error("Invalid Input, please try again");
71 void printConfiguration(const EnrolleeConf& conf)
73 cout << "===========================================" << endl;
74 cout << "\tDevice Name : " << conf.getDeviceName() << endl;
76 for(auto it : conf.getWiFiModes())
78 cout << "\tSupported WiFi modes : " << it << endl;
81 cout << "\tSupported WiFi freq : " << static_cast<int>(conf.getWiFiFreq()) << endl;
82 cout << "\tCloud accessibility: " << conf.isCloudAccessible() << endl;
83 cout << "===========================================" << endl;
86 void printStatus(const EnrolleeStatus& status)
88 cout << "===========================================" << endl;
89 cout << "\tProvStatus : " << status.getProvStatus() << endl;
90 cout << "\tLastErrCode : " << status.getLastErrCode() << endl;
91 cout << "===========================================" << endl;
94 ESOwnershipTransferData provisionSecurityStatusCallback(std::shared_ptr<SecProvisioningStatus> secProvisioningStatus)
96 cout << "provisionSecurityStatusCallback IN" << endl;
97 cout << "ESResult : " << secProvisioningStatus->getESResult() << std::endl;
98 cout << "Device ID : " << secProvisioningStatus->getDeviceUUID() << std::endl;
100 if(secProvisioningStatus->getESResult() == ES_SECURE_RESOURCE_IS_DISCOVERED)
103 cout << "Owned Status : " << secProvisioningStatus->isOwnedDevice() << std::endl;
104 cout << "OT Method : " << secProvisioningStatus->getSelectedOTMethod() << std::endl;
105 #ifdef MULTIPLE_OWNER
106 cout << "MOT Enabled : " << secProvisioningStatus->isMOTEnabled() << std::endl;
109 ESOwnershipTransferData OTData;
110 OTData.setMOTMethod(OIC_PRECONFIG_PIN, "12345678");
112 cout << "Enter!" << std::endl;
120 else if(secProvisioningStatus->getESResult() == ES_OK)
122 cout << "provisionSecurity is success." << std::endl;
126 cout << "provisionSecurity is failed." << endl;
132 void provisionSecurity()
136 std::cout << "RemoteEnrollee is null, retry Discovery EnrolleeResource." << endl;
142 remoteEnrollee->provisionSecurity((SecurityProvStatusCbWithOption)provisionSecurityStatusCallback);
144 catch (OCException &e)
146 std::cout << "Exception during provisionSecurity call" << e.reason();
151 void getStatusCallback(std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus)
153 if(getEnrolleeStatus->getESResult() != ES_OK)
155 cout << "getStatus is failed." << endl;
160 cout << "getStatus is success." << endl;
161 printStatus(getEnrolleeStatus->getEnrolleeStatus());
170 std::cout << "RemoteEnrollee is null, retry Discovery EnrolleeResource." << endl;
176 remoteEnrollee->getStatus(getStatusCallback);
178 catch (OCException &e)
180 std::cout << "Exception during getConfiguration call" << e.reason();
185 void getConfigurationCallback(std::shared_ptr< GetConfigurationStatus > getConfigurationStatus)
187 if(getConfigurationStatus->getESResult() != ES_OK)
189 cout << "GetConfigurationStatus is failed." << endl;
194 cout << "GetConfigurationStatus is success." << endl;
195 printConfiguration(getConfigurationStatus->getEnrolleeConf());
199 void getConfiguration()
203 std::cout << "RemoteEnrollee is null, retry Discovery EnrolleeResource." << endl;
209 remoteEnrollee->getConfiguration(getConfigurationCallback);
211 catch (OCException &e)
213 std::cout << "Exception during getConfiguration call" << e.reason();
218 void deviceProvisioningStatusCallback(std::shared_ptr< DevicePropProvisioningStatus > provStatus)
220 if(provStatus->getESResult() != ES_OK)
222 cout << "Device Provisioning is failed." << endl;
227 cout << "Device Provisioning is success." << endl;
231 void provisionDeviceProperty()
235 std::cout << "RemoteEnrollee is null, retry Discovery EnrolleeResource." << endl;
240 devProp.setWiFiProp("Iotivity_SSID", "Iotivity_PWD", WPA2_PSK, TKIP_AES);
244 remoteEnrollee->provisionDeviceProperties(devProp, deviceProvisioningStatusCallback);
246 catch (OCException &e)
248 std::cout << "Exception during provisionDeviceProperties call" << e.reason();
253 void connectRequestStatusCallback(std::shared_ptr< ConnectRequestStatus > requestStatus)
255 if(requestStatus->getESResult() != ES_OK)
257 cout << "Request to connection is failed." << endl;
262 cout << "Request to connection is success." << endl;
266 void requestToConnect()
270 std::cout << "RemoteEnrollee is null, retry Discovery EnrolleeResource." << endl;
276 std::vector<ES_CONNECT_TYPE> types;
277 types.push_back(ES_CONNECT_WIFI);
278 types.push_back(ES_CONNECT_COAPCLOUD);
279 remoteEnrollee->requestToConnect(types, connectRequestStatusCallback);
281 catch (OCException &e)
283 std::cout << "Exception during provisionDeviceProperties call" << e.reason();
288 void cloudProvisioningStatusCallback(std::shared_ptr< CloudPropProvisioningStatus > provStatus)
290 switch (provStatus->getESResult())
293 cout << "Cloud Provisioning is success." << endl;
295 case ES_SECURE_RESOURCE_DISCOVERY_FAILURE:
296 cout << "Enrollee is not found in a given network." << endl;
298 case ES_ACL_PROVISIONING_FAILURE:
299 cout << "ACL provisioning is failed." << endl;
301 case ES_CERT_PROVISIONING_FAILURE:
302 cout << "CERT provisioning is failed." << endl;
305 cout << "Cloud Provisioning is failed." << endl;
310 void provisionCloudProperty()
314 std::cout << "RemoteEnrollee is null, retry Discovery EnrolleeResource." << endl;
319 cloudProp.setCloudProp("authCode", "authProvider", "ciServer");
320 cloudProp.setCloudID("f002ae8b-c42c-40d3-8b8d-1927c17bd1b3");
321 cloudProp.setCredID(1);
325 remoteEnrollee->provisionCloudProperties(cloudProp, cloudProvisioningStatusCallback);
327 catch (OCException &e)
329 std::cout << "Exception during provisionCloudProperties call" << e.reason();
334 // Callback to found resources
335 void foundResource(std::shared_ptr<OC::OCResource> resource)
337 std::string resourceURI;
338 std::string hostAddress;
341 // Do some operations with resource object.
344 resource->getResourceTypes().at(0) == OC_RSRVD_ES_RES_TYPE_EASYSETUP)
346 std::cout<<"DISCOVERED Resource:"<<std::endl;
347 // Get the resource URI
348 resourceURI = resource->uri();
349 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
351 // Get the resource host address
352 hostAddress = resource->host();
353 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
355 // Get the resource types
356 std::cout << "\tList of resource types: " << std::endl;
357 for(auto &resourceTypes : resource->getResourceTypes())
359 std::cout << "\t\t" << resourceTypes << std::endl;
362 // Get the resource interfaces
363 std::cout << "\tList of resource interfaces: " << std::endl;
364 for(auto &resourceInterfaces : resource->getResourceInterfaces())
366 std::cout << "\t\t" << resourceInterfaces << std::endl;
369 if(curResource == nullptr)
371 remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource);
374 std::cout << "RemoteEnrollee object is failed for some reasons!" << std::endl;
378 curResource = resource;
379 std::cout << "RemoteEnrollee object is successfully created!" << std::endl;
385 catch(std::exception& e)
387 std::cerr << "Exception in foundResource: "<< e.what() << std::endl;
391 void discoveryEnrolleeResource()
395 std::ostringstream requestURI;
396 requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=" << OC_RSRVD_ES_RES_TYPE_EASYSETUP;
397 OCPlatform::findResource("", requestURI.str(), CT_DEFAULT, &foundResource);
398 std::cout<< "Finding Resource... " <<std::endl;
400 std::unique_lock<std::mutex> lck(g_discoverymtx);
401 g_cond.wait_for(lck, std::chrono::seconds(5));
403 catch (OCException& e)
405 std::cout << "Exception in discoveryEnrolleeResource: "<<e.what();
411 constexpr int DISCOVERY_ENROLLEE = 1;
412 constexpr int PROVISION_SECURITY = 2;
413 constexpr int GET_STATUS = 3;
414 constexpr int GET_CONFIGURATION = 4;
415 constexpr int PROVISION_DEVICE_PROPERTY = 5;
416 constexpr int REQUEST_TO_CONNECT = 6;
417 constexpr int PROVISION_CLOUD_PROPERTY = 7;
419 std::cout << "========================================================\n";
420 std::cout << DISCOVERY_ENROLLEE << ". Discovery Enrollee Resource \n";
421 std::cout << PROVISION_SECURITY << ". Provision Security to Enrollee \n";
422 std::cout << GET_STATUS << ". Get Status from Enrollee \n";
423 std::cout << GET_CONFIGURATION << ". Get Configuration from Enrollee \n";
424 std::cout << PROVISION_DEVICE_PROPERTY << ". Provision Device Property\n";
425 std::cout << REQUEST_TO_CONNECT << ". Request to Connect \n";
426 std::cout << PROVISION_CLOUD_PROPERTY << ". Provision Cloud Property \n";
427 std::cout << "========================================================\n";
429 int selection = processUserInput(DISCOVERY_ENROLLEE, PROVISION_CLOUD_PROPERTY);
433 case DISCOVERY_ENROLLEE:
434 discoveryEnrolleeResource();
436 case PROVISION_SECURITY:
442 case GET_CONFIGURATION:
445 case PROVISION_DEVICE_PROPERTY:
446 provisionDeviceProperty();
448 case REQUEST_TO_CONNECT:
451 case PROVISION_CLOUD_PROPERTY:
452 provisionCloudProperty();
459 static FILE* client_open(const char *UNUSED_PARAM, const char *mode)
462 return fopen(JSON_DB_PATH, mode);
467 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
469 PlatformConfig config
471 OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::HighQos, &ps
474 OCPlatform::Configure(config);
479 //Initializing the provisioning client stack using the db path provided by the application.
480 OCStackResult result = OCSecure::provisionInit("PDM_sub.db");
482 if (result != OC_STACK_OK)
489 std::cout << "Exception in main: " << std::endl;
500 std::cout << "Exception caught in main " << std::endl;
504 std::cout << "Stopping the client" << std::endl;