Merge remote-tracking branch 'origin/master' into notification-service
[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
36 /**
37  * Secure Virtual Resource database for Iotivity Server
38  * It contains Server's Identity and the PSK credentials
39  * of other devices which the server trusts
40  */
41 static char CRED_FILE[] = "oic_svr_db_server.dat";
42
43 OCPersistentStorage ps;
44
45 /**
46  * @var gIsSecured
47  * @brief Variable to check if secure mode is enabled or not.
48  */
49 static bool gIsSecured = false;
50
51 UserProperties g_userProperties;
52
53 void PrintMenu()
54 {
55     printf("============\n");
56     printf("S: Enabled Security\n");
57     printf("I: Init & Start EasySetup\n");
58     printf("D: Set DeviceInfo\n");
59     printf("T: Terminate\n");
60     printf("U: set Callback for userdata\n");
61     printf("Q: Quit\n");
62     printf("============\n");
63 }
64
65 void WiFiProvCbInApp(ESWiFiProvData *eventData)
66 {
67     printf("WiFiProvCbInApp IN\n");
68
69     if(eventData == NULL)
70     {
71         printf("ESWiFiProvData is NULL\n");
72         return ;
73     }
74
75     printf("SSID : %s\n", eventData->ssid);
76     printf("Password : %s\n", eventData->pwd);
77     printf("AuthType : %d\n", eventData->authtype);
78     printf("EncType : %d\n", eventData->enctype);
79
80     if(eventData->userdata != NULL)
81     {
82         printf("userValue : %d\n", ((UserProperties *)(eventData->userdata))->userValue_int);
83     }
84
85     printf("WiFiProvCbInApp OUT\n");
86 }
87
88 void DevConfProvCbInApp(ESDevConfProvData *eventData)
89 {
90     printf("DevConfProvCbInApp IN\n");
91
92     if(eventData == NULL)
93     {
94         printf("ESDevConfProvData is NULL\n");
95         return ;
96     }
97
98     printf("Language : %s\n", eventData->language);
99     printf("Country : %s\n", eventData->country);
100
101     printf("DevConfProvCbInApp OUT\n");
102 }
103
104 void CloudDataProvCbInApp(ESCloudProvData *eventData)
105 {
106     printf("CloudDataProvCbInApp IN\n");
107
108     if(eventData == NULL)
109     {
110         printf("ESCloudProvData is NULL\n");
111         return ;
112     }
113
114     printf("AuthCode : %s\n", eventData->authCode);
115     printf("AuthProvider : %s\n", eventData->authProvider);
116     printf("CI Server : %s\n", eventData->ciServer);
117
118     printf("CloudDataProvCbInApp OUT\n");
119 }
120
121 ESProvisioningCallbacks gCallbacks = {
122     .WiFiProvCb = &WiFiProvCbInApp,
123     .DevConfProvCb = &DevConfProvCbInApp,
124     .CloudDataProvCb = &CloudDataProvCbInApp
125 };
126
127 FILE* server_fopen(const char *path, const char *mode)
128 {
129     (void) path;
130     return fopen(CRED_FILE, mode);
131 }
132
133 void EnableSecurity()
134 {
135     printf("Inside EnableSecurity API..\n");
136
137     gIsSecured = true;
138
139     // Initialize Persistent Storage for SVR database
140     ps = (OCPersistentStorage){ server_fopen, fread, fwrite, fclose, unlink };
141     OCRegisterPersistentStorageHandler(&ps);
142 }
143
144 void StartEasySetup()
145 {
146     printf("StartEasySetup IN\n");
147
148     if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
149     {
150         printf("OCStack init error!!\n");
151         return;
152     }
153
154     ESResourceMask resourcemMask = ES_WIFI_RESOURCE | ES_CLOUD_RESOURCE | ES_DEVCONF_RESOURCE;
155     if(ESInitEnrollee(gIsSecured, resourcemMask, gCallbacks) != ES_OK)
156     {
157         printf("OCStack init error!!\n");
158         return;
159     }
160     printf("ESInitEnrollee Success\n");
161
162     pthread_t thread_handle = NULL;
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     //stop OC Stack
204     if (OCStop() != OC_STACK_OK)
205     {
206         printf("OCStack stop failed!!\n");
207         return;
208     }
209
210     printf("StopEasySetup OUT\n");
211 }
212
213 void SetCallbackForUserdata()
214 {
215     ESSetCallbackForUserdata(&ReadUserdataCb, &WriteUserdataCb);
216 }
217
218 int main()
219 {
220     printf("#########################\n");
221     printf("EasySetup Enrollee SAMPLE\n");
222     printf("#########################\n");
223     PrintMenu();
224     char option;
225
226     while(true)
227     {
228         if(scanf("%c", &option) != 1)
229         {
230             printf("Failed to read input data\n");
231             continue;
232         }
233
234         if(option!= '\n')
235         {
236             switch (option)
237             {
238                 case 'H': // help
239                 case 'h':
240                     PrintMenu();
241                     break;
242
243                 case 'Q': // quit
244                 case 'q':
245                     printf("quit");
246                     break;
247
248                 case 'S': // Enable Security
249                 case 's':
250                     EnableSecurity();
251                     PrintMenu();
252                     break;
253
254                 case 'I': // Init EasySetup
255                 case 'i':
256                     StartEasySetup();
257                     PrintMenu();
258                     break;
259
260                 case 'D': // Set Device Info
261                 case 'd':
262                     SetDeviceInfo();
263                     PrintMenu();
264                     break;
265                 case 'T': // stop easy setup
266                 case 't':
267                     StopEasySetup();
268                     PrintMenu();
269                     break;
270                 case 'U': // set callback
271                 case 'u':
272                     SetCallbackForUserdata();
273                     PrintMenu();
274                     break;
275
276                 default:
277                     printf("wrong option\n");
278                     PrintMenu();
279                     break;
280             }
281             if (option == 'Q' || option == 'q') { break; }
282         }
283     }
284     return 0;
285 }
286
287 void *listeningFunc(void * data)
288 {
289     (void)data;
290     OCStackResult result = OC_STACK_ERROR;
291
292     while (true)
293     {
294         result = OCProcess();
295         if (result != OC_STACK_OK)
296         {
297            printf("OCStack stop error");
298         }
299     }
300     return NULL;
301 }
302