gdbus: add and use helpers for table declarations
[framework/connectivity/connman.git] / gdbus / gdbus.h
1 /*
2  *
3  *  D-Bus helper library
4  *
5  *  Copyright (C) 2004-2011  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 #ifndef __GDBUS_H
25 #define __GDBUS_H
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 #include <dbus/dbus.h>
32 #include <glib.h>
33
34 typedef void (* GDBusWatchFunction) (DBusConnection *connection,
35                                                         void *user_data);
36
37 typedef gboolean (* GDBusSignalFunction) (DBusConnection *connection,
38                                         DBusMessage *message, void *user_data);
39
40 DBusConnection *g_dbus_setup_bus(DBusBusType type, const char *name,
41                                                         DBusError *error);
42
43 DBusConnection *g_dbus_setup_private(DBusBusType type, const char *name,
44                                                         DBusError *error);
45
46 gboolean g_dbus_request_name(DBusConnection *connection, const char *name,
47                                                         DBusError *error);
48
49 gboolean g_dbus_set_disconnect_function(DBusConnection *connection,
50                                 GDBusWatchFunction function,
51                                 void *user_data, DBusFreeFunction destroy);
52
53 typedef void (* GDBusDestroyFunction) (void *user_data);
54
55 typedef DBusMessage * (* GDBusMethodFunction) (DBusConnection *connection,
56                                         DBusMessage *message, void *user_data);
57
58 typedef guint32 GDBusPendingReply;
59
60 typedef void (* GDBusSecurityFunction) (DBusConnection *connection,
61                                                 const char *action,
62                                                 gboolean interaction,
63                                                 GDBusPendingReply pending);
64
65 typedef enum {
66         G_DBUS_METHOD_FLAG_DEPRECATED = (1 << 0),
67         G_DBUS_METHOD_FLAG_NOREPLY    = (1 << 1),
68         G_DBUS_METHOD_FLAG_ASYNC      = (1 << 2),
69 } GDBusMethodFlags;
70
71 typedef enum {
72         G_DBUS_SIGNAL_FLAG_DEPRECATED = (1 << 0),
73 } GDBusSignalFlags;
74
75 typedef enum {
76         G_DBUS_PROPERTY_FLAG_DEPRECATED = (1 << 0),
77 } GDBusPropertyFlags;
78
79 typedef enum {
80         G_DBUS_SECURITY_FLAG_DEPRECATED        = (1 << 0),
81         G_DBUS_SECURITY_FLAG_BUILTIN           = (1 << 1),
82         G_DBUS_SECURITY_FLAG_ALLOW_INTERACTION = (1 << 2),
83 } GDBusSecurityFlags;
84
85 typedef struct {
86         const char *name;
87         const char *signature;
88 } GDBusArgInfo;
89
90 typedef struct {
91         const char *name;
92         const char *signature;
93         const char *reply;
94         GDBusMethodFunction function;
95         GDBusMethodFlags flags;
96         unsigned int privilege;
97         const GDBusArgInfo *in_args;
98         const GDBusArgInfo *out_args;
99 } GDBusMethodTable;
100
101 typedef struct {
102         const char *name;
103         const char *signature;
104         GDBusSignalFlags flags;
105         const GDBusArgInfo *args;
106 } GDBusSignalTable;
107
108 typedef struct {
109         const char *name;
110         const char *type;
111         GDBusPropertyFlags flags;
112 } GDBusPropertyTable;
113
114 typedef struct {
115         unsigned int privilege;
116         const char *action;
117         GDBusSecurityFlags flags;
118         GDBusSecurityFunction function;
119 } GDBusSecurityTable;
120
121 #define GDBUS_ARGS(args...) (const GDBusArgInfo[]) { args, { } }
122
123 #define _GDBUS_METHOD(_name, _signature, _reply, _in_args, _out_args, _function) \
124         .name = _name, \
125         .signature = _signature, \
126         .reply = _reply, \
127         .in_args = _in_args, \
128         .out_args = _out_args, \
129         .function = _function
130
131 #define _GDBUS_ASYNC_METHOD(_name, _signature, _reply, _in_args, _out_args, _function) \
132         .name = _name, \
133         .signature = _signature, \
134         .reply = _reply, \
135         .in_args = _in_args, \
136         .out_args = _out_args, \
137         .function = _function, \
138         .flags = G_DBUS_METHOD_FLAG_ASYNC
139
140 #define _GDBUS_DEPRECATED_METHOD(_name, _signature, _reply, _in_args, _out_args, _function) \
141         .name = _name, \
142         .signature = _signature, \
143         .reply = _reply, \
144         .in_args = _in_args, \
145         .out_args = _out_args, \
146         .function = _function, \
147         .flags = G_DBUS_METHOD_FLAG_DEPRECATED
148
149 #define _GDBUS_DEPRECATED_ASYNC_METHOD(_name, _signature, _reply, _in_args, _out_args, _function) \
150         .name = _name, \
151         .signature = _signature, \
152         .reply = _reply, \
153         .in_args = _in_args, \
154         .out_args = _out_args, \
155         .function = _function, \
156         .flags = G_DBUS_METHOD_FLAG_ASYNC | G_DBUS_METHOD_FLAG_DEPRECATED
157
158 #define _GDBUS_SIGNAL(_name, _signature, _args) \
159         .name = _name, \
160         .signature = _signature, \
161         .args = _args
162
163 #define _GDBUS_DEPRECATED_SIGNAL(_name, _signature, _args) \
164         .name = _name, \
165         .signature = _signature, \
166         .args = _args, \
167         .flags = G_DBUS_SIGNAL_FLAG_DEPRECATED
168
169 /* Helpers with no signature and reply */
170
171 #define GDBUS_METHOD(_name, _in_args, _out_args, _function) \
172         .name = _name, \
173         .in_args = _in_args, \
174         .out_args = _out_args, \
175         .function = _function
176
177 #define GDBUS_ASYNC_METHOD(_name, _in_args, _out_args, _function) \
178         .name = _name, \
179         .in_args = _in_args, \
180         .out_args = _out_args, \
181         .function = _function, \
182         .flags = G_DBUS_METHOD_FLAG_ASYNC
183
184 #define GDBUS_DEPRECATED_METHOD(_name, _in_args, _out_args, _function) \
185         .name = _name, \
186         .in_args = _in_args, \
187         .out_args = _out_args, \
188         .function = _function, \
189         .flags = G_DBUS_METHOD_FLAG_DEPRECATED
190
191 #define GDBUS_DEPRECATED_ASYNC_METHOD(_name, _in_args, _out_args, _function) \
192         .name = _name, \
193         .in_args = _in_args, \
194         .out_args = _out_args, \
195         .function = _function, \
196         .flags = G_DBUS_METHOD_FLAG_ASYNC | G_DBUS_METHOD_FLAG_DEPRECATED
197
198 #define GDBUS_SIGNAL(_name, _args) \
199         .name = _name, \
200         .args = _args
201
202 #define GDBUS_DEPRECATED_SIGNAL(_name, _args) \
203         .name = _name, \
204         .args = _args, \
205         .flags = G_DBUS_SIGNAL_FLAG_DEPRECATED
206
207 gboolean g_dbus_register_interface(DBusConnection *connection,
208                                         const char *path, const char *name,
209                                         const GDBusMethodTable *methods,
210                                         const GDBusSignalTable *signals,
211                                         const GDBusPropertyTable *properties,
212                                         void *user_data,
213                                         GDBusDestroyFunction destroy);
214 gboolean g_dbus_unregister_interface(DBusConnection *connection,
215                                         const char *path, const char *name);
216
217 gboolean g_dbus_register_security(const GDBusSecurityTable *security);
218 gboolean g_dbus_unregister_security(const GDBusSecurityTable *security);
219
220 void g_dbus_pending_success(DBusConnection *connection,
221                                         GDBusPendingReply pending);
222 void g_dbus_pending_error(DBusConnection *connection,
223                                 GDBusPendingReply pending,
224                                 const char *name, const char *format, ...)
225                                         __attribute__((format(printf, 4, 5)));
226 void g_dbus_pending_error_valist(DBusConnection *connection,
227                                 GDBusPendingReply pending, const char *name,
228                                         const char *format, va_list args);
229
230 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
231                                                 const char *format, ...)
232                                         __attribute__((format(printf, 3, 4)));
233 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
234                                         const char *format, va_list args);
235 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...);
236 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
237                                                 int type, va_list args);
238
239 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message);
240 gboolean g_dbus_send_reply(DBusConnection *connection,
241                                 DBusMessage *message, int type, ...);
242 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
243                                 DBusMessage *message, int type, va_list args);
244
245 gboolean g_dbus_emit_signal(DBusConnection *connection,
246                                 const char *path, const char *interface,
247                                 const char *name, int type, ...);
248 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
249                                 const char *path, const char *interface,
250                                 const char *name, int type, va_list args);
251
252 guint g_dbus_add_service_watch(DBusConnection *connection, const char *name,
253                                 GDBusWatchFunction connect,
254                                 GDBusWatchFunction disconnect,
255                                 void *user_data, GDBusDestroyFunction destroy);
256 guint g_dbus_add_disconnect_watch(DBusConnection *connection, const char *name,
257                                 GDBusWatchFunction function,
258                                 void *user_data, GDBusDestroyFunction destroy);
259 guint g_dbus_add_signal_watch(DBusConnection *connection,
260                                 const char *sender, const char *path,
261                                 const char *interface, const char *member,
262                                 GDBusSignalFunction function, void *user_data,
263                                 GDBusDestroyFunction destroy);
264 gboolean g_dbus_remove_watch(DBusConnection *connection, guint tag);
265 void g_dbus_remove_all_watches(DBusConnection *connection);
266
267 #ifdef __cplusplus
268 }
269 #endif
270
271 #endif /* __GDBUS_H */