File: daemon.c

package info (click to toggle)
libgpiod 2.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,108 kB
  • sloc: ansic: 26,612; sh: 7,554; cpp: 4,944; python: 2,426; makefile: 811; xml: 49
file content (820 lines) | stat: -rw-r--r-- 25,223 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
// SPDX-License-Identifier: GPL-2.0-or-later
// SPDX-FileCopyrightText: 2022-2024 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

#include <gpiod-glib.h>
#include <gpiodbus.h>
#include <gudev/gudev.h>

#include "daemon.h"
#include "helpers.h"

struct _GpiodbusDaemon {
	GObject parent;
	GDBusConnection *con;
	GUdevClient *udev;
	GDBusObjectManagerServer *chip_manager;
	GDBusObjectManagerServer *request_manager;
	GHashTable *chips;
	GHashTable *requests;
	GTree *req_id_root;
};

G_DEFINE_TYPE(GpiodbusDaemon, gpiodbus_daemon, G_TYPE_OBJECT);

typedef struct {
	GpiodglibChip *chip;
	GpiodbusChip *dbus_chip;
	GpiodbusDaemon *daemon;
	GDBusObjectManagerServer *line_manager;
	GHashTable *lines;
} GpiodbusDaemonChipData;

typedef struct {
	GpiodglibLineRequest *request;
	GpiodbusRequest *dbus_request;
	gint id;
	GpiodbusDaemonChipData *chip_data;
} GpiodbusDaemonRequestData;

typedef struct {
	GpiodbusLine *dbus_line;
	GpiodbusDaemonChipData *chip_data;
	GpiodbusDaemonRequestData *req_data;
} GpiodbusDaemonLineData;

static const gchar *const gpiodbus_daemon_udev_subsystems[] = { "gpio", NULL };

static void gpiodbus_daemon_dispose(GObject *obj)
{
	GpiodbusDaemon *self = GPIODBUS_DAEMON(obj);

	g_debug("disposing of the GPIO daemon");

	g_clear_pointer(&self->chips, g_hash_table_unref);
	/*
	 * REVISIT: Do we even need to unref the request hash table here at
	 * all? All requests should have been freed when removing their parent
	 * chips.
	 */
	g_clear_pointer(&self->requests, g_hash_table_unref);
	g_clear_pointer(&self->req_id_root, g_tree_destroy);
	g_clear_object(&self->con);

	G_OBJECT_CLASS(gpiodbus_daemon_parent_class)->dispose(obj);
}

static void gpiodbus_daemon_finalize(GObject *obj)
{
	GpiodbusDaemon *self = GPIODBUS_DAEMON(obj);

	g_debug("finalizing GPIO daemon");

	g_clear_object(&self->request_manager);
	g_clear_object(&self->chip_manager);
	g_clear_object(&self->udev);

	G_OBJECT_CLASS(gpiodbus_daemon_parent_class)->finalize(obj);
}

static void gpiodbus_daemon_class_init(GpiodbusDaemonClass *daemon_class)
{
	GObjectClass *class = G_OBJECT_CLASS(daemon_class);

	class->dispose = gpiodbus_daemon_dispose;
	class->finalize = gpiodbus_daemon_finalize;
}

static gboolean
gpiodbus_remove_request_if_chip_matches(gpointer key G_GNUC_UNUSED,
					gpointer value, gpointer user_data)
{
	GpiodbusDaemonChipData *chip_data = user_data;
	GpiodbusDaemonRequestData *req_data = value;

	return req_data->chip_data == chip_data;
}

