Skip to contentSkip to conversation

treewide: another bunch of cleanups for v2.0

PATCH In review Bartosz Golaszewski 3 participants 11 messages Mar 9, 2021 – Mar 17, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover treewide: another bunch of cleanups for v2.0

From: Bartosz Golaszewski <[email protected]>

Kent suggested I send the patches for v2.0 API in smaller batches for earlier review instead of dropping a huge patch bomb that will require more work from my side and may later be negatively reviewed so here's a hopefully the last part of cleanups that can go into master already before first patches with the new C API will drop.

The idea for the new C API is this: all objects are opaque and refcounted. This is why the chip was converted to reference counting. Lines were removed and their functionality split into line_info and line_request, this is why I'm reworking line lookup to return the HW offset within a chip and not the line object. Other patches are mostly cleanups and cruft shave-off.

Bartosz Golaszewski (6):
treewide: simplify line lookup
tests: remove line bulk test cases
core: switch to reference counting for gpio chip objects
treewide: remove is_requested() and is_free()
treewide: kill line updating
core: hide the GPIOD_API symbol

 bindings/cxx/Makefile.am               |   2 +-
 bindings/cxx/chip.cpp                  |  54 +++---
 bindings/cxx/examples/gpiofindcxx.cpp  |   7 +-
 bindings/cxx/gpiod.hpp                 | 249 ++++++++++++-------------
 bindings/cxx/internal.hpp              |   9 +
 bindings/cxx/iter.cpp                  |  18 +-
 bindings/cxx/line.cpp                  |  86 ++++-----
 bindings/cxx/line_bulk.cpp             |  78 ++++----
 bindings/cxx/tests/tests-chip.cpp      |   6 +-
 bindings/cxx/tests/tests-line.cpp      |  57 ------
 bindings/python/examples/gpiofind.py   |   7 +-
 bindings/python/gpiodmodule.c          | 101 ++--------
 bindings/python/tests/gpiod_py_test.py |  49 +----
 include/gpiod.h                        | 229 +++++++++--------------
 lib/Makefile.am                        |   2 +-
 lib/core.c                             | 195 ++++++++++---------
 lib/helpers.c                          | 155 ++++++---------
 lib/internal.h                         |  11 ++
 lib/misc.c                             |   4 +-
 tests/Makefile.am                      |   1 -
 tests/gpiod-test.h                     |   2 +-
 tests/tests-bulk.c                     | 145 --------------
 tests/tests-chip.c                     |  17 +-
 tools/gpiodetect.c                     |   2 +-
 tools/gpiofind.c                       |  11 +-
 tools/gpioget.c                        |   2 +-
 tools/gpioinfo.c                       |   4 +-
 tools/gpiomon.c                        |   2 +-
 tools/gpioset.c                        |   2 +-
 29 files changed, 547 insertions(+), 960 deletions(-)
 create mode 100644 bindings/cxx/internal.hpp
 create mode 100644 lib/internal.h
 delete mode 100644 tests/tests-bulk.c
Signature · 1 line
2.30.1
#1 lore raw

PATCH 1/6 treewide: simplify line lookup

From: Bartosz Golaszewski <[email protected]>

We're preparing to entirely remove the line objects from the API and split their functionality between two new objects: line_info and line_request. The lookup functions must be limited in the process.

This reworks all the find_line methods to: a) always assume that names looked for are unique within a single chip (because while it's technically possible for GPIO line names to be non-unique, it doesn't make sense to look for two lines named the same) and b) return the hardware offset within the chip instead of the line object.

---
 bindings/cxx/chip.cpp                  | 14 +++----
 bindings/cxx/examples/gpiofindcxx.cpp  |  7 ++--
 bindings/cxx/gpiod.hpp                 | 15 +++----
 bindings/cxx/tests/tests-chip.cpp      |  6 +--
 bindings/python/examples/gpiofind.py   |  7 ++--
 bindings/python/gpiodmodule.c          | 51 ++++++++---------------
 bindings/python/tests/gpiod_py_test.py |  9 ++--
 include/gpiod.h                        | 28 +++----------
 lib/helpers.c                          | 57 +++-----------------------
 tests/tests-chip.c                     | 17 +++++---
 tools/gpiofind.c                       |  9 ++--
 11 files changed, 66 insertions(+), 154 deletions(-)
bindings/cxx/chip.cpp +5 -9
index 5b8125b..527fb34 100644
@@ -92,22 +92,18 @@ line chip::get_line(unsigned int offset) const
 	return line(line_handle, *this);
 }
 
