class IoTDevice_impl: public IoTDevice
{
-public:
+public:
+ const unsigned CALLBACK_WAIT_TIMEOUT_S = 1;
+
IoTDevice_impl(std::shared_ptr<OC::OCResource> device_resource);
~IoTDevice_impl() override;
const std::string& getType() override;
+ const std::string& getHost() override;
+
bool isOnline() override;
void ownDevice() override;
std::string spec_ver;
std::mutex mtx;
std::condition_variable cond_var;
+
+ static const std::string dev_info_uri;
};
}
OC::QualityOfService _QoS /*= OC::QualityOfService::LowQos*/,
int _wait_for_sec /*= 3*/ )
{
-// OC::OCPlatform::findResourceList( _host,
-// _request_uri,
-// _connectivity_type,
-// [this](std::vector<std::shared_ptr<OC::OCResource>> _resources)
-// {
-// std::copy(_resources.begin(), _resources.end(), std::back_inserter(this->m_devices));
-// },
-// _QoS );
-// std::this_thread::sleep_for(std::chrono::seconds(_wait_for_sec));
+ OC::OCPlatform::findResourceList( _host,
+ _request_uri,
+ _connectivity_type,
+ [this](std::vector<std::shared_ptr<OC::OCResource>> _resources)
+ {
+ std::copy(_resources.begin(), _resources.end(), std::back_inserter(this->m_devices));
+ },
+ _QoS );
+ std::this_thread::sleep_for(std::chrono::seconds(_wait_for_sec));
return m_devices;
}
#include "iotdevice_impl.h"
+#include <chrono>
using namespace OC;
namespace NetworkManager {
+const std::string IoTDevice_impl::dev_info_uri{"/oic/d"};
IoTDevice_impl::IoTDevice_impl(std::shared_ptr<OCResource> device_resource)
: dev(device_resource), name("unknown"), model("unknown"), type("unknown"), uuid("unknown"), spec_ver("unknown")
{
OCPlatform::getDeviceInfo(device_resource->host(),
- device_resource->uri(),
+ dev_info_uri,
device_resource->connectivityType(),
[this](const OCRepresentation& rep){
std::string value;
- std::cout << "In get device info callback" << std::endl;
if(rep.getValue("di", value))
{
- std::cout << "uid: " << value << std::endl;
this->uuid = std::move(value);
}
if(rep.getValue("n", value))
{
- std::cout << "Name: " << value << std::endl;
this->name = std::move(value);
}
if(rep.getValue("lcv", value))
{
- std::cout << "lcv: " << value << std::endl;
this->spec_ver = std::move(value);
}
if(rep.getValue("dmv", value))
{
- std::cout << "dmv: " << value << std::endl;
this->model = std::move(value);
}
- std::cout << "Notify done" << std::endl;
this->cond_var.notify_all();
});
- std::cout << "Lock until device info callback call" << std::endl;
std::unique_lock<std::mutex> lock(mtx);
-
- cond_var.wait(lock);
- std::cout << "unlocked" << std::endl;
+ cond_var.wait_for(lock, std::chrono::seconds(CALLBACK_WAIT_TIMEOUT_S));
}
IoTDevice_impl::~IoTDevice_impl()
return type;
}
+const std::string& IoTDevice_impl::getHost()
+{
+ return dev->host();
+}
+
bool IoTDevice_impl::isOnline()
{
// TODO: add implementation
#include <OCPlatform.h>
#pragma GCC diagnostic pop
#include <unistd.h>
+#include "nmexceptions.h"
namespace NetworkManager {
}
-static FILE *open_ps(const char * /*path*/, const char *mode)
-{
- return fopen("/tmp/ps.dat", mode);
-}
+//static FILE *open_ps(const char * /*path*/, const char *mode)
+//{
+// return fopen("/tmp/ps.dat", mode);
+//}
IoTivity* IoTivity::getInstance()
{
"0.0.0.0",
0,
OC::QualityOfService::HighQos
- },
- {open_ps, fread, fwrite, fclose, unlink}
+ }
+ //,{open_ps, fread, fwrite, fclose, unlink}
};
- std::cout << "IoTivity init start" << std::endl;
OC::OCPlatform::Configure(instance->params->config);
- std::cout << "IoTivity init ok" << std::endl;
}
return instance;
}
{
IoTDevicesMap map{};
IOT_DeviceFinder dev_finder;
- std::cout << "Search for IoT devices" << std::endl;
+
auto devs = dev_finder();
- std::cout << "Found " << devs.size() << " devices" << std::endl;
- for (auto d : devs) {
- std::cout << "insert" << std::endl;
+
+ for (auto d : devs)
+ {
std::shared_ptr<IoTDevice> dev(new IoTDevice_impl(d));
- auto res = map.emplace(dev->getName(), dev);
+ auto it = map.find(dev->getUUID());
+
+ if (it != map.end())
+ {
+ // TODO: implement selection of most preferable protocol
+ }
+ else
+ {
+ auto res = map.emplace(dev->getUUID(), dev);
- if (!res.second) {
- std::cerr << "IoTDevicesMap insertion failed" << std::endl;
+ if (!res.second) {
+ throw IoTInternalError("Out of memory");
+ }
}
}
return map;
NM_ErrorCode NM_deviceListForEach(NM_hDeviceList dev_list, NM_deviceEnumerationCb callback, void* user_defined)
{
- std::cout << "in for each " << dev_list->map.size() << std::endl;
+ if (dev_list == nullptr || callback == nullptr) return EC_NULL_POINTER;
+
for (auto dev: dev_list->map)
{
- std::cout << "cb call" << std::endl;
callback(dev_list, dev.first.c_str(), user_defined);
}
- std::cout << "out for each" << std::endl;
return EC_OK;
}
virtual const std::string& getType() = 0;
+ virtual const std::string& getHost() = 0;
+
virtual bool isOnline() = 0;
virtual void ownDevice() = 0;
void TearDown() override
{
- std::cout << "clean up" << std::endl;
ASSERT_NO_THROW(NM_cleanup(&ctx));
}
protected:
void each_callback(NM_hDeviceList list, const char* uid, void* param)
{
- std::cout << "in call back" << std::endl;
NM_DeviceInfo info;
ASSERT_EQ(EC_OK, NM_getDeviceInfo(list, uid, &info));
}
-TEST_F(IoTDevManagerTest, owned_dev_discovery)
+TEST_F(IoTDevManagerTest, unowned_dev_discovery)
{
NM_hDeviceList dev_list;
ASSERT_EQ(EC_NULL_POINTER, NM_getUnownedDevices(ctx, nullptr));
ASSERT_EQ(EC_OK, NM_getUnownedDevices(ctx, &dev_list));
- std::cout << "1" << std::endl;
- /*ASSERT_NO_THROW(NM_getListSize(dev_list));
- std::cout << "2" << std::endl;
+ ASSERT_NO_THROW(NM_getListSize(dev_list));
ASSERT_EQ(EC_NULL_POINTER, NM_deviceListForEach(nullptr, each_callback, nullptr));
- std::cout << "3" << std::endl;
ASSERT_EQ(EC_OK, NM_deviceListForEach(dev_list, each_callback, nullptr));
- std::cout << "4" << std::endl;
NM_freeDeviceList(nullptr);
- std::cout << "5" << std::endl;
- NM_freeDeviceList(&dev_list);*/
- std::cout << "6" << std::endl;
+ NM_freeDeviceList(&dev_list);
}