static void gpiodbus_daemon_chip_data_free(gpointer data)
{
	GpiodbusDaemonChipData *chip_data = data;
	const gchar *obj_path;

	obj_path = g_dbus_interface_skeleton_get_object_path(
			G_DBUS_INTERFACE_SKELETON(chip_data->dbus_chip));

	g_debug("unexporting object for GPIO chip: '%s'", obj_path);

	g_hash_table_foreach_remove(chip_data->daemon->requests,
				    gpiodbus_remove_request_if_chip_matches,
				    chip_data);

	g_dbus_object_manager_server_unexport(chip_data->daemon->chip_manager,
					      obj_path);

	g_hash_table_unref(chip_data->lines);
	g_object_unref(chip_data->line_manager);
	g_object_unref(chip_data->chip);
	g_object_unref(chip_data->dbus_chip);
	g_free(chip_data);
}

static void gpiodbus_daemon_line_data_free(gpointer data)
{
	GpiodbusDaemonLineData *line_data = data;
	const gchar *obj_path;

	obj_path = g_dbus_interface_skeleton_get_object_path(
			G_DBUS_INTERFACE_SKELETON(line_data->dbus_line));

	g_debug("unexporting object for GPIO line: '%s'",
		obj_path);

	g_dbus_object_manager_server_unexport(
				line_data->chip_data->line_manager, obj_path);

	g_object_unref(line_data->dbus_line);
	g_free(line_data);
}

static void gpiodbus_lines_set_managed(GpiodbusDaemonRequestData *req_data,
				       gboolean managed)
{
	g_autoptr(GDBusObject) obj = NULL;
	const gchar *const *line_paths;
	GpiodbusLine *line;
	const gchar *path;
	guint i;

	line_paths = gpiodbus_request_get_line_paths(req_data->dbus_request);

	for (path = line_paths[0], i = 0; path; path = line_paths[++i]) {
		obj = g_dbus_object_manager_get_object(
			G_DBUS_OBJECT_MANAGER(
				req_data->chip_data->line_manager), path);
		line = gpiodbus_object_peek_line(GPIODBUS_OBJECT(obj));

		g_debug("Setting line %u on chip object '%s' to '%s'",
			gpiodbus_line_get_offset(line),
			g_dbus_interface_skeleton_get_object_path(
				G_DBUS_INTERFACE_SKELETON(
					req_data->chip_data->dbus_chip)),
			managed ? "managed" : "unmanaged");

		gpiodbus_line_set_managed(line, managed);
		gpiodbus_line_set_request_path(line,
			managed ? g_dbus_interface_skeleton_get_object_path(
				G_DBUS_INTERFACE_SKELETON(
					req_data->dbus_request)) : NULL);
		g_dbus_interface_skeleton_flush(
					G_DBUS_INTERFACE_SKELETON(line));
	}
}

static void gpiodbus_daemon_request_data_free(gpointer data)
{
	GpiodbusDaemonRequestData *req_data = data;
	const gchar *obj_path;

	obj_path = g_dbus_interface_skeleton_get_object_path(
			G_DBUS_INTERFACE_SKELETON(req_data->dbus_request));

	g_debug("unexporting object for GPIO request: '%s'", obj_path);

	g_dbus_object_manager_server_unexport(
		req_data->chip_data->daemon->request_manager, obj_path);

	gpiodbus_lines_set_managed(req_data, FALSE);
	gpiodbus_id_free(req_data->chip_data->daemon->req_id_root,
			 req_data->id);
	g_object_unref(req_data->request);
	g_object_unref(req_data->dbus_request);
	g_free(req_data);
}

static void gpiodbus_daemon_init(GpiodbusDaemon *self)
{
	g_debug("initializing GPIO D-Bus daemon");

	self->con = NULL;
	self->udev = g_udev_client_new(gpiodbus_daemon_udev_subsystems);
	self->chip_manager =
			g_dbus_object_manager_server_new("/io/gpiod1/chips");
	self->request_manager =
			g_dbus_object_manager_server_new("/io/gpiod1/requests");
	self->chips = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
					    gpiodbus_daemon_chip_data_free);
	self->requests = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
					gpiodbus_daemon_request_data_free);
	self->req_id_root = g_tree_new_full(gpiodbus_id_cmp, NULL,
					    g_free, NULL);
}

