Debian Codegraph @

dbus/tests/tests-chip.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPDX-FileCopyrightText: 2022-2023 Bartosz Golaszewski <[email protected]>
3
4 #include <gio/gio.h>
5 #include <glib.h>
6 #include <gpiod-test.h>
7 #include <gpiod-test-common.h>
8 #include <gpiodbus.h>
9 #include <gpiosim-glib.h>
10
11 #include "daemon-process.h"
12 #include "helpers.h"
13
14 #define GPIOD_TEST_GROUP "gpiodbus/chip"
15
16 GPIOD_TEST_CASE(read_chip_info)
17 {
18 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8,
19 "label", "foobar",
20 NULL);
21 g_autoptr(GpiodbusDaemonProcess) mgr = NULL;
22 g_autoptr(GpiodbusChip) chip = NULL;
23 g_autofree gchar *obj_path = NULL;
24
25 mgr = gpiodbus_daemon_process_new();
26 gpiodbus_test_wait_for_sim_intf(sim);
27
28 obj_path = g_strdup_printf("/io/gpiod1/chips/%s",
29 g_gpiosim_chip_get_name(sim));
30 chip = gpiodbus_test_get_chip_proxy_or_fail(obj_path);
31
32 g_assert_cmpstr(gpiodbus_chip_get_name(chip), ==,
33 g_gpiosim_chip_get_name(sim));
34 g_assert_cmpstr(gpiodbus_chip_get_label(chip), ==, "foobar");
35 g_assert_cmpuint(gpiodbus_chip_get_num_lines(chip), ==, 8);
36 g_assert_cmpstr(gpiodbus_chip_get_path(chip), ==,
37 g_gpiosim_chip_get_dev_path(sim));
38 }
39
40 static gboolean on_timeout(gpointer user_data)
41 {
42 gboolean *timed_out = user_data;
43
44 *timed_out = TRUE;
45
46 return G_SOURCE_REMOVE;
47 }
48
49 static void on_object_event(GDBusObjectManager *manager G_GNUC_UNUSED,
50 GpiodbusObject *object, gpointer user_data)
51 {
52 gchar **obj_path = user_data;
53
54 *obj_path = g_strdup(g_dbus_object_get_object_path(
55 G_DBUS_OBJECT(object)));
56 }
57
58 GPIOD_TEST_CASE(chip_added)
59 {
60 g_autoptr(GDBusObjectManager) manager = NULL;
61 g_autoptr(GpiodbusDaemonProcess) mgr = NULL;
62 g_autofree gchar *sim_obj_path = NULL;
63 g_autoptr(GPIOSimChip) sim = NULL;
64 g_autofree gchar *obj_path = NULL;
65 gboolean timed_out = FALSE;
66 guint timeout_id;
67
68 mgr = gpiodbus_daemon_process_new();
69
70 manager = gpiodbus_test_get_chip_object_manager_or_fail();
71
72 g_signal_connect(manager, "object-added", G_CALLBACK(on_object_event),
73 &obj_path);
74 timeout_id = g_timeout_add_seconds(5, on_timeout, &timed_out);
75
76 sim = g_gpiosim_chip_new(NULL);
77
78 while (!obj_path && !timed_out)
79 g_main_context_iteration(NULL, TRUE);
80
81 if (timed_out) {
82 g_test_fail_printf("timeout reached waiting for chip to be added");
83 return;
84 }
85
86 sim_obj_path = g_strdup_printf("/io/gpiod1/chips/%s",
87 g_gpiosim_chip_get_name(sim));
88
89 g_assert_cmpstr(sim_obj_path, ==, obj_path);
90
91 g_source_remove(timeout_id);
92 }
93
94 GPIOD_TEST_CASE(chip_removed)
95 {
96 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new(NULL);
97 g_autoptr(GDBusObjectManager) manager = NULL;
98 g_autoptr(GpiodbusDaemonProcess) mgr = NULL;
99 g_autofree gchar *sim_obj_path = NULL;
100 g_autoptr(GpiodbusChip) chip = NULL;
101 g_autofree gchar *obj_path = NULL;
102 gboolean timed_out = FALSE;
103 guint timeout_id;
104
105 sim_obj_path = g_strdup_printf("/io/gpiod1/chips/%s",
106 g_gpiosim_chip_get_name(sim));
107
108 mgr = gpiodbus_daemon_process_new();
109 gpiodbus_test_wait_for_sim_intf(sim);
110
111 obj_path = g_strdup_printf("/io/gpiod1/chips/%s",
112 g_gpiosim_chip_get_name(sim));
113 chip = gpiodbus_test_get_chip_proxy_or_fail(obj_path);
114 manager = gpiodbus_test_get_chip_object_manager_or_fail();
115
116 g_signal_connect(manager, "object-removed", G_CALLBACK(on_object_event),
117 &obj_path);
118 timeout_id = g_timeout_add_seconds(5, on_timeout, &timed_out);
119
120 g_clear_object(&sim);
121
122 while (!obj_path && !timed_out)
123 g_main_context_iteration(NULL, TRUE);
124
125 if (timed_out) {
126 g_test_fail_printf("timeout reached waiting for chip to be removed");
127 return;
128 }
129
130 g_assert_cmpstr(sim_obj_path, ==, obj_path);
131
132 g_source_remove(timeout_id);
133 }