Development of CoAP-HTTP Proxy
[platform/upstream/iotivity.git] / resource / csdk / stack / samples / linux / SimpleClientServer / occlient.cpp
index 061c94f..4a90b45 100644 (file)
@@ -66,6 +66,10 @@ static OCDevAddr serverAddr;
 static char discoveryAddr[100];
 static std::string coapServerResource = "/a/light";
 
+// 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
 OCDoHandle gPresenceHandle;
@@ -105,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");
@@ -140,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,
@@ -158,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);
 
@@ -403,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;
@@ -415,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;
         }
 
@@ -455,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:
@@ -691,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];
@@ -809,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)
         {
@@ -822,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;
@@ -830,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;