Update copyright information of D-Bus helper library
[framework/connectivity/connman.git] / gdbus / mainloop.c
1 /*
2  *
3  *  D-Bus helper library
4  *
5  *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
6  *
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdint.h>
29
30 #include <glib.h>
31 #include <dbus/dbus.h>
32
33 #ifdef NEED_DBUS_WATCH_GET_UNIX_FD
34 #define dbus_watch_get_unix_fd dbus_watch_get_fd
35 #endif
36
37 #include "gdbus.h"
38
39 #define DISPATCH_TIMEOUT  0
40
41 #define info(fmt...)
42 #define error(fmt...)
43 #define debug(fmt...)
44
45 typedef struct {
46         uint32_t id;
47         DBusTimeout *timeout;
48 } timeout_handler_t;
49
50 struct watch_info {
51         guint watch_id;
52         GIOChannel *io;
53         DBusConnection *conn;
54 };
55
56 struct server_info {
57         guint watch_id;
58         GIOChannel *io;
59         DBusServer *server;
60 };
61
62 struct disconnect_data {
63         GDBusWatchFunction disconnect_cb;
64         void *user_data;
65 };
66
67 static gboolean disconnected_signal(DBusConnection *conn,
68                                                 DBusMessage *msg, void *data)
69 {
70         struct disconnect_data *dc_data = data;
71
72         error("Got disconnected from the system message bus");
73         dc_data->disconnect_cb(conn, dc_data->user_data);
74         dbus_connection_unref(conn);
75
76         return TRUE;
77 }
78
79 static gboolean message_dispatch_cb(void *data)
80 {
81         DBusConnection *connection = data;
82
83         dbus_connection_ref(connection);
84
85         /* Dispatch messages */
86         while (dbus_connection_dispatch(connection) == DBUS_DISPATCH_DATA_REMAINS);
87
88         dbus_connection_unref(connection);
89
90         return FALSE;
91 }
92
93 static gboolean watch_func(GIOChannel *chan, GIOCondition cond, gpointer data)
94 {
95         DBusWatch *watch = data;
96         struct watch_info *info = dbus_watch_get_data(watch);
97         int flags = 0;
98
99         if (cond & G_IO_IN)  flags |= DBUS_WATCH_READABLE;
100         if (cond & G_IO_OUT) flags |= DBUS_WATCH_WRITABLE;
101         if (cond & G_IO_HUP) flags |= DBUS_WATCH_HANGUP;
102         if (cond & G_IO_ERR) flags |= DBUS_WATCH_ERROR;
103
104         dbus_watch_handle(watch, flags);
105
106         if (dbus_connection_get_dispatch_status(info->conn) == DBUS_DISPATCH_DATA_REMAINS)
107                 g_timeout_add(DISPATCH_TIMEOUT, message_dispatch_cb, info->conn);
108
109         return TRUE;
110 }
111
112 static dbus_bool_t add_watch(DBusWatch *watch, void *data)
113 {
114         GIOCondition cond = G_IO_HUP | G_IO_ERR;
115         DBusConnection *conn = data;
116         struct watch_info *info;
117         int fd, flags;
118
119         if (!dbus_watch_get_enabled(watch))
120                 return TRUE;
121
122         info = g_new(struct watch_info, 1);
123
124         fd = dbus_watch_get_unix_fd(watch);
125         info->io = g_io_channel_unix_new(fd);
126         info->conn = dbus_connection_ref(conn);
127
128         dbus_watch_set_data(watch, info, NULL);
129
130         flags = dbus_watch_get_flags(watch);
131
132         if (flags & DBUS_WATCH_READABLE) cond |= G_IO_IN;
133         if (flags & DBUS_WATCH_WRITABLE) cond |= G_IO_OUT;
134
135         info->watch_id = g_io_add_watch(info->io, cond, watch_func, watch);
136
137         return TRUE;
138 }
139
140 static void remove_watch(DBusWatch *watch, void *data)
141 {
142         struct watch_info *info = dbus_watch_get_data(watch);
143
144         dbus_watch_set_data(watch, NULL, NULL);
145
146         if (info) {
147                 g_source_remove(info->watch_id);
148                 g_io_channel_unref(info->io);
149                 dbus_connection_unref(info->conn);
150                 g_free(info);
151         }
152 }
153
154 static void watch_toggled(DBusWatch *watch, void *data)
155 {
156         /* Because we just exit on OOM, enable/disable is
157          * no different from add/remove */
158         if (dbus_watch_get_enabled(watch))
159                 add_watch(watch, data);
160         else
161                 remove_watch(watch, data);
162 }
163
164 static gboolean timeout_handler_dispatch(gpointer data)
165 {
166         timeout_handler_t *handler = data;
167
168         /* if not enabled should not be polled by the main loop */
169         if (dbus_timeout_get_enabled(handler->timeout) != TRUE)
170                 return FALSE;
171
172         dbus_timeout_handle(handler->timeout);
173
174         return FALSE;
175 }
176
177 static void timeout_handler_free(void *data)
178 {
179         timeout_handler_t *handler = data;
180         if (!handler)
181                 return;
182
183         if (handler->id > 0) {
184                 g_source_remove(handler->id);
185                 handler->id = 0;
186         }
187
188         g_free(handler);
189 }
190
191 static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data)
192 {
193         timeout_handler_t *handler;
194
195         if (!dbus_timeout_get_enabled(timeout))
196                 return TRUE;
197
198         handler = g_new0(timeout_handler_t, 1);
199
200         handler->timeout = timeout;
201         handler->id = g_timeout_add(dbus_timeout_get_interval(timeout),
202                                         timeout_handler_dispatch, handler);
203
204         dbus_timeout_set_data(timeout, handler, timeout_handler_free);
205
206         return TRUE;
207 }
208
209 static void remove_timeout(DBusTimeout *timeout, void *data)
210 {
211         timeout_handler_t *handler;
212
213         handler = dbus_timeout_get_data(timeout);
214
215         if (!handler)
216                 return;
217
218         if (handler->id > 0) {
219                 g_source_remove(handler->id);
220                 handler->id = 0;
221         }
222 }
223
224 static void timeout_toggled(DBusTimeout *timeout, void *data)
225 {
226         if (dbus_timeout_get_enabled(timeout))
227                 add_timeout(timeout, data);
228         else
229                 remove_timeout(timeout, data);
230 }
231
232 static void dispatch_status_cb(DBusConnection *conn,
233                                 DBusDispatchStatus new_status, void *data)
234 {
235         if (!dbus_connection_get_is_connected(conn))
236                 return;
237
238         if (new_status == DBUS_DISPATCH_DATA_REMAINS)
239                 g_timeout_add(DISPATCH_TIMEOUT, message_dispatch_cb, data);
240 }
241
242 static void setup_dbus_with_main_loop(DBusConnection *conn)
243 {
244         dbus_connection_set_watch_functions(conn, add_watch, remove_watch,
245                                                 watch_toggled, conn, NULL);
246
247         dbus_connection_set_timeout_functions(conn, add_timeout, remove_timeout,
248                                                 timeout_toggled, conn, NULL);
249
250         dbus_connection_set_dispatch_status_function(conn, dispatch_status_cb,
251                                                                 conn, NULL);
252 }
253
254 DBusConnection *g_dbus_setup_bus(DBusBusType type, const char *name,
255                                                         DBusError *error)
256 {
257         DBusConnection *conn;
258
259         conn = dbus_bus_get(type, error);
260
261         if (error != NULL) {
262                 if (dbus_error_is_set(error) == TRUE)
263                         return NULL;
264         }
265
266         if (conn == NULL)
267                 return NULL;
268
269         if (name != NULL) {
270                 if (dbus_bus_request_name(conn, name,
271                                 DBUS_NAME_FLAG_DO_NOT_QUEUE, error) !=
272                                 DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER ) {
273                         dbus_connection_unref(conn);
274                         return NULL;
275                 }
276
277                 if (error != NULL) {
278                         if (dbus_error_is_set(error) == TRUE) {
279                                 dbus_connection_unref(conn);
280                                 return NULL;
281                         }
282                 }
283         }
284
285         setup_dbus_with_main_loop(conn);
286
287         if (dbus_connection_get_dispatch_status(conn) == DBUS_DISPATCH_DATA_REMAINS)
288                 g_timeout_add(DISPATCH_TIMEOUT, message_dispatch_cb, conn);
289
290         return conn;
291 }
292
293 gboolean g_dbus_request_name(DBusConnection *connection, const char *name,
294                                                         DBusError *error)
295 {
296         return TRUE;
297 }
298
299 gboolean g_dbus_set_disconnect_function(DBusConnection *connection,
300                                 GDBusWatchFunction function,
301                                 void *user_data, DBusFreeFunction destroy)
302 {
303         struct disconnect_data *dc_data;
304
305         dc_data = g_new(struct disconnect_data, 1);
306
307         dc_data->disconnect_cb = function;
308         dc_data->user_data = user_data;
309
310         dbus_connection_set_exit_on_disconnect(connection, FALSE);
311
312         if (g_dbus_add_signal_watch(connection, NULL, NULL,
313                                 DBUS_INTERFACE_LOCAL, "Disconnected",
314                                 disconnected_signal, dc_data,
315                                 g_free) == 0) {
316                 error("Can't add watch for D-Bus Disconnected signal\n");
317                 g_free(dc_data);
318                 return FALSE;
319         }
320
321         return TRUE;
322 }