Merge branch 'master' into easysetup
[platform/upstream/iotivity.git] / service / easy-setup / sampleapp / enrollee / tizen-sdb / EnrolleeSample / enrolleewifi.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
22 #include "easysetup.h"
23
24 #include <string.h>
25 #include <iostream>
26 #include <pthread.h>
27
28 #define TAG "TS"
29
30 using namespace std;
31
32 void *listeningFunc(void*);
33
34 /**
35  * @var ssid
36  * @brief Target SSID of the Soft Access point to which the device has to connect
37  */
38 static char ssid[] = "EasySetup123";
39
40 /**
41  * @var passwd
42  * @brief Password of the Soft Access point to which the device has to connect
43  */
44 static char passwd[] = "EasySetup123";
45
46 /**
47  * Secure Virtual Resource database for Iotivity Server
48  * It contains Server's Identity and the PSK credentials
49  * of other devices which the server trusts
50  */
51 static char CRED_FILE[] = "oic_svr_db_server.json";
52
53 /**
54  * @var gIsSecured
55  * @brief Variable to check if secure mode is enabled or not.
56  */
57 static bool gIsSecured = false;
58
59 void PrintMenu()
60 {
61     cout<<"============"<<endl;
62     cout<<"I: Init easy setup"<<endl;
63     cout<<"S: Enabled Security"<<endl;
64     cout<<"P: start provisioning resources"<<endl;
65     cout<<"T: terminate"<<endl;
66     cout<<"Q: quit"<<endl;
67     cout<<"============"<<endl;
68 }
69
70 void EventCallbackInApp(ESResult esResult, EnrolleeState enrolleeState)
71 {
72     cout<<"Easy setup event callback"<<endl;
73
74     if(esResult == ES_OK)
75     {
76         if(enrolleeState == ES_ON_BOARDED_STATE)
77         {
78             cout<<"Device is successfully OnBoared on Adhoc network"<<endl;
79         }
80         else if (enrolleeState == ES_PROVISIONED_STATE)
81         {
82             cout<<"Device is provisioned with target network's credentials"<<endl;
83         }
84         else if (enrolleeState == ES_ON_BOARDED_TARGET_NETWORK_STATE)
85         {
86             cout<<"Device is onboarded/connected with target network"<<endl;
87         }
88         else
89         {
90             cout<<"Wrong state !! Easy setup is failed at Enrollee state = "<<enrolleeState<<endl;
91         }
92     }
93     else
94     {
95         cout<<"Easy stup is failed at Enrollee state = "<<enrolleeState<<endl;
96     }
97
98     PrintMenu();
99 }
100
101 FILE* server_fopen(const char *path, const char *mode)
102 {
103     (void) path;
104     return fopen(CRED_FILE, mode);
105 }
106
107 void EnableSecurity()
108 {
109     cout << "Inside EnableSecurity API.." << endl;
110
111     gIsSecured = true;
112
113     // Initialize Persistent Storage for SVR database
114     OCPersistentStorage ps = { server_fopen, fread, fwrite, fclose, unlink };
115     OCRegisterPersistentStorageHandler(&ps);
116 }
117
118 void StartEasySetup()
119 {
120     cout<<"StartEasySetup and onboarding started.."<<endl;
121
122     if(InitEasySetup(CT_ADAPTER_IP, ssid, passwd, gIsSecured, EventCallbackInApp) == ES_ERROR)
123     {
124         cout<<"StartEasySetup and onboarding Fail!!"<<endl;
125         return;
126     }
127
128     pthread_t thread_handle;
129     if (pthread_create(&thread_handle, NULL, listeningFunc, NULL))
130     {
131         cout<<"Thread creation failed"<<endl;
132     }
133 }
134
135 void StartOICStackAndStartResources()
136 {
137     cout<<"Starting Enrollee Provisioning"<<endl;
138
139     // Initialize the OC Stack in Server mode
140     if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
141     {
142         cout<<"OCStack init error!!"<<endl;
143         return;
144     }
145
146     if (InitProvisioning() == ES_ERROR)
147     {
148         cout<<"Init Provisioning Failed!!"<<endl;
149         return;
150     }
151
152     cout<<"InitProvisioning Success"<<endl;
153 }
154
155 void StopEasySetup()
156 {
157     cout<<"StopEasySetup IN"<<endl;
158
159     if (TerminateEasySetup() == ES_ERROR)
160     {
161         cout<<"TerminateEasySetup Failed!!"<<endl;
162         return;
163     }
164
165     //stop OC Stack
166     if (OCStop() != OC_STACK_OK)
167     {
168         cout<<"OCStack stop failed!!"<<endl;
169         return;
170     }
171
172     cout<<"StopEasySetup OUT"<<endl;
173 }
174
175 int main()
176 {
177     cout<<"#########################"<<endl;
178     cout<<"EasySetup Enrollee SAMPLE"<<endl;
179     cout<<"#########################"<<endl;
180     PrintMenu();
181     char option;
182
183     while(true)
184     {
185         cin>>option;
186         switch (option)
187         {
188             case 'H': // help
189             case 'h':
190                 PrintMenu();
191                 break;
192
193             case 'Q': // quit
194             case 'q':
195                 cout<<"quit";
196                 break;
197
198             case 'I': // Init EasySetup
199             case 'i':
200                 StartEasySetup();
201                 break;
202
203             case 'S': // Enable Security
204             case 's':
205                 EnableSecurity();
206                 break;
207
208             case 'P': // start provisioning
209             case 'p':
210                 StartOICStackAndStartResources();
211                 break;
212
213             case 'T': // stop easy setup
214             case 't':
215                 StopEasySetup();
216                 break;
217
218             default:
219                 cout<<"wrong option"<<endl;
220                 break;
221         }
222         if (option == 'Q' || option == 'q') break;
223     }
224     return 0;
225 }
226
227 void *listeningFunc(void*)
228 {
229     OCStackResult result;
230
231     while (true)
232     {
233         result = OCProcess();
234         if (result != OC_STACK_OK)
235         {
236            cout<<"OCStack stop error";
237         }
238     }
239     return NULL;
240 }
241