-::std::vector<line> chip::find_line(const ::std::string& name, bool unique) const
+int chip::find_line(const ::std::string& name) const
 {
 	this->throw_if_noref();
 
-	::std::vector<line> lines;
+	for (unsigned int offset = 0; offset < this->num_lines(); offset++) {
+		auto line = this->get_line(offset);
 
-	for (auto& line: ::gpiod::line_iter(*this)) {
 		if (line.name() == name)
-			lines.push_back(line);
+			return offset;
 	}
 
-	if (unique && lines.size() > 1)
-		throw ::std::system_error(ERANGE, ::std::system_category(),
-					  "multiple lines with the same name found");
-
-	return lines;
+	return -1;
 }
 
 line_bulk chip::get_lines(const ::std::vector<unsigned int>& offsets) const
bindings/cxx/examples/gpiofindcxx.cpp +3 -4
index 0bccd94..ec4d79b 100644
@@ -20,10 +20,9 @@ int main(int argc, char **argv)
 		if (::gpiod::is_gpiochip_device(entry.path())) {
 			::gpiod::chip chip(entry.path());
 
-			auto lines = chip.find_line(argv[1], true);
-			if (!lines.empty()) {
-				::std::cout << lines.front().name() << " " <<
-					       lines.front().offset() << ::std::endl;
+			auto offset = chip.find_line(argv[1]);
+			if (offset >= 0) {
+				::std::cout << chip.name() << " " << offset << ::std::endl;
 				return EXIT_SUCCESS;
 			}
 		}
bindings/cxx/gpiod.hpp +6 -9
index d987b3a..3a043a1 100644
@@ -129,15 +129,12 @@ public:
 	GPIOD_API line get_line(unsigned int offset) const;
 
 	/**
-	 * @brief Find all GPIO lines by name among lines exposed by this GPIO
-	 *        chip.
-	 * @param name Line name.
-	 * @param unique If set to true: throw an error if multiple lines match
-	 *               the name.
-	 * @return Vector of all matching lines.
-	 */
-	GPIOD_API ::std::vector<line> find_line(const ::std::string& name,
-						bool unique = false) const;
+	 * @brief Map a GPIO line's name to its offset within the chip.
+	 * @param name Name of the GPIO line to map.
+	 * @return Offset of the line within the chip or -1 if a line with
+	 *         given name is not exposed by the chip.
+	 */
+	GPIOD_API int find_line(const ::std::string& name) const;
 
 	/**
 	 * @brief Get a set of lines exposed by this chip at given offsets.
bindings/cxx/tests/tests-chip.cpp +3 -3
index a84b150..aea00fa 100644
@@ -113,8 +113,8 @@ TEST_CASE("Lines can be retrieved from chip objects", "[chip]")
 
 	SECTION("find single line by name")
 	{
-		auto line = chip.find_line("gpio-mockup-B-3", true).front();
-		REQUIRE(line.offset() == 3);
+		auto offset = chip.find_line("gpio-mockup-B-3");
+		REQUIRE(offset == 3);
 	}
 
 	SECTION("get multiple lines by offsets")
@@ -168,6 +168,6 @@ TEST_CASE("Errors occurring when retrieving lines are correctly reported", "[chi
 
 	SECTION("line not found by name")
 	{
-		REQUIRE(chip.find_line("nonexistent-line").empty());
+		REQUIRE(chip.find_line("nonexistent-line") == -1);
 	}
 }
bindings/python/examples/gpiofind.py +3 -4
index 117d583..a9ec734 100755
@@ -12,10 +12,9 @@ if __name__ == '__main__':
     for entry in os.scandir('/dev/'):
         if gpiod.is_gpiochip_device(entry.path):
             with gpiod.Chip(entry.path) as chip:
-                lines = chip.find_line(sys.argv[1], unique=True)
-                if lines is not None:
-                     line = lines.to_list()[0]
-                     print('{} {}'.format(line.owner().name(), line.offset()))
+                offset = chip.find_line(sys.argv[1], unique=True)
+                if offset is not None:
+                     print('{} {}'.format(line.owner().name(), offset))
                      sys.exit(0)
 
     sys.exit(1)
bindings/python/gpiodmodule.c +16 -35

83 lines not on this page · raw message

bindings/python/tests/gpiod_py_test.py +5 -4
index f264db3..c8ee85b 100755
@@ -136,8 +136,9 @@ class ChipGetLines(MockupTestCase):
 
     def test_find_single_line_by_name(self):
         with gpiod.Chip(mockup.chip_path(1)) as chip:
-            lines = chip.find_line('gpio-mockup-B-4', unique=True).to_list()
-            self.assertEqual(lines[0].offset(), 4)
+            offset = chip.find_line('gpio-mockup-B-4')
+            self.assertIsNotNone(offset)
+            self.assertEqual(offset, 4)
 
     def test_get_single_line_invalid_offset(self):
         with gpiod.Chip(mockup.chip_path(1)) as chip:
@@ -148,8 +149,8 @@ class ChipGetLines(MockupTestCase):
 
     def test_find_single_line_nonexistent(self):
         with gpiod.Chip(mockup.chip_path(1)) as chip:
-            lines = chip.find_line('nonexistent-line', unique=True)
-            self.assertEqual(lines, None)
+            offset = chip.find_line('nonexistent-line')
+            self.assertIsNone(offset)
 
     def test_get_multiple_lines_by_offsets_in_tuple(self):
         with gpiod.Chip(mockup.chip_path(1)) as chip:
include/gpiod.h +5 -23
index 76aa1dd..6ed9c6c 100644
@@ -136,31 +136,13 @@ struct gpiod_line_bulk *
 gpiod_chip_get_all_lines(struct gpiod_chip *chip) GPIOD_API;
 
 /**
- * @brief Find all GPIO lines by name among lines exposed by this GPIO chip.
+ * @brief Map a GPIO line's name to its offset within the chip.
  * @param chip The GPIO chip object.
- * @param name GPIO line name to look for.
- * @return New line bulk object containing all matching lines or NULL on error.
- *
- * If no line with given name is associated with this chip, the function sets
- * errno to ENOENT.
- */
-struct gpiod_line_bulk *
-gpiod_chip_find_line(struct gpiod_chip *chip, const char *name) GPIOD_API;
-
-/**
- * @brief Find a unique line by name among lines exposed by this GPIO chip.
- * @param chip The GPIO chip object.
- * @param name Name of the GPIO line.
- * @return Pointer to the GPIO line handle or NULL if the line could not be
- *         found or an error occurred.
- *
- * If no line with given name is associated with this chip, the function sets
- * errno to ENOENT. If more than one line with given name is associated with
- * this chip, the function sets errno to ERANGE.
+ * @param name Name of the GPIO line to map.
+ * @return Offset of the line within the chip or -1 if a line with given name
+ *         is not exposed by the chip.
  */
-struct gpiod_line *
-gpiod_chip_find_line_unique(struct gpiod_chip *chip,
-			    const char *name) GPIOD_API;
+int gpiod_chip_find_line(struct gpiod_chip *chip, const char *name) GPIOD_API;
 
 /**
  * @}
lib/helpers.c +5 -52

77 lines not on this page · raw message

tests/tests-chip.c +11 -6
index 928dc49..a87dc9a 100644
@@ -190,19 +190,24 @@ GPIOD_TEST_CASE(get_all_lines, 0, { 4 })
 	g_assert_cmpuint(gpiod_line_offset(line3), ==, 3);
 }
 
-GPIOD_TEST_CASE(find_line_good_unique, GPIOD_TEST_FLAG_NAMED_LINES, { 8, 8, 8 })
+GPIOD_TEST_CASE(find_line_good, GPIOD_TEST_FLAG_NAMED_LINES, { 8, 8, 8 })
 {
 	g_autoptr(gpiod_chip_struct) chip = NULL;
 	struct gpiod_line *line;
+	int offset;
 
 	chip = gpiod_chip_open(gpiod_test_chip_path(1));
 	g_assert_nonnull(chip);
 	gpiod_test_return_if_failed();
 
-	line = gpiod_chip_find_line_unique(chip, "gpio-mockup-B-4");
+	offset = gpiod_chip_find_line(chip, "gpio-mockup-B-4");
+	g_assert_cmpint(offset, ==, 4);
+	gpiod_test_return_if_failed();
+
+	line = gpiod_chip_get_line(chip, 4);
 	g_assert_nonnull(line);
 	gpiod_test_return_if_failed();
-	g_assert_cmpuint(gpiod_line_offset(line), ==, 4);
+
 	g_assert_cmpstr(gpiod_line_name(line), ==, "gpio-mockup-B-4");
 }
 
@@ -210,13 +215,13 @@ GPIOD_TEST_CASE(find_line_unique_not_found,
 		GPIOD_TEST_FLAG_NAMED_LINES, { 8, 8, 8 })
 {
 	g_autoptr(gpiod_chip_struct) chip = NULL;
-	struct gpiod_line *line;
+	int offset;
 
 	chip = gpiod_chip_open(gpiod_test_chip_path(1));
 	g_assert_nonnull(chip);
 	gpiod_test_return_if_failed();
 
-	line = gpiod_chip_find_line_unique(chip, "nonexistent");
-	g_assert_null(line);
+	offset = gpiod_chip_find_line(chip, "nonexistent");
+	g_assert_cmpint(offset, ==, -1);
 	g_assert_cmpint(errno, ==, ENOENT);
 }
tools/gpiofind.c +4 -5
index 4936c4f..0fc07d9 100644
@@ -31,9 +31,8 @@ static void print_help(void)
 
 int main(int argc, char **argv)
 {
-	int i, num_chips, optc, opti;
+	int i, num_chips, optc, opti, offset;
 	struct gpiod_chip *chip;
-	struct gpiod_line *line;
 	struct dirent **entries;
 
 	for (;;) {
@@ -74,10 +73,10 @@ int main(int argc, char **argv)
 			die_perror("unable to open %s", entries[i]->d_name);
 		}
 
-		line = gpiod_chip_find_line_unique(chip, argv[0]);
-		if (line) {
+		offset = gpiod_chip_find_line(chip, argv[0]);
+		if (offset >= 0) {
 			printf("%s %u\n",
-			       gpiod_chip_name(chip), gpiod_line_offset(line));
+			       gpiod_chip_name(chip), offset);
 			gpiod_chip_close(chip);
 			return EXIT_SUCCESS;
 		}
Signature · 1 line
2.30.1
#2 lore raw

PATCH 2/6 tests: remove line bulk test cases

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Together with the removal of line objects, we'll entirely drop the line bulk concept. In order to avoid having to update tests that will soon be unneeded when introducing further changes, let's remove line bulk test cases already.

---
 bindings/cxx/tests/tests-line.cpp      |  43 --------
 bindings/python/tests/gpiod_py_test.py |  15 ---
 tests/Makefile.am                      |   1 -
 tests/tests-bulk.c                     | 145 -------------------------
 4 files changed, 204 deletions(-)
 delete mode 100644 tests/tests-bulk.c
bindings/cxx/tests/tests-line.cpp +0 -43
index c1ad8d2..648012a 100644
@@ -153,49 +153,6 @@ TEST_CASE("Line information can be correctly retrieved", "[line]")
 	}
 }
 
-TEST_CASE("Line bulk object works correctly", "[line][bulk]")
-{
-	mockup::probe_guard mockup_chips({ 8 }, mockup::FLAG_NAMED_LINES);
-	::gpiod::chip chip(mockup::instance().chip_path(0));
-
-	SECTION("lines can be added, accessed and cleared")
-	{
-		::gpiod::line_bulk lines;
-
-		REQUIRE(lines.empty());
-
-		lines.append(chip.get_line(0));
-		lines.append(chip.get_line(1));
-		lines.append(chip.get_line(2));
-
-		REQUIRE(lines.size() == 3);
-		REQUIRE(lines.get(1).name() == "gpio-mockup-A-1");
-		REQUIRE(lines[2].name() == "gpio-mockup-A-2");
-
-		lines.clear();
-
-		REQUIRE(lines.empty());
-	}
-
-	SECTION("bulk iterator works")
-	{
-		auto lines = chip.get_all_lines();
-		unsigned int count = 0;
-
-		for (auto& it: lines)
-			REQUIRE(it.offset() == count++);
-
-		REQUIRE(count == chip.num_lines());
-	}
-
-	SECTION("accessing lines out of range throws exception")
-	{
-		auto lines = chip.get_all_lines();
-
-		REQUIRE_THROWS_AS(lines.get(11), ::std::out_of_range);
-	}
-}
-
 TEST_CASE("Line values can be set and read", "[line]")
 {
 	mockup::probe_guard mockup_chips({ 8 });
bindings/python/tests/gpiod_py_test.py +0 -15
index c8ee85b..b7c30de 100755
@@ -670,21 +670,6 @@ class LineIterator(MockupTestCase):
 
             self.assertEqual(count, chip.num_lines())
 
-class LineBulkIter(MockupTestCase):
-
-    chip_sizes = ( 4, )
-
-    def test_line_bulk_iterator(self):
-        with gpiod.Chip(mockup.chip_path(0)) as chip:
-            lines = chip.get_all_lines()
-            count = 0
-
-            for line in lines:
-                self.assertEqual(line.offset(), count)
-                count += 1
-
-            self.assertEqual(count, chip.num_lines())
-
 #
 # Event test cases
 #
tests/Makefile.am +0 -1
index 49aa215..43b215e 100644
@@ -17,7 +17,6 @@ bin_PROGRAMS = gpiod-test
 gpiod_test_SOURCES =			\
 		gpiod-test.c		\
 		gpiod-test.h		\
-		tests-bulk.c		\
 		tests-chip.c		\
 		tests-event.c		\
 		tests-line.c		\
tests/tests-bulk.c deleted +0 -145

145 lines not on this page · raw message

Signature · 1 line
2.30.1
#3 lore raw

PATCH 3/6 core: switch to reference counting for gpio chip objects

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

The preferred approach in low-level system libraries is to make all exposed data structures opaque and use reference counting for their memory management. This changes the chip objects to only close their underlying character device and release all resources once the reference count goes down to 0. We remove the gpiod_chip_close() function and replace it with gpiod_chip_ref() and gpiod_chip_unref().

Other objects in the API will either be removed or are not opaque yet and will be reworked later.

---
 bindings/cxx/chip.cpp         |  2 +-
 bindings/python/gpiodmodule.c |  4 ++--
 include/gpiod.h               | 12 ++++++++++--
 lib/core.c                    | 15 ++++++++++++++-
 tests/gpiod-test.h            |  2 +-
 tools/gpiodetect.c            |  2 +-
 tools/gpiofind.c              |  2 +-
 tools/gpioget.c               |  2 +-
 tools/gpioinfo.c              |  4 ++--
 tools/gpiomon.c               |  2 +-
 tools/gpioset.c               |  2 +-
 11 files changed, 35 insertions(+), 14 deletions(-)
bindings/cxx/chip.cpp +1 -1
index 527fb34..0886717 100644
@@ -13,7 +13,7 @@ namespace {
 
 void chip_deleter(::gpiod_chip* chip)
 {
-	::gpiod_chip_close(chip);
+	::gpiod_chip_unref(chip);
 }
 
 } /* namespace */
bindings/python/gpiodmodule.c +2 -2
index ac0fd83..e54c3ad 100644
@@ -1953,7 +1953,7 @@ static int gpiod_Chip_init(gpiod_ChipObject *self,
 static void gpiod_Chip_dealloc(gpiod_ChipObject *self)
 {
 	if (self->chip)
-		gpiod_chip_close(self->chip);
+		gpiod_chip_unref(self->chip);
 
 	PyObject_Del(self);
 }
@@ -1981,7 +1981,7 @@ static PyObject *gpiod_Chip_close(gpiod_ChipObject *self,
 	if (gpiod_ChipIsClosed(self))
 		return NULL;
 
-	gpiod_chip_close(self->chip);
+	gpiod_chip_unref(self->chip);
 	self->chip = NULL;
 
 	Py_RETURN_NONE;
include/gpiod.h +10 -2
index 6ed9c6c..71abb2a 100644
@@ -81,10 +81,18 @@ bool gpiod_is_gpiochip_device(const char *path) GPIOD_API;
 struct gpiod_chip *gpiod_chip_open(const char *path) GPIOD_API;
 
 /**
- * @brief Close a GPIO chip handle and release all allocated resources.
+ * @brief Increase the refcount on this GPIO object.
  * @param chip The GPIO chip object.
+ * @return Passed reference to the GPIO chip.
  */
-void gpiod_chip_close(struct gpiod_chip *chip) GPIOD_API;
+struct gpiod_chip *gpiod_chip_ref(struct gpiod_chip *chip) GPIOD_API;
+
+/**
+ * @brief Decrease the refcount on this GPIO object. If the refcount reaches 0,
+ *        close the chip device and free all associated resources.
+ * @param chip The GPIO chip object.
+ */
+void gpiod_chip_unref(struct gpiod_chip *chip) GPIOD_API;
 
 /**
  * @brief Get the GPIO chip name as represented in the kernel.
lib/core.c +14 -1
index 077ea3e..bab438f 100644
@@ -64,6 +64,8 @@ struct gpiod_line {
 };
 
 struct gpiod_chip {
+	int refcount;
+
 	struct gpiod_line **lines;
 	unsigned int num_lines;
 
@@ -297,6 +299,7 @@ struct gpiod_chip *gpiod_chip_open(const char *path)
 
 	chip->fd = fd;
 	chip->num_lines = info.lines;
+	chip->refcount = 1;
 
 	/*
 	 * GPIO device must have a name - don't bother checking this field. In
@@ -324,11 +327,21 @@ err_close_fd:
 	return NULL;
 }
 
-void gpiod_chip_close(struct gpiod_chip *chip)
+struct gpiod_chip *gpiod_chip_ref(struct gpiod_chip *chip)
+{
+	chip->refcount++;
+	return chip;
+}
+
+void gpiod_chip_unref(struct gpiod_chip *chip)
 {
 	struct gpiod_line *line;
 	unsigned int i;
 
+	chip->refcount--;
+	if (chip->refcount > 0)
+		return;
+
 	if (chip->lines) {
 		for (i = 0; i < chip->num_lines; i++) {
 			line = chip->lines[i];
tests/gpiod-test.h +1 -1
index 2688d3c..a093f83 100644
@@ -22,7 +22,7 @@
 typedef struct gpiod_chip gpiod_chip_struct;
 typedef struct gpiod_line_bulk gpiod_line_bulk_struct;
 
-G_DEFINE_AUTOPTR_CLEANUP_FUNC(gpiod_chip_struct, gpiod_chip_close);
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(gpiod_chip_struct, gpiod_chip_unref);
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(gpiod_line_bulk_struct, gpiod_line_bulk_free);
 
 /* These are private definitions and should not be used directly. */
tools/gpiodetect.c +1 -1
index 7a2015e..9f4c28c 100644
@@ -80,7 +80,7 @@ int main(int argc, char **argv)
 		       gpiod_chip_label(chip),
 		       gpiod_chip_num_lines(chip));
 
-		gpiod_chip_close(chip);
+		gpiod_chip_unref(chip);
 		free(entries[i]);
 	}
 
tools/gpiofind.c +1 -1
index 0fc07d9..83af76b 100644
@@ -77,7 +77,7 @@ int main(int argc, char **argv)
 		if (offset >= 0) {
 			printf("%s %u\n",
 			       gpiod_chip_name(chip), offset);
-			gpiod_chip_close(chip);
+			gpiod_chip_unref(chip);
 			return EXIT_SUCCESS;
 		}
 	}
tools/gpioget.c +1 -1
index 527dc22..ceeec56 100644
@@ -123,7 +123,7 @@ int main(int argc, char **argv)
 	printf("\n");
 
 	gpiod_line_release_bulk(lines);
-	gpiod_chip_close(chip);
+	gpiod_chip_unref(chip);
 	gpiod_line_bulk_free(lines);
 	free(values);
 	free(offsets);
tools/gpioinfo.c +2 -2
index 84588bc..eba8c72 100644
@@ -230,7 +230,7 @@ int main(int argc, char **argv)
 
 			list_lines(chip);
 
-			gpiod_chip_close(chip);
+			gpiod_chip_unref(chip);
 		}
 	} else {
 		for (i = 0; i < argc; i++) {
@@ -240,7 +240,7 @@ int main(int argc, char **argv)
 
 			list_lines(chip);
 
-			gpiod_chip_close(chip);
+			gpiod_chip_unref(chip);
 		}
 	}
 
