Debian Codegraph @

dbus/tests/tests-line.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPDX-FileCopyrightText: 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/line"
15
16 GPIOD_TEST_CASE(read_line_properties)
17 {
18 static const GPIOSimLineName names[] = {
19 { .offset = 1, .name = "foo", },
20 { .offset = 2, .name = "bar", },
21 { .offset = 4, .name = "baz", },
22 { .offset = 5, .name = "xyz", },
23 { }
24 };
25
26 static const GPIOSimHog hogs[] = {
27 {
28 .offset = 3,
29 .name = "hog3",
30 .direction = G_GPIOSIM_DIRECTION_OUTPUT_HIGH,
31 },
32 {
33 .offset = 4,
34 .name = "hog4",
35 .direction = G_GPIOSIM_DIRECTION_OUTPUT_LOW,
36 },
37 { }
38 };
39
40 g_autoptr(GpiodbusDaemonProcess) mgr = NULL;
41 g_autoptr(GpiodbusLine) line4 = NULL;
42 g_autoptr(GpiodbusLine) line6 = NULL;
43 g_autofree gchar *obj_path_4 = NULL;
44 g_autofree gchar *obj_path_6 = NULL;
45 g_autoptr(GPIOSimChip) sim = NULL;
46 g_autoptr(GVariant) vnames = g_gpiosim_package_line_names(names);
47 g_autoptr(GVariant) vhogs = g_gpiosim_package_hogs(hogs);
48
49 sim = g_gpiosim_chip_new(
50 "num-lines", 8,
51 "line-names", vnames,
52 "hogs", vhogs,
53 NULL);
54
55 mgr = gpiodbus_daemon_process_new();
56 gpiodbus_test_wait_for_sim_intf(sim);
57
58 obj_path_4 = g_strdup_printf("/io/gpiod1/chips/%s/line4",
59 g_gpiosim_chip_get_name(sim));
60 line4 = gpiodbus_test_get_line_proxy_or_fail(obj_path_4);
61
62 obj_path_6 = g_strdup_printf("/io/gpiod1/chips/%s/line6",
63 g_gpiosim_chip_get_name(sim));
64 line6 = gpiodbus_test_get_line_proxy_or_fail(obj_path_6);
65
66 g_assert_cmpuint(gpiodbus_line_get_offset(line4), ==, 4);
67 g_assert_cmpstr(gpiodbus_line_get_name(line4), ==, "baz");
68 g_assert_cmpstr(gpiodbus_line_get_consumer(line4), ==, "hog4");
69 g_assert_true(gpiodbus_line_get_used(line4));
70 g_assert_false(gpiodbus_line_get_managed(line4));
71 g_assert_cmpstr(gpiodbus_line_get_direction(line4), ==, "output");
72 g_assert_cmpstr(gpiodbus_line_get_edge_detection(line4), ==, "none");
73 g_assert_false(gpiodbus_line_get_active_low(line4));
74 g_assert_cmpstr(gpiodbus_line_get_bias(line4), ==, "unknown");
75 g_assert_cmpstr(gpiodbus_line_get_drive(line4), ==, "push-pull");
76 g_assert_cmpstr(gpiodbus_line_get_event_clock(line4), ==, "monotonic");
77 g_assert_false(gpiodbus_line_get_debounced(line4));
78 g_assert_cmpuint(gpiodbus_line_get_debounce_period_us(line4), ==, 0);
79
80 g_assert_cmpuint(gpiodbus_line_get_offset(line6), ==, 6);
81 g_assert_cmpstr(gpiodbus_line_get_name(line6), ==, "");
82 g_assert_cmpstr(gpiodbus_line_get_consumer(line6), ==, "");
83 g_assert_false(gpiodbus_line_get_used(line6));
84 }
85
86 static gboolean on_timeout(gpointer user_data)
87 {
88 gboolean *timed_out = user_data;
89
90 *timed_out = TRUE;
91
92 return G_SOURCE_REMOVE;
93 }
94
95 static void
96 on_properties_changed(GpiodbusLine *line G_GNUC_UNUSED,
97 GVariant *changed_properties,
98 GStrv invalidated_properties G_GNUC_UNUSED,
99 gpointer user_data)
100 {
101 GHashTable *changed_props = user_data;
102 GVariantIter iter;
103 GVariant *variant;
104 gchar *str;
105
106 g_variant_iter_init(&iter, changed_properties);
107 while (g_variant_iter_next(&iter, "{sv}", &str, &variant)) {
108 g_hash_table_insert(changed_props, str, NULL);
109 g_variant_unref(variant);
110 }
111 }
112
113 static void check_props_requested(GHashTable *props)
114 {
115 if (!g_hash_table_contains(props, "Direction") ||
116 !g_hash_table_contains(props, "Consumer") ||
117 !g_hash_table_contains(props, "Used") ||
118 !g_hash_table_contains(props, "RequestPath") ||
119 !g_hash_table_contains(props, "Managed"))
120 g_test_fail_printf("Not all expected properties have changed");
121 }
122
123 static void check_props_released(GHashTable *props)
124 {
125 if (!g_hash_table_contains(props, "RequestPath") ||
126 !g_hash_table_contains(props, "Consumer") ||
127 !g_hash_table_contains(props, "Used") ||
128 !g_hash_table_contains(props, "Managed"))
129 g_test_fail_printf("Not all expected properties have changed");
130 }
131
132 static GVariant *make_props_changed_line_config(void)
133 {
134 g_autoptr(GVariant) output_values = NULL;
135 g_autoptr(GVariant) line_settings = NULL;
136 g_autoptr(GVariant) line_offsets = NULL;
137 g_autoptr(GVariant) line_configs = NULL;
138 g_autoptr(GVariant) line_config = NULL;
139 GVariantBuilder builder;
140
141 g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY);
142 g_variant_builder_add_value(&builder, g_variant_new_uint32(4));
143 line_offsets = g_variant_builder_end(&builder);
144
145 g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY);
146 g_variant_builder_add_value(&builder,
147 g_variant_new("{sv}", "direction",
148 g_variant_new_string("output")));
149 line_settings = g_variant_builder_end(&builder);
150
151 g_variant_builder_init(&builder, G_VARIANT_TYPE_TUPLE);
152 g_variant_builder_add_value(&builder, g_variant_ref(line_offsets));
153 g_variant_builder_add_value(&builder, g_variant_ref(line_settings));
154 line_config = g_variant_builder_end(&builder);
155
156 g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY);
157 g_variant_builder_add_value(&builder, g_variant_ref(line_config));
158 line_configs = g_variant_builder_end(&builder);
159
160 output_values = g_variant_new("ai", NULL);
161
162 g_variant_builder_init(&builder, G_VARIANT_TYPE_TUPLE);
163 g_variant_builder_add_value(&builder, g_variant_ref(line_configs));
164 g_variant_builder_add_value(&builder, g_variant_ref(output_values));
165
166 return g_variant_ref_sink(g_variant_builder_end(&builder));
167 }
168
169 GPIOD_TEST_CASE(properties_changed)
170 {
171 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
172 g_autoptr(GpiodbusDaemonProcess) mgr = NULL;
173 g_autoptr(GHashTable) changed_props = NULL;
174 g_autoptr(GpiodbusRequest) request = NULL;
175 g_autoptr(GVariant) request_config = NULL;
176 g_autoptr(GVariant) line_config = NULL;
177 g_autofree gchar *line_obj_path = NULL;
178 g_autofree gchar *chip_obj_path = NULL;
179 g_autofree gchar *request_path = NULL;
180 g_autoptr(GpiodbusChip) chip = NULL;
181 g_autoptr(GpiodbusLine) line = NULL;
182 gboolean timed_out = FALSE;
183 guint timeout_id;
184
185 mgr = gpiodbus_daemon_process_new();
186 gpiodbus_test_wait_for_sim_intf(sim);
187
188 line_obj_path = g_strdup_printf("/io/gpiod1/chips/%s/line4",
189 g_gpiosim_chip_get_name(sim));
190 line = gpiodbus_test_get_line_proxy_or_fail(line_obj_path);
191
192 chip_obj_path = g_strdup_printf("/io/gpiod1/chips/%s",
193 g_gpiosim_chip_get_name(sim));
194 chip = gpiodbus_test_get_chip_proxy_or_fail(chip_obj_path);
195
196 changed_props = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
197 NULL);
198
199 g_signal_connect(line, "g-properties-changed",
200 G_CALLBACK(on_properties_changed), changed_props);
201 timeout_id = g_timeout_add_seconds(5, on_timeout, &timed_out);
202
203 line_config = make_props_changed_line_config();
204 request_config = gpiodbus_test_make_empty_request_config();
205
206 gpiodbus_test_chip_call_request_lines_sync_or_fail(chip, line_config,
207 request_config,
208 &request_path);
209
210 while (g_hash_table_size(changed_props) < 5 && !timed_out)
211 g_main_context_iteration(NULL, TRUE);
212
213 check_props_requested(changed_props);
214
215 g_hash_table_destroy(g_hash_table_ref(changed_props));
216
217 request = gpiodbus_test_get_request_proxy_or_fail(request_path);
218 gpiodbus_test_request_call_release_sync_or_fail(request);
219
220 while (g_hash_table_size(changed_props) < 4 && !timed_out)
221 g_main_context_iteration(NULL, TRUE);
222
223 check_props_released(changed_props);
224
225 if (timed_out) {
226 g_test_fail_printf("timeout reached waiting for line properties to change");
227 return;
228 }
229
230 g_source_remove(timeout_id);
231 }