unown primitive devices stub
authori.metelytsia <i.metelytsia@samsung.com>
Fri, 30 Jun 2017 11:58:32 +0000 (14:58 +0300)
committeri.metelytsia <i.metelytsia@samsung.com>
Fri, 30 Jun 2017 11:58:32 +0000 (14:58 +0300)
device_core/iotivity_lib/IoT/IOT_HubResource.cpp
device_core/iotivity_lib/IoT/IOT_HubResource.h
device_core/iotivity_lib/IoT/IOT_PrimitiveResource.cpp [new file with mode: 0644]
device_core/iotivity_lib/IoT/IOT_PrimitiveResource.h [new file with mode: 0644]
device_core/iotivity_lib/IoT/IOT_Resource.h
device_core/nmdaemon/main_thread.cpp
device_core/utest/test_iot_dev_manager.cpp

index 0819bc2..b60d07c 100644 (file)
@@ -13,6 +13,8 @@
 using namespace OC;
 using namespace NetworkManager;
 
+namespace PH = std::placeholders;
+
 std::ostream& operator<<(std::ostream& _os, const IOT_HubResource& _obj)
 {
     _os << "owned devices :" << std::endl;
@@ -28,8 +30,11 @@ std::ostream& operator<<(std::ostream& _os, const IOT_HubResource& _obj)
 
 IOT_HubResource::IOT_HubResource(const std::string& _uri, const std::vector<std::string>& _types, const std::vector<std::string>& _interfaces, const std::string& _devices_file_path_name) :
     IOT_Resource(_uri, _types, _interfaces),
-    m_devices_file_path_name(_devices_file_path_name), m_owned_devices{}, m_unowned_devices{}, m_thread(nullptr)
+    m_devices_file_path_name(_devices_file_path_name), m_iotivity(nullptr), m_owned_devices{}, m_unowned_devices{}, m_thread(nullptr)
 {
+    m_iotivity = IoTivity::getInstance();
+    assert(m_iotivity);
+
     loadOwnedDevicesInfo();
     m_representation.setValue("devices", "");
 
@@ -180,6 +185,26 @@ void IOT_HubResource::findUnOwnedDevices()
             devs_map.emplace(d->getUUID(), d);
     }
 
+#if 0
+    ZZZ
+    assert(m_iotivity);
+
+    std::cout << "=== FIND ===" << std::endl;
+
+    auto devs = m_iotivity->findDevices();
+    for(auto dev : devs)
+    {
+        std::cout << "=== " << dev->getUUID() << " : " << dev->isCloudAccessibility() << std::endl;
+
+        if(dev->getUUID().empty() || dev->isCloudAccessibility())
+            continue;
+
+        if(devs_map.find(dev->getUUID()) == devs_map.end())
+            devs_map.emplace(dev->getUUID(), dev);
+    }
+    ZZZ
+#endif
+
     if(m_mutex.try_lock())
     {
         m_unowned_devices = devs_map;
@@ -205,8 +230,8 @@ void IOT_HubResource::ownDevice(const std::string& _id)
     u_devs_map = m_unowned_devices;
     m_mutex.unlock();
 
-    const auto& it = u_devs_map.find(_id);
-    if(it != u_devs_map.end())
+    const auto it = u_devs_map.find(_id);
+    if(it != u_devs_map.cend())
     {
         assert(!it->second->isCloudAccessibility());
 
@@ -225,6 +250,50 @@ void IOT_HubResource::ownDevice(const std::string& _id)
 }
 void IOT_HubResource::unownDevice(const std::string& _id)
 {
+    IoTDevicesMap o_devs_map;
+    IoTDevicesMap u_devs_map;
+
+    m_mutex.lock();
+    o_devs_map = m_owned_devices;
+    u_devs_map = m_unowned_devices;
+    m_mutex.unlock();
+
+    const auto it = o_devs_map.find(_id);
+    if(it != o_devs_map.cend())
+    {
+        std::shared_ptr<OC::OCResource> resource = m_iotivity->findResource(it->second->getHost(), /*it->second->getType()*/std::string{"device.primitive"});
+        if(!resource)
+        {
+            throw IoTInternalError("HubResource primitive resource not found", EC_GENERIC_ERROR);
+        }
+
+        QueryParamsMap query;
+        query["state"] = "unown";
+
+        PostResourceCallback::Sptr callback = std::make_shared<PostResourceCallback>();
+
+        auto result = resource->post(it->second->getType(), DEFAULT_INTERFACE, OCRepresentation{}, query, bind_callback(callback, PH::_1, PH::_2, PH::_3));
+
+        if (OC_STACK_OK != result)
+        {
+            throw IoTInternalError("HubResource unownDevice failed", result);
+        }
+
+        if (!callback->wait())
+        {
+            throw IoTInternalError("HubResource unownDevice failed callback not called", EC_UNAUTHORIZED);
+        }
+
+        u_devs_map.emplace(it->second->getUUID(), it->second);
+        o_devs_map.erase(it);
+
+        m_mutex.lock();
+        m_owned_devices = o_devs_map;
+        m_unowned_devices = u_devs_map;
+        m_mutex.unlock();
+
+        saveOwnedDevicesInfo();
+    }
 }
 
 void IOT_HubResource::saveOwnedDevicesInfo()
index 3fdb707..830fbf2 100644 (file)
 #include "OCApi.h"
 
 #include "nmlib.h"
+#include "iotivity.h"
 #include "IOT_DeviceFinder.h"
 #include "iotdevice.h"
 #include "iotdevice_impl.h"
 #include "nmexceptions.h"
 #include "IOT_Resource.h"
+#include "resource_callbacks.h"
 
 using namespace OC;
 using namespace NetworkManager;
@@ -63,6 +65,8 @@ private:
 
     std::string m_devices_file_path_name;
 
+    IoTivity* m_iotivity;
+
     IoTDevicesMap m_owned_devices;
     IoTDevicesMap m_unowned_devices;
 
diff --git a/device_core/iotivity_lib/IoT/IOT_PrimitiveResource.cpp b/device_core/iotivity_lib/IoT/IOT_PrimitiveResource.cpp
new file mode 100644 (file)
index 0000000..c719cf1
--- /dev/null
@@ -0,0 +1,99 @@
+#include <iostream>
+#include <fstream>
+#include <memory>
+#include <string>
+#include <cassert>
+
+#include "OCPlatform.h"
+#include "OCApi.h"
+
+#include "IOT_PrimitiveResource.h"
+
+using namespace OC;
+using namespace NetworkManager;
+
+IOT_PrimitiveResource::IOT_PrimitiveResource(const std::string& _uri, const std::vector<std::string>& _types, const std::vector<std::string>& _interfaces) :
+    IOT_Resource(_uri, _types, _interfaces)
+{
+}
+
+void IOT_PrimitiveResource::setRepresentation(OCRepresentation& _rep)
+{
+}
+
+OCStackResult IOT_PrimitiveResource::registerResource()
+{
+    auto interfaces = m_representation.getResourceInterfaces();
+    std::string uri = m_representation.getUri();
+    auto result =  OCPlatform::registerResource(
+            m_handle,
+            uri,
+            m_representation.getResourceTypes()[0],
+            interfaces[0],
+            std::bind(&IOT_Resource::entityHandler, this, std::placeholders::_1),
+            OC_DISCOVERABLE);
+
+    if (interfaces.size() > 1u)
+    {
+        auto it = interfaces.begin();
+        while (++it != interfaces.end())
+        {
+            bindInterface(*it);
+        }
+    }
+    return result;
+}
+
+/*virtual*/ OCEntityHandlerResult IOT_PrimitiveResource::entityHandler(std::shared_ptr<OCResourceRequest> _request)
+{
+    OCEntityHandlerResult res = OC_EH_ERROR;
+
+    if(_request)
+    {
+        std::string rt = _request->getRequestType();
+        int rf = _request->getRequestHandlerFlag();
+
+        if(rf & RequestHandlerFlag::RequestFlag)
+        {
+            if(rt == "GET")
+            {
+            }
+            else if(rt == "PUT")
+            {
+            }
+            else if(rt == "POST")
+            {
+                std::cout << "=============== POST" << std::endl;
+
+                if(sendRepresentation(_request) == OC_STACK_OK)
+                    res = OC_EH_OK;
+
+#if 0
+                std::string id = _request->getQueryParameters().find("uid")->second;
+                std::string state = _request->getQueryParameters().find("state")->second;
+
+                if(state == "own" || state == "unown")
+                {
+                    boost::thread t{boost::bind(&IOT_HubResource::ownDevices, this, id, state)};
+
+                    if(sendRepresentation(_request) == OC_STACK_OK)
+                        res = OC_EH_OK;
+                }
+                else
+                {
+                    res = OC_EH_BAD_REQ;
+                }
+#endif
+            }
+            else if(rt == "DELETE")
+            {
+            }
+        }
+    }
+    else
+    {
+        //cout << "Request invalid" << endl;
+    }
+
+    return res;
+}
diff --git a/device_core/iotivity_lib/IoT/IOT_PrimitiveResource.h b/device_core/iotivity_lib/IoT/IOT_PrimitiveResource.h
new file mode 100644 (file)
index 0000000..7ba641c
--- /dev/null
@@ -0,0 +1,37 @@
+#ifndef __IOT_PRIMITIVE_RESOURCE_H__
+#define __IOT_PRIMITIVE_RESOURCE_H__
+
+#include <memory>
+#include <string>
+
+#include "OCPlatform.h"
+#include "OCApi.h"
+
+#include "nmlib.h"
+#include "iotdevice.h"
+#include "iotdevice_impl.h"
+#include "IOT_Resource.h"
+
+using namespace OC;
+using namespace NetworkManager;
+
+class IOT_PrimitiveResource : public IOT_Resource
+{
+public:
+    IOT_PrimitiveResource(const std::string& _uri, const std::vector<std::string>& _types, const std::vector<std::string>& _interfaces);
+    IOT_PrimitiveResource(const IOT_PrimitiveResource& _obj) = default;
+
+    virtual ~IOT_PrimitiveResource() = default;
+
+    IOT_PrimitiveResource& operator=(const IOT_PrimitiveResource& _obj) = default;
+
+    void setRepresentation(OCRepresentation& _rep) override;
+
+    OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> _request) override;
+
+    OCStackResult registerResource();
+
+private:
+};
+
+#endif /* __IOT_PRIMITIVE_RESOURCE_H__ */
index 291ff91..d78595d 100644 (file)
@@ -77,6 +77,11 @@ public:
 
     static void printRepresentation(const OCRepresentation& _rep);
 
+    OCResourceHandle& getHandle()
+    {
+        return m_handle;
+    }
+
 protected:
     OCResourceHandle m_handle;
     OCRepresentation m_representation;
index 1b75573..11bb5b6 100644 (file)
@@ -23,6 +23,8 @@
 
 #ifndef __BUILD_PRIMITIVE__
 #include "RDClient.h"
+#else
+#include "IOT_PrimitiveResource.h"
 #endif
 
 using namespace NMD;
@@ -121,6 +123,7 @@ void main_thread::routine()
             throw std::runtime_error("register hub resource failed");
         }
 
