replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / stack / samples / tizen / SimpleClientServer / ocserver.cpp
index c5af603..650b428 100644 (file)
@@ -29,6 +29,7 @@
 #include <signal.h>
 #include <pthread.h>
 #include <array>
+#include "oic_malloc.h"
 #include "ocstack.h"
 #include "logger.h"
 #include "ocpayload.h"
@@ -38,12 +39,17 @@ using namespace std;
 //string length of "/a/light/" + std::numeric_limits<int>::digits10 + '\0'"
 // 9 + 9 + 1 = 19
 const int URI_MAXSIZE = 19;
+static const char* DEFAULT_DB_FILE_PATH = "/opt/usr/etc/oic_svr_db_server.dat";
 
 static int gObserveNotifyType = 3;
+static int gSecure = 0;
 
 int gQuitFlag = 0;
 int gLightUnderObservation = 0;
 
+static GMainLoop *g_mainloop = NULL;
+pthread_t g_thread;
+
 static LightResource Light;
 // This variable determines instance number of the Light resource.
 // Used by POST method to create a new instance of Light resource.
@@ -59,20 +65,22 @@ static int stopPresenceCount = 10;
 #endif
 
 char *gResourceUri= (char *)"/a/light";
-const char *dateOfManufacture = "myDateOfManufacture";
+const char *dateOfManufacture = "2016-01-15";
 const char *deviceName = "myDeviceName";
-const char *deviceUUID = "myDeviceUUID";
+const char *deviceUUID = "51b55ddc-ccbb-4cb3-a57f-494eeca13a21";
 const char *firmwareVersion = "myFirmwareVersion";
 const char *manufacturerName = "myName";
 const char *operatingSystemVersion = "myOS";
 const char *hardwareVersion = "myHardwareVersion";
-const char* platformID = "myPlatformID";
-const char *manufacturerUrl = "myManufacturerUrl";
+const char *platformID = "0A3E0D6F-DBF5-404E-8719-D6880042463A";
+const char *manufacturerLink = "https://www.iotivity.org";
 const char *modelNumber = "myModelNumber";
 const char *platformVersion = "myPlatformVersion";
-const char *supportUrl = "mySupportUrl";
+const char *supportLink = "https://www.iotivity.org";
 const char *version = "myVersion";
 const char *systemTime = "2015-05-15T11.04";
+const char *specVersion = "core.1.1.0";
+const char *dataModelVersions = "res.1.1.0,sh.1.1.0";
 
 // Entity handler should check for resourceTypeName and ResourceInterface in order to GET
 // the existence of a known resource
@@ -499,6 +507,7 @@ OCDeviceEntityHandlerCb (OCEntityHandlerFlag flag,
         }
     }
 
+    OCPayloadDestroy(response.payload);
     return ehResult;
 }
 
@@ -518,7 +527,7 @@ OCEntityHandlerCb (OCEntityHandlerFlag flag,
     cout << "\nInside entity handler - flags: " << flag;
 
     OCEntityHandlerResult ehResult = OC_EH_OK;
-    OCEntityHandlerResponse response;
+    OCEntityHandlerResponse response = { 0, 0, OC_EH_ERROR, 0, 0, { },{ 0 }, false };
 
     // Validate pointer
     if (!entityHandlerRequest)
@@ -729,13 +738,20 @@ void *presenceNotificationGenerator(void *param)
         if(res == OC_STACK_OK)
         {
             sleep(2);
+
+            uint8_t resourceProperties = OC_DISCOVERABLE | OC_OBSERVABLE;
+            if (gSecure)
+            {
+                resourceProperties |= OC_SECURE;
+            }
+
             res = OCCreateResource(&presenceNotificationHandles[i],
                     presenceNotificationResources.at(i).c_str(),
                     OC_RSRVD_INTERFACE_DEFAULT,
                     presenceNotificationUris.at(i).c_str(),
                     OCNOPEntityHandlerCb,
                     NULL,
-                    OC_DISCOVERABLE|OC_OBSERVABLE);
+                    resourceProperties);
         }
         if(res != OC_STACK_OK)
         {
@@ -774,13 +790,20 @@ int createLightResource (char *uri, LightResource *lightResource)
 
     lightResource->state = false;
     lightResource->power= 0;
+
+    uint8_t resourceProperties = OC_DISCOVERABLE | OC_OBSERVABLE;
+    if (gSecure)
+    {
+        resourceProperties |= OC_SECURE;
+    }
+
     OCStackResult res = OCCreateResource(&(lightResource->handle),
             "core.light",
             "oc.mi.def",
             uri,
             OCEntityHandlerCb,
             NULL,
-            OC_DISCOVERABLE|OC_OBSERVABLE);
+            resourceProperties);
     cout << "\nCreated Light resource with result " << getResult(res);
 
     return 0;
@@ -804,6 +827,8 @@ void DeletePlatformInfo()
 void DeleteDeviceInfo()
 {
     free (deviceInfo.deviceName);
+    free (deviceInfo.specVersion);
+    OCFreeOCStringLL (deviceInfo.dataModelVersions);
 }
 
 bool DuplicateString(char** targetString, const char* sourceString)
