replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / stack / samples / tizen / SimpleClientServer / ocserver.cpp
index 4305399..650b428 100644 (file)
@@ -39,8 +39,10 @@ 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;
@@ -505,6 +507,7 @@ OCDeviceEntityHandlerCb (OCEntityHandlerFlag flag,
         }
     }
 
+    OCPayloadDestroy(response.payload);
     return ehResult;
 }
 
@@ -735,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)
         {
@@ -780,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;
@@ -936,9 +953,11 @@ OCStackResult SetDeviceInfo(const char* deviceName, const char* specVersion, con
 
 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)
@@ -963,6 +982,11 @@ void *GMainLoopThread(void *param)
     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[])
 {
     pthread_t threadId;
@@ -976,20 +1000,24 @@ int main(int argc, char* argv[])
         return 0;
     }
 
-    while ((opt = getopt(argc, argv, "o:")) != -1)
+    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;
@@ -997,6 +1025,9 @@ 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";