All of lore.kernel.org
 help / color / mirror / Atom feed
* [libgpiod v2][PATCH 0/4] core: add gpiod_request_lines
@ 2022-03-31  1:11 Kent Gibson
  2022-03-31  1:11 ` [libgpiod v2][PATCH 1/4] " Kent Gibson
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: Kent Gibson @ 2022-03-31  1:11 UTC (permalink / raw)
  To: linux-gpio, brgl; +Cc: Kent Gibson

In my review of the CXX bindings I suggested a top-level version of
Chip.request_lines(), and possibly a C API version as well, so here
is the C version, plus a couple of semi-related tweaks I made along
the way.

The first patch adds the gpiod_request_lines().
Patch 3 migrates the appropriate tools.
Patch 4 minimizes the lifetimes of objects in the tools as I've
previously seen confusion over how long lived objects need to be.
Patch 2 is just a rename cos "inexistent" looks weird to me.
Strictly speaking it is fine, but unless there is a problem with
using "nonexistent" I would go with the latter.

This series may be require my unsigned values patch.

Cheers,
Kent.

Kent Gibson (4):
  core: add gpiod_request_lines
  tools: rename inexistent to nonexistent
  tools: migrate to gpiod_request_lines
  tools: minimize object lifetimes

 include/gpiod.h            | 15 ++++++++
 lib/line-request.c         | 17 +++++++++
 tests/tests-line-request.c | 73 ++++++++++++++++++++++++++++++++++++++
 tools/gpio-tools-test.bats |  4 +--
 tools/gpiodetect.c         |  2 +-
 tools/gpiofind.c           |  2 +-
 tools/gpioget.c            | 25 +++++++------
 tools/gpioinfo.c           |  4 +--
 tools/gpiomon.c            | 16 ++++-----
 tools/gpioset.c            | 18 +++++-----
 tools/tools-common.c       | 50 ++++++++++----------------
 tools/tools-common.h       |  5 +--
 12 files changed, 164 insertions(+), 67 deletions(-)

-- 
2.35.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [libgpiod v2][PATCH 1/4] core: add gpiod_request_lines
  2022-03-31  1:11 [libgpiod v2][PATCH 0/4] core: add gpiod_request_lines Kent Gibson
@ 2022-03-31  1:11 ` Kent Gibson
  2022-03-31  1:11 ` [libgpiod v2][PATCH 2/4] tools: rename inexistent to nonexistent Kent Gibson
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Kent Gibson @ 2022-03-31  1:11 UTC (permalink / raw)
  To: linux-gpio, brgl; +Cc: Kent Gibson

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


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [libgpiod v2][PATCH 2/4] tools: rename inexistent to nonexistent
  2022-03-31  1:11 [libgpiod v2][PATCH 0/4] core: add gpiod_request_lines Kent Gibson
  2022-03-31  1:11 ` [libgpiod v2][PATCH 1/4] " Kent Gibson
@ 2022-03-31  1:11 ` Kent Gibson
  2022-03-31  1:11 ` [libgpiod v2][PATCH 3/4] tools: migrate to gpiod_request_lines Kent Gibson
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Kent Gibson @ 2022-03-31  1:11 UTC (permalink / raw)
  To: linux-gpio, brgl; +Cc: Kent Gibson

The use of inexistent is virtually nonexistent, so switch to the more
usual form.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
---
 tools/gpio-tools-test.bats | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/gpio-tools-test.bats b/tools/gpio-tools-test.bats
index 69ad786..32c3861 100755
--- a/tools/gpio-tools-test.bats
+++ b/tools/gpio-tools-test.bats
@@ -309,8 +309,8 @@ teardown() {
 	assert_fail output_regex_match "\\s+line\\s+7:\\s+unnamed\\s+unused\\s+input\\s+active-high"
 }
 
