a3126c0c71c51d955c848d78652b2f06ac9f635c
[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 static char CRED_FILE[] = "oic_svr_db_server.dat";
43
44 OCPersistentStorage ps;
45
46 /**
47  * @var gIsSecured
48  * @brief Variable to check if secure mode is enabled or not.
49  */
50 static bool gIsSecured = false;
51
52 UserProperties g_userProperties;
53
54 void PrintMenu()
55 {
56     printf("============\n");
57     printf("S: Enabled Security\n");
58     printf("I: Init & Start EasySetup\n");
59     printf("D: Set DeviceInfo\n");
60     printf("T: Terminate\n");
61     printf("U: set Callback for userdata\n");
62     printf("Q: Quit\n");
63     printf("============\n");
64 }
65
66 void WiFiProvCbInApp(ESWiFiProvData *eventData)
67 {
68     printf("WiFiProvCbInApp IN\n");
69
70     if(eventData == NULL)
71     {
72         printf("ESWiFiProvData is NULL\n");
73         return ;
74     }
75
76     printf("SSID : %s\n", eventData->ssid);
77     printf("Password : %s\n", eventData->pwd);
78     printf("AuthType : %d\n", eventData->authtype);
79     printf("EncType : %d\n", eventData->enctype);
80
81     if(eventData->userdata != NULL)
82     {
83         printf("userValue : %d\n", ((UserProperties *)(eventData->userdata))->userValue_int);
84     }
85
86     printf("WiFiProvCbInApp OUT\n");
87 }
88
89 void DevConfProvCbInApp(ESDevConfProvData *eventData)
90 {
91     printf("DevConfProvCbInApp IN\n");
92
93     if(eventData == NULL)
94     {
95         printf("ESDevConfProvData is NULL\n");
96         return ;
97     }
98
99     printf("Language : %s\n", eventData->language);
100     printf("Country : %s\n", eventData->country);
101
102     printf("DevConfProvCbInApp OUT\n");
103 }
104
105 void CloudDataProvCbInApp(ESCloudProvData *eventData)
106 {
107     printf("CloudDataProvCbInApp IN\n");
108
109     if(eventData == NULL)
110     {
111         printf("ESCloudProvData is NULL\n");
112         return ;
113     }
114
115     printf("AuthCode : %s\n", eventData->authCode);
116     printf("AuthProvider : %s\n", eventData->authProvider);
117     printf("CI Server : %s\n", eventData->ciServer);
118
119     printf("CloudDataProvCbInApp OUT\n");
120 }
121
122 ESProvisioningCallbacks gCallbacks = {
123     .WiFiProvCb = &WiFiProvCbInApp,
124     .DevConfProvCb = &DevConfProvCbInApp,
125     .CloudDataProvCb = &CloudDataProvCbInApp
126 };
127
128 FILE* server_fopen(const char *path, const char *mode)
129 {
130     (void) path;
131     return fopen(CRED_FILE, mode);
132 }
133
134 void EnableSecurity()
135 {
136     printf("Inside EnableSecurity API..\n");
137
138     gIsSecured = true;
139
140     // Initialize Persistent Storage for SVR database
141     ps = (OCPersistentStorage){ server_fopen, fread, fwrite, fclose, unlink };
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
163     if (pthread_create(&thread_handle, NULL, listeningFunc, NULL))
164     {
165         printf("Thread creation failed\n");
166     }
167
168     printf("StartEasySetup OUT\n");
169 }
170
171 void SetDeviceInfo()
172 {
173     printf("SetDeviceInfo IN\n");
174
175     ESDeviceProperty deviceProperty = {
176         {{WIFI_11G, WIFI_11N, WIFI_11AC, WiFi_EOF}, WIFI_5G}, {"Test Device", "Test Model Number"}
177     };
178
179     // Set user properties if needed
180     char userValue_str[] = "user_str";
181     g_userProperties.userValue_int = 0;
182     strcpy(g_userProperties.userValue_str, userValue_str);
183     SetUserProperties(&g_userProperties);
184
185     if(ESSetDeviceProperty(&deviceProperty) == ES_ERROR)
186     {
187         printf("ESSetDeviceProperty Error\n");
188     }
189
190     printf("SetDeviceInfo OUT\n");
191 }
192
193 void StopEasySetup()
194 {
195     printf("StopEasySetup IN\n");
196
197     if (ESTerminateEnrollee() == ES_ERROR)
198     {
199         printf("ESTerminateEnrollee Failed!!\n");
200         return;
201     }
202
203     if (0 != pthread_cancel(thread_handle))
204     {
205         printf("Thread cancellation failed\n");
206         return;
207     }
208
209     //stop OC Stack
210     if (OCStop() != OC_STACK_OK)
211     {
212         printf("OCStack stop failed!!\n");
213         return;
214     }
215
216     printf("StopEasySetup OUT\n");
217 }
218
219 void SetCallbackForUserdata()
220 {
221     ESSetCallbackForUserdata(&ReadUserdataCb, &WriteUserdataCb);
222 }
223
224 int main()
225 {
226     printf("#########################\n");
227     printf("EasySetup Enrollee SAMPLE\n");
228     printf("#########################\n");
229     PrintMenu();
230     char option;
231
232     while(true)
233     {
234         if(scanf("%c", &option) != 1)
235         {
236             printf("Failed to read input data\n");
237             continue;
238         }
239
240         if(option!= '\n')
241         {
242             switch (option)
243             {
244                 case 'H': // help
245                 case 'h':
246                     PrintMenu();
247                     break;
248
249                 case 'Q': // quit
250                 case 'q':
251                     printf("quit");
252                     break;
253
254                 case 'S': // Enable Security
255                 case 's':
256                     EnableSecurity();
257                     PrintMenu();
258                     break;
259
260                 case 'I': // Init EasySetup
261                 case 'i':
262                     StartEasySetup();
263                     PrintMenu();
264                     break;
265
266                 case 'D': // Set Device Info
267                 case 'd':
268                     SetDeviceInfo();
269                     PrintMenu();
270                     break;
271                 case 'T': // stop easy setup
272                 case 't':
273                     StopEasySetup();
274                     PrintMenu();
275                     break;
276                 case 'U': // set callback
277                 case 'u':
278                     SetCallbackForUserdata();
279                     PrintMenu();
280                     break;
281
282                 default:
283                     printf("wrong option\n");
284                     PrintMenu();
285                     break;
286             }
287             if (option == 'Q' || option == 'q') { break; }
288         }
289     }
290     return 0;
291 }
292
293 void *listeningFunc(void * data)
294 {
295     (void)data;
296     OCStackResult result = OC_STACK_ERROR;
297
298     while (true)
299     {
300         result = OCProcess();
301         if (result != OC_STACK_OK)
302         {
303            printf("OCStack stop error");
304         }
305     }
306     return NULL;
307 }
308