Debian Codegraph @

tests/tests-line-info.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 <errno.h>
5 #include <glib.h>
6 #include <gpiod.h>
7 #include <gpiod-test.h>
8 #include <gpiod-test-common.h>
9 #include <gpiosim-glib.h>
10
11 #include "helpers.h"
12
13 #define GPIOD_TEST_GROUP "line-info"
14
15 GPIOD_TEST_CASE(get_line_info_good)
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
23 info = gpiod_test_chip_get_line_info_or_fail(chip, 3);
24 g_assert_cmpuint(gpiod_line_info_get_offset(info), ==, 3);
25 }
26
27 GPIOD_TEST_CASE(get_line_info_offset_out_of_range)
28 {
29 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
30 g_autoptr(struct_gpiod_chip) chip = NULL;
31 g_autoptr(struct_gpiod_line_info) info = NULL;
32
33 chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim));
34
35 info = gpiod_chip_get_line_info(chip, 8);
36 g_assert_null(info);
37 gpiod_test_expect_errno(EINVAL);
38 }
39
40 GPIOD_TEST_CASE(line_info_basic_properties)
41 {
42 static const GPIOSimLineName names[] = {
43 { .offset = 1, .name = "foo", },
44 { .offset = 2, .name = "bar", },
45 { .offset = 4, .name = "baz", },
46 { .offset = 5, .name = "xyz", },
47 { }
48 };
49
50 static const GPIOSimHog hogs[] = {
51 {
52 .offset = 3,
53 .name = "hog3",
54 .direction = G_GPIOSIM_DIRECTION_OUTPUT_HIGH,
55 },
56 {
57 .offset = 4,
58 .name = "hog4",
59 .direction = G_GPIOSIM_DIRECTION_OUTPUT_LOW,
60 },
61 { }
62 };
63
64 g_autoptr(GPIOSimChip) sim = NULL;
65 g_autoptr(struct_gpiod_chip) chip = NULL;
66 g_autoptr(struct_gpiod_line_info) info4 = NULL;
67 g_autoptr(struct_gpiod_line_info) info6 = NULL;
68 g_autoptr(GVariant) vnames = g_gpiosim_package_line_names(names);
69 g_autoptr(GVariant) vhogs = g_gpiosim_package_hogs(hogs);
70
71 sim = g_gpiosim_chip_new(
72 "num-lines", 8,
73 "line-names", vnames,
74 "hogs", vhogs,
75 NULL);
76
77 chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim));
78 info4 = gpiod_test_chip_get_line_info_or_fail(chip, 4);
79 info6 = gpiod_test_chip_get_line_info_or_fail(chip, 6);
80
81 g_assert_cmpuint(gpiod_line_info_get_offset(info4), ==, 4);
82 g_assert_cmpstr(gpiod_line_info_get_name(info4), ==, "baz");
83 g_assert_cmpstr(gpiod_line_info_get_consumer(info4), ==, "hog4");
84 g_assert_true(gpiod_line_info_is_used(info4));
85 g_assert_cmpint(gpiod_line_info_get_direction(info4), ==,
86 GPIOD_LINE_DIRECTION_OUTPUT);
87 g_assert_cmpint(gpiod_line_info_get_edge_detection(info4), ==,
88 GPIOD_LINE_EDGE_NONE);
89 g_assert_false(gpiod_line_info_is_active_low(info4));
90 g_assert_cmpint(gpiod_line_info_get_bias(info4), ==,
91 GPIOD_LINE_BIAS_UNKNOWN);
92 g_assert_cmpint(gpiod_line_info_get_drive(info4), ==,
93 GPIOD_LINE_DRIVE_PUSH_PULL);
94 g_assert_cmpint(gpiod_line_info_get_event_clock(info4), ==,
95 GPIOD_LINE_CLOCK_MONOTONIC);
96 g_assert_false(gpiod_line_info_is_debounced(info4));
97 g_assert_cmpuint(gpiod_line_info_get_debounce_period_us(info4), ==, 0);
98 }
99
100 GPIOD_TEST_CASE(copy_line_info)
101 {
102 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
103 g_autoptr(struct_gpiod_chip) chip = NULL;
104 g_autoptr(struct_gpiod_line_info) info = NULL;
105 g_autoptr(struct_gpiod_line_info) copy = NULL;
106
107 chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim));
108 info = gpiod_test_chip_get_line_info_or_fail(chip, 3);
109
110 copy = gpiod_line_info_copy(info);
111 g_assert_nonnull(copy);
112 g_assert_true(info != copy);
113 g_assert_cmpuint(gpiod_line_info_get_offset(info), ==,
114 gpiod_line_info_get_offset(copy));
115 }
116
117 GPIOD_TEST_CASE(direction_settings)
118 {
119 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
120 g_autoptr(struct_gpiod_chip) chip = NULL;
121 g_autoptr(struct_gpiod_line_config) line_cfg = NULL;
122 g_autoptr(struct_gpiod_line_settings) settings = NULL;
123 g_autoptr(struct_gpiod_line_request) request = NULL;
124 g_autoptr(struct_gpiod_line_info) info0 = NULL;
125 g_autoptr(struct_gpiod_line_info) info1 = NULL;
126 g_autoptr(struct_gpiod_line_info) info2 = NULL;
127 guint offset;
128
129 chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim));
130 settings = gpiod_test_create_line_settings_or_fail();
131 line_cfg = gpiod_test_create_line_config_or_fail();
132
133 gpiod_line_settings_set_direction(settings,
134 GPIOD_LINE_DIRECTION_OUTPUT);
135 offset = 0;
136 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
137 settings);
138 gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_INPUT);
139 offset = 1;
140 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
141 settings);
142 gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_AS_IS);
143 offset = 2;
144 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
145 settings);
146
147 request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg);
148 info0 = gpiod_test_chip_get_line_info_or_fail(chip, 0);
149 info1 = gpiod_test_chip_get_line_info_or_fail(chip, 1);
150 info2 = gpiod_test_chip_get_line_info_or_fail(chip, 2);
151
152 g_assert_cmpint(gpiod_line_info_get_direction(info0), ==,
153 GPIOD_LINE_DIRECTION_OUTPUT);
154 g_assert_cmpint(gpiod_line_info_get_direction(info1), ==,
155 GPIOD_LINE_DIRECTION_INPUT);
156 g_assert_cmpint(gpiod_line_info_get_direction(info2), ==,
157 GPIOD_LINE_DIRECTION_INPUT);
158 }
159
160 GPIOD_TEST_CASE(active_high)
161 {
162 static const guint offset = 5;
163
164 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
165 g_autoptr(struct_gpiod_chip) chip = NULL;
166 g_autoptr(struct_gpiod_line_config) line_cfg = NULL;
167 g_autoptr(struct_gpiod_line_settings) settings = NULL;
168 g_autoptr(struct_gpiod_line_request) request = NULL;
169 g_autoptr(struct_gpiod_line_info) info = NULL;
170
171 chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim));
172 settings = gpiod_test_create_line_settings_or_fail();
173 line_cfg = gpiod_test_create_line_config_or_fail();
174
175 gpiod_line_settings_set_active_low(settings, true);
176 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset,
177 1, settings);
178
179 request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg);
180 info = gpiod_test_chip_get_line_info_or_fail(chip, 5);
181
182 g_assert_true(gpiod_line_info_is_active_low(info));
183 }
184
185 GPIOD_TEST_CASE(edge_detection_settings)
186 {
187 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
188 g_autoptr(struct_gpiod_chip) chip = NULL;
189 g_autoptr(struct_gpiod_line_config) line_cfg = NULL;
190 g_autoptr(struct_gpiod_line_request) request = NULL;
191 g_autoptr(struct_gpiod_line_settings) settings = NULL;
192 g_autoptr(struct_gpiod_line_info) info0 = NULL;
193 g_autoptr(struct_gpiod_line_info) info1 = NULL;
194 g_autoptr(struct_gpiod_line_info) info2 = NULL;
195 g_autoptr(struct_gpiod_line_info) info3 = NULL;
196 guint offset;
197
198 chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim));
199 line_cfg = gpiod_test_create_line_config_or_fail();
200 settings = gpiod_test_create_line_settings_or_fail();
201
202 gpiod_line_settings_set_edge_detection(settings, GPIOD_LINE_EDGE_NONE);
203 offset = 0;
204 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
205 settings);
206 gpiod_line_settings_set_edge_detection(settings,
207 GPIOD_LINE_EDGE_RISING);
208 offset = 1;
209 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
210 settings);
211 gpiod_line_settings_set_edge_detection(settings,
212 GPIOD_LINE_EDGE_FALLING);
213 offset = 2;
214 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
215 settings);
216 gpiod_line_settings_set_edge_detection(settings, GPIOD_LINE_EDGE_BOTH);
217 offset = 3;
218 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
219 settings);
220
221 request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg);
222 info0 = gpiod_test_chip_get_line_info_or_fail(chip, 0);
223 info1 = gpiod_test_chip_get_line_info_or_fail(chip, 1);
224 info2 = gpiod_test_chip_get_line_info_or_fail(chip, 2);
225 info3 = gpiod_test_chip_get_line_info_or_fail(chip, 3);
226
227 g_assert_cmpint(gpiod_line_info_get_edge_detection(info0), ==,
228 GPIOD_LINE_EDGE_NONE);
229 g_assert_cmpint(gpiod_line_info_get_edge_detection(info1), ==,
230 GPIOD_LINE_EDGE_RISING);
231 g_assert_cmpint(gpiod_line_info_get_edge_detection(info2), ==,
232 GPIOD_LINE_EDGE_FALLING);
233 g_assert_cmpint(gpiod_line_info_get_edge_detection(info3), ==,
234 GPIOD_LINE_EDGE_BOTH);
235 }
236
237 GPIOD_TEST_CASE(bias_settings)
238 {
239 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
240 g_autoptr(struct_gpiod_chip) chip = NULL;
241 g_autoptr(struct_gpiod_line_config) line_cfg = NULL;
242 g_autoptr(struct_gpiod_line_settings) settings = NULL;
243 g_autoptr(struct_gpiod_line_request) request = NULL;
244 g_autoptr(struct_gpiod_line_info) info0 = NULL;
245 g_autoptr(struct_gpiod_line_info) info1 = NULL;
246 g_autoptr(struct_gpiod_line_info) info2 = NULL;
247 g_autoptr(struct_gpiod_line_info) info3 = NULL;
248 guint offset;
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
254 gpiod_line_settings_set_direction(settings,
255 GPIOD_LINE_DIRECTION_OUTPUT);
256 offset = 0;
257 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
258 settings);
259 gpiod_line_settings_set_bias(settings, GPIOD_LINE_BIAS_DISABLED);
260 offset = 1;
261 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
262 settings);
263 gpiod_line_settings_set_bias(settings, GPIOD_LINE_BIAS_PULL_DOWN);
264 offset = 2;
265 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
266 settings);
267 gpiod_line_settings_set_bias(settings, GPIOD_LINE_BIAS_PULL_UP);
268 offset = 3;
269 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
270 settings);
271
272 request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg);
273 info0 = gpiod_test_chip_get_line_info_or_fail(chip, 0);
274 info1 = gpiod_test_chip_get_line_info_or_fail(chip, 1);
275 info2 = gpiod_test_chip_get_line_info_or_fail(chip, 2);
276 info3 = gpiod_test_chip_get_line_info_or_fail(chip, 3);
277
278 g_assert_cmpint(gpiod_line_info_get_bias(info0), ==,
279 GPIOD_LINE_BIAS_UNKNOWN);
280 g_assert_cmpint(gpiod_line_info_get_bias(info1), ==,
281 GPIOD_LINE_BIAS_DISABLED);
282 g_assert_cmpint(gpiod_line_info_get_bias(info2), ==,
283 GPIOD_LINE_BIAS_PULL_DOWN);
284 g_assert_cmpint(gpiod_line_info_get_bias(info3), ==,
285 GPIOD_LINE_BIAS_PULL_UP);
286 }
287
288 GPIOD_TEST_CASE(drive_settings)
289 {
290 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
291 g_autoptr(struct_gpiod_chip) chip = NULL;
292 g_autoptr(struct_gpiod_line_config) line_cfg = NULL;
293 g_autoptr(struct_gpiod_line_settings) settings = NULL;
294 g_autoptr(struct_gpiod_line_request) request = NULL;
295 g_autoptr(struct_gpiod_line_info) info0 = NULL;
296 g_autoptr(struct_gpiod_line_info) info1 = NULL;
297 g_autoptr(struct_gpiod_line_info) info2 = NULL;
298 guint offset;
299
300 chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim));
301 line_cfg = gpiod_test_create_line_config_or_fail();
302 settings = gpiod_test_create_line_settings_or_fail();
303
304 gpiod_line_settings_set_direction(settings,
305 GPIOD_LINE_DIRECTION_OUTPUT);
306 offset = 0;
307 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
308 settings);
309 gpiod_line_settings_set_drive(settings, GPIOD_LINE_DRIVE_OPEN_DRAIN);
310 offset = 1;
311 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
312 settings);
313 gpiod_line_settings_set_drive(settings, GPIOD_LINE_DRIVE_OPEN_SOURCE);
314 offset = 2;
315 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
316 settings);
317
318 request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg);
319 info0 = gpiod_test_chip_get_line_info_or_fail(chip, 0);
320 info1 = gpiod_test_chip_get_line_info_or_fail(chip, 1);
321 info2 = gpiod_test_chip_get_line_info_or_fail(chip, 2);
322
323 g_assert_cmpint(gpiod_line_info_get_drive(info0), ==,
324 GPIOD_LINE_DRIVE_PUSH_PULL);
325 g_assert_cmpint(gpiod_line_info_get_drive(info1), ==,
326 GPIOD_LINE_DRIVE_OPEN_DRAIN);
327 g_assert_cmpint(gpiod_line_info_get_drive(info2), ==,
328 GPIOD_LINE_DRIVE_OPEN_SOURCE);
329 }
330
331 GPIOD_TEST_CASE(debounce_period)
332 {
333 static const guint offset = 5;
334
335 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
336 g_autoptr(struct_gpiod_chip) chip = NULL;
337 g_autoptr(struct_gpiod_line_config) line_cfg = NULL;
338 g_autoptr(struct_gpiod_line_settings) settings = NULL;
339 g_autoptr(struct_gpiod_line_request) request = NULL;
340 g_autoptr(struct_gpiod_line_info) info = NULL;
341
342 chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim));
343 line_cfg = gpiod_test_create_line_config_or_fail();
344 settings = gpiod_test_create_line_settings_or_fail();
345
346 gpiod_line_settings_set_edge_detection(settings, GPIOD_LINE_EDGE_BOTH);
347 gpiod_line_settings_set_debounce_period_us(settings, 1000);
348
349 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
350 settings);
351
352 request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg);
353 info = gpiod_test_chip_get_line_info_or_fail(chip, 5);
354
355 g_assert_cmpuint(gpiod_line_info_get_debounce_period_us(info), ==,
356 1000);
357 }
358
359 GPIOD_TEST_CASE(event_clock)
360 {
361 g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
362 g_autoptr(struct_gpiod_chip) chip = NULL;
363 g_autoptr(struct_gpiod_line_config) line_cfg = NULL;
364 g_autoptr(struct_gpiod_line_settings) settings = NULL;
365 g_autoptr(struct_gpiod_line_request) request = NULL;
366 g_autoptr(struct_gpiod_line_info) info0 = NULL;
367 g_autoptr(struct_gpiod_line_info) info1 = NULL;
368 g_autoptr(struct_gpiod_line_info) info2 = NULL;
369 guint offset;
370
371 chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim));
372 line_cfg = gpiod_test_create_line_config_or_fail();
373 settings = gpiod_test_create_line_settings_or_fail();
374
375 offset = 0;
376 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
377 settings);
378 gpiod_line_settings_set_event_clock(settings,
379 GPIOD_LINE_CLOCK_REALTIME);
380 offset = 1;
381 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
382 settings);
383
384 gpiod_line_settings_set_event_clock(settings,
385 GPIOD_LINE_CLOCK_HTE);
386 offset = 2;
387 gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
388 settings);
389
390 request = gpiod_chip_request_lines(chip, NULL, line_cfg);
391 if (!request && errno == EOPNOTSUPP) {
392 g_test_skip("HTE support not available");
393 return;
394 }
395
396 gpiod_test_return_if_failed();
397
398 info0 = gpiod_test_chip_get_line_info_or_fail(chip, 0);
399 info1 = gpiod_test_chip_get_line_info_or_fail(chip, 1);
400 info2 = gpiod_test_chip_get_line_info_or_fail(chip, 2);
401
402 g_assert_cmpint(gpiod_line_info_get_event_clock(info0), ==,
403 GPIOD_LINE_CLOCK_MONOTONIC);
404 g_assert_cmpint(gpiod_line_info_get_event_clock(info1), ==,
405 GPIOD_LINE_CLOCK_REALTIME);
406 g_assert_cmpint(gpiod_line_info_get_event_clock(info2), ==,
407 GPIOD_LINE_CLOCK_HTE);
408 }