Debian Codegraph @

bindings/glib/tests/tests-info-event.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPDX-FileCopyrightText: 2023-2024 Bartosz Golaszewski <[email protected]>
3
4 #include <gpiod-glib.h>
5 #include <gpiod-test.h>
6 #include <gpiod-test-common.h>
7 #include <gpiosim-glib.h>
8
9 #include "helpers.h"
10
11 #define GPIOD_TEST_GROUP "glib/info-event"
12
13 GPIOD_TEST_CASE(watching_info_events_returns_line_info)
14 {
15 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
16 g_autoptr(GpiodglibChip) chip = NULL;
17 g_autoptr(GpiodglibLineInfo) info = NULL;
18
19 chip = gpiodglib_test_new_chip_or_fail(
20 g_gpiosim_chip_get_dev_path(sim));
21 info = gpiodglib_test_chip_watch_line_info_or_fail(chip, 3);
22 g_assert_cmpuint(gpiodglib_line_info_get_offset(info), ==, 3);
23 }
24
25 GPIOD_TEST_CASE(try_offset_of_out_range)
26 {
27 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
28 g_autoptr(GpiodglibChip) chip = NULL;
29 g_autoptr(GpiodglibLineInfo) info = NULL;
30 g_autoptr(GError) err = NULL;
31
32 chip = gpiodglib_test_new_chip_or_fail(
33 g_gpiosim_chip_get_dev_path(sim));
34 info = gpiodglib_chip_watch_line_info(chip, 11, &err);
35 g_assert_null(info);
36 g_assert_error(err, GPIODGLIB_ERROR, GPIODGLIB_ERR_INVAL);
37 }
38
39 static void on_bad_info_event(GpiodglibChip *chip G_GNUC_UNUSED,
40 GpiodglibInfoEvent *event G_GNUC_UNUSED,
41 gpointer data G_GNUC_UNUSED)
42 {
43 g_test_fail_printf("unexpected info event received");
44 }
45
46 static gboolean on_expected_timeout(gpointer data)
47 {
48 gboolean *done = data;
49
50 *done = TRUE;
51
52 return G_SOURCE_REMOVE;
53 }
54
55 GPIOD_TEST_CASE(event_timeout)
56 {
57 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
58 g_autoptr(GpiodglibChip) chip = NULL;
59 g_autoptr(GpiodglibLineInfo) info = NULL;
60 gboolean done = FALSE;
61
62 chip = gpiodglib_test_new_chip_or_fail(
63 g_gpiosim_chip_get_dev_path(sim));
64
65 g_signal_connect(chip, "info-event",
66 G_CALLBACK(on_bad_info_event), NULL);
67 g_timeout_add(100, on_expected_timeout, &done);
68
69 info = gpiodglib_test_chip_watch_line_info_or_fail(chip, 3);
70
71 while (!done && !g_test_failed())
72 g_main_context_iteration(NULL, TRUE);
73 }
74
75 typedef struct {
76 const gchar *chip_path;
77 guint offset;
78 } RequestContext;
79
80 typedef struct {
81 GPtrArray *events;
82 guint done;
83 gboolean failed;
84 } EventContext;
85
86 static gpointer request_reconfigure_release_line(gpointer data)
87 {
88 g_autoptr(GpiodglibLineRequest) request = NULL;
89 g_autoptr(GpiodglibLineSettings) settings = NULL;
90 g_autoptr(GpiodglibLineConfig) config = NULL;
91 g_autoptr(GArray) offsets = NULL;
92 g_autoptr(GpiodglibChip) chip = NULL;
93 g_autoptr(GError) err = NULL;
94 RequestContext *ctx = data;
95 gboolean ret;
96
97 chip = gpiodglib_chip_new(ctx->chip_path, &err);
98 g_assert_no_error(err);
99 if (g_test_failed())
100 return NULL;
101
102 offsets = gpiodglib_test_array_from_const(&ctx->offset, 1,
103 sizeof(guint));
104 config = gpiodglib_line_config_new();
105 settings = gpiodglib_line_settings_new(NULL);
106
107 ret = gpiodglib_line_config_add_line_settings(config, offsets,
108 settings, &err);
109 g_assert_true(ret);
110 g_assert_no_error(err);
111 if (g_test_failed())
112 return NULL;
113
114 g_usleep(1000);
115
116 request = gpiodglib_chip_request_lines(chip, NULL, config, &err);
117 g_assert_nonnull(request);
118 g_assert_no_error(err);
119
120 g_usleep(1000);
121
122 gpiodglib_line_config_reset(config);
123 gpiodglib_line_settings_set_direction(settings,
124 GPIODGLIB_LINE_DIRECTION_OUTPUT);
125 ret = gpiodglib_line_config_add_line_settings(config, offsets,
126 settings, &err);
127 g_assert_true(ret);
128 g_assert_no_error(err);
129 if (g_test_failed())
130 return NULL;
131
132 ret = gpiodglib_line_request_reconfigure_lines(request, config, &err);
133 g_assert_true(ret);
134 g_assert_no_error(err);
135 if (g_test_failed())
136 return NULL;
137
138 g_usleep(1000);
139
140 gpiodglib_line_request_release(request);
141
142 return NULL;
143 }
144
145 static void basic_on_info_event(GpiodglibChip *chip G_GNUC_UNUSED,
146 GpiodglibInfoEvent *event, gpointer data)
147 {
148 EventContext *ctx = data;
149
150 g_ptr_array_add(ctx->events, g_object_ref(event));
151 ctx->done++;
152 }
153
154 static gboolean on_timeout(gpointer data)
155 {
156 gboolean *failed = data;
157
158 g_test_fail_printf("wait for info event timed out");
159 *failed = TRUE;
160
161 return G_SOURCE_CONTINUE;
162 }
163
164 GPIOD_TEST_CASE(request_reconfigure_release_events)
165 {
166 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
167 g_autoptr(GpiodglibChip) chip = NULL;
168 g_autoptr(GpiodglibLineInfo) info = NULL;
169 g_autoptr(GPtrArray) events = NULL;
170 g_autoptr(GThread) thread = NULL;
171 const gchar *chip_path = g_gpiosim_chip_get_dev_path(sim);
172 GpiodglibInfoEvent *req_ev, *reconf_ev, *rel_ev;
173 guint64 req_ts, reconf_ts, rel_ts;
174 EventContext ev_ctx;
175 RequestContext req_ctx;
176 guint timeout_id;
177
178 events = g_ptr_array_new_full(3, g_object_unref);
179
180 chip = gpiodglib_test_new_chip_or_fail(chip_path);
181 g_signal_connect(chip, "info-event", G_CALLBACK(basic_on_info_event),
182 &ev_ctx);
183 timeout_id = g_timeout_add_seconds(5, on_timeout, &ev_ctx.failed);
184
185 info = gpiodglib_test_chip_watch_line_info_or_fail(chip, 3);
186
187 g_assert_false(gpiodglib_line_info_is_used(info));
188
189 req_ctx.chip_path = chip_path;
190 req_ctx.offset = 3;
191
192 thread = g_thread_new("request-reconfigure-release",
193 request_reconfigure_release_line, &req_ctx);
194 g_thread_ref(thread);
195
196 ev_ctx.done = 0;
197 ev_ctx.failed = FALSE;
198 ev_ctx.events = events;
199
200 while (ev_ctx.done != 3 && !ev_ctx.failed)
201 g_main_context_iteration(NULL, TRUE);
202
203 g_source_remove(timeout_id);
204 g_thread_join(thread);
205
206 req_ev = g_ptr_array_index(events, 0);
207 reconf_ev = g_ptr_array_index(events, 1);
208 rel_ev = g_ptr_array_index(events, 2);
209
210 g_assert_cmpint(gpiodglib_info_event_get_event_type(req_ev), ==,
211 GPIODGLIB_INFO_EVENT_LINE_REQUESTED);
212 g_assert_cmpint(gpiodglib_info_event_get_event_type(reconf_ev), ==,
213 GPIODGLIB_INFO_EVENT_LINE_CONFIG_CHANGED);
214 g_assert_cmpint(gpiodglib_info_event_get_event_type(rel_ev), ==,
215 GPIODGLIB_INFO_EVENT_LINE_RELEASED);
216
217 req_ts = gpiodglib_info_event_get_timestamp_ns(req_ev);
218 reconf_ts = gpiodglib_info_event_get_timestamp_ns(reconf_ev);
219 rel_ts = gpiodglib_info_event_get_timestamp_ns(rel_ev);
220
221 g_assert_cmpuint(req_ts, <, reconf_ts);
222 g_assert_cmpuint(reconf_ts, <, rel_ts);
223 }
224
225 static void unwatch_on_info_event(GpiodglibChip *chip G_GNUC_UNUSED,
226 GpiodglibInfoEvent *event G_GNUC_UNUSED,
227 gpointer data)
228 {
229 gboolean *got_event = data;
230
231 *got_event = TRUE;
232 }
233
234 GPIOD_TEST_CASE(unwatch_and_check_that_no_events_are_generated)
235 {
236 static const guint offset = 3;
237
238 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
239 g_autoptr(GpiodglibChip) chip = NULL;
240 g_autoptr(GpiodglibLineInfo) info = NULL;
241 g_autoptr(GpiodglibLineRequest) request = NULL;
242 g_autoptr(GpiodglibLineSettings) settings = NULL;
243 g_autoptr(GpiodglibLineConfig) config = NULL;
244 g_autoptr(GArray) offsets = NULL;
245 gboolean got_event = FALSE;
246 guint cnt;
247
248 chip = gpiodglib_test_new_chip_or_fail(
249 g_gpiosim_chip_get_dev_path(sim));
250 g_signal_connect(chip, "info-event", G_CALLBACK(unwatch_on_info_event),
251 &got_event);
252
253 offsets = gpiodglib_test_array_from_const(&offset, 1, sizeof(guint));
254 config = gpiodglib_line_config_new();
255 settings = gpiodglib_line_settings_new(NULL);
256
257 gpiodglib_test_line_config_add_line_settings_or_fail(config, offsets,
258 settings);
259
260 info = gpiodglib_test_chip_watch_line_info_or_fail(chip, offset);
261
262 request = gpiodglib_test_chip_request_lines_or_fail(chip, NULL,
263 config);
264
265 for (cnt = 5; cnt && !got_event; cnt--)
266 g_main_context_iteration(NULL, TRUE);
267
268 g_assert_true(got_event);
269
270 gpiodglib_test_chip_unwatch_line_info_or_fail(chip, offset);
271
272 got_event = FALSE;
273 gpiodglib_line_request_release(request);
274
275 for (cnt = 5; cnt && !got_event; cnt--)
276 g_main_context_iteration(NULL, TRUE);
277
278 g_assert_false(got_event);
279 }
280
281 static void check_line_info_on_info_event(GpiodglibChip *chip G_GNUC_UNUSED,
282 GpiodglibInfoEvent *event,
283 gpointer data)
284 {
285 GpiodglibLineInfo **info = data;
286
287 *info = gpiodglib_info_event_get_line_info(event);
288 }
289
290 GPIOD_TEST_CASE(info_event_contains_new_line_info)
291 {
292 static const guint offset = 3;
293
294 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
295 g_autoptr(GpiodglibChip) chip = NULL;
296 g_autoptr(GpiodglibLineInfo) initial_info = NULL;
297 g_autoptr(GpiodglibLineInfo) event_info = NULL;
298 g_autoptr(GpiodglibLineRequest) request = NULL;
299 g_autoptr(GpiodglibLineSettings) settings = NULL;
300 g_autoptr(GpiodglibLineConfig) config = NULL;
301 g_autoptr(GArray) offsets = NULL;
302 guint cnt;
303
304 chip = gpiodglib_test_new_chip_or_fail(
305 g_gpiosim_chip_get_dev_path(sim));
306 g_signal_connect(chip, "info-event",
307 G_CALLBACK(check_line_info_on_info_event),
308 &event_info);
309
310 offsets = gpiodglib_test_array_from_const(&offset, 1, sizeof(guint));
311 config = gpiodglib_line_config_new();
312 settings = gpiodglib_line_settings_new(NULL);
313
314 gpiodglib_test_line_config_add_line_settings_or_fail(config, offsets,
315 settings);
316
317 initial_info = gpiodglib_test_chip_watch_line_info_or_fail(chip,
318 offset);
319
320 request = gpiodglib_test_chip_request_lines_or_fail(chip, NULL,
321 config);
322
323 for (cnt = 5; cnt && !event_info; cnt--)
324 g_main_context_iteration(NULL, TRUE);
325
326 g_assert_nonnull(event_info);
327 }