replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / resource-encapsulation / examples / linux / SampleResourceClient.cpp
old mode 100755 (executable)
new mode 100644 (file)
index 61287a0..d7336ca
@@ -21,6 +21,7 @@
 #include <iostream>
 
 #include "RCSDiscoveryManager.h"
+#include "RCSRepresentation.h"
 #include "RCSRemoteResourceObject.h"
 #include "RCSResourceAttributes.h"
 #include "RCSAddress.h"
@@ -86,12 +87,15 @@ int processUserInput(int min = std::numeric_limits<int>::min(),
 {
     assert(min <= max);
 
-    int input;
+    int input = 0;
 
     std::cin >> input;
     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
 
-    if (!std::cin.fail() && min <= input && input <= max) return input;
+    if (!std::cin.fail() && min <= input && input <= max)
+    {
+        return input;
+    }
 
     std::cin.clear();
     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
@@ -114,7 +118,7 @@ void displayItems(const std::vector<T>& items)
 
     const auto width = (items.size() + 1) / 10 + 1;
 
-    for(size_t i = 0; i < items.size(); ++i)
+    for (size_t i = 0; i < items.size(); ++i)
     {
         displayItem(width, i + 1, items[i]);
     }
@@ -126,7 +130,10 @@ void selectItem(const std::vector<T>& items)
 {
     int selected = processUserInput(1, items.size() + 1) - 1;
 
-    if(selected == static_cast<int>(items.size())) throw CloseApp();
+    if (selected == static_cast<int>(items.size()))
+    {
+        throw CloseApp();
+    }
 
     onSelected(items[selected]);
 }
@@ -138,6 +145,30 @@ void handleItems(const std::vector<T>& items)
     selectItem(items);
 }
 
+std::string inputInterface()
+{
+    std::string interfaceName;
+
+    std::cout << "\tInput the Interface you want to set : ";
+    std::cin >> interfaceName;
+
+    return interfaceName;
+}
+
+RCSResourceAttributes inputKeyValue()
+{
+    std::string key;
+
+    std::cout << "\tEnter the Key you want to set : ";
+    std::cin >> key;
+
+    std::cout << "\tEnter the value(INT) you want to set :";
+    RCSResourceAttributes attrs;
+    attrs[key] = processUserInput();
+
+    return attrs;
+}
+
 void printAttribute(const std::string& key, const RCSResourceAttributes::Value& value)
 {
     std::cout << "\tkey : " << key << std::endl
@@ -151,17 +182,71 @@ void printAttributes(const RCSResourceAttributes& attributes)
        std::cout << "\tattributes is empty" << std::endl;
     }
 
-    for(const auto& attr : attributes)
+    for (const auto& attr : attributes)
     {
         printAttribute(attr.key(), attr.value());
     }
 }
 
+void print(const std::string& name, const std::vector< std::string >& elements)
+{
+    if (elements.empty())
+    {
+       std::cout << "\t" << name << " is empty" << std::endl;
+       return;
+    }
+
+    std::cout << "\t" << name << " : " << std::endl;
+    for (const auto& item : elements)
+    {
+        std::cout << item << " ";
+    }
+    std::cout << std::endl;
+}
+
+void printUri(const std::string& uri)
+{
+    if (uri.empty())
+    {
+        std::cout << "\turi is empty" << std::endl;
+    }
+    else
+    {
+        std::cout << "\turi : " << uri << std::endl;
+    }
+}
+
+void printRepresentation(const RCSRepresentation& rep)
+{
+    printUri(rep.getUri());
+    print("interfaces", rep.getInterfaces());
+    print("resourceTypes", rep.getResourceTypes());
+    printAttributes(rep.getAttributes());
+
+    const auto& children = rep.getChildren();
+
+    if (children.empty())
+    {
+        std::cout << "\tchildren is empty" << std::endl;
+    }
+    else
+    {
+        int cnt = 0;
+        for (const auto& child : children)
+        {
+            std::cout << "========================================================" << std::endl;
+            std::cout << ++cnt << " chlid" << std::endl;
+            printRepresentation(child);
+            std::cout << "========================================================" << std::endl;
+        }
+    }
+}
+
 void onResourceStateChanged(ResourceState resourceState)
 {
     std::cout << "onResourceStateChanged callback" << std::endl;
 
-    switch(resourceState)
+    switch (resourceState)
     {
         case ResourceState::NONE:
             std::cout << "\tState changed to : NOT_MONITORING" << std::endl;
@@ -185,9 +270,9 @@ void onResourceStateChanged(ResourceState resourceState)
     }
 }
 
-void onCacheUpdated(const RCSResourceAttributes& attributes)
+void onCacheUpdated(const RCSResourceAttributes& attributes, int eCode)
 {
-    std::cout << "onCacheUpdated callback" << std::endl;
+    std::cout << "onCacheUpdated callback : " << eCode << std::endl;
 
     printAttributes(attributes);
 }
