diff mbox series

[libgpiod,v3,2/3] API: extend the line request functionality

Message ID 20210727143400.32543-3-brgl@bgdev.pl
State New
Headers show
Series libgpiod v2: C++ bindings | expand

Commit Message

Bartosz Golaszewski July 27, 2021, 2:33 p.m. UTC
This adds new public functions for interacting with line request objects.
First we implement getters for the number of lines in a request and for
the array of offsets of requested lines. Next we rename the existing
gpiod_line_request_get/set_values() functions to
gpiod_line_request_get/set_values_subset() and add new functions that are
named gpiod_line_request_get/set_values() but allow to get/set the values
of all requested lines.

Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
---
 include/gpiod.h    | 54 ++++++++++++++++++++++++++++++++++++++++------
 lib/line-request.c | 50 +++++++++++++++++++++++++++++++++---------
 tools/gpioget.c    |  3 +--
 3 files changed, 89 insertions(+), 18 deletions(-)
diff mbox series

Patch

diff --git a/include/gpiod.h b/include/gpiod.h
index 8fc20ed..51e93be 100644
--- a/include/gpiod.h
+++ b/include/gpiod.h
@@ -814,6 +814,22 @@  gpiod_request_config_set_event_buffer_size(struct gpiod_request_config *config,
  */
 void gpiod_line_request_release(struct gpiod_line_request *request);
 
+/**
+ * @brief Get the number of lines in this request.
+ * @param request Line request object.
+ * @return Number of requested lines.
+ */
+unsigned int gpiod_line_request_get_num_lines(struct gpiod_line_request *request);
+
+/**
+ * @brief Get the hardware offsets of lines in this request.
+ * @param request Line request object.
+ * @param offsets Array to store offsets in. Must hold at least the number of
+ *                lines returned by ::gpiod_line_request_get_num_lines.
+ */
+void gpiod_line_request_get_offsets(struct gpiod_line_request *request,
+				    unsigned int *offsets);
+
 /**
  * @brief Read the value of a single line associated with this request.
  * @param request Line request object.
@@ -824,7 +840,7 @@  int gpiod_line_request_get_value(struct gpiod_line_request *request,
 				 unsigned int offset);
 
 /**
- * @brief Read values of lines associated with this request.
+ * @brief Read values of a subset of lines associated with this request.
  * @param request GPIO line request.
  * @param num_lines Number of lines for which to read values.
  * @param offsets Array of offsets corresponding with the lines associated with
@@ -832,9 +848,23 @@  int gpiod_line_request_get_value(struct gpiod_line_request *request,
  * @param values Array in which the values will be stored.
  * @return 0 on success, -1 on failure.
  */
+int gpiod_line_request_get_values_subset(struct gpiod_line_request *request,
+					 unsigned num_lines,
+					 const unsigned int *offsets,
+					 int *values);
+
+/**
+ * @brief Read values of all lines associated with this request.
+ * @param request GPIO line request.
+ * @param values Array in which the values will be stored. Must hold at least
+ *               the number of lines returned by
+ *               ::gpiod_line_request_get_num_lines. The index of each value
+ *               will be associated with the offset at the same index in the
+ *               offset array returned by ::gpiod_line_request_get_offsets.
+ * @return 0 on success, -1 on failure.
+ */
 int gpiod_line_request_get_values(struct gpiod_line_request *request,
-				  unsigned num_lines,
-				  const unsigned int *offsets, int *values);
+				  int *values);
 
 /**
  * @brief Set the value of a single line associated with this request.
@@ -846,7 +876,7 @@  int gpiod_line_request_set_value(struct gpiod_line_request *request,
 				 unsigned int offset, int value);
 
 /**
- * @brief Set values of lines associated with this line request.
+ * @brief Set values of a subset of lines associated with this line request.
  * @param request GPIO line request.
  * @param num_lines Number of lines for which to set values.
  * @param offsets Array of offsets corresponding with the lines associated with
@@ -855,9 +885,21 @@  int gpiod_line_request_set_value(struct gpiod_line_request *request,
  *               correspond with the offsets in the previous argument.
  * @return 0 on success, -1 on failure.
  */
