File: dbus-proxy.c

package info (click to toggle)
iwd 3.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,112 kB
  • sloc: ansic: 140,705; sh: 4,960; makefile: 714
file content (918 lines) | stat: -rw-r--r-- 20,647 bytes parent folder | download | duplicates (2)
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
/*
 *
 *  Wireless daemon for Linux
 *
 *  Copyright (C) 2017-2020  Intel Corporation. All rights reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; 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 <stdio.h>
#include <ell/ell.h>

#include "client/agent-manager.h"
#include "client/dbus-proxy.h"
#include "client/display.h"
#include "client/command.h"
#include "client/properties.h"

#define IWD_SERVICE		"net.connman.iwd"
#define IWD_ROOT_PATH		"/"

struct proxy_interface {
	void *data;
	char *path;
	const struct proxy_interface_type *type;
};

static struct l_dbus *dbus;

static struct l_queue *proxy_interfaces;
static struct l_queue *proxy_interface_types;

void proxy_properties_display_inline(const struct proxy_interface *proxy,
					const char *margin,
					unsigned int name_column_width,
					unsigned int value_column_width)
{
	const void *data;
	const struct proxy_interface_property *properties;
	size_t i;

	if (!proxy->type->properties)
		return;

	data = proxy_interface_get_data(proxy);
	properties = proxy->type->properties;

	for (i = 0; properties[i].name; i++) {
		const char *str;

		if (!properties[i].tostr)
			continue;

		str = properties[i].tostr(data);

		display_table_row(MARGIN, 3, 8, properties[i].is_read_write ?
				COLOR_BOLDGRAY("       *") : "",
				name_column_width, properties[i].name,
				value_column_width, str ? : "");
	}
}

void proxy_properties_display_header(const char *caption, const char *margin,
					unsigned int name_column_width,
					unsigned int value_column_width)
{
	display_table_header(caption, "%s%-*s  %-*s  %-*s", margin,
				8, "Settable",
				name_column_width, "Property",
				value_column_width, "Value");
}

void proxy_properties_display(const struct proxy_interface *proxy,
				const char *caption, const char *margin,
				unsigned int name_column_width,
				unsigned int value_column_width)
{
	if (!proxy->type->properties)
		return;

	proxy_properties_display_header(caption, margin, name_column_width,
					value_column_width);

	proxy_properties_display_inline(proxy, margin, name_column_width,
					value_column_width);
}

static const void *proxy_interface_property_tostr(
					const struct proxy_interface *proxy,
					const char *name)
{
	size_t i;
	const struct proxy_interface_property *property_table =
							proxy->type->properties;

	for (i = 0; property_table[i].name; i++) {
		if (strcmp(property_table[i].name, name))
			continue;

		if (!property_table[i].tostr)
			break;

		return property_table[i].tostr(proxy->data);
	}

	return NULL;
}

static void proxy_interface_property_update(struct proxy_interface *proxy,
					const char *name,
					struct l_dbus_message_iter *variant)
{
	size_t i;
	const struct proxy_interface_property *property_table =
							proxy->type->properties;

	if (!property_table)
		return;

	for (i = 0; property_table[i].name; i++) {
		if (strcmp(property_table[i].name, name))
			continue;

		if (!property_table[i].update)
			return;

		property_table[i].update(proxy->data, variant);

		return;
	}

	l_debug("Unknown property name: %s for interface %s", name,
							proxy->type->interface);
}

static void interface_update_properties(struct proxy_interface *proxy,
					struct l_dbus_message_iter *changed,
					struct l_dbus_message_iter *invalidated)
{
	const char *name;
	struct l_dbus_message_iter variant;

	while (l_dbus_message_iter_next_entry(changed, &name, &variant))
		proxy_interface_property_update(proxy, name, &variant);

	if (!invalidated)
		return;

	while (l_dbus_message_iter_next_entry(invalidated, &name))
		proxy_interface_property_update(proxy, name, NULL);
}

static char *strdup_quoted_for_spaces(const char *str)
{
	const char *p;

	for (p = str; *p; p++) {
		if (*p != ' ')
			continue;

		return l_strdup_printf("\"%s\"", str);
	}

	return l_strdup(str);
}

char *proxy_property_str_completion(const struct proxy_interface_type *type,
					proxy_property_match_func_t function,
					const char *property_name,
					const void *value, int state,
					const char *extra_interface)
{
	static struct l_queue *match;
	static const struct l_queue_entry *entry;

	if (!state) {
		match = proxy_interface_find_all(type->interface, function,
									value);
		if (!match)
			return NULL;

		entry = l_queue_get_entries(match);
	}

	while (entry) {
		const struct proxy_interface *proxy = entry->data;
		const char *str;

		entry = entry->next;

		if (extra_interface) {
			const char *path = proxy_interface_get_path(proxy);
			const struct proxy_interface *extra =
				proxy_interface_find(extra_interface, path);

			if (!extra)
				continue;
		}

		str = proxy_interface_property_tostr(proxy, property_name);
		if (!str)
			goto done;

		return strdup_quoted_for_spaces(str);
	}
done:
	l_queue_destroy(match, NULL);
	match = NULL;
	entry = NULL;

	return NULL;
}

static char *proxy_property_completion_value_options(const char **options,
							const char *text,
							int state)
{
	static int index;
	static int len;
	const char *opt;

	if (!state) {
		index = 0;
		len = strlen(text);
	}

	while ((opt = options[index++])) {
		if (strncmp(opt, text, len))
			continue;

		return l_strdup(opt);
	}

	return NULL;
}

char *proxy_property_completion(
			const struct proxy_interface_property *properties,
			const char *text, int state)
{
	static size_t i;
	static size_t j;
	static size_t len;
	static bool first_pass;
	const char *name;

	if (!state) {
		j = 0;
		first_pass = true;
	}

	while (first_pass && (name = properties[j].name)) {
		if (!properties[j].is_read_write)
			goto next;

		if (!command_line_find_token(name, 2))
			goto next;

		if (!properties[j].options)
			goto next;

		return proxy_property_completion_value_options(
					properties[j].options, text,
					state);
next:
		j++;
	}

	if (first_pass) {
		i = 0;
		first_pass = false;
		len = strlen(text);
	}

	while ((name = properties[i].name)) {
		if (!properties[i++].is_read_write)
			continue;

		if (strncmp(name, text, len))
			continue;

		return l_strdup(name);
	}

	return NULL;
}

static const struct proxy_interface_property *proxy_property_find(
				const struct proxy_interface_property *types,
				const char *name)
{
	size_t i;

	for (i = 0; types[i].name; i++) {
		if (strcmp(types[i].name, name))
			continue;

		return &types[i];
	}

	return NULL;
}

struct proxy_callback_data {
	l_dbus_message_func_t callback;
	void *user_data;
};

static void proxy_callback(struct l_dbus_message *message, void *user_data)
{
	struct proxy_callback_data *callback_data = user_data;
	const struct proxy_interface *proxy;
	const char *name;
	const char *text;

	if (callback_data->callback)
		callback_data->callback(message, callback_data->user_data);

	if (command_is_interactive_mode())
		return;

	if (l_dbus_message_get_error(message, &name, &text)) {
		command_set_exit_status(EXIT_FAILURE);
		goto quit;
	}

	proxy = callback_data->user_data;
	if (!strcmp(proxy->type->interface, IWD_AGENT_MANAGER_INTERFACE) ||
			!strcmp(proxy->type->interface, IWD_DAEMON_INTERFACE))
		return;

quit:
	l_main_quit();
}

bool proxy_property_set(const struct proxy_interface *proxy, const char *name,
			const char *value_str, l_dbus_message_func_t callback)
{
	struct l_dbus_message_builder *builder;
	struct l_dbus_message *msg;
	const struct proxy_interface_property *property;
	struct proxy_callback_data *callback_data;

	if (!proxy || !name)
		return false;

	property = proxy_property_find(proxy->type->properties, name);
	if (!property)
		return false;

	if (!property->is_read_write)
		return false;

	if (!property->append)
		return false;

	msg = l_dbus_message_new_method_call(dbus, IWD_SERVICE, proxy->path,
						L_DBUS_INTERFACE_PROPERTIES,
						"Set");
	if (!msg)
		return false;


	builder = l_dbus_message_builder_new(msg);
	if (!builder) {
		l_dbus_message_unref(msg);
		return false;
	}

	l_dbus_message_builder_append_basic(builder, 's',
							proxy->type->interface);
	l_dbus_message_builder_append_basic(builder, 's', property->name);
	l_dbus_message_builder_enter_variant(builder, property->type);

	if (!property->append(builder, value_str)) {
		l_dbus_message_builder_destroy(builder);
		l_dbus_message_unref(msg);
		return false;
	}

	l_dbus_message_builder_leave_variant(builder);
	l_dbus_message_builder_finalize(builder);
	l_dbus_message_builder_destroy(builder);

	callback_data = l_new(struct proxy_callback_data, 1);
	callback_data->callback = callback;
	callback_data->user_data = (void *) proxy;

	l_dbus_send_with_reply(dbus, msg, proxy_callback, callback_data,
									l_free);

	return true;
}

bool dbus_message_has_error(struct l_dbus_message *message)
{
	const char *name;
	const char *text;

	if (l_dbus_message_get_error(message, &name, &text)) {
		display_error(text);
		return true;
	}

	return false;
}

static bool interface_match_by_type_name(const void *a, const void *b)
{
	const struct proxy_interface_type *type = a;
	const char *interface = b;

	return !strcmp(type->interface, interface);
}

struct proxy_interface *proxy_interface_find(const char *interface,
							const char *path)
{
	const struct l_queue_entry *entry;

	if (!interface || !path)
		return NULL;

	for (entry = l_queue_get_entries(proxy_interfaces); entry;
							entry = entry->next) {
		struct proxy_interface *proxy = entry->data;

		if (strcmp(proxy->path, path))
			continue;

		if (strcmp(proxy->type->interface, interface))
			continue;

		return proxy;
	}

	return NULL;
}

struct l_queue *proxy_interface_find_all(const char *interface,
					proxy_property_match_func_t function,
					const void *value)
{
	const struct l_queue_entry *entry;
	struct l_queue *match = NULL;

	if (!interface)
		return NULL;

	for (entry = l_queue_get_entries(proxy_interfaces); entry;
							entry = entry->next) {
		struct proxy_interface *proxy = entry->data;

		if (!interface_match_by_type_name(proxy->type, interface))
			continue;

		if (function && !function(proxy->data, value))
			continue;

		if (!match)
			match = l_queue_new();

		l_queue_push_tail(match, proxy);
	}

	return match;
}

bool proxy_interface_is_same(const struct proxy_interface *a,
					const struct proxy_interface *b)
{
	return !strcmp(a->path, b->path);
}

static void properties_changed_callback(struct l_dbus_message *message,
								void *data)
{
	struct proxy_interface *proxy;
	const char *path;
	const char *interface;
	struct l_dbus_message_iter changed;
	struct l_dbus_message_iter invalidated;

	if (dbus_message_has_error(message))
		return;

	if (!l_dbus_message_get_arguments(message, "sa{sv}as", &interface,
						&changed, &invalidated)) {
		l_debug("Failed to parse properties changed callback message");

		return;
	}

	path = l_dbus_message_get_path(message);
	if (!path)
		return;

	proxy = proxy_interface_find(interface, path);
	if (!proxy)
		return;

	interface_update_properties(proxy, &changed, &invalidated);
}

static bool is_ignorable(const char *interface)
{
	size_t i;

	static const struct {
		const char *interface;
	} interfaces_to_ignore[] = {
		{ L_DBUS_INTERFACE_OBJECT_MANAGER },
		{ L_DBUS_INTERFACE_INTROSPECTABLE },
		{ L_DBUS_INTERFACE_PROPERTIES },
		{ }
	};

	for (i = 0; interfaces_to_ignore[i].interface; i++)
		if (!strcmp(interfaces_to_ignore[i].interface, interface))
			return true;

	return false;
}

static void proxy_interfaces_update_properties(const char *path,
					struct l_dbus_message_iter *interfaces)
{
	const char *interface;
	struct l_dbus_message_iter properties;
	struct proxy_interface *proxy;
	struct proxy_interface_type *interface_type;

	if (!path)
		return;

	while (l_dbus_message_iter_next_entry(interfaces, &interface,
								&properties)) {
		interface_type = l_queue_find(proxy_interface_types,
						interface_match_by_type_name,
						interface);
		if (!interface_type)
			continue;

		proxy = proxy_interface_find(interface_type->interface, path);
		if (!proxy)
			continue;

		interface_update_properties(proxy, &properties, NULL);
	}
}

static void proxy_interface_create(const char *path,
					struct l_dbus_message_iter *interfaces)
{
	const char *interface;
	struct l_dbus_message_iter properties;
	struct proxy_interface *proxy;
	struct proxy_interface_type *interface_type;

	if (!path)
		return;

	while (l_dbus_message_iter_next_entry(interfaces, &interface,
								&properties)) {
		interface_type = l_queue_find(proxy_interface_types,
						interface_match_by_type_name,
						interface);

		if (!interface_type) {
			if (!is_ignorable(interface))
				l_debug("Unknown DBus interface type %s",
								interface);

			continue;
		}

		proxy = proxy_interface_find(interface_type->interface, path);

		if (proxy)
			continue;

		proxy = l_new(struct proxy_interface, 1);
		proxy->path = l_strdup(path);
		proxy->type = interface_type;

		l_queue_push_tail(proxy_interfaces, proxy);

		if (interface_type->ops && interface_type->ops->create)
			proxy->data = interface_type->ops->create();
	}
}

static void proxy_interface_destroy(void *data)
{
	struct proxy_interface *proxy = data;

	l_free(proxy->path);

	if (proxy->type->ops && proxy->type->ops->destroy)
		proxy->type->ops->destroy(proxy->data);

	proxy->type = NULL;

	l_free(proxy);
}

bool proxy_interface_method_call(const struct proxy_interface *proxy,
					const char *name, const char *signature,
					l_dbus_message_func_t callback, ...)
{
	struct proxy_callback_data *callback_data;
	struct l_dbus_message *call;
	va_list args;

	if (!proxy || !name)
		return false;

	call = l_dbus_message_new_method_call(dbus, IWD_SERVICE, proxy->path,
						 proxy->type->interface, name);

	va_start(args, callback);
	l_dbus_message_set_arguments_valist(call, signature, args);
	va_end(args);

	callback_data = l_new(struct proxy_callback_data, 1);
	callback_data->callback = callback;
	callback_data->user_data = (void *) proxy;

	l_dbus_send_with_reply(dbus, call, proxy_callback, callback_data,
									l_free);

	return true;
}

void *proxy_interface_get_data(const struct proxy_interface *proxy)
{
	return proxy->data;
}

const char *proxy_interface_get_interface(const struct proxy_interface *proxy)
{
	return proxy->type->interface;
}

const char *proxy_interface_get_path(const struct proxy_interface *proxy)
{
	return proxy->path;
}

const char *proxy_interface_get_identity_str(
					const struct proxy_interface *proxy)
{
	if (proxy->type->ops && proxy->type->ops->identity)
		return proxy->type->ops->identity(proxy->data);

	return NULL;
}

void proxy_interface_display_list(const char *interface)
{
	const struct l_queue_entry *entry;

	for (entry = l_queue_get_entries(proxy_interfaces); entry;
							entry = entry->next) {
		const struct proxy_interface *proxy = entry->data;

		if (!interface_match_by_type_name(proxy->type, interface))
			continue;

		if (!proxy->type->ops || !proxy->type->ops->display)
			break;

		proxy->type->ops->display(MARGIN, proxy->data);
	}
}

static void interfaces_added_callback(struct l_dbus_message *message,
								void *user_data)
{
	const char *path;
	struct l_dbus_message_iter object;

	if (dbus_message_has_error(message))
		return;

	if (!l_dbus_message_get_arguments(message, "oa{sa{sv}}", &path,
								&object))
		return;

	proxy_interface_create(path, &object);

	if (!l_dbus_message_get_arguments(message, "oa{sa{sv}}", &path,
								&object))
		return;

	proxy_interfaces_update_properties(path, &object);
}

static void interfaces_removed_callback(struct l_dbus_message *message,
								void *user_data)
{
	const char *interface;
	const char *path;
	struct l_dbus_message_iter interfaces;
	struct proxy_interface *proxy;

	if (dbus_message_has_error(message))
		return;

	if (!l_dbus_message_get_arguments(message, "oas", &path, &interfaces))
		return;

	while (l_dbus_message_iter_next_entry(&interfaces, &interface)) {
		proxy = proxy_interface_find(interface, path);

		if (!proxy)
			continue;

		l_queue_remove(proxy_interfaces, proxy);

		proxy_interface_destroy(proxy);
	}
}

static void get_managed_objects_callback(struct l_dbus_message *message,
								void *user_data)
{
	struct l_dbus_message_iter objects;
	struct l_dbus_message_iter object;
	const char *path;

	if (dbus_message_has_error(message)) {
		display_error("Failed to retrieve IWD dbus objects, "
							"quitting...\n");

		goto error;
	}

	if (!l_dbus_message_get_arguments(message, "a{oa{sa{sv}}}", &objects)) {
		display_error("Failed to parse IWD dbus objects, "
							"quitting...\n");

		goto error;
	}

	while (l_dbus_message_iter_next_entry(&objects, &path, &object))
		proxy_interface_create(path, &object);

	if (!l_dbus_message_get_arguments(message, "a{oa{sa{sv}}}", &objects))
		/*
		 * Shouldn't happen since we parsed it above, but check return
		 * anyway.
		 */
		goto error;

	while (l_dbus_message_iter_next_entry(&objects, &path, &object))
		proxy_interfaces_update_properties(path, &object);

	if (command_is_interactive_mode())
		display_enable_cmd_prompt();
	else
		command_noninteractive_trigger();

	return;