+        rhandles.push_back(hub.getHandle());
         iotivity->publishResources(rhandles);
 
         ProxyThread proxy_thread;
@@ -131,6 +134,15 @@ void main_thread::routine()
         report_resorce.registerResource();
 #endif
 
+#ifdef __BUILD_PRIMITIVE__
+        IOT_PrimitiveResource primitive("/sec/primitive", {"device.primitive"}, {DEFAULT_INTERFACE, BATCH_INTERFACE, LINK_INTERFACE});
+
+        if(OC_STACK_OK != primitive.registerResource())
+        {
+            throw std::runtime_error("register primitive resource failed");
+        }
+#endif
+
         rmi_thread rmithread(iotivity, proxy_thread_ptr);
 
         if(!rmithread.start())
index 0c1b409..5bcdc33 100644 (file)
@@ -211,6 +211,8 @@ TEST_F(IoTDevManagerTest, device_discovery_ex)
         {
             printDeviceInfo(dev_uuids[idx]);
         }
+
+
     }
     NM_freeDeviceArray(&dev_uuids);
 }
@@ -220,11 +222,54 @@ TEST_F(IoTDevManagerTest, device_discovery_ex)
  * 1. Search for hub resource
  * 2. Try to receive list of primitive devices
  */
-TEST_F(IoTDevManagerTest, resource_hub_discovery)
+TEST_F(IoTDevManagerTest, DISABLED_resource_hub_discovery)
 {
     auto hub_client = std::make_shared<HubClient>(cloud_host);
     ASSERT_TRUE(!!hub_client);
 
+    try
+    {
+        IoTDevicesMap dev_map;
+        hub_client->getUnownedDevices(dev_map);
+        for (auto dev: dev_map)
+        {
+            std::cout << "Unowned Device [" << dev.second->getUUID() << "]" << std::endl
+                      << "\tname: " << dev.second->getName() << std::endl
+                      << "\tmodel: " << dev.second->getModel() << std::endl
+                      << "\ttype: " << dev.second->getType() << std::endl
+                      << "\tstate: " << (dev.second->isOnline() ? "online" : "offline") << std::endl << std::endl;
+
+            hub_client->ownDevice(dev.second->getUUID());
+        }
+
+        dev_map.clear();
+
+        hub_client->getOwnedDevices(dev_map);
+        for (auto dev: dev_map)
+        {
+            std::cout << "Owned Device [" << dev.second->getUUID() << "]" << std::endl
+                      << "\tname: " << dev.second->getName() << std::endl
+                      << "\tmodel: " << dev.second->getModel() << std::endl
+                      << "\ttype: " << dev.second->getType() << std::endl
+                      << "\tstate: " << (dev.second->isOnline() ? "online" : "offline") << std::endl << std::endl;
+
+
+            hub_client->unOwnDevice(dev.second->getUUID());
+        }
+    }
+    catch(std::exception& e)
+    {
+        FAIL() << e.what();
+    }
+
+
+
+
+
+#if 0
+    auto hub_client = std::make_shared<HubClient>(cloud_host);
+    ASSERT_TRUE(!!hub_client);
+
     IoTDevicesMap dev_map;
     try
     {
@@ -244,6 +289,7 @@ TEST_F(IoTDevManagerTest, resource_hub_discovery)
     {
         FAIL() << e.what();
     }
+#endif
 }
 
 /**