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"
31 #include "register_device.h"
33 #define TAG "ENROLLEE_SAMPLE"
35 void *listeningFunc(void *);
36 pthread_t thread_handle = 0;
39 * Secure Virtual Resource database for Iotivity Server
40 * It contains Server's Identity and the PSK credentials
41 * of other devices which the server trusts
43 static char CRED_FILE[] = "oic_svr_db_server.dat";
45 OCPersistentStorage ps;
49 * @brief Variable to check if secure mode is enabled or not.
51 static bool gIsSecured = false;
53 UserProperties g_userProperties;
57 printf("============\n");
58 printf("S: Enabled Security\n");
59 printf("I: Init & Start EasySetup\n");
60 printf("D: Set DeviceInfo\n");
61 printf("T: Terminate\n");
62 printf("U: set Callback for userdata\n");
64 printf("============\n");
67 void WiFiProvCbInApp(ESWiFiProvData *eventData)
69 printf("WiFiProvCbInApp IN\n");
73 printf("ESWiFiProvData is NULL\n");
77 printf("SSID : %s\n", eventData->ssid);
78 printf("Password : %s\n", eventData->pwd);
79 printf("AuthType : %d\n", eventData->authtype);
80 printf("EncType : %d\n", eventData->enctype);
82 if(eventData->userdata != NULL)
84 printf("userValue : %d\n", ((UserProperties *)(eventData->userdata))->userValue_int);
87 printf("WiFiProvCbInApp OUT\n");
90 void DevConfProvCbInApp(ESDevConfProvData *eventData)
92 printf("DevConfProvCbInApp IN\n");
96 printf("ESDevConfProvData is NULL\n");
100 printf("Language : %s\n", eventData->language);
101 printf("Country : %s\n", eventData->country);
103 printf("DevConfProvCbInApp OUT\n");
106 void CloudDataProvCbInApp(ESCloudProvData *eventData)
108 printf("CloudDataProvCbInApp IN\n");
110 if(eventData == NULL)
112 printf("ESCloudProvData is NULL\n");
116 printf("AuthCode : %s\n", eventData->authCode);
117 printf("AuthProvider : %s\n", eventData->authProvider);
118 printf("CI Server : %s\n", eventData->ciServer);
120 printf("CloudDataProvCbInApp OUT\n");
123 ESProvisioningCallbacks gCallbacks = {&WiFiProvCbInApp, &DevConfProvCbInApp, &CloudDataProvCbInApp};
125 FILE* server_fopen(const char *path, const char *mode)
128 return fopen(CRED_FILE, mode);
131 void EnableSecurity()
133 printf("Inside EnableSecurity API..\n");
137 // Initialize Persistent Storage for SVR database
138 ps = (OCPersistentStorage)
140 server_fopen, fread, fwrite, fclose, unlink
142 OCRegisterPersistentStorageHandler(&ps);
145 void StartEasySetup()
147 printf("StartEasySetup IN\n");
149 if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
151 printf("OCStack init error!!\n");
155 ESResourceMask resourcemMask = ES_WIFI_RESOURCE | ES_CLOUD_RESOURCE | ES_DEVCONF_RESOURCE;
156 if(ESInitEnrollee(gIsSecured, resourcemMask, gCallbacks) != ES_OK)
158 printf("OCStack init error!!\n");
161 printf("ESInitEnrollee Success\n");
162 if (pthread_create(&thread_handle, NULL, listeningFunc, NULL))
164 printf("Thread creation failed\n");
167 printf("StartEasySetup OUT\n");
173 printf("SetDeviceInfo IN\n");
175 ESDeviceProperty deviceProperty =
177 {{WIFI_11G, WIFI_11N, WIFI_11AC, WiFi_EOF}, WIFI_5G}
180 sprintf(deviceProperty.DevConf.deviceName, "Console Device %d", rand()%1000000);
181 sprintf(deviceProperty.DevConf.modelNumber, "%d", rand()%100);
183 printf("\n\nDevice name: %s\nModel number: %s\n\n\n",
184 deviceProperty.DevConf.deviceName,
185 deviceProperty.DevConf.modelNumber);
187 registerDeviceInfoX(deviceProperty.DevConf.deviceName, deviceProperty.DevConf.modelNumber);
189 // Set user properties if needed
190 char userValue_str[] = "user_str";
191 g_userProperties.userValue_int = 0;
192 strcpy(g_userProperties.userValue_str, userValue_str);
193 SetUserProperties(&g_userProperties);
195 if(ESSetDeviceProperty(&deviceProperty) == ES_ERROR)
197 printf("ESSetDeviceProperty Error\n");
200 printf("SetDeviceInfo OUT\n");
205 printf("StopEasySetup IN\n");
207 if (ESTerminateEnrollee() == ES_ERROR)
209 printf("ESTerminateEnrollee Failed!!\n");
213 if (0 != pthread_cancel(thread_handle))
215 printf("Thread cancellation failed\n");
220 if (OCStop() != OC_STACK_OK)
222 printf("OCStack stop failed!!\n");
226 printf("StopEasySetup OUT\n");
229 void SetCallbackForUserdata()
231 ESSetCallbackForUserdata(&ReadUserdataCb, &WriteUserdataCb);
244 if(scanf("%c", &option) != 1)
246 printf("Failed to read input data\n");
259 case 'S': // Enable Security
265 case 'I': // Init EasySetup
271 case 'D': // Set Device Info
276 case 'T': // stop easy setup
281 case 'U': // set callback
283 SetCallbackForUserdata();
288 printf("wrong option\n");
292 if (option == 'Q' || option == 'q')
301 void *listeningFunc(void * data)
304 OCStackResult result = OC_STACK_ERROR;
308 result = OCProcess();
309 if (result != OC_STACK_OK)
311 printf("OCStack stop error");