@@ -833,12 +858,12 @@ OCStackResult SetPlatformInfo(const char* platformID, const char *manufacturerNa
 
     bool success = true;
 
-    if(manufacturerName != NULL && (strlen(manufacturerName) > MAX_MANUFACTURER_NAME_LENGTH))
+    if(manufacturerName != NULL && (strlen(manufacturerName) > MAX_PLATFORM_NAME_LENGTH))
     {
         return OC_STACK_INVALID_PARAM;
     }
 
-    if(manufacturerUrl != NULL && (strlen(manufacturerUrl) > MAX_MANUFACTURER_URL_LENGTH))
+    if(manufacturerUrl != NULL && (strlen(manufacturerUrl) > MAX_PLATFORM_URL_LENGTH))
     {
         return OC_STACK_INVALID_PARAM;
     }
@@ -907,20 +932,59 @@ OCStackResult SetPlatformInfo(const char* platformID, const char *manufacturerNa
     return OC_STACK_ERROR;
 }
 
-OCStackResult SetDeviceInfo(const char* deviceName)
+OCStackResult SetDeviceInfo(const char* deviceName, const char* specVersion, const char* dataModelVersions)
 {
     if(!DuplicateString(&deviceInfo.deviceName, deviceName))
     {
         return OC_STACK_ERROR;
     }
+    if(!DuplicateString(&deviceInfo.specVersion, specVersion))
+    {
+        return OC_STACK_ERROR;
+    }
+    OCFreeOCStringLL(deviceInfo.dataModelVersions);
+    deviceInfo.dataModelVersions = OCCreateOCStringLL(dataModelVersions);
+    if (!deviceInfo.dataModelVersions)
+    {
+        return OC_STACK_ERROR;
+    }
     return OC_STACK_OK;
 }
 
 static void PrintUsage()
 {
-    cout << "\nUsage : ocserver -o <0|1>";
+    cout << "\nUsage : ocserver -o <0|1> -s <0|1>";
     cout << "\n-o 0 : Notify all observers";
     cout << "\n-o 1 : Notify list of observers";
+    cout << "\n-s 0 : Non secure resource";
+    cout << "\n-s 1 : Secure resource";
+}
+
+void *GMainLoopThread(void *param)
+{
+
+    while (!gQuitFlag)
+    {
+        if (OCProcess() != OC_STACK_OK)
+        {
+            cout << "\nOCStack process error";
+            return NULL;
+        }
+#ifndef ROUTING_GATEWAY
+        sleep(1);
+#endif
+    }
+
+    if (g_mainloop)
+    {
+        g_main_loop_quit(g_mainloop);
+    }
+    return NULL;
+}
+
+static FILE *server_fopen(const char */*path*/, const char *mode)
+{
+    return fopen(DEFAULT_DB_FILE_PATH, mode);
 }
 
 int main(int argc, char* argv[])
@@ -929,20 +993,31 @@ int main(int argc, char* argv[])
     pthread_t threadId_presence;
     int opt;
 
-    while ((opt = getopt(argc, argv, "o:")) != -1)
+    g_mainloop = g_main_loop_new(NULL, FALSE);
+    if(!g_mainloop)
+    {
+        printf("g_main_loop_new failed\n");
+        return 0;
+    }
+
+    while ((opt = getopt(argc, argv, "o:s:")) != -1)
     {
         switch(opt)
         {
             case 'o':
                 gObserveNotifyType = atoi(optarg);
                 break;
+            case 's':
+                gSecure = atoi(optarg);
+                break;
             default:
                 PrintUsage();
                 return -1;
         }
     }
 
-    if ((gObserveNotifyType != 0) && (gObserveNotifyType != 1))
+    if ((gObserveNotifyType != 0) && (gObserveNotifyType != 1) &&
+            (gSecure != 0) && (gSecure != 1))
     {
         PrintUsage();
         return -1;
@@ -950,11 +1025,15 @@ int main(int argc, char* argv[])
 
     cout << "\nOCServer is starting...";
 
+    OCPersistentStorage ps{ server_fopen, fread, fwrite, fclose, unlink };
+    OCRegisterPersistentStorageHandler(&ps);
+
     if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
     {
         cout << "\nOCStack init error";
         return 0;
     }
+
 #ifdef WITH_PRESENCE
     if (OCStartPresence(0) != OC_STACK_OK)
     {
@@ -966,9 +1045,9 @@ int main(int argc, char* argv[])
     OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandlerCb, NULL);
 
     OCStackResult registrationResult =
-        SetPlatformInfo(platformID, manufacturerName, manufacturerUrl, modelNumber,
+        SetPlatformInfo(platformID, manufacturerName, manufacturerLink, modelNumber,
             dateOfManufacture, platformVersion,  operatingSystemVersion,  hardwareVersion,
-            firmwareVersion,  supportUrl, systemTime);
+            firmwareVersion,  supportLink, systemTime);
 
     if (registrationResult != OC_STACK_OK)
     {
@@ -984,7 +1063,7 @@ int main(int argc, char* argv[])
         exit (EXIT_FAILURE);
     }
 
-    registrationResult = SetDeviceInfo(deviceName);
+    registrationResult = SetDeviceInfo(deviceName, specVersion, dataModelVersions);
 
     if (registrationResult != OC_STACK_OK)
     {
@@ -992,6 +1071,8 @@ int main(int argc, char* argv[])
         exit (EXIT_FAILURE);
     }
 
+    OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.d.tv");
+
     registrationResult = OCSetDeviceInfo(deviceInfo);
 
     if (registrationResult != OC_STACK_OK)
@@ -1033,18 +1114,15 @@ int main(int argc, char* argv[])
 
     signal(SIGINT, handleSigInt);
 
-    while (!gQuitFlag)
+    int result = pthread_create(&g_thread, NULL, GMainLoopThread, (void *)NULL);
+    if (result < 0)
     {
-        if (OCProcess() != OC_STACK_OK)
-        {
-            cout << "\nOCStack process error";
-            return 0;
-        }
-#ifndef ROUTING_GATEWAY
-        sleep(1);
-#endif
+        printf("pthread_create failed in initialize\n");
+        return 0;
     }
 
+    g_main_loop_run(g_mainloop);
+
     /*
      * Cancel the Light thread and wait for it to terminate
      */