Merge branch 'master' into windows-port
[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     OC_UNUSED(easySetupStatus);
78     OIC_LOG_V(DEBUG, ES_SAMPLE_APP_TAG, "easySetupStatusCallback status is Status = %d",
79             easySetupStatus->getEasySetupState());
80
81 }
82
83 void startProvisioning()
84 {
85     try
86     {
87         remoteEnrollee->startProvisioning();
88     }
89     catch(OCException &exception)
90     {
91         std::cout << "Exception : " << exception.reason();
92     }
93 }
94
95 void initEasySetup()
96 {
97
98     easySetupIntance = EasySetup::getInstance();
99
100     cout<<"\n Enter the IP address : ";
101     cin>>ipaddress;
102     cout<<"\n Enter the Target Network SSID : ";
103     cin>>ssid;
104     cout<<"\n Enter the Target Network Password : ";
105     cin>>pwd;
106     cout<<"\n Enable Security: [Y/N] ";
107     cin>>security;
108
109     if ( ipaddress.size() == 0 || ssid.size() == 0  || pwd.size()==0 )
110     {
111          cout<<"\n Invalid information try again !!!";
112     }
113     else
114      {
115            cout <<"\n Entered details are :  \n";
116            cout<<"\n IP address : "<<ipaddress;
117            cout<<"\n Target Network SSID : "<<ssid;
118            cout<<"\n Target Network Password : "<<pwd;
119
120           if (security == 'Y' || security == 'y' )
121           {
122                   onboardingConn.isSecured = true;
123                   cout<<"\n Security is Enabled\n\n\n";
124            }
125            else
126            {
127                  onboardingConn.isSecured = false;
128                  cout<<"\n Security is not Enabled\n\n\n";
129            }
130
131           netInfo.connType = CT_ADAPTER_IP;
132
133          OICStrcpy(netInfo.provData.WIFI.ssid, NET_WIFI_SSID_SIZE - 1, ssid.c_str());
134          OICStrcpy(netInfo.provData.WIFI.pwd, NET_WIFI_PWD_SIZE - 1, pwd.c_str());
135
136          OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
137
138          try
139          {
140              remoteEnrollee = easySetupIntance->createEnrolleeDevice(netInfo,onboardingConn);
141          }
142          catch (OCException &e)
143          {
144              std::cout << "Exception during createEnrolleeDevice call" << e.reason();
145              return;
146          }
147
148          remoteEnrollee->registerEasySetupStatusHandler(&easySetupStatusCallback);
149     }
150 }
151
152 void runEasySetupMenu()
153 {
154     constexpr int EASY_SETUP_INIT = 1;
155     constexpr int START_PROVISIONING = 2;
156     constexpr int STOP_PROVISIONING = 3;
157     constexpr int STOP_EASY_SETUP = 4;
158
159     std::cout << "========================================================\n";
160     std::cout << EASY_SETUP_INIT << ". Easy Setup Init                    \n";
161     std::cout << START_PROVISIONING << ". Start Provisioning              \n";
162     std::cout << STOP_PROVISIONING << ". Stop Provisioning                \n";
163     std::cout << STOP_EASY_SETUP << ". Stop Easy Setup                    \n";
164     std::cout << STOP_EASY_SETUP + 1 << ". Quit                           \n";
165     std::cout << "========================================================\n";
166
167     int selection = processUserInput(EASY_SETUP_INIT, STOP_EASY_SETUP);
168
169     switch (selection)
170     {
171         case EASY_SETUP_INIT:
172             initEasySetup();
173             break;
174         case START_PROVISIONING:
175             startProvisioning();
176             break;
177         case STOP_PROVISIONING:
178             //stopProvisioning();
179             break;
180         case STOP_EASY_SETUP:
181             //stopEasySetup();
182             break;
183         default:
184             break;
185     };
186 }
187
188 static FILE* client_open(const char *UNUSED_PARAM, const char *mode)
189 {
190     (void)UNUSED_PARAM;
191     return fopen(JSON_DB_PATH, mode);
192 }
193
194 int main()
195 {
196     OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
197
198     PlatformConfig config
199     {
200         OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos, &ps
201     };
202
203     OCPlatform::Configure(config);
204
205 #ifdef __WITH_DTLS__
206     //Initializing the provisioning client stack using the db path provided by the application.
207     OCStackResult result = OCSecure::provisionInit("");
208
209     if (result != OC_STACK_OK)
210     {
211         return -1;
212     }
213 #endif
214
215     g_currentRun = runEasySetupMenu;
216
217     while (true)
218     {
219         try
220         {
221             g_currentRun();
222         }
223         catch (const std::exception& e)
224         {
225             std::cout << "Exception caught in main " << e.what() << std::endl;
226         }
227         catch (const CloseApp&)
228         {
229             break;
230         }
231     }
232
233     std::cout << "Stopping the client" << std::endl;
234
235     return 0;
236 }
237