-@test "gpioinfo: inexistent chip" {
-	run_tool gpioinfo "inexistent"
+@test "gpioinfo: nonexistent chip" {
+	run_tool gpioinfo "nonexistent"
 
 	test "$status" -eq 1
 }
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [libgpiod v2][PATCH 3/4] tools: migrate to gpiod_request_lines
  2022-03-31  1:11 [libgpiod v2][PATCH 0/4] core: add gpiod_request_lines Kent Gibson
  2022-03-31  1:11 ` [libgpiod v2][PATCH 1/4] " Kent Gibson
  2022-03-31  1:11 ` [libgpiod v2][PATCH 2/4] tools: rename inexistent to nonexistent Kent Gibson
@ 2022-03-31  1:11 ` 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
  4 siblings, 0 replies; 7+ messages in thread
From: Kent Gibson @ 2022-03-31  1:11 UTC (permalink / raw)
  To: linux-gpio, brgl; +Cc: Kent Gibson

Switch to gpiod_request_lines() for tools that only use the chip to
request lines.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
---
 tools/gpiodetect.c   |  2 +-
 tools/gpiofind.c     |  2 +-
 tools/gpioget.c      | 11 +++++-----
 tools/gpioinfo.c     |  4 ++--
 tools/gpiomon.c      | 11 +++++-----
 tools/gpioset.c      | 11 +++++-----
 tools/tools-common.c | 50 +++++++++++++++++---------------------------
 tools/tools-common.h |  5 +++--
 8 files changed, 41 insertions(+), 55 deletions(-)

diff --git a/tools/gpiodetect.c b/tools/gpiodetect.c
index 8f6e8b3..2fada05 100644
--- a/tools/gpiodetect.c
+++ b/tools/gpiodetect.c
@@ -67,7 +67,7 @@ int main(int argc, char **argv)
 		die_perror("unable to scan /dev");
 
 	for (i = 0; i < num_chips; i++) {
-		chip = chip_open_by_name(entries[i]->d_name);
+		chip = chip_open(chip_path_from_name(entries[i]->d_name));
 		if (!chip)
 			die_perror("unable to open %s", entries[i]->d_name);
 
diff --git a/tools/gpiofind.c b/tools/gpiofind.c
index 36eba86..17e1702 100644
--- a/tools/gpiofind.c
+++ b/tools/gpiofind.c
@@ -67,7 +67,7 @@ int main(int argc, char **argv)
 		die_perror("unable to scan /dev");
 
 	for (i = 0; i < num_chips; i++) {
-		chip = chip_open_by_name(entries[i]->d_name);
+		chip = chip_open(chip_path_from_name(entries[i]->d_name));
 		if (!chip) {
 			if (errno == EACCES)
 				continue;
diff --git a/tools/gpioget.c b/tools/gpioget.c
index 2cf5eae..f4c5d46 100644
--- a/tools/gpioget.c
+++ b/tools/gpioget.c
@@ -46,11 +46,10 @@ int main(int argc, char **argv)
 	struct gpiod_request_config *req_cfg;
 	struct gpiod_line_request *request;
 	struct gpiod_line_config *line_cfg;
-	struct gpiod_chip *chip;
 	bool active_low = false;
 	unsigned int *offsets, *values;
 	size_t i, num_lines;
-	char *device, *end;
+	char *device, *end, *path;
 
 	for (;;) {
 		optc = getopt_long(argc, argv, shortopts, longopts, &opti);
@@ -103,8 +102,8 @@ int main(int argc, char **argv)
 			die("invalid GPIO offset: %s", argv[i + 1]);
 	}
 
-	chip = chip_open_lookup(device);
-	if (!chip)
+	path = chip_path_lookup(device);
+	if (!path)
 		die_perror("unable to open %s", device);
 
 	line_cfg = gpiod_line_config_new();
@@ -126,7 +125,7 @@ int main(int argc, char **argv)
 	gpiod_request_config_set_consumer(req_cfg, "gpioget");
 	gpiod_request_config_set_offsets(req_cfg, num_lines, offsets);
 
-	request = gpiod_chip_request_lines(chip, req_cfg, line_cfg);
+	request = gpiod_request_lines(path, req_cfg, line_cfg);
 	if (!request)
 		die_perror("unable to request lines");
 
@@ -144,7 +143,7 @@ int main(int argc, char **argv)
 	gpiod_line_request_release(request);
 	gpiod_request_config_free(req_cfg);
 	gpiod_line_config_free(line_cfg);
-	gpiod_chip_close(chip);
+	free(path);
 	free(offsets);
 	free(values);
 
diff --git a/tools/gpioinfo.c b/tools/gpioinfo.c
index fbe2a13..f0f9926 100644
--- a/tools/gpioinfo.c
+++ b/tools/gpioinfo.c
@@ -227,7 +227,7 @@ int main(int argc, char **argv)
 			die_perror("unable to scan /dev");
 
 		for (i = 0; i < num_chips; i++) {
-			chip = chip_open_by_name(entries[i]->d_name);
+			chip = chip_open(chip_path_from_name(entries[i]->d_name));
 			if (!chip)
 				die_perror("unable to open %s",
 					   entries[i]->d_name);
@@ -240,7 +240,7 @@ int main(int argc, char **argv)
 		free(entries);
 	} else {
 		for (i = 0; i < argc; i++) {
-			chip = chip_open_lookup(argv[i]);
+			chip = chip_open(chip_path_lookup(argv[i]));
 			if (!chip)
 				die_perror("looking up chip %s", argv[i]);
 
diff --git a/tools/gpiomon.c b/tools/gpiomon.c
index f6a0dba..e461458 100644
--- a/tools/gpiomon.c
+++ b/tools/gpiomon.c
@@ -164,9 +164,8 @@ int main(int argc, char **argv)
 	struct gpiod_line_config *line_cfg;
 	unsigned int offsets[64], offset;
 	struct gpiod_edge_event *event;
-	struct gpiod_chip *chip;
 	struct mon_ctx ctx;
-	char *end;
+	char *end, *path;
 
 	/*
 	 * FIXME: use signalfd once the API has been converted to using a single file
@@ -250,8 +249,8 @@ int main(int argc, char **argv)
 		num_lines++;
 	}
 
-	chip = chip_open_lookup(argv[0]);
-	if (!chip)
+	path = chip_path_lookup(argv[0]);
+	if (!path)
 		die_perror("unable to open %s", argv[0]);
 
 	line_cfg = gpiod_line_config_new();
@@ -271,7 +270,7 @@ int main(int argc, char **argv)
 	gpiod_request_config_set_consumer(req_cfg, "gpiomon");
 	gpiod_request_config_set_offsets(req_cfg, num_lines, offsets);
 
-	request = gpiod_chip_request_lines(chip, req_cfg, line_cfg);
+	request = gpiod_request_lines(path, req_cfg, line_cfg);
 	if (!request)
 		die_perror("unable to request lines");
 
@@ -314,7 +313,7 @@ done:
 	gpiod_line_request_release(request);
 	gpiod_request_config_free(req_cfg);
 	gpiod_line_config_free(line_cfg);
-	gpiod_chip_close(chip);
+	free(path);
 
 	return EXIT_SUCCESS;
 }
diff --git a/tools/gpioset.c b/tools/gpioset.c
index 1c11470..7497eab 100644
--- a/tools/gpioset.c
+++ b/tools/gpioset.c
@@ -195,11 +195,10 @@ int main(int argc, char **argv)
 	struct gpiod_line_request *request;
 	struct gpiod_line_config *line_cfg;
 	struct callback_data cbdata;
-	struct gpiod_chip *chip;
 	bool active_low = false;
 	unsigned int *offsets, *values;
 	size_t i, num_lines;
-	char *device, *end;
+	char *device, *end, *path;
 
 	memset(&cbdata, 0, sizeof(cbdata));
 
@@ -288,8 +287,8 @@ int main(int argc, char **argv)
 			die("invalid offset: %s", argv[i + 1]);
 	}
 
-	chip = chip_open_lookup(device);
-	if (!chip)
+	path = chip_path_lookup(device);
+	if (!path)
 		die_perror("unable to open %s", device);
 
 	line_cfg = gpiod_line_config_new();
@@ -314,7 +313,7 @@ int main(int argc, char **argv)
 	gpiod_request_config_set_consumer(req_cfg, "gpioset");
 	gpiod_request_config_set_offsets(req_cfg, num_lines, offsets);
 
-	request = gpiod_chip_request_lines(chip, req_cfg, line_cfg);
+	request = gpiod_request_lines(path, req_cfg, line_cfg);
 	if (!request)
 		die_perror("unable to request lines");
 
@@ -324,7 +323,7 @@ int main(int argc, char **argv)
 	gpiod_line_request_release(request);
 	gpiod_request_config_free(req_cfg);
 	gpiod_line_config_free(line_cfg);
-	gpiod_chip_close(chip);
+	free(path);
 	free(offsets);
 
 	return EXIT_SUCCESS;
diff --git a/tools/tools-common.c b/tools/tools-common.c
index f5fd50c..8d4ca12 100644
--- a/tools/tools-common.c
+++ b/tools/tools-common.c
@@ -114,36 +114,24 @@ int chip_dir_filter(const struct dirent *entry)
 	return !!is_chip;
 }
 
-struct gpiod_chip *chip_open_by_name(const char *name)
+char *chip_path_from_name(const char *name)
 {
-	struct gpiod_chip *chip;
 	char *path;
-	int ret;
 
-	ret = asprintf(&path, "/dev/%s", name);
-	if (ret < 0)
+	if (asprintf(&path, "/dev/%s", name) < 0)
 		return NULL;
 
-	chip = gpiod_chip_open(path);
-	free(path);
-
-	return chip;
+	return path;
 }
 
-static struct gpiod_chip *chip_open_by_number(unsigned int num)
+static char *chip_path_from_number(unsigned int num)
 {
-	struct gpiod_chip *chip;
 	char *path;
-	int ret;
 
-	ret = asprintf(&path, "/dev/gpiochip%u", num);
-	if (!ret)
+	if (asprintf(&path, "/dev/gpiochip%u", num) < 0)
 		return NULL;
 
-	chip = gpiod_chip_open(path);
-	free(path);
-
-	return chip;
+	return path;
 }
 
 static bool isuint(const char *str)
@@ -154,18 +142,18 @@ static bool isuint(const char *str)
 	return *str == '\0';
 }
 
-struct gpiod_chip *chip_open_lookup(const char *device)
+char *chip_path_lookup(const char *device)
 {
-	struct gpiod_chip *chip;
-
-	if (isuint(device)) {
-		chip = chip_open_by_number(strtoul(device, NULL, 10));
-	} else {
-		if (strncmp(device, "/dev/", 5))
-			chip = chip_open_by_name(device);
-		else
-			chip = gpiod_chip_open(device);
-	}
-
-	return chip;
+	if (isuint(device))
+		return chip_path_from_number(strtoul(device, NULL, 10));
+	if (strncmp(device, "/dev/", 5))
+		return chip_path_from_name(device);
+	return strdup(device);
+}
+
+struct gpiod_chip *chip_open(const char *path)
+{
+	if (path == NULL)
+		return NULL;
+	return gpiod_chip_open(path);
 }
diff --git a/tools/tools-common.h b/tools/tools-common.h
index f059440..d0b3a83 100644
--- a/tools/tools-common.h
+++ b/tools/tools-common.h
@@ -29,7 +29,8 @@ int parse_bias(const char *option);
 void print_bias_help(void);
 int make_signalfd(void);
 int chip_dir_filter(const struct dirent *entry);
-struct gpiod_chip *chip_open_by_name(const char *name);
-struct gpiod_chip *chip_open_lookup(const char *device);
+struct gpiod_chip *chip_open(const char *path);
+char *chip_path_from_name(const char *name);
+char *chip_path_lookup(const char *device);
 
 #endif /* __GPIOD_TOOLS_COMMON_H__ */
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [libgpiod v2][PATCH 4/4] tools: minimize object lifetimes
  2022-03-31  1:11 [libgpiod v2][PATCH 0/4] core: add gpiod_request_lines Kent Gibson
                   ` (2 preceding siblings ...)
  2022-03-31  1:11 ` [libgpiod v2][PATCH 3/4] tools: migrate to gpiod_request_lines Kent Gibson
@ 2022-03-31  1:11 ` Kent Gibson
  2022-04-02 12:47 ` [libgpiod v2][PATCH 0/4] core: add gpiod_request_lines Bartosz Golaszewski
  4 siblings, 0 replies; 7+ messages in thread
From: Kent Gibson @ 2022-03-31  1:11 UTC (permalink / raw)
  To: linux-gpio, brgl; +Cc: Kent Gibson

The tools double as examples of API usage, so keep object lifetimes to a
minimum to highlight where transient objects are no longer required and
may be discarded.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
---
 tools/gpioget.c | 16 ++++++++++------
 tools/gpiomon.c |  7 ++++---
 tools/gpioset.c |  9 +++++----
 3 files changed, 19 insertions(+), 13 deletions(-)

diff --git a/tools/gpioget.c b/tools/gpioget.c
index f4c5d46..f789198 100644
--- a/tools/gpioget.c
+++ b/tools/gpioget.c
@@ -92,8 +92,7 @@ int main(int argc, char **argv)
 	num_lines = argc - 1;
 
 	offsets = calloc(num_lines, sizeof(*offsets));
-	values = calloc(num_lines, sizeof(*values));
-	if (!offsets || ! values)
+	if (!offsets)
 		die("out of memory");
 
 	for (i = 0; i < num_lines; i++) {
@@ -124,11 +123,20 @@ int main(int argc, char **argv)
 
 	gpiod_request_config_set_consumer(req_cfg, "gpioget");
 	gpiod_request_config_set_offsets(req_cfg, num_lines, offsets);
+	free(offsets);
 
 	request = gpiod_request_lines(path, req_cfg, line_cfg);
 	if (!request)
 		die_perror("unable to request lines");
 
+	free(path);
+	gpiod_request_config_free(req_cfg);
+	gpiod_line_config_free(line_cfg);
+
+	values = calloc(num_lines, sizeof(*values));
+	if (!values)
+		die("out of memory");
+
 	ret = gpiod_line_request_get_values(request, values);
 	if (ret)
 		die_perror("unable to read GPIO line values");
@@ -141,10 +149,6 @@ int main(int argc, char **argv)
 	printf("\n");
 
 	gpiod_line_request_release(request);
-	gpiod_request_config_free(req_cfg);
-	gpiod_line_config_free(line_cfg);
-	free(path);
-	free(offsets);
 	free(values);
 
 	return EXIT_SUCCESS;
diff --git a/tools/gpiomon.c b/tools/gpiomon.c
index e461458..34de2b2 100644
--- a/tools/gpiomon.c
+++ b/tools/gpiomon.c
@@ -274,6 +274,10 @@ int main(int argc, char **argv)
 	if (!request)
 		die_perror("unable to request lines");
 
+	free(path);
+	gpiod_request_config_free(req_cfg);
+	gpiod_line_config_free(line_cfg);
+
 	event_buffer = gpiod_edge_event_buffer_new(EVENT_BUF_SIZE);
 	if (!event_buffer)
 		die_perror("unable to allocate the line event buffer");
@@ -311,9 +315,6 @@ int main(int argc, char **argv)
 done:
 	gpiod_edge_event_buffer_free(event_buffer);
 	gpiod_line_request_release(request);
-	gpiod_request_config_free(req_cfg);
-	gpiod_line_config_free(line_cfg);
-	free(path);
 
 	return EXIT_SUCCESS;
 }
diff --git a/tools/gpioset.c b/tools/gpioset.c
index 7497eab..f28f8b6 100644
--- a/tools/gpioset.c
+++ b/tools/gpioset.c
@@ -312,19 +312,20 @@ int main(int argc, char **argv)
 
 	gpiod_request_config_set_consumer(req_cfg, "gpioset");
 	gpiod_request_config_set_offsets(req_cfg, num_lines, offsets);
+	free(offsets);
 
 	request = gpiod_request_lines(path, req_cfg, line_cfg);
 	if (!request)
 		die_perror("unable to request lines");
 
+	free(path);
+	gpiod_request_config_free(req_cfg);
+	gpiod_line_config_free(line_cfg);
+
 	if (mode->callback)
 		mode->callback(&cbdata);
 
 	gpiod_line_request_release(request);
-	gpiod_request_config_free(req_cfg);
-	gpiod_line_config_free(line_cfg);
-	free(path);
-	free(offsets);
 
 	return EXIT_SUCCESS;
 }
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [libgpiod v2][PATCH 0/4] core: add gpiod_request_lines
  2022-03-31  1:11 [libgpiod v2][PATCH 0/4] core: add gpiod_request_lines Kent Gibson
                   ` (3 preceding siblings ...)
  2022-03-31  1:11 ` [libgpiod v2][PATCH 4/4] tools: minimize object lifetimes Kent Gibson
@ 2022-04-02 12:47 ` Bartosz Golaszewski
  2022-04-04  9:54   ` Kent Gibson
  4 siblings, 1 reply; 7+ messages in thread
From: Bartosz Golaszewski @ 2022-04-02 12:47 UTC (permalink / raw)
  To: Kent Gibson; +Cc: open list:GPIO SUBSYSTEM

On Thu, Mar 31, 2022 at 3:12 AM Kent Gibson <warthog618@gmail.com> wrote:
>
> In my review of the CXX bindings I suggested a top-level version of
> Chip.request_lines(), and possibly a C API version as well, so here
> is the C version, plus a couple of semi-related tweaks I made along
> the way.
>
> The first patch adds the gpiod_request_lines().
> Patch 3 migrates the appropriate tools.
> Patch 4 minimizes the lifetimes of objects in the tools as I've
> previously seen confusion over how long lived objects need to be.
> Patch 2 is just a rename cos "inexistent" looks weird to me.
> Strictly speaking it is fine, but unless there is a problem with
> using "nonexistent" I would go with the latter.
>
> This series may be require my unsigned values patch.
>
> Cheers,
> Kent.
>
> Kent Gibson (4):
>   core: add gpiod_request_lines
>   tools: rename inexistent to nonexistent
>   tools: migrate to gpiod_request_lines
>   tools: minimize object lifetimes
>
>  include/gpiod.h            | 15 ++++++++
>  lib/line-request.c         | 17 +++++++++
>  tests/tests-line-request.c | 73 ++++++++++++++++++++++++++++++++++++++
>  tools/gpio-tools-test.bats |  4 +--
>  tools/gpiodetect.c         |  2 +-
>  tools/gpiofind.c           |  2 +-
>  tools/gpioget.c            | 25 +++++++------
>  tools/gpioinfo.c           |  4 +--
>  tools/gpiomon.c            | 16 ++++-----
>  tools/gpioset.c            | 18 +++++-----
>  tools/tools-common.c       | 50 ++++++++++----------------
>  tools/tools-common.h       |  5 +--
>  12 files changed, 164 insertions(+), 67 deletions(-)
>
> --
> 2.35.1
>

Ugh, I didn't respond under the C++ review in time before you spent
time on this. :/

I don't agree with this change. For C API I think the intention for v2
was to avoid having all kinds of high-level helpers and limit the
number of functions to only those that are necessary to fully leverage
the kernel uAPI and this one isn't necessary. I think we discussed it
multiple times and agreed that the C library needs to be minimal this
time.

For C++ and Python the issue is irrelevant because you can do:

auto request = gpiod::Chip("/dev/gpiochip0").request_lines(req_cfg, line_cfg);

or

request = gpiod.Chip("/dev/gpiochip0").request_lines(req_cfg, line_cfg)

respectively and achieve the same result while still using a one-liner.

Unless there's a *really* good reason to do this, it's a NAK from my side.

Bart

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [libgpiod v2][PATCH 0/4] core: add gpiod_request_lines
  2022-04-02 12:47 ` [libgpiod v2][PATCH 0/4] core: add gpiod_request_lines Bartosz Golaszewski
@ 2022-04-04  9:54   ` Kent Gibson
  0 siblings, 0 replies; 7+ messages in thread
From: Kent Gibson @ 2022-04-04  9:54 UTC (permalink / raw)
  To: Bartosz Golaszewski; +Cc: open list:GPIO SUBSYSTEM

On Sat, Apr 02, 2022 at 02:47:31PM +0200, Bartosz Golaszewski wrote:
> On Thu, Mar 31, 2022 at 3:12 AM Kent Gibson <warthog618@gmail.com> wrote:
> >
> > In my review of the CXX bindings I suggested a top-level version of
> > Chip.request_lines(), and possibly a C API version as well, so here
> > is the C version, plus a couple of semi-related tweaks I made along
> > the way.
> >
> > The first patch adds the gpiod_request_lines().
> > Patch 3 migrates the appropriate tools.
> > Patch 4 minimizes the lifetimes of objects in the tools as I've
> > previously seen confusion over how long lived objects need to be.
> > Patch 2 is just a rename cos "inexistent" looks weird to me.
> > Strictly speaking it is fine, but unless there is a problem with
> > using "nonexistent" I would go with the latter.
> >
> > This series may be require my unsigned values patch.
> >
> > Cheers,
> > Kent.
> >
> > Kent Gibson (4):
> >   core: add gpiod_request_lines
> >   tools: rename inexistent to nonexistent
> >   tools: migrate to gpiod_request_lines
> >   tools: minimize object lifetimes
> >
> >  include/gpiod.h            | 15 ++++++++
> >  lib/line-request.c         | 17 +++++++++
> >  tests/tests-line-request.c | 73 ++++++++++++++++++++++++++++++++++++++
> >  tools/gpio-tools-test.bats |  4 +--
> >  tools/gpiodetect.c         |  2 +-
> >  tools/gpiofind.c           |  2 +-
> >  tools/gpioget.c            | 25 +++++++------
> >  tools/gpioinfo.c           |  4 +--
> >  tools/gpiomon.c            | 16 ++++-----
> >  tools/gpioset.c            | 18 +++++-----
> >  tools/tools-common.c       | 50 ++++++++++----------------
> >  tools/tools-common.h       |  5 +--
> >  12 files changed, 164 insertions(+), 67 deletions(-)
> >
> > --
> > 2.35.1
> >
> 
> Ugh, I didn't respond under the C++ review in time before you spent
> time on this. :/
> 

Not a problem - it was just something to fill in time.

> I don't agree with this change. For C API I think the intention for v2
> was to avoid having all kinds of high-level helpers and limit the
> number of functions to only those that are necessary to fully leverage
> the kernel uAPI and this one isn't necessary. I think we discussed it
> multiple times and agreed that the C library needs to be minimal this
> time.
> 
> For C++ and Python the issue is irrelevant because you can do:
> 
> auto request = gpiod::Chip("/dev/gpiochip0").request_lines(req_cfg, line_cfg);
> 
> or
> 
> request = gpiod.Chip("/dev/gpiochip0").request_lines(req_cfg, line_cfg)
> 

The point isn't being able to do a one liner, the point is to remove the
chip object from the set of concerns for the basic use case, and so
provide an easier on-ramp for the casual user.

> respectively and achieve the same result while still using a one-liner.
> 
> Unless there's a *really* good reason to do this, it's a NAK from my side.
> 

That's fine.

Cheers,
Kent.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-04-04  9:54 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-31  1:11 [libgpiod v2][PATCH 0/4] core: add gpiod_request_lines Kent Gibson
2022-03-31  1:11 ` [libgpiod v2][PATCH 1/4] " Kent Gibson
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

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.