replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / examples / simpleclient.cpp
index 6bc6efe..5667b07 100644 (file)
@@ -20,6 +20,7 @@
 
 // OCClient.cpp : Defines the entry point for the console application.
 //
+#include "iotivity_config.h"
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
@@ -45,6 +46,7 @@ typedef std::map<OCResourceIdentifier, std::shared_ptr<OCResource>> DiscoveredRe
 DiscoveredResourceMap discoveredResources;
 std::shared_ptr<OCResource> curResource;
 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
+static OCConnectivityType TRANSPORT_TYPE_TO_USE = OCConnectivityType::CT_ADAPTER_IP;
 std::mutex curResourceLock;
 
 class Light
@@ -73,16 +75,12 @@ void onObserve(const HeaderOptions /*headerOptions*/, const OCRepresentation& re
 {
     try
     {
-        if(eCode == OC_STACK_OK && sequenceNumber != OC_OBSERVE_NO_OPTION)
+        if(eCode == OC_STACK_OK && sequenceNumber <= MAX_SEQUENCE_NUMBER)
         {
             if(sequenceNumber == OC_OBSERVE_REGISTER)
             {
                 std::cout << "Observe registration action is successful" << std::endl;
             }
-            else if(sequenceNumber == OC_OBSERVE_DEREGISTER)
-            {
-                std::cout << "Observe De-registration action is successful" << std::endl;
-            }
 
             std::cout << "OBSERVE RESULT:"<<std::endl;
             std::cout << "\tSequenceNumber: "<< sequenceNumber << std::endl;
@@ -107,9 +105,11 @@ void onObserve(const HeaderOptions /*headerOptions*/, const OCRepresentation& re
         }
         else
         {
-            if(sequenceNumber == OC_OBSERVE_NO_OPTION)
+            if(eCode == OC_STACK_OK)
             {
-                std::cout << "Observe registration or de-registration action is failed" << std::endl;
+                std::cout << "No observe option header is returned in the response." << std::endl;
+                std::cout << "For a registration request, it means the registration failed"
+                        << std::endl;
             }
             else
             {
@@ -130,7 +130,8 @@ void onPost2(const HeaderOptions& /*headerOptions*/,
 {
     try
     {
-        if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
+        if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED
+                || eCode == OC_STACK_RESOURCE_CHANGED)
         {
             std::cout << "POST request was successful" << std::endl;
 
@@ -176,7 +177,8 @@ void onPost(const HeaderOptions& /*headerOptions*/,
 {
     try
     {
-        if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
+        if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED
+                || eCode == OC_STACK_RESOURCE_CHANGED)
         {
             std::cout << "POST request was successful" << std::endl;
 
@@ -245,7 +247,7 @@ void onPut(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep,
 {
     try
     {
-        if(eCode == OC_STACK_OK)
+        if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CHANGED)
         {
             std::cout << "PUT request was successful" << std::endl;
 
@@ -392,9 +394,15 @@ void foundResource(std::shared_ptr<OCResource> resource)
 
             if(resourceURI == "/a/light")
             {
-                curResource = resource;
-                // Call a local function which will internally invoke get API on the resource pointer
-                getLightRepresentation(resource);
+                if (resource->connectivityType() & TRANSPORT_TYPE_TO_USE)
+                {
+                    curResource = resource;
+                    // Get the resource host address
+                    std::cout << "\tAddress of selected resource: " << resource->host() << std::endl;
+
+                    // Call a local function which will internally invoke get API on the resource pointer
+                    getLightRepresentation(resource);
+                }
             }
         }
         else
@@ -414,9 +422,11 @@ void printUsage()
 {
     std::cout << std::endl;
     std::cout << "---------------------------------------------------------------------\n";
-    std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
+    std::cout << "Usage : simpleclient <ObserveType> <TransportType>" << std::endl;
     std::cout << "   ObserveType : 1 - Observe" << std::endl;
     std::cout << "   ObserveType : 2 - ObserveAll" << std::endl;
+    std::cout << "   TransportType : 1 - IP" << std::endl;
+    std::cout << "   TransportType : 2 - TCP" << std::endl;
     std::cout << "---------------------------------------------------------------------\n\n";
 }
 
@@ -439,6 +449,25 @@ void checkObserverValue(int value)
     }
 }
 
+void checkTransportValue(int value)
+{
+    if (1 == value)
+    {
+        TRANSPORT_TYPE_TO_USE = OCConnectivityType::CT_ADAPTER_IP;
+        std::cout << "<===Setting TransportType to IP===>\n\n";
+    }
+    else if (2 == value)
+    {
+        TRANSPORT_TYPE_TO_USE = OCConnectivityType::CT_ADAPTER_TCP;
+        std::cout << "<===Setting TransportType to TCP===>\n\n";
+    }
+    else
+    {
+        std::cout << "<===Invalid TransportType selected."
+                  <<" Setting TransportType to IP===>\n\n";
+    }
+}
+
 static FILE* client_open(const char* /*path*/, const char *mode)
 {
     return fopen(SVR_DB_FILE_NAME, mode);
@@ -447,7 +476,7 @@ static FILE* client_open(const char* /*path*/, const char *mode)
 int main(int argc, char* argv[]) {
 
     std::ostringstream requestURI;
-    OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
+    OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink, NULL, NULL};
     try
     {
         printUsage();
@@ -459,6 +488,11 @@ int main(int argc, char* argv[]) {
         {
             checkObserverValue(std::stoi(argv[1]));
         }
+        else if (argc == 3)
+        {
+            checkObserverValue(std::stoi(argv[1]));
+            checkTransportValue(std::stoi(argv[2]));
+        }
         else
         {
             std::cout << "<===Invalid number of command line arguments===>\n\n";
@@ -475,9 +509,10 @@ int main(int argc, char* argv[]) {
     PlatformConfig cfg {
         OC::ServiceType::InProc,
         OC::ModeType::Both,
-        "0.0.0.0",
-        0,
-        OC::QualityOfService::LowQos,
+        OCConnectivityType::CT_ADAPTER_IP,
+        OCConnectivityType::CT_ADAPTER_IP,
+        (OCTransportAdapter)(OCTransportAdapter::OC_ADAPTER_IP|OCTransportAdapter::OC_ADAPTER_TCP),
+        OC::QualityOfService::HighQos,
         &ps
     };