7214b9d4b66d1a6d0d6970c4b3e4ca0be267c5d2
[apps/native/tizen-things-daemon.git] / daemon / src / ttd-worker-interface.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://floralicense.org/license/
9  *
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.
15  */
16
17 #include <glib.h>
18 #include "ttd-log.h"
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"
23
24 struct _worker_interface_h {
25         guint owner_id;
26         GDBusObjectManagerServer *m_server;
27         TtdWorker *dbus_obj;
28 };
29
30 static gboolean
31 __received_from_worker(TtdWorker *obj,
32                 GDBusMethodInvocation *invocation,
33                 const gchar *cmd_id,
34                 const gchar *report,
35                 gint working_state,
36                 gpointer user_data)
37 {
38         int ret = 0;
39
40         if (!cmd_id) {
41                 _E("cmd_id is NULL");
42                 ret = -1;
43                 goto EXIT;
44         }
45
46         if ((working_state < TTD_CMD_RESULT_RUNNING) ||
47                         (working_state > TTD_CMD_RESULT_FAIL)) {
48                 _E("invail working state [%d]", working_state);
49                 ret = -1;
50                 goto EXIT;
51         }
52
53         if (!report)
54                 _W("report is NULL");
55
56         ret = ttd_cmd_mgr_push_result(cmd_id, working_state, report);
57
58 EXIT:
59         ttd_worker_complete_report(obj, invocation, ret);
60
61         return FALSE;
62 }
63
64 static void __on_name_acquired_cb(GDBusConnection *connection, const gchar *name, gpointer user_data)
65 {
66         _D("On Name[%s] is acquired", name);
67 }
68
69 static void __on_name_lost_cb(GDBusConnection *connection, const gchar *name, gpointer user_data)
70 {
71         _D("On Name[%s] is lost", name);
72 }
73
74 static void __on_bus_acquired_cb(GDBusConnection *connection, const gchar *name, gpointer user_data)
75 {
76         gulong ret = 0;
77         _D("On Bus acquired[%s]", name);
78
79         if (!user_data) {
80                 _E("DBus handle is not delivered, something wrong");
81                 return;
82         }
83         worker_interface_h h = user_data;
84
85         h->m_server = g_dbus_object_manager_server_new(TTD_WORKER_DBUS_OBJECT_PATH);
86         if (!h->m_server) {
87                 _E("Failed to create dbus server");
88                 ttd_worker_interface_fini(h);
89                 return;
90         }
91
92         h->dbus_obj = ttd_worker_skeleton_new();
93         if (!h->dbus_obj) {
94                 _E("Failed to create dbus skeleton");
95                 ttd_worker_interface_fini(h);
96                 return;
97         }
98
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);
103                 return;
104         }
105
106         ret = g_signal_connect(h->dbus_obj, "handle-report",
107                         G_CALLBACK (__received_from_worker), h);
108         if (!ret) {
109                 _E("Failed to connect handle: report");
110                 ttd_worker_interface_fini(h);
111                 return;
112         }
113
114         g_dbus_object_manager_server_set_connection(h->m_server, connection);
115 }
116
117 int ttd_worker_interface_init(worker_interface_h *h)
118 {
119         worker_interface_h _h = NULL;
120
121         if (!h) {
122                 _E("Worker Interface handler is NULL");
123                 return -1;
124         }
125
126         _h = g_try_malloc0(sizeof(worker_interface_h));
127         if (!_h) {
128                 _E("Failed to allocate memory for dbus handle");
129                 return -1;
130         }
131
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,
137                         __on_name_lost_cb,
138                         _h,
139                         NULL);
140         if (!_h->owner_id) {
141                 _E("Failed to get identifier for dbus");
142                 g_free(_h);
143                 return -1;
144         }
145
146         *h = _h;
147
148         return 0;
149 }
150
151 void ttd_worker_interface_fini(worker_interface_h h)
152 {
153         if (!h) {
154                 _E("Worker interface handle is NULL");
155                 return;
156         }
157
158         if (h->dbus_obj)
159                 g_object_unref(h->dbus_obj);
160
161         if (h->owner_id)
162                 g_bus_unown_name(h->owner_id);
163
164         if (h->m_server)
165                 g_object_unref(h->m_server);
166
167         g_free(h);
168
169         return;
170 }