error:
	if (!command_is_interactive_mode())
		command_set_exit_status(EXIT_FAILURE);

	l_main_quit();
}

static void get_managed_objects(void)
{
	l_dbus_method_call(dbus, IWD_SERVICE, IWD_ROOT_PATH,
					L_DBUS_INTERFACE_OBJECT_MANAGER,
					"GetManagedObjects", NULL,
					get_managed_objects_callback,
					NULL, NULL);
}

static void service_appeared_callback(struct l_dbus *dbus, void *user_data)
{
	l_dbus_add_signal_watch(dbus, IWD_SERVICE, IWD_ROOT_PATH,
					L_DBUS_INTERFACE_OBJECT_MANAGER,
					"InterfacesAdded", L_DBUS_MATCH_NONE,
					interfaces_added_callback, NULL);

	l_dbus_add_signal_watch(dbus, IWD_SERVICE, IWD_ROOT_PATH,
					L_DBUS_INTERFACE_OBJECT_MANAGER,
					"InterfacesRemoved", L_DBUS_MATCH_NONE,
					interfaces_removed_callback, NULL);

	l_dbus_add_signal_watch(dbus, IWD_SERVICE, NULL,
					L_DBUS_INTERFACE_PROPERTIES,
					"PropertiesChanged", L_DBUS_MATCH_NONE,
					properties_changed_callback, NULL);

	get_managed_objects();
}

