1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 #include "easysetup.h"
23 #include "easysetup_x.h"
29 #include <oic_string.h>
30 #include "oic_malloc.h"
32 #define TAG "ENROLLEE_SAMPLE"
34 void *listeningFunc(void *);
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
41 static char CRED_FILE[] = "oic_svr_db_server.dat";
43 OCPersistentStorage ps;
47 * @brief Variable to check if secure mode is enabled or not.
49 static bool gIsSecured = false;
51 UserProperties g_userProperties;
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");
62 printf("============\n");
65 void WiFiProvCbInApp(ESWiFiProvData *eventData)
67 printf("WiFiProvCbInApp IN\n");
71 printf("ESWiFiProvData is NULL\n");
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);
80 if(eventData->userdata != NULL)
82 printf("userValue : %d\n", ((UserProperties *)(eventData->userdata))->userValue_int);
85 printf("WiFiProvCbInApp OUT\n");
88 void DevConfProvCbInApp(ESDevConfProvData *eventData)
90 printf("DevConfProvCbInApp IN\n");
94 printf("ESDevConfProvData is NULL\n");
98 printf("Language : %s\n", eventData->language);
99 printf("Country : %s\n", eventData->country);
101 printf("DevConfProvCbInApp OUT\n");
104 void CloudDataProvCbInApp(ESCloudProvData *eventData)
106 printf("CloudDataProvCbInApp IN\n");
108 if(eventData == NULL)
110 printf("ESCloudProvData is NULL\n");
114 printf("AuthCode : %s\n", eventData->authCode);
115 printf("AuthProvider : %s\n", eventData->authProvider);
116 printf("CI Server : %s\n", eventData->ciServer);
118 printf("CloudDataProvCbInApp OUT\n");
121 ESProvisioningCallbacks gCallbacks = {
122 .WiFiProvCb = &WiFiProvCbInApp,
123 .DevConfProvCb = &DevConfProvCbInApp,
124 .CloudDataProvCb = &CloudDataProvCbInApp
127 FILE* server_fopen(const char *path, const char *mode)
130 return fopen(CRED_FILE, mode);
133 void EnableSecurity()
135 printf("Inside EnableSecurity API..\n");
139 // Initialize Persistent Storage for SVR database
140 ps = (OCPersistentStorage){ server_fopen, fread, fwrite, fclose, unlink };
141 OCRegisterPersistentStorageHandler(&ps);
144 void StartEasySetup()
146 printf("StartEasySetup IN\n");
148 if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
150 printf("OCStack init error!!\n");
154 ESResourceMask resourcemMask = ES_WIFI_RESOURCE | ES_CLOUD_RESOURCE | ES_DEVCONF_RESOURCE;
155 if(ESInitEnrollee(gIsSecured, resourcemMask, gCallbacks) != ES_OK)
157 printf("OCStack init error!!\n");
160 printf("ESInitEnrollee Success\n");
162 pthread_t thread_handle = NULL;
163 if (pthread_create(&thread_handle, NULL, listeningFunc, NULL))
165 printf("Thread creation failed\n");
168 printf("StartEasySetup OUT\n");
173 printf("SetDeviceInfo IN\n");
175 ESDeviceProperty deviceProperty = {
176 {{WIFI_11G, WIFI_11N, WIFI_11AC, WiFi_EOF}, WIFI_5G}, {"Test Device"}
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);
185 if(ESSetDeviceProperty(&deviceProperty) == ES_ERROR)
187 printf("ESSetDeviceProperty Error\n");
190 printf("SetDeviceInfo OUT\n");
195 printf("StopEasySetup IN\n");
197 if (ESTerminateEnrollee() == ES_ERROR)
199 printf("ESTerminateEnrollee Failed!!\n");
204 if (OCStop() != OC_STACK_OK)
206 printf("OCStack stop failed!!\n");
210 printf("StopEasySetup OUT\n");
213 void SetCallbackForUserdata()
215 ESSetCallbackForUserdata(&ReadUserdataCb, &WriteUserdataCb);
220 printf("#########################\n");
221 printf("EasySetup Enrollee SAMPLE\n");
222 printf("#########################\n");
228 if(scanf("%c", &option) != 1)
230 printf("Failed to read input data\n");
248 case 'S': // Enable Security
254 case 'I': // Init EasySetup
260 case 'D': // Set Device Info
265 case 'T': // stop easy setup
270 case 'U': // set callback
272 SetCallbackForUserdata();
277 printf("wrong option\n");
281 if (option == 'Q' || option == 'q') { break; }
287 void *listeningFunc(void * data)
290 OCStackResult result = OC_STACK_ERROR;
294 result = OCProcess();
295 if (result != OC_STACK_OK)
297 printf("OCStack stop error");