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_client.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 << "\tProvStatus : " << conf.getProvStatus() << endl;
75 cout << "\tLastErrCode : " << conf.getLastErrCode() << endl;
76 cout << "\tDevice Name : " << conf.getDeviceName() << endl;
77 cout << "\tModel Number : " << conf.getModelNumber() << endl;
79 for(auto it : conf.getWiFiModes())
81 cout << "\tSupported WiFi modes : " << it << endl;
84 cout << "\tSupported WiFi freq : " << static_cast<int>(conf.getWiFiFreq()) << endl;
85 cout << "\tCloud accessibility: " << conf.isCloudAccessible() << endl;
86 cout << "===========================================" << endl;
89 void printStatus(const EnrolleeStatus& status)
91 cout << "===========================================" << endl;
92 cout << "\tProvStatus : " << status.getProvStatus() << endl;
93 cout << "\tLastErrCode : " << status.getLastErrCode() << endl;
94 cout << "===========================================" << endl;
97 ESOwnershipTransferData provisionSecurityStatusCallback(std::shared_ptr<SecProvisioningStatus> secProvisioningStatus)
99 cout << "provisionSecurityStatusCallback IN" << endl;
100 cout << "ESResult : " << secProvisioningStatus->getESResult() << std::endl;
101 cout << "Device ID : " << secProvisioningStatus->getDeviceUUID() << std::endl;
103 if(secProvisioningStatus->getESResult() == ES_SECURE_RESOURCE_IS_DISCOVERED)
106 cout << "Owned Status : " << secProvisioningStatus->isOwnedDevice() << std::endl;
107 if(secProvisioningStatus->isOwnedDevice())
109 cout << "Owner ID : " << secProvisioningStatus->getOwnerID() << std::endl;
111 cout << "OT Method : " << secProvisioningStatus->getSelectedOTMethod() << std::endl;
112 #ifdef MULTIPLE_OWNER
113 cout << "MOT Enabled : " << secProvisioningStatus->isMOTEnabled() << std::endl;
116 ESOwnershipTransferData OTData;
117 OTData.setMOTMethod(OIC_PRECONFIG_PIN, "12345678");
119 cout << "Enter!" << std::endl;
126 else if(secProvisioningStatus->getESResult() == ES_OK)
128 cout << "provisionSecurity is success." << std::endl;
132 cout << "provisionSecurity is failed." << endl;
138 void provisionSecurity()
142 std::cout << "RemoteEnrollee is null, retry Discovery EnrolleeResource." << endl;
148 remoteEnrollee->provisionSecurity((SecurityProvStatusCbWithOption)provisionSecurityStatusCallback);
150 catch (OCException &e)
152 std::cout << "Exception during provisionSecurity call" << e.reason();
157 void getStatusCallback(std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus)
159 if(getEnrolleeStatus->getESResult() != ES_OK)
161 cout << "getStatus is failed." << endl;
166 cout << "getStatus is success." << endl;
167 printStatus(getEnrolleeStatus->getEnrolleeStatus());
176 std::cout << "RemoteEnrollee is null, retry Discovery EnrolleeResource." << endl;
182 remoteEnrollee->getStatus(getStatusCallback);
184 catch (OCException &e)
186 std::cout << "Exception during getConfiguration call" << e.reason();
191 void getConfigurationCallback(std::shared_ptr< GetConfigurationStatus > getConfigurationStatus)
193 if(getConfigurationStatus->getESResult() != ES_OK)
195 cout << "GetConfigurationStatus is failed." << endl;
200 cout << "GetConfigurationStatus is success." << endl;
201 printConfiguration(getConfigurationStatus->getEnrolleeConf());
205 void getConfiguration()
209 std::cout << "RemoteEnrollee is null, retry Discovery EnrolleeResource." << endl;
215 remoteEnrollee->getConfiguration(getConfigurationCallback);
217 catch (OCException &e)
219 std::cout << "Exception during getConfiguration call" << e.reason();
224 void deviceProvisioningStatusCallback(std::shared_ptr< DevicePropProvisioningStatus > provStatus)
226 if(provStatus->getESResult() != ES_OK)
228 cout << "Device Provisioning is failed." << endl;
233 cout << "Device Provisioning is success." << endl;
237 void provisionDeviceProperty()
241 std::cout << "RemoteEnrollee is null, retry Discovery EnrolleeResource." << endl;
246 devProp.setWiFiProp("Iotivity_SSID", "Iotivity_PWD", WPA2_PSK, TKIP_AES);
247 devProp.setDevConfProp("korean", "Korea", "Location");
251 remoteEnrollee->provisionDeviceProperties(devProp, deviceProvisioningStatusCallback);
253 catch (OCException &e)
255 std::cout << "Exception during provisionDeviceProperties call" << e.reason();
260 void connectRequestStatusCallback(std::shared_ptr< ConnectRequestStatus > requestStatus)
262 if(requestStatus->getESResult() != ES_OK)
264 cout << "Request to connection is failed." << endl;
269 cout << "Request to connection is success." << endl;
273 void requestToConnect()
277 std::cout << "RemoteEnrollee is null, retry Discovery EnrolleeResource." << endl;
283 std::vector<ES_CONNECT_TYPE> types;
284 types.push_back(ES_CONNECT_WIFI);
285 types.push_back(ES_CONNECT_COAPCLOUD);
286 remoteEnrollee->requestToConnect(types, connectRequestStatusCallback);
288 catch (OCException &e)
290 std::cout << "Exception during provisionDeviceProperties call" << e.reason();
295 void cloudProvisioningStatusCallback(std::shared_ptr< CloudPropProvisioningStatus > provStatus)
297 switch (provStatus->getESResult())
300 cout << "Cloud Provisioning is success." << endl;
302 case ES_SECURE_RESOURCE_DISCOVERY_FAILURE:
303 cout << "Enrollee is not found in a given network." << endl;
305 case ES_ACL_PROVISIONING_FAILURE:
306 cout << "ACL provisioning is failed." << endl;
308 case ES_CERT_PROVISIONING_FAILURE:
309 cout << "CERT provisioning is failed." << endl;
312 cout << "Cloud Provisioning is failed." << endl;
317 void provisionCloudProperty()
321 std::cout << "RemoteEnrollee is null, retry Discovery EnrolleeResource." << endl;
326 cloudProp.setCloudPropWithAccessToken("accessToken", OAUTH_TOKENTYPE_BEARER, "authProvider", "ciServer");
327 cloudProp.setCloudID("f002ae8b-c42c-40d3-8b8d-1927c17bd1b3");
328 cloudProp.setCredID(1);
332 remoteEnrollee->provisionCloudProperties(cloudProp, cloudProvisioningStatusCallback);
334 catch (OCException &e)
336 std::cout << "Exception during provisionCloudProperties call" << e.reason();
341 // Callback to found resources
342 void foundResource(std::shared_ptr<OC::OCResource> resource)
344 std::string resourceURI;
345 std::string hostAddress;
348 // Do some operations with resource object.
351 resource->getResourceTypes().at(0) == OC_RSRVD_ES_RES_TYPE_EASYSETUP)
353 std::cout<<"DISCOVERED Resource:"<<std::endl;
354 // Get the resource URI
355 resourceURI = resource->uri();
356 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
358 // Get the resource host address
359 hostAddress = resource->host();
360 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
362 // Get the resource types
363 std::cout << "\tList of resource types: " << std::endl;
364 for(auto &resourceTypes : resource->getResourceTypes())
366 std::cout << "\t\t" << resourceTypes << std::endl;
369 // Get the resource interfaces
370 std::cout << "\tList of resource interfaces: " << std::endl;
371 for(auto &resourceInterfaces : resource->getResourceInterfaces())
373 std::cout << "\t\t" << resourceInterfaces << std::endl;
376 if(curResource == nullptr)
378 remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource);
381 std::cout << "RemoteEnrollee object is failed for some reasons!" << std::endl;
385 curResource = resource;
386 std::cout << "RemoteEnrollee object is successfully created!" << std::endl;
392 catch(std::exception& e)
394 std::cerr << "Exception in foundResource: "<< e.what() << std::endl;
398 void discoveryEnrolleeResource()
402 std::ostringstream requestURI;
403 requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=" << OC_RSRVD_ES_RES_TYPE_EASYSETUP;
404 OCPlatform::findResource("", requestURI.str(), CT_DEFAULT, &foundResource);
405 std::cout<< "Finding Resource... " <<std::endl;
407 std::unique_lock<std::mutex> lck(g_discoverymtx);
408 g_cond.wait_for(lck, std::chrono::seconds(5));
410 catch (OCException &e)
412 std::cout << "Exception in discoveryEnrolleeResource: "<<e.what();
418 constexpr int DISCOVERY_ENROLLEE = 1;
419 constexpr int PROVISION_SECURITY = 2;
420 constexpr int GET_STATUS = 3;
421 constexpr int GET_CONFIGURATION = 4;
422 constexpr int PROVISION_DEVICE_PROPERTY = 5;
423 constexpr int REQUEST_TO_CONNECT = 6;
424 constexpr int PROVISION_CLOUD_PROPERTY = 7;
426 std::cout << "========================================================\n";
427 std::cout << DISCOVERY_ENROLLEE << ". Discovery Enrollee Resource \n";
428 std::cout << PROVISION_SECURITY << ". Provision Security to Enrollee \n";
429 std::cout << GET_STATUS << ". Get Status from Enrollee \n";
430 std::cout << GET_CONFIGURATION << ". Get Configuration from Enrollee \n";
431 std::cout << PROVISION_DEVICE_PROPERTY << ". Provision Device Property\n";
432 std::cout << REQUEST_TO_CONNECT << ". Request to Connect \n";
433 std::cout << PROVISION_CLOUD_PROPERTY << ". Provision Cloud Property \n";
434 std::cout << "========================================================\n";
436 int selection = processUserInput(DISCOVERY_ENROLLEE, PROVISION_CLOUD_PROPERTY);
440 case DISCOVERY_ENROLLEE:
441 discoveryEnrolleeResource();
443 case PROVISION_SECURITY:
449 case GET_CONFIGURATION:
452 case PROVISION_DEVICE_PROPERTY:
453 provisionDeviceProperty();
455 case REQUEST_TO_CONNECT:
458 case PROVISION_CLOUD_PROPERTY:
459 provisionCloudProperty();
466 static FILE* client_open(const char *path, const char *mode)
468 if (0 == strcmp(path, OC_SECURITY_DB_DAT_FILE_NAME))
470 return fopen(JSON_DB_PATH, mode);
474 return fopen(path, mode);
480 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
482 PlatformConfig config
484 OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::HighQos, &ps
487 OCPlatform::Configure(config);
492 //Initializing the provisioning client stack using the db path provided by the application.
493 OCStackResult result = OCSecure::provisionInit("");
495 if (result != OC_STACK_OK)
502 std::cout << "Exception in main: " << std::endl;
513 std::cout << "Exception caught in main " << std::endl;
517 std::cout << "Stopping the client" << std::endl;