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 WiFiProvCbInApp(ESWiFiProvData *eventData)
75 cout << "WiFiProvCbInApp IN" << endl;
78 ESSetState(ES_STATE_CONNECTING_TO_ENROLLER);
82 cout << "ESWiFiProvData is NULL" << endl;
86 cout << "SSID : " << eventData->ssid << endl;
87 cout << "Password : " << eventData->pwd << endl;
88 cout << "AuthType : " << eventData->authtype << endl;
89 cout << "EncType : " << eventData->enctype << endl;
91 memset(gSSID, 0, OIC_STRING_MAX_VALUE);
92 memset(gPasswd, 0, OIC_STRING_MAX_VALUE);
93 if(eventData->ssid != NULL)
95 strncpy(gSSID, eventData->ssid, strlen(eventData->ssid));
98 if(eventData->pwd != NULL)
100 strncpy(gPasswd, eventData->pwd, strlen(eventData->pwd));
103 cout << "WiFiProvCbInApp OUT" << endl;
107 void DevConfProvCbInApp(ESDevConfProvData *eventData)
109 cout << "DevConfProvCbInApp IN" << endl;
111 if(eventData == NULL)
113 cout << "ESDevConfProvData is NULL" << endl;
117 cout << "Language : " << eventData->language << endl;
118 cout << "Country : " << eventData->country << endl;
119 cout << "DevConfProvCbInApp OUT" << endl;
123 void CloudDataProvCbInApp(ESCloudProvData *eventData)
125 cout << "CloudDataProvCbInApp IN" << endl;
127 if(eventData == NULL)
129 cout << "ESCloudProvData is NULL" << endl;
133 cout << "AuthCode : " << eventData->authCode << endl;
134 cout << "AuthProvider : " << eventData->authProvider << endl;
135 cout << "CI Server : " << eventData->ciServer << endl;
136 cout << "CloudDataProvCbInApp OUT" << endl;
140 ESProvisioningCallbacks gCallbacks = {
141 .WiFiProvCb = &WiFiProvCbInApp,
142 .DevConfProvCb = &DevConfProvCbInApp,
143 .CloudDataProvCb = &CloudDataProvCbInApp
146 FILE* server_fopen(const char *path, const char *mode)
149 return fopen(CRED_FILE, mode);
152 void EnableSecurity()
154 printf("Inside EnableSecurity API..\n");
158 // Initialize Persistent Storage for SVR database
159 ps = (OCPersistentStorage){ server_fopen, fread, fwrite, fclose, unlink };
160 OCRegisterPersistentStorageHandler(&ps);
163 void StartEasySetup()
165 cout << "StartEasySetup IN" << endl;
167 if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
169 cout << "OCStack init error!!" << endl;
173 ESResourceMask resourcemMask = (ESResourceMask) (ES_WIFI_RESOURCE | ES_CLOUD_RESOURCE | ES_DEVCONF_RESOURCE);
174 cout << "resourcemMask : " << resourcemMask << endl;
175 if(ESInitEnrollee(gIsSecured, resourcemMask, gCallbacks) != ES_OK)
177 cout << "OCStack init error!!" << endl;
181 cout << "ESInitEnrollee Success" << endl;
183 if(gThreadHandle == 0) {
185 if (pthread_create(&gThreadHandle, NULL, listeningFunc, NULL)) {
186 cout << "Thread creation failed" << endl;
192 ESSetState(ES_STATE_INIT);
193 ESSetErrorCode(ES_ERRCODE_NO_ERROR);
197 ret = TizenWiFiInit();
198 if(ret != WIFI_NO_ERROR) {
199 cout << "WiFi Init Error" << endl;
202 cout << "WiFi Init Success" << endl;
204 ret = TizenWiFiScanStart();
205 if(ret != WIFI_NO_ERROR) {
206 cout << "WiFi Scan Error" << endl;
209 cout << "WiFi Scan Success" << endl;
211 cout << "StartEasySetup OUT" << endl;
216 cout << "SetDeviceInfo IN" << endl;
218 ESDeviceProperty deviceProperty = {
219 {{WIFI_11G, WIFI_11N, WIFI_11AC, WiFi_EOF}, WIFI_5G}, {"Tizen Device"}
222 if(ESSetDeviceProperty(&deviceProperty) == ES_ERROR)
224 cout << "ESSetDeviceProperty Error" << endl;
227 // Set user properties if needed
229 cout << "SetDeviceInfo OUT" << endl;
234 cout << "StopEasySetup IN" << endl;
235 if (ESTerminateEnrollee() == ES_ERROR)
237 cout << "ESTerminateEnrollee Failed!!" << endl;
245 if (OCStop() != OC_STACK_OK)
247 cout << "OCStack stop failed!!" << endl;
251 if(gThreadHandle != 0) {
252 pthread_cancel(gThreadHandle);
253 pthread_join(gThreadHandle, NULL);
256 cout << "StopEasySetup OUT" << endl;
259 void ConnectToTargetAP()
262 cout << "WiFi Provisioning is needed to be preceded" << endl;
266 WiFiConnErrCode ret = WIFI_NO_ERROR;
267 ret = TizenWiFiScanStart();
268 if(ret != WIFI_NO_ERROR){
269 ESSetState(ES_STATE_CONNECTED_FAIL_TO_ENROLLER);
270 ESSetErrorCode(ES_ERRCODE_UNKNOWN);
271 cout << "WiFi Scan Error" << endl;
275 cout << "WiFi Scan Succss" << endl;
278 ret = TizenWiFiConn(gSSID, gPasswd);
279 if(ret != WIFI_NO_ERROR) {
280 ESSetState(ES_STATE_CONNECTED_FAIL_TO_ENROLLER);
282 if(ret == WIFI_NOTFOUND_SSID_ERROR) {
283 ESSetErrorCode(ES_ERRCODE_SSID_NOT_FOUND);
285 else if(ret == WIFI_WRONG_PWD_ERROR) {
286 ESSetErrorCode(ES_ERRCODE_PW_WRONG);
289 ESSetErrorCode(ES_ERRCODE_TIMEOUT);
291 cout << "WiFi Connection Error" << endl;
295 cout << "WIFI Connection Success" << endl;
296 ESSetState(ES_STATE_CONNECTED_TO_ENROLLER);
297 ESSetErrorCode(ES_ERRCODE_NO_ERROR);
301 gboolean mainThread(GIOChannel *source, GIOCondition condition, gpointer data)
305 cin.ignore(numeric_limits<streamsize>::max(), '\n');
313 case 'A': // Enable Security
318 cout << "Sample is not built with secured mode" << endl;
323 case 'B': // Init EasySetup
329 case 'C': // Set Device Info
335 case 'D': // Start to connect target AP
341 case 'E': // Print Menu
347 cout << "Wrong option" << endl;
352 if(entered == 'q' || entered == 'Q'){
353 g_main_loop_quit(gMainloop);
361 cout << "#########################" << endl;
362 cout << "EasySetup Enrollee SAMPLE" << endl;
363 cout << "#########################" << endl;
366 gMainloop = g_main_loop_new (NULL, FALSE);
367 if(gMainloop == NULL) {
368 cout << "Create Min Loop Error" << endl;
372 GIOChannel *channel = g_io_channel_unix_new(0);
373 g_io_add_watch(channel, (GIOCondition)(G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL), mainThread, NULL);
375 g_main_loop_run (gMainloop);
377 cout << "#########################" << endl;
378 cout << "Terminate Enrollee SAMPLE" << endl;
379 cout << "#########################" << endl;
384 void *listeningFunc(void*)
386 OCStackResult result;
390 result = OCProcess();
391 if (result != OC_STACK_OK)
393 cout<<"OCStack stop error";