replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / stack / samples / tizen / SimpleClientServer / ocserver.cpp
index b5fc0ca..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;
@@ -63,22 +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 = "myDeviceSpecVersion";
-const char *dataModelVersions = "myDeviceModelVersions";
+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
@@ -505,6 +507,7 @@ OCDeviceEntityHandlerCb (OCEntityHandlerFlag flag,
         }
     }
 
+    OCPayloadDestroy(response.payload);
     return ehResult;
 }
 
@@ -524,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)
@@ -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;
@@ -841,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;
     }
@@ -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";
@@ -1014,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)
     {