GpiodbusDaemon *gpiodbus_daemon_new(void)
{
	return GPIODBUS_DAEMON(g_object_new(GPIODBUS_DAEMON_TYPE, NULL));
}

static void gpiodbus_daemon_on_info_event(GpiodglibChip *chip G_GNUC_UNUSED,
					  GpiodglibInfoEvent *event,
					  gpointer data)
{
	GpiodbusDaemonChipData *chip_data = data;
	g_autoptr(GpiodglibLineInfo) info = NULL;
	GpiodbusDaemonLineData *line_data;
	guint offset;

	info = gpiodglib_info_event_get_line_info(event);
	offset = gpiodglib_line_info_get_offset(info);

	g_debug("line info event received for offset %u on chip '%s'",
		offset,
		g_dbus_interface_skeleton_get_object_path(
			G_DBUS_INTERFACE_SKELETON(chip_data->dbus_chip)));

	line_data = g_hash_table_lookup(chip_data->lines,
					GINT_TO_POINTER(offset));
	if (!line_data)
		g_error("failed to retrieve line data - programming bug?");

	gpiodbus_line_set_props(line_data->dbus_line, info);
}

static void gpiodbus_daemon_export_line(GpiodbusDaemon *self,
					GpiodbusDaemonChipData *chip_data,
					GpiodglibLineInfo *info)
{
	g_autofree GpiodbusDaemonLineData *line_data = NULL;
	g_autoptr(GpiodbusObjectSkeleton) skeleton = NULL;
	g_autoptr(GpiodbusLine) dbus_line = NULL;
	g_autofree gchar *obj_path = NULL;
	const gchar *obj_prefix;
	guint line_offset;
	gboolean ret;

	obj_prefix = g_dbus_object_manager_get_object_path(
				G_DBUS_OBJECT_MANAGER(chip_data->line_manager));
	line_offset = gpiodglib_line_info_get_offset(info);
	dbus_line = gpiodbus_line_skeleton_new();
	obj_path = g_strdup_printf("%s/line%u", obj_prefix, line_offset);

	gpiodbus_line_set_props(dbus_line, info);

	skeleton = gpiodbus_object_skeleton_new(obj_path);
	gpiodbus_object_skeleton_set_line(skeleton, GPIODBUS_LINE(dbus_line));

	g_debug("exporting object for GPIO line: '%s'", obj_path);

	g_dbus_object_manager_server_export(chip_data->line_manager,
					    G_DBUS_OBJECT_SKELETON(skeleton));
	g_dbus_object_manager_server_set_connection(chip_data->line_manager,
						    self->con);

	line_data = g_malloc0(sizeof(*line_data));
	line_data->dbus_line = g_steal_pointer(&dbus_line);
	line_data->chip_data = chip_data;

	ret = g_hash_table_insert(chip_data->lines,
				  GUINT_TO_POINTER(line_offset),
				  g_steal_pointer(&line_data));
	/* It's a programming bug if the line is already in the hashmap. */
	g_assert(ret);
}

static gboolean gpiodbus_daemon_export_lines(GpiodbusDaemon *self,
					     GpiodbusDaemonChipData *chip_data)
{
	g_autoptr(GpiodglibChipInfo) chip_info = NULL;
	GpiodglibChip *chip = chip_data->chip;
	g_autoptr(GError) err = NULL;
	guint i, num_lines;
	gint j;

	chip_info = gpiodglib_chip_get_info(chip, &err);
	if (!chip_info) {
		g_critical("failed to read chip info: %s", err->message);
		return FALSE;
	}

	num_lines = gpiodglib_chip_info_get_num_lines(chip_info);

	g_signal_connect(chip, "info-event",
			 G_CALLBACK(gpiodbus_daemon_on_info_event), chip_data);

	for (i = 0; i < num_lines; i++) {
		g_autoptr(GpiodglibLineInfo) linfo = NULL;

		linfo = gpiodglib_chip_watch_line_info(chip, i, &err);
		if (!linfo) {
			g_critical("failed to setup a line-info watch: %s",
				   err->message);
			for (j = i; j >= 0; j--)
				gpiodglib_chip_unwatch_line_info(chip, i, NULL);
			return FALSE;
		}

		gpiodbus_daemon_export_line(self, chip_data, linfo);
	}

	return TRUE;
}

