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();
72 bool cloudable = propData.isCloudable();
74 cout << "\tDeviceConfig.id : " << devConfig.id << endl;
75 cout << "\tDeviceConfig.name : " << devConfig.name << endl;
76 cout << "\tDeviceConfig.language : " << devConfig.language << endl;
77 cout << "\tDeviceConfig.country : " << devConfig.country << endl;
79 for(auto type = netInfo.types.begin(); type != netInfo.types.end(); ++type)
81 cout << "\tnetInfo.types : " << static_cast<int>(*type) << endl;
83 cout << "\tnetInfo.freq : " << static_cast<int>(netInfo.freq) << endl;
84 cout << "===========================================" << endl;
87 void RequestPropertyDataStatusCallback(std::shared_ptr< RequestPropertyDataStatus > requestPropertyDataStatus)
89 cout << "Enter RequestPropertyDataStatusCb." << endl;
91 if(requestPropertyDataStatus->getESResult() != ES_OK)
93 cout << "requestPropertyDataStatus is failed." << endl;
98 cout << "requestPropertyDataStatus is success." << endl;
99 printPropertyData(requestPropertyDataStatus->getPropertyData());
103 void dataProvisioningStatusCallback(std::shared_ptr< DataProvisioningStatus > provStatus)
105 cout << "Enter dataProvisioningStatusCallback." << endl;
107 if(provStatus->getESResult() != ES_OK)
109 cout << "dataProvisioningStatusCallback is failed." << endl;
114 cout << "dataProvisioningStatusCallback is success." << endl;
115 cout << "ESDataProvState : " << provStatus->getESDataProvState() << endl;
119 void cloudProvisioningStatusCallback(std::shared_ptr< CloudProvisioningStatus > status)
121 cout << "Enter cloudProvisioningStatusCallback." << endl;
122 cout << "CloudProvStatus : " << status->getESCloudState() << endl;
125 void createRemoteEnrollee()
127 easySetupIntance = EasySetup::getInstance();
130 remoteEnrollee = easySetupIntance->createRemoteEnrollee();
132 catch (OCException &e)
134 std::cout << "Exception during createEnrolleeDevice call" << e.reason();
137 cout << "createRemoteEnrollee is success." << endl;
140 void initRemoteEnrollee()
144 remoteEnrollee->initRemoteEnrollee();
146 catch (OCException &e)
148 std::cout << "Exception during initRemoteEnrollee call" << e.reason();
153 void requestPropertyData()
157 remoteEnrollee->requestPropertyData(RequestPropertyDataStatusCallback);
159 catch (OCException &e)
161 std::cout << "Exception during requestPropertyData call" << e.reason();
166 void setDataProvInfo()
168 DataProvInfo dataProvInfo;
169 dataProvInfo.WIFI.ssid = "Iotivity_2.4G";
170 dataProvInfo.WIFI.pwd = "1234567890";
171 dataProvInfo.WIFI.authtype = WPA2_PSK;
172 dataProvInfo.WIFI.enctype = TKIP_AES;
173 dataProvInfo.Device.language = "korean";
174 dataProvInfo.Device.country = "korea";
176 remoteEnrollee->setDataProvInfo(dataProvInfo);
179 void setCloudProvInfo()
181 CloudProvInfo cloudProvInfo;
182 cloudProvInfo.authCode = "authCode";
183 cloudProvInfo.authProvider = "authProvider";
184 cloudProvInfo.ciServer = "ciServer";
186 remoteEnrollee->setCloudProvInfo(cloudProvInfo);
189 void startDataProvisioning()
193 remoteEnrollee->startDataProvisioning(dataProvisioningStatusCallback);
195 catch (OCException &e)
197 std::cout << "Exception during startDataProvisioning call" << e.reason();
202 void startCloudProvisioning()
206 remoteEnrollee->startCloudProvisioning(cloudProvisioningStatusCallback);
208 catch (OCException &e)
210 std::cout << "Exception during startDataProvisioning call" << e.reason();
217 constexpr int CREATE_REMOTE_ENROLLEE = 1;
218 constexpr int EASY_SETUP_INIT = 2;
219 constexpr int REQUEST_PROPERTY_DATA = 3;
220 constexpr int SET_DATA_PROVISIONING_INFO = 4;
221 constexpr int START_DATA_PROVISIONING = 5;
222 constexpr int SET_CLOUD_PROVISIONING_INFO = 6;
223 constexpr int START_CLOUD_PROVISIONING = 7;
225 std::cout << "========================================================\n";
226 std::cout << CREATE_REMOTE_ENROLLEE << ". Create Remote Enrollee \n";
227 std::cout << EASY_SETUP_INIT << ". Easy Setup Init \n";
228 std::cout << REQUEST_PROPERTY_DATA << ". Request PropertyData \n";
229 std::cout << SET_DATA_PROVISIONING_INFO << ". Set Data Provisioning Info \n";
230 std::cout << START_DATA_PROVISIONING << ". Start Data Provisioning \n";
231 std::cout << SET_CLOUD_PROVISIONING_INFO << ". Set Cloud Provisioning Info \n";
232 std::cout << START_CLOUD_PROVISIONING << ". Start Cloud Provisioning \n";
233 std::cout << "========================================================\n";
235 int selection = processUserInput(CREATE_REMOTE_ENROLLEE, START_CLOUD_PROVISIONING);
239 case CREATE_REMOTE_ENROLLEE:
240 createRemoteEnrollee();
242 case EASY_SETUP_INIT:
243 initRemoteEnrollee();
245 case REQUEST_PROPERTY_DATA:
246 requestPropertyData();
248 case SET_DATA_PROVISIONING_INFO:
251 case START_DATA_PROVISIONING:
252 startDataProvisioning();
254 case SET_CLOUD_PROVISIONING_INFO:
257 case START_CLOUD_PROVISIONING:
258 startCloudProvisioning();
265 static FILE* client_open(const char *UNUSED_PARAM, const char *mode)
268 return fopen(JSON_DB_PATH, mode);
273 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
275 PlatformConfig config
277 OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos, &ps
280 OCPlatform::Configure(config);
283 //Initializing the provisioning client stack using the db path provided by the application.
284 OCStackResult result = OCSecure::provisionInit("");
286 if (result != OC_STACK_OK)
298 catch (const std::exception& e)
300 std::cout << "Exception caught in main " << e.what() << std::endl;
304 std::cout << "Stopping the client" << std::endl;