Merge remote-tracking branch 'origin/master' into extended-easysetup
[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 "ESRichCommon.h"
27 #include "OCPlatform.h"
28 #include "logger.h"
29 #include "OCProvisioningManager.h"
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 std::shared_ptr<RemoteEnrollee> remoteEnrollee = nullptr;
41
42 static std::string ipaddress, ssid, pwd;
43 char security;
44
45 typedef void (*Runner)();
46
47 Runner g_currentRun;
48
49 int processUserInput(int min = std::numeric_limits<int>::min(),
50         int max = std::numeric_limits<int>::max())
51 {
52     assert(min <= max);
53
54     int input;
55
56     std::cin >> input;
57     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
58
59     if (!std::cin.fail() && min <= input && input <= max) return input;
60
61     std::cin.clear();
62     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
63
64     throw std::runtime_error("Invalid Input, please try again");
65 }
66
67 void printPropertyData(PropertyData propData)
68 {
69     cout << "===========================================" << endl;
70     DeviceConfig devConfig = propData.getDevConf();
71     NetworkInfo netInfo = propData.getNetInfo();
72
73     cout << "\tDeviceConfig.id : " << devConfig.id << endl;
74     cout << "\tDeviceConfig.name : " << devConfig.name << endl;
75     cout << "\tDeviceConfig.language : " << devConfig.language << endl;
76     cout << "\tDeviceConfig.country : " << devConfig.country << endl;
77
78     for(auto type = netInfo.types.begin(); type != netInfo.types.end(); ++type)
79     {
80         cout << "\tnetInfo.types : " << static_cast<int>(*type) << endl;
81     }
82     cout << "\tnetInfo.freq : " << static_cast<int>(netInfo.freq) << endl;
83     cout << "===========================================" << endl;
84 }
85
86 void RequestPropertyDataStatusCallback(std::shared_ptr< RequestPropertyDataStatus > requestPropertyDataStatus)
87 {
88     cout << "Enter RequestPropertyDataStatusCb." << endl;
89
90     if(requestPropertyDataStatus->getESResult() != ES_OK)
91     {
92       cout << "requestPropertyDataStatus is failed." << endl;
93       return;
94     }
95     else
96     {
97       cout << "requestPropertyDataStatus is success." << endl;
98       printPropertyData(requestPropertyDataStatus->getPropertyData());
99     }
100 }
101
102 void dataProvisioningStatusCallback(std::shared_ptr< DataProvisioningStatus > provStatus)
103 {
104     cout << "Enter dataProvisioningStatusCallback." << endl;
105
106     if(provStatus->getESResult() != ES_OK)
107     {
108       cout << "dataProvisioningStatusCallback is failed." << endl;
109       return;
110     }
111     else
112     {
113       cout << "dataProvisioningStatusCallback is success." << endl;
114       cout << "ESDataProvState : " << provStatus->getESDataProvState() << endl;
115     }
116 }
117
118 void cloudProvisioningStatusCallback(std::shared_ptr< CloudProvisioningStatus > status)
119 {
120     cout << "Enter cloudProvisioningStatusCallback." << endl;
121     cout << "CloudProvStatus : " <<  status->getESCloudState() << endl;
122 }
123
124 void createRemoteEnrollee()
125 {
126     easySetupIntance = EasySetup::getInstance();
127     try
128     {
129         remoteEnrollee = easySetupIntance->createRemoteEnrollee();
130     }
131     catch (OCException &e)
132     {
133         std::cout << "Exception during createEnrolleeDevice call" << e.reason();
134         return;
135     }
136     cout << "createRemoteEnrollee is success." << endl;
137 }
138
139 void initRemoteEnrollee()
140 {
141     try
142     {
143         remoteEnrollee->initRemoteEnrollee();
144     }
145     catch (OCException &e)
146     {
147         std::cout << "Exception during initRemoteEnrollee call" << e.reason();
148         return;
149     }
150 }
151
152 void requestPropertyData()
153 {
154     try
155     {
156         remoteEnrollee->requestPropertyData(RequestPropertyDataStatusCallback);
157     }
158     catch (OCException &e)
159     {
160         std::cout << "Exception during requestPropertyData call" << e.reason();
161         return;
162     }
163 }
164
165 void setDataProvInfo()
166 {
167     DataProvInfo dataProvInfo;
168     dataProvInfo.WIFI.ssid = "Iotivity_2.4G";
169     dataProvInfo.WIFI.pwd = "1234567890";
170     dataProvInfo.WIFI.authtype = WPA2_PSK;
171     dataProvInfo.WIFI.enctype = TKIP_AES;
172     dataProvInfo.Device.language = "korean";
173     dataProvInfo.Device.country = "korea";
174
175     remoteEnrollee->setDataProvInfo(dataProvInfo);
176 }
177
178 void setCloudProvInfo()
179 {
180     CloudProvInfo cloudProvInfo;
181     cloudProvInfo.authCode = "authCode";
182     cloudProvInfo.authProvider = "authProvider";
183     cloudProvInfo.ciServer = "ciServer";
184
185     remoteEnrollee->setCloudProvInfo(cloudProvInfo);
186 }
187
188 void startDataProvisioning()
189 {
190     try
191     {
192         remoteEnrollee->startDataProvisioning(dataProvisioningStatusCallback);
193     }
194     catch (OCException &e)
195     {
196         std::cout << "Exception during startDataProvisioning call" << e.reason();
197         return;
198     }
199 }
200
201 void startCloudProvisioning()
202 {
203     try
204     {
205         remoteEnrollee->startCloudProvisioning(cloudProvisioningStatusCallback);
206     }
207     catch (OCException &e)
208     {
209         std::cout << "Exception during startDataProvisioning call" << e.reason();
210         return;
211     }
212 }
213
214 void DisplayMenu()
215 {
216     constexpr int CREATE_REMOTE_ENROLLEE = 1;
217     constexpr int EASY_SETUP_INIT = 2;
218     constexpr int REQUEST_PROPERTY_DATA = 3;
219     constexpr int SET_DATA_PROVISIONING_INFO = 4;
220     constexpr int START_DATA_PROVISIONING = 5;
221     constexpr int SET_CLOUD_PROVISIONING_INFO = 6;
222     constexpr int START_CLOUD_PROVISIONING = 7;
223
224     std::cout << "========================================================\n";
225     std::cout << CREATE_REMOTE_ENROLLEE << ". Create Remote Enrollee                    \n";
226     std::cout << EASY_SETUP_INIT << ". Easy Setup Init                    \n";
227     std::cout << REQUEST_PROPERTY_DATA << ". Request PropertyData              \n";
228     std::cout << SET_DATA_PROVISIONING_INFO << ". Set Data Provisioning Info              \n";
229     std::cout << START_DATA_PROVISIONING << ". Start Data Provisioning              \n";
230     std::cout << SET_CLOUD_PROVISIONING_INFO << ". Set Cloud Provisioning Info              \n";
231     std::cout << START_CLOUD_PROVISIONING << ". Start Cloud Provisioning              \n";
232     std::cout << "========================================================\n";
233
234     int selection = processUserInput(CREATE_REMOTE_ENROLLEE, START_CLOUD_PROVISIONING);
235
236     switch (selection)
237     {
238         case CREATE_REMOTE_ENROLLEE:
239             createRemoteEnrollee();
240             break;
241         case EASY_SETUP_INIT:
242             initRemoteEnrollee();
243             break;
244         case REQUEST_PROPERTY_DATA:
245             requestPropertyData();
246             break;
247         case SET_DATA_PROVISIONING_INFO:
248             setDataProvInfo();
249             break;
250         case START_DATA_PROVISIONING:
251             startDataProvisioning();
252             break;
253         case SET_CLOUD_PROVISIONING_INFO:
254             setCloudProvInfo();
255             break;
256         case START_CLOUD_PROVISIONING:
257             startCloudProvisioning();
258             break;
259         default:
260             break;
261     };
262 }
263
264 static FILE* client_open(const char *UNUSED_PARAM, const char *mode)
265 {
266     (void)UNUSED_PARAM;
267     return fopen(JSON_DB_PATH, mode);
268 }
269
270 int main()
271 {
272     OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
273
274     PlatformConfig config
275     {
276         OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos, &ps
277     };
278
279     OCPlatform::Configure(config);
280
281 #ifdef __WITH_DTLS__
282     //Initializing the provisioning client stack using the db path provided by the application.
283     OCStackResult result = OCSecure::provisionInit("");
284
285     if (result != OC_STACK_OK)
286     {
287         return -1;
288     }
289 #endif
290
291     while (true)
292     {
293         try
294         {
295             DisplayMenu();
296         }
297         catch (const std::exception& e)
298         {
299             std::cout << "Exception caught in main " << e.what() << std::endl;
300         }
301     }
302
303     std::cout << "Stopping the client" << std::endl;
304
305     return 0;
306 }
307