static gboolean
gpiodbus_daemon_handle_release_lines(GpiodbusRequest *request,
				     GDBusMethodInvocation *invocation,
				     gpointer user_data)
{
	GpiodbusDaemonRequestData *req_data = user_data;
	g_autofree gchar *obj_path = NULL;
	gboolean ret;

	obj_path = g_strdup(g_dbus_interface_skeleton_get_object_path(
					G_DBUS_INTERFACE_SKELETON(request)));

	g_debug("release call received on request '%s'", obj_path);

	ret = g_hash_table_remove(req_data->chip_data->daemon->requests,
				  obj_path);
	/* It's a programming bug if the request was not in the hashmap. */
	if (!ret)
		g_warning("request '%s' is not registered - logic error?",
			  obj_path);

	g_dbus_method_invocation_return_value(invocation, NULL);

	return G_SOURCE_CONTINUE;
}

static gboolean
gpiodbus_daemon_handle_reconfigure_lines(GpiodbusRequest *request,
					 GDBusMethodInvocation *invocation,
					 GVariant *arg_line_cfg,
					 gpointer user_data)
{
	GpiodbusDaemonRequestData *req_data = user_data;
	g_autoptr(GpiodglibLineConfig) line_cfg = NULL;
	g_autofree gchar *line_cfg_str = NULL;
	g_autoptr(GError) err = NULL;
	const gchar *obj_path;
	gboolean ret;

	obj_path = g_dbus_interface_skeleton_get_object_path(
					G_DBUS_INTERFACE_SKELETON(request));
	line_cfg_str = g_variant_print(arg_line_cfg, FALSE);

	g_debug("reconfigure call received on request '%s', line config: %s",
		obj_path, line_cfg_str);

	line_cfg = gpiodbus_line_config_from_variant(arg_line_cfg);
	if (!line_cfg) {
		g_critical("failed to convert method call arguments '%s' to line config",
			   line_cfg_str);
		g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR,
						      G_DBUS_ERROR_INVALID_ARGS,
						      "Invalid line configuration");
		goto out;
	}

	ret = gpiodglib_line_request_reconfigure_lines(req_data->request,
						       line_cfg, &err);
	if (!ret) {
		g_critical("failed to reconfigure GPIO lines on request '%s': %s",
			   obj_path, err->message);
		g_dbus_method_invocation_return_dbus_error(invocation,
						"io.gpiod1.ReconfigureFailed",
						err->message);
		goto out;
	}

	g_dbus_method_invocation_return_value(invocation, NULL);

out:
	return G_SOURCE_CONTINUE;
}

