Merge branch 'master' into windows-port
[platform/upstream/iotivity.git] / resource / csdk / stack / samples / linux / secure / occlientbasicops.cpp
index 38aefe1..fdcf3cc 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 #include <signal.h>
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
+#endif
+#ifdef HAVE_WINDOWS_H
+#include <windows.h>
+/** @todo stop-gap for naming issue. Windows.h does not like us to use ERROR */
+#ifdef ERROR
+#undef ERROR
+#endif
+#endif
 #include <iostream>
 #include <sstream>
+#include <getopt.h>
+#include "platform_features.h"
 #include "ocstack.h"
 #include "logger.h"
 #include "occlientbasicops.h"
@@ -37,6 +48,7 @@
 static int UnicastDiscovery = 0;
 static int TestCase = 0;
 static int ConnType = 0;
+static int DevOwner = 0;
 
 static char DISCOVERY_QUERY[] = "%s/oic/res";
 OCConnectivityType discoveryReqConnType = CT_ADAPTER_IP;
@@ -49,7 +61,8 @@ static OCConnectivityType ocConnType;
 //Secure Virtual Resource database for Iotivity Client application
 //It contains Client's Identity and the PSK credentials
 //of other devices which the client trusts
-static char CRED_FILE[] = "oic_svr_db_client.json";
+static char CRED_FILE_DEVOWNER[] = "oic_svr_db_client_devowner.dat";
+static char CRED_FILE_NONDEVOWNER[] = "oic_svr_db_client_nondevowner.dat";
 
 
 int gQuitFlag = 0;
@@ -89,6 +102,8 @@ static void PrintUsage()
     OIC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Confirmable Get/Put/Post Requests");
     OIC_LOG(INFO, TAG, "-c 0 : Default auto-selection");
     OIC_LOG(INFO, TAG, "-c 1 : IP Connectivity Type");
+    OIC_LOG(INFO, TAG, "-d 0 : Client as Device Owner");
+    OIC_LOG(INFO, TAG, "-d 1 : Client as Non Device Owner");
 }
 
 OCStackResult InvokeOCDoResource(std::ostringstream &query,
@@ -203,7 +218,7 @@ OCStackApplicationResult discoveryReqCB(void*, OCDoHandle,
 
 int InitPutRequest(OCQualityOfService qos)
 {
-    OIC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
+    OIC_LOG_V(INFO, TAG, "Executing %s", __func__);
     std::ostringstream query;
     query << coapServerResource;
     endpoint.flags = (OCTransportFlags)(endpoint.flags|OC_SECURE);
@@ -214,7 +229,8 @@ int InitPutRequest(OCQualityOfService qos)
 int InitPostRequest(OCQualityOfService qos)
 {
     OCStackResult result;
-    OIC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
+
+    OIC_LOG_V(INFO, TAG, "Executing %s", __func__);
     std::ostringstream query;
     query << coapServerResource;
     endpoint.flags = (OCTransportFlags)(endpoint.flags|OC_SECURE);
@@ -246,7 +262,7 @@ int InitPostRequest(OCQualityOfService qos)
 
 int InitGetRequest(OCQualityOfService qos)
 {
-    OIC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
+    OIC_LOG_V(INFO, TAG, "Executing %s", __func__);
     std::ostringstream query;
     query << coapServerResource;
     endpoint.flags = (OCTransportFlags)(endpoint.flags|OC_SECURE);
@@ -299,18 +315,24 @@ int InitDiscovery()
     return ret;
 }
 
-FILE* client_fopen(const char *path, const char *mode)
+FILE* client_fopen_devowner(const char *path, const char *mode)
 {
     (void)path;
-    return fopen(CRED_FILE, mode);
+    return fopen(CRED_FILE_DEVOWNER, mode);
 }
 
+FILE* client_fopen_nondevowner(const char *path, const char *mode)
+{
+    (void)path;
+    return fopen(CRED_FILE_NONDEVOWNER, mode);
+}
 int main(int argc, char* argv[])
 {
     int opt;
     struct timespec timeout;
+    OCPersistentStorage ps;
 
-    while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
+    while ((opt = getopt(argc, argv, "u:t:c:d:")) != -1)
     {
         switch(opt)
         {
@@ -323,6 +345,9 @@ int main(int argc, char* argv[])
             case 'c':
                 ConnType = atoi(optarg);
                 break;
+            case 'd':
+                DevOwner = atoi(optarg);
+                break;
             default:
                 PrintUsage();
                 return -1;
@@ -350,7 +375,10 @@ int main(int argc, char* argv[])
 
 
     // Initialize Persistent Storage for SVR database
-    OCPersistentStorage ps = { client_fopen, fread, fwrite, fclose, unlink };
+    if (DevOwner)
+        ps = { client_fopen_devowner, fread, fwrite, fclose, unlink };
+    else
+        ps = { client_fopen_nondevowner, fread, fwrite, fclose, unlink };
     OCRegisterPersistentStorageHandler(&ps);
 
     /* Initialize OCStack*/