diff mbox series

[libgpiod,v2,1/4] core: add gpiod_request_lines

Message ID 20220331011141.53489-2-warthog618@gmail.com
State New
Headers show
Series core: add gpiod_request_lines | expand

Commit Message

Kent Gibson March 31, 2022, 1:11 a.m. UTC
Add a wrapper around gpiod_chip_open() and gpiod_chip_request_lines()
for the common use case of only requesting a single set of lines from a
chip.  The chip itself is only required for the request so hide it from
the user.

Signed-off-by: Kent Gibson<warthog618@gmail.com>
---
 include/gpiod.h            | 15 ++++++++
 lib/line-request.c         | 17 +++++++++
 tests/tests-line-request.c | 73 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 105 insertions(+)
diff mbox series

Patch

diff --git a/include/gpiod.h b/include/gpiod.h
index 456c2b8..8f64d7f 100644
--- a/include/gpiod.h
+++ b/include/gpiod.h
@@ -50,6 +50,21 @@  struct gpiod_info_event;
 struct gpiod_edge_event;
 struct gpiod_edge_event_buffer;
 
+/**
+ * @brief Request a set of lines for exclusive usage.
+ * @param path GPIO chip path.
+ * @param req_cfg Request config object.
+ * @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 for lines that are not requested are
+ *	 silently ignored.
+ */
+struct gpiod_line_request *
+gpiod_request_lines(const char *path,
+		    struct gpiod_request_config *req_cfg,
+		    struct gpiod_line_config *line_cfg);
+
 /**
  * @defgroup chips GPIO chips
  * @{
diff --git a/lib/line-request.c b/lib/line-request.c
index d578a9e..639a66f 100644
--- a/lib/line-request.c
+++ b/lib/line-request.c
@@ -16,6 +16,23 @@  struct gpiod_line_request {
 	int fd;
 };
 
+GPIOD_API struct gpiod_line_request *
+gpiod_request_lines(const char *path,
+		    struct gpiod_request_config *req_cfg,
+		    struct gpiod_line_config *line_cfg)
+{
+	struct gpiod_chip *chip;
+	struct gpiod_line_request *request;
+
+	chip = gpiod_chip_open(path);
+	if (chip == NULL)
+		return NULL;
+
+	request = gpiod_chip_request_lines(chip, req_cfg, line_cfg);
+	gpiod_chip_close(chip);
+	return request;
+}
+
 struct gpiod_line_request *
 gpiod_line_request_from_uapi(struct gpio_v2_line_request *uapi_req)
 {
diff --git a/tests/tests-line-request.c b/tests/tests-line-request.c
index 531e466..bf7ebb1 100644
--- a/tests/tests-line-request.c
+++ b/tests/tests-line-request.c
@@ -332,6 +332,79 @@  GPIOD_TEST_CASE(request_survives_parent_chip)
 	gpiod_test_return_if_failed();
 }
 
+GPIOD_TEST_CASE(request_chipless)
+{
+	static const guint offset = 0;
+
+	g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 4, 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;
+
+	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_direction_default(line_cfg,
+						GPIOD_LINE_DIRECTION_OUTPUT);
+	gpiod_line_config_set_output_value_default(line_cfg,
+						   GPIOD_LINE_VALUE_ACTIVE);
+
+	request = gpiod_request_lines(g_gpiosim_chip_get_dev_path(sim),
+				      req_cfg, line_cfg);
+	g_assert_nonnull(request);
+
+	g_assert_cmpint(gpiod_line_request_get_value(request, offset), ==,
+			GPIOD_LINE_VALUE_ACTIVE);
+
+	ret = gpiod_line_request_set_value(request, offset,
+					   GPIOD_LINE_VALUE_ACTIVE);
+	g_assert_cmpint(ret, ==, 0);
+	gpiod_test_return_if_failed();
+
+	ret = gpiod_line_request_set_value(request, offset,
+					   GPIOD_LINE_VALUE_ACTIVE);
+	g_assert_cmpint(ret, ==, 0);
+	gpiod_test_return_if_failed();
+}
+
+GPIOD_TEST_CASE(request_chipless_bad_device)
+{
+	static const guint offset = 0;
+
+	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;
+
+	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_direction_default(line_cfg,
+						GPIOD_LINE_DIRECTION_OUTPUT);
+	gpiod_line_config_set_output_value_default(line_cfg,
+						   GPIOD_LINE_VALUE_ACTIVE);
+
+	request = gpiod_request_lines("/dev/nonexistent",
+				      req_cfg, line_cfg);
+	g_assert_null(request);
+	gpiod_test_expect_errno(ENOENT);
+
+	request = gpiod_request_lines("/tmp",
+				      req_cfg, line_cfg);
+	g_assert_null(request);
+	gpiod_test_expect_errno(ENOTTY);
+
+
+	request = gpiod_request_lines("/dev/null",
+				      req_cfg, line_cfg);
+	g_assert_null(request);
+	gpiod_test_expect_errno(ENODEV);
+}
+
+
 GPIOD_TEST_CASE(request_with_overridden_direction)
 {
 	static const guint offsets[] = { 0, 1, 2, 3 };