[ENROLLEE] Tizen enrollee sample application build using scons command
[platform/upstream/iotivity.git] / service / easy-setup / sampleapp / mediator / linux / 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 EnrolleeNWProvInfo netInfo;
39 static RemoteEnrollee::shared_ptr remoteEnrollee = nullptr;
40
41 static std::string ipaddress, ssid, pwd;
42
43 struct CloseApp
44 {
45 };
46
47 typedef void (*Runner)();
48
49 Runner g_currentRun;
50
51 int processUserInput(int min, int max)
52 {
53     assert(min <= max);
54
55     int input;
56
57     std::cin >> input;
58
59     if (!std::cin.fail())
60     {
61         if(input == max + 1) throw CloseApp();
62         if(min <= input && input <= max) return input;
63     }
64
65     std::cin.clear();
66     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
67
68     throw std::runtime_error("Invalid Input, please try again");
69 }
70
71 void easySetupStatusCallback (std::shared_ptr< EasySetupStatus > easySetupStatus)
72 {
73     OC_LOG_V(DEBUG, ES_SAMPLE_APP_TAG, "easySetupStatusCallback status is, IP = %s, Status = %d",
74             easySetupStatus->getEasySetupNWProvInfo().netAddressInfo.WIFI.ipAddress,
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 = "10.113.64.106";
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 = true;
111     OICStrcpy(netInfo.netAddressInfo.WIFI.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
112     OICStrcpy(netInfo.netAddressInfo.WIFI.ssid, NET_WIFI_SSID_SIZE - 1, ssid.c_str());
113     OICStrcpy(netInfo.netAddressInfo.WIFI.pwd, NET_WIFI_PWD_SIZE - 1, pwd.c_str());
114
115     try
116     {
117         remoteEnrollee = easySetupIntance->createEnrolleeDevice(netInfo);
118     }
119     catch (OCException &e)
120     {
121         std::cout << "Exception during createEnrolleeDevice call" << e.reason();
122         return;
123     }
124
125     remoteEnrollee->registerEasySetupStatusHandler(&easySetupStatusCallback);
126 }
127 void runEasySetupMenu()
128 {
129     constexpr int EASY_SETUP_INIT = 1;
130     constexpr int START_PROVISIONING = 2;
131     constexpr int STOP_PROVISIONING = 3;
132     constexpr int STOP_EASY_SETUP = 4;
133
134     std::cout << "========================================================\n";
135     std::cout << EASY_SETUP_INIT << ". Easy Setup Init                    \n";
136     std::cout << START_PROVISIONING << ". Start Provisioning              \n";
137     std::cout << STOP_PROVISIONING << ". Stop Provisioning                \n";
138     std::cout << STOP_EASY_SETUP << ". Stop Easy Setup                    \n";
139     std::cout << STOP_EASY_SETUP + 1 << ". Quit                           \n";
140     std::cout << "========================================================\n";
141
142     int selection = processUserInput(EASY_SETUP_INIT, STOP_EASY_SETUP);
143
144     switch (selection)
145     {
146         case EASY_SETUP_INIT:
147             initEasySetup();
148             break;
149         case START_PROVISIONING:
150             startProvisioning();
151             break;
152         case STOP_PROVISIONING:
153             //stopProvisioning();
154             break;
155         case STOP_EASY_SETUP:
156             //stopEasySetup();
157             break;
158         default:
159             break;
160     };
161 }
162
163 static FILE* client_open(const char *UNUSED_PARAM, const char *mode)
164 {
165     (void)UNUSED_PARAM;
166     return fopen(JSON_DB_PATH, mode);
167 }
168
169 int main()
170 {
171     OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
172
173     PlatformConfig config
174     {
175         OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos, &ps
176     };
177     OCPlatform::Configure(config);
178
179     g_currentRun = runEasySetupMenu;
180
181     while (true)
182     {
183         try
184         {
185             g_currentRun();
186         }
187         catch (const std::exception& e)
188         {
189             std::cout << "Exception caught in main " << e.what() << std::endl;
190         }
191         catch (const CloseApp&)
192         {
193             break;
194         }
195     }
196
197     std::cout << "Stopping the client" << std::endl;
198
199     return 0;
200 }
201