tools/gpiomon.c +1 -1
index 8bf2c70..dda9f6f 100644
@@ -306,7 +306,7 @@ done:
 	gpiod_line_release_bulk(lines);
 	gpiod_line_bulk_free(lines);
 	gpiod_line_bulk_free(evlines);
-	gpiod_chip_close(chip);
+	gpiod_chip_unref(chip);
 
 	return EXIT_SUCCESS;
 }
tools/gpioset.c +1 -1
index 7e9d88f..a088ec6 100644
@@ -305,7 +305,7 @@ int main(int argc, char **argv)
 	mode->callback(&cbdata);
 
 	gpiod_line_release_bulk(lines);
-	gpiod_chip_close(chip);
+	gpiod_chip_unref(chip);
 	gpiod_line_bulk_free(lines);
 	free(offsets);
 	free(values);
Signature · 1 line
2.30.1
#4 lore raw

PATCH 4/6 treewide: remove is_requested() and is_free()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

This removes another two functions from the C API as well as their bindings that don't make much sense (since the user should know if they have requested the line or not).

---
 bindings/cxx/gpiod.hpp                 |  6 -----
 bindings/cxx/line.cpp                  |  8 ------
 bindings/cxx/tests/tests-line.cpp      |  9 -------
 bindings/python/gpiodmodule.c          | 23 ----------------
 bindings/python/tests/gpiod_py_test.py | 20 --------------
 include/gpiod.h                        | 15 -----------
 lib/core.c                             | 37 +++++---------------------
 7 files changed, 7 insertions(+), 111 deletions(-)
