1
0
mirror of https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git synced 2026-10-24 00:48:53 +03:00
Commit Graph

37 Commits

Author SHA1 Message Date
Bartosz Golaszewski
e60e38375c tests: split out reusable test code into a local static library
In order to allow the upcoming GLib and DBus bindings to reuse the test
code, let's put all common elements into reusable libtool objects and
export the relevant symbols in internal headers.

Tested-by: Alexander Sverdlin <alexander.sverdlin@siemens.com>
Link: https://lore.kernel.org/r/20240812-dbus-v5-1-ead288509217@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-08-13 10:38:40 +02:00
Bartosz Golaszewski
19c6942f0c tests: add a test-case for looking up non-standard GPIO line names
Make sure line names with whitespaces or some less common characters (that
can be found in real-life names in mainline .dts files) can be correctly
mapped to offsets.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-03-03 15:04:32 +01:00
Bartosz Golaszewski
96b686aacb tests: use GLib typedef conventions for line names and hogs
The GVariant packaging works in the context of GLib unit testing so use
GLib conventions for declaring data types.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-02-15 13:21:56 +01:00
Bartosz Golaszewski
4c4dced3c7 core: make the library NULL-aware
Currently we almost never check the validity of pointers passed to the
core C API routines. While we cannot know if a non-NULL pointer points
to an actual object, we can at least verify that it is not NULL before
dereferencing it.

Conceptually we can think of two categories of pointers in libgpiod:

First, there are the objects we manipulate using the API. These are
normally the first arguments in any given method and they are only
instantiated inside the library and never dereferenced by the user.

They should always be valid, so passing a NULL pointer here should
always lead to a crash. Currently it will take the form of a segfault
at the moment of dereference but with this change, we make the user
program abort() with the stack trace pointing right at the offender.

The second category would be pointers to objects that are logically
parameters of methods i.e. not their first argument. Example is:
gpiod_chip_request_lines(chip, req_cfg, line_cfg) (whose logical OOP
version would be: chip->request_lines(req_cfg, line_cfg)). Here we
accept a NULL req_cfg but we'll segfault on a NULL line_cfg. For
consistency: don't crash here but instead return -1 and set errno
to EINVAL. Same everywhere else.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-01-06 12:46:02 +01:00
Bartosz Golaszewski
55cc5af0f4 tests: unref GVariant objects
We need to drop the reference count of the GVariants we create when
packing hogs and line names. This fixes all memory leaks in tests that
are caused by this bug.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2022-12-05 14:47:29 +01:00
Bartosz Golaszewski
b7ba732e6a treewide: libgpiod v2 implementation
This commit is the result of a squash of all the commits created during
the development of libgpiod v2. This is done to preserve bisectability
of the git tree. It contains significant re-writes of the core C library
as well as C++ and python bindings. The tools have been adjusted to work
with the new API but have not undergone significant changes yet.

This commit contains the bulk of the work on libgpiod v2 but the
development continues with more changes planned.

Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
[Ben: fix format specifier for uint64_t]
Signed-off-by: Ben Hutchings <ben.hutchings@mind.be>
[Viresh: Fix ioctl number for gpiod_line_request_reconfigure_lines()]
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
[Kent: bug fixes, improvements, better pointer contracts, type strictness]
Signed-off-by: Kent Gibson <warthog618@gmail.com>
2022-10-31 11:15:14 +01:00
Bartosz Golaszewski
55a8c62e71 treewide: rename chip property accessors
In v2 API all getters will be called using the following pattern:

    gpiod_<object>_get_<what>

Apply this to already existing getters for gpiod_chip.

Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
2021-04-17 13:36:02 +02:00
Bartosz Golaszewski
0dcbafee3d treewide: simplify line lookup
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 <bgolaszewski@baylibre.com>
2021-03-18 09:34:15 +01:00
Bartosz Golaszewski
154e7c5a82 licensing: relicense non-library code under GPL-2.0-or-later
LGPL-2.1 is a license meant for shared libraries. Because I didn't know
any better I used it for all files in the repository, including programs
linking against libgpiod. The standard approach for many similar projects
is to use LGPL for library code and GPL for programs (and also Makefiles
and other files containing code or configuration).

Relicense all programs, tests, makefiles and autotools files under
GPL-2.0 or later.

Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Acked-by: Kent Gibson <warthog618@gmail.com>
2021-02-18 17:42:12 +01:00
Bartosz Golaszewski
dfb707c255 licensing: make the project REUSE-compliant
In order to make the licensing situation of libgpiod clear and enable
easy reusing of the code, let's make all the files in the repository
compliant with the REUSE v3.0 specification.

