Merge "Merge branch 'master' into easysetup" into 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 "OCPlatform.h"
27 #include "logger.h"
28
29 #define ES_SAMPLE_APP_TAG "ES_SAMPLE_APP_TAG"
30 #define DECLARE_MENU(FUNC, ...) { #FUNC, FUNC }
31
32 #define JSON_DB_PATH "./oic_svr_db_client.json"
33
34 using namespace OC;
35 using namespace OIC::Service;
36
37 static EasySetup *easySetupIntance = nullptr;
38 static ProvConfig netInfo;
39 static WiFiOnboadingConnection onboardingConn;
40 static RemoteEnrollee::shared_ptr remoteEnrollee = nullptr;
41
42 static std::string ipaddress, ssid, pwd;
43
44 struct CloseApp
45 {
46 };
47
48 typedef void (*Runner)();
49
50 Runner g_currentRun;
51
52 int processUserInput(int min, int max)
53 {
54     assert(min <= max);
55
56     int input;
57
58     std::cin >> input;
59
60     if (!std::cin.fail())
61     {
62         if(input == max + 1) throw CloseApp();
63         if(min <= input && input <= max) return input;
64     }
65
66     std::cin.clear();
67     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
68
69     throw std::runtime_error("Invalid Input, please try again");
70 }
71
72 void easySetupStatusCallback (std::shared_ptr< EasySetupStatus > easySetupStatus)
73 {
74     OC_LOG_V(DEBUG, ES_SAMPLE_APP_TAG, "easySetupStatusCallback status is Status = %d",
75             easySetupStatus->getEasySetupState());
76
77 }
78
79 void startProvisioning()
80 {
81     try
82     {
83         remoteEnrollee->startProvisioning();
84     }
85     catch(OCException &exception)
86     {
87         std::cout << "Exception : " << exception.reason();
88     }
89 }
90
91 void initEasySetup()
92 {
93
94     easySetupIntance = EasySetup::getInstance();
95
96     ipaddress = "192.168.1.104";
97     //std::cout << "Enter the target enrollee ipv4 address ";
98
99
100     ssid = "hub2.4G";
101     //std::cout << "Enter the ssid of the target Enrolleer ";
102     //std:: cin >> ssid;
103
104     pwd = "22221111";
105     //std::cout << "Enter the pwd of the target Enrolleer ";
106     //std::cin >> pwd;
107
108     netInfo.connType = CT_ADAPTER_IP;
109     //netInfo.isSecured = false;
110     //netInfo.needSecuredEasysetup = false;
111
112     OICStrcpy(netInfo.provData.WIFI.ssid, NET_WIFI_SSID_SIZE - 1, ssid.c_str());
113     OICStrcpy(netInfo.provData.WIFI.pwd, NET_WIFI_PWD_SIZE - 1, pwd.c_str());
114
115     onboardingConn.isSecured = false;
116     OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
117
118
119     try
120     {
121         remoteEnrollee = easySetupIntance->createEnrolleeDevice(netInfo,onboardingConn);
122     }
123     catch (OCException &e)
124     {
125         std::cout << "Exception during createEnrolleeDevice call" << e.reason();
126         return;
127     }
128
129     remoteEnrollee->registerEasySetupStatusHandler(&easySetupStatusCallback);
130 }
131 void runEasySetupMenu()
132 {
133     constexpr int EASY_SETUP_INIT = 1;
134     constexpr int START_PROVISIONING = 2;
135     constexpr int STOP_PROVISIONING = 3;
136     constexpr int STOP_EASY_SETUP = 4;
137
138     std::cout << "========================================================\n";
139     std::cout << EASY_SETUP_INIT << ". Easy Setup Init                    \n";
140     std::cout << START_PROVISIONING << ". Start Provisioning              \n";
141     std::cout << STOP_PROVISIONING << ". Stop Provisioning                \n";
142     std::cout << STOP_EASY_SETUP << ". Stop Easy Setup                    \n";
143     std::cout << STOP_EASY_SETUP + 1 << ". Quit                           \n";
144     std::cout << "========================================================\n";
145
146     int selection = processUserInput(EASY_SETUP_INIT, STOP_EASY_SETUP);
147
148     switch (selection)
149     {
150         case EASY_SETUP_INIT:
151             initEasySetup();
152             break;
153         case START_PROVISIONING:
154             startProvisioning();
155             break;
156         case STOP_PROVISIONING:
157             //stopProvisioning();
158             break;
159         case STOP_EASY_SETUP:
160             //stopEasySetup();
161             break;
162         default:
163             break;
164     };
165 }
166
167 static FILE* client_open(const char *UNUSED_PARAM, const char *mode)
168 {
169     (void)UNUSED_PARAM;
170     return fopen(JSON_DB_PATH, mode);
171 }
172
173 int main()
174 {
175     OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
176
177     PlatformConfig config
178     {
179         OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos, &ps
180     };
181     OCPlatform::Configure(config);
182
183     g_currentRun = runEasySetupMenu;
184
185     while (true)
186     {
187         try
188         {
189             g_currentRun();
190         }
191         catch (const std::exception& e)
192         {
193             std::cout << "Exception caught in main " << e.what() << std::endl;
194         }
195         catch (const CloseApp&)
196         {
197             break;
198         }
199     }
200
201     std::cout << "Stopping the client" << std::endl;
202
203     return 0;
204 }
205