All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kent Gibson <warthog618@gmail.com>
To: linux-gpio@vger.kernel.org, brgl@bgdev.pl
Cc: Kent Gibson <warthog618@gmail.com>
Subject: [libgpiod v2][PATCH 1/4] core: add gpiod_request_lines
Date: Thu, 31 Mar 2022 09:11:38 +0800	[thread overview]
Message-ID: <20220331011141.53489-2-warthog618@gmail.com> (raw)
In-Reply-To: <20220331011141.53489-1-warthog618@gmail.com>

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 --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 };
-- 
2.35.1


  reply	other threads:[~2022-03-31  1:12 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-31  1:11 [libgpiod v2][PATCH 0/4] core: add gpiod_request_lines Kent Gibson
2022-03-31  1:11 ` Kent Gibson [this message]
2022-03-31  1:11 ` [libgpiod v2][PATCH 2/4] tools: rename inexistent to nonexistent Kent Gibson
2022-03-31  1:11 ` [libgpiod v2][PATCH 3/4] tools: migrate to gpiod_request_lines Kent Gibson
2022-03-31  1:11 ` [libgpiod v2][PATCH 4/4] tools: minimize object lifetimes Kent Gibson
2022-04-02 12:47 ` [libgpiod v2][PATCH 0/4] core: add gpiod_request_lines Bartosz Golaszewski
2022-04-04  9:54   ` Kent Gibson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220331011141.53489-2-warthog618@gmail.com \
    --to=warthog618@gmail.com \
    --cc=brgl@bgdev.pl \
    --cc=linux-gpio@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.