df91cd0633d4940c2622f0b7225df0a9421417d1
[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         printf("[SC] SSO List : %s\n", data->ssoList);
111     }
112
113     printf("DevConfProvCbInApp OUT\n");
114 }
115
116 void CloudDataProvCbInApp(ESCoapCloudConfData *eventData)
117 {
118     printf("CloudDataProvCbInApp IN\n");
119
120     if(eventData == NULL)
121     {
122         printf("ESCloudProvData is NULL\n");
123         return ;
124     }
125
126     printf("AuthCode : %s\n", eventData->authCode);
127     printf("AuthProvider : %s\n", eventData->authProvider);
128     printf("CI Server : %s\n", eventData->ciServer);
129
130     if(eventData->userdata != NULL)
131     {
132         SCCoapCloudServerConfProperties *data = eventData->userdata;
133         printf("[SC] ClientID : %s\n", data->clientID);
134     }
135
136     printf("CloudDataProvCbInApp OUT\n");
137 }
138
139 ESProvisioningCallbacks gCallbacks = {
140     .WiFiConfProvCb = &WiFiProvCbInApp,
141     .DevConfProvCb = &DevConfProvCbInApp,
142     .CoapCloudConfProvCb = &CloudDataProvCbInApp
143 };
144
145 FILE* server_fopen(const char *path, const char *mode)
146 {
147     (void) path;
148     return fopen(CRED_FILE, mode);
149 }
150
151 void EnableSecurity()
152 {
153     printf("Inside EnableSecurity API..\n");
154
155     gIsSecured = true;
156
157     // Initialize Persistent Storage for SVR database
158     ps = (OCPersistentStorage){ server_fopen, fread, fwrite, fclose, unlink };
159     OCRegisterPersistentStorageHandler(&ps);
160 }
161
162 void StartEasySetup()
163 {
164     printf("StartEasySetup IN\n");
165
166     if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
167     {
168         printf("OCStack init error!!\n");
169         return;
170     }
171
172     ESResourceMask resourcemMask = ES_WIFICONF_RESOURCE | ES_COAPCLOUDCONF_RESOURCE | ES_DEVCONF_RESOURCE;
173     if(ESInitEnrollee(gIsSecured, resourcemMask, gCallbacks) != ES_OK)
174     {
175         printf("OCStack init error!!\n");
176         return;
177     }
178     printf("ESInitEnrollee Success\n");
179
180     // Set callbacks for Samsung
181     ESSetCallbackForUserdata(&ReadUserdataCb, &WriteUserdataCb);
182
183     pthread_t thread_handle;
184     if (pthread_create(&thread_handle, NULL, listeningFunc, NULL))
185     {
186         printf("Thread creation failed\n");
187     }
188
189     printf("StartEasySetup OUT\n");
190 }
191
192 void SetDeviceInfo()
193 {
194     printf("SetDeviceInfo IN\n");
195
196     ESDeviceProperty deviceProperty = {
197         {{WIFI_11G, WIFI_11N, WIFI_11AC, WiFi_EOF}, WIFI_5G},
198             {"Test Device"}
199     };
200
201     if(ESSetDeviceProperty(&deviceProperty) == ES_ERROR)
202         printf("ESSetDeviceProperty Error\n");
203
204     // Set user properties if needed
205     strncpy(g_SCProperties.deviceType, "deviceType", MAXLEN_STRING);
206     strncpy(g_SCProperties.deviceSubType, "deviceSubType", MAXLEN_STRING);
207     g_SCProperties.netConnectionState = NET_STATE_INIT;
208     g_SCProperties.discoveryChannel = WIFI_DISCOVERY_CHANNEL_INIT;
209     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);
210     strncpy(g_SCProperties.nwProvInfo, "{\"IMEI\":\"123456789012345 / 01\",\"IMSI\":\"123401234567890\",\"MCC_MNC\":\"100_10\",\"SN\":\"XY0123456XYZ\"}", MAXLEN_STRING);
211     strncpy(g_SCProperties.pnpPin, "pinNumber", MAXLEN_STRING);
212     strncpy(g_SCProperties.modelNumber, "Model Number", MAXLEN_STRING);
213     strncpy(g_SCProperties.esProtocolVersion, "2.0", MAXLEN_STRING);
214
215     if(SetSCProperties(&g_SCProperties) == ES_ERROR)
216         printf("SetSCProperties Error\n");
217
218     printf("SetDeviceInfo OUT\n");
219 }
220
221 void StopEasySetup()
222 {
223     printf("StopEasySetup IN\n");
224
225     if (ESTerminateEnrollee() == ES_ERROR)
226     {
227         printf("ESTerminateEnrollee Failed!!\n");
228         return;
229     }
230
231     //stop OC Stack
232     if (OCStop() != OC_STACK_OK)
233     {
234         printf("OCStack stop failed!!\n");
235         return;
236     }
237
238     printf("StopEasySetup OUT\n");
239 }
240
241 int main()
242 {
243     printf("#########################\n");
244     printf("EasySetup Enrollee SAMPLE\n");
245     printf("#########################\n");
246     PrintMenu();
247     char option;
248
249     while(true)
250     {
251         if(scanf("%c", &option) != 1)
252         {
253             printf("Failed to read input data\n");
254             continue;
255         }
256
257         if(option!= '\n')
258         {
259             switch (option)
260             {
261                 case 'H': // help
262                 case 'h':
263                     PrintMenu();
264                     break;
265
266                 case 'Q': // quit
267                 case 'q':
268                     printf("quit");
269                     break;
270
271                 case 'S': // Enable Security
272                 case 's':
273                     EnableSecurity();
274                     PrintMenu();
275                     break;
276
277                 case 'I': // Init EasySetup
278                 case 'i':
279                     StartEasySetup();
280                     PrintMenu();
281                     break;
282
283                 case 'D': // Set Device Info
284                 case 'd':
285                     SetDeviceInfo();
286                     PrintMenu();
287                     break;
288                 case 'T': // stop easy setup
289                 case 't':
290                     StopEasySetup();
291                     PrintMenu();
292                     break;
293                 default:
294                     printf("wrong option\n");
295                     PrintMenu();
296                     break;
297             }
298             if (option == 'Q' || option == 'q') break;
299         }
300     }
301     return 0;
302 }
303
304 void *listeningFunc(void * data)
305 {
306     (void)data;
307     OCStackResult result;
308
309     while (true)
310     {
311         result = OCProcess();
312         if (result != OC_STACK_OK)
313         {
314            printf("OCStack stop error");
315         }
316     }
317     return NULL;
318 }
319