diff mbox series

[libgpiod,v2,1/3] chip: provide gpiod_chip_get_path()

Message ID 20210730144356.23079-2-brgl@bgdev.pl
State New
Headers show
Series libgpiod v2: new API improvements | expand

Commit Message

Bartosz Golaszewski July 30, 2021, 2:43 p.m. UTC
Provide a chip getter for retrieving the path to the device file used to
get the GPIO chip handle.

Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
---
 include/gpiod.h |  7 +++++++
 lib/chip.c      | 15 ++++++++++++++-
 2 files changed, 21 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/include/gpiod.h b/include/gpiod.h
index 56dc986..d3c327b 100644
--- a/include/gpiod.h
+++ b/include/gpiod.h
@@ -88,6 +88,13 @@  const char *gpiod_chip_get_name(struct gpiod_chip *chip);
  */
 const char *gpiod_chip_get_label(struct gpiod_chip *chip);
 
+/**
+ * @brief Get the path used to open this GPIO chip.
+ * @param chip GPIO chip object.
+ * @return Path to the file passed as argument to ::gpiod_chip_open.
+ */
+const char *gpiod_chip_get_path(struct gpiod_chip *chip);
+
 /**
  * @brief Get the number of GPIO lines exposed by this chip.
  * @param chip GPIO chip object.
diff --git a/lib/chip.c b/lib/chip.c
index a200ef2..6fcada9 100644
--- a/lib/chip.c
+++ b/lib/chip.c
@@ -18,6 +18,7 @@  struct gpiod_chip {
 	unsigned int num_lines;
 	char name[32];
 	char label[32];
+	char *path;
 };
 
 GPIOD_API struct gpiod_chip *gpiod_chip_open(const char *path)
@@ -40,9 +41,13 @@  GPIOD_API struct gpiod_chip *gpiod_chip_open(const char *path)
 	memset(chip, 0, sizeof(*chip));
 	memset(&info, 0, sizeof(info));
 
+	chip->path = strdup(path);
+	if (!chip->path)
+		goto err_free_chip;
+
 	ret = ioctl(fd, GPIO_GET_CHIPINFO_IOCTL, &info);
 	if (ret < 0)
-		goto err_free_chip;
+		goto err_free_path;
 
 	chip->fd = fd;
 	chip->num_lines = info.lines;
@@ -65,6 +70,8 @@  GPIOD_API struct gpiod_chip *gpiod_chip_open(const char *path)
 
 	return chip;
 
+err_free_path:
+	free(chip->path);
 err_free_chip:
 	free(chip);
 err_close_fd:
@@ -79,6 +86,7 @@  GPIOD_API void gpiod_chip_close(struct gpiod_chip *chip)
 		return;
 
 	close(chip->fd);
+	free(chip->path);
 	free(chip);
 }
 
@@ -92,6 +100,11 @@  GPIOD_API const char *gpiod_chip_get_label(struct gpiod_chip *chip)
 	return chip->label;
 }
 
+GPIOD_API const char *gpiod_chip_get_path(struct gpiod_chip *chip)
+{
+	return chip->path;
+}
+
 GPIOD_API unsigned int gpiod_chip_get_num_lines(struct gpiod_chip *chip)
 {
 	return chip->num_lines;