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