Debian Codegraph @

tests/tests-info-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 "info-event"
14
15 GPIOD_TEST_CASE(watching_info_events_returns_line_info)
16 {
17 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
18 g_autoptr(struct_gpiod_chip) chip = NULL;
19 g_autoptr(struct_gpiod_line_info) info = NULL;
20
21 chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim));
22 info = gpiod_test_chip_watch_line_info_or_fail(chip, 3);
23 g_assert_cmpuint(gpiod_line_info_get_offset(info), ==, 3);
24 }
25
26 GPIOD_TEST_CASE(try_offset_out_of_range)
27 {
28 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
29 g_autoptr(struct_gpiod_chip) chip = NULL;
30 g_autoptr(struct_gpiod_line_info) info = NULL;
31
32 chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim));
33 info = gpiod_chip_watch_line_info(chip, 10);
34 g_assert_null(info);
35 gpiod_test_expect_errno(EINVAL);
36 }
37
38 GPIOD_TEST_CASE(event_timeout)
39 {
40 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
41 g_autoptr(struct_gpiod_chip) chip = NULL;
42 g_autoptr(struct_gpiod_line_info) info = NULL;
43 gint ret;
44
45 chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim));
46 info = gpiod_test_chip_watch_line_info_or_fail(chip, 6);
47
48 ret = gpiod_chip_wait_info_event(chip, 100000000);
49 g_assert_cmpint(ret, ==, 0);
50 }
51
52 struct request_ctx {
53 const char *path;
54 guint offset;
55 };
56
57 static gpointer request_reconfigure_release_line(gpointer data)
58 {
59 g_autoptr(struct_gpiod_line_settings) settings = NULL;
60 g_autoptr(struct_gpiod_line_config) line_cfg = NULL;
61 g_autoptr(struct_gpiod_line_request) request = NULL;
62 g_autoptr(struct_gpiod_chip) chip = NULL;
63 struct request_ctx *ctx = data;
64 gint ret;
65
66 chip = gpiod_chip_open(ctx->path);
67 g_assert_nonnull(chip);
68 if (g_test_failed())
69 return NULL;
70
71 line_cfg = gpiod_line_config_new();
72 g_assert_nonnull(line_cfg);
73 if (g_test_failed())
74 return NULL;
75
76 settings = gpiod_line_settings_new();
77 g_assert_nonnull(settings);
78 if (g_test_failed())
79 return NULL;
80
81 g_usleep(1000);
82
83 ret = gpiod_line_config_add_line_settings(line_cfg, &ctx->offset,
84 1, settings);
85 g_assert_cmpint(ret, ==, 0);
86 if (g_test_failed())
87 return NULL;
88
89 request = gpiod_chip_request_lines(chip, NULL, line_cfg);
90 g_assert_nonnull(request);
91 if (g_test_failed())
92 return NULL;
93
94 g_usleep(1000);
95
96 gpiod_line_config_reset(line_cfg);
97 gpiod_line_settings_set_direction(settings,
98 GPIOD_LINE_DIRECTION_OUTPUT);
99 ret = gpiod_line_config_add_line_settings(line_cfg, &ctx->offset,
100 1, settings);
101 g_assert_cmpint(ret, ==, 0);
102 if (g_test_failed())
103 return NULL;
104
105 ret = gpiod_line_request_reconfigure_lines(request, line_cfg);
106 g_assert_cmpint(ret, ==, 0);
107 if (g_test_failed())
108 return NULL;
109
110 g_usleep(1000);
111
112 gpiod_line_request_release(request);
113 request = NULL;
114
115 return NULL;
116 }
117
118 GPIOD_TEST_CASE(request_reconfigure_release_events)
119 {
120 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
121 g_autoptr(struct_gpiod_chip) chip = NULL;
122 g_autoptr(struct_gpiod_line_info) info = NULL;
123 g_autoptr(struct_gpiod_info_event) request_event = NULL;
124 g_autoptr(struct_gpiod_info_event) reconfigure_event = NULL;
125 g_autoptr(struct_gpiod_info_event) release_event = NULL;
126 g_autoptr(GThread) thread = NULL;
127 struct gpiod_line_info *request_info, *reconfigure_info, *release_info;
128 guint64 request_ts, reconfigure_ts, release_ts;
129 struct request_ctx ctx;
130 const char *chip_path = g_gpiosim_chip_get_dev_path(sim);
131 gint ret;
132
133 chip = gpiod_test_open_chip_or_fail(chip_path);
134 info = gpiod_test_chip_watch_line_info_or_fail(chip, 3);
135
136 g_assert_false(gpiod_line_info_is_used(info));
137
138 ctx.path = chip_path;
139 ctx.offset = 3;
140
141 thread = g_thread_new("request-release",
142 request_reconfigure_release_line, &ctx);
143 g_thread_ref(thread);
144
145 ret = gpiod_chip_wait_info_event(chip, 1000000000);
146 g_assert_cmpint(ret, >, 0);
147 gpiod_test_join_thread_and_return_if_failed(thread);
148
149 request_event = gpiod_chip_read_info_event(chip);
150 g_assert_nonnull(request_event);
151 gpiod_test_join_thread_and_return_if_failed(thread);
152
153 g_assert_cmpint(gpiod_info_event_get_event_type(request_event), ==,
154 GPIOD_INFO_EVENT_LINE_REQUESTED);
155
156 request_info = gpiod_info_event_get_line_info(request_event);
157
158 g_assert_cmpuint(gpiod_line_info_get_offset(request_info), ==, 3);
159 g_assert_true(gpiod_line_info_is_used(request_info));
160 g_assert_cmpint(gpiod_line_info_get_direction(request_info), ==,
161 GPIOD_LINE_DIRECTION_INPUT);
162
163 ret = gpiod_chip_wait_info_event(chip, 1000000000);
164 g_assert_cmpint(ret, >, 0);
165 gpiod_test_join_thread_and_return_if_failed(thread);
166
167 reconfigure_event = gpiod_chip_read_info_event(chip);
168 g_assert_nonnull(reconfigure_event);
169 gpiod_test_join_thread_and_return_if_failed(thread);
170
171 g_assert_cmpint(gpiod_info_event_get_event_type(reconfigure_event), ==,
172 GPIOD_INFO_EVENT_LINE_CONFIG_CHANGED);
173
174 reconfigure_info = gpiod_info_event_get_line_info(reconfigure_event);
175
176 g_assert_cmpuint(gpiod_line_info_get_offset(reconfigure_info), ==, 3);
177 g_assert_true(gpiod_line_info_is_used(reconfigure_info));
178 g_assert_cmpint(gpiod_line_info_get_direction(reconfigure_info), ==,
179 GPIOD_LINE_DIRECTION_OUTPUT);
180
181 ret = gpiod_chip_wait_info_event(chip, 1000000000);
182 g_assert_cmpint(ret, >, 0);
183 gpiod_test_join_thread_and_return_if_failed(thread);
184
185 release_event = gpiod_chip_read_info_event(chip);
186 g_assert_nonnull(release_event);
187 gpiod_test_join_thread_and_return_if_failed(thread);
188
189 g_assert_cmpint(gpiod_info_event_get_event_type(release_event), ==,
190 GPIOD_INFO_EVENT_LINE_RELEASED);
191
192 release_info = gpiod_info_event_get_line_info(release_event);
193
194 g_assert_cmpuint(gpiod_line_info_get_offset(release_info), ==, 3);
195 g_assert_false(gpiod_line_info_is_used(release_info));
196
197 g_thread_join(thread);
198
199 request_ts = gpiod_info_event_get_timestamp_ns(request_event);
200 reconfigure_ts = gpiod_info_event_get_timestamp_ns(reconfigure_event);
201 release_ts = gpiod_info_event_get_timestamp_ns(release_event);
202
203 g_assert_cmpuint(request_ts, <, reconfigure_ts);
204 g_assert_cmpuint(reconfigure_ts, <, release_ts);
205 }
206
207 GPIOD_TEST_CASE(chip_fd_can_be_polled)
208 {
209 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
210 g_autoptr(struct_gpiod_chip) chip = NULL;
211 g_autoptr(struct_gpiod_line_info) info = NULL;
212 g_autoptr(struct_gpiod_info_event) event = NULL;
213 g_autoptr(GThread) thread = NULL;
214 const char *chip_path = g_gpiosim_chip_get_dev_path(sim);
215 struct gpiod_line_info *evinfo;
216 struct request_ctx ctx;
217 struct timespec ts;
218 struct pollfd pfd;
219 gint ret, fd;
220
221 chip = gpiod_test_open_chip_or_fail(chip_path);
222 info = gpiod_test_chip_watch_line_info_or_fail(chip, 3);
223
224 g_assert_false(gpiod_line_info_is_used(info));
225
226 ctx.path = chip_path;
227 ctx.offset = 3;
228
229 thread = g_thread_new("request-release",
230 request_reconfigure_release_line, &ctx);
231 g_thread_ref(thread);
232
233 fd = gpiod_chip_get_fd(chip);
234
235 memset(&pfd, 0, sizeof(pfd));
236 pfd.fd = fd;
237 pfd.events = POLLIN | POLLPRI;
238
239 ts.tv_sec = 1;
240 ts.tv_nsec = 0;
241
242 ret = ppoll(&pfd, 1, &ts, NULL);
243 g_assert_cmpint(ret, >, 0);
244 gpiod_test_join_thread_and_return_if_failed(thread);
245
246 event = gpiod_chip_read_info_event(chip);
247 g_assert_nonnull(event);
248 gpiod_test_join_thread_and_return_if_failed(thread);
249
250 g_assert_cmpint(gpiod_info_event_get_event_type(event), ==,
251 GPIOD_INFO_EVENT_LINE_REQUESTED);
252
253 evinfo = gpiod_info_event_get_line_info(event);
254
255 g_assert_cmpuint(gpiod_line_info_get_offset(evinfo), ==, 3);
256 g_assert_true(gpiod_line_info_is_used(evinfo));
257
258 g_thread_join(thread);
259 }
260
261 GPIOD_TEST_CASE(unwatch_and_check_that_no_events_are_generated)
262 {
263 static const guint offset = 3;
264
265 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
266 g_autoptr(struct_gpiod_chip) chip = NULL;
267 g_autoptr(struct_gpiod_line_info) info = NULL;
268 g_autoptr(struct_gpiod_info_event) event = NULL;
269 g_autoptr(struct_gpiod_line_config) line_cfg = NULL;
270 g_autoptr(struct_gpiod_line_request) request = NULL;
271 gint ret;
272
273 chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim));
274 line_cfg = gpiod_test_create_line_config_or_fail();
275
276 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
277 NULL);
278
279 info = gpiod_test_chip_watch_line_info_or_fail(chip, 3);
280
281 request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg);
282
283 ret = gpiod_chip_wait_info_event(chip, 100000000);
284 g_assert_cmpint(ret, >, 0);
285 gpiod_test_return_if_failed();
286
287 event = gpiod_chip_read_info_event(chip);
288 g_assert_nonnull(event);
289 gpiod_test_return_if_failed();
290
291 ret = gpiod_chip_unwatch_line_info(chip, 3);
292 g_assert_cmpint(ret, ==, 0);
293 gpiod_test_return_if_failed();
294
295 gpiod_line_request_release(request);
296 request = NULL;
297
298 ret = gpiod_chip_wait_info_event(chip, 100000000);
299 g_assert_cmpint(ret, ==, 0);
300 }