bindings/cxx/gpiod.hpp +0 -6
index 3a043a1..189a133 100644
@@ -342,12 +342,6 @@ public:
 	 */
 	GPIOD_API void release(void) const;
 
-	/**
-	 * @brief Check if this user has ownership of this line.
-	 * @return True if the user has ownership of this line, false otherwise.
-	 */
-	GPIOD_API bool is_requested(void) const;
-
 	/**
 	 * @brief Read the line value.
 	 * @return Current value (0 or 1).
bindings/cxx/line.cpp +0 -8
index 1d114ac..058f7ce 100644
@@ -127,14 +127,6 @@ void line::release(void) const
 	bulk.release();
 }
 
-bool line::is_requested(void) const
-{
-	this->throw_if_null();
-	line::chip_guard lock_chip(*this);
-
-	return ::gpiod_line_is_requested(this->_m_line);
-}
-
 /*
  * REVISIT: Check the performance of get/set_value & event_wait compared to
  * the C API. Creating a line_bulk object involves a memory allocation every
bindings/cxx/tests/tests-line.cpp +0 -9
index 648012a..17fdd89 100644
@@ -27,7 +27,6 @@ TEST_CASE("Line information can be correctly retrieved", "[line]")
 		REQUIRE(line.direction() == ::gpiod::line::DIRECTION_INPUT);
 		REQUIRE_FALSE(line.is_active_low());
 		REQUIRE(line.consumer().empty());
-		REQUIRE_FALSE(line.is_requested());
 		REQUIRE_FALSE(line.is_used());
 		REQUIRE(line.drive() == ::gpiod::line::DRIVE_PUSH_PULL);
 		REQUIRE(line.bias() == ::gpiod::line::BIAS_UNKNOWN);
@@ -45,7 +44,6 @@ TEST_CASE("Line information can be correctly retrieved", "[line]")
 		REQUIRE(line.name() == "gpio-mockup-A-4");
 		REQUIRE(line.direction() == ::gpiod::line::DIRECTION_OUTPUT);
 		REQUIRE_FALSE(line.is_active_low());
-		REQUIRE(line.is_requested());
 		REQUIRE(line.is_used());
 		REQUIRE(line.drive() == ::gpiod::line::DRIVE_PUSH_PULL);
 		REQUIRE(line.bias() == ::gpiod::line::BIAS_UNKNOWN);
@@ -65,7 +63,6 @@ TEST_CASE("Line information can be correctly retrieved", "[line]")
 		REQUIRE(line.name() == "gpio-mockup-A-4");
 		REQUIRE(line.direction() == ::gpiod::line::DIRECTION_OUTPUT);
 		REQUIRE(line.is_active_low());
-		REQUIRE(line.is_requested());
 		REQUIRE(line.is_used());
 		REQUIRE(line.drive() == ::gpiod::line::DRIVE_OPEN_DRAIN);
 		REQUIRE(line.bias() == ::gpiod::line::BIAS_UNKNOWN);
@@ -84,7 +81,6 @@ TEST_CASE("Line information can be correctly retrieved", "[line]")
 		REQUIRE(line.name() == "gpio-mockup-A-4");
 		REQUIRE(line.direction() == ::gpiod::line::DIRECTION_OUTPUT);
 		REQUIRE_FALSE(line.is_active_low());
-		REQUIRE(line.is_requested());
 		REQUIRE(line.is_used());
 		REQUIRE(line.drive() == ::gpiod::line::DRIVE_OPEN_SOURCE);
 		REQUIRE(line.bias() == ::gpiod::line::BIAS_UNKNOWN);
@@ -103,7 +99,6 @@ TEST_CASE("Line information can be correctly retrieved", "[line]")
 		REQUIRE(line.name() == "gpio-mockup-A-4");
 		REQUIRE(line.direction() == ::gpiod::line::DIRECTION_OUTPUT);
 		REQUIRE_FALSE(line.is_active_low());
-		REQUIRE(line.is_requested());
 		REQUIRE(line.is_used());
 		REQUIRE(line.drive() == ::gpiod::line::DRIVE_PUSH_PULL);
 		REQUIRE(line.bias() == ::gpiod::line::BIAS_DISABLED);
@@ -122,7 +117,6 @@ TEST_CASE("Line information can be correctly retrieved", "[line]")
 		REQUIRE(line.name() == "gpio-mockup-A-4");
 		REQUIRE(line.direction() == ::gpiod::line::DIRECTION_OUTPUT);
 		REQUIRE_FALSE(line.is_active_low());;
-		REQUIRE(line.is_requested());
 		REQUIRE(line.is_used());
 		REQUIRE(line.drive() == ::gpiod::line::DRIVE_PUSH_PULL);
 		REQUIRE(line.bias() == ::gpiod::line::BIAS_PULL_DOWN);
@@ -141,7 +135,6 @@ TEST_CASE("Line information can be correctly retrieved", "[line]")
 		REQUIRE(line.name() == "gpio-mockup-A-4");
 		REQUIRE(line.direction() == ::gpiod::line::DIRECTION_OUTPUT);
 		REQUIRE_FALSE(line.is_active_low());
-		REQUIRE(line.is_requested());
 		REQUIRE(line.is_used());
 		REQUIRE(line.drive() == ::gpiod::line::DRIVE_PUSH_PULL);
 		REQUIRE(line.bias() == ::gpiod::line::BIAS_PULL_UP);
@@ -383,12 +376,10 @@ TEST_CASE("Exported line can be released", "[line]")
 
 	line.request(config);
 
-	REQUIRE(line.is_requested());
 	REQUIRE(line.get_value() == 0);
 
 	line.release();
 
-	REQUIRE_FALSE(line.is_requested());
 	REQUIRE_THROWS_AS(line.get_value(), ::std::system_error);
 }
 
bindings/python/gpiodmodule.c +0 -23
index e54c3ad..12a6867 100644
@@ -501,23 +501,6 @@ static PyObject *gpiod_Line_request(gpiod_LineObject *self,
 	return ret;
 }
 
-PyDoc_STRVAR(gpiod_Line_is_requested_doc,
-"is_requested() -> boolean\n"
-"\n"
-"Check if this user has ownership of this line.");
-
-static PyObject *gpiod_Line_is_requested(gpiod_LineObject *self,
-					 PyObject *Py_UNUSED(ignored))
-{
-	if (gpiod_ChipIsClosed(self->owner))
-		return NULL;
-
-	if (gpiod_line_is_requested(self->line))
-		Py_RETURN_TRUE;
-
-	Py_RETURN_FALSE;
-}
-
 PyDoc_STRVAR(gpiod_Line_get_value_doc,
 "get_value() -> integer\n"
 "\n"
@@ -983,12 +966,6 @@ static PyMethodDef gpiod_Line_methods[] = {
 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
 		.ml_doc = gpiod_Line_request_doc,
 	},
-	{
-		.ml_name = "is_requested",
-		.ml_meth = (PyCFunction)gpiod_Line_is_requested,
-		.ml_flags = METH_NOARGS,
-		.ml_doc = gpiod_Line_is_requested_doc,
-	},
 	{
 		.ml_name = "get_value",
 		.ml_meth = (PyCFunction)gpiod_Line_get_value,
bindings/python/tests/gpiod_py_test.py +0 -20
index b7c30de..d448776 100755
@@ -204,7 +204,6 @@ class LineInfo(MockupTestCase):
             self.assertFalse(line.is_active_low())
             self.assertEqual(line.consumer(), None)
             self.assertFalse(line.is_used())
-            self.assertFalse(line.is_requested())
 
     def test_exported_line(self):
         with gpiod.Chip(mockup.chip_path(0)) as chip:
@@ -218,7 +217,6 @@ class LineInfo(MockupTestCase):
             self.assertTrue(line.is_active_low())
             self.assertEqual(line.consumer(), default_consumer)
             self.assertTrue(line.is_used())
-            self.assertTrue(line.is_requested())
 
     def test_exported_line_with_flags(self):
         with gpiod.Chip(mockup.chip_path(0)) as chip:
@@ -234,7 +232,6 @@ class LineInfo(MockupTestCase):
             self.assertTrue(line.is_active_low())
             self.assertEqual(line.consumer(), default_consumer)
             self.assertTrue(line.is_used())
-            self.assertTrue(line.is_requested())
             self.assertEqual(line.drive(), gpiod.Line.DRIVE_OPEN_DRAIN)
             self.assertEqual(line.bias(), gpiod.Line.BIAS_UNKNOWN)
 
@@ -251,7 +248,6 @@ class LineInfo(MockupTestCase):
             self.assertFalse(line.is_active_low())
             self.assertEqual(line.consumer(), default_consumer)
             self.assertTrue(line.is_used())
-            self.assertTrue(line.is_requested())
             self.assertEqual(line.drive(), gpiod.Line.DRIVE_OPEN_DRAIN)
             self.assertEqual(line.bias(), gpiod.Line.BIAS_UNKNOWN)
 
@@ -268,7 +264,6 @@ class LineInfo(MockupTestCase):
             self.assertFalse(line.is_active_low())
             self.assertEqual(line.consumer(), default_consumer)
             self.assertTrue(line.is_used())
-            self.assertTrue(line.is_requested())
             self.assertEqual(line.drive(), gpiod.Line.DRIVE_OPEN_SOURCE)
             self.assertEqual(line.bias(), gpiod.Line.BIAS_UNKNOWN)
 
@@ -285,7 +280,6 @@ class LineInfo(MockupTestCase):
             self.assertFalse(line.is_active_low())
             self.assertEqual(line.consumer(), default_consumer)
             self.assertTrue(line.is_used())
-            self.assertTrue(line.is_requested())
             self.assertEqual(line.drive(), gpiod.Line.DRIVE_PUSH_PULL)
             self.assertEqual(line.bias(), gpiod.Line.BIAS_DISABLED)
 
@@ -302,7 +296,6 @@ class LineInfo(MockupTestCase):
             self.assertFalse(line.is_active_low())
             self.assertEqual(line.consumer(), default_consumer)
             self.assertTrue(line.is_used())
-            self.assertTrue(line.is_requested())
             self.assertEqual(line.drive(), gpiod.Line.DRIVE_PUSH_PULL)
             self.assertEqual(line.bias(), gpiod.Line.BIAS_PULL_DOWN)
 
@@ -319,7 +312,6 @@ class LineInfo(MockupTestCase):
             self.assertFalse(line.is_active_low())
             self.assertEqual(line.consumer(), default_consumer)
             self.assertTrue(line.is_used())
-            self.assertTrue(line.is_requested())
             self.assertEqual(line.drive(), gpiod.Line.DRIVE_PUSH_PULL)
             self.assertEqual(line.bias(), gpiod.Line.BIAS_PULL_UP)
 
@@ -605,16 +597,6 @@ class LineRequestBehavior(MockupTestCase):
 
     chip_sizes = ( 8, )
 
-    def test_line_export_release(self):
-        with gpiod.Chip(mockup.chip_path(0)) as chip:
-            line = chip.get_line(3)
-            line.request(consumer=default_consumer,
-                         type=gpiod.LINE_REQ_DIR_IN)
-            self.assertTrue(line.is_requested())
-            self.assertEqual(line.get_value(), 0)
-            line.release()
-            self.assertFalse(line.is_requested())
-
     def test_line_request_twice_two_calls(self):
         with gpiod.Chip(mockup.chip_path(0)) as chip:
             line = chip.get_line(3)
@@ -647,10 +629,8 @@ class LineRequestBehavior(MockupTestCase):
         with gpiod.Chip(mockup.chip_path(0)) as chip:
             line = chip.get_line(2)
             line.request(default_consumer)
-            self.assertTrue(line.is_requested())
             self.assertEqual(line.direction(), gpiod.Line.DIRECTION_INPUT)
             line.release()
-            self.assertFalse(line.is_requested())
 
 #
 # Iterator test cases
include/gpiod.h +0 -15
index 71abb2a..a5e09e2 100644
@@ -700,21 +700,6 @@ void gpiod_line_release(struct gpiod_line *line) GPIOD_API;
  */
 void gpiod_line_release_bulk(struct gpiod_line_bulk *bulk) GPIOD_API;
 
