Add option to create proxy as secured resource
authorAbhishek Sharma <ce.abhishek@samsung.com>
Wed, 21 Sep 2016 13:14:46 +0000 (18:44 +0530)
committerUze Choi <uzchoi@samsung.com>
Mon, 26 Sep 2016 12:10:49 +0000 (12:10 +0000)
Modify CHPInitialize() api to allow secured
mode. Also minor code formating optimizations.

Change-Id: I4041c506d8ce41769b9c55d4850d551fae87ad2e
Signed-off-by: Abhishek Sharma <ce.abhishek@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/12071
Reviewed-by: Uze Choi <uzchoi@samsung.com>
Tested-by: Uze Choi <uzchoi@samsung.com>
(cherry picked from commit 58d543419f76b030ae06ca7fd4ff6be28f050d78)
Reviewed-on: https://gerrit.iotivity.org/gerrit/12209
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
service/coap-http-proxy/include/CoapHttpHandler.h
service/coap-http-proxy/samples/proxy_main.c
service/coap-http-proxy/src/CoapHttpHandler.c

index 7843000..9f3fa07 100644 (file)
@@ -35,9 +35,10 @@ extern "C"
 
 /**
  * Initialize the CoAP HTTP Proxy.
+ * @param[in]   secure      Set to advertise proxy as secured resource.
  * @return  ::OC_STACK_OK or Appropriate error code.
  */
-OCStackResult CHPInitialize();
+OCStackResult CHPInitialize(bool secure);
 
 /**
  * Terminate the CoAP HTTP Proxy.
index 9f2479d..3f32f4b 100644 (file)
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
+#include <stdlib.h>
 
-int g_quitFlag = 0;
+static int g_quitFlag = 0;
+static int g_secureFlag = 0;
 
 void handleSigInt(int signum);
 
+static void PrintUsage()
+{
+    printf("Usage : proxy_main -s <0|1>\n");
+    printf("-s 0 : Launch proxy in unsecure mode.\n");
+    printf("-s 1 : Launch proxy in secure mode.\n");
+}
+
 /*
 * This method is an entry point of CoAP-HTTP Proxy.
 */
