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