replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / stack / samples / linux / SimpleClientServer / ocservercoll.cpp
index d3fed5a..bedce0d 100644 (file)
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
 
+#include "iotivity_config.h"
 #include <stdio.h>
 #include <string.h>
 #include <string>
 #include <stdlib.h>
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
+#endif
+#ifdef HAVE_WINDOWS_H
+#include <windows.h>
+#endif
 #include <signal.h>
+#ifdef HAVE_PTHREAD_H
 #include <pthread.h>
+#endif
 #include <ocstack.h>
 #include <logger.h>
+#include <getopt.h>
 #include "ocpayload.h"
-
-const char *getResult(OCStackResult result);
+#include "common.h"
 
 #define TAG PCF("ocservercontainer")
 
@@ -59,10 +67,10 @@ typedef enum
 
 void PrintUsage()
 {
-    OC_LOG(INFO, TAG, "Usage : ocservercoll -t <Test Case>");
-    OC_LOG(INFO, TAG,
+    OIC_LOG(INFO, TAG, "Usage : ocservercoll -t <Test Case>");
+    OIC_LOG(INFO, TAG,
             "Test Case 1 : Create room resource with default collection entity handler.");
-    OC_LOG(INFO, TAG,
+    OIC_LOG(INFO, TAG,
             "Test Case 2 : Create room resource with application collection entity handler.");
 }
 
@@ -99,34 +107,34 @@ PrintReceivedMsgInfo(OCEntityHandlerFlag flag, OCEntityHandlerRequest * ehReques
         typeOfMethod = "OC_REST_PUT";
     }
 
-    OC_LOG_V(INFO, TAG, "Receiving message type: %s, method %s", typeOfMessage,
+    OIC_LOG_V(INFO, TAG, "Receiving message type: %s, method %s", typeOfMessage,
             typeOfMethod);
 }
 
 //The only case when this entity handler is for a non-existing resource.
 OCEntityHandlerResult
 OCDeviceEntityHandlerCb (OCEntityHandlerFlag flag,
-        OCEntityHandlerRequest *entityHandlerRequest, char* uri, void* callbackParam)
+        OCEntityHandlerRequest *entityHandlerRequest, char* uri, void* /*callbackParam*/)
 {
-    OC_LOG_V(INFO, TAG, "Inside device default entity handler - flags: 0x%x, uri: %s", flag, uri);
+    OIC_LOG_V(INFO, TAG, "Inside device default entity handler - flags: 0x%x, uri: %s", flag, uri);
 
     OCEntityHandlerResult ehResult = OC_EH_OK;
     OCEntityHandlerResponse response;
 
     if (!entityHandlerRequest)
     {
-        OC_LOG(ERROR, TAG, "Invalid request pointer");
+        OIC_LOG(ERROR, TAG, "Invalid request pointer");
         return OC_EH_ERROR;
     }
 
     if (entityHandlerRequest->resource == NULL)
     {
-        OC_LOG(INFO, TAG, "Received request from client to a non-existing resource");
+        OIC_LOG(INFO, TAG, "Received request from client to a non-existing resource");
         ehResult = OC_EH_RESOURCE_NOT_FOUND;
     }
     else
     {
-        OC_LOG_V(INFO, TAG, "Device Handler: Received unsupported request from client %d",
+        OIC_LOG_V(INFO, TAG, "Device Handler: Received unsupported request from client %d",
                         entityHandlerRequest->method);
         ehResult = OC_EH_ERROR;
     }
@@ -147,7 +155,7 @@ OCDeviceEntityHandlerCb (OCEntityHandlerFlag flag,
         // Send the response
         if (OCDoResponse(&response) != OC_STACK_OK)
         {
-            OC_LOG(ERROR, TAG, "Error sending response");
+            OIC_LOG(ERROR, TAG, "Error sending response");
             ehResult = OC_EH_ERROR;
         }
     }
@@ -156,18 +164,18 @@ OCDeviceEntityHandlerCb (OCEntityHandlerFlag flag,
 
 OCEntityHandlerResult OCEntityHandlerRoomCb(OCEntityHandlerFlag flag,
                                             OCEntityHandlerRequest * ehRequest,
-                                            void* callback)
+                                            void* /*callback*/)
 {
     OCEntityHandlerResult ret = OC_EH_OK;
     OCEntityHandlerResponse response;
-    OCRepPayload* payload = OCRepPayloadCreate();
 
-    OC_LOG_V(INFO, TAG, "Callback for Room");
+    OIC_LOG_V(INFO, TAG, "Callback for Room");
     PrintReceivedMsgInfo(flag, ehRequest );
 
     if(ehRequest && flag == OC_REQUEST_FLAG )
     {
         std::string query = (const char*)ehRequest->query;
+        OCRepPayload* payload = OCRepPayloadCreate();
 
         if(OC_REST_GET == ehRequest->method)
         {
@@ -229,7 +237,7 @@ OCEntityHandlerResult OCEntityHandlerRoomCb(OCEntityHandlerFlag flag,
                 // Send the response
                 if (OCDoResponse(&response) != OC_STACK_OK)
                 {
-                    OC_LOG(ERROR, TAG, "Error sending response");
+                    OIC_LOG(ERROR, TAG, "Error sending response");
                     ret = OC_EH_ERROR;
                 }
             }
@@ -302,17 +310,18 @@ OCEntityHandlerResult OCEntityHandlerRoomCb(OCEntityHandlerFlag flag,
                 // Send the response
                 if (OCDoResponse(&response) != OC_STACK_OK)
                 {
-                    OC_LOG(ERROR, TAG, "Error sending response");
+                    OIC_LOG(ERROR, TAG, "Error sending response");
                     ret = OC_EH_ERROR;
                 }
             }
         }
         else
         {
-            OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
+            OIC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
                     ehRequest->method);
             ret = OC_EH_ERROR;
         }
+        OCRepPayloadDestroy(payload);
     }
     else if (ehRequest && flag == OC_OBSERVE_FLAG)
     {
@@ -322,17 +331,17 @@ OCEntityHandlerResult OCEntityHandlerRoomCb(OCEntityHandlerFlag flag,
 }
 
 OCEntityHandlerResult OCEntityHandlerLightCb(OCEntityHandlerFlag flag,
-        OCEntityHandlerRequest * ehRequest,void* callbackParam)
+        OCEntityHandlerRequest * ehRequest,void* /*callbackParam*/)
 {
     OCEntityHandlerResult ret = OC_EH_OK;
     OCEntityHandlerResponse response;
-    OCRepPayload* payload = OCRepPayloadCreate();
 
-    OC_LOG_V(INFO, TAG, "Callback for Light");
+    OIC_LOG_V(INFO, TAG, "Callback for Light");
     PrintReceivedMsgInfo(flag, ehRequest );
 
     if(ehRequest && flag == OC_REQUEST_FLAG)
     {
+        OCRepPayload* payload = OCRepPayloadCreate();
         if(OC_REST_GET == ehRequest->method)
         {
             OCRepPayloadSetUri(payload, gLightResourceUri);
@@ -347,7 +356,7 @@ OCEntityHandlerResult OCEntityHandlerLightCb(OCEntityHandlerFlag flag,
         }
         else
         {
-            OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
+            OIC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
                     ehRequest->method);
             ret = OC_EH_ERROR;
         }
@@ -369,10 +378,11 @@ OCEntityHandlerResult OCEntityHandlerLightCb(OCEntityHandlerFlag flag,
             // Send the response
             if (OCDoResponse(&response) != OC_STACK_OK)
             {
-                OC_LOG(ERROR, TAG, "Error sending response");
+                OIC_LOG(ERROR, TAG, "Error sending response");
                 ret = OC_EH_ERROR;
             }
         }
+        OCRepPayloadDestroy(payload);
     }
     else if (ehRequest && flag == OC_OBSERVE_FLAG)
     {
@@ -383,17 +393,18 @@ OCEntityHandlerResult OCEntityHandlerLightCb(OCEntityHandlerFlag flag,
 }
 
 OCEntityHandlerResult OCEntityHandlerFanCb(OCEntityHandlerFlag flag,
-        OCEntityHandlerRequest * ehRequest, void* callback)
+        OCEntityHandlerRequest * ehRequest, void* /*callback*/)
 {
     OCEntityHandlerResult ret = OC_EH_OK;
     OCEntityHandlerResponse response;
-    OCRepPayload* payload = OCRepPayloadCreate();
 
-    OC_LOG_V(INFO, TAG, "Callback for Fan");
+    OIC_LOG_V(INFO, TAG, "Callback for Fan");
     PrintReceivedMsgInfo(flag, ehRequest );
 
     if(ehRequest && flag == OC_REQUEST_FLAG)
     {
+        OCRepPayload* payload = OCRepPayloadCreate();
+
         if(OC_REST_GET == ehRequest->method)
         {
             OCRepPayloadSetUri(payload, gFanResourceUri);
@@ -408,7 +419,7 @@ OCEntityHandlerResult OCEntityHandlerFanCb(OCEntityHandlerFlag flag,
         }
         else
         {
-            OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
+            OIC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
                     ehRequest->method);
             ret = OC_EH_ERROR;
         }
@@ -430,10 +441,11 @@ OCEntityHandlerResult OCEntityHandlerFanCb(OCEntityHandlerFlag flag,
             // Send the response
             if (OCDoResponse(&response) != OC_STACK_OK)
             {
-                OC_LOG(ERROR, TAG, "Error sending response");
+                OIC_LOG(ERROR, TAG, "Error sending response");
                 ret = OC_EH_ERROR;
             }
         }
+        OCRepPayloadDestroy(payload);
 
     }
     else if (ehRequest && flag == OC_OBSERVE_FLAG)
@@ -464,7 +476,7 @@ void *ChangeLightRepresentation (void *param)
         light.power += 5;
         if (gLightUnderObservation)
         {
-            OC_LOG_V(INFO, TAG,
+            OIC_LOG_V(INFO, TAG,
                 " =====> Notifying stack of new power level %d\n", light.power);
             result = OCNotifyAllObservers (light.handle, OC_NA_QOS);
             if (OC_STACK_NO_OBSERVERS == result)
@@ -499,11 +511,11 @@ int main(int argc, char* argv[])
         return -1;
     }
 
-    OC_LOG(DEBUG, TAG, "OCServer is starting...");
+    OIC_LOG(DEBUG, TAG, "OCServer is starting...");
 
     if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
     {
-        OC_LOG(ERROR, TAG, "OCStack init error");
+        OIC_LOG(ERROR, TAG, "OCStack init error");
         return 0;
     }
 
@@ -520,13 +532,13 @@ int main(int argc, char* argv[])
     pthread_create (&threadId, NULL, ChangeLightRepresentation, (void *)NULL);
 
     // Break from loop with Ctrl-C
-    OC_LOG(INFO, TAG, "Entering ocserver main loop...");
+    OIC_LOG(INFO, TAG, "Entering ocserver main loop...");
     signal(SIGINT, handleSigInt);
     while (!gQuitFlag)
     {
         if (OCProcess() != OC_STACK_OK)
         {
-            OC_LOG(ERROR, TAG, "OCStack process error");
+            OIC_LOG(ERROR, TAG, "OCStack process error");
             return 0;
         }
         sleep(2);
@@ -535,14 +547,16 @@ int main(int argc, char* argv[])
     /*
      * Cancel the light thread and wait for it to terminate
      */
+#ifdef HAVE_PTHREAD_H
     pthread_cancel(threadId);
     pthread_join(threadId, NULL);
+#endif
 
-    OC_LOG(INFO, TAG, "Exiting ocserver main loop...");
+    OIC_LOG(INFO, TAG, "Exiting ocserver main loop...");
 
     if (OCStop() != OC_STACK_OK)
     {
-        OC_LOG(ERROR, TAG, "OCStack process error");
+        OIC_LOG(ERROR, TAG, "OCStack process error");
     }
 
     return 0;
@@ -560,7 +574,7 @@ void createResources()
             OCEntityHandlerFanCb,
             NULL,
             OC_DISCOVERABLE|OC_OBSERVABLE);
-    OC_LOG_V(INFO, TAG, "Created fan resource with result: %s", getResult(res));
+    OIC_LOG_V(INFO, TAG, "Created fan resource with result: %s", getResult(res));
 
     OCResourceHandle light;
     res = OCCreateResource(&light,
@@ -570,7 +584,7 @@ void createResources()
             OCEntityHandlerLightCb,
             NULL,
             OC_DISCOVERABLE|OC_OBSERVABLE);
-    OC_LOG_V(INFO, TAG, "Created light resource with result: %s", getResult(res));
+    OIC_LOG_V(INFO, TAG, "Created light resource with result: %s", getResult(res));
 
     OCResourceHandle room;
 
@@ -595,14 +609,14 @@ void createResources()
                 OC_DISCOVERABLE);
     }
 
-    OC_LOG_V(INFO, TAG, "Created room resource with result: %s", getResult(res));
+    OIC_LOG_V(INFO, TAG, "Created room resource with result: %s", getResult(res));
     OCBindResourceInterfaceToResource(room, OC_RSRVD_INTERFACE_LL);
     OCBindResourceInterfaceToResource(room, OC_RSRVD_INTERFACE_DEFAULT);
 
     res = OCBindResource(room, light);
-    OC_LOG_V(INFO, TAG, "OC Bind Contained Resource to resource: %s", getResult(res));
+    OIC_LOG_V(INFO, TAG, "OC Bind Contained Resource to resource: %s", getResult(res));
 
     res = OCBindResource(room, fan);
-    OC_LOG_V(INFO, TAG, "OC Bind Contained Resource to resource: %s", getResult(res));
+    OIC_LOG_V(INFO, TAG, "OC Bind Contained Resource to resource: %s", getResult(res));
 }