-
-int main()
+int main(int argc, char* argv[])
 {
-    printf("CoAP-HTTP proxy is starting..\n");
+    int opt = 0;
+    while ((opt = getopt(argc, argv, "s:")) != -1)
+    {
+        switch (opt)
+        {
+            case 's':
+                g_secureFlag = atoi(optarg);
+                break;
+            default:
+                PrintUsage();
+                return -1;
+        }
+    }
+
+    if (g_secureFlag != 0 && g_secureFlag != 1)
+    {
+        PrintUsage();
+        return -1;
+    }
+
+    printf("CoAP-HTTP proxy is starting. Mode [%d].\n", g_secureFlag);
     OCStackResult result = OCInit(NULL, 0, OC_SERVER);
     if (result != OC_STACK_OK)
     {
@@ -42,7 +70,7 @@ int main()
         return 0;
     }
 
-    if (CHPInitialize() != OC_STACK_OK)
+    if (CHPInitialize(g_secureFlag) != OC_STACK_OK)
     {
         printf("Failed to start proxy.\n");
         OCStop();
index e0b830c..0d9624c 100644 (file)
@@ -67,9 +67,9 @@ bool CHPIsInitialized()
     return g_isCHProxyInitialized;
 }
 
-OCStackResult CHPInitialize()
+OCStackResult CHPInitialize(bool secure)
 {
-    OIC_LOG(DEBUG, TAG, "CHPInitialize IN");
+    OIC_LOG_V(DEBUG, TAG, "%s IN", __func__);
     if (g_isCHProxyInitialized)
     {
         OIC_LOG(DEBUG, TAG, "CH Proxy already initialized");
@@ -91,13 +91,18 @@ OCStackResult CHPInitialize()
         return result;
     }
 
+    uint8_t prop = OC_ACTIVE | OC_DISCOVERABLE | OC_SLOW;
+    if(secure)
+    {
+        prop |= OC_SECURE;
+    }
     result = OCCreateResource(&g_proxyHandle,
                                CHP_RESOURCE_TYPE_NAME,
                                CHP_RESOURCE_INTF_NAME,
                                OC_RSRVD_PROXY_URI,
                                CHPEntityHandler,
                                NULL,
-                               OC_ACTIVE | OC_DISCOVERABLE | OC_SLOW);
+                               prop);
 
     if (OC_STACK_OK != result)
     {
@@ -107,13 +112,13 @@ OCStackResult CHPInitialize()
     }
 
     g_isCHProxyInitialized = true;
-    OIC_LOG(DEBUG, TAG, "CHPInitialize OUT");
+    OIC_LOG_V(DEBUG, TAG, "%s OUT", __func__);
     return OC_STACK_OK;
 }
 
 OCStackResult CHPTerminate()
 {
-    OIC_LOG(DEBUG, TAG, "CHPTerminate IN");
+    OIC_LOG_V(DEBUG, TAG, "%s IN", __func__);
     OCStackResult result = CHPParserTerminate();
     if (OC_STACK_OK != result)
     {
@@ -129,13 +134,13 @@ OCStackResult CHPTerminate()
     g_proxyHandle = NULL;
     g_isCHProxyInitialized = false;
     return result;
-    OIC_LOG(DEBUG, TAG, "CHPTerminate OUT");
+    OIC_LOG_V(DEBUG, TAG, "%s OUT", __func__);
 }
 
 static void CHPGetProxyURI(OCHeaderOption* options, uint8_t *numOptions, char* uri,
                            size_t uriLength)
 {
-    OIC_LOG(DEBUG, TAG, "CHPGetProxyURI IN");
+    OIC_LOG_V(DEBUG, TAG, "%s IN", __func__);
     if (!uri || uriLength <= 0)
     {
         OIC_LOG (INFO, TAG, "Invalid uri buffer");
@@ -165,7 +170,7 @@ static void CHPGetProxyURI(OCHeaderOption* options, uint8_t *numOptions, char* u
         }
     }
 
-    OIC_LOG(DEBUG, TAG, "CHPGetProxyURI OUT");
+    OIC_LOG_V(DEBUG, TAG, "%s OUT", __func__);
     return;
 }
 
@@ -188,7 +193,7 @@ OCEntityHandlerResult CHPEntityHandler(OCEntityHandlerFlag flag,
                                        OCEntityHandlerRequest* entityHandlerRequest,
                                        void* callbackParam)
 {
-    OIC_LOG_V(INFO, TAG, "Proxy request received");
+    OIC_LOG(INFO, TAG, "Proxy request received");
     UNUSED(callbackParam);
 
     if (!g_isCHProxyInitialized)
@@ -274,7 +279,7 @@ OCEntityHandlerResult CHPEntityHandler(OCEntityHandlerFlag flag,
 
 void CHPHandleHttpResponse(const HttpResponse_t *httpResponse, void *context)
 {
-    OIC_LOG(DEBUG, TAG, "CHPHandleHttpResponse IN");
+    OIC_LOG_V(DEBUG, TAG, "%s IN", __func__);
     if (!httpResponse || !context)
     {
         OIC_LOG(ERROR, TAG, "Invalid arguements");
@@ -390,7 +395,7 @@ void CHPHandleHttpResponse(const HttpResponse_t *httpResponse, void *context)
     }
 
     //OICFree(coapResponseInfo.info.payload);
-    OIC_LOG(DEBUG, TAG, "CHPHandleHttpResponse OUT");
+    OIC_LOG_V(DEBUG, TAG, "%s OUT", __func__);
 }
 
 OCStackResult CHPHandleOCFRequest(const OCEntityHandlerRequest* requestInfo,