From: Lomtev Dmytro Date: Thu, 31 Aug 2017 05:47:54 +0000 (+0300) Subject: Implemented deleteApp command proxying to the primitive devices. X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=75df41e38f47fdbcc3c3cc1570e28a1a1a42741f;p=platform%2Fcore%2Fsecurity%2Fsuspicious-activity-monitor.git Implemented deleteApp command proxying to the primitive devices. --- diff --git a/device_core/ctrl_app_lib/inc/nmlib.h b/device_core/ctrl_app_lib/inc/nmlib.h index f6e5d47..349c149 100644 --- a/device_core/ctrl_app_lib/inc/nmlib.h +++ b/device_core/ctrl_app_lib/inc/nmlib.h @@ -76,6 +76,7 @@ typedef struct const char* duid; const char* policy; const char* appname; + const char* parentUuid; CallbackState callbackState; } NM_NotificationData; @@ -334,12 +335,13 @@ NM_ErrorCode NM_getDeviceAgents(NM_hContext ctx, const char* dev_id, char** agen /** * @brief NM_deleteApp sends application uninstall request to specified device - * @param ctx [in] handle to context - * @param dev_id [in] device id - * @param app_name [in] application name to delete + * @param ctx [in] handle to context + * @param dev_id [in] device id + * @param parentUuid [in] id of the parent device + * @param app_name [in] application name to delete * @return error code */ -NM_ErrorCode NM_deleteApp(NM_hContext ctx, const char* dev_id, const char* app_name); +NM_ErrorCode NM_deleteApp(NM_hContext ctx, const char* dev_id, const char* parentUuid, const char* app_name); /** * @brief subscribeNotifications subscribes to secure server notifications. diff --git a/device_core/ctrl_app_lib/src/ctrl_app_support.cpp b/device_core/ctrl_app_lib/src/ctrl_app_support.cpp index 35fd745..40ed37b 100644 --- a/device_core/ctrl_app_lib/src/ctrl_app_support.cpp +++ b/device_core/ctrl_app_lib/src/ctrl_app_support.cpp @@ -20,6 +20,7 @@ #include "securitycontext.h" #include "ctrl_app_support.h" #include "registration_mq.h" +#include "device_control.h" #include #include @@ -800,27 +801,30 @@ NM_ErrorCode NM_getDeviceAgents(NM_hContext ctx, const char* dev_id, char** agen return EC_OK; } -NM_ErrorCode NM_deleteApp(NM_hContext ctx, const char* dev_id, const char* app_name) +NM_ErrorCode NM_deleteApp(NM_hContext ctx, const char* dev_id, const char* parentUuid, const char* app_name) { FN_VISIT - if (ctx == nullptr || dev_id == nullptr || app_name == nullptr) return EC_NULL_POINTER; + if (ctx == nullptr || dev_id == nullptr || app_name == nullptr || parentUuid == nullptr) return EC_NULL_POINTER; + + NM_ErrorCode result = EC_OK; try { - IoTDevice::deleteApp(dev_id, ctx->instance, app_name); + DeviceControl control(ctx->instance->host(), dev_id, ctx->instance); + result = control.deleteApp(app_name, parentUuid); } catch(NMexception& e) { LOG_E(TAG, "Delete applicatio error: %s, error code %d", e.what(), e.errorCode()); - return static_cast(e.errorCode()); + result = static_cast(e.errorCode()); } catch (std::exception& e) { LOG_E(TAG, "Delete applicatio error: %s", e.what()); - return EC_INTERNAL_ERROR; + result = EC_INTERNAL_ERROR; } - return EC_OK; + return result; } void NM_freeCharBuffer(char* buffer) diff --git a/device_core/ctrl_app_lib/src/securitycontext.cpp b/device_core/ctrl_app_lib/src/securitycontext.cpp index 03ebb90..23f608b 100644 --- a/device_core/ctrl_app_lib/src/securitycontext.cpp +++ b/device_core/ctrl_app_lib/src/securitycontext.cpp @@ -270,6 +270,7 @@ void SecurityContext::subscribeNotifications(NM_NotificationCb callback, void* u string duid = rep.getValueToString("duid"); string policy = rep.getValueToString("policy"); string appname = rep.getValueToString("appname"); + string parentUuid = rep.getValueToString("parentUuid"); NM_NotificationType type = NM_NotificationType(stoi_secure(stype, NT_Undefined)); int pid = stoi_secure(spid, -1); int time = stoi_secure(stime); @@ -284,6 +285,7 @@ void SecurityContext::subscribeNotifications(NM_NotificationCb callback, void* u duid.c_str(), policy.c_str(), appname.c_str(), + parentUuid.c_str(), callbackState }; @@ -374,13 +376,11 @@ void SecurityContext::postPolicy(const std::string& uuid, const std::string& pol { FN_VISIT auto it = getIoTDevice(uuid); - auto route = it->getRouting(); OCRepresentation repr; repr.setValue("policy", policy); - repr.setValue("route", route); repr.setValue("duid", uuid); - LOG_D(TAG, "Save policy for: %s with route %s", uuid.c_str(), route.c_str()); + LOG_D(TAG, "Save policy for: %s", uuid.c_str()); iotivity->getMqHandler()->publish(POLICY_TOPIC, repr); } diff --git a/device_core/iotivity_lib/inc/device_control.h b/device_core/iotivity_lib/inc/device_control.h new file mode 100644 index 0000000..60e2c06 --- /dev/null +++ b/device_core/iotivity_lib/inc/device_control.h @@ -0,0 +1,52 @@ +#ifndef DEVICE_CONTROL_H +#define DEVICE_CONTROL_H + +#include +#include "iotivity.h" +#include "nmlib.h" + +namespace NetworkManager +{ + +class DeviceControl +{ +public: + + /** + * @brief Type of the device control resource + */ + static const std::string CTRL_RESOURCE_TYPE; + + /** + * @brief Constructor + * @param host [in] host where resorce located + * @param duid [in] device id which expose resource + * @param iotivity [in] pointer to iotivity entity + */ + DeviceControl(const std::string& host, const std::string& duid, IoTivity* iotivity); + + /** + * @brief Sends application delete erquest + * @param app_name [in] name of the application to delete + * @param duid [in] id of the device for which command is sending + * @return error code + */ + NM_ErrorCode deleteApp(const std::string& app_name, const std::string& duid); + + /** + * @brief Sends unpair request to device + * @return error code + */ + NM_ErrorCode unOwn(); + +private: + + NM_ErrorCode sendCommand(const OC::OCRepresentation& representation); + + OC::OCResource::Ptr m_resource; + IoTivity* m_iotivity; +}; + +} // namespace NetworkManager + +#endif // DEVICE_CONTROL_H diff --git a/device_core/iotivity_lib/inc/iot_resource_server.h b/device_core/iotivity_lib/inc/iot_resource_server.h index 3b90916..926f54b 100644 --- a/device_core/iotivity_lib/inc/iot_resource_server.h +++ b/device_core/iotivity_lib/inc/iot_resource_server.h @@ -6,9 +6,24 @@ namespace NetworkManager { -class IotRequestHandler +/** + * @brief The IotResource class provides abstraction of IoTivity resource model + */ +class IotResourceServer { public: + + IotResourceServer(const std::string& uri, const std::string& type); + + IotResourceServer(const std::string& uri, const std::string& type, const std::vector& interfaces); + + /** + * @brief entityHandler - resource users request handler (called by OCF) + * @param request [in] request for resource + * @return error code or OC_EH_OK + */ + OCEntityHandlerResult entityHandler(std::shared_ptr request); + /** * @brief OCResource GET method handler * @param representation [in] request body @@ -69,30 +84,6 @@ public: return OC_EH_BAD_REQ; } - virtual OCResourceHandle getHandle() = 0; - - virtual ~IotRequestHandler() {} -}; - - -/** - * @brief The IotResource class provides abstraction of IoTivity resource model - */ -class IotResourceServer -{ -public: - - IotResourceServer(IotRequestHandler* request_handler, const std::string& uri, const std::string& type); - - IotResourceServer(IotRequestHandler* request_handler, const std::string& uri, const std::string& type, const std::vector& interfaces); - - /** - * @brief entityHandler - resource users request handler (called by OCF) - * @param request [in] request for resource - * @return error code or OC_EH_OK - */ - OCEntityHandlerResult entityHandler(std::shared_ptr request); - /** * @brief getHandle - retruns handle used for publishing of the resource on resource directory * @return handle @@ -104,7 +95,6 @@ public: private: OCResourceHandle m_handle; - IotRequestHandler* m_request_handler; }; } // namespace NetworkManager diff --git a/device_core/iotivity_lib/inc/iotdevice.h b/device_core/iotivity_lib/inc/iotdevice.h index 9a3686f..c54332f 100644 --- a/device_core/iotivity_lib/inc/iotdevice.h +++ b/device_core/iotivity_lib/inc/iotdevice.h @@ -130,15 +130,6 @@ public: { } - /** - * @brief deleteApp performes app uninstallation on remote device - * @param duid [in] id of the device where application should be removed - * @param iotivity [in] pointer to the IoTivity instance - * @param app_name [in] application name - */ - static void deleteApp(const std::string& duid, IoTivity* iotivity, const std::string& app_name); - - // deleteApp: implemented in iotdevice_inpl.cpp }; typedef std::shared_ptr IoTDevicePtr; diff --git a/device_core/iotivity_lib/src/device_control.cpp b/device_core/iotivity_lib/src/device_control.cpp new file mode 100644 index 0000000..2b9bf9e --- /dev/null +++ b/device_core/iotivity_lib/src/device_control.cpp @@ -0,0 +1,68 @@ +#include "device_control.h" +#include "device_commands.h" +#include "nmexceptions.h" +#include "resource_callbacks.h" +#include "logging.h" +#include "network_manager_tag.h" + +namespace PH = std::placeholders; + +namespace NetworkManager +{ + +const std::string DeviceControl::CTRL_RESOURCE_TYPE = "device.control"; + +DeviceControl::DeviceControl(const std::string& host, const std::string& duid, IoTivity* iotivity) +{ + m_resource = iotivity->findResource(host, CTRL_RESOURCE_TYPE, OC_RSRVD_WELL_KNOWN_URI, duid); +} + +NM_ErrorCode DeviceControl::deleteApp(const std::string& app_name, const std::string& duid) +{ + if(!m_resource) + { + return EC_ITEM_NOT_FOUND; + } + + OC::OCRepresentation request; + request.setValue("command", int(DeviceCommands::UNINSTALL)); + request.setValue("name", app_name); + request.setValue("duid", duid); + + return sendCommand(request); +} + +NM_ErrorCode DeviceControl::unOwn() +{ + if(!m_resource) + { + return EC_ITEM_NOT_FOUND; + } + + OC::OCRepresentation request; + request.setValue("command", int(DeviceCommands::UNOWN)); + return sendCommand(request); +} + +NM_ErrorCode DeviceControl::sendCommand(const OC::OCRepresentation& representation) +{ + NetworkManager::PostResourceCallback::Sptr callback = std::make_shared(); + auto result = m_resource->post(representation, QueryParamsMap{}, bind_callback(callback, PH::_1, PH::_2, PH::_3)); + + NM_ErrorCode error = EC_OK; + + if (OC_STACK_OK != result) + { + LOG_E(TAG, "Post to control resource request failed with code: %d", result); + error = result == OC_STACK_UNAUTHORIZED_REQ? EC_UNAUTHORIZED : EC_IOTIVITY_ERROR; + } + else if (!callback->wait()) + { + LOG_E(TAG, "Post to control resource error - callback not called"); + error = EC_NO_CONNECTION; + } + + return error; +} + +} diff --git a/device_core/iotivity_lib/src/iot_resource_server.cpp b/device_core/iotivity_lib/src/iot_resource_server.cpp index d63a790..cb2431f 100644 --- a/device_core/iotivity_lib/src/iot_resource_server.cpp +++ b/device_core/iotivity_lib/src/iot_resource_server.cpp @@ -10,15 +10,15 @@ namespace PH = std::placeholders; namespace NetworkManager { -IotResourceServer::IotResourceServer(IotRequestHandler* request_handler, const std::string& uri, const std::string& type) - : IotResourceServer(request_handler, uri, type, {OC::DEFAULT_INTERFACE}) +IotResourceServer::IotResourceServer(const std::string& uri, const std::string& type) + : IotResourceServer(uri, type, {OC::DEFAULT_INTERFACE}) { } -IotResourceServer::IotResourceServer(IotRequestHandler* request_handler, const std::string& uri, const std::string& type, const std::vector& interfaces) - : m_handle(nullptr), m_request_handler(request_handler) +IotResourceServer::IotResourceServer(const std::string& uri, const std::string& type, const std::vector& interfaces) + : m_handle(nullptr) { - if (uri.empty() || type.empty() || interfaces.size() == 0 || request_handler == nullptr) + if (uri.empty() || type.empty() || interfaces.size() == 0) { throw IoTInternalError("Register resource failed due to bad parameters", EC_BAD_PARAMETER); } @@ -58,7 +58,7 @@ OCEntityHandlerResult IotResourceServer::entityHandler(std::shared_ptrgetRequestType(); int requestFlag = request->getRequestHandlerFlag(); @@ -75,26 +75,26 @@ OCEntityHandlerResult IotResourceServer::entityHandler(std::shared_ptrgetHandler(representation, params, response_body); + res = getHandler(representation, params, response_body); } else if (method == "POST") { - res = m_request_handler->postHandler(representation, params, response_body); + res = postHandler(representation, params, response_body); } else if (method == "PUT") { - res = m_request_handler->putHandler(representation, params, response_body); + res = putHandler(representation, params, response_body); } else if (method == "DELETE") { - res = m_request_handler->deleteHandler(representation, params, response_body); + res = deleteHandler(representation, params, response_body); } } if (requestFlag & OC::RequestHandlerFlag::ObserverFlag) { const OC::ObservationInfo& observationInfo = request->getObservationInfo(); - res = m_request_handler->observHandler(representation, params, observationInfo); + res = observHandler(representation, params, observationInfo); } response->setResponseResult(res); diff --git a/device_core/iotivity_lib/src/iotdevice_impl.cpp b/device_core/iotivity_lib/src/iotdevice_impl.cpp index 241d074..1d4a875 100644 --- a/device_core/iotivity_lib/src/iotdevice_impl.cpp +++ b/device_core/iotivity_lib/src/iotdevice_impl.cpp @@ -9,7 +9,7 @@ #include #include "resource_callbacks.h" #include "iotutils.h" -#include "device_commands.h" +#include "device_control.h" #include "EasySetup.hpp" #include "ESRichCommon.h" @@ -24,7 +24,6 @@ namespace const std::string dev_info_uri{"/oic/d"}; const std::string dev_type_resource{"oic.d."}; const std::string dev_type_uri{"/oic/res?rt=oic.wk.d"}; -const std::string CTRL_RESOURCE_TYPE = "device.control"; void esErrorGuard(ESResult error) { @@ -50,48 +49,11 @@ void esErrorGuard(ESResult error) } } -void sendCommand(OCResource::Ptr ctrl, const OC::OCRepresentation& representation) -{ - NetworkManager::PostResourceCallback::Sptr callback = std::make_shared(); - auto result = ctrl->post(representation, QueryParamsMap{}, bind_callback(callback, PH::_1, PH::_2, PH::_3)); - - if (OC_STACK_OK != result) - { - throw NetworkManager::IoTInternalError("Post of \"unown\" request failed with code: ", result); - } - - if (!callback->wait()) - { - throw NetworkManager::IoTInternalError("Post of \"unown\" request error - callback not called", EC_UNAUTHORIZED); - } -} - } namespace NetworkManager { -/** - * @brief Implementation of static method IoTDevice::deleteApp - * @param duid [in] id of the device where application should be removed - * @param iotivity [in] pointer to the IoTivity instance - * @param app_name [in] application name - */ -void IoTDevice::deleteApp(const std::string& duid, IoTivity* iotivity, const std::string& app_name) -{ - OC::OCResource::Ptr ctrl_resource = iotivity->findResource(iotivity->host(), CTRL_RESOURCE_TYPE, OC_RSRVD_WELL_KNOWN_URI, duid); - - if(!ctrl_resource) - { - throw IoTInternalError("Device not found", EC_ITEM_NOT_FOUND); - } - - OC::OCRepresentation request; - request.setValue("command", int(DeviceCommands::UNINSTALL)); - request.setValue("name", std::string{app_name}); - sendCommand(ctrl_resource, request); -} - IoTDevice_impl::IoTDevice_impl(std::shared_ptr device_resource, bool connected) : dev(device_resource), name("unknown"), model("unknown"), type("unknown"), uuid(""), spec_ver("unknown"), online(connected), cloud_accessibility(false) @@ -178,30 +140,15 @@ IoTDevice_impl::~IoTDevice_impl() { } -OCResource::Ptr IoTDevice_impl::getControlResource() +void IoTDevice_impl::unOwnDevice() { - if(host.empty()) + if (host.empty()) { - throw IoTInternalError("host is empty", EC_NOT_INITIALIZED); + throw IoTInternalError("Device not active", EC_ITEM_NOT_FOUND); } - auto iotinst = IoTivity::getInstance(); - OCResource::Ptr ctrl_resource = iotinst->findResource(host, CTRL_RESOURCE_TYPE); - - if(!ctrl_resource) - { - throw IoTInternalError("Control resource not found", EC_GENERIC_ERROR); - } - - return ctrl_resource; -} - -void IoTDevice_impl::unOwnDevice() -{ - auto ctrl_resource = getControlResource(); - OCRepresentation request; - request.setValue("command", int(DeviceCommands::UNOWN)); - sendCommand(ctrl_resource, request); + DeviceControl control(host, uuid, IoTivity::getInstance()); + control.unOwn(); } void IoTDevice_impl::setCloudProperties(const std::string& host, const std::string& provider, const std::string& token) diff --git a/device_core/iotivity_lib/src/registration_mq.cpp b/device_core/iotivity_lib/src/registration_mq.cpp index c2be0a2..e1e0e55 100644 --- a/device_core/iotivity_lib/src/registration_mq.cpp +++ b/device_core/iotivity_lib/src/registration_mq.cpp @@ -36,7 +36,7 @@ void RegistrationMQ::reg( OC::OCRepresentation rpr; rpr.setValue("duid", duid); rpr.setValue("uuid", uuid); - rpr.setValue("route", (parent.empty() ? duid : parent)); + rpr.setValue("parentUuid", parent); rpr.setValue("name", name); rpr.setValue("model", model); rpr.setValue("type", type); diff --git a/device_core/nmdaemon/audit_trail_client.cpp b/device_core/nmdaemon/audit_trail_client.cpp index cff23a6..470e47e 100644 --- a/device_core/nmdaemon/audit_trail_client.cpp +++ b/device_core/nmdaemon/audit_trail_client.cpp @@ -129,14 +129,7 @@ void AuditTrailClient::sendReport(std::string report) rpr.setValue("report", makeReport(m_device_id, "smack", 0, report)); rpr.setValue("duid", m_device_id); - if (m_mode == WorkingMode::Hub) - { - m_proxy_thread->addAction(std::async(std::launch::deferred, &ReportHandler::pass, m_report_handler, rpr, OC::QueryParamsMap{})); - } - else - { - m_report_handler->pass(rpr, OC::QueryParamsMap{}); - } + m_report_handler->pass(rpr, OC::QueryParamsMap{}); } } // namespace NMD diff --git a/device_core/nmdaemon/commandhandler.cpp b/device_core/nmdaemon/commandhandler.cpp index a3c4063..8a3034e 100644 --- a/device_core/nmdaemon/commandhandler.cpp +++ b/device_core/nmdaemon/commandhandler.cpp @@ -3,6 +3,7 @@ #include "device_commands.h" #include "application_service.h" #include "logging.h" +#include "device_control.h" #define TAG "nmdaemon" @@ -52,11 +53,33 @@ bool CommandHandler::process(const OC::OCRepresentation& command) bool CommandHandler::uninstallCommand(const OC::OCRepresentation& command) { std::string name = command.getValue("name"); + std::string duid = command.getValue("duid"); LOG_D(TAG, "Request to uninstall \"%s\"", name.c_str()); if (name.empty()) return false; - return (0 == ApplicationService::uninstall(name)); + bool result = false; + + if (duid == m_iotivity->getDeviceID()) + { + result = 0 == ApplicationService::uninstall(name); + } + else + { + if ((result = m_hub->isOwned(duid))) + { + NetworkManager::IoTivity* iot = m_iotivity; + auto deleter = [duid, iot, name] + { + NetworkManager::DeviceControl control("", duid, iot); + control.deleteApp(name, duid); + }; + + m_proxy_thread->addAction(std::async(std::launch::deferred, deleter)); + } + } + + return result; } bool CommandHandler::unOwnCommand(const OC::OCRepresentation& command) diff --git a/device_core/nmdaemon/control_resource.cpp b/device_core/nmdaemon/control_resource.cpp index c059097..b2083bf 100644 --- a/device_core/nmdaemon/control_resource.cpp +++ b/device_core/nmdaemon/control_resource.cpp @@ -24,7 +24,7 @@ const std::string CTRL_RESOURCE_TYPE = "device.control"; namespace NMD { ControlResource::ControlResource(ICommandHandler* handler) - : m_resource(this, CTRL_RESOURCE_URI, CTRL_RESOURCE_TYPE) + : NetworkManager::IotResourceServer(CTRL_RESOURCE_URI, CTRL_RESOURCE_TYPE) , m_handler(handler) { assert(nullptr != handler); @@ -32,6 +32,7 @@ ControlResource::ControlResource(ICommandHandler* handler) OCEntityHandlerResult ControlResource::postHandler(const OC::OCRepresentation& representation, const OC::QueryParamsMap& /*params*/, OC::OCRepresentation& /*response_body*/) { + LOG_D(TAG, "ControlResource::postHandler < IN"); return m_handler->process(representation) ? OC_EH_OK : OC_EH_BAD_REQ; } diff --git a/device_core/nmdaemon/control_resource.h b/device_core/nmdaemon/control_resource.h index 74c9f12..a30ad2d 100644 --- a/device_core/nmdaemon/control_resource.h +++ b/device_core/nmdaemon/control_resource.h @@ -16,7 +16,7 @@ namespace NMD { -class ControlResource : public NetworkManager::IotRequestHandler //IotResource +class ControlResource : public NetworkManager::IotResourceServer //IotResource { public: /** @@ -31,13 +31,8 @@ public: OCEntityHandlerResult postHandler(const OC::OCRepresentation& representation, const OC::QueryParamsMap& params, OC::OCRepresentation& response_body) override; - OCResourceHandle getHandle() override - { - m_resource.getHandle(); - } private: ICommandHandler* m_handler; - NetworkManager::IotResourceServer m_resource; }; } // namespace NMD diff --git a/device_core/nmdaemon/hub_policy_resource.cpp b/device_core/nmdaemon/hub_policy_resource.cpp index 4dd27c1..3b5933f 100644 --- a/device_core/nmdaemon/hub_policy_resource.cpp +++ b/device_core/nmdaemon/hub_policy_resource.cpp @@ -131,7 +131,7 @@ OCEntityHandlerResult PolicyResource::entityHandler(std::shared_ptrgetResourceRepresentation(); - QueryParamsMap qp{{"did", did}, {"route", hub_did}, {"agent", agent_id}}; + QueryParamsMap qp{{"did", did}, {"agent", agent_id}}; if (bool(proxy)) { diff --git a/device_core/nmdaemon/hub_resource.cpp b/device_core/nmdaemon/hub_resource.cpp index 4a31854..3c91423 100644 --- a/device_core/nmdaemon/hub_resource.cpp +++ b/device_core/nmdaemon/hub_resource.cpp @@ -30,7 +30,7 @@ HubResource::HubResource( std::shared_ptr proxy_thread, const std::string& _devices_file_path_name ) - : m_resource(this, HUB_RESOURCE_URI, HUB_RESORCE_TYPE) + : NetworkManager::IotResourceServer(HUB_RESOURCE_URI, HUB_RESORCE_TYPE) , m_devices_file_path_name(_devices_file_path_name) , m_iotivity(iotivity) , m_proxy_thread(proxy_thread) @@ -70,6 +70,12 @@ void HubResource::unownAll() } } +bool HubResource::isOwned(const std::string& duid) +{ + std::unique_lock lock(m_mutex); + return m_owned_devices.find(duid) != m_owned_devices.cend(); +} + OCEntityHandlerResult HubResource::getHandler(const OCRepresentation& /*request_body*/, const OC::QueryParamsMap& params, OCRepresentation& response_body) { OCEntityHandlerResult res = OC_EH_BAD_REQ; @@ -89,6 +95,8 @@ OCEntityHandlerResult HubResource::getHandler(const OCRepresentation& /*request_ } response_body.setValue("devices", dev_list); + + res = OC_EH_OK; } return res; @@ -182,7 +190,15 @@ void HubResource::ownDevice(const std::string& did) if (device) { device->setWiFiProperties(m_iotivity->getDeviceID(), "Iotivity_PWD"); - RegistrationMQ::reg(m_iotivity->getMqHandler(), device->getUUID(), m_iotivity->getCloudAuthId(), device->getName(), device->getModel(), device->getType()); + RegistrationMQ::reg( + m_iotivity->getMqHandler(), + device->getUUID(), + m_iotivity->getCloudAuthId(), + device->getName(), + device->getModel(), + device->getType(), + m_iotivity->getDeviceID() + ); ok = true; } diff --git a/device_core/nmdaemon/hub_resource.h b/device_core/nmdaemon/hub_resource.h index 0406dd4..4fc1496 100644 --- a/device_core/nmdaemon/hub_resource.h +++ b/device_core/nmdaemon/hub_resource.h @@ -11,7 +11,7 @@ namespace NMD { -class HubResource : public NetworkManager::IotRequestHandler /*NetworkManager::IotResource*/ +class HubResource : public NetworkManager::IotResourceServer { public: @@ -59,6 +59,13 @@ public: void unownAll(); /** + * @brief Check that device with specified device id is owned by this hub. + * @param duid [in] device id. + * @return true if device owned by this hub. + */ + bool isOwned(const std::string& duid); + + /** * @brief Set enabled state of the hub. If set to false the hub sends empty device list * on response to "get device list" requests even if devices are found. */ @@ -71,7 +78,7 @@ public: * @brief Returns state of the hub. * @return true if enabled and false otherwise. */ - bool isEnabled() + bool isEnabled() const { return m_enabled; } @@ -80,11 +87,6 @@ public: OCEntityHandlerResult postHandler(const OC::OCRepresentation& representation, const OC::QueryParamsMap& params, OC::OCRepresentation& response_body) override; - OCResourceHandle getHandle() override - { - m_resource.getHandle(); - } - private: /** @@ -129,7 +131,6 @@ private: static const int m_find_devices_interval = 10; static const int m_own_devices_interval = 10; - NetworkManager::IotResourceServer m_resource; std::string m_devices_file_path_name; NetworkManager::IoTivity* m_iotivity; std::shared_ptr m_proxy_thread; diff --git a/device_core/nmdaemon/policyhandler.cpp b/device_core/nmdaemon/policyhandler.cpp index e639b55..783d67f 100644 --- a/device_core/nmdaemon/policyhandler.cpp +++ b/device_core/nmdaemon/policyhandler.cpp @@ -24,14 +24,14 @@ void PolicyHandler::observeCallback(const HeaderOptions& head_options, const OCR { std::string policy = rep.getValue("policy"); std::string did = rep.getValue("duid"); - std::string route = rep.getValue("route"); + std::string parentUuid = rep.getValue("parentUuid"); LOG_D(TAG, "[Recieved Policy for %s]\n%s\n", did.c_str(), policy.c_str()); const std::string& device_id = NetworkManager::IoTivity::getInstance()->getDeviceID(); if (did == device_id) { iot::core::PolicyEnforce::GetInstance().ParsePolicy(policy); } - else if (callback && route == device_id) + else if (callback && parentUuid == device_id) { callback(head_options, rep, ecode, seq_number); } diff --git a/device_core/nmdaemon/policyhandlermq.cpp b/device_core/nmdaemon/policyhandlermq.cpp index 100c394..8d07bad 100644 --- a/device_core/nmdaemon/policyhandlermq.cpp +++ b/device_core/nmdaemon/policyhandlermq.cpp @@ -32,16 +32,6 @@ void PolicyHandlerMQ::pass(const OC::OCRepresentation& rep, const OC::QueryParam } msg.setValue("duid", duid); - it = params.find("hub_did"); - - if (it != params.end()) - { - msg.setValue("route", it->second); - } - else - { - msg.setValue("route", duid); - } it = params.find("agent"); diff --git a/device_core/utest/test_registrationmq.cpp b/device_core/utest/test_registrationmq.cpp index 38b2a2e..a8e50a8 100644 --- a/device_core/utest/test_registrationmq.cpp +++ b/device_core/utest/test_registrationmq.cpp @@ -46,7 +46,7 @@ TEST(test_RegistrationMQ, test_reg) OC::OCRepresentation representation1; representation1.setValue("duid", DUID); representation1.setValue("uuid", UUID); - representation1.setValue("route", DUID); + representation1.setValue("parentUuid", DUID); representation1.setValue("name", NAME); representation1.setValue("model", MODEL); representation1.setValue("type", TYPE); @@ -54,7 +54,7 @@ TEST(test_RegistrationMQ, test_reg) OC::OCRepresentation representation2; representation2.setValue("duid", DUID); representation2.setValue("uuid", UUID); - representation2.setValue("route", PARENT); + representation2.setValue("parentUuid", PARENT); representation2.setValue("name", NAME); representation2.setValue("model", MODEL); representation2.setValue("type", TYPE);