static gboolean
gpiodbus_daemon_handle_get_values(GpiodbusRequest *request,
				  GDBusMethodInvocation *invocation,
				  GVariant *arg_offsets, gpointer user_data)
{
	GpiodbusDaemonRequestData *req_data = user_data;
	g_autoptr(GVariant) out_values = NULL;
	g_autofree gchar *offsets_str = NULL;
	g_autoptr(GVariant) response = NULL;
	g_autoptr(GArray) offsets = NULL;
	g_autoptr(GArray) values = NULL;
	g_autoptr(GError) err = NULL;
	GVariantBuilder builder;
	const gchar *obj_path;
	GVariantIter iter;
	gsize num_offsets;
	guint offset, i;
	gboolean ret;

	obj_path = g_dbus_interface_skeleton_get_object_path(
					G_DBUS_INTERFACE_SKELETON(request));
	offsets_str = g_variant_print(arg_offsets, FALSE);
	num_offsets = g_variant_n_children(arg_offsets);

	g_debug("get-values call received on request '%s' for offsets: %s",
		obj_path, offsets_str);

	if (num_offsets == 0) {
		ret = gpiodglib_line_request_get_values(req_data->request,
							&values, &err);
	} else {
		offsets = g_array_sized_new(FALSE, TRUE, sizeof(offset),
					    num_offsets);
		g_variant_iter_init(&iter, arg_offsets);
		while (g_variant_iter_next(&iter, "u", &offset))
			g_array_append_val(offsets, offset);

		ret = gpiodglib_line_request_get_values_subset(
				req_data->request, offsets, &values, &err);
	}
	if (!ret) {
		g_critical("failed to get GPIO line values on request '%s': %s",
			   obj_path, err->message);
		g_dbus_method_invocation_return_dbus_error(invocation,
						"io.gpiod1.GetValuesFailed",
						err->message);
		goto out;
	}

	g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY);
	for (i = 0; i < values->len; i++)
		g_variant_builder_add(&builder, "i",
				      g_array_index(values, gint, i));
	out_values = g_variant_ref_sink(g_variant_builder_end(&builder));

	g_variant_builder_init(&builder, G_VARIANT_TYPE_TUPLE);
	g_variant_builder_add_value(&builder, out_values);
	response = g_variant_ref_sink(g_variant_builder_end(&builder));

	g_dbus_method_invocation_return_value(invocation, response);

out:
	return G_SOURCE_CONTINUE;
}

static gboolean
gpiodbus_daemon_handle_set_values(GpiodbusRequest *request,
				  GDBusMethodInvocation *invocation,
				  GVariant *arg_values, gpointer user_data)
{
	GpiodbusDaemonRequestData *req_data = user_data;
	g_autofree gchar *values_str = NULL;
	g_autoptr(GArray) offsets = NULL;
	g_autoptr(GArray) values = NULL;
	g_autoptr(GError) err = NULL;
	const gchar *obj_path;
	GVariantIter iter;
	gsize num_values;
	guint offset;
	gboolean ret;
	gint value;

	obj_path = g_dbus_interface_skeleton_get_object_path(
					G_DBUS_INTERFACE_SKELETON(request));
	values_str = g_variant_print(arg_values, FALSE);
	num_values = g_variant_n_children(arg_values);

	g_debug("set-values call received on request '%s': %s",
		obj_path, values_str);

	if (num_values == 0) {
		g_critical("Client passed no offset to value mappings");
		g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR,
						      G_DBUS_ERROR_INVALID_ARGS,
						      "No offset <-> value mappings specified");
		goto out;
	}

	offsets = g_array_sized_new(FALSE, TRUE, sizeof(offset), num_values);
	values = g_array_sized_new(FALSE, TRUE, sizeof(value), num_values);

	g_variant_iter_init(&iter, arg_values);
	while (g_variant_iter_next(&iter, "{ui}", &offset, &value)) {
		g_array_append_val(offsets, offset);
		g_array_append_val(values, value);
	}

	ret = gpiodglib_line_request_set_values_subset(req_data->request,
						       offsets, values, &err);
	if (!ret) {
		g_critical("failed to set GPIO line values on request '%s': %s",
			   obj_path, err->message);
		g_dbus_method_invocation_return_dbus_error(invocation,
						"io.gpiod1.SetValuesFailed",
						err->message);
		goto out;
	}

	g_dbus_method_invocation_return_value(invocation, NULL);

out:
	return G_SOURCE_CONTINUE;
}

