Delete client callback for error response.
[platform/upstream/iotivity.git] / resource / examples / simpleclientHQ.cpp
index af0bea4..6d631d3 100644 (file)
 
 // OCClient.cpp : Defines the entry point for the console application.
 //
+#include "iotivity_config.h"
+
 #include <set>
 #include <string>
 #include <cstdlib>
-#include <pthread.h>
 #include <mutex>
 #include <condition_variable>
 #include "OCPlatform.h"
 #include "OCApi.h"
 
+#if defined(HAVE_PTHREAD_H)
+#include <pthread.h>
+#endif
+#if defined(HAVE_WINDOWS_H)
+#include <windows.h>
+#endif
+
 using namespace OC;
 
 struct dereference_compare
@@ -67,7 +75,7 @@ int observe_count()
     return ++oc;
 }
 
-void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
+void onObserve(const HeaderOptions /*headerOptions*/, const OCRepresentation& rep,
                     const int& eCode, const int& sequenceNumber)
 {
     if(eCode == SUCCESS_RESPONSE)
@@ -97,7 +105,7 @@ void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
         std::cout << "\tpower: " << mylight.m_power << std::endl;
         std::cout << "\tname: " << mylight.m_name << std::endl;
 
-        if(observe_count() > 10)
+        if(observe_count() == 11)
         {
             std::cout<<"Cancelling Observe..."<<std::endl;
             OCStackResult result = curResource->cancelObserve(OC::QualityOfService::HighQos);
@@ -112,9 +120,9 @@ void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
     }
 }
 
-void onPost2(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
+void onPost2(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
 {
-    if(eCode == SUCCESS_RESPONSE)
+    if(eCode == SUCCESS_RESPONSE || eCode == OC_STACK_RESOURCE_CHANGED)
     {
         std::cout << "POST request was successful" << std::endl;
 
@@ -150,9 +158,10 @@ void onPost2(const HeaderOptions& headerOptions, const OCRepresentation& rep, co
     }
 }
 
-void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
+void onPost(const HeaderOptions& /*headerOptions*/,
+        const OCRepresentation& rep, const int eCode)
 {
-    if(eCode == SUCCESS_RESPONSE)
+    if(eCode == SUCCESS_RESPONSE || eCode == OC_STACK_RESOURCE_CHANGED)
     {
         std::cout << "POST request was successful" << std::endl;
 
@@ -212,9 +221,9 @@ void postLightRepresentation(std::shared_ptr<OCResource> resource)
 }
 
 // callback handler on PUT request
-void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
+void onPut(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
 {
-    if(eCode == SUCCESS_RESPONSE)
+    if(eCode == SUCCESS_RESPONSE || eCode == OC_STACK_RESOURCE_CHANGED)
     {
         std::cout << "PUT request was successful" << std::endl;
 
@@ -256,7 +265,7 @@ void putLightRepresentation(std::shared_ptr<OCResource> resource)
 }
 
 // Callback handler on GET request
-void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
+void onGet(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
 {
     if(eCode == SUCCESS_RESPONSE)
     {
@@ -370,35 +379,57 @@ void foundResource(std::shared_ptr<OCResource> resource)
 void PrintUsage()
 {
     std::cout << std::endl;
-    std::cout << "Usage : simpleclientHQ <ObserveType>" << std::endl;
+    std::cout << "Usage : simpleclientHQ <ObserveType> <ConnectivityType>" << std::endl;
     std::cout << "   ObserveType : 1 - Observe" << std::endl;
     std::cout << "   ObserveType : 2 - ObserveAll" << std::endl;
+    std::cout << "   ConnectivityType: Default IP" << std::endl;
+    std::cout << "   ConnectivityType : 0 - IP"<< std::endl;
 }
 
 int main(int argc, char* argv[]) {
 
     std::ostringstream requestURI;
 
+    OCConnectivityType connectivityType = CT_ADAPTER_IP;
     try
     {
         if (argc == 1)
         {
             OBSERVE_TYPE_TO_USE = ObserveType::Observe;
         }
-        else if (argc == 2)
+        else if (argc ==2 || argc==3)
         {
             int value = std::stoi(argv[1]);
             if (value == 1)
-            {
                 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
-            }
             else if (value == 2)
-            {
                 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
-            }
             else
-            {
                 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
+
+            if(argc == 3)
+            {
+                std::size_t inputValLen;
+                int optionSelected = std::stoi(argv[2], &inputValLen);
+
+                if(inputValLen == strlen(argv[2]))
+                {
+                    if(optionSelected == 0)
+                    {
+                        std::cout << "Using IP."<< std::endl;
+                        connectivityType = CT_ADAPTER_IP;
+                    }
+                    else
+                    {
+                        std::cout << "Invalid connectivity type selected. Using default IP"
+                            << std::endl;
+                    }
+                }
+                else
+                {
+                    std::cout << "Invalid connectivity type selected. Using default IP"
+                            << std::endl;
+                }
             }
         }
         else
@@ -409,9 +440,12 @@ int main(int argc, char* argv[]) {
     }
     catch(std::exception&)
     {
-        std::cout << "Invalid input argument. Using Observe as observe type" << std::endl;
+        std::cout << "Invalid input argument." << std::endl;
+        PrintUsage();
+        return -1;
     }
 
+
     // Create PlatformConfig object
     PlatformConfig cfg {
         OC::ServiceType::InProc,
@@ -426,17 +460,17 @@ int main(int argc, char* argv[]) {
     try
     {
         // Find all resources
-        requestURI << OC_MULTICAST_DISCOVERY_URI << "?rt=core.light";
+        requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.light";
 
         OCPlatform::findResource("", requestURI.str(),
-                OC_ALL, &foundResource, OC::QualityOfService::LowQos);
+                connectivityType, &foundResource, OC::QualityOfService::LowQos);
         std::cout<< "Finding Resource... " <<std::endl;
 
         // Find resource is done twice so that we discover the original resources a second time.
         // These resources will have the same uniqueidentifier (yet be different objects), so that
         // we can verify/show the duplicate-checking code in foundResource(above);
         OCPlatform::findResource("", requestURI.str(),
-                OC_ALL, &foundResource, OC::QualityOfService::LowQos);
+                connectivityType, &foundResource, OC::QualityOfService::LowQos);
         std::cout<< "Finding Resource for second time... " <<std::endl;
 
         // A condition variable will free the mutex it is given, then do a non-