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 OIC_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";
102 netInfo.connType = CT_ADAPTER_IP;
104 OICStrcpy(netInfo.provData.WIFI.ssid, NET_WIFI_SSID_SIZE - 1, ssid.c_str());
105 OICStrcpy(netInfo.provData.WIFI.pwd, NET_WIFI_PWD_SIZE - 1, pwd.c_str());
107 onboardingConn.isSecured = false;
108 OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
112 remoteEnrollee = easySetupIntance->createEnrolleeDevice(netInfo,onboardingConn);
114 catch (OCException &e)
116 std::cout << "Exception during createEnrolleeDevice call" << e.reason();
120 remoteEnrollee->registerEasySetupStatusHandler(&easySetupStatusCallback);
122 void runEasySetupMenu()
124 constexpr int EASY_SETUP_INIT = 1;
125 constexpr int START_PROVISIONING = 2;
126 constexpr int STOP_PROVISIONING = 3;
127 constexpr int STOP_EASY_SETUP = 4;
129 std::cout << "========================================================\n";
130 std::cout << EASY_SETUP_INIT << ". Easy Setup Init \n";
131 std::cout << START_PROVISIONING << ". Start Provisioning \n";
132 std::cout << STOP_PROVISIONING << ". Stop Provisioning \n";
133 std::cout << STOP_EASY_SETUP << ". Stop Easy Setup \n";
134 std::cout << STOP_EASY_SETUP + 1 << ". Quit \n";
135 std::cout << "========================================================\n";
137 int selection = processUserInput(EASY_SETUP_INIT, STOP_EASY_SETUP);
141 case EASY_SETUP_INIT:
144 case START_PROVISIONING:
147 case STOP_PROVISIONING:
148 //stopProvisioning();
150 case STOP_EASY_SETUP:
158 static FILE* client_open(const char *UNUSED_PARAM, const char *mode)
161 return fopen(JSON_DB_PATH, mode);
166 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
168 PlatformConfig config
170 OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos, &ps
173 OCPlatform::Configure(config);
176 //Initializing the provisioning client stack using the db path provided by the application.
177 OCStackResult result = OCSecure::provisionInit("");
179 if (result != OC_STACK_OK)
185 g_currentRun = runEasySetupMenu;
193 catch (const std::exception& e)
195 std::cout << "Exception caught in main " << e.what() << std::endl;
197 catch (const CloseApp&)
203 std::cout << "Stopping the client" << std::endl;