replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / stack / samples / linux / secure / occlientbasicops.cpp
index fdcf3cc..d6ce307 100644 (file)
@@ -18,6 +18,7 @@
 //
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
+#include "iotivity_config.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -35,7 +36,6 @@
 #include <iostream>
 #include <sstream>
 #include <getopt.h>
-#include "platform_features.h"
 #include "ocstack.h"
 #include "logger.h"
 #include "occlientbasicops.h"
@@ -49,6 +49,7 @@ static int UnicastDiscovery = 0;
 static int TestCase = 0;
 static int ConnType = 0;
 static int DevOwner = 0;
+static int WithTcp = 0;
 
 static char DISCOVERY_QUERY[] = "%s/oic/res";
 OCConnectivityType discoveryReqConnType = CT_ADAPTER_IP;
@@ -63,7 +64,8 @@ static OCConnectivityType ocConnType;
 //of other devices which the client trusts
 static char CRED_FILE_DEVOWNER[] = "oic_svr_db_client_devowner.dat";
 static char CRED_FILE_NONDEVOWNER[] = "oic_svr_db_client_nondevowner.dat";
-
+const char * OIC_RSRC_DOXM_URI =  "/oic/sec/doxm";
+const char * OIC_RSRC_PSTAT_URI = "/oic/sec/pstat";
 
 int gQuitFlag = 0;
 
@@ -102,8 +104,10 @@ 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");
+    OIC_LOG(INFO, TAG, "-d 0 : Client as Non Device Owner");
+    OIC_LOG(INFO, TAG, "-d 1 : Client as Device Owner");
+    OIC_LOG(INFO, TAG, "-p 0 : Use UDP protocol");
+    OIC_LOG(INFO, TAG, "-p 1 : Use TCP protocol");
 }
 
 OCStackResult InvokeOCDoResource(std::ostringstream &query,
@@ -120,9 +124,12 @@ OCStackResult InvokeOCDoResource(std::ostringstream &query,
     cbData.context = NULL;
     cbData.cd = NULL;
 
-    ret = OCDoResource(NULL, method, query.str().c_str(), dest,
-            (method == OC_REST_PUT || method == OC_REST_POST) ? putPayload() : NULL,
-            ocConnType, qos, &cbData, options, numOptions);
+    OCPayload* payload = (method == OC_REST_PUT || method == OC_REST_POST) ? putPayload() : NULL;
+
+    ret = OCDoRequest(NULL, method, query.str().c_str(), dest,
+                      payload, ocConnType, qos, &cbData, options, numOptions);
+
+    OCPayloadDestroy(payload);
 
     if (ret != OC_STACK_OK)
     {
@@ -140,7 +147,7 @@ OCStackApplicationResult putReqCB(void*, OCDoHandle, OCClientResponse * clientRe
     {
         OIC_LOG_V(INFO, TAG, "StackResult: %s",  getResult(clientResponse->result));
         OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
-        OIC_LOG(INFO, TAG, ("=============> Put Response"));
+        OIC_LOG(INFO, TAG, "=============> Put Response");
     }
     return OC_STACK_DELETE_TRANSACTION;
 }
@@ -153,7 +160,7 @@ OCStackApplicationResult postReqCB(void *, OCDoHandle, OCClientResponse *clientR
     {
         OIC_LOG_V(INFO, TAG, "StackResult: %s",  getResult(clientResponse->result));
         OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
-        OIC_LOG(INFO, TAG, ("=============> Post Response"));
+        OIC_LOG(INFO, TAG, "=============> Post Response");
     }
     return OC_STACK_DELETE_TRANSACTION;
 }
@@ -167,7 +174,7 @@ OCStackApplicationResult getReqCB(void*, OCDoHandle, OCClientResponse * clientRe
         OIC_LOG_V(INFO, TAG, "StackResult: %s",  getResult(clientResponse->result));
         OIC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
         OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
-        OIC_LOG(INFO, TAG, ("=============> Get Response"));
+        OIC_LOG(INFO, TAG, "=============> Get Response");
     }
     return OC_STACK_DELETE_TRANSACTION;
 }
@@ -200,12 +207,12 @@ OCStackApplicationResult discoveryReqCB(void*, OCDoHandle,
                     case TEST_NON_CON_OP:
                         InitGetRequest(OC_LOW_QOS);
                         InitPutRequest(OC_LOW_QOS);
-                        //InitPostRequest(OC_LOW_QOS);
+                        InitPostRequest(OC_LOW_QOS);
                         break;
                     case TEST_CON_OP:
                         InitGetRequest(OC_HIGH_QOS);
                         InitPutRequest(OC_HIGH_QOS);
-                        //InitPostRequest(OC_HIGH_QOS);
+                        InitPostRequest(OC_HIGH_QOS);
                         break;
                 }
             }
@@ -215,12 +222,15 @@ OCStackApplicationResult discoveryReqCB(void*, OCDoHandle,
     return (UnicastDiscovery) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION ;
 
 }
-
 int InitPutRequest(OCQualityOfService qos)
 {
     OIC_LOG_V(INFO, TAG, "Executing %s", __func__);
     std::ostringstream query;
     query << coapServerResource;
+    if(WithTcp)
+    {
+        endpoint.adapter = OC_ADAPTER_TCP;
+    }
     endpoint.flags = (OCTransportFlags)(endpoint.flags|OC_SECURE);
     return (InvokeOCDoResource(query, OC_REST_PUT, &endpoint,
             ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS), putReqCB, NULL, 0));
@@ -233,6 +243,10 @@ int InitPostRequest(OCQualityOfService qos)
     OIC_LOG_V(INFO, TAG, "Executing %s", __func__);
     std::ostringstream query;
     query << coapServerResource;
+    if(WithTcp)
+    {
+        endpoint.adapter = OC_ADAPTER_TCP;
+    }
     endpoint.flags = (OCTransportFlags)(endpoint.flags|OC_SECURE);
 
     // First POST operation (to create an LED instance)
@@ -254,10 +268,16 @@ int InitPostRequest(OCQualityOfService qos)
         OIC_LOG(INFO, TAG, "Second POST call did not succeed");
     }
 