@@ -199,6 +284,20 @@ void onRemoteAttributesReceived(const RCSResourceAttributes& attributes, int)
     printAttributes(attributes);
 }
 
+void onRemoteGetReceived(const HeaderOpts&, const RCSRepresentation& rep, int)
+{
+    std::cout << "onRemoteGetReceived callback" << std::endl;
+
+    printRepresentation(rep);
+}
+
+void onRemoteSetReceived(const HeaderOpts&, const RCSRepresentation& rep, int)
+{
+    std::cout << "onRemoteSetReceived callback" << std::endl;
+
+    printRepresentation(rep);
+}
+
 void startMonitoring()
 {
     if (g_selectedResource->isMonitoring())
@@ -230,16 +329,23 @@ void getRemoteAttributes()
 
 void setRemoteAttributes()
 {
-    std::string key;
+    g_selectedResource->setRemoteAttributes(inputKeyValue(), onRemoteAttributesReceived);
+}
 
-    std::cout << "\tEnter the Key you want to set : ";
-    std::cin >> key;
+void getWithInterface()
+{
+    RCSQueryParams queryParams;
+    queryParams.setResourceInterface(inputInterface());
 
-    std::cout << "\tEnter the value(INT) you want to set :";
-    RCSResourceAttributes attrs;
-    attrs[key] = processUserInput();
+    g_selectedResource->get(queryParams, onRemoteGetReceived);
+}
+
+void setWithInterface()
+{
+    RCSQueryParams queryParams;
+    queryParams.setResourceInterface(inputInterface());
 
-    g_selectedResource->setRemoteAttributes(attrs, onRemoteAttributesReceived);
+    g_selectedResource->set(queryParams, inputKeyValue(), onRemoteSetReceived);
 }
 
 void startCaching(RCSRemoteResourceObject::CacheUpdatedCallback cb)
@@ -298,7 +404,7 @@ void getCachedAttribute()
 
 void stopCaching()
 {
-    if(!g_selectedResource->isCaching())
+    if (!g_selectedResource->isCaching())
     {
         std::cout << "\tCaching not started..." << std::endl;
         return;
@@ -317,12 +423,16 @@ std::string selectResourceType()
 
     switch (processUserInput(RESOURCE_TEMP, RESOURCE_LIGHT))
     {
-    case RESOURCE_TEMP:
-        g_attrKey = "Temperature";
-        return RESOURCE_TYPE_TEMP;
-    case RESOURCE_LIGHT:
-        g_attrKey = "Brightness";
-        return RESOURCE_TYPE_LIGHT;
+        case RESOURCE_TEMP:
+        {
+            g_attrKey = "Temperature";
+            return RESOURCE_TYPE_TEMP;
+        }
+        case RESOURCE_LIGHT:
+        {
+            g_attrKey = "Brightness";
+            return RESOURCE_TYPE_LIGHT;
+        }
     }
 
     throw std::logic_error("unreachable");
@@ -336,7 +446,10 @@ RCSAddress inputAddress()
 
     std::string address;
 
-    if(std::cin.peek() != '\n') std::cin >> address;
+    if (std::cin.peek() != '\n')
+    {
+        std::cin >> address;
+    }
 
     return address.empty() ? RCSAddress::multicast() : RCSAddress::unicast(address);
 }
@@ -353,7 +466,7 @@ void discoverResource()
     {
         std::cout << "onResourceDiscovered callback :: " << std::endl;
 
-        std::cout << "luri : " << discoveredResource->getUri() << std::endl;
+        std::cout << "uri : " << discoveredResource->getUri() << std::endl;
         std::cout << "host address : " << discoveredResource->getAddress() << std::endl;
 
         g_discoveredResources.push_back(discoveredResource);
@@ -369,7 +482,7 @@ void discoverResource()
     auto discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(address,
             resourceType, onResourceDiscovered);
 
-    while(processUserInput() != 1);
+    while (processUserInput() != 1);
 
     discoveryTask->cancel();
 }
@@ -381,6 +494,8 @@ void runResourceControl()
         DECLARE_MENU(stopMonitoring),
         DECLARE_MENU(getRemoteAttributes),
         DECLARE_MENU(setRemoteAttributes),
+        DECLARE_MENU(getWithInterface),
+        DECLARE_MENU(setWithInterface),
         DECLARE_MENU(startCachingWithoutCallback),
         DECLARE_MENU(startCachingWithCallback),
         DECLARE_MENU(getResourceCacheState),
@@ -407,7 +522,10 @@ void runDiscovery()
 
     handleItems(discoveryMenuItems);
 
-    if (g_discoveredResources.empty()) throw std::runtime_error("No resource found!");
+    if (g_discoveredResources.empty())
+    {
+        throw std::runtime_error("No resource found!");
+    }
 
     g_currentRun = runResourceSelection;
 }