static void
gpiodbus_daemon_on_edge_event(GpiodglibLineRequest *request G_GNUC_UNUSED,
			      GpiodglibEdgeEvent *event, gpointer user_data)
{
	GpiodbusDaemonRequestData *req_data = user_data;
	guint64 line_seqno, global_seqno, timestamp;
	GpiodbusDaemonLineData *line_data;
	GpiodglibEdgeEventType edge;
	guint offset;
	gint32 val;

	edge = gpiodglib_edge_event_get_event_type(event);
	offset = gpiodglib_edge_event_get_line_offset(event);
	timestamp = gpiodglib_edge_event_get_timestamp_ns(event);
	global_seqno = gpiodglib_edge_event_get_global_seqno(event);
	line_seqno = gpiodglib_edge_event_get_line_seqno(event);

	val = edge == GPIODGLIB_EDGE_EVENT_RISING_EDGE ? 1 : 0;

	g_debug("%s edge event received for offset %u on request '%s'",
		val ? "rising" : "falling", offset,
		g_dbus_interface_skeleton_get_object_path(
			G_DBUS_INTERFACE_SKELETON(req_data->dbus_request)));

	line_data = g_hash_table_lookup(req_data->chip_data->lines,
					GINT_TO_POINTER(offset));
	if (!line_data)
		g_error("failed to retrieve line data - programming bug?");

	gpiodbus_line_emit_edge_event(line_data->dbus_line,
				      g_variant_new("(ittt)", val, timestamp,
						    global_seqno, line_seqno));
}

static void
gpiodbus_daemon_export_request(GpiodbusDaemon *self,
			       GpiodglibLineRequest *request,
			       GpiodbusDaemonChipData *chip_data, gint id)
{
	g_autofree GpiodbusDaemonRequestData *req_data = NULL;
	g_autoptr(GpiodbusObjectSkeleton) skeleton = NULL;
	g_autoptr(GpiodbusRequest) dbus_req = NULL;
	g_autofree gchar *obj_path = NULL;
	gboolean ret;

	dbus_req = gpiodbus_request_skeleton_new();
	obj_path = g_strdup_printf("/io/gpiod1/requests/request%d", id);

	gpiodbus_request_set_props(dbus_req, request, chip_data->dbus_chip,
				G_DBUS_OBJECT_MANAGER(chip_data->line_manager));

	skeleton = gpiodbus_object_skeleton_new(obj_path);
	gpiodbus_object_skeleton_set_request(skeleton,
					     GPIODBUS_REQUEST(dbus_req));

	g_debug("exporting object for GPIO request: '%s'", obj_path);

	g_dbus_object_manager_server_export(self->request_manager,
					    G_DBUS_OBJECT_SKELETON(skeleton));

	req_data = g_malloc0(sizeof(*req_data));
	req_data->chip_data = chip_data;
	req_data->dbus_request = g_steal_pointer(&dbus_req);
	req_data->id = id;
	req_data->request = g_object_ref(request);

	g_signal_connect(req_data->dbus_request, "handle-release",
			 G_CALLBACK(gpiodbus_daemon_handle_release_lines),
			 req_data);
	g_signal_connect(req_data->dbus_request, "handle-reconfigure-lines",
			 G_CALLBACK(gpiodbus_daemon_handle_reconfigure_lines),
			 req_data);
	g_signal_connect(req_data->dbus_request, "handle-get-values",
			 G_CALLBACK(gpiodbus_daemon_handle_get_values),
			 req_data);
	g_signal_connect(req_data->dbus_request, "handle-set-values",
			 G_CALLBACK(gpiodbus_daemon_handle_set_values),
			 req_data);
	g_signal_connect(req_data->request, "edge-event",
			 G_CALLBACK(gpiodbus_daemon_on_edge_event), req_data);

	gpiodbus_lines_set_managed(req_data, TRUE);

	ret = g_hash_table_insert(self->requests, g_steal_pointer(&obj_path),
				  g_steal_pointer(&req_data));
	/* It's a programming bug if the request is already in the hashmap. */
	g_assert(ret);
}

