Make dbus method name shorter
[apps/native/tizen-things-daemon.git] / daemon / src / ttd-worker-interface.c
index 9d828ab..7214b9d 100644 (file)
  */
 
 #include <glib.h>
-#include "tizen-things-daemon-dbus.h"
-
 #include "ttd-log.h"
 #include "ttd-worker-interface.h"
-
-#define TTSD_DBUS_OBJECT_PATH "/ttsd_worker_dbus_object"
-#define TTSD_DBUS_NAME "ttsd.worker.dbus"
+#include "common-worker-inf-def.h"
+#include "common-worker-inf-dbus.h"
+#include "ttd-cmd-mgr.h"
 
 struct _worker_interface_h {
        guint owner_id;
        GDBusObjectManagerServer *m_server;
-       TtsdWorkerDbus *dbus_obj;
+       TtdWorker *dbus_obj;
 };
 
-static gboolean __received_from_worker(TtsdWorkerDbus *obj, GDBusMethodInvocation *context, gchar *report, void *user_data)
+static gboolean
+__received_from_worker(TtdWorker *obj,
+               GDBusMethodInvocation *invocation,
+               const gchar *cmd_id,
+               const gchar *report,
+               gint working_state,
+               gpointer user_data)
 {
-       _D("Report[%s] is arrived from worker", report);
+       int ret = 0;
+
+       if (!cmd_id) {
+               _E("cmd_id is NULL");
+               ret = -1;
+               goto EXIT;
+       }
 
-       g_assert(obj != NULL);
-       g_assert(context != NULL);
-       g_assert(report != NULL);
+       if ((working_state < TTD_CMD_RESULT_RUNNING) ||
+                       (working_state > TTD_CMD_RESULT_FAIL)) {
+               _E("invail working state [%d]", working_state);
+               ret = -1;
+               goto EXIT;
+       }
+
+       if (!report)
+               _W("report is NULL");
 
-       ttsd_worker_dbus_complete_ttsd_worker_submit_report(obj, context);
+       ret = ttd_cmd_mgr_push_result(cmd_id, working_state, report);
 
-       /* Put report into queue */
+EXIT:
+       ttd_worker_complete_report(obj, invocation, ret);
 
        return FALSE;
 }
 
 static void __on_name_acquired_cb(GDBusConnection *connection, const gchar *name, gpointer user_data)
 {
-       _D("DBus name[%s] is acquired", name);
+       _D("On Name[%s] is acquired", name);
+}
+
+static void __on_name_lost_cb(GDBusConnection *connection, const gchar *name, gpointer user_data)
+{
+       _D("On Name[%s] is lost", name);
 }
 
 static void __on_bus_acquired_cb(GDBusConnection *connection, const gchar *name, gpointer user_data)
 {
        gulong ret = 0;
+       _D("On Bus acquired[%s]", name);
 
        if (!user_data) {
                _E("DBus handle is not delivered, something wrong");
                return;
        }
-       worker_interface_h *h = user_data;
+       worker_interface_h h = user_data;
 
-       h->m_server = g_dbus_object_manager_server_new(TTSD_DBUS_OBJECT_PATH);
+       h->m_server = g_dbus_object_manager_server_new(TTD_WORKER_DBUS_OBJECT_PATH);
        if (!h->m_server) {
                _E("Failed to create dbus server");
                ttd_worker_interface_fini(h);
                return;
        }
 
-       h->dbus_obj = ttsd_worker_dbus_skeleton_new();
+       h->dbus_obj = ttd_worker_skeleton_new();
        if (!h->dbus_obj) {
                _E("Failed to create dbus skeleton");
                ttd_worker_interface_fini(h);
@@ -74,16 +97,16 @@ static void __on_bus_acquired_cb(GDBusConnection *connection, const gchar *name,
        }
 
        if (!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(h->dbus_obj),
-                               connection, TTSD_DBUS_OBJECT_PATH, NULL)) {
+                               connection, TTD_WORKER_DBUS_OBJECT_PATH, NULL)) {
                _E("Failed to export interface with object path");
                ttd_worker_interface_fini(h);
                return;
        }
 
-       ret = g_signal_connect(h->dbus_obj, "handle-ttsd-worker-submit-report",
+       ret = g_signal_connect(h->dbus_obj, "handle-report",
                        G_CALLBACK (__received_from_worker), h);
        if (!ret) {
-               _E("Failed to connect handle: submit-report");
+               _E("Failed to connect handle: report");
                ttd_worker_interface_fini(h);
                return;
        }
@@ -91,26 +114,41 @@ static void __on_bus_acquired_cb(GDBusConnection *connection, const gchar *name,
        g_dbus_object_manager_server_set_connection(h->m_server, connection);
 }
 
-int ttd_worker_interface_init(worker_interface_h **h)
+int ttd_worker_interface_init(worker_interface_h *h)
 {
-       *h = (worker_interface_h *)calloc(1, sizeof(worker_interface_h));
-       if (!*h) {
+       worker_interface_h _h = NULL;
+
+       if (!h) {
+               _E("Worker Interface handler is NULL");
+               return -1;
+       }
+
+       _h = g_try_malloc0(sizeof(worker_interface_h));
+       if (!_h) {
                _E("Failed to allocate memory for dbus handle");
                return -1;
        }
 
-       (*h)->owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM, TTSD_DBUS_NAME, G_BUS_NAME_OWNER_FLAGS_NONE,
-                       __on_bus_acquired_cb, __on_name_acquired_cb, NULL, *h, NULL);
-       if (!(*h)->owner_id) {
+       _h->owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
+                       TTD_WORKER_DBUS_NAME,
+                       G_BUS_NAME_OWNER_FLAGS_NONE,
+                       __on_bus_acquired_cb,
+                       __on_name_acquired_cb,
+                       __on_name_lost_cb,
+                       _h,
+                       NULL);
+       if (!_h->owner_id) {
                _E("Failed to get identifier for dbus");
-               g_free(*h);
+               g_free(_h);
                return -1;
        }
 
+       *h = _h;
+
        return 0;
 }
 
-void ttd_worker_interface_fini(worker_interface_h *h)
+void ttd_worker_interface_fini(worker_interface_h h)
 {
        if (!h) {
                _E("Worker interface handle is NULL");