-/**
- * @brief Check if the calling user has ownership of this line.
- * @param line GPIO line object.
- * @return True if given line was requested, false otherwise.
- */
-bool gpiod_line_is_requested(struct gpiod_line *line) GPIOD_API;
-
-/**
- * @brief Check if the calling user has neither requested ownership of this
- *        line nor configured any event notifications.
- * @param line GPIO line object.
- * @return True if given line is free, false otherwise.
- */
-bool gpiod_line_is_free(struct gpiod_line *line) GPIOD_API;
-
 /**
  * @}
  *
lib/core.c +7 -30
index bab438f..8e3d8a7 100644
@@ -561,13 +561,19 @@ int gpiod_line_update(struct gpiod_line *line)
 	return 0;
 }
 
+static bool line_is_requested(struct gpiod_line *line)
+{
+	return (line->state == LINE_REQUESTED_VALUES ||
+		line->state == LINE_REQUESTED_EVENTS);
+}
+
 static bool line_bulk_all_requested(struct gpiod_line_bulk *bulk)
 {
 	struct gpiod_line *line;
 	unsigned int idx;
 
 	line_bulk_foreach_line(bulk, line, idx) {
-		if (!gpiod_line_is_requested(line)) {
+		if (!line_is_requested(line)) {
 			errno = EPERM;
 			return false;
 		}
@@ -591,21 +597,6 @@ static bool line_bulk_all_requested_values(struct gpiod_line_bulk *bulk)
 	return true;
 }
 
-static bool line_bulk_all_free(struct gpiod_line_bulk *bulk)
-{
-	struct gpiod_line *line;
-	unsigned int idx;
-
-	line_bulk_foreach_line(bulk, line, idx) {
-		if (!gpiod_line_is_free(line)) {
-			errno = EBUSY;
-			return false;
-		}
-	}
-
-	return true;
-}
-
 static bool line_request_direction_is_valid(int direction)
 {
 	if ((direction == GPIOD_LINE_REQUEST_DIRECTION_AS_IS) ||
@@ -872,9 +863,6 @@ int gpiod_line_request_bulk(struct gpiod_line_bulk *bulk,
 			    const struct gpiod_line_request_config *config,
 			    const int *vals)
 {
-	if (!line_bulk_all_free(bulk))
-		return -1;
-
 	if (line_request_is_direction(config->request_type))
 		return line_request_values(bulk, config, vals);
 	else if (line_request_is_events(config->request_type))
@@ -904,17 +892,6 @@ void gpiod_line_release_bulk(struct gpiod_line_bulk *bulk)
 	}
 }
 
-bool gpiod_line_is_requested(struct gpiod_line *line)
-{
-	return (line->state == LINE_REQUESTED_VALUES ||
-		line->state == LINE_REQUESTED_EVENTS);
-}
-
-bool gpiod_line_is_free(struct gpiod_line *line)
-{
-	return line->state == LINE_FREE;
-}
-
 int gpiod_line_get_value(struct gpiod_line *line)
 {
 	struct gpiod_line_bulk bulk = BULK_SINGLE_LINE_INIT(line);
Signature · 1 line
2.30.1
#5 lore raw

PATCH 5/6 treewide: kill line updating

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

This removes any trace of line updating from the API (C, C++ and Python). The line objects will soon disappear entirely so in order to make the gradual transition to the new data model easier, remove gpiod_line_update().

---
 bindings/cxx/gpiod.hpp                 |  5 -----
 bindings/cxx/line.cpp                  | 12 ------------
 bindings/cxx/tests/tests-line.cpp      |  5 -----
 bindings/python/gpiodmodule.c          | 23 -----------------------
 bindings/python/tests/gpiod_py_test.py |  5 -----
 include/gpiod.h                        | 22 ----------------------
 lib/core.c                             | 12 +++++++-----
 7 files changed, 7 insertions(+), 77 deletions(-)
bindings/cxx/gpiod.hpp +0 -5
index 189a133..003acdd 100644
@@ -412,11 +412,6 @@ public:
 	 */
 	GPIOD_API const chip get_chip(void) const;
 
