Debian Codegraph @

bindings/glib/examples/find_line_by_name_glib.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPDX-FileCopyrightText: 2023-2024 Bartosz Golaszewski <[email protected]>
3
4 /* Minimal example of finding a line with the given name. */
5
6 #include <glib.h>
7 #include <gpiod-glib.h>
8 #include <stdlib.h>
9
10 int main(void)
11 {
12 /* Example configuration - customize to suit your situation. */
13 static const gchar *const line_name = "GPIO0";
14
15 g_autoptr(GpiodglibChipInfo) info = NULL;
16 g_autoptr(GError) err = NULL;
17 g_autoptr(GDir) dir = NULL;
18 const gchar *filename;
19 gboolean ret;
20 guint offset;
21
22 dir = g_dir_open("/dev", 0, &err);
23 if (err) {
24 g_printerr("Unable to open /dev: %s\n", err->message);
25 return EXIT_FAILURE;
26 }
27
28 /*
29 * Names are not guaranteed unique, so this finds the first line with
30 * the given name.
31 */
32 while ((filename = g_dir_read_name(dir))) {
33 g_autoptr(GpiodglibChip) chip = NULL;
34 g_autofree gchar *path = NULL;
35 g_autofree gchar *name = NULL;
36
37 path = g_build_filename("/dev", filename, NULL);
38 if (!gpiodglib_is_gpiochip_device(path))
39 continue;
40
41 chip = gpiodglib_chip_new(path, &err);
42 if (err) {
43 g_printerr("Failed to open the GPIO chip at '%s': %s\n",
44 path, err->message);
45 return EXIT_FAILURE;
46 }
47
48 ret = gpiodglib_chip_get_line_offset_from_name(chip, line_name,
49 &offset, &err);
50 if (!ret) {
51 g_printerr("Failed to map the line name '%s' to offset: %s\n",
52 line_name, err->message);
53 return EXIT_FAILURE;
54 }
55
56 info = gpiodglib_chip_get_info(chip, &err);
57 if (!info) {
58 g_printerr("Failed to get chip info: %s\n",
59 err->message);
60 return EXIT_FAILURE;
61 }
62
63 name = gpiodglib_chip_info_dup_name(info);
64
65 g_print("%s %u\n", name, offset);
66 }
67
68 g_print("line '%s' not found\n", line_name);
69
70 return EXIT_SUCCESS;
71 }