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 *);
35 pthread_t thread_handle = NULL;
38 * Secure Virtual Resource database for Iotivity Server
39 * It contains Server's Identity and the PSK credentials
40 * of other devices which the server trusts
42 static char CRED_FILE[] = "oic_svr_db_server.dat";
44 OCPersistentStorage ps;
48 * @brief Variable to check if secure mode is enabled or not.
50 static bool gIsSecured = false;
52 UserProperties g_userProperties;
56 printf("============\n");
57 printf("S: Enabled Security\n");
58 printf("I: Init & Start EasySetup\n");
59 printf("D: Set DeviceInfo\n");
60 printf("T: Terminate\n");
61 printf("U: set Callback for userdata\n");
63 printf("============\n");
66 void WiFiProvCbInApp(ESWiFiProvData *eventData)
68 printf("WiFiProvCbInApp IN\n");
72 printf("ESWiFiProvData is NULL\n");
76 printf("SSID : %s\n", eventData->ssid);
77 printf("Password : %s\n", eventData->pwd);
78 printf("AuthType : %d\n", eventData->authtype);
79 printf("EncType : %d\n", eventData->enctype);
81 if(eventData->userdata != NULL)
83 printf("userValue : %d\n", ((UserProperties *)(eventData->userdata))->userValue_int);
86 printf("WiFiProvCbInApp OUT\n");
89 void DevConfProvCbInApp(ESDevConfProvData *eventData)
91 printf("DevConfProvCbInApp IN\n");
95 printf("ESDevConfProvData is NULL\n");
99 printf("Language : %s\n", eventData->language);
100 printf("Country : %s\n", eventData->country);
102 printf("DevConfProvCbInApp OUT\n");
105 void CloudDataProvCbInApp(ESCloudProvData *eventData)
107 printf("CloudDataProvCbInApp IN\n");
109 if(eventData == NULL)
111 printf("ESCloudProvData is NULL\n");
115 printf("AuthCode : %s\n", eventData->authCode);
116 printf("AuthProvider : %s\n", eventData->authProvider);
117 printf("CI Server : %s\n", eventData->ciServer);
119 printf("CloudDataProvCbInApp OUT\n");
122 ESProvisioningCallbacks gCallbacks = {
123 .WiFiProvCb = &WiFiProvCbInApp,
124 .DevConfProvCb = &DevConfProvCbInApp,
125 .CloudDataProvCb = &CloudDataProvCbInApp
128 FILE* server_fopen(const char *path, const char *mode)
131 return fopen(CRED_FILE, mode);
134 void EnableSecurity()
136 printf("Inside EnableSecurity API..\n");
140 // Initialize Persistent Storage for SVR database
141 ps = (OCPersistentStorage){ 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");
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", "Test Model Number"}
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");
203 if (0 != pthread_cancel(thread_handle))
205 printf("Thread cancellation failed\n");
210 if (OCStop() != OC_STACK_OK)
212 printf("OCStack stop failed!!\n");
216 printf("StopEasySetup OUT\n");
219 void SetCallbackForUserdata()
221 ESSetCallbackForUserdata(&ReadUserdataCb, &WriteUserdataCb);
226 printf("#########################\n");
227 printf("EasySetup Enrollee SAMPLE\n");
228 printf("#########################\n");
234 if(scanf("%c", &option) != 1)
236 printf("Failed to read input data\n");
254 case 'S': // Enable Security
260 case 'I': // Init EasySetup
266 case 'D': // Set Device Info
271 case 'T': // stop easy setup
276 case 'U': // set callback
278 SetCallbackForUserdata();
283 printf("wrong option\n");
287 if (option == 'Q' || option == 'q') { break; }
293 void *listeningFunc(void * data)
296 OCStackResult result = OC_STACK_ERROR;
300 result = OCProcess();
301 if (result != OC_STACK_OK)
303 printf("OCStack stop error");