From patchwork Tue Mar 9 13:26:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 396329 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3BF90C43381 for ; Tue, 9 Mar 2021 13:27:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 00BB965272 for ; Tue, 9 Mar 2021 13:27:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230465AbhCIN0q (ORCPT ); Tue, 9 Mar 2021 08:26:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44794 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231140AbhCIN0n (ORCPT ); Tue, 9 Mar 2021 08:26:43 -0500 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7758DC06174A for ; Tue, 9 Mar 2021 05:26:43 -0800 (PST) Received: by mail-wr1-x431.google.com with SMTP id e10so15512774wro.12 for ; Tue, 09 Mar 2021 05:26:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=JEE7qYHc4u6/CS6KJ9UlT6fiBdwtYetmHSPTpvx0RNI=; b=z2wa/vfnF/yGzD9Bo342WBiGhWzw112Qc4WQsBaPVU9FYtg4YKglUyki9pFrwi0ttb 8hMpWG+Km8oVUZ1emIcaIbbT8UdrHRfSNudbgQKokEF3A2ohXSNkqaL0Xt2lte7x5PcP IFxSvmrElod6c9EM3WQr0sEnhsPj5mBAJg52Z4TBTB/Pgl9cUr954on/IctuPnOfoFt9 Xvr3zInhv53i3d7GSQ0z9MjBO5VYk6BRY0pj4As9ZVyrvFr3vflbMZD1G1ymTrQtqkrq ik+wjOFBFRv+5wnKNUPcVsa8IIm7KEZ6bnBpumK1fpJnFMzQWk6rc1NHfLxeT4fFEv6P RSwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=JEE7qYHc4u6/CS6KJ9UlT6fiBdwtYetmHSPTpvx0RNI=; b=r1qr9AbTNugq0t9ANT5vVVg/8dDHZUXZKHCkxQVvymd3nWpRGyub/Weq0OJkWL86Bd U5GJIfKpIA3MnmRJOJbccMiJUz9M9d1NUOnV1FeyEinm98yKcgy1FMCx4RalpztLvZsY UUnZMNyB6t3Elcr+FPFL7yCcgg1XfyP1GqIwR30X5tzyv2r6W5VHPGrgoJuQwkbSUSZI F9vrTk9xx6FHsOIXrzhLz9PARk+vilLtRLxCRwqs7P/wu7zmsYowErJvIwCCdeBU2GkX WukesqGP+SatttvpWzkSY7IkUvyqIc6obl1FDfvbOqOAeQJ3w/x5y/NRfAfahkIkTCWV WIRw== X-Gm-Message-State: AOAM532UhLadaAawwnyW+XQlTOj2YFDpXuIoPbIQh6vKN2gTTgfX2IcO U0FdH+9emOCyIOVKGRWOsHqldw== X-Google-Smtp-Source: ABdhPJwGWHervEmgXvH1F0F+IznEXKGIo5VBfvvKF6I4MofC+7dserQnvu5LQIpfae5YBoT03oXqGg== X-Received: by 2002:adf:e54a:: with SMTP id z10mr28087229wrm.87.1615296402146; Tue, 09 Mar 2021 05:26:42 -0800 (PST) Received: from debian-brgl.home (amarseille-656-1-4-167.w90-8.abo.wanadoo.fr. [90.8.158.167]) by smtp.gmail.com with ESMTPSA id u23sm4123366wmn.26.2021.03.09.05.26.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Mar 2021 05:26:41 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski Subject: [libgpiod][PATCH 1/6] treewide: simplify line lookup Date: Tue, 9 Mar 2021 14:26:34 +0100 Message-Id: <20210309132639.29069-2-brgl@bgdev.pl> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210309132639.29069-1-brgl@bgdev.pl> References: <20210309132639.29069-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski 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. Signed-off-by: Bartosz Golaszewski --- 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(-) diff --git a/bindings/cxx/chip.cpp b/bindings/cxx/chip.cpp index 5b8125b..527fb34 100644 --- a/bindings/cxx/chip.cpp +++ b/bindings/cxx/chip.cpp @@ -92,22 +92,18 @@ line chip::get_line(unsigned int offset) const return line(line_handle, *this); } -::std::vector 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 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& offsets) const diff --git a/bindings/cxx/examples/gpiofindcxx.cpp b/bindings/cxx/examples/gpiofindcxx.cpp index 0bccd94..ec4d79b 100644 --- a/bindings/cxx/examples/gpiofindcxx.cpp +++ b/bindings/cxx/examples/gpiofindcxx.cpp @@ -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; } } diff --git a/bindings/cxx/gpiod.hpp b/bindings/cxx/gpiod.hpp index d987b3a..3a043a1 100644 --- a/bindings/cxx/gpiod.hpp +++ b/bindings/cxx/gpiod.hpp @@ -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 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. diff --git a/bindings/cxx/tests/tests-chip.cpp b/bindings/cxx/tests/tests-chip.cpp index a84b150..aea00fa 100644 --- a/bindings/cxx/tests/tests-chip.cpp +++ b/bindings/cxx/tests/tests-chip.cpp @@ -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); } } diff --git a/bindings/python/examples/gpiofind.py b/bindings/python/examples/gpiofind.py index 117d583..a9ec734 100755 --- a/bindings/python/examples/gpiofind.py +++ b/bindings/python/examples/gpiofind.py @@ -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) diff --git a/bindings/python/gpiodmodule.c b/bindings/python/gpiodmodule.c index dca6a98..ac0fd83 100644 --- a/bindings/python/gpiodmodule.c +++ b/bindings/python/gpiodmodule.c @@ -2151,59 +2151,40 @@ gpiod_LineBulkObjectFromBulk(gpiod_ChipObject *chip, struct gpiod_line_bulk *bul } PyDoc_STRVAR(gpiod_Chip_find_line_doc, -"find_line(name) -> gpiod.LineBulk object or None\n" +"find_line(name) -> integer or None\n" "\n" -"Find all GPIO lines by name among lines exposed by this GPIO chip..\n" +"Find the offset of the line with given name among lines exposed by this\n" +"GPIO chip.\n" "\n" " name\n" " Line name (string)\n" -" unique\n" -" Indicates whether an exception should be raised if more than one lines\n" -" matches the name\n" "\n" -"Returns a gpiod.LineBulk object containing all matching lines or None if\n" -"line with given name is not associated with this chip."); +"Returns the offset of the line with given name or None if it is not\n" +"associated with this chip."); -static gpiod_LineBulkObject * -gpiod_Chip_find_line(gpiod_ChipObject *self, PyObject *args, PyObject *kwds) +static PyObject *gpiod_Chip_find_line(gpiod_ChipObject *self, PyObject *args) { - static char *kwlist[] = { "", "unique", NULL }; - - gpiod_LineBulkObject *bulk_obj; - struct gpiod_line_bulk *bulk; - int rv, unique = 0; const char *name; + int rv, offset; if (gpiod_ChipIsClosed(self)) return NULL; - rv = PyArg_ParseTupleAndKeywords(args, kwds, "s|p", - kwlist, &name, &unique); + rv = PyArg_ParseTuple(args, "s", &name); if (!rv) return NULL; Py_BEGIN_ALLOW_THREADS; - bulk = gpiod_chip_find_line(self->chip, name); + offset = gpiod_chip_find_line(self->chip, name); Py_END_ALLOW_THREADS; - if (!bulk) { - if (errno == ENOENT) { - Py_INCREF(Py_None); - return (gpiod_LineBulkObject *)Py_None; - } - - return (gpiod_LineBulkObject *)PyErr_SetFromErrno( - PyExc_OSError); - } + if (offset < 0) { + if (errno == ENOENT) + Py_RETURN_NONE; - if (unique && gpiod_line_bulk_num_lines(bulk) > 1) { - gpiod_line_bulk_free(bulk); - PyErr_SetString(PyExc_RuntimeError, "line not unique"); - return NULL; + return PyErr_SetFromErrno(PyExc_OSError); } - bulk_obj = gpiod_LineBulkObjectFromBulk(self, bulk); - gpiod_line_bulk_free(bulk); - return bulk_obj; + return Py_BuildValue("i", offset); } PyDoc_STRVAR(gpiod_Chip_get_lines_doc, @@ -2353,8 +2334,8 @@ static PyMethodDef gpiod_Chip_methods[] = { }, { .ml_name = "find_line", - .ml_meth = (PyCFunction)(void (*)(void))gpiod_Chip_find_line, - .ml_flags = METH_VARARGS | METH_KEYWORDS, + .ml_meth = (PyCFunction)gpiod_Chip_find_line, + .ml_flags = METH_VARARGS, .ml_doc = gpiod_Chip_find_line_doc, }, { diff --git a/bindings/python/tests/gpiod_py_test.py b/bindings/python/tests/gpiod_py_test.py index f264db3..c8ee85b 100755 --- a/bindings/python/tests/gpiod_py_test.py +++ b/bindings/python/tests/gpiod_py_test.py @@ -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: diff --git a/include/gpiod.h b/include/gpiod.h index 76aa1dd..6ed9c6c 100644 --- a/include/gpiod.h +++ b/include/gpiod.h @@ -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; /** * @} diff --git a/lib/helpers.c b/lib/helpers.c index 0071a2d..76d8fc2 100644 --- a/lib/helpers.c +++ b/lib/helpers.c @@ -59,10 +59,8 @@ struct gpiod_line_bulk *gpiod_chip_get_all_lines(struct gpiod_chip *chip) return bulk; } -struct gpiod_line_bulk * -gpiod_chip_find_line(struct gpiod_chip *chip, const char *name) +int gpiod_chip_find_line(struct gpiod_chip *chip, const char *name) { - struct gpiod_line_bulk *bulk = NULL; unsigned int offset, num_lines; struct gpiod_line *line; const char *tmp; @@ -70,62 +68,17 @@ gpiod_chip_find_line(struct gpiod_chip *chip, const char *name) num_lines = gpiod_chip_num_lines(chip); for (offset = 0; offset < num_lines; offset++) { - line = gpiod_chip_get_line(chip, offset); - if (!line) { - if (bulk) - gpiod_line_bulk_free(bulk); - - return NULL; - } - - tmp = gpiod_line_name(line); - if (tmp && strcmp(tmp, name) == 0) { - if (!bulk) { - bulk = gpiod_line_bulk_new(num_lines); - if (!bulk) - return NULL; - } - - gpiod_line_bulk_add_line(bulk, line); - } - } - - if (!bulk) - errno = ENOENT; - - return bulk; -} - -struct gpiod_line * -gpiod_chip_find_line_unique(struct gpiod_chip *chip, const char *name) -{ - struct gpiod_line *line, *matching = NULL; - unsigned int offset, num_found = 0; - const char *tmp; - - for (offset = 0; offset < gpiod_chip_num_lines(chip); offset++) { line = gpiod_chip_get_line(chip, offset); if (!line) - return NULL; + return -1; tmp = gpiod_line_name(line); - if (tmp && strcmp(tmp, name) == 0) { - matching = line; - num_found++; - } - } - - if (matching) { - if (num_found > 1) { - errno = ERANGE; - return NULL; - } - - return matching; + if (tmp && strcmp(tmp, name) == 0) + return gpiod_line_offset(line); } errno = ENOENT; - return NULL; + return -1; } int gpiod_line_request_input(struct gpiod_line *line, const char *consumer) diff --git a/tests/tests-chip.c b/tests/tests-chip.c index 928dc49..a87dc9a 100644 --- a/tests/tests-chip.c +++ b/tests/tests-chip.c @@ -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); } diff --git a/tools/gpiofind.c b/tools/gpiofind.c index 4936c4f..0fc07d9 100644 --- a/tools/gpiofind.c +++ b/tools/gpiofind.c @@ -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; } From patchwork Tue Mar 9 13:26:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 397033 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6DD80C4332E for ; Tue, 9 Mar 2021 13:27:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4166664D8F for ; Tue, 9 Mar 2021 13:27:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230303AbhCIN0r (ORCPT ); Tue, 9 Mar 2021 08:26:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231387AbhCIN0o (ORCPT ); Tue, 9 Mar 2021 08:26:44 -0500 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0006AC06174A for ; Tue, 9 Mar 2021 05:26:43 -0800 (PST) Received: by mail-wr1-x434.google.com with SMTP id l11so12155613wrp.7 for ; Tue, 09 Mar 2021 05:26:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=6R4ghm7tGz7EGoUHwQaNcH3TJrYiYArcaAyQ45p8lfY=; b=yUm0p89NZPS0F/pY2jy/iTe1pb1MFF8/4DRHVAJ84E5ri+b8GQDk8nF4jn9r6PNPwm 4SV9cxgu2kOcfnj1oSkjIIMHJbNyLjiwOZQQk98vNlHUCI2E2jYnKbeoR1GOOcLH3RG2 Ku1HUIOb6IdrrJj5vv8YMHZhVPelwhhUTIwLvrrIR7J2Liiq2h88zZ/akowmJCCOWeEB LXuCy25kZ/4m0VP583RTM7XTLuUCdnnhmfngcWhv/YFCHOETSZCLIFFf/lHQOEckukFD yWqLYOslL7OBHcOI4K0cJ27Tpg9QqmC4zPZeO9yykK6IZ/gel/H1H1u9IZHyA+lbitKZ AQ7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=6R4ghm7tGz7EGoUHwQaNcH3TJrYiYArcaAyQ45p8lfY=; b=pmDJL9tp8reiJz4QBmnurSoepzsZUU+GVdTT2pH77tigc9FNOyo2X6QxhVtglOJWPm 2jgI4Y0RGTNisw9MahJndr5ub6Jv5nTOKnkptfrFe2IBAWYu5qDg/Ww6WhIE0xc6/lwy KF08h9TVTG3APw+GwDltpksrx8eUmFk4sZxE7ReXnpVqhYoSovYoag3gjEYxS0tKBKXL Qemdf5HqYQYtBU2vPLD8Zs4TEunrmytYB3Bha71ioGjsbw+MBwQoHeFN1gRDOKv+pLBW gzwKi/yY4EAx53hj4i9gNX3nBYWq119/SbnAd45drE1DTAzDp4RQfwsJ4Zb2ml0maH8n sBGQ== X-Gm-Message-State: AOAM533I0MrkFrxsnjFo2Uz9wGhJmtYczst8izCHYfb7csiPe+Yu5hgt EjPrbqWDFF5zTkEZezzgzQ2Fsg== X-Google-Smtp-Source: ABdhPJw9DuYtGUFa2Rf9IHf6B+a3lgfIsC24zCjCeaul9aoKw+tWazfdFGPQt4b/yIZ0jBO6qhuJEw== X-Received: by 2002:adf:c64a:: with SMTP id u10mr28176251wrg.412.1615296402613; Tue, 09 Mar 2021 05:26:42 -0800 (PST) Received: from debian-brgl.home (amarseille-656-1-4-167.w90-8.abo.wanadoo.fr. [90.8.158.167]) by smtp.gmail.com with ESMTPSA id u23sm4123366wmn.26.2021.03.09.05.26.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Mar 2021 05:26:42 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski Subject: [libgpiod][PATCH 2/6] tests: remove line bulk test cases Date: Tue, 9 Mar 2021 14:26:35 +0100 Message-Id: <20210309132639.29069-3-brgl@bgdev.pl> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210309132639.29069-1-brgl@bgdev.pl> References: <20210309132639.29069-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski 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. Signed-off-by: Bartosz Golaszewski --- 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 diff --git a/bindings/cxx/tests/tests-line.cpp b/bindings/cxx/tests/tests-line.cpp index c1ad8d2..648012a 100644 --- a/bindings/cxx/tests/tests-line.cpp +++ b/bindings/cxx/tests/tests-line.cpp @@ -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 }); diff --git a/bindings/python/tests/gpiod_py_test.py b/bindings/python/tests/gpiod_py_test.py index c8ee85b..b7c30de 100755 --- a/bindings/python/tests/gpiod_py_test.py +++ b/bindings/python/tests/gpiod_py_test.py @@ -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 # diff --git a/tests/Makefile.am b/tests/Makefile.am index 49aa215..43b215e 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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 \ diff --git a/tests/tests-bulk.c b/tests/tests-bulk.c deleted file mode 100644 index ad08f2d..0000000 --- a/tests/tests-bulk.c +++ /dev/null @@ -1,145 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-or-later -// SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski - -#include - -#include "gpiod-test.h" - -#define GPIOD_TEST_GROUP "bulk" - -GPIOD_TEST_CASE(alloc_zero_lines, 0, { 1 }) -{ - struct gpiod_line_bulk *bulk; - - bulk = gpiod_line_bulk_new(0); - g_assert_null(bulk); - g_assert_cmpint(errno, ==, EINVAL); -} - -GPIOD_TEST_CASE(add_too_many_lines, 0, { 8 }) -{ - g_autoptr(gpiod_line_bulk_struct) bulk = NULL; - g_autoptr(gpiod_chip_struct) chip = NULL; - struct gpiod_line *lineA, *lineB, *lineC; - gint ret; - - chip = gpiod_chip_open(gpiod_test_chip_path(0)); - g_assert_nonnull(chip); - gpiod_test_return_if_failed(); - - bulk = gpiod_line_bulk_new(2); - g_assert_nonnull(bulk); - gpiod_test_return_if_failed(); - - lineA = gpiod_chip_get_line(chip, 0); - lineB = gpiod_chip_get_line(chip, 1); - lineC = gpiod_chip_get_line(chip, 2); - g_assert_nonnull(lineA); - g_assert_nonnull(lineB); - g_assert_nonnull(lineC); - gpiod_test_return_if_failed(); - - ret = gpiod_line_bulk_add_line(bulk, lineA); - g_assert_cmpint(ret, ==, 0); - ret = gpiod_line_bulk_add_line(bulk, lineB); - g_assert_cmpint(ret, ==, 0); - ret = gpiod_line_bulk_add_line(bulk, lineC); - g_assert_cmpint(ret, ==, -1); - g_assert_cmpint(errno, ==, EINVAL); -} - -GPIOD_TEST_CASE(add_lines_from_different_chips, 0, { 8, 8 }) -{ - g_autoptr(gpiod_line_bulk_struct) bulk = NULL; - g_autoptr(gpiod_chip_struct) chipA = NULL; - g_autoptr(gpiod_chip_struct) chipB = NULL; - struct gpiod_line *lineA, *lineB; - gint ret; - - chipA = gpiod_chip_open(gpiod_test_chip_path(0)); - chipB = gpiod_chip_open(gpiod_test_chip_path(1)); - g_assert_nonnull(chipA); - g_assert_nonnull(chipB); - gpiod_test_return_if_failed(); - - bulk = gpiod_line_bulk_new(4); - g_assert_nonnull(bulk); - gpiod_test_return_if_failed(); - - lineA = gpiod_chip_get_line(chipA, 0); - lineB = gpiod_chip_get_line(chipB, 0); - g_assert_nonnull(lineA); - g_assert_nonnull(lineB); - gpiod_test_return_if_failed(); - - ret = gpiod_line_bulk_add_line(bulk, lineA); - g_assert_cmpint(ret, ==, 0); - ret = gpiod_line_bulk_add_line(bulk, lineB); - g_assert_cmpint(ret, ==, -1); - g_assert_cmpint(errno, ==, EINVAL); -} - -static const gchar *const bulk_foreach_line_names[] = { - "gpio-mockup-A-0", - "gpio-mockup-A-1", - "gpio-mockup-A-2", - "gpio-mockup-A-3" -}; - -static int bulk_foreach_callback_all(struct gpiod_line *line, void *data) -{ - gint *i = data; - - g_assert_cmpstr(gpiod_line_name(line), ==, - bulk_foreach_line_names[(*i)++]); - - return GPIOD_LINE_BULK_CB_NEXT; -} - -static int bulk_foreach_callback_stop(struct gpiod_line *line, void *data) -{ - gint *i = data; - - g_assert_cmpstr(gpiod_line_name(line), ==, - bulk_foreach_line_names[(*i)++]); - if (*i == 2) - return GPIOD_LINE_BULK_CB_STOP; - - return GPIOD_LINE_BULK_CB_NEXT; -} - -GPIOD_TEST_CASE(foreach_all_lines, GPIOD_TEST_FLAG_NAMED_LINES, { 4 }) -{ - g_autoptr(gpiod_line_bulk_struct) bulk = NULL; - g_autoptr(gpiod_chip_struct) chip = NULL; - gint i = 0; - - chip = gpiod_chip_open(gpiod_test_chip_path(0)); - g_assert_nonnull(chip); - gpiod_test_return_if_failed(); - - bulk = gpiod_chip_get_all_lines(chip); - g_assert_nonnull(bulk); - gpiod_test_return_if_failed(); - - gpiod_line_bulk_foreach_line(bulk, bulk_foreach_callback_all, &i); - gpiod_test_return_if_failed(); -} - -GPIOD_TEST_CASE(foreach_two_lines, GPIOD_TEST_FLAG_NAMED_LINES, { 8 }) -{ - g_autoptr(gpiod_line_bulk_struct) bulk = NULL; - g_autoptr(gpiod_chip_struct) chip = NULL; - gint i = 0; - - chip = gpiod_chip_open(gpiod_test_chip_path(0)); - g_assert_nonnull(chip); - gpiod_test_return_if_failed(); - - bulk = gpiod_chip_get_all_lines(chip); - g_assert_nonnull(bulk); - gpiod_test_return_if_failed(); - - gpiod_line_bulk_foreach_line(bulk, bulk_foreach_callback_stop, &i); - g_assert_cmpint(i, ==, 2); -} From patchwork Tue Mar 9 13:26:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 396330 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B22D5C433E0 for ; Tue, 9 Mar 2021 13:27:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6913A65173 for ; Tue, 9 Mar 2021 13:27:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230183AbhCIN0r (ORCPT ); Tue, 9 Mar 2021 08:26:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231278AbhCIN0o (ORCPT ); Tue, 9 Mar 2021 08:26:44 -0500 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D913C06174A for ; Tue, 9 Mar 2021 05:26:44 -0800 (PST) Received: by mail-wm1-x32f.google.com with SMTP id r10-20020a05600c35cab029010c946c95easo6252656wmq.4 for ; Tue, 09 Mar 2021 05:26:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=BZ2U1VkcTDAB3eVL/HI2CHKpWdfRVZxi57li2qH8vZk=; b=bTDokHHD8fHZeOz3eMMfC8BQAB66GJgp4I37iUmseIS/odTGbnA69mPmxRVUQDmmMi 6REjalQe0H69NqmQD5T51ExSyFgOCHRLCiJ8+i509sp/fXkEr9G6Ixan0dOvrDhkSEjc Wql8Fdwkcy8+T21otAzpbIAVxTG1IMRaAA3un/h9w2ik6Qfhqj0+ZDFFOtgGn1tPrNm/ b2s3L3IO5wsYHUb4XJ1iARDmm9Th3ut9ldBe9bjsdsihHXYtTi1q7Uis3Et5QSqYaoKH xwl1zfIZavsrBwzoBnzX24RusgEFuCtZ/FtNarL/5T41kUcBs2KQN21xEnDniudQru0x Q3fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=BZ2U1VkcTDAB3eVL/HI2CHKpWdfRVZxi57li2qH8vZk=; b=S/kvGjNFuhDPzXBtZwAvdgtAF0qtixR7SLm/eTfLXJF1w117hskBGiKLGLWSiSohtE nHh5AEUY3VlZvCidArI8PrLaXa1vcj2DLAMTsz1CRvjDRCIK/PUhBJ9g9ZcTOMCGHcvh 8vGcmxCJN72c6dFs8JAb7nH0oDLyJIku4/y/+u3jCXBRdayzlvsW6PsHkDwjWkL7tOhK jLoFLRCrsxzzlJivm0iJwKNjNvE5BnGJtg3AUh4aZ6yc4Avzagtm8yxNRSumYFLViwvy 6nfRT5mm6eFaSWZo3/hfIaGLaimBQlLn2DJRzRFhLuyg0i2CXk2kgsmRpMOxTUQrPacw eEyg== X-Gm-Message-State: AOAM530MEDl019h/hNc7qiUK4C1IyEVQ4yqbFD8aw+Nn/YdiBwhj4O6l WXM+Qc/NCW7yN6IyWGSFTkGoiQfo9rV76g== X-Google-Smtp-Source: ABdhPJyzj5ZgTp+gb8QvBwuQ13oGjSakoEQKwsJtODfT4jQP4+gTPfRbXLd4dmvZLBUl/bDWaWgdFg== X-Received: by 2002:a1c:498b:: with SMTP id w133mr4199295wma.134.1615296403168; Tue, 09 Mar 2021 05:26:43 -0800 (PST) Received: from debian-brgl.home (amarseille-656-1-4-167.w90-8.abo.wanadoo.fr. [90.8.158.167]) by smtp.gmail.com with ESMTPSA id u23sm4123366wmn.26.2021.03.09.05.26.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Mar 2021 05:26:42 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski Subject: [libgpiod][PATCH 3/6] core: switch to reference counting for gpio chip objects Date: Tue, 9 Mar 2021 14:26:36 +0100 Message-Id: <20210309132639.29069-4-brgl@bgdev.pl> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210309132639.29069-1-brgl@bgdev.pl> References: <20210309132639.29069-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski 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. Signed-off-by: Bartosz Golaszewski --- 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(-) diff --git a/bindings/cxx/chip.cpp b/bindings/cxx/chip.cpp index 527fb34..0886717 100644 --- a/bindings/cxx/chip.cpp +++ b/bindings/cxx/chip.cpp @@ -13,7 +13,7 @@ namespace { void chip_deleter(::gpiod_chip* chip) { - ::gpiod_chip_close(chip); + ::gpiod_chip_unref(chip); } } /* namespace */ diff --git a/bindings/python/gpiodmodule.c b/bindings/python/gpiodmodule.c index ac0fd83..e54c3ad 100644 --- a/bindings/python/gpiodmodule.c +++ b/bindings/python/gpiodmodule.c @@ -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; diff --git a/include/gpiod.h b/include/gpiod.h index 6ed9c6c..71abb2a 100644 --- a/include/gpiod.h +++ b/include/gpiod.h @@ -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. diff --git a/lib/core.c b/lib/core.c index 077ea3e..bab438f 100644 --- a/lib/core.c +++ b/lib/core.c @@ -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]; diff --git a/tests/gpiod-test.h b/tests/gpiod-test.h index 2688d3c..a093f83 100644 --- a/tests/gpiod-test.h +++ b/tests/gpiod-test.h @@ -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. */ diff --git a/tools/gpiodetect.c b/tools/gpiodetect.c index 7a2015e..9f4c28c 100644 --- a/tools/gpiodetect.c +++ b/tools/gpiodetect.c @@ -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]); } diff --git a/tools/gpiofind.c b/tools/gpiofind.c index 0fc07d9..83af76b 100644 --- a/tools/gpiofind.c +++ b/tools/gpiofind.c @@ -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; } } diff --git a/tools/gpioget.c b/tools/gpioget.c index 527dc22..ceeec56 100644 --- a/tools/gpioget.c +++ b/tools/gpioget.c @@ -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); diff --git a/tools/gpioinfo.c b/tools/gpioinfo.c index 84588bc..eba8c72 100644 --- a/tools/gpioinfo.c +++ b/tools/gpioinfo.c @@ -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); } } diff --git a/tools/gpiomon.c b/tools/gpiomon.c index 8bf2c70..dda9f6f 100644 --- a/tools/gpiomon.c +++ b/tools/gpiomon.c @@ -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; } diff --git a/tools/gpioset.c b/tools/gpioset.c index 7e9d88f..a088ec6 100644 --- a/tools/gpioset.c +++ b/tools/gpioset.c @@ -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); From patchwork Tue Mar 9 13:26:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 396328 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4186BC4332B for ; Tue, 9 Mar 2021 13:27:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1DB1A650E6 for ; Tue, 9 Mar 2021 13:27:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230414AbhCIN0r (ORCPT ); Tue, 9 Mar 2021 08:26:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231464AbhCIN0p (ORCPT ); Tue, 9 Mar 2021 08:26:45 -0500 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 009F7C06174A for ; Tue, 9 Mar 2021 05:26:45 -0800 (PST) Received: by mail-wr1-x436.google.com with SMTP id l12so15543460wry.2 for ; Tue, 09 Mar 2021 05:26:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=qEWobHGIY7KquDfwJRWAwRxBYgGjcQ9monRMXSr9aiE=; b=2F6xVDgvUOIb6h/NHrC5zlUd6IixC0XxrtL6uObO7QyNC9Sg7EcYeA2wcaCTN7NZ8C o6hVTa3HJ6odQjLI8Ovf3g6vb5yL8wCFLllP5hQLzaSDNrjynSSSEiClNR8Dvy0KzWGM JVjzMkfeCpx6d/cWhpDJZ6G6KwNgfbrrhs2OZmdnRCs/pkaxQXWQ8HTN4PdOjKYfs0Sv y3kDsm9Lign5cPKiHW4jWeWJwBr0Uyj87Yr8aNxCdFvYlwNR7c1W2OnWv+0rg2k+Ho/B 4ZeZmzW+zEkhYq/7Qnktf6HqjzZduN3qVXgRoDU+HkrU7zXkXRCgBi/JBTqpQoTJI8m4 XSwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=qEWobHGIY7KquDfwJRWAwRxBYgGjcQ9monRMXSr9aiE=; b=DmscZbnVdwhT7nfOxy89sQOZaKHd4QLEWW/1jaa5j5cGgZqgXt93WJCgxzAqNagWcS TZBhl5C/Mxr3q4BVVuMQeU+Iu9NBaVUmAMmDRlllnsbxwM9kUZJYVJQRjqQVX2cnw2m7 9+Q1cIFVejisjMlbc4eNOLeZjmBPGw5pr/qRxOzg269u6UDcAlYiCqBQcyDWNjJYznrI d8c6kUKvcICfS3jeS5572GEAsXaIorRbcx+m+q/V04tixaDZhvOswQm5d7qyaEmKna2s kFR7NMI1h+KtR37HpRIL3tOKOe5pwMDrrDOL4YmO2GHAiDE9VI4sgEZbD2MwN4CV9BLQ Szwg== X-Gm-Message-State: AOAM532tCpQ/20VoR4T4MsGf9ek57OV0E93yoWQJqBm0HRmiK2qNY7+0 RQLymB3jKedos/9YRcm/CiVSZw== X-Google-Smtp-Source: ABdhPJyRmWvt/Bk8lWQWiAnP7XTXUK6V5eOYWyD6pYB1J3EHROv05lMLKClEnDf90y+dJdq3S2HgAg== X-Received: by 2002:a5d:534b:: with SMTP id t11mr27666768wrv.186.1615296403744; Tue, 09 Mar 2021 05:26:43 -0800 (PST) Received: from debian-brgl.home (amarseille-656-1-4-167.w90-8.abo.wanadoo.fr. [90.8.158.167]) by smtp.gmail.com with ESMTPSA id u23sm4123366wmn.26.2021.03.09.05.26.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Mar 2021 05:26:43 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski Subject: [libgpiod][PATCH 4/6] treewide: remove is_requested() and is_free() Date: Tue, 9 Mar 2021 14:26:37 +0100 Message-Id: <20210309132639.29069-5-brgl@bgdev.pl> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210309132639.29069-1-brgl@bgdev.pl> References: <20210309132639.29069-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski 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). Signed-off-by: Bartosz Golaszewski --- 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(-) diff --git a/bindings/cxx/gpiod.hpp b/bindings/cxx/gpiod.hpp index 3a043a1..189a133 100644 --- a/bindings/cxx/gpiod.hpp +++ b/bindings/cxx/gpiod.hpp @@ -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). diff --git a/bindings/cxx/line.cpp b/bindings/cxx/line.cpp index 1d114ac..058f7ce 100644 --- a/bindings/cxx/line.cpp +++ b/bindings/cxx/line.cpp @@ -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 diff --git a/bindings/cxx/tests/tests-line.cpp b/bindings/cxx/tests/tests-line.cpp index 648012a..17fdd89 100644 --- a/bindings/cxx/tests/tests-line.cpp +++ b/bindings/cxx/tests/tests-line.cpp @@ -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); } diff --git a/bindings/python/gpiodmodule.c b/bindings/python/gpiodmodule.c index e54c3ad..12a6867 100644 --- a/bindings/python/gpiodmodule.c +++ b/bindings/python/gpiodmodule.c @@ -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, diff --git a/bindings/python/tests/gpiod_py_test.py b/bindings/python/tests/gpiod_py_test.py index b7c30de..d448776 100755 --- a/bindings/python/tests/gpiod_py_test.py +++ b/bindings/python/tests/gpiod_py_test.py @@ -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 diff --git a/include/gpiod.h b/include/gpiod.h index 71abb2a..a5e09e2 100644 --- a/include/gpiod.h +++ b/include/gpiod.h @@ -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; - /** * @} * diff --git a/lib/core.c b/lib/core.c index bab438f..8e3d8a7 100644 --- a/lib/core.c +++ b/lib/core.c @@ -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); From patchwork Tue Mar 9 13:26:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 397032 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 90AE0C433DB for ; Tue, 9 Mar 2021 13:27:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 31D31650F5 for ; Tue, 9 Mar 2021 13:27:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230495AbhCIN1S (ORCPT ); Tue, 9 Mar 2021 08:27:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231481AbhCIN0p (ORCPT ); Tue, 9 Mar 2021 08:26:45 -0500 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 83B79C06174A for ; Tue, 9 Mar 2021 05:26:45 -0800 (PST) Received: by mail-wr1-x42d.google.com with SMTP id l11so12155715wrp.7 for ; Tue, 09 Mar 2021 05:26:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Gmsp8IZDH53kiko9aSQ4QmCnUqJM7FgPbxSSXJNQk4s=; b=G9CYXz4fiOsmUk423D9in8c2LrqP168RM+D5jDjjiR49E6X8zdF9y6y59EQgXGK8GY Zv9PljtqubthW81T0i6SPXonmunV0df3U0bkthmVQAvmIr5VFgr7RmYIAOV+AMlLJW+q JF92KckmnOViRur2Ug/LumUfp+NZnFqOH3ThYctkaPWSVkfow0IoXCqMz6w8cB3lDGIR XYjBOPJDP0sOuCf1ZXoc4vX5M3IwXOBtaBfLX5DXivRVsyr6VZ2xtKcdwYG0TM1agyOu 0ReWNNRsUwiY+CGyzRgml+ZQnh1KIh19+yMINkJ41sdjUZK6EyKmbuAtqqCG0P6QwxC6 35oQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Gmsp8IZDH53kiko9aSQ4QmCnUqJM7FgPbxSSXJNQk4s=; b=gkPuS4V3isiDrcS+CQsjo/IkI2m0/FB4RtyEIhG3brmuVrqcASWgNGHX+0Vnej3TD7 KfEIE8SeJWYR/P+WVPQoWwm5X+BiwzDfRAdssY8wbvWvNDzJpet9d5oYYOjNHjNhVs41 PhSuP8u2iyOZHKlAbBqmxAgsyPbK5D9vdfTV3YSHKmu8Ih4Ip+JQxvpkqR6mI8l6T+hH YcEVgbmItGZSZ6zhkT3bw4U2+ebwKDW4I5kW0mha4CVPkk9uSPqQiBgJRtM4W8k4Ufl2 Ql/o6+oD6xlLQXWsLDRCbAci2ttcEcHF6/JdFnjh0LjkGCxX3T2yOnKmu3K00aSMGhYR bI9Q== X-Gm-Message-State: AOAM532ZqhyatfbZmMr8H0aANEVF8V4rW+EfnmdiI3jT1Ij/Sua+EvS3 zJmhFqYqe7J4nn48nluZ9crC9g== X-Google-Smtp-Source: ABdhPJzH/RtUGbBqNh5Is1mIzPBF0pb2i+wHmQUmP4AfVm7YIC51mYcMIVHEYVnGPd6idoyuh70XhQ== X-Received: by 2002:a5d:5104:: with SMTP id s4mr29707026wrt.62.1615296404319; Tue, 09 Mar 2021 05:26:44 -0800 (PST) Received: from debian-brgl.home (amarseille-656-1-4-167.w90-8.abo.wanadoo.fr. [90.8.158.167]) by smtp.gmail.com with ESMTPSA id u23sm4123366wmn.26.2021.03.09.05.26.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Mar 2021 05:26:44 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski Subject: [libgpiod][PATCH 5/6] treewide: kill line updating Date: Tue, 9 Mar 2021 14:26:38 +0100 Message-Id: <20210309132639.29069-6-brgl@bgdev.pl> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210309132639.29069-1-brgl@bgdev.pl> References: <20210309132639.29069-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski 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(). Signed-off-by: Bartosz Golaszewski --- 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(-) diff --git a/bindings/cxx/gpiod.hpp b/bindings/cxx/gpiod.hpp index 189a133..003acdd 100644 --- a/bindings/cxx/gpiod.hpp +++ b/bindings/cxx/gpiod.hpp @@ -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. * diff --git a/bindings/cxx/line.cpp b/bindings/cxx/line.cpp index 058f7ce..bf84867 100644 --- a/bindings/cxx/line.cpp +++ b/bindings/cxx/line.cpp @@ -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; diff --git a/bindings/cxx/tests/tests-line.cpp b/bindings/cxx/tests/tests-line.cpp index 17fdd89..ababf8b 100644 --- a/bindings/cxx/tests/tests-line.cpp +++ b/bindings/cxx/tests/tests-line.cpp @@ -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]") diff --git a/bindings/python/gpiodmodule.c b/bindings/python/gpiodmodule.c index 12a6867..8bfb4c4 100644 --- a/bindings/python/gpiodmodule.c +++ b/bindings/python/gpiodmodule.c @@ -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, diff --git a/bindings/python/tests/gpiod_py_test.py b/bindings/python/tests/gpiod_py_test.py index d448776..f93c72c 100755 --- a/bindings/python/tests/gpiod_py_test.py +++ b/bindings/python/tests/gpiod_py_test.py @@ -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, ) diff --git a/include/gpiod.h b/include/gpiod.h index a5e09e2..0ee9778 100644 --- a/include/gpiod.h +++ b/include/gpiod.h @@ -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. diff --git a/lib/core.c b/lib/core.c index 8e3d8a7..8fdc503 100644 --- a/lib/core.c +++ b/lib/core.c @@ -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; } From patchwork Tue Mar 9 13:26:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 396327 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 200DFC433E6 for ; Tue, 9 Mar 2021 13:27:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AFF8D64EBB for ; Tue, 9 Mar 2021 13:27:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231162AbhCIN1S (ORCPT ); Tue, 9 Mar 2021 08:27:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230116AbhCIN0s (ORCPT ); Tue, 9 Mar 2021 08:26:48 -0500 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C7F6C06174A for ; Tue, 9 Mar 2021 05:26:47 -0800 (PST) Received: by mail-wm1-x332.google.com with SMTP id i9so1857876wml.0 for ; Tue, 09 Mar 2021 05:26:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=QuIht14uLLmapAMbskQwEfod4dUk+kZtfqeNlH73Ryw=; b=btKML2NrQbD81W3gZOB1OVwk7LpvThDjqqh6J7qrQ0HkmAEFMrOSW6dXt+lhPgtNm0 hOVUQu6mRdw6Ne4z+eeWWgEPVRGZorpddTWx2ltQ44CssYAk8TaqtbCPP5I4jLMab28o olM0Xf5ZLPMcSrcYePfit/gGktSN38JGbrdsyWDGPZ2Qb0QdAMnPINAJXKr57Sagcn7j rBdA2MI+X1eeuH7SrJ9ggtssOjY8l3vRsgS6SQwcMQxpBLMum+x1FrsWH2u+/yHziEKU WMVmK70a8IP18rXio7u77+iGLFcciuZHlkf+kXGuuIvpwUBvknHOOb8o/JN8pHp5c9TP 4Rtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=QuIht14uLLmapAMbskQwEfod4dUk+kZtfqeNlH73Ryw=; b=YyUSNW20hhgHeCMmE6mhiCV/LWoUaQUjQCGgH2ww0xYS9q/qJs/nwmUHH3KhuLUO1s 8WM1Xgh43YpEL9M/QO0/iSvba2IbmwDK4C0bX7HELT08NqmdIG1AE8pQ7zjBul0L7dEc YNV84uaapt3nNp5tIoiZntRSh00U2NnS4BATB5M9TlWzXo7+IFfBiH+MdZ/cYiDGGyv7 D9E3JX/zLMEN0xSDD2yJiSLBPhwuOUEH6vqvlI+p565nzCuTPDi3lMUvYkGhrs9Q3yyQ d88/mbgjtYoOI/AU5S3Y1mlniCjLzNWzn/UNDtMuP+1YLu0cfsXu50XGXFq4Iwamrr3i sNLQ== X-Gm-Message-State: AOAM530iBHz/xXoIN7EUffMcWo52FM50diUnvyX9R4ahSDmhACzME5Xr iVWxDNPgCCUdQSwkHiPfoJrrcQUC7TnHNA== X-Google-Smtp-Source: ABdhPJzfZbsUy5Sgi7GaXltJTduUVNUeHhCLvlUi+34lrl7NppgIc3Vl0YMUynHlfY8eop0v7Yj9ow== X-Received: by 2002:a7b:c316:: with SMTP id k22mr4014012wmj.176.1615296405133; Tue, 09 Mar 2021 05:26:45 -0800 (PST) Received: from debian-brgl.home (amarseille-656-1-4-167.w90-8.abo.wanadoo.fr. [90.8.158.167]) by smtp.gmail.com with ESMTPSA id u23sm4123366wmn.26.2021.03.09.05.26.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Mar 2021 05:26:44 -0800 (PST) From: Bartosz Golaszewski To: Kent Gibson , Linus Walleij , Andy Shevchenko Cc: linux-gpio@vger.kernel.org, Bartosz Golaszewski Subject: [libgpiod][PATCH 6/6] core: hide the GPIOD_API symbol Date: Tue, 9 Mar 2021 14:26:39 +0100 Message-Id: <20210309132639.29069-7-brgl@bgdev.pl> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210309132639.29069-1-brgl@bgdev.pl> References: <20210309132639.29069-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski 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. Signed-off-by: Bartosz Golaszewski --- 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 diff --git a/bindings/cxx/Makefile.am b/bindings/cxx/Makefile.am index 655d1cd..d9fa577 100644 --- a/bindings/cxx/Makefile.am +++ b/bindings/cxx/Makefile.am @@ -2,7 +2,7 @@ # SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski 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)) diff --git a/bindings/cxx/chip.cpp b/bindings/cxx/chip.cpp index 0886717..d5a9837 100644 --- a/bindings/cxx/chip.cpp +++ b/bindings/cxx/chip.cpp @@ -7,41 +7,43 @@ #include #include +#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& offsets) const +GPIOD_CXX_API line_bulk chip::get_lines(const ::std::vector& offsets) const { line_bulk lines; @@ -116,7 +118,7 @@ line_bulk chip::get_lines(const ::std::vector& 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"); diff --git a/bindings/cxx/gpiod.hpp b/bindings/cxx/gpiod.hpp index 003acdd..e3ce2fc 100644 --- a/bindings/cxx/gpiod.hpp +++ b/bindings/cxx/gpiod.hpp @@ -34,7 +34,7 @@ struct line_event; * @return True if the file exists and is a GPIO chip character device or a * symbolic link to it. */ -bool is_gpiochip_device(const ::std::string& path) GPIOD_API; +bool is_gpiochip_device(const ::std::string& path); /** * @brief Represents a GPIO chip. @@ -50,44 +50,44 @@ public: /** * @brief Default constructor. Creates an empty GPIO chip object. */ - GPIOD_API chip(void) = default; + chip(void) = default; /** * @brief Constructor. Opens the chip using chip::open. * @param path Path to the GPIO chip device. */ - GPIOD_API chip(const ::std::string& path); + chip(const ::std::string& path); /** * @brief Copy constructor. References the object held by other. * @param other Other chip object. */ - GPIOD_API chip(const chip& other) = default; + chip(const chip& other) = default; /** * @brief Move constructor. References the object held by other. * @param other Other chip object. */ - GPIOD_API chip(chip&& other) = default; + chip(chip&& other) = default; /** * @brief Assignment operator. References the object held by other. * @param other Other chip object. * @return Reference to this object. */ - GPIOD_API chip& operator=(const chip& other) = default; + chip& operator=(const chip& other) = default; /** * @brief Move assignment operator. References the object held by other. * @param other Other chip object. * @return Reference to this object. */ - GPIOD_API chip& operator=(chip&& other) = default; + chip& operator=(chip&& other) = default; /** * @brief Destructor. Unreferences the internal chip object. */ - GPIOD_API ~chip(void) = default; + ~chip(void) = default; /** * @brief Open a GPIO chip. @@ -96,37 +96,37 @@ public: * If the object already holds a reference to an open chip, it will be * closed and the reference reset. */ - GPIOD_API void open(const ::std::string &path); + void open(const ::std::string &path); /** * @brief Reset the internal smart pointer owned by this object. */ - GPIOD_API void reset(void) noexcept; + void reset(void) noexcept; /** * @brief Return the name of the chip held by this object. * @return Name of the GPIO chip. */ - GPIOD_API ::std::string name(void) const; + ::std::string name(void) const; /** * @brief Return the label of the chip held by this object. * @return Label of the GPIO chip. */ - GPIOD_API ::std::string label(void) const; + ::std::string label(void) const; /** * @brief Return the number of lines exposed by this chip. * @return Number of lines. */ - GPIOD_API unsigned int num_lines(void) const; + unsigned int num_lines(void) const; /** * @brief Get the line exposed by this chip at given offset. * @param offset Offset of the line. * @return Line object. */ - GPIOD_API line get_line(unsigned int offset) const; + line get_line(unsigned int offset) const; /** * @brief Map a GPIO line's name to its offset within the chip. @@ -134,46 +134,46 @@ public: * @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; + int find_line(const ::std::string& name) const; /** * @brief Get a set of lines exposed by this chip at given offsets. * @param offsets Vector of line offsets. * @return Set of lines held by a line_bulk object. */ - GPIOD_API line_bulk get_lines(const ::std::vector& offsets) const; + line_bulk get_lines(const ::std::vector& offsets) const; /** * @brief Get all lines exposed by this chip. * @return All lines exposed by this chip held by a line_bulk object. */ - GPIOD_API line_bulk get_all_lines(void) const; + line_bulk get_all_lines(void) const; /** * @brief Equality operator. * @param rhs Right-hand side of the equation. * @return True if rhs references the same chip. False otherwise. */ - GPIOD_API bool operator==(const chip& rhs) const noexcept; + bool operator==(const chip& rhs) const noexcept; /** * @brief Inequality operator. * @param rhs Right-hand side of the equation. * @return False if rhs references the same chip. True otherwise. */ - GPIOD_API bool operator!=(const chip& rhs) const noexcept; + bool operator!=(const chip& rhs) const noexcept; /** * @brief Check if this object holds a reference to a GPIO chip. * @return True if this object references a GPIO chip, false otherwise. */ - GPIOD_API explicit operator bool(void) const noexcept; + explicit operator bool(void) const noexcept; /** * @brief Check if this object doesn't hold a reference to a GPIO chip. * @return False if this object references a GPIO chip, true otherwise. */ - GPIOD_API bool operator!(void) const noexcept; + bool operator!(void) const noexcept; private: @@ -212,17 +212,17 @@ struct line_request /**< Listen for all types of events. */ }; - GPIOD_API static const ::std::bitset<32> FLAG_ACTIVE_LOW; + static const ::std::bitset<32> FLAG_ACTIVE_LOW; /**< Set the active state to 'low' (high is the default). */ - GPIOD_API static const ::std::bitset<32> FLAG_OPEN_SOURCE; + static const ::std::bitset<32> FLAG_OPEN_SOURCE; /**< The line is an open-source port. */ - GPIOD_API static const ::std::bitset<32> FLAG_OPEN_DRAIN; + static const ::std::bitset<32> FLAG_OPEN_DRAIN; /**< The line is an open-drain port. */ - GPIOD_API static const ::std::bitset<32> FLAG_BIAS_DISABLED; + static const ::std::bitset<32> FLAG_BIAS_DISABLED; /**< The line has neither pull-up nor pull-down resistor enabled. */ - GPIOD_API static const ::std::bitset<32> FLAG_BIAS_PULL_DOWN; + static const ::std::bitset<32> FLAG_BIAS_PULL_DOWN; /**< The line has a configurable pull-down resistor enabled. */ - GPIOD_API static const ::std::bitset<32> FLAG_BIAS_PULL_UP; + static const ::std::bitset<32> FLAG_BIAS_PULL_UP; /**< The line has a configurable pull-up resistor enabled. */ ::std::string consumer; @@ -247,112 +247,112 @@ public: /** * @brief Default constructor. Creates an empty line object. */ - GPIOD_API line(void); + line(void); /** * @brief Copy constructor. * @param other Other line object. */ - GPIOD_API line(const line& other) = default; + line(const line& other) = default; /** * @brief Move constructor. * @param other Other line object. */ - GPIOD_API line(line&& other) = default; + line(line&& other) = default; /** * @brief Assignment operator. * @param other Other line object. * @return Reference to this object. */ - GPIOD_API line& operator=(const line& other) = default; + line& operator=(const line& other) = default; /** * @brief Move assignment operator. * @param other Other line object. * @return Reference to this object. */ - GPIOD_API line& operator=(line&& other) = default; + line& operator=(line&& other) = default; /** * @brief Destructor. */ - GPIOD_API ~line(void) = default; + ~line(void) = default; /** * @brief Get the offset of this line. * @return Offet of this line. */ - GPIOD_API unsigned int offset(void) const; + unsigned int offset(void) const; /** * @brief Get the name of this line (if any). * @return Name of this line or an empty string if it is unnamed. */ - GPIOD_API ::std::string name(void) const; + ::std::string name(void) const; /** * @brief Get the consumer of this line (if any). * @return Name of the consumer of this line or an empty string if it * is unused. */ - GPIOD_API ::std::string consumer(void) const; + ::std::string consumer(void) const; /** * @brief Get current direction of this line. * @return Current direction setting. */ - GPIOD_API int direction(void) const; + int direction(void) const; /** * @brief Check if this line's signal is inverted. * @return True if this line is "active-low", false otherwise. */ - GPIOD_API bool is_active_low(void) const; + bool is_active_low(void) const; /** * @brief Get current bias of this line. * @return Current bias setting. */ - GPIOD_API int bias(void) const; + int bias(void) const; /** * @brief Check if this line is used by the kernel or other user space * process. * @return True if this line is in use, false otherwise. */ - GPIOD_API bool is_used(void) const; + bool is_used(void) const; /** * @brief Get current drive setting of this line. * @return Current drive setting. */ - GPIOD_API int drive(void) const; + int drive(void) const; /** * @brief Request this line. * @param config Request config (see gpiod::line_request). * @param default_val Default value - only matters for OUTPUT direction. */ - GPIOD_API void request(const line_request& config, int default_val = 0) const; + void request(const line_request& config, int default_val = 0) const; /** * @brief Release the line if it was previously requested. */ - GPIOD_API void release(void) const; + void release(void) const; /** * @brief Read the line value. * @return Current value (0 or 1). */ - GPIOD_API int get_value(void) const; + int get_value(void) const; /** * @brief Set the value of this line. * @param val New value (0 or 1). */ - GPIOD_API void set_value(int val) const; + void set_value(int val) const; /** * @brief Set configuration of this line. @@ -360,25 +360,24 @@ public: * @param flags Replacement flags. * @param value New value (0 or 1) - only matters for OUTPUT direction. */ - GPIOD_API void set_config(int direction, ::std::bitset<32> flags, - int value = 0) const; + void set_config(int direction, ::std::bitset<32> flags, int value = 0) const; /** * @brief Set configuration flags of this line. * @param flags Replacement flags. */ - GPIOD_API void set_flags(::std::bitset<32> flags) const; + void set_flags(::std::bitset<32> flags) const; /** * @brief Change the direction this line to input. */ - GPIOD_API void set_direction_input() const; + void set_direction_input() const; /** * @brief Change the direction this lines to output. * @param value New value (0 or 1). */ - GPIOD_API void set_direction_output(int value = 0) const; + void set_direction_output(int value = 0) const; /** * @brief Wait for an event on this line. @@ -386,31 +385,31 @@ public: * @return True if an event occurred and can be read, false if the wait * timed out. */ - GPIOD_API bool event_wait(const ::std::chrono::nanoseconds& timeout) const; + bool event_wait(const ::std::chrono::nanoseconds& timeout) const; /** * @brief Read a line event. * @return Line event object. */ - GPIOD_API line_event event_read(void) const; + line_event event_read(void) const; /** * @brief Read multiple line events. * @return Vector of line event objects. */ - GPIOD_API ::std::vector event_read_multiple(void) const; + ::std::vector event_read_multiple(void) const; /** * @brief Get the event file descriptor associated with this line. * @return File descriptor number. */ - GPIOD_API int event_get_fd(void) const; + int event_get_fd(void) const; /** * @brief Get the parent chip. * @return Parent chip of this line. */ - GPIOD_API const chip get_chip(void) const; + const chip get_chip(void) const; /** * @brief Reset the state of this object. @@ -419,34 +418,34 @@ public: * but wants to drop the reference to the GPIO chip indirectly held by * the line being the source of the event. */ - GPIOD_API void reset(void); + void reset(void); /** * @brief Check if two line objects reference the same GPIO line. * @param rhs Right-hand side of the equation. * @return True if both objects reference the same line, fale otherwise. */ - GPIOD_API bool operator==(const line& rhs) const noexcept; + bool operator==(const line& rhs) const noexcept; /** * @brief Check if two line objects reference different GPIO lines. * @param rhs Right-hand side of the equation. * @return False if both objects reference the same line, true otherwise. */ - GPIOD_API bool operator!=(const line& rhs) const noexcept; + bool operator!=(const line& rhs) const noexcept; /** * @brief Check if this object holds a reference to any GPIO line. * @return True if this object references a GPIO line, false otherwise. */ - GPIOD_API explicit operator bool(void) const noexcept; + explicit operator bool(void) const noexcept; /** * @brief Check if this object doesn't reference any GPIO line. * @return True if this object doesn't reference any GPIO line, true * otherwise. */ - GPIOD_API bool operator!(void) const noexcept; + bool operator!(void) const noexcept; /** * @brief Possible direction settings. @@ -550,45 +549,45 @@ public: /** * @brief Default constructor. Creates an empty line_bulk object. */ - GPIOD_API line_bulk(void) = default; + line_bulk(void) = default; /** * @brief Construct a line_bulk from a vector of lines. * @param lines Vector of gpiod::line objects. * @note All lines must be owned by the same GPIO chip. */ - GPIOD_API line_bulk(const ::std::vector& lines); + line_bulk(const ::std::vector& lines); /** * @brief Copy constructor. * @param other Other line_bulk object. */ - GPIOD_API line_bulk(const line_bulk& other) = default; + line_bulk(const line_bulk& other) = default; /** * @brief Move constructor. * @param other Other line_bulk object. */ - GPIOD_API line_bulk(line_bulk&& other) = default; + line_bulk(line_bulk&& other) = default; /** * @brief Assignment operator. * @param other Other line_bulk object. * @return Reference to this object. */ - GPIOD_API line_bulk& operator=(const line_bulk& other) = default; + line_bulk& operator=(const line_bulk& other) = default; /** * @brief Move assignment operator. * @param other Other line_bulk object. * @return Reference to this object. */ - GPIOD_API line_bulk& operator=(line_bulk&& other) = default; + line_bulk& operator=(line_bulk&& other) = default; /** * @brief Destructor. */ - GPIOD_API ~line_bulk(void) = default; + ~line_bulk(void) = default; /** * @brief Add a line to this line_bulk object. @@ -596,7 +595,7 @@ public: * @note The new line must be owned by the same chip as all the other * lines already held by this line_bulk object. */ - GPIOD_API void append(const line& new_line); + void append(const line& new_line); /** * @brief Get the line at given offset. @@ -605,7 +604,7 @@ public: * @note This method will throw if index is equal or greater than the * number of lines currently held by this bulk. */ - GPIOD_API line& get(unsigned int index); + line& get(unsigned int index); /** * @brief Get the line at given offset without bounds checking. @@ -613,24 +612,24 @@ public: * @return Reference to the line object. * @note No bounds checking is performed. */ - GPIOD_API line& operator[](unsigned int index); + line& operator[](unsigned int index); /** * @brief Get the number of lines currently held by this object. * @return Number of elements in this line_bulk. */ - GPIOD_API unsigned int size(void) const noexcept; + unsigned int size(void) const noexcept; /** * @brief Check if this line_bulk doesn't hold any lines. * @return True if this object is empty, false otherwise. */ - GPIOD_API bool empty(void) const noexcept; + bool empty(void) const noexcept; /** * @brief Remove all lines from this object. */ - GPIOD_API void clear(void); + void clear(void); /** * @brief Request all lines held by this object. @@ -638,27 +637,27 @@ public: * @param default_vals Vector of default values. Only relevant for * output direction requests. */ - GPIOD_API void request(const line_request& config, - const ::std::vector default_vals = ::std::vector()) const; + void request(const line_request& config, + const ::std::vector default_vals = ::std::vector()) const; /** * @brief Release all lines held by this object. */ - GPIOD_API void release(void) const; + void release(void) const; /** * @brief Read values from all lines held by this object. * @return Vector containing line values the order of which corresponds * with the order of lines in the internal array. */ - GPIOD_API ::std::vector get_values(void) const; + ::std::vector get_values(void) const; /** * @brief Set values of all lines held by this object. * @param values Vector of values to set. Must be the same size as the * number of lines held by this line_bulk. */ - GPIOD_API void set_values(const ::std::vector& values) const; + void set_values(const ::std::vector& values) const; /** * @brief Set configuration of all lines held by this object. @@ -668,26 +667,26 @@ public: * number of lines held by this line_bulk. * Only relevant for output direction requests. */ - GPIOD_API void set_config(int direction, ::std::bitset<32> flags, - const ::std::vector values = ::std::vector()) const; + void set_config(int direction, ::std::bitset<32> flags, + const ::std::vector values = ::std::vector()) const; /** * @brief Set configuration flags of all lines held by this object. * @param flags Replacement flags. */ - GPIOD_API void set_flags(::std::bitset<32> flags) const; + void set_flags(::std::bitset<32> flags) const; /** * @brief Change the direction all lines held by this object to input. */ - GPIOD_API void set_direction_input() const; + void set_direction_input() const; /** * @brief Change the direction all lines held by this object to output. * @param values Vector of values to set. Must be the same size as the * number of lines held by this line_bulk. */ - GPIOD_API void set_direction_output(const ::std::vector& values) const; + void set_direction_output(const ::std::vector& values) const; /** * @brief Poll the set of lines for line events. @@ -696,24 +695,24 @@ public: * @return Returns a line_bulk object containing lines on which events * occurred. */ - GPIOD_API line_bulk event_wait(const ::std::chrono::nanoseconds& timeout) const; + line_bulk event_wait(const ::std::chrono::nanoseconds& timeout) const; /** * @brief Check if this object holds any lines. * @return True if this line_bulk holds at least one line, false otherwise. */ - GPIOD_API explicit operator bool(void) const noexcept; + explicit operator bool(void) const noexcept; /** * @brief Check if this object doesn't hold any lines. * @return True if this line_bulk is empty, false otherwise. */ - GPIOD_API bool operator!(void) const noexcept; + bool operator!(void) const noexcept; /** * @brief Max number of lines that this object can hold. */ - GPIOD_API static const unsigned int MAX_LINES; + static const unsigned int MAX_LINES; /** * @brief Iterator for iterating over lines held by line_bulk. @@ -725,56 +724,56 @@ public: /** * @brief Default constructor. Builds an empty iterator object. */ - GPIOD_API iterator(void) = default; + iterator(void) = default; /** * @brief Copy constructor. * @param other Other line_bulk iterator. */ - GPIOD_API iterator(const iterator& other) = default; + iterator(const iterator& other) = default; /** * @brief Move constructor. * @param other Other line_bulk iterator. */ - GPIOD_API iterator(iterator&& other) = default; + iterator(iterator&& other) = default; /** * @brief Assignment operator. * @param other Other line_bulk iterator. * @return Reference to this iterator. */ - GPIOD_API iterator& operator=(const iterator& other) = default; + iterator& operator=(const iterator& other) = default; /** * @brief Move assignment operator. * @param other Other line_bulk iterator. * @return Reference to this iterator. */ - GPIOD_API iterator& operator=(iterator&& other) = default; + iterator& operator=(iterator&& other) = default; /** * @brief Destructor. */ - GPIOD_API ~iterator(void) = default; + ~iterator(void) = default; /** * @brief Advance the iterator by one element. * @return Reference to this iterator. */ - GPIOD_API iterator& operator++(void); + iterator& operator++(void); /** * @brief Dereference current element. * @return Current GPIO line by reference. */ - GPIOD_API const line& operator*(void) const; + const line& operator*(void) const; /** * @brief Member access operator. * @return Current GPIO line by pointer. */ - GPIOD_API const line* operator->(void) const; + const line* operator->(void) const; /** * @brief Check if this operator points to the same element. @@ -782,7 +781,7 @@ public: * @return True if this iterator points to the same GPIO line, * false otherwise. */ - GPIOD_API bool operator==(const iterator& rhs) const noexcept; + bool operator==(const iterator& rhs) const noexcept; /** * @brief Check if this operator doesn't point to the same element. @@ -790,7 +789,7 @@ public: * @return True if this iterator doesn't point to the same GPIO * line, false otherwise. */ - GPIOD_API bool operator!=(const iterator& rhs) const noexcept; + bool operator!=(const iterator& rhs) const noexcept; private: @@ -805,13 +804,13 @@ public: * @brief Returns an iterator to the first line. * @return A line_bulk iterator. */ - GPIOD_API iterator begin(void) noexcept; + iterator begin(void) noexcept; /** * @brief Returns an iterator to the element following the last line. * @return A line_bulk iterator. */ - GPIOD_API iterator end(void) noexcept; + iterator end(void) noexcept; private: @@ -835,14 +834,14 @@ private: * @param iter A line iterator. * @return Iterator unchanged. */ -GPIOD_API line_iter begin(line_iter iter) noexcept; +line_iter begin(line_iter iter) noexcept; /** * @brief Support for range-based loops for line iterators. * @param iter A line iterator. * @return New end iterator. */ -GPIOD_API line_iter end(const line_iter& iter) noexcept; +line_iter end(const line_iter& iter) noexcept; /** * @brief Allows to iterate over all lines owned by a GPIO chip. @@ -854,62 +853,62 @@ public: /** * @brief Default constructor. Creates the end iterator. */ - GPIOD_API line_iter(void) = default; + line_iter(void) = default; /** * @brief Constructor. Creates the begin iterator. * @param owner Chip owning the GPIO lines over which we want to iterate. */ - GPIOD_API line_iter(const chip& owner); + line_iter(const chip& owner); /** * @brief Copy constructor. * @param other Other line iterator. */ - GPIOD_API line_iter(const line_iter& other) = default; + line_iter(const line_iter& other) = default; /** * @brief Move constructor. * @param other Other line iterator. */ - GPIOD_API line_iter(line_iter&& other) = default; + line_iter(line_iter&& other) = default; /** * @brief Assignment operator. * @param other Other line iterator. * @return Reference to this line_iter. */ - GPIOD_API line_iter& operator=(const line_iter& other) = default; + line_iter& operator=(const line_iter& other) = default; /** * @brief Move assignment operator. * @param other Other line iterator. * @return Reference to this line_iter. */ - GPIOD_API line_iter& operator=(line_iter&& other) = default; + line_iter& operator=(line_iter&& other) = default; /** * @brief Destructor. */ - GPIOD_API ~line_iter(void) = default; + ~line_iter(void) = default; /** * @brief Advance the iterator by one element. * @return Reference to this iterator. */ - GPIOD_API line_iter& operator++(void); + line_iter& operator++(void); /** * @brief Dereference current element. * @return Current GPIO line by reference. */ - GPIOD_API const line& operator*(void) const; + const line& operator*(void) const; /** * @brief Member access operator. * @return Current GPIO line by pointer. */ - GPIOD_API const line* operator->(void) const; + const line* operator->(void) const; /** * @brief Check if this operator points to the same element. @@ -917,7 +916,7 @@ public: * @return True if this iterator points to the same line_iter, * false otherwise. */ - GPIOD_API bool operator==(const line_iter& rhs) const noexcept; + bool operator==(const line_iter& rhs) const noexcept; /** * @brief Check if this operator doesn't point to the same element. @@ -925,7 +924,7 @@ public: * @return True if this iterator doesn't point to the same line_iter, * false otherwise. */ - GPIOD_API bool operator!=(const line_iter& rhs) const noexcept; + bool operator!=(const line_iter& rhs) const noexcept; private: diff --git a/bindings/cxx/internal.hpp b/bindings/cxx/internal.hpp new file mode 100644 index 0000000..9406d30 --- /dev/null +++ b/bindings/cxx/internal.hpp @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* SPDX-FileCopyrightText: 2021 Bartosz Golaszewski */ + +#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__ */ diff --git a/bindings/cxx/iter.cpp b/bindings/cxx/iter.cpp index eba7d30..09d46f3 100644 --- a/bindings/cxx/iter.cpp +++ b/bindings/cxx/iter.cpp @@ -4,25 +4,27 @@ #include #include +#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; } diff --git a/bindings/cxx/line.cpp b/bindings/cxx/line.cpp index bf84867..cfcf2fb 100644 --- a/bindings/cxx/line.cpp +++ b/bindings/cxx/line.cpp @@ -6,6 +6,8 @@ #include #include +#include "internal.hpp" + namespace gpiod { namespace { @@ -25,21 +27,21 @@ const ::std::map bias_mapping = { } /* namespace */ -line::line(void) +GPIOD_CXX_API line::line(void) : _m_line(nullptr), _m_owner() { } -line::line(::gpiod_line* line, const chip& owner) +GPIOD_CXX_API line::line(::gpiod_line* line, const chip& owner) : _m_line(line), _m_owner(owner._m_chip) { } -unsigned int line::offset(void) const +GPIOD_CXX_API unsigned int line::offset(void) const { this->throw_if_null(); line::chip_guard lock_chip(*this); @@ -47,7 +49,7 @@ unsigned int line::offset(void) const return ::gpiod_line_offset(this->_m_line); } -::std::string line::name(void) const +GPIOD_CXX_API ::std::string line::name(void) const { this->throw_if_null(); line::chip_guard lock_chip(*this); @@ -57,7 +59,7 @@ unsigned int line::offset(void) const return name ? ::std::string(name) : ::std::string(); } -::std::string line::consumer(void) const +GPIOD_CXX_API ::std::string line::consumer(void) const { this->throw_if_null(); line::chip_guard lock_chip(*this); @@ -67,7 +69,7 @@ unsigned int line::offset(void) const return consumer ? ::std::string(consumer) : ::std::string(); } -int line::direction(void) const +GPIOD_CXX_API int line::direction(void) const { this->throw_if_null(); line::chip_guard lock_chip(*this); @@ -77,7 +79,7 @@ int line::direction(void) const return dir == GPIOD_LINE_DIRECTION_INPUT ? DIRECTION_INPUT : DIRECTION_OUTPUT; } -bool line::is_active_low(void) const +GPIOD_CXX_API bool line::is_active_low(void) const { this->throw_if_null(); line::chip_guard lock_chip(*this); @@ -85,7 +87,7 @@ bool line::is_active_low(void) const return ::gpiod_line_is_active_low(this->_m_line); } -int line::bias(void) const +GPIOD_CXX_API int line::bias(void) const { this->throw_if_null(); line::chip_guard lock_chip(*this); @@ -93,7 +95,7 @@ int line::bias(void) const return bias_mapping.at(::gpiod_line_bias(this->_m_line)); } -bool line::is_used(void) const +GPIOD_CXX_API bool line::is_used(void) const { this->throw_if_null(); line::chip_guard lock_chip(*this); @@ -101,7 +103,7 @@ bool line::is_used(void) const return ::gpiod_line_is_used(this->_m_line); } -int line::drive(void) const +GPIOD_CXX_API int line::drive(void) const { this->throw_if_null(); line::chip_guard lock_chip(*this); @@ -109,7 +111,7 @@ int line::drive(void) const return drive_mapping.at(::gpiod_line_drive(this->_m_line)); } -void line::request(const line_request& config, int default_val) const +GPIOD_CXX_API void line::request(const line_request& config, int default_val) const { this->throw_if_null(); @@ -118,7 +120,7 @@ void line::request(const line_request& config, int default_val) const bulk.request(config, { default_val }); } -void line::release(void) const +GPIOD_CXX_API void line::release(void) const { this->throw_if_null(); @@ -135,7 +137,7 @@ void line::release(void) const * polling for events on single lines directly. */ -int line::get_value(void) const +GPIOD_CXX_API int line::get_value(void) const { this->throw_if_null(); @@ -144,7 +146,7 @@ int line::get_value(void) const return bulk.get_values()[0]; } -void line::set_value(int val) const +GPIOD_CXX_API void line::set_value(int val) const { this->throw_if_null(); @@ -153,8 +155,8 @@ void line::set_value(int val) const bulk.set_values({ val }); } -void line::set_config(int direction, ::std::bitset<32> flags, - int value) const +GPIOD_CXX_API void line::set_config(int direction, ::std::bitset<32> flags, + int value) const { this->throw_if_null(); @@ -163,7 +165,7 @@ void line::set_config(int direction, ::std::bitset<32> flags, bulk.set_config(direction, flags, { value }); } -void line::set_flags(::std::bitset<32> flags) const +GPIOD_CXX_API void line::set_flags(::std::bitset<32> flags) const { this->throw_if_null(); @@ -172,7 +174,7 @@ void line::set_flags(::std::bitset<32> flags) const bulk.set_flags(flags); } -void line::set_direction_input() const +GPIOD_CXX_API void line::set_direction_input() const { this->throw_if_null(); @@ -181,7 +183,7 @@ void line::set_direction_input() const bulk.set_direction_input(); } -void line::set_direction_output(int value) const +GPIOD_CXX_API void line::set_direction_output(int value) const { this->throw_if_null(); @@ -190,7 +192,7 @@ void line::set_direction_output(int value) const bulk.set_direction_output({ value }); } -bool line::event_wait(const ::std::chrono::nanoseconds& timeout) const +GPIOD_CXX_API bool line::event_wait(const ::std::chrono::nanoseconds& timeout) const { this->throw_if_null(); @@ -201,7 +203,7 @@ bool line::event_wait(const ::std::chrono::nanoseconds& timeout) const return !!event_bulk; } -line_event line::make_line_event(const ::gpiod_line_event& event) const noexcept +GPIOD_CXX_API line_event line::make_line_event(const ::gpiod_line_event& event) const noexcept { line_event ret; @@ -219,7 +221,7 @@ line_event line::make_line_event(const ::gpiod_line_event& event) const noexcept return ret; } -line_event line::event_read(void) const +GPIOD_CXX_API line_event line::event_read(void) const { this->throw_if_null(); line::chip_guard lock_chip(*this); @@ -236,7 +238,7 @@ line_event line::event_read(void) const return this->make_line_event(event_buf); } -::std::vector line::event_read_multiple(void) const +GPIOD_CXX_API ::std::vector line::event_read_multiple(void) const { this->throw_if_null(); line::chip_guard lock_chip(*this); @@ -259,7 +261,7 @@ line_event line::event_read(void) const return events; } -int line::event_get_fd(void) const +GPIOD_CXX_API int line::event_get_fd(void) const { this->throw_if_null(); line::chip_guard lock_chip(*this); @@ -273,44 +275,44 @@ int line::event_get_fd(void) const return ret; } -const chip line::get_chip(void) const +GPIOD_CXX_API const chip line::get_chip(void) const { return chip(this->_m_owner); } -void line::reset(void) +GPIOD_CXX_API void line::reset(void) { this->_m_line = nullptr; this->_m_owner.reset(); } -bool line::operator==(const line& rhs) const noexcept +GPIOD_CXX_API bool line::operator==(const line& rhs) const noexcept { return this->_m_line == rhs._m_line; } -bool line::operator!=(const line& rhs) const noexcept +GPIOD_CXX_API bool line::operator!=(const line& rhs) const noexcept { return this->_m_line != rhs._m_line; } -line::operator bool(void) const noexcept +GPIOD_CXX_API line::operator bool(void) const noexcept { return this->_m_line != nullptr; } -bool line::operator!(void) const noexcept +GPIOD_CXX_API bool line::operator!(void) const noexcept { return this->_m_line == nullptr; } -void line::throw_if_null(void) const +GPIOD_CXX_API void line::throw_if_null(void) const { if (!this->_m_line) throw ::std::logic_error("object not holding a GPIO line handle"); } -line::chip_guard::chip_guard(const line& line) +GPIOD_CXX_API line::chip_guard::chip_guard(const line& line) : _m_chip(line._m_owner) { diff --git a/bindings/cxx/line_bulk.cpp b/bindings/cxx/line_bulk.cpp index d40364d..a9261c0 100644 --- a/bindings/cxx/line_bulk.cpp +++ b/bindings/cxx/line_bulk.cpp @@ -5,14 +5,16 @@ #include #include +#include "internal.hpp" + namespace gpiod { -const ::std::bitset<32> line_request::FLAG_ACTIVE_LOW(GPIOD_BIT(0)); -const ::std::bitset<32> line_request::FLAG_OPEN_SOURCE(GPIOD_BIT(1)); -const ::std::bitset<32> line_request::FLAG_OPEN_DRAIN(GPIOD_BIT(2)); -const ::std::bitset<32> line_request::FLAG_BIAS_DISABLED(GPIOD_BIT(3)); -const ::std::bitset<32> line_request::FLAG_BIAS_PULL_DOWN(GPIOD_BIT(4)); -const ::std::bitset<32> line_request::FLAG_BIAS_PULL_UP(GPIOD_BIT(5)); +GPIOD_CXX_API const ::std::bitset<32> line_request::FLAG_ACTIVE_LOW(GPIOD_BIT(0)); +GPIOD_CXX_API const ::std::bitset<32> line_request::FLAG_OPEN_SOURCE(GPIOD_BIT(1)); +GPIOD_CXX_API const ::std::bitset<32> line_request::FLAG_OPEN_DRAIN(GPIOD_BIT(2)); +GPIOD_CXX_API const ::std::bitset<32> line_request::FLAG_BIAS_DISABLED(GPIOD_BIT(3)); +GPIOD_CXX_API const ::std::bitset<32> line_request::FLAG_BIAS_PULL_DOWN(GPIOD_BIT(4)); +GPIOD_CXX_API const ::std::bitset<32> line_request::FLAG_BIAS_PULL_UP(GPIOD_BIT(5)); namespace { @@ -44,9 +46,9 @@ const ::std::map<::std::bitset<32>, int, bitset_cmp> reqflag_mapping = { } /* namespace */ -const unsigned int line_bulk::MAX_LINES = 64; +GPIOD_CXX_API const unsigned int line_bulk::MAX_LINES = 64; -line_bulk::line_bulk(const ::std::vector& lines) +GPIOD_CXX_API line_bulk::line_bulk(const ::std::vector& lines) : _m_bulk() { this->_m_bulk.reserve(lines.size()); @@ -55,7 +57,7 @@ line_bulk::line_bulk(const ::std::vector& lines) this->append(it); } -void line_bulk::append(const line& new_line) +GPIOD_CXX_API void line_bulk::append(const line& new_line) { if (!new_line) throw ::std::logic_error("line_bulk cannot hold empty line objects"); @@ -69,32 +71,32 @@ void line_bulk::append(const line& new_line) this->_m_bulk.push_back(new_line); } -line& line_bulk::get(unsigned int index) +GPIOD_CXX_API line& line_bulk::get(unsigned int index) { return this->_m_bulk.at(index); } -line& line_bulk::operator[](unsigned int index) +GPIOD_CXX_API line& line_bulk::operator[](unsigned int index) { return this->_m_bulk[index]; } -unsigned int line_bulk::size(void) const noexcept +GPIOD_CXX_API unsigned int line_bulk::size(void) const noexcept { return this->_m_bulk.size(); } -bool line_bulk::empty(void) const noexcept +GPIOD_CXX_API bool line_bulk::empty(void) const noexcept { return this->_m_bulk.empty(); } -void line_bulk::clear(void) +GPIOD_CXX_API void line_bulk::clear(void) { this->_m_bulk.clear(); } -void line_bulk::request(const line_request& config, const ::std::vector default_vals) const +GPIOD_CXX_API void line_bulk::request(const line_request& config, const ::std::vector default_vals) const { this->throw_if_empty(); line::chip_guard lock_chip(this->_m_bulk.front()); @@ -123,7 +125,7 @@ void line_bulk::request(const line_request& config, const ::std::vector def "error requesting GPIO lines"); } -void line_bulk::release(void) const +GPIOD_CXX_API void line_bulk::release(void) const { this->throw_if_empty(); line::chip_guard lock_chip(this->_m_bulk.front()); @@ -133,7 +135,7 @@ void line_bulk::release(void) const ::gpiod_line_release_bulk(bulk.get()); } -::std::vector line_bulk::get_values(void) const +GPIOD_CXX_API ::std::vector line_bulk::get_values(void) const { this->throw_if_empty(); line::chip_guard lock_chip(this->_m_bulk.front()); @@ -152,7 +154,7 @@ void line_bulk::release(void) const return values; } -void line_bulk::set_values(const ::std::vector& values) const +GPIOD_CXX_API void line_bulk::set_values(const ::std::vector& values) const { this->throw_if_empty(); line::chip_guard lock_chip(this->_m_bulk.front()); @@ -169,8 +171,8 @@ void line_bulk::set_values(const ::std::vector& values) const "error setting GPIO line values"); } -void line_bulk::set_config(int direction, ::std::bitset<32> flags, - const ::std::vector values) const +GPIOD_CXX_API void line_bulk::set_config(int direction, ::std::bitset<32> flags, + const ::std::vector values) const { this->throw_if_empty(); line::chip_guard lock_chip(this->_m_bulk.front()); @@ -195,7 +197,7 @@ void line_bulk::set_config(int direction, ::std::bitset<32> flags, "error setting GPIO line config"); } -void line_bulk::set_flags(::std::bitset<32> flags) const +GPIOD_CXX_API void line_bulk::set_flags(::std::bitset<32> flags) const { this->throw_if_empty(); line::chip_guard lock_chip(this->_m_bulk.front()); @@ -216,7 +218,7 @@ void line_bulk::set_flags(::std::bitset<32> flags) const "error setting GPIO line flags"); } -void line_bulk::set_direction_input() const +GPIOD_CXX_API void line_bulk::set_direction_input() const { this->throw_if_empty(); line::chip_guard lock_chip(this->_m_bulk.front()); @@ -230,7 +232,7 @@ void line_bulk::set_direction_input() const "error setting GPIO line direction to input"); } -void line_bulk::set_direction_output(const ::std::vector& values) const +GPIOD_CXX_API void line_bulk::set_direction_output(const ::std::vector& values) const { this->throw_if_empty(); line::chip_guard lock_chip(this->_m_bulk.front()); @@ -247,7 +249,7 @@ void line_bulk::set_direction_output(const ::std::vector& values) const "error setting GPIO line direction to output"); } -line_bulk line_bulk::event_wait(const ::std::chrono::nanoseconds& timeout) const +GPIOD_CXX_API line_bulk line_bulk::event_wait(const ::std::chrono::nanoseconds& timeout) const { this->throw_if_empty(); line::chip_guard lock_chip(this->_m_bulk.front()); @@ -276,66 +278,66 @@ line_bulk line_bulk::event_wait(const ::std::chrono::nanoseconds& timeout) const return ret; } -line_bulk::operator bool(void) const noexcept +GPIOD_CXX_API line_bulk::operator bool(void) const noexcept { return !this->_m_bulk.empty(); } -bool line_bulk::operator!(void) const noexcept +GPIOD_CXX_API bool line_bulk::operator!(void) const noexcept { return this->_m_bulk.empty(); } -line_bulk::iterator::iterator(const ::std::vector::iterator& it) +GPIOD_CXX_API line_bulk::iterator::iterator(const ::std::vector::iterator& it) : _m_iter(it) { } -line_bulk::iterator& line_bulk::iterator::operator++(void) +GPIOD_CXX_API line_bulk::iterator& line_bulk::iterator::operator++(void) { this->_m_iter++; return *this; } -const line& line_bulk::iterator::operator*(void) const +GPIOD_CXX_API const line& line_bulk::iterator::operator*(void) const { return *this->_m_iter; } -const line* line_bulk::iterator::operator->(void) const +GPIOD_CXX_API const line* line_bulk::iterator::operator->(void) const { return this->_m_iter.operator->(); } -bool line_bulk::iterator::operator==(const iterator& rhs) const noexcept +GPIOD_CXX_API bool line_bulk::iterator::operator==(const iterator& rhs) const noexcept { return this->_m_iter == rhs._m_iter; } -bool line_bulk::iterator::operator!=(const iterator& rhs) const noexcept +GPIOD_CXX_API bool line_bulk::iterator::operator!=(const iterator& rhs) const noexcept { return this->_m_iter != rhs._m_iter; } -line_bulk::iterator line_bulk::begin(void) noexcept +GPIOD_CXX_API line_bulk::iterator line_bulk::begin(void) noexcept { return line_bulk::iterator(this->_m_bulk.begin()); } -line_bulk::iterator line_bulk::end(void) noexcept +GPIOD_CXX_API line_bulk::iterator line_bulk::end(void) noexcept { return line_bulk::iterator(this->_m_bulk.end()); } -void line_bulk::throw_if_empty(void) const +GPIOD_CXX_API void line_bulk::throw_if_empty(void) const { if (this->_m_bulk.empty()) throw ::std::logic_error("line_bulk not holding any GPIO lines"); } -line_bulk::line_bulk_ptr line_bulk::make_line_bulk_ptr(void) const +GPIOD_CXX_API line_bulk::line_bulk_ptr line_bulk::make_line_bulk_ptr(void) const { line_bulk_ptr bulk(::gpiod_line_bulk_new(this->size())); @@ -346,7 +348,7 @@ line_bulk::line_bulk_ptr line_bulk::make_line_bulk_ptr(void) const return bulk; } -line_bulk::line_bulk_ptr line_bulk::to_line_bulk(void) const +GPIOD_CXX_API line_bulk::line_bulk_ptr line_bulk::to_line_bulk(void) const { line_bulk_ptr bulk = this->make_line_bulk_ptr(); @@ -356,7 +358,7 @@ line_bulk::line_bulk_ptr line_bulk::to_line_bulk(void) const return bulk; } -void line_bulk::line_bulk_deleter::operator()(::gpiod_line_bulk *bulk) +GPIOD_CXX_API void line_bulk::line_bulk_deleter::operator()(::gpiod_line_bulk *bulk) { ::gpiod_line_bulk_free(bulk); } diff --git a/include/gpiod.h b/include/gpiod.h index 0ee9778..5aea01f 100644 --- a/include/gpiod.h +++ b/include/gpiod.h @@ -44,11 +44,6 @@ struct gpiod_line_bulk; * Commonly used utility macros. */ -/** - * @brief Makes symbol visible. - */ -#define GPIOD_API __attribute__((visibility("default"))) - /** * @brief Shift 1 by given offset. * @param nr Bit position. @@ -71,49 +66,49 @@ struct gpiod_line_bulk; * @return True if the file exists and is a GPIO chip character device or a * symbolic link to it. */ -bool gpiod_is_gpiochip_device(const char *path) GPIOD_API; +bool gpiod_is_gpiochip_device(const char *path); /** * @brief Open a gpiochip by path. * @param path Path to the gpiochip device file. * @return GPIO chip handle or NULL if an error occurred. */ -struct gpiod_chip *gpiod_chip_open(const char *path) GPIOD_API; +struct gpiod_chip *gpiod_chip_open(const char *path); /** * @brief Increase the refcount on this GPIO object. * @param chip The GPIO chip object. * @return Passed reference to the GPIO chip. */ -struct gpiod_chip *gpiod_chip_ref(struct gpiod_chip *chip) GPIOD_API; +struct gpiod_chip *gpiod_chip_ref(struct gpiod_chip *chip); /** * @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; +void gpiod_chip_unref(struct gpiod_chip *chip); /** * @brief Get the GPIO chip name as represented in the kernel. * @param chip The GPIO chip object. * @return Pointer to a human-readable string containing the chip name. */ -const char *gpiod_chip_name(struct gpiod_chip *chip) GPIOD_API; +const char *gpiod_chip_name(struct gpiod_chip *chip); /** * @brief Get the GPIO chip label as represented in the kernel. * @param chip The GPIO chip object. * @return Pointer to a human-readable string containing the chip label. */ -const char *gpiod_chip_label(struct gpiod_chip *chip) GPIOD_API; +const char *gpiod_chip_label(struct gpiod_chip *chip); /** * @brief Get the number of GPIO lines exposed by this chip. * @param chip The GPIO chip object. * @return Number of GPIO lines. */ -unsigned int gpiod_chip_num_lines(struct gpiod_chip *chip) GPIOD_API; +unsigned int gpiod_chip_num_lines(struct gpiod_chip *chip); /** * @brief Get the handle to the GPIO line at given offset. @@ -122,7 +117,7 @@ unsigned int gpiod_chip_num_lines(struct gpiod_chip *chip) GPIOD_API; * @return Pointer to the GPIO line handle or NULL if an error occured. */ struct gpiod_line * -gpiod_chip_get_line(struct gpiod_chip *chip, unsigned int offset) GPIOD_API; +gpiod_chip_get_line(struct gpiod_chip *chip, unsigned int offset); /** * @brief Retrieve a set of lines and store them in a line bulk object. @@ -133,7 +128,7 @@ gpiod_chip_get_line(struct gpiod_chip *chip, unsigned int offset) GPIOD_API; */ struct gpiod_line_bulk * gpiod_chip_get_lines(struct gpiod_chip *chip, unsigned int *offsets, - unsigned int num_offsets) GPIOD_API; + unsigned int num_offsets); /** * @brief Retrieve all lines exposed by a chip and store them in a bulk object. @@ -141,7 +136,7 @@ gpiod_chip_get_lines(struct gpiod_chip *chip, unsigned int *offsets, * @return New line bulk object or NULL on error. */ struct gpiod_line_bulk * -gpiod_chip_get_all_lines(struct gpiod_chip *chip) GPIOD_API; +gpiod_chip_get_all_lines(struct gpiod_chip *chip); /** * @brief Map a GPIO line's name to its offset within the chip. @@ -150,7 +145,7 @@ gpiod_chip_get_all_lines(struct gpiod_chip *chip) GPIOD_API; * @return Offset of the line within the chip or -1 if a line with given name * is not exposed by the chip. */ -int gpiod_chip_find_line(struct gpiod_chip *chip, const char *name) GPIOD_API; +int gpiod_chip_find_line(struct gpiod_chip *chip, const char *name); /** * @} @@ -172,19 +167,19 @@ int gpiod_chip_find_line(struct gpiod_chip *chip, const char *name) GPIOD_API; * @param max_lines Maximum number of lines this object can hold. * @return New line bulk object or NULL on error. */ -struct gpiod_line_bulk *gpiod_line_bulk_new(unsigned int max_lines) GPIOD_API; +struct gpiod_line_bulk *gpiod_line_bulk_new(unsigned int max_lines); /** * @brief Reset a bulk object. Remove all lines and set size to 0. * @param bulk Bulk object to reset. */ -void gpiod_line_bulk_reset(struct gpiod_line_bulk *bulk) GPIOD_API; +void gpiod_line_bulk_reset(struct gpiod_line_bulk *bulk); /** * @brief Release all resources allocated for this bulk object. * @param bulk Bulk object to free. */ -void gpiod_line_bulk_free(struct gpiod_line_bulk *bulk) GPIOD_API; +void gpiod_line_bulk_free(struct gpiod_line_bulk *bulk); /** * @brief Add a single line to a GPIO bulk object. @@ -198,7 +193,7 @@ void gpiod_line_bulk_free(struct gpiod_line_bulk *bulk) GPIOD_API; * the other lines already held by this object. */ int gpiod_line_bulk_add_line(struct gpiod_line_bulk *bulk, - struct gpiod_line *line) GPIOD_API; + struct gpiod_line *line); /** * @brief Retrieve the line handle from a line bulk object at given index. @@ -208,15 +203,14 @@ int gpiod_line_bulk_add_line(struct gpiod_line_bulk *bulk, * the number of lines this bulk can hold. */ struct gpiod_line * -gpiod_line_bulk_get_line(struct gpiod_line_bulk *bulk, - unsigned int index) GPIOD_API; +gpiod_line_bulk_get_line(struct gpiod_line_bulk *bulk, unsigned int index); /** * @brief Retrieve the number of GPIO lines held by this line bulk object. * @param bulk Line bulk object. * @return Number of lines held by this line bulk. */ -unsigned int gpiod_line_bulk_num_lines(struct gpiod_line_bulk *bulk) GPIOD_API; +unsigned int gpiod_line_bulk_num_lines(struct gpiod_line_bulk *bulk); /** * @brief Values returned by the callback passed to @@ -244,7 +238,7 @@ typedef int (*gpiod_line_bulk_foreach_cb)(struct gpiod_line *, void *); */ void gpiod_line_bulk_foreach_line(struct gpiod_line_bulk *bulk, gpiod_line_bulk_foreach_cb func, - void *data) GPIOD_API; + void *data); /** * @} @@ -297,7 +291,7 @@ enum { * @param line GPIO line object. * @return Line offset. */ -unsigned int gpiod_line_offset(struct gpiod_line *line) GPIOD_API; +unsigned int gpiod_line_offset(struct gpiod_line *line); /** * @brief Read the GPIO line name. @@ -306,7 +300,7 @@ unsigned int gpiod_line_offset(struct gpiod_line *line) GPIOD_API; * routine returns a pointer to a null-terminated string or NULL if * the line is unnamed. */ -const char *gpiod_line_name(struct gpiod_line *line) GPIOD_API; +const char *gpiod_line_name(struct gpiod_line *line); /** * @brief Read the GPIO line consumer name. @@ -315,21 +309,21 @@ const char *gpiod_line_name(struct gpiod_line *line) GPIOD_API; * kernel. This routine returns a pointer to a null-terminated string * or NULL if the line is not used. */ -const char *gpiod_line_consumer(struct gpiod_line *line) GPIOD_API; +const char *gpiod_line_consumer(struct gpiod_line *line); /** * @brief Read the GPIO line direction setting. * @param line GPIO line object. * @return Returns GPIOD_LINE_DIRECTION_INPUT or GPIOD_LINE_DIRECTION_OUTPUT. */ -int gpiod_line_direction(struct gpiod_line *line) GPIOD_API; +int gpiod_line_direction(struct gpiod_line *line); /** * @brief Check if the signal of this line is inverted. * @param line GPIO line object. * @return True if this line is "active-low", false otherwise. */ -bool gpiod_line_is_active_low(struct gpiod_line *line) GPIOD_API; +bool gpiod_line_is_active_low(struct gpiod_line *line); /** * @brief Read the GPIO line bias setting. @@ -337,7 +331,7 @@ bool gpiod_line_is_active_low(struct gpiod_line *line) GPIOD_API; * @return Returns GPIOD_LINE_BIAS_PULL_UP, GPIOD_LINE_BIAS_PULL_DOWN, * GPIOD_LINE_BIAS_DISABLE or GPIOD_LINE_BIAS_UNKNOWN. */ -int gpiod_line_bias(struct gpiod_line *line) GPIOD_API; +int gpiod_line_bias(struct gpiod_line *line); /** * @brief Check if the line is currently in use. @@ -348,7 +342,7 @@ int gpiod_line_bias(struct gpiod_line *line) GPIOD_API; * requested by another process or hogged by the kernel. It only matters that * the line is used and we can't request it. */ -bool gpiod_line_is_used(struct gpiod_line *line) GPIOD_API; +bool gpiod_line_is_used(struct gpiod_line *line); /** * @brief Read the GPIO line drive setting. @@ -356,14 +350,14 @@ bool gpiod_line_is_used(struct gpiod_line *line) GPIOD_API; * @return Returns GPIOD_LINE_DRIVE_PUSH_PULL, GPIOD_LINE_DRIVE_OPEN_DRAIN or * GPIOD_LINE_DRIVE_OPEN_SOURCE. */ -int gpiod_line_drive(struct gpiod_line *line) GPIOD_API; +int gpiod_line_drive(struct gpiod_line *line); /** * @brief Get the handle to the GPIO chip controlling this line. * @param line The GPIO line object. * @return Pointer to the GPIO chip handle controlling this line. */ -struct gpiod_chip *gpiod_line_get_chip(struct gpiod_line *line) GPIOD_API; +struct gpiod_chip *gpiod_line_get_chip(struct gpiod_line *line); /** * @} @@ -437,7 +431,7 @@ struct gpiod_line_request_config { */ int gpiod_line_request(struct gpiod_line *line, const struct gpiod_line_request_config *config, - int default_val) GPIOD_API; + int default_val); /** * @brief Reserve a single line, set the direction to input. @@ -445,8 +439,7 @@ int gpiod_line_request(struct gpiod_line *line, * @param consumer Name of the consumer. * @return 0 if the line was properly reserved, -1 on failure. */ -int gpiod_line_request_input(struct gpiod_line *line, - const char *consumer) GPIOD_API; +int gpiod_line_request_input(struct gpiod_line *line, const char *consumer); /** * @brief Reserve a single line, set the direction to output. @@ -456,7 +449,7 @@ int gpiod_line_request_input(struct gpiod_line *line, * @return 0 if the line was properly reserved, -1 on failure. */ int gpiod_line_request_output(struct gpiod_line *line, - const char *consumer, int default_val) GPIOD_API; + const char *consumer, int default_val); /** * @brief Request rising edge event notifications on a single line. @@ -465,7 +458,7 @@ int gpiod_line_request_output(struct gpiod_line *line, * @return 0 if the operation succeeds, -1 on failure. */ int gpiod_line_request_rising_edge_events(struct gpiod_line *line, - const char *consumer) GPIOD_API; + const char *consumer); /** * @brief Request falling edge event notifications on a single line. @@ -474,7 +467,7 @@ int gpiod_line_request_rising_edge_events(struct gpiod_line *line, * @return 0 if the operation succeeds, -1 on failure. */ int gpiod_line_request_falling_edge_events(struct gpiod_line *line, - const char *consumer) GPIOD_API; + const char *consumer); /** * @brief Request all event type notifications on a single line. @@ -483,7 +476,7 @@ int gpiod_line_request_falling_edge_events(struct gpiod_line *line, * @return 0 if the operation succeeds, -1 on failure. */ int gpiod_line_request_both_edges_events(struct gpiod_line *line, - const char *consumer) GPIOD_API; + const char *consumer); /** * @brief Reserve a single line, set the direction to input. @@ -493,7 +486,7 @@ int gpiod_line_request_both_edges_events(struct gpiod_line *line, * @return 0 if the line was properly reserved, -1 on failure. */ int gpiod_line_request_input_flags(struct gpiod_line *line, - const char *consumer, int flags) GPIOD_API; + const char *consumer, int flags); /** * @brief Reserve a single line, set the direction to output. @@ -505,7 +498,7 @@ int gpiod_line_request_input_flags(struct gpiod_line *line, */ int gpiod_line_request_output_flags(struct gpiod_line *line, const char *consumer, int flags, - int default_val) GPIOD_API; + int default_val); /** * @brief Request rising edge event notifications on a single line. @@ -516,7 +509,7 @@ int gpiod_line_request_output_flags(struct gpiod_line *line, */ int gpiod_line_request_rising_edge_events_flags(struct gpiod_line *line, const char *consumer, - int flags) GPIOD_API; + int flags); /** * @brief Request falling edge event notifications on a single line. @@ -527,7 +520,7 @@ int gpiod_line_request_rising_edge_events_flags(struct gpiod_line *line, */ int gpiod_line_request_falling_edge_events_flags(struct gpiod_line *line, const char *consumer, - int flags) GPIOD_API; + int flags); /** * @brief Request all event type notifications on a single line. @@ -538,7 +531,7 @@ int gpiod_line_request_falling_edge_events_flags(struct gpiod_line *line, */ int gpiod_line_request_both_edges_events_flags(struct gpiod_line *line, const char *consumer, - int flags) GPIOD_API; + int flags); /** * @brief Reserve a set of GPIO lines. @@ -555,7 +548,7 @@ int gpiod_line_request_both_edges_events_flags(struct gpiod_line *line, */ int gpiod_line_request_bulk(struct gpiod_line_bulk *bulk, const struct gpiod_line_request_config *config, - const int *default_vals) GPIOD_API; + const int *default_vals); /** * @brief Reserve a set of GPIO lines, set the direction to input. @@ -564,7 +557,7 @@ int gpiod_line_request_bulk(struct gpiod_line_bulk *bulk, * @return 0 if the lines were properly reserved, -1 on failure. */ int gpiod_line_request_bulk_input(struct gpiod_line_bulk *bulk, - const char *consumer) GPIOD_API; + const char *consumer); /** * @brief Reserve a set of GPIO lines, set the direction to output. @@ -575,7 +568,7 @@ int gpiod_line_request_bulk_input(struct gpiod_line_bulk *bulk, */ int gpiod_line_request_bulk_output(struct gpiod_line_bulk *bulk, const char *consumer, - const int *default_vals) GPIOD_API; + const int *default_vals); /** * @brief Request rising edge event notifications on a set of lines. @@ -584,7 +577,7 @@ int gpiod_line_request_bulk_output(struct gpiod_line_bulk *bulk, * @return 0 if the operation succeeds, -1 on failure. */ int gpiod_line_request_bulk_rising_edge_events(struct gpiod_line_bulk *bulk, - const char *consumer) GPIOD_API; + const char *consumer); /** * @brief Request falling edge event notifications on a set of lines. @@ -593,7 +586,7 @@ int gpiod_line_request_bulk_rising_edge_events(struct gpiod_line_bulk *bulk, * @return 0 if the operation succeeds, -1 on failure. */ int gpiod_line_request_bulk_falling_edge_events(struct gpiod_line_bulk *bulk, - const char *consumer) GPIOD_API; + const char *consumer); /** * @brief Request all event type notifications on a set of lines. @@ -602,7 +595,7 @@ int gpiod_line_request_bulk_falling_edge_events(struct gpiod_line_bulk *bulk, * @return 0 if the operation succeeds, -1 on failure. */ int gpiod_line_request_bulk_both_edges_events(struct gpiod_line_bulk *bulk, - const char *consumer) GPIOD_API; + const char *consumer); /** * @brief Reserve a set of GPIO lines, set the direction to input. @@ -612,8 +605,7 @@ int gpiod_line_request_bulk_both_edges_events(struct gpiod_line_bulk *bulk, * @return 0 if the lines were properly reserved, -1 on failure. */ int gpiod_line_request_bulk_input_flags(struct gpiod_line_bulk *bulk, - const char *consumer, - int flags) GPIOD_API; + const char *consumer, int flags); /** * @brief Reserve a set of GPIO lines, set the direction to output. @@ -625,7 +617,7 @@ int gpiod_line_request_bulk_input_flags(struct gpiod_line_bulk *bulk, */ int gpiod_line_request_bulk_output_flags(struct gpiod_line_bulk *bulk, const char *consumer, int flags, - const int *default_vals) GPIOD_API; + const int *default_vals); /** * @brief Request rising edge event notifications on a set of lines. @@ -636,8 +628,7 @@ int gpiod_line_request_bulk_output_flags(struct gpiod_line_bulk *bulk, */ int gpiod_line_request_bulk_rising_edge_events_flags( struct gpiod_line_bulk *bulk, - const char *consumer, - int flags) GPIOD_API; + const char *consumer, int flags); /** * @brief Request falling edge event notifications on a set of lines. @@ -648,8 +639,7 @@ int gpiod_line_request_bulk_rising_edge_events_flags( */ int gpiod_line_request_bulk_falling_edge_events_flags( struct gpiod_line_bulk *bulk, - const char *consumer, - int flags) GPIOD_API; + const char *consumer, int flags); /** * @brief Request all event type notifications on a set of lines. @@ -660,14 +650,13 @@ int gpiod_line_request_bulk_falling_edge_events_flags( */ int gpiod_line_request_bulk_both_edges_events_flags( struct gpiod_line_bulk *bulk, - const char *consumer, - int flags) GPIOD_API; + const char *consumer, int flags); /** * @brief Release a previously reserved line. * @param line GPIO line object. */ -void gpiod_line_release(struct gpiod_line *line) GPIOD_API; +void gpiod_line_release(struct gpiod_line *line); /** * @brief Release a set of previously reserved lines. @@ -676,7 +665,7 @@ void gpiod_line_release(struct gpiod_line *line) GPIOD_API; * If the lines were not previously requested together, the behavior is * undefined. */ -void gpiod_line_release_bulk(struct gpiod_line_bulk *bulk) GPIOD_API; +void gpiod_line_release_bulk(struct gpiod_line_bulk *bulk); /** * @} @@ -694,7 +683,7 @@ void gpiod_line_release_bulk(struct gpiod_line_bulk *bulk) GPIOD_API; * @return 0 or 1 if the operation succeeds. On error this routine returns -1 * and sets the last error number. */ -int gpiod_line_get_value(struct gpiod_line *line) GPIOD_API; +int gpiod_line_get_value(struct gpiod_line *line); /** * @brief Read current values of a set of GPIO lines. @@ -707,8 +696,7 @@ int gpiod_line_get_value(struct gpiod_line *line) GPIOD_API; * the same order, the lines are added to line_bulk. If the lines were not * previously requested together, the behavior is undefined. */ -int gpiod_line_get_value_bulk(struct gpiod_line_bulk *bulk, - int *values) GPIOD_API; +int gpiod_line_get_value_bulk(struct gpiod_line_bulk *bulk, int *values); /** * @brief Set the value of a single GPIO line. @@ -717,7 +705,7 @@ int gpiod_line_get_value_bulk(struct gpiod_line_bulk *bulk, * @return 0 is the operation succeeds. In case of an error this routine * returns -1 and sets the last error number. */ -int gpiod_line_set_value(struct gpiod_line *line, int value) GPIOD_API; +int gpiod_line_set_value(struct gpiod_line *line, int value); /** * @brief Set the values of a set of GPIO lines. @@ -730,8 +718,7 @@ int gpiod_line_set_value(struct gpiod_line *line, int value) GPIOD_API; * If the lines were not previously requested together, the behavior is * undefined. */ -int gpiod_line_set_value_bulk(struct gpiod_line_bulk *bulk, - const int *values) GPIOD_API; +int gpiod_line_set_value_bulk(struct gpiod_line_bulk *bulk, const int *values); /** * @} @@ -757,7 +744,7 @@ int gpiod_line_set_value_bulk(struct gpiod_line_bulk *bulk, * returns -1 and sets the last error number. */ int gpiod_line_set_config(struct gpiod_line *line, int direction, - int flags, int value) GPIOD_API; + int flags, int value); /** * @brief Update the configuration of a set of GPIO lines. @@ -778,8 +765,7 @@ int gpiod_line_set_config(struct gpiod_line *line, int direction, * undefined. */ int gpiod_line_set_config_bulk(struct gpiod_line_bulk *bulk, - int direction, int flags, - const int *values) GPIOD_API; + int direction, int flags, const int *values); /** @@ -789,7 +775,7 @@ int gpiod_line_set_config_bulk(struct gpiod_line_bulk *bulk, * @return 0 is the operation succeeds. In case of an error this routine * returns -1 and sets the last error number. */ -int gpiod_line_set_flags(struct gpiod_line *line, int flags) GPIOD_API; +int gpiod_line_set_flags(struct gpiod_line *line, int flags); /** * @brief Update the configuration flags of a set of GPIO lines. @@ -801,8 +787,7 @@ int gpiod_line_set_flags(struct gpiod_line *line, int flags) GPIOD_API; * If the lines were not previously requested together, the behavior is * undefined. */ -int gpiod_line_set_flags_bulk(struct gpiod_line_bulk *bulk, - int flags) GPIOD_API; +int gpiod_line_set_flags_bulk(struct gpiod_line_bulk *bulk, int flags); /** * @brief Set the direction of a single GPIO line to input. @@ -810,7 +795,7 @@ int gpiod_line_set_flags_bulk(struct gpiod_line_bulk *bulk, * @return 0 is the operation succeeds. In case of an error this routine * returns -1 and sets the last error number. */ -int gpiod_line_set_direction_input(struct gpiod_line *line) GPIOD_API; +int gpiod_line_set_direction_input(struct gpiod_line *line); /** * @brief Set the direction of a set of GPIO lines to input. @@ -822,7 +807,7 @@ int gpiod_line_set_direction_input(struct gpiod_line *line) GPIOD_API; * undefined. */ int -gpiod_line_set_direction_input_bulk(struct gpiod_line_bulk *bulk) GPIOD_API; +gpiod_line_set_direction_input_bulk(struct gpiod_line_bulk *bulk); /** * @brief Set the direction of a single GPIO line to output. @@ -831,8 +816,7 @@ gpiod_line_set_direction_input_bulk(struct gpiod_line_bulk *bulk) GPIOD_API; * @return 0 is the operation succeeds. In case of an error this routine * returns -1 and sets the last error number. */ -int gpiod_line_set_direction_output(struct gpiod_line *line, - int value) GPIOD_API; +int gpiod_line_set_direction_output(struct gpiod_line *line, int value); /** * @brief Set the direction of a set of GPIO lines to output. @@ -847,7 +831,7 @@ int gpiod_line_set_direction_output(struct gpiod_line *line, * undefined. */ int gpiod_line_set_direction_output_bulk(struct gpiod_line_bulk *bulk, - const int *values) GPIOD_API; + const int *values); /** * @} @@ -891,7 +875,7 @@ struct gpiod_line_event { * occurred. */ int gpiod_line_event_wait(struct gpiod_line *line, - const struct timespec *timeout) GPIOD_API; + const struct timespec *timeout); /** * @brief Wait for events on a set of lines. @@ -904,7 +888,7 @@ int gpiod_line_event_wait(struct gpiod_line *line, */ int gpiod_line_event_wait_bulk(struct gpiod_line_bulk *bulk, const struct timespec *timeout, - struct gpiod_line_bulk *event_bulk) GPIOD_API; + struct gpiod_line_bulk *event_bulk); /** * @brief Read next pending event from the GPIO line. @@ -914,7 +898,7 @@ int gpiod_line_event_wait_bulk(struct gpiod_line_bulk *bulk, * @note This function will block if no event was queued for this line. */ int gpiod_line_event_read(struct gpiod_line *line, - struct gpiod_line_event *event) GPIOD_API; + struct gpiod_line_event *event); /** * @brief Read up to a certain number of events from the GPIO line. @@ -927,7 +911,7 @@ int gpiod_line_event_read(struct gpiod_line *line, */ int gpiod_line_event_read_multiple(struct gpiod_line *line, struct gpiod_line_event *events, - unsigned int num_events) GPIOD_API; + unsigned int num_events); /** * @brief Get the event file descriptor. @@ -939,7 +923,7 @@ int gpiod_line_event_read_multiple(struct gpiod_line *line, * Users may want to poll the event file descriptor on their own. This routine * allows to access it. */ -int gpiod_line_event_get_fd(struct gpiod_line *line) GPIOD_API; +int gpiod_line_event_get_fd(struct gpiod_line *line); /** * @brief Read the last GPIO event directly from a file descriptor. @@ -951,7 +935,7 @@ int gpiod_line_event_get_fd(struct gpiod_line *line) GPIOD_API; * directly read the event data from it using this routine. This function * translates the kernel representation of the event to the libgpiod format. */ -int gpiod_line_event_read_fd(int fd, struct gpiod_line_event *event) GPIOD_API; +int gpiod_line_event_read_fd(int fd, struct gpiod_line_event *event); /** * @brief Read up to a certain number of events directly from a file descriptor. @@ -963,7 +947,7 @@ int gpiod_line_event_read_fd(int fd, struct gpiod_line_event *event) GPIOD_API; * failure -1 is returned. */ int gpiod_line_event_read_fd_multiple(int fd, struct gpiod_line_event *events, - unsigned int num_events) GPIOD_API; + unsigned int num_events); /** * @} @@ -980,7 +964,7 @@ int gpiod_line_event_read_fd_multiple(int fd, struct gpiod_line_event *events, * @brief Get the API version of the library as a human-readable string. * @return Human-readable string containing the library version. */ -const char *gpiod_version_string(void) GPIOD_API; +const char *gpiod_version_string(void); /** * @} diff --git a/lib/Makefile.am b/lib/Makefile.am index d8b19f0..8441584 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -2,7 +2,7 @@ # SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski 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 diff --git a/lib/core.c b/lib/core.c index 8fdc503..c1fb8ec 100644 --- a/lib/core.c +++ b/lib/core.c @@ -17,6 +17,7 @@ #include #include +#include "internal.h" #include "uapi/gpio.h" #define LINE_REQUEST_MAX_LINES 64 @@ -90,7 +91,7 @@ struct gpiod_line_bulk { #define BULK_SINGLE_LINE_INIT(line) \ { 1, 1, { (line) } } -struct gpiod_line_bulk *gpiod_line_bulk_new(unsigned int max_lines) +GPIOD_API struct gpiod_line_bulk *gpiod_line_bulk_new(unsigned int max_lines) { struct gpiod_line_bulk *bulk; size_t size; @@ -113,19 +114,19 @@ struct gpiod_line_bulk *gpiod_line_bulk_new(unsigned int max_lines) return bulk; } -void gpiod_line_bulk_reset(struct gpiod_line_bulk *bulk) +GPIOD_API void gpiod_line_bulk_reset(struct gpiod_line_bulk *bulk) { bulk->num_lines = 0; memset(bulk->lines, 0, bulk->max_lines * sizeof(struct line *)); } -void gpiod_line_bulk_free(struct gpiod_line_bulk *bulk) +GPIOD_API void gpiod_line_bulk_free(struct gpiod_line_bulk *bulk) { free(bulk); } -int gpiod_line_bulk_add_line(struct gpiod_line_bulk *bulk, - struct gpiod_line *line) +GPIOD_API int gpiod_line_bulk_add_line(struct gpiod_line_bulk *bulk, + struct gpiod_line *line) { if (bulk->num_lines == bulk->max_lines) { errno = EINVAL; @@ -144,7 +145,7 @@ int gpiod_line_bulk_add_line(struct gpiod_line_bulk *bulk, return 0; } -struct gpiod_line * +GPIOD_API struct gpiod_line * gpiod_line_bulk_get_line(struct gpiod_line_bulk *bulk, unsigned int index) { if (index >= bulk->num_lines) { @@ -155,13 +156,14 @@ gpiod_line_bulk_get_line(struct gpiod_line_bulk *bulk, unsigned int index) return bulk->lines[index]; } -unsigned int gpiod_line_bulk_num_lines(struct gpiod_line_bulk *bulk) +GPIOD_API unsigned int gpiod_line_bulk_num_lines(struct gpiod_line_bulk *bulk) { return bulk->num_lines; } -void gpiod_line_bulk_foreach_line(struct gpiod_line_bulk *bulk, - gpiod_line_bulk_foreach_cb func, void *data) +GPIOD_API void gpiod_line_bulk_foreach_line(struct gpiod_line_bulk *bulk, + gpiod_line_bulk_foreach_cb func, + void *data) { unsigned int index; int ret; @@ -178,7 +180,7 @@ void gpiod_line_bulk_foreach_line(struct gpiod_line_bulk *bulk, (index) < (bulk)->num_lines; \ (index)++, (line) = (bulk)->lines[(index)]) -bool gpiod_is_gpiochip_device(const char *path) +GPIOD_API bool gpiod_is_gpiochip_device(const char *path) { char *name, *realname, *sysfsp, sysfsdev[16], devstr[16]; struct stat statbuf; @@ -269,7 +271,7 @@ out: return ret; } -struct gpiod_chip *gpiod_chip_open(const char *path) +GPIOD_API struct gpiod_chip *gpiod_chip_open(const char *path) { struct gpiochip_info info; struct gpiod_chip *chip; @@ -327,13 +329,13 @@ err_close_fd: return NULL; } -struct gpiod_chip *gpiod_chip_ref(struct gpiod_chip *chip) +GPIOD_API struct gpiod_chip *gpiod_chip_ref(struct gpiod_chip *chip) { chip->refcount++; return chip; } -void gpiod_chip_unref(struct gpiod_chip *chip) +GPIOD_API void gpiod_chip_unref(struct gpiod_chip *chip) { struct gpiod_line *line; unsigned int i; @@ -358,24 +360,24 @@ void gpiod_chip_unref(struct gpiod_chip *chip) free(chip); } -const char *gpiod_chip_name(struct gpiod_chip *chip) +GPIOD_API const char *gpiod_chip_name(struct gpiod_chip *chip) { return chip->name; } -const char *gpiod_chip_label(struct gpiod_chip *chip) +GPIOD_API const char *gpiod_chip_label(struct gpiod_chip *chip) { return chip->label; } -unsigned int gpiod_chip_num_lines(struct gpiod_chip *chip) +GPIOD_API 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_API struct gpiod_line * gpiod_chip_get_line(struct gpiod_chip *chip, unsigned int offset) { struct gpiod_line *line; @@ -458,37 +460,37 @@ static int line_get_fd(struct gpiod_line *line) return line->fd_handle->fd; } -struct gpiod_chip *gpiod_line_get_chip(struct gpiod_line *line) +GPIOD_API struct gpiod_chip *gpiod_line_get_chip(struct gpiod_line *line) { return line->chip; } -unsigned int gpiod_line_offset(struct gpiod_line *line) +GPIOD_API unsigned int gpiod_line_offset(struct gpiod_line *line) { return line->offset; } -const char *gpiod_line_name(struct gpiod_line *line) +GPIOD_API const char *gpiod_line_name(struct gpiod_line *line) { return line->name[0] == '\0' ? NULL : line->name; } -const char *gpiod_line_consumer(struct gpiod_line *line) +GPIOD_API const char *gpiod_line_consumer(struct gpiod_line *line) { return line->consumer[0] == '\0' ? NULL : line->consumer; } -int gpiod_line_direction(struct gpiod_line *line) +GPIOD_API int gpiod_line_direction(struct gpiod_line *line) { return line->direction; } -bool gpiod_line_is_active_low(struct gpiod_line *line) +GPIOD_API bool gpiod_line_is_active_low(struct gpiod_line *line) { return line->active_low; } -int gpiod_line_bias(struct gpiod_line *line) +GPIOD_API int gpiod_line_bias(struct gpiod_line *line) { if (line->info_flags & GPIOLINE_FLAG_BIAS_DISABLE) return GPIOD_LINE_BIAS_DISABLED; @@ -500,12 +502,12 @@ int gpiod_line_bias(struct gpiod_line *line) return GPIOD_LINE_BIAS_UNKNOWN; } -bool gpiod_line_is_used(struct gpiod_line *line) +GPIOD_API bool gpiod_line_is_used(struct gpiod_line *line) { return line->info_flags & GPIOLINE_FLAG_KERNEL; } -int gpiod_line_drive(struct gpiod_line *line) +GPIOD_API int gpiod_line_drive(struct gpiod_line *line) { if (line->info_flags & GPIOLINE_FLAG_OPEN_DRAIN) return GPIOD_LINE_DRIVE_OPEN_DRAIN; @@ -838,9 +840,9 @@ static int line_request_events(struct gpiod_line_bulk *bulk, return 0; } -int gpiod_line_request(struct gpiod_line *line, - const struct gpiod_line_request_config *config, - int default_val) +GPIOD_API int gpiod_line_request(struct gpiod_line *line, + const struct gpiod_line_request_config *config, + int default_val) { struct gpiod_line_bulk bulk = BULK_SINGLE_LINE_INIT(line); @@ -861,9 +863,10 @@ static bool line_request_is_events(int request) request == GPIOD_LINE_REQUEST_EVENT_BOTH_EDGES; } -int gpiod_line_request_bulk(struct gpiod_line_bulk *bulk, - const struct gpiod_line_request_config *config, - const int *vals) +GPIOD_API int +gpiod_line_request_bulk(struct gpiod_line_bulk *bulk, + const struct gpiod_line_request_config *config, + const int *vals) { if (line_request_is_direction(config->request_type)) return line_request_values(bulk, config, vals); @@ -874,14 +877,14 @@ int gpiod_line_request_bulk(struct gpiod_line_bulk *bulk, return -1; } -void gpiod_line_release(struct gpiod_line *line) +GPIOD_API void gpiod_line_release(struct gpiod_line *line) { struct gpiod_line_bulk bulk = BULK_SINGLE_LINE_INIT(line); gpiod_line_release_bulk(&bulk); } -void gpiod_line_release_bulk(struct gpiod_line_bulk *bulk) +GPIOD_API void gpiod_line_release_bulk(struct gpiod_line_bulk *bulk) { struct gpiod_line *line; unsigned int idx; @@ -894,7 +897,7 @@ void gpiod_line_release_bulk(struct gpiod_line_bulk *bulk) } } -int gpiod_line_get_value(struct gpiod_line *line) +GPIOD_API int gpiod_line_get_value(struct gpiod_line *line) { struct gpiod_line_bulk bulk = BULK_SINGLE_LINE_INIT(line); int rv, value; @@ -906,7 +909,8 @@ int gpiod_line_get_value(struct gpiod_line *line) return value; } -int gpiod_line_get_value_bulk(struct gpiod_line_bulk *bulk, int *values) +GPIOD_API int gpiod_line_get_value_bulk(struct gpiod_line_bulk *bulk, + int *values) { struct gpio_v2_line_values lv; struct gpiod_line *line; @@ -951,14 +955,15 @@ int gpiod_line_get_value_bulk(struct gpiod_line_bulk *bulk, int *values) return 0; } -int gpiod_line_set_value(struct gpiod_line *line, int value) +GPIOD_API int gpiod_line_set_value(struct gpiod_line *line, int value) { struct gpiod_line_bulk bulk = BULK_SINGLE_LINE_INIT(line); return gpiod_line_set_value_bulk(&bulk, &value); } -int gpiod_line_set_value_bulk(struct gpiod_line_bulk *bulk, const int *values) +GPIOD_API int gpiod_line_set_value_bulk(struct gpiod_line_bulk *bulk, + const int *values) { struct gpio_v2_line_values lv; struct gpiod_line *line; @@ -988,17 +993,17 @@ int gpiod_line_set_value_bulk(struct gpiod_line_bulk *bulk, const int *values) return 0; } -int gpiod_line_set_config(struct gpiod_line *line, int direction, - int flags, int value) +GPIOD_API int gpiod_line_set_config(struct gpiod_line *line, int direction, + int flags, int value) { struct gpiod_line_bulk bulk = BULK_SINGLE_LINE_INIT(line); return gpiod_line_set_config_bulk(&bulk, direction, flags, &value); } -int gpiod_line_set_config_bulk(struct gpiod_line_bulk *bulk, - int direction, int flags, - const int *values) +GPIOD_API int gpiod_line_set_config_bulk(struct gpiod_line_bulk *bulk, + int direction, int flags, + const int *values) { struct gpio_v2_line_config hcfg; struct gpiod_line *line; @@ -1045,14 +1050,14 @@ int gpiod_line_set_config_bulk(struct gpiod_line_bulk *bulk, return 0; } -int gpiod_line_set_flags(struct gpiod_line *line, int flags) +GPIOD_API int gpiod_line_set_flags(struct gpiod_line *line, int flags) { struct gpiod_line_bulk bulk = BULK_SINGLE_LINE_INIT(line); return gpiod_line_set_flags_bulk(&bulk, flags); } -int gpiod_line_set_flags_bulk(struct gpiod_line_bulk *bulk, int flags) +GPIOD_API int gpiod_line_set_flags_bulk(struct gpiod_line_bulk *bulk, int flags) { struct gpiod_line *line; int values[LINE_REQUEST_MAX_LINES]; @@ -1073,13 +1078,13 @@ int gpiod_line_set_flags_bulk(struct gpiod_line_bulk *bulk, int flags) flags, values); } -int gpiod_line_set_direction_input(struct gpiod_line *line) +GPIOD_API int gpiod_line_set_direction_input(struct gpiod_line *line) { return gpiod_line_set_config(line, GPIOD_LINE_REQUEST_DIRECTION_INPUT, line->req_flags, 0); } -int gpiod_line_set_direction_input_bulk(struct gpiod_line_bulk *bulk) +GPIOD_API int gpiod_line_set_direction_input_bulk(struct gpiod_line_bulk *bulk) { struct gpiod_line *line; @@ -1089,14 +1094,15 @@ int gpiod_line_set_direction_input_bulk(struct gpiod_line_bulk *bulk) line->req_flags, NULL); } -int gpiod_line_set_direction_output(struct gpiod_line *line, int value) +GPIOD_API int gpiod_line_set_direction_output(struct gpiod_line *line, + int value) { return gpiod_line_set_config(line, GPIOD_LINE_REQUEST_DIRECTION_OUTPUT, line->req_flags, value); } -int gpiod_line_set_direction_output_bulk(struct gpiod_line_bulk *bulk, - const int *values) +GPIOD_API int gpiod_line_set_direction_output_bulk(struct gpiod_line_bulk *bulk, + const int *values) { struct gpiod_line *line; @@ -1106,17 +1112,17 @@ int gpiod_line_set_direction_output_bulk(struct gpiod_line_bulk *bulk, line->req_flags, values); } -int gpiod_line_event_wait(struct gpiod_line *line, - const struct timespec *timeout) +GPIOD_API int gpiod_line_event_wait(struct gpiod_line *line, + const struct timespec *timeout) { struct gpiod_line_bulk bulk = BULK_SINGLE_LINE_INIT(line); return gpiod_line_event_wait_bulk(&bulk, timeout, NULL); } -int gpiod_line_event_wait_bulk(struct gpiod_line_bulk *bulk, - const struct timespec *timeout, - struct gpiod_line_bulk *event_bulk) +GPIOD_API int gpiod_line_event_wait_bulk(struct gpiod_line_bulk *bulk, + const struct timespec *timeout, + struct gpiod_line_bulk *event_bulk) { struct pollfd fds[LINE_REQUEST_MAX_LINES]; unsigned int off, num_lines; @@ -1162,8 +1168,8 @@ int gpiod_line_event_wait_bulk(struct gpiod_line_bulk *bulk, return 1; } -int gpiod_line_event_read(struct gpiod_line *line, - struct gpiod_line_event *event) +GPIOD_API int gpiod_line_event_read(struct gpiod_line *line, + struct gpiod_line_event *event) { int ret; @@ -1174,9 +1180,9 @@ int gpiod_line_event_read(struct gpiod_line *line, return 0; } -int gpiod_line_event_read_multiple(struct gpiod_line *line, - struct gpiod_line_event *events, - unsigned int num_events) +GPIOD_API int gpiod_line_event_read_multiple(struct gpiod_line *line, + struct gpiod_line_event *events, + unsigned int num_events) { int fd; @@ -1187,7 +1193,7 @@ int gpiod_line_event_read_multiple(struct gpiod_line *line, return gpiod_line_event_read_fd_multiple(fd, events, num_events); } -int gpiod_line_event_get_fd(struct gpiod_line *line) +GPIOD_API int gpiod_line_event_get_fd(struct gpiod_line *line) { if (line->state != LINE_REQUESTED_EVENTS) { errno = EPERM; @@ -1197,7 +1203,7 @@ int gpiod_line_event_get_fd(struct gpiod_line *line) return line_get_fd(line); } -int gpiod_line_event_read_fd(int fd, struct gpiod_line_event *event) +GPIOD_API int gpiod_line_event_read_fd(int fd, struct gpiod_line_event *event) { int ret; @@ -1208,8 +1214,9 @@ int gpiod_line_event_read_fd(int fd, struct gpiod_line_event *event) return 0; } -int gpiod_line_event_read_fd_multiple(int fd, struct gpiod_line_event *events, - unsigned int num_events) +GPIOD_API int gpiod_line_event_read_fd_multiple(int fd, + struct gpiod_line_event *events, + unsigned int num_events) { /* * 16 is the maximum number of events the kernel can store in the FIFO diff --git a/lib/helpers.c b/lib/helpers.c index 76d8fc2..9c4b28e 100644 --- a/lib/helpers.c +++ b/lib/helpers.c @@ -11,7 +11,9 @@ #include #include -struct gpiod_line_bulk * +#include "internal.h" + +GPIOD_API struct gpiod_line_bulk * gpiod_chip_get_lines(struct gpiod_chip *chip, unsigned int *offsets, unsigned int num_offsets) { @@ -36,7 +38,8 @@ gpiod_chip_get_lines(struct gpiod_chip *chip, return bulk; } -struct gpiod_line_bulk *gpiod_chip_get_all_lines(struct gpiod_chip *chip) +GPIOD_API struct gpiod_line_bulk * +gpiod_chip_get_all_lines(struct gpiod_chip *chip) { struct gpiod_line_bulk *bulk; struct gpiod_line *line; @@ -59,7 +62,7 @@ struct gpiod_line_bulk *gpiod_chip_get_all_lines(struct gpiod_chip *chip) return bulk; } -int gpiod_chip_find_line(struct gpiod_chip *chip, const char *name) +GPIOD_API int gpiod_chip_find_line(struct gpiod_chip *chip, const char *name) { unsigned int offset, num_lines; struct gpiod_line *line; @@ -81,7 +84,8 @@ int gpiod_chip_find_line(struct gpiod_chip *chip, const char *name) return -1; } -int gpiod_line_request_input(struct gpiod_line *line, const char *consumer) +GPIOD_API int gpiod_line_request_input(struct gpiod_line *line, + const char *consumer) { struct gpiod_line_request_config config = { .consumer = consumer, @@ -91,8 +95,8 @@ int gpiod_line_request_input(struct gpiod_line *line, const char *consumer) return gpiod_line_request(line, &config, 0); } -int gpiod_line_request_output(struct gpiod_line *line, - const char *consumer, int default_val) +GPIOD_API int gpiod_line_request_output(struct gpiod_line *line, + const char *consumer, int default_val) { struct gpiod_line_request_config config = { .consumer = consumer, @@ -102,8 +106,8 @@ int gpiod_line_request_output(struct gpiod_line *line, return gpiod_line_request(line, &config, default_val); } -int gpiod_line_request_input_flags(struct gpiod_line *line, - const char *consumer, int flags) +GPIOD_API int gpiod_line_request_input_flags(struct gpiod_line *line, + const char *consumer, int flags) { struct gpiod_line_request_config config = { .consumer = consumer, @@ -114,9 +118,9 @@ int gpiod_line_request_input_flags(struct gpiod_line *line, return gpiod_line_request(line, &config, 0); } -int gpiod_line_request_output_flags(struct gpiod_line *line, - const char *consumer, int flags, - int default_val) +GPIOD_API int gpiod_line_request_output_flags(struct gpiod_line *line, + const char *consumer, int flags, + int default_val) { struct gpiod_line_request_config config = { .consumer = consumer, @@ -139,52 +143,55 @@ static int line_event_request_type(struct gpiod_line *line, return gpiod_line_request(line, &config, 0); } -int gpiod_line_request_rising_edge_events(struct gpiod_line *line, - const char *consumer) +GPIOD_API int gpiod_line_request_rising_edge_events(struct gpiod_line *line, + const char *consumer) { return line_event_request_type(line, consumer, 0, GPIOD_LINE_REQUEST_EVENT_RISING_EDGE); } -int gpiod_line_request_falling_edge_events(struct gpiod_line *line, - const char *consumer) +GPIOD_API int gpiod_line_request_falling_edge_events(struct gpiod_line *line, + const char *consumer) { return line_event_request_type(line, consumer, 0, GPIOD_LINE_REQUEST_EVENT_FALLING_EDGE); } -int gpiod_line_request_both_edges_events(struct gpiod_line *line, - const char *consumer) +GPIOD_API int gpiod_line_request_both_edges_events(struct gpiod_line *line, + const char *consumer) { return line_event_request_type(line, consumer, 0, GPIOD_LINE_REQUEST_EVENT_BOTH_EDGES); } -int gpiod_line_request_rising_edge_events_flags(struct gpiod_line *line, - const char *consumer, - int flags) +GPIOD_API int +gpiod_line_request_rising_edge_events_flags(struct gpiod_line *line, + const char *consumer, + int flags) { return line_event_request_type(line, consumer, flags, GPIOD_LINE_REQUEST_EVENT_RISING_EDGE); } -int gpiod_line_request_falling_edge_events_flags(struct gpiod_line *line, - const char *consumer, - int flags) +GPIOD_API int +gpiod_line_request_falling_edge_events_flags(struct gpiod_line *line, + const char *consumer, + int flags) { return line_event_request_type(line, consumer, flags, GPIOD_LINE_REQUEST_EVENT_FALLING_EDGE); } -int gpiod_line_request_both_edges_events_flags(struct gpiod_line *line, - const char *consumer, int flags) +GPIOD_API int +gpiod_line_request_both_edges_events_flags(struct gpiod_line *line, + const char *consumer, int flags) { return line_event_request_type(line, consumer, flags, GPIOD_LINE_REQUEST_EVENT_BOTH_EDGES); } -int gpiod_line_request_bulk_input(struct gpiod_line_bulk *bulk, - const char *consumer) +GPIOD_API int gpiod_line_request_bulk_input(struct gpiod_line_bulk *bulk, + const char *consumer) { struct gpiod_line_request_config config = { .consumer = consumer, @@ -194,9 +201,9 @@ int gpiod_line_request_bulk_input(struct gpiod_line_bulk *bulk, return gpiod_line_request_bulk(bulk, &config, 0); } -int gpiod_line_request_bulk_output(struct gpiod_line_bulk *bulk, - const char *consumer, - const int *default_vals) +GPIOD_API int gpiod_line_request_bulk_output(struct gpiod_line_bulk *bulk, + const char *consumer, + const int *default_vals) { struct gpiod_line_request_config config = { .consumer = consumer, @@ -219,29 +226,33 @@ static int line_event_request_type_bulk(struct gpiod_line_bulk *bulk, return gpiod_line_request_bulk(bulk, &config, 0); } -int gpiod_line_request_bulk_rising_edge_events(struct gpiod_line_bulk *bulk, - const char *consumer) +GPIOD_API int +gpiod_line_request_bulk_rising_edge_events(struct gpiod_line_bulk *bulk, + const char *consumer) { return line_event_request_type_bulk(bulk, consumer, 0, GPIOD_LINE_REQUEST_EVENT_RISING_EDGE); } -int gpiod_line_request_bulk_falling_edge_events(struct gpiod_line_bulk *bulk, - const char *consumer) +GPIOD_API int +gpiod_line_request_bulk_falling_edge_events(struct gpiod_line_bulk *bulk, + const char *consumer) { return line_event_request_type_bulk(bulk, consumer, 0, GPIOD_LINE_REQUEST_EVENT_FALLING_EDGE); } -int gpiod_line_request_bulk_both_edges_events(struct gpiod_line_bulk *bulk, - const char *consumer) +GPIOD_API int +gpiod_line_request_bulk_both_edges_events(struct gpiod_line_bulk *bulk, + const char *consumer) { return line_event_request_type_bulk(bulk, consumer, 0, GPIOD_LINE_REQUEST_EVENT_BOTH_EDGES); } -int gpiod_line_request_bulk_input_flags(struct gpiod_line_bulk *bulk, - const char *consumer, int flags) +GPIOD_API int gpiod_line_request_bulk_input_flags(struct gpiod_line_bulk *bulk, + const char *consumer, + int flags) { struct gpiod_line_request_config config = { .consumer = consumer, @@ -252,9 +263,10 @@ int gpiod_line_request_bulk_input_flags(struct gpiod_line_bulk *bulk, return gpiod_line_request_bulk(bulk, &config, 0); } -int gpiod_line_request_bulk_output_flags(struct gpiod_line_bulk *bulk, - const char *consumer, int flags, - const int *default_vals) +GPIOD_API int gpiod_line_request_bulk_output_flags(struct gpiod_line_bulk *bulk, + const char *consumer, + int flags, + const int *default_vals) { struct gpiod_line_request_config config = { .consumer = consumer, @@ -265,7 +277,7 @@ int gpiod_line_request_bulk_output_flags(struct gpiod_line_bulk *bulk, return gpiod_line_request_bulk(bulk, &config, default_vals); } -int gpiod_line_request_bulk_rising_edge_events_flags( +GPIOD_API int gpiod_line_request_bulk_rising_edge_events_flags( struct gpiod_line_bulk *bulk, const char *consumer, int flags) { @@ -273,7 +285,7 @@ int gpiod_line_request_bulk_rising_edge_events_flags( GPIOD_LINE_REQUEST_EVENT_RISING_EDGE); } -int gpiod_line_request_bulk_falling_edge_events_flags( +GPIOD_API int gpiod_line_request_bulk_falling_edge_events_flags( struct gpiod_line_bulk *bulk, const char *consumer, int flags) { @@ -281,7 +293,7 @@ int gpiod_line_request_bulk_falling_edge_events_flags( GPIOD_LINE_REQUEST_EVENT_FALLING_EDGE); } -int gpiod_line_request_bulk_both_edges_events_flags( +GPIOD_API int gpiod_line_request_bulk_both_edges_events_flags( struct gpiod_line_bulk *bulk, const char *consumer, int flags) { diff --git a/lib/internal.h b/lib/internal.h new file mode 100644 index 0000000..8b3f69a --- /dev/null +++ b/lib/internal.h @@ -0,0 +1,11 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* SPDX-FileCopyrightText: 2021 Bartosz Golaszewski */ + +#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__ */ diff --git a/lib/misc.c b/lib/misc.c index 5583eb0..984405b 100644 --- a/lib/misc.c +++ b/lib/misc.c @@ -5,7 +5,9 @@ #include -const char *gpiod_version_string(void) +#include "internal.h" + +GPIOD_API const char *gpiod_version_string(void) { return GPIOD_VERSION_STR; }