-	/**
-	 * @brief Re-read the line info from the kernel.
-	 */
-	GPIOD_API void update(void) const;
-
 	/**
 	 * @brief Reset the state of this object.
 	 *
bindings/cxx/line.cpp +0 -12
index 058f7ce..bf84867 100644
@@ -278,18 +278,6 @@ const chip line::get_chip(void) const
 	return chip(this->_m_owner);
 }
 
-void line::update(void) const
-{
-	this->throw_if_null();
-	line::chip_guard lock_chip(*this);
-
-	int ret = ::gpiod_line_update(this->_m_line);
-
-	if (ret < 0)
-		throw ::std::system_error(errno, ::std::system_category(),
-					  "unable to update the line info");
-}
-
 void line::reset(void)
 {
 	this->_m_line = nullptr;
bindings/cxx/tests/tests-line.cpp +0 -5
index 17fdd89..ababf8b 100644
@@ -139,11 +139,6 @@ TEST_CASE("Line information can be correctly retrieved", "[line]")
 		REQUIRE(line.drive() == ::gpiod::line::DRIVE_PUSH_PULL);
 		REQUIRE(line.bias() == ::gpiod::line::BIAS_PULL_UP);
 	}
-
-	SECTION("update line info")
-	{
-		REQUIRE_NOTHROW(line.update());
-	}
 }
 
 TEST_CASE("Line values can be set and read", "[line]")
bindings/python/gpiodmodule.c +0 -23
index 12a6867..8bfb4c4 100644
@@ -721,23 +721,6 @@ static PyObject *gpiod_Line_release(gpiod_LineObject *self,
 	return ret;
 }
 
-PyDoc_STRVAR(gpiod_Line_update_doc,
-"update() -> None\n"
-"\n"
-"Re-read the line information from the kernel.");
-
-static PyObject *gpiod_Line_update(gpiod_LineObject *self,
-				   PyObject *Py_UNUSED(ignored))
-{
-	int ret;
-
-	ret = gpiod_line_update(self->line);
-	if (ret)
-		return PyErr_SetFromErrno(PyExc_OSError);
-
-	Py_RETURN_NONE;
-}
-
 PyDoc_STRVAR(gpiod_Line_event_wait_doc,
 "event_wait([sec[ ,nsec]]) -> boolean\n"
 "\n"
@@ -1008,12 +991,6 @@ static PyMethodDef gpiod_Line_methods[] = {
 		.ml_flags = METH_NOARGS,
 		.ml_doc = gpiod_Line_release_doc,
 	},
-	{
-		.ml_name = "update",
-		.ml_meth = (PyCFunction)gpiod_Line_update,
-		.ml_flags = METH_NOARGS,
-		.ml_doc = gpiod_Line_update_doc,
-	},
 	{
 		.ml_name = "event_wait",
 		.ml_meth = (PyCFunction)(void (*)(void))gpiod_Line_event_wait,
bindings/python/tests/gpiod_py_test.py +0 -5
index d448776..f93c72c 100755
@@ -315,11 +315,6 @@ class LineInfo(MockupTestCase):
             self.assertEqual(line.drive(), gpiod.Line.DRIVE_PUSH_PULL)
             self.assertEqual(line.bias(), gpiod.Line.BIAS_PULL_UP)
 
-    def test_update_line_info(self):
-        with gpiod.Chip(mockup.chip_path(0)) as chip:
-            line = chip.get_line(3)
-            line.update()
-
 class LineValues(MockupTestCase):
 
     chip_sizes = ( 8, )
include/gpiod.h +0 -22
index a5e09e2..0ee9778 100644
@@ -358,28 +358,6 @@ bool gpiod_line_is_used(struct gpiod_line *line) GPIOD_API;
  */
 int gpiod_line_drive(struct gpiod_line *line) GPIOD_API;
 
-/**
- * @brief Re-read the line info.
- * @param line GPIO line object.
- * @return 0 if the operation succeeds. In case of an error this routine
- *         returns -1 and sets the last error number.
- *
- * The line info is initially retrieved from the kernel by
- * gpiod_chip_get_line() and is later re-read after every successful request.
- * Users can use this function to manually re-read the line info when needed.
- *
- * We currently have no mechanism provided by the kernel for keeping the line
- * info synchronized and for the sake of speed and simplicity of this low-level
- * library we don't want to re-read the line info automatically everytime
- * a property is retrieved. Any daemon using this library must track the state
- * of lines on its own and call this routine if needed.
- *
- * The state of requested lines is kept synchronized (or rather cannot be
- * changed by external agents while the ownership of the line is taken) so
- * there's no need to call this function in that case.
- */
-int gpiod_line_update(struct gpiod_line *line) GPIOD_API;
-
 /**
  * @brief Get the handle to the GPIO chip controlling this line.
  * @param line The GPIO line object.
lib/core.c +7 -5
index 8e3d8a7..8fdc503 100644
@@ -373,6 +373,8 @@ unsigned int gpiod_chip_num_lines(struct gpiod_chip *chip)
 	return chip->num_lines;
 }
 
+static int line_update(struct gpiod_line *line);
+
 struct gpiod_line *
 gpiod_chip_get_line(struct gpiod_chip *chip, unsigned int offset)
 {
@@ -406,7 +408,7 @@ gpiod_chip_get_line(struct gpiod_chip *chip, unsigned int offset)
 		line = chip->lines[offset];
 	}
 
-	rv = gpiod_line_update(line);
+	rv = line_update(line);
 	if (rv < 0)
 		return NULL;
 
@@ -535,7 +537,7 @@ static int line_info_v2_to_info_flags(struct gpio_v2_line_info *info)
 	return iflags;
 }
 
-int gpiod_line_update(struct gpiod_line *line)
+static int line_update(struct gpiod_line *line)
 {
 	struct gpio_v2_line_info info;
 	int rv;
@@ -766,7 +768,7 @@ static int line_request_values(struct gpiod_line_bulk *bulk,
 				req.config.attrs[0].attr.values, i);
 		line_set_fd(line, line_fd);
 
-		rv = gpiod_line_update(line);
+		rv = line_update(line);
 		if (rv) {
 			gpiod_line_release_bulk(bulk);
 			return rv;
@@ -805,7 +807,7 @@ static int line_request_event_single(struct gpiod_line *line,
 	line->req_flags = config->flags;
 	line_set_fd(line, line_fd);
 
-	rv = gpiod_line_update(line);
+	rv = line_update(line);
 	if (rv) {
 		gpiod_line_release(line);
 		return rv;
@@ -1036,7 +1038,7 @@ int gpiod_line_set_config_bulk(struct gpiod_line_bulk *bulk,
 			line->output_value = lines_bitmap_test_bit(
 				hcfg.attrs[0].attr.values, i);
 
-		rv = gpiod_line_update(line);
+		rv = line_update(line);
 		if (rv < 0)
 			return rv;
 	}
Signature · 1 line
2.30.1
#6 lore raw

PATCH 6/6 core: hide the GPIOD_API symbol

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

GPIOD_API is unneccesarily polluting the user-facing headers. There's no advantage to having it in public files. Let's make them internal to the library. Upcoming modifications will also make GPIOD_BIT() redundant so we'll be able to remove all macros unrelated to the main functionality from gpiod.h.

---
 bindings/cxx/Makefile.am   |   2 +-
 bindings/cxx/chip.cpp      |  40 +++----
 bindings/cxx/gpiod.hpp     | 227 ++++++++++++++++++-------------------
 bindings/cxx/internal.hpp  |   9 ++
 bindings/cxx/iter.cpp      |  18 +--
 bindings/cxx/line.cpp      |  66 +++++------
 bindings/cxx/line_bulk.cpp |  78 ++++++-------
 include/gpiod.h            | 158 ++++++++++++--------------
 lib/Makefile.am            |   2 +-
 lib/core.c                 | 135 +++++++++++-----------
 lib/helpers.c              | 100 +++++++++-------
 lib/internal.h             |  11 ++
 lib/misc.c                 |   4 +-
 13 files changed, 441 insertions(+), 409 deletions(-)
 create mode 100644 bindings/cxx/internal.hpp
 create mode 100644 lib/internal.h
bindings/cxx/Makefile.am +1 -1
index 655d1cd..d9fa577 100644
@@ -2,7 +2,7 @@
 # SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski <[email protected]>
 
 lib_LTLIBRARIES = libgpiodcxx.la
-libgpiodcxx_la_SOURCES = chip.cpp iter.cpp line.cpp line_bulk.cpp
+libgpiodcxx_la_SOURCES = chip.cpp internal.h iter.cpp line.cpp line_bulk.cpp
 libgpiodcxx_la_CPPFLAGS = -Wall -Wextra -g -std=gnu++11
 libgpiodcxx_la_CPPFLAGS += -fvisibility=hidden -I$(top_srcdir)/include/
 libgpiodcxx_la_LDFLAGS = -version-info $(subst .,:,$(ABI_CXX_VERSION))
bindings/cxx/chip.cpp +21 -19
index 0886717..d5a9837 100644
@@ -7,41 +7,43 @@
 #include <system_error>
 #include <utility>
 
+#include "internal.hpp"
+
 namespace gpiod {
 
 namespace {
 
-void chip_deleter(::gpiod_chip* chip)
+GPIOD_CXX_API void chip_deleter(::gpiod_chip* chip)
 {
 	::gpiod_chip_unref(chip);
 }
 
 } /* namespace */
 
