replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / easy-setup / sampleapp / enrollee / linux / enrolleewifi.c
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 #include "easysetup_x.h"
24
25 #include <unistd.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <pthread.h>
29 #include <oic_string.h>
30 #include "oic_malloc.h"
31
32 #define TAG "ENROLLEE_SAMPLE"
33
34 void *listeningFunc(void *);
35 pthread_t thread_handle = NULL;
36
37 /**
38  * Secure Virtual Resource database for Iotivity Server
39  * It contains Server's Identity and the PSK credentials
40  * of other devices which the server trusts
41  */
42 #ifdef MULTIPLE_OWNER
43 static char CRED_FILE[] = "oic_svr_db_server_MOT.dat";
44 #else
45 static char CRED_FILE[] = "oic_svr_db_server.dat";
46 #endif
47
48 OCPersistentStorage ps;
49
50 /**
51  * @var gIsSecured
52  * @brief Variable to check if secure mode is enabled or not.
53  */
54 static bool gIsSecured = false;
55
56 UserProperties g_userProperties;
57
58 void PrintMenu()
59 {
60     printf("============\n");
61     printf("S: Enabled Security\n");
62     printf("I: Init & Start EasySetup\n");
63     printf("D: Set DeviceInfo\n");
64     printf("T: Terminate\n");
65     printf("U: set Callback for userdata\n");
66     printf("Q: Quit\n");
67     printf("============\n");
68 }
69
70 void ConnectRequestCbInApp(ESConnectRequest *connectRequest)
71 {
72     printf("ConnectRequestCbInApp IN\n");
73
74     if(connectRequest == NULL)
75     {
76         printf("connectRequest is NULL\n");
77         return ;
78     }
79
80     for(int i = 0 ; i < connectRequest->numRequest ; ++i)
81     {
82         printf("connect : %d\n", connectRequest->connect[i]);
83     }
84
85     printf("ConnectRequestCbInApp OUT\n");
86 }
87
88 void WiFiConfProvCbInApp(ESWiFiConfData *eventData)
89 {
90     printf("WiFiConfProvCbInApp IN\n");
91
92     if(eventData == NULL)
93     {
94         printf("ESWiFiConfData is NULL\n");
95         return ;
96     }
97
98     printf("SSID : %s\n", eventData->ssid);
99     printf("Password : %s\n", eventData->pwd);
100     printf("AuthType : %d\n", eventData->authtype);
101     printf("EncType : %d\n", eventData->enctype);
102
103     if(eventData->userdata != NULL)
104     {
105         printf("userValue : %d\n", ((UserProperties *)(eventData->userdata))->userValue_int);
106     }
107
108     printf("WiFiConfProvCbInApp OUT\n");
109 }
110
111 void DevConfProvCbInApp(ESDevConfData *eventData)
112 {
113     printf("DevConfProvCbInApp IN\n");
114
115     if(eventData == NULL)
116     {
117         printf("ESDevConfData is NULL\n");
118         return ;
119     }
120
121     printf("DevConfProvCbInApp OUT\n");
122 }
123
124 void CoapCloudConfProvCbInApp(ESCoapCloudConfData *eventData)
125 {
126     printf("CoapCloudConfProvCbInApp IN\n");
127
128     if(eventData == NULL)
129     {
130         printf("ESCoapCloudConfData is NULL\n");
131         return ;
132     }
133
134     printf("AuthCode : %s\n", eventData->authCode);
135     printf("AcessToken : %s\n", eventData->accessToken);
136     printf("AcessTokenType : %d\n", eventData->accessTokenType);
137     printf("AuthProvider : %s\n", eventData->authProvider);
138     printf("CI Server : %s\n", eventData->ciServer);
139
140     printf("CoapCloudConfProvCbInApp OUT\n");
141 }
142
143 ESProvisioningCallbacks gCallbacks = {
144     .ConnectRequestCb = &ConnectRequestCbInApp,
145     .WiFiConfProvCb = &WiFiConfProvCbInApp,
146     .DevConfProvCb = &DevConfProvCbInApp,
147     .CoapCloudConfProvCb = &CoapCloudConfProvCbInApp
148 };
149
150 FILE* server_fopen(const char *path, const char *mode)
151 {
152     (void) path;
153     return fopen(CRED_FILE, mode);
154 }
155
156 void EnableSecurity()
157 {
158     printf("Inside EnableSecurity API..\n");
159
160     gIsSecured = true;
161
162     // Initialize Persistent Storage for SVR database
163     ps = (OCPersistentStorage){ server_fopen, fread, fwrite, fclose, unlink };
164     OCRegisterPersistentStorageHandler(&ps);
165 }
166
167 void StartEasySetup()
168 {
169     printf("StartEasySetup IN\n");
170
171     if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
172     {
173         printf("OCStack init error!!\n");
174         return;
175     }
176
177     ESResourceMask resourcemMask = ES_WIFICONF_RESOURCE | ES_COAPCLOUDCONF_RESOURCE | ES_DEVCONF_RESOURCE;
178     if(ESInitEnrollee(gIsSecured, resourcemMask, gCallbacks) != ES_OK)
179     {
180         printf("OCStack init error!!\n");
181         return;
182     }
183     printf("ESInitEnrollee Success\n");
184
185     if (pthread_create(&thread_handle, NULL, listeningFunc, NULL))
186     {
187         printf("Thread creation failed\n");
188     }
189
190     printf("StartEasySetup OUT\n");
191 }
192
193 void SetDeviceInfo()
194 {
195     printf("SetDeviceInfo IN\n");
196
197     ESDeviceProperty deviceProperty = {
198         {{WIFI_11G, WIFI_11N, WIFI_11AC, WiFi_EOF}, WIFI_5G}, {"Test Device"}
199     };
200
201     // Set user properties if needed
202     char userValue_str[] = "user_str";
203     g_userProperties.userValue_int = 0;
204
205     strncpy(g_userProperties.userValue_str, userValue_str, strlen(userValue_str) + 1);
206     SetUserProperties(&g_userProperties);
207
208     if(ESSetDeviceProperty(&deviceProperty) == ES_ERROR)
209     {
210         printf("ESSetDeviceProperty Error\n");
211     }
212
213     printf("SetDeviceInfo OUT\n");
214 }
215
216 void StopEasySetup()
217 {
218     printf("StopEasySetup IN\n");
219
220     if (ESTerminateEnrollee() == ES_ERROR)
221     {
222         printf("ESTerminateEnrollee Failed!!\n");
223         return;
224     }
225
226     if (0 != pthread_cancel(thread_handle))
227     {
228         printf("Thread cancellation failed\n");
229         return;
230     }
231
232     //stop OC Stack
233     if (OCStop() != OC_STACK_OK)
234     {
235         printf("OCStack stop failed!!\n");
236         return;
237     }
238
239     printf("StopEasySetup OUT\n");
240 }
241
242 void SetCallbackForUserdata()
243 {
244     ESSetCallbackForUserdata(&ReadUserdataCb, &WriteUserdataCb);
245 }
246
247 int main()
248 {
249     printf("#########################\n");
250     printf("EasySetup Enrollee SAMPLE\n");
251     printf("#########################\n");
252     PrintMenu();
253     char option = '\0';
254
255     while(true)
256     {
257         if(scanf("%c", &option) != 1)
258         {
259             printf("Failed to read input data\n");
260             continue;
261         }
262
263         if(option!= '\n')
264         {
265             switch (option)
266             {
267                 case 'H': // help
268                 case 'h':
269                     PrintMenu();
270                     break;
271
272                 case 'Q': // quit
273                 case 'q':
274                     printf("quit");
275                     break;
276
277                 case 'S': // Enable Security
278                 case 's':
279                     EnableSecurity();
280                     PrintMenu();
281                     break;
282
283                 case 'I': // Init EasySetup
284                 case 'i':
285                     StartEasySetup();
286                     PrintMenu();
287                     break;
288
289                 case 'D': // Set Device Info
290                 case 'd':
291                     SetDeviceInfo();
292                     PrintMenu();
293                     break;
294                 case 'T': // stop easy setup
295                 case 't':
296                     StopEasySetup();
297                     PrintMenu();
298                     break;
299                 case 'U': // set callback
300                 case 'u':
301                     SetCallbackForUserdata();
302                     PrintMenu();
303                     break;
304
305                 default:
306                     printf("wrong option\n");
307                     PrintMenu();
308                     break;
309             }
310             if (option == 'Q' || option == 'q')
311             {
312                 break;
313             }
314         }
315     }
316     return 0;
317 }
318
319 void *listeningFunc(void * data)
320 {
321     (void)data;
322     OCStackResult result = OC_STACK_ERROR;
323
324     while (true)
325     {
326         result = OCProcess();
327         if (result != OC_STACK_OK)
328         {
329            printf("OCStack stop error");
330         }
331     }
332     return NULL;
333 }
334