replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / examples / simpleclient.cpp
index 4e3f820..5667b07 100644 (file)
 
 // OCClient.cpp : Defines the entry point for the console application.
 //
+#include "iotivity_config.h"
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#ifdef HAVE_PTHREAD_H
+#include <pthread.h>
+#endif
+#ifdef HAVE_WINDOWS_H
+#include <Windows.h>
+#endif
 #include <string>
 #include <map>
 #include <cstdlib>
-#include <pthread.h>
 #include <mutex>
 #include <condition_variable>
 #include "OCPlatform.h"
 
 using namespace OC;
 
+static const char* SVR_DB_FILE_NAME = "./oic_svr_db_client.dat";
 typedef std::map<OCResourceIdentifier, std::shared_ptr<OCResource>> DiscoveredResourceMap;
 
 DiscoveredResourceMap discoveredResources;
 std::shared_ptr<OCResource> curResource;
 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
-static OCConnectivityType connectivityType = OC_WIFI;
+static OCConnectivityType TRANSPORT_TYPE_TO_USE = OCConnectivityType::CT_ADAPTER_IP;
 std::mutex curResourceLock;
 
 class Light
@@ -60,13 +70,18 @@ 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)
 {
     try
     {
-        if(eCode == OC_STACK_OK)
+        if(eCode == OC_STACK_OK && sequenceNumber <= MAX_SEQUENCE_NUMBER)
         {
+            if(sequenceNumber == OC_OBSERVE_REGISTER)
+            {
+                std::cout << "Observe registration action is successful" << std::endl;
+            }
+
             std::cout << "OBSERVE RESULT:"<<std::endl;
             std::cout << "\tSequenceNumber: "<< sequenceNumber << std::endl;
             rep.getValue("state", mylight.m_state);
@@ -77,7 +92,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() > 30)
+            if(observe_count() == 11)
             {
                 std::cout<<"Cancelling Observe..."<<std::endl;
                 OCStackResult result = curResource->cancelObserve();
@@ -90,8 +105,17 @@ void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
         }
         else
         {
-            std::cout << "onObserve Response error: " << eCode << std::endl;
-            std::exit(-1);
+            if(eCode == OC_STACK_OK)
+            {
+                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
+            {
+                std::cout << "onObserve Response error: " << eCode << std::endl;
+                std::exit(-1);
+            }
         }
     }
     catch(std::exception& e)
@@ -101,11 +125,13 @@ 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)
 {
     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;
 
@@ -146,11 +172,13 @@ 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)
 {
     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;
 
@@ -215,11 +243,11 @@ 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)
 {
     try
     {
-        if(eCode == OC_STACK_OK)
+        if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CHANGED)
         {
             std::cout << "PUT request was successful" << std::endl;
 
@@ -266,7 +294,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)
 {
     try
     {
@@ -366,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
@@ -380,7 +414,7 @@ void foundResource(std::shared_ptr<OCResource> resource)
     }
     catch(std::exception& e)
     {
-        //log(e.what());
+        std::cerr << "Exception in foundResource: "<< e.what() << std::endl;
     }
 }
 
@@ -388,12 +422,11 @@ void printUsage()
 {
     std::cout << std::endl;
     std::cout << "---------------------------------------------------------------------\n";
-    std::cout << "Usage : simpleclient <ObserveType> <ConnectivityType>" << 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 << "   connectivityType: Default WIFI" << std::endl;
-    std::cout << "   ConnectivityType : 0 - ETHERNET"<< std::endl;
-    std::cout << "   ConnectivityType : 1 - WIFI"<< std::endl;
+    std::cout << "   TransportType : 1 - IP" << std::endl;
+    std::cout << "   TransportType : 2 - TCP" << std::endl;
     std::cout << "---------------------------------------------------------------------\n\n";
 }
 
@@ -416,47 +449,49 @@ void checkObserverValue(int value)
     }
 }
 
-void checkConnectivityValue(int value)
+void checkTransportValue(int value)
 {
-    if(value == 0)
+    if (1 == value)
     {
-        connectivityType = OC_ETHERNET;
-        std::cout << "<===Setting connectivityType  to Ethernet===>\n\n";
+        TRANSPORT_TYPE_TO_USE = OCConnectivityType::CT_ADAPTER_IP;
+        std::cout << "<===Setting TransportType to IP===>\n\n";
     }
-    else if(value == 1)
+    else if (2 == value)
     {
-        connectivityType = OC_WIFI;
-        std::cout << "<===Setting connectivityType  to WIFI===>\n\n";
+        TRANSPORT_TYPE_TO_USE = OCConnectivityType::CT_ADAPTER_TCP;
+        std::cout << "<===Setting TransportType to TCP===>\n\n";
     }
     else
     {
-        std::cout << "<===Invalid ConnectivitType selected."
-                  <<"Setting ConnectivityType to WIFI===>\n\n";
+        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);
+}
+
 int main(int argc, char* argv[]) {
 
     std::ostringstream requestURI;
-
+    OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink, NULL, NULL};
     try
     {
         printUsage();
         if (argc == 1)
         {
-            std::cout << "<===Setting ObserveType to Observe and ConnectivityType to WIFI===>\n\n";
+            std::cout << "<===Setting ObserveType to Observe and ConnectivityType to IP===>\n\n";
         }
         else if (argc == 2)
         {
-
             checkObserverValue(std::stoi(argv[1]));
-            std::cout << "<===No ConnectivtyType selected. "
-                      << "Setting ConnectivityType to WIFI===>\n\n";
         }
-        else if(argc == 3)
+        else if (argc == 3)
         {
             checkObserverValue(std::stoi(argv[1]));
-            checkConnectivityValue(std::stoi(argv[2]));
+            checkTransportValue(std::stoi(argv[2]));
         }
         else
         {
@@ -464,7 +499,7 @@ int main(int argc, char* argv[]) {
             return -1;
         }
     }
-    catch(std::exception& e)
+    catch(std::exception& )
     {
         std::cout << "<===Invalid input arguments===>\n\n";
         return -1;
@@ -473,10 +508,12 @@ int main(int argc, char* argv[]) {
     // Create PlatformConfig object
     PlatformConfig cfg {
         OC::ServiceType::InProc,
-        OC::ModeType::Client,
-        "0.0.0.0",
-        0,
-        OC::QualityOfService::LowQos
+        OC::ModeType::Both,
+        OCConnectivityType::CT_ADAPTER_IP,
+        OCConnectivityType::CT_ADAPTER_IP,
+        (OCTransportAdapter)(OCTransportAdapter::OC_ADAPTER_IP|OCTransportAdapter::OC_ADAPTER_TCP),
+        OC::QualityOfService::HighQos,
+        &ps
     };
 
     OCPlatform::Configure(cfg);
@@ -485,17 +522,17 @@ int main(int argc, char* argv[]) {
         // makes it so that all boolean values are printed as 'true/false' in this stream
         std::cout.setf(std::ios::boolalpha);
         // Find all resources
-        requestURI << OC_WELL_KNOWN_QUERY << "?rt=core.light";
+        requestURI << OC_RSRVD_WELL_KNOWN_URI;// << "?rt=core.light";
 
         OCPlatform::findResource("", requestURI.str(),
-                connectivityType, &foundResource);
+                CT_DEFAULT, &foundResource);
         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(),
-                connectivityType, &foundResource);
+                CT_DEFAULT, &foundResource);
         std::cout<< "Finding Resource for second time..." << std::endl;
 
         // A condition variable will free the mutex it is given, then do a non-