-bool is_gpiochip_device(const ::std::string& path)
+GPIOD_CXX_API bool is_gpiochip_device(const ::std::string& path)
 {
 	return ::gpiod_is_gpiochip_device(path.c_str());
 }
 
-chip::chip(const ::std::string& path)
+GPIOD_CXX_API chip::chip(const ::std::string& path)
 	: _m_chip()
 {
 	this->open(path);
 }
 
-chip::chip(::gpiod_chip* chip)
+GPIOD_CXX_API chip::chip(::gpiod_chip* chip)
 	: _m_chip(chip, chip_deleter)
 {
 
 }
 
-chip::chip(const ::std::weak_ptr<::gpiod_chip>& chip_ptr)
+GPIOD_CXX_API chip::chip(const ::std::weak_ptr<::gpiod_chip>& chip_ptr)
 	: _m_chip(chip_ptr)
 {
 
 }
 
-void chip::open(const ::std::string& path)
+GPIOD_CXX_API void chip::open(const ::std::string& path)
 {
 	::gpiod_chip *chip = ::gpiod_chip_open(path.c_str());
 	if (!chip)
@@ -51,33 +53,33 @@ void chip::open(const ::std::string& path)
 	this->_m_chip.reset(chip, chip_deleter);
 }
 
-void chip::reset(void) noexcept
+GPIOD_CXX_API void chip::reset(void) noexcept
 {
 	this->_m_chip.reset();
 }
 
-::std::string chip::name(void) const
+GPIOD_CXX_API ::std::string chip::name(void) const
 {
 	this->throw_if_noref();
 
 	return ::std::string(::gpiod_chip_name(this->_m_chip.get()));
 }
 
-::std::string chip::label(void) const
+GPIOD_CXX_API ::std::string chip::label(void) const
 {
 	this->throw_if_noref();
 
 	return ::std::string(::gpiod_chip_label(this->_m_chip.get()));
 }
 
-unsigned int chip::num_lines(void) const
+GPIOD_CXX_API unsigned int chip::num_lines(void) const
 {
 	this->throw_if_noref();
 
 	return ::gpiod_chip_num_lines(this->_m_chip.get());
 }
 
-line chip::get_line(unsigned int offset) const
+GPIOD_CXX_API line chip::get_line(unsigned int offset) const
 {
 	this->throw_if_noref();
 
@@ -92,7 +94,7 @@ line chip::get_line(unsigned int offset) const
 	return line(line_handle, *this);
 }
 
-int chip::find_line(const ::std::string& name) const
+GPIOD_CXX_API int chip::find_line(const ::std::string& name) const
 {
 	this->throw_if_noref();
 
@@ -106,7 +108,7 @@ int chip::find_line(const ::std::string& name) const
 	return -1;
 }
 
-line_bulk chip::get_lines(const ::std::vector<unsigned int>& offsets) const
+GPIOD_CXX_API line_bulk chip::get_lines(const ::std::vector<unsigned int>& offsets) const
 {
 	line_bulk lines;
 
@@ -116,7 +118,7 @@ line_bulk chip::get_lines(const ::std::vector<unsigned int>& offsets) const
 	return lines;
 }
 
-line_bulk chip::get_all_lines(void) const
+GPIOD_CXX_API line_bulk chip::get_all_lines(void) const
 {
 	line_bulk lines;
 
@@ -126,27 +128,27 @@ line_bulk chip::get_all_lines(void) const
 	return lines;
 }
 
-bool chip::operator==(const chip& rhs) const noexcept
+GPIOD_CXX_API bool chip::operator==(const chip& rhs) const noexcept
 {
 	return this->_m_chip.get() == rhs._m_chip.get();
 }
 
-bool chip::operator!=(const chip& rhs) const noexcept
+GPIOD_CXX_API bool chip::operator!=(const chip& rhs) const noexcept
 {
 	return this->_m_chip.get() != rhs._m_chip.get();
 }
 
-chip::operator bool(void) const noexcept
+GPIOD_CXX_API chip::operator bool(void) const noexcept
 {
 	return this->_m_chip.get() != nullptr;
 }
 
-bool chip::operator!(void) const noexcept
+GPIOD_CXX_API bool chip::operator!(void) const noexcept
 {
 	return this->_m_chip.get() == nullptr;
 }
 
