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
43 static char CRED_FILE[] = "oic_svr_db_server_MOT.dat";
45 static char CRED_FILE[] = "oic_svr_db_server.dat";
48 OCPersistentStorage ps;
52 * @brief Variable to check if secure mode is enabled or not.
54 static bool gIsSecured = false;
56 UserProperties g_userProperties;
60 printf("============\n");
61 printf("S: Enabled Security\n");
62 printf("I: Init & Start EasySetup\n");
63 printf("D: Set DeviceInfo\n");
64 printf("T: Terminate\n");
65 printf("U: set Callback for userdata\n");
67 printf("============\n");
70 void ConnectRequestCbInApp(ESConnectRequest *connectRequest)
72 printf("ConnectRequestCbInApp IN\n");
74 if(connectRequest == NULL)
76 printf("connectRequest is NULL\n");
80 for(int i = 0 ; i < connectRequest->numRequest ; ++i)
82 printf("connect : %d\n", connectRequest->connect[i]);
85 printf("ConnectRequestCbInApp OUT\n");
88 void WiFiConfProvCbInApp(ESWiFiConfData *eventData)
90 printf("WiFiConfProvCbInApp IN\n");
94 printf("ESWiFiConfData is NULL\n");
98 printf("SSID : %s\n", eventData->ssid);
99 printf("Password : %s\n", eventData->pwd);
100 printf("AuthType : %d\n", eventData->authtype);
101 printf("EncType : %d\n", eventData->enctype);
103 if(eventData->userdata != NULL)
105 printf("userValue : %d\n", ((UserProperties *)(eventData->userdata))->userValue_int);
108 printf("WiFiConfProvCbInApp OUT\n");
111 void DevConfProvCbInApp(ESDevConfData *eventData)
113 printf("DevConfProvCbInApp IN\n");
115 if(eventData == NULL)
117 printf("ESDevConfData is NULL\n");
121 printf("DevConfProvCbInApp OUT\n");
124 void CoapCloudConfProvCbInApp(ESCoapCloudConfData *eventData)
126 printf("CoapCloudConfProvCbInApp IN\n");
128 if(eventData == NULL)
130 printf("ESCoapCloudConfData is NULL\n");
134 printf("AuthCode : %s\n", eventData->authCode);
135 printf("AcessToken : %s\n", eventData->accessToken);
136 printf("AcessTokenType : %d\n", eventData->accessTokenType);
137 printf("AuthProvider : %s\n", eventData->authProvider);
138 printf("CI Server : %s\n", eventData->ciServer);
140 printf("CoapCloudConfProvCbInApp OUT\n");
143 ESProvisioningCallbacks gCallbacks = {
144 .ConnectRequestCb = &ConnectRequestCbInApp,
145 .WiFiConfProvCb = &WiFiConfProvCbInApp,
146 .DevConfProvCb = &DevConfProvCbInApp,
147 .CoapCloudConfProvCb = &CoapCloudConfProvCbInApp
150 FILE* server_fopen(const char *path, const char *mode)
153 return fopen(CRED_FILE, mode);
156 void EnableSecurity()
158 printf("Inside EnableSecurity API..\n");
162 // Initialize Persistent Storage for SVR database
163 ps = (OCPersistentStorage){ server_fopen, fread, fwrite, fclose, unlink };
164 OCRegisterPersistentStorageHandler(&ps);
167 void StartEasySetup()
169 printf("StartEasySetup IN\n");
171 if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
173 printf("OCStack init error!!\n");
177 ESResourceMask resourcemMask = ES_WIFICONF_RESOURCE | ES_COAPCLOUDCONF_RESOURCE | ES_DEVCONF_RESOURCE;
178 if(ESInitEnrollee(gIsSecured, resourcemMask, gCallbacks) != ES_OK)
180 printf("OCStack init error!!\n");
183 printf("ESInitEnrollee Success\n");
185 if (pthread_create(&thread_handle, NULL, listeningFunc, NULL))
187 printf("Thread creation failed\n");
190 printf("StartEasySetup OUT\n");
195 printf("SetDeviceInfo IN\n");
197 ESDeviceProperty deviceProperty = {
198 {{WIFI_11G, WIFI_11N, WIFI_11AC, WiFi_EOF}, WIFI_5G}, {"Test Device"}
201 // Set user properties if needed
202 char userValue_str[] = "user_str";
203 g_userProperties.userValue_int = 0;
205 strncpy(g_userProperties.userValue_str, userValue_str, strlen(userValue_str) + 1);
206 SetUserProperties(&g_userProperties);
208 if(ESSetDeviceProperty(&deviceProperty) == ES_ERROR)
210 printf("ESSetDeviceProperty Error\n");
213 printf("SetDeviceInfo OUT\n");
218 printf("StopEasySetup IN\n");
220 if (ESTerminateEnrollee() == ES_ERROR)
222 printf("ESTerminateEnrollee Failed!!\n");
226 if (0 != pthread_cancel(thread_handle))
228 printf("Thread cancellation failed\n");
233 if (OCStop() != OC_STACK_OK)
235 printf("OCStack stop failed!!\n");
239 printf("StopEasySetup OUT\n");
242 void SetCallbackForUserdata()
244 ESSetCallbackForUserdata(&ReadUserdataCb, &WriteUserdataCb);
249 printf("#########################\n");
250 printf("EasySetup Enrollee SAMPLE\n");
251 printf("#########################\n");
257 if(scanf("%c", &option) != 1)
259 printf("Failed to read input data\n");
277 case 'S': // Enable Security
283 case 'I': // Init EasySetup
289 case 'D': // Set Device Info
294 case 'T': // stop easy setup
299 case 'U': // set callback
301 SetCallbackForUserdata();
306 printf("wrong option\n");
310 if (option == 'Q' || option == 'q')
319 void *listeningFunc(void * data)
322 OCStackResult result = OC_STACK_ERROR;
326 result = OCProcess();
327 if (result != OC_STACK_OK)
329 printf("OCProcess error");
332 // Sleep for 100 ms to avoid high CPU Utilization.
333 usleep(100 * 1000); // 100 milli-seconds