replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / easy-setup / sampleapp / enrollee / linux / enrolleewifi.c
old mode 100644 (file)
new mode 100755 (executable)
index de21486..ccc096a
 
 
 #include "easysetup.h"
+#include "easysetup_x.h"
 
 #include <unistd.h>
 #include <string.h>
 #include <stdio.h>
 #include <pthread.h>
 #include <oic_string.h>
+#include "oic_malloc.h"
 
 #define TAG "ENROLLEE_SAMPLE"
 
 void *listeningFunc(void *);
-
+pthread_t thread_handle = NULL;
 
 /**
  * Secure Virtual Resource database for Iotivity Server
  * It contains Server's Identity and the PSK credentials
  * of other devices which the server trusts
  */
+#ifdef MULTIPLE_OWNER
+static char CRED_FILE[] = "oic_svr_db_server_MOT.dat";
+#else
 static char CRED_FILE[] = "oic_svr_db_server.dat";
+#endif
 
-OCPersistentStorage ps ;
-
+OCPersistentStorage ps;
 
 /**
  * @var gIsSecured
@@ -48,6 +53,8 @@ OCPersistentStorage ps ;
  */
 static bool gIsSecured = false;
 
+UserProperties g_userProperties;
+
 void PrintMenu()
 {
     printf("============\n");
@@ -55,128 +62,89 @@ void PrintMenu()
     printf("I: Init & Start EasySetup\n");
     printf("D: Set DeviceInfo\n");
     printf("T: Terminate\n");
+    printf("U: set Callback for userdata\n");
     printf("Q: Quit\n");
     printf("============\n");
 }
 
-void WiFiProvCbInApp(ESWiFiProvData *eventData)
+void ConnectRequestCbInApp(ESConnectRequest *connectRequest)
 {
-    printf("WiFiProvCbInApp IN\n");
+    printf("ConnectRequestCbInApp IN\n");
 
-    if(eventData->ssid != NULL)
-    {
-        printf("SSID : %s\n", eventData->ssid);
-    }
-    else
+    if(connectRequest == NULL)
     {
-        printf("ERROR! SSID IS NULL\n");
-        return;
+        printf("connectRequest is NULL\n");
+        return ;
     }
 
-    if(eventData->pwd != NULL)
+    for(int i = 0 ; i < connectRequest->numRequest ; ++i)
     {
-        printf("Password : %s\n", eventData->pwd);
-    }
-    else
-    {
-        printf("ERROR! Password IS NULL\n");
-        return;
+        printf("connect : %d\n", connectRequest->connect[i]);
     }
 
-    if(eventData->authtype == NONE_AUTH || eventData->authtype == WEP || \
-        eventData->authtype == WPA_PSK || eventData->authtype == WPA2_PSK)
-    {
-        printf("AuthType : %d\n", eventData->authtype);
-    }
-    else
-    {
-        printf("ERROR! AuthType IS NULL\n");
-        return;
-    }
+    printf("ConnectRequestCbInApp OUT\n");
+}
 
-    if(eventData->enctype == NONE_ENC || eventData->enctype == WEP_64 || \
-        eventData->enctype == WEP_128 || eventData->enctype == TKIP || \
-        eventData->enctype == AES || eventData->enctype == TKIP_AES)
+void WiFiConfProvCbInApp(ESWiFiConfData *eventData)
+{
+    printf("WiFiConfProvCbInApp IN\n");
+
+    if(eventData == NULL)
     {
-        printf("EncType : %d\n", eventData->enctype);
+        printf("ESWiFiConfData is NULL\n");
+        return ;
     }
-    else
+
+    printf("SSID : %s\n", eventData->ssid);
+    printf("Password : %s\n", eventData->pwd);
+    printf("AuthType : %d\n", eventData->authtype);
+    printf("EncType : %d\n", eventData->enctype);
+
+    if(eventData->userdata != NULL)
     {
-        printf("ERROR! EncType IS NULL\n");
-        return;
+        printf("userValue : %d\n", ((UserProperties *)(eventData->userdata))->userValue_int);
     }
 
-    printf("WiFiProvCbInApp OUT\n");
+    printf("WiFiConfProvCbInApp OUT\n");
 }
 
-void DevConfProvCbInApp(ESDevConfProvData *eventData)
+void DevConfProvCbInApp(ESDevConfData *eventData)
 {
     printf("DevConfProvCbInApp IN\n");
 
-    if(eventData->language != NULL)
-    {
-        printf("Language : %s\n", eventData->language);
-    }
-    else
-    {
-        printf("ERROR! Language IS NULL\n");
-        return;
-    }
-
-    if(eventData->country != NULL)
+    if(eventData == NULL)
     {
-        printf("Country : %s\n", eventData->country);
-    }
-    else
-    {
-        printf("ERROR! Country IS NULL\n");
-        return;
+        printf("ESDevConfData is NULL\n");
+        return ;
     }
 
     printf("DevConfProvCbInApp OUT\n");
 }
 
