1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
|
/*
*
* D-Bus helper library
*
* Copyright (C) 2004-2011 Marcel Holtmann <marcel@holtmann.org>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <glib.h>
#include <dbus/dbus.h>
#include "gdbus.h"
struct GDBusClient {
gint ref_count;
DBusConnection *dbus_conn;
char *service_name;
char *unique_name;
char *base_path;
char *match_rules[4];
DBusPendingCall *pending_call;
GDBusWatchFunction connect_func;
void *connect_data;
GDBusWatchFunction disconn_func;
void *disconn_data;
GDBusMessageFunction signal_func;
void *signal_data;
GDBusProxyFunction proxy_added;
GDBusProxyFunction proxy_removed;
void *proxy_data;
GList *proxy_list;
};
struct GDBusProxy {
gint ref_count;
GDBusClient *client;
char *obj_path;
char *interface;
};
static GDBusProxy *proxy_new(GDBusClient *client, const char *path,
const char *interface)
{
GDBusProxy *proxy;
proxy = g_try_new0(GDBusProxy, 1);
if (proxy == NULL)
return NULL;
proxy->client = client;
proxy->obj_path = g_strdup(path);
proxy->interface = g_strdup(interface);
return g_dbus_proxy_ref(proxy);
}
static void proxy_free(gpointer data)
{
GDBusProxy *proxy = data;
if (proxy->client) {
GDBusClient *client = proxy->client;
if (client->proxy_removed)
client->proxy_removed(proxy, client->proxy_data);
proxy->client = NULL;
}
g_dbus_proxy_unref(proxy);
}
static void proxy_remove(GDBusClient *client, const char *path,
const char *interface)
{
GList *list;
for (list = g_list_first(client->proxy_list); list;
list = g_list_next(list)) {
GDBusProxy *proxy = list->data;
if (g_str_equal(proxy->interface, interface) == TRUE &&
g_str_equal(proxy->obj_path, path) == TRUE) {
client->proxy_list =
g_list_delete_link(client->proxy_list, list);
proxy_free(proxy);
break;
}
}
}
GDBusProxy *g_dbus_proxy_ref(GDBusProxy *proxy)
{
if (proxy == NULL)
return NULL;
g_atomic_int_inc(&proxy->ref_count);
return proxy;
}
void g_dbus_proxy_unref(GDBusProxy *proxy)
{
if (proxy == NULL)
return;
if (g_atomic_int_dec_and_test(&proxy->ref_count) == FALSE)
return;
g_free(proxy->obj_path);
g_free(proxy->interface);
g_free(proxy);
}
const char *g_dbus_proxy_get_path(GDBusProxy *proxy)
{
if (proxy == NULL)
return NULL;
return proxy->obj_path;
}
const char *g_dbus_proxy_get_interface(GDBusProxy *proxy)
{
if (proxy == NULL)
return NULL;
return proxy->interface;
}
static void parse_properties(GDBusClient *client, const char *path,
const char *interface, DBusMessageIter *iter)
{
GDBusProxy *proxy;
if (g_str_equal(interface, DBUS_INTERFACE_INTROSPECTABLE) == TRUE)
return;
if (g_str_equal(interface, DBUS_INTERFACE_PROPERTIES) == TRUE)
return;
proxy = proxy_new(client, path, interface);
if (client->proxy_added)
client->proxy_added(proxy, client->proxy_data);
client->proxy_list = g_list_append(client->proxy_list, proxy);
}
static void parse_interfaces(GDBusClient *client, const char *path,
DBusMessageIter *iter)
{
DBusMessageIter dict;
if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
return;
dbus_message_iter_recurse(iter, &dict);
while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
DBusMessageIter entry, value;
const char *interface;
dbus_message_iter_recurse(&dict, &entry);
if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
break;
dbus_message_iter_get_basic(&entry, &interface);
dbus_message_iter_next(&entry);
dbus_message_iter_recurse(&entry, &value);
parse_properties(client, path, interface, &value);
dbus_message_iter_next(&dict);
}
}
static void interfaces_added(GDBusClient *client, DBusMessage *msg)
{
DBusMessageIter iter;
const char *path;
if (dbus_message_iter_init(msg, &iter) == FALSE)
return;
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_OBJECT_PATH)
return;
dbus_message_iter_get_basic(&iter, &path);
dbus_message_iter_next(&iter);
parse_interfaces(client, path, &iter);
}
static void interfaces_removed(GDBusClient *client, DBusMessage *msg)
{
DBusMessageIter iter, entry;
const char *path;
if (dbus_message_iter_init(msg, &iter) == FALSE)
return;
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_OBJECT_PATH)
return;
dbus_message_iter_get_basic(&iter, &path);
dbus_message_iter_next(&iter);
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY)
return;
dbus_message_iter_recurse(&iter, &entry);
while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
const char *interface;
dbus_message_iter_get_basic(&entry, &interface);
proxy_remove(client, path, interface);
dbus_message_iter_next(&entry);
}
}
static void parse_managed_objects(GDBusClient *client, DBusMessage *msg)
{
DBusMessageIter iter, dict;
if (dbus_message_iter_init(msg, &iter) == FALSE)
return;
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY)
return;
dbus_message_iter_recurse(&iter, &dict);
while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
DBusMessageIter entry;
const char *path;
dbus_message_iter_recurse(&dict, &entry);
if (dbus_message_iter_get_arg_type(&entry) !=
DBUS_TYPE_OBJECT_PATH)
break;
dbus_message_iter_get_basic(&entry, &path);
dbus_message_iter_next(&entry);
parse_interfaces(client, path, &entry);
dbus_message_iter_next(&dict);
}
}
static void get_managed_objects_reply(DBusPendingCall *call, void *user_data)
{
GDBusClient *client = user_data;
DBusMessage *reply = dbus_pending_call_steal_reply(call);
DBusError error;
dbus_error_init(&error);
if (dbus_set_error_from_message(&error, reply) == TRUE) {
dbus_error_free(&error);
goto done;
}
parse_managed_objects(client, reply);
done:
dbus_message_unref(reply);
dbus_pending_call_unref(client->pending_call);
client->pending_call = NULL;
}
static void get_managed_objects(GDBusClient *client)
{
DBusMessage *msg;
msg = dbus_message_new_method_call("org.bluez", "/",
DBUS_INTERFACE_DBUS ".ObjectManager",
"GetManagedObjects");
if (msg == NULL)
return;
dbus_message_append_args(msg, DBUS_TYPE_INVALID);
if (dbus_connection_send_with_reply(client->dbus_conn, msg,
&client->pending_call, -1) == FALSE) {
dbus_message_unref(msg);
return;
}
dbus_pending_call_set_notify(client->pending_call,
get_managed_objects_reply, client, NULL);
dbus_message_unref(msg);
}
static void modify_match_reply(DBusPendingCall *call, void *user_data)
{
DBusMessage *reply = dbus_pending_call_steal_reply(call);
DBusError error;
dbus_error_init(&error);
if (dbus_set_error_from_message(&error, reply) == TRUE)
dbus_error_free(&error);
dbus_message_unref(reply);
}
static gboolean modify_match(DBusConnection *conn, const char *member,
const char *rule)
{
DBusMessage *msg;
DBusPendingCall *call;
msg = dbus_message_new_method_call(DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
DBUS_INTERFACE_DBUS, member);
if (msg == NULL)
return FALSE;
dbus_message_append_args(msg, DBUS_TYPE_STRING, &rule,
DBUS_TYPE_INVALID);
if (dbus_connection_send_with_reply(conn, msg, &call, -1) == FALSE) {
dbus_message_unref(msg);
return FALSE;
}
dbus_pending_call_set_notify(call, modify_match_reply, NULL, NULL);
dbus_pending_call_unref(call);
dbus_message_unref(msg);
return TRUE;
}
static void get_name_owner_reply(DBusPendingCall *call, void *user_data)
{
GDBusClient *client = user_data;
DBusMessage *reply = dbus_pending_call_steal_reply(call);
DBusError error;
const char *name;
dbus_error_init(&error);
if (dbus_set_error_from_message(&error, reply) == TRUE) {
dbus_error_free(&error);
goto done;
}
if (dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &name,
DBUS_TYPE_INVALID) == FALSE)
goto done;
g_free(client->unique_name);
client->unique_name = g_strdup(name);
if (client->connect_func)
client->connect_func(client->dbus_conn, client->connect_data);
done:
dbus_message_unref(reply);
dbus_pending_call_unref(client->pending_call);
client->pending_call = NULL;
get_managed_objects(client);
}
static void get_name_owner(GDBusClient *client, const char *name)
{
DBusMessage *msg;
msg = dbus_message_new_method_call(DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
DBUS_INTERFACE_DBUS, "GetNameOwner");
if (msg == NULL)
return;
dbus_message_append_args(msg, DBUS_TYPE_STRING, &name,
DBUS_TYPE_INVALID);
if (dbus_connection_send_with_reply(client->dbus_conn, msg,
&client->pending_call, -1) == FALSE) {
dbus_message_unref(msg);
return;
}
dbus_pending_call_set_notify(client->pending_call,
get_name_owner_reply, client, NULL);
dbus_message_unref(msg);
}
static DBusHandlerResult message_filter(DBusConnection *connection,
DBusMessage *message, void *user_data)
{
GDBusClient *client = user_data;
const char *sender;
if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_SIGNAL)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
sender = dbus_message_get_sender(message);
if (g_str_equal(sender, DBUS_SERVICE_DBUS) == TRUE) {
const char *interface, *member;
const char *name, *old, *new;
interface = dbus_message_get_interface(message);
if (g_str_equal(interface, DBUS_INTERFACE_DBUS) == FALSE)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
member = dbus_message_get_member(message);
if (g_str_equal(member, "NameOwnerChanged") == FALSE)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
if (dbus_message_get_args(message, NULL,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_STRING, &old,
DBUS_TYPE_STRING, &new,
DBUS_TYPE_INVALID) == FALSE)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
if (g_str_equal(name, client->service_name) == FALSE)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
if (*new == '\0') {
if (client->disconn_func)
client->disconn_func(client->dbus_conn,
client->disconn_data);
g_free(client->unique_name);
client->unique_name = NULL;
} else if (*old == '\0') {
g_free(client->unique_name);
client->unique_name = g_strdup(new);
if (client->connect_func)
client->connect_func(client->dbus_conn,
client->connect_data);
get_managed_objects(client);
}
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
if (client->unique_name == NULL)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
if (g_str_equal(sender, client->unique_name) == TRUE) {
const char *path;
path = dbus_message_get_path(message);
if (g_str_equal(path, "/") == TRUE) {
const char *interface, *member;
interface = dbus_message_get_interface(message);
if (g_str_equal(interface, DBUS_INTERFACE_DBUS
".ObjectManager") == FALSE)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
member = dbus_message_get_member(message);
if (g_str_equal(member, "InterfacesAdded") == TRUE) {
interfaces_added(client, message);
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
if (g_str_equal(member, "InterfacesRemoved") == TRUE) {
interfaces_removed(client, message);
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
if (g_str_has_prefix(path, client->base_path) == FALSE)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
if (client->signal_func)
client->signal_func(client->dbus_conn,
message, client->signal_data);
}
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
GDBusClient *g_dbus_client_new(DBusConnection *connection,
const char *service, const char *path)
{
GDBusClient *client;
int i;
if (connection == NULL)
return NULL;
client = g_try_new0(GDBusClient, 1);
if (client == NULL)
return NULL;
if (dbus_connection_add_filter(connection, message_filter,
client, NULL) == FALSE) {
g_free(client);
return NULL;
}
client->dbus_conn = dbus_connection_ref(connection);
client->service_name = g_strdup(service);
client->base_path = g_strdup(path);
get_name_owner(client, client->service_name);
client->match_rules[0] = g_strdup_printf("type='signal',sender='%s',"
"path='%s',interface='%s',"
"member='NameOwnerChanged',arg0='%s'",
DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
DBUS_INTERFACE_DBUS, client->service_name);
client->match_rules[1] = g_strdup_printf("type='signal',sender='%s',"
"path='/',interface='%s.ObjectManager',"
"member='InterfacesAdded'",
client->service_name, DBUS_INTERFACE_DBUS);
client->match_rules[2] = g_strdup_printf("type='signal',sender='%s',"
"path='/',interface='%s.ObjectManager',"
"member='InterfacesRemoved'",
client->service_name, DBUS_INTERFACE_DBUS);
client->match_rules[3] = g_strdup_printf("type='signal',sender='%s',"
"path_namespace='%s'",
client->service_name, client->base_path);
for (i = 0; i < 4; i++)
modify_match(client->dbus_conn, "AddMatch",
client->match_rules[i]);
return g_dbus_client_ref(client);
}
GDBusClient *g_dbus_client_ref(GDBusClient *client)
{
if (client == NULL)
return NULL;
g_atomic_int_inc(&client->ref_count);
return client;
}
void g_dbus_client_unref(GDBusClient *client)
{
int i;
if (client == NULL)
return;
if (g_atomic_int_dec_and_test(&client->ref_count) == FALSE)
return;
if (client->pending_call != NULL) {
dbus_pending_call_cancel(client->pending_call);
dbus_pending_call_unref(client->pending_call);
}
for (i = 0; i < 4; i++) {
modify_match(client->dbus_conn, "RemoveMatch",
client->match_rules[i]);
g_free(client->match_rules[i]);
}
dbus_connection_remove_filter(client->dbus_conn,
message_filter, client);
g_list_free_full(client->proxy_list, proxy_free);
if (client->disconn_func)
client->disconn_func(client->dbus_conn, client->disconn_data);
dbus_connection_unref(client->dbus_conn);
g_free(client->service_name);
g_free(client->unique_name);
g_free(client->base_path);
g_free(client);
}
gboolean g_dbus_client_set_connect_watch(GDBusClient *client,
GDBusWatchFunction function, void *user_data)
{
if (client == NULL)
return FALSE;
client->connect_func = function;
client->connect_data = user_data;
return TRUE;
}
gboolean g_dbus_client_set_disconnect_watch(GDBusClient *client,
GDBusWatchFunction function, void *user_data)
{
if (client == NULL)
return FALSE;
client->disconn_func = function;
client->disconn_data = user_data;
return TRUE;
}
gboolean g_dbus_client_set_signal_watch(GDBusClient *client,
GDBusMessageFunction function, void *user_data)
{
if (client == NULL)
return FALSE;
client->signal_func = function;
client->signal_data = user_data;
return TRUE;
}
gboolean g_dbus_client_set_proxy_handlers(GDBusClient *client,
GDBusProxyFunction added,
GDBusProxyFunction removed, void *user_data)
{
if (client == NULL)
return FALSE;
client->proxy_added = added;
client->proxy_removed = removed;
client->proxy_data = user_data;
return TRUE;
}
|