From 9004fc0d62edc2cbd3392cb05b9919387861dc6e Mon Sep 17 00:00:00 2001 From: Lomtev Dmytro Date: Fri, 4 Aug 2017 10:11:46 +0300 Subject: [PATCH] Fixed owned device discovery. Fixed standard and hub unpairing. --- device_core/ctrl_app_lib/src/hub_client.cpp | 10 +++- device_core/ctrl_app_lib/src/json_utils.cpp | 10 +--- device_core/ctrl_app_lib/src/securitycontext.cpp | 75 ++++++++++-------------- device_core/iotivity_lib/inc/iotdevice_impl.h | 1 + device_core/nmdaemon/main_thread.cpp | 2 +- 5 files changed, 43 insertions(+), 55 deletions(-) diff --git a/device_core/ctrl_app_lib/src/hub_client.cpp b/device_core/ctrl_app_lib/src/hub_client.cpp index aafca7b..27e727d 100644 --- a/device_core/ctrl_app_lib/src/hub_client.cpp +++ b/device_core/ctrl_app_lib/src/hub_client.cpp @@ -93,7 +93,15 @@ void HubClient::getPrimitiveDevices(const OC::QueryParamsMap& query, IoTDevicesM std::string model = dev["model"].asString(); std::string type = dev["type"].asString(); bool state = dev["state"].asBool(); - device_map.emplace(did, std::make_shared(this_shared, did, name, model, type, state)); + auto it = device_map.find(did); + if (it == device_map.end()) + { + device_map.emplace(did, std::make_shared(this_shared, did, name, model, type, state)); + } + else + { + it->second = std::make_shared(this_shared, did, name, model, type, state); + } } } diff --git a/device_core/ctrl_app_lib/src/json_utils.cpp b/device_core/ctrl_app_lib/src/json_utils.cpp index b204284..cc5662f 100644 --- a/device_core/ctrl_app_lib/src/json_utils.cpp +++ b/device_core/ctrl_app_lib/src/json_utils.cpp @@ -67,16 +67,8 @@ IoTDevicePtr ToDevice(const std::string &json) model = contents["model"].asString(); } - int status = 0; - - if (contents.isMember("status") && - contents["status"].isInt()) - { - status = contents["status"].asInt(); - } - IoTDevicePtr device = std::make_shared(uuid, name, type, model); - device->setState(status); + device->setState(false); return device; } diff --git a/device_core/ctrl_app_lib/src/securitycontext.cpp b/device_core/ctrl_app_lib/src/securitycontext.cpp index e63b1f6..7010f0f 100644 --- a/device_core/ctrl_app_lib/src/securitycontext.cpp +++ b/device_core/ctrl_app_lib/src/securitycontext.cpp @@ -167,55 +167,42 @@ const IoTDevicesMap& SecurityContext::getOwnedDevices() RestService rest_service(dsm_address); owned = rest_service.getOwnedDevices(m_uid); - return owned; -// FN_VISIT -// if (!iotivity->isSignedIn()) -// { -// throw IoTInternalError("Not signed in", EC_UNAUTHORIZED); -// } -// else if (!iotivity->isConnected()) -// { -// iotivity->signIn(); -// } + if (!iotivity->isSignedIn()) + { + throw IoTInternalError("Not signed in", EC_UNAUTHORIZED); + } + else if (!iotivity->isConnected()) + { + iotivity->signIn(); + } -// owned.clear(); + auto found_devices = iotivity->findResourceList(true, "oic.wk.d", "/oic/res", CT_ADAPTER_TCP); -// std::vector found_devices = iotivity->findDevices(true, CT_ADAPTER_TCP, "/oic/res?rt=oic.wk.d"); + for (auto found_device : found_devices) + { + std::string sid = found_device->sid(); + std::cout << "Found device: " << sid << ", " << found_device->host() << ", " << std::hex << found_device->connectivityType() << std::endl; + auto it = owned.find(sid); -// for (auto found_device : found_devices) -// { -// if (found_device->getUUID().empty() || found_device->getType() == "secserver") continue; - -// auto it = owned.find(found_device->getUUID()); - -// if (it != owned.end()) -// { -// it->second = found_device; -// } -// else -// { -// auto emplace_result = owned.emplace(found_device->getUUID(), found_device); - -// if (!emplace_result.second) -// { -// throw NMexception("Out of memory", EC_OUT_OF_MEMORY); -// } -// } -// } + if (it != owned.end() && !it->second->isActive()) + { + it->second->activate(found_device); + } + } -// try -// { -// checkResource(hubClient, iotivity->host()); -// hubClient->getOwnedDevices(owned); -// } -// catch (std::exception& e) -// { -// hubClient.reset(); -// iotivity->signIn(); -// LOG_E(TAG, "getOwnedDevices from Hub exception: %s", e.what()); -// } + try + { + checkResource(hubClient, iotivity->host()); + hubClient->getOwnedDevices(owned); + } + catch (std::exception& e) + { + hubClient.reset(); + iotivity->signIn(); + LOG_E(TAG, "getOwnedDevices from Hub exception: %s", e.what()); + } -// return owned; + return owned; } const IoTDevicesMap& SecurityContext::getUnOwnedDevices() diff --git a/device_core/iotivity_lib/inc/iotdevice_impl.h b/device_core/iotivity_lib/inc/iotdevice_impl.h index de21bb0..02e64eb 100644 --- a/device_core/iotivity_lib/inc/iotdevice_impl.h +++ b/device_core/iotivity_lib/inc/iotdevice_impl.h @@ -82,6 +82,7 @@ public: { assert(resource != nullptr); dev = resource; + host = dev->host(); online = true; } diff --git a/device_core/nmdaemon/main_thread.cpp b/device_core/nmdaemon/main_thread.cpp index 2e02e4c..1636e36 100644 --- a/device_core/nmdaemon/main_thread.cpp +++ b/device_core/nmdaemon/main_thread.cpp @@ -243,7 +243,6 @@ void MainThread::unregister_proc(IoTivity* iotivity, std::shared_ptr proxy_thread) { assert(iotivity); - assert(hub); assert(report_handler); assert(policy_handler); assert(notification_handler); @@ -257,6 +256,7 @@ void MainThread::unregister_proc(IoTivity* iotivity, if (g_working_mode == WorkingMode::Hub) { // disable hub to send found devices list + assert(hub); hub->setEnabled(false); clear_hub_cache(); report_handler->disable(); -- 2.7.4