static void service_disappeared_callback(struct l_dbus *dbus,
							void *user_data)
{
	if (!command_is_interactive_mode()) {
		command_set_exit_status(EXIT_FAILURE);
		l_main_quit();
	}

	l_queue_clear(proxy_interfaces, proxy_interface_destroy);

	display_disable_cmd_prompt();
}

static void dbus_disconnect_callback(void *user_data)
{
	if (!command_is_interactive_mode())
		return;

	l_main_quit();
}

void proxy_interface_type_register(
			const struct proxy_interface_type *interface_type)
{
	l_queue_push_tail(proxy_interface_types, (void *) interface_type);
}

void proxy_interface_type_unregister(
			const struct proxy_interface_type *interface_type)
{
	l_queue_remove(proxy_interface_types, (void *) interface_type);
}

struct l_dbus *dbus_get_bus(void)
{
	return dbus;
}

extern struct interface_type_desc __start___interface[];
extern struct interface_type_desc __stop___interface[];

bool dbus_proxy_init(void)
{
	struct interface_type_desc *desc;

	if (dbus)
		return true;

	dbus = l_dbus_new_default(L_DBUS_SYSTEM_BUS);
	if (!dbus)
		return false;

	proxy_interface_types = l_queue_new();
	proxy_interfaces = l_queue_new();

	for (desc = __start___interface; desc < __stop___interface; desc++) {
		if (!desc->init)
			continue;

		desc->init();
	}

	l_dbus_set_disconnect_handler(dbus, dbus_disconnect_callback, NULL,
									NULL);

	if (command_is_interactive_mode())
		l_dbus_add_service_watch(dbus, IWD_SERVICE,
						service_appeared_callback,
						service_disappeared_callback,
						NULL, NULL);
	else
		get_managed_objects();

	return true;
}

bool dbus_proxy_exit(void)
{
	struct interface_type_desc *desc;

	agent_manager_unregister_agent();

	for (desc = __start___interface; desc < __stop___interface; desc++) {
		if (!desc->exit)
			continue;

		desc->exit();
	}

	l_queue_destroy(proxy_interface_types, NULL);
	proxy_interface_types = NULL;

	l_queue_destroy(proxy_interfaces, proxy_interface_destroy);
	proxy_interfaces = NULL;

	l_dbus_destroy(dbus);
	dbus = NULL;

	return true;
}