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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 #include "oic_string.h"
25 #include "EasySetup.h"
26 #include "ESRichCommon.h"
27 #include "OCPlatform.h"
29 #include "OCProvisioningManager.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 EasySetup *easySetupIntance = nullptr;
40 static std::shared_ptr<RemoteEnrollee> remoteEnrollee = nullptr;
42 static std::string ipaddress, ssid, pwd;
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 printPropertyData(PropertyData propData)
69 cout << "===========================================" << endl;
70 DeviceConfig devConfig = propData.getDevConf();
71 NetworkInfo netInfo = propData.getNetInfo();
73 cout << "\tDeviceConfig.id : " << devConfig.id << endl;
74 cout << "\tDeviceConfig.name : " << devConfig.name << endl;
75 cout << "\tDeviceConfig.language : " << devConfig.language << endl;
76 cout << "\tDeviceConfig.country : " << devConfig.country << endl;
78 for(auto type = netInfo.types.begin(); type != netInfo.types.end(); ++type)
80 cout << "\tnetInfo.types : " << static_cast<int>(*type) << endl;
82 cout << "\tnetInfo.freq : " << static_cast<int>(netInfo.freq) << endl;
83 cout << "===========================================" << endl;
86 void RequestPropertyDataStatusCallback(std::shared_ptr< RequestPropertyDataStatus > requestPropertyDataStatus)
88 cout << "Enter RequestPropertyDataStatusCb." << endl;
90 if(requestPropertyDataStatus->getESResult() != ES_OK)
92 cout << "requestPropertyDataStatus is failed." << endl;
97 cout << "requestPropertyDataStatus is success." << endl;
98 printPropertyData(requestPropertyDataStatus->getPropertyData());
102 void dataProvisioningStatusCallback(std::shared_ptr< DataProvisioningStatus > provStatus)
104 cout << "Enter dataProvisioningStatusCallback." << endl;
106 if(provStatus->getESResult() != ES_OK)
108 cout << "dataProvisioningStatusCallback is failed." << endl;
113 cout << "dataProvisioningStatusCallback is success." << endl;
114 cout << "ESDataProvState : " << provStatus->getESDataProvState() << endl;
118 void cloudProvisioningStatusCallback(std::shared_ptr< CloudProvisioningStatus > status)
120 cout << "Enter cloudProvisioningStatusCallback." << endl;
121 cout << "CloudProvStatus : " << status->getESCloudState() << endl;
124 void createRemoteEnrollee()
126 easySetupIntance = EasySetup::getInstance();
129 remoteEnrollee = easySetupIntance->createRemoteEnrollee();
131 catch (OCException &e)
133 std::cout << "Exception during createEnrolleeDevice call" << e.reason();
136 cout << "createRemoteEnrollee is success." << endl;
139 void initRemoteEnrollee()
143 remoteEnrollee->initRemoteEnrollee();
145 catch (OCException &e)
147 std::cout << "Exception during initRemoteEnrollee call" << e.reason();
152 void requestPropertyData()
156 remoteEnrollee->requestPropertyData(RequestPropertyDataStatusCallback);
158 catch (OCException &e)
160 std::cout << "Exception during requestPropertyData call" << e.reason();
165 void setDataProvInfo()
167 DataProvInfo dataProvInfo;
168 dataProvInfo.WIFI.ssid = "Iotivity_2.4G";
169 dataProvInfo.WIFI.pwd = "1234567890";
170 dataProvInfo.WIFI.authtype = WPA2_PSK;
171 dataProvInfo.WIFI.enctype = TKIP_AES;
172 dataProvInfo.Device.language = "korean";
173 dataProvInfo.Device.country = "korea";
175 remoteEnrollee->setDataProvInfo(dataProvInfo);
178 void setCloudProvInfo()
180 CloudProvInfo cloudProvInfo;
181 cloudProvInfo.authCode = "authCode";
182 cloudProvInfo.authProvider = "authProvider";
183 cloudProvInfo.ciServer = "ciServer";
185 remoteEnrollee->setCloudProvInfo(cloudProvInfo);
188 void startDataProvisioning()
192 remoteEnrollee->startDataProvisioning(dataProvisioningStatusCallback);
194 catch (OCException &e)
196 std::cout << "Exception during startDataProvisioning call" << e.reason();
201 void startCloudProvisioning()
205 remoteEnrollee->startCloudProvisioning(cloudProvisioningStatusCallback);
207 catch (OCException &e)
209 std::cout << "Exception during startDataProvisioning call" << e.reason();
216 constexpr int CREATE_REMOTE_ENROLLEE = 1;
217 constexpr int EASY_SETUP_INIT = 2;
218 constexpr int REQUEST_PROPERTY_DATA = 3;
219 constexpr int SET_DATA_PROVISIONING_INFO = 4;
220 constexpr int START_DATA_PROVISIONING = 5;
221 constexpr int SET_CLOUD_PROVISIONING_INFO = 6;
222 constexpr int START_CLOUD_PROVISIONING = 7;
224 std::cout << "========================================================\n";
225 std::cout << CREATE_REMOTE_ENROLLEE << ". Create Remote Enrollee \n";
226 std::cout << EASY_SETUP_INIT << ". Easy Setup Init \n";
227 std::cout << REQUEST_PROPERTY_DATA << ". Request PropertyData \n";
228 std::cout << SET_DATA_PROVISIONING_INFO << ". Set Data Provisioning Info \n";
229 std::cout << START_DATA_PROVISIONING << ". Start Data Provisioning \n";
230 std::cout << SET_CLOUD_PROVISIONING_INFO << ". Set Cloud Provisioning Info \n";
231 std::cout << START_CLOUD_PROVISIONING << ". Start Cloud Provisioning \n";
232 std::cout << "========================================================\n";
234 int selection = processUserInput(CREATE_REMOTE_ENROLLEE, START_CLOUD_PROVISIONING);
238 case CREATE_REMOTE_ENROLLEE:
239 createRemoteEnrollee();
241 case EASY_SETUP_INIT:
242 initRemoteEnrollee();
244 case REQUEST_PROPERTY_DATA:
245 requestPropertyData();
247 case SET_DATA_PROVISIONING_INFO:
250 case START_DATA_PROVISIONING:
251 startDataProvisioning();
253 case SET_CLOUD_PROVISIONING_INFO:
256 case START_CLOUD_PROVISIONING:
257 startCloudProvisioning();
264 static FILE* client_open(const char *UNUSED_PARAM, const char *mode)
267 return fopen(JSON_DB_PATH, mode);
272 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
274 PlatformConfig config
276 OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos, &ps
279 OCPlatform::Configure(config);
282 //Initializing the provisioning client stack using the db path provided by the application.
283 OCStackResult result = OCSecure::provisionInit("");
285 if (result != OC_STACK_OK)
297 catch (const std::exception& e)
299 std::cout << "Exception caught in main " << e.what() << std::endl;
303 std::cout << "Stopping the client" << std::endl;