Development of CoAP-HTTP Proxy
[platform/upstream/iotivity.git] / resource / csdk / stack / samples / linux / SimpleClientServer / occlient.cpp
index 23053e7..03292cc 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>
+#endif
 #include <iostream>
 #include <sstream>
+#include <getopt.h>
 #include "ocstack.h"
 #include "logger.h"
 #include "occlient.h"
 #include "ocpayload.h"
 #include "payload_logging.h"
+#include "common.h"
+#include "platform_features.h"
 
 #ifdef ROUTING_GATEWAY
 /**
@@ -58,7 +66,9 @@ static OCDevAddr serverAddr;
 static char discoveryAddr[100];
 static std::string coapServerResource = "/a/light";
 
-void StripNewLineChar(char* str);
+// Following resource is used to verify coap-http proxy
+static std::string coapProxyResource = OC_RSRVD_PROXY_URI;
+static std::string httpResource;    // Will be taken as user input
 
 #ifdef WITH_PRESENCE
 // The handle for observe registration
@@ -99,7 +109,7 @@ OCPayload* putPayload()
 
 static void PrintUsage()
 {
-    OIC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1..17> -c <0|1>");
+    OIC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1..21> -c <0|1>");
     OIC_LOG(INFO, TAG, "-u <0|1> : Perform multicast/unicast discovery of resources");
     OIC_LOG(INFO, TAG, "-c 0 : Use Default connectivity(IP)");
     OIC_LOG(INFO, TAG, "-c 1 : IP Connectivity Type");
@@ -134,6 +144,7 @@ static void PrintUsage()
             "add  vendor specific header options");
     OIC_LOG(INFO, TAG, "-t 19 :  Discover Platform");
     OIC_LOG(INFO, TAG, "-t 20 :  Discover Devices");
+    OIC_LOG(INFO, TAG, "-t 21 -p \"http_uri\":  Discover Proxy and Initiate Nonconfirmable Get Request");
 }
 
 OCStackResult InvokeOCDoResource(std::ostringstream &query,
@@ -152,7 +163,8 @@ OCStackResult InvokeOCDoResource(std::ostringstream &query,
     cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
     cbData.cd = NULL;
 
-    ret = OCDoResource(&handle, method, query.str().c_str(), remoteAddr,
+    const char *uri = query.str().length() ? query.str().c_str() : NULL;
+    ret = OCDoResource(&handle, method, uri, remoteAddr,
                        (method == OC_REST_PUT) ? putPayload() : NULL,
                        (ConnType), qos, &cbData, options, numOptions);
 
@@ -397,9 +409,12 @@ OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle /*handle*/,
 
         OCResourcePayload *resource = (OCResourcePayload*) payload->resources;
         int found = 0;
+
+        std::string resourceToFind = (TestCase == TEST_PROXY_GET_REQ_NON) ?
+                                            coapProxyResource : coapServerResource;
         while (resource)
         {
-            if(resource->uri && strcmp(resource->uri, coapServerResource.c_str()) == 0)
+            if(resource->uri && strcmp(resource->uri, resourceToFind.c_str()) == 0)
             {
                 found = 1;
                 break;
@@ -409,7 +424,7 @@ OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle /*handle*/,
 
         if(!found)
         {
-            OIC_LOG_V (INFO, TAG, "No /a/light in payload");
+            OIC_LOG_V (INFO, TAG, "No %s in payload", resourceToFind.c_str());
             return OC_STACK_KEEP_TRANSACTION;
         }
 
@@ -449,6 +464,9 @@ OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle /*handle*/,
             case TEST_OBS_REQ_CON:
                 InitObserveRequest(OC_HIGH_QOS);
                 break;
+            case TEST_PROXY_GET_REQ_NON:
+                InitProxyGetRequest(OC_LOW_QOS);
+                break;
 #ifdef WITH_PRESENCE
             case TEST_OBS_PRESENCE:
             case TEST_OBS_PRESENCE_WITH_FILTER:
@@ -685,7 +703,27 @@ int InitDeleteRequest(OCQualityOfService qos)
     return result;
 }
 
-int InitGetRequest(OCQualityOfService qos, uint8_t withVendorSpecificHeaderOptions, bool getWithQuery)
+int InitProxyGetRequest(OCQualityOfService qos)
+{
+    OIC_LOG(INFO, TAG, "InitProxyGetRequest");
+    OCHeaderOption option;
+    memset(&option, 0, sizeof(option));
+
+    option.protocolID = OC_COAP_ID;
+    option.optionID = OC_RSRVD_PROXY_OPTION_ID;
+    memcpy(option.optionData, (uint8_t *)httpResource.c_str(), httpResource.length());
+    option.optionLength = httpResource.length();
+
+    std::ostringstream query;
+    // A request with proxy uri shall not have resource uri
+    // query << coapProxyResource;
+
+    return (InvokeOCDoResource(query, &serverAddr, OC_REST_GET,
+                (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS, getReqCB, &option, 1));
+}
+
+int InitGetRequest(OCQualityOfService qos, uint8_t withVendorSpecificHeaderOptions,
+                   bool getWithQuery)
 {
 
     OCHeaderOption options[MAX_HEADER_OPTIONS];
@@ -733,7 +771,7 @@ int InitPlatformDiscovery(OCQualityOfService qos)
 
     OCStackResult ret;
     OCCallbackData cbData;
-    char szQueryUri[64] = { 0 };
+    char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
 
     snprintf(szQueryUri, sizeof (szQueryUri) - 1, PLATFORM_DISCOVERY_QUERY, discoveryAddr);
 
@@ -758,7 +796,7 @@ int InitDeviceDiscovery(OCQualityOfService qos)
 
     OCStackResult ret;
     OCCallbackData cbData;
-    char szQueryUri[100] = { 0 };
+    char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
 
     snprintf(szQueryUri, sizeof (szQueryUri) - 1, DEVICE_DISCOVERY_QUERY, discoveryAddr);
 
@@ -781,7 +819,7 @@ int InitDiscovery(OCQualityOfService qos)
 {
     OCStackResult ret;
     OCCallbackData cbData;
-    char szQueryUri[100] = { 0 };
+    char szQueryUri[MAX_QUERY_LENGTH] = { 0 };
 
     snprintf(szQueryUri, sizeof (szQueryUri) - 1, RESOURCE_DISCOVERY_QUERY, discoveryAddr);
 
@@ -803,7 +841,7 @@ int main(int argc, char* argv[])
 {
     int opt;
 
-    while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
+    while ((opt = getopt(argc, argv, "u:t:c:p:")) != -1)
     {
         switch(opt)
         {
@@ -816,6 +854,12 @@ int main(int argc, char* argv[])
             case 'c':
                 Connectivity = atoi(optarg);
                 break;
+            case 'p':
+                if(optarg)
+                {
+                    httpResource = optarg;
+                }
+                break;
             default:
                 PrintUsage();
                 return -1;
@@ -824,7 +868,8 @@ int main(int argc, char* argv[])
 
     if ((UnicastDiscovery != 0 && UnicastDiscovery != 1) ||
             (TestCase < TEST_DISCOVER_REQ || TestCase >= MAX_TESTS) ||
-            (Connectivity < CT_ADAPTER_DEFAULT || Connectivity >= MAX_CT))
+            (Connectivity < CT_ADAPTER_DEFAULT || Connectivity >= MAX_CT) ||
+            (TestCase == TEST_PROXY_GET_REQ_NON && httpResource.length() == 0) )
     {
         PrintUsage();
         return -1;