1 //******************************************************************
3 // Copyright 2016 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_wifi_conn.h"
32 #define TAG "TIZEN_ES"
36 gboolean mainThread(GIOChannel *source, GIOCondition condition, gpointer data);
37 void *listeningFunc(void *);
40 static pthread_t gThreadHandle = 0;
44 * Secure Virtual Resource database for Iotivity Server
45 * It contains Server's Identity and the PSK credentials
46 * of other devices which the server trusts
48 static char CRED_FILE[] = "oic_svr_db_server.dat";
50 OCPersistentStorage ps;
54 * @brief Variable to check if secure mode is enabled or not.
56 static bool gIsSecured = false;
57 static bool gWiFiCBflag = false;
58 static char gSSID[OIC_STRING_MAX_VALUE];
59 static char gPasswd[OIC_STRING_MAX_VALUE];
63 cout << "========================" << endl;
64 cout << "A: Enabled Security" << endl;
65 cout << "B: Init & Start EasySetup" << endl;
66 cout << "C: Set DeviceInfo" << endl;
67 cout << "D: Connect to TargetAP" << endl;
68 cout << "E: Show Menu......." << endl;
69 cout << "Q: Terminate" << endl;
70 cout << "========================" << endl;
73 void ConnectRequestCbInApp(ESConnectRequest *connectRequest)
75 cout << "ConnectRequestCbInApp IN" << endl;
77 if(connectRequest == NULL)
79 cout << "connectRequest is NULL" << endl;
83 for(int i = 0 ; i < connectRequest->numRequest ; ++i)
85 cout << "connect : " << connectRequest->connect[i] << endl;
88 cout << "ConnectRequestCbInApp OUT" << endl;
92 void WiFiConfProvCbInApp(ESWiFiConfData *eventData)
94 cout << "WiFiConfProvCbInApp IN" << endl;
97 ESSetState(ES_STATE_CONNECTING_TO_ENROLLER);
101 cout << "ESWiFiConfData is NULL" << endl;
105 cout << "SSID : " << eventData->ssid << endl;
106 cout << "Password : " << eventData->pwd << endl;
107 cout << "AuthType : " << eventData->authtype << endl;
108 cout << "EncType : " << eventData->enctype << endl;
110 memset(gSSID, 0, OIC_STRING_MAX_VALUE);
111 memset(gPasswd, 0, OIC_STRING_MAX_VALUE);
112 if(eventData->ssid != NULL)
114 strncpy(gSSID, eventData->ssid, strlen(eventData->ssid));
117 if(eventData->pwd != NULL)
119 strncpy(gPasswd, eventData->pwd, strlen(eventData->pwd));
122 cout << "WiFiConfProvCbInApp OUT" << endl;
126 void DevConfProvCbInApp(ESDevConfData *eventData)
128 cout << "DevConfProvCbInApp IN" << endl;
130 if(eventData == NULL)
132 cout << "ESDevConfData is NULL" << endl;
136 cout << "DevConfProvCbInApp OUT" << endl;
140 void CoapCloudConfProvCbInApp(ESCoapCloudConfData *eventData)
142 cout << "CoapCloudConfProvCbInApp IN" << endl;
144 if(eventData == NULL)
146 cout << "ESCoapCloudConfData is NULL" << endl;
150 cout << "AuthCode : " << eventData->authCode << endl;
151 cout << "AuthProvider : " << eventData->authProvider << endl;
152 cout << "CI Server : " << eventData->ciServer << endl;
153 cout << "CoapCloudConfProvCbInApp OUT" << endl;
157 ESProvisioningCallbacks gCallbacks = {
158 .ConnectRequestCb = &ConnectRequestCbInApp,
159 .WiFiConfProvCb = &WiFiConfProvCbInApp,
160 .DevConfProvCb = &DevConfProvCbInApp,
161 .CoapCloudConfProvCb = &CoapCloudConfProvCbInApp
164 FILE* server_fopen(const char *path, const char *mode)
167 return fopen(CRED_FILE, mode);
170 void EnableSecurity()
172 printf("Inside EnableSecurity API..\n");
176 // Initialize Persistent Storage for SVR database
177 ps = (OCPersistentStorage){ server_fopen, fread, fwrite, fclose, unlink };
178 OCRegisterPersistentStorageHandler(&ps);
181 void StartEasySetup()
183 cout << "StartEasySetup IN" << endl;
185 if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
187 cout << "OCStack init error!!" << endl;
191 ESResourceMask resourcemMask = (ESResourceMask) (ES_WIFICONF_RESOURCE | ES_COAPCLOUDCONF_RESOURCE | ES_DEVCONF_RESOURCE);
192 cout << "resourcemMask : " << resourcemMask << endl;
193 if(ESInitEnrollee(gIsSecured, resourcemMask, gCallbacks) != ES_OK)
195 cout << "OCStack init error!!" << endl;
199 cout << "ESInitEnrollee Success" << endl;
201 if(gThreadHandle == 0) {
203 if (pthread_create(&gThreadHandle, NULL, listeningFunc, NULL)) {
204 cout << "Thread creation failed" << endl;
210 ESSetState(ES_STATE_INIT);
211 ESSetErrorCode(ES_ERRCODE_NO_ERROR);
215 ret = TizenWiFiInit();
216 if(ret != WIFI_NO_ERROR) {
217 cout << "WiFi Init Error" << endl;
220 cout << "WiFi Init Success" << endl;
222 ret = TizenWiFiScanStart();
223 if(ret != WIFI_NO_ERROR) {
224 cout << "WiFi Scan Error" << endl;
227 cout << "WiFi Scan Success" << endl;
229 cout << "StartEasySetup OUT" << endl;
234 cout << "SetDeviceInfo IN" << endl;
236 ESDeviceProperty deviceProperty = {
237 {{WIFI_11G, WIFI_11N, WIFI_11AC, WiFi_EOF}, WIFI_5G}, {"Tizen Device"}
240 if(ESSetDeviceProperty(&deviceProperty) == ES_ERROR)
242 cout << "ESSetDeviceProperty Error" << endl;
245 // Set user properties if needed
247 cout << "SetDeviceInfo OUT" << endl;
252 cout << "StopEasySetup IN" << endl;
253 if (ESTerminateEnrollee() == ES_ERROR)
255 cout << "ESTerminateEnrollee Failed!!" << endl;
263 if (OCStop() != OC_STACK_OK)
265 cout << "OCStack stop failed!!" << endl;
269 if(gThreadHandle != 0) {
270 pthread_cancel(gThreadHandle);
271 pthread_join(gThreadHandle, NULL);
274 cout << "StopEasySetup OUT" << endl;
277 void ConnectToTargetAP()
280 cout << "WiFi Provisioning is needed to be preceded" << endl;
284 WiFiConnErrCode ret = WIFI_NO_ERROR;
285 ret = TizenWiFiScanStart();
286 if(ret != WIFI_NO_ERROR){
287 ESSetState(ES_STATE_FAILED_TO_CONNECT_TO_ENROLLER);
288 ESSetErrorCode(ES_ERRCODE_UNKNOWN);
289 cout << "WiFi Scan Error" << endl;
293 cout << "WiFi Scan Succss" << endl;
296 ret = TizenWiFiConn(gSSID, gPasswd);
297 if(ret != WIFI_NO_ERROR) {
298 ESSetState(ES_STATE_FAILED_TO_CONNECT_TO_ENROLLER);
300 if(ret == WIFI_NOTFOUND_SSID_ERROR) {
301 ESSetErrorCode(ES_ERRCODE_SSID_NOT_FOUND);
303 else if(ret == WIFI_WRONG_PWD_ERROR) {
304 ESSetErrorCode(ES_ERRCODE_PW_WRONG);
307 ESSetErrorCode(ES_ERRCODE_TIMEOUT);
309 cout << "WiFi Connection Error" << endl;
313 cout << "WIFI Connection Success" << endl;
314 ESSetState(ES_STATE_CONNECTED_TO_ENROLLER);
315 ESSetErrorCode(ES_ERRCODE_NO_ERROR);
319 gboolean mainThread(GIOChannel *source, GIOCondition condition, gpointer data)
323 cin.ignore(numeric_limits<streamsize>::max(), '\n');
331 case 'A': // Enable Security
336 cout << "Sample is not built with secured mode" << endl;
341 case 'B': // Init EasySetup
347 case 'C': // Set Device Info
353 case 'D': // Start to connect target AP
359 case 'E': // Print Menu
365 cout << "Wrong option" << endl;
370 if(entered == 'q' || entered == 'Q'){
371 g_main_loop_quit(gMainloop);
379 cout << "#########################" << endl;
380 cout << "EasySetup Enrollee SAMPLE" << endl;
381 cout << "#########################" << endl;
384 gMainloop = g_main_loop_new (NULL, FALSE);
385 if(gMainloop == NULL) {
386 cout << "Create Main Loop Error" << endl;
390 GIOChannel *channel = g_io_channel_unix_new(0);
391 g_io_add_watch(channel, (GIOCondition)(G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL), mainThread, NULL);
393 g_main_loop_run (gMainloop);
395 cout << "#########################" << endl;
396 cout << "Terminate Enrollee SAMPLE" << endl;
397 cout << "#########################" << endl;
402 void *listeningFunc(void*)
404 OCStackResult result;
408 result = OCProcess();
409 if (result != OC_STACK_OK)
411 cout<<"OCStack stop error";