Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / resource / examples / devicediscoveryclient.cpp
index f08a8c7..90d22d8 100644 (file)
 using namespace OC;
 
 //Callback after device information is received
-void receivedDeviceInfo(const OCRepresentation& rep)
+void receivedPlatformInfo(const OCRepresentation& rep)
 {
-    std::cout << "\nDevice Information received ---->\n";
-
-    std::string contentType;
-    std::string dateOfManufacture;
-    std::string deviceName;
-    std::string deviceUUID;
-    std::string firmwareVersion;
-    std::string hostName;
-    std::string manufacturerName;
-    std::string manufacturerUrl;
-    std::string modelNumber;
-    std::string platformVersion;
-    std::string supportUrl;
-    std::string version;
-
-    if(rep.getValue("ct", contentType))
-    {
-        std::cout << "Content Type: " << contentType << std::endl;
-    }
-
-    if(rep.getValue("mndt", dateOfManufacture))
-    {
-        std::cout << "Date of manufacture: " << dateOfManufacture << std::endl;
-    }
-
-    if(rep.getValue("dn", deviceName))
-    {
-        std::cout << "Device Name: " << deviceName << std::endl;
-    }
-
-    if(rep.getValue("di", deviceUUID))
+    std::cout << "\nPlatform Information received ---->\n";
+    std::string value;
+    std::string values[22] =
     {
-        std::cout << "Device UUID: " << deviceUUID << std::endl;
-    }
+        "pi",   "Platform ID                    ",
+        "mnmn", "Manufacturer name              ",
+        "mnml", "Manufacturer url               ",
+        "mnmo", "Manufacturer Model No          ",
+        "mndt", "Manufactured Date              ",
+        "mnpv", "Manufacturer Platform Version  ",
+        "mnos", "Manufacturer OS version        ",
+        "mnhw", "Manufacturer hardware version  ",
+        "mnfv", "Manufacturer firmware version  ",
+        "mnsl", "Manufacturer support url       ",
+        "st",   "Manufacturer system time       "
+    };
 
-    if(rep.getValue("mnfv", firmwareVersion))
+    for (int i = 0; i < 22; i += 2)
     {
-        std::cout << "Firmware Version: " << firmwareVersion << std::endl;
+        if(rep.getValue(values[i], value))
+        {
+            std::cout << values[i + 1] << " : "<< value << std::endl;
+        }
     }
+}
 
-    if(rep.getValue("hn", hostName))
-    {
-        std::cout << "Host Name: " << hostName << std::endl;
-    }
+void receivedDeviceInfo(const OCRepresentation& rep)
+{
+    std::cout << "Implement me !" << std::endl;
+}
 
-    if(rep.getValue("mnmn", manufacturerName))
-    {
-        std::cout << "Manufacturer Name: " << manufacturerName << std::endl;
-    }
+int main(int argc, char* argv[]) {
 
-    if(rep.getValue("mnml", manufacturerUrl))
-    {
-        std::cout << "Manufacturer Url: " << manufacturerUrl << std::endl;
-    }
+    std::ostringstream platformDiscoveryRequest;
+    std::ostringstream deviceDiscoveryRequest;
 
-    if(rep.getValue("mnmo", modelNumber))
-    {
-        std::cout << "Model No. : " << modelNumber << std::endl;
-    }
+    std::string platformDiscoveryURI = "/oic/p";
+    std::string deviceDiscoveryURI   = "/oic/d";
 
-    if(rep.getValue("mnpv", platformVersion))
-    {
-        std::cout << "Platform Version: " << platformVersion << std::endl;
-    }
+    OCConnectivityType connectivityType = OC_IPV4;
 
-    if(rep.getValue("mnsl", supportUrl))
+    if(argc == 2)
     {
-        std::cout << "Support URL: " << supportUrl << std::endl;
+        try
+        {
+            std::size_t inputValLen;
+            int optionSelected = std::stoi(argv[1], &inputValLen);
+
+            if(inputValLen == strlen(argv[1]))
+            {
+                if(optionSelected == 0)
+                {
+                    connectivityType = OC_IPV4;
+                }
+                else if(optionSelected == 1)
+                {
+                    // TODO: re-enable IPv4/IPv6 command line selection when IPv6 is supported
+                    //connectivityType = OC_IPV6;
+                    connectivityType = OC_IPV4;
+                    std::cout << "IPv6 not currently supported. Using default IPv4" << std::endl;
+                }
+                else
+                {
+                    std::cout << "Invalid connectivity type selected. Using default IPv4"
+                    << std::endl;
+                }
+            }
+            else
+            {
+                std::cout << "Invalid connectivity type selected. Using default IPv4" << std::endl;
+            }
+        }
+        catch(std::exception&)
+        {
+            std::cout << "Invalid input argument. Using IPv4 as connectivity type" << std::endl;
+        }
     }
-
-    if(rep.getValue("icv", version))
+    else
     {
-        std::cout << "Version: " << version << std::endl;
+        std::cout << "Usage devicediscoveryclient <connectivityType(0|1)>" << std::endl;
+        std::cout << "connectivityType: Default IPv4" << std::endl;
+        std::cout << "connectivityType 0: IPv4" << std::endl;
+        std::cout << "connectivityType 1: IPv6 (not currently supported)" << std::endl;
     }
-}
-
-int main() {
-
     // Create PlatformConfig object
     PlatformConfig cfg {
         OC::ServiceType::InProc,
@@ -125,9 +131,42 @@ int main() {
     OCPlatform::Configure(cfg);
     try
     {
-        OCPlatform::getDeviceInfo("", "coap://224.0.1.187/oc/core/d", &receivedDeviceInfo);
-        std::cout<< "Querying for device information... " <<std::endl;
-
+        platformDiscoveryRequest << OC_MULTICAST_PREFIX << platformDiscoveryURI;
+        deviceDiscoveryRequest << OC_MULTICAST_PREFIX << deviceDiscoveryURI;
+
+        OCStackResult ret;
+
+        std::cout<< "Querying for platform information... ";
+
+        ret = OCPlatform::getPlatformInfo("", platformDiscoveryRequest.str(), connectivityType,
+                &receivedPlatformInfo);
+
+        if (ret == OC_STACK_OK)
+        {
+            std::cout << "done." << std::endl;
+        }
+        else
+        {
+            std::cout << "failed." << std::endl;
+        }
+
+        bool is_oic_d_implemented = false;
+        if (is_oic_d_implemented)
+        {
+            std::cout<< "Querying for device information... ";
+
+            ret = OCPlatform::getDeviceInfo("", deviceDiscoveryRequest.str(), connectivityType,
+                    &receivedDeviceInfo);
+
+            if (ret == OC_STACK_OK)
+            {
+                std::cout << "done." << std::endl;
+            }
+            else
+            {
+                std::cout << "failed." << std::endl;
+            }
+        }
         // A condition variable will free the mutex it is given, then do a non-
         // intensive block until 'notify' is called on it.  In this case, since we
         // don't ever call cv.notify, this should be a non-processor intensive version
@@ -135,13 +174,14 @@ int main() {
         std::mutex blocker;
         std::condition_variable cv;
         std::unique_lock<std::mutex> lock(blocker);
-        cv.wait(lock);
+        cv.wait(lock, []{return false;});
 
     }catch(OCException& e)
     {
-        //log(e.what());
+        std::cerr << "Failure in main thread: "<<e.reason()<<std::endl;
     }
 
     return 0;
 }
 
+