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 "OCPlatform.h"
28 #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 ProvConfig netInfo;
41 static WiFiOnboadingConnection onboardingConn;
42 static RemoteEnrollee::shared_ptr remoteEnrollee = nullptr;
44 static std::string ipaddress, ssid, pwd;
51 typedef void (*Runner)();
55 int processUserInput(int min, int max)
65 if(input == max + 1) throw CloseApp();
66 if(min <= input && input <= max) return input;
70 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
72 throw std::runtime_error("Invalid Input, please try again");
75 void easySetupStatusCallback (std::shared_ptr< EasySetupStatus > easySetupStatus)
77 OC_UNUSED(easySetupStatus);
78 OIC_LOG_V(DEBUG, ES_SAMPLE_APP_TAG, "easySetupStatusCallback status is Status = %d",
79 easySetupStatus->getEasySetupState());
83 void startProvisioning()
87 remoteEnrollee->startProvisioning();
89 catch(OCException &exception)
91 std::cout << "Exception : " << exception.reason();
98 easySetupIntance = EasySetup::getInstance();
100 cout<<"\n Enter the IP address : ";
102 cout<<"\n Enter the Target Network SSID : ";
104 cout<<"\n Enter the Target Network Password : ";
106 cout<<"\n Enable Security: [Y/N] ";
109 if ( ipaddress.size() == 0 || ssid.size() == 0 || pwd.size()==0 )
111 cout<<"\n Invalid information try again !!!";
115 cout <<"\n Entered details are : \n";
116 cout<<"\n IP address : "<<ipaddress;
117 cout<<"\n Target Network SSID : "<<ssid;
118 cout<<"\n Target Network Password : "<<pwd;
120 if (security == 'Y' || security == 'y' )
122 onboardingConn.isSecured = true;
123 cout<<"\n Security is Enabled\n\n\n";
127 onboardingConn.isSecured = false;
128 cout<<"\n Security is not Enabled\n\n\n";
131 netInfo.connType = CT_ADAPTER_IP;
133 OICStrcpy(netInfo.provData.WIFI.ssid, NET_WIFI_SSID_SIZE - 1, ssid.c_str());
134 OICStrcpy(netInfo.provData.WIFI.pwd, NET_WIFI_PWD_SIZE - 1, pwd.c_str());
136 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
140 remoteEnrollee = easySetupIntance->createEnrolleeDevice(netInfo,onboardingConn);
142 catch (OCException &e)
144 std::cout << "Exception during createEnrolleeDevice call" << e.reason();
148 remoteEnrollee->registerEasySetupStatusHandler(&easySetupStatusCallback);
152 void runEasySetupMenu()
154 constexpr int EASY_SETUP_INIT = 1;
155 constexpr int START_PROVISIONING = 2;
156 constexpr int STOP_PROVISIONING = 3;
157 constexpr int STOP_EASY_SETUP = 4;
159 std::cout << "========================================================\n";
160 std::cout << EASY_SETUP_INIT << ". Easy Setup Init \n";
161 std::cout << START_PROVISIONING << ". Start Provisioning \n";
162 std::cout << STOP_PROVISIONING << ". Stop Provisioning \n";
163 std::cout << STOP_EASY_SETUP << ". Stop Easy Setup \n";
164 std::cout << STOP_EASY_SETUP + 1 << ". Quit \n";
165 std::cout << "========================================================\n";
167 int selection = processUserInput(EASY_SETUP_INIT, STOP_EASY_SETUP);
171 case EASY_SETUP_INIT:
174 case START_PROVISIONING:
177 case STOP_PROVISIONING:
178 //stopProvisioning();
180 case STOP_EASY_SETUP:
188 static FILE* client_open(const char *UNUSED_PARAM, const char *mode)
191 return fopen(JSON_DB_PATH, mode);
196 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
198 PlatformConfig config
200 OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos, &ps
203 OCPlatform::Configure(config);
206 //Initializing the provisioning client stack using the db path provided by the application.
207 OCStackResult result = OCSecure::provisionInit("");
209 if (result != OC_STACK_OK)
215 g_currentRun = runEasySetupMenu;
223 catch (const std::exception& e)
225 std::cout << "Exception caught in main " << e.what() << std::endl;
227 catch (const CloseApp&)
233 std::cout << "Stopping the client" << std::endl;