If "default_vals" is not provided in the kwds then default_vals are
passed uninitialized to gpiod_line_request_bulk(), so rename the
existing default_vals to vals and introduce a new default_vals that
points to vals, or NULL if no defaults have been passed.
Fixes: 96c524c495 (bindings: implement python bindings)
Reported-by: Pedro Botella <pbotella@gmail.com>
Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Open-source and open-drain drive settings are mutually exclusive just like
the bias settings. Make them into an enum so that becomes clear.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
When we're reading the current bias setting of a line, it is already
disabled so the flag should be named as such. While BIAS_DISABLE would
technically be fine when modifying the setting, let's rename it to
BIAS_DISABLED too for consistency.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
When inspecting the current bias setting of a GPIO line, the AS_IS name
of one of the possible values really means that the kernel GPIO subsystem
can't determine the bias setting because it didn't set it itself (e.g.
the hardware may have internally initialized pull-up or pull-down
resistors). In this case it's better to change the name to BIAS_UNKNOWN
to reflect that.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Unlike line direction - where input and output modes are equal, no signal
inversion (active-high) is the natural state of the line while active-low
is less likely. Let's drop the ACTIVE_STATE enum treewide and provide a
boolean property for lines - is_active_low() - to reflect that fact. This
function returning false means the line is "active-high".
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
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>
Chip iterators require the user to have permission to access all GPIO
chips. They also don't take into account symbolic links. In general
they're badly designed so remove them treewide in favor of scanning /dev
manually using the provided gpiod_is_gpiochip_device() helper.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Example code for bindings is currently always built if bindings are
enabled. Make it conditional with a new configure switch.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
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>
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>
Global line lookup doesn't really work correctly because GPIO line names
are not unique. We'd have to return a list of matching lines. Also: not
all chips may be accessible by user in which case the chip iterator will
fail. We'll soon be removing chip iterators entirely so for now drop the
global line lookup and let users iterate over chips themselves.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
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>
Hand-crafted iterators don't make much sense in C and impose an
additional layer of memory allocation and resource releasing. Remove
the line iterators from the core C library.
We're leaving the iterators where they make sense: in C++ and Python
bindings but we convert them to using other means of keeping track of
lines.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
The limit of 64 lines max per bulk object is wrong. We may want to
retrieve all lines from a chip exporting more than 64. We'll be reducing
the role of bulk objects soon so drop this limit now.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
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>
The library now uses v2 of the GPIO uAPI. The new user interface is
scheduled for release as part of linux v5.10. Linux v5.10-rc1 is now
tagged so make v5.10 headers a requirement for the build and update
all tests to check for the correct version.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
We support the standard __version__ module attribute. There's no need
to duplicate this functionality in the form of the version_string()
function.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
The freq field of struct gpiod_test_event_thread is actually used as a
period measured in milliseconds, so rename it to period_ms in the struct
and wherever it is used as a function parameter.
Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
On an attempt to call the 'request' method of a Line object, the program
crashes with this exception:
> SystemError: ../Objects/dictobject.c:2606: bad argument to internal function
>
> The above exception was the direct cause of the following exception:
>
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> SystemError: <class 'gpiod.LineBulk'> returned a result with an error set
The problem is that keyword args are NULL (rather than an empty dict) if
they are not present. However, PyDict_Size sets an exception if it gets
NULL.
Fixes: 02a3d0a2ab ("bindings: python: fix segfault when calling Line.request()")
Signed-off-by: Jiri Benc <jbenc@upir.cz>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Calling Line.request() with no keyword arguments leads to an exception
being raised by PyDict_Size(). Add a test-case exposing this bug. It
will be fixed in the subsequent commit.
Reported-by: Jiri Benc <jbenc@upir.cz>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
tp_basicsize is the size of the allocated object, not of the class.
Fixes: 96c524c495 ("bindings: implement python bindings")
Signed-off-by: Jiri Benc <jbenc@upir.cz>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
The python module provides the version_string() function that returns
the API version but the standard way to do this according to PEP 396
is by providing the __version__ attribute at the module level.
Add __version__ constant to gpiod module and mark version_string() as
deprecated in the doc.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
We're already using it implicitly but GCC may change the default
standard to gnu99 at some point, so use gnu89 explicitly whenever
building C files.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
The test-suite python scripts is added to bin_SCRIPTS in Makefile.am.
This means it's not distributed by default. Add it to dist_bin_SCRIPTS
and make it part of the distribution tarball.
Fixes: 452696601a ("bindings: python: use unittest to implement a proper test-suite")
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Extend the test coverage of Python bindings with tests of reading of
multiple line events at once.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
When Line.request() is called without the required 'consumer=value'
argument, the module attempts access an empty dictionary object
resulting in a segfault. This patch avoids such access when the
dictionary is empty and maintains the current design where the
LineBulk object is responsible for validation of arguments.
Signed-off-by: Joel Savitz <jsavitz@redhat.com>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Change the vals in gpiod_Line_set_value from a tuple of tuples to just a
tuple. This brings it into line with other vals in the module such as
gpiod_Line_set_config and gpiod_Line_set_direction_output.
The previous usage made the vals equivalent to an args (a tuple of
arguments equivalent to argc,argv in C), as per gpiod_Line_set_flags.
Renaming vals to args in gpiod_Line_set_value was not an option as there
is already an args parameter.
Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Extend test coverage over set_config, set_flags, set_direction_input, and
set_direction_output methods.
Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Restructured gpiod_LineBulk_set_values to move the conversion of values
from Python tuple to int array into a helper function as it is useful for
similar functions.
Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Extend test coverage to cover the bias flags in requests and the bias
setting returned by line.bias().
Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Add support for bias flags in line requests and returning the line bias
setting via a bias accessor.
Based on initial work by Drew Fustini <drew@pdp7.com>.
Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Trivial grammar fix. "correspond with" can mean either being in agreement
with, happening at the same time, or communication between parties.
"correspond to" is used to mean equivalance, which is the intended use
throughout the documentation.
Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
PyErr_SetFromErrno() always returns NULL. We can return from functions
on failure directly instead of calling it and then returning NULL in
the next line.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Python bindings are missing the functionality provided by the core C
library's gpiod_line_update(). Extend the Line class with the update()
method that allows to re-read the line information from the kernel.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Don't put a tab between the asignment and the first string - just move
the whole list one line below.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
The examples are programs meant to be executed directly and not
imported. Use the standard "if __name__ == '__main__':" before
any code.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Kernel release candidate version strings seem to not be working
correctly with python's version.parse() function. Split the string
retrieved with os.uname().release using '-' as delimiter and pass
only the first part - 'major.minor.release' - to version.parse().
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Mainline commit 2c60e6b5c924 ("gpiolib: never report open-drain/source
lines as 'input' to user-space") in the linux kernel has now been
backported to v5.2 stable branch and released in linux v5.2.11. We can
now enable back the assertions that were previously failed in test
suites (core, C++ and python). The required kernel versions must be
updated as well.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
When a call is expected to raise the OSError exception, verify that its
errno field is set to the expected value.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
After setting the line values in gpioset.py: don't exit until the user
presses ENTER. This makes the example work the same as the 'wait' mode
in the original gpioset utility.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
In gpioset.py: when calling lines.set_values(), there's no point in
storing the return value as it's always None. It's also not used
anywhere.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Modify the gpiomon.py example to describe the detected events in
a format similar to the one used by the original gpiomon tool.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Currently the only tests we have for python bindings are in a simple
script that makes a lot of assumptions about the environment and
doesn't even work anymore with recent kernels.
Remove it and replace it with a proper test-suite implemented using
libgpiod and the standard python unittest package.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
The argument in Line.request() is called default_val, not default_vals.
For backward compatibility with older versions default_vals would still
work if it was passed a tuple or list, but let's not confuse users:
default_val is the preferred way for single lines.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
GCC8 has added a new warning heuristic to detect invalid function
casts. This causes a lot of warnings when building libgpiod python
bindings.
Let's use Py_UNUSED where applicable to make function prototypes match
that of PyCFunction. For keyword functions, let's cast them to a
function pointer taking no arguments before casting them again to
PyCFunction. This may not be the best solution but it has to do for
the moment.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
The src directory containing lib and tools is not necessary and will
actually cause problems if we tried to add a new component to src/
which would depend on libraries from the bindings directory because it
(bindings) already depends on src/lib.
Simplify the directory structure. Move lib and tools to the root source
directory and update all relevant files. That way we can specify the
exact build order of components from the top source Makefile.am.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Until now Line.request() only took 'default_vals' argument which was
passed down directly to LineBulk.request() internally. This meant that
even when requesting a single line in output mode, the default value
had to be passed in a sequence.
Add support for the 'default_val' argument which makes it possible to
specify the default value as an integer. The previous 'default_vals'
argument is still supported, but the two cannot be passed at the same
time.
Documentation for default_vals is removed, as it's now deprecated for
Line.request().
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
When breaking a line, the arguments should be aligned with the opening
bracket of the function call.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>