ff41ecc09c8f1f7ec2d3bed4920ee693fca46f8b
[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     OIC_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     ssid = "hub2.4G";
100     pwd = "22221111";
101
102     netInfo.connType = CT_ADAPTER_IP;
103
104     OICStrcpy(netInfo.provData.WIFI.ssid, NET_WIFI_SSID_SIZE - 1, ssid.c_str());
105     OICStrcpy(netInfo.provData.WIFI.pwd, NET_WIFI_PWD_SIZE - 1, pwd.c_str());
106
107     onboardingConn.isSecured = false;
108     OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
109
110     try
111     {
112         remoteEnrollee = easySetupIntance->createEnrolleeDevice(netInfo,onboardingConn);
113     }
114     catch (OCException &e)
115     {
116         std::cout << "Exception during createEnrolleeDevice call" << e.reason();
117         return;
118     }
119
120     remoteEnrollee->registerEasySetupStatusHandler(&easySetupStatusCallback);
121 }
122 void runEasySetupMenu()
123 {
124     constexpr int EASY_SETUP_INIT = 1;
125     constexpr int START_PROVISIONING = 2;
126     constexpr int STOP_PROVISIONING = 3;
127     constexpr int STOP_EASY_SETUP = 4;
128
129     std::cout << "========================================================\n";
130     std::cout << EASY_SETUP_INIT << ". Easy Setup Init                    \n";
131     std::cout << START_PROVISIONING << ". Start Provisioning              \n";
132     std::cout << STOP_PROVISIONING << ". Stop Provisioning                \n";
133     std::cout << STOP_EASY_SETUP << ". Stop Easy Setup                    \n";
134     std::cout << STOP_EASY_SETUP + 1 << ". Quit                           \n";
135     std::cout << "========================================================\n";
136
137     int selection = processUserInput(EASY_SETUP_INIT, STOP_EASY_SETUP);
138
139     switch (selection)
140     {
141         case EASY_SETUP_INIT:
142             initEasySetup();
143             break;
144         case START_PROVISIONING:
145             startProvisioning();
146             break;
147         case STOP_PROVISIONING:
148             //stopProvisioning();
149             break;
150         case STOP_EASY_SETUP:
151             //stopEasySetup();
152             break;
153         default:
154             break;
155     };
156 }
157
158 static FILE* client_open(const char *UNUSED_PARAM, const char *mode)
159 {
160     (void)UNUSED_PARAM;
161     return fopen(JSON_DB_PATH, mode);
162 }
163
164 int main()
165 {
166     OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
167
168     PlatformConfig config
169     {
170         OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos, &ps
171     };
172
173     OCPlatform::Configure(config);
174
175 #ifdef __WITH_DTLS__
176     //Initializing the provisioning client stack using the db path provided by the application.
177     OCStackResult result = OCSecure::provisionInit("");
178
179     if (result != OC_STACK_OK)
180     {
181         return -1;
182     }
183 #endif
184
185     g_currentRun = runEasySetupMenu;
186
187     while (true)
188     {
189         try
190         {
191             g_currentRun();
192         }
193         catch (const std::exception& e)
194         {
195             std::cout << "Exception caught in main " << e.what() << std::endl;
196         }
197         catch (const CloseApp&)
198         {
199             break;
200         }
201     }
202
203     std::cout << "Stopping the client" << std::endl;
204
205     return 0;
206 }
207