Debian Codegraph @

include/gpiod.h

raw symbols SCIP
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /* SPDX-FileCopyrightText: 2017-2022 Bartosz Golaszewski <[email protected]> */
3
4 /**
5 * @file gpiod.h
6 */
7
8 #ifndef __LIBGPIOD_GPIOD_H__
9 #define __LIBGPIOD_GPIOD_H__
10
11 #include <stdbool.h>
12 #include <stddef.h>
13 #include <stdint.h>
14
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18
19 /**
20 * @struct gpiod_chip
21 * @{
22 *
23 * Refer to @ref chips for functions that operate on gpiod_chip.
24 *
25 * @}
26 */
27 struct gpiod_chip;
28
29 /**
30 * @struct gpiod_chip_info
31 * @{
32 *
33 * Refer to @ref chip_info for functions that operate on gpiod_chip_info.
34 *
35 * @}
36 */
37 struct gpiod_chip_info;
38
39 /**
40 * @struct gpiod_line_info
41 * @{
42 *
43 * Refer to @ref line_info for functions that operate on gpiod_line_info.
44 *
45 * @}
46 */
47 struct gpiod_line_info;
48
49 /**
50 * @struct gpiod_line_settings
51 * @{
52 *
53 * Refer to @ref line_settings for functions that operate on
54 * gpiod_line_settings.
55 *
56 * @}
57 */
58 struct gpiod_line_settings;
59
60 /**
61 * @struct gpiod_line_config
62 * @{
63 *
64 * Refer to @ref line_config for functions that operate on gpiod_line_config.
65 *
66 * @}
67 */
68 struct gpiod_line_config;
69
70 /**
71 * @struct gpiod_request_config
72 * @{
73 *
74 * Refer to @ref request_config for functions that operate on
75 * gpiod_request_config.
76 *
77 * @}
78 */
79 struct gpiod_request_config;
80
81 /**
82 * @struct gpiod_line_request
83 * @{
84 *
85 * Refer to @ref line_request for functions that operate on
86 * gpiod_line_request.
87 *
88 * @}
89 */
90 struct gpiod_line_request;
91
92 /**
93 * @struct gpiod_info_event
94 * @{
95 *
96 * Refer to @ref line_watch for functions that operate on gpiod_info_event.
97 *
98 * @}
99 */
100 struct gpiod_info_event;
101
102 /**
103 * @struct gpiod_edge_event
104 * @{
105 *
106 * Refer to @ref edge_event for functions that operate on gpiod_edge_event.
107 *
108 * @}
109 */
110 struct gpiod_edge_event;
111
112 /**
113 * @struct gpiod_edge_event_buffer
114 * @{
115 *
116 * Refer to @ref edge_event for functions that operate on
117 * gpiod_edge_event_buffer.
118 *
119 * @}
120 */
121 struct gpiod_edge_event_buffer;
122
123 /**
124 * @defgroup chips GPIO chips
125 * @{
126 *
127 * Functions and data structures for GPIO chip operations.
128 *
129 * A GPIO chip object is associated with an open file descriptor to the GPIO
130 * character device. It exposes basic information about the chip and allows
131 * callers to retrieve information about each line, watch lines for state
132 * changes and make line requests.
133 */
134
135 /**
136 * @brief Open a chip by path.
137 * @param path Path to the gpiochip device file.
138 * @return GPIO chip object or NULL if an error occurred. The returned object
139 * must be closed by the caller using ::gpiod_chip_close.
140 */
141 struct gpiod_chip *gpiod_chip_open(const char *path);
142
143 /**
144 * @brief Close the chip and release all associated resources.
145 * @param chip Chip to close.
146 */
147 void gpiod_chip_close(struct gpiod_chip *chip);
148
149 /**
150 * @brief Get information about the chip.
151 * @param chip GPIO chip object.
152 * @return New GPIO chip info object or NULL if an error occurred. The returned
153 * object must be freed by the caller using ::gpiod_chip_info_free.
154 */
155 struct gpiod_chip_info *gpiod_chip_get_info(struct gpiod_chip *chip);
156
157 /**
158 * @brief Get the path used to open the chip.
159 * @param chip GPIO chip object.
160 * @return Path to the file passed as argument to ::gpiod_chip_open. The
161 * returned pointer is valid for the lifetime of the chip object and
162 * must not be freed by the caller.
163 */
164 const char *gpiod_chip_get_path(struct gpiod_chip *chip);
165
166 /**
167 * @brief Get a snapshot of information about a line.
168 * @param chip GPIO chip object.
169 * @param offset The offset of the GPIO line.
170 * @return New GPIO line info object or NULL if an error occurred. The returned
171 * object must be freed by the caller using ::gpiod_line_info_free.
172 */
173 struct gpiod_line_info *gpiod_chip_get_line_info(struct gpiod_chip *chip,
174 unsigned int offset);
175
176 /**
177 * @brief Get a snapshot of the status of a line and start watching it for
178 * future changes.
179 * @param chip GPIO chip object.
180 * @param offset The offset of the GPIO line.
181 * @return New GPIO line info object or NULL if an error occurred. The returned
182 * object must be freed by the caller using ::gpiod_line_info_free.
183 * @note Line status does not include the line value. To monitor the line
184 * value the line must be requested as an input with edge detection set.
185 */
186 struct gpiod_line_info *gpiod_chip_watch_line_info(struct gpiod_chip *chip,
187 unsigned int offset);
188
189 /**
190 * @brief Stop watching a line for status changes.
191 * @param chip GPIO chip object.
192 * @param offset The offset of the line to stop watching.
193 * @return 0 on success, -1 on failure.
194 */
195 int gpiod_chip_unwatch_line_info(struct gpiod_chip *chip, unsigned int offset);
196
197 /**
198 * @brief Get the file descriptor associated with the chip.
199 * @param chip GPIO chip object.
200 * @return File descriptor number for the chip.
201 *
202 * This function never fails. The returned file descriptor must not be closed
203 * by the caller. Call ::gpiod_chip_close to close the file descriptor by
204 * closing the chip owning it.
205 */
206 int gpiod_chip_get_fd(struct gpiod_chip *chip);
207
208 /**
209 * @brief Wait for line status change events on any of the watched lines
210 * on the chip.
211 * @param chip GPIO chip object.
212 * @param timeout_ns Wait time limit in nanoseconds. If set to 0, the function
213 * returns immediately. If set to a negative number, the
214 * function blocks indefinitely until an event becomes
215 * available.
216 * @return 0 if wait timed out, -1 if an error occurred, 1 if an event is
217 * pending.
218 */
219 int gpiod_chip_wait_info_event(struct gpiod_chip *chip, int64_t timeout_ns);
220
221 /**
222 * @brief Read a single line status change event from the chip.
223 * @param chip GPIO chip object.
224 * @return Newly read watch event object or NULL on error. The event must be
225 * freed by the caller using ::gpiod_info_event_free.
226 * @note If no events are pending, this function will block.
227 */
228 struct gpiod_info_event *gpiod_chip_read_info_event(struct gpiod_chip *chip);
229
230 /**
231 * @brief Map a line's name to its offset within the chip.
232 * @param chip GPIO chip object.
233 * @param name Name of the GPIO line to map.
234 * @return Offset of the line within the chip or -1 on error.
235 * @note If a line with given name is not exposed by the chip, the function
236 * sets errno to ENOENT.
237 */
238 int gpiod_chip_get_line_offset_from_name(struct gpiod_chip *chip,
239 const char *name);
240
241 /**
242 * @brief Request a set of lines for exclusive usage.
243 * @param chip GPIO chip object.
244 * @param req_cfg Request config object. Can be NULL for default settings.
245 * @param line_cfg Line config object.
246 * @return New line request object or NULL if an error occurred. The request
247 * must be released by the caller using ::gpiod_line_request_release.
248 */
249 struct gpiod_line_request *
250 gpiod_chip_request_lines(struct gpiod_chip *chip,
251 struct gpiod_request_config *req_cfg,
252 struct gpiod_line_config *line_cfg);
253
254 /**
255 * @}
256 *
257 * @defgroup chip_info Chip info
258 * @{
259 *
260 * Functions for retrieving kernel information about chips.
261 *
262 * Line info object contains an immutable snapshot of a chip's status.
263 *
264 * The chip info contains all the publicly available information about a
265 * chip.
266 *
267 * Some accessor methods return pointers. Those pointers refer to internal
268 * fields. The lifetimes of those fields are tied to the lifetime of the
269 * containing chip info object. Such pointers remain valid until
270 * ::gpiod_chip_info_free is called on the containing chip info object. They
271 * must not be freed by the caller.
272 */
273
274 /**
275 * @brief Free a chip info object and release all associated resources.
276 * @param info GPIO chip info object to free.
277 */
278 void gpiod_chip_info_free(struct gpiod_chip_info *info);
279
280 /**
281 * @brief Get the name of the chip as represented in the kernel.
282 * @param info GPIO chip info object.
283 * @return Valid pointer to a human-readable string containing the chip name.
284 * The string lifetime is tied to the chip info object so the pointer
285 * must not be freed by the caller.
286 */
287 const char *gpiod_chip_info_get_name(struct gpiod_chip_info *info);
288
289 /**
290 * @brief Get the label of the chip as represented in the kernel.
291 * @param info GPIO chip info object.
292 * @return Valid pointer to a human-readable string containing the chip label.
293 * The string lifetime is tied to the chip info object so the pointer
294 * must not be freed by the caller.
295 */
296 const char *gpiod_chip_info_get_label(struct gpiod_chip_info *info);
297
298 /**
299 * @brief Get the number of lines exposed by the chip.
300 * @param info GPIO chip info object.
301 * @return Number of GPIO lines.
302 */
303 size_t gpiod_chip_info_get_num_lines(struct gpiod_chip_info *info);
304
305 /**
306 * @}
307 *
308 * @defgroup line_defs Line definitions
309 * @{
310 *
311 * These defines are used across the API.
312 */
313
314 /**
315 * @brief Logical line state.
316 */
317 enum gpiod_line_value {
318 GPIOD_LINE_VALUE_ERROR = -1,
319 /**< Returned to indicate an error when reading the value. */
320 GPIOD_LINE_VALUE_INACTIVE = 0,
321 /**< Line is logically inactive. */
322 GPIOD_LINE_VALUE_ACTIVE = 1,
323 /**< Line is logically active. */
324 };
325
326 /**
327 * @brief Direction settings.
328 */
329 enum gpiod_line_direction {
330 GPIOD_LINE_DIRECTION_AS_IS = 1,
331 /**< Request the line(s), but don't change direction. */
332 GPIOD_LINE_DIRECTION_INPUT,
333 /**< Direction is input - for reading the value of an externally driven
334 * GPIO line. */
335 GPIOD_LINE_DIRECTION_OUTPUT,
336 /**< Direction is output - for driving the GPIO line. */
337 };
338
339 /**
340 * @brief Edge detection settings.
341 */
342 enum gpiod_line_edge {
343 GPIOD_LINE_EDGE_NONE = 1,
344 /**< Line edge detection is disabled. */
345 GPIOD_LINE_EDGE_RISING,
346 /**< Line detects rising edge events. */
347 GPIOD_LINE_EDGE_FALLING,
348 /**< Line detects falling edge events. */
349 GPIOD_LINE_EDGE_BOTH,
350 /**< Line detects both rising and falling edge events. */
351 };
352
353 /**
354 * @brief Internal bias settings.
355 */
356 enum gpiod_line_bias {
357 GPIOD_LINE_BIAS_AS_IS = 1,
358 /**< Don't change the bias setting when applying line config. */
359 GPIOD_LINE_BIAS_UNKNOWN,
360 /**< The internal bias state is unknown. */
361 GPIOD_LINE_BIAS_DISABLED,
362 /**< The internal bias is disabled. */
363 GPIOD_LINE_BIAS_PULL_UP,
364 /**< The internal pull-up bias is enabled. */
365 GPIOD_LINE_BIAS_PULL_DOWN,
366 /**< The internal pull-down bias is enabled. */
367 };
368
369 /**
370 * @brief Drive settings.
371 */
372 enum gpiod_line_drive {
373 GPIOD_LINE_DRIVE_PUSH_PULL = 1,
374 /**< Drive setting is push-pull. */
375 GPIOD_LINE_DRIVE_OPEN_DRAIN,
376 /**< Line output is open-drain. */
377 GPIOD_LINE_DRIVE_OPEN_SOURCE,
378 /**< Line output is open-source. */
379 };
380
381 /**
382 * @brief Clock settings.
383 */
384 enum gpiod_line_clock {
385 GPIOD_LINE_CLOCK_MONOTONIC = 1,
386 /**< Line uses the monotonic clock for edge event timestamps. */
387 GPIOD_LINE_CLOCK_REALTIME,
388 /**< Line uses the realtime clock for edge event timestamps. */
389 GPIOD_LINE_CLOCK_HTE,
390 /**< Line uses the hardware timestamp engine for event timestamps. */
391 };
392
393 /**
394 * @}
395 *
396 * @defgroup line_info Line info
397 * @{
398 *
399 * Functions for retrieving kernel information about both requested and free
400 * lines.
401 *
402 * Line info object contains an immutable snapshot of a line's status.
403 *
404 * The line info contains all the publicly available information about a
405 * line, which does not include the line value. The line must be requested
406 * to access the line value.
407 *
408 * Some accessor methods return pointers. Those pointers refer to internal
409 * fields. The lifetimes of those fields are tied to the lifetime of the
410 * containing line info object. Such pointers remain valid until
411 * ::gpiod_line_info_free is called on the containing line info object. They
412 * must not be freed by the caller.
413 */
414
415 /**
416 * @brief Free a line info object and release all associated resources.
417 * @param info GPIO line info object to free.
418 */
419 void gpiod_line_info_free(struct gpiod_line_info *info);
420
421 /**
422 * @brief Copy a line info object.
423 * @param info Line info to copy.
424 * @return Copy of the line info or NULL on error. The returned object must
425 * be freed by the caller using :gpiod_line_info_free.
426 */
427 struct gpiod_line_info *gpiod_line_info_copy(struct gpiod_line_info *info);
428
429 /**
430 * @brief Get the offset of the line.
431 * @param info GPIO line info object.
432 * @return Offset of the line within the parent chip.
433 *
434 * The offset uniquely identifies the line on the chip. The combination of the
435 * chip and offset uniquely identifies the line within the system.
436 */
437 unsigned int gpiod_line_info_get_offset(struct gpiod_line_info *info);
438
439 /**
440 * @brief Get the name of the line.
441 * @param info GPIO line info object.
442 * @return Name of the GPIO line as it is represented in the kernel.
443 * This function returns a valid pointer to a null-terminated string
444 * or NULL if the line is unnamed. The string lifetime is tied to the
445 * line info object so the pointer must not be freed.
446 */
447 const char *gpiod_line_info_get_name(struct gpiod_line_info *info);
448
449 /**
450 * @brief Check if the line is in use.
451 * @param info GPIO line object.
452 * @return True if the line is in use, false otherwise.
453 *
454 * The exact reason a line is busy cannot be determined from user space.
455 * It may have been requested by another process or hogged by the kernel.
456 * It only matters that the line is used and can't be requested until
457 * released by the existing consumer.
458 */
459 bool gpiod_line_info_is_used(struct gpiod_line_info *info);
460
461 /**
462 * @brief Get the name of the consumer of the line.
463 * @param info GPIO line info object.
464 * @return Name of the GPIO consumer as it is represented in the kernel.
465 * This function returns a valid pointer to a null-terminated string
466 * or NULL if the consumer name is not set.
467 * The string lifetime is tied to the line info object so the pointer
468 * must not be freed.
469 */
470 const char *gpiod_line_info_get_consumer(struct gpiod_line_info *info);
471
472 /**
473 * @brief Get the direction setting of the line.
474 * @param info GPIO line info object.
475 * @return Returns ::GPIOD_LINE_DIRECTION_INPUT or
476 * ::GPIOD_LINE_DIRECTION_OUTPUT.
477 */
478 enum gpiod_line_direction
479 gpiod_line_info_get_direction(struct gpiod_line_info *info);
480
481 /**
482 * @brief Get the edge detection setting of the line.
483 * @param info GPIO line info object.
484 * @return Returns ::GPIOD_LINE_EDGE_NONE, ::GPIOD_LINE_EDGE_RISING,
485 * ::GPIOD_LINE_EDGE_FALLING or ::GPIOD_LINE_EDGE_BOTH.
486 */
487 enum gpiod_line_edge
488 gpiod_line_info_get_edge_detection(struct gpiod_line_info *info);
489
490 /**
491 * @brief Get the bias setting of the line.
492 * @param info GPIO line object.
493 * @return Returns ::GPIOD_LINE_BIAS_PULL_UP, ::GPIOD_LINE_BIAS_PULL_DOWN,
494 * ::GPIOD_LINE_BIAS_DISABLED or ::GPIOD_LINE_BIAS_UNKNOWN.
495 */
496 enum gpiod_line_bias
497 gpiod_line_info_get_bias(struct gpiod_line_info *info);
498
499 /**
500 * @brief Get the drive setting of the line.
501 * @param info GPIO line info object.
502 * @return Returns ::GPIOD_LINE_DRIVE_PUSH_PULL, ::GPIOD_LINE_DRIVE_OPEN_DRAIN
503 * or ::GPIOD_LINE_DRIVE_OPEN_SOURCE.
504 */
505 enum gpiod_line_drive
506 gpiod_line_info_get_drive(struct gpiod_line_info *info);
507
508 /**
509 * @brief Check if the logical value of the line is inverted compared to the
510 * physical.
511 * @param info GPIO line object.
512 * @return True if the line is "active-low", false otherwise.
513 */
514 bool gpiod_line_info_is_active_low(struct gpiod_line_info *info);
515
516 /**
517 * @brief Check if the line is debounced (either by hardware or by the kernel
518 * software debouncer).
519 * @param info GPIO line info object.
520 * @return True if the line is debounced, false otherwise.
521 */
522 bool gpiod_line_info_is_debounced(struct gpiod_line_info *info);
523
524 /**
525 * @brief Get the debounce period of the line, in microseconds.
526 * @param info GPIO line info object.
527 * @return Debounce period in microseconds.
528 * 0 if the line is not debounced.
529 */
530 unsigned long
531 gpiod_line_info_get_debounce_period_us(struct gpiod_line_info *info);
532
533 /**
534 * @brief Get the event clock setting used for edge event timestamps for the
535 * line.
536 * @param info GPIO line info object.
537 * @return Returns ::GPIOD_LINE_CLOCK_MONOTONIC, ::GPIOD_LINE_CLOCK_HTE or
538 * ::GPIOD_LINE_CLOCK_REALTIME.
539 */
540 enum gpiod_line_clock
541 gpiod_line_info_get_event_clock(struct gpiod_line_info *info);
542
543 /**
544 * @}
545 *
546 * @defgroup line_watch Line status watch events
547 * @{
548 *
549 * Accessors for the info event objects allowing to monitor changes in GPIO
550 * line status.
551 *
552 * Callers are notified about changes in a line's status due to GPIO uAPI
553 * calls. Each info event contains information about the event itself
554 * (timestamp, type) as well as a snapshot of line's status in the form
555 * of a line-info object.
556 */
557
558 /**
559 * @brief Line status change event types.
560 */
561 enum gpiod_info_event_type {
562 GPIOD_INFO_EVENT_LINE_REQUESTED = 1,
563 /**< Line has been requested. */
564 GPIOD_INFO_EVENT_LINE_RELEASED,
565 /**< Previously requested line has been released. */
566 GPIOD_INFO_EVENT_LINE_CONFIG_CHANGED,
567 /**< Line configuration has changed. */
568 };
569
570 /**
571 * @brief Free the info event object and release all associated resources.
572 * @param event Info event to free.
573 */
574 void gpiod_info_event_free(struct gpiod_info_event *event);
575
576 /**
577 * @brief Get the event type of the status change event.
578 * @param event Line status watch event.
579 * @return One of ::GPIOD_INFO_EVENT_LINE_REQUESTED,
580 * ::GPIOD_INFO_EVENT_LINE_RELEASED or
581 * ::GPIOD_INFO_EVENT_LINE_CONFIG_CHANGED.
582 */
583 enum gpiod_info_event_type
584 gpiod_info_event_get_event_type(struct gpiod_info_event *event);
585
586 /**
587 * @brief Get the timestamp of the event.
588 * @param event Line status watch event.
589 * @return Timestamp in nanoseconds, read from the monotonic clock.
590 */
591 uint64_t gpiod_info_event_get_timestamp_ns(struct gpiod_info_event *event);
592
593 /**
594 * @brief Get the snapshot of line-info associated with the event.
595 * @param event Line info event object.
596 * @return Returns a pointer to the line-info object associated with the event.
597 * The object lifetime is tied to the event object, so the pointer must
598 * be not be freed by the caller.
599 * @warning Thread-safety:
600 * Since the line-info object is tied to the event, different threads
601 * may not operate on the event and line-info at the same time. The
602 * line-info can be copied using ::gpiod_line_info_copy in order to
603 * create a standalone object - which then may safely be used from a
604 * different thread concurrently.
605 */
606 struct gpiod_line_info *
607 gpiod_info_event_get_line_info(struct gpiod_info_event *event);
608
609 /**
610 * @}
611 *
612 * @defgroup line_settings Line settings objects
613 * @{
614 *
615 * Functions for manipulating line settings objects.
616 *
617 * Line settings object contains a set of line properties that can be used
618 * when requesting lines or reconfiguring an existing request.
619 *
620 * Mutators in general can only fail if the new property value is invalid. The
621 * return values can be safely ignored - the object remains valid even after
622 * a mutator fails and simply uses the sane default appropriate for given
623 * property.
624 */
625
626 /**
627 * @brief Create a new line settings object.
628 * @return New line settings object or NULL on error. The returned object must
629 * be freed by the caller using ::gpiod_line_settings_free.
630 */
631 struct gpiod_line_settings *gpiod_line_settings_new(void);
632
633 /**
634 * @brief Free the line settings object and release all associated resources.
635 * @param settings Line settings object.
636 */
637 void gpiod_line_settings_free(struct gpiod_line_settings *settings);
638
639 /**
640 * @brief Reset the line settings object to its default values.
641 * @param settings Line settings object.
642 */
643 void gpiod_line_settings_reset(struct gpiod_line_settings *settings);
644
645 /**
646 * @brief Copy the line settings object.
647 * @param settings Line settings object to copy.
648 * @return New line settings object that must be freed using
649 * ::gpiod_line_settings_free or NULL on failure.
650 */
651 struct gpiod_line_settings *
652 gpiod_line_settings_copy(struct gpiod_line_settings *settings);
653
654 /**
655 * @brief Set direction.
656 * @param settings Line settings object.
657 * @param direction New direction.
658 * @return 0 on success, -1 on error.
659 */
660 int gpiod_line_settings_set_direction(struct gpiod_line_settings *settings,
661 enum gpiod_line_direction direction);
662
663 /**
664 * @brief Get direction.
665 * @param settings Line settings object.
666 * @return Current direction.
667 */
668 enum gpiod_line_direction
669 gpiod_line_settings_get_direction(struct gpiod_line_settings *settings);
670
671 /**
672 * @brief Set edge detection.
673 * @param settings Line settings object.
674 * @param edge New edge detection setting.
675 * @return 0 on success, -1 on failure.
676 */
677 int gpiod_line_settings_set_edge_detection(struct gpiod_line_settings *settings,
678 enum gpiod_line_edge edge);
679
680 /**
681 * @brief Get edge detection.
682 * @param settings Line settings object.
683 * @return Current edge detection setting.
684 */
685 enum gpiod_line_edge
686 gpiod_line_settings_get_edge_detection(struct gpiod_line_settings *settings);
687
688 /**
689 * @brief Set bias.
690 * @param settings Line settings object.
691 * @param bias New bias.
692 * @return 0 on success, -1 on failure.
693 */
694 int gpiod_line_settings_set_bias(struct gpiod_line_settings *settings,
695 enum gpiod_line_bias bias);
696
697 /**
698 * @brief Get bias.
699 * @param settings Line settings object.
700 * @return Current bias setting.
701 */
702 enum gpiod_line_bias
703 gpiod_line_settings_get_bias(struct gpiod_line_settings *settings);
704
705 /**
706 * @brief Set drive.
707 * @param settings Line settings object.
708 * @param drive New drive setting.
709 * @return 0 on success, -1 on failure.
710 */
711 int gpiod_line_settings_set_drive(struct gpiod_line_settings *settings,
712 enum gpiod_line_drive drive);
713
714 /**
715 * @brief Get drive.
716 * @param settings Line settings object.
717 * @return Current drive setting.
718 */
719 enum gpiod_line_drive
720 gpiod_line_settings_get_drive(struct gpiod_line_settings *settings);
721
722 /**
723 * @brief Set active-low setting.
724 * @param settings Line settings object.
725 * @param active_low New active-low setting.
726 */
727 void gpiod_line_settings_set_active_low(struct gpiod_line_settings *settings,
728 bool active_low);
729
730 /**
731 * @brief Get active-low setting.
732 * @param settings Line settings object.
733 * @return True if active-low is enabled, false otherwise.
734 */
735 bool gpiod_line_settings_get_active_low(struct gpiod_line_settings *settings);
736
737 /**
738 * @brief Set debounce period.
739 * @param settings Line settings object.
740 * @param period New debounce period in microseconds.
741 */
742 void
743 gpiod_line_settings_set_debounce_period_us(struct gpiod_line_settings *settings,
744 unsigned long period);
745
746 /**
747 * @brief Get debounce period.
748 * @param settings Line settings object.
749 * @return Current debounce period in microseconds.
750 */
751 unsigned long
752 gpiod_line_settings_get_debounce_period_us(
753 struct gpiod_line_settings *settings);
754
755 /**
756 * @brief Set event clock.
757 * @param settings Line settings object.
758 * @param event_clock New event clock.
759 * @return 0 on success, -1 on failure.
760 */
761 int gpiod_line_settings_set_event_clock(struct gpiod_line_settings *settings,
762 enum gpiod_line_clock event_clock);
763
764 /**
765 * @brief Get event clock setting.
766 * @param settings Line settings object.
767 * @return Current event clock setting.
768 */
769 enum gpiod_line_clock
770 gpiod_line_settings_get_event_clock(struct gpiod_line_settings *settings);
771
772 /**
773 * @brief Set the output value.
774 * @param settings Line settings object.
775 * @param value New output value.
776 * @return 0 on success, -1 on failure.
777 */
778 int gpiod_line_settings_set_output_value(struct gpiod_line_settings *settings,
779 enum gpiod_line_value value);
780
781 /**
782 * @brief Get the output value.
783 * @param settings Line settings object.
784 * @return Current output value.
785 */
786 enum gpiod_line_value
787 gpiod_line_settings_get_output_value(struct gpiod_line_settings *settings);
788
789 /**
790 * @}
791 *
792 * @defgroup line_config Line configuration objects
793 * @{
794 *
795 * Functions for manipulating line configuration objects.
796 *
797 * The line-config object contains the configuration for lines that can be
798 * used in two cases:
799 * - when making a line request
800 * - when reconfiguring a set of already requested lines.
801 *
802 * A new line-config object is empty. Using it in a request will lead to an
803 * error. In order to a line-config to become useful, it needs to be assigned
804 * at least one offset-to-settings mapping by calling
805 * ::gpiod_line_config_add_line_settings.
806 *
807 * When calling ::gpiod_chip_request_lines, the library will request all
808 * offsets that were assigned settings in the order that they were assigned.
809 * If any of the offsets was duplicated, the last one will take precedence.
810 */
811
812 /**
813 * @brief Create a new line config object.
814 * @return New line config object or NULL on error. The returned object must
815 * be freed by the caller using ::gpiod_line_config_free.
816 */
817 struct gpiod_line_config *gpiod_line_config_new(void);
818
819 /**
820 * @brief Free the line config object and release all associated resources.
821 * @param config Line config object to free.
822 */
823 void gpiod_line_config_free(struct gpiod_line_config *config);
824
825 /**
826 * @brief Reset the line config object.
827 * @param config Line config object to free.
828 *
829 * Resets the entire configuration stored in the object. This is useful if
830 * the user wants to reuse the object without reallocating it.
831 */
832 void gpiod_line_config_reset(struct gpiod_line_config *config);
833
834 /**
835 * @brief Add line settings for a set of offsets.
836 * @param config Line config object.
837 * @param offsets Array of offsets for which to apply the settings.
838 * @param num_offsets Number of offsets stored in the offsets array.
839 * @param settings Line settings to apply.
840 * @return 0 on success, -1 on failure.
841 */
842 int gpiod_line_config_add_line_settings(struct gpiod_line_config *config,
843 const unsigned int *offsets,
844 size_t num_offsets,
845 struct gpiod_line_settings *settings);
846
847 /**
848 * @brief Get line settings for offset.
849 * @param config Line config object.
850 * @param offset Offset for which to get line settings.
851 * @return New line settings object (must be freed by the caller) or NULL on
852 * error.
853 */
854 struct gpiod_line_settings *
855 gpiod_line_config_get_line_settings(struct gpiod_line_config *config,
856 unsigned int offset);
857
858 /**
859 * @brief Set output values for a number of lines.
860 * @param config Line config object.
861 * @param values Buffer containing the output values.
862 * @param num_values Number of values in the buffer.
863 * @return 0 on success, -1 on error.
864 *
865 * This is a helper that allows users to set multiple (potentially different)
866 * output values at once while using the same line settings object. Instead of
867 * modifying the output value in the settings object and calling
868 * ::gpiod_line_config_add_line_settings multiple times, we can specify the
869 * settings, add them for a set of offsets and then call this function to
870 * set the output values.
871 *
872 * Values set by this function override whatever values were specified in the
873 * regular line settings.
874 *
875 * Each value must be associated with the line identified by the corresponding
876 * entry in the offset array filled by
877 * ::gpiod_line_request_get_requested_offsets.
878 */
879 int gpiod_line_config_set_output_values(struct gpiod_line_config *config,
880 const enum gpiod_line_value *values,
881 size_t num_values);
882
883 /**
884 * @brief Get the number of configured line offsets.
885 * @param config Line config object.
886 * @return Number of offsets for which line settings have been added.
887 */
888 size_t
889 gpiod_line_config_get_num_configured_offsets(struct gpiod_line_config *config);
890
891 /**
892 * @brief Get configured offsets.
893 * @param config Line config object.
894 * @param offsets Array to store offsets.
895 * @param max_offsets Number of offsets that can be stored in the offsets array.
896 * @return Number of offsets stored in the offsets array.
897 *
898 * If max_offsets is lower than the number of lines actually requested (this
899 * value can be retrieved using ::gpiod_line_config_get_num_configured_offsets),
900 * then only up to max_lines offsets will be stored in offsets.
901 */
902 size_t
903 gpiod_line_config_get_configured_offsets(struct gpiod_line_config *config,
904 unsigned int *offsets,
905 size_t max_offsets);
906
907 /**
908 * @}
909 *
910 * @defgroup request_config Request configuration objects
911 * @{
912 *
913 * Functions for manipulating request configuration objects.
914 *
915 * Request config objects are used to pass a set of options to the kernel at
916 * the time of the line request. The mutators don't return error values. If the
917 * values are invalid, in general they are silently adjusted to acceptable
918 * ranges.
919 */
920
921 /**
922 * @brief Create a new request config object.
923 * @return New request config object or NULL on error. The returned object must
924 * be freed by the caller using ::gpiod_request_config_free.
925 */
926 struct gpiod_request_config *gpiod_request_config_new(void);
927
928 /**
929 * @brief Free the request config object and release all associated resources.
930 * @param config Line config object.
931 */
932 void gpiod_request_config_free(struct gpiod_request_config *config);
933
934 /**
935 * @brief Set the consumer name for the request.
936 * @param config Request config object.
937 * @param consumer Consumer name.
938 * @note If the consumer string is too long, it will be truncated to the max
939 * accepted length.
940 */
941 void gpiod_request_config_set_consumer(struct gpiod_request_config *config,
942 const char *consumer);
943
944 /**
945 * @brief Get the consumer name configured in the request config.
946 * @param config Request config object.
947 * @return Consumer name stored in the request config.
948 */
949 const char *
950 gpiod_request_config_get_consumer(struct gpiod_request_config *config);
951
952 /**
953 * @brief Set the size of the kernel event buffer for the request.
954 * @param config Request config object.
955 * @param event_buffer_size New event buffer size.
956 * @note The kernel may adjust the value if it's too high. If set to 0, the
957 * default value will be used.
958 * @note The kernel buffer is distinct from and independent of the user space
959 * buffer (::gpiod_edge_event_buffer_new).
960 */
961 void
962 gpiod_request_config_set_event_buffer_size(struct gpiod_request_config *config,
963 size_t event_buffer_size);
964
965 /**
966 * @brief Get the edge event buffer size for the request config.
967 * @param config Request config object.
968 * @return Edge event buffer size setting from the request config.
969 */
970 size_t
971 gpiod_request_config_get_event_buffer_size(struct gpiod_request_config *config);
972
973 /**
974 * @}
975 *
976 * @defgroup line_request Line request operations
977 * @{
978 *
979 * Functions allowing interactions with requested lines.
980 */
981
982 /**
983 * @brief Release the requested lines and free all associated resources.
984 * @param request Line request object to release.
985 */
986 void gpiod_line_request_release(struct gpiod_line_request *request);
987
988 /**
989 * @brief Get the name of the chip this request was made on.
990 * @param request Line request object.
991 * @return Name the GPIO chip device. The returned pointer is valid for the
992 * lifetime of the request object and must not be freed by the caller.
993 */
994 const char *
995 gpiod_line_request_get_chip_name(struct gpiod_line_request *request);
996
997 /**
998 * @brief Get the number of lines in the request.
999 * @param request Line request object.
1000 * @return Number of requested lines.
1001 */
1002 size_t
1003 gpiod_line_request_get_num_requested_lines(struct gpiod_line_request *request);
1004
1005 /**
1006 * @brief Get the offsets of the lines in the request.
1007 * @param request Line request object.
1008 * @param offsets Array to store offsets.
1009 * @param max_offsets Number of offsets that can be stored in the offsets array.
1010 * @return Number of offsets stored in the offsets array.
1011 *
1012 * If max_offsets is lower than the number of lines actually requested (this
1013 * value can be retrieved using ::gpiod_line_request_get_num_requested_lines),
1014 * then only up to max_lines offsets will be stored in offsets.
1015 */
1016 size_t
1017 gpiod_line_request_get_requested_offsets(struct gpiod_line_request *request,
1018 unsigned int *offsets,
1019 size_t max_offsets);
1020
1021 /**
1022 * @brief Get the value of a single requested line.
1023 * @param request Line request object.
1024 * @param offset The offset of the line of which the value should be read.
1025 * @return Returns 1 or 0 on success and -1 on error.
1026 */
1027 enum gpiod_line_value
1028 gpiod_line_request_get_value(struct gpiod_line_request *request,
1029 unsigned int offset);
1030
1031 /**
1032 * @brief Get the values of a subset of requested lines.
1033 * @param request GPIO line request.
1034 * @param num_values Number of lines for which to read values.
1035 * @param offsets Array of offsets identifying the subset of requested lines
1036 * from which to read values.
1037 * @param values Array in which the values will be stored. Must be sized
1038 * to hold \p num_values entries. Each value is associated with
1039 * the line identified by the corresponding entry in \p offsets.
1040 * @return 0 on success, -1 on failure.
1041 */
1042 int gpiod_line_request_get_values_subset(struct gpiod_line_request *request,
1043 size_t num_values,
1044 const unsigned int *offsets,
1045 enum gpiod_line_value *values);
1046
1047 /**
1048 * @brief Get the values of all requested lines.
1049 * @param request GPIO line request.
1050 * @param values Array in which the values will be stored. Must be sized to
1051 * hold the number of lines filled by
1052 * ::gpiod_line_request_get_num_requested_lines.
1053 * Each value is associated with the line identified by the
1054 * corresponding entry in the offset array filled by
1055 * ::gpiod_line_request_get_requested_offsets.
1056 * @return 0 on success, -1 on failure.
1057 */
1058 int gpiod_line_request_get_values(struct gpiod_line_request *request,
1059 enum gpiod_line_value *values);
1060
1061 /**
1062 * @brief Set the value of a single requested line.
1063 * @param request Line request object.
1064 * @param offset The offset of the line for which the value should be set.
1065 * @param value Value to set.
1066 * @return 0 on success, -1 on failure.
1067 */
1068 int gpiod_line_request_set_value(struct gpiod_line_request *request,
1069 unsigned int offset,
1070 enum gpiod_line_value value);
1071
1072 /**
1073 * @brief Set the values of a subset of requested lines.
1074 * @param request GPIO line request.
1075 * @param num_values Number of lines for which to set values.
1076 * @param offsets Array of offsets, containing the number of entries specified
1077 * by \p num_values, identifying the requested lines for
1078 * which to set values.
1079 * @param values Array of values to set, containing the number of entries
1080 * specified by \p num_values. Each value is associated with the
1081 * line identified by the corresponding entry in \p offsets.
1082 * @return 0 on success, -1 on failure.
1083 */
1084 int gpiod_line_request_set_values_subset(struct gpiod_line_request *request,
1085 size_t num_values,
1086 const unsigned int *offsets,
1087 const enum gpiod_line_value *values);
1088
1089 /**
1090 * @brief Set the values of all lines associated with a request.
1091 * @param request GPIO line request.
1092 * @param values Array containing the values to set. Must be sized to
1093 * contain the number of lines filled by
1094 * ::gpiod_line_request_get_num_requested_lines.
1095 * Each value is associated with the line identified by the
1096 * corresponding entry in the offset array filled by
1097 * ::gpiod_line_request_get_requested_offsets.
1098 * @return 0 on success, -1 on failure.
1099 */
1100 int gpiod_line_request_set_values(struct gpiod_line_request *request,
1101 const enum gpiod_line_value *values);
1102
1103 /**
1104 * @brief Update the configuration of lines associated with a line request.
1105 * @param request GPIO line request.
1106 * @param config New line config to apply.
1107 * @return 0 on success, -1 on failure.
1108 * @note The new line configuration completely replaces the old.
1109 * @note Any requested lines without overrides are configured to the requested
1110 * defaults.
1111 * @note Any configured overrides for lines that have not been requested
1112 * are silently ignored.
1113 */
1114 int gpiod_line_request_reconfigure_lines(struct gpiod_line_request *request,
1115 struct gpiod_line_config *config);
1116
1117 /**
1118 * @brief Get the file descriptor associated with a line request.
1119 * @param request GPIO line request.
1120 * @return The file descriptor associated with the request.
1121 * This function never fails.
1122 * The returned file descriptor must not be closed by the caller.
1123 * Call ::gpiod_line_request_release to close the file.
1124 */
1125 int gpiod_line_request_get_fd(struct gpiod_line_request *request);
1126
1127 /**
1128 * @brief Wait for edge events on any of the requested lines.
1129 * @param request GPIO line request.
1130 * @param timeout_ns Wait time limit in nanoseconds. If set to 0, the function
1131 * returns immediately. If set to a negative number, the
1132 * function blocks indefinitely until an event becomes
1133 * available.
1134 * @return 0 if wait timed out, -1 if an error occurred, 1 if an event is
1135 * pending.
1136 *
1137 * Lines must have edge detection set for edge events to be emitted.
1138 * By default edge detection is disabled.
1139 */
1140 int gpiod_line_request_wait_edge_events(struct gpiod_line_request *request,
1141 int64_t timeout_ns);
1142
1143 /**
1144 * @brief Read a number of edge events from a line request.
1145 * @param request GPIO line request.
1146 * @param buffer Edge event buffer, sized to hold at least \p max_events.
1147 * @param max_events Maximum number of events to read.
1148 * @return On success returns the number of events read from the file
1149 * descriptor, on failure return -1.
1150 * @note This function will block if no event was queued for the line request.
1151 * @note Any exising events in the buffer are overwritten. This is not an
1152 * append operation.
1153 */
1154 int gpiod_line_request_read_edge_events(struct gpiod_line_request *request,
1155 struct gpiod_edge_event_buffer *buffer,
1156 size_t max_events);
1157
1158 /**
1159 * @}
1160 *
1161 * @defgroup edge_event Line edge events handling
1162 * @{
1163 *
1164 * Functions and data types for handling edge events.
1165 *
1166 * An edge event object contains information about a single line edge event.
1167 * It contains the event type, timestamp and the offset of the line on which
1168 * the event occurred as well as two sequence numbers (global for all lines
1169 * in the associated request and local for this line only).
1170 *
1171 * Edge events are stored into an edge-event buffer object to improve
1172 * performance and to limit the number of memory allocations when a large
1173 * number of events are being read.
1174 */
1175
1176 /**
1177 * @brief Event types.
1178 */
1179 enum gpiod_edge_event_type {
1180 GPIOD_EDGE_EVENT_RISING_EDGE = 1,
1181 /**< Rising edge event. */
1182 GPIOD_EDGE_EVENT_FALLING_EDGE,
1183 /**< Falling edge event. */
1184 };
1185
1186 /**
1187 * @brief Free the edge event object.
1188 * @param event Edge event object to free.
1189 */
1190 void gpiod_edge_event_free(struct gpiod_edge_event *event);
1191
1192 /**
1193 * @brief Copy the edge event object.
1194 * @param event Edge event to copy.
1195 * @return Copy of the edge event or NULL on error. The returned object must
1196 * be freed by the caller using ::gpiod_edge_event_free.
1197 */
1198 struct gpiod_edge_event *gpiod_edge_event_copy(struct gpiod_edge_event *event);
1199
1200 /**
1201 * @brief Get the event type.
1202 * @param event GPIO edge event.
1203 * @return The event type (::GPIOD_EDGE_EVENT_RISING_EDGE or
1204 * ::GPIOD_EDGE_EVENT_FALLING_EDGE).
1205 */
1206 enum gpiod_edge_event_type
1207 gpiod_edge_event_get_event_type(struct gpiod_edge_event *event);
1208
1209 /**
1210 * @brief Get the timestamp of the event.
1211 * @param event GPIO edge event.
1212 * @return Timestamp in nanoseconds.
1213 * @note The source clock for the timestamp depends on the event_clock
1214 * setting for the line.
1215 */
1216 uint64_t gpiod_edge_event_get_timestamp_ns(struct gpiod_edge_event *event);
1217
1218 /**
1219 * @brief Get the offset of the line which triggered the event.
1220 * @param event GPIO edge event.
1221 * @return Line offset.
1222 */
1223 unsigned int gpiod_edge_event_get_line_offset(struct gpiod_edge_event *event);
1224
1225 /**
1226 * @brief Get the global sequence number of the event.
1227 * @param event GPIO edge event.
1228 * @return Sequence number of the event in the series of events for all lines
1229 * in the associated line request.
1230 */
1231 unsigned long gpiod_edge_event_get_global_seqno(struct gpiod_edge_event *event);
1232
1233 /**
1234 * @brief Get the event sequence number specific to the line.
1235 * @param event GPIO edge event.
1236 * @return Sequence number of the event in the series of events only for this
1237 * line within the lifetime of the associated line request.
1238 */
1239 unsigned long gpiod_edge_event_get_line_seqno(struct gpiod_edge_event *event);
1240
1241 /**
1242 * @brief Create a new edge event buffer.
1243 * @param capacity Number of events the buffer can store (min = 1, max = 1024).
1244 * @return New edge event buffer or NULL on error.
1245 * @note If capacity equals 0, it will be set to a default value of 64. If
1246 * capacity is larger than 1024, it will be limited to 1024.
1247 * @note The user space buffer is independent of the kernel buffer
1248 * (::gpiod_request_config_set_event_buffer_size). As the user space
1249 * buffer is filled from the kernel buffer, there is no benefit making
1250 * the user space buffer larger than the kernel buffer.
1251 * The default kernel buffer size for each request is (16 * num_lines).
1252 */
1253 struct gpiod_edge_event_buffer *
1254 gpiod_edge_event_buffer_new(size_t capacity);
1255
1256 /**
1257 * @brief Get the capacity (the max number of events that can be stored) of
1258 * the event buffer.
1259 * @param buffer Edge event buffer.
1260 * @return The capacity of the buffer.
1261 */
1262 size_t
1263 gpiod_edge_event_buffer_get_capacity(struct gpiod_edge_event_buffer *buffer);
1264
1265 /**
1266 * @brief Free the edge event buffer and release all associated resources.
1267 * @param buffer Edge event buffer to free.
1268 */
1269 void gpiod_edge_event_buffer_free(struct gpiod_edge_event_buffer *buffer);
1270
1271 /**
1272 * @brief Get an event stored in the buffer.
1273 * @param buffer Edge event buffer.
1274 * @param index Index of the event in the buffer.
1275 * @return Pointer to an event stored in the buffer. The lifetime of the
1276 * event is tied to the buffer object. Users must not free the event
1277 * returned by this function.
1278 * @warning Thread-safety:
1279 * Since events are tied to the buffer instance, different threads
1280 * may not operate on the buffer and any associated events at the same
1281 * time. Events can be copied using ::gpiod_edge_event_copy in order
1282 * to create a standalone objects - which each may safely be used from
1283 * a different thread concurrently.
1284 */
1285 struct gpiod_edge_event *
1286 gpiod_edge_event_buffer_get_event(struct gpiod_edge_event_buffer *buffer,
1287 unsigned long index);
1288
1289 /**
1290 * @brief Get the number of events a buffer has stored.
1291 * @param buffer Edge event buffer.
1292 * @return Number of events stored in the buffer.
1293 */
1294 size_t
1295 gpiod_edge_event_buffer_get_num_events(struct gpiod_edge_event_buffer *buffer);
1296
1297 /**
1298 * @}
1299 *
1300 * @defgroup misc Stuff that didn't fit anywhere else
1301 * @{
1302 *
1303 * Various libgpiod-related functions.
1304 */
1305
1306 /**
1307 * @brief Check if the file pointed to by path is a GPIO chip character device.
1308 * @param path Path to check.
1309 * @return True if the file exists and is either a GPIO chip character device
1310 * or a symbolic link to one.
1311 */
1312 bool gpiod_is_gpiochip_device(const char *path);
1313
1314 /**
1315 * @brief Get the API version of the library as a human-readable string.
1316 * @return A valid pointer to a human-readable string containing the library
1317 * version. The pointer is valid for the lifetime of the program and
1318 * must not be freed by the caller.
1319 */
1320 const char *gpiod_api_version(void);
1321
1322 /**
1323 * @}
1324 */
1325
1326 #ifdef __cplusplus
1327 } /* extern "C" */
1328 #endif
1329
1330 #endif /* __LIBGPIOD_GPIOD_H__ */