libgpiod v2: rewrite tests for the C library
Subsystems (2)
v2 cover libgpiod v2: rewrite tests for the C library
This series contains a rework of the line_config interface and a bit patch that replaces the old test suite for libgpiod v1 based on gpio-mockup with a one covering around 95% of the libgpiod v2 code based on the upcoming gpio-sim module.
v1 -> v2:
- drop applied patches
- improve documentation
- skip offsets that are overridden in line_config but not actually requested
Bartosz Golaszewski (2):
line-config: expose the override logic to users
tests: rewrite core C tests using libgpiosim
configure.ac | 8 +- include/gpiod.h | 509 ++++++++++------ lib/line-config.c | 752 ++++++++++++++--------- tests/Makefile.am | 20 +- tests/gpiod-test-helpers.c | 49 ++ tests/gpiod-test-helpers.h | 139 +++++ tests/gpiod-test-sim.c | 303 ++++++++++ tests/gpiod-test-sim.h | 42 ++ tests/gpiod-test.c | 233 +------- tests/gpiod-test.h | 81 +-- 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 | 420 +++++++++++++ tests/tests-event.c | 908 ---------------------------- tests/tests-info-event.c | 301 ++++++++++ tests/tests-line-config.c | 448 ++++++++++++++ tests/tests-line-info.c | 316 ++++++++++ tests/tests-line-request.c | 468 +++++++++++++++ tests/tests-line.c | 1091 ---------------------------------- tests/tests-misc.c | 86 ++- tests/tests-request-config.c | 90 +++ tools/gpioget.c | 6 +- tools/gpiomon.c | 6 +- tools/gpioset.c | 9 +- 27 files changed, 3630 insertions(+), 3481 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 v2 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 identify offsets for which at least one setting is overriden.
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 | 509 ++++++++++++++++++++----------- lib/line-config.c | 752 +++++++++++++++++++++++++++++----------------- tools/gpioget.c | 6 +- tools/gpiomon.c | 6 +- tools/gpioset.c | 9 +- 5 files changed, 821 insertions(+), 461 deletions(-)
include/gpiod.h +340 -169
@@ -450,12 +450,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 given offset), a +* function for testing if a setting is overridden for 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. */ /**
@@ -480,329 +505,424 @@ 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 Check if the direction 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 direction is overridden at this offset, false otherwise. +*/ +bool gpiod_line_config_direction_is_overridden (struct gpiod_line_config *config, +unsigned int offset);/** -* @brief Get the direction setting for a given line. +* @brief Get the default direction setting. +* @param config Line config object. +* @return Direction setting that would have been used for any non-overridden +* offset. +*/ +int gpiod_line_config_get_direction_default (struct gpiod_line_config *config); + +/** +* @brief Get the direction setting for a 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. */ -int gpiod_line_config_get_direction (struct gpiod_line_config *config, -unsigned int offset); +int gpiod_line_config_get_direction_offset (struct gpiod_line_config *config, +unsigned int offset);/** -* @brief Set the edge event detection for all lines. +* @brief Set the default edge event detection. * @param config Line config object. * @param edge Type of edge events to detect. */ -void gpiod_line_config_set_edge_detection (struct gpiod_line_config *config, -int edge); +void +gpiod_line_config_set_edge_detection_default (struct gpiod_line_config *config, +int edge);/** -* @brief Set the edge event detection for a single line at given offset. +* @brief Set the edge detection override at given offset. * @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. +* @param edge New edge detection setting. +* @param offset Offset of the line for which to set the override. */ void -gpiod_line_config_set_edge_detection_offset (struct gpiod_line_config *config, -int edge,unsigned int offset); +gpiod_line_config_set_edge_detection_override (struct gpiod_line_config *config, +int edge,unsigned int offset);/** -* @brief Set the edge event detection for a subset of lines. +* @brief Clear 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 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. +* @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 Line offset 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_clear_bias_override (struct gpiod_line_config *config, +unsigned int offset); + +/** +* @brief Check if the bias 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 bias is overridden at this offset, false otherwise. +*/ +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. */ -void gpiod_line_config_set_bias_subset (struct gpiod_line_config *config, -int bias,unsigned int num_offsets, -const unsigned int *offsets); +int gpiod_line_config_get_bias_default (struct gpiod_line_config *config);/** -* @brief Get the bias setting for a given line. +* @brief Get the bias setting for a 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. */ -int gpiod_line_config_get_bias (struct gpiod_line_config *config, +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 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. */ -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 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. */ -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. +* 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. */ -void gpiod_line_config_set_event_clock_subset (struct gpiod_line_config *config, -int clock, -unsigned int num_offsets, -const unsigned int *offsets); +bool +gpiod_line_config_event_clock_is_overridden (struct gpiod_line_config *config, +unsigned int offset);/** -* @brief Get the event clock setting for a given line. +* @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. +*/ +int gpiod_line_config_get_event_clock_default (struct gpiod_line_config *config); + +/** +* @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. */ -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 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 Set the output value for a single offset. +* @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. */
@@ -811,14 +931,65 @@ 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 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 Get the number of offsets for which this config object contains +* at least one setting that is different from the defaults. +* @param config Line config object. +* @return Number of offsets with at least one overridden setting. +*/ +unsigned int +gpiod_line_config_get_num_overridden_offsets (struct gpiod_line_config *config); + +/** +* @brief Get the offsets for which this config object contains at least one +* setting that is different from the defaults. +* @param config Line config object. +* @param offsets Array to store the offsets. Must hold at least the number +* of offsets returned by +* ::gpiod_line_config_get_num_overridden_offsets. +*/ +void gpiod_line_config_get_overridden_offsets (struct gpiod_line_config *config, +unsigned int *offsets);/** * @}
lib/line-config.c +470 -282
1,099 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
Re: [libgpiod v2][PATCH v2 1/2] line-config: expose the override logic to users
Quoted 13 lines · Bartosz Golaszewski
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.
Quoted 9 lines
We also complete the API by providing functions that allow to identify offsets for which at least one setting is overriden.
This way the caller can fully inspect the
line_configand high-level language bindings can provide stringification methods.While at it: we fix a couple bugs in the implementation of struct
line_config.
TLDR: I've got a lot of nitpicks on the doco, but it might be simpler for you to apply this patch as is and for me to submit a patch with doco tweaks than for you to try to sort out my comments!
The actual code looks good, so
with or without the suggested doco changes.
Quoted 42 lines
Signed-off-by Bartosz Golaszewski <[email protected]>--- include/gpiod.h | 509 ++++++++++++++++++++----------- lib/line-config.c | 752 +++++++++++++++++++++++++++++----------------- tools/gpioget.c | 6 +- tools/gpiomon.c | 6 +- tools/gpioset.c | 9 +- 5 files changed, 821 insertions(+), 461 deletions(-)include/gpiod.h +19
-6@@ -450,12 +450,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 given offset), a +* function for testing if a setting is overridden for 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
include/gpiod.h gpiod_info_event_get_line_info(struct gpiod_info_event *event); · line 481
Quoted 10 lines
include/gpiod.h +10
-0+* 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. +*
This should be noted in the function documentation, not just here. If only one place, then there not here.
include/gpiod.h void gpiod_line_config_free(struct gpiod_line_config *config); · line 508
Quoted 11 lines
include/gpiod.h +3
-1+* In cases where all requested lines are using the global defaults, the +* offsets can be entirely ignored when preparing the line configuration. */ /** @@ -480,329 +505,424 @@ 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.
Nitpick: "setting" feels redundant given the "Set". Same thoughout, including the "Get" cases.
include/gpiod.h void gpiod_line_config_free(struct gpiod_line_config *config); · line 516
Quoted 11 lines
include/gpiod.h +3
-3* @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.
Nitpick: "at given offset" -> "for the line" throughout. Similarly "at this offset" -> "for this line", and "line at given offset" -> "line",
and "for a given offset" -> "for a line".
Quoted 39 lines
include/gpiod.h +19
-11* @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 Check if the direction 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 direction is overridden at this offset, false otherwise. +*/ +bool gpiod_line_config_direction_is_overridden (struct gpiod_line_config *config, +unsigned int offset);/**
include/gpiod.h void gpiod_line_config_free(struct gpiod_line_config *config); · line 553
Quoted 10 lines
include/gpiod.h +9
-1-* @brief Get the direction setting for a given line. +* @brief Get the default direction setting. +* @param config Line config object. +* @return Direction setting that would have been used for any non-overridden +* offset. +*/ +int gpiod_line_config_get_direction_default (struct gpiod_line_config *config); + +/** +* @brief Get the direction setting for a given offset.
Preferred "given line". The direction is an attribute of the line, not the offset. The offset is the line identifier.
Same throughout.
include/gpiod.h void gpiod_line_config_free(struct gpiod_line_config *config); · line 555
Quoted 2 lines
include/gpiod.h +0
-0* @param config Line config object. * @param offset Line offset for which to read the direction setting.
Nitpick: "Line offset" -> "Offset of the line"
Quoted 22 lines
include/gpiod.h +6
-7* @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. */ -int gpiod_line_config_get_direction (struct gpiod_line_config *config, -unsigned int offset); +int gpiod_line_config_get_direction_offset (struct gpiod_line_config *config, +unsigned int offset);/** -* @brief Set the edge event detection for all lines. +* @brief Set the default edge event detection. * @param config Line config object. * @param edge Type of edge events to detect. */ -void gpiod_line_config_set_edge_detection (struct gpiod_line_config *config, -int edge); +void +gpiod_line_config_set_edge_detection_default (struct gpiod_line_config *config, +int edge);
include/gpiod.h void gpiod_line_config_free(struct gpiod_line_config *config); · line 577
Quoted 10 lines
include/gpiod.h +3
-3/** -* @brief Set the edge event detection for a single line at given offset. +* @brief Set the edge detection override at given offset. * @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. +* @param edge New edge detection setting. +* @param offset Offset of the line for which to set the override. */ void
Nitpicking here, but I prefer the
@param edge Type of edge events to detect.
style, that is an actual short description of the edge parameter, as used above over
@param edge New edge detection setting.
as the latter is very bland boiler plate that doesn't really say anything.
And can the @param edge description be consistent throughout all the edge functions?
Similar elsewhere, on both points.
And again, this is being really nitpicky so feel free to leave it for a subsequent doco tweaking pass.
Quoted 1,749 lines
include/gpiod.h +268
-137-gpiod_line_config_set_edge_detection_offset(struct gpiod_line_config *config, - int edge, unsigned int offset); +gpiod_line_config_set_edge_detection_override(struct gpiod_line_config *config, + int edge, unsigned int offset);/** -* @brief Set the edge event detection for a subset of lines. +* @brief Clear 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 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. +* @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 Line offset 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_clear_bias_override (struct gpiod_line_config *config, +unsigned int offset); + +/** +* @brief Check if the bias 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 bias is overridden at this offset, false otherwise. +*/ +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. */ -void gpiod_line_config_set_bias_subset (struct gpiod_line_config *config, -int bias,unsigned int num_offsets, -const unsigned int *offsets); +int gpiod_line_config_get_bias_default (struct gpiod_line_config *config);/** -* @brief Get the bias setting for a given line. +* @brief Get the bias setting for a 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. */ -int gpiod_line_config_get_bias (struct gpiod_line_config *config, +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 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. */ -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 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. */ -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. +* 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. */ -void gpiod_line_config_set_event_clock_subset (struct gpiod_line_config *config, -int clock, -unsigned int num_offsets, -const unsigned int *offsets); +bool +gpiod_line_config_event_clock_is_overridden (struct gpiod_line_config *config, +unsigned int offset);/** -* @brief Get the event clock setting for a given line. +* @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. +*/ +int gpiod_line_config_get_event_clock_default (struct gpiod_line_config *config); + +/** +* @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. */ -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 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 Set the output value for a single offset. +* @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. */ @@ -811,14 +931,65 @@ 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 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 Get the number of offsets for which this config object contains +* at least one setting that is different from the defaults. +* @param config Line config object. +* @return Number of offsets with at least one overridden setting. +*/ +unsigned int +gpiod_line_config_get_num_overridden_offsets (struct gpiod_line_config *config); + +/** +* @brief Get the offsets for which this config object contains at least one +* setting that is different from the defaults. +* @param config Line config object. +* @param offsets Array to store the offsets. Must hold at least the number +* of offsets returned by +* ::gpiod_line_config_get_num_overridden_offsets. +*/ +void gpiod_line_config_get_overridden_offsets (struct gpiod_line_config *config, +unsigned int *offsets);/** * @} lib/line-config.c +470
-282@@ -18,6 +18,7 @@ struct base_config { bool active_low : 1; unsigned int clock : 2;unsigned long debounce_period_us; + unsigned int value : 1; }GPIOD_PACKED ;#define OVERRIDE_FLAG_DIRECTION GPIOD_BIT(0)@@ -27,6 +28,7 @@ struct base_config {#define OVERRIDE_FLAG_ACTIVE_LOW GPIOD_BIT(4)#define OVERRIDE_FLAG_CLOCK GPIOD_BIT(5)#define OVERRIDE_FLAG_DEBOUNCE_PERIOD GPIOD_BIT(6) +#define OVERRIDE_FLAG_OUTPUT_VALUE GPIOD_BIT(7)/* * Config overriding the defaults for a single line offset. Only flagged @@ -35,16 +37,13 @@ struct base_config {struct override_config {struct base_config base;unsigned int offset; -bool value_set :1 ; -unsigned int value :1 ; -unsigned int override_flags :7 ; +unsigned int override_flags :8 ; }GPIOD_PACKED ;struct gpiod_line_config {bool too_complex;struct base_config defaults;struct override_config overrides[GPIO_V2_LINES_MAX ]; -unsigned int num_overrides; };static void init_base_config (struct base_config *config)@@ -58,6 +57,17 @@ static void init_base_config(struct base_config *config) config->debounce_period_us =0 ; } +static void init_override_config (struct override_config *override) +{ + override->override_flags =0 ; +init_base_config (&override->base); +} + +static bool override_used (struct override_config *override) +{ +return !!override->override_flags; +} + GPIOD_API struct gpiod_line_config *gpiod_line_config_new(void) {struct gpiod_line_config *config;@@ -82,8 +92,8 @@ GPIOD_API void gpiod_line_config_reset(struct gpiod_line_config *config)memset (config,0 ,sizeof (*config));init_base_config (&config->defaults); -for (i =0 ; i <GPIO_V2_LINE_NUM_ATTRS_MAX ; i++) -init_base_config (&config->overrides[i].base); +for (i =0 ; i <GPIO_V2_LINES_MAX ; i++) +init_override_config (&config->overrides[i]); } static struct override_config *@@ -92,7 +102,7 @@ get_override_by_offset(struct gpiod_line_config *config, unsigned int offset)struct override_config *override;unsigned int i; -for (i =0 ; i < config->num_overrides; i++) { +for (i =0 ; i <GPIO_V2_LINES_MAX ; i++) { override = &config->overrides[i];if (override->offset == offset)@@ -103,19 +113,24 @@ get_override_by_offset(struct gpiod_line_config *config, unsigned int offset) }static struct override_config * -get_new_override (struct gpiod_line_config *config,unsigned int offset) +get_free_override (struct gpiod_line_config *config,unsigned int offset) {struct override_config *override; +unsigned int i; -if (config->num_overrides ==GPIO_V2_LINES_MAX ) { - config->too_complex = true; -return NULL ; - } +for (i =0 ; i <GPIO_V2_LINES_MAX ; i++) { + override = &config->overrides[i]; - override = &config->overrides[config->num_overrides++]; - override->offset = offset; +if (override->override_flags) +continue ; -return override; + override->offset = offset; +return override; + } + +/* No more free overrides. */ + config->too_complex = true; +return NULL ; } static struct override_config *@@ -129,7 +144,7 @@ get_override_config_for_writing(struct gpiod_line_config *config, override =get_override_by_offset (config, offset);if (!override) { - override =get_new_override (config, offset); + override =get_free_override (config, offset);if (!override)return NULL ; }@@ -139,7 +154,7 @@ get_override_config_for_writing(struct gpiod_line_config *config,static struct base_config *get_base_config_for_reading (struct gpiod_line_config *config, -unsigned int offset,unsigned int flag) +unsigned int offset,unsigned int flag) {struct override_config *override;@@ -150,6 +165,35 @@ get_base_config_for_reading(struct gpiod_line_config *config,return &config->defaults; } +static void clear_override (struct gpiod_line_config *config, +unsigned int offset,int flag) +{ +struct override_config *override; + + override =get_override_config_for_writing (config, offset); +if (!override) +return ; + +if (override->override_flags & flag) { + override->override_flags &= ~flag; + +if (!override->override_flags) +init_override_config (override); + } +} + +static bool check_override (struct gpiod_line_config *config, +unsigned int offset,int flag) +{ +struct override_config *override; + + override =get_override_config_for_writing (config, offset); +if (!override) +return false; + +return override->override_flags & flag; +} +static void set_direction (struct base_config *config,int direction) {switch (direction) {@@ -165,38 +209,49 @@ static void set_direction(struct base_config *config, int direction) } GPIOD_API void -gpiod_line_config_set_direction (struct gpiod_line_config *config,int direction) +gpiod_line_config_set_direction_default (struct gpiod_line_config *config, +int direction) {set_direction (&config->defaults, direction); } GPIOD_API void -gpiod_line_config_set_direction_offset (struct gpiod_line_config *config, +gpiod_line_config_set_direction_override (struct gpiod_line_config *config,int direction,unsigned int offset) { -gpiod_line_config_set_direction_subset (config, direction,1 , &offset); +struct override_config *override; + + override =get_override_config_for_writing (config, offset); +if (!override) +return ; + +set_direction (&override->base, direction); + override->override_flags |=OVERRIDE_FLAG_DIRECTION ; } GPIOD_API void -gpiod_line_config_set_direction_subset (struct gpiod_line_config *config, -int direction,unsigned int num_offsets, -const unsigned int *offsets) +gpiod_line_config_clear_direction_override (struct gpiod_line_config *config, +unsigned int offset) { -struct override_config *override; -unsigned int i; +clear_override (config, offset,OVERRIDE_FLAG_DIRECTION ); +} -for (i =0 ; i < num_offsets; i++) { - override =get_override_config_for_writing (config, offsets[i]); -if (!override) -return ; +GPIOD_API bool +gpiod_line_config_direction_is_overridden (struct gpiod_line_config *config, +unsigned int offset) +{ +return check_override (config, offset,OVERRIDE_FLAG_DIRECTION ); +} -set_direction (&override->base, direction); - override->override_flags |=OVERRIDE_FLAG_DIRECTION ; - } +GPIOD_API int +gpiod_line_config_get_direction_default (struct gpiod_line_config *config) +{ +return config->defaults.direction; } -GPIOD_API int gpiod_line_config_get_direction(struct gpiod_line_config *config, -unsigned int offset) +GPIOD_API int +gpiod_line_config_get_direction_offset (struct gpiod_line_config *config, +unsigned int offset) {struct base_config *base;@@ -222,39 +277,49 @@ static void set_edge_detection(struct base_config *config, int edge) } GPIOD_API void -gpiod_line_config_set_edge_detection (struct gpiod_line_config *config,int edge) +gpiod_line_config_set_edge_detection_default (struct gpiod_line_config *config, +int edge) {set_edge_detection (&config->defaults, edge); } GPIOD_API void -gpiod_line_config_set_edge_detection_offset (struct gpiod_line_config *config, -int edge,unsigned int offset) +gpiod_line_config_set_edge_detection_override (struct gpiod_line_config *config, +int edge,unsigned int offset) { -gpiod_line_config_set_edge_detection_subset (config, edge,1 , &offset); +struct override_config *override; + + override =get_override_config_for_writing (config, offset); +if (!override) +return ; + +set_edge_detection (&override->base, edge); + override->override_flags |=OVERRIDE_FLAG_EDGE ; } GPIOD_API 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) { -struct override_config *override; -unsigned int i; +clear_override (config, offset,OVERRIDE_FLAG_EDGE ); +} -for (i =0 ; i < num_offsets; i++) { - override =get_override_config_for_writing (config, offsets[i]); -if (!override) -return ; +GPIOD_API bool +gpiod_line_config_edge_detection_is_overridden (struct gpiod_line_config *config, +unsigned int offset) +{ +return check_override (config, offset,OVERRIDE_FLAG_EDGE ); +} -set_edge_detection (&override->base, edge); - override->override_flags |=OVERRIDE_FLAG_EDGE ; - } +GPIOD_API int +gpiod_line_config_get_edge_detection_default (struct gpiod_line_config *config) +{ +return config->defaults.edge; } GPIOD_API int -gpiod_line_config_get_edge_detection (struct gpiod_line_config *config, -unsigned int offset) +gpiod_line_config_get_edge_detection_offset (struct gpiod_line_config *config, +unsigned int offset) {struct base_config *base;@@ -279,38 +344,48 @@ static void set_bias(struct base_config *config, int bias) } GPIOD_API void -gpiod_line_config_set_bias (struct gpiod_line_config *config,int bias) +gpiod_line_config_set_bias_default (struct gpiod_line_config *config,int bias) {set_bias (&config->defaults, bias); } GPIOD_API void -gpiod_line_config_set_bias_offset (struct gpiod_line_config *config, +gpiod_line_config_set_bias_override (struct gpiod_line_config *config,int bias,unsigned int offset) { -gpiod_line_config_set_bias_subset (config, bias,1 , &offset); +struct override_config *override; + + override =get_override_config_for_writing (config, offset); +if (!override) +return ; + +set_bias (&override->base, bias); + override->override_flags |=OVERRIDE_FLAG_BIAS ; } GPIOD_API void -gpiod_line_config_set_bias_subset (struct gpiod_line_config *config, -int bias,unsigned int num_offsets, -const unsigned int *offsets) +gpiod_line_config_clear_bias_override (struct gpiod_line_config *config, +unsigned int offset) { -struct override_config *override; -unsigned int i; +clear_override (config, offset,OVERRIDE_FLAG_BIAS ); +} -for (i =0 ; i < num_offsets; i++) { - override =get_override_config_for_writing (config, offsets[i]); -if (!override) -return ; +GPIOD_API bool +gpiod_line_config_bias_is_overridden (struct gpiod_line_config *config, +unsigned int offset) +{ +return check_override (config, offset,OVERRIDE_FLAG_BIAS ); +} -set_bias (&override->base, bias); - override->override_flags |=OVERRIDE_FLAG_BIAS ; - } +GPIOD_API int +gpiod_line_config_get_bias_default (struct gpiod_line_config *config) +{ +return config->defaults.bias; } -GPIOD_API int gpiod_line_config_get_bias(struct gpiod_line_config *config, -unsigned int offset) +GPIOD_API int +gpiod_line_config_get_bias_offset (struct gpiod_line_config *config, +unsigned int offset) {struct base_config *base;@@ -334,38 +409,48 @@ static void set_drive(struct base_config *config, int drive) } GPIOD_API void -gpiod_line_config_set_drive (struct gpiod_line_config *config,int drive) +gpiod_line_config_set_drive_default (struct gpiod_line_config *config,int drive) {set_drive (&config->defaults, drive); } GPIOD_API void -gpiod_line_config_set_drive_offset (struct gpiod_line_config *config, -int drive,unsigned int offset) +gpiod_line_config_set_drive_override (struct gpiod_line_config *config, +int drive,unsigned int offset) { -gpiod_line_config_set_drive_subset (config, drive,1 , &offset); +struct override_config *override; + + override =get_override_config_for_writing (config, offset); +if (!override) +return ; + +set_drive (&override->base, drive); + override->override_flags |=OVERRIDE_FLAG_DRIVE ; } GPIOD_API void -gpiod_line_config_set_drive_subset (struct gpiod_line_config *config, -int drive,unsigned int num_offsets, -const unsigned int *offsets) +gpiod_line_config_clear_drive_override (struct gpiod_line_config *config, +unsigned int offset) { -struct override_config *override; -unsigned int i; +clear_override (config, offset,OVERRIDE_FLAG_DRIVE ); +} -for (i =0 ; i < num_offsets; i++) { - override =get_override_config_for_writing (config, offsets[i]); -if (!override) -return ; +GPIOD_API bool +gpiod_line_config_drive_is_overridden (struct gpiod_line_config *config, +unsigned int offset) +{ +return check_override (config, offset,OVERRIDE_FLAG_DRIVE ); +} -set_drive (&override->base, drive); - override->override_flags |=OVERRIDE_FLAG_DRIVE ; - } +GPIOD_API int +gpiod_line_config_get_drive_default (struct gpiod_line_config *config) +{ +return config->defaults.drive; } -GPIOD_API int gpiod_line_config_get_drive(struct gpiod_line_config *config, -unsigned int offset) +GPIOD_API int +gpiod_line_config_get_drive_offset (struct gpiod_line_config *config, +unsigned int offset) {struct base_config *base;@@ -375,49 +460,50 @@ GPIOD_API int gpiod_line_config_get_drive(struct gpiod_line_config *config, } GPIOD_API void -gpiod_line_config_set_active_low (struct gpiod_line_config *config) +gpiod_line_config_set_active_low_default (struct gpiod_line_config *config, +bool active_low) { - config->defaults.active_low = true; + config->defaults.active_low = active_low; } GPIOD_API void -gpiod_line_config_set_active_low_offset (struct gpiod_line_config *config, -unsigned int offset) -{ -gpiod_line_config_set_active_low_subset (config,1 , &offset); -} - -static void -gpiod_line_config_set_active_setting (struct gpiod_line_config *config, -unsigned int num_offsets, -const unsigned int *offsets, -bool active_low) +gpiod_line_config_set_active_low_override (struct gpiod_line_config *config, +bool active_low, +unsigned int offset) {struct override_config *override; -unsigned int i; -for (i =0 ; i < num_offsets; i++) { - override =get_override_config_for_writing (config, offsets[i]); -if (!override) -return ; + override =get_override_config_for_writing (config, offset); +if (!override) +return ; - override->base.active_low = active_low; - override->override_flags |=OVERRIDE_FLAG_ACTIVE_LOW ; - } + override->base.active_low = active_low; + override->override_flags |=OVERRIDE_FLAG_ACTIVE_LOW ; } GPIOD_API void -gpiod_line_config_set_active_low_subset (struct gpiod_line_config *config, -unsigned int num_offsets, -const unsigned int *offsets) +gpiod_line_config_clear_active_low_override (struct gpiod_line_config *config, +unsigned int offset) { -gpiod_line_config_set_active_setting (config, - num_offsets, offsets, true); +clear_override (config, offset,OVERRIDE_FLAG_ACTIVE_LOW ); } GPIOD_API bool -gpiod_line_config_get_active_low (struct gpiod_line_config *config, -unsigned int offset) +gpiod_line_config_active_low_is_overridden (struct gpiod_line_config *config, +unsigned int offset) +{ +return check_override (config, offset,OVERRIDE_FLAG_ACTIVE_LOW ); +} + +GPIOD_API bool +gpiod_line_config_get_active_low_default (struct gpiod_line_config *config) +{ +return config->defaults.active_low; +} + +GPIOD_API bool +gpiod_line_config_get_active_low_offset (struct gpiod_line_config *config, +unsigned int offset) {struct base_config *base;@@ -428,69 +514,52 @@ gpiod_line_config_get_active_low(struct gpiod_line_config *config, } GPIOD_API void -gpiod_line_config_set_active_high (struct gpiod_line_config *config) +gpiod_line_config_set_debounce_period_us_default ( +struct gpiod_line_config *config,unsigned long period) { - config->defaults.active_low = false; + config->defaults.debounce_period_us = period; } GPIOD_API void -gpiod_line_config_set_active_high_offset (struct gpiod_line_config *config, -unsigned int offset) +gpiod_line_config_set_debounce_period_us_override ( +struct gpiod_line_config *config, +unsigned long period, +unsigned int offset) { -gpiod_line_config_set_active_high_subset (config,1 , &offset); -} +struct override_config *override; -GPIOD_API void -gpiod_line_config_set_active_high_subset (struct gpiod_line_config *config, -unsigned int num_offsets, -const unsigned int *offsets) -{ -gpiod_line_config_set_active_setting (config, - num_offsets, offsets, false); -} + override =get_override_config_for_writing (config, offset); +if (!override) +return ; -GPIOD_API void -gpiod_line_config_set_debounce_period_us (struct gpiod_line_config *config, -unsigned long period) -{ - config->defaults.debounce_period_us = period; + override->base.debounce_period_us = period; + override->override_flags |=OVERRIDE_FLAG_DEBOUNCE_PERIOD ; } -GPIOD_API void -gpiod_line_config_set_debounce_period_us_offset ( +GPIOD_API void gpiod_line_config_clear_debounce_period_us_override(struct gpiod_line_config *config, -unsigned long period,unsigned int offset) { -gpiod_line_config_set_debounce_period_us_subset (config, period, -1 , &offset); +clear_override (config, offset,OVERRIDE_FLAG_DEBOUNCE_PERIOD ); } -GPIOD_API void -gpiod_line_config_set_debounce_period_us_subset ( +GPIOD_API bool gpiod_line_config_debounce_period_us_is_overridden(struct gpiod_line_config *config, -unsigned long period, -unsigned int num_offsets, -const unsigned int *offsets) +unsigned int offset) { -struct override_config *override; -unsigned int i, offset; - -for (i =0 ; i < num_offsets; i++) { - offset = offsets[i]; - - override =get_override_config_for_writing (config, offset); -if (!override) -return ; +return check_override (config, offset,OVERRIDE_FLAG_DEBOUNCE_PERIOD ); +} - override->base.debounce_period_us = period; - override->override_flags |=OVERRIDE_FLAG_DEBOUNCE_PERIOD ; - } +GPIOD_API unsigned long +gpiod_line_config_get_debounce_period_us_default ( +struct gpiod_line_config *config) +{ +return config->defaults.debounce_period_us; }GPIOD_API 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) {struct base_config *base;@@ -514,38 +583,49 @@ static void set_event_clock(struct base_config *config, int clock) } GPIOD_API void -gpiod_line_config_set_event_clock (struct gpiod_line_config *config,int clock) +gpiod_line_config_set_event_clock_default (struct gpiod_line_config *config, +int clock) {set_event_clock (&config->defaults, clock); } GPIOD_API void -gpiod_line_config_set_event_clock_offset (struct gpiod_line_config *config, -int clock,unsigned int offset) +gpiod_line_config_set_event_clock_override (struct gpiod_line_config *config, +int clock,unsigned int offset) { -gpiod_line_config_set_event_clock_subset (config, clock,1 , &offset); +struct override_config *override; + + override =get_override_config_for_writing (config, offset); +if (!override) +return ; + +set_event_clock (&override->base, clock); + override->override_flags |=OVERRIDE_FLAG_CLOCK ; } GPIOD_API void -gpiod_line_config_set_event_clock_subset (struct gpiod_line_config *config, -int clock,unsigned int num_offsets, -const unsigned int *offsets) +gpiod_line_config_clear_event_clock_override (struct gpiod_line_config *config, +unsigned int offset) { -struct override_config *override; -unsigned int i; +clear_override (config, offset,OVERRIDE_FLAG_CLOCK ); +} -for (i =0 ; i < num_offsets; i++) { - override =get_override_config_for_writing (config, offsets[i]); -if (!override) -return ; +GPIOD_API bool +gpiod_line_config_event_clock_is_overridden (struct gpiod_line_config *config, +unsigned int offset) +{ +return check_override (config, offset,OVERRIDE_FLAG_CLOCK ); +} -set_event_clock (&override->base, clock); - } +GPIOD_API int +gpiod_line_config_get_event_clock_default (struct gpiod_line_config *config) +{ +return config->defaults.clock; } GPIOD_API int -gpiod_line_config_get_event_clock (struct gpiod_line_config *config, -unsigned int offset) +gpiod_line_config_get_event_clock_offset (struct gpiod_line_config *config, +unsigned int offset) {struct base_config *base;@@ -554,17 +634,25 @@ gpiod_line_config_get_event_clock(struct gpiod_line_config *config,return base->clock; } -static void set_output_value (struct override_config *override,int value) +GPIOD_API void +gpiod_line_config_set_output_value_default (struct gpiod_line_config *config, +int value) { - override->value = !!value; - override->value_set = true; + config->defaults.value = value; } GPIOD_API void -gpiod_line_config_set_output_value (struct gpiod_line_config *config, -unsigned int offset,int value) +gpiod_line_config_set_output_value_override (struct gpiod_line_config *config, +unsigned int offset,int value) { -gpiod_line_config_set_output_values (config,1 , &offset, &value); +struct override_config *override; + + override =get_override_config_for_writing (config, offset); +if (!override) +return ; + + override->base.value = !!value; + override->override_flags |=OVERRIDE_FLAG_OUTPUT_VALUE ; }GPIOD_API void@@ -573,36 +661,103 @@ gpiod_line_config_set_output_values(struct gpiod_line_config *config,const unsigned int *offsets, const int *values) { - struct override_config *override; -unsigned int i, offset, val; + unsigned int i; -for (i =0 ; i < num_values; i++) { - offset = offsets[i]; - val = values[i]; +for (i =0 ; i < num_values; i++) +gpiod_line_config_set_output_value_override (config, + offsets[i], + values[i]); +} - override =get_override_by_offset (config, offset); -if (!override) { - override =get_new_override (config, offset); -if (!override) -return ; - } +GPIOD_API void +gpiod_line_config_clear_output_value_override (struct gpiod_line_config *config, +unsigned int offset) +{ +clear_override (config, offset,OVERRIDE_FLAG_OUTPUT_VALUE ); +} -set_output_value (override, val); - } +GPIOD_API bool +gpiod_line_config_output_value_is_overridden (struct gpiod_line_config *config, +unsigned int offset) +{ +return check_override (config, offset,OVERRIDE_FLAG_OUTPUT_VALUE ); } GPIOD_API int -gpiod_line_config_get_output_value (struct gpiod_line_config *config, -unsigned int offset) +gpiod_line_config_get_output_value_default (struct gpiod_line_config *config) +{ +return config->defaults.value; +} + +GPIOD_API int +gpiod_line_config_get_output_value_offset (struct gpiod_line_config *config, +unsigned int offset) {struct override_config *override; override =get_override_by_offset (config, offset); -if (override && override->value_set) -return override->value; +if (override && (override->override_flags &OVERRIDE_FLAG_OUTPUT_VALUE )) +return override->base.value; - errno =ENXIO ; -return -1 ; +return config->defaults.value; +} + +static bool base_config_flags_are_equal (struct base_config *base, +struct override_config *override) +{ +if (((override->override_flags &OVERRIDE_FLAG_DIRECTION ) && + base->direction != override->base.direction) || + ((override->override_flags &OVERRIDE_FLAG_EDGE ) && + base->edge != override->base.edge) || + ((override->override_flags &OVERRIDE_FLAG_DRIVE ) && + base->drive != override->base.drive) || + ((override->override_flags &OVERRIDE_FLAG_BIAS ) && + base->bias != override->base.bias) || + ((override->override_flags &OVERRIDE_FLAG_ACTIVE_LOW ) && + base->active_low != override->base.active_low) || + ((override->override_flags &OVERRIDE_FLAG_CLOCK ) && + base->clock != override->base.clock)) +return false; + +return true; +} + +static bool base_debounce_period_is_equal (struct base_config *base, +struct override_config *override) +{ +if ((override->override_flags &OVERRIDE_FLAG_DEBOUNCE_PERIOD ) && + base->debounce_period_us != override->base.debounce_period_us) +return false; + +return true; +} + +GPIOD_API unsigned int +gpiod_line_config_get_num_overridden_offsets (struct gpiod_line_config *config) +{ +unsigned int i, ret =0 ; + +for (i =0 ; i <GPIO_V2_LINES_MAX ; i++) { +if (override_used (&config->overrides[i])) + ret++; + } + +return ret; +} + +GPIOD_API void +gpiod_line_config_get_overridden_offsets (struct gpiod_line_config *config, +unsigned int *offsets) +{ +struct override_config *override; +unsigned int i, j; + +for (i =0 , j =0 ; i <GPIO_V2_LINES_MAX ; i++) { + override = &config->overrides[i]; + +if (override_used (override)) + offsets[j++] = override->offset; + } }static uint64_t make_kernel_flags (const struct base_config *config)@@ -683,10 +838,10 @@ static int find_bitmap_index(unsigned int needle, unsigned int num_lines,return -1 ; } -static int set_kernel_output_values (uint64_t *mask,uint64_t *vals, -struct gpiod_line_config *config, -unsigned int num_lines, -const unsigned int *offsets) +static void set_kernel_output_values (uint64_t *mask,uint64_t *vals, +struct gpiod_line_config *config, +unsigned int num_lines, +const unsigned int *offsets) {struct override_config *override;unsigned int i;@@ -695,44 +850,69 @@ static int set_kernel_output_values(uint64_t *mask, uint64_t *vals,gpiod_line_mask_zero (mask);gpiod_line_mask_zero (vals); -for (i =0 ; i < config->num_overrides; i++) { - override = &config->overrides[i]; +if (config->defaults.direction ==GPIOD_LINE_DIRECTION_OUTPUT ) { +/* +* Default direction is output - assign the default output +* value to all lines. Overrides that may set some lines to +* input will be handled later and may re-assign the output +* values. +*/ +for (i =0 ; i < num_lines; i++) { +gpiod_line_mask_set_bit (mask, i); +gpiod_line_mask_assign_bit (vals, i, + config->defaults.value); + } + }else { +/* +* Default output value is not output. Iterate over overrides +* and set the default output value for those that override the +* direction to output. Don't touch the ones which override +* the output value. +*/ +for (i =0 ; i <GPIO_V2_LINES_MAX ; i++) { + override = &config->overrides[i]; + +if (override->base.direction != +GPIOD_LINE_DIRECTION_OUTPUT || + !(override->override_flags & +OVERRIDE_FLAG_DIRECTION ) || + (override->override_flags & +OVERRIDE_FLAG_OUTPUT_VALUE )) +continue ; -if (override->value_set) { idx =find_bitmap_index (override->offset, num_lines, offsets); -if (idx <0 ) { - errno =EINVAL ; -return -1 ; - } +if (idx <0 ) +continue ;gpiod_line_mask_set_bit (mask, idx);gpiod_line_mask_assign_bit (vals, idx, - !!override->value); + !!config->defaults.value); } } -return 0 ; -} +/* +* Finally iterate over the overrides again and set the overridden +* output values. +*/ +for (i =0 ; i <GPIO_V2_LINES_MAX ; i++) { + override = &config->overrides[i]; -static bool base_config_flags_are_equal (struct base_config *base, -struct override_config *override) -{ -if (((override->override_flags &OVERRIDE_FLAG_DIRECTION ) && - base->direction != override->base.direction) || - ((override->override_flags &OVERRIDE_FLAG_EDGE ) && - base->edge != override->base.edge) || - ((override->override_flags &OVERRIDE_FLAG_DRIVE ) && - base->drive != override->base.drive) || - ((override->override_flags &OVERRIDE_FLAG_BIAS ) && - base->bias != override->base.bias) || - ((override->override_flags &OVERRIDE_FLAG_ACTIVE_LOW ) && - base->active_low != override->base.active_low) || - ((override->override_flags &OVERRIDE_FLAG_CLOCK ) && - base->clock != override->base.clock)) -return false; +if (!(override->override_flags &OVERRIDE_FLAG_OUTPUT_VALUE )) +continue ; -return true; +if (config->defaults.direction !=GPIOD_LINE_DIRECTION_OUTPUT && + (!(override->override_flags &OVERRIDE_FLAG_DIRECTION ) || + override->base.direction !=GPIOD_LINE_DIRECTION_OUTPUT )) +continue ; + + idx =find_bitmap_index (override->offset, num_lines, offsets); +if (idx <0 ) +continue ; + +gpiod_line_mask_set_bit (mask, idx); +gpiod_line_mask_assign_bit (vals, idx, !!override->base.value); + } } static bool override_config_flags_are_equal(struct override_config *a,@@ -771,16 +951,6 @@ static void set_base_config_flags(struct gpio_v2_line_attribute *attr, attr->flags =make_kernel_flags (&base); } -static bool base_debounce_period_is_equal (struct base_config *base, -struct override_config *override) -{ -if ((override->override_flags &OVERRIDE_FLAG_DEBOUNCE_PERIOD ) && - base->debounce_period_us != override->base.debounce_period_us) -return false; - -return true; -} -static bool override_config_debounce_period_is_equal (struct override_config *a,struct override_config *b) {@@ -801,36 +971,40 @@ set_base_config_debounce_period(struct gpio_v2_line_attribute *attr, attr->debounce_period_us = override->base.debounce_period_us; } -static int set_kernel_attr_mask (uint64_t *out,const uint64_t *in, -unsigned int num_lines, -const unsigned int *offsets, -const struct gpiod_line_config *config) +static void set_kernel_attr_mask (uint64_t *out,const uint64_t *in, +unsigned int num_lines, +const unsigned int *offsets, +struct gpiod_line_config *config) { +struct override_config *override;unsigned int i, j;int off;gpiod_line_mask_zero (out); -for (i =0 ; i < config->num_overrides; i++) { -if (!gpiod_line_mask_test_bit (in, i)) +for (i =0 ; i <GPIO_V2_LINES_MAX ; i++) { + override = &config->overrides[i]; + +if (!override_used (override) || + !gpiod_line_mask_test_bit (in, i))continue ;for (j =0 , off =-1 ; j < num_lines; j++) { -if (offsets[j] == config->overrides[i].offset) { +if (offsets[j] == override->offset) { off = j;break ; } } -if (off <0 ) { - errno =EINVAL ; -return -1 ; - } +/* +* Overridden offsets that are not in the list of offsets to +* request (or already requested) are silently ignored. +*/ +if (off <0 ) +continue ;gpiod_line_mask_set_bit (out, off); } - -return 0 ; } static int process_overrides(struct gpiod_line_config *config,@@ -852,11 +1026,18 @@ static int process_overrides(struct gpiod_line_config *config,struct override_config *current, *next;unsigned int i, j; -for (i =0 ; i < config->num_overrides; i++) { -if (gpiod_line_mask_test_bit (&processed, i)) +for (i =0 ; i <GPIO_V2_LINES_MAX ; i++) { + current = &config->overrides[i]; + +if (!override_used (current) || +gpiod_line_mask_test_bit (&processed, i))continue ; - current = &config->overrides[i]; +if (*attr_idx ==GPIO_V2_LINE_NUM_ATTRS_MAX ) { + errno =E2BIG ; +return -1 ; + } +gpiod_line_mask_set_bit (&processed, i);if (defaults_equal_func (&config->defaults, current))@@ -865,12 +1046,13 @@ static int process_overrides(struct gpiod_line_config *config, marked =0 ;gpiod_line_mask_set_bit (&marked, i); -for (j = i +1 ; j < config->num_overrides; j++) { -if (gpiod_line_mask_test_bit (&processed, j)) -continue ; - +for (j = i +1 ; j <GPIO_V2_LINES_MAX ; j++) { next = &config->overrides[j]; +if (!override_used (next) || +gpiod_line_mask_test_bit (&processed, j)) +continue ; +if (override_equal_func (current, next)) {gpiod_line_mask_set_bit (&marked, j);gpiod_line_mask_set_bit (&processed, j);@@ -878,10 +1060,6 @@ static int process_overrides(struct gpiod_line_config *config, } attr = &cfgbuf->attrs[(*attr_idx)++]; -if (*attr_idx ==GPIO_V2_LINE_NUM_ATTRS_MAX ) { - errno =E2BIG ; -return -1 ; - }set_kernel_attr_mask (&mask, &marked, num_lines, offsets, config);@@ -892,14 +1070,31 @@ static int process_overrides(struct gpiod_line_config *config,return 0 ; } +static bool has_at_least_one_output_direction (struct gpiod_line_config *config) +{ +struct override_config *override; +unsigned int i; + +if (config->defaults.direction ==GPIOD_LINE_DIRECTION_OUTPUT ) +return true; + +for (i =0 ; i <GPIO_V2_LINES_MAX ; i++) { + override = &config->overrides[i]; + +if (override->base.direction ==GPIOD_LINE_DIRECTION_OUTPUT ) +return true; + } + +return false; +} +int gpiod_line_config_to_kernel (struct gpiod_line_config *config,struct gpio_v2_line_config *cfgbuf,unsigned int num_lines,const unsigned int *offsets) {struct gpio_v2_line_config_attribute *attr; -struct override_config *override; -unsigned int attr_idx =0 , i; +unsigned int attr_idx =0 ;uint64_t mask, values;int ret;@@ -907,26 +1102,19 @@ int gpiod_line_config_to_kernel(struct gpiod_line_config *config, goto err_2big;/* -* First check if we have at least one default output value configured. +* First check if we have at least one line configured in output mode. * If so, let's take one attribute for the default values. */ -for (i =0 ; i < config->num_overrides; i++) { - override = &config->overrides[i]; - -if (override->value_set) { - attr = &cfgbuf->attrs[attr_idx++]; - attr->attr.id =GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES ; +if (has_at_least_one_output_direction (config)) { + attr = &cfgbuf->attrs[attr_idx++]; + attr->attr.id =GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES ; - ret =set_kernel_output_values (&mask, &values, config, - num_lines, offsets); -if (ret) -return ret; +set_kernel_output_values (&mask, &values, config, + num_lines, offsets); - attr->attr.values = values; - attr->mask = mask; + attr->attr.values = values; + attr->mask = mask; -break ; - } }/* If we have a default debounce period - use another attribute. */ 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
Re: [libgpiod v2][PATCH v2 1/2] line-config: expose the override logic to users
Quoted 28 lines · Kent Gibson
Quoted 13 lines · Bartosz Golaszewski
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.
Quoted 9 lines
We also complete the API by providing functions that allow to identify offsets for which at least one setting is overriden.
This way the caller can fully inspect the
line_configand high-level language bindings can provide stringification methods.While at it: we fix a couple bugs in the implementation of struct
line_config.TLDR: I've got a lot of nitpicks on the doco, but it might be simpler for you to apply this patch as is and for me to submit a patch with doco tweaks than for you to try to sort out my comments!
Quoted 5 lines
The actual code looks good, so
Reviewed-by Kent Gibson <[email protected]>with or without the suggested doco changes.
Thank you, Kent, for persistent review, I'm sorry, folks, I have no time to look into this, but I believe that you together will give good enough result and we can amend it later.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [libgpiod v2][PATCH v2 1/2] line-config: expose the override logic to users
In reply to Kent GibsonQuoted 82 lines · Kent Gibson
Quoted 13 lines · Bartosz Golaszewski
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.
Quoted 9 lines
We also complete the API by providing functions that allow to identify offsets for which at least one setting is overriden.
This way the caller can fully inspect the
line_configand high-level language bindings can provide stringification methods.While at it: we fix a couple bugs in the implementation of struct
line_config.TLDR: I've got a lot of nitpicks on the doco, but it might be simpler for you to apply this patch as is and for me to submit a patch with doco tweaks than for you to try to sort out my comments!
The actual code looks good, so
Reviewed-by Kent Gibson <[email protected]>with or without the suggested doco changes.
Quoted 47 lines
Signed-off-by Bartosz Golaszewski <[email protected]>--- include/gpiod.h | 509 ++++++++++++++++++++----------- lib/line-config.c | 752 +++++++++++++++++++++++++++++----------------- tools/gpioget.c | 6 +- tools/gpiomon.c | 6 +- tools/gpioset.c | 9 +- 5 files changed, 821 insertions(+), 461 deletions(-)include/gpiod.h +24
-6@@ -450,12 +450,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 given offset), a +* function for testing if a setting is overridden for 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.
Quoted 9 lines
include/gpiod.h gpiod_info_event_get_line_info(struct gpiod_info_event *event); · line 481
Quoted 5 lines
include/gpiod.h +5
-0+* +* 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. +* This should be noted in the function documentation, not just here. If only one place, then there not here.
Done in v3.
Quoted 8 lines
Quoted 8 lines
include/gpiod.h +2
-0+* In cases where all requested lines are using the global defaults, the +* offsets can be entirely ignored when preparing the line configuration. */ /** @@ -480,329 +505,424 @@ void gpiod_line_config_free(struct gpiod_line_config *config);void gpiod_line_config_reset (struct gpiod_line_config *config);
Quoted 7 lines
include/gpiod.h void gpiod_line_config_free(struct gpiod_line_config *config); · line 508
Quoted 3 lines
include/gpiod.h +1
-1/** -* @brief Set the direction of all lines. +* @brief Set the default direction setting. Nitpick: "setting" feels redundant given the "Set". Same thoughout, including the "Get" cases.
What would you prefer it to be? "@brief Direction of all lines"? Doesn't look right to me. It's normal for the doc to use a verb to say what the function does.
Quoted 8 lines
Quoted 8 lines
include/gpiod.h +2
-2* @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);
Quoted 9 lines
include/gpiod.h void gpiod_line_config_free(struct gpiod_line_config *config); · line 516
Quoted 3 lines
include/gpiod.h +1
-1/** -* @brief Set the direction for a single line at given offset. +* @brief Set the direction override at given offset. Nitpick: "at given offset" -> "for the line" throughout. Similarly "at this offset" -> "for this line", and "line at given offset" -> "line",
and "for a given offset" -> "for a line".
I think that speaking of offsets makes it more explicit and switching to your alternatives dilutes the meaning here. This is still the configuration and we don't have any real lines yet. Just offset -> value mappings.
Quoted 39 lines
Quoted 39 lines
include/gpiod.h +19
-11* @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 Check if the direction 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 direction is overridden at this offset, false otherwise. +*/ +bool gpiod_line_config_direction_is_overridden (struct gpiod_line_config *config, +unsigned int offset);
Quoted 15 lines
include/gpiod.h void gpiod_line_config_free(struct gpiod_line_config *config); · line 553
Quoted 11 lines
include/gpiod.h +9
-1/** -* @brief Get the direction setting for a given line. +* @brief Get the default direction setting. +* @param config Line config object. +* @return Direction setting that would have been used for any non-overridden +* offset. +*/ +int gpiod_line_config_get_direction_default (struct gpiod_line_config *config); + +/** +* @brief Get the direction setting for a given offset. Preferred "given line". The direction is an attribute of the line, not the offset. The offset is the line identifier.
Same throughout.
If I were consistent with my own previous docs, it would be "for the line at given offset".
Quoted 5 lines
include/gpiod.h void gpiod_line_config_free(struct gpiod_line_config *config); · line 555
Quoted 3 lines
include/gpiod.h +0
-0* @param config Line config object. * @param offset Line offset for which to read the direction setting. Nitpick: "Line offset" -> "Offset of the line"
Sounds good.
Quoted 43 lines
Quoted 23 lines
include/gpiod.h +6
-7* @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. */ -int gpiod_line_config_get_direction (struct gpiod_line_config *config, -unsigned int offset); +int gpiod_line_config_get_direction_offset (struct gpiod_line_config *config, +unsigned int offset);/** -* @brief Set the edge event detection for all lines. +* @brief Set the default edge event detection. * @param config Line config object. * @param edge Type of edge events to detect. */ -void gpiod_line_config_set_edge_detection (struct gpiod_line_config *config, -int edge); +void +gpiod_line_config_set_edge_detection_default (struct gpiod_line_config *config, +int edge);include/gpiod.h void gpiod_line_config_free(struct gpiod_line_config *config); · line 577
Quoted 10 lines
include/gpiod.h +3
-3/** -* @brief Set the edge event detection for a single line at given offset. +* @brief Set the edge detection override at given offset. * @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. +* @param edge New edge detection setting. +* @param offset Offset of the line for which to set the override. */ void Nitpicking here, but I prefer the
@param edge Type of edge events to detect.
style, that is an actual short description of the edge parameter, as used above over
@param edge New edge detection setting.
Quoted 8 lines
as the latter is very bland boiler plate that doesn't really say anything.
And can the @param edge description be consistent throughout all the edge functions?
Similar elsewhere, on both points.
Will do.
Quoted 3 lines
And again, this is being really nitpicky so feel free to leave it for a subsequent doco tweaking pass.
Since I'm already sending a v3, I'll incorporate some changes and the rest can be done later.
Quoted 1,746 lines
Quoted 1,746 lines
include/gpiod.h +268
-137-gpiod_line_config_set_edge_detection_offset(struct gpiod_line_config *config, - int edge, unsigned int offset); +gpiod_line_config_set_edge_detection_override(struct gpiod_line_config *config, + int edge, unsigned int offset);/** -* @brief Set the edge event detection for a subset of lines. +* @brief Clear 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 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. +* @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 Line offset 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_clear_bias_override (struct gpiod_line_config *config, +unsigned int offset); + +/** +* @brief Check if the bias 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 bias is overridden at this offset, false otherwise. +*/ +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. */ -void gpiod_line_config_set_bias_subset (struct gpiod_line_config *config, -int bias,unsigned int num_offsets, -const unsigned int *offsets); +int gpiod_line_config_get_bias_default (struct gpiod_line_config *config);/** -* @brief Get the bias setting for a given line. +* @brief Get the bias setting for a 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. */ -int gpiod_line_config_get_bias (struct gpiod_line_config *config, +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 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. */ -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 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. */ -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. +* 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. */ -void gpiod_line_config_set_event_clock_subset (struct gpiod_line_config *config, -int clock, -unsigned int num_offsets, -const unsigned int *offsets); +bool +gpiod_line_config_event_clock_is_overridden (struct gpiod_line_config *config, +unsigned int offset);/** -* @brief Get the event clock setting for a given line. +* @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. +*/ +int gpiod_line_config_get_event_clock_default (struct gpiod_line_config *config); + +/** +* @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. */ -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 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 Set the output value for a single offset. +* @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. */ @@ -811,14 +931,65 @@ 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 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 Get the number of offsets for which this config object contains +* at least one setting that is different from the defaults. +* @param config Line config object. +* @return Number of offsets with at least one overridden setting. +*/ +unsigned int +gpiod_line_config_get_num_overridden_offsets (struct gpiod_line_config *config); + +/** +* @brief Get the offsets for which this config object contains at least one +* setting that is different from the defaults. +* @param config Line config object. +* @param offsets Array to store the offsets. Must hold at least the number +* of offsets returned by +* ::gpiod_line_config_get_num_overridden_offsets. +*/ +void gpiod_line_config_get_overridden_offsets (struct gpiod_line_config *config, +unsigned int *offsets);/** * @} lib/line-config.c +470
-282@@ -18,6 +18,7 @@ struct base_config { bool active_low : 1; unsigned int clock : 2;unsigned long debounce_period_us; + unsigned int value : 1; }GPIOD_PACKED ;#define OVERRIDE_FLAG_DIRECTION GPIOD_BIT(0)@@ -27,6 +28,7 @@ struct base_config {#define OVERRIDE_FLAG_ACTIVE_LOW GPIOD_BIT(4)#define OVERRIDE_FLAG_CLOCK GPIOD_BIT(5)#define OVERRIDE_FLAG_DEBOUNCE_PERIOD GPIOD_BIT(6) +#define OVERRIDE_FLAG_OUTPUT_VALUE GPIOD_BIT(7)/* * Config overriding the defaults for a single line offset. Only flagged @@ -35,16 +37,13 @@ struct base_config {struct override_config {struct base_config base;unsigned int offset; -bool value_set :1 ; -unsigned int value :1 ; -unsigned int override_flags :7 ; +unsigned int override_flags :8 ; }GPIOD_PACKED ;struct gpiod_line_config {bool too_complex;struct base_config defaults;struct override_config overrides[GPIO_V2_LINES_MAX ]; -unsigned int num_overrides; };static void init_base_config (struct base_config *config)@@ -58,6 +57,17 @@ static void init_base_config(struct base_config *config) config->debounce_period_us =0 ; } +static void init_override_config (struct override_config *override) +{ + override->override_flags =0 ; +init_base_config (&override->base); +} + +static bool override_used (struct override_config *override) +{ +return !!override->override_flags; +} + GPIOD_API struct gpiod_line_config *gpiod_line_config_new(void) {struct gpiod_line_config *config;@@ -82,8 +92,8 @@ GPIOD_API void gpiod_line_config_reset(struct gpiod_line_config *config)memset (config,0 ,sizeof (*config));init_base_config (&config->defaults); -for (i =0 ; i <GPIO_V2_LINE_NUM_ATTRS_MAX ; i++) -init_base_config (&config->overrides[i].base); +for (i =0 ; i <GPIO_V2_LINES_MAX ; i++) +init_override_config (&config->overrides[i]); } static struct override_config *@@ -92,7 +102,7 @@ get_override_by_offset(struct gpiod_line_config *config, unsigned int offset)struct override_config *override;unsigned int i; -for (i =0 ; i < config->num_overrides; i++) { +for (i =0 ; i <GPIO_V2_LINES_MAX ; i++) { override = &config->overrides[i];if (override->offset == offset)@@ -103,19 +113,24 @@ get_override_by_offset(struct gpiod_line_config *config, unsigned int offset) }static struct override_config * -get_new_override (struct gpiod_line_config *config,unsigned int offset) +get_free_override (struct gpiod_line_config *config,unsigned int offset) {struct override_config *override; +unsigned int i; -if (config->num_overrides ==GPIO_V2_LINES_MAX ) { - config->too_complex = true; -return NULL ; - } +for (i =0 ; i <GPIO_V2_LINES_MAX ; i++) { + override = &config->overrides[i]; - override = &config->overrides[config->num_overrides++]; - override->offset = offset; +if (override->override_flags) +continue ; -return override; + override->offset = offset; +return override; + } + +/* No more free overrides. */ + config->too_complex = true; +return NULL ; } static struct override_config *@@ -129,7 +144,7 @@ get_override_config_for_writing(struct gpiod_line_config *config, override =get_override_by_offset (config, offset);if (!override) { - override =get_new_override (config, offset); + override =get_free_override (config, offset);if (!override)return NULL ; }@@ -139,7 +154,7 @@ get_override_config_for_writing(struct gpiod_line_config *config,static struct base_config *get_base_config_for_reading (struct gpiod_line_config *config, -unsigned int offset,unsigned int flag) +unsigned int offset,unsigned int flag) {struct override_config *override;@@ -150,6 +165,35 @@ get_base_config_for_reading(struct gpiod_line_config *config,return &config->defaults; } +static void clear_override (struct gpiod_line_config *config, +unsigned int offset,int flag) +{ +struct override_config *override; + + override =get_override_config_for_writing (config, offset); +if (!override) +return ; + +if (override->override_flags & flag) { + override->override_flags &= ~flag; + +if (!override->override_flags) +init_override_config (override); + } +} + +static bool check_override (struct gpiod_line_config *config, +unsigned int offset,int flag) +{ +struct override_config *override; + + override =get_override_config_for_writing (config, offset); +if (!override) +return false; + +return override->override_flags & flag; +} +static void set_direction (struct base_config *config,int direction) {switch (direction) {@@ -165,38 +209,49 @@ static void set_direction(struct base_config *config, int direction) } GPIOD_API void -gpiod_line_config_set_direction (struct gpiod_line_config *config,int direction) +gpiod_line_config_set_direction_default (struct gpiod_line_config *config, +int direction) {set_direction (&config->defaults, direction); } GPIOD_API void -gpiod_line_config_set_direction_offset (struct gpiod_line_config *config, +gpiod_line_config_set_direction_override (struct gpiod_line_config *config,int direction,unsigned int offset) { -gpiod_line_config_set_direction_subset (config, direction,1 , &offset); +struct override_config *override; + + override =get_override_config_for_writing (config, offset); +if (!override) +return ; + +set_direction (&override->base, direction); + override->override_flags |=OVERRIDE_FLAG_DIRECTION ; } GPIOD_API void -gpiod_line_config_set_direction_subset (struct gpiod_line_config *config, -int direction,unsigned int num_offsets, -const unsigned int *offsets) +gpiod_line_config_clear_direction_override (struct gpiod_line_config *config, +unsigned int offset) { -struct override_config *override; -unsigned int i; +clear_override (config, offset,OVERRIDE_FLAG_DIRECTION ); +} -for (i =0 ; i < num_offsets; i++) { - override =get_override_config_for_writing (config, offsets[i]); -if (!override) -return ; +GPIOD_API bool +gpiod_line_config_direction_is_overridden (struct gpiod_line_config *config, +unsigned int offset) +{ +return check_override (config, offset,OVERRIDE_FLAG_DIRECTION ); +} -set_direction (&override->base, direction); - override->override_flags |=OVERRIDE_FLAG_DIRECTION ; - } +GPIOD_API int +gpiod_line_config_get_direction_default (struct gpiod_line_config *config) +{ +return config->defaults.direction; } -GPIOD_API int gpiod_line_config_get_direction(struct gpiod_line_config *config, -unsigned int offset) +GPIOD_API int +gpiod_line_config_get_direction_offset (struct gpiod_line_config *config, +unsigned int offset) {struct base_config *base;@@ -222,39 +277,49 @@ static void set_edge_detection(struct base_config *config, int edge) } GPIOD_API void -gpiod_line_config_set_edge_detection (struct gpiod_line_config *config,int edge) +gpiod_line_config_set_edge_detection_default (struct gpiod_line_config *config, +int edge) {set_edge_detection (&config->defaults, edge); } GPIOD_API void -gpiod_line_config_set_edge_detection_offset (struct gpiod_line_config *config, -int edge,unsigned int offset) +gpiod_line_config_set_edge_detection_override (struct gpiod_line_config *config, +int edge,unsigned int offset) { -gpiod_line_config_set_edge_detection_subset (config, edge,1 , &offset); +struct override_config *override; + + override =get_override_config_for_writing (config, offset); +if (!override) +return ; + +set_edge_detection (&override->base, edge); + override->override_flags |=OVERRIDE_FLAG_EDGE ; } GPIOD_API 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) { -struct override_config *override; -unsigned int i; +clear_override (config, offset,OVERRIDE_FLAG_EDGE ); +} -for (i =0 ; i < num_offsets; i++) { - override =get_override_config_for_writing (config, offsets[i]); -if (!override) -return ; +GPIOD_API bool +gpiod_line_config_edge_detection_is_overridden (struct gpiod_line_config *config, +unsigned int offset) +{ +return check_override (config, offset,OVERRIDE_FLAG_EDGE ); +} -set_edge_detection (&override->base, edge); - override->override_flags |=OVERRIDE_FLAG_EDGE ; - } +GPIOD_API int +gpiod_line_config_get_edge_detection_default (struct gpiod_line_config *config) +{ +return config->defaults.edge; } GPIOD_API int -gpiod_line_config_get_edge_detection (struct gpiod_line_config *config, -unsigned int offset) +gpiod_line_config_get_edge_detection_offset (struct gpiod_line_config *config, +unsigned int offset) {struct base_config *base;@@ -279,38 +344,48 @@ static void set_bias(struct base_config *config, int bias) } GPIOD_API void -gpiod_line_config_set_bias (struct gpiod_line_config *config,int bias) +gpiod_line_config_set_bias_default (struct gpiod_line_config *config,int bias) {set_bias (&config->defaults, bias); } GPIOD_API void -gpiod_line_config_set_bias_offset (struct gpiod_line_config *config, +gpiod_line_config_set_bias_override (struct gpiod_line_config *config,int bias,unsigned int offset) { -gpiod_line_config_set_bias_subset (config, bias,1 , &offset); +struct override_config *override; + + override =get_override_config_for_writing (config, offset); +if (!override) +return ; + +set_bias (&override->base, bias); + override->override_flags |=OVERRIDE_FLAG_BIAS ; } GPIOD_API void -gpiod_line_config_set_bias_subset (struct gpiod_line_config *config, -int bias,unsigned int num_offsets, -const unsigned int *offsets) +gpiod_line_config_clear_bias_override (struct gpiod_line_config *config, +unsigned int offset) { -struct override_config *override; -unsigned int i; +clear_override (config, offset,OVERRIDE_FLAG_BIAS ); +} -for (i =0 ; i < num_offsets; i++) { - override =get_override_config_for_writing (config, offsets[i]); -if (!override) -return ; +GPIOD_API bool +gpiod_line_config_bias_is_overridden (struct gpiod_line_config *config, +unsigned int offset) +{ +return check_override (config, offset,OVERRIDE_FLAG_BIAS ); +} -set_bias (&override->base, bias); - override->override_flags |=OVERRIDE_FLAG_BIAS ; - } +GPIOD_API int +gpiod_line_config_get_bias_default (struct gpiod_line_config *config) +{ +return config->defaults.bias; } -GPIOD_API int gpiod_line_config_get_bias(struct gpiod_line_config *config, -unsigned int offset) +GPIOD_API int +gpiod_line_config_get_bias_offset (struct gpiod_line_config *config, +unsigned int offset) {struct base_config *base;@@ -334,38 +409,48 @@ static void set_drive(struct base_config *config, int drive) } GPIOD_API void -gpiod_line_config_set_drive (struct gpiod_line_config *config,int drive) +gpiod_line_config_set_drive_default (struct gpiod_line_config *config,int drive) {set_drive (&config->defaults, drive); } GPIOD_API void -gpiod_line_config_set_drive_offset (struct gpiod_line_config *config, -int drive,unsigned int offset) +gpiod_line_config_set_drive_override (struct gpiod_line_config *config, +int drive,unsigned int offset) { -gpiod_line_config_set_drive_subset (config, drive,1 , &offset); +struct override_config *override; + + override =get_override_config_for_writing (config, offset); +if (!override) +return ; + +set_drive (&override->base, drive); + override->override_flags |=OVERRIDE_FLAG_DRIVE ; } GPIOD_API void -gpiod_line_config_set_drive_subset (struct gpiod_line_config *config, -int drive,unsigned int num_offsets, -const unsigned int *offsets) +gpiod_line_config_clear_drive_override (struct gpiod_line_config *config, +unsigned int offset) { -struct override_config *override; -unsigned int i; +clear_override (config, offset,OVERRIDE_FLAG_DRIVE ); +} -for (i =0 ; i < num_offsets; i++) { - override =get_override_config_for_writing (config, offsets[i]); -if (!override) -return ; +GPIOD_API bool +gpiod_line_config_drive_is_overridden (struct gpiod_line_config *config, +unsigned int offset) +{ +return check_override (config, offset,OVERRIDE_FLAG_DRIVE ); +} -set_drive (&override->base, drive); - override->override_flags |=OVERRIDE_FLAG_DRIVE ; - } +GPIOD_API int +gpiod_line_config_get_drive_default (struct gpiod_line_config *config) +{ +return config->defaults.drive; } -GPIOD_API int gpiod_line_config_get_drive(struct gpiod_line_config *config, -unsigned int offset) +GPIOD_API int +gpiod_line_config_get_drive_offset (struct gpiod_line_config *config, +unsigned int offset) {struct base_config *base;@@ -375,49 +460,50 @@ GPIOD_API int gpiod_line_config_get_drive(struct gpiod_line_config *config, } GPIOD_API void -gpiod_line_config_set_active_low (struct gpiod_line_config *config) +gpiod_line_config_set_active_low_default (struct gpiod_line_config *config, +bool active_low) { - config->defaults.active_low = true; + config->defaults.active_low = active_low; } GPIOD_API void -gpiod_line_config_set_active_low_offset (struct gpiod_line_config *config, -unsigned int offset) -{ -gpiod_line_config_set_active_low_subset (config,1 , &offset); -} - -static void -gpiod_line_config_set_active_setting (struct gpiod_line_config *config, -unsigned int num_offsets, -const unsigned int *offsets, -bool active_low) +gpiod_line_config_set_active_low_override (struct gpiod_line_config *config, +bool active_low, +unsigned int offset) {struct override_config *override; -unsigned int i; -for (i =0 ; i < num_offsets; i++) { - override =get_override_config_for_writing (config, offsets[i]); -if (!override) -return ; + override =get_override_config_for_writing (config, offset); +if (!override) +return ; - override->base.active_low = active_low; - override->override_flags |=OVERRIDE_FLAG_ACTIVE_LOW ; - } + override->base.active_low = active_low; + override->override_flags |=OVERRIDE_FLAG_ACTIVE_LOW ; } GPIOD_API void -gpiod_line_config_set_active_low_subset (struct gpiod_line_config *config, -unsigned int num_offsets, -const unsigned int *offsets) +gpiod_line_config_clear_active_low_override (struct gpiod_line_config *config, +unsigned int offset) { -gpiod_line_config_set_active_setting (config, - num_offsets, offsets, true); +clear_override (config, offset,OVERRIDE_FLAG_ACTIVE_LOW ); } GPIOD_API bool -gpiod_line_config_get_active_low (struct gpiod_line_config *config, -unsigned int offset) +gpiod_line_config_active_low_is_overridden (struct gpiod_line_config *config, +unsigned int offset) +{ +return check_override (config, offset,OVERRIDE_FLAG_ACTIVE_LOW ); +} + +GPIOD_API bool +gpiod_line_config_get_active_low_default (struct gpiod_line_config *config) +{ +return config->defaults.active_low; +} + +GPIOD_API bool +gpiod_line_config_get_active_low_offset (struct gpiod_line_config *config, +unsigned int offset) {struct base_config *base;@@ -428,69 +514,52 @@ gpiod_line_config_get_active_low(struct gpiod_line_config *config, } GPIOD_API void -gpiod_line_config_set_active_high (struct gpiod_line_config *config) +gpiod_line_config_set_debounce_period_us_default ( +struct gpiod_line_config *config,unsigned long period) { - config->defaults.active_low = false; + config->defaults.debounce_period_us = period; } GPIOD_API void -gpiod_line_config_set_active_high_offset (struct gpiod_line_config *config, -unsigned int offset) +gpiod_line_config_set_debounce_period_us_override ( +struct gpiod_line_config *config, +unsigned long period, +unsigned int offset) { -gpiod_line_config_set_active_high_subset (config,1 , &offset); -} +struct override_config *override; -GPIOD_API void -gpiod_line_config_set_active_high_subset (struct gpiod_line_config *config, -unsigned int num_offsets, -const unsigned int *offsets) -{ -gpiod_line_config_set_active_setting (config, - num_offsets, offsets, false); -} + override =get_override_config_for_writing (config, offset); +if (!override) +return ; -GPIOD_API void -gpiod_line_config_set_debounce_period_us (struct gpiod_line_config *config, -unsigned long period) -{ - config->defaults.debounce_period_us = period; + override->base.debounce_period_us = period; + override->override_flags |=OVERRIDE_FLAG_DEBOUNCE_PERIOD ; } -GPIOD_API void -gpiod_line_config_set_debounce_period_us_offset ( +GPIOD_API void gpiod_line_config_clear_debounce_period_us_override(struct gpiod_line_config *config, -unsigned long period,unsigned int offset) { -gpiod_line_config_set_debounce_period_us_subset (config, period, -1 , &offset); +clear_override (config, offset,OVERRIDE_FLAG_DEBOUNCE_PERIOD ); } -GPIOD_API void -gpiod_line_config_set_debounce_period_us_subset ( +GPIOD_API bool gpiod_line_config_debounce_period_us_is_overridden(struct gpiod_line_config *config, -unsigned long period, -unsigned int num_offsets, -const unsigned int *offsets) +unsigned int offset) { -struct override_config *override; -unsigned int i, offset; - -for (i =0 ; i < num_offsets; i++) { - offset = offsets[i]; - - override =get_override_config_for_writing (config, offset); -if (!override) -return ; +return check_override (config, offset,OVERRIDE_FLAG_DEBOUNCE_PERIOD ); +} - override->base.debounce_period_us = period; - override->override_flags |=OVERRIDE_FLAG_DEBOUNCE_PERIOD ; - } +GPIOD_API unsigned long +gpiod_line_config_get_debounce_period_us_default ( +struct gpiod_line_config *config) +{ +return config->defaults.debounce_period_us; }GPIOD_API 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) {struct base_config *base;@@ -514,38 +583,49 @@ static void set_event_clock(struct base_config *config, int clock) } GPIOD_API void -gpiod_line_config_set_event_clock (struct gpiod_line_config *config,int clock) +gpiod_line_config_set_event_clock_default (struct gpiod_line_config *config, +int clock) {set_event_clock (&config->defaults, clock); } GPIOD_API void -gpiod_line_config_set_event_clock_offset (struct gpiod_line_config *config, -int clock,unsigned int offset) +gpiod_line_config_set_event_clock_override (struct gpiod_line_config *config, +int clock,unsigned int offset) { -gpiod_line_config_set_event_clock_subset (config, clock,1 , &offset); +struct override_config *override; + + override =get_override_config_for_writing (config, offset); +if (!override) +return ; + +set_event_clock (&override->base, clock); + override->override_flags |=OVERRIDE_FLAG_CLOCK ; } GPIOD_API void -gpiod_line_config_set_event_clock_subset (struct gpiod_line_config *config, -int clock,unsigned int num_offsets, -const unsigned int *offsets) +gpiod_line_config_clear_event_clock_override (struct gpiod_line_config *config, +unsigned int offset) { -struct override_config *override; -unsigned int i; +clear_override (config, offset,OVERRIDE_FLAG_CLOCK ); +} -for (i =0 ; i < num_offsets; i++) { - override =get_override_config_for_writing (config, offsets[i]); -if (!override) -return ; +GPIOD_API bool +gpiod_line_config_event_clock_is_overridden (struct gpiod_line_config *config, +unsigned int offset) +{ +return check_override (config, offset,OVERRIDE_FLAG_CLOCK ); +} -set_event_clock (&override->base, clock); - } +GPIOD_API int +gpiod_line_config_get_event_clock_default (struct gpiod_line_config *config) +{ +return config->defaults.clock; } GPIOD_API int -gpiod_line_config_get_event_clock (struct gpiod_line_config *config, -unsigned int offset) +gpiod_line_config_get_event_clock_offset (struct gpiod_line_config *config, +unsigned int offset) {struct base_config *base;@@ -554,17 +634,25 @@ gpiod_line_config_get_event_clock(struct gpiod_line_config *config,return base->clock; } -static void set_output_value (struct override_config *override,int value) +GPIOD_API void +gpiod_line_config_set_output_value_default (struct gpiod_line_config *config, +int value) { - override->value = !!value; - override->value_set = true; + config->defaults.value = value; } GPIOD_API void -gpiod_line_config_set_output_value (struct gpiod_line_config *config, -unsigned int offset,int value) +gpiod_line_config_set_output_value_override (struct gpiod_line_config *config, +unsigned int offset,int value) { -gpiod_line_config_set_output_values (config,1 , &offset, &value); +struct override_config *override; + + override =get_override_config_for_writing (config, offset); +if (!override) +return ; + + override->base.value = !!value; + override->override_flags |=OVERRIDE_FLAG_OUTPUT_VALUE ; }GPIOD_API void@@ -573,36 +661,103 @@ gpiod_line_config_set_output_values(struct gpiod_line_config *config,const unsigned int *offsets, const int *values) { - struct override_config *override; -unsigned int i, offset, val; + unsigned int i; -for (i =0 ; i < num_values; i++) { - offset = offsets[i]; - val = values[i]; +for (i =0 ; i < num_values; i++) +gpiod_line_config_set_output_value_override (config, + offsets[i], + values[i]); +} - override =get_override_by_offset (config, offset); -if (!override) { - override =get_new_override (config, offset); -if (!override) -return ; - } +GPIOD_API void +gpiod_line_config_clear_output_value_override (struct gpiod_line_config *config, +unsigned int offset) +{ +clear_override (config, offset,OVERRIDE_FLAG_OUTPUT_VALUE ); +} -set_output_value (override, val); - } +GPIOD_API bool +gpiod_line_config_output_value_is_overridden (struct gpiod_line_config *config, +unsigned int offset) +{ +return check_override (config, offset,OVERRIDE_FLAG_OUTPUT_VALUE ); } GPIOD_API int -gpiod_line_config_get_output_value (struct gpiod_line_config *config, -unsigned int offset) +gpiod_line_config_get_output_value_default (struct gpiod_line_config *config) +{ +return config->defaults.value; +} + +GPIOD_API int +gpiod_line_config_get_output_value_offset (struct gpiod_line_config *config, +unsigned int offset) {struct override_config *override; override =get_override_by_offset (config, offset); -if (override && override->value_set) -return override->value; +if (override && (override->override_flags &OVERRIDE_FLAG_OUTPUT_VALUE )) +return override->base.value; - errno =ENXIO ; -return -1 ; +return config->defaults.value; +} + +static bool base_config_flags_are_equal (struct base_config *base, +struct override_config *override) +{ +if (((override->override_flags &OVERRIDE_FLAG_DIRECTION ) && + base->direction != override->base.direction) || + ((override->override_flags &OVERRIDE_FLAG_EDGE ) && + base->edge != override->base.edge) || + ((override->override_flags &OVERRIDE_FLAG_DRIVE ) && + base->drive != override->base.drive) || + ((override->override_flags &OVERRIDE_FLAG_BIAS ) && + base->bias != override->base.bias) || + ((override->override_flags &OVERRIDE_FLAG_ACTIVE_LOW ) && + base->active_low != override->base.active_low) || + ((override->override_flags &OVERRIDE_FLAG_CLOCK ) && + base->clock != override->base.clock)) +return false; + +return true; +} + +static bool base_debounce_period_is_equal (struct base_config *base, +struct override_config *override) +{ +if ((override->override_flags &OVERRIDE_FLAG_DEBOUNCE_PERIOD ) && + base->debounce_period_us != override->base.debounce_period_us) +return false; + +return true; +} + +GPIOD_API unsigned int +gpiod_line_config_get_num_overridden_offsets (struct gpiod_line_config *config) +{ +unsigned int i, ret =0 ; + +for (i =0 ; i <GPIO_V2_LINES_MAX ; i++) { +if (override_used (&config->overrides[i])) + ret++; + } + +return ret; +} + +GPIOD_API void +gpiod_line_config_get_overridden_offsets (struct gpiod_line_config *config, +unsigned int *offsets) +{ +struct override_config *override; +unsigned int i, j; + +for (i =0 , j =0 ; i <GPIO_V2_LINES_MAX ; i++) { + override = &config->overrides[i]; + +if (override_used (override)) + offsets[j++] = override->offset; + } }static uint64_t make_kernel_flags (const struct base_config *config)@@ -683,10 +838,10 @@ static int find_bitmap_index(unsigned int needle, unsigned int num_lines,return -1 ; } -static int set_kernel_output_values (uint64_t *mask,uint64_t *vals, -struct gpiod_line_config *config, -unsigned int num_lines, -const unsigned int *offsets) +static void set_kernel_output_values (uint64_t *mask,uint64_t *vals, +struct gpiod_line_config *config, +unsigned int num_lines, +const unsigned int *offsets) {struct override_config *override;unsigned int i;@@ -695,44 +850,69 @@ static int set_kernel_output_values(uint64_t *mask, uint64_t *vals,gpiod_line_mask_zero (mask);gpiod_line_mask_zero (vals); -for (i =0 ; i < config->num_overrides; i++) { - override = &config->overrides[i]; +if (config->defaults.direction ==GPIOD_LINE_DIRECTION_OUTPUT ) { +/* +* Default direction is output - assign the default output +* value to all lines. Overrides that may set some lines to +* input will be handled later and may re-assign the output +* values. +*/ +for (i =0 ; i < num_lines; i++) { +gpiod_line_mask_set_bit (mask, i); +gpiod_line_mask_assign_bit (vals, i, + config->defaults.value); + } + }else { +/* +* Default output value is not output. Iterate over overrides +* and set the default output value for those that override the +* direction to output. Don't touch the ones which override +* the output value. +*/ +for (i =0 ; i <GPIO_V2_LINES_MAX ; i++) { + override = &config->overrides[i]; + +if (override->base.direction != +GPIOD_LINE_DIRECTION_OUTPUT || + !(override->override_flags & +OVERRIDE_FLAG_DIRECTION ) || + (override->override_flags & +OVERRIDE_FLAG_OUTPUT_VALUE )) +continue ; -if (override->value_set) { idx =find_bitmap_index (override->offset, num_lines, offsets); -if (idx <0 ) { - errno =EINVAL ; -return -1 ; - } +if (idx <0 ) +continue ;gpiod_line_mask_set_bit (mask, idx);gpiod_line_mask_assign_bit (vals, idx, - !!override->value); + !!config->defaults.value); } } -return 0 ; -} +/* +* Finally iterate over the overrides again and set the overridden +* output values. +*/ +for (i =0 ; i <GPIO_V2_LINES_MAX ; i++) { + override = &config->overrides[i]; -static bool base_config_flags_are_equal (struct base_config *base, -struct override_config *override) -{ -if (((override->override_flags &OVERRIDE_FLAG_DIRECTION ) && - base->direction != override->base.direction) || - ((override->override_flags &OVERRIDE_FLAG_EDGE ) && - base->edge != override->base.edge) || - ((override->override_flags &OVERRIDE_FLAG_DRIVE ) && - base->drive != override->base.drive) || - ((override->override_flags &OVERRIDE_FLAG_BIAS ) && - base->bias != override->base.bias) || - ((override->override_flags &OVERRIDE_FLAG_ACTIVE_LOW ) && - base->active_low != override->base.active_low) || - ((override->override_flags &OVERRIDE_FLAG_CLOCK ) && - base->clock != override->base.clock)) -return false; +if (!(override->override_flags &OVERRIDE_FLAG_OUTPUT_VALUE )) +continue ; -return true; +if (config->defaults.direction !=GPIOD_LINE_DIRECTION_OUTPUT && + (!(override->override_flags &OVERRIDE_FLAG_DIRECTION ) || + override->base.direction !=GPIOD_LINE_DIRECTION_OUTPUT )) +continue ; + + idx =find_bitmap_index (override->offset, num_lines, offsets); +if (idx <0 ) +continue ; + +gpiod_line_mask_set_bit (mask, idx); +gpiod_line_mask_assign_bit (vals, idx, !!override->base.value); + } } static bool override_config_flags_are_equal(struct override_config *a,@@ -771,16 +951,6 @@ static void set_base_config_flags(struct gpio_v2_line_attribute *attr, attr->flags =make_kernel_flags (&base); } -static bool base_debounce_period_is_equal (struct base_config *base, -struct override_config *override) -{ -if ((override->override_flags &OVERRIDE_FLAG_DEBOUNCE_PERIOD ) && - base->debounce_period_us != override->base.debounce_period_us) -return false; - -return true; -} -static bool override_config_debounce_period_is_equal (struct override_config *a,struct override_config *b) {@@ -801,36 +971,40 @@ set_base_config_debounce_period(struct gpio_v2_line_attribute *attr, attr->debounce_period_us = override->base.debounce_period_us; } -static int set_kernel_attr_mask (uint64_t *out,const uint64_t *in, -unsigned int num_lines, -const unsigned int *offsets, -const struct gpiod_line_config *config) +static void set_kernel_attr_mask (uint64_t *out,const uint64_t *in, +unsigned int num_lines, +const unsigned int *offsets, +struct gpiod_line_config *config) { +struct override_config *override;unsigned int i, j;int off;gpiod_line_mask_zero (out); -for (i =0 ; i < config->num_overrides; i++) { -if (!gpiod_line_mask_test_bit (in, i)) +for (i =0 ; i <GPIO_V2_LINES_MAX ; i++) { + override = &config->overrides[i]; + +if (!override_used (override) || + !gpiod_line_mask_test_bit (in, i))continue ;for (j =0 , off =-1 ; j < num_lines; j++) { -if (offsets[j] == config->overrides[i].offset) { +if (offsets[j] == override->offset) { off = j;break ; } } -if (off <0 ) { - errno =EINVAL ; -return -1 ; - } +/* +* Overridden offsets that are not in the list of offsets to +* request (or already requested) are silently ignored. +*/ +if (off <0 ) +continue ;gpiod_line_mask_set_bit (out, off); } - -return 0 ; } static int process_overrides(struct gpiod_line_config *config,@@ -852,11 +1026,18 @@ static int process_overrides(struct gpiod_line_config *config,struct override_config *current, *next;unsigned int i, j; -for (i =0 ; i < config->num_overrides; i++) { -if (gpiod_line_mask_test_bit (&processed, i)) +for (i =0 ; i <GPIO_V2_LINES_MAX ; i++) { + current = &config->overrides[i]; + +if (!override_used (current) || +gpiod_line_mask_test_bit (&processed, i))continue ; - current = &config->overrides[i]; +if (*attr_idx ==GPIO_V2_LINE_NUM_ATTRS_MAX ) { + errno =E2BIG ; +return -1 ; + } +gpiod_line_mask_set_bit (&processed, i);if (defaults_equal_func (&config->defaults, current))@@ -865,12 +1046,13 @@ static int process_overrides(struct gpiod_line_config *config, marked =0 ;gpiod_line_mask_set_bit (&marked, i); -for (j = i +1 ; j < config->num_overrides; j++) { -if (gpiod_line_mask_test_bit (&processed, j)) -continue ; - +for (j = i +1 ; j <GPIO_V2_LINES_MAX ; j++) { next = &config->overrides[j]; +if (!override_used (next) || +gpiod_line_mask_test_bit (&processed, j)) +continue ; +if (override_equal_func (current, next)) {gpiod_line_mask_set_bit (&marked, j);gpiod_line_mask_set_bit (&processed, j);@@ -878,10 +1060,6 @@ static int process_overrides(struct gpiod_line_config *config, } attr = &cfgbuf->attrs[(*attr_idx)++]; -if (*attr_idx ==GPIO_V2_LINE_NUM_ATTRS_MAX ) { - errno =E2BIG ; -return -1 ; - }set_kernel_attr_mask (&mask, &marked, num_lines, offsets, config);@@ -892,14 +1070,31 @@ static int process_overrides(struct gpiod_line_config *config,return 0 ; } +static bool has_at_least_one_output_direction (struct gpiod_line_config *config) +{ +struct override_config *override; +unsigned int i; + +if (config->defaults.direction ==GPIOD_LINE_DIRECTION_OUTPUT ) +return true; + +for (i =0 ; i <GPIO_V2_LINES_MAX ; i++) { + override = &config->overrides[i]; + +if (override->base.direction ==GPIOD_LINE_DIRECTION_OUTPUT ) +return true; + } + +return false; +} +int gpiod_line_config_to_kernel (struct gpiod_line_config *config,struct gpio_v2_line_config *cfgbuf,unsigned int num_lines,const unsigned int *offsets) {struct gpio_v2_line_config_attribute *attr; -struct override_config *override; -unsigned int attr_idx =0 , i; +unsigned int attr_idx =0 ;uint64_t mask, values;int ret;@@ -907,26 +1102,19 @@ int gpiod_line_config_to_kernel(struct gpiod_line_config *config, goto err_2big;/* -* First check if we have at least one default output value configured. +* First check if we have at least one line configured in output mode. * If so, let's take one attribute for the default values. */ -for (i =0 ; i < config->num_overrides; i++) { - override = &config->overrides[i]; - -if (override->value_set) { - attr = &cfgbuf->attrs[attr_idx++]; - attr->attr.id =GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES ; +if (has_at_least_one_output_direction (config)) { + attr = &cfgbuf->attrs[attr_idx++]; + attr->attr.id =GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES ; - ret =set_kernel_output_values (&mask, &values, config, - num_lines, offsets); -if (ret) -return ret; +set_kernel_output_values (&mask, &values, config, + num_lines, offsets); - attr->attr.values = values; - attr->mask = mask; + attr->attr.values = values; + attr->mask = mask; -break ; - } }/* If we have a default debounce period - use another attribute. */ 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);
Quoted 3 lines
Quoted 3 lines
--
2.30.1
Bart
Re: [libgpiod v2][PATCH v2 1/2] line-config: expose the override logic to users
Quoted 45 lines · Bartosz Golaszewski
Quoted 44 lines · Kent Gibson
Quoted 13 lines · Bartosz Golaszewski
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.
Quoted 9 lines
We also complete the API by providing functions that allow to identify offsets for which at least one setting is overriden.
This way the caller can fully inspect the
line_configand high-level language bindings can provide stringification methods.While at it: we fix a couple bugs in the implementation of struct
line_config.TLDR: I've got a lot of nitpicks on the doco, but it might be simpler for you to apply this patch as is and for me to submit a patch with doco tweaks than for you to try to sort out my comments!
The actual code looks good, so
Reviewed-by Kent Gibson <[email protected]>with or without the suggested doco changes.
Quoted 9 lines
Signed-off-by Bartosz Golaszewski <[email protected]>--- include/gpiod.h | 509 ++++++++++++++++++++----------- lib/line-config.c | 752 +++++++++++++++++++++++++++++----------------- tools/gpioget.c | 6 +- tools/gpiomon.c | 6 +- tools/gpioset.c | 9 +- 5 files changed, 821 insertions(+), 461 deletions(-)
[snip]
Quoted 12 lines
Quoted 11 lines
include/gpiod.h void gpiod_line_config_free(struct gpiod_line_config *config); · line 508
Quoted 7 lines
/**
include/gpiod.h +1
-1@@ -480,329 +505,424 @@ 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. Nitpick: "setting" feels redundant given the "Set". Same thoughout, including the "Get" cases.
Quoted 4 lines
What would you prefer it to be? "@brief Direction of all lines"? Doesn't look right to me. It's normal for the doc to use a verb to say what the function does.
"setting" is the redundant word to be removed:
@brief Set the default direction.
If you want to describe the scope as well then:
@brief Set the default direction for all requested lines.
Though the function scopes are described in the object documentation, I'm good with the shorter version.
Quoted 18 lines
Quoted 8 lines
Quoted 8 lines
include/gpiod.h +2
-2* @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);Quoted 9 lines
include/gpiod.h void gpiod_line_config_free(struct gpiod_line_config *config); · line 516
Quoted 3 lines
include/gpiod.h +1
-1/** -* @brief Set the direction for a single line at given offset. +* @brief Set the direction override at given offset. Nitpick: "at given offset" -> "for the line" throughout. Similarly "at this offset" -> "for this line", and "line at given offset" -> "line",
and "for a given offset" -> "for a line".
Quoted 5 lines
I think that speaking of offsets makes it more explicit and switching to your alternatives dilutes the meaning here. This is still the configuration and we don't have any real lines yet. Just offset -> value mappings.
Are you just winding me up ;-)?
You don't have any __requested__ lines yet, but the lines physically exist and here you are specifying the config to be applied TO THE LINE when it is requested. So the config is most certainly for the line
- the offset is just a line identifier (as is the line name).
If the name were the identifier then your documentation would read:
@brief Set the direction override with the given name.
which makes no sense.
whereas mine would still read:
@brief Set the direction override for the line.
Perhaps rename the function and object "gpiod_offset_config" for clarity?? (being sarcastic - please don't).
Still prefer my suggestion, or even the previous version to the v2 version.
Quoted 55 lines
Quoted 39 lines
Quoted 39 lines
include/gpiod.h +19
-11* @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 Check if the direction 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 direction is overridden at this offset, false otherwise. +*/ +bool gpiod_line_config_direction_is_overridden (struct gpiod_line_config *config, +unsigned int offset);Quoted 15 lines
include/gpiod.h void gpiod_line_config_free(struct gpiod_line_config *config); · line 553
Quoted 11 lines
include/gpiod.h +9
-1/** -* @brief Get the direction setting for a given line. +* @brief Get the default direction setting. +* @param config Line config object. +* @return Direction setting that would have been used for any non-overridden +* offset. +*/ +int gpiod_line_config_get_direction_default (struct gpiod_line_config *config); + +/** +* @brief Get the direction setting for a given offset. Preferred "given line". The direction is an attribute of the line, not the offset. The offset is the line identifier.
Same throughout.
Quoted 3 lines
If I were consistent with my own previous docs, it would be "for the line at given offset".
Sure, but I find the "at given offset" redundant in the function @brief since you repeat it in the @param offset below. Once is enough, and if anywhere then in the @param.
Quoted 8 lines
Quoted 5 lines
Review void gpiod_line_config_free(struct gpiod_line_config *config); · line 555
Quoted 3 lines
(quoted diff) +0
-0* @param config Line config object. * @param offset Line offset for which to read the direction setting.Nitpick: "Line offset" -> "Offset of the line"
Sounds good.
I was on the fence about suggesting adding the "The" as well, so "The offset of the line" to make it more grammatically correct, but it is frequently dropped from documentation and missing in so many other places that I went without for consistency.
But again, this is all just documentation tweaks, so don't get hung up on anything - looking forward to v3.
Cheers,
Kent.
Re: [libgpiod v2][PATCH v2 1/2] line-config: expose the override logic to users
Quoted 62 lines · Kent Gibson
Quoted 45 lines · Bartosz Golaszewski
Quoted 44 lines · Kent Gibson
Quoted 13 lines · Bartosz Golaszewski
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.
Quoted 9 lines
We also complete the API by providing functions that allow to identify offsets for which at least one setting is overriden.
This way the caller can fully inspect the
line_configand high-level language bindings can provide stringification methods.While at it: we fix a couple bugs in the implementation of struct
line_config.TLDR: I've got a lot of nitpicks on the doco, but it might be simpler for you to apply this patch as is and for me to submit a patch with doco tweaks than for you to try to sort out my comments!
The actual code looks good, so
Reviewed-by Kent Gibson <[email protected]>with or without the suggested doco changes.
Quoted 9 lines
Signed-off-by Bartosz Golaszewski <[email protected]>--- include/gpiod.h | 509 ++++++++++++++++++++----------- lib/line-config.c | 752 +++++++++++++++++++++++++++++----------------- tools/gpioget.c | 6 +- tools/gpiomon.c | 6 +- tools/gpioset.c | 9 +- 5 files changed, 821 insertions(+), 461 deletions(-)[snip]
Quoted 12 lines
Quoted 11 lines
include/gpiod.h void gpiod_line_config_free(struct gpiod_line_config *config); · line 508
Quoted 7 lines
/**
include/gpiod.h +1
-1@@ -480,329 +505,424 @@ 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. Nitpick: "setting" feels redundant given the "Set". Same thoughout, including the "Get" cases.
Quoted 7 lines
Quoted 4 lines
What would you prefer it to be? "@brief Direction of all lines"? Doesn't look right to me. It's normal for the doc to use a verb to say what the function does.
"setting" is the redundant word to be removed:
Sorry, it's my ESL.
Quoted 127 lines
@brief Set the default direction.
If you want to describe the scope as well then:
@brief Set the default direction for all requested lines.
Though the function scopes are described in the object documentation, I'm good with the shorter version.
Quoted 18 lines
Quoted 8 lines
Quoted 8 lines
include/gpiod.h +2
-2* @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);Quoted 9 lines
include/gpiod.h void gpiod_line_config_free(struct gpiod_line_config *config); · line 516
Quoted 3 lines
include/gpiod.h +1
-1/** -* @brief Set the direction for a single line at given offset. +* @brief Set the direction override at given offset. Nitpick: "at given offset" -> "for the line" throughout. Similarly "at this offset" -> "for this line", and "line at given offset" -> "line",
and "for a given offset" -> "for a line".Quoted 5 lines
I think that speaking of offsets makes it more explicit and switching to your alternatives dilutes the meaning here. This is still the configuration and we don't have any real lines yet. Just offset -> value mappings.
Are you just winding me up ;-)?
You don't have any
__requested__lines yet, but the lines physically exist and here you are specifying the config to be applied TO THE LINE when it is requested. So the config is most certainly for the line
- the offset is just a line identifier (as is the line name).If the name were the identifier then your documentation would read:
@brief Set the direction override with the given name.
which makes no sense.
whereas mine would still read:
@brief Set the direction override for the line.
Perhaps rename the function and object "
gpiod_offset_config" for clarity?? (being sarcastic - please don't).Still prefer my suggestion, or even the previous version to the v2 version.
Quoted 55 lines
Quoted 39 lines
Quoted 39 lines
include/gpiod.h +19
-11* @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 Check if the direction 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 direction is overridden at this offset, false otherwise. +*/ +bool gpiod_line_config_direction_is_overridden (struct gpiod_line_config *config, +unsigned int offset);Quoted 15 lines
include/gpiod.h void gpiod_line_config_free(struct gpiod_line_config *config); · line 553
Quoted 11 lines
include/gpiod.h +9
-1/** -* @brief Get the direction setting for a given line. +* @brief Get the default direction setting. +* @param config Line config object. +* @return Direction setting that would have been used for any non-overridden +* offset. +*/ +int gpiod_line_config_get_direction_default (struct gpiod_line_config *config); + +/** +* @brief Get the direction setting for a given offset. Preferred "given line". The direction is an attribute of the line, not the offset. The offset is the line identifier.
Same throughout.Quoted 3 lines
If I were consistent with my own previous docs, it would be "for the line at given offset".
Sure, but I find the "at given offset" redundant in the function @brief since you repeat it in the @param offset below. Once is enough, and if anywhere then in the @param.
Quoted 8 lines
Quoted 5 lines
Review void gpiod_line_config_free(struct gpiod_line_config *config); · line 555
Quoted 3 lines
(quoted diff) +0
-0* @param config Line config object. * @param offset Line offset for which to read the direction setting.Nitpick: "Line offset" -> "Offset of the line"
Sounds good.
Quoted 8 lines
I was on the fence about suggesting adding the "The" as well, so "The offset of the line" to make it more grammatically correct, but it is frequently dropped from documentation and missing in so many other places that I went without for consistency.
But again, this is all just documentation tweaks, so don't get hung up on anything - looking forward to v3.
Yeah, I'll just resend what I have and let you handle the docs later. I'm not a native English speaker so what am I even doing second guessing you here. :)
Quoted 2 lines
Cheers,
Kent.
PATCH v2 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 | 20 +- tests/gpiod-test-helpers.c | 49 ++ tests/gpiod-test-helpers.h | 139 +++++ tests/gpiod-test-sim.c | 303 ++++++++++ tests/gpiod-test-sim.h | 42 ++ tests/gpiod-test.c | 233 +------- tests/gpiod-test.h | 81 +-- 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 | 420 +++++++++++++ tests/tests-event.c | 908 ---------------------------- tests/tests-info-event.c | 301 ++++++++++ tests/tests-line-config.c | 448 ++++++++++++++ tests/tests-line-info.c | 316 ++++++++++ tests/tests-line-request.c | 468 +++++++++++++++ tests/tests-line.c | 1091 ---------------------------------- tests/tests-misc.c | 86 ++- tests/tests-request-config.c | 90 +++ 22 files changed, 2809 insertions(+), 3020 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])
@@ -137,14 +136,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
@@ -235,7 +234,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 +14 -6
@@ -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_FLAGS) 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 +303 -0
303 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 -66
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 +420 -0
420 lines not on this page · raw message
tests/tests-event.c deleted +0 -908
708 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 +448 -0
448 lines not on this page · raw message
tests/tests-line-info.c new file +316 -0
316 lines not on this page · raw message
tests/tests-line-request.c new file +468 -0
468 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 +79 -7
86 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.
Re: [libgpiod v2][PATCH v2 2/2] tests: rewrite core C tests using libgpiosim
Quoted 7 lines · Bartosz Golaszewski
This 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.
I was having trouble running the tests in a qemu instance where entropy is slow to collect. The error I get is
Bail out! gpiod-test-FATAL-ERROR: Unable to instantiate new GPIO device: Resource temporarily unavailable
which I backtracked to the getrandom() call in make_random_dir_at().
I have a dislike for random elements in tests as it negatively impacts repeatability. They are only used here for chip and bank names in the configfs, and otherwise have no bearing on the tests?
Why not deterministic naming, say using the test case name, pid,...
Quoted 41 lines
Signed-off-by Bartosz Golaszewski <[email protected]>--- configure.ac | 8 +- tests/Makefile.am | 20 +- tests/gpiod-test-helpers.c | 49 ++ tests/gpiod-test-helpers.h | 139 +++++ tests/gpiod-test-sim.c | 303 ++++++++++ tests/gpiod-test-sim.h | 42 ++ tests/gpiod-test.c | 233 +------- tests/gpiod-test.h | 81 +-- 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 | 420 +++++++++++++ tests/tests-event.c | 908 ---------------------------- tests/tests-info-event.c | 301 ++++++++++ tests/tests-line-config.c | 448 ++++++++++++++ tests/tests-line-info.c | 316 ++++++++++ tests/tests-line-request.c | 468 +++++++++++++++ tests/tests-line.c | 1091 ---------------------------------- tests/tests-misc.c | 86 ++- tests/tests-request-config.c | 90 +++ 22 files changed, 2809 insertions(+), 3020 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
[snip]
Quoted 14 lines
tests/gpiod-test.c +0
-2@@ -2,8 +2,6 @@// SPDX-FileCopyrightText: 2017-2022 Bartosz Golaszewski <[email protected]> #include <errno.h> -#include <glib/gstdio.h> -#include <gpiosim.h> #include <linux/version.h> #include <stdio.h> #include <sys/utsname.h>
tests/gpiod-test.c line 13
Quoted 5 lines
tests/gpiod-test.c +1
-1@@ -12,28 +10,13 @@#include "gpiod-test.h" #define MIN_KERNEL_MAJOR 5 -#define MIN_KERNEL_MINOR 10 +#define MIN_KERNEL_MINOR 16
Might as well bump this to 17 as RCs are available and 16 isn't really sufficient. Similarly gpiosim.c.
[snip]
Quoted 124 lines
tests/tests-edge-event.c new file +118
-0@@ -0,0 +1,420 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +// SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski <[email protected]> + +#include <glib.h> +#include <gpiod.h> + +#include "gpiod-test.h" +#include "gpiod-test-helpers.h" +#include "gpiod-test-sim.h" + +#define GPIOD_TEST_GROUP "edge-event" + +GPIOD_TEST_CASE (edge_event_buffer_capacity) +{ +g_autoptr (struct_gpiod_edge_event_buffer) buffer =NULL ; + + buffer =gpiod_test_create_edge_event_buffer_or_fail (32 ); + + g_assert_cmpuint(gpiod_edge_event_buffer_get_capacity(buffer), ==, 32); +} + +GPIOD_TEST_CASE (edge_event_buffer_max_capacity) +{ +g_autoptr (struct_gpiod_edge_event_buffer) buffer =NULL ; + + buffer =gpiod_test_create_edge_event_buffer_or_fail (16 *64 *2 ); + +g_assert_cmpuint (gpiod_edge_event_buffer_get_capacity (buffer), + ==, 16 * 64); +} + +GPIOD_TEST_CASE (edge_event_wait_timeout) +{ +static const guint offset =4 ; + +g_autoptr (GPIOSimChip) sim =g_gpiosim_chip_new ("num-lines" ,8 ,NULL ); +g_autoptr (struct_gpiod_chip) chip =NULL ; +g_autoptr (struct_gpiod_request_config) req_cfg =NULL ; +g_autoptr (struct_gpiod_line_config) line_cfg =NULL ; +g_autoptr (struct_gpiod_line_request) request =NULL ; +gint ret; + + chip =gpiod_test_open_chip_or_fail (g_gpiosim_chip_get_dev_path (sim)); + req_cfg =gpiod_test_create_request_config_or_fail (); + line_cfg =gpiod_test_create_line_config_or_fail (); + +gpiod_request_config_set_offsets (req_cfg,1 , &offset); +gpiod_line_config_set_edge_detection_default (line_cfg, +GPIOD_LINE_EDGE_BOTH ); + + request =gpiod_test_request_lines_or_fail (chip, req_cfg, line_cfg); + + ret =gpiod_line_request_edge_event_wait (request,1000000 ); + g_assert_cmpint(ret, ==, 0); +} + +GPIOD_TEST_CASE (cannot_request_lines_in_output_mode_with_edge_detection) +{ +static const guint offset =4 ; + +g_autoptr (GPIOSimChip) sim =g_gpiosim_chip_new ("num-lines" ,8 ,NULL ); +g_autoptr (struct_gpiod_chip) chip =NULL ; +g_autoptr (struct_gpiod_request_config) req_cfg =NULL ; +g_autoptr (struct_gpiod_line_config) line_cfg =NULL ; +g_autoptr (struct_gpiod_line_request) request =NULL ; + + chip =gpiod_test_open_chip_or_fail (g_gpiosim_chip_get_dev_path (sim)); + req_cfg =gpiod_test_create_request_config_or_fail (); + line_cfg =gpiod_test_create_line_config_or_fail (); + +gpiod_request_config_set_offsets (req_cfg,1 , &offset); +gpiod_line_config_set_edge_detection_default (line_cfg, +GPIOD_LINE_EDGE_BOTH ); +gpiod_line_config_set_direction_default (line_cfg, +GPIOD_LINE_DIRECTION_OUTPUT ); + + request =gpiod_chip_request_lines (chip, req_cfg, line_cfg); +g_assert_null (request); +gpiod_test_expect_errno (EINVAL ); +} + +static gpointer falling_and_rising_edge_events (gpointer data) +{ +GPIOSimChip *sim = data; + +g_usleep (50 ); + +g_gpiosim_chip_set_pull (sim,2 ,G_GPIOSIM_PULL_UP ); + +g_usleep (50 ); + +g_gpiosim_chip_set_pull (sim,2 ,G_GPIOSIM_PULL_DOWN ); + +return NULL ; +} + +GPIOD_TEST_CASE (read_both_events) +{ +static const guint offset =2 ; + +g_autoptr (GPIOSimChip) sim =g_gpiosim_chip_new ("num-lines" ,8 ,NULL ); +g_autoptr (struct_gpiod_chip) chip =NULL ; +g_autoptr (struct_gpiod_request_config) req_cfg =NULL ; +g_autoptr (struct_gpiod_line_config) line_cfg =NULL ; +g_autoptr (struct_gpiod_line_request) request =NULL ; +g_autoptr (GThread) thread =NULL ; +g_autoptr (struct_gpiod_edge_event_buffer) buffer =NULL ; +struct gpiod_edge_event *event; +guint64 ts_rising, ts_falling; +gint ret; + + chip =gpiod_test_open_chip_or_fail (g_gpiosim_chip_get_dev_path (sim)); + req_cfg =gpiod_test_create_request_config_or_fail (); + line_cfg =gpiod_test_create_line_config_or_fail (); + buffer =gpiod_test_create_edge_event_buffer_or_fail (64 ); + +gpiod_request_config_set_offsets (req_cfg,1 , &offset); + gpiod_line_config_set_direction_default(line_cfg,
tests/tests-edge-event.c line 128
Quoted 10 lines
tests/tests-edge-event.c new file +10
-0+ GPIOD_LINE_DIRECTION_INPUT); +gpiod_line_config_set_edge_detection_default (line_cfg, +GPIOD_LINE_EDGE_BOTH ); + + request =gpiod_test_request_lines_or_fail (chip, req_cfg, line_cfg); + + thread =g_thread_new ("request-release" , + falling_and_rising_edge_events, sim); +g_thread_ref (thread); +
Can you find a more appropraite name than "request-release"? Cos it doesn't do either. Maybe "edge-generator"? (Ah, it is a cut-and-paste from tests-info-events.c)
And perhaps edge_gen rather than thread?
Same elsewhere.
[snip]
Quoted 20 lines
tests/tests-info-event.c new file +14
-0@@ -0,0 +1,301 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +// SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski <[email protected]> + +#include <glib.h> +#include <gpiod.h> +#include <poll.h> + +#include "gpiod-test.h" +#include "gpiod-test-helpers.h" +#include "gpiod-test-sim.h" + +#define GPIOD_TEST_GROUP "info-event" + +GPIOD_TEST_CASE (watching_info_events_returns_line_info)
tests/tests-info-event.c line 24
Quoted 10 lines
tests/tests-info-event.c new file +10
-0+{ +g_autoptr (GPIOSimChip) sim =g_gpiosim_chip_new ("num-lines" ,8 ,NULL ); +g_autoptr (struct_gpiod_chip) chip =NULL ; +g_autoptr (struct_gpiod_line_info) info =NULL ; + + chip =gpiod_test_open_chip_or_fail (g_gpiosim_chip_get_dev_path (sim)); + + info =gpiod_chip_watch_line_info (chip,3 ); +g_assert_nonnull (info); + g_assert_cmpuint(gpiod_line_info_get_offset(info), ==, 3);
Other info fields?
Quoted 158 lines
tests/tests-info-event.c new file +158
-0+} + +GPIOD_TEST_CASE (try_offset_out_of_range) +{ +g_autoptr (GPIOSimChip) sim =g_gpiosim_chip_new ("num-lines" ,8 ,NULL ); +g_autoptr (struct_gpiod_chip) chip =NULL ; +g_autoptr (struct_gpiod_line_info) info =NULL ; + + chip =gpiod_test_open_chip_or_fail (g_gpiosim_chip_get_dev_path (sim)); + + info =gpiod_chip_watch_line_info (chip,10 ); +g_assert_null (info); +gpiod_test_expect_errno (EINVAL ); +} + +GPIOD_TEST_CASE (event_timeout) +{ +g_autoptr (GPIOSimChip) sim =g_gpiosim_chip_new ("num-lines" ,8 ,NULL ); +g_autoptr (struct_gpiod_chip) chip =NULL ; +g_autoptr (struct_gpiod_line_info) info =NULL ; +gint ret; + + chip =gpiod_test_open_chip_or_fail (g_gpiosim_chip_get_dev_path (sim)); + + info =gpiod_chip_watch_line_info (chip,6 ); +g_assert_nonnull (info); +gpiod_test_return_if_failed (); + + ret =gpiod_chip_info_event_wait (chip,100000000 ); + g_assert_cmpint(ret, ==, 0); +} + +struct request_ctx { +struct gpiod_chip *chip; +struct gpiod_request_config *req_cfg; +struct gpiod_line_config *line_cfg; +}; + +static gpointer request_release_line (gpointer data) +{ +g_autoptr (struct_gpiod_line_request) request =NULL ; +struct request_ctx *ctx = data; +gint ret; + +g_usleep (50 ); + + request =gpiod_chip_request_lines (ctx->chip, + ctx->req_cfg, ctx->line_cfg); +g_assert_nonnull (request); +if (g_test_failed ()) +return NULL ; + +g_usleep (50 ); + +gpiod_line_config_set_direction_default (ctx->line_cfg, +GPIOD_LINE_DIRECTION_OUTPUT ); + + ret =gpiod_line_request_reconfigure_lines (request, ctx->line_cfg); + g_assert_cmpint(ret, ==, 0); +if (g_test_failed ()) +return NULL ; + +g_usleep (50 ); + +gpiod_line_request_release (request); + request =NULL ; + +return NULL ; +} + +GPIOD_TEST_CASE (request_reconfigure_release_events) +{ +static const guint offset =3 ; + +g_autoptr (GPIOSimChip) sim =g_gpiosim_chip_new ("num-lines" ,8 ,NULL ); +g_autoptr (struct_gpiod_chip) chip =NULL ; +g_autoptr (struct_gpiod_line_info) info =NULL ; +g_autoptr (struct_gpiod_info_event) request_event =NULL ; +g_autoptr (struct_gpiod_info_event) reconfigure_event =NULL ; +g_autoptr (struct_gpiod_info_event) release_event =NULL ; +g_autoptr (struct_gpiod_request_config) req_cfg =NULL ; +g_autoptr (struct_gpiod_line_config) line_cfg =NULL ; +g_autoptr (GThread) thread =NULL ; +struct gpiod_line_info *request_info, *reconfigure_info, *release_info; +guint64 request_ts, reconfigure_ts, release_ts; +struct request_ctx ctx; +gint ret; + + chip =gpiod_test_open_chip_or_fail (g_gpiosim_chip_get_dev_path (sim)); + req_cfg =gpiod_test_create_request_config_or_fail (); + line_cfg =gpiod_test_create_line_config_or_fail (); + +gpiod_request_config_set_offsets (req_cfg,1 , &offset); + + info =gpiod_chip_watch_line_info (chip,3 ); +g_assert_nonnull (info); +gpiod_test_return_if_failed (); + +g_assert_false (gpiod_line_info_is_used (info)); + + ctx.chip = chip; + ctx.req_cfg = req_cfg; + ctx.line_cfg = line_cfg; + + thread =g_thread_new ("request-release" , request_release_line, &ctx); +g_thread_ref (thread); + + ret =gpiod_chip_info_event_wait (chip,1000000000 ); + g_assert_cmpint(ret, >, 0); +gpiod_test_join_thread_and_return_if_failed (thread); + + request_event =gpiod_chip_info_event_read (chip); +g_assert_nonnull (request_event); +gpiod_test_join_thread_and_return_if_failed (thread); + + g_assert_cmpint(gpiod_info_event_get_event_type(request_event), ==, +GPIOD_INFO_EVENT_LINE_REQUESTED ); + + request_info =gpiod_info_event_get_line_info (request_event); + + g_assert_cmpuint(gpiod_line_info_get_offset(request_info), ==, 3); +g_assert_true (gpiod_line_info_is_used (request_info)); + g_assert_cmpint(gpiod_line_info_get_direction(request_info), ==, +GPIOD_LINE_DIRECTION_INPUT ); + + ret =gpiod_chip_info_event_wait (chip,1000000000 ); + g_assert_cmpint(ret, >, 0); +gpiod_test_join_thread_and_return_if_failed (thread); + + reconfigure_event =gpiod_chip_info_event_read (chip); +g_assert_nonnull (reconfigure_event); +gpiod_test_join_thread_and_return_if_failed (thread); + + g_assert_cmpint(gpiod_info_event_get_event_type(reconfigure_event), ==, +GPIOD_INFO_EVENT_LINE_CONFIG_CHANGED ); + + reconfigure_info =gpiod_info_event_get_line_info (reconfigure_event); + + g_assert_cmpuint(gpiod_line_info_get_offset(reconfigure_info), ==, 3); +g_assert_true (gpiod_line_info_is_used (reconfigure_info)); + g_assert_cmpint(gpiod_line_info_get_direction(reconfigure_info), ==, +GPIOD_LINE_DIRECTION_OUTPUT ); + + ret =gpiod_chip_info_event_wait (chip,1000000000 ); + g_assert_cmpint(ret, >, 0); +gpiod_test_join_thread_and_return_if_failed (thread); + + release_event =gpiod_chip_info_event_read (chip); +g_assert_nonnull (release_event); +gpiod_test_join_thread_and_return_if_failed (thread); + + g_assert_cmpint(gpiod_info_event_get_event_type(release_event), ==, +GPIOD_INFO_EVENT_LINE_RELEASED ); + + release_info =gpiod_info_event_get_line_info (release_event); + + g_assert_cmpuint(gpiod_line_info_get_offset(release_info), ==, 3); +g_assert_false (gpiod_line_info_is_used (release_info));
tests/tests-info-event.c line 192
Quoted 10 lines
tests/tests-info-event.c new file +10
-0+ +g_thread_join (thread); + + request_ts =gpiod_info_event_get_timestamp (request_event); + reconfigure_ts =gpiod_info_event_get_timestamp (reconfigure_event); + release_ts =gpiod_info_event_get_timestamp (release_event); + + g_assert_cmpuint(request_ts, <, reconfigure_ts); + g_assert_cmpuint(reconfigure_ts, <, release_ts); +}
Is multi-threading really necessary here (and elsewhere, but this the first case where the secondary thread is ALSO asserting)? Couldn't you provide the stimulous and check the result from the one thread?
[snip]
Quoted 93 lines
tests/tests-info-event.c new file +92
-0+++ b/tests/tests-line-config.c@@ -0,0 +1,448 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +// SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski <[email protected]> + +#include <errno.h> +#include <glib.h> +#include <gpiod.h> +#include <stdint.h> + +#include "gpiod-test.h" +#include "gpiod-test-helpers.h" +#include "gpiod-test-sim.h" + +#define GPIOD_TEST_GROUP "line-config" + +GPIOD_TEST_CASE (default_config) +{ +g_autoptr (struct_gpiod_line_config) config =NULL ; + + config =gpiod_test_create_line_config_or_fail (); + + g_assert_cmpint(gpiod_line_config_get_direction_default(config), ==, +GPIOD_LINE_DIRECTION_AS_IS ); +g_assert_cmpint (gpiod_line_config_get_edge_detection_default (config), + ==, GPIOD_LINE_EDGE_NONE); + g_assert_cmpint(gpiod_line_config_get_bias_default(config), ==, +GPIOD_LINE_BIAS_AS_IS ); + g_assert_cmpint(gpiod_line_config_get_drive_default(config), ==, +GPIOD_LINE_DRIVE_PUSH_PULL ); +g_assert_false (gpiod_line_config_get_active_low_default (config)); +g_assert_cmpuint ( + gpiod_line_config_get_debounce_period_us_default(config), ==, +0 ); + g_assert_cmpint(gpiod_line_config_get_event_clock_default(config), ==, +GPIOD_LINE_EVENT_CLOCK_MONOTONIC ); + g_assert_cmpint(gpiod_line_config_get_output_value_default(config), ==, +0 ); +g_assert_cmpuint (gpiod_line_config_get_num_overridden_offsets (config), + ==, 0); +} + +GPIOD_TEST_CASE (defaults_are_used_for_non_overridden_offsets) +{ +g_autoptr (struct_gpiod_line_config) config =NULL ; + + config =gpiod_test_create_line_config_or_fail (); + + g_assert_cmpint(gpiod_line_config_get_direction_offset(config, 4), ==, +GPIOD_LINE_DIRECTION_AS_IS ); +g_assert_cmpint (gpiod_line_config_get_edge_detection_offset (config,4 ), + ==, GPIOD_LINE_EDGE_NONE); + g_assert_cmpint(gpiod_line_config_get_bias_offset(config, 4), ==, +GPIOD_LINE_BIAS_AS_IS ); + g_assert_cmpint(gpiod_line_config_get_drive_offset(config, 4), ==, +GPIOD_LINE_DRIVE_PUSH_PULL ); +g_assert_false (gpiod_line_config_get_active_low_offset (config,4 )); +g_assert_cmpuint ( + gpiod_line_config_get_debounce_period_us_offset(config, 4), ==, +0 ); +g_assert_cmpint (gpiod_line_config_get_event_clock_offset (config,4 ), + ==, GPIOD_LINE_EVENT_CLOCK_MONOTONIC); +g_assert_cmpint (gpiod_line_config_get_output_value_offset (config,4 ), + ==, 0); +g_assert_cmpuint (gpiod_line_config_get_num_overridden_offsets (config), + ==, 0); +} + +GPIOD_TEST_CASE (set_and_clear_direction_override) +{ +g_autoptr (struct_gpiod_line_config) config =NULL ; + + config =gpiod_test_create_line_config_or_fail (); + + g_assert_cmpint(gpiod_line_config_get_direction_default(config), ==, +GPIOD_LINE_DIRECTION_AS_IS ); +gpiod_line_config_set_direction_override (config, +GPIOD_LINE_DIRECTION_OUTPUT , +3 ); + + g_assert_cmpint(gpiod_line_config_get_direction_default(config), ==, +GPIOD_LINE_DIRECTION_AS_IS ); + g_assert_cmpint(gpiod_line_config_get_direction_offset(config, 3), ==, +GPIOD_LINE_DIRECTION_OUTPUT ); +g_assert_true (gpiod_line_config_direction_is_overridden (config,3 )); +gpiod_line_config_clear_direction_override (config,3 ); + g_assert_cmpint(gpiod_line_config_get_direction_offset(config, 3), ==, +GPIOD_LINE_DIRECTION_AS_IS ); +g_assert_false (gpiod_line_config_direction_is_overridden (config,3 )); +} + +GPIOD_TEST_CASE (invalid_direction) +{
tests/tests-info-event.c line 101
Quoted 10 lines
tests/tests-info-event.c new file +10
-0+g_autoptr (struct_gpiod_line_config) config =NULL ; + + config =gpiod_test_create_line_config_or_fail (); + +gpiod_line_config_set_direction_default (config,INT32_MAX ); +g_assert_cmpint (gpiod_line_config_get_direction_default (config), + ==, GPIOD_LINE_DIRECTION_AS_IS); +} + +GPIOD_TEST_CASE (set_and_clear_edge_override)
That should be _edge_detection_ to fit the pattern.
Quoted 45 lines
tests/tests-info-event.c new file +45
-0+{ +g_autoptr (struct_gpiod_line_config) config =NULL ; + + config =gpiod_test_create_line_config_or_fail (); + +g_assert_cmpint (gpiod_line_config_get_edge_detection_default (config), + ==, GPIOD_LINE_EDGE_NONE); +gpiod_line_config_set_edge_detection_override (config, +GPIOD_LINE_EDGE_FALLING ,3 ); + +g_assert_cmpint (gpiod_line_config_get_edge_detection_default (config), + ==, GPIOD_LINE_EDGE_NONE); +g_assert_cmpint (gpiod_line_config_get_edge_detection_offset (config,3 ), + ==, GPIOD_LINE_EDGE_FALLING); +g_assert_true (gpiod_line_config_edge_detection_is_overridden (config, +3 )); +gpiod_line_config_clear_edge_detection_override (config,3 ); +g_assert_cmpint (gpiod_line_config_get_edge_detection_offset (config,3 ), + ==, GPIOD_LINE_EDGE_NONE); +g_assert_false (gpiod_line_config_edge_detection_is_overridden (config, +3 )); +} + +GPIOD_TEST_CASE (invalid_edge) +{ +g_autoptr (struct_gpiod_line_config) config =NULL ; + + config =gpiod_test_create_line_config_or_fail (); + +gpiod_line_config_set_edge_detection_default (config,INT32_MAX ); +g_assert_cmpint (gpiod_line_config_get_edge_detection_default (config), + ==, GPIOD_LINE_EDGE_NONE); +} + +GPIOD_TEST_CASE (set_and_clear_bias_override) +{ +g_autoptr (struct_gpiod_line_config) config =NULL ; + + config =gpiod_test_create_line_config_or_fail (); + +g_assert_cmpint (gpiod_line_config_get_bias_default (config), + ==, GPIOD_LINE_BIAS_AS_IS); +gpiod_line_config_set_bias_override (config,GPIOD_LINE_BIAS_PULL_UP ,3 ); + + g_assert_cmpint(gpiod_line_config_get_bias_default(config),
tests/tests-info-event.c line 156
Quoted 10 lines
tests/tests-info-event.c new file +10
-0+ ==, GPIOD_LINE_BIAS_AS_IS); +g_assert_cmpint (gpiod_line_config_get_bias_offset (config,3 ), + ==, GPIOD_LINE_BIAS_PULL_UP); +g_assert_true (gpiod_line_config_bias_is_overridden (config,3 )); +gpiod_line_config_clear_bias_override (config,3 ); +g_assert_cmpint (gpiod_line_config_get_bias_offset (config,3 ), + ==, GPIOD_LINE_BIAS_AS_IS); +g_assert_false (gpiod_line_config_bias_is_overridden (config,3 )); +} +
You like to use 3 and 4 for offsets. Do yopu test 0 somewhere? Cos it is an edge case (no pun intended).
Quoted 52 lines
tests/tests-info-event.c new file +52
-0+GPIOD_TEST_CASE (invalid_bias) +{ +g_autoptr (struct_gpiod_line_config) config =NULL ; + + config =gpiod_test_create_line_config_or_fail (); + +gpiod_line_config_set_bias_default (config,INT32_MAX ); +g_assert_cmpint (gpiod_line_config_get_bias_default (config), + ==, GPIOD_LINE_BIAS_AS_IS); +} + +GPIOD_TEST_CASE (set_and_clear_drive_override) +{ +g_autoptr (struct_gpiod_line_config) config =NULL ; + + config =gpiod_test_create_line_config_or_fail (); + +g_assert_cmpint (gpiod_line_config_get_drive_default (config), + ==, GPIOD_LINE_DRIVE_PUSH_PULL); +gpiod_line_config_set_drive_override (config, +GPIOD_LINE_DRIVE_OPEN_DRAIN ,3 ); + +g_assert_cmpint (gpiod_line_config_get_drive_default (config), + ==, GPIOD_LINE_DRIVE_PUSH_PULL); +g_assert_cmpint (gpiod_line_config_get_drive_offset (config,3 ), + ==, GPIOD_LINE_DRIVE_OPEN_DRAIN); +g_assert_true (gpiod_line_config_drive_is_overridden (config,3 )); +gpiod_line_config_clear_drive_override (config,3 ); +g_assert_cmpint (gpiod_line_config_get_drive_offset (config,3 ), + ==, GPIOD_LINE_DRIVE_PUSH_PULL); +g_assert_false (gpiod_line_config_drive_is_overridden (config,3 )); +} + +GPIOD_TEST_CASE (invalid_drive) +{ +g_autoptr (struct_gpiod_line_config) config =NULL ; + + config =gpiod_test_create_line_config_or_fail (); + +gpiod_line_config_set_drive_default (config,INT32_MAX ); +g_assert_cmpint (gpiod_line_config_get_drive_default (config), + ==, GPIOD_LINE_BIAS_AS_IS); +} + +GPIOD_TEST_CASE (set_and_clear_active_low_override) +{ +g_autoptr (struct_gpiod_line_config) config =NULL ; + + config =gpiod_test_create_line_config_or_fail (); + +g_assert_false (gpiod_line_config_get_active_low_default (config)); +gpiod_line_config_set_active_low_override (config, true,3 );
tests/tests-info-event.c line 218
Quoted 10 lines
tests/tests-info-event.c new file +10
-0+ +g_assert_false (gpiod_line_config_get_active_low_default (config)); +g_assert_true (gpiod_line_config_get_active_low_offset (config,3 )); +g_assert_true (gpiod_line_config_active_low_is_overridden (config,3 )); +gpiod_line_config_clear_active_low_override (config,3 ); +g_assert_false (gpiod_line_config_get_active_low_offset (config,3 )); +g_assert_false (gpiod_line_config_active_low_is_overridden (config,3 )); +} + +GPIOD_TEST_CASE (set_and_clear_debounce_period_override)
I'll let that one slide.
[snip]
Quoted 108 lines
tests/tests-line-info.c new file +102
-0@@ -0,0 +1,316 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +// SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski <[email protected]> + +#include <errno.h> +#include <glib.h> +#include <gpiod.h> + +#include "gpiod-test.h" +#include "gpiod-test-helpers.h" +#include "gpiod-test-sim.h" + +#define GPIOD_TEST_GROUP "line-info" + +GPIOD_TEST_CASE (get_line_info_good) +{ +g_autoptr (GPIOSimChip) sim =g_gpiosim_chip_new ("num-lines" ,8 ,NULL ); +g_autoptr (struct_gpiod_chip) chip =NULL ; +g_autoptr (struct_gpiod_line_info) info =NULL ; + + chip =gpiod_test_open_chip_or_fail (g_gpiosim_chip_get_dev_path (sim)); + + info =gpiod_chip_get_line_info (chip,3 ); +g_assert_nonnull (info); + g_assert_cmpuint(gpiod_line_info_get_offset(info), ==, 3); +} + +GPIOD_TEST_CASE (get_line_info_offset_out_of_range) +{ +g_autoptr (GPIOSimChip) sim =g_gpiosim_chip_new ("num-lines" ,8 ,NULL ); +g_autoptr (struct_gpiod_chip) chip =NULL ; +g_autoptr (struct_gpiod_line_info) info =NULL ; + + chip =gpiod_test_open_chip_or_fail (g_gpiosim_chip_get_dev_path (sim)); + + info =gpiod_chip_get_line_info (chip,8 ); +g_assert_null (info); +gpiod_test_expect_errno (EINVAL ); +} + +GPIOD_TEST_CASE (line_info_basic_properties) +{ +static const struct gpiod_test_line_name names[] = { + { .offset =1 , .name ="foo" , }, + { .offset =2 , .name ="bar" , }, + { .offset =4 , .name ="baz" , }, + { .offset =5 , .name ="xyz" , }, + { } + }; + +static const struct gpiod_test_hog hogs[] = { + { + .offset =3 , + .name ="hog3" , + .direction =G_GPIOSIM_HOG_DIR_OUTPUT_HIGH , + }, + { + .offset =4 , + .name ="hog4" , + .direction =G_GPIOSIM_HOG_DIR_OUTPUT_LOW , + }, + { } + }; + +g_autoptr (GPIOSimChip) sim =NULL ; +g_autoptr (struct_gpiod_chip) chip =NULL ; +g_autoptr (struct_gpiod_line_info) info4 =NULL ; +g_autoptr (struct_gpiod_line_info) info6 =NULL ; + + sim =g_gpiosim_chip_new ( +"num-lines" ,8 , +"line-names" ,gpiod_test_package_line_names (names), +"hogs" ,gpiod_test_package_hogs (hogs), +NULL ); + + chip =gpiod_test_open_chip_or_fail (g_gpiosim_chip_get_dev_path (sim)); + info4 =gpiod_test_get_line_info_or_fail (chip,4 ); + info6 =gpiod_test_get_line_info_or_fail (chip,6 ); + + g_assert_cmpuint(gpiod_line_info_get_offset(info4), ==, 4); + g_assert_cmpstr(gpiod_line_info_get_name(info4), ==, "baz"); + g_assert_cmpstr(gpiod_line_info_get_consumer(info4), ==, "hog4"); +g_assert_true (gpiod_line_info_is_used (info4)); + g_assert_cmpint(gpiod_line_info_get_direction(info4), ==, +GPIOD_LINE_DIRECTION_OUTPUT ); + g_assert_cmpint(gpiod_line_info_get_edge_detection(info4), ==, +GPIOD_LINE_EDGE_NONE ); +g_assert_false (gpiod_line_info_is_active_low (info4)); + g_assert_cmpint(gpiod_line_info_get_bias(info4), ==, +GPIOD_LINE_BIAS_UNKNOWN ); + g_assert_cmpint(gpiod_line_info_get_drive(info4), ==, +GPIOD_LINE_DRIVE_PUSH_PULL ); + g_assert_cmpint(gpiod_line_info_get_event_clock(info4), ==, +GPIOD_LINE_EVENT_CLOCK_MONOTONIC ); +g_assert_false (gpiod_line_info_is_debounced (info4)); + g_assert_cmpuint(gpiod_line_info_get_debounce_period_us(info4), ==, 0); +} + +GPIOD_TEST_CASE (copy_line_info) +{ +g_autoptr (GPIOSimChip) sim =g_gpiosim_chip_new ("num-lines" ,8 ,NULL ); +g_autoptr (struct_gpiod_chip) chip =NULL ; +g_autoptr (struct_gpiod_line_info) info =NULL ;
tests/tests-line-info.c line 112
Quoted 10 lines
tests/tests-line-info.c new file +10
-0+g_autoptr (struct_gpiod_line_info) copy =NULL ; + + chip =gpiod_test_open_chip_or_fail (g_gpiosim_chip_get_dev_path (sim)); + info =gpiod_test_get_line_info_or_fail (chip,3 ); + + copy =gpiod_line_info_copy (info); +g_assert_nonnull (copy); +g_assert_true (info != copy); +} +
So the pointers are different.
Are the contents the same?
Quoted 15 lines
tests/tests-line-info.c new file +15
-0+GPIOD_TEST_CASE (active_high) +{ +static const guint offset =5 ; + +g_autoptr (GPIOSimChip) sim =g_gpiosim_chip_new ("num-lines" ,8 ,NULL ); +g_autoptr (struct_gpiod_chip) chip =NULL ; +g_autoptr (struct_gpiod_request_config) req_cfg =NULL ; +g_autoptr (struct_gpiod_line_config) line_cfg =NULL ; +g_autoptr (struct_gpiod_line_request) request =NULL ; +g_autoptr (struct_gpiod_line_info) info =NULL ; + + chip =gpiod_test_open_chip_or_fail (g_gpiosim_chip_get_dev_path (sim)); + req_cfg =gpiod_test_create_request_config_or_fail (); + line_cfg =gpiod_test_create_line_config_or_fail (); +
tests/tests-line-info.c line 137
Quoted 10 lines
tests/tests-line-info.c new file +10
-0+gpiod_request_config_set_offsets (req_cfg,1 , &offset); +gpiod_line_config_set_active_low_default (line_cfg, true); + + request =gpiod_test_request_lines_or_fail (chip, req_cfg, line_cfg); + info =gpiod_chip_get_line_info (chip,5 ); + +g_assert_true (gpiod_line_info_is_active_low (info)); +} + +GPIOD_TEST_CASE (edge_settings)
_edge_detection_
[snip]
tests/tests-line-request.c line 3
Quoted 9 lines
tests/tests-line-request.c new file +3
-0@@ -0,0 +1,468 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +// SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski <[email protected]> +
Only a couple of minor points for this file:
How about some tests where the offsets are not ordered? And a few more with len(offsets) != 4.
[snip to end]
Nothing major.
Cheers,
Kent.
Re: [libgpiod v2][PATCH v2 2/2] tests: rewrite core C tests using libgpiosim
Quoted 13 lines · Kent Gibson
Quoted 7 lines · Bartosz Golaszewski
This 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.
I was having trouble running the tests in a qemu instance where entropy is slow to collect. The error I get is
Quoted 9 lines
Bail out! gpiod-test-FATAL-ERROR: Unable to instantiate new GPIO device: Resource temporarily unavailable
which I backtracked to the
getrandom()call inmake_random_dir_at().I have a dislike for random elements in tests as it negatively impacts repeatability. They are only used here for chip and bank names in the configfs, and otherwise have no bearing on the tests?
Why not deterministic naming, say using the test case name, pid,...
We can have multiple random names in use at once. And on the off chance that something goes wrong, a random configfs dir just stays there while a deterministic one can hose other tests. Can't you just install havaged in whatever rootfs you're using?
[snip]
Quoted 8 lines
tests/gpiod-test.c line 13
Quoted 4 lines
#define
MIN_KERNEL_MAJOR5tests/gpiod-test.c +1
-1-#define MIN_KERNEL_MINOR 10 +#define MIN_KERNEL_MINOR 16Might as well bump this to 17 as RCs are available and 16 isn't really sufficient. Similarly
gpiosim.c.
I'm building this on my laptop so prefer to have it at 16 for now. We'll have 5.17 out in three weeks, I'll bump it then.
[snip]
Quoted 96 lines
Quoted 96 lines
tests/tests-info-event.c new file +96
-0+ +GPIOD_TEST_CASE (request_reconfigure_release_events) +{ +static const guint offset =3 ; + +g_autoptr (GPIOSimChip) sim =g_gpiosim_chip_new ("num-lines" ,8 ,NULL ); +g_autoptr (struct_gpiod_chip) chip =NULL ; +g_autoptr (struct_gpiod_line_info) info =NULL ; +g_autoptr (struct_gpiod_info_event) request_event =NULL ; +g_autoptr (struct_gpiod_info_event) reconfigure_event =NULL ; +g_autoptr (struct_gpiod_info_event) release_event =NULL ; +g_autoptr (struct_gpiod_request_config) req_cfg =NULL ; +g_autoptr (struct_gpiod_line_config) line_cfg =NULL ; +g_autoptr (GThread) thread =NULL ; +struct gpiod_line_info *request_info, *reconfigure_info, *release_info; +guint64 request_ts, reconfigure_ts, release_ts; +struct request_ctx ctx; +gint ret; + + chip =gpiod_test_open_chip_or_fail (g_gpiosim_chip_get_dev_path (sim)); + req_cfg =gpiod_test_create_request_config_or_fail (); + line_cfg =gpiod_test_create_line_config_or_fail (); + +gpiod_request_config_set_offsets (req_cfg,1 , &offset); + + info =gpiod_chip_watch_line_info (chip,3 ); +g_assert_nonnull (info); +gpiod_test_return_if_failed (); + +g_assert_false (gpiod_line_info_is_used (info)); + + ctx.chip = chip; + ctx.req_cfg = req_cfg; + ctx.line_cfg = line_cfg; + + thread =g_thread_new ("request-release" , request_release_line, &ctx); +g_thread_ref (thread); + + ret =gpiod_chip_info_event_wait (chip,1000000000 ); + g_assert_cmpint(ret, >, 0); +gpiod_test_join_thread_and_return_if_failed (thread); + + request_event =gpiod_chip_info_event_read (chip); +g_assert_nonnull (request_event); +gpiod_test_join_thread_and_return_if_failed (thread); + + g_assert_cmpint(gpiod_info_event_get_event_type(request_event), ==, +GPIOD_INFO_EVENT_LINE_REQUESTED ); + + request_info =gpiod_info_event_get_line_info (request_event); + + g_assert_cmpuint(gpiod_line_info_get_offset(request_info), ==, 3); +g_assert_true (gpiod_line_info_is_used (request_info)); + g_assert_cmpint(gpiod_line_info_get_direction(request_info), ==, +GPIOD_LINE_DIRECTION_INPUT ); + + ret =gpiod_chip_info_event_wait (chip,1000000000 ); + g_assert_cmpint(ret, >, 0); +gpiod_test_join_thread_and_return_if_failed (thread); + + reconfigure_event =gpiod_chip_info_event_read (chip); +g_assert_nonnull (reconfigure_event); +gpiod_test_join_thread_and_return_if_failed (thread); + + g_assert_cmpint(gpiod_info_event_get_event_type(reconfigure_event), ==, +GPIOD_INFO_EVENT_LINE_CONFIG_CHANGED ); + + reconfigure_info =gpiod_info_event_get_line_info (reconfigure_event); + + g_assert_cmpuint(gpiod_line_info_get_offset(reconfigure_info), ==, 3); +g_assert_true (gpiod_line_info_is_used (reconfigure_info)); + g_assert_cmpint(gpiod_line_info_get_direction(reconfigure_info), ==, +GPIOD_LINE_DIRECTION_OUTPUT ); + + ret =gpiod_chip_info_event_wait (chip,1000000000 ); + g_assert_cmpint(ret, >, 0); +gpiod_test_join_thread_and_return_if_failed (thread); + + release_event =gpiod_chip_info_event_read (chip); +g_assert_nonnull (release_event); +gpiod_test_join_thread_and_return_if_failed (thread); + + g_assert_cmpint(gpiod_info_event_get_event_type(release_event), ==, +GPIOD_INFO_EVENT_LINE_RELEASED ); + + release_info =gpiod_info_event_get_line_info (release_event); + + g_assert_cmpuint(gpiod_line_info_get_offset(release_info), ==, 3); +g_assert_false (gpiod_line_info_is_used (release_info)); + +g_thread_join (thread); + + request_ts =gpiod_info_event_get_timestamp (request_event); + reconfigure_ts =gpiod_info_event_get_timestamp (reconfigure_event); + release_ts =gpiod_info_event_get_timestamp (release_event); +
Quoted 8 lines
tests/tests-info-event.c line 192
Quoted 3 lines
tests/tests-info-event.c new file +3
-0+ g_assert_cmpuint(request_ts, <, reconfigure_ts); + g_assert_cmpuint(reconfigure_ts, <, release_ts); +}Is multi-threading really necessary here (and elsewhere, but this the first case where the secondary thread is ALSO asserting)? Couldn't you provide the stimulous and check the result from the one thread?
I think it's a good way to test a real-life situation. I agree that they are not technically required but I'd still keep them, they don't hurt.
[snip]
Quoted 4 lines
Only a couple of minor points for this file:
How about some tests where the offsets are not ordered? And a few more with len(offsets) != 4.
Good points, will do. And will fix the rest mentioned. Thanks!
Bart
Quoted 7 lines
[snip to end]
Nothing major.
Cheers,
Kent.
Re: [libgpiod v2][PATCH v2 2/2] tests: rewrite core C tests using libgpiosim
Quoted 24 lines · Bartosz Golaszewski
Quoted 13 lines · Kent Gibson
Quoted 7 lines · Bartosz Golaszewski
This 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.
I was having trouble running the tests in a qemu instance where entropy is slow to collect. The error I get is
Quoted 9 lines
Bail out! gpiod-test-FATAL-ERROR: Unable to instantiate new GPIO device: Resource temporarily unavailable
which I backtracked to the
getrandom()call inmake_random_dir_at().I have a dislike for random elements in tests as it negatively impacts repeatability. They are only used here for chip and bank names in the configfs, and otherwise have no bearing on the tests?
Why not deterministic naming, say using the test case name, pid,...
Quoted 5 lines
We can have multiple random names in use at once. And on the off chance that something goes wrong, a random configfs dir just stays there while a deterministic one can hose other tests. Can't you just install havaged in whatever rootfs you're using?
Sure, but incorporating the PID in the names would serve a similar purpose, and also provide some indication as to what created the sim in the first place. Random is just noise. How can you tell if your configfs objects are orphaned?
Are you sure you aren't biased towards random as it also simplifies your test setup logic (no pesky names to worry about - just pass a NULL)?
My test setup is a pretty vanilla buildroot/qemu setup configured to build local kernels and libgpiod, repeated for a few different platforms. Though in this instance I was only building for x86_64.
No network interfaces, as I'm only testing GPIO, but that also means few sources of entropy. So far that hadn't been an issue.
Hadn't heard of haveged, but will give it a go.
<a few minutes later>
That works. Cool. Though I still don't like the randomness.
Quoted 11 lines
[snip]
Quoted 8 lines
tests/gpiod-test.c line 13
Quoted 4 lines
#define
MIN_KERNEL_MAJOR5tests/gpiod-test.c +1
-1-#define MIN_KERNEL_MINOR 10 +#define MIN_KERNEL_MINOR 16Might as well bump this to 17 as RCs are available and 16 isn't really sufficient. Similarly
gpiosim.c.
Quoted 3 lines
I'm building this on my laptop so prefer to have it at 16 for now. We'll have 5.17 out in three weeks, I'll bump it then.
Not sure why that prevents you from testing against 5.17-rc5, or one of the earlier RCs, but ok.
Quoted 107 lines
[snip]
Quoted 96 lines
Quoted 96 lines
tests/tests-info-event.c new file +96
-0+ +GPIOD_TEST_CASE (request_reconfigure_release_events) +{ +static const guint offset =3 ; + +g_autoptr (GPIOSimChip) sim =g_gpiosim_chip_new ("num-lines" ,8 ,NULL ); +g_autoptr (struct_gpiod_chip) chip =NULL ; +g_autoptr (struct_gpiod_line_info) info =NULL ; +g_autoptr (struct_gpiod_info_event) request_event =NULL ; +g_autoptr (struct_gpiod_info_event) reconfigure_event =NULL ; +g_autoptr (struct_gpiod_info_event) release_event =NULL ; +g_autoptr (struct_gpiod_request_config) req_cfg =NULL ; +g_autoptr (struct_gpiod_line_config) line_cfg =NULL ; +g_autoptr (GThread) thread =NULL ; +struct gpiod_line_info *request_info, *reconfigure_info, *release_info; +guint64 request_ts, reconfigure_ts, release_ts; +struct request_ctx ctx; +gint ret; + + chip =gpiod_test_open_chip_or_fail (g_gpiosim_chip_get_dev_path (sim)); + req_cfg =gpiod_test_create_request_config_or_fail (); + line_cfg =gpiod_test_create_line_config_or_fail (); + +gpiod_request_config_set_offsets (req_cfg,1 , &offset); + + info =gpiod_chip_watch_line_info (chip,3 ); +g_assert_nonnull (info); +gpiod_test_return_if_failed (); + +g_assert_false (gpiod_line_info_is_used (info)); + + ctx.chip = chip; + ctx.req_cfg = req_cfg; + ctx.line_cfg = line_cfg; + + thread =g_thread_new ("request-release" , request_release_line, &ctx); +g_thread_ref (thread); + + ret =gpiod_chip_info_event_wait (chip,1000000000 ); + g_assert_cmpint(ret, >, 0); +gpiod_test_join_thread_and_return_if_failed (thread); + + request_event =gpiod_chip_info_event_read (chip); +g_assert_nonnull (request_event); +gpiod_test_join_thread_and_return_if_failed (thread); + + g_assert_cmpint(gpiod_info_event_get_event_type(request_event), ==, +GPIOD_INFO_EVENT_LINE_REQUESTED ); + + request_info =gpiod_info_event_get_line_info (request_event); + + g_assert_cmpuint(gpiod_line_info_get_offset(request_info), ==, 3); +g_assert_true (gpiod_line_info_is_used (request_info)); + g_assert_cmpint(gpiod_line_info_get_direction(request_info), ==, +GPIOD_LINE_DIRECTION_INPUT ); + + ret =gpiod_chip_info_event_wait (chip,1000000000 ); + g_assert_cmpint(ret, >, 0); +gpiod_test_join_thread_and_return_if_failed (thread); + + reconfigure_event =gpiod_chip_info_event_read (chip); +g_assert_nonnull (reconfigure_event); +gpiod_test_join_thread_and_return_if_failed (thread); + + g_assert_cmpint(gpiod_info_event_get_event_type(reconfigure_event), ==, +GPIOD_INFO_EVENT_LINE_CONFIG_CHANGED ); + + reconfigure_info =gpiod_info_event_get_line_info (reconfigure_event); + + g_assert_cmpuint(gpiod_line_info_get_offset(reconfigure_info), ==, 3); +g_assert_true (gpiod_line_info_is_used (reconfigure_info)); + g_assert_cmpint(gpiod_line_info_get_direction(reconfigure_info), ==, +GPIOD_LINE_DIRECTION_OUTPUT ); + + ret =gpiod_chip_info_event_wait (chip,1000000000 ); + g_assert_cmpint(ret, >, 0); +gpiod_test_join_thread_and_return_if_failed (thread); + + release_event =gpiod_chip_info_event_read (chip); +g_assert_nonnull (release_event); +gpiod_test_join_thread_and_return_if_failed (thread); + + g_assert_cmpint(gpiod_info_event_get_event_type(release_event), ==, +GPIOD_INFO_EVENT_LINE_RELEASED ); + + release_info =gpiod_info_event_get_line_info (release_event); + + g_assert_cmpuint(gpiod_line_info_get_offset(release_info), ==, 3); +g_assert_false (gpiod_line_info_is_used (release_info)); + +g_thread_join (thread); + + request_ts =gpiod_info_event_get_timestamp (request_event); + reconfigure_ts =gpiod_info_event_get_timestamp (reconfigure_event); + release_ts =gpiod_info_event_get_timestamp (release_event); +Quoted 8 lines
tests/tests-info-event.c line 192
Quoted 3 lines
tests/tests-info-event.c new file +3
-0+ g_assert_cmpuint(request_ts, <, reconfigure_ts); + g_assert_cmpuint(reconfigure_ts, <, release_ts); +}Is multi-threading really necessary here (and elsewhere, but this the first case where the secondary thread is ALSO asserting)? Couldn't you provide the stimulous and check the result from the one thread?
Quoted 4 lines
I think it's a good way to test a real-life situation. I agree that they are not technically required but I'd still keep them, they don't hurt.
Sure, and that is why I didn't have a problem with the earlier instances. But here you have both threads asserting, so splitting the test logic. That crosses a line for me. Dumb helper threads I'm ok with.
Cheers,
Kent.