From dfca511bce542be06283100caea5deebcf8b2610 Mon Sep 17 00:00:00 2001 From: Lomtev Dmytro Date: Fri, 18 Aug 2017 09:51:37 +0300 Subject: [PATCH] Fixes for NM_deleteApp. --- device_core/ctrl_app_lib/inc/iotchilddevice_impl.h | 2 - device_core/ctrl_app_lib/src/ctrl_app_support.cpp | 3 +- .../ctrl_app_lib/src/iotchilddevice_impl.cpp | 5 -- device_core/iotivity_lib/inc/iotdevice.h | 18 ++++-- device_core/iotivity_lib/inc/iotdevice_impl.h | 3 - device_core/iotivity_lib/src/iotdevice_impl.cpp | 67 ++++++++++++---------- device_core/nmdaemon/application_service.cpp | 5 ++ device_core/nmdaemon/commandhandler.cpp | 4 ++ device_core/nmdaemon/control_resource.cpp | 3 + device_core/utest/test_iotdevice_impl.cpp | 3 - 10 files changed, 62 insertions(+), 51 deletions(-) diff --git a/device_core/ctrl_app_lib/inc/iotchilddevice_impl.h b/device_core/ctrl_app_lib/inc/iotchilddevice_impl.h index 9f3d959..7c91128 100644 --- a/device_core/ctrl_app_lib/inc/iotchilddevice_impl.h +++ b/device_core/ctrl_app_lib/inc/iotchilddevice_impl.h @@ -74,8 +74,6 @@ public: void unOwnDevice() override; - void deleteApp(const std::string& app_name) override; - void setState(bool is_online) override { online = is_online; 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 467fee2..35fd745 100644 --- a/device_core/ctrl_app_lib/src/ctrl_app_support.cpp +++ b/device_core/ctrl_app_lib/src/ctrl_app_support.cpp @@ -807,8 +807,7 @@ NM_ErrorCode NM_deleteApp(NM_hContext ctx, const char* dev_id, const char* app_n try { - auto dev = ctx->context->getIoTDevice(dev_id); - dev->deleteApp(app_name); + IoTDevice::deleteApp(dev_id, ctx->instance, app_name); } catch(NMexception& e) { diff --git a/device_core/ctrl_app_lib/src/iotchilddevice_impl.cpp b/device_core/ctrl_app_lib/src/iotchilddevice_impl.cpp index 77b15e1..5b1a42d 100644 --- a/device_core/ctrl_app_lib/src/iotchilddevice_impl.cpp +++ b/device_core/ctrl_app_lib/src/iotchilddevice_impl.cpp @@ -52,9 +52,4 @@ void IoTChildDevice_impl::unOwnDevice() dev->unOwnDevice(uuid); } -void IoTChildDevice_impl::deleteApp(const std::string& app_name) -{ - throw NMexception("IoTChildDevice_impl::deleteApp not implemeted", EC_NOT_IMPLEMENTED_YET); -} - } diff --git a/device_core/iotivity_lib/inc/iotdevice.h b/device_core/iotivity_lib/inc/iotdevice.h index 990dbe7..9a3686f 100644 --- a/device_core/iotivity_lib/inc/iotdevice.h +++ b/device_core/iotivity_lib/inc/iotdevice.h @@ -9,6 +9,8 @@ namespace NetworkManager { +class IoTivity; + /** * @brief The IoTDevice class represents iotivity device */ @@ -108,12 +110,6 @@ public: virtual void unOwnDevice() = 0; /** - * @brief deleteApp performes app uninstallation on remote device - * @param app_name [in] application name - */ - virtual void deleteApp(const std::string& app_name) = 0; - - /** * @brief setState sets online/offline state for device * @param state true if device is online */ @@ -133,6 +129,16 @@ public: virtual void deactivate() { } + + /** + * @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/inc/iotdevice_impl.h b/device_core/iotivity_lib/inc/iotdevice_impl.h index 45260f0..362c95d 100644 --- a/device_core/iotivity_lib/inc/iotdevice_impl.h +++ b/device_core/iotivity_lib/inc/iotdevice_impl.h @@ -74,8 +74,6 @@ public: void unOwnDevice() override; - void deleteApp(const std::string& app_name) override; - void setState(bool is_online) override { online = is_online; @@ -100,7 +98,6 @@ private: void ownPrimitiveDevice(); void infoFromRepresentation(const OC::OCRepresentation& rep); OC::OCResource::Ptr getControlResource(); - void sendCommand(OC::OCResource::Ptr ctrl, const OC::OCRepresentation& rep); OC::OCResource::Ptr dev; std::string name; diff --git a/device_core/iotivity_lib/src/iotdevice_impl.cpp b/device_core/iotivity_lib/src/iotdevice_impl.cpp index 94ee7b8..241d074 100644 --- a/device_core/iotivity_lib/src/iotdevice_impl.cpp +++ b/device_core/iotivity_lib/src/iotdevice_impl.cpp @@ -50,11 +50,48 @@ 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) @@ -159,22 +196,6 @@ OCResource::Ptr IoTDevice_impl::getControlResource() return ctrl_resource; } -void IoTDevice_impl::sendCommand(OCResource::Ptr ctrl, const OC::OCRepresentation& representation) -{ - 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 IoTInternalError("Post of \"unown\" request failed with code: ", result); - } - - if (!callback->wait()) - { - throw IoTInternalError("Post of \"unown\" request error - callback not called", EC_UNAUTHORIZED); - } -} - void IoTDevice_impl::unOwnDevice() { auto ctrl_resource = getControlResource(); @@ -183,20 +204,6 @@ void IoTDevice_impl::unOwnDevice() sendCommand(ctrl_resource, request); } -void IoTDevice_impl::deleteApp(const std::string& app_name) -{ - if (app_name.empty()) - { - throw NMexception("deleteApp called with empty pid or name", EC_BAD_PARAMETER); - } - - auto ctrl_resource = getControlResource(); - OCRepresentation request; - request.setValue("command", int(DeviceCommands::UNINSTALL)); - request.setValue("name", app_name); - sendCommand(ctrl_resource, request); -} - void IoTDevice_impl::setCloudProperties(const std::string& host, const std::string& provider, const std::string& token) { auto iotinst = IoTivity::getInstance(); diff --git a/device_core/nmdaemon/application_service.cpp b/device_core/nmdaemon/application_service.cpp index 8429848..a02f416 100644 --- a/device_core/nmdaemon/application_service.cpp +++ b/device_core/nmdaemon/application_service.cpp @@ -14,6 +14,9 @@ #endif #include "application_service.h" +#include "logging.h" + +#define TAG "nmdaemon" namespace NMD { @@ -39,9 +42,11 @@ int ApplicationService::uninstall(const std::string& package_name) #ifndef __BUILD_UBUNTU__ int res = -1; device_policy_manager_h handle = dpm_manager_create(); + LOG_D(TAG, "DPM handle = 0x%08x", int(handle)); if(handle) { res = dpm_application_uninstall_package(handle, package_name.c_str()); + LOG_D(TAG, "dpm_application_uninstall_package returned %d", res); dpm_manager_destroy(handle); } return res; diff --git a/device_core/nmdaemon/commandhandler.cpp b/device_core/nmdaemon/commandhandler.cpp index 4c9ebbc..a3c4063 100644 --- a/device_core/nmdaemon/commandhandler.cpp +++ b/device_core/nmdaemon/commandhandler.cpp @@ -38,10 +38,13 @@ bool CommandHandler::process(const OC::OCRepresentation& command) switch (action) { case DeviceCommands::UNOWN: + LOG_D(TAG, "Unown command received"); return unOwnCommand(command); case DeviceCommands::UNINSTALL: + LOG_D(TAG, "Uninstall command received"); return uninstallCommand(command); default: + LOG_E(TAG, "Error: unknown command received"); return false; } } @@ -49,6 +52,7 @@ bool CommandHandler::process(const OC::OCRepresentation& command) bool CommandHandler::uninstallCommand(const OC::OCRepresentation& command) { std::string name = command.getValue("name"); + LOG_D(TAG, "Request to uninstall \"%s\"", name.c_str()); if (name.empty()) return false; diff --git a/device_core/nmdaemon/control_resource.cpp b/device_core/nmdaemon/control_resource.cpp index 672434a..4a8f3a9 100644 --- a/device_core/nmdaemon/control_resource.cpp +++ b/device_core/nmdaemon/control_resource.cpp @@ -55,13 +55,16 @@ OCEntityHandlerResult ControlResource::entityHandler(std::shared_ptrgetResourceRepresentation(); if (!m_handler->process(representation)) { + LOG_E(TAG, "Control resource handler returned false"); return res; } + LOG_E(TAG, "Control resource handler returned true"); if (OC_STACK_OK == sendRepresentation(request)) { res = OC_EH_OK; diff --git a/device_core/utest/test_iotdevice_impl.cpp b/device_core/utest/test_iotdevice_impl.cpp index c01f553..792c7fe 100644 --- a/device_core/utest/test_iotdevice_impl.cpp +++ b/device_core/utest/test_iotdevice_impl.cpp @@ -83,9 +83,6 @@ TEST(test_IoTDevice_Impl, test_all) EXPECT_NO_THROW(dev.unOwnDevice()); - EXPECT_ANY_THROW(dev.deleteApp("")); - EXPECT_NO_THROW(dev.deleteApp("app1")); - dev.deactivate(); EXPECT_FALSE(dev.isOnline()); EXPECT_FALSE(dev.isActive()); -- 2.7.4