Skip to contentSkip to conversation

libgpiod v2: rewrite tests for the C library

PATCH Superseded Bartosz Golaszewski 2 participants 8 messages Mar 3, 2022 – Mar 5, 2022

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 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

v2 -> v3:
- add a patch adding an enum for line values
- rework the mechanism for retrieving overrides
- add new "constructors" for line_config
- incorporate part of Kent's points in the documentation
- a bunch of improvements to tests as per Kent's reviews

Bartosz Golaszewski (3):
API: add an enum for line values
line-config: expose the override logic to users
tests: rewrite core C tests using libgpiosim

 configure.ac                 |    8 +-
 include/gpiod.h              |  603 +++++++++++++------
 lib/line-config.c            |  871 ++++++++++++++++++---------
 tests/Makefile.am            |   24 +-
 tests/gpiod-test-helpers.c   |   49 ++
 tests/gpiod-test-helpers.h   |  139 +++++
 tests/gpiod-test-sim.c       |  308 ++++++++++
 tests/gpiod-test-sim.h       |   42 ++
 tests/gpiod-test.c           |  233 +-------
 tests/gpiod-test.h           |   83 +--
 tests/gpiosim/gpiosim.c      |    1 +
 tests/mockup/Makefile.am     |   11 -
 tests/mockup/gpio-mockup.c   |  496 ----------------
 tests/mockup/gpio-mockup.h   |   36 --
 tests/tests-chip.c           |  282 ++++-----
 tests/tests-edge-event.c     |  490 +++++++++++++++
 tests/tests-event.c          |  908 ----------------------------
 tests/tests-info-event.c     |  301 ++++++++++
 tests/tests-line-config.c    |  503 ++++++++++++++++
 tests/tests-line-info.c      |  318 ++++++++++
 tests/tests-line-request.c   |  526 ++++++++++++++++
 tests/tests-line.c           | 1091 ----------------------------------
 tests/tests-misc.c           |   80 ++-
 tests/tests-request-config.c |   90 +++
 tools/gpioget.c              |    6 +-
 tools/gpiomon.c              |    6 +-
 tools/gpioset.c              |    9 +-
 27 files changed, 4017 insertions(+), 3497 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
#1 lore raw

PATCH v3 1/3 API: add an enum for line values

In order to explicitly stress that line values as understood by libgpiod are logical values, expose a two-value enum with values called: ACTIVE and INACTIVE that should be used whenever referring to the state of GPIO lines.

The value of INACTIVE is set to 0 while that of ACTIVE to 1 so that users can still use integers in C (where no scoped enums exist).

---
 include/gpiod.h | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
