2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
4 * Licensed under the Flora License, Version 1.1 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "ttd-worker-interface.h"
20 #include "common-worker-inf-def.h"
21 #include "common-worker-inf-dbus.h"
22 #include "ttd-cmd-mgr.h"
24 struct _worker_interface_h {
26 GDBusObjectManagerServer *m_server;
31 __received_from_worker(TtdWorker *obj,
32 GDBusMethodInvocation *invocation,
46 if ((working_state < TTD_CMD_RESULT_RUNNING) ||
47 (working_state > TTD_CMD_RESULT_FAIL)) {
48 _E("invail working state [%d]", working_state);
56 ret = ttd_cmd_mgr_push_result(cmd_id, working_state, report);
59 ttd_worker_complete_report(obj, invocation, ret);
64 static void __on_name_acquired_cb(GDBusConnection *connection, const gchar *name, gpointer user_data)
66 _D("On Name[%s] is acquired", name);
69 static void __on_name_lost_cb(GDBusConnection *connection, const gchar *name, gpointer user_data)
71 _D("On Name[%s] is lost", name);
74 static void __on_bus_acquired_cb(GDBusConnection *connection, const gchar *name, gpointer user_data)
77 _D("On Bus acquired[%s]", name);
80 _E("DBus handle is not delivered, something wrong");
83 worker_interface_h h = user_data;
85 h->m_server = g_dbus_object_manager_server_new(TTD_WORKER_DBUS_OBJECT_PATH);
87 _E("Failed to create dbus server");
88 ttd_worker_interface_fini(h);
92 h->dbus_obj = ttd_worker_skeleton_new();
94 _E("Failed to create dbus skeleton");
95 ttd_worker_interface_fini(h);
99 if (!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(h->dbus_obj),
100 connection, TTD_WORKER_DBUS_OBJECT_PATH, NULL)) {
101 _E("Failed to export interface with object path");
102 ttd_worker_interface_fini(h);
106 ret = g_signal_connect(h->dbus_obj, "handle-report",
107 G_CALLBACK (__received_from_worker), h);
109 _E("Failed to connect handle: report");
110 ttd_worker_interface_fini(h);
114 g_dbus_object_manager_server_set_connection(h->m_server, connection);
117 int ttd_worker_interface_init(worker_interface_h *h)
119 worker_interface_h _h = NULL;
122 _E("Worker Interface handler is NULL");
126 _h = g_try_malloc0(sizeof(worker_interface_h));
128 _E("Failed to allocate memory for dbus handle");
132 _h->owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
133 TTD_WORKER_DBUS_NAME,
134 G_BUS_NAME_OWNER_FLAGS_NONE,
135 __on_bus_acquired_cb,
136 __on_name_acquired_cb,
141 _E("Failed to get identifier for dbus");
151 void ttd_worker_interface_fini(worker_interface_h h)
154 _E("Worker interface handle is NULL");
159 g_object_unref(h->dbus_obj);
162 g_bus_unown_name(h->owner_id);
165 g_object_unref(h->m_server);