static gboolean
gpiodbus_daemon_handle_request_lines(GpiodbusChip *chip,
				     GDBusMethodInvocation *invocation,
				     GVariant *arg_line_cfg,
				     GVariant *arg_req_cfg,
				     gpointer user_data)
{
	GpiodbusDaemonChipData *chip_data = user_data;
	g_autoptr(GpiodglibRequestConfig) req_cfg = NULL;
	g_autoptr(GpiodglibLineRequest) request = NULL;
	g_autoptr(GpiodglibLineConfig) line_cfg = NULL;
	g_autofree gchar *line_cfg_str = NULL;
	g_autofree gchar *req_cfg_str = NULL;
	g_autofree gchar *response = NULL;
	g_autoptr(GError) err = NULL;
	const gchar *obj_path;
	guint id;

	obj_path = g_dbus_interface_skeleton_get_object_path(
			G_DBUS_INTERFACE_SKELETON(chip));
	line_cfg_str = g_variant_print(arg_line_cfg, FALSE);
	req_cfg_str = g_variant_print(arg_req_cfg, FALSE);

	g_debug("line request received on chip '%s', line config: %s, request_config: %s",
		obj_path, line_cfg_str, req_cfg_str);

	line_cfg = gpiodbus_line_config_from_variant(arg_line_cfg);
	if (!line_cfg) {
		g_critical("failed to convert method call arguments '%s' to line config",
			   line_cfg_str);
		g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR,
						      G_DBUS_ERROR_INVALID_ARGS,
						      "Invalid line configuration");
		goto out;
	}

	req_cfg = gpiodbus_request_config_from_variant(arg_req_cfg);
	if (!req_cfg) {
		g_critical("failed to convert method call arguments '%s' to request config",
			   req_cfg_str);
		g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR,
						      G_DBUS_ERROR_INVALID_ARGS,
						      "Invalid request configuration");
		goto out;
	}

	request = gpiodglib_chip_request_lines(chip_data->chip, req_cfg,
					      line_cfg, &err);
	if (err) {
		g_critical("failed to request GPIO lines on chip '%s': %s",
			   obj_path, err->message);
		g_dbus_method_invocation_return_dbus_error(invocation,
				"io.gpiod1.RequestFailed", err->message);
		goto out;
	}

	g_debug("line request succeeded on chip '%s'", obj_path);

	id = gpiodbus_id_alloc(chip_data->daemon->req_id_root);
	gpiodbus_daemon_export_request(chip_data->daemon, request,
				       chip_data, id);

	response = g_strdup_printf("/io/gpiod1/requests/request%d", id);
	g_dbus_method_invocation_return_value(invocation,
					      g_variant_new("(o)", response));

out:
	return G_SOURCE_CONTINUE;
}

static void gpiodbus_daemon_export_chip(GpiodbusDaemon *self, GUdevDevice *dev)
{
	g_autofree GpiodbusDaemonChipData *chip_data = NULL;
	g_autoptr(GDBusObjectManagerServer) manager = NULL;
	g_autoptr(GpiodbusObjectSkeleton) skeleton = NULL;
	const gchar *devname, *devpath, *obj_prefix;
	g_autoptr(GpiodbusChip) dbus_chip = NULL;
	g_autoptr(GpiodglibChip) chip = NULL;
	g_autoptr(GHashTable) lines = NULL;
	g_autofree gchar *obj_path = NULL;
	g_autoptr(GError) err = NULL;
	gboolean ret;

	devname = g_udev_device_get_name(dev);
	devpath = g_udev_device_get_device_file(dev);
	obj_prefix = g_dbus_object_manager_get_object_path(
				G_DBUS_OBJECT_MANAGER(self->chip_manager));

	chip = gpiodglib_chip_new(devpath, &err);
	if (!chip) {
		g_critical("failed to open GPIO chip %s: %s",
			   devpath, err->message);
		return;
	}

	dbus_chip = gpiodbus_chip_skeleton_new();
	obj_path = g_strdup_printf("%s/%s", obj_prefix, devname);

	ret = gpiodbus_chip_set_props(dbus_chip, chip, &err);
	if (!ret) {
		g_critical("failed to set chip properties: %s", err->message);
		return;
	}

	skeleton = gpiodbus_object_skeleton_new(obj_path);
	gpiodbus_object_skeleton_set_chip(skeleton, GPIODBUS_CHIP(dbus_chip));

	g_debug("exporting object for GPIO chip: '%s'", obj_path);

	g_dbus_object_manager_server_export(self->chip_manager,
					    G_DBUS_OBJECT_SKELETON(skeleton));

	lines = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
				      gpiodbus_daemon_line_data_free);
	manager = g_dbus_object_manager_server_new(obj_path);

	chip_data = g_malloc0(sizeof(*chip_data));
	chip_data->daemon = self;
	chip_data->chip = g_steal_pointer(&chip);
	chip_data->dbus_chip = g_steal_pointer(&dbus_chip);
	chip_data->lines = g_steal_pointer(&lines);
	chip_data->line_manager = g_steal_pointer(&manager);

	ret = gpiodbus_daemon_export_lines(self, chip_data);
	if (!ret) {
		g_dbus_object_manager_server_unexport(self->chip_manager,
						      obj_path);
		return;
	}

	g_signal_connect(chip_data->dbus_chip, "handle-request-lines",
			 G_CALLBACK(gpiodbus_daemon_handle_request_lines),
			 chip_data);

	ret = g_hash_table_insert(self->chips, g_strdup(devname),
				  g_steal_pointer(&chip_data));
	/* It's a programming bug if the chip is already in the hashmap. */
	g_assert(ret);
}