include/gpiod.h +10 -2
index 074e395..34fdad6 100644
@@ -185,12 +185,20 @@ gpiod_chip_request_lines(struct gpiod_chip *chip,
 /**
  * @}
  *
- * @defgroup line_settings Line settings
+ * @defgroup line_settings Line definitions
  * @{
  *
- * These defines are used both by gpiod_line_info and gpiod_line_config.
+ * These defines are used across the API.
  */
 
+/**
+ * @brief Logical line state.
+ */
+enum {
+	GPIOD_LINE_VALUE_INACTIVE = 0,
+	GPIOD_LINE_VALUE_ACTIVE = 1,
+};
+
 /**
  * @brief Direction settings.
  */
Signature · 1 line
2.30.1
Reviewed-by on v3 1/3
#2 lore raw

Re: [libgpiod v2][PATCH v3 1/3] API: add an enum for line values

Quoted 9 lines · Bartosz Golaszewski

In order to explicitly stress that line values as understood by libgpiod are logical values, expose a two-value enum with values called: ACTIVE and INACTIVE that should be used whenever referring to the state of GPIO lines.

The value of INACTIVE is set to 0 while that of ACTIVE to 1 so that users can still use integers in C (where no scoped enums exist).

Works for me.

Quoted 34 lines
---
 include/gpiod.h | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
include/gpiod.h +10 -2
index 074e395..34fdad6 100644
@@ -185,12 +185,20 @@ gpiod_chip_request_lines(struct gpiod_chip *chip,
 /**
  * @}
  *
- * @defgroup line_settings Line settings
+ * @defgroup line_settings Line definitions
  * @{
  *
- * These defines are used both by gpiod_line_info and gpiod_line_config.
+ * These defines are used across the API.
  */
 
+/**
+ * @brief Logical line state.
+ */
+enum {
+	GPIOD_LINE_VALUE_INACTIVE = 0,
+	GPIOD_LINE_VALUE_ACTIVE = 1,
+};
+
 /**
  * @brief Direction settings.
  */
Signature · 1 line
2.30.1
#3 lore raw

PATCH v3 2/3 line-config: expose the override logic to users

In reply to 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.

We also complete the API by providing functions that allow to retrieve the overridden offsets and their corresponding property types.

This way the caller can fully inspect the line_config and high-level language bindings can provide stringification methods.

While at it: we fix a couple bugs in the implementation of struct line_config and add new constructors that take a variable list of arguments.

---
 include/gpiod.h   | 591 +++++++++++++++++++++----------
 lib/line-config.c | 871 +++++++++++++++++++++++++++++++---------------
 tools/gpioget.c   |   6 +-
 tools/gpiomon.c   |   6 +-
 tools/gpioset.c   |   9 +-
 5 files changed, 1012 insertions(+), 471 deletions(-)
include/gpiod.h +411 -180
index 34fdad6..dc50634 100644
@@ -9,6 +9,7 @@
 #ifndef __LIBGPIOD_GPIOD_H__
 #define __LIBGPIOD_GPIOD_H__
 
+#include <stdarg.h>
 #include <stdbool.h>
 #include <stdint.h>
 
@@ -176,6 +177,8 @@ int gpiod_chip_find_line(struct gpiod_chip *chip, const char *name);
  * @param line_cfg Line config object.
  * @return New line request object or NULL if an error occurred. The request
  *         must be released by the caller using ::gpiod_line_request_release.
+ * @note Line configuration overrides set for offsets that don't end up being
+ *       requested are silently ignored.
  */
 struct gpiod_line_request *
 gpiod_chip_request_lines(struct gpiod_chip *chip,
@@ -458,13 +461,65 @@ gpiod_info_event_get_line_info(struct gpiod_info_event *event);
  *
  * The line-config object stores the configuration for lines that can be used
  * in two cases: when making a line request and when reconfiguring a set of
- * already requested lines. The mutators for the line request don't return
- * errors. If the set of options is too complex to be translated into kernel
- * uAPI structures - the error will be returned at the time of the request or
- * reconfiguration. If an invalid value was passed to any of the getters - the
- * default value will be silently used instead. Each option can be set
- * globally, for a single line offset or for multiple line offsets.
+ * already requested lines.
+ *
+ * A new line-config object is instantiated containing a set of sane defaults
+ * for all supported configuration settings. Those defaults can be modified by
+ * the caller. Default values can be overridden by applying different values
+ * for specific line offsets. When making a request or reconfiguring an
+ * existing one, the overridden settings for specific offsets will be taken
+ * into account first and for every other offset and setting the defaults will
+ * be used.
+ *
+ * For every setting there are two mutators (one setting the default and one
+ * for the per-offset override), two getters (one for reading the global
+ * default and one for retrieving the effective value for the line at given
+ * offset), a function for testing if a setting is overridden for the line at
+ * given offset and finally a function for clearing the overrides (per offset).
+ *
+ * The mutators don't return errors. If the set of options is too complex to
+ * be translated into kernel uAPI structures - the error will be returned at
+ * the time of the request or reconfiguration. If an invalid value was passed
+ * to any of the mutators - the default value will be silently used instead.
+ *
+ * Operating on offsets in struct line_config has no effect on real GPIOs. It
+ * only manipulates the object in memory and is only applied to the hardware
+ * at the time of the request or reconfiguration.
+ *
+ * Overrides set for offsets that don't end up being requested are silently
+ * ignored both in ::gpiod_chip_request_lines as well as in
+ * ::gpiod_line_request_reconfigure_lines.
+ *
+ * In cases where all requested lines are using the global defaults, the
+ * offsets can be entirely ignored when preparing the line configuration.
+ */
+
+/**
+ * @brief List of properties that can be stored in a line_config object.
+ *
+ * Used when setting the defaults with ::gpiod_line_config_new_defaults and
+ * when retrieving the overrides.
  */
+enum {
+	GPIOD_LINE_CONFIG_PROP_END = 0,
+	/**< Delimiter. */
+	GPIOD_LINE_CONFIG_PROP_DIRECTION,
+	/**< Line direction. */
+	GPIOD_LINE_CONFIG_PROP_EDGE,
+	/**< Edge detection. */
+	GPIOD_LINE_CONFIG_PROP_BIAS,
+	/**< Bias. */
+	GPIOD_LINE_CONFIG_PROP_DRIVE,
+	/**< Drive. */
+	GPIOD_LINE_CONFIG_PROP_ACTIVE_LOW,
+	/**< Active-low setting. */
+	GPIOD_LINE_CONFIG_PROP_DEBOUNCE_PERIOD,
+	/** Debounce period. */
+	GPIOD_LINE_CONFIG_PROP_EVENT_CLOCK,
+	/**< Event clock type. */
+	GPIOD_LINE_CONFIG_PROP_OUTPUT_VALUE,
+	/**< Output value. */
+};
 
 /**
  * @brief Create a new line config object.
@@ -472,6 +527,26 @@ gpiod_info_event_get_line_info(struct gpiod_info_event *event);
  */
 struct gpiod_line_config *gpiod_line_config_new(void);
 
+/**
+ * @brief Create a new line config object with a set of default properties.
+ * @param first First property type.
+ * @return New line config object or NULL on error.
+ *
+ * The list of arguments consists of a number of type->value pairs and must
+ * end with ::GPIOD_LINE_CONFIG_PROP_END.
+ */
+struct gpiod_line_config *gpiod_line_config_new_defaults(int first, ...);
+
+/**
+ * @brief Variant of ::gpiod_line_config_new_defaults taking va_list as
+ *        argument.
+ * @param first First property type.
+ * @param va List of variadic arguments.
+ * @return New line config object or NULL on error.
+ */
+struct gpiod_line_config *
+gpiod_line_config_new_defaults_va(int first, va_list va);
+
 /**
  * @brief Free the line config object and release all associated resources.
  * @param config Line config object to free.
@@ -488,329 +563,428 @@ void gpiod_line_config_free(struct gpiod_line_config *config);
 void gpiod_line_config_reset(struct gpiod_line_config *config);
 
 /**
- * @brief Set the direction of all lines.
+ * @brief Set the default direction setting.
  * @param config Line config object.
  * @param direction New direction.
  */
-void gpiod_line_config_set_direction(struct gpiod_line_config *config,
-				     int direction);
+void gpiod_line_config_set_direction_default(struct gpiod_line_config *config,
+					     int direction);
 
 /**
- * @brief Set the direction for a single line at given offset.
+ * @brief Set the direction override at given offset.
  * @param config Line config object.
  * @param direction New direction.
- * @param offset Offset of the line for which to set the direction.
+ * @param offset Offset of the line for which to set the override.
  */
-void gpiod_line_config_set_direction_offset(struct gpiod_line_config *config,
-					    int direction, unsigned int offset);
+void gpiod_line_config_set_direction_override(struct gpiod_line_config *config,
+					      int direction,
+					      unsigned int offset);
 
 /**
- * @brief Set the direction for a subset of lines.
+ * @brief Clear the direction override at given offset.
  * @param config Line config object.
- * @param direction New direction.
- * @param num_offsets Number of offsets in the array.
- * @param offsets Array of line offsets for which to set the direction.
+ * @param offset Offset of the line for which to clear the override.
+ * @note Does nothing if no override is set for this line.
+ */
+void
+gpiod_line_config_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.
  */
-void gpiod_line_config_set_direction_subset(struct gpiod_line_config *config,
-					    int direction,
-					    unsigned int num_offsets,
-					    const unsigned int *offsets);
+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.
- * @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.
+ * @return Direction setting that would have been used for any non-overridden
+ *         offset.
  */
-int gpiod_line_config_get_direction(struct gpiod_line_config *config,
-				    unsigned int offset);
+int gpiod_line_config_get_direction_default(struct gpiod_line_config *config);
 
 /**
- * @brief Set the edge event detection for all lines.
+ * @brief Get the direction setting for the line 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 read the direction setting.
+ * @return Direction setting that would have been used for the line at given
+ *         offset if the config object was used in a request at the time of the
+ *         call.
  */
-void gpiod_line_config_set_edge_detection(struct gpiod_line_config *config,
-					  int edge);
+int gpiod_line_config_get_direction_offset(struct gpiod_line_config *config,
+					   unsigned int offset);
 
 /**
- * @brief Set the edge event detection for a single line at given offset.
+ * @brief Set the default edge event detection.
  * @param config Line config object.
  * @param edge Type of edge events to detect.
- * @param offset Offset of the line for which to set the edge detection.
  */
 void
-gpiod_line_config_set_edge_detection_offset(struct gpiod_line_config *config,
-					    int edge, unsigned int offset);
+gpiod_line_config_set_edge_detection_default(struct gpiod_line_config *config,
+					     int edge);
 
 /**
- * @brief Set the edge event detection for a subset of lines.
+ * @brief Set the edge detection override at given offset.
  * @param config Line config object.
  * @param edge Type of edge events to detect.
- * @param num_offsets Number of offsets in the array.
- * @param offsets Array of line offsets for which to set the edge detection.
+ * @param offset Offset of the line for which to set the override.
  */
 void
-gpiod_line_config_set_edge_detection_subset(struct gpiod_line_config *config,
-					    int edge, unsigned int num_offsets,
-					    const unsigned int *offsets);
+gpiod_line_config_set_edge_detection_override(struct gpiod_line_config *config,
+					      int edge, unsigned int offset);
 
 /**
- * @brief Get the edge event detection setting for a given line.
+ * @brief Clear the edge detection override at given offset.
  * @param config Line config object.
- * @param offset Line offset for which to read the edge event detection setting.
+ * @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_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 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 Offset of the line for which to read the edge event detection
+ *               setting.
  * @return Edge event detection setting that would have been used for given
  *         offset if the config object was used in a request at the time of
  *         the call.
- * @note If an offset is used for which no config was provided, the function
- *       will return the global default value.
  */
-int gpiod_line_config_get_edge_detection(struct gpiod_line_config *config,
-					 unsigned int offset);
+int
+gpiod_line_config_get_edge_detection_offset(struct gpiod_line_config *config,
+					    unsigned int offset);
 
 /**
- * @brief Set the bias of all lines.
+ * @brief Set the default bias setting.
  * @param config Line config object.
  * @param bias New bias.
  */
-void gpiod_line_config_set_bias(struct gpiod_line_config *config, int bias);
+void gpiod_line_config_set_bias_default(struct gpiod_line_config *config,
+					int bias);
 
 /**
- * @brief Set the bias for a single line at given offset.
+ * @brief Set the bias override at given offset.
  * @param config Line config object.
- * @param bias New bias.
- * @param offset Offset of the line for which to set the bias.
+ * @param bias New bias setting.
+ * @param offset Offset of the line for which to set the override.
  */
-void gpiod_line_config_set_bias_offset(struct gpiod_line_config *config,
-				       int bias, unsigned int offset);
+void gpiod_line_config_set_bias_override(struct gpiod_line_config *config,
+					 int bias, unsigned int offset);
 
 /**
- * @brief Set the bias for a subset of lines.
+ * @brief Clear the bias override at given offset.
  * @param config Line config object.
- * @param bias New bias.
- * @param num_offsets Number of offsets in the array.
- * @param offsets Array of line offsets for which to set the bias.
+ * @param offset Offset of the line for which to clear the override.
+ * @note Does nothing if no override is set for this line.
  */
-void gpiod_line_config_set_bias_subset(struct gpiod_line_config *config,
-				       int bias, unsigned int num_offsets,
-				       const unsigned int *offsets);
+void gpiod_line_config_clear_bias_override(struct gpiod_line_config *config,
+					   unsigned int offset);
 
 /**
- * @brief Get the bias setting for a given line.
+ * @brief Check if the bias setting is overridden at given offset.
  * @param config Line config object.
- * @param offset Line offset for which to read the bias setting.
- * @return Bias setting that would have been used for given offset if the
- *         config object was used in a request at the time of the call.
- * @note If an offset is used for which no config was provided, the function
- *       will return the global default value.
+ * @param offset Offset of the line for which to check the override.
+ * @return True if bias is overridden at this offset, false otherwise.
  */
-int gpiod_line_config_get_bias(struct gpiod_line_config *config,
+bool gpiod_line_config_bias_is_overridden(struct gpiod_line_config *config,
+					  unsigned int offset);
+/**
+ * @brief Get the default bias setting.
+ * @param config Line config object.
+ * @return Bias setting that would have been used for any offset not assigned
+ *         its own direction value.
+ */
+int gpiod_line_config_get_bias_default(struct gpiod_line_config *config);
+
+/**
+ * @brief Get the bias setting for a given offset.
+ * @param config Line config object.
+ * @param offset Offset of the line for which to read the bias setting.
+ * @return Bias setting that would have been used for the line at given offset
+ *         if the config object was used in a request at the time of the call.
+ */
+int gpiod_line_config_get_bias_offset(struct gpiod_line_config *config,
 			       unsigned int offset);
 
 /**
- * @brief Set the drive of all lines.
+ * @brief Set the default drive setting.
  * @param config Line config object.
  * @param drive New drive.
  */
-void gpiod_line_config_set_drive(struct gpiod_line_config *config, int drive);
+void gpiod_line_config_set_drive_default(struct gpiod_line_config *config,
+					 int drive);
 
 /**
- * @brief Set the drive for a single line at given offset.
+ * @brief Set the drive override at given offset.
  * @param config Line config object.
- * @param drive New drive.
- * @param offset Offset of the line for which to set the drive.
+ * @param drive New drive setting.
+ * @param offset Offset of the line for which to set the override.
  */
-void gpiod_line_config_set_drive_offset(struct gpiod_line_config *config,
-					int drive, unsigned int offset);
+void gpiod_line_config_set_drive_override(struct gpiod_line_config *config,
+					  int drive, unsigned int offset);
 
 /**
- * @brief Set the drive for a subset of lines.
+ * @brief Clear the drive override at given offset.
  * @param config Line config object.
- * @param drive New drive.
- * @param num_offsets Number of offsets in the array.
- * @param offsets Array of line offsets for which to set the drive.
+ * @param offset Offset of the line for which to clear the override.
+ * @note Does nothing if no override is set for this line.
  */
-void gpiod_line_config_set_drive_subset(struct gpiod_line_config *config,
-					int drive, unsigned int num_offsets,
-					const unsigned int *offsets);
+void gpiod_line_config_clear_drive_override(struct gpiod_line_config *config,
+					    unsigned int offset);
 
 /**
- * @brief Get the drive setting for a given line.
+ * @brief Check if the drive setting is overridden at given offset.
  * @param config Line config object.
- * @param offset Line offset for which to read the drive setting.
- * @return Drive setting that would have been used for given offset if the
- *         config object was used in a request at the time of the call.
- * @note If an offset is used for which no config was provided, the function
- *       will return the global default value.
+ * @param offset Offset of the line for which to check the override.
+ * @return True if drive is overridden at this offset, false otherwise.
  */
-int gpiod_line_config_get_drive(struct gpiod_line_config *config,
-				unsigned int offset);
+bool gpiod_line_config_drive_is_overridden(struct gpiod_line_config *config,
+					   unsigned int offset);
 
 /**
- * @brief Set all lines as active-low.
+ * @brief Get the default drive setting.
  * @param config Line config object.
+ * @return Drive setting that would have been used for any offset not assigned
+ *         its own direction value.
  */
-void gpiod_line_config_set_active_low(struct gpiod_line_config *config);
+int gpiod_line_config_get_drive_default(struct gpiod_line_config *config);
 
 /**
- * @brief Set a single line as active-low.
+ * @brief Get the drive setting for a given offset.
  * @param config Line config object.
- * @param offset Offset of the line for which to set the active setting.
+ * @param offset Offset of the line for which to read the drive setting.
+ * @return Drive setting that would have been used for the line at given offset
+ *         if the config object was used in a request at the time of the call.
  */
-void gpiod_line_config_set_active_low_offset(struct gpiod_line_config *config,
-					     unsigned int offset);
+int gpiod_line_config_get_drive_offset(struct gpiod_line_config *config,
+				       unsigned int offset);
 
 /**
- * @brief Set a subset of lines as active-low.
+ * @brief Set lines to active-low by default.
  * @param config Line config object.
- * @param num_offsets Number of offsets in the array.
- * @param offsets Array of line offsets for which to set the active setting.
+ * @param active_low New active-low setting.
  */
-void gpiod_line_config_set_active_low_subset(struct gpiod_line_config *config,
-					     unsigned int num_offsets,
-					     const unsigned int *offsets);
+void gpiod_line_config_set_active_low_default(struct gpiod_line_config *config,
+					      bool active_low);
 
 /**
- * @brief Check if the line at given offset was configured as active-low.
+ * @brief Override the active-low setting at given offset.
  * @param config Line config object.
- * @param offset Line offset for which to read the active-low setting.
- * @return Active-low setting that would have been used for given offset if the
- *         config object was used in a request at the time of the call.
- * @note If an offset is used for which no config was provided, the function
- *       will return the global default value.
+ * @param active_low New active-low setting.
+ * @param offset Offset of the line for which to set the override.
  */
-bool gpiod_line_config_get_active_low(struct gpiod_line_config *config,
-				      unsigned int offset);
+void gpiod_line_config_set_active_low_override(struct gpiod_line_config *config,
+					       bool active_low,
+					       unsigned int offset);
 
 /**
- * @brief Set all lines as active-high.
+ * @brief Clear the active-low override at given offset.
  * @param config Line config object.
+ * @param offset Offset of the line for which to clear the override.
+ * @note Does nothing if no override is set for this line.
  */
-void gpiod_line_config_set_active_high(struct gpiod_line_config *config);
+void
+gpiod_line_config_clear_active_low_override(struct gpiod_line_config *config,
+					    unsigned int offset);
 
 /**
- * @brief Set a single line as active-high.
+ * @brief Check if the active-low setting is overridden at given offset.
  * @param config Line config object.
- * @param offset Offset of the line for which to set the active setting.
+ * @param offset Offset of the line for which to check the override.
+ * @return True if active-low is overridden at this offset, false otherwise.
  */
-void gpiod_line_config_set_active_high_offset(struct gpiod_line_config *config,
-					      unsigned int offset);
+bool
+gpiod_line_config_active_low_is_overridden(struct gpiod_line_config *config,
+					   unsigned int offset);
 
 /**
- * @brief Set a subset of lines as active-high.
+ * @brief Check if active-low is the default setting.
  * @param config Line config object.
- * @param num_offsets Number of offsets in the array.
- * @param offsets Array of line offsets for which to set the active setting.
+ * @return Active-low setting that would have been used for any offset not
+ *         assigned its own value.
  */
-void gpiod_line_config_set_active_high_subset(struct gpiod_line_config *config,
-					      unsigned int num_offsets,
-					      const unsigned int *offsets);
+bool gpiod_line_config_get_active_low_default(struct gpiod_line_config *config);
 
 /**
- * @brief Set the debounce period for all lines.
+ * @brief Check if the line at given offset was configured as active-low.
  * @param config Line config object.
- * @param period New debounce period. Disables debouncing if 0.
+ * @param offset Offset of the line for which to read the active-low setting.
+ * @return Active-low setting that would have been used for the line at given
+ *         offset if the config object was used in a request at the time of the
+ *         call.
  */
-void gpiod_line_config_set_debounce_period_us(struct gpiod_line_config *config,
-					      unsigned long period);
+bool gpiod_line_config_get_active_low_offset(struct gpiod_line_config *config,
+					     unsigned int offset);
 
 /**
- * @brief Set the debounce period for a single line at given offset.
+ * @brief Set the default debounce period.
  * @param config Line config object.
  * @param period New debounce period. Disables debouncing if 0.
- * @param offset Offset of the line for which to set the debounce period.
+ */
+void gpiod_line_config_set_debounce_period_us_default(
+		struct gpiod_line_config *config, unsigned long period);
+
+/**
+ * @brief Override the debounce period setting at given offset.
+ * @param config Line config object.
+ * @param period New debounce period in microseconds.
+ * @param offset Offset of the line for which to set the override.
  */
 void
-gpiod_line_config_set_debounce_period_us_offset(
+gpiod_line_config_set_debounce_period_us_override(
 					struct gpiod_line_config *config,
 					unsigned long period,
 					unsigned int offset);
 
 /**
- * @brief Set the debounce period for a subset of lines.
+ * @brief Clear the debounce period override at given offset.
  * @param config Line config object.
- * @param period New debounce period. Disables debouncing if 0.
- * @param num_offsets Number of offsets in the array.
- * @param offsets Array of line offsets for which to set the debounce period.
+ * @param offset Offset of the line for which to clear the override.
+ * @note Does nothing if no override is set for this line.
  */
-void
-gpiod_line_config_set_debounce_period_us_subset(
+void gpiod_line_config_clear_debounce_period_us_override(
 					struct gpiod_line_config *config,
-					unsigned long period,
-					unsigned int num_offsets,
-					const unsigned int *offsets);
+					unsigned int offset);
+
+/**
+ * @brief Check if the debounce period setting is overridden at given offset.
+ * @param config Line config object.
+ * @param offset Offset of the line for which to check the override.
+ * @return True if debounce period is overridden at this offset, false
+ *         otherwise.
+ */
+bool gpiod_line_config_debounce_period_us_is_overridden(
+					struct gpiod_line_config *config,
+					unsigned int offset);
+
+/**
+ * @brief Get the default debounce period.
+ * @param config Line config object.
+ * @return Debounce period that would have been used for any offset not
+ *         assigned its own debounce period. 0 if not debouncing is disabled.
+ */
+unsigned long gpiod_line_config_get_debounce_period_us_default(
+					struct gpiod_line_config *config);
 
 /**
- * @brief Get the debounce period for a given line.
+ * @brief Get the debounce period for a given offset.
  * @param config Line config object.
- * @param offset Line offset for which to read the debounce period.
- * @return Debounce period that would have been used for given offset if the
- *         config object was used in a request at the time of the call.
- * @note If an offset is used for which no config was provided, the function
- *       will return the global default value.
+ * @param offset Offset of the line for which to read the debounce period.
+ * @return Debounce period that would have been used for the line at given
+ *         offset if the config object was used in a request at the time of
+ *         the call. 0 if debouncing is disabled.
  */
 unsigned long
-gpiod_line_config_get_debounce_us_period(struct gpiod_line_config *config,
-					 unsigned int offset);
+gpiod_line_config_get_debounce_period_us_offset(
+			struct gpiod_line_config *config, unsigned int offset);
 
 /**
- * @brief Set the event timestamp clock for all lines.
+ * @brief Set the default event timestamp clock.
  * @param config Line config object.
  * @param clock New clock to use.
  */
-void gpiod_line_config_set_event_clock(struct gpiod_line_config *config,
-				       int clock);
+void gpiod_line_config_set_event_clock_default(struct gpiod_line_config *config,
+					       int clock);
 
 /**
- * @brief Set the event clock for a single line at given offset.
+ * @brief Override the event clock setting at given offset.
  * @param config Line config object.
  * @param clock New event clock to use.
- * @param offset Offset of the line for which to set the event clock type.
+ * @param offset Offset of the line for which to set the override.
  */
-void gpiod_line_config_set_event_clock_offset(struct gpiod_line_config *config,
-					      int clock, unsigned int offset);
+void
+gpiod_line_config_set_event_clock_override(struct gpiod_line_config *config,
+					   int clock, unsigned int offset);
 
 /**
- * @brief Set the event clock for a subset of lines.
+ * @brief Clear the event clock override at given offset.
  * @param config Line config object.
- * @param clock New event clock to use.
- * @param num_offsets Number of offsets in the array.
- * @param offsets Array of line offsets for which to set the event clock type.
+ * @param offset Offset of the line for which to clear the override.
+ * @note Does nothing if no override is set for this line.
+ */
+void
+gpiod_line_config_clear_event_clock_override(struct gpiod_line_config *config,
+					     unsigned int offset);
+
+/**
+ * @brief Check if the event clock setting is overridden at given offset.
+ * @param config Line config object.
+ * @param offset Offset of the line for which to check the override.
+ * @return True if event clock period is overridden at this offset, false
+ *         otherwise.
+ */
+bool
+gpiod_line_config_event_clock_is_overridden(struct gpiod_line_config *config,
+					    unsigned int offset);
+
+/**
+ * @brief Get the default event clock setting.
+ * @param config Line config object.
+ * @return Event clock setting that would have been used for any offset not
+ *         assigned its own direction value.
  */
-void gpiod_line_config_set_event_clock_subset(struct gpiod_line_config *config,
-					      int clock,
-					      unsigned int num_offsets,
-					      const unsigned int *offsets);
+int gpiod_line_config_get_event_clock_default(struct gpiod_line_config *config);
 
 /**
- * @brief Get the event clock setting for a given line.
+ * @brief Get the event clock setting for a given offset.
  * @param config Line config object.
- * @param offset Line offset for which to read the event clock setting.
- * @return Event clock setting that would have been used for given offset if
- *         the config object was used in a request at the time of the call.
- * @note If an offset is used for which no config was provided, the function
- *       will return the global default value.
+ * @param offset Offset of the line for which to read the event clock setting.
+ * @return Event clock setting that would have been used for the line at given
+ *         offset if the config object was used in a request at the time of the
+ *         call.
  */
-int gpiod_line_config_get_event_clock(struct gpiod_line_config *config,
-				      unsigned int offset);
+int gpiod_line_config_get_event_clock_offset(struct gpiod_line_config *config,
+					     unsigned int offset);
 
 /**
- * @brief Set the output value for a single offset.
+ * @brief Set the default output value.
+ * @param config Line config object.
+ * @param value New value.
+ */
+void
+gpiod_line_config_set_output_value_default(struct gpiod_line_config *config,
+					   int value);
+
+/**
+ * @brief Override the output value for a single offset.
  * @param config Line config object.
  * @param offset Offset of the line.
  * @param value Output value to set.
  */
-void gpiod_line_config_set_output_value(struct gpiod_line_config *config,
-					unsigned int offset, int value);
+void
+gpiod_line_config_set_output_value_override(struct gpiod_line_config *config,
+					    unsigned int offset, int value);
 
 /**
- * @brief Set the output values for a set of offsets.
+ * @brief Override the output values for multiple offsets.
  * @param config Line config object.
- * @param num_values Number of offsets for which to set values.
- * @param offsets Array of line offsets to set values for.
+ * @param num_values Number of offsets for which to override values.
+ * @param offsets Array of line offsets to override values for.
  * @param values Array of output values associated with the offsets passed in
  *               the previous argument.
  */
@@ -819,14 +993,69 @@ void gpiod_line_config_set_output_values(struct gpiod_line_config *config,
 					 const unsigned int *offsets,
 					 const int *values);
 
+/**
+ * @brief Clear the output value override at given offset.
+ * @param config Line config object.
+ * @param offset Offset of the line for which to clear the override.
+ * @note Does nothing if no override is set for this line.
+ */
+void
+gpiod_line_config_clear_output_value_override(struct gpiod_line_config *config,
+					      unsigned int offset);
+
+/**
+ * @brief Check if the output value is overridden at given offset.
+ * @param config Line config object.
+ * @param offset Offset of the line for which to check the override.
+ * @return True if output value period is overridden at this offset, false
+ *         otherwise.
+ */
+bool
+gpiod_line_config_output_value_is_overridden(struct gpiod_line_config *config,
+					     unsigned int offset);
+
+/**
+ * @brief Get the default output value.
+ * @param config Line config object.
+ * @return Output value that would have been used for any offset not
+ *         assigned its own output value.
+ */
+int
+gpiod_line_config_get_output_value_default(struct gpiod_line_config *config);
+
 /**
  * @brief Get the output value configured for a given line.
  * @param config Line config object.
  * @param offset Line offset for which to read the value.
- * @return 1 or 0 if the value was configured for this line, -1 otherwise.
+ * @return Output value that would have been used for the line at given offset
+ *         if the config object was used in a request at the time of the call.
  */
-int gpiod_line_config_get_output_value(struct gpiod_line_config *config,
-				       unsigned int offset);
+int gpiod_line_config_get_output_value_offset(struct gpiod_line_config *config,
+					      unsigned int offset);
+
+/**
+ * @brief Get the total number of overridden settings currently stored by this
+ *        line config object.
+ * @param config Line config object.
+ * @return Number of individual overridden settings.
+ */
+unsigned int
+gpiod_line_config_get_num_overrides(struct gpiod_line_config *config);
+
+/**
+ * @brief Get the list of overridden offsets and the corresponding types of
+ *        overridden settings.
+ * @param config Line config object.
+ * @param offsets Array to store the overidden offsets. Must hold at least the
+ *                number of unsigned integers returned by
+ *                ::gpiod_line_config_get_output_value_offset.
+ * @param props Array to store the types of overridden settings. Must hold at
+ *              least the number of integers returned by
+ *              gpiod_line_config_get_output_value_offset.
+ */
+void
+gpiod_line_config_get_overrides(struct gpiod_line_config *config,
+				unsigned int *offsets, int *props);
 
 /**
  * @}
@@ -1028,6 +1257,8 @@ int gpiod_line_request_set_values(struct gpiod_line_request *request,
  * @param request GPIO line request.
  * @param config New line config to apply.
  * @return 0 on success, -1 on failure.
+ * @note Line configuration overrides set for offsets that don't end up being
+ *       requested are silently ignored.
  */
 int gpiod_line_request_reconfigure_lines(struct gpiod_line_request *request,
 					 struct gpiod_line_config *config);
lib/line-config.c +590 -281

1,227 lines not on this page · raw message

tools/gpioget.c +3 -3
index 965af3b..112257c 100644
@@ -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
index c09f2f5..31ea294 100644
@@ -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
index 55fcfe9..c27525a 100644
@@ -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
#4 lore raw

Re: [libgpiod v2][PATCH v3 2/3] line-config: expose the override logic to users

Quoted 16 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.

We also complete the API by providing functions that allow to retrieve the overridden offsets and their corresponding property types.

Quoted 7 lines

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 and add new constructors that take a variable list of arguments.

The variadic constructor is new for patch v3.
It bundles default constructor + default mutators, so doesn't add functionality that wasn't already available - it just makes it accessible via a single function call.
Is the variadic form beneficial for bindings, say Python, where you would prefer not to be making a bunch of C calls?
Or is this just sugar?

No major objection, just curious about the rationale for adding it.

[snip]

Quoted 10 lines
include/gpiod.h +10 -0
+/**
+ * @brief Get the total number of overridden settings currently stored by this
+ *        line config object.
+ * @param config Line config object.
+ * @return Number of individual overridden settings.
+ */
+unsigned int
+gpiod_line_config_get_num_overrides(struct gpiod_line_config *config);
+
+/**

include/gpiod.h void gpiod_line_config_set_output_values(struct gpiod_line_config *config, · line 1055

Quoted 10 lines
include/gpiod.h +10 -0
+ * @brief Get the list of overridden offsets and the corresponding types of
+ *        overridden settings.
+ * @param config Line config object.
+ * @param offsets Array to store the overidden offsets. Must hold at least the
+ *                number of unsigned integers returned by
+ *                ::gpiod_line_config_get_output_value_offset.
+ * @param props Array to store the types of overridden settings. Must hold at
+ *              least the number of integers returned by
+ *              gpiod_line_config_get_output_value_offset.
+ */

The purpose of the offsets and props arrays is not clear. Clarify that you are returning a list of (offset,prop), split across the two arrays.
Replace them with a single array of (offset,prop) unless there is a good reason to keep them separate?

Guessing it should be gpiod_line_config_get_num_overrides(), not gpiod_line_config_get_output_value_offset() which returns 0 or 1, or even better -1 for inputs?

[snip]

Quoted 12 lines
lib/line-config.c +10 -1
+GPIOD_API unsigned int
+gpiod_line_config_get_num_overrides(struct gpiod_line_config *config)
+{
+	struct override_config *override;
+	unsigned int i, j, count = 0;
 
-	errno = ENXIO;
+	for (i = 0; i < GPIO_V2_LINES_MAX; i++) {
+		override = &config->overrides[i];
+
+		if (override_used(override)) {
+			for (j = 0; j < NUM_OVERRIDE_FLAGS; j++) {

lib/line-config.c gpiod_line_config_set_output_values(struct gpiod_line_config *config, · line 834

Quoted 10 lines
lib/line-config.c +10 -0
+				if (override->override_flags &
+				    override_flag_list[j])
+					count++;
+			}
+		}
+	}
+
+	return count;
+}
+

Using GPIO_V2_LINES_MAX for the size of the overrides array is confusing, and the two should be de-coupled so you can more easily resize the array if necessary.
Provide a NUM_OVERRIDES_MAX, or similar, and use that when referring to the size of the overrides array.

Quoted 39 lines
lib/line-config.c +36 -0
+static int override_flag_to_prop(int flag)
+{
+	switch (flag) {
+	case OVERRIDE_FLAG_DIRECTION:
+		return GPIOD_LINE_CONFIG_PROP_DIRECTION;
+	case OVERRIDE_FLAG_EDGE:
+		return GPIOD_LINE_CONFIG_PROP_EDGE;
+	case OVERRIDE_FLAG_BIAS:
+		return GPIOD_LINE_CONFIG_PROP_BIAS;
+	case OVERRIDE_FLAG_DRIVE:
+		return GPIOD_LINE_CONFIG_PROP_DRIVE;
+	case OVERRIDE_FLAG_ACTIVE_LOW:
+		return GPIOD_LINE_CONFIG_PROP_ACTIVE_LOW;
+	case OVERRIDE_FLAG_DEBOUNCE_PERIOD:
+		return GPIOD_LINE_CONFIG_PROP_DEBOUNCE_PERIOD;
+	case OVERRIDE_FLAG_CLOCK:
+		return GPIOD_LINE_CONFIG_PROP_EVENT_CLOCK;
+	case OVERRIDE_FLAG_OUTPUT_VALUE:
+		return GPIOD_LINE_CONFIG_PROP_OUTPUT_VALUE;
+	}
+
+	/* Can't happen. */
 	return -1;
 }
 
+GPIOD_API void
+gpiod_line_config_get_overrides(struct gpiod_line_config *config,
+				unsigned int *offsets, int *props)
+{
+	struct override_config *override;
+	unsigned int i, j, count = 0;
+
+	for (i = 0; i < GPIO_V2_LINES_MAX; i++) {
+		override = &config->overrides[i];
+
+		if (override_used(override)) {
+			for (j = 0; j < NUM_OVERRIDE_FLAGS; j++) {
+				if (override->override_flags &
+				    override_flag_list[j]) {

lib/line-config.c gpiod_line_config_set_output_values(struct gpiod_line_config *config, · line 883

Quoted 10 lines
lib/line-config.c +10 -0
+					offsets[count] = override->offset;
+					props[count] = override_flag_to_prop(
+							override_flag_list[j]);
+					count++;
+				}
+			}
+		}
+	}
+}
+

Return the count?
Would be a bit redundant, as the user needs to call gpiod_line_config_get_num_overrides() to size the offsets and props arrays beforehand, but the usual patten when writing into a passed array is to return the number of elements written.

Cheers,
Kent.

#5 lore raw

Re: [libgpiod v2][PATCH v3 2/3] line-config: expose the override logic to users

Quoted 26 lines · Kent Gibson
Quoted 16 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.

We also complete the API by providing functions that allow to retrieve the overridden offsets and their corresponding property types.

Quoted 7 lines

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 and add new constructors that take a variable list of arguments.

Quoted 8 lines

The variadic constructor is new for patch v3.
It bundles default constructor + default mutators, so doesn't add functionality that wasn't already available - it just makes it accessible via a single function call.
Is the variadic form beneficial for bindings, say Python, where you would prefer not to be making a bunch of C calls?
Or is this just sugar?

No, there's no benefit for the bindings as they can't possibly construct a va_list. I very much intend to have keyword arguments in Python but the only reason I added the new constructors is to justify adding the GPIOD_LINE_CONFIG_PROP_* enum because without them it would only be used for retrieving the override list.

Quoted 2 lines

No major objection, just curious about the rationale for adding it.

Actually looking at them now, I think they don't make much sense and I think about dropping them from the next iteration. It's not like the enum increases the executable size really.

Quoted 20 lines

[snip]

Quoted 18 lines
include/gpiod.h +18 -0
+/**
+ * @brief Get the total number of overridden settings currently stored by this
+ *        line config object.
+ * @param config Line config object.
+ * @return Number of individual overridden settings.
+ */
+unsigned int
+gpiod_line_config_get_num_overrides(struct gpiod_line_config *config);
+
+/**
+ * @brief Get the list of overridden offsets and the corresponding types of
+ *        overridden settings.
+ * @param config Line config object.
+ * @param offsets Array to store the overidden offsets. Must hold at least the
+ *                number of unsigned integers returned by
+ *                ::gpiod_line_config_get_output_value_offset.
+ * @param props Array to store the types of overridden settings. Must hold at
+ *              least the number of integers returned by
Quoted 8 lines
Quoted 2 lines

+ * gpiod_line_config_get_output_value_offset.
+ */

The purpose of the offsets and props arrays is not clear. Clarify that you are returning a list of (offset,prop), split across the two arrays.
Replace them with a single array of (offset,prop) unless there is a good reason to keep them separate?

The only (but good) reason is to not introduce another data structure. Especially a public one. This function is not going to be used a lot I suppose so I don't care if it's awkward to use.

Quoted 5 lines

Guessing it should be gpiod_line_config_get_num_overrides(), not gpiod_line_config_get_output_value_offset() which returns 0 or 1, or even better -1 for inputs?

Yep, just a wrong copy/paste.

Quoted 22 lines

[snip]

Quoted 20 lines
lib/line-config.c +18 -1
+GPIOD_API unsigned int
+gpiod_line_config_get_num_overrides(struct gpiod_line_config *config)
+{
+     struct override_config *override;
+     unsigned int i, j, count = 0;
 
-     errno = ENXIO;
+     for (i = 0; i < GPIO_V2_LINES_MAX; i++) {
+             override = &config->overrides[i];
+
+             if (override_used(override)) {
+                     for (j = 0; j < NUM_OVERRIDE_FLAGS; j++) {
+                             if (override->override_flags &
+                                 override_flag_list[j])
+                                     count++;
+                     }
+             }
+     }
+
+     return count;
Quoted 9 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+}
+

Using GPIO_V2_LINES_MAX for the size of the overrides array is confusing, and the two should be de-coupled so you can more easily resize the array if necessary.
Provide a NUM_OVERRIDES_MAX, or similar, and use that when referring to the size of the overrides array.

Makes sense.

Quoted 47 lines
Quoted 47 lines
lib/line-config.c +44 -0
+static int override_flag_to_prop(int flag)
+{
+     switch (flag) {
+     case OVERRIDE_FLAG_DIRECTION:
+             return GPIOD_LINE_CONFIG_PROP_DIRECTION;
+     case OVERRIDE_FLAG_EDGE:
+             return GPIOD_LINE_CONFIG_PROP_EDGE;
+     case OVERRIDE_FLAG_BIAS:
+             return GPIOD_LINE_CONFIG_PROP_BIAS;
+     case OVERRIDE_FLAG_DRIVE:
+             return GPIOD_LINE_CONFIG_PROP_DRIVE;
+     case OVERRIDE_FLAG_ACTIVE_LOW:
+             return GPIOD_LINE_CONFIG_PROP_ACTIVE_LOW;
+     case OVERRIDE_FLAG_DEBOUNCE_PERIOD:
+             return GPIOD_LINE_CONFIG_PROP_DEBOUNCE_PERIOD;
+     case OVERRIDE_FLAG_CLOCK:
+             return GPIOD_LINE_CONFIG_PROP_EVENT_CLOCK;
+     case OVERRIDE_FLAG_OUTPUT_VALUE:
+             return GPIOD_LINE_CONFIG_PROP_OUTPUT_VALUE;
+     }
+
+     /* Can't happen. */
      return -1;
 }
 
+GPIOD_API void
+gpiod_line_config_get_overrides(struct gpiod_line_config *config,
+                             unsigned int *offsets, int *props)
+{
+     struct override_config *override;
+     unsigned int i, j, count = 0;
+
+     for (i = 0; i < GPIO_V2_LINES_MAX; i++) {
+             override = &config->overrides[i];
+
+             if (override_used(override)) {
+                     for (j = 0; j < NUM_OVERRIDE_FLAGS; j++) {
+                             if (override->override_flags &
+                                 override_flag_list[j]) {
+                                     offsets[count] = override->offset;
+                                     props[count] = override_flag_to_prop(
+                                                     override_flag_list[j]);
+                                     count++;
+                             }
+                     }
+             }
+     }
Quoted 9 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+}
+

Return the count?
Would be a bit redundant, as the user needs to call gpiod_line_config_get_num_overrides() to size the offsets and props arrays beforehand, but the usual patten when writing into a passed array is to return the number of elements written.

So there are two typical approaches: take the size of the passed buffer as argument and return the number of elements actually written or define the required size of the buffer (in this case: the size returned by the get_num_overrides func) and don't return that number. I don't think it would be of any use so let's not return a value nobody would check.

Quoted 2 lines

Cheers,
Kent.

Bart

#6 lore raw

PATCH v3 3/3 tests: rewrite core C tests using libgpiosim

In reply to 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.

---
 configure.ac                 |    8 +-
 tests/Makefile.am            |   24 +-
 tests/gpiod-test-helpers.c   |   49 ++
 tests/gpiod-test-helpers.h   |  139 +++++
 tests/gpiod-test-sim.c       |  308 ++++++++++
 tests/gpiod-test-sim.h       |   42 ++
 tests/gpiod-test.c           |  233 +-------
 tests/gpiod-test.h           |   83 +--
 tests/gpiosim/gpiosim.c      |    1 +
 tests/mockup/Makefile.am     |   11 -
 tests/mockup/gpio-mockup.c   |  496 ----------------
 tests/mockup/gpio-mockup.h   |   36 --
 tests/tests-chip.c           |  282 ++++-----
 tests/tests-edge-event.c     |  490 +++++++++++++++
 tests/tests-event.c          |  908 ----------------------------
 tests/tests-info-event.c     |  301 ++++++++++
 tests/tests-line-config.c    |  503 ++++++++++++++++
 tests/tests-line-info.c      |  318 ++++++++++
 tests/tests-line-request.c   |  526 ++++++++++++++++
 tests/tests-line.c           | 1091 ----------------------------------
 tests/tests-misc.c           |   80 ++-
 tests/tests-request-config.c |   90 +++
 22 files changed, 2995 insertions(+), 3024 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
index cb4c1fd..f8d34ed 100644
@@ -28,9 +28,8 @@ AC_SUBST(VERSION_STR, [$PACKAGE_VERSION$EXTRA_VERSION])
 AC_SUBST(ABI_VERSION, [4.1.2])
 # Have a separate ABI version for C++ bindings:
 AC_SUBST(ABI_CXX_VERSION, [2.1.1])
-# ABI version for libgpiomockup (we need this since it can be installed if we
+# ABI version for libgpiosim (we need this since it can be installed if we
 # enable install-tests).
-AC_SUBST(ABI_MOCKUP_VERSION, [0.1.0])
 AC_SUBST(ABI_GPIOSIM_VERSION, [0.1.0])
 
 AC_CONFIG_AUX_DIR([autostuff])
@@ -138,14 +137,14 @@ AC_DEFUN([FUNC_NOT_FOUND_TESTS],
 
 if test "x$with_tests" = xtrue
 then
-	# For libgpiomockup & libgpiosim
+	# For libgpiosim
 	AC_CHECK_FUNC([qsort], [], [FUNC_NOT_FOUND_TESTS([qsort])])
 	PKG_CHECK_MODULES([KMOD], [libkmod >= 18])
-	PKG_CHECK_MODULES([UDEV], [libudev >= 215])
 	PKG_CHECK_MODULES([MOUNT], [mount >= 2.33.1])
 
 	# For core library tests
 	PKG_CHECK_MODULES([GLIB], [glib-2.0 >= 2.50])
+	PKG_CHECK_MODULES([GOBJECT], [gobject-2.0 >= 2.50])
 
 	if test "x$with_tools" = xtrue
 	then
@@ -236,7 +235,6 @@ AC_CONFIG_FILES([Makefile
 		 lib/libgpiod.pc
 		 tools/Makefile
 		 tests/Makefile
-		 tests/mockup/Makefile
 		 tests/gpiosim/Makefile
 		 bindings/cxx/libgpiodcxx.pc
 		 bindings/Makefile
tests/Makefile.am +16 -8
index 2b1e082..dce9a5a 100644
@@ -1,24 +1,32 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
 # SPDX-FileCopyrightText: 2017-2022 Bartosz Golaszewski <[email protected]>
 
-SUBDIRS = mockup gpiosim
-
+SUBDIRS = gpiosim
+ 
 AM_CFLAGS = -I$(top_srcdir)/include/ -I$(top_srcdir)/tests/gpiosim/
 AM_CFLAGS += -include $(top_builddir)/config.h
-AM_CFLAGS += -Wall -Wextra -g -std=gnu89 $(GLIB_CFLAGS)
+AM_CFLAGS += -Wall -Wextra -g -std=gnu89 $(GLIB_CFLAGS) $(GOBJECT_CFLAGS)
 AM_CFLAGS += -DG_LOG_DOMAIN=\"gpiod-test\"
 AM_CFLAGS += $(PROFILING_CFLAGS)
-AM_LDFLAGS = -pthread $(PROFILING_LDFLAGS)
+AM_LDFLAGS = -pthread
 LDADD = $(top_builddir)/lib/libgpiod.la
 LDADD += $(top_builddir)/tests/gpiosim/libgpiosim.la
-LDADD += $(GLIB_LIBS)
+LDADD += $(GLIB_LIBS) $(GOBJECT_LIBS)
 
 bin_PROGRAMS = gpiod-test
 
 gpiod_test_SOURCES =			\
 		gpiod-test.c		\
 		gpiod-test.h		\
+		gpiod-test-helpers.c	\
+		gpiod-test-helpers.h	\
+		gpiod-test-sim.c	\
+		gpiod-test-sim.h	\
 		tests-chip.c		\
-		tests-event.c		\
-		tests-line.c		\
-		tests-misc.c
+		tests-edge-event.c	\
+		tests-info-event.c	\
+		tests-line-config.c	\
+		tests-line-info.c	\
+		tests-line-request.c	\
+		tests-misc.c		\
+		tests-request-config.c
tests/gpiod-test-helpers.c new file +49 -0

49 lines not on this page · raw message

tests/gpiod-test-helpers.h new file +139 -0

139 lines not on this page · raw message

tests/gpiod-test-sim.c new file +308 -0

308 lines not on this page · raw message

tests/gpiod-test-sim.h new file +42 -0

42 lines not on this page · raw message

tests/gpiod-test.c +9 -224

276 lines not on this page · raw message

tests/gpiod-test.h +15 -68

101 lines not on this page · raw message

tests/gpiosim/gpiosim.c +1 -0
index 338fbc1..eab01c6 100644
@@ -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
deleted file mode 100644
index 36cd397..0000000
@@ -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
deleted file mode 100644
index 4a55032..0000000
@@ -1,36 +0,0 @@
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
-/* SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski <[email protected]> */
-
-#ifndef __GPIO_MOCKUP_H__
-#define __GPIO_MOCKUP_H__
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-struct gpio_mockup;
-
-#define GPIO_MOCKUP_FLAG_NAMED_LINES	(1 << 0)
-
-struct gpio_mockup *gpio_mockup_new(void);
-void gpio_mockup_ref(struct gpio_mockup *ctx);
-void gpio_mockup_unref(struct gpio_mockup *ctx);
-
-int gpio_mockup_probe(struct gpio_mockup *ctx, unsigned int num_chips,
-		      const unsigned int *chip_sizes, int flags);
-int gpio_mockup_remove(struct gpio_mockup *ctx);
-
-const char *gpio_mockup_chip_name(struct gpio_mockup *ctx, unsigned int idx);
-const char *gpio_mockup_chip_path(struct gpio_mockup *ctx, unsigned int idx);
-int gpio_mockup_chip_num(struct gpio_mockup *ctx, unsigned int idx);
-
-int gpio_mockup_get_value(struct gpio_mockup *ctx,
-			  unsigned int chip_idx, unsigned int line_offset);
-int gpio_mockup_set_pull(struct gpio_mockup *ctx, unsigned int chip_idx,
-			 unsigned int line_offset, int pull);
-
-#ifdef __cplusplus
-} /* extern "C" */
-#endif
-
-#endif /* __GPIO_MOCKUP_H__ */
tests/tests-chip.c +112 -170

341 lines not on this page · raw message

tests/tests-edge-event.c new file +490 -0

490 lines not on this page · raw message

tests/tests-event.c deleted +0 -908

631 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 +503 -0

503 lines not on this page · raw message

tests/tests-line-info.c new file +318 -0

318 lines not on this page · raw message

tests/tests-line-request.c new file +526 -0

526 lines not on this page · raw message

tests/tests-line.c deleted +0 -1091

1,091 lines not on this page · raw message

tests/tests-misc.c +73 -7

80 lines not on this page · raw message

tests/tests-request-config.c new file +90 -0

90 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.30.1

Part of the diff is not shown here. View the raw message.

#7 lore raw

Re: [libgpiod v2][PATCH v3 3/3] tests: rewrite core C tests using libgpiosim

Quoted 93 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.

---
 configure.ac                 |    8 +-
 tests/Makefile.am            |   24 +-
 tests/gpiod-test-helpers.c   |   49 ++
 tests/gpiod-test-helpers.h   |  139 +++++
 tests/gpiod-test-sim.c       |  308 ++++++++++
 tests/gpiod-test-sim.h       |   42 ++
 tests/gpiod-test.c           |  233 +-------
 tests/gpiod-test.h           |   83 +--
 tests/gpiosim/gpiosim.c      |    1 +
 tests/mockup/Makefile.am     |   11 -
 tests/mockup/gpio-mockup.c   |  496 ----------------
 tests/mockup/gpio-mockup.h   |   36 --
 tests/tests-chip.c           |  282 ++++-----
 tests/tests-edge-event.c     |  490 +++++++++++++++
 tests/tests-event.c          |  908 ----------------------------
 tests/tests-info-event.c     |  301 ++++++++++
 tests/tests-line-config.c    |  503 ++++++++++++++++
 tests/tests-line-info.c      |  318 ++++++++++
 tests/tests-line-request.c   |  526 ++++++++++++++++
 tests/tests-line.c           | 1091 ----------------------------------
 tests/tests-misc.c           |   80 ++-
 tests/tests-request-config.c |   90 +++
 22 files changed, 2995 insertions(+), 3024 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
index cb4c1fd..f8d34ed 100644
@@ -28,9 +28,8 @@ AC_SUBST(VERSION_STR, [$PACKAGE_VERSION$EXTRA_VERSION])
 AC_SUBST(ABI_VERSION, [4.1.2])
 # Have a separate ABI version for C++ bindings:
 AC_SUBST(ABI_CXX_VERSION, [2.1.1])
-# ABI version for libgpiomockup (we need this since it can be installed if we
+# ABI version for libgpiosim (we need this since it can be installed if we
 # enable install-tests).
-AC_SUBST(ABI_MOCKUP_VERSION, [0.1.0])
 AC_SUBST(ABI_GPIOSIM_VERSION, [0.1.0])
 
 AC_CONFIG_AUX_DIR([autostuff])
@@ -138,14 +137,14 @@ AC_DEFUN([FUNC_NOT_FOUND_TESTS],
 
 if test "x$with_tests" = xtrue
 then
-	# For libgpiomockup & libgpiosim
+	# For libgpiosim
 	AC_CHECK_FUNC([qsort], [], [FUNC_NOT_FOUND_TESTS([qsort])])
 	PKG_CHECK_MODULES([KMOD], [libkmod >= 18])
-	PKG_CHECK_MODULES([UDEV], [libudev >= 215])
 	PKG_CHECK_MODULES([MOUNT], [mount >= 2.33.1])
 
 	# For core library tests
 	PKG_CHECK_MODULES([GLIB], [glib-2.0 >= 2.50])
+	PKG_CHECK_MODULES([GOBJECT], [gobject-2.0 >= 2.50])
 
 	if test "x$with_tools" = xtrue
 	then
@@ -236,7 +235,6 @@ AC_CONFIG_FILES([Makefile
 		 lib/libgpiod.pc
 		 tools/Makefile
 		 tests/Makefile
-		 tests/mockup/Makefile
 		 tests/gpiosim/Makefile
 		 bindings/cxx/libgpiodcxx.pc
 		 bindings/Makefile
tests/Makefile.am +0 -0
index 2b1e082..dce9a5a 100644

tests/Makefile.am line 5

Quoted 7 lines
tests/Makefile.am +2 -2
@@ -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
+ 

Whitespace error.

Other than that all good.

Good to see the randomness removed from the gpio-sim naming. I would like to see the option removed from libgpiosim as well, but that is outside the scope of this patch series.

Cheers,
Kent.