libgpiod v2: rewrite tests for the C library
Subsystems (2)
v4 cover libgpiod v2: rewrite tests for the C library
v1 -> v2:
- drop applied patches
- improve documentation
- skip offsets that are overridden in line_config but not actually requested
v2 -> v3:
- add a patch adding an enum for line values
- rework the mechanism for retrieving overrides
- add new "constructors" for line_config
- incorporate part of Kent's points in the documentation
- a bunch of improvements to tests as per Kent's reviews
v3 -> v4:
- drop the new constructors added in v3
- improve the docs for override interfaces
- use NUM_OVERRIDES_MAX when referring to the max number of overrides
Bartosz Golaszewski (2):
line-config: expose the override logic to users
tests: rewrite core C tests using libgpiosim
configure.ac | 8 +- include/gpiod.h | 573 ++++++++++++------ lib/line-config.c | 808 ++++++++++++++++--------- tests/Makefile.am | 22 +- tests/gpiod-test-helpers.c | 49 ++ tests/gpiod-test-helpers.h | 139 +++++ tests/gpiod-test-sim.c | 308 ++++++++++ tests/gpiod-test-sim.h | 42 ++ tests/gpiod-test.c | 233 +------- tests/gpiod-test.h | 83 +-- tests/gpiosim/gpiosim.c | 1 + tests/mockup/Makefile.am | 11 - tests/mockup/gpio-mockup.c | 496 ---------------- tests/mockup/gpio-mockup.h | 36 -- tests/tests-chip.c | 282 ++++----- tests/tests-edge-event.c | 490 +++++++++++++++ tests/tests-event.c | 908 ---------------------------- tests/tests-info-event.c | 301 ++++++++++ tests/tests-line-config.c | 457 ++++++++++++++ tests/tests-line-info.c | 318 ++++++++++ tests/tests-line-request.c | 526 ++++++++++++++++ tests/tests-line.c | 1091 ---------------------------------- tests/tests-misc.c | 80 ++- tests/tests-request-config.c | 90 +++ tools/gpioget.c | 6 +- tools/gpiomon.c | 6 +- tools/gpioset.c | 9 +- 27 files changed, 3878 insertions(+), 3495 deletions(-) create mode 100644 tests/gpiod-test-helpers.c create mode 100644 tests/gpiod-test-helpers.h create mode 100644 tests/gpiod-test-sim.c create mode 100644 tests/gpiod-test-sim.h delete mode 100644 tests/mockup/Makefile.am delete mode 100644 tests/mockup/gpio-mockup.c delete mode 100644 tests/mockup/gpio-mockup.h create mode 100644 tests/tests-edge-event.c delete mode 100644 tests/tests-event.c create mode 100644 tests/tests-info-event.c create mode 100644 tests/tests-line-config.c create mode 100644 tests/tests-line-info.c create mode 100644 tests/tests-line-request.c delete mode 100644 tests/tests-line.c create mode 100644 tests/tests-request-config.c
Signature · 1 line
2.30.1
PATCH v4 1/2 line-config: expose the override logic to users
We've already added getters for line-config but without exposing some parts of the internal logic of the object, the user can't really get the full picture and inspect the contents. This patch reworks the accessors further by providing access to the underlying override mechanism.
For every setting, we expose a getter and setter for the default value as well as a set of four functions for setting, getting, clearing and checking per-offset overrides.
An override can initially have the same value as the defaults but will retain the overridden value should the defaults change.
We also complete the API by providing functions that allow to retrieve the overridden offsets and their corresponding property types.
This way the caller can fully inspect the line_config and high-level language bindings can provide stringification methods.
While at it: we fix a couple bugs in the implementation of struct line_config.
--- include/gpiod.h | 573 +++++++++++++++++++++----------- lib/line-config.c | 808 ++++++++++++++++++++++++++++++---------------- tools/gpioget.c | 6 +- tools/gpiomon.c | 6 +- tools/gpioset.c | 9 +- 5 files changed, 930 insertions(+), 472 deletions(-)
include/gpiod.h +393 -180
@@ -9,6 +9,7 @@#ifndef __LIBGPIOD_GPIOD_H__#define __LIBGPIOD_GPIOD_H__ +#include <stdarg.h> #include <stdbool.h> #include <stdint.h>
@@ -176,6 +177,8 @@ int gpiod_chip_find_line(struct gpiod_chip *chip, const char *name);* @param line_cfg Line config object. * @return New line request object or NULL if an error occurred. The request * must be released by the caller using ::gpiod_line_request_release. +* @note Line configuration overrides set for offsets that don't end up being +* requested are silently ignored. */ struct gpiod_line_request * gpiod_chip_request_lines(struct gpiod_chip *chip,
@@ -458,12 +461,37 @@ gpiod_info_event_get_line_info(struct gpiod_info_event *event);* * The line-config object stores the configuration for lines that can be used * in two cases: when making a line request and when reconfiguring a set of -* already requested lines. The mutators for the line request don't return -* errors. If the set of options is too complex to be translated into kernel -* uAPI structures - the error will be returned at the time of the request or -* reconfiguration. If an invalid value was passed to any of the getters - the -* default value will be silently used instead. Each option can be set -* globally, for a single line offset or for multiple line offsets. +* already requested lines. +* +* A new line-config object is instantiated containing a set of sane defaults +* for all supported configuration settings. Those defaults can be modified by +* the caller. Default values can be overridden by applying different values +* for specific line offsets. When making a request or reconfiguring an +* existing one, the overridden settings for specific offsets will be taken +* into account first and for every other offset and setting the defaults will +* be used. +* +* For every setting there are two mutators (one setting the default and one +* for the per-offset override), two getters (one for reading the global +* default and one for retrieving the effective value for the line at given +* offset), a function for testing if a setting is overridden for the line at +* given offset and finally a function for clearing the overrides (per offset). +* +* The mutators don't return errors. If the set of options is too complex to +* be translated into kernel uAPI structures - the error will be returned at +* the time of the request or reconfiguration. If an invalid value was passed +* to any of the mutators - the default value will be silently used instead. +* +* Operating on offsets in struct line_config has no effect on real GPIOs. It +* only manipulates the object in memory and is only applied to the hardware +* at the time of the request or reconfiguration. +* +* Overrides set for offsets that don't end up being requested are silently +* ignored both in ::gpiod_chip_request_lines as well as in +* ::gpiod_line_request_reconfigure_lines. +* +* In cases where all requested lines are using the global defaults, the +* offsets can be entirely ignored when preparing the line configuration. */ /**
@@ -488,329 +516,428 @@ void gpiod_line_config_free(struct gpiod_line_config *config);void gpiod_line_config_reset (struct gpiod_line_config *config);/** -* @brief Set the direction of all lines. +* @brief Set the default direction setting. * @param config Line config object. * @param direction New direction. */ -void gpiod_line_config_set_direction (struct gpiod_line_config *config, -int direction); +void gpiod_line_config_set_direction_default (struct gpiod_line_config *config, +int direction);/** -* @brief Set the direction for a single line at given offset. +* @brief Set the direction override at given offset. * @param config Line config object. * @param direction New direction. -* @param offset Offset of the line for which to set the direction. +* @param offset Offset of the line for which to set the override. */ -void gpiod_line_config_set_direction_offset (struct gpiod_line_config *config, -int direction,unsigned int offset); +void gpiod_line_config_set_direction_override (struct gpiod_line_config *config, +int direction, +unsigned int offset);/** -* @brief Set the direction for a subset of lines. +* @brief Clear the direction override at given offset. * @param config Line config object. -* @param direction New direction. -* @param num_offsets Number of offsets in the array. -* @param offsets Array of line offsets for which to set the direction. +* @param offset Offset of the line for which to clear the override. +* @note Does nothing if no override is set for this line. */ -void gpiod_line_config_set_direction_subset (struct gpiod_line_config *config, -int direction, -unsigned int num_offsets, -const unsigned int *offsets); +void +gpiod_line_config_clear_direction_override (struct gpiod_line_config *config, +unsigned int offset);/** -* @brief Get the direction setting for a given line. +* @brief Check if the direction setting is overridden at given offset. * @param config Line config object. -* @param offset Line offset for which to read the direction setting. -* @return Direction setting that would have been used for given offset if the -* config object was used in a request at the time of the call. -* @note If an offset is used for which no config was provided, the function -* will return the global default value. +* @param offset Offset of the line for which to check the override. +* @return True if direction is overridden at this offset, false otherwise. */ -int gpiod_line_config_get_direction (struct gpiod_line_config *config, -unsigned int offset); +bool gpiod_line_config_direction_is_overridden (struct gpiod_line_config *config, +unsigned int offset);/** -* @brief Set the edge event detection for all lines. +* @brief Get the default direction setting. * @param config Line config object. -* @param edge Type of edge events to detect. +* @return Direction setting that would have been used for any non-overridden +* offset. */ -void gpiod_line_config_set_edge_detection (struct gpiod_line_config *config, -int edge); +int gpiod_line_config_get_direction_default (struct gpiod_line_config *config);/** -* @brief Set the edge event detection for a single line at given offset. +* @brief Get the direction setting for the line at given offset. +* @param config Line config object. +* @param offset Offset of the line for which to read the direction setting. +* @return Direction setting that would have been used for the line at given +* offset if the config object was used in a request at the time of the +* call. +*/ +int gpiod_line_config_get_direction_offset (struct gpiod_line_config *config, +unsigned int offset); + +/** +* @brief Set the default edge event detection. * @param config Line config object. * @param edge Type of edge events to detect. -* @param offset Offset of the line for which to set the edge detection. */ void -gpiod_line_config_set_edge_detection_offset (struct gpiod_line_config *config, -int edge,unsigned int offset); +gpiod_line_config_set_edge_detection_default (struct gpiod_line_config *config, +int edge);/** -* @brief Set the edge event detection for a subset of lines. +* @brief Set the edge detection override at given offset. * @param config Line config object. * @param edge Type of edge events to detect. -* @param num_offsets Number of offsets in the array. -* @param offsets Array of line offsets for which to set the edge detection. +* @param offset Offset of the line for which to set the override. +*/ +void +gpiod_line_config_set_edge_detection_override (struct gpiod_line_config *config, +int edge,unsigned int offset); + +/** +* @brief Clear the edge detection override at given offset. +* @param config Line config object. +* @param offset Offset of the line for which to clear the override. +* @note Does nothing if no override is set for this line. */ void -gpiod_line_config_set_edge_detection_subset (struct gpiod_line_config *config, -int edge,unsigned int num_offsets, -const unsigned int *offsets); +gpiod_line_config_clear_edge_detection_override ( +struct gpiod_line_config *config,unsigned int offset); + +/** +* @brief Check if the edge detection setting is overridden at given offset. +* @param config Line config object. +* @param offset Offset of the line for which to check the override. +* @return True if edge detection is overridden at this offset, false otherwise. +*/ +bool +gpiod_line_config_edge_detection_is_overridden (struct gpiod_line_config *config, +unsigned int offset);/** -* @brief Get the edge event detection setting for a given line. +* @brief Get the default edge detection setting. * @param config Line config object. -* @param offset Line offset for which to read the edge event detection setting. +* @return Edge detection setting that would have been used for any offset not +* assigned its own direction value. +*/ +int +gpiod_line_config_get_edge_detection_default (struct gpiod_line_config *config); + +/** +* @brief Get the edge event detection setting for a given offset. +* @param config Line config object. +* @param offset Offset of the line for which to read the edge event detection +* setting. * @return Edge event detection setting that would have been used for given * offset if the config object was used in a request at the time of * the call. -* @note If an offset is used for which no config was provided, the function -* will return the global default value. */ -int gpiod_line_config_get_edge_detection (struct gpiod_line_config *config, -unsigned int offset); +int +gpiod_line_config_get_edge_detection_offset (struct gpiod_line_config *config, +unsigned int offset);/** -* @brief Set the bias of all lines. +* @brief Set the default bias setting. * @param config Line config object. * @param bias New bias. */ -void gpiod_line_config_set_bias (struct gpiod_line_config *config,int bias); +void gpiod_line_config_set_bias_default (struct gpiod_line_config *config, +int bias);/** -* @brief Set the bias for a single line at given offset. +* @brief Set the bias override at given offset. * @param config Line config object. -* @param bias New bias. -* @param offset Offset of the line for which to set the bias. +* @param bias New bias setting. +* @param offset Offset of the line for which to set the override. */ -void gpiod_line_config_set_bias_offset (struct gpiod_line_config *config, -int bias,unsigned int offset); +void gpiod_line_config_set_bias_override (struct gpiod_line_config *config, +int bias,unsigned int offset);/** -* @brief Set the bias for a subset of lines. +* @brief Clear the bias override at given offset. * @param config Line config object. -* @param bias New bias. -* @param num_offsets Number of offsets in the array. -* @param offsets Array of line offsets for which to set the bias. +* @param offset Offset of the line for which to clear the override. +* @note Does nothing if no override is set for this line. */ -void gpiod_line_config_set_bias_subset (struct gpiod_line_config *config, -int bias,unsigned int num_offsets, -const unsigned int *offsets); +void gpiod_line_config_clear_bias_override (struct gpiod_line_config *config, +unsigned int offset);/** -* @brief Get the bias setting for a given line. +* @brief Check if the bias setting is overridden at given offset. * @param config Line config object. -* @param offset Line offset for which to read the bias setting. -* @return Bias setting that would have been used for given offset if the -* config object was used in a request at the time of the call. -* @note If an offset is used for which no config was provided, the function -* will return the global default value. +* @param offset Offset of the line for which to check the override. +* @return True if bias is overridden at this offset, false otherwise. */ -int gpiod_line_config_get_bias (struct gpiod_line_config *config, +bool gpiod_line_config_bias_is_overridden (struct gpiod_line_config *config, +unsigned int offset); +/** +* @brief Get the default bias setting. +* @param config Line config object. +* @return Bias setting that would have been used for any offset not assigned +* its own direction value. +*/ +int gpiod_line_config_get_bias_default (struct gpiod_line_config *config); + +/** +* @brief Get the bias setting for a given offset. +* @param config Line config object. +* @param offset Offset of the line for which to read the bias setting. +* @return Bias setting that would have been used for the line at given offset +* if the config object was used in a request at the time of the call. +*/ +int gpiod_line_config_get_bias_offset (struct gpiod_line_config *config,unsigned int offset);/** -* @brief Set the drive of all lines. +* @brief Set the default drive setting. * @param config Line config object. * @param drive New drive. */ -void gpiod_line_config_set_drive (struct gpiod_line_config *config,int drive); +void gpiod_line_config_set_drive_default (struct gpiod_line_config *config, +int drive);/** -* @brief Set the drive for a single line at given offset. +* @brief Set the drive override at given offset. * @param config Line config object. -* @param drive New drive. -* @param offset Offset of the line for which to set the drive. +* @param drive New drive setting. +* @param offset Offset of the line for which to set the override. */ -void gpiod_line_config_set_drive_offset (struct gpiod_line_config *config, -int drive,unsigned int offset); +void gpiod_line_config_set_drive_override (struct gpiod_line_config *config, +int drive,unsigned int offset);/** -* @brief Set the drive for a subset of lines. +* @brief Clear the drive override at given offset. * @param config Line config object. -* @param drive New drive. -* @param num_offsets Number of offsets in the array. -* @param offsets Array of line offsets for which to set the drive. +* @param offset Offset of the line for which to clear the override. +* @note Does nothing if no override is set for this line. */ -void gpiod_line_config_set_drive_subset (struct gpiod_line_config *config, -int drive,unsigned int num_offsets, -const unsigned int *offsets); +void gpiod_line_config_clear_drive_override (struct gpiod_line_config *config, +unsigned int offset);/** -* @brief Get the drive setting for a given line. +* @brief Check if the drive setting is overridden at given offset. * @param config Line config object. -* @param offset Line offset for which to read the drive setting. -* @return Drive setting that would have been used for given offset if the -* config object was used in a request at the time of the call. -* @note If an offset is used for which no config was provided, the function -* will return the global default value. +* @param offset Offset of the line for which to check the override. +* @return True if drive is overridden at this offset, false otherwise. */ -int gpiod_line_config_get_drive (struct gpiod_line_config *config, -unsigned int offset); +bool gpiod_line_config_drive_is_overridden (struct gpiod_line_config *config, +unsigned int offset);/** -* @brief Set all lines as active-low. +* @brief Get the default drive setting. * @param config Line config object. +* @return Drive setting that would have been used for any offset not assigned +* its own direction value. */ -void gpiod_line_config_set_active_low (struct gpiod_line_config *config); +int gpiod_line_config_get_drive_default (struct gpiod_line_config *config);/** -* @brief Set a single line as active-low. +* @brief Get the drive setting for a given offset. * @param config Line config object. -* @param offset Offset of the line for which to set the active setting. +* @param offset Offset of the line for which to read the drive setting. +* @return Drive setting that would have been used for the line at given offset +* if the config object was used in a request at the time of the call. */ -void gpiod_line_config_set_active_low_offset (struct gpiod_line_config *config, -unsigned int offset); +int gpiod_line_config_get_drive_offset (struct gpiod_line_config *config, +unsigned int offset);/** -* @brief Set a subset of lines as active-low. +* @brief Set lines to active-low by default. * @param config Line config object. -* @param num_offsets Number of offsets in the array. -* @param offsets Array of line offsets for which to set the active setting. +* @param active_low New active-low setting. */ -void gpiod_line_config_set_active_low_subset (struct gpiod_line_config *config, -unsigned int num_offsets, -const unsigned int *offsets); +void gpiod_line_config_set_active_low_default (struct gpiod_line_config *config, +bool active_low);/** -* @brief Check if the line at given offset was configured as active-low. +* @brief Override the active-low setting at given offset. * @param config Line config object. -* @param offset Line offset for which to read the active-low setting. -* @return Active-low setting that would have been used for given offset if the -* config object was used in a request at the time of the call. -* @note If an offset is used for which no config was provided, the function -* will return the global default value. +* @param active_low New active-low setting. +* @param offset Offset of the line for which to set the override. */ -bool gpiod_line_config_get_active_low (struct gpiod_line_config *config, -unsigned int offset); +void gpiod_line_config_set_active_low_override (struct gpiod_line_config *config, +bool active_low, +unsigned int offset);/** -* @brief Set all lines as active-high. +* @brief Clear the active-low override at given offset. * @param config Line config object. +* @param offset Offset of the line for which to clear the override. +* @note Does nothing if no override is set for this line. */ -void gpiod_line_config_set_active_high (struct gpiod_line_config *config); +void +gpiod_line_config_clear_active_low_override (struct gpiod_line_config *config, +unsigned int offset);/** -* @brief Set a single line as active-high. +* @brief Check if the active-low setting is overridden at given offset. * @param config Line config object. -* @param offset Offset of the line for which to set the active setting. +* @param offset Offset of the line for which to check the override. +* @return True if active-low is overridden at this offset, false otherwise. */ -void gpiod_line_config_set_active_high_offset (struct gpiod_line_config *config, -unsigned int offset); +bool +gpiod_line_config_active_low_is_overridden (struct gpiod_line_config *config, +unsigned int offset);/** -* @brief Set a subset of lines as active-high. +* @brief Check if active-low is the default setting. * @param config Line config object. -* @param num_offsets Number of offsets in the array. -* @param offsets Array of line offsets for which to set the active setting. +* @return Active-low setting that would have been used for any offset not +* assigned its own value. */ -void gpiod_line_config_set_active_high_subset (struct gpiod_line_config *config, -unsigned int num_offsets, -const unsigned int *offsets); +bool gpiod_line_config_get_active_low_default (struct gpiod_line_config *config);/** -* @brief Set the debounce period for all lines. +* @brief Check if the line at given offset was configured as active-low. * @param config Line config object. -* @param period New debounce period. Disables debouncing if 0. +* @param offset Offset of the line for which to read the active-low setting. +* @return Active-low setting that would have been used for the line at given +* offset if the config object was used in a request at the time of the +* call. */ -void gpiod_line_config_set_debounce_period_us (struct gpiod_line_config *config, -unsigned long period); +bool gpiod_line_config_get_active_low_offset (struct gpiod_line_config *config, +unsigned int offset);/** -* @brief Set the debounce period for a single line at given offset. +* @brief Set the default debounce period. * @param config Line config object. * @param period New debounce period. Disables debouncing if 0. -* @param offset Offset of the line for which to set the debounce period. +*/ +void gpiod_line_config_set_debounce_period_us_default ( +struct gpiod_line_config *config,unsigned long period); + +/** +* @brief Override the debounce period setting at given offset. +* @param config Line config object. +* @param period New debounce period in microseconds. +* @param offset Offset of the line for which to set the override. */ void -gpiod_line_config_set_debounce_period_us_offset ( +gpiod_line_config_set_debounce_period_us_override (struct gpiod_line_config *config,unsigned long period,unsigned int offset);/** -* @brief Set the debounce period for a subset of lines. +* @brief Clear the debounce period override at given offset. * @param config Line config object. -* @param period New debounce period. Disables debouncing if 0. -* @param num_offsets Number of offsets in the array. -* @param offsets Array of line offsets for which to set the debounce period. +* @param offset Offset of the line for which to clear the override. +* @note Does nothing if no override is set for this line. */ -void -gpiod_line_config_set_debounce_period_us_subset ( +void gpiod_line_config_clear_debounce_period_us_override (struct gpiod_line_config *config, -unsigned long period, -unsigned int num_offsets, -const unsigned int *offsets); +unsigned int offset); + +/** +* @brief Check if the debounce period setting is overridden at given offset. +* @param config Line config object. +* @param offset Offset of the line for which to check the override. +* @return True if debounce period is overridden at this offset, false +* otherwise. +*/ +bool gpiod_line_config_debounce_period_us_is_overridden ( +struct gpiod_line_config *config, +unsigned int offset); + +/** +* @brief Get the default debounce period. +* @param config Line config object. +* @return Debounce period that would have been used for any offset not +* assigned its own debounce period. 0 if not debouncing is disabled. +*/ +unsigned long gpiod_line_config_get_debounce_period_us_default ( +struct gpiod_line_config *config);/** -* @brief Get the debounce period for a given line. +* @brief Get the debounce period for a given offset. * @param config Line config object. -* @param offset Line offset for which to read the debounce period. -* @return Debounce period that would have been used for given offset if the -* config object was used in a request at the time of the call. -* @note If an offset is used for which no config was provided, the function -* will return the global default value. +* @param offset Offset of the line for which to read the debounce period. +* @return Debounce period that would have been used for the line at given +* offset if the config object was used in a request at the time of +* the call. 0 if debouncing is disabled. */ unsigned long -gpiod_line_config_get_debounce_us_period (struct gpiod_line_config *config, -unsigned int offset); +gpiod_line_config_get_debounce_period_us_offset ( +struct gpiod_line_config *config,unsigned int offset);/** -* @brief Set the event timestamp clock for all lines. +* @brief Set the default event timestamp clock. * @param config Line config object. * @param clock New clock to use. */ -void gpiod_line_config_set_event_clock (struct gpiod_line_config *config, -int clock); +void gpiod_line_config_set_event_clock_default (struct gpiod_line_config *config, +int clock);/** -* @brief Set the event clock for a single line at given offset. +* @brief Override the event clock setting at given offset. * @param config Line config object. * @param clock New event clock to use. -* @param offset Offset of the line for which to set the event clock type. +* @param offset Offset of the line for which to set the override. */ -void gpiod_line_config_set_event_clock_offset (struct gpiod_line_config *config, -int clock,unsigned int offset); +void +gpiod_line_config_set_event_clock_override (struct gpiod_line_config *config, +int clock,unsigned int offset);/** -* @brief Set the event clock for a subset of lines. +* @brief Clear the event clock override at given offset. * @param config Line config object. -* @param clock New event clock to use. -* @param num_offsets Number of offsets in the array. -* @param offsets Array of line offsets for which to set the event clock type. +* @param offset Offset of the line for which to clear the override. +* @note Does nothing if no override is set for this line. +*/ +void +gpiod_line_config_clear_event_clock_override (struct gpiod_line_config *config, +unsigned int offset); + +/** +* @brief Check if the event clock setting is overridden at given offset. +* @param config Line config object. +* @param offset Offset of the line for which to check the override. +* @return True if event clock period is overridden at this offset, false +* otherwise. +*/ +bool +gpiod_line_config_event_clock_is_overridden (struct gpiod_line_config *config, +unsigned int offset); + +/** +* @brief Get the default event clock setting. +* @param config Line config object. +* @return Event clock setting that would have been used for any offset not +* assigned its own direction value. */ -void gpiod_line_config_set_event_clock_subset (struct gpiod_line_config *config, -int clock, -unsigned int num_offsets, -const unsigned int *offsets); +int gpiod_line_config_get_event_clock_default (struct gpiod_line_config *config);/** -* @brief Get the event clock setting for a given line. +* @brief Get the event clock setting for a given offset. * @param config Line config object. -* @param offset Line offset for which to read the event clock setting. -* @return Event clock setting that would have been used for given offset if -* the config object was used in a request at the time of the call. -* @note If an offset is used for which no config was provided, the function -* will return the global default value. +* @param offset Offset of the line for which to read the event clock setting. +* @return Event clock setting that would have been used for the line at given +* offset if the config object was used in a request at the time of the +* call. */ -int gpiod_line_config_get_event_clock (struct gpiod_line_config *config, -unsigned int offset); +int gpiod_line_config_get_event_clock_offset (struct gpiod_line_config *config, +unsigned int offset);/** -* @brief Set the output value for a single offset. +* @brief Set the default output value. +* @param config Line config object. +* @param value New value. +*/ +void +gpiod_line_config_set_output_value_default (struct gpiod_line_config *config, +int value); + +/** +* @brief Override the output value for a single offset. * @param config Line config object. * @param offset Offset of the line. * @param value Output value to set. */ -void gpiod_line_config_set_output_value (struct gpiod_line_config *config, -unsigned int offset,int value); +void +gpiod_line_config_set_output_value_override (struct gpiod_line_config *config, +unsigned int offset,int value);/** -* @brief Set the output values for a set of offsets. +* @brief Override the output values for multiple offsets. * @param config Line config object. -* @param num_values Number of offsets for which to set values. -* @param offsets Array of line offsets to set values for. +* @param num_values Number of offsets for which to override values. +* @param offsets Array of line offsets to override values for. * @param values Array of output values associated with the offsets passed in * the previous argument. */
@@ -819,14 +946,98 @@ void gpiod_line_config_set_output_values(struct gpiod_line_config *config,const unsigned int *offsets, const int *values); +/** +* @brief Clear the output value override at given offset. +* @param config Line config object. +* @param offset Offset of the line for which to clear the override. +* @note Does nothing if no override is set for this line. +*/ +void +gpiod_line_config_clear_output_value_override (struct gpiod_line_config *config, +unsigned int offset); + +/** +* @brief Check if the output value is overridden at given offset. +* @param config Line config object. +* @param offset Offset of the line for which to check the override. +* @return True if output value period is overridden at this offset, false +* otherwise. +*/ +bool +gpiod_line_config_output_value_is_overridden (struct gpiod_line_config *config, +unsigned int offset); + +/** +* @brief Get the default output value. +* @param config Line config object. +* @return Output value that would have been used for any offset not +* assigned its own output value. +*/ +int +gpiod_line_config_get_output_value_default (struct gpiod_line_config *config); +/** * @brief Get the output value configured for a given line. * @param config Line config object. * @param offset Line offset for which to read the value. -* @return 1 or 0 if the value was configured for this line, -1 otherwise. +* @return Output value that would have been used for the line at given offset +* if the config object was used in a request at the time of the call. */ -int gpiod_line_config_get_output_value (struct gpiod_line_config *config, -unsigned int offset); +int gpiod_line_config_get_output_value_offset (struct gpiod_line_config *config, +unsigned int offset); + +/** +* @brief List of properties that can be stored in a line_config object. +* +* Used when retrieving the overrides. +*/ +enum { +GPIOD_LINE_CONFIG_PROP_END =0 , +/**< Delimiter. */ +GPIOD_LINE_CONFIG_PROP_DIRECTION , +/**< Line direction. */ +GPIOD_LINE_CONFIG_PROP_EDGE , +/**< Edge detection. */ +GPIOD_LINE_CONFIG_PROP_BIAS , +/**< Bias. */ +GPIOD_LINE_CONFIG_PROP_DRIVE , +/**< Drive. */ +GPIOD_LINE_CONFIG_PROP_ACTIVE_LOW , +/**< Active-low setting. */ +GPIOD_LINE_CONFIG_PROP_DEBOUNCE_PERIOD , +/** Debounce period. */ +GPIOD_LINE_CONFIG_PROP_EVENT_CLOCK , +/**< Event clock type. */ +GPIOD_LINE_CONFIG_PROP_OUTPUT_VALUE , +/**< Output value. */ +}; + +/** +* @brief Get the total number of overridden settings currently stored by this +* line config object. +* @param config Line config object. +* @return Number of individual overridden settings. +*/ +unsigned int +gpiod_line_config_get_num_overrides (struct gpiod_line_config *config); + +/** +* @brief Get the list of overridden offsets and the corresponding types of +* overridden settings. +* @param config Line config object. +* @param offsets Array to store the overidden offsets. Must hold at least the +* number of unsigned integers returned by +* ::gpiod_line_config_get_num_overrides. +* @param props Array to store the types of overridden settings. Must hold at +* least the number of integers returned by +* ::gpiod_line_config_get_num_overrides. +* +* The overridden offsets are stored in the offsets array with the array index +* corresponding with the property stored in the props array at the same index. +*/ +void +gpiod_line_config_get_overrides (struct gpiod_line_config *config, +unsigned int *offsets,int *props);/** * @}
@@ -1028,6 +1239,8 @@ int gpiod_line_request_set_values(struct gpiod_line_request *request,* @param request GPIO line request. * @param config New line config to apply. * @return 0 on success, -1 on failure. +* @note Line configuration overrides set for offsets that don't end up being +* requested are silently ignored. */ int gpiod_line_request_reconfigure_lines (struct gpiod_line_request *request,struct gpiod_line_config *config);
lib/line-config.c +526 -282
1,157 lines not on this page · raw message
tools/gpioget.c +3 -3
@@ -110,13 +110,13 @@ int main(int argc, char **argv)if (!line_cfg)die_perror ("unable to allocate the line config structure" ); -gpiod_line_config_set_direction (line_cfg, direction); +gpiod_line_config_set_direction_default (line_cfg, direction);if (bias) -gpiod_line_config_set_bias (line_cfg, bias); +gpiod_line_config_set_bias_default (line_cfg, bias);if (active_low) -gpiod_line_config_set_active_low (line_cfg); +gpiod_line_config_set_active_low_default (line_cfg, true); req_cfg =gpiod_request_config_new ();if (!req_cfg)
tools/gpiomon.c +3 -3
@@ -259,10 +259,10 @@ int main(int argc, char **argv)die_perror ("unable to allocate the line config structure" );if (bias) -gpiod_line_config_set_bias (line_cfg, bias); +gpiod_line_config_set_bias_default (line_cfg, bias);if (active_low) -gpiod_line_config_set_active_low (line_cfg); -gpiod_line_config_set_edge_detection (line_cfg, edge); +gpiod_line_config_set_active_low_default (line_cfg, true); +gpiod_line_config_set_edge_detection_default (line_cfg, edge); req_cfg =gpiod_request_config_new ();if (!req_cfg)
tools/gpioset.c +5 -4
@@ -296,12 +296,13 @@ int main(int argc, char **argv)die_perror ("unable to allocate the line config structure" );if (bias) -gpiod_line_config_set_bias (line_cfg, bias); +gpiod_line_config_set_bias_default (line_cfg, bias);if (drive) -gpiod_line_config_set_drive (line_cfg, drive); +gpiod_line_config_set_drive_default (line_cfg, drive);if (active_low) -gpiod_line_config_set_active_low (line_cfg); -gpiod_line_config_set_direction (line_cfg,GPIOD_LINE_DIRECTION_OUTPUT ); +gpiod_line_config_set_active_low_default (line_cfg, true); +gpiod_line_config_set_direction_default (line_cfg, +GPIOD_LINE_DIRECTION_OUTPUT );gpiod_line_config_set_output_values (line_cfg, num_lines, offsets, values);
Signature · 1 line
2.30.1
PATCH v4 2/2 tests: rewrite core C tests using libgpiosim
In reply to Bartosz GolaszewskiThis replaces the old tests for the C API v1 based on gpio-mockup with a test suite based on gpio-sim that covers around 95% of the libgpiod v2 codebase.
The test harness has been rebuilt and shrank significantly as well. The libgpiosim API has been wrapped in a gobject interface.
--- configure.ac | 8 +- tests/Makefile.am | 22 +- tests/gpiod-test-helpers.c | 49 ++ tests/gpiod-test-helpers.h | 139 +++++ tests/gpiod-test-sim.c | 308 ++++++++++ tests/gpiod-test-sim.h | 42 ++ tests/gpiod-test.c | 233 +------- tests/gpiod-test.h | 83 +-- tests/gpiosim/gpiosim.c | 1 + tests/mockup/Makefile.am | 11 - tests/mockup/gpio-mockup.c | 496 ---------------- tests/mockup/gpio-mockup.h | 36 -- tests/tests-chip.c | 282 ++++----- tests/tests-edge-event.c | 490 +++++++++++++++ tests/tests-event.c | 908 ---------------------------- tests/tests-info-event.c | 301 ++++++++++ tests/tests-line-config.c | 457 ++++++++++++++ tests/tests-line-info.c | 318 ++++++++++ tests/tests-line-request.c | 526 ++++++++++++++++ tests/tests-line.c | 1091 ---------------------------------- tests/tests-misc.c | 80 ++- tests/tests-request-config.c | 90 +++ 22 files changed, 2948 insertions(+), 3023 deletions(-) create mode 100644 tests/gpiod-test-helpers.c create mode 100644 tests/gpiod-test-helpers.h create mode 100644 tests/gpiod-test-sim.c create mode 100644 tests/gpiod-test-sim.h delete mode 100644 tests/mockup/Makefile.am delete mode 100644 tests/mockup/gpio-mockup.c delete mode 100644 tests/mockup/gpio-mockup.h create mode 100644 tests/tests-edge-event.c delete mode 100644 tests/tests-event.c create mode 100644 tests/tests-info-event.c create mode 100644 tests/tests-line-config.c create mode 100644 tests/tests-line-info.c create mode 100644 tests/tests-line-request.c delete mode 100644 tests/tests-line.c create mode 100644 tests/tests-request-config.c
configure.ac +3 -5
@@ -28,9 +28,8 @@ AC_SUBST(VERSION_STR, [$PACKAGE_VERSION$EXTRA_VERSION]) AC_SUBST(ABI_VERSION, [4.1.2]) # Have a separate ABI version for C++ bindings: AC_SUBST(ABI_CXX_VERSION, [2.1.1]) -# ABI version for libgpiomockup (we need this since it can be installed if we +# ABI version for libgpiosim (we need this since it can be installed if we # enable install-tests). -AC_SUBST(ABI_MOCKUP_VERSION, [0.1.0]) AC_SUBST(ABI_GPIOSIM_VERSION, [0.1.0]) AC_CONFIG_AUX_DIR([autostuff])
@@ -138,14 +137,14 @@ AC_DEFUN([FUNC_NOT_FOUND_TESTS], if test "x$with_tests" = xtrue then - # For libgpiomockup & libgpiosim + # For libgpiosim AC_CHECK_FUNC([qsort], [], [FUNC_NOT_FOUND_TESTS([qsort])]) PKG_CHECK_MODULES([KMOD], [libkmod >= 18]) - PKG_CHECK_MODULES([UDEV], [libudev >= 215]) PKG_CHECK_MODULES([MOUNT], [mount >= 2.33.1]) # For core library tests PKG_CHECK_MODULES([GLIB], [glib-2.0 >= 2.50]) + PKG_CHECK_MODULES([GOBJECT], [gobject-2.0 >= 2.50]) if test "x$with_tools" = xtrue then
@@ -236,7 +235,6 @@ AC_CONFIG_FILES([Makefile lib/libgpiod.pc tools/Makefile tests/Makefile - tests/mockup/Makefile tests/gpiosim/Makefile bindings/cxx/libgpiodcxx.pc bindings/Makefile
tests/Makefile.am +15 -7
@@ -1,24 +1,32 @@ # SPDX-License-Identifier: GPL-2.0-or-later # SPDX-FileCopyrightText: 2017-2022 Bartosz Golaszewski <[email protected]> -SUBDIRS = mockup gpiosim +SUBDIRS = gpiosim AM_CFLAGS = -I$(top_srcdir)/include/ -I$(top_srcdir)/tests/gpiosim/ AM_CFLAGS += -include $(top_builddir)/config.h -AM_CFLAGS += -Wall -Wextra -g -std=gnu89 $(GLIB_CFLAGS) +AM_CFLAGS += -Wall -Wextra -g -std=gnu89 $(GLIB_CFLAGS) $(GOBJECT_CFLAGS) AM_CFLAGS += -DG_LOG_DOMAIN=\"gpiod-test\" AM_CFLAGS += $(PROFILING_CFLAGS) -AM_LDFLAGS = -pthread $(PROFILING_LDFLAGS) +AM_LDFLAGS = -pthread LDADD = $(top_builddir)/lib/libgpiod.la LDADD += $(top_builddir)/tests/gpiosim/libgpiosim.la -LDADD += $(GLIB_LIBS) +LDADD += $(GLIB_LIBS) $(GOBJECT_LIBS) bin_PROGRAMS = gpiod-test gpiod_test_SOURCES = \ gpiod-test.c \ gpiod-test.h \ + gpiod-test-helpers.c \ + gpiod-test-helpers.h \ + gpiod-test-sim.c \ + gpiod-test-sim.h \ tests-chip.c \ - tests-event.c \ - tests-line.c \ - tests-misc.c + tests-edge-event.c \ + tests-info-event.c \ + tests-line-config.c \ + tests-line-info.c \ + tests-line-request.c \ + tests-misc.c \ + tests-request-config.c
tests/gpiod-test-helpers.c new file +49 -0
49 lines not on this page · raw message
tests/gpiod-test-helpers.h new file +139 -0
139 lines not on this page · raw message
tests/gpiod-test-sim.c new file +308 -0
308 lines not on this page · raw message
tests/gpiod-test-sim.h new file +42 -0
42 lines not on this page · raw message
tests/gpiod-test.c +9 -224
276 lines not on this page · raw message
tests/gpiod-test.h +15 -68
101 lines not on this page · raw message
tests/gpiosim/gpiosim.c +1 -0
@@ -736,6 +736,7 @@ static void bank_release(struct refcount *ref)unsigned int i;char buf[64 ]; +/* FIXME should be based on dirent because num_lines can change. */ for (i =0 ; i < bank->num_lines; i++) {snprintf (buf,sizeof (buf),"line%u/hog" , i);unlinkat (bank->cfs_dir_fd, buf,AT_REMOVEDIR );
tests/mockup/Makefile.am deleted +0 -11
@@ -1,11 +0,0 @@ -# SPDX-License-Identifier: GPL-2.0-or-later -# SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski <[email protected]> - -lib_LTLIBRARIES = libgpiomockup.la - -libgpiomockup_la_SOURCES = gpio-mockup.c gpio-mockup.h -libgpiomockup_la_CFLAGS = -Wall -Wextra -g -fvisibility=hidden -std=gnu89 -libgpiomockup_la_CFLAGS += -include $(top_builddir)/config.h -libgpiomockup_la_CFLAGS += $(KMOD_CFLAGS) $(UDEV_CFLAGS) -libgpiomockup_la_LDFLAGS = -version-info $(subst .,:,$(ABI_MOCKUP_VERSION)) -libgpiomockup_la_LDFLAGS += $(KMOD_LIBS) $(UDEV_LIBS)
tests/mockup/gpio-mockup.c deleted +0 -496
496 lines not on this page · raw message
tests/mockup/gpio-mockup.h deleted +0 -36
@@ -1,36 +0,0 @@ -/* SPDX-License-Identifier: LGPL-2.1-or-later */ -/* SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski <[email protected]> */ - -#ifndef __GPIO_MOCKUP_H__ -#define __GPIO_MOCKUP_H__ - -#ifdef __cplusplus -extern "C" { -#endif - -struct gpio_mockup ; - -#define GPIO_MOCKUP_FLAG_NAMED_LINES (1 << 0) - -struct gpio_mockup *gpio_mockup_new (void ); -void gpio_mockup_ref (struct gpio_mockup *ctx); -void gpio_mockup_unref (struct gpio_mockup *ctx); - -int gpio_mockup_probe (struct gpio_mockup *ctx,unsigned int num_chips, -const unsigned int *chip_sizes,int flags); -int gpio_mockup_remove (struct gpio_mockup *ctx); - -const char *gpio_mockup_chip_name (struct gpio_mockup *ctx,unsigned int idx); -const char *gpio_mockup_chip_path (struct gpio_mockup *ctx,unsigned int idx); -int gpio_mockup_chip_num (struct gpio_mockup *ctx,unsigned int idx); - -int gpio_mockup_get_value (struct gpio_mockup *ctx, -unsigned int chip_idx,unsigned int line_offset); -int gpio_mockup_set_pull (struct gpio_mockup *ctx,unsigned int chip_idx, -unsigned int line_offset,int pull); - -#ifdef __cplusplus -}/* extern "C" */ -#endif - -#endif /* __GPIO_MOCKUP_H__ */
tests/tests-chip.c +112 -170
341 lines not on this page · raw message
tests/tests-edge-event.c new file +490 -0
490 lines not on this page · raw message
tests/tests-event.c deleted +0 -908
632 lines not on this page · raw message
tests/tests-info-event.c new file +301 -0
301 lines not on this page · raw message
tests/tests-line-config.c new file +457 -0
457 lines not on this page · raw message
tests/tests-line-info.c new file +318 -0
318 lines not on this page · raw message
tests/tests-line-request.c new file +526 -0
526 lines not on this page · raw message
tests/tests-line.c deleted +0 -1091
1,091 lines not on this page · raw message
tests/tests-misc.c +73 -7
80 lines not on this page · raw message
tests/tests-request-config.c new file +90 -0
90 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.30.1
Part of the diff is not shown here. View the raw message.
Quoted 12 lines · Bartosz Golaszewski
v1 -> v2:
- drop applied patches
- improve documentation
- skip offsets that are overridden inline_configbut not actually requestedv2 -> v3:
- add a patch adding an enum for line values
- rework the mechanism for retrieving overrides
- add new "constructors" forline_config
- incorporate part of Kent's points in the documentation
- a bunch of improvements to tests as per Kent's reviews
Quoted 5 lines
v3 -> v4:
- drop the new constructors added in v3
- improve the docs for override interfaces
- useNUM_OVERRIDES_MAXwhen referring to the max number of overrides
Series looks goods to me.
Cheers,
Kent.
Quoted 51 lines
Bartosz Golaszewski (2):
line-config: expose the override logic to users
tests: rewrite core C tests using libgpiosimconfigure.ac | 8 +- include/gpiod.h | 573 ++++++++++++------ lib/line-config.c | 808 ++++++++++++++++--------- tests/Makefile.am | 22 +- tests/gpiod-test-helpers.c | 49 ++ tests/gpiod-test-helpers.h | 139 +++++ tests/gpiod-test-sim.c | 308 ++++++++++ tests/gpiod-test-sim.h | 42 ++ tests/gpiod-test.c | 233 +------- tests/gpiod-test.h | 83 +-- tests/gpiosim/gpiosim.c | 1 + tests/mockup/Makefile.am | 11 - tests/mockup/gpio-mockup.c | 496 ---------------- tests/mockup/gpio-mockup.h | 36 -- tests/tests-chip.c | 282 ++++----- tests/tests-edge-event.c | 490 +++++++++++++++ tests/tests-event.c | 908 ---------------------------- tests/tests-info-event.c | 301 ++++++++++ tests/tests-line-config.c | 457 ++++++++++++++ tests/tests-line-info.c | 318 ++++++++++ tests/tests-line-request.c | 526 ++++++++++++++++ tests/tests-line.c | 1091 ---------------------------------- tests/tests-misc.c | 80 ++- tests/tests-request-config.c | 90 +++ tools/gpioget.c | 6 +- tools/gpiomon.c | 6 +- tools/gpioset.c | 9 +- 27 files changed, 3878 insertions(+), 3495 deletions(-) create mode 100644 tests/gpiod-test-helpers.c create mode 100644 tests/gpiod-test-helpers.h create mode 100644 tests/gpiod-test-sim.c create mode 100644 tests/gpiod-test-sim.h delete mode 100644 tests/mockup/Makefile.am delete mode 100644 tests/mockup/gpio-mockup.c delete mode 100644 tests/mockup/gpio-mockup.h create mode 100644 tests/tests-edge-event.c delete mode 100644 tests/tests-event.c create mode 100644 tests/tests-info-event.c create mode 100644 tests/tests-line-config.c create mode 100644 tests/tests-line-info.c create mode 100644 tests/tests-line-request.c delete mode 100644 tests/tests-line.c create mode 100644 tests/tests-request-config.cSignature · 1 line
2.30.1