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.h"
28 #include "EasySetup.h"
29 #include "ESRichCommon.h"
31 #define ES_SAMPLE_APP_TAG "ES_SAMPLE_APP_TAG"
32 #define DECLARE_MENU(FUNC, ...) { #FUNC, FUNC }
34 #define JSON_DB_PATH "./oic_svr_db_client.dat"
37 using namespace OIC::Service;
39 static std::shared_ptr<RemoteEnrollee> remoteEnrollee = nullptr;
40 static std::shared_ptr<OC::OCResource> curResource = nullptr;
42 static std::mutex g_discoverymtx;
43 static std::condition_variable g_cond;
45 typedef void (*Runner)();
49 int processUserInput(int min = std::numeric_limits<int>::min(),
50 int max = std::numeric_limits<int>::max())
57 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
59 if (!std::cin.fail() && min <= input && input <= max) return input;
62 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
64 throw std::runtime_error("Invalid Input, please try again");
67 void printConfiguration(EnrolleeConf conf)
69 cout << "===========================================" << endl;
70 cout << "\tDevice Name : " << conf.getDeviceName() << endl;
71 cout << "\tModel Number : " << conf.getModelNumber() << endl;
73 for(auto it : conf.getWiFiModes())
75 cout << "\tSupported WiFi modes : " << it << endl;
78 cout << "\tSupported WiFi freq : " << static_cast<int>(conf.getWiFiFreq()) << endl;
79 cout << "\tCloud accessibility: " << conf.isCloudAccessible() << endl;
80 cout << "===========================================" << endl;
83 void printStatus(EnrolleeStatus status)
85 cout << "===========================================" << endl;
86 cout << "\tProvStatus : " << status.getProvStatus() << endl;
87 cout << "\tLastErrCode : " << status.getLastErrCode() << endl;
88 cout << "===========================================" << endl;
91 void provisionSecurityStatusCallback(std::shared_ptr<SecProvisioningStatus> secProvisioningStatus)
93 if(secProvisioningStatus->getESResult() != ES_OK)
95 cout << "provisionSecurity is failed." << endl;
100 cout << "provisionSecurity is success." << endl;
101 cout << "uuid : " << secProvisioningStatus->getDeviceUUID()<< endl;
105 void provisionSecurity()
109 std::cout << "RemoteEnrollee is null, retry Discovery EnrolleeResource." << endl;
115 remoteEnrollee->provisionSecurity(provisionSecurityStatusCallback);
117 catch (OCException &e)
119 std::cout << "Exception during provisionSecurity call" << e.reason();
124 void getStatusCallback(std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus)
126 if(getEnrolleeStatus->getESResult() != ES_OK)
128 cout << "getStatus is failed." << endl;
133 cout << "getStatus is success." << endl;
134 printStatus(getEnrolleeStatus->getEnrolleeStatus());
143 std::cout << "RemoteEnrollee is null, retry Discovery EnrolleeResource." << endl;
149 remoteEnrollee->getStatus(getStatusCallback);
151 catch (OCException &e)
153 std::cout << "Exception during getConfiguration call" << e.reason();
158 void getConfigurationCallback(std::shared_ptr< GetConfigurationStatus > getConfigurationStatus)
160 if(getConfigurationStatus->getESResult() != ES_OK)
162 cout << "GetConfigurationStatus is failed." << endl;
167 cout << "GetConfigurationStatus is success." << endl;
168 printConfiguration(getConfigurationStatus->getEnrolleeConf());
172 void getConfiguration()
176 std::cout << "RemoteEnrollee is null, retry Discovery EnrolleeResource." << endl;
182 remoteEnrollee->getConfiguration(getConfigurationCallback);
184 catch (OCException &e)
186 std::cout << "Exception during getConfiguration call" << e.reason();
191 void deviceProvisioningStatusCallback(std::shared_ptr< DevicePropProvisioningStatus > provStatus)
193 if(provStatus->getESResult() != ES_OK)
195 cout << "Device Provisioning is failed." << endl;
200 cout << "Device Provisioning is success." << endl;
204 void provisionDeviceProperty()
208 std::cout << "RemoteEnrollee is null, retry Discovery EnrolleeResource." << endl;
213 devProp.setWiFiProp("Iotivity_SSID", "Iotivity_PWD", WPA2_PSK, TKIP_AES);
214 devProp.setDevConfProp("korean", "Korea", "Location");
218 remoteEnrollee->provisionDeviceProperties(devProp, deviceProvisioningStatusCallback);
220 catch (OCException &e)
222 std::cout << "Exception during provisionDeviceProperties call" << e.reason();
227 void cloudProvisioningStatusCallback(std::shared_ptr< CloudPropProvisioningStatus > provStatus)
229 switch (provStatus->getESResult())
232 cout << "Cloud Provisioning is success." << endl;
234 case ES_FOUND_ENROLLEE:
235 cout << "Enrollee is found in a given network." << endl;
237 case ES_NOT_FOUND_ENROLLEE:
238 cout << "Enrollee is not found in a given network." << endl;
241 cout << "Cloud Provisioning is failed." << endl;
246 void provisionCloudProperty()
250 std::cout << "RemoteEnrollee is null, retry Discovery EnrolleeResource." << endl;
255 cloudProp.setCloudProp("authCode", "authProvider", "ciServer");
256 cloudProp.setCloudID("f002ae8b-c42c-40d3-8b8d-1927c17bd1b3");
257 cloudProp.setCredID(1);
261 remoteEnrollee->provisionCloudProperties(cloudProp, cloudProvisioningStatusCallback);
263 catch (OCException &e)
265 std::cout << "Exception during provisionCloudProperties call" << e.reason();
270 // Callback to found resources
271 void foundResource(std::shared_ptr<OC::OCResource> resource)
273 std::string resourceURI;
274 std::string hostAddress;
277 // Do some operations with resource object.
280 resource->getResourceTypes().at(0) == OC_RSRVD_ES_RES_TYPE_PROV)
282 std::cout<<"DISCOVERED Resource:"<<std::endl;
283 // Get the resource URI
284 resourceURI = resource->uri();
285 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
287 // Get the resource host address
288 hostAddress = resource->host();
289 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
291 // Get the resource types
292 std::cout << "\tList of resource types: " << std::endl;
293 for(auto &resourceTypes : resource->getResourceTypes())
295 std::cout << "\t\t" << resourceTypes << std::endl;
298 // Get the resource interfaces
299 std::cout << "\tList of resource interfaces: " << std::endl;
300 for(auto &resourceInterfaces : resource->getResourceInterfaces())
302 std::cout << "\t\t" << resourceInterfaces << std::endl;
305 if(curResource == nullptr)
307 remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource);
310 std::cout << "RemoteEnrollee object is failed for some reasons!" << std::endl;
314 curResource = resource;
315 std::cout << "RemoteEnrollee object is successfully created!" << std::endl;
321 catch(std::exception& e)
323 std::cerr << "Exception in foundResource: "<< e.what() << std::endl;
327 void discoveryEnrolleeResource()
331 std::ostringstream requestURI;
332 requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=" << OC_RSRVD_ES_RES_TYPE_PROV;
333 OCPlatform::findResource("", requestURI.str(), CT_DEFAULT, &foundResource);
334 std::cout<< "Finding Resource... " <<std::endl;
336 std::unique_lock<std::mutex> lck(g_discoverymtx);
337 g_cond.wait_for(lck, std::chrono::seconds(5));
339 catch (OCException& e)
341 std::cout << "Exception in discoveryEnrolleeResource: "<<e.what();
347 constexpr int DISCOVERY_ENROLLEE = 1;
348 constexpr int PROVISION_SECURITY = 2;
349 constexpr int GET_STATUS = 3;
350 constexpr int GET_CONFIGURATION = 4;
351 constexpr int PROVISION_DEVICE_PROPERTY = 5;
352 constexpr int PROVISION_CLOUD_PROPERTY = 6;
354 std::cout << "========================================================\n";
355 std::cout << DISCOVERY_ENROLLEE << ". Discovery Enrollee Resource \n";
356 std::cout << PROVISION_SECURITY << ". Provision Security to Enrollee \n";
357 std::cout << GET_STATUS << ". Get Status from Enrollee \n";
358 std::cout << GET_CONFIGURATION << ". Get Configuration from Enrollee \n";
359 std::cout << PROVISION_DEVICE_PROPERTY << ". Provision Device Property\n";
360 std::cout << PROVISION_CLOUD_PROPERTY << ". Provision Cloud Property \n";
361 std::cout << "========================================================\n";
363 int selection = processUserInput(DISCOVERY_ENROLLEE, PROVISION_CLOUD_PROPERTY);
367 case DISCOVERY_ENROLLEE:
368 discoveryEnrolleeResource();
370 case PROVISION_SECURITY:
376 case GET_CONFIGURATION:
379 case PROVISION_DEVICE_PROPERTY:
380 provisionDeviceProperty();
382 case PROVISION_CLOUD_PROPERTY:
383 provisionCloudProperty();
390 static FILE* client_open(const char *UNUSED_PARAM, const char *mode)
393 return fopen(JSON_DB_PATH, mode);
398 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
400 PlatformConfig config
402 OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos, &ps
405 OCPlatform::Configure(config);
410 //Initializing the provisioning client stack using the db path provided by the application.
411 OCStackResult result = OCSecure::provisionInit("");
413 if (result != OC_STACK_OK)
418 }catch(OCException& e)
420 std::cout << "Exception in main: "<<e.what();
429 catch (const std::exception& e)
431 std::cout << "Exception caught in main " << e.what() << std::endl;
435 std::cout << "Stopping the client" << std::endl;