Easysetup - Security PDM db file init flow change & fix compilation issue in Java...
[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.json"
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
46 struct CloseApp
47 {
48 };
49
50 typedef void (*Runner)();
51
52 Runner g_currentRun;
53
54 int processUserInput(int min, int max)
55 {
56     assert(min <= max);
57
58     int input;
59
60     std::cin >> input;
61
62     if (!std::cin.fail())
63     {
64         if(input == max + 1) throw CloseApp();
65         if(min <= input && input <= max) return input;
66     }
67
68     std::cin.clear();
69     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
70
71     throw std::runtime_error("Invalid Input, please try again");
72 }
73
74 void easySetupStatusCallback (std::shared_ptr< EasySetupStatus > easySetupStatus)
75 {
76     OC_LOG_V(DEBUG, ES_SAMPLE_APP_TAG, "easySetupStatusCallback status is Status = %d",
77             easySetupStatus->getEasySetupState());
78
79 }
80
81 void startProvisioning()
82 {
83     try
84     {
85         remoteEnrollee->startProvisioning();
86     }
87     catch(OCException &exception)
88     {
89         std::cout << "Exception : " << exception.reason();
90     }
91 }
92
93 void initEasySetup()
94 {
95
96     easySetupIntance = EasySetup::getInstance();
97
98     ipaddress = "192.168.1.104";
99     //std::cout << "Enter the target enrollee ipv4 address ";
100
101
102     ssid = "hub2.4G";
103     //std::cout << "Enter the ssid of the target Enrolleer ";
104     //std:: cin >> ssid;
105
106     pwd = "22221111";
107     //std::cout << "Enter the pwd of the target Enrolleer ";
108     //std::cin >> pwd;
109
110     netInfo.connType = CT_ADAPTER_IP;
111     //netInfo.isSecured = false;
112     //netInfo.needSecuredEasysetup = false;
113
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());
116
117     onboardingConn.isSecured = false;
118     OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
119
120
121     try
122     {
123         remoteEnrollee = easySetupIntance->createEnrolleeDevice(netInfo,onboardingConn);
124     }
125     catch (OCException &e)
126     {
127         std::cout << "Exception during createEnrolleeDevice call" << e.reason();
128         return;
129     }
130
131     remoteEnrollee->registerEasySetupStatusHandler(&easySetupStatusCallback);
132 }
133 void runEasySetupMenu()
134 {
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;
139
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";
147
148     int selection = processUserInput(EASY_SETUP_INIT, STOP_EASY_SETUP);
149
150     switch (selection)
151     {
152         case EASY_SETUP_INIT:
153             initEasySetup();
154             break;
155         case START_PROVISIONING:
156             startProvisioning();
157             break;
158         case STOP_PROVISIONING:
159             //stopProvisioning();
160             break;
161         case STOP_EASY_SETUP:
162             //stopEasySetup();
163             break;
164         default:
165             break;
166     };
167 }
168
169 static FILE* client_open(const char *UNUSED_PARAM, const char *mode)
170 {
171     (void)UNUSED_PARAM;
172     return fopen(JSON_DB_PATH, mode);
173 }
174
175 int main()
176 {
177     OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
178
179     PlatformConfig config
180     {
181         OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos, &ps
182     };
183
184     OCPlatform::Configure(config);
185
186 #ifdef __WITH_DTLS__
187     //Initializing the provisioning client stack using the db path provided by the application.
188     OCStackResult result = OCSecure::provisionInit("");
189
190     if (result != OC_STACK_OK)
191     {
192         return -1;
193     }
194 #endif
195
196     g_currentRun = runEasySetupMenu;
197
198     while (true)
199     {
200         try
201         {
202             g_currentRun();
203         }
204         catch (const std::exception& e)
205         {
206             std::cout << "Exception caught in main " << e.what() << std::endl;
207         }
208         catch (const CloseApp&)
209         {
210             break;
211         }
212     }
213
214     std::cout << "Stopping the client" << std::endl;
215
216     return 0;
217 }
218