In order to achieve that:
  - put all used licenses into a separate LICENSES/ directory
  - add SPDX license identifiers to all files that were missing them with
    regular text files being licensed under CC-BY-SA-4.0, while files
    containing code and configuration are licensed under GPL-2.0-or-later
  - replace custom copyright texts with SPDX-FileCopyrightText
  - update the gpio.h kernel uAPI header with the upstream version where
    the SDPX license identifier was fixed to use 'GPL-2.0-only' variant
    instead of the deprecated 'GPL-2.0'

Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Acked-by: Kent Gibson <warthog618@gmail.com>
2021-02-18 17:42:08 +01:00
Bartosz Golaszewski
eb690e46ef treewide: remove helpers for opening chips by name & number
Helper wrappers around gpiod_chip_open() shouldn't really be part of
the core, low-level library. They assume that devtmpfs is mounted at
/dev and that GPIO chips follow the gpiochipX naming convention.

This changeset removes all variants of gpiod_chip_open() other than
the one taking the path as argument. We're doing this treewide so
C++ and Python bindings lose the 'how' argument that currently allows
to change the way the chips are opened by the chip's constructors or
the open() method in C++.

The gpio-tools programs still support opening chips by number, name
and path but they implement this functionality locally rather than
using the library code.

Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
2020-12-17 18:04:17 +01:00
Bartosz Golaszewski
e6f20c261d core: rework gpiod_chip_find_line()
GPIO line names are not unique and the library has been incorrectly
making such assumption in v1.x series. We've already dropped interfaces
that would be too complicated to maintain when taking this into account.

Let's now rework the remaming API. This introduces a new function to the
C API: gpiod_chip_find_line_unique() which assumes that the line name is
unique and signals an error if it's not. The previous
gpiod_chip_find_line() function now returns a bulk object containing all
matching lines.

