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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
|
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- */
/* Libnul - Another Utility Library
* Copyright (C) 2008 Søren Sandmann
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <stdarg.h>
#include <dbus/dbus.h>
#include <string.h>
#include "libnul.h"
#define INTROSPECT_INTERFACE "org.freedesktop.DBus.Introspectable"
#define INTROSPECT_METHOD "Introspect"
struct nul_dbus_service_t
{
char *name;
nul_ptr_t *objects;
DBusConnection *connection;
gboolean running;
DBusBusType type;
};
struct nul_dbus_object_t
{
char *name;
nul_ptr_t data;
nul_ptr_t *interfaces;
};
struct nul_dbus_interface_t
{
char *name;
nul_ptr_t *members;
};
struct nul_dbus_member_t
{
char * name;
/* For now only methods are supported, although
* later we may also have properties and signals
*/
nul_dbus_interface_t * interface; /* containing interface */
nul_dbus_function_t function;
nul_ptr_t * parameters;
int n_inputs;
int n_outputs;
nul_fun_def_t * fun_def;
nul_fun_def_t * reply_fun;
};
struct nul_dbus_parameter_t
{
char * name;
nul_dbus_type_t * type;
gboolean out;
};
typedef enum
{
INT32,
UINT32,
STRING
} Primitive;
struct nul_dbus_type_t
{
Primitive type;
};
static void
process_watch (DBusWatch *watch, int flags)
{
dbus_watch_handle (watch, flags);
}
static void
on_hangup (gpointer data)
{
DBusWatch *watch = data;
process_watch (watch, DBUS_WATCH_HANGUP);
}
static void
on_error (gpointer data)
{
DBusWatch *watch = data;
process_watch (watch, DBUS_WATCH_ERROR);
}
static void
on_read (gpointer data)
{
GList *list = data;
for (list = data; list != NULL; list = list->next)
{
DBusWatch *watch = list->data;
if (dbus_watch_get_enabled (watch) &&
(dbus_watch_get_flags (watch) & DBUS_WATCH_READABLE))
{
process_watch (watch, DBUS_WATCH_READABLE);
}
}
}
static void
on_write (gpointer data)
{
GList *list = data;
for (list = data; list != NULL; list = list->next)
{
DBusWatch *watch = list->data;
if (dbus_watch_get_enabled (watch) &&
(dbus_watch_get_flags (watch) & DBUS_WATCH_WRITABLE))
{
process_watch (watch, DBUS_WATCH_WRITABLE);
}
}
}
static void
update_fd (int fd)
{
int enabled, flags;
GList *watches, *list;
watches = nul_fd_get_data (fd);
enabled = FALSE;
flags = 0;
for (list = watches; list != NULL; list = list->next)
{
DBusWatch *watch = list->data;
if (dbus_watch_get_enabled (watch))
{
enabled = TRUE;
flags |= dbus_watch_get_flags (watch);
}
}
if (enabled)
{
nul_fd_set_error_callback (fd, on_hangup);
nul_fd_set_hangup_callback (fd, on_error);
if (flags & DBUS_WATCH_READABLE)
nul_fd_set_read_callback (fd, on_read);
else
nul_fd_set_read_callback (fd, NULL);
if (flags & DBUS_WATCH_WRITABLE)
nul_fd_set_write_callback (fd, on_write);
else
nul_fd_set_write_callback (fd, NULL);
}
else
{
nul_fd_set_error_callback (fd, NULL);
nul_fd_set_hangup_callback (fd, NULL);
nul_fd_set_read_callback (fd, NULL);
nul_fd_set_write_callback (fd, NULL);
}
}
static dbus_bool_t
add_watch (DBusWatch *watch, void *data)
{
DBusConnection *connection = data;
int fd = dbus_watch_get_unix_fd (watch);
dbus_watch_set_data (watch, connection, NULL);
/* DBus apparently will create multiple watches for
* the same fd.
*/
if (nul_fd_is_watched (fd))
{
GList *watches = nul_fd_get_data (fd);
watches = g_list_prepend (watches, watch);
nul_fd_set_data (fd, watches);
}
else
{
nul_fd_add_watch (fd, g_list_prepend (NULL, watch));
}
update_fd (fd);
return TRUE;
}
static void
remove_watch (DBusWatch *watch, void *data)
{
int fd = dbus_watch_get_unix_fd (watch);
GList *watches = nul_fd_get_data (fd);
watches = g_list_remove (watches, watch);
if (!watches)
{
nul_fd_remove_watch (fd);
}
else
{
nul_fd_set_data (fd, watches);
update_fd (fd);
}
}
static void
toggle_watch (DBusWatch *watch, void *data)
{
update_fd (dbus_watch_get_unix_fd (watch));
}
static gboolean
on_timeout (gpointer data)
{
DBusTimeout *timeout = data;
dbus_timeout_handle (timeout);
return TRUE;
}
static dbus_bool_t
add_timeout (DBusTimeout *timeout, void *data)
{
dbus_timeout_set_data (timeout, GINT_TO_POINTER (0), NULL);
if (dbus_timeout_get_enabled (timeout))
{
int id;
id = g_timeout_add (dbus_timeout_get_interval (timeout),
on_timeout, timeout);
dbus_timeout_set_data (timeout, GINT_TO_POINTER (id), NULL);
}
return TRUE;
}
static void
remove_timeout (DBusTimeout *timeout, void *data)
{
int id = GPOINTER_TO_INT (dbus_timeout_get_data (timeout));
if (id)
{
g_source_remove (id);
dbus_timeout_set_data (timeout, GINT_TO_POINTER (0), NULL);
}
}
static void
toggle_timeout (DBusTimeout *timeout, void *data)
{
remove_timeout (timeout, NULL);
add_timeout (timeout, NULL);
}
static void
unregister_connection (DBusConnection *connection,
gpointer data)
{
/* nothing */
}
nul_ptr_t *free_us;
static int idle_handler;
static gboolean
do_free (gpointer data)
{
nul_ptr_t *p;
for (p = free_us; *p; ++p)
g_free (*p);
nul_ptr_array_free (free_us);
free_us = nul_ptr_array_new ();
idle_handler = 0;
return FALSE;
}
static gpointer
idle_free (gpointer p)
{
if (!free_us)
free_us = nul_ptr_array_new ();
free_us = nul_ptr_array_append (free_us, p);
if (!idle_handler)
idle_handler = g_idle_add (do_free, NULL);
return p;
}
static const char *
make_signature (nul_dbus_type_t *type)
{
switch (type->type)
{
case UINT32:
return "u";
case INT32:
return "i";
case STRING:
return "s";
}
return "";
}
static const char *
introspect (nul_dbus_object_t *object)
{
GString *xml = g_string_new (NULL);
nul_ptr_t *p;
g_string_append_printf (xml, "<node>\n");
for (p = object->interfaces; *p; ++p)
{
nul_dbus_interface_t *interface = *p;
nul_ptr_t *q;
g_string_append_printf (
xml, " <interface name=\"%s\">\n", interface->name);
for (q = interface->members; *q; ++q)
{
nul_dbus_member_t *member = *q;
nul_ptr_t *r;
g_string_append_printf (
xml, " <method name=\"%s\">\n", member->name);
for (r = member->parameters; *r; ++r)
{
nul_dbus_parameter_t *parameter = *r;
g_string_append_printf (
xml,
" <arg name=\"%s\" direction=\"%s\" type=\"%s\"/>\n",
parameter->name,
parameter->out? "out" : "in",
make_signature (parameter->type));
}
g_string_append_printf (
xml, " </method>\n");
}
g_string_append_printf (xml, " </interface>\n");
}
g_string_append_printf (xml, "</node>\n");
return idle_free (g_string_free (xml, FALSE));
}
static gboolean
decode_message (DBusMessage *message,
int n_parameters,
nul_dbus_parameter_t **parameters,
nul_arg_t *args)
{
gboolean has_args;
int i;
DBusMessageIter iter;
const char *error;
has_args = dbus_message_iter_init (message, &iter);
/* FIXME: this function should make sure args are initialized
* to NULL values when it fails
*/
for (i = 0; i < n_parameters; ++i)
{
int dbus_type;
if (!has_args)
{
error = "Received too few arguments method\n";
goto fail;
}
dbus_type = dbus_message_iter_get_arg_type (&iter);
switch (parameters[i]->type->type)
{
case UINT32:
if (dbus_type != DBUS_TYPE_UINT32)
{
error = "Received wrong type, should be UINT32";
goto fail;
}
dbus_message_iter_get_basic (&iter, &(args[i].v_uint32));
break;
case INT32:
if (dbus_type != DBUS_TYPE_INT32)
{
error = "Received wrong type, should be INT32";
goto fail;
}
dbus_message_iter_get_basic (&iter, &(args[i].v_int32));
break;
case STRING:
if (dbus_type != DBUS_TYPE_STRING)
{
error = "Received wrong type, should be STRING";
goto fail;
}
dbus_message_iter_get_basic (&iter, &(args[i].v_pointer));
break;
}
if (dbus_type == DBUS_TYPE_INVALID)
has_args = FALSE;
dbus_message_iter_next (&iter);
}
return TRUE;
fail:
g_print ("ERROR: %s\n", error);
return FALSE;
}
static void
encode_message (DBusMessage *message,
int n_parameters,
nul_dbus_parameter_t **parameters,
nul_arg_t *args)
{
DBusMessageIter iter;
int i;
dbus_message_iter_init_append (message, &iter);
for (i = 0; i < n_parameters; ++i)
{
nul_dbus_parameter_t *parameter = parameters[i];
switch (parameter->type->type)
{
case UINT32:
dbus_message_iter_append_basic (
&iter, DBUS_TYPE_UINT32, &(args[i].v_uint32));
break;
case INT32:
dbus_message_iter_append_basic (
&iter, DBUS_TYPE_INT32, &(args[i].v_int32));
break;
case STRING:
dbus_message_iter_append_basic (
&iter, DBUS_TYPE_STRING, &(args[i].v_pointer));
/* FIXME: it feels wrong that we are freeing data
* passed in by the user. Maybe we should just make the
* user idle_free it.
*
* Even better actually: Have a nul_dbus_return (...) that
* the user is expected to call.
*/
g_free (args[i].v_pointer);
break;
}
}
}
static DBusHandlerResult
invoke (DBusConnection *connection,
nul_dbus_object_t *object,
nul_dbus_member_t *member,
DBusMessage *message)
{
nul_arg_t *args = idle_free (
g_new0 (nul_arg_t, nul_ptr_array_len (member->parameters) + 1));
DBusMessage *error_reply;
nul_arg_t result;
int i;
if (!member->function)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
/* Add the data pointer */
args[0].v_pointer = object->data;
if (!decode_message (
message, member->n_inputs,
(nul_dbus_parameter_t **)member->parameters, &(args[1])))
{
error_reply = dbus_message_new_error (
message, DBUS_ERROR_FAILED, "FIXME: generate a real error");
dbus_connection_send (connection, error_reply, NULL);
return DBUS_HANDLER_RESULT_HANDLED;
}
for (i = member->n_inputs; member->parameters[i] != NULL; ++i)
{
nul_dbus_parameter_t *parameter = member->parameters[i];
g_assert (parameter->out);
args[i + 1].v_pointer = &(args[i + 1]);
}
result = nul_fun_def_invoke (member->fun_def, (nul_function_t)member->function, args);
if (result.v_int32)
{
DBusMessage *reply = dbus_message_new_method_return (message);
encode_message (reply,
member->n_outputs,
(nul_dbus_parameter_t **)&(member->parameters[member->n_inputs]),
&(args[member->n_inputs + 1]));
dbus_connection_send (connection, reply, NULL);
dbus_message_unref (reply);
return DBUS_HANDLER_RESULT_HANDLED;
}
else
{
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
}
static DBusHandlerResult
message_function (DBusConnection *connection,
DBusMessage *message,
gpointer data)
{
const char *msg_path = dbus_message_get_path (message);
const char *msg_interface = dbus_message_get_interface (message);
const char *msg_member = dbus_message_get_member (message);
nul_dbus_object_t *object = data;
nul_ptr_t *p;
g_assert (strcmp (object->name, msg_path) == 0);
/* Introspection */
if (strcmp (msg_interface, INTROSPECT_INTERFACE) == 0 &&
strcmp (msg_member, INTROSPECT_METHOD) == 0)
{
DBusMessage *reply = dbus_message_new_method_return (message);
const char *xml = introspect (object);
dbus_message_append_args (reply,
DBUS_TYPE_STRING, &xml,
DBUS_TYPE_INVALID);
dbus_connection_send (connection, reply, NULL);
return DBUS_HANDLER_RESULT_HANDLED;
}
/* Normal method */
for (p = object->interfaces; *p; ++p)
{
nul_dbus_interface_t *interface = *p;
if (strcmp (msg_interface, interface->name) == 0)
{
nul_ptr_t *q;
for (q = interface->members; *q; ++q)
{
nul_dbus_member_t *member = *q;
if (strcmp (member->name, msg_member) == 0)
return invoke (connection, object, member, message);
}
}
}
g_print ("got unknown message\n");
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
static const DBusObjectPathVTable vtable =
{
unregister_connection,
message_function,
NULL,
};
static nul_ptr_t *
make_ptr_array (gpointer first, va_list parameters)
{
nul_ptr_t *array = nul_ptr_array_new ();
gpointer v;
if (first)
{
array = nul_ptr_array_append (array, first);
v = va_arg (parameters, gpointer);
while (v)
{
array = nul_ptr_array_append (array, v);
v = va_arg (parameters, gpointer);
}
}
return array;
}
static dbus_int32_t idle_id_slot = -1;
static int
get_idle_id (DBusConnection *connection)
{
if (idle_id_slot == -1)
dbus_connection_allocate_data_slot (&idle_id_slot);
return (int)dbus_connection_get_data (connection, idle_id_slot);
}
static void
set_idle_id (DBusConnection *connection, int id)
{
if (idle_id_slot == -1)
dbus_connection_allocate_data_slot (&idle_id_slot);
dbus_connection_set_data (connection, idle_id_slot, (void *)id, NULL);
}
static gboolean
do_dispatch (gpointer data)
{
DBusConnection *connection = data;
while (dbus_connection_dispatch (connection) == DBUS_DISPATCH_DATA_REMAINS)
;
set_idle_id (connection, 0);
return FALSE;
}
static void
on_dispatch_status_changed (DBusConnection *connection,
DBusDispatchStatus new_status,
void *data)
{
if (!get_idle_id (connection))
set_idle_id (connection, g_idle_add (do_dispatch, connection));
}
static DBusConnection *
ensure_connection (DBusConnection **connection,
DBusBusType type,
DBusError *err)
{
if (!*connection)
{
*connection = dbus_bus_get (type, err);
if (*connection)
{
dbus_connection_set_dispatch_status_function (
*connection,
on_dispatch_status_changed, *connection, NULL);
dbus_connection_set_watch_functions (
*connection,
add_watch, remove_watch, toggle_watch,
*connection, NULL);
dbus_connection_set_timeout_functions (
*connection,
add_timeout, remove_timeout, toggle_timeout,
*connection, NULL);
}
do_dispatch (*connection);
}
return *connection;
}
static DBusConnection *
get_connection (DBusBusType type, DBusError *err)
{
static DBusConnection *session_bus;
static DBusConnection *system_bus;
if (type == DBUS_BUS_SESSION)
return ensure_connection (&session_bus, DBUS_BUS_SESSION, err);
else if (type == DBUS_BUS_SYSTEM)
return ensure_connection (&system_bus, DBUS_BUS_SYSTEM, err);
else
return NULL;
}
static nul_dbus_service_t *
make_service (const char *name,
DBusBusType type,
nul_dbus_object_t *object1,
va_list args)
{
nul_dbus_service_t *service = g_new0 (nul_dbus_service_t, 1);
service->name = g_strdup (name);
service->objects = make_ptr_array (object1, args);
service->type = type;
service->connection = get_connection (type, NULL);
if (!service->connection)
{
/* FIXME */
return NULL;
}
return service;
}
nul_dbus_service_t *
nul_dbus_session_service (const char *name,
nul_dbus_object_t *object1,
...)
{
va_list args;
nul_dbus_service_t *service;
va_start (args, object1);
service = make_service (name, DBUS_BUS_SESSION, object1, args);
va_end (args);
return service;
}
void
nul_dbus_service_add_object (nul_dbus_service_t *service,
nul_dbus_object_t *object)
{
service->objects = nul_ptr_array_append (service->objects, object);
dbus_connection_register_object_path (
service->connection, object->name, &vtable, object);
}
gboolean
nul_dbus_service_start (nul_dbus_service_t *service)
{
DBusError err = DBUS_ERROR_INIT;
int i;
int result;
if (service->running)
return FALSE;
/* Possible flags:
*
* DBUS_NAME_FLAG_ALLOW_REPLACEMENT
* DBUS_NAME_FLAG_REPLACE_EXISTING
* DBUS_FLAG_DO_NOT_QUEUE
*/
result = dbus_bus_request_name (service->connection,
service->name,
DBUS_NAME_FLAG_DO_NOT_QUEUE,
&err);
if (result == -1)
{
/* FIXME: handle error */
return FALSE;
}
switch (result)
{
case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
g_assert_not_reached();
break;
case DBUS_REQUEST_NAME_REPLY_EXISTS:
g_print ("Couldn't get name\n");
return FALSE;
break;
case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
g_print ("We own this already (?)\n");
return FALSE;
break;
case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
break;
}
/* Register objects */
for (i = 0; service->objects[i] != NULL; ++i)
{
nul_dbus_object_t *object = service->objects[i];
dbus_connection_register_object_path (
service->connection, object->name, &vtable, object);
}
service->running = TRUE;
return TRUE;
}
void
nul_dbus_service_stop (nul_dbus_service_t *service)
{
nul_ptr_t *p;
if (!service->running)
return;
for (p = service->objects; *p != NULL; ++p)
{
nul_dbus_object_t *object = *p;
dbus_connection_unregister_object_path (
service->connection, object->name);
}
dbus_bus_release_name (service->connection, service->name, NULL);
service->running = FALSE;
}
nul_dbus_object_t *
nul_dbus_object (const char *name,
gpointer data,
nul_dbus_interface_t *interface1,
...)
{
nul_dbus_object_t *object = g_new0 (nul_dbus_object_t, 1);
va_list args;
object->name = g_strdup (name);
object->data = data;
va_start (args, interface1);
object->interfaces = make_ptr_array (interface1, args);
va_end (args);
/* Add introspection interface */
/* Note: the actual introspection is special cased in invoke(), but we
* still add it here so that introspection can treat the introspection
* interface as any other interface
*/
object->interfaces = nul_ptr_array_append (
object->interfaces, nul_dbus_interface (
"org.freedesktop.DBus.Introspectable",
nul_dbus_method (
"Introspect",
(nul_dbus_function_t)NULL,
nul_dbus_parameter_out (
"data",
nul_dbus_type_string()),
NULL),
NULL));
return object;
}
nul_dbus_interface_t *
nul_dbus_interface (const char *name,
nul_dbus_member_t *member1,
...)
{
nul_dbus_interface_t *interface = g_new0 (nul_dbus_interface_t, 1);
va_list parameters;
nul_ptr_t *p;
interface->name = g_strdup (name);
va_start (parameters, member1);
interface->members = make_ptr_array (member1, parameters);
va_end (parameters);
for (p = interface->members; *p; ++p)
{
nul_dbus_member_t *member = *p;
member->interface = interface;
}
return interface;
}
static nul_dbus_type_t *
type_copy (const nul_dbus_type_t *type)
{
nul_dbus_type_t *copy = g_new0 (nul_dbus_type_t, 1);
copy->type = type->type;
return copy;
}
static nul_type_t
invoke_type_from_nul_dbus_type (nul_dbus_type_t *t)
{
switch (t->type)
{
case INT32:
return NUL_TYPE_INT32;
case UINT32:
return NUL_TYPE_UINT32;
case STRING:
return NUL_TYPE_POINTER;
}
g_warning ("Unknown type %d\n", t->type);
return -1;
}
static nul_fun_def_t *
make_fun_def (nul_dbus_member_t *member)
{
nul_type_t *types;
int i;
types = idle_free (g_new0 (nul_type_t,
nul_ptr_array_len (member->parameters) + 1));
types[0] = NUL_TYPE_POINTER; /* For object->data */
for (i = 1; i < nul_ptr_array_len (member->parameters); ++i)
{
nul_dbus_parameter_t *par = member->parameters[i - 1];
if (par->out)
types[i] = NUL_TYPE_POINTER;
else
types[i] = invoke_type_from_nul_dbus_type (par->type);
}
return nul_fun_def_new (NUL_TYPE_INT,
nul_ptr_array_len (member->parameters) + 1,
types);
}
static nul_fun_def_t *
make_reply_fun (nul_dbus_member_t *member)
{
nul_type_t *types;
nul_ptr_t *p;
int n;
types = idle_free (g_new0 (nul_type_t,
nul_ptr_array_len (member->parameters) + 2));
n = 0;
for (p = member->parameters; *p; ++p)
{
nul_dbus_parameter_t *par = *p;
g_assert (*p != NULL);
g_assert (par != NULL);
if (par->out)
types[n++] = invoke_type_from_nul_dbus_type (par->type);
}
types[n++] = NUL_TYPE_POINTER; /* data */
types[n++] = NUL_TYPE_POINTER; /* const GError *err */
return nul_fun_def_new (NUL_TYPE_VOID, n, types);
}
nul_dbus_member_t *
nul_dbus_method (const char *name,
nul_dbus_function_t function,
nul_dbus_parameter_t *parameter1,
...)
{
nul_dbus_member_t *member = g_new0 (nul_dbus_member_t, 1);
va_list parameters;
nul_ptr_t *p;
member->name = g_strdup (name);
member->function = function;
va_start (parameters, parameter1);
member->parameters = make_ptr_array ((nul_dbus_type_t *)parameter1, parameters);
va_end (parameters);
member->n_inputs = 0;
member->n_outputs = 0;
for (p = member->parameters; *p; ++p)
{
nul_dbus_parameter_t *par = *p;
g_assert (*p != NULL);
g_assert (par != NULL);
if (par->out)
{
member->n_outputs++;
}
else
{
member->n_inputs++;
if (member->n_outputs)
{
g_critical ("All output parameters must be specified after the input parameters");
return NULL;
}
}
}
member->fun_def = make_fun_def (member);
member->reply_fun = make_reply_fun (member);
return member;
}
static nul_dbus_parameter_t *
make_parameter (const char *name,
const nul_dbus_type_t *type,
gboolean out)
{
nul_dbus_parameter_t *parameter = g_new0 (nul_dbus_parameter_t, 1);
parameter->name = g_strdup (name);
parameter->type = type_copy (type);
parameter->out = out;
return parameter;
}
nul_dbus_parameter_t *
nul_dbus_parameter_in (const char *name,
const nul_dbus_type_t *type)
{
return make_parameter (name, type, FALSE);
}
nul_dbus_parameter_t *
nul_dbus_parameter_out (const char *name,
const nul_dbus_type_t *type)
{
return make_parameter (name, type, TRUE);
}
static nul_dbus_type_t *
make_type (nul_type_t t)
{
nul_dbus_type_t *type = idle_free (g_new0 (nul_dbus_type_t, 1));
type->type = t;
return type;
}
const nul_dbus_type_t *
nul_dbus_type_int32 (void)
{
return make_type (INT32);
}
const nul_dbus_type_t *
nul_dbus_type_uint32 (void)
{
return make_type (UINT32);
}
const nul_dbus_type_t *
nul_dbus_type_string (void)
{
return make_type (STRING);
}
typedef struct
{
nul_dbus_function_t callback;
gpointer data;
nul_dbus_member_t *method;
} InvokeInfo;
static void
on_reply (DBusPendingCall *pending,
gpointer data)
{
InvokeInfo *info = data;
int n_outputs = info->method->n_outputs;
nul_arg_t *args = idle_free (g_new0 (nul_arg_t, n_outputs + 2));
DBusMessage *reply;
if (!info->callback)
return;
reply = dbus_pending_call_steal_reply (pending);
if (!reply)
{
/* FIXME: can this happen? If it can, we should
* generate an error.
*/
return;
}
args[n_outputs].v_pointer = NULL; /* GError */
if (!decode_message (reply,
n_outputs,
(nul_dbus_parameter_t **)&(info->method->parameters[info->method->n_inputs]),
args))
{
/* FIXME: generate an error into args[n_outputs + 1] */
args[n_outputs].v_pointer = "ERR";
}
args[n_outputs + 1].v_pointer = info->data;
nul_fun_def_invoke (info->method->reply_fun, (nul_function_t)info->callback, args);
}
/* Decodes strings like this:
*
* /org.gnome.ScreenSaver.Throttle
*
*/
static gboolean
decode_method_desc (const char *method_desc,
char **object_path,
char **interface,
char **method)
{
char *m;
g_return_val_if_fail (method_desc != NULL, FALSE);
g_return_val_if_fail (strchr (method_desc, '/') != NULL, FALSE);
g_return_val_if_fail (strchr (method_desc, '.') != NULL, FALSE);
m = strrchr (method_desc, '/');
*object_path = g_strndup (method_desc, m - method_desc);
method_desc = m + 1;
m = strrchr (method_desc, '.');
*interface = g_strndup (method_desc, m - method_desc);
method_desc = m;
*method = g_strdup (m + 1);
return TRUE;
}
static nul_dbus_object_t *
find_object (nul_dbus_service_t *service, const char *name)
{
nul_ptr_t *p;
for (p = service->objects; *p != NULL; ++p)
{
nul_dbus_object_t *object = *p;
if (strcmp (object->name, name) == 0)
return object;
}
return NULL;
}
static nul_dbus_interface_t *
find_interface (nul_dbus_object_t *object, const char *name)
{
nul_ptr_t *p;
for (p = object->interfaces; *p; ++p)
{
nul_dbus_interface_t *interface = *p;
if (strcmp (interface->name, name) == 0)
return interface;
}
return NULL;
}
static nul_dbus_member_t *
find_member (nul_dbus_interface_t *interface, const char *name)
{
nul_ptr_t *p;
for (p = interface->members; *p; ++p)
{
nul_dbus_member_t *member = *p;
if (strcmp (member->name, name) == 0)
return member;
}
return NULL;
}
void
nul_dbus_invoke (nul_dbus_service_t *service,
const char *method_desc,
nul_dbus_function_t callback,
gpointer data,
...)
{
char *object_str, *interface_str, *method_str;
nul_dbus_object_t *object;
nul_dbus_interface_t *interface;
nul_dbus_member_t *method;
DBusMessage *message;
DBusMessageIter iter;
DBusConnection *connection;
va_list args;
InvokeInfo *info;
DBusPendingCall *pending;
nul_ptr_t *p;
g_return_if_fail (service != NULL);
if (!decode_method_desc (method_desc, &object_str, &interface_str, &method_str))
return;
object = find_object (service, object_str);
if (!object)
{
g_critical ("Object '%s' not found", object_str);
return;
}
interface = find_interface (object, interface_str);
if (!interface)
{
g_critical ("Interface '%s' not found", interface_str);
return;
}
method = find_member (interface, method_str);
if (!method)
{
g_critical ("Method '%s' not found", method_str);
return;
}
message = dbus_message_new_method_call (
service->name, object->name, interface->name, method->name);
dbus_message_iter_init_append (message, &iter);
va_start (args, data);
for (p = method->parameters; *p; ++p)
{
nul_dbus_parameter_t *parameter = *p;
int32_t i32;
uint32_t u32;
const char *s;
if (parameter->out)
continue;
switch (parameter->type->type)
{
case INT32:
i32 = va_arg (args, int32_t);
dbus_message_iter_append_basic (&iter, DBUS_TYPE_INT32, &i32);
break;
case UINT32:
u32 = va_arg (args, uint32_t);
dbus_message_iter_append_basic (&iter, DBUS_TYPE_UINT32, &u32);
break;
case STRING:
s = va_arg (args, const char *);
dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &s);
break;
}
}
va_end (args);
connection = get_connection (service->type, NULL);
dbus_connection_send_with_reply (connection, message, &pending, -1);
if (!pending)
{
/* FIXME: generate an error and call the callback */
/* Or alternatively, we should deal with getting disconnected,
* though it's not clear what we should do with that
*/
}
else
{
info = g_new0 (InvokeInfo, 1);
info->callback = callback;
info->data = data;
info->method = method;
dbus_pending_call_set_notify (pending, on_reply, info, g_free);
}
dbus_message_unref (message);
}
|