replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / stack / samples / linux / SimpleClientServer / ocremoteaccessclient.cpp
index 94a8ad3..7090bef 100644 (file)
@@ -30,6 +30,7 @@
 #include "ocpayload.h"
 #include "payload_logging.h"
 #include "ocremoteaccessclient.h"
+#include "common.h"
 
 #define SET_BUT_NOT_USED(x) (void) x
 // Tracking user input
@@ -43,7 +44,6 @@ static std::string coapServerResource = "/a/light";
 static OCDevAddr responseAddr;
 //Use ipv4addr for both InitDiscovery and InitPlatformOrDeviceDiscovery
 char remoteServerJabberID[MAX_ADDR_STR_SIZE];
-void StripNewLineChar(char* str);
 static uint16_t maxNotification = 15;
 
 // The handle for the observe registration
@@ -85,15 +85,15 @@ OCPayload* putPayload()
 
 static void PrintUsage()
 {
-    OC_LOG(INFO, TAG, "This sample makes all requests via the remote access adapter");
-    OC_LOG(INFO, TAG, "Usage : ocremoteaccessclient -t <number>");
-    OC_LOG(INFO, TAG, "-t 1  :  Discover Resources");
-    OC_LOG(INFO, TAG, "-t 2  :  Discover & Get");
-    OC_LOG(INFO, TAG, "-t 3  :  Discover & Put");
-    OC_LOG(INFO, TAG, "-t 4  :  Discover & Post");
-    OC_LOG(INFO, TAG, "-t 5  :  Discover & Delete");
-    OC_LOG(INFO, TAG, "-t 6  :  Discover & Observe");
-    OC_LOG(INFO, TAG, "-t 7  :  Discover & Observe then cancel immediately with High QOS");
+    OIC_LOG(INFO, TAG, "This sample makes all requests via the remote access adapter");
+    OIC_LOG(INFO, TAG, "Usage : ocremoteaccessclient -t <number>");
+    OIC_LOG(INFO, TAG, "-t 1  :  Discover Resources");
+    OIC_LOG(INFO, TAG, "-t 2  :  Discover & Get");
+    OIC_LOG(INFO, TAG, "-t 3  :  Discover & Put");
+    OIC_LOG(INFO, TAG, "-t 4  :  Discover & Post");
+    OIC_LOG(INFO, TAG, "-t 5  :  Discover & Delete");
+    OIC_LOG(INFO, TAG, "-t 6  :  Discover & Observe");
+    OIC_LOG(INFO, TAG, "-t 7  :  Discover & Observe then cancel immediately with High QOS");
 }
 
 OCStackResult InvokeOCDoResource(std::ostringstream &query,
@@ -110,21 +110,24 @@ OCStackResult InvokeOCDoResource(std::ostringstream &query,
     cbData.context  = (void*)DEFAULT_CONTEXT_VALUE;
     cbData.cd       = NULL;
 
-    OCStackResult ret = OCDoResource(
-        &handle,
-        method,
-        query.str().c_str(),
-        &responseAddr,
-        (method == OC_REST_PUT) ? putPayload() : NULL,
-        CT_ADAPTER_REMOTE_ACCESS,
-        qos,
-        &cbData,
-        options,
-        numOptions);
+    OCPayload* payload = (method == OC_REST_PUT) ? putPayload() : NULL;
+
+    OCStackResult ret = OCDoRequest(&handle,
+                                    method,
+                                    query.str().c_str(),
+                                    &responseAddr,
+                                    payload,
+                                    CT_ADAPTER_REMOTE_ACCESS,
+                                    qos,
+                                    &cbData,
+                                    options,
+                                    numOptions);
+
+    OCPayloadDestroy(payload);
 
     if (ret != OC_STACK_OK)
     {
-        OC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
+        OIC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
     }
     else if (method == OC_REST_OBSERVE || method == OC_REST_OBSERVE_ALL)
     {
@@ -145,21 +148,21 @@ OCStackApplicationResult restRequestCB(void* ctx,
 {
     if(clientResponse == NULL)
     {
-        OC_LOG(INFO, TAG, "Received NULL response");
+        OIC_LOG(INFO, TAG, "Received NULL response");
         return   OC_STACK_DELETE_TRANSACTION;
     }
     if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
     {
-        OC_LOG(INFO, TAG, "Callback Context recvd successfully");
+        OIC_LOG(INFO, TAG, "Callback Context recvd successfully");
     }
 
-    OC_LOG_V(INFO, TAG, "StackResult: %s",  getResult(clientResponse->result));
-    OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
-    OC_LOG_PAYLOAD(INFO, clientResponse->payload);
+    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);
 
     if(clientResponse->numRcvdVendorSpecificHeaderOptions > 0)
     {
-        OC_LOG (INFO, TAG, "Received vendor specific options. Ignoring");
+        OIC_LOG (INFO, TAG, "Received vendor specific options. Ignoring");
     }
     SET_BUT_NOT_USED(handle);
     return OC_STACK_DELETE_TRANSACTION;
@@ -169,23 +172,23 @@ OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle handle, OCClientResponse
 {
     if(!clientResponse)
     {
-        OC_LOG_V(INFO, TAG, "obsReqCB received NULL response");
+        OIC_LOG_V(INFO, TAG, "obsReqCB received NULL response");
     }
     if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
     {
-        OC_LOG(INFO, TAG, "Callback Context recvd successfully");
+        OIC_LOG(INFO, TAG, "Callback Context recvd successfully");
     }
-    OC_LOG_V(INFO, TAG, "StackResult: %s",  getResult(clientResponse->result));
-    OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
-    OC_LOG_V(INFO, TAG, "OBSERVE notification %d recvd", gNumObserveNotifies);
-    OC_LOG_PAYLOAD(INFO, clientResponse->payload);
+    OIC_LOG_V(INFO, TAG, "StackResult: %s",  getResult(clientResponse->result));
+    OIC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
+    OIC_LOG_V(INFO, TAG, "OBSERVE notification %d recvd", gNumObserveNotifies);
+    OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
 
     gNumObserveNotifies++;
     if (gNumObserveNotifies == maxNotification)
     {
         if (OCCancel (gObserveDoHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
         {
-            OC_LOG(ERROR, TAG, "Observe cancel error");
+            OIC_LOG(ERROR, TAG, "Observe cancel error");
         }
         return OC_STACK_DELETE_TRANSACTION;
     }
@@ -193,21 +196,21 @@ OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle handle, OCClientResponse
     {
         if (OCCancel (gObserveDoHandle, OC_HIGH_QOS, NULL, 0) != OC_STACK_OK)
         {
-            OC_LOG(ERROR, TAG, "Observe cancel error");
+            OIC_LOG(ERROR, TAG, "Observe cancel error");
         }
     }
     if(clientResponse->sequenceNumber == OC_OBSERVE_REGISTER)
     {
-        OC_LOG(INFO, TAG, "Registration confirmed");
+        OIC_LOG(INFO, TAG, "Registration confirmed");
     }
     else if(clientResponse->sequenceNumber == OC_OBSERVE_DEREGISTER)
     {
-        OC_LOG(INFO, TAG, "de-registration confirmed");
+        OIC_LOG(INFO, TAG, "de-registration confirmed");
         return OC_STACK_DELETE_TRANSACTION;
     }
     else if(clientResponse->sequenceNumber == OC_OBSERVE_NO_OPTION)
     {
-        OC_LOG(INFO, TAG, "Registration/deregistration failed");
+        OIC_LOG(INFO, TAG, "Registration/deregistration failed");
         return OC_STACK_DELETE_TRANSACTION;
     }
 
@@ -220,29 +223,29 @@ OCStackApplicationResult presenceCB(void* ctx,
 {
     if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
     {
-        OC_LOG(INFO, TAG, "Callback Context recvd successfully");
+        OIC_LOG(INFO, TAG, "Callback Context recvd successfully");
     }
 
     if (clientResponse)
     {
-        OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
-        OC_LOG_V(INFO, TAG, "NONCE NUMBER: %u", clientResponse->sequenceNumber);
-        OC_LOG_V(INFO, TAG, "PRESENCE notification %d recvd", gNumPresenceNotifies);
-        OC_LOG_PAYLOAD(INFO, clientResponse->payload);
+        OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
+        OIC_LOG_V(INFO, TAG, "NONCE NUMBER: %u", clientResponse->sequenceNumber);
+        OIC_LOG_V(INFO, TAG, "PRESENCE notification %d recvd", gNumPresenceNotifies);
+        OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
 
         gNumPresenceNotifies++;
         if (gNumPresenceNotifies == maxNotification)
         {
             if (OCCancel(gPresenceHandle, OC_LOW_QOS, NULL, 0) != OC_STACK_OK)
             {
-                OC_LOG(ERROR, TAG, "Presence cancel error");
+                OIC_LOG(ERROR, TAG, "Presence cancel error");
             }
             return OC_STACK_DELETE_TRANSACTION;
         }
     }
     else
     {
-        OC_LOG_V(INFO, TAG, "presenceCB received Null clientResponse");
+        OIC_LOG_V(INFO, TAG, "presenceCB received Null clientResponse");
     }
     SET_BUT_NOT_USED(handle);
     return OC_STACK_KEEP_TRANSACTION;
@@ -255,22 +258,22 @@ OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
 {
     if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
     {
-        OC_LOG(INFO, TAG, "DISCOVER  callback recvd");
+        OIC_LOG(INFO, TAG, "DISCOVER  callback recvd");
     }
 
     if (!clientResponse)
     {
-        OC_LOG_V(INFO, TAG, "discoveryReqCB received Null clientResponse");
+        OIC_LOG_V(INFO, TAG, "discoveryReqCB received Null clientResponse");
     }
 
-    OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
-    OC_LOG_PAYLOAD(INFO, clientResponse->payload);
+    OIC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
+    OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
 
     responseAddr = clientResponse->devAddr;
 
     switch(TEST_CASE)
     {
-        OC_LOG_V(INFO, TAG, "TEST_CASE %u\n", TEST_CASE);
+        OIC_LOG_V(INFO, TAG, "TEST_CASE %u\n", TEST_CASE);
         case TEST_GET_REQ_NON:
             InitGetRequest(OC_LOW_QOS);
             break;
@@ -300,16 +303,16 @@ OCStackApplicationResult PlatformDiscoveryReqCB (void* ctx, OCDoHandle handle,
 {
     if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
     {
-        OC_LOG(INFO, TAG, "Callback Context for Platform DISCOVER query recvd successfully");
+        OIC_LOG(INFO, TAG, "Callback Context for Platform DISCOVER query recvd successfully");
     }
 
     if(clientResponse)
     {
-        OC_LOG_PAYLOAD(INFO, clientResponse->payload);
+        OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
     }
     else
     {
-        OC_LOG_V(INFO, TAG, "PlatformDiscoveryReqCB received Null clientResponse");
+        OIC_LOG_V(INFO, TAG, "PlatformDiscoveryReqCB received Null clientResponse");
     }
 
     SET_BUT_NOT_USED(handle);
@@ -321,16 +324,16 @@ OCStackApplicationResult DeviceDiscoveryReqCB (void* ctx, OCDoHandle handle,
 {
     if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
     {
-        OC_LOG(INFO, TAG, "Callback Context for Device DISCOVER query recvd successfully");
+        OIC_LOG(INFO, TAG, "Callback Context for Device DISCOVER query recvd successfully");
     }
 
     if(clientResponse)
     {
-        OC_LOG_PAYLOAD(INFO, clientResponse->payload);
+        OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
     }
     else
     {
-        OC_LOG_V(INFO, TAG, "PlatformDiscoveryReqCB received Null clientResponse");
+        OIC_LOG_V(INFO, TAG, "PlatformDiscoveryReqCB received Null clientResponse");
     }
 
     SET_BUT_NOT_USED(handle);
@@ -339,7 +342,7 @@ OCStackApplicationResult DeviceDiscoveryReqCB (void* ctx, OCDoHandle handle,
 
 int InitObserveRequest(OCQualityOfService qos)
 {
-    OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
+    OIC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
     std::ostringstream query;
     query << coapServerResource;
     return (InvokeOCDoResource(query,
@@ -348,7 +351,7 @@ int InitObserveRequest(OCQualityOfService qos)
 
 int InitPutRequest(OCQualityOfService qos)
 {
-    OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
+    OIC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
     std::ostringstream query;
     query << coapServerResource;
     return (InvokeOCDoResource(query, OC_REST_PUT, (qos == OC_HIGH_QOS)? OC_HIGH_QOS:OC_LOW_QOS,
@@ -357,7 +360,7 @@ int InitPutRequest(OCQualityOfService qos)
 
 int InitPostRequest(OCQualityOfService qos)
 {
-    OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
+    OIC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
     std::ostringstream query;
     query << coapServerResource;
     // First POST operation (to create an Light instance)
@@ -367,7 +370,7 @@ int InitPostRequest(OCQualityOfService qos)
     if (OC_STACK_OK != result)
     {
         // Error can happen if for example, network connectivity is down
-        OC_LOG(INFO, TAG, "First POST call did not succeed");
+        OIC_LOG(INFO, TAG, "First POST call did not succeed");
     }
 
     // Second POST operation (to create an Light instance)
@@ -376,7 +379,7 @@ int InitPostRequest(OCQualityOfService qos)
                                restRequestCB, NULL, 0);
     if (OC_STACK_OK != result)
     {
-        OC_LOG(INFO, TAG, "Second POST call did not succeed");
+        OIC_LOG(INFO, TAG, "Second POST call did not succeed");
     }
 
     // This POST operation will update the original resourced /a/light
@@ -389,7 +392,7 @@ int InitDeleteRequest(OCQualityOfService qos)
 {
     std::ostringstream query;
     query << coapServerResource;
-    OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
+    OIC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
 
     // First DELETE operation
     OCStackResult result = InvokeOCDoResource(query, OC_REST_DELETE,
@@ -398,14 +401,14 @@ int InitDeleteRequest(OCQualityOfService qos)
     if (OC_STACK_OK != result)
     {
         // Error can happen if for example, network connectivity is down
-        OC_LOG(INFO, TAG, "DELETE call did not succeed");
+        OIC_LOG(INFO, TAG, "DELETE call did not succeed");
     }
     return result;
 }
 
 int InitGetRequest(OCQualityOfService qos)
 {
-    OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
+    OIC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
     std::ostringstream query;
     query << coapServerResource;
     return (InvokeOCDoResource(query, OC_REST_GET,
@@ -424,28 +427,27 @@ int InitDiscovery(OCQualityOfService qos)
     dest.flags      = OC_DEFAULT_FLAGS;
     strncpy (dest.addr, remoteServerJabberID, MAX_ADDR_STR_SIZE - 1);
 
-    OCStackResult ret = OCDoResource(NULL,
-                OC_REST_GET,
-                MULTICAST_RESOURCE_DISCOVERY_QUERY,
-                &dest,
-                NULL,
-                CT_ADAPTER_REMOTE_ACCESS,
-                (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
-                &cbData,
-                NULL,
-                0
-            );
+    OCStackResult ret = OCDoRequest(NULL,
+                                    OC_REST_GET,
+                                    MULTICAST_RESOURCE_DISCOVERY_QUERY,
+                                    &dest,
+                                    NULL,
+                                    CT_ADAPTER_REMOTE_ACCESS,
+                                    (qos == OC_HIGH_QOS) ? OC_HIGH_QOS : OC_LOW_QOS,
+                                    &cbData,
+                                    NULL,
+                                    0);
 
     if (ret != OC_STACK_OK)
     {
-        OC_LOG_V(ERROR, TAG, "Error %u in OCDoResource with discovery", ret);
+        OIC_LOG_V(ERROR, TAG, "Error %u in OCDoResource with discovery", ret);
     }
     return ret;
 }
 
 static void jidbound(char *jid)
 {
-    OC_LOG_V(INFO, TAG, "\n\n    Bound JID: %s\n\n", jid);
+    OIC_LOG_V(INFO, TAG, "\n\n    Bound JID: %s\n\n", jid);
 }
 
 int main(int argc, char* argv[])
@@ -502,7 +504,7 @@ int main(int argc, char* argv[])
 
     if (OCSetRAInfo(&rainfo) != OC_STACK_OK)
     {
-        OC_LOG(ERROR, TAG, "Error initiating remote access adapter");
+        OIC_LOG(ERROR, TAG, "Error initiating remote access adapter");
         return 0;
     }
 
@@ -514,41 +516,41 @@ int main(int argc, char* argv[])
 
     if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
     {
-        OC_LOG(ERROR, TAG, "OCStack init error");
+        OIC_LOG(ERROR, TAG, "OCStack init error");
         return 0;
     }
 
-    OC_LOG(INFO, TAG, "Enter JID of remote server");
+    OIC_LOG(INFO, TAG, "Enter JID of remote server");
     if (fgets(remoteServerJabberID, MAX_ADDR_STR_SIZE, stdin))
     {
         StripNewLineChar(remoteServerJabberID);
     }
     else
     {
-        OC_LOG(ERROR, TAG, "Bad input for jabberID");
+        OIC_LOG(ERROR, TAG, "Bad input for jabberID");
         return OC_STACK_INVALID_PARAM;
     }
 
     InitDiscovery(OC_LOW_QOS);
 
     // Break from loop with Ctrl+C
-    OC_LOG(INFO, TAG, "Press CTRL+C to stop the stack");
+    OIC_LOG(INFO, TAG, "Press CTRL+C to stop the stack");
     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);
     }
-    OC_LOG(INFO, TAG, "Exiting ocremoteaccessclient main loop...");
+    OIC_LOG(INFO, TAG, "Exiting ocremoteaccessclient main loop...");
 
     if (OCStop() != OC_STACK_OK)
     {
-        OC_LOG(ERROR, TAG, "OCStack stop error");
+        OIC_LOG(ERROR, TAG, "OCStack stop error");
     }
 
     return 0;