Python and C++ bindings are updated: their find_line() functions return
bulk objects (or an ::std::vector> too now and they take an additional
argument specifying whether we're looking for a unique line or not.

Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
2020-12-14 15:57:07 +01:00
Bartosz Golaszewski
8496158bc0 treewide: kill find_lines()
GPIO line names are not unique. Looking up multiple lines by names would
require us to return a list of matching lines for every name. We're
simplifying the library API so drop this interface treewide.

Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
2020-12-14 15:57:05 +01:00
Bartosz Golaszewski
44ed2c098e treewide: kill opening chips by label
Chip labels are not unique - opening chips by label may lead to errors
or to ignoring chips with duplicate labels. Users can easily implement
chip lookup by label themselves so remove this part from the core library
and all bindings.

Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
2020-12-14 15:56:46 +01:00
Bartosz Golaszewski
617005be20 core: export gpiod_is_gpiochip_device()
We'll be dropping chip iterators treewide. Instead we'll encourage
scanning /dev for GPIO chip devices. Exporting this function allows
users to easily check if given file represents a GPIO chip character
device.

Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
2020-12-14 15:56:37 +01:00
Bartosz Golaszewski
2b02d7ae1a treewide: rework struct gpiod_line_bulk
Current implementation of struct gpiod_line_bulk uses stack memory
excessively. The structure is big: it's an array 64 pointers + 4 bytes
size. That amounts to 260 bytes on 32-bit and 516 bytes on 64-bit
architectures respectively. It's also used everywhere as all functions
dealing with single lines eventually end up calling bulk counterparts.

The rework addresses it by making the bulk structure opaque and
providing appropriate interfaces for library users while retaining a way
for internal users to allocate single line bulks on the stack.

The macro-based loop has been removed. In its place we provide a function
iterating over all lines held by a bulk and calling the provided callback
function for each line.

Since bulk operations can now fail, a bunch of test-cases has been added
to cover the relevant code.

While at it: using the word offset both when referring to line's HW
offset in a chip as well as the offset in a bulk leads to confusion.
This patch renames the bulk offset to index.

Some additional improvements to the patch by:
  Kent Gibson <warthog618@gmail.com>

Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
2020-11-04 16:39:09 +01:00
Bartosz Golaszewski
9c5a6f31eb tests: use GLib for library test cases and bats for gpio-tools
This replaces the custom hand-crafted testing suite with well-known
open-source tools while keeping the same coverage. The core library
is now tested using the GLib unit testing framework. The gpio-tools
are now tested in bash using the bats testing system instead of being
invoked from a C program.

This simplifies and shrinks the testing code and makes it less prone
to bugs.

Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
2019-08-01 17:31:31 +02:00
Bartosz Golaszewski
937ded4dbc treewide: order includes alphabetically
Unify the header includes by ordering them alphabetically and always
having local headers in a second group below the global ones (separated
by a newline).

Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
2019-02-21 09:38:57 +01:00
Bartosz Golaszewski
5da2e8d790 helpers: provide gpiod_chip_find_lines()
Implement a new helper that allows to look-up multiple GPIO lines by
their names. Include a set of test cases.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-05-14 12:00:24 +02:00
Bartosz Golaszewski
0a9faf3abf license: use C++-style comments for SPDX headers in source files
Follow the linux kernel licensing style and only use C-style comments
in C headers.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-04-04 13:19:05 +02:00
Bartosz Golaszewski
b59666510f tests: use SPDX license identifier
Use the SPDX license identifier for LGPLv2.1 or later in source files
of the testing framework.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-03-04 10:38:50 +01:00
Bartosz Golaszewski
5c5a4ca9b3 tests: add a new cleanup macro
The most commonly used cleanup attribute is the one used for automatic
closing of GPIO chips. For brevity: hide the long TEST_CLEANUP(...)
invocation behind a shorter TEST_CLEANUP_CHIP macro.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-02-22 21:21:56 +01:00
Bartosz Golaszewski
6b6bed27cc API: provide new line accessors
Add two new public functions that allow to retrieve multiple lines from
GPIO chips at once. Include test cases.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-02-22 21:15:43 +01:00
Bartosz Golaszewski
0704bcdbbf all: update the copyright notice
All these files have been updated in 2018.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-01-13 23:16:31 +01:00
Bartosz Golaszewski
7df216bf4d all: modify the license preamble in files
This project contains both a library and executables so it's better to
use an umbrella term "This program ..." than "This library ..." in all
the source files.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2018-01-10 01:50:36 +01:00
SZ Lin
7d61309458 all: make license type consistent between source files and COPYING file
The license type of COPYING file is "LGPL-2.1+", but it is declared
as "LGPL-2.1" in the preamble of source files.

This patch fixes license type inconsistency issue in each source file.

Signed-off-by: SZ Lin <sz.lin@moxa.com>
[Bartosz: tweaked the commit message]
Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2017-12-01 18:43:54 +01:00
Bartosz Golaszewski
19a9f62a65 tests: use test_build_str() in test cases
Shrink the test code by using the newly provided helper function for
building custom strings wherever asprintf() was used before.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2017-11-05 14:31:46 +01:00
Bartosz Golaszewski
bce3718def helpers: set errno in gpiod_chip_open_by_label()
If a GPIO chip with given label cannot be found but no other error
occurred, set errno to ENOENT before returning a NULL pointer.

Update the relevant test case.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2017-10-24 14:25:02 +02:00
Bartosz Golaszewski
45044fb890 tests: chip: add test cases for gpiod_chip_find_line()
Test two use cases: correct lookup and a search for a non-existent
line.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2017-09-29 17:25:07 +02:00
Bartosz Golaszewski
920d8e8947 tests: use TEST_ASSERT_ERRNO_IS()
Use the new assert macro wherever we're checking the errno value.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2017-09-29 17:22:16 +02:00
Bartosz Golaszewski
091d36e360 libgpiod: unify the header comments
Make the header comment look the same in every file. Move additional
descriptions of files' contents below the header.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2017-07-12 17:22:09 +02:00
Bartosz Golaszewski
cc0b3fac72 core: kill custom error handling
The libgpiod-specific error codes are not really necessary and can be
easily replaced with regular libc error numbers defined in errno.h.

Remove all routines and definitions dealing with error numbers and
switch to using errnos in the library code.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2017-07-06 11:11:36 +02:00
Bartosz Golaszewski
859ffb7a98 tests: chip: fix potential segfault
The automatically cleaned up string is not initialized, but if
asprintf() failed, its contents would be undefined which could lead
to a segmentation fault.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2017-06-14 07:48:00 +02:00
Bartosz Golaszewski
96a17a0501 tests: convert named_lines into a general 'flags' field
We currently have the boolean 'named_lines' field in the test
description structure. Convert it to an integer in order to allow
passing multiple test switches in the future.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2017-05-14 16:55:27 +02:00
Bartosz Golaszewski
86a94bf1e1 tests: change the test API prefix
The gu_ prefix stood for gpiod unit, but it's no longer valid. These
definitions are not visible outside of the tests subdirectory, so
there's no need for a unique prefix. Just start all symbols with test_
or TEST_.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2017-05-14 13:26:48 +02:00
Bartosz Golaszewski
2f5d950907 tests: rename gpiod-unit to gpiod-test
The testing framework will be reused for all kinds of tests. Also: the
tests we have are not really separated, but use external components so
calling them unit tests is wrong anyway.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2017-05-14 13:18:39 +02:00
Bartosz Golaszewski
715791a3a3 tests: remove the unit subdirectory
Initially we planned to have different subdirectories for different
kinds of tests (unit, tools etc.) but since it was decided that we'll
reuse the already existing framework for all tests (in order to avoid
having to reimplement the same functionality) we can get rid of the
unit subdirectory.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
2017-05-14 13:09:36 +02:00