-void chip::throw_if_noref(void) const
+GPIOD_CXX_API void chip::throw_if_noref(void) const
 {
 	if (!this->_m_chip.get())
 		throw ::std::logic_error("object not associated with an open GPIO chip");
bindings/cxx/gpiod.hpp +113 -114

799 lines not on this page · raw message

bindings/cxx/internal.hpp new file +9 -0
new file mode 100644
index 0000000..9406d30
@@ -0,0 +1,9 @@
+/* SPDX-License-Identifier: LGPL-3.0-or-later */
+/* SPDX-FileCopyrightText: 2021 Bartosz Golaszewski <[email protected]> */
+
+#ifndef __LIBGPIOD_GPIOD_CXX_INTERNAL_HPP__
+#define __LIBGPIOD_GPIOD_CXX_INTERNAL_HPP__
+
+#define GPIOD_CXX_API __attribute__((visibility("default")))
+
+#endif /* __LIBGPIOD_GPIOD_CXX_INTERNAL_HPP__ */
bindings/cxx/iter.cpp +10 -8
index eba7d30..09d46f3 100644
@@ -4,25 +4,27 @@
 #include <gpiod.hpp>
 #include <system_error>
 
+#include "internal.hpp"
+
 namespace gpiod {
 
-line_iter begin(line_iter iter) noexcept
+GPIOD_CXX_API line_iter begin(line_iter iter) noexcept
 {
 	return iter;
 }
 
-line_iter end(const line_iter&) noexcept
+GPIOD_CXX_API line_iter end(const line_iter&) noexcept
 {
 	return line_iter();
 }
 
-line_iter::line_iter(const chip& owner)
+GPIOD_CXX_API line_iter::line_iter(const chip& owner)
 	: _m_current(owner.get_line(0))
 {
 
 }
 
-line_iter& line_iter::operator++(void)
+GPIOD_CXX_API line_iter& line_iter::operator++(void)
 {
 	unsigned int offset = this->_m_current.offset() + 1;
 	chip owner = this->_m_current.get_chip();
@@ -35,22 +37,22 @@ line_iter& line_iter::operator++(void)
 	return *this;
 }
 
-const line& line_iter::operator*(void) const
+GPIOD_CXX_API const line& line_iter::operator*(void) const
 {
 	return this->_m_current;
 }
 
-const line* line_iter::operator->(void) const
+GPIOD_CXX_API const line* line_iter::operator->(void) const
 {
 	return ::std::addressof(this->_m_current);
 }
 
-bool line_iter::operator==(const line_iter& rhs) const noexcept
+GPIOD_CXX_API bool line_iter::operator==(const line_iter& rhs) const noexcept
 {
 	return this->_m_current._m_line == rhs._m_current._m_line;
 }
 
-bool line_iter::operator!=(const line_iter& rhs) const noexcept
+GPIOD_CXX_API bool line_iter::operator!=(const line_iter& rhs) const noexcept
 {
 	return this->_m_current._m_line != rhs._m_current._m_line;
 }
bindings/cxx/line.cpp +34 -32

246 lines not on this page · raw message

bindings/cxx/line_bulk.cpp +40 -38

239 lines not on this page · raw message

include/gpiod.h +71 -87

587 lines not on this page · raw message

lib/Makefile.am +1 -1
index d8b19f0..8441584 100644
@@ -2,7 +2,7 @@
 # SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski <[email protected]>
 
 lib_LTLIBRARIES = libgpiod.la
-libgpiod_la_SOURCES = core.c helpers.c misc.c uapi/gpio.h
+libgpiod_la_SOURCES = core.c helpers.c internal.h misc.c uapi/gpio.h
 libgpiod_la_CFLAGS = -Wall -Wextra -g -std=gnu89
 libgpiod_la_CFLAGS += -fvisibility=hidden -I$(top_srcdir)/include/
 libgpiod_la_CFLAGS += -include $(top_builddir)/config.h
lib/core.c +71 -64

397 lines not on this page · raw message

lib/helpers.c +56 -44

229 lines not on this page · raw message

lib/internal.h new file +11 -0
new file mode 100644
index 0000000..8b3f69a
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/* SPDX-FileCopyrightText: 2021 Bartosz Golaszewski <[email protected]> */
+
+#ifndef __LIBGPIOD_GPIOD_INTERNAL_H__
+#define __LIBGPIOD_GPIOD_INTERNAL_H__
+
+/* For internal library use only. */
+
+#define GPIOD_API __attribute__((visibility("default")))
+
+#endif /* __LIBGPIOD_GPIOD_INTERNAL_H__ */
lib/misc.c +3 -1
index 5583eb0..984405b 100644
@@ -5,7 +5,9 @@
 
 #include <gpiod.h>
 
-const char *gpiod_version_string(void)
+#include "internal.h"
+
+GPIOD_API const char *gpiod_version_string(void)
 {
 	return GPIOD_VERSION_STR;
 }
Signature · 1 line
2.30.1
#7 lore raw

Re: [libgpiod][PATCH 6/6] core: hide the GPIOD_API symbol

bindings/cxx/Makefile.am line 5

Quoted 10 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

GPIOD_API is unneccesarily polluting the user-facing headers. There's no advantage to having it in public files. Let's make them internal to the library. Upcoming modifications will also make GPIOD_BIT() redundant so we'll be able to remove all macros unrelated to the main functionality from gpiod.h.

bindings/cxx/Makefile.am +1 -1
-libgpiodcxx_la_SOURCES = chip.cpp iter.cpp line.cpp line_bulk.cpp
+libgpiodcxx_la_SOURCES = chip.cpp internal.h iter.cpp line.cpp line_bulk.cpp

Don't we have something like _HEADERS ?

...

Quoted 1 line

+#include "internal.hpp"

I forgot C++ approximately when I tried it first time :-)
But I'm confused between this and above file name. Do we have *.h and *.hpp with the same name?

...

lib/Makefile.am line 5

Quoted 2 lines
lib/Makefile.am +1 -1
-libgpiod_la_SOURCES = core.c helpers.c misc.c uapi/gpio.h
+libgpiod_la_SOURCES = core.c helpers.c internal.h misc.c uapi/gpio.h

As above.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#8 lore raw

Re: [libgpiod][PATCH 6/6] core: hide the GPIOD_API symbol

Quoted 10 lines · Andy Shevchenko
Quoted 7 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

GPIOD_API is unneccesarily polluting the user-facing headers. There's no advantage to having it in public files. Let's make them internal to the library. Upcoming modifications will also make GPIOD_BIT() redundant so we'll be able to remove all macros unrelated to the main functionality from gpiod.h.

Quoted 5 lines

bindings/cxx/Makefile.am line 5

Quoted 2 lines
bindings/cxx/Makefile.am +1 -1
-libgpiodcxx_la_SOURCES = chip.cpp iter.cpp line.cpp line_bulk.cpp
+libgpiodcxx_la_SOURCES = chip.cpp internal.h iter.cpp line.cpp line_bulk.cpp

Don't we have something like _HEADERS ?

_HEADERS is for headers that are installed. Headers for internal use should be defined as part of sources in autotools.

Quoted 8 lines

...

Quoted 1 line

+#include "internal.hpp"

I forgot C++ approximately when I tried it first time :-)
But I'm confused between this and above file name. Do we have *.h and *.hpp with the same name?

Yes, internal.h is used by the core C library and lives in lib/, internal.hpp is for C++ (it will grow because we'll have to share some symbols between classes in the future with the new API) and lives in bindings/cxx/.

Bart

Quoted 12 lines

...

lib/Makefile.am line 5

Quoted 2 lines
lib/Makefile.am +1 -1
-libgpiod_la_SOURCES = core.c helpers.c misc.c uapi/gpio.h
+libgpiod_la_SOURCES = core.c helpers.c internal.h misc.c uapi/gpio.h

As above.

--
With Best Regards,
Andy Shevchenko

#9 lore raw

Re: [libgpiod][PATCH 6/6] core: hide the GPIOD_API symbol

Quoted 13 lines · Bartosz Golaszewski
Quoted 10 lines · Andy Shevchenko
Quoted 7 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

GPIOD_API is unneccesarily polluting the user-facing headers. There's no advantage to having it in public files. Let's make them internal to the library. Upcoming modifications will also make GPIOD_BIT() redundant so we'll be able to remove all macros unrelated to the main functionality from gpiod.h.

Quoted 8 lines
Quoted 5 lines

bindings/cxx/Makefile.am line 5

Quoted 2 lines
bindings/cxx/Makefile.am +1 -1
-libgpiodcxx_la_SOURCES = chip.cpp iter.cpp line.cpp line_bulk.cpp
+libgpiodcxx_la_SOURCES = chip.cpp internal.h iter.cpp line.cpp line_bulk.cpp

Don't we have something like _HEADERS ?

_HEADERS is for headers that are installed. Headers for internal use should be defined as part of sources in autotools.

TIL :-)

"The noinst_HEADERS variable can be used for such headers. However when the header actually belongs to a single convenience library or program, we recommend listing it in the program’s or library’s _SOURCES variable (see Program Sources) instead of in noinst_HEADERS. This is clearer for the Makefile.am reader. noinst_HEADERS would be the right variable to use in a directory containing only headers and no associated library or program."

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#10 lore raw
In reply to Bartosz Golaszewski
Quoted 15 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Kent suggested I send the patches for v2.0 API in smaller batches for earlier review instead of dropping a huge patch bomb that will require more work from my side and may later be negatively reviewed so here's a hopefully the last part of cleanups that can go into master already before first patches with the new C API will drop.

The idea for the new C API is this: all objects are opaque and refcounted. This is why the chip was converted to reference counting. Lines were removed and their functionality split into line_info and line_request, this is why I'm reworking line lookup to return the HW offset within a chip and not the line object. Other patches are mostly cleanups and cruft shave-off.

Quoted 8 lines

Bartosz Golaszewski (6):
treewide: simplify line lookup
tests: remove line bulk test cases
core: switch to reference counting for gpio chip objects
treewide: remove is_requested() and is_free()
treewide: kill line updating
core: hide the GPIOD_API symbol

If there are no objections, I will apply those soon.

Bart