Merge branch '1.1-rel'
[platform/upstream/iotivity.git] / service / easy-setup / sampleapp / mediator / linux / richsdk_sample / mediator_cpp.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include <iostream>
22 #include<stdio.h>
23
24 #include "oic_string.h"
25 #include "EasySetup.h"
26 #include "OCPlatform.h"
27 #include "logger.h"
28 #include "OCProvisioningManager.h"
29
30
31 #define ES_SAMPLE_APP_TAG "ES_SAMPLE_APP_TAG"
32 #define DECLARE_MENU(FUNC, ...) { #FUNC, FUNC }
33
34 #define JSON_DB_PATH "./oic_svr_db_client.dat"
35
36 using namespace OC;
37 using namespace OIC::Service;
38
39 static EasySetup *easySetupIntance = nullptr;
40 static ProvConfig netInfo;
41 static WiFiOnboadingConnection onboardingConn;
42 static RemoteEnrollee::shared_ptr remoteEnrollee = nullptr;
43
44 static std::string ipaddress, ssid, pwd;
45 char security;
46
47 struct CloseApp
48 {
49 };
50
51 typedef void (*Runner)();
52
53 Runner g_currentRun;
54
55 int processUserInput(int min, int max)
56 {
57     assert(min <= max);
58
59     int input;
60
61     std::cin >> input;
62
63     if (!std::cin.fail())
64     {
65         if(input == max + 1) throw CloseApp();
66         if(min <= input && input <= max) return input;
67     }
68
69     std::cin.clear();
70     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
71
72     throw std::runtime_error("Invalid Input, please try again");
73 }
74
75 void easySetupStatusCallback (std::shared_ptr< EasySetupStatus > easySetupStatus)
76 {
77     OIC_LOG_V(DEBUG, ES_SAMPLE_APP_TAG, "easySetupStatusCallback status is Status = %d",
78             easySetupStatus->getEasySetupState());
79
80 }
81
82 void startProvisioning()
83 {
84     try
85     {
86         remoteEnrollee->startProvisioning();
87     }
88     catch(OCException &exception)
89     {
90         std::cout << "Exception : " << exception.reason();
91     }
92 }
93
94 void initEasySetup()
95 {
96
97     easySetupIntance = EasySetup::getInstance();
98
99     cout<<"\n Enter the IP address : ";
100     cin>>ipaddress;
101     cout<<"\n Enter the Target Network SSID : ";
102     cin>>ssid;
103     cout<<"\n Enter the Target Network Password : ";
104     cin>>pwd;
105     cout<<"\n Enable Security: [Y/N] ";
106     cin>>security;
107
108     if ( ipaddress.size() == 0 || ssid.size() == 0  || pwd.size()==0 )
109     {
110          cout<<"\n Invalid information try again !!!";
111     }
112     else
113      {
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;
118
119           if (security == 'Y' || security == 'y' )
120           {
121                   onboardingConn.isSecured = true;
122                   cout<<"\n Security is Enabled\n\n\n";
123            }
124            else
125            {
126                  onboardingConn.isSecured = false;
127                  cout<<"\n Security is not Enabled\n\n\n";
128            }
129
130           netInfo.connType = CT_ADAPTER_IP;
131
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());
134
135          OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
136
137          try
138          {
139              remoteEnrollee = easySetupIntance->createEnrolleeDevice(netInfo,onboardingConn);
140          }
141          catch (OCException &e)
142          {
143              std::cout << "Exception during createEnrolleeDevice call" << e.reason();
144              return;
145          }
146
147          remoteEnrollee->registerEasySetupStatusHandler(&easySetupStatusCallback);
148     }
149
150 }
151 void runEasySetupMenu()
152 {
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;
157
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";
165
166     int selection = processUserInput(EASY_SETUP_INIT, STOP_EASY_SETUP);
167
168     switch (selection)
169     {
170         case EASY_SETUP_INIT:
171             initEasySetup();
172             break;
173         case START_PROVISIONING:
174             startProvisioning();
175             break;
176         case STOP_PROVISIONING:
177             //stopProvisioning();
178             break;
179         case STOP_EASY_SETUP:
180             //stopEasySetup();
181             break;
182         default:
183             break;
184     };
185 }
186
187 static FILE* client_open(const char *UNUSED_PARAM, const char *mode)
188 {
189     (void)UNUSED_PARAM;
190     return fopen(JSON_DB_PATH, mode);
191 }
192
193 int main()
194 {
195     OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
196
197     PlatformConfig config
198     {
199         OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos, &ps
200     };
201
202     OCPlatform::Configure(config);
203
204 #ifdef __WITH_DTLS__
205     //Initializing the provisioning client stack using the db path provided by the application.
206     OCStackResult result = OCSecure::provisionInit("");
207
208     if (result != OC_STACK_OK)
209     {
210         return -1;
211     }
212 #endif
213
214     g_currentRun = runEasySetupMenu;
215
216     while (true)
217     {
218         try
219         {
220             g_currentRun();
221         }
222         catch (const std::exception& e)
223         {
224             std::cout << "Exception caught in main " << e.what() << std::endl;
225         }
226         catch (const CloseApp&)
227         {
228             break;
229         }
230     }
231
232     std::cout << "Stopping the client" << std::endl;
233
234     return 0;
235 }
236