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.json"
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;
50 typedef void (*Runner)();
54 int processUserInput(int min, int max)
64 if(input == max + 1) throw CloseApp();
65 if(min <= input && input <= max) return input;
69 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
71 throw std::runtime_error("Invalid Input, please try again");
74 void easySetupStatusCallback (std::shared_ptr< EasySetupStatus > easySetupStatus)
76 OC_LOG_V(DEBUG, ES_SAMPLE_APP_TAG, "easySetupStatusCallback status is Status = %d",
77 easySetupStatus->getEasySetupState());
81 void startProvisioning()
85 remoteEnrollee->startProvisioning();
87 catch(OCException &exception)
89 std::cout << "Exception : " << exception.reason();
96 easySetupIntance = EasySetup::getInstance();
98 ipaddress = "192.168.1.104";
99 //std::cout << "Enter the target enrollee ipv4 address ";
103 //std::cout << "Enter the ssid of the target Enrolleer ";
107 //std::cout << "Enter the pwd of the target Enrolleer ";
110 netInfo.connType = CT_ADAPTER_IP;
111 //netInfo.isSecured = false;
112 //netInfo.needSecuredEasysetup = false;
114 OICStrcpy(netInfo.provData.WIFI.ssid, NET_WIFI_SSID_SIZE - 1, ssid.c_str());
115 OICStrcpy(netInfo.provData.WIFI.pwd, NET_WIFI_PWD_SIZE - 1, pwd.c_str());
117 onboardingConn.isSecured = false;
118 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
123 remoteEnrollee = easySetupIntance->createEnrolleeDevice(netInfo,onboardingConn);
125 catch (OCException &e)
127 std::cout << "Exception during createEnrolleeDevice call" << e.reason();
131 remoteEnrollee->registerEasySetupStatusHandler(&easySetupStatusCallback);
133 void runEasySetupMenu()
135 constexpr int EASY_SETUP_INIT = 1;
136 constexpr int START_PROVISIONING = 2;
137 constexpr int STOP_PROVISIONING = 3;
138 constexpr int STOP_EASY_SETUP = 4;
140 std::cout << "========================================================\n";
141 std::cout << EASY_SETUP_INIT << ". Easy Setup Init \n";
142 std::cout << START_PROVISIONING << ". Start Provisioning \n";
143 std::cout << STOP_PROVISIONING << ". Stop Provisioning \n";
144 std::cout << STOP_EASY_SETUP << ". Stop Easy Setup \n";
145 std::cout << STOP_EASY_SETUP + 1 << ". Quit \n";
146 std::cout << "========================================================\n";
148 int selection = processUserInput(EASY_SETUP_INIT, STOP_EASY_SETUP);
152 case EASY_SETUP_INIT:
155 case START_PROVISIONING:
158 case STOP_PROVISIONING:
159 //stopProvisioning();
161 case STOP_EASY_SETUP:
169 static FILE* client_open(const char *UNUSED_PARAM, const char *mode)
172 return fopen(JSON_DB_PATH, mode);
177 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
179 PlatformConfig config
181 OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos, &ps
184 OCPlatform::Configure(config);
187 //Initializing the provisioning client stack using the db path provided by the application.
188 OCStackResult result = OCSecure::provisionInit("");
190 if (result != OC_STACK_OK)
196 g_currentRun = runEasySetupMenu;
204 catch (const std::exception& e)
206 std::cout << "Exception caught in main " << e.what() << std::endl;
208 catch (const CloseApp&)
214 std::cout << "Stopping the client" << std::endl;