Add working state for plural reports
[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
23 struct _worker_interface_h {
24         guint owner_id;
25         GDBusObjectManagerServer *m_server;
26         TtdWorkerDbus *dbus_obj;
27 };
28
29 static void
30 __received_from_worker(TtdWorkerDbus *obj,
31                 GDBusMethodInvocation *context,
32                 GVariant *parameters,
33                 void *user_data)
34 {
35         int ret = 0;
36
37         ret_if(!obj);
38         ret_if(!context);
39         ret_if(!parameters);
40
41         ttd_worker_dbus_complete_ttd_worker_submit_report(obj, context, ret);
42 }
43
44 static void __on_name_acquired_cb(GDBusConnection *connection, const gchar *name, gpointer user_data)
45 {
46         _D("On Name[%s] is acquired", name);
47 }
48
49 static void __on_name_lost_cb(GDBusConnection *connection, const gchar *name, gpointer user_data)
50 {
51         _D("On Name[%s] is lost", name);
52 }
53
54 static void __on_bus_acquired_cb(GDBusConnection *connection, const gchar *name, gpointer user_data)
55 {
56         gulong ret = 0;
57         _D("On Bus acquired[%s]", name);
58
59         if (!user_data) {
60                 _E("DBus handle is not delivered, something wrong");
61                 return;
62         }
63         worker_interface_h h = user_data;
64
65         h->m_server = g_dbus_object_manager_server_new(TTD_WORKER_DBUS_OBJECT_PATH);
66         if (!h->m_server) {
67                 _E("Failed to create dbus server");
68                 ttd_worker_interface_fini(h);
69                 return;
70         }
71
72         h->dbus_obj = ttd_worker_dbus_skeleton_new();
73         if (!h->dbus_obj) {
74                 _E("Failed to create dbus skeleton");
75                 ttd_worker_interface_fini(h);
76                 return;
77         }
78
79         if (!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(h->dbus_obj),
80                                 connection, TTD_WORKER_DBUS_OBJECT_PATH, NULL)) {
81                 _E("Failed to export interface with object path");
82                 ttd_worker_interface_fini(h);
83                 return;
84         }
85
86         ret = g_signal_connect(h->dbus_obj, "handle-ttd-worker-submit-report",
87                         G_CALLBACK (__received_from_worker), h);
88         if (!ret) {
89                 _E("Failed to connect handle: submit-report");
90                 ttd_worker_interface_fini(h);
91                 return;
92         }
93
94         g_dbus_object_manager_server_set_connection(h->m_server, connection);
95 }
96
97 int ttd_worker_interface_init(worker_interface_h *h)
98 {
99         worker_interface_h _h = NULL;
100
101         if (!h) {
102                 _E("Worker Interface handler is NULL");
103                 return -1;
104         }
105
106         _h = g_try_malloc0(sizeof(worker_interface_h));
107         if (!_h) {
108                 _E("Failed to allocate memory for dbus handle");
109                 return -1;
110         }
111
112         _h->owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
113                         TTD_WORKER_DBUS_NAME,
114                         G_BUS_NAME_OWNER_FLAGS_NONE,
115                         __on_bus_acquired_cb,
116                         __on_name_acquired_cb,
117                         __on_name_lost_cb,
118                         _h,
119                         NULL);
120         if (!_h->owner_id) {
121                 _E("Failed to get identifier for dbus");
122                 g_free(_h);
123                 return -1;
124         }
125
126         *h = _h;
127
128         return 0;
129 }
130
131 void ttd_worker_interface_fini(worker_interface_h h)
132 {
133         if (!h) {
134                 _E("Worker interface handle is NULL");
135                 return;
136         }
137
138         if (h->dbus_obj)
139                 g_object_unref(h->dbus_obj);
140
141         if (h->owner_id)
142                 g_bus_unown_name(h->owner_id);
143
144         if (h->m_server)
145                 g_object_unref(h->m_server);
146
147         g_free(h);
148
149         return;
150 }