Debian Codegraph @

examples/watch_line_info.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]>
3
4 /* Minimal example of watching for info changes on particular lines. */
5
6 #include <errno.h>
7 #include <gpiod.h>
8 #include <inttypes.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #define NUM_LINES 3
14
15 static const char *event_type(struct gpiod_info_event *event)
16 {
17 switch (gpiod_info_event_get_event_type(event)) {
18 case GPIOD_INFO_EVENT_LINE_REQUESTED:
19 return "Requested";
20 case GPIOD_INFO_EVENT_LINE_RELEASED:
21 return "Released";
22 case GPIOD_INFO_EVENT_LINE_CONFIG_CHANGED:
23 return "Reconfig";
24 default:
25 return "Unknown";
26 }
27 }
28
29 int main(void)
30 {
31 /* Example configuration - customize to suit your situation. */
32 static const char *const chip_path = "/dev/gpiochip0";
33 static const unsigned int line_offsets[NUM_LINES] = { 5, 3, 7 };
34
35 struct gpiod_info_event *event;
36 struct gpiod_line_info *info;
37 struct gpiod_chip *chip;
38 uint64_t timestamp_ns;
39 unsigned int i;
40
41 chip = gpiod_chip_open(chip_path);
42 if (!chip) {
43 fprintf(stderr, "failed to open chip: %s\n", strerror(errno));
44 return EXIT_FAILURE;
45 }
46
47 for (i = 0; i < NUM_LINES; i++) {
48 info = gpiod_chip_watch_line_info(chip, line_offsets[i]);
49 if (!info) {
50 fprintf(stderr, "failed to read info: %s\n",
51 strerror(errno));
52 return EXIT_FAILURE;
53 }
54 }
55
56 for (;;) {
57 /* Blocks until an event is available. */
58 event = gpiod_chip_read_info_event(chip);
59 if (!event) {
60 fprintf(stderr, "failed to read event: %s\n",
61 strerror(errno));
62 return EXIT_FAILURE;
63 }
64
65 info = gpiod_info_event_get_line_info(event);
66 timestamp_ns = gpiod_info_event_get_timestamp_ns(event);
67 printf("line %3d: %-9s %" PRIu64 ".%" PRIu64 "\n",
68 gpiod_line_info_get_offset(info), event_type(event),
69 timestamp_ns / 1000000000, timestamp_ns % 1000000000);
70
71 gpiod_info_event_free(event);
72 }
73 }