Debian Codegraph @

tests/tests-edge-event.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski <[email protected]>
3
4 #include <glib.h>
5 #include <gpiod.h>
6 #include <gpiod-test.h>
7 #include <gpiod-test-common.h>
8 #include <gpiosim-glib.h>
9 #include <poll.h>
10
11 #include "helpers.h"
12
13 #define GPIOD_TEST_GROUP "edge-event"
14
15 GPIOD_TEST_CASE(edge_event_buffer_capacity)
16 {
17 g_autoptr(struct_gpiod_edge_event_buffer) buffer = NULL;
18
19 buffer = gpiod_test_create_edge_event_buffer_or_fail(32);
20
21 g_assert_cmpuint(gpiod_edge_event_buffer_get_capacity(buffer), ==, 32);
22 }
23
24 GPIOD_TEST_CASE(edge_event_buffer_max_capacity)
25 {
26 g_autoptr(struct_gpiod_edge_event_buffer) buffer = NULL;
27
28 buffer = gpiod_test_create_edge_event_buffer_or_fail(16 * 64 * 2);
29
30 g_assert_cmpuint(gpiod_edge_event_buffer_get_capacity(buffer),
31 ==, 16 * 64);
32 }
33
34 GPIOD_TEST_CASE(edge_event_wait_timeout)
35 {
36 static const guint offset = 4;
37
38 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
39 g_autoptr(struct_gpiod_chip) chip = NULL;
40 g_autoptr(struct_gpiod_line_config) line_cfg = NULL;
41 g_autoptr(struct_gpiod_line_settings) settings = NULL;
42 g_autoptr(struct_gpiod_line_request) request = NULL;
43 gint ret;
44
45 chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim));
46 settings = gpiod_test_create_line_settings_or_fail();
47 line_cfg = gpiod_test_create_line_config_or_fail();
48
49 gpiod_line_settings_set_edge_detection(settings, GPIOD_LINE_EDGE_BOTH);
50 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
51 settings);
52
53 request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg);
54
55 ret = gpiod_line_request_wait_edge_events(request, 1000000);
56 g_assert_cmpint(ret, ==, 0);
57 }
58
59 GPIOD_TEST_CASE(cannot_request_lines_in_output_mode_with_edge_detection)
60 {
61 static const guint offset = 4;
62
63 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
64 g_autoptr(struct_gpiod_chip) chip = NULL;
65 g_autoptr(struct_gpiod_line_settings) settings = NULL;
66 g_autoptr(struct_gpiod_line_config) line_cfg = NULL;
67 g_autoptr(struct_gpiod_line_request) request = NULL;
68
69 chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim));
70 settings = gpiod_test_create_line_settings_or_fail();
71 line_cfg = gpiod_test_create_line_config_or_fail();
72
73 gpiod_line_settings_set_edge_detection(settings, GPIOD_LINE_EDGE_BOTH);
74 gpiod_line_settings_set_direction(settings,
75 GPIOD_LINE_DIRECTION_OUTPUT);
76
77 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
78 settings);
79
80 request = gpiod_chip_request_lines(chip, NULL, line_cfg);
81 g_assert_null(request);
82 gpiod_test_expect_errno(EINVAL);
83 }
84
85 static gpointer falling_and_rising_edge_events(gpointer data)
86 {
87 GPIOSimChip *sim = data;
88
89 g_usleep(1000);
90
91 g_gpiosim_chip_set_pull(sim, 2, G_GPIOSIM_PULL_UP);
92
93 g_usleep(1000);
94
95 g_gpiosim_chip_set_pull(sim, 2, G_GPIOSIM_PULL_DOWN);
96
97 return NULL;
98 }
99
100 GPIOD_TEST_CASE(read_both_events)
101 {
102 static const guint offset = 2;
103
104 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
105 g_autoptr(struct_gpiod_chip) chip = NULL;
106 g_autoptr(struct_gpiod_line_settings) settings = NULL;
107 g_autoptr(struct_gpiod_line_config) line_cfg = NULL;
108 g_autoptr(struct_gpiod_line_request) request = NULL;
109 g_autoptr(GThread) thread = NULL;
110 g_autoptr(struct_gpiod_edge_event_buffer) buffer = NULL;
111 struct gpiod_edge_event *event;
112 guint64 ts_rising, ts_falling;
113 gint ret;
114
115 chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim));
116 settings = gpiod_test_create_line_settings_or_fail();
117 line_cfg = gpiod_test_create_line_config_or_fail();
118 buffer = gpiod_test_create_edge_event_buffer_or_fail(64);
119
120 gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_INPUT);
121 gpiod_line_settings_set_edge_detection(settings, GPIOD_LINE_EDGE_BOTH);
122
123 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
124 settings);
125
126 request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg);
127
128 thread = g_thread_new("request-release",
129 falling_and_rising_edge_events, sim);
130 g_thread_ref(thread);
131
132 /* First event. */
133
134 ret = gpiod_line_request_wait_edge_events(request, 1000000000);
135 g_assert_cmpint(ret, >, 0);
136 gpiod_test_join_thread_and_return_if_failed(thread);
137
138 ret = gpiod_line_request_read_edge_events(request, buffer, 1);
139 g_assert_cmpint(ret, ==, 1);
140 gpiod_test_join_thread_and_return_if_failed(thread);
141
142 g_assert_cmpuint(gpiod_edge_event_buffer_get_num_events(buffer), ==, 1);
143 event = gpiod_edge_event_buffer_get_event(buffer, 0);
144 g_assert_nonnull(event);
145 gpiod_test_join_thread_and_return_if_failed(thread);
146
147 g_assert_cmpint(gpiod_edge_event_get_event_type(event), ==,
148 GPIOD_EDGE_EVENT_RISING_EDGE);
149 g_assert_cmpuint(gpiod_edge_event_get_line_offset(event), ==, 2);
150 ts_rising = gpiod_edge_event_get_timestamp_ns(event);
151
152 /* Second event. */
153
154 ret = gpiod_line_request_wait_edge_events(request, 1000000000);
155 g_assert_cmpint(ret, >, 0);
156 gpiod_test_join_thread_and_return_if_failed(thread);
157
158 ret = gpiod_line_request_read_edge_events(request, buffer, 1);
159 g_assert_cmpint(ret, ==, 1);
160 gpiod_test_join_thread_and_return_if_failed(thread);
161
162 g_assert_cmpuint(gpiod_edge_event_buffer_get_num_events(buffer), ==, 1);
163 event = gpiod_edge_event_buffer_get_event(buffer, 0);
164 g_assert_nonnull(event);
165 gpiod_test_join_thread_and_return_if_failed(thread);
166
167 g_assert_cmpint(gpiod_edge_event_get_event_type(event), ==,
168 GPIOD_EDGE_EVENT_FALLING_EDGE);
169 g_assert_cmpuint(gpiod_edge_event_get_line_offset(event), ==, 2);
170 ts_falling = gpiod_edge_event_get_timestamp_ns(event);
171
172 g_thread_join(thread);
173
174 g_assert_cmpuint(ts_falling, >, ts_rising);
175 }
176
177 GPIOD_TEST_CASE(read_rising_edge_event)
178 {
179 static const guint offset = 2;
180
181 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
182 g_autoptr(struct_gpiod_chip) chip = NULL;
183 g_autoptr(struct_gpiod_line_settings) settings = NULL;
184 g_autoptr(struct_gpiod_line_config) line_cfg = NULL;
185 g_autoptr(struct_gpiod_line_request) request = NULL;
186 g_autoptr(GThread) thread = NULL;
187 g_autoptr(struct_gpiod_edge_event_buffer) buffer = NULL;
188 struct gpiod_edge_event *event;
189 gint ret;
190
191 chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim));
192 settings = gpiod_test_create_line_settings_or_fail();
193 line_cfg = gpiod_test_create_line_config_or_fail();
194 buffer = gpiod_test_create_edge_event_buffer_or_fail(64);
195
196 gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_INPUT);
197 gpiod_line_settings_set_edge_detection(settings,
198 GPIOD_LINE_EDGE_RISING);
199
200 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
201 settings);
202
203 request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg);
204
205 thread = g_thread_new("edge-generator",
206 falling_and_rising_edge_events, sim);
207 g_thread_ref(thread);
208
209 /* First event. */
210
211 ret = gpiod_line_request_wait_edge_events(request, 1000000000);
212 g_assert_cmpint(ret, >, 0);
213 gpiod_test_join_thread_and_return_if_failed(thread);
214
215 ret = gpiod_line_request_read_edge_events(request, buffer, 1);
216 g_assert_cmpint(ret, ==, 1);
217 gpiod_test_join_thread_and_return_if_failed(thread);
218
219 g_assert_cmpuint(gpiod_edge_event_buffer_get_num_events(buffer), ==, 1);
220 event = gpiod_edge_event_buffer_get_event(buffer, 0);
221 g_assert_nonnull(event);
222 gpiod_test_join_thread_and_return_if_failed(thread);
223
224 g_assert_cmpint(gpiod_edge_event_get_event_type(event), ==,
225 GPIOD_EDGE_EVENT_RISING_EDGE);
226 g_assert_cmpuint(gpiod_edge_event_get_line_offset(event), ==, 2);
227
228 /* Second event. */
229
230 ret = gpiod_line_request_wait_edge_events(request, 1000000);
231 g_assert_cmpint(ret, ==, 0); /* Time-out. */
232
233 g_thread_join(thread);
234 }
235
236 GPIOD_TEST_CASE(read_falling_edge_event)
237 {
238 static const guint offset = 2;
239
240 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
241 g_autoptr(struct_gpiod_chip) chip = NULL;
242 g_autoptr(struct_gpiod_line_settings) settings = NULL;
243 g_autoptr(struct_gpiod_line_config) line_cfg = NULL;
244 g_autoptr(struct_gpiod_line_request) request = NULL;
245 g_autoptr(GThread) thread = NULL;
246 g_autoptr(struct_gpiod_edge_event_buffer) buffer = NULL;
247 struct gpiod_edge_event *event;
248 gint ret;
249
250 chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim));
251 settings = gpiod_test_create_line_settings_or_fail();
252 line_cfg = gpiod_test_create_line_config_or_fail();
253 buffer = gpiod_test_create_edge_event_buffer_or_fail(64);
254
255 gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_INPUT);
256 gpiod_line_settings_set_edge_detection(settings,
257 GPIOD_LINE_EDGE_FALLING);
258
259 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
260 settings);
261
262 request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg);
263
264 thread = g_thread_new("request-release",
265 falling_and_rising_edge_events, sim);
266 g_thread_ref(thread);
267
268 /* First event is the second generated. */
269
270 ret = gpiod_line_request_wait_edge_events(request, 1000000000);
271 g_assert_cmpint(ret, >, 0);
272 gpiod_test_join_thread_and_return_if_failed(thread);
273
274 ret = gpiod_line_request_read_edge_events(request, buffer, 1);
275 g_assert_cmpint(ret, ==, 1);
276 gpiod_test_join_thread_and_return_if_failed(thread);
277
278 g_assert_cmpuint(gpiod_edge_event_buffer_get_num_events(buffer), ==, 1);
279 event = gpiod_edge_event_buffer_get_event(buffer, 0);
280 g_assert_nonnull(event);
281 gpiod_test_join_thread_and_return_if_failed(thread);
282
283 g_assert_cmpint(gpiod_edge_event_get_event_type(event), ==,
284 GPIOD_EDGE_EVENT_FALLING_EDGE);
285 g_assert_cmpuint(gpiod_edge_event_get_line_offset(event), ==, 2);
286
287 /* No more events. */
288
289 ret = gpiod_line_request_wait_edge_events(request, 1000000);
290 g_assert_cmpint(ret, ==, 0); /* Time-out. */
291
292 g_thread_join(thread);
293 }
294
295 GPIOD_TEST_CASE(read_rising_edge_event_polled)
296 {
297 static const guint offset = 2;
298
299 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
300 g_autoptr(struct_gpiod_chip) chip = NULL;
301 g_autoptr(struct_gpiod_line_settings) settings = NULL;
302 g_autoptr(struct_gpiod_line_config) line_cfg = NULL;
303 g_autoptr(struct_gpiod_line_request) request = NULL;
304 g_autoptr(GThread) thread = NULL;
305 g_autoptr(struct_gpiod_edge_event_buffer) buffer = NULL;
306 struct gpiod_edge_event *event;
307 struct timespec ts;
308 struct pollfd pfd;
309 gint ret, fd;
310
311 chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim));
312 settings = gpiod_test_create_line_settings_or_fail();
313 line_cfg = gpiod_test_create_line_config_or_fail();
314 buffer = gpiod_test_create_edge_event_buffer_or_fail(64);
315
316 gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_INPUT);
317 gpiod_line_settings_set_edge_detection(settings,
318 GPIOD_LINE_EDGE_RISING);
319
320 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
321 settings);
322
323 request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg);
324
325 thread = g_thread_new("edge-generator",
326 falling_and_rising_edge_events, sim);
327 g_thread_ref(thread);
328
329 /* First event. */
330
331 fd = gpiod_line_request_get_fd(request);
332
333 memset(&pfd, 0, sizeof(pfd));
334 pfd.fd = fd;
335 pfd.events = POLLIN | POLLPRI;
336
337 ts.tv_sec = 1;
338 ts.tv_nsec = 0;
339
340 ret = ppoll(&pfd, 1, &ts, NULL);
341 g_assert_cmpint(ret, >, 0);
342 gpiod_test_join_thread_and_return_if_failed(thread);
343
344 ret = gpiod_line_request_read_edge_events(request, buffer, 1);
345 g_assert_cmpint(ret, ==, 1);
346 gpiod_test_join_thread_and_return_if_failed(thread);
347
348 g_assert_cmpuint(gpiod_edge_event_buffer_get_num_events(buffer), ==, 1);
349 event = gpiod_edge_event_buffer_get_event(buffer, 0);
350 g_assert_nonnull(event);
351 gpiod_test_join_thread_and_return_if_failed(thread);
352
353 g_assert_cmpint(gpiod_edge_event_get_event_type(event), ==,
354 GPIOD_EDGE_EVENT_RISING_EDGE);
355 g_assert_cmpuint(gpiod_edge_event_get_line_offset(event), ==, 2);
356
357 /* Second event. */
358
359 ret = gpiod_line_request_wait_edge_events(request, 1000000);
360 g_assert_cmpint(ret, ==, 0); /* Time-out. */
361
362 g_thread_join(thread);
363 }
364
365 GPIOD_TEST_CASE(read_both_events_blocking)
366 {
367 /*
368 * This time without polling so that the read gets a chance to block
369 * and we can make sure it doesn't immediately return an error.
370 */
371
372 static const guint offset = 2;
373
374 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
375 g_autoptr(struct_gpiod_chip) chip = NULL;
376 g_autoptr(struct_gpiod_line_settings) settings = NULL;
377 g_autoptr(struct_gpiod_line_config) line_cfg = NULL;
378 g_autoptr(struct_gpiod_line_request) request = NULL;
379 g_autoptr(GThread) thread = NULL;
380 g_autoptr(struct_gpiod_edge_event_buffer) buffer = NULL;
381 struct gpiod_edge_event *event;
382 gint ret;
383
384 chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim));
385 settings = gpiod_test_create_line_settings_or_fail();
386 line_cfg = gpiod_test_create_line_config_or_fail();
387 buffer = gpiod_test_create_edge_event_buffer_or_fail(64);
388
389 gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_INPUT);
390 gpiod_line_settings_set_edge_detection(settings, GPIOD_LINE_EDGE_BOTH);
391
392 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
393 settings);
394
395 request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg);
396
397 thread = g_thread_new("request-release",
398 falling_and_rising_edge_events, sim);
399 g_thread_ref(thread);
400
401 /* First event. */
402
403 ret = gpiod_line_request_read_edge_events(request, buffer, 1);
404 g_assert_cmpint(ret, ==, 1);
405 gpiod_test_join_thread_and_return_if_failed(thread);
406
407 g_assert_cmpuint(gpiod_edge_event_buffer_get_num_events(buffer), ==, 1);
408 event = gpiod_edge_event_buffer_get_event(buffer, 0);
409 g_assert_nonnull(event);
410 gpiod_test_join_thread_and_return_if_failed(thread);
411
412 g_assert_cmpint(gpiod_edge_event_get_event_type(event), ==,
413 GPIOD_EDGE_EVENT_RISING_EDGE);
414 g_assert_cmpuint(gpiod_edge_event_get_line_offset(event), ==, 2);
415
416 /* Second event. */
417
418 ret = gpiod_line_request_read_edge_events(request, buffer, 1);
419 g_assert_cmpint(ret, ==, 1);
420 gpiod_test_join_thread_and_return_if_failed(thread);
421
422 g_assert_cmpuint(gpiod_edge_event_buffer_get_num_events(buffer), ==, 1);
423 event = gpiod_edge_event_buffer_get_event(buffer, 0);
424 g_assert_nonnull(event);
425 gpiod_test_join_thread_and_return_if_failed(thread);
426
427 g_assert_cmpint(gpiod_edge_event_get_event_type(event), ==,
428 GPIOD_EDGE_EVENT_FALLING_EDGE);
429 g_assert_cmpuint(gpiod_edge_event_get_line_offset(event), ==, 2);
430
431 g_thread_join(thread);
432 }
433
434 static gpointer rising_edge_events_on_two_offsets(gpointer data)
435 {
436 GPIOSimChip *sim = data;
437
438 g_usleep(1000);
439
440 g_gpiosim_chip_set_pull(sim, 2, G_GPIOSIM_PULL_UP);
441
442 g_usleep(1000);
443
444 g_gpiosim_chip_set_pull(sim, 3, G_GPIOSIM_PULL_UP);
445
446 return NULL;
447 }
448
449 GPIOD_TEST_CASE(seqno)
450 {
451 static const guint offsets[] = { 2, 3 };
452
453 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
454 g_autoptr(struct_gpiod_chip) chip = NULL;
455 g_autoptr(struct_gpiod_line_settings) settings = NULL;
456 g_autoptr(struct_gpiod_line_config) line_cfg = NULL;
457 g_autoptr(struct_gpiod_line_request) request = NULL;
458 g_autoptr(GThread) thread = NULL;
459 g_autoptr(struct_gpiod_edge_event_buffer) buffer = NULL;
460 struct gpiod_edge_event *event;
461 gint ret;
462
463 chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim));
464 settings = gpiod_test_create_line_settings_or_fail();
465 line_cfg = gpiod_test_create_line_config_or_fail();
466 buffer = gpiod_test_create_edge_event_buffer_or_fail(64);
467
468 gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_INPUT);
469 gpiod_line_settings_set_edge_detection(settings, GPIOD_LINE_EDGE_BOTH);
470
471 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, offsets, 2,
472 settings);
473
474 request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg);
475
476 thread = g_thread_new("request-release",
477 rising_edge_events_on_two_offsets, sim);
478 g_thread_ref(thread);
479
480 /* First event. */
481
482 ret = gpiod_line_request_wait_edge_events(request, 1000000000);
483 g_assert_cmpint(ret, >, 0);
484 gpiod_test_join_thread_and_return_if_failed(thread);
485
486 ret = gpiod_line_request_read_edge_events(request, buffer, 1);
487 g_assert_cmpint(ret, ==, 1);
488 gpiod_test_join_thread_and_return_if_failed(thread);
489
490 g_assert_cmpuint(gpiod_edge_event_buffer_get_num_events(buffer), ==, 1);
491 event = gpiod_edge_event_buffer_get_event(buffer, 0);
492 g_assert_nonnull(event);
493 gpiod_test_join_thread_and_return_if_failed(thread);
494
495 g_assert_cmpuint(gpiod_edge_event_get_line_offset(event), ==, 2);
496 g_assert_cmpuint(gpiod_edge_event_get_global_seqno(event), ==, 1);
497 g_assert_cmpuint(gpiod_edge_event_get_line_seqno(event), ==, 1);
498
499 /* Second event. */
500
501 ret = gpiod_line_request_wait_edge_events(request, 1000000000);
502 g_assert_cmpint(ret, >, 0);
503 gpiod_test_join_thread_and_return_if_failed(thread);
504
505 ret = gpiod_line_request_read_edge_events(request, buffer, 1);
506 g_assert_cmpint(ret, ==, 1);
507 gpiod_test_join_thread_and_return_if_failed(thread);
508
509 g_assert_cmpuint(gpiod_edge_event_buffer_get_num_events(buffer), ==, 1);
510 event = gpiod_edge_event_buffer_get_event(buffer, 0);
511 g_assert_nonnull(event);
512 gpiod_test_join_thread_and_return_if_failed(thread);
513
514 g_assert_cmpuint(gpiod_edge_event_get_line_offset(event), ==, 3);
515 g_assert_cmpuint(gpiod_edge_event_get_global_seqno(event), ==, 2);
516 g_assert_cmpuint(gpiod_edge_event_get_line_seqno(event), ==, 1);
517
518 g_thread_join(thread);
519 }
520
521 GPIOD_TEST_CASE(event_copy)
522 {
523 static const guint offset = 2;
524
525 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
526 g_autoptr(struct_gpiod_chip) chip = NULL;
527 g_autoptr(struct_gpiod_line_settings) settings = NULL;
528 g_autoptr(struct_gpiod_line_config) line_cfg = NULL;
529 g_autoptr(struct_gpiod_line_request) request = NULL;
530 g_autoptr(struct_gpiod_edge_event_buffer) buffer = NULL;
531 g_autoptr(struct_gpiod_edge_event) copy = NULL;
532 struct gpiod_edge_event *event;
533 gint ret;
534
535 chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim));
536 settings = gpiod_test_create_line_settings_or_fail();
537 line_cfg = gpiod_test_create_line_config_or_fail();
538 buffer = gpiod_test_create_edge_event_buffer_or_fail(64);
539
540 gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_INPUT);
541 gpiod_line_settings_set_edge_detection(settings, GPIOD_LINE_EDGE_BOTH);
542
543 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
544 settings);
545
546 request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg);
547
548 g_gpiosim_chip_set_pull(sim, 2, G_GPIOSIM_PULL_UP);
549
550 ret = gpiod_line_request_wait_edge_events(request, 1000000000);
551 g_assert_cmpint(ret, >, 0);
552 gpiod_test_return_if_failed();
553
554 ret = gpiod_line_request_read_edge_events(request, buffer, 1);
555 g_assert_cmpint(ret, ==, 1);
556 gpiod_test_return_if_failed();
557
558 event = gpiod_edge_event_buffer_get_event(buffer, 0);
559 g_assert_nonnull(event);
560 gpiod_test_return_if_failed();
561
562 copy = gpiod_edge_event_copy(event);
563 g_assert_nonnull(copy);
564 g_assert_true(copy != event);
565 }
566
567 GPIOD_TEST_CASE(reading_more_events_than_the_queue_contains_doesnt_block)
568 {
569 static const guint offset = 2;
570
571 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
572 g_autoptr(struct_gpiod_chip) chip = NULL;
573 g_autoptr(struct_gpiod_line_settings) settings = NULL;
574 g_autoptr(struct_gpiod_line_config) line_cfg = NULL;
575 g_autoptr(struct_gpiod_line_request) request = NULL;
576 g_autoptr(struct_gpiod_edge_event_buffer) buffer = NULL;
577 gint ret;
578
579 chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim));
580 settings = gpiod_test_create_line_settings_or_fail();
581 line_cfg = gpiod_test_create_line_config_or_fail();
582 buffer = gpiod_test_create_edge_event_buffer_or_fail(64);
583
584 gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_INPUT);
585 gpiod_line_settings_set_edge_detection(settings, GPIOD_LINE_EDGE_BOTH);
586
587 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
588 settings);
589
590 request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg);
591
592 g_gpiosim_chip_set_pull(sim, 2, G_GPIOSIM_PULL_UP);
593 g_usleep(500);
594 g_gpiosim_chip_set_pull(sim, 2, G_GPIOSIM_PULL_DOWN);
595 g_usleep(500);
596 g_gpiosim_chip_set_pull(sim, 2, G_GPIOSIM_PULL_UP);
597 g_usleep(500);
598 g_gpiosim_chip_set_pull(sim, 2, G_GPIOSIM_PULL_DOWN);
599 g_usleep(500);
600 g_gpiosim_chip_set_pull(sim, 2, G_GPIOSIM_PULL_UP);
601 g_usleep(500);
602 g_gpiosim_chip_set_pull(sim, 2, G_GPIOSIM_PULL_DOWN);
603 g_usleep(500);
604 g_gpiosim_chip_set_pull(sim, 2, G_GPIOSIM_PULL_UP);
605 g_usleep(500);
606
607 ret = gpiod_line_request_read_edge_events(request, buffer, 12);
608 g_assert_cmpint(ret, ==, 7);
609 gpiod_test_return_if_failed();
610
611 ret = gpiod_line_request_wait_edge_events(request, 1000);
612 g_assert_cmpint(ret, ==, 0);
613 gpiod_test_return_if_failed();
614 }
615
616 GPIOD_TEST_CASE(reading_zero_events_is_a_noop)
617 {
618 static const guint offset = 2;
619
620 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
621 g_autoptr(struct_gpiod_chip) chip = NULL;
622 g_autoptr(struct_gpiod_line_settings) settings = NULL;
623 g_autoptr(struct_gpiod_line_config) line_cfg = NULL;
624 g_autoptr(struct_gpiod_line_request) request = NULL;
625 g_autoptr(struct_gpiod_edge_event_buffer) buffer = NULL;
626 gint ret;
627
628 chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim));
629 settings = gpiod_test_create_line_settings_or_fail();
630 line_cfg = gpiod_test_create_line_config_or_fail();
631 buffer = gpiod_test_create_edge_event_buffer_or_fail(64);
632
633 gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_INPUT);
634 gpiod_line_settings_set_edge_detection(settings, GPIOD_LINE_EDGE_BOTH);
635
636 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
637 settings);
638
639 request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg);
640
641 g_gpiosim_chip_set_pull(sim, 2, G_GPIOSIM_PULL_UP);
642 g_usleep(500);
643
644 ret = gpiod_line_request_read_edge_events(request, buffer, 0);
645 g_assert_cmpint(ret, ==, 0);
646 gpiod_test_return_if_failed();
647
648 g_assert_cmpuint(gpiod_edge_event_buffer_get_num_events(buffer), ==, 0);
649 }
650
651 GPIOD_TEST_CASE(null_buffer)
652 {
653 static const guint offset = 2;
654
655 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
656 g_autoptr(struct_gpiod_chip) chip = NULL;
657 g_autoptr(struct_gpiod_line_settings) settings = NULL;
658 g_autoptr(struct_gpiod_line_config) line_cfg = NULL;
659 g_autoptr(struct_gpiod_line_request) request = NULL;
660 gint ret;
661
662 chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim));
663 settings = gpiod_test_create_line_settings_or_fail();
664 line_cfg = gpiod_test_create_line_config_or_fail();
665
666 gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_INPUT);
667 gpiod_line_settings_set_edge_detection(settings, GPIOD_LINE_EDGE_BOTH);
668
669 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
670 settings);
671
672 request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg);
673
674 ret = gpiod_line_request_read_edge_events(request, NULL, 1);
675 g_assert_cmpint(ret, ==, -1);
676 gpiod_test_expect_errno(EINVAL);
677 }
678
679 GPIOD_TEST_CASE(get_edge_event_index_out_of_bounds)
680 {
681 static const guint offset = 2;
682
683 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
684 g_autoptr(struct_gpiod_chip) chip = NULL;
685 g_autoptr(struct_gpiod_line_settings) settings = NULL;
686 g_autoptr(struct_gpiod_line_config) line_cfg = NULL;
687 g_autoptr(struct_gpiod_line_request) request = NULL;
688 g_autoptr(struct_gpiod_edge_event_buffer) buffer = NULL;
689 struct gpiod_edge_event *event;
690 gint ret;
691
692 chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim));
693 settings = gpiod_test_create_line_settings_or_fail();
694 line_cfg = gpiod_test_create_line_config_or_fail();
695 buffer = gpiod_test_create_edge_event_buffer_or_fail(64);
696
697 gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_INPUT);
698 gpiod_line_settings_set_edge_detection(settings, GPIOD_LINE_EDGE_BOTH);
699
700 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
701 settings);
702
703 request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg);
704
705 g_gpiosim_chip_set_pull(sim, 2, G_GPIOSIM_PULL_UP);
706 g_usleep(500);
707 g_gpiosim_chip_set_pull(sim, 2, G_GPIOSIM_PULL_DOWN);
708 g_usleep(500);
709 g_gpiosim_chip_set_pull(sim, 2, G_GPIOSIM_PULL_UP);
710 g_usleep(500);
711
712 ret = gpiod_line_request_read_edge_events(request, buffer, 3);
713 g_assert_cmpint(ret, ==, 3);
714 gpiod_test_return_if_failed();
715
716 event = gpiod_edge_event_buffer_get_event(buffer, 5);
717 g_assert_null(event);
718 gpiod_test_expect_errno(EINVAL);
719 }