+int gpiod_line_request_set_values_subset(struct gpiod_line_request *request,
+					 unsigned int num_lines,
+					 const unsigned int *offsets,
+					 const int *values);
+
+/**
+ * @brief Set values of all lines associated with this request.
+ * @param request GPIO line request.
+ * @param values Array containing the values to set. Must hold at least the
+ *               number of lines returned by ::gpiod_line_request_get_num_lines.
+ *               The index of each value be associated with the offset at the
+ *               same index in the offset array returned by
+ *               ::gpiod_line_request_get_offsets.
+ */
 int gpiod_line_request_set_values(struct gpiod_line_request *request,
-				  unsigned int num_lines,
-				  const unsigned int *offsets,
 				  const int *values);
 
 /**
diff --git a/lib/line-request.c b/lib/line-request.c
index 6cbb08a..8008b89 100644
--- a/lib/line-request.c
+++ b/lib/line-request.c
@@ -45,13 +45,27 @@  GPIOD_API void gpiod_line_request_release(struct gpiod_line_request *request)
 	free(request);
 }
 
+GPIOD_API unsigned int
+gpiod_line_request_get_num_lines(struct gpiod_line_request *request)
+{
+	return request->num_lines;
+}
+
+GPIOD_API void
+gpiod_line_request_get_offsets(struct gpiod_line_request *request,
+			       unsigned int *offsets)
+{
+	memcpy(offsets, request->offsets,
+	       sizeof(*offsets) * request->num_lines);
+}
+
 GPIOD_API int gpiod_line_request_get_value(struct gpiod_line_request *request,
 					   unsigned int offset)
 {
 	unsigned int ret;
 	int val;
 
-	ret = gpiod_line_request_get_values(request, 1, &offset, &val);
+	ret = gpiod_line_request_get_values_subset(request, 1, &offset, &val);
 	if (ret)
 		return -1;
 
@@ -71,10 +85,10 @@  static int offset_to_bit(struct gpiod_line_request *request,
 	return -1;
 }
 
-GPIOD_API int gpiod_line_request_get_values(struct gpiod_line_request *request,
-					    unsigned num_lines,
-					    const unsigned int *offsets,
-					    int *values)
+GPIOD_API int
+gpiod_line_request_get_values_subset(struct gpiod_line_request *request,
+				     unsigned num_lines,
+				     const unsigned int *offsets, int *values)
 {
 	struct gpio_v2_line_values buf;
 	uint64_t mask = 0, bits = 0;
@@ -119,16 +133,25 @@  GPIOD_API int gpiod_line_request_get_values(struct gpiod_line_request *request,
 	return 0;
 }
 
+GPIOD_API int gpiod_line_request_get_values(struct gpiod_line_request *request,
+					    int *values)
+{
+	return gpiod_line_request_get_values_subset(request, request->num_lines,
+						    request->offsets, values);
+}
+
 GPIOD_API int gpiod_line_request_set_value(struct gpiod_line_request *request,
 					   unsigned int offset, int value)
 {
-	return gpiod_line_request_set_values(request, 1, &offset, &value);
+	return gpiod_line_request_set_values_subset(request, 1,
+						    &offset, &value);
 }
 
-GPIOD_API int gpiod_line_request_set_values(struct gpiod_line_request *request,
-					    unsigned int num_lines,
-					    const unsigned int *offsets,
-					    const int *values)
+GPIOD_API int
+gpiod_line_request_set_values_subset(struct gpiod_line_request *request,
+				     unsigned int num_lines,
+				     const unsigned int *offsets,
+				     const int *values)
 {
 	struct gpio_v2_line_values buf;
 	uint64_t mask = 0, bits = 0;
@@ -153,6 +176,13 @@  GPIOD_API int gpiod_line_request_set_values(struct gpiod_line_request *request,
 	return ioctl(request->fd, GPIO_V2_LINE_SET_VALUES_IOCTL, &buf);
 }
 
+GPIOD_API int gpiod_line_request_set_values(struct gpiod_line_request *request,
+					    const int *values)
+{
+	return gpiod_line_request_set_values_subset(request, request->num_lines,
+						    request->offsets, values);
+}
+
 GPIOD_API int
 gpiod_line_request_reconfigure_lines(struct gpiod_line_request *request,
 				     struct gpiod_line_config *config)
diff --git a/tools/gpioget.c b/tools/gpioget.c
index e0c630d..afa2ccb 100644
--- a/tools/gpioget.c
+++ b/tools/gpioget.c
@@ -129,8 +129,7 @@  int main(int argc, char **argv)
 	if (!request)
 		die_perror("unable to request lines");
 
-	ret = gpiod_line_request_get_values(request, num_lines,
-					    offsets, values);
+	ret = gpiod_line_request_get_values(request, values);
 	if (ret)
 		die_perror("unable to read GPIO line values");