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