Modify lib interface to use code-gen
[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 "tizen-things-daemon-dbus.h"
19
20 #include "ttd-log.h"
21 #include "ttd-worker-interface.h"
22
23 #define TTSD_DBUS_OBJECT_PATH "/ttsd_worker_dbus_object"
24 #define TTSD_DBUS_NAME "ttsd.worker.dbus"
25
26 struct _worker_interface_h {
27         guint owner_id;
28         GDBusObjectManagerServer *m_server;
29         TtsdWorkerDbus *dbus_obj;
30 };
31
32 static gboolean __received_from_worker(TtsdWorkerDbus *obj, GDBusMethodInvocation *context, gchar *report, void *user_data)
33 {
34         _D("Report[%s] is arrived from worker", report);
35
36         g_assert(obj != NULL);
37         g_assert(context != NULL);
38         g_assert(report != NULL);
39
40         ttsd_worker_dbus_complete_ttsd_worker_submit_report(obj, context);
41
42         /* Put report into queue */
43
44         return FALSE;
45 }
46
47 static void __on_name_acquired_cb(GDBusConnection *connection, const gchar *name, gpointer user_data)
48 {
49         _D("DBus name[%s] is acquired", name);
50 }
51
52 static void __on_bus_acquired_cb(GDBusConnection *connection, const gchar *name, gpointer user_data)
53 {
54         gulong ret = 0;
55
56         if (!user_data) {
57                 _E("DBus handle is not delivered, something wrong");
58                 return;
59         }
60         worker_interface_h *h = user_data;
61
62         h->m_server = g_dbus_object_manager_server_new(TTSD_DBUS_OBJECT_PATH);
63         if (!h->m_server) {
64                 _E("Failed to create dbus server");
65                 ttd_worker_interface_fini(h);
66                 return;
67         }
68
69         h->dbus_obj = ttsd_worker_dbus_skeleton_new();
70         if (!h->dbus_obj) {
71                 _E("Failed to create dbus skeleton");
72                 ttd_worker_interface_fini(h);
73                 return;
74         }
75
76         if (!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(h->dbus_obj),
77                                 connection, TTSD_DBUS_OBJECT_PATH, NULL)) {
78                 _E("Failed to export interface with object path");
79                 ttd_worker_interface_fini(h);
80                 return;
81         }
82
83         ret = g_signal_connect(h->dbus_obj, "handle-ttsd-worker-submit-report",
84                         G_CALLBACK (__received_from_worker), h);
85         if (!ret) {
86                 _E("Failed to connect handle: submit-report");
87                 ttd_worker_interface_fini(h);
88                 return;
89         }
90
91         g_dbus_object_manager_server_set_connection(h->m_server, connection);
92 }
93
94 int ttd_worker_interface_init(worker_interface_h **h)
95 {
96         *h = (worker_interface_h *)calloc(1, sizeof(worker_interface_h));
97         if (!*h) {
98                 _E("Failed to allocate memory for dbus handle");
99                 return -1;
100         }
101
102         (*h)->owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM, TTSD_DBUS_NAME, G_BUS_NAME_OWNER_FLAGS_NONE,
103                         __on_bus_acquired_cb, __on_name_acquired_cb, NULL, *h, NULL);
104         if (!(*h)->owner_id) {
105                 _E("Failed to get identifier for dbus");
106                 g_free(*h);
107                 return -1;
108         }
109
110         return 0;
111 }
112
113 void ttd_worker_interface_fini(worker_interface_h *h)
114 {
115         if (!h) {
116                 _E("Worker interface handle is NULL");
117                 return;
118         }
119
120         if (h->dbus_obj)
121                 g_object_unref(h->dbus_obj);
122
123         if (h->owner_id)
124                 g_bus_unown_name(h->owner_id);
125
126         if (h->m_server)
127                 g_object_unref(h->m_server);
128
129         g_free(h);
130
131         return;
132 }