Fixes for NM_deleteApp.
authorLomtev Dmytro <d.lomtev@samsung.com>
Fri, 18 Aug 2017 06:51:37 +0000 (09:51 +0300)
committerLomtev Dmytro <d.lomtev@samsung.com>
Fri, 18 Aug 2017 06:51:37 +0000 (09:51 +0300)
device_core/ctrl_app_lib/inc/iotchilddevice_impl.h
device_core/ctrl_app_lib/src/ctrl_app_support.cpp
device_core/ctrl_app_lib/src/iotchilddevice_impl.cpp
device_core/iotivity_lib/inc/iotdevice.h
device_core/iotivity_lib/inc/iotdevice_impl.h
device_core/iotivity_lib/src/iotdevice_impl.cpp
device_core/nmdaemon/application_service.cpp
device_core/nmdaemon/commandhandler.cpp
device_core/nmdaemon/control_resource.cpp
device_core/utest/test_iotdevice_impl.cpp

index 9f3d959..7c91128 100644 (file)
@@ -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;
index 467fee2..35fd745 100644 (file)
@@ -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)
     {
index 77b15e1..5b1a42d 100644 (file)
@@ -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);
-}
-
 }
index 990dbe7..9a3686f 100644 (file)
@@ -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<IoTDevice> IoTDevicePtr;
index 45260f0..362c95d 100644 (file)
@@ -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;
index 94ee7b8..241d074 100644 (file)
@@ -50,11 +50,48 @@ void esErrorGuard(ESResult error)
     }
 }
 
+void sendCommand(OCResource::Ptr ctrl, const OC::OCRepresentation& representation)
+{
+    NetworkManager::PostResourceCallback::Sptr callback = std::make_shared<NetworkManager::PostResourceCallback>();
+    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<OCResource> 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<PostResourceCallback>();
-    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();
index 8429848..a02f416 100644 (file)
@@ -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;
index 4c9ebbc..a3c4063 100644 (file)
@@ -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<std::string>("name");
+    LOG_D(TAG, "Request to uninstall \"%s\"", name.c_str());
 
     if (name.empty()) return false;
 
index 672434a..4a8f3a9 100644 (file)
@@ -55,13 +55,16 @@ OCEntityHandlerResult ControlResource::entityHandler(std::shared_ptr<OCResourceR
             }
             else if(rt == "POST")
             {
+                LOG_D(TAG, "Control resource command received");
                 const OCRepresentation& representation = request->getResourceRepresentation();
 
                 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;
index c01f553..792c7fe 100644 (file)
@@ -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());