static void gpiodbus_daemon_unexport_chip(GpiodbusDaemon *self,
					  GUdevDevice *dev)
{
	const gchar *name = g_udev_device_get_name(dev);
	gboolean ret;

	ret = g_hash_table_remove(self->chips, name);
	/* It's a programming bug if the chip was not in the hashmap. */
	if (!ret)
		g_warning("chip '%s' is not registered - exporting failed?",
			  name);
}

/*
 * We can get two uevents per action per gpiochip. One is for the new-style
 * character device, the other for legacy sysfs devices. We are only concerned
 * with the former, which we can tell from the latter by the presence of
 * the device file.
 */
static gboolean gpiodbus_daemon_is_gpiochip_device(GUdevDevice *dev)
{
	return g_udev_device_get_device_file(dev) != NULL;
}

static void gpiodbus_daemon_on_uevent(GUdevClient *udev G_GNUC_UNUSED,
				      const gchar *action, GUdevDevice *dev,
				      gpointer data)
{
	GpiodbusDaemon *self = data;

	if (!gpiodbus_daemon_is_gpiochip_device(dev))
		return;

	g_debug("uevent: %s action on %s device",
		action, g_udev_device_get_name(dev));

	if (g_strcmp0(action, "bind") == 0)
		gpiodbus_daemon_export_chip(self, dev);
	else if (g_strcmp0(action, "unbind") == 0)
		gpiodbus_daemon_unexport_chip(self, dev);
}

static void gpiodbus_daemon_process_chip_dev(gpointer data, gpointer user_data)
{
	GpiodbusDaemon *daemon = user_data;
	GUdevDevice *dev = data;

	if (gpiodbus_daemon_is_gpiochip_device(dev))
		gpiodbus_daemon_export_chip(daemon, dev);
}

void gpiodbus_daemon_start(GpiodbusDaemon *self, GDBusConnection *con)
{
	g_autolist(GUdevDevice) devs = NULL;

	g_assert(self);
	g_assert(!self->con); /* Don't allow to call this twice. */

	self->con = g_object_ref(con);

	/* Subscribe for GPIO uevents. */
	g_signal_connect(self->udev, "uevent",
			 G_CALLBACK(gpiodbus_daemon_on_uevent), self);

	devs = g_udev_client_query_by_subsystem(self->udev, "gpio");
	g_list_foreach(devs, gpiodbus_daemon_process_chip_dev, self);

	g_dbus_object_manager_server_set_connection(self->chip_manager,
						    self->con);
	g_dbus_object_manager_server_set_connection(self->request_manager,
						    self->con);

	g_debug("GPIO daemon now listening");
}