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;
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", "");
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;
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());
}
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()
#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;
std::string m_devices_file_path_name;
+ IoTivity* m_iotivity;
+
IoTDevicesMap m_owned_devices;
IoTDevicesMap m_unowned_devices;
--- /dev/null
+#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;
+}
--- /dev/null
+#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__ */
static void printRepresentation(const OCRepresentation& _rep);
+ OCResourceHandle& getHandle()
+ {
+ return m_handle;
+ }
+
protected:
OCResourceHandle m_handle;
OCRepresentation m_representation;
#ifndef __BUILD_PRIMITIVE__
#include "RDClient.h"
+#else
+#include "IOT_PrimitiveResource.h"
#endif
using namespace NMD;
throw std::runtime_error("register hub resource failed");
}
+ rhandles.push_back(hub.getHandle());
iotivity->publishResources(rhandles);
ProxyThread proxy_thread;
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())
{
printDeviceInfo(dev_uuids[idx]);
}
+
+
}
NM_freeDeviceArray(&dev_uuids);
}
* 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
{
{
FAIL() << e.what();
}
+#endif
}
/**