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