-    // This POST operation will update the original resourced /a/led
-    return (InvokeOCDoResource(query, OC_REST_POST, &endpoint,
-                ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
-                postReqCB, NULL, 0));
+    // This POST operation will update the original resourced /a/led (as long as
+    // the server is set to max 2 /lcd resources)
+    result = InvokeOCDoResource(query, OC_REST_POST, &endpoint,
+            ((qos == OC_HIGH_QOS) ? OC_HIGH_QOS: OC_LOW_QOS),
+            postReqCB, NULL, 0);
+    if (OC_STACK_OK != result)
+    {
+        OIC_LOG(INFO, TAG, "Third POST call did not succeed");
+    }
+    return result;
 }
 
 int InitGetRequest(OCQualityOfService qos)
@@ -265,6 +285,10 @@ int InitGetRequest(OCQualityOfService qos)
     OIC_LOG_V(INFO, TAG, "Executing %s", __func__);
     std::ostringstream query;
     query << coapServerResource;
+    if(WithTcp)
+    {
+        endpoint.adapter = OC_ADAPTER_TCP;
+    }
     endpoint.flags = (OCTransportFlags)(endpoint.flags|OC_SECURE);
 
     return (InvokeOCDoResource(query, OC_REST_GET, &endpoint,
@@ -306,8 +330,8 @@ int InitDiscovery()
         (UnicastDiscovery) ? "Unicast" : "Multicast",
         queryUri);
 
-    ret = OCDoResource(NULL, OC_REST_DISCOVER, queryUri, 0, 0, CT_DEFAULT,
-                       OC_LOW_QOS, &cbData, NULL, 0);
+    ret = OCDoRequest(NULL, OC_REST_DISCOVER, queryUri, 0, 0, CT_DEFAULT,
+                      OC_LOW_QOS, &cbData, NULL, 0);
     if (ret != OC_STACK_OK)
     {
         OIC_LOG(ERROR, TAG, "OCStack resource error");
@@ -332,7 +356,7 @@ int main(int argc, char* argv[])
     struct timespec timeout;
     OCPersistentStorage ps;
 
-    while ((opt = getopt(argc, argv, "u:t:c:d:")) != -1)
+    while ((opt = getopt(argc, argv, "u:t:c:d:p:")) != -1)
     {
         switch(opt)
         {
@@ -348,6 +372,16 @@ int main(int argc, char* argv[])
             case 'd':
                 DevOwner = atoi(optarg);
                 break;
+            case 'p':
+            {
+                WithTcp = atoi(optarg);
+                if(WithTcp > 1)
+                {
+                    PrintUsage();
+                    return -1;
+                }
+            }
+                break;
             default:
                 PrintUsage();
                 return -1;
@@ -428,10 +462,32 @@ int parseClientResponse(OCClientResponse * clientResponse)
     {
         coapServerResource.assign(res->uri);
         OIC_LOG_V(INFO, TAG, "Uri -- %s", coapServerResource.c_str());
-
+        if (0 == strcmp(coapServerResource.c_str(),OIC_RSRC_DOXM_URI))
+        {
+            OIC_LOG(INFO,TAG,"Skip: doxm is secure virtual resource");
+            res = res->next;
+            continue;
+        }
+        if (0 == strcmp(coapServerResource.c_str(),OIC_RSRC_PSTAT_URI))
+        {
+            OIC_LOG(INFO,TAG,"Skip: pstat is secure virtual resource");
+            res = res->next;
+            continue;
+        }
         if (res->secure)
         {
-            endpoint.port = res->port;
+            if(WithTcp)
+            {
+#ifdef TCP_ADAPTER
+                OIC_LOG_V(INFO,TAG,"SECUREPORT tcp: %d",res->tcpPort);
+                endpoint.port = res->tcpPort;
+#endif
+            }
+            else
+            {
+                OIC_LOG_V(INFO,TAG,"SECUREPORT udp: %d",res->port);
+                endpoint.port = res->port;
+            }
             coapSecureResource = 1;
         }