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 OIC_LOG_V(DEBUG, ES_SAMPLE_APP_TAG, "easySetupStatusCallback status is Status = %d",
78 easySetupStatus->getEasySetupState());
82 void startProvisioning()
86 remoteEnrollee->startProvisioning();
88 catch(OCException &exception)
90 std::cout << "Exception : " << exception.reason();
97 easySetupIntance = EasySetup::getInstance();
99 cout<<"\n Enter the IP address : ";
101 cout<<"\n Enter the Target Network SSID : ";
103 cout<<"\n Enter the Target Network Password : ";
105 cout<<"\n Enable Security: [Y/N] ";
108 if ( ipaddress.size() == 0 || ssid.size() == 0 || pwd.size()==0 )
110 cout<<"\n Invalid information try again !!!";
114 cout <<"\n Entered details are : \n";
115 cout<<"\n IP address : "<<ipaddress;
116 cout<<"\n Target Network SSID : "<<ssid;
117 cout<<"\n Target Network Password : "<<pwd;
119 if (security == 'Y' || security == 'y' )
121 onboardingConn.isSecured = true;
122 cout<<"\n Security is Enabled\n\n\n";
126 onboardingConn.isSecured = false;
127 cout<<"\n Security is not Enabled\n\n\n";
130 netInfo.connType = CT_ADAPTER_IP;
132 OICStrcpy(netInfo.provData.WIFI.ssid, NET_WIFI_SSID_SIZE - 1, ssid.c_str());
133 OICStrcpy(netInfo.provData.WIFI.pwd, NET_WIFI_PWD_SIZE - 1, pwd.c_str());
135 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
139 remoteEnrollee = easySetupIntance->createEnrolleeDevice(netInfo,onboardingConn);
141 catch (OCException &e)
143 std::cout << "Exception during createEnrolleeDevice call" << e.reason();
147 remoteEnrollee->registerEasySetupStatusHandler(&easySetupStatusCallback);
151 void runEasySetupMenu()
153 constexpr int EASY_SETUP_INIT = 1;
154 constexpr int START_PROVISIONING = 2;
155 constexpr int STOP_PROVISIONING = 3;
156 constexpr int STOP_EASY_SETUP = 4;
158 std::cout << "========================================================\n";
159 std::cout << EASY_SETUP_INIT << ". Easy Setup Init \n";
160 std::cout << START_PROVISIONING << ". Start Provisioning \n";
161 std::cout << STOP_PROVISIONING << ". Stop Provisioning \n";
162 std::cout << STOP_EASY_SETUP << ". Stop Easy Setup \n";
163 std::cout << STOP_EASY_SETUP + 1 << ". Quit \n";
164 std::cout << "========================================================\n";
166 int selection = processUserInput(EASY_SETUP_INIT, STOP_EASY_SETUP);
170 case EASY_SETUP_INIT:
173 case START_PROVISIONING:
176 case STOP_PROVISIONING:
177 //stopProvisioning();
179 case STOP_EASY_SETUP:
187 static FILE* client_open(const char *UNUSED_PARAM, const char *mode)
190 return fopen(JSON_DB_PATH, mode);
195 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
197 PlatformConfig config
199 OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos, &ps
202 OCPlatform::Configure(config);
205 //Initializing the provisioning client stack using the db path provided by the application.
206 OCStackResult result = OCSecure::provisionInit("");
208 if (result != OC_STACK_OK)
214 g_currentRun = runEasySetupMenu;
222 catch (const std::exception& e)
224 std::cout << "Exception caught in main " << e.what() << std::endl;
226 catch (const CloseApp&)
232 std::cout << "Stopping the client" << std::endl;