-void CloudDataProvCbInApp(ESCloudProvData *eventData)
+void CoapCloudConfProvCbInApp(ESCoapCloudConfData *eventData)
 {
-    printf("CloudDataProvCbInApp IN\n");
+    printf("CoapCloudConfProvCbInApp IN\n");
 
-    if(eventData->authCode != NULL)
+    if(eventData == NULL)
     {
-        printf("AuthCode : %s\n", eventData->authCode);
-    }
-    else
-    {
-        printf("ERROR! AuthCode IS NULL\n");
-        return;
+        printf("ESCoapCloudConfData is NULL\n");
+        return ;
     }
 
-    if(eventData->authProvider != NULL)
-    {
-        printf("AuthProvider : %s\n", eventData->authProvider);
-    }
-    else
-    {
-        printf("ERROR! AuthProvider IS NULL\n");
-        return;
-    }
+    printf("AuthCode : %s\n", eventData->authCode);
+    printf("AcessToken : %s\n", eventData->accessToken);
+    printf("AcessTokenType : %d\n", eventData->accessTokenType);
+    printf("AuthProvider : %s\n", eventData->authProvider);
+    printf("CI Server : %s\n", eventData->ciServer);
 
-    if(eventData->ciServer != NULL)
-    {
-        printf("CI Server : %s\n", eventData->ciServer);
-    }
-    else
-    {
-        printf("ERROR! CI Server IS NULL\n");
-        return;
-    }
-
-    printf("CloudDataProvCbInApp OUT\n");
+    printf("CoapCloudConfProvCbInApp OUT\n");
 }
 
 ESProvisioningCallbacks gCallbacks = {
-    .WiFiProvCb = &WiFiProvCbInApp,
+    .ConnectRequestCb = &ConnectRequestCbInApp,
+    .WiFiConfProvCb = &WiFiConfProvCbInApp,
     .DevConfProvCb = &DevConfProvCbInApp,
-    .CloudDataProvCb = &CloudDataProvCbInApp
+    .CoapCloudConfProvCb = &CoapCloudConfProvCbInApp
 };
 
 FILE* server_fopen(const char *path, const char *mode)
@@ -206,7 +174,7 @@ void StartEasySetup()
         return;
     }
 
-    ESResourceMask resourcemMask = ES_WIFI_RESOURCE | ES_CLOUD_RESOURCE | ES_DEVCONF_RESOURCE;
+    ESResourceMask resourcemMask = ES_WIFICONF_RESOURCE | ES_COAPCLOUDCONF_RESOURCE | ES_DEVCONF_RESOURCE;
     if(ESInitEnrollee(gIsSecured, resourcemMask, gCallbacks) != ES_OK)
     {
         printf("OCStack init error!!\n");
@@ -214,7 +182,6 @@ void StartEasySetup()
     }
     printf("ESInitEnrollee Success\n");
 
-    pthread_t thread_handle;
     if (pthread_create(&thread_handle, NULL, listeningFunc, NULL))
     {
         printf("Thread creation failed\n");
@@ -228,11 +195,20 @@ void SetDeviceInfo()
     printf("SetDeviceInfo IN\n");
 
     ESDeviceProperty deviceProperty = {
-        {{WiFi_11G, WiFi_11N, WiFi_11AC, WiFi_EOF}, WiFi_5G}, {"Test Device"}
-    } ;
+        {{WIFI_11G, WIFI_11N, WIFI_11AC, WiFi_EOF}, WIFI_5G}, {"Test Device"}
+    };
+
+    // Set user properties if needed
+    char userValue_str[] = "user_str";
+    g_userProperties.userValue_int = 0;
+
+    strncpy(g_userProperties.userValue_str, userValue_str, strlen(userValue_str) + 1);
+    SetUserProperties(&g_userProperties);
 
     if(ESSetDeviceProperty(&deviceProperty) == ES_ERROR)
+    {
         printf("ESSetDeviceProperty Error\n");
+    }
 
     printf("SetDeviceInfo OUT\n");
 }
@@ -247,6 +223,12 @@ void StopEasySetup()
         return;
     }
 
+    if (0 != pthread_cancel(thread_handle))
+    {
+        printf("Thread cancellation failed\n");
+        return;
+    }
+
     //stop OC Stack
     if (OCStop() != OC_STACK_OK)
     {
@@ -257,13 +239,18 @@ void StopEasySetup()
     printf("StopEasySetup OUT\n");
 }
 
+void SetCallbackForUserdata()
+{
+    ESSetCallbackForUserdata(&ReadUserdataCb, &WriteUserdataCb);
+}
+
 int main()
 {
     printf("#########################\n");
     printf("EasySetup Enrollee SAMPLE\n");
     printf("#########################\n");
     PrintMenu();
-    char option;
+    char option = '\0';
 
     while(true)
     {
@@ -309,13 +296,21 @@ int main()
                     StopEasySetup();
                     PrintMenu();
                     break;
+                case 'U': // set callback
+                case 'u':
+                    SetCallbackForUserdata();
+                    PrintMenu();
+                    break;
 
                 default:
                     printf("wrong option\n");
                     PrintMenu();
                     break;
             }
-            if (option == 'Q' || option == 'q') break;
+            if (option == 'Q' || option == 'q')
+            {
+                break;
+            }
         }
     }
     return 0;
@@ -323,7 +318,8 @@ int main()
 
 void *listeningFunc(void * data)
 {
-    OCStackResult result;
+    (void)data;
+    OCStackResult result = OC_STACK_ERROR;
 
     while (true)
     {