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"
29 #define ES_SAMPLE_APP_TAG "ES_SAMPLE_APP_TAG"
30 #define DECLARE_MENU(FUNC, ...) { #FUNC, FUNC }
32 #define JSON_DB_PATH "./oic_svr_db_client.json"
35 using namespace OIC::Service;
37 static EasySetup *easySetupIntance = nullptr;
38 static ProvConfig netInfo;
39 static WiFiOnboadingConnection onboardingConn;
40 static RemoteEnrollee::shared_ptr remoteEnrollee = nullptr;
42 static std::string ipaddress, ssid, pwd;
48 typedef void (*Runner)();
52 int processUserInput(int min, int max)
62 if(input == max + 1) throw CloseApp();
63 if(min <= input && input <= max) return input;
67 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
69 throw std::runtime_error("Invalid Input, please try again");
72 void easySetupStatusCallback (std::shared_ptr< EasySetupStatus > easySetupStatus)
74 OC_LOG_V(DEBUG, ES_SAMPLE_APP_TAG, "easySetupStatusCallback status is Status = %d",
75 easySetupStatus->getEasySetupState());
79 void startProvisioning()
83 remoteEnrollee->startProvisioning();
85 catch(OCException &exception)
87 std::cout << "Exception : " << exception.reason();
94 easySetupIntance = EasySetup::getInstance();
96 ipaddress = "192.168.1.104";
97 //std::cout << "Enter the target enrollee ipv4 address ";
101 //std::cout << "Enter the ssid of the target Enrolleer ";
105 //std::cout << "Enter the pwd of the target Enrolleer ";
108 netInfo.connType = CT_ADAPTER_IP;
109 //netInfo.isSecured = false;
110 //netInfo.needSecuredEasysetup = false;
112 OICStrcpy(netInfo.provData.WIFI.ssid, NET_WIFI_SSID_SIZE - 1, ssid.c_str());
113 OICStrcpy(netInfo.provData.WIFI.pwd, NET_WIFI_PWD_SIZE - 1, pwd.c_str());
115 onboardingConn.isSecured = false;
116 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
121 remoteEnrollee = easySetupIntance->createEnrolleeDevice(netInfo,onboardingConn);
123 catch (OCException &e)
125 std::cout << "Exception during createEnrolleeDevice call" << e.reason();
129 remoteEnrollee->registerEasySetupStatusHandler(&easySetupStatusCallback);
131 void runEasySetupMenu()
133 constexpr int EASY_SETUP_INIT = 1;
134 constexpr int START_PROVISIONING = 2;
135 constexpr int STOP_PROVISIONING = 3;
136 constexpr int STOP_EASY_SETUP = 4;
138 std::cout << "========================================================\n";
139 std::cout << EASY_SETUP_INIT << ". Easy Setup Init \n";
140 std::cout << START_PROVISIONING << ". Start Provisioning \n";
141 std::cout << STOP_PROVISIONING << ". Stop Provisioning \n";
142 std::cout << STOP_EASY_SETUP << ". Stop Easy Setup \n";
143 std::cout << STOP_EASY_SETUP + 1 << ". Quit \n";
144 std::cout << "========================================================\n";
146 int selection = processUserInput(EASY_SETUP_INIT, STOP_EASY_SETUP);
150 case EASY_SETUP_INIT:
153 case START_PROVISIONING:
156 case STOP_PROVISIONING:
157 //stopProvisioning();
159 case STOP_EASY_SETUP:
167 static FILE* client_open(const char *UNUSED_PARAM, const char *mode)
170 return fopen(JSON_DB_PATH, mode);
175 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
177 PlatformConfig config
179 OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos, &ps
181 OCPlatform::Configure(config);
183 g_currentRun = runEasySetupMenu;
191 catch (const std::exception& e)
193 std::cout << "Exception caught in main " << e.what() << std::endl;
195 catch (const CloseApp